blob: 51f82db61d6fe727099b237678afba13ec43dcf7 [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)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel33583052010-01-28 23:15:27 +000052/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000053llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000054 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000055 if (!Context)
56 return CompileUnit;
57
58 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
59 I = RegionMap.find(Context);
60 if (I != RegionMap.end())
61 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000062
Devang Pateleb6d79b2010-02-01 21:34:11 +000063 // Check namespace.
64 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
65 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
66
Devang Patel979ec2e2009-10-06 00:35:31 +000067 return CompileUnit;
68}
69
Devang Patel9c6c3a02010-01-14 00:36:21 +000070/// getFunctionName - Get function name for the given FunctionDecl. If the
71/// name is constructred on demand (e.g. C++ destructor) then the name
72/// is stored on the side.
73llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
74 assert (FD && "Invalid FunctionDecl!");
75 IdentifierInfo *FII = FD->getIdentifier();
76 if (FII)
77 return FII->getName();
78
79 // Otherwise construct human readable name for debug info.
80 std::string NS = FD->getNameAsString();
81
82 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000083 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000084 memcpy(StrPtr, NS.data(), NS.length());
85 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000086}
87
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000088/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000089/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000090llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000091 // Get source file information.
92 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000093 SourceManager &SM = CGM.getContext().getSourceManager();
Daniel Dunbar831570c2009-01-22 00:09:25 +000094 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000095 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
96 FileName = PLoc.getFilename();
Devang Pateld263e7b2010-02-10 01:09:50 +000097 unsigned FID = PLoc.getIncludeLoc().getRawEncoding();
Mike Stump1eb44332009-09-09 15:08:12 +000098
Devang Pateld263e7b2010-02-10 01:09:50 +000099 // See if this compile unit has been used before for this valid location.
100 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
101 if (!Unit.isNull()) return Unit;
102 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000103
Devang Patel446c6192009-04-17 21:06:59 +0000104 // Get absolute path name.
105 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000106 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000107
Devang Patel72240d72009-06-26 18:32:22 +0000108 // See if thie compile unit is representing main source file. Each source
109 // file has corresponding compile unit. There is only one main source
110 // file at a time.
111 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000112 const LangOptions &LO = CGM.getLangOptions();
113 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000114 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000115 if (!CGO.MainFileName.empty()) {
116 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000117 isMain = true;
118 } else {
119 if (Loc.isValid() && SM.isFromMainFile(Loc))
120 isMain = true;
121 }
122 if (isMain)
123 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000124 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000125
Chris Lattner515455a2009-03-25 03:28:08 +0000126 unsigned LangTag;
127 if (LO.CPlusPlus) {
128 if (LO.ObjC1)
129 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
130 else
131 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
132 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000133 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000134 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000135 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000136 } else {
137 LangTag = llvm::dwarf::DW_LANG_C89;
138 }
Devang Patel446c6192009-04-17 21:06:59 +0000139
Benjamin Kramer47daf682009-12-08 11:02:29 +0000140 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000141#ifdef CLANG_VENDOR
142 CLANG_VENDOR
143#endif
144 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000145
146 // Figure out which version of the ObjC runtime we have.
147 unsigned RuntimeVers = 0;
148 if (LO.ObjC1)
149 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000150
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000151 // Create new compile unit.
Devang Pateld263e7b2010-02-10 01:09:50 +0000152 return DebugFactory.CreateCompileUnit(
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000153 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
154 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000155}
156
Devang Patel65e99f22009-02-25 01:36:11 +0000157/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000158/// one if necessary.
159llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000160 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000161 unsigned Encoding = 0;
162 switch (BT->getKind()) {
163 default:
164 case BuiltinType::Void:
165 return llvm::DIType();
166 case BuiltinType::UChar:
167 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
168 case BuiltinType::Char_S:
169 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
170 case BuiltinType::UShort:
171 case BuiltinType::UInt:
172 case BuiltinType::ULong:
173 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
174 case BuiltinType::Short:
175 case BuiltinType::Int:
176 case BuiltinType::Long:
177 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
178 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
179 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000180 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000181 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000182 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000183 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000184 uint64_t Size = CGM.getContext().getTypeSize(BT);
185 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000186 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Devang Patelca80a5f2009-10-20 19:55:01 +0000188 llvm::DIType DbgTy =
189 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000190 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000191 Unit, 0, Size, Align,
192 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000193 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000194}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000195
Chris Lattnerb7003772009-04-23 06:13:01 +0000196llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
197 llvm::DICompileUnit Unit) {
198 // Bit size, align and offset of the type.
199 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
200 if (Ty->isComplexIntegerType())
201 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000202
Anders Carlsson20f12a22009-12-06 18:00:51 +0000203 uint64_t Size = CGM.getContext().getTypeSize(Ty);
204 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000205 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000206
Devang Patelca80a5f2009-10-20 19:55:01 +0000207 llvm::DIType DbgTy =
208 DebugFactory.CreateBasicType(Unit, "complex",
209 Unit, 0, Size, Align,
210 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000211 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000212}
213
John McCalla1805292009-09-25 01:40:47 +0000214/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000215/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000216llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
217 QualifierCollector Qc;
218 const Type *T = Qc.strip(Ty);
219
220 // Ignore these qualifiers for now.
221 Qc.removeObjCGCAttr();
222 Qc.removeAddressSpace();
223
Chris Lattner9c85ba32008-11-10 06:08:34 +0000224 // We will create one Derived type for one qualifier and recurse to handle any
225 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000226 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000227 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000228 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000229 Qc.removeConst();
230 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000232 Qc.removeVolatile();
233 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000234 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000235 Qc.removeRestrict();
236 } else {
237 assert(Qc.empty() && "Unknown type qualifier for debug info");
238 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000239 }
Mike Stump1eb44332009-09-09 15:08:12 +0000240
John McCalla1805292009-09-25 01:40:47 +0000241 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
242
Daniel Dunbar3845f862008-10-31 03:54:29 +0000243 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
244 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000245 llvm::DIType DbgTy =
246 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
247 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000248 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000249}
250
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000251llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
252 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000253 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000254 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
255 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000256 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000257}
258
Chris Lattner9c85ba32008-11-10 06:08:34 +0000259llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
260 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000261 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
262 Ty->getPointeeType(), Unit);
263}
264
265llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
266 const Type *Ty,
267 QualType PointeeTy,
268 llvm::DICompileUnit Unit) {
269 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000270
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000271 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000272
273 // Size is always the size of a pointer. We can't use getTypeSize here
274 // because that does not return the correct value for references.
275 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000276 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
277 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000278
Devang Patelca80a5f2009-10-20 19:55:01 +0000279 return
Anders Carlssona031b352009-11-06 19:19:55 +0000280 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000281 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000282
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000283}
284
Mike Stump9bc093c2009-05-14 02:03:51 +0000285llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
286 llvm::DICompileUnit Unit) {
287 if (BlockLiteralGenericSet)
288 return BlockLiteralGeneric;
289
290 llvm::DICompileUnit DefUnit;
291 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
292
293 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
294
295 llvm::DIType FieldTy;
296
297 QualType FType;
298 uint64_t FieldSize, FieldOffset;
299 unsigned FieldAlign;
300
301 llvm::DIArray Elements;
302 llvm::DIType EltTy, DescTy;
303
304 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000305 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000306 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000307 FieldSize = CGM.getContext().getTypeSize(FType);
308 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000309 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
310 "reserved", DefUnit,
311 0, FieldSize, FieldAlign,
312 FieldOffset, 0, FieldTy);
313 EltTys.push_back(FieldTy);
314
315 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000316 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000317 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000318 FieldSize = CGM.getContext().getTypeSize(FType);
319 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000320 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
321 "Size", DefUnit,
322 0, FieldSize, FieldAlign,
323 FieldOffset, 0, FieldTy);
324 EltTys.push_back(FieldTy);
325
326 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000327 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000328 EltTys.clear();
329
Mike Stump3d363c52009-10-02 02:30:50 +0000330 unsigned Flags = llvm::DIType::FlagAppleBlock;
331
Mike Stump9bc093c2009-05-14 02:03:51 +0000332 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000333 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000334 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Mike Stump9bc093c2009-05-14 02:03:51 +0000336 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000337 uint64_t Size = CGM.getContext().getTypeSize(Ty);
338 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Mike Stump9bc093c2009-05-14 02:03:51 +0000340 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
341 Unit, "", llvm::DICompileUnit(),
342 0, Size, Align, 0, 0, EltTy);
343
344 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000345 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000346 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000347 FieldSize = CGM.getContext().getTypeSize(FType);
348 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000349 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
350 "__isa", DefUnit,
351 0, FieldSize, FieldAlign,
352 FieldOffset, 0, FieldTy);
353 EltTys.push_back(FieldTy);
354
355 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000356 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000358 FieldSize = CGM.getContext().getTypeSize(FType);
359 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000360 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
361 "__flags", DefUnit,
362 0, FieldSize, FieldAlign,
363 FieldOffset, 0, FieldTy);
364 EltTys.push_back(FieldTy);
365
366 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000367 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000368 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000369 FieldSize = CGM.getContext().getTypeSize(FType);
370 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000371 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
372 "__reserved", DefUnit,
373 0, FieldSize, FieldAlign,
374 FieldOffset, 0, FieldTy);
375 EltTys.push_back(FieldTy);
376
377 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000378 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000380 FieldSize = CGM.getContext().getTypeSize(FType);
381 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000382 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
383 "__FuncPtr", DefUnit,
384 0, FieldSize, FieldAlign,
385 FieldOffset, 0, FieldTy);
386 EltTys.push_back(FieldTy);
387
388 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000389 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000390 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000391 FieldSize = CGM.getContext().getTypeSize(Ty);
392 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000393 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
394 "__descriptor", DefUnit,
395 0, FieldSize, FieldAlign,
396 FieldOffset, 0, FieldTy);
397 EltTys.push_back(FieldTy);
398
399 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000400 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000401
402 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000403 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000404 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Mike Stump9bc093c2009-05-14 02:03:51 +0000406 BlockLiteralGenericSet = true;
407 BlockLiteralGeneric
408 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
409 "", llvm::DICompileUnit(),
410 0, Size, Align, 0, 0, EltTy);
411 return BlockLiteralGeneric;
412}
413
Chris Lattner9c85ba32008-11-10 06:08:34 +0000414llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
415 llvm::DICompileUnit Unit) {
416 // Typedefs are derived from some other type. If we have a typedef of a
417 // typedef, make sure to emit the whole chain.
418 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000419
Chris Lattner9c85ba32008-11-10 06:08:34 +0000420 // We don't set size information, but do specify where the typedef was
421 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000422 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000423 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000424 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000425
Devang Pateleb6d79b2010-02-01 21:34:11 +0000426 llvm::DIDescriptor TyContext
427 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
428 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000429 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000430 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000431 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000432 Ty->getDecl()->getName(), Unit,
433 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000434 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000435}
436
Chris Lattner9c85ba32008-11-10 06:08:34 +0000437llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
438 llvm::DICompileUnit Unit) {
439 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000440
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 // Add the result type at least.
442 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Chris Lattner9c85ba32008-11-10 06:08:34 +0000444 // Set up remainder of arguments if there is a prototype.
445 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000446 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
448 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
449 } else {
450 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000451 }
452
Chris Lattner9c85ba32008-11-10 06:08:34 +0000453 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000454 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000455
Devang Patelca80a5f2009-10-20 19:55:01 +0000456 llvm::DIType DbgTy =
457 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
458 Unit, "", llvm::DICompileUnit(),
459 0, 0, 0, 0, 0,
460 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000461 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000462}
463
Devang Patel428deb52010-01-19 00:00:59 +0000464/// CollectRecordFields - A helper function to collect debug info for
465/// record fields. This is used while creating debug info entry for a Record.
466void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000467CollectRecordFields(const RecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000468 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
469 unsigned FieldNo = 0;
470 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +0000471 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
472 for (RecordDecl::field_iterator I = RD->field_begin(),
473 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000474 I != E; ++I, ++FieldNo) {
475 FieldDecl *Field = *I;
476 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
477
478 llvm::StringRef FieldName = Field->getName();
479
480 // Ignore unnamed fields.
481 if (FieldName.empty())
482 continue;
483
484 // Get the location for the field.
485 SourceLocation FieldDefLoc = Field->getLocation();
486 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
487 llvm::DICompileUnit FieldDefUnit;
488 unsigned FieldLine = 0;
489
490 if (!PLoc.isInvalid()) {
491 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
492 FieldLine = PLoc.getLine();
493 }
494
495 QualType FType = Field->getType();
496 uint64_t FieldSize = 0;
497 unsigned FieldAlign = 0;
498 if (!FType->isIncompleteArrayType()) {
499
500 // Bit size, align and offset of the type.
501 FieldSize = CGM.getContext().getTypeSize(FType);
502 Expr *BitWidth = Field->getBitWidth();
503 if (BitWidth)
504 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
505
506 FieldAlign = CGM.getContext().getTypeAlign(FType);
507 }
508
509 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
510
511 // Create a DW_TAG_member node to remember the offset of this field in the
512 // struct. FIXME: This is an absolutely insane way to capture this
513 // information. When we gut debug info, this should be fixed.
514 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
515 FieldName, FieldDefUnit,
516 FieldLine, FieldSize, FieldAlign,
517 FieldOffset, 0, FieldTy);
518 EltTys.push_back(FieldTy);
519 }
520}
521
Devang Patela6da1922010-01-28 00:28:01 +0000522/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
523/// function type is not updated to include implicit "this" pointer. Use this
524/// routine to get a method type which includes "this" pointer.
525llvm::DIType
526CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
527 llvm::DICompileUnit Unit) {
528 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000529
530 // Static methods do not need "this" pointer argument.
531 if (Method->isStatic())
532 return FnTy;
533
Devang Patela6da1922010-01-28 00:28:01 +0000534 // Add "this" pointer.
535
536 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
537 assert (Args.getNumElements() && "Invalid number of arguments!");
538
539 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
540
541 // First element is always return type. For 'void' functions it is NULL.
542 Elts.push_back(Args.getElement(0));
543
544 // "this" pointer is always first argument.
545 ASTContext &Context = CGM.getContext();
546 QualType ThisPtr =
547 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000548 llvm::DIType ThisPtrType =
549 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
550 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType.getNode();
551 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000552
553 // Copy rest of the arguments.
554 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
555 Elts.push_back(Args.getElement(i));
556
557 llvm::DIArray EltTypeArray =
558 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
559
560 return
561 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
562 Unit, "", llvm::DICompileUnit(),
563 0, 0, 0, 0, 0,
564 llvm::DIType(), EltTypeArray);
565}
566
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000567/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
568/// a single member function GlobalDecl.
569llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000570CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000571 llvm::DICompileUnit Unit,
572 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000573 bool IsCtorOrDtor =
574 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
575
576 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000577 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000578 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000579
580 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
581 // make sense to give a single ctor/dtor a linkage name.
582 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000583 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000584
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000585 SourceManager &SM = CGM.getContext().getSourceManager();
586
587 // Get the location for the method.
588 SourceLocation MethodDefLoc = Method->getLocation();
589 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
590 llvm::DICompileUnit MethodDefUnit;
591 unsigned MethodLine = 0;
592
593 if (!PLoc.isInvalid()) {
594 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
595 MethodLine = PLoc.getLine();
596 }
597
598 // Collect virtual method info.
599 llvm::DIType ContainingType;
600 unsigned Virtuality = 0;
601 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000602
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000603 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000604 if (Method->isPure())
605 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
606 else
607 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
608
609 // It doesn't make sense to give a virtual destructor a vtable index,
610 // since a single destructor has two entries in the vtable.
611 if (!isa<CXXDestructorDecl>(Method))
612 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000613 ContainingType = RecordTy;
614 }
615
616 llvm::DISubprogram SP =
617 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
618 MethodLinkageName,
619 MethodDefUnit, MethodLine,
620 MethodTy, /*isLocalToUnit=*/false,
621 Method->isThisDeclarationADefinition(),
622 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000623
624 // Don't cache ctors or dtors since we have to emit multiple functions for
625 // a single ctor or dtor.
626 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
627 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000628
629 return SP;
630}
631
Devang Patel4125fd22010-01-19 01:54:44 +0000632/// CollectCXXMemberFunctions - A helper function to collect debug info for
633/// C++ member functions.This is used while creating debug info entry for
634/// a Record.
635void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000636CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000637 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
638 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000639 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
640 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000641 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000642
Devang Pateld5322da2010-02-09 19:09:28 +0000643 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000644 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000645
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000646 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000647 }
648}
649
Devang Patela245c5b2010-01-25 23:32:18 +0000650/// CollectCXXBases - A helper function to collect debug info for
651/// C++ base classes. This is used while creating debug info entry for
652/// a Record.
653void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000654CollectCXXBases(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000655 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
656 llvm::DICompositeType &RecordTy) {
657
Devang Patel239cec62010-02-01 21:39:52 +0000658 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
659 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
660 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000661 unsigned BFlags = 0;
662 uint64_t BaseOffset;
663
664 const CXXRecordDecl *Base =
665 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
666
667 if (BI->isVirtual()) {
Devang Pateld5322da2010-02-09 19:09:28 +0000668 // virtual base offset index is -ve. The code generator emits dwarf
669 // expression where it expects +ve number.
670 BaseOffset = 0 - CGM.getVtableInfo().getVirtualBaseOffsetIndex(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000671 BFlags = llvm::DIType::FlagVirtual;
672 } else
673 BaseOffset = RL.getBaseClassOffset(Base);
674
675 AccessSpecifier Access = BI->getAccessSpecifier();
676 if (Access == clang::AS_private)
677 BFlags |= llvm::DIType::FlagPrivate;
678 else if (Access == clang::AS_protected)
679 BFlags |= llvm::DIType::FlagProtected;
680
681 llvm::DIType DTy =
682 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
683 RecordTy, llvm::StringRef(),
684 llvm::DICompileUnit(), 0, 0, 0,
685 BaseOffset, BFlags,
686 getOrCreateType(BI->getType(),
687 Unit));
688 EltTys.push_back(DTy);
689 }
Devang Patela245c5b2010-01-25 23:32:18 +0000690}
691
Devang Patel4ce3f202010-01-28 18:11:52 +0000692/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
693llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
694 if (!VTablePtrType.isNull())
695 return VTablePtrType;
696
697 ASTContext &Context = CGM.getContext();
698
699 /* Function type */
700 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
701 STys.push_back(getOrCreateType(Context.IntTy, Unit));
702 llvm::DIArray SElements =
703 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
704 llvm::DIType SubTy =
705 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
706 Unit, "", llvm::DICompileUnit(),
707 0, 0, 0, 0, 0, llvm::DIType(), SElements);
708
709 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
710 llvm::DIType vtbl_ptr_type
711 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
712 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
713 0, Size, 0, 0, 0, SubTy);
714
715 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
716 Unit, "", llvm::DICompileUnit(),
717 0, Size, 0, 0, 0, vtbl_ptr_type);
718 return VTablePtrType;
719}
720
721/// getVtableName - Get vtable name for the given Class.
Devang Patel239cec62010-02-01 21:39:52 +0000722llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000723 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000724 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000725
726 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000727 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000728 memcpy(StrPtr, Name.data(), Name.length());
729 return llvm::StringRef(StrPtr, Name.length());
730}
731
732
733/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
734/// debug info entry in EltTys vector.
735void CGDebugInfo::
Devang Patel239cec62010-02-01 21:39:52 +0000736CollectVtableInfo(const CXXRecordDecl *RD, llvm::DICompileUnit Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000737 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000738 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000739
740 // If there is a primary base then it will hold vtable info.
741 if (RL.getPrimaryBase())
742 return;
743
744 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000745 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000746 return;
747
748 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
749 llvm::DIType VPTR
750 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel239cec62010-02-01 21:39:52 +0000751 getVtableName(RD), llvm::DICompileUnit(),
Devang Patel4ce3f202010-01-28 18:11:52 +0000752 0, Size, 0, 0, 0,
753 getOrCreateVTablePtrType(Unit));
754 EltTys.push_back(VPTR);
755}
756
Devang Patel65e99f22009-02-25 01:36:11 +0000757/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000758llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
759 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000760 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000761
Chris Lattner9c85ba32008-11-10 06:08:34 +0000762 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000763 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000764 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000765 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000766 Tag = llvm::dwarf::DW_TAG_union_type;
767 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000768 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000769 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000770 }
771
Anders Carlsson20f12a22009-12-06 18:00:51 +0000772 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000773
Chris Lattner9c85ba32008-11-10 06:08:34 +0000774 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000775 PresumedLoc PLoc = SM.getPresumedLoc(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000776 llvm::DICompileUnit DefUnit;
777 unsigned Line = 0;
778 if (!PLoc.isInvalid()) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000779 DefUnit = getOrCreateCompileUnit(RD->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000780 Line = PLoc.getLine();
781 }
Mike Stump1eb44332009-09-09 15:08:12 +0000782
Chris Lattner9c85ba32008-11-10 06:08:34 +0000783 // Records and classes and unions can all be recursive. To handle them, we
784 // first generate a debug descriptor for the struct as a forward declaration.
785 // Then (if it is a definition) we go through and get debug info for all of
786 // its members. Finally, we create a descriptor for the complete type (which
787 // may refer to the forward decl if the struct is recursive) and replace all
788 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000789
Devang Pateld6c5a262010-02-01 21:52:22 +0000790 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000791 // are uniqued so use type name to ensure uniquness.
Devang Pateld0f251b2010-01-20 23:56:40 +0000792 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel411894b2010-02-01 22:40:08 +0000793 llvm::DIDescriptor FDContext =
794 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000795 llvm::DICompositeType FwdDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000796 DebugFactory.CreateCompositeType(Tag, FDContext,
797 STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000798 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000800
Chris Lattner9c85ba32008-11-10 06:08:34 +0000801 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000802 if (!RD->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000803 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000804
Eli Friedman14d63652009-11-16 21:04:30 +0000805 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000806 // Otherwise, insert it into the TypeCache so that recursive uses will find
807 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000808 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000809
810 // Convert all the elements.
811 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
812
Devang Pateld6c5a262010-02-01 21:52:22 +0000813 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000814 if (CXXDecl) {
815 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000816 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000817 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000818 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000819 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000820 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000821 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000822
823 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000824 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000825 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
826 ContainingType =
827 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
828 else if (CXXDecl->isDynamicClass())
829 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000830 }
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Chris Lattner9c85ba32008-11-10 06:08:34 +0000832 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000833 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834
835 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000836 uint64_t Size = CGM.getContext().getTypeSize(Ty);
837 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000838
Devang Patel411894b2010-02-01 22:40:08 +0000839 llvm::DIDescriptor RDContext =
840 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel0ce73f62009-07-22 18:57:00 +0000841 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000842 DebugFactory.CreateCompositeType(Tag, RDContext,
843 RD->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000844 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000845 llvm::DIType(), Elements,
846 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000847
848 // Now that we have a real decl for the struct, replace anything using the
849 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000850 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000851
Chris Lattner9c85ba32008-11-10 06:08:34 +0000852 return RealDecl;
853}
854
Devang Patel9ca36b62009-02-26 21:10:26 +0000855/// CreateType - get objective-c interface type.
856llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
857 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000858 ObjCInterfaceDecl *ID = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Devang Patel9ca36b62009-02-26 21:10:26 +0000860 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000861 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000862
863 // Get overall information about the record type for the debug info.
Devang Pateld6c5a262010-02-01 21:52:22 +0000864 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(ID->getLocation());
865 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000866 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
867
Mike Stump1eb44332009-09-09 15:08:12 +0000868
Daniel Dunbard86d3362009-05-18 20:51:58 +0000869 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000870
Devang Patel9ca36b62009-02-26 21:10:26 +0000871 // To handle recursive interface, we
872 // first generate a debug descriptor for the struct as a forward declaration.
873 // Then (if it is a definition) we go through and get debug info for all of
874 // its members. Finally, we create a descriptor for the complete type (which
875 // may refer to the forward decl if the struct is recursive) and replace all
876 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000877 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000878 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000879 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000880 llvm::DIType(), llvm::DIArray(),
881 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000882
Devang Patel9ca36b62009-02-26 21:10:26 +0000883 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000884 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000885 return FwdDecl;
886
Devang Patelffffb032009-11-16 20:09:38 +0000887 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000888 // Otherwise, insert it into the TypeCache so that recursive uses will find
889 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000890 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000891
892 // Convert all the elements.
893 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
894
Devang Pateld6c5a262010-02-01 21:52:22 +0000895 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000896 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000897 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000898 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000899 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000900 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000901 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000902 0 /* offset */, 0, SClassTy);
903 EltTys.push_back(InhTag);
904 }
905
Devang Pateld6c5a262010-02-01 21:52:22 +0000906 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000907
908 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000909 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
910 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000911 ObjCIvarDecl *Field = *I;
912 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
913
Devang Patel73621622009-11-25 17:37:31 +0000914 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000915
Devang Patelde135022009-04-27 22:40:36 +0000916 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000917 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000918 continue;
919
Devang Patel9ca36b62009-02-26 21:10:26 +0000920 // Get the location for the field.
921 SourceLocation FieldDefLoc = Field->getLocation();
922 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000923 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
924 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
925
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Devang Patel99c20eb2009-03-20 18:24:39 +0000927 QualType FType = Field->getType();
928 uint64_t FieldSize = 0;
929 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000930
Devang Patel99c20eb2009-03-20 18:24:39 +0000931 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000932
Devang Patel99c20eb2009-03-20 18:24:39 +0000933 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000934 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000935 Expr *BitWidth = Field->getBitWidth();
936 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000937 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000938
Anders Carlsson20f12a22009-12-06 18:00:51 +0000939 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000940 }
941
Mike Stump1eb44332009-09-09 15:08:12 +0000942 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
943
Devang Patelc20482b2009-03-19 00:23:53 +0000944 unsigned Flags = 0;
945 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
946 Flags = llvm::DIType::FlagProtected;
947 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
948 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Devang Patel9ca36b62009-02-26 21:10:26 +0000950 // Create a DW_TAG_member node to remember the offset of this field in the
951 // struct. FIXME: This is an absolutely insane way to capture this
952 // information. When we gut debug info, this should be fixed.
953 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
954 FieldName, FieldDefUnit,
955 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000956 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000957 EltTys.push_back(FieldTy);
958 }
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Devang Patel9ca36b62009-02-26 21:10:26 +0000960 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000961 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000962
963 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000964 uint64_t Size = CGM.getContext().getTypeSize(Ty);
965 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000966
Devang Patel6c1fddf2009-07-27 18:42:03 +0000967 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000968 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000969 Line, Size, Align, 0, 0, llvm::DIType(),
970 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000971
972 // Now that we have a real decl for the struct, replace anything using the
973 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000974 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000975
Devang Patel9ca36b62009-02-26 21:10:26 +0000976 return RealDecl;
977}
978
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
980 llvm::DICompileUnit Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000981 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000982
983 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
984
985 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000986 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +0000987 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000988 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000989 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000990 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000991 }
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Chris Lattner9c85ba32008-11-10 06:08:34 +0000993 // Return a CompositeType for the enum itself.
994 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000995 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000996
Devang Pateld6c5a262010-02-01 21:52:22 +0000997 SourceLocation DefLoc = ED->getLocation();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000998 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000999 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001000 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
1001 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
1002
Mike Stump1eb44332009-09-09 15:08:12 +00001003
Chris Lattner9c85ba32008-11-10 06:08:34 +00001004 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001005 uint64_t Size = 0;
1006 unsigned Align = 0;
1007 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001008 Size = CGM.getContext().getTypeSize(Ty);
1009 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Devang Patelca80a5f2009-10-20 19:55:01 +00001012 llvm::DIType DbgTy =
1013 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001014 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001015 Size, Align, 0, 0,
1016 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001017 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001018}
1019
1020llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1021 llvm::DICompileUnit Unit) {
1022 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1023 return CreateType(RT, Unit);
1024 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1025 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001026
Chris Lattner9c85ba32008-11-10 06:08:34 +00001027 return llvm::DIType();
1028}
1029
1030llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1031 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001032 uint64_t Size;
1033 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001034
1035
Nuno Lopes010d5142009-01-28 00:35:17 +00001036 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001037 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001038 Size = 0;
1039 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001040 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001041 } else if (Ty->isIncompleteArrayType()) {
1042 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001043 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001044 } else {
1045 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001046 Size = CGM.getContext().getTypeSize(Ty);
1047 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001048 }
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Chris Lattner9c85ba32008-11-10 06:08:34 +00001050 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1051 // interior arrays, do we care? Why aren't nested arrays represented the
1052 // obvious/recursive way?
1053 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1054 QualType EltTy(Ty, 0);
1055 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001056 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001057 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001058 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001059 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060 // FIXME: Verify this is right for VLAs.
1061 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1062 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001063 }
Mike Stump1eb44332009-09-09 15:08:12 +00001064
Chris Lattner9c85ba32008-11-10 06:08:34 +00001065 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001066 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001067
Devang Patelca80a5f2009-10-20 19:55:01 +00001068 llvm::DIType DbgTy =
1069 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1070 Unit, "", llvm::DICompileUnit(),
1071 0, Size, Align, 0, 0,
1072 getOrCreateType(EltTy, Unit),
1073 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001074 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001075}
1076
Anders Carlssona031b352009-11-06 19:19:55 +00001077llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1078 llvm::DICompileUnit Unit) {
1079 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1080 Ty, Ty->getPointeeType(), Unit);
1081}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001082
Anders Carlsson20f12a22009-12-06 18:00:51 +00001083llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1084 llvm::DICompileUnit U) {
1085 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1086 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1087
1088 if (!Ty->getPointeeType()->isFunctionType()) {
1089 // We have a data member pointer type.
1090 return PointerDiffDITy;
1091 }
1092
1093 // We have a member function pointer type. Treat it as a struct with two
1094 // ptrdiff_t members.
1095 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1096
1097 uint64_t FieldOffset = 0;
1098 llvm::DIDescriptor ElementTypes[2];
1099
1100 // FIXME: This should probably be a function type instead.
1101 ElementTypes[0] =
1102 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1103 "ptr", llvm::DICompileUnit(), 0,
1104 Info.first, Info.second, FieldOffset, 0,
1105 PointerDiffDITy);
1106 FieldOffset += Info.first;
1107
1108 ElementTypes[1] =
1109 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1110 "ptr", llvm::DICompileUnit(), 0,
1111 Info.first, Info.second, FieldOffset, 0,
1112 PointerDiffDITy);
1113
1114 llvm::DIArray Elements =
1115 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1116 llvm::array_lengthof(ElementTypes));
1117
1118 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1119 U, llvm::StringRef("test"),
1120 llvm::DICompileUnit(), 0, FieldOffset,
1121 0, 0, 0, llvm::DIType(), Elements);
1122}
1123
Douglas Gregor840943d2009-12-21 20:18:30 +00001124static QualType UnwrapTypeForDebugInfo(QualType T) {
1125 do {
1126 QualType LastT = T;
1127 switch (T->getTypeClass()) {
1128 default:
1129 return T;
1130 case Type::TemplateSpecialization:
1131 T = cast<TemplateSpecializationType>(T)->desugar();
1132 break;
1133 case Type::TypeOfExpr: {
1134 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1135 T = Ty->getUnderlyingExpr()->getType();
1136 break;
1137 }
1138 case Type::TypeOf:
1139 T = cast<TypeOfType>(T)->getUnderlyingType();
1140 break;
1141 case Type::Decltype:
1142 T = cast<DecltypeType>(T)->getUnderlyingType();
1143 break;
1144 case Type::QualifiedName:
1145 T = cast<QualifiedNameType>(T)->getNamedType();
1146 break;
1147 case Type::SubstTemplateTypeParm:
1148 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1149 break;
1150 case Type::Elaborated:
1151 T = cast<ElaboratedType>(T)->getUnderlyingType();
1152 break;
1153 }
1154
1155 assert(T != LastT && "Type unwrapping failed to unwrap!");
1156 if (T == LastT)
1157 return T;
1158 } while (true);
1159
1160 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001161}
1162
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001163/// getOrCreateType - Get the type from the cache or create a new
1164/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001165llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1166 llvm::DICompileUnit Unit) {
1167 if (Ty.isNull())
1168 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001169
Douglas Gregor840943d2009-12-21 20:18:30 +00001170 // Unwrap the type as needed for debug information.
1171 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001172
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001173 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001174 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001175 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001176 if (it != TypeCache.end()) {
1177 // Verify that the debug info still exists.
1178 if (&*it->second)
1179 return llvm::DIType(cast<llvm::MDNode>(it->second));
1180 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001181
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001182 // Otherwise create the type.
1183 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001184
1185 // And update the type cache.
1186 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001187 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001188}
1189
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001190/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001191llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1192 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001193 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001194 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001195 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001196
Douglas Gregor2101a822009-12-21 19:57:21 +00001197 const char *Diag = 0;
1198
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001199 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001200 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001201#define TYPE(Class, Base)
1202#define ABSTRACT_TYPE(Class, Base)
1203#define NON_CANONICAL_TYPE(Class, Base)
1204#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1205#include "clang/AST/TypeNodes.def"
1206 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001207
Anders Carlssonbfe69952009-11-06 18:24:04 +00001208 // FIXME: Handle these.
1209 case Type::ExtVector:
1210 case Type::Vector:
1211 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001212
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001213 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001214 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001215 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001216 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1217 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1218 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1219 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001220 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001221 return CreateType(cast<BlockPointerType>(Ty), Unit);
1222 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001223 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001224 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001225 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001226 case Type::FunctionProto:
1227 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001228 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001229 case Type::ConstantArray:
1230 case Type::VariableArray:
1231 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001232 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001233
1234 case Type::LValueReference:
1235 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1236
Anders Carlsson20f12a22009-12-06 18:00:51 +00001237 case Type::MemberPointer:
1238 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001239
1240 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001241 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001242 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001243 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001244 case Type::TypeOfExpr:
1245 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001246 case Type::Decltype:
1247 llvm_unreachable("type should have been unwrapped!");
1248 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001249
1250 case Type::RValueReference:
1251 // FIXME: Implement!
1252 Diag = "rvalue references";
1253 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001254 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001255
1256 assert(Diag && "Fall through without a diagnostic?");
1257 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1258 "debug information for %0 is not yet supported");
1259 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1260 << Diag;
1261 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001262}
1263
1264/// EmitFunctionStart - Constructs the debug code for entering a function -
1265/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001266void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001267 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001268 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001269
Devang Patel9c6c3a02010-01-14 00:36:21 +00001270 llvm::StringRef Name;
1271 llvm::StringRef LinkageName;
1272
1273 const Decl *D = GD.getDecl();
1274 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001275 // If there is a DISubprogram for this function available then use it.
1276 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1277 FI = SPCache.find(FD);
1278 if (FI != SPCache.end()) {
1279 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1280 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1281 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001282 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001283 return;
1284 }
1285 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001286 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001287 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001288 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001289 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001290 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001291 } else {
1292 // Use llvm function name as linkage name.
1293 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001294 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001295 if (!Name.empty() && Name[0] == '\01')
1296 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001297 }
Mike Stump1eb44332009-09-09 15:08:12 +00001298
Devang Patel98a200b2010-01-14 18:06:13 +00001299 // It is expected that CurLoc is set before using EmitFunctionStart.
1300 // Usually, CurLoc points to the left bracket location of compound
1301 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001302 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001303 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001304 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001305
Chris Lattner9c85ba32008-11-10 06:08:34 +00001306 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001307 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001308 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001309 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001310
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001311 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001312 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001313 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001314}
1315
1316
Chris Lattner9c85ba32008-11-10 06:08:34 +00001317void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001318 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001319
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001320 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001321 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001322 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001323 || (SM.getInstantiationLineNumber(CurLoc) ==
1324 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001325 && SM.isFromSameFile(CurLoc, PrevLoc)))
1326 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001327
1328 // Update last state.
1329 PrevLoc = CurLoc;
1330
1331 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001332 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001333 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001334
Devang Patel8fae0602009-11-13 19:10:24 +00001335 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001336 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1337 llvm::DILocation DO(NULL);
1338 llvm::DILocation DL =
1339 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1340 DS, DO);
1341 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001342}
1343
1344/// EmitRegionStart- Constructs the debug code for entering a declarative
1345/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001346void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001347 llvm::DIDescriptor D =
1348 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1349 llvm::DIDescriptor() :
1350 llvm::DIDescriptor(RegionStack.back()));
1351 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001352}
1353
1354/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1355/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001356void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001357 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1358
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001359 // Provide an region stop point.
1360 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001361
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001362 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001363}
1364
Devang Patel809b9bb2010-02-10 18:49:08 +00001365// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1366// See BuildByRefType.
1367llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1368 uint64_t *XOffset) {
1369
1370 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1371
1372 QualType FType;
1373 uint64_t FieldSize, FieldOffset;
1374 unsigned FieldAlign;
1375
1376 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
1377 QualType Type = VD->getType();
1378
1379 FieldOffset = 0;
1380 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1381 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1382 FieldSize = CGM.getContext().getTypeSize(FType);
1383 FieldAlign = CGM.getContext().getTypeAlign(FType);
1384 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1385 "__isa", llvm::DICompileUnit(),
1386 0, FieldSize, FieldAlign,
1387 FieldOffset, 0, FieldTy);
1388 EltTys.push_back(FieldTy);
1389 FieldOffset += FieldSize;
1390
1391 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1392 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,
1396 "__forwarding", llvm::DICompileUnit(),
1397 0, FieldSize, FieldAlign,
1398 FieldOffset, 0, FieldTy);
1399 EltTys.push_back(FieldTy);
1400 FieldOffset += FieldSize;
1401
1402 FType = CGM.getContext().IntTy;
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,
1407 "__flags", llvm::DICompileUnit(),
1408 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,
1418 "__size", llvm::DICompileUnit(),
1419 0, FieldSize, FieldAlign,
1420 FieldOffset, 0, FieldTy);
1421 EltTys.push_back(FieldTy);
1422 FieldOffset += FieldSize;
1423
1424 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1425 if (HasCopyAndDispose) {
1426 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1427 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1428 FieldSize = CGM.getContext().getTypeSize(FType);
1429 FieldAlign = CGM.getContext().getTypeAlign(FType);
1430 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1431 "__copy_helper",
1432 llvm::DICompileUnit(),
1433 0, FieldSize, FieldAlign,
1434 FieldOffset, 0, FieldTy);
1435 EltTys.push_back(FieldTy);
1436 FieldOffset += FieldSize;
1437
1438 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
1439 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1440 FieldSize = CGM.getContext().getTypeSize(FType);
1441 FieldAlign = CGM.getContext().getTypeAlign(FType);
1442 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1443 "__destroy_helper",
1444 llvm::DICompileUnit(),
1445 0, FieldSize, FieldAlign,
1446 FieldOffset, 0, FieldTy);
1447 EltTys.push_back(FieldTy);
1448 FieldOffset += FieldSize;
1449 }
1450
1451 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1452 if (Align > CharUnits::fromQuantity(
1453 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1454 unsigned AlignedOffsetInBytes
1455 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1456 unsigned NumPaddingBytes
1457 = AlignedOffsetInBytes - FieldOffset/8;
1458
1459 if (NumPaddingBytes > 0) {
1460 llvm::APInt pad(32, NumPaddingBytes);
1461 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1462 pad, ArrayType::Normal, 0);
1463 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1464 FieldSize = CGM.getContext().getTypeSize(FType);
1465 FieldAlign = CGM.getContext().getTypeAlign(FType);
1466 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1467 Unit, "", llvm::DICompileUnit(),
1468 0, FieldSize, FieldAlign,
1469 FieldOffset, 0, FieldTy);
1470 EltTys.push_back(FieldTy);
1471 FieldOffset += FieldSize;
1472 }
1473 }
1474
1475 FType = Type;
1476 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1477 FieldSize = CGM.getContext().getTypeSize(FType);
1478 FieldAlign = Align.getQuantity()*8;
1479
1480 *XOffset = FieldOffset;
1481 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1482 VD->getName(), llvm::DICompileUnit(),
1483 0, FieldSize, FieldAlign,
1484 FieldOffset, 0, FieldTy);
1485 EltTys.push_back(FieldTy);
1486 FieldOffset += FieldSize;
1487
1488 llvm::DIArray Elements =
1489 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1490
1491 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1492
1493 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1494 Unit, "",
1495 llvm::DICompileUnit(),
1496 0, FieldOffset, 0, 0, Flags,
1497 llvm::DIType(), Elements);
1498
1499}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001500/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001501void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001502 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001503 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1504
Devang Patel07739032009-03-27 23:16:32 +00001505 // Do not emit variable debug information while generating optimized code.
1506 // The llvm optimizer and code generator are not yet ready to support
1507 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001508 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001509 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001510 return;
1511
Devang Patel239cec62010-02-01 21:39:52 +00001512 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001513 llvm::DIType Ty;
1514 uint64_t XOffset = 0;
1515 if (VD->hasAttr<BlocksAttr>())
1516 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1517 else
1518 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001519
Chris Lattner9c85ba32008-11-10 06:08:34 +00001520 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001521 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel239cec62010-02-01 21:39:52 +00001522 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001523 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001524 unsigned Column = 0;
Devang Pateld263e7b2010-02-10 01:09:50 +00001525 if (PLoc.isInvalid())
1526 PLoc = SM.getPresumedLoc(CurLoc);
1527 if (PLoc.isValid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001528 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001529 Column = PLoc.getColumn();
Devang Pateld263e7b2010-02-10 01:09:50 +00001530 Unit = getOrCreateCompileUnit(CurLoc);
Eli Friedman1468ac72009-11-16 20:33:31 +00001531 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001532 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001533 }
Mike Stump1eb44332009-09-09 15:08:12 +00001534
Chris Lattner9c85ba32008-11-10 06:08:34 +00001535 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001536 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001537 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001538 VD->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001539 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001540 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001541 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001542 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001543
Devang Patel8fae0602009-11-13 19:10:24 +00001544 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001545 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001546 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1547
Chris Lattner23e92c02009-12-28 23:41:39 +00001548 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001549}
1550
Mike Stumpb1a6e682009-09-30 02:43:10 +00001551/// EmitDeclare - Emit local variable declaration debug info.
1552void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1553 llvm::Value *Storage, CGBuilderTy &Builder,
1554 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001555 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001556 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1557
1558 // Do not emit variable debug information while generating optimized code.
1559 // The llvm optimizer and code generator are not yet ready to support
1560 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001561 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001562 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001563 return;
1564
1565 uint64_t XOffset = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001566 llvm::DICompileUnit Unit = getOrCreateCompileUnit(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001567 llvm::DIType Ty;
1568 if (VD->hasAttr<BlocksAttr>())
1569 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1570 else
1571 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001572
1573 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001574 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001575 PresumedLoc PLoc = SM.getPresumedLoc(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001576 unsigned Line = 0;
1577 if (!PLoc.isInvalid())
1578 Line = PLoc.getLine();
1579 else
1580 Unit = llvm::DICompileUnit();
1581
Devang Pateld6c5a262010-02-01 21:52:22 +00001582 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001583 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001584 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1585 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1586 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1587 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001588 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001589 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1590 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001591 // offset of __forwarding field
1592 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001593 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1594 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1595 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001596 // offset of x field
1597 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001598 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001599 }
1600
1601 // Create the descriptor for the variable.
1602 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001603 DebugFactory.CreateComplexVariable(Tag,
1604 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001605 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001606 addr);
1607 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001608 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001609 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001610
Devang Patel8fae0602009-11-13 19:10:24 +00001611 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001612 llvm::DILocation DO(NULL);
1613 llvm::DILocation DL =
1614 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001615
Chris Lattner23e92c02009-12-28 23:41:39 +00001616 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001617}
1618
Devang Pateld6c5a262010-02-01 21:52:22 +00001619void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001620 llvm::Value *Storage,
1621 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001622 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001623}
1624
Mike Stumpb1a6e682009-09-30 02:43:10 +00001625void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1626 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1627 CodeGenFunction *CGF) {
1628 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1629}
1630
Chris Lattner9c85ba32008-11-10 06:08:34 +00001631/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1632/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001633void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001634 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001635 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001636}
1637
1638
1639
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001640/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001641void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001642 const VarDecl *D) {
1643
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001644 // Create global variable debug descriptor.
Devang Pateleb6d79b2010-02-01 21:34:11 +00001645 llvm::DICompileUnit Unit = getOrCreateCompileUnit(D->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001646 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001647 PresumedLoc PLoc = SM.getPresumedLoc(D->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001648 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001649
Devang Pateleb6d79b2010-02-01 21:34:11 +00001650 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001651 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001652
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001653 // CodeGen turns int[] into int[1] so we'll do the same here.
1654 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001655
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001656 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001657 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001658
Anders Carlsson20f12a22009-12-06 18:00:51 +00001659 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001660 ArrayType::Normal, 0);
1661 }
Devang Pateleb6d79b2010-02-01 21:34:11 +00001662 llvm::StringRef DeclName = D->getName();
1663 llvm::DIDescriptor DContext =
1664 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
1665 DebugFactory.CreateGlobalVariable(DContext, DeclName,
Devang Patel33583052010-01-28 23:15:27 +00001666 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001667 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001668 Var->hasInternalLinkage(),
1669 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001670}
1671
Devang Patel9ca36b62009-02-26 21:10:26 +00001672/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001673void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001674 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001675 // Create global variable debug descriptor.
Devang Pateld6c5a262010-02-01 21:52:22 +00001676 llvm::DICompileUnit Unit = getOrCreateCompileUnit(ID->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001677 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld6c5a262010-02-01 21:52:22 +00001678 PresumedLoc PLoc = SM.getPresumedLoc(ID->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +00001679 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001680
Devang Pateld6c5a262010-02-01 21:52:22 +00001681 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001682
Devang Pateld6c5a262010-02-01 21:52:22 +00001683 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001684 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001685
Devang Patel9ca36b62009-02-26 21:10:26 +00001686 // CodeGen turns int[] into int[1] so we'll do the same here.
1687 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001688
Devang Patel9ca36b62009-02-26 21:10:26 +00001689 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001690 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001691
Anders Carlsson20f12a22009-12-06 18:00:51 +00001692 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001693 ArrayType::Normal, 0);
1694 }
1695
Devang Patelf6a39b72009-10-20 18:26:30 +00001696 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001697 getOrCreateType(T, Unit),
1698 Var->hasInternalLinkage(),
1699 true/*definition*/, Var);
1700}
Devang Patelabb485f2010-02-01 19:16:32 +00001701
1702/// getOrCreateNamesSpace - Return namespace descriptor for the given
1703/// namespace decl.
1704llvm::DINameSpace
1705CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1706 llvm::DIDescriptor Unit) {
1707 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1708 NameSpaceCache.find(NSDecl);
1709 if (I != NameSpaceCache.end())
1710 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1711
1712 SourceManager &SM = CGM.getContext().getSourceManager();
1713 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1714 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1715
1716 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001717 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001718 llvm::DINameSpace NS =
1719 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1720 llvm::DICompileUnit(Unit.getNode()), LineNo);
1721 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1722 return NS;
1723}