blob: d04e2748098eded1f0be8918419028b87fb8d399 [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()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 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 Patel17800552010-03-09 00:44:50 +0000100 return DebugFactory.CreateFile(AbsFileName.getBasename(),
101 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 Patel17800552010-03-09 00:44:50 +0000231 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DIFile(),
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 Patel17800552010-03-09 00:44:50 +0000265 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DIFile(),
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
Devang Patel17800552010-03-09 00:44:50 +0000275 llvm::DIFile DefUnit;
Mike Stump9bc093c2009-05-14 02:03:51 +0000276 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
277
278 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
279
280 llvm::DIType FieldTy;
281
282 QualType FType;
283 uint64_t FieldSize, FieldOffset;
284 unsigned FieldAlign;
285
286 llvm::DIArray Elements;
287 llvm::DIType EltTy, DescTy;
288
289 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000290 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000291 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000292 FieldSize = CGM.getContext().getTypeSize(FType);
293 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000294 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
295 "reserved", DefUnit,
296 0, FieldSize, FieldAlign,
297 FieldOffset, 0, FieldTy);
298 EltTys.push_back(FieldTy);
299
300 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000301 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000302 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000303 FieldSize = CGM.getContext().getTypeSize(FType);
304 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000305 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
306 "Size", DefUnit,
307 0, FieldSize, FieldAlign,
308 FieldOffset, 0, FieldTy);
309 EltTys.push_back(FieldTy);
310
311 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000312 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000313 EltTys.clear();
314
Mike Stump3d363c52009-10-02 02:30:50 +0000315 unsigned Flags = llvm::DIType::FlagAppleBlock;
316
Mike Stump9bc093c2009-05-14 02:03:51 +0000317 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000318 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000319 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000320
Mike Stump9bc093c2009-05-14 02:03:51 +0000321 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000322 uint64_t Size = CGM.getContext().getTypeSize(Ty);
323 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Mike Stump9bc093c2009-05-14 02:03:51 +0000325 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel17800552010-03-09 00:44:50 +0000326 Unit, "", llvm::DIFile(),
Mike Stump9bc093c2009-05-14 02:03:51 +0000327 0, Size, Align, 0, 0, EltTy);
328
329 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000330 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000332 FieldSize = CGM.getContext().getTypeSize(FType);
333 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000334 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
335 "__isa", DefUnit,
336 0, FieldSize, FieldAlign,
337 FieldOffset, 0, FieldTy);
338 EltTys.push_back(FieldTy);
339
340 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000341 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000342 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000343 FieldSize = CGM.getContext().getTypeSize(FType);
344 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000345 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
346 "__flags", DefUnit,
347 0, FieldSize, FieldAlign,
348 FieldOffset, 0, FieldTy);
349 EltTys.push_back(FieldTy);
350
351 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000352 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000353 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000354 FieldSize = CGM.getContext().getTypeSize(FType);
355 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000356 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
357 "__reserved", DefUnit,
358 0, FieldSize, FieldAlign,
359 FieldOffset, 0, FieldTy);
360 EltTys.push_back(FieldTy);
361
362 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000363 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000364 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000365 FieldSize = CGM.getContext().getTypeSize(FType);
366 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000367 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
368 "__FuncPtr", DefUnit,
369 0, FieldSize, FieldAlign,
370 FieldOffset, 0, FieldTy);
371 EltTys.push_back(FieldTy);
372
373 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000374 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000375 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000376 FieldSize = CGM.getContext().getTypeSize(Ty);
377 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000378 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
379 "__descriptor", DefUnit,
380 0, FieldSize, FieldAlign,
381 FieldOffset, 0, FieldTy);
382 EltTys.push_back(FieldTy);
383
384 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000385 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000386
387 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000388 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000389 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000390
Mike Stump9bc093c2009-05-14 02:03:51 +0000391 BlockLiteralGenericSet = true;
392 BlockLiteralGeneric
393 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Patel17800552010-03-09 00:44:50 +0000394 "", llvm::DIFile(),
Mike Stump9bc093c2009-05-14 02:03:51 +0000395 0, Size, Align, 0, 0, EltTy);
396 return BlockLiteralGeneric;
397}
398
Chris Lattner9c85ba32008-11-10 06:08:34 +0000399llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000400 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000401 // Typedefs are derived from some other type. If we have a typedef of a
402 // typedef, make sure to emit the whole chain.
403 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Chris Lattner9c85ba32008-11-10 06:08:34 +0000405 // We don't set size information, but do specify where the typedef was
406 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000407 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000408 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000409 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000410
Devang Pateleb6d79b2010-02-01 21:34:11 +0000411 llvm::DIDescriptor TyContext
412 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
413 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000414 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000415 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000416 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000417 Ty->getDecl()->getName(), Unit,
418 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000419 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000420}
421
Chris Lattner9c85ba32008-11-10 06:08:34 +0000422llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000423 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000424 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000425
Chris Lattner9c85ba32008-11-10 06:08:34 +0000426 // Add the result type at least.
427 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000428
Chris Lattner9c85ba32008-11-10 06:08:34 +0000429 // Set up remainder of arguments if there is a prototype.
430 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000431 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000432 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
433 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
434 } else {
435 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000436 }
437
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000439 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Devang Patelca80a5f2009-10-20 19:55:01 +0000441 llvm::DIType DbgTy =
442 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel17800552010-03-09 00:44:50 +0000443 Unit, "", llvm::DIFile(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000444 0, 0, 0, 0, 0,
445 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000446 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000447}
448
Devang Patel428deb52010-01-19 00:00:59 +0000449/// CollectRecordFields - A helper function to collect debug info for
450/// record fields. This is used while creating debug info entry for a Record.
451void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000452CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000453 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
454 unsigned FieldNo = 0;
455 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000456 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
457 for (RecordDecl::field_iterator I = RD->field_begin(),
458 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000459 I != E; ++I, ++FieldNo) {
460 FieldDecl *Field = *I;
461 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
462
463 llvm::StringRef FieldName = Field->getName();
464
Devang Patel4835fdd2010-02-12 01:31:06 +0000465 // Ignore unnamed fields. Do not ignore unnamed records.
466 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000467 continue;
468
469 // Get the location for the field.
470 SourceLocation FieldDefLoc = Field->getLocation();
471 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000472 llvm::DIFile FieldDefUnit;
Devang Patel428deb52010-01-19 00:00:59 +0000473 unsigned FieldLine = 0;
474
475 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000476 FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel428deb52010-01-19 00:00:59 +0000477 FieldLine = PLoc.getLine();
478 }
479
480 QualType FType = Field->getType();
481 uint64_t FieldSize = 0;
482 unsigned FieldAlign = 0;
483 if (!FType->isIncompleteArrayType()) {
484
485 // Bit size, align and offset of the type.
486 FieldSize = CGM.getContext().getTypeSize(FType);
487 Expr *BitWidth = Field->getBitWidth();
488 if (BitWidth)
489 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
490
491 FieldAlign = CGM.getContext().getTypeAlign(FType);
492 }
493
494 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
495
496 // Create a DW_TAG_member node to remember the offset of this field in the
497 // struct. FIXME: This is an absolutely insane way to capture this
498 // information. When we gut debug info, this should be fixed.
499 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
500 FieldName, FieldDefUnit,
501 FieldLine, FieldSize, FieldAlign,
502 FieldOffset, 0, FieldTy);
503 EltTys.push_back(FieldTy);
504 }
505}
506
Devang Patela6da1922010-01-28 00:28:01 +0000507/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
508/// function type is not updated to include implicit "this" pointer. Use this
509/// routine to get a method type which includes "this" pointer.
510llvm::DIType
511CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000512 llvm::DIFile Unit) {
Devang Patela6da1922010-01-28 00:28:01 +0000513 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000514
515 // Static methods do not need "this" pointer argument.
516 if (Method->isStatic())
517 return FnTy;
518
Devang Patela6da1922010-01-28 00:28:01 +0000519 // Add "this" pointer.
520
521 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
522 assert (Args.getNumElements() && "Invalid number of arguments!");
523
524 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
525
526 // First element is always return type. For 'void' functions it is NULL.
527 Elts.push_back(Args.getElement(0));
528
529 // "this" pointer is always first argument.
530 ASTContext &Context = CGM.getContext();
531 QualType ThisPtr =
532 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000533 llvm::DIType ThisPtrType =
534 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
535 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
536 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000537
538 // Copy rest of the arguments.
539 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
540 Elts.push_back(Args.getElement(i));
541
542 llvm::DIArray EltTypeArray =
543 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
544
545 return
546 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel17800552010-03-09 00:44:50 +0000547 Unit, "", llvm::DIFile(),
Devang Patela6da1922010-01-28 00:28:01 +0000548 0, 0, 0, 0, 0,
549 llvm::DIType(), EltTypeArray);
550}
551
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000552/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
553/// a single member function GlobalDecl.
554llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000555CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000556 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000557 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000558 bool IsCtorOrDtor =
559 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
560
561 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000562 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000563 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000564
565 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
566 // make sense to give a single ctor/dtor a linkage name.
567 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000568 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000569
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000570 SourceManager &SM = CGM.getContext().getSourceManager();
571
572 // Get the location for the method.
573 SourceLocation MethodDefLoc = Method->getLocation();
574 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
Devang Patel17800552010-03-09 00:44:50 +0000575 llvm::DIFile MethodDefUnit;
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000576 unsigned MethodLine = 0;
577
578 if (!PLoc.isInvalid()) {
Devang Patel17800552010-03-09 00:44:50 +0000579 MethodDefUnit = getOrCreateFile(MethodDefLoc);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000580 MethodLine = PLoc.getLine();
581 }
582
583 // Collect virtual method info.
584 llvm::DIType ContainingType;
585 unsigned Virtuality = 0;
586 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000587
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000588 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000589 if (Method->isPure())
590 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
591 else
592 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
593
594 // It doesn't make sense to give a virtual destructor a vtable index,
595 // since a single destructor has two entries in the vtable.
596 if (!isa<CXXDestructorDecl>(Method))
597 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000598 ContainingType = RecordTy;
599 }
600
601 llvm::DISubprogram SP =
602 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
603 MethodLinkageName,
604 MethodDefUnit, MethodLine,
605 MethodTy, /*isLocalToUnit=*/false,
606 Method->isThisDeclarationADefinition(),
607 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000608
609 // Don't cache ctors or dtors since we have to emit multiple functions for
610 // a single ctor or dtor.
611 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
612 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000613
614 return SP;
615}
616
Devang Patel4125fd22010-01-19 01:54:44 +0000617/// CollectCXXMemberFunctions - A helper function to collect debug info for
618/// C++ member functions.This is used while creating debug info entry for
619/// a Record.
620void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000621CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000622 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
623 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000624 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
625 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000626 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000627
Devang Pateld5322da2010-02-09 19:09:28 +0000628 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000629 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000630
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000631 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000632 }
633}
634
Devang Patela245c5b2010-01-25 23:32:18 +0000635/// CollectCXXBases - A helper function to collect debug info for
636/// C++ base classes. This is used while creating debug info entry for
637/// a Record.
638void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000639CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000640 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
641 llvm::DICompositeType &RecordTy) {
642
Devang Patel239cec62010-02-01 21:39:52 +0000643 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
644 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
645 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000646 unsigned BFlags = 0;
647 uint64_t BaseOffset;
648
649 const CXXRecordDecl *Base =
650 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
651
652 if (BI->isVirtual()) {
Devang Pateld5322da2010-02-09 19:09:28 +0000653 // virtual base offset index is -ve. The code generator emits dwarf
654 // expression where it expects +ve number.
655 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000656 BFlags = llvm::DIType::FlagVirtual;
657 } else
658 BaseOffset = RL.getBaseClassOffset(Base);
659
660 AccessSpecifier Access = BI->getAccessSpecifier();
661 if (Access == clang::AS_private)
662 BFlags |= llvm::DIType::FlagPrivate;
663 else if (Access == clang::AS_protected)
664 BFlags |= llvm::DIType::FlagProtected;
665
666 llvm::DIType DTy =
667 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
668 RecordTy, llvm::StringRef(),
Devang Patel17800552010-03-09 00:44:50 +0000669 llvm::DIFile(), 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000670 BaseOffset, BFlags,
671 getOrCreateType(BI->getType(),
672 Unit));
673 EltTys.push_back(DTy);
674 }
Devang Patela245c5b2010-01-25 23:32:18 +0000675}
676
Devang Patel4ce3f202010-01-28 18:11:52 +0000677/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000678llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000679 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000680 return VTablePtrType;
681
682 ASTContext &Context = CGM.getContext();
683
684 /* Function type */
685 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
686 STys.push_back(getOrCreateType(Context.IntTy, Unit));
687 llvm::DIArray SElements =
688 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
689 llvm::DIType SubTy =
690 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel17800552010-03-09 00:44:50 +0000691 Unit, "", llvm::DIFile(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000692 0, 0, 0, 0, 0, llvm::DIType(), SElements);
693
694 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
695 llvm::DIType vtbl_ptr_type
696 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel17800552010-03-09 00:44:50 +0000697 Unit, "__vtbl_ptr_type", llvm::DIFile(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000698 0, Size, 0, 0, 0, SubTy);
699
700 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel17800552010-03-09 00:44:50 +0000701 Unit, "", llvm::DIFile(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000702 0, Size, 0, 0, 0, vtbl_ptr_type);
703 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 Patel17800552010-03-09 00:44:50 +0000736 getVtableName(RD), llvm::DIFile(),
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 Pateld0f251b2010-01-20 23:56:40 +0000777 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel411894b2010-02-01 22:40:08 +0000778 llvm::DIDescriptor FDContext =
779 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000780 llvm::DICompositeType FwdDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000781 DebugFactory.CreateCompositeType(Tag, FDContext,
782 STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000783 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000784 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000785
Chris Lattner9c85ba32008-11-10 06:08:34 +0000786 // If this is just a forward declaration, return it.
Douglas Gregor952b0172010-02-11 01:04:33 +0000787 if (!RD->getDefinition())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000788 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000789
Eli Friedman14d63652009-11-16 21:04:30 +0000790 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000791 // Otherwise, insert it into the TypeCache so that recursive uses will find
792 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000793 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000794
795 // Convert all the elements.
796 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
797
Devang Pateld6c5a262010-02-01 21:52:22 +0000798 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000799 if (CXXDecl) {
800 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000801 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000802 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000803 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000804 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000805 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000806 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000807
808 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000809 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000810 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
811 ContainingType =
812 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
813 else if (CXXDecl->isDynamicClass())
814 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000815 }
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Chris Lattner9c85ba32008-11-10 06:08:34 +0000817 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000818 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000819
820 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000821 uint64_t Size = CGM.getContext().getTypeSize(Ty);
822 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000823
Devang Patel411894b2010-02-01 22:40:08 +0000824 llvm::DIDescriptor RDContext =
825 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000826 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000827 DebugFactory.CreateCompositeType(Tag, RDContext,
828 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000829 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000830 llvm::DIType(), Elements,
831 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000832
833 // Now that we have a real decl for the struct, replace anything using the
834 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000835 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000836
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837 return RealDecl;
838}
839
Devang Patel9ca36b62009-02-26 21:10:26 +0000840/// CreateType - get objective-c interface type.
841llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000842 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000843 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Devang Patel9ca36b62009-02-26 21:10:26 +0000845 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000846 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000847
848 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000849 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Pateld6c5a262010-02-01 21:52:22 +0000850 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000851 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
852
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Devang Patel17800552010-03-09 00:44:50 +0000854 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000855
Devang Patel9ca36b62009-02-26 21:10:26 +0000856 // To handle recursive interface, we
857 // first generate a debug descriptor for the struct as a forward declaration.
858 // Then (if it is a definition) we go through and get debug info for all of
859 // its members. Finally, we create a descriptor for the complete type (which
860 // may refer to the forward decl if the struct is recursive) and replace all
861 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000862 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000863 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000864 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000865 llvm::DIType(), llvm::DIArray(),
866 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000867
Devang Patel9ca36b62009-02-26 21:10:26 +0000868 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000869 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000870 return FwdDecl;
871
Devang Patelffffb032009-11-16 20:09:38 +0000872 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000873 // Otherwise, insert it into the TypeCache so that recursive uses will find
874 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000875 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000876
877 // Convert all the elements.
878 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
879
Devang Pateld6c5a262010-02-01 21:52:22 +0000880 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000881 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000882 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000883 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000884 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000885 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Patel17800552010-03-09 00:44:50 +0000886 Unit, "", llvm::DIFile(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000887 0 /* offset */, 0, SClassTy);
888 EltTys.push_back(InhTag);
889 }
890
Devang Pateld6c5a262010-02-01 21:52:22 +0000891 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000892
893 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000894 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
895 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000896 ObjCIvarDecl *Field = *I;
897 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
898
Devang Patel73621622009-11-25 17:37:31 +0000899 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000900
Devang Patelde135022009-04-27 22:40:36 +0000901 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000902 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000903 continue;
904
Devang Patel9ca36b62009-02-26 21:10:26 +0000905 // Get the location for the field.
906 SourceLocation FieldDefLoc = Field->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000907 llvm::DIFile FieldDefUnit = getOrCreateFile(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000908 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
909 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
910
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Devang Patel99c20eb2009-03-20 18:24:39 +0000912 QualType FType = Field->getType();
913 uint64_t FieldSize = 0;
914 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000915
Devang Patel99c20eb2009-03-20 18:24:39 +0000916 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Devang Patel99c20eb2009-03-20 18:24:39 +0000918 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000919 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000920 Expr *BitWidth = Field->getBitWidth();
921 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000922 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000923
Anders Carlsson20f12a22009-12-06 18:00:51 +0000924 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000925 }
926
Mike Stump1eb44332009-09-09 15:08:12 +0000927 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
928
Devang Patelc20482b2009-03-19 00:23:53 +0000929 unsigned Flags = 0;
930 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
931 Flags = llvm::DIType::FlagProtected;
932 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
933 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000934
Devang Patel9ca36b62009-02-26 21:10:26 +0000935 // Create a DW_TAG_member node to remember the offset of this field in the
936 // struct. FIXME: This is an absolutely insane way to capture this
937 // information. When we gut debug info, this should be fixed.
938 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
939 FieldName, FieldDefUnit,
940 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000941 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000942 EltTys.push_back(FieldTy);
943 }
Mike Stump1eb44332009-09-09 15:08:12 +0000944
Devang Patel9ca36b62009-02-26 21:10:26 +0000945 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000946 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000947
948 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000949 uint64_t Size = CGM.getContext().getTypeSize(Ty);
950 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000951
Devang Patel6c1fddf2009-07-27 18:42:03 +0000952 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000953 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000954 Line, Size, Align, 0, 0, llvm::DIType(),
955 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000956
957 // Now that we have a real decl for the struct, replace anything using the
958 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000959 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000960
Devang Patel9ca36b62009-02-26 21:10:26 +0000961 return RealDecl;
962}
963
Chris Lattner9c85ba32008-11-10 06:08:34 +0000964llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000965 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000966 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000967
968 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
969
970 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000971 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000972 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000973 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000974 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000975 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000976 }
Mike Stump1eb44332009-09-09 15:08:12 +0000977
Chris Lattner9c85ba32008-11-10 06:08:34 +0000978 // Return a CompositeType for the enum itself.
979 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000980 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981
Devang Pateld6c5a262010-02-01 21:52:22 +0000982 SourceLocation DefLoc = ED->getLocation();
Devang Patel17800552010-03-09 00:44:50 +0000983 llvm::DIFile DefUnit = getOrCreateFile(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000984 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000985 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
986 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
987
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Chris Lattner9c85ba32008-11-10 06:08:34 +0000989 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000990 uint64_t Size = 0;
991 unsigned Align = 0;
992 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000993 Size = CGM.getContext().getTypeSize(Ty);
994 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +0000995 }
Mike Stump1eb44332009-09-09 15:08:12 +0000996
Devang Patelca80a5f2009-10-20 19:55:01 +0000997 llvm::DIType DbgTy =
998 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +0000999 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001000 Size, Align, 0, 0,
1001 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001002 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001003}
1004
1005llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001006 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001007 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1008 return CreateType(RT, Unit);
1009 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1010 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Chris Lattner9c85ba32008-11-10 06:08:34 +00001012 return llvm::DIType();
1013}
1014
Devang Patel70c23cd2010-02-23 22:59:39 +00001015llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001016 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001017 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1018 uint64_t NumElems = Ty->getNumElements();
1019 if (NumElems > 0)
1020 --NumElems;
1021 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1022 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, NumElems));
1023
1024 llvm::DIArray SubscriptArray =
1025 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
1026
1027 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1028 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1029
1030 return
1031 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Patel17800552010-03-09 00:44:50 +00001032 Unit, "", llvm::DIFile(),
Devang Patel70c23cd2010-02-23 22:59:39 +00001033 0, Size, Align, 0, 0,
1034 ElementTy, SubscriptArray);
1035}
1036
Chris Lattner9c85ba32008-11-10 06:08:34 +00001037llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001038 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001039 uint64_t Size;
1040 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001041
1042
Nuno Lopes010d5142009-01-28 00:35:17 +00001043 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001044 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001045 Size = 0;
1046 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001047 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001048 } else if (Ty->isIncompleteArrayType()) {
1049 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001050 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001051 } else {
1052 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001053 Size = CGM.getContext().getTypeSize(Ty);
1054 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001055 }
Mike Stump1eb44332009-09-09 15:08:12 +00001056
Chris Lattner9c85ba32008-11-10 06:08:34 +00001057 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1058 // interior arrays, do we care? Why aren't nested arrays represented the
1059 // obvious/recursive way?
1060 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1061 QualType EltTy(Ty, 0);
1062 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001063 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001064 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001065 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001066 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067 // FIXME: Verify this is right for VLAs.
1068 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1069 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001073 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001074
Devang Patelca80a5f2009-10-20 19:55:01 +00001075 llvm::DIType DbgTy =
1076 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Patel17800552010-03-09 00:44:50 +00001077 Unit, "", llvm::DIFile(),
Devang Patelca80a5f2009-10-20 19:55:01 +00001078 0, Size, Align, 0, 0,
1079 getOrCreateType(EltTy, Unit),
1080 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001081 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001082}
1083
Anders Carlssona031b352009-11-06 19:19:55 +00001084llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001085 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001086 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1087 Ty, Ty->getPointeeType(), Unit);
1088}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001089
Anders Carlsson20f12a22009-12-06 18:00:51 +00001090llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001091 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001092 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1093 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1094
1095 if (!Ty->getPointeeType()->isFunctionType()) {
1096 // We have a data member pointer type.
1097 return PointerDiffDITy;
1098 }
1099
1100 // We have a member function pointer type. Treat it as a struct with two
1101 // ptrdiff_t members.
1102 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1103
1104 uint64_t FieldOffset = 0;
1105 llvm::DIDescriptor ElementTypes[2];
1106
1107 // FIXME: This should probably be a function type instead.
1108 ElementTypes[0] =
1109 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel17800552010-03-09 00:44:50 +00001110 "ptr", llvm::DIFile(), 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001111 Info.first, Info.second, FieldOffset, 0,
1112 PointerDiffDITy);
1113 FieldOffset += Info.first;
1114
1115 ElementTypes[1] =
1116 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel17800552010-03-09 00:44:50 +00001117 "ptr", llvm::DIFile(), 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001118 Info.first, Info.second, FieldOffset, 0,
1119 PointerDiffDITy);
1120
1121 llvm::DIArray Elements =
1122 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1123 llvm::array_lengthof(ElementTypes));
1124
1125 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1126 U, llvm::StringRef("test"),
Devang Patel17800552010-03-09 00:44:50 +00001127 llvm::DIFile(), 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001128 0, 0, 0, llvm::DIType(), Elements);
1129}
1130
Douglas Gregor840943d2009-12-21 20:18:30 +00001131static QualType UnwrapTypeForDebugInfo(QualType T) {
1132 do {
1133 QualType LastT = T;
1134 switch (T->getTypeClass()) {
1135 default:
1136 return T;
1137 case Type::TemplateSpecialization:
1138 T = cast<TemplateSpecializationType>(T)->desugar();
1139 break;
1140 case Type::TypeOfExpr: {
1141 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1142 T = Ty->getUnderlyingExpr()->getType();
1143 break;
1144 }
1145 case Type::TypeOf:
1146 T = cast<TypeOfType>(T)->getUnderlyingType();
1147 break;
1148 case Type::Decltype:
1149 T = cast<DecltypeType>(T)->getUnderlyingType();
1150 break;
1151 case Type::QualifiedName:
1152 T = cast<QualifiedNameType>(T)->getNamedType();
1153 break;
1154 case Type::SubstTemplateTypeParm:
1155 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1156 break;
1157 case Type::Elaborated:
1158 T = cast<ElaboratedType>(T)->getUnderlyingType();
1159 break;
1160 }
1161
1162 assert(T != LastT && "Type unwrapping failed to unwrap!");
1163 if (T == LastT)
1164 return T;
1165 } while (true);
1166
1167 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001168}
1169
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001170/// getOrCreateType - Get the type from the cache or create a new
1171/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001172llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001173 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001174 if (Ty.isNull())
1175 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001176
Douglas Gregor840943d2009-12-21 20:18:30 +00001177 // Unwrap the type as needed for debug information.
1178 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001179
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001180 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001181 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001182 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001183 if (it != TypeCache.end()) {
1184 // Verify that the debug info still exists.
1185 if (&*it->second)
1186 return llvm::DIType(cast<llvm::MDNode>(it->second));
1187 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001188
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001189 // Otherwise create the type.
1190 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001191
1192 // And update the type cache.
1193 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001194 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001195}
1196
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001197/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001198llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001199 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001200 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001201 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001202 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001203
Douglas Gregor2101a822009-12-21 19:57:21 +00001204 const char *Diag = 0;
1205
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001206 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001207 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001208#define TYPE(Class, Base)
1209#define ABSTRACT_TYPE(Class, Base)
1210#define NON_CANONICAL_TYPE(Class, Base)
1211#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1212#include "clang/AST/TypeNodes.def"
1213 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001214
Anders Carlssonbfe69952009-11-06 18:24:04 +00001215 // FIXME: Handle these.
1216 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001217 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001218
1219 case Type::Vector:
1220 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001221 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001222 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001223 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001224 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1225 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1226 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1227 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001228 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001229 return CreateType(cast<BlockPointerType>(Ty), Unit);
1230 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001231 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001232 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001233 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001234 case Type::FunctionProto:
1235 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001236 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001237 case Type::ConstantArray:
1238 case Type::VariableArray:
1239 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001240 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001241
1242 case Type::LValueReference:
1243 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1244
Anders Carlsson20f12a22009-12-06 18:00:51 +00001245 case Type::MemberPointer:
1246 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001247
1248 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001249 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001250 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001251 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001252 case Type::TypeOfExpr:
1253 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001254 case Type::Decltype:
1255 llvm_unreachable("type should have been unwrapped!");
1256 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001257
1258 case Type::RValueReference:
1259 // FIXME: Implement!
1260 Diag = "rvalue references";
1261 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001262 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001263
1264 assert(Diag && "Fall through without a diagnostic?");
1265 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1266 "debug information for %0 is not yet supported");
1267 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1268 << Diag;
1269 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001270}
1271
1272/// EmitFunctionStart - Constructs the debug code for entering a function -
1273/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001274void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001275 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001276 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001277
Devang Patel9c6c3a02010-01-14 00:36:21 +00001278 llvm::StringRef Name;
1279 llvm::StringRef LinkageName;
1280
1281 const Decl *D = GD.getDecl();
1282 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001283 // If there is a DISubprogram for this function available then use it.
1284 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1285 FI = SPCache.find(FD);
1286 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001287 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1288 if (SP.isSubprogram() && llvm::DISubprogram(SP.getNode()).isDefinition()) {
Devang Patel4125fd22010-01-19 01:54:44 +00001289 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001290 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001291 return;
1292 }
1293 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001294 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001295 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001296 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001297 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001298 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001299 } else {
1300 // Use llvm function name as linkage name.
1301 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001302 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001303 if (!Name.empty() && Name[0] == '\01')
1304 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001305 }
Mike Stump1eb44332009-09-09 15:08:12 +00001306
Devang Patel98a200b2010-01-14 18:06:13 +00001307 // It is expected that CurLoc is set before using EmitFunctionStart.
1308 // Usually, CurLoc points to the left bracket location of compound
1309 // statement representing function body.
Devang Patel17800552010-03-09 00:44:50 +00001310 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001311 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001312 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001313
Chris Lattner9c85ba32008-11-10 06:08:34 +00001314 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001315 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001316 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001317 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001318
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001319 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001320 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001321 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001322}
1323
1324
Chris Lattner9c85ba32008-11-10 06:08:34 +00001325void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001326 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001327
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001328 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001329 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001330 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001331 || (SM.getInstantiationLineNumber(CurLoc) ==
1332 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001333 && SM.isFromSameFile(CurLoc, PrevLoc)))
1334 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001335
1336 // Update last state.
1337 PrevLoc = CurLoc;
1338
1339 // Get the appropriate compile unit.
Devang Patel17800552010-03-09 00:44:50 +00001340 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001341 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001342
Devang Patel8fae0602009-11-13 19:10:24 +00001343 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001344 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1345 llvm::DILocation DO(NULL);
1346 llvm::DILocation DL =
1347 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1348 DS, DO);
1349 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001350}
1351
1352/// EmitRegionStart- Constructs the debug code for entering a declarative
1353/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001354void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Pateld19429f2010-02-16 21:41:20 +00001355 SourceManager &SM = CGM.getContext().getSourceManager();
1356 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel8fae0602009-11-13 19:10:24 +00001357 llvm::DIDescriptor D =
1358 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1359 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001360 llvm::DIDescriptor(RegionStack.back()),
1361 PLoc.getLine(), PLoc.getColumn());
Devang Patel8fae0602009-11-13 19:10:24 +00001362 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001363}
1364
1365/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1366/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001367void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001368 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1369
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001370 // Provide an region stop point.
1371 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001372
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001373 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001374}
1375
Devang Patel809b9bb2010-02-10 18:49:08 +00001376// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1377// See BuildByRefType.
1378llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1379 uint64_t *XOffset) {
1380
1381 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1382
1383 QualType FType;
1384 uint64_t FieldSize, FieldOffset;
1385 unsigned FieldAlign;
1386
Devang Patel17800552010-03-09 00:44:50 +00001387 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001388 QualType Type = VD->getType();
1389
1390 FieldOffset = 0;
1391 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1392 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1393 FieldSize = CGM.getContext().getTypeSize(FType);
1394 FieldAlign = CGM.getContext().getTypeAlign(FType);
1395 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel17800552010-03-09 00:44:50 +00001396 "__isa", llvm::DIFile(),
Devang Patel809b9bb2010-02-10 18:49:08 +00001397 0, FieldSize, FieldAlign,
1398 FieldOffset, 0, FieldTy);
1399 EltTys.push_back(FieldTy);
1400 FieldOffset += FieldSize;
1401
1402 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1403 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1404 FieldSize = CGM.getContext().getTypeSize(FType);
1405 FieldAlign = CGM.getContext().getTypeAlign(FType);
1406 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel17800552010-03-09 00:44:50 +00001407 "__forwarding", llvm::DIFile(),
Devang Patel809b9bb2010-02-10 18:49:08 +00001408 0, FieldSize, FieldAlign,
1409 FieldOffset, 0, FieldTy);
1410 EltTys.push_back(FieldTy);
1411 FieldOffset += FieldSize;
1412
1413 FType = CGM.getContext().IntTy;
1414 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1415 FieldSize = CGM.getContext().getTypeSize(FType);
1416 FieldAlign = CGM.getContext().getTypeAlign(FType);
1417 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel17800552010-03-09 00:44:50 +00001418 "__flags", llvm::DIFile(),
Devang Patel809b9bb2010-02-10 18:49:08 +00001419 0, FieldSize, FieldAlign,
1420 FieldOffset, 0, FieldTy);
1421 EltTys.push_back(FieldTy);
1422 FieldOffset += FieldSize;
1423
1424 FType = CGM.getContext().IntTy;
1425 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1426 FieldSize = CGM.getContext().getTypeSize(FType);
1427 FieldAlign = CGM.getContext().getTypeAlign(FType);
1428 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel17800552010-03-09 00:44:50 +00001429 "__size", llvm::DIFile(),
Devang Patel809b9bb2010-02-10 18:49:08 +00001430 0, FieldSize, FieldAlign,
1431 FieldOffset, 0, FieldTy);
1432 EltTys.push_back(FieldTy);
1433 FieldOffset += FieldSize;
1434
1435 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1436 if (HasCopyAndDispose) {
1437 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1438 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1439 FieldSize = CGM.getContext().getTypeSize(FType);
1440 FieldAlign = CGM.getContext().getTypeAlign(FType);
1441 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1442 "__copy_helper",
Devang Patel17800552010-03-09 00:44:50 +00001443 llvm::DIFile(),
Devang Patel809b9bb2010-02-10 18:49:08 +00001444 0, FieldSize, FieldAlign,
1445 FieldOffset, 0, FieldTy);
1446 EltTys.push_back(FieldTy);
1447 FieldOffset += FieldSize;
1448
1449 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1450 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1451 FieldSize = CGM.getContext().getTypeSize(FType);
1452 FieldAlign = CGM.getContext().getTypeAlign(FType);
1453 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1454 "__destroy_helper",
Devang Patel17800552010-03-09 00:44:50 +00001455 llvm::DIFile(),
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 Patel17800552010-03-09 00:44:50 +00001478 Unit, "", llvm::DIFile(),
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 Patel17800552010-03-09 00:44:50 +00001493 VD->getName(), llvm::DIFile(),
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,
1505 Unit, "",
Devang Patel17800552010-03-09 00:44:50 +00001506 llvm::DIFile(),
Devang Patel809b9bb2010-02-10 18:49:08 +00001507 0, FieldOffset, 0, 0, Flags,
1508 llvm::DIType(), Elements);
1509
1510}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001511/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001512void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001513 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001514 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1515
Devang Patel07739032009-03-27 23:16:32 +00001516 // Do not emit variable debug information while generating optimized code.
1517 // The llvm optimizer and code generator are not yet ready to support
1518 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001519 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001520 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001521 return;
1522
Devang Patel17800552010-03-09 00:44:50 +00001523 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001524 llvm::DIType Ty;
1525 uint64_t XOffset = 0;
1526 if (VD->hasAttr<BlocksAttr>())
1527 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1528 else
1529 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001530
Chris Lattner9c85ba32008-11-10 06:08:34 +00001531 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001532 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001533 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001534 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001535 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001536 if (PLoc.isInvalid())
1537 PLoc = SM.getPresumedLoc(CurLoc);
1538 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001539 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001540 Column = PLoc.getColumn();
Devang Patel17800552010-03-09 00:44:50 +00001541 Unit = getOrCreateFile(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001542 } else {
Devang Patel17800552010-03-09 00:44:50 +00001543 Unit = llvm::DIFile();
Eli Friedman1468ac72009-11-16 20:33:31 +00001544 }
Mike Stump1eb44332009-09-09 15:08:12 +00001545
Chris Lattner9c85ba32008-11-10 06:08:34 +00001546 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001547 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001548 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001549 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001550 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001551 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001552 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001553 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001554
Devang Patel8fae0602009-11-13 19:10:24 +00001555 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001556 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001557 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1558
Chris Lattner23e92c02009-12-28 23:41:39 +00001559 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001560}
1561
Mike Stumpb1a6e682009-09-30 02:43:10 +00001562/// EmitDeclare - Emit local variable declaration debug info.
1563void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1564 llvm::Value *Storage, CGBuilderTy &Builder,
1565 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001566 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001567 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1568
1569 // Do not emit variable debug information while generating optimized code.
1570 // The llvm optimizer and code generator are not yet ready to support
1571 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001572 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001573 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001574 return;
1575
1576 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001577 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001578 llvm::DIType Ty;
1579 if (VD->hasAttr<BlocksAttr>())
1580 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1581 else
1582 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001583
1584 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001585 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001586 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001587 unsigned Line = 0;
1588 if (!PLoc.isInvalid())
1589 Line = PLoc.getLine();
1590 else
Devang Patel17800552010-03-09 00:44:50 +00001591 Unit = llvm::DIFile();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001592
Devang Pateld6c5a262010-02-01 21:52:22 +00001593 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001594 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001595 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1596 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1597 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1598 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001599 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001600 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1601 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001602 // offset of __forwarding field
1603 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001604 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1605 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1606 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001607 // offset of x field
1608 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001609 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001610 }
1611
1612 // Create the descriptor for the variable.
1613 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001614 DebugFactory.CreateComplexVariable(Tag,
1615 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001616 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001617 addr);
1618 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001619 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001620 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001621
Devang Patel8fae0602009-11-13 19:10:24 +00001622 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001623 llvm::DILocation DO(NULL);
1624 llvm::DILocation DL =
1625 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001626
Chris Lattner23e92c02009-12-28 23:41:39 +00001627 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001628}
1629
Devang Pateld6c5a262010-02-01 21:52:22 +00001630void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001631 llvm::Value *Storage,
1632 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001633 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001634}
1635
Mike Stumpb1a6e682009-09-30 02:43:10 +00001636void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1637 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1638 CodeGenFunction *CGF) {
1639 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1640}
1641
Chris Lattner9c85ba32008-11-10 06:08:34 +00001642/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1643/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001644void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001645 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001646 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001647}
1648
1649
1650
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001651/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001652void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001653 const VarDecl *D) {
1654
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001655 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001656 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001657 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001658 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001659 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001660
Devang Pateleb6d79b2010-02-01 21:34:11 +00001661 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001662 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001663
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001664 // CodeGen turns int[] into int[1] so we'll do the same here.
1665 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001666
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001667 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001668 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001669
Anders Carlsson20f12a22009-12-06 18:00:51 +00001670 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001671 ArrayType::Normal, 0);
1672 }
Sanjiv Gupta67b14f02010-02-25 05:20:44 +00001673 llvm::StringRef DeclName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001674 llvm::DIDescriptor DContext =
1675 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1676 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001677 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001678 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001679 Var->hasInternalLinkage(),
1680 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001681}
1682
Devang Patel9ca36b62009-02-26 21:10:26 +00001683/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001684void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001685 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001686 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001687 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001688 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001689 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001690 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001691
Devang Pateld6c5a262010-02-01 21:52:22 +00001692 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001693
Devang Pateld6c5a262010-02-01 21:52:22 +00001694 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001695 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001696
Devang Patel9ca36b62009-02-26 21:10:26 +00001697 // CodeGen turns int[] into int[1] so we'll do the same here.
1698 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001699
Devang Patel9ca36b62009-02-26 21:10:26 +00001700 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001701 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001702
Anders Carlsson20f12a22009-12-06 18:00:51 +00001703 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001704 ArrayType::Normal, 0);
1705 }
1706
Devang Patelf6a39b72009-10-20 18:26:30 +00001707 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001708 getOrCreateType(T, Unit),
1709 Var->hasInternalLinkage(),
1710 true/*definition*/, Var);
1711}
Devang Patelabb485f2010-02-01 19:16:32 +00001712
1713/// getOrCreateNamesSpace - Return namespace descriptor for the given
1714/// namespace decl.
1715llvm::DINameSpace
1716CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1717 llvm::DIDescriptor Unit) {
1718 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1719 NameSpaceCache.find(NSDecl);
1720 if (I != NameSpaceCache.end())
1721 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1722
1723 SourceManager &SM = CGM.getContext().getSourceManager();
1724 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1725 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1726
1727 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001728 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001729 llvm::DINameSpace NS =
1730 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel17800552010-03-09 00:44:50 +00001731 llvm::DIFile(Unit.getNode()), LineNo);
Devang Patelabb485f2010-02-01 19:16:32 +00001732 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1733 return NS;
1734}