blob: b0f2c0fa4298eabc5bd7ddbefd08359f1a980fc9 [file] [log] [blame]
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruthbc55fe22009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson3efc6e62009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump31f099c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000041}
42
Chris Lattneraffb3732008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000045}
46
Chris Lattneraffb3732008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson3efc6e62009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000050}
51
Devang Patel7bfc5962010-01-28 23:15:27 +000052/// getContextDescriptor - Get context info for the decl.
Devang Patel92e25412010-01-29 18:11:03 +000053llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *D,
Devang Patel7bfc5962010-01-28 23:15:27 +000054 llvm::DIDescriptor &CompileUnit) {
Devang Patel92e25412010-01-29 18:11:03 +000055 if (const Decl *Parent = dyn_cast<Decl>(D->getDeclContext())) {
56 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
57 I = RegionMap.find(Parent);
58 if (I != RegionMap.end())
59 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patelfaf7e9a2009-10-06 00:35:31 +000060 }
61 return CompileUnit;
62}
63
Devang Patel934661e2010-01-14 00:36:21 +000064/// getFunctionName - Get function name for the given FunctionDecl. If the
65/// name is constructred on demand (e.g. C++ destructor) then the name
66/// is stored on the side.
67llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
68 assert (FD && "Invalid FunctionDecl!");
69 IdentifierInfo *FII = FD->getIdentifier();
70 if (FII)
71 return FII->getName();
72
73 // Otherwise construct human readable name for debug info.
74 std::string NS = FD->getNameAsString();
75
76 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +000077 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000078 memcpy(StrPtr, NS.data(), NS.length());
79 return llvm::StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000080}
81
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000082/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbardec8a892008-10-24 08:38:36 +000083/// one if necessary. This returns null for invalid source locations.
Chris Lattneraffb3732008-11-10 06:08:34 +000084llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel75009452009-04-17 21:06:59 +000085 // Get source file information.
86 const char *FileName = "<unknown>";
Anders Carlsson3efc6e62009-12-06 18:00:51 +000087 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattner96adcd52009-04-19 06:50:29 +000088 unsigned FID = 0;
Daniel Dunbar56493b02009-01-22 00:09:25 +000089 if (Loc.isValid()) {
Devang Patel75009452009-04-17 21:06:59 +000090 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
91 FileName = PLoc.getFilename();
92 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar56493b02009-01-22 00:09:25 +000093 }
Mike Stump11289f42009-09-09 15:08:12 +000094
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000095 // See if this compile unit has been used before.
Devang Patel75009452009-04-17 21:06:59 +000096 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattneraffb3732008-11-10 06:08:34 +000097 if (!Unit.isNull()) return Unit;
Daniel Dunbar3b358a32009-04-08 05:11:16 +000098
Devang Patel75009452009-04-17 21:06:59 +000099 // Get absolute path name.
100 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer1d205642009-12-08 11:02:29 +0000101 AbsFileName.makeAbsolute();
Devang Patel75009452009-04-17 21:06:59 +0000102
Devang Patel0d425352009-06-26 18:32:22 +0000103 // See if thie compile unit is representing main source file. Each source
104 // file has corresponding compile unit. There is only one main source
105 // file at a time.
106 bool isMain = false;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000107 const LangOptions &LO = CGM.getLangOptions();
108 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel0d425352009-06-26 18:32:22 +0000109 if (isMainCompileUnitCreated == false) {
Daniel Dunbar9eac0652009-11-29 02:38:34 +0000110 if (!CGO.MainFileName.empty()) {
111 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel0d425352009-06-26 18:32:22 +0000112 isMain = true;
113 } else {
114 if (Loc.isValid() && SM.isFromMainFile(Loc))
115 isMain = true;
116 }
117 if (isMain)
118 isMainCompileUnitCreated = true;
Devang Patel75009452009-04-17 21:06:59 +0000119 }
Daniel Dunbar3b358a32009-04-08 05:11:16 +0000120
Chris Lattner8c37df42009-03-25 03:28:08 +0000121 unsigned LangTag;
122 if (LO.CPlusPlus) {
123 if (LO.ObjC1)
124 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
125 else
126 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
127 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000128 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000129 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000130 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000131 } else {
132 LangTag = llvm::dwarf::DW_LANG_C89;
133 }
Devang Patel75009452009-04-17 21:06:59 +0000134
Benjamin Kramer1d205642009-12-08 11:02:29 +0000135 const char *Producer =
Mike Stumpfc8ff632009-10-09 18:38:12 +0000136#ifdef CLANG_VENDOR
137 CLANG_VENDOR
138#endif
139 "clang " CLANG_VERSION_STRING;
Chris Lattner5912de12009-05-02 01:00:04 +0000140
141 // Figure out which version of the ObjC runtime we have.
142 unsigned RuntimeVers = 0;
143 if (LO.ObjC1)
144 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000145
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000146 // Create new compile unit.
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000147 return Unit = DebugFactory.CreateCompileUnit(
148 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
149 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000150}
151
Devang Patel410dc002009-02-25 01:36:11 +0000152/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000153/// one if necessary.
154llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel410dc002009-02-25 01:36:11 +0000155 llvm::DICompileUnit Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000156 unsigned Encoding = 0;
157 switch (BT->getKind()) {
158 default:
159 case BuiltinType::Void:
160 return llvm::DIType();
161 case BuiltinType::UChar:
162 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
163 case BuiltinType::Char_S:
164 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
165 case BuiltinType::UShort:
166 case BuiltinType::UInt:
167 case BuiltinType::ULong:
168 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
169 case BuiltinType::Short:
170 case BuiltinType::Int:
171 case BuiltinType::Long:
172 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
173 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
174 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000175 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000176 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000177 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000178 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000179 uint64_t Size = CGM.getContext().getTypeSize(BT);
180 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattneraffb3732008-11-10 06:08:34 +0000181 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000182
Devang Patele21912d2009-10-20 19:55:01 +0000183 llvm::DIType DbgTy =
184 DebugFactory.CreateBasicType(Unit,
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000185 BT->getName(CGM.getContext().getLangOptions()),
Devang Patele21912d2009-10-20 19:55:01 +0000186 Unit, 0, Size, Align,
187 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000188 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000189}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000190
Chris Lattner7b0344f2009-04-23 06:13:01 +0000191llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
192 llvm::DICompileUnit Unit) {
193 // Bit size, align and offset of the type.
194 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
195 if (Ty->isComplexIntegerType())
196 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000197
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000198 uint64_t Size = CGM.getContext().getTypeSize(Ty);
199 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattner7b0344f2009-04-23 06:13:01 +0000200 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000201
Devang Patele21912d2009-10-20 19:55:01 +0000202 llvm::DIType DbgTy =
203 DebugFactory.CreateBasicType(Unit, "complex",
204 Unit, 0, Size, Align,
205 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000206 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000207}
208
John McCall0cf15512009-09-25 01:40:47 +0000209/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000210/// a new one if necessary.
John McCall0cf15512009-09-25 01:40:47 +0000211llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
212 QualifierCollector Qc;
213 const Type *T = Qc.strip(Ty);
214
215 // Ignore these qualifiers for now.
216 Qc.removeObjCGCAttr();
217 Qc.removeAddressSpace();
218
Chris Lattneraffb3732008-11-10 06:08:34 +0000219 // We will create one Derived type for one qualifier and recurse to handle any
220 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000221 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000222 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000223 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000224 Qc.removeConst();
225 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000226 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000227 Qc.removeVolatile();
228 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000229 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000230 Qc.removeRestrict();
231 } else {
232 assert(Qc.empty() && "Unknown type qualifier for debug info");
233 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000234 }
Mike Stump11289f42009-09-09 15:08:12 +0000235
John McCall0cf15512009-09-25 01:40:47 +0000236 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
237
Daniel Dunbara290ded2008-10-31 03:54:29 +0000238 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
239 // CVR derived types.
Devang Patele21912d2009-10-20 19:55:01 +0000240 llvm::DIType DbgTy =
241 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
242 0, 0, 0, 0, 0, FromTy);
Devang Patele21912d2009-10-20 19:55:01 +0000243 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000244}
245
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000246llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
247 llvm::DICompileUnit Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000248 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000249 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
250 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000251 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000252}
253
Chris Lattneraffb3732008-11-10 06:08:34 +0000254llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
255 llvm::DICompileUnit Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000256 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
257 Ty->getPointeeType(), Unit);
258}
259
260llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
261 const Type *Ty,
262 QualType PointeeTy,
263 llvm::DICompileUnit Unit) {
264 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000265
Sanjiv Gupta98070572008-05-25 05:15:42 +0000266 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000267
268 // Size is always the size of a pointer. We can't use getTypeSize here
269 // because that does not return the correct value for references.
270 uint64_t Size =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000271 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
272 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000273
Devang Patele21912d2009-10-20 19:55:01 +0000274 return
Anders Carlsson443f6772009-11-06 19:19:55 +0000275 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patele21912d2009-10-20 19:55:01 +0000276 0, Size, Align, 0, 0, EltTy);
Anders Carlsson443f6772009-11-06 19:19:55 +0000277
Sanjiv Gupta98070572008-05-25 05:15:42 +0000278}
279
Mike Stump31f099c2009-05-14 02:03:51 +0000280llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
281 llvm::DICompileUnit Unit) {
282 if (BlockLiteralGenericSet)
283 return BlockLiteralGeneric;
284
285 llvm::DICompileUnit DefUnit;
286 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
287
288 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
289
290 llvm::DIType FieldTy;
291
292 QualType FType;
293 uint64_t FieldSize, FieldOffset;
294 unsigned FieldAlign;
295
296 llvm::DIArray Elements;
297 llvm::DIType EltTy, DescTy;
298
299 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000300 FType = CGM.getContext().UnsignedLongTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000301 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000302 FieldSize = CGM.getContext().getTypeSize(FType);
303 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000304 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
305 "reserved", DefUnit,
306 0, FieldSize, FieldAlign,
307 FieldOffset, 0, FieldTy);
308 EltTys.push_back(FieldTy);
309
310 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000311 FType = CGM.getContext().UnsignedLongTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000312 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000313 FieldSize = CGM.getContext().getTypeSize(FType);
314 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000315 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
316 "Size", DefUnit,
317 0, FieldSize, FieldAlign,
318 FieldOffset, 0, FieldTy);
319 EltTys.push_back(FieldTy);
320
321 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000322 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000323 EltTys.clear();
324
Mike Stump581b9ad2009-10-02 02:30:50 +0000325 unsigned Flags = llvm::DIType::FlagAppleBlock;
326
Mike Stump31f099c2009-05-14 02:03:51 +0000327 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump581b9ad2009-10-02 02:30:50 +0000328 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000329 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000330
Mike Stump31f099c2009-05-14 02:03:51 +0000331 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000332 uint64_t Size = CGM.getContext().getTypeSize(Ty);
333 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000334
Mike Stump31f099c2009-05-14 02:03:51 +0000335 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
336 Unit, "", llvm::DICompileUnit(),
337 0, Size, Align, 0, 0, EltTy);
338
339 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000340 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000341 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000342 FieldSize = CGM.getContext().getTypeSize(FType);
343 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000344 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
345 "__isa", DefUnit,
346 0, FieldSize, FieldAlign,
347 FieldOffset, 0, FieldTy);
348 EltTys.push_back(FieldTy);
349
350 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000351 FType = CGM.getContext().IntTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000352 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000353 FieldSize = CGM.getContext().getTypeSize(FType);
354 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000355 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
356 "__flags", DefUnit,
357 0, FieldSize, FieldAlign,
358 FieldOffset, 0, FieldTy);
359 EltTys.push_back(FieldTy);
360
361 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000362 FType = CGM.getContext().IntTy;
Mike Stump31f099c2009-05-14 02:03:51 +0000363 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000364 FieldSize = CGM.getContext().getTypeSize(FType);
365 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000366 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
367 "__reserved", DefUnit,
368 0, FieldSize, FieldAlign,
369 FieldOffset, 0, FieldTy);
370 EltTys.push_back(FieldTy);
371
372 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000373 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000374 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000375 FieldSize = CGM.getContext().getTypeSize(FType);
376 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump31f099c2009-05-14 02:03:51 +0000377 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
378 "__FuncPtr", DefUnit,
379 0, FieldSize, FieldAlign,
380 FieldOffset, 0, FieldTy);
381 EltTys.push_back(FieldTy);
382
383 FieldOffset += FieldSize;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000384 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000385 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000386 FieldSize = CGM.getContext().getTypeSize(Ty);
387 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump31f099c2009-05-14 02:03:51 +0000388 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
389 "__descriptor", DefUnit,
390 0, FieldSize, FieldAlign,
391 FieldOffset, 0, FieldTy);
392 EltTys.push_back(FieldTy);
393
394 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000395 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000396
397 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump440af3d2009-10-02 02:23:37 +0000398 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump31f099c2009-05-14 02:03:51 +0000399 llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000400
Mike Stump31f099c2009-05-14 02:03:51 +0000401 BlockLiteralGenericSet = true;
402 BlockLiteralGeneric
403 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
404 "", llvm::DICompileUnit(),
405 0, Size, Align, 0, 0, EltTy);
406 return BlockLiteralGeneric;
407}
408
Chris Lattneraffb3732008-11-10 06:08:34 +0000409llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
410 llvm::DICompileUnit Unit) {
411 // Typedefs are derived from some other type. If we have a typedef of a
412 // typedef, make sure to emit the whole chain.
413 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000414
Chris Lattneraffb3732008-11-10 06:08:34 +0000415 // We don't set size information, but do specify where the typedef was
416 // declared.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000417 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelbb4820d2010-01-29 22:29:31 +0000418 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000419 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta98070572008-05-25 05:15:42 +0000420
Devang Patele21912d2009-10-20 19:55:01 +0000421 llvm::DIType DbgTy =
Devang Patelbb4820d2010-01-29 22:29:31 +0000422 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
423 getContextDescriptor(Ty->getDecl(), Unit),
424 Ty->getDecl()->getName(), Unit,
425 Line, 0, 0, 0, 0, Src);
Devang Patele21912d2009-10-20 19:55:01 +0000426 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000427}
428
Chris Lattneraffb3732008-11-10 06:08:34 +0000429llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
430 llvm::DICompileUnit Unit) {
431 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000432
Chris Lattneraffb3732008-11-10 06:08:34 +0000433 // Add the result type at least.
434 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000435
Chris Lattneraffb3732008-11-10 06:08:34 +0000436 // Set up remainder of arguments if there is a prototype.
437 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000438 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000439 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
440 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
441 } else {
442 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000443 }
444
Chris Lattneraffb3732008-11-10 06:08:34 +0000445 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000446 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000447
Devang Patele21912d2009-10-20 19:55:01 +0000448 llvm::DIType DbgTy =
449 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
450 Unit, "", llvm::DICompileUnit(),
451 0, 0, 0, 0, 0,
452 llvm::DIType(), EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000453 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000454}
455
Devang Patel889ce762010-01-19 00:00:59 +0000456/// CollectRecordFields - A helper function to collect debug info for
457/// record fields. This is used while creating debug info entry for a Record.
458void CGDebugInfo::
459CollectRecordFields(const RecordDecl *Decl,
460 llvm::DICompileUnit Unit,
461 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
462 unsigned FieldNo = 0;
463 SourceManager &SM = CGM.getContext().getSourceManager();
464 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
465 for (RecordDecl::field_iterator I = Decl->field_begin(),
466 E = Decl->field_end();
467 I != E; ++I, ++FieldNo) {
468 FieldDecl *Field = *I;
469 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
470
471 llvm::StringRef FieldName = Field->getName();
472
473 // Ignore unnamed fields.
474 if (FieldName.empty())
475 continue;
476
477 // Get the location for the field.
478 SourceLocation FieldDefLoc = Field->getLocation();
479 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
480 llvm::DICompileUnit FieldDefUnit;
481 unsigned FieldLine = 0;
482
483 if (!PLoc.isInvalid()) {
484 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
485 FieldLine = PLoc.getLine();
486 }
487
488 QualType FType = Field->getType();
489 uint64_t FieldSize = 0;
490 unsigned FieldAlign = 0;
491 if (!FType->isIncompleteArrayType()) {
492
493 // Bit size, align and offset of the type.
494 FieldSize = CGM.getContext().getTypeSize(FType);
495 Expr *BitWidth = Field->getBitWidth();
496 if (BitWidth)
497 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
498
499 FieldAlign = CGM.getContext().getTypeAlign(FType);
500 }
501
502 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
503
504 // Create a DW_TAG_member node to remember the offset of this field in the
505 // struct. FIXME: This is an absolutely insane way to capture this
506 // information. When we gut debug info, this should be fixed.
507 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
508 FieldName, FieldDefUnit,
509 FieldLine, FieldSize, FieldAlign,
510 FieldOffset, 0, FieldTy);
511 EltTys.push_back(FieldTy);
512 }
513}
514
Devang Patel3d4e6d92010-01-28 00:28:01 +0000515/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
516/// function type is not updated to include implicit "this" pointer. Use this
517/// routine to get a method type which includes "this" pointer.
518llvm::DIType
519CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
520 llvm::DICompileUnit Unit) {
521 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000522
523 // Static methods do not need "this" pointer argument.
524 if (Method->isStatic())
525 return FnTy;
526
Devang Patel3d4e6d92010-01-28 00:28:01 +0000527 // Add "this" pointer.
528
529 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
530 assert (Args.getNumElements() && "Invalid number of arguments!");
531
532 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
533
534 // First element is always return type. For 'void' functions it is NULL.
535 Elts.push_back(Args.getElement(0));
536
537 // "this" pointer is always first argument.
538 ASTContext &Context = CGM.getContext();
539 QualType ThisPtr =
540 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
541 Elts.push_back(getOrCreateType(ThisPtr, Unit));
542
543 // Copy rest of the arguments.
544 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
545 Elts.push_back(Args.getElement(i));
546
547 llvm::DIArray EltTypeArray =
548 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
549
550 return
551 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
552 Unit, "", llvm::DICompileUnit(),
553 0, 0, 0, 0, 0,
554 llvm::DIType(), EltTypeArray);
555}
556
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000557/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
558/// a single member function GlobalDecl.
559llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000560CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000561 llvm::DICompileUnit Unit,
562 llvm::DICompositeType &RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000563 bool IsCtorOrDtor =
564 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
565
566 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000567 llvm::StringRef MethodLinkageName;
Devang Patel3d4e6d92010-01-28 00:28:01 +0000568 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000569
570 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
571 // make sense to give a single ctor/dtor a linkage name.
572 if (!IsCtorOrDtor)
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000573 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000574
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000575 SourceManager &SM = CGM.getContext().getSourceManager();
576
577 // Get the location for the method.
578 SourceLocation MethodDefLoc = Method->getLocation();
579 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
580 llvm::DICompileUnit MethodDefUnit;
581 unsigned MethodLine = 0;
582
583 if (!PLoc.isInvalid()) {
584 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
585 MethodLine = PLoc.getLine();
586 }
587
588 // Collect virtual method info.
589 llvm::DIType ContainingType;
590 unsigned Virtuality = 0;
591 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000592
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000593 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000594 if (Method->isPure())
595 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
596 else
597 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
598
599 // It doesn't make sense to give a virtual destructor a vtable index,
600 // since a single destructor has two entries in the vtable.
601 if (!isa<CXXDestructorDecl>(Method))
602 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000603 ContainingType = RecordTy;
604 }
605
606 llvm::DISubprogram SP =
607 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
608 MethodLinkageName,
609 MethodDefUnit, MethodLine,
610 MethodTy, /*isLocalToUnit=*/false,
611 Method->isThisDeclarationADefinition(),
612 Virtuality, VIndex, ContainingType);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000613
614 // Don't cache ctors or dtors since we have to emit multiple functions for
615 // a single ctor or dtor.
616 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
617 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000618
619 return SP;
620}
621
Devang Patel7a12ad02010-01-19 01:54:44 +0000622/// CollectCXXMemberFunctions - A helper function to collect debug info for
623/// C++ member functions.This is used while creating debug info entry for
624/// a Record.
625void CGDebugInfo::
626CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
627 llvm::DICompileUnit Unit,
628 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
629 llvm::DICompositeType &RecordTy) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000630 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
631 E = Decl->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000632 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000633
634 if (Method->isImplicit())
635 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000636
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000637 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000638 }
639}
640
Devang Patelc54353d2010-01-25 23:32:18 +0000641/// CollectCXXBases - A helper function to collect debug info for
642/// C++ base classes. This is used while creating debug info entry for
643/// a Record.
644void CGDebugInfo::
645CollectCXXBases(const CXXRecordDecl *Decl,
646 llvm::DICompileUnit Unit,
647 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
648 llvm::DICompositeType &RecordTy) {
649
Devang Patel128aa9d2010-01-28 21:54:15 +0000650 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
651 for (CXXRecordDecl::base_class_const_iterator BI = Decl->bases_begin(),
652 BE = Decl->bases_end(); BI != BE; ++BI) {
653 unsigned BFlags = 0;
654 uint64_t BaseOffset;
655
656 const CXXRecordDecl *Base =
657 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
658
659 if (BI->isVirtual()) {
660 BaseOffset = RL.getVBaseClassOffset(Base);
661 BFlags = llvm::DIType::FlagVirtual;
662 } else
663 BaseOffset = RL.getBaseClassOffset(Base);
664
665 AccessSpecifier Access = BI->getAccessSpecifier();
666 if (Access == clang::AS_private)
667 BFlags |= llvm::DIType::FlagPrivate;
668 else if (Access == clang::AS_protected)
669 BFlags |= llvm::DIType::FlagProtected;
670
671 llvm::DIType DTy =
672 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
673 RecordTy, llvm::StringRef(),
674 llvm::DICompileUnit(), 0, 0, 0,
675 BaseOffset, BFlags,
676 getOrCreateType(BI->getType(),
677 Unit));
678 EltTys.push_back(DTy);
679 }
Devang Patelc54353d2010-01-25 23:32:18 +0000680}
681
Devang Patel84033fb2010-01-28 18:11:52 +0000682/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
683llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
684 if (!VTablePtrType.isNull())
685 return VTablePtrType;
686
687 ASTContext &Context = CGM.getContext();
688
689 /* Function type */
690 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
691 STys.push_back(getOrCreateType(Context.IntTy, Unit));
692 llvm::DIArray SElements =
693 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
694 llvm::DIType SubTy =
695 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
696 Unit, "", llvm::DICompileUnit(),
697 0, 0, 0, 0, 0, llvm::DIType(), SElements);
698
699 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
700 llvm::DIType vtbl_ptr_type
701 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
702 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
703 0, Size, 0, 0, 0, SubTy);
704
705 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
706 Unit, "", llvm::DICompileUnit(),
707 0, Size, 0, 0, 0, vtbl_ptr_type);
708 return VTablePtrType;
709}
710
711/// getVtableName - Get vtable name for the given Class.
712llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *Decl) {
713 // Otherwise construct gdb compatible name name.
714 std::string Name = "_vptr$" + Decl->getNameAsString();
715
716 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000717 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000718 memcpy(StrPtr, Name.data(), Name.length());
719 return llvm::StringRef(StrPtr, Name.length());
720}
721
722
723/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
724/// debug info entry in EltTys vector.
725void CGDebugInfo::
726CollectVtableInfo(const CXXRecordDecl *Decl,
727 llvm::DICompileUnit Unit,
728 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
729 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
730
731 // If there is a primary base then it will hold vtable info.
732 if (RL.getPrimaryBase())
733 return;
734
735 // If this class is not dynamic then there is not any vtable info to collect.
736 if (!Decl->isDynamicClass())
737 return;
738
739 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
740 llvm::DIType VPTR
741 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
742 getVtableName(Decl), llvm::DICompileUnit(),
743 0, Size, 0, 0, 0,
744 getOrCreateVTablePtrType(Unit));
745 EltTys.push_back(VPTR);
746}
747
Devang Patel410dc002009-02-25 01:36:11 +0000748/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000749llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
750 llvm::DICompileUnit Unit) {
Douglas Gregore0295612008-12-11 17:59:21 +0000751 RecordDecl *Decl = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000752
Chris Lattneraffb3732008-11-10 06:08:34 +0000753 unsigned Tag;
754 if (Decl->isStruct())
755 Tag = llvm::dwarf::DW_TAG_structure_type;
756 else if (Decl->isUnion())
757 Tag = llvm::dwarf::DW_TAG_union_type;
758 else {
759 assert(Decl->isClass() && "Unknown RecordType!");
760 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000761 }
762
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000763 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000764
Chris Lattneraffb3732008-11-10 06:08:34 +0000765 // Get overall information about the record type for the debug info.
Devang Patel12f0dea2009-04-17 21:35:15 +0000766 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner448a2282009-05-05 05:16:17 +0000767 llvm::DICompileUnit DefUnit;
768 unsigned Line = 0;
769 if (!PLoc.isInvalid()) {
770 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
771 Line = PLoc.getLine();
772 }
Mike Stump11289f42009-09-09 15:08:12 +0000773
Chris Lattneraffb3732008-11-10 06:08:34 +0000774 // Records and classes and unions can all be recursive. To handle them, we
775 // first generate a debug descriptor for the struct as a forward declaration.
776 // Then (if it is a definition) we go through and get debug info for all of
777 // its members. Finally, we create a descriptor for the complete type (which
778 // may refer to the forward decl if the struct is recursive) and replace all
779 // uses of the forward declaration with the final definition.
Devang Patel3f4a77e2010-01-20 23:56:40 +0000780
781 // A Decl->getName() is not unique. However, the debug info descriptors
782 // are uniqued. The debug info descriptor describing record's context is
783 // necessary to keep two Decl's descriptor unique if their name match.
784 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
785 // use type's name in FwdDecl.
786 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel06cceef2009-07-22 18:57:00 +0000787 llvm::DICompositeType FwdDecl =
Devang Patel3f4a77e2010-01-20 23:56:40 +0000788 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000789 DefUnit, Line, 0, 0, 0, 0,
Chris Lattneraffb3732008-11-10 06:08:34 +0000790 llvm::DIType(), llvm::DIArray());
Mike Stump11289f42009-09-09 15:08:12 +0000791
Chris Lattneraffb3732008-11-10 06:08:34 +0000792 // If this is just a forward declaration, return it.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000793 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattneraffb3732008-11-10 06:08:34 +0000794 return FwdDecl;
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +0000795
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000796 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattneraffb3732008-11-10 06:08:34 +0000797 // Otherwise, insert it into the TypeCache so that recursive uses will find
798 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000799 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattneraffb3732008-11-10 06:08:34 +0000800
801 // Convert all the elements.
802 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
803
Devang Patel84033fb2010-01-28 18:11:52 +0000804 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl);
Devang Patel946edc12010-01-28 21:41:35 +0000805 if (CXXDecl) {
806 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel84033fb2010-01-28 18:11:52 +0000807 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +0000808 }
Devang Patel889ce762010-01-19 00:00:59 +0000809 CollectRecordFields(Decl, Unit, EltTys);
Devang Patelabb44132010-01-28 00:54:21 +0000810 llvm::MDNode *ContainingType = NULL;
Devang Patel84033fb2010-01-28 18:11:52 +0000811 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000812 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelabb44132010-01-28 00:54:21 +0000813
814 // A class's primary base or the class itself contains the vtable.
815 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
816 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
817 ContainingType =
818 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
819 else if (CXXDecl->isDynamicClass())
820 ContainingType = FwdDecl.getNode();
Devang Patelc54353d2010-01-25 23:32:18 +0000821 }
Mike Stump11289f42009-09-09 15:08:12 +0000822
Chris Lattneraffb3732008-11-10 06:08:34 +0000823 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000824 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000825
826 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000827 uint64_t Size = CGM.getContext().getTypeSize(Ty);
828 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000829
Devang Patel06cceef2009-07-22 18:57:00 +0000830 llvm::DICompositeType RealDecl =
Devang Patel58bf6e12009-11-25 17:37:31 +0000831 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000832 DefUnit, Line, Size, Align, 0, 0,
Devang Patelabb44132010-01-28 00:54:21 +0000833 llvm::DIType(), Elements,
834 0, ContainingType);
Chris Lattneraffb3732008-11-10 06:08:34 +0000835
836 // Now that we have a real decl for the struct, replace anything using the
837 // old decl with the new one. This will recursively update the debug info.
Eli Friedman00dbf4c2009-11-16 21:04:30 +0000838 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000839
Chris Lattneraffb3732008-11-10 06:08:34 +0000840 return RealDecl;
841}
842
Devang Patelf4c205b2009-02-26 21:10:26 +0000843/// CreateType - get objective-c interface type.
844llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
845 llvm::DICompileUnit Unit) {
846 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000847
Devang Patelf4c205b2009-02-26 21:10:26 +0000848 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000849 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patelf4c205b2009-02-26 21:10:26 +0000850
851 // Get overall information about the record type for the debug info.
Devang Patelf4c205b2009-02-26 21:10:26 +0000852 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel12f0dea2009-04-17 21:35:15 +0000853 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
854 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
855
Mike Stump11289f42009-09-09 15:08:12 +0000856
Daniel Dunbarc61d0bd2009-05-18 20:51:58 +0000857 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000858
Devang Patelf4c205b2009-02-26 21:10:26 +0000859 // To handle recursive interface, we
860 // first generate a debug descriptor for the struct as a forward declaration.
861 // Then (if it is a definition) we go through and get debug info for all of
862 // its members. Finally, we create a descriptor for the complete type (which
863 // may refer to the forward decl if the struct is recursive) and replace all
864 // uses of the forward declaration with the final definition.
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000865 llvm::DICompositeType FwdDecl =
Devang Patel58bf6e12009-11-25 17:37:31 +0000866 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patel7bdf0962009-11-12 00:51:46 +0000867 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000868 llvm::DIType(), llvm::DIArray(),
869 RuntimeLang);
Mike Stump11289f42009-09-09 15:08:12 +0000870
Devang Patelf4c205b2009-02-26 21:10:26 +0000871 // If this is just a forward declaration, return it.
872 if (Decl->isForwardDecl())
873 return FwdDecl;
874
Devang Patel10909d52009-11-16 20:09:38 +0000875 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patelf4c205b2009-02-26 21:10:26 +0000876 // Otherwise, insert it into the TypeCache so that recursive uses will find
877 // it.
Daniel Dunbar1cbaae52009-09-19 19:27:24 +0000878 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patelf4c205b2009-02-26 21:10:26 +0000879
880 // Convert all the elements.
881 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
882
Devang Patelc0f58ea2009-03-10 21:30:26 +0000883 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
884 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +0000885 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000886 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000887 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +0000888 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattnerf216fd92009-05-05 05:05:36 +0000889 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +0000890 0 /* offset */, 0, SClassTy);
891 EltTys.push_back(InhTag);
892 }
893
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000894 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patelf4c205b2009-02-26 21:10:26 +0000895
896 unsigned FieldNo = 0;
897 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
898 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
899 ObjCIvarDecl *Field = *I;
900 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
901
Devang Patel58bf6e12009-11-25 17:37:31 +0000902 llvm::StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +0000903
Devang Pateldf348f12009-04-27 22:40:36 +0000904 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +0000905 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +0000906 continue;
907
Devang Patelf4c205b2009-02-26 21:10:26 +0000908 // Get the location for the field.
909 SourceLocation FieldDefLoc = Field->getLocation();
910 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel12f0dea2009-04-17 21:35:15 +0000911 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
912 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
913
Mike Stump11289f42009-09-09 15:08:12 +0000914
Devang Patel9f804932009-03-20 18:24:39 +0000915 QualType FType = Field->getType();
916 uint64_t FieldSize = 0;
917 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +0000918
Devang Patel9f804932009-03-20 18:24:39 +0000919 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +0000920
Devang Patel9f804932009-03-20 18:24:39 +0000921 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000922 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000923 Expr *BitWidth = Field->getBitWidth();
924 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000925 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +0000926
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000927 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +0000928 }
929
Mike Stump11289f42009-09-09 15:08:12 +0000930 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
931
Devang Patelec4bad52009-03-19 00:23:53 +0000932 unsigned Flags = 0;
933 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
934 Flags = llvm::DIType::FlagProtected;
935 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
936 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +0000937
Devang Patelf4c205b2009-02-26 21:10:26 +0000938 // Create a DW_TAG_member node to remember the offset of this field in the
939 // struct. FIXME: This is an absolutely insane way to capture this
940 // information. When we gut debug info, this should be fixed.
941 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
942 FieldName, FieldDefUnit,
943 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +0000944 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +0000945 EltTys.push_back(FieldTy);
946 }
Mike Stump11289f42009-09-09 15:08:12 +0000947
Devang Patelf4c205b2009-02-26 21:10:26 +0000948 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +0000949 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +0000950
951 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000952 uint64_t Size = CGM.getContext().getTypeSize(Ty);
953 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000954
Devang Patel6a3b3fe2009-07-27 18:42:03 +0000955 llvm::DICompositeType RealDecl =
Devang Patel58bf6e12009-11-25 17:37:31 +0000956 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patel7bdf0962009-11-12 00:51:46 +0000957 Line, Size, Align, 0, 0, llvm::DIType(),
958 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +0000959
960 // Now that we have a real decl for the struct, replace anything using the
961 // old decl with the new one. This will recursively update the debug info.
Devang Patel10909d52009-11-16 20:09:38 +0000962 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +0000963
Devang Patelf4c205b2009-02-26 21:10:26 +0000964 return RealDecl;
965}
966
Chris Lattneraffb3732008-11-10 06:08:34 +0000967llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
968 llvm::DICompileUnit Unit) {
969 EnumDecl *Decl = Ty->getDecl();
970
971 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
972
973 // Create DIEnumerator elements for each enumerator.
Mike Stump11289f42009-09-09 15:08:12 +0000974 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidiscfbfe782009-06-30 02:36:12 +0000975 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor91f84212008-12-11 16:49:14 +0000976 Enum != EnumEnd; ++Enum) {
Devang Patel58bf6e12009-11-25 17:37:31 +0000977 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor91f84212008-12-11 16:49:14 +0000978 Enum->getInitVal().getZExtValue()));
Chris Lattneraffb3732008-11-10 06:08:34 +0000979 }
Mike Stump11289f42009-09-09 15:08:12 +0000980
Chris Lattneraffb3732008-11-10 06:08:34 +0000981 // Return a CompositeType for the enum itself.
982 llvm::DIArray EltArray =
Jay Foad7d0479f2009-05-21 09:52:38 +0000983 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000984
Chris Lattneraffb3732008-11-10 06:08:34 +0000985 SourceLocation DefLoc = Decl->getLocation();
986 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000987 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +0000988 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
989 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
990
Mike Stump11289f42009-09-09 15:08:12 +0000991
Chris Lattneraffb3732008-11-10 06:08:34 +0000992 // Size and align of the type.
Eli Friedman2ad7e172009-05-04 04:39:55 +0000993 uint64_t Size = 0;
994 unsigned Align = 0;
995 if (!Ty->isIncompleteType()) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000996 Size = CGM.getContext().getTypeSize(Ty);
997 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman2ad7e172009-05-04 04:39:55 +0000998 }
Mike Stump11289f42009-09-09 15:08:12 +0000999
Devang Patele21912d2009-10-20 19:55:01 +00001000 llvm::DIType DbgTy =
1001 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel58bf6e12009-11-25 17:37:31 +00001002 Unit, Decl->getName(), DefUnit, Line,
Devang Patele21912d2009-10-20 19:55:01 +00001003 Size, Align, 0, 0,
1004 llvm::DIType(), EltArray);
Devang Patele21912d2009-10-20 19:55:01 +00001005 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001006}
1007
1008llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1009 llvm::DICompileUnit Unit) {
1010 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1011 return CreateType(RT, Unit);
1012 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1013 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001014
Chris Lattneraffb3732008-11-10 06:08:34 +00001015 return llvm::DIType();
1016}
1017
1018llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1019 llvm::DICompileUnit Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001020 uint64_t Size;
1021 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001022
1023
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001024 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001025 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001026 Size = 0;
1027 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001028 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001029 } else if (Ty->isIncompleteArrayType()) {
1030 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001031 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001032 } else {
1033 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001034 Size = CGM.getContext().getTypeSize(Ty);
1035 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001036 }
Mike Stump11289f42009-09-09 15:08:12 +00001037
Chris Lattneraffb3732008-11-10 06:08:34 +00001038 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1039 // interior arrays, do we care? Why aren't nested arrays represented the
1040 // obvious/recursive way?
1041 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1042 QualType EltTy(Ty, 0);
1043 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001044 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001045 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +00001046 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +00001047 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +00001048 // FIXME: Verify this is right for VLAs.
1049 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1050 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001051 }
Mike Stump11289f42009-09-09 15:08:12 +00001052
Chris Lattneraffb3732008-11-10 06:08:34 +00001053 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +00001054 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001055
Devang Patele21912d2009-10-20 19:55:01 +00001056 llvm::DIType DbgTy =
1057 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1058 Unit, "", llvm::DICompileUnit(),
1059 0, Size, Align, 0, 0,
1060 getOrCreateType(EltTy, Unit),
1061 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001062 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001063}
1064
Anders Carlsson443f6772009-11-06 19:19:55 +00001065llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1066 llvm::DICompileUnit Unit) {
1067 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1068 Ty, Ty->getPointeeType(), Unit);
1069}
Chris Lattneraffb3732008-11-10 06:08:34 +00001070
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001071llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1072 llvm::DICompileUnit U) {
1073 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1074 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1075
1076 if (!Ty->getPointeeType()->isFunctionType()) {
1077 // We have a data member pointer type.
1078 return PointerDiffDITy;
1079 }
1080
1081 // We have a member function pointer type. Treat it as a struct with two
1082 // ptrdiff_t members.
1083 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1084
1085 uint64_t FieldOffset = 0;
1086 llvm::DIDescriptor ElementTypes[2];
1087
1088 // FIXME: This should probably be a function type instead.
1089 ElementTypes[0] =
1090 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1091 "ptr", llvm::DICompileUnit(), 0,
1092 Info.first, Info.second, FieldOffset, 0,
1093 PointerDiffDITy);
1094 FieldOffset += Info.first;
1095
1096 ElementTypes[1] =
1097 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1098 "ptr", llvm::DICompileUnit(), 0,
1099 Info.first, Info.second, FieldOffset, 0,
1100 PointerDiffDITy);
1101
1102 llvm::DIArray Elements =
1103 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1104 llvm::array_lengthof(ElementTypes));
1105
1106 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1107 U, llvm::StringRef("test"),
1108 llvm::DICompileUnit(), 0, FieldOffset,
1109 0, 0, 0, llvm::DIType(), Elements);
1110}
1111
Douglas Gregor0f139a12009-12-21 20:18:30 +00001112static QualType UnwrapTypeForDebugInfo(QualType T) {
1113 do {
1114 QualType LastT = T;
1115 switch (T->getTypeClass()) {
1116 default:
1117 return T;
1118 case Type::TemplateSpecialization:
1119 T = cast<TemplateSpecializationType>(T)->desugar();
1120 break;
1121 case Type::TypeOfExpr: {
1122 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1123 T = Ty->getUnderlyingExpr()->getType();
1124 break;
1125 }
1126 case Type::TypeOf:
1127 T = cast<TypeOfType>(T)->getUnderlyingType();
1128 break;
1129 case Type::Decltype:
1130 T = cast<DecltypeType>(T)->getUnderlyingType();
1131 break;
1132 case Type::QualifiedName:
1133 T = cast<QualifiedNameType>(T)->getNamedType();
1134 break;
1135 case Type::SubstTemplateTypeParm:
1136 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1137 break;
1138 case Type::Elaborated:
1139 T = cast<ElaboratedType>(T)->getUnderlyingType();
1140 break;
1141 }
1142
1143 assert(T != LastT && "Type unwrapping failed to unwrap!");
1144 if (T == LastT)
1145 return T;
1146 } while (true);
1147
1148 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001149}
1150
Sanjiv Gupta98070572008-05-25 05:15:42 +00001151/// getOrCreateType - Get the type from the cache or create a new
1152/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001153llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1154 llvm::DICompileUnit Unit) {
1155 if (Ty.isNull())
1156 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001157
Douglas Gregor0f139a12009-12-21 20:18:30 +00001158 // Unwrap the type as needed for debug information.
1159 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001160
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001161 // Check for existing entry.
Daniel Dunbar99961382009-09-19 20:17:48 +00001162 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001163 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001164 if (it != TypeCache.end()) {
1165 // Verify that the debug info still exists.
1166 if (&*it->second)
1167 return llvm::DIType(cast<llvm::MDNode>(it->second));
1168 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001169
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001170 // Otherwise create the type.
1171 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001172
1173 // And update the type cache.
1174 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001175 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001176}
1177
Anders Carlsson6037e782009-11-14 20:52:05 +00001178/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001179llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1180 llvm::DICompileUnit Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001181 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001182 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001183 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001184
Douglas Gregor0915b432009-12-21 19:57:21 +00001185 const char *Diag = 0;
1186
Sanjiv Gupta98070572008-05-25 05:15:42 +00001187 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001188 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001189#define TYPE(Class, Base)
1190#define ABSTRACT_TYPE(Class, Base)
1191#define NON_CANONICAL_TYPE(Class, Base)
1192#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1193#include "clang/AST/TypeNodes.def"
1194 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001195
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001196 // FIXME: Handle these.
1197 case Type::ExtVector:
1198 case Type::Vector:
1199 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001200
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001201 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001202 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001203 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001204 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1205 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1206 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1207 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001208 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001209 return CreateType(cast<BlockPointerType>(Ty), Unit);
1210 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001211 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001212 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001213 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001214 case Type::FunctionProto:
1215 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001216 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001217 case Type::ConstantArray:
1218 case Type::VariableArray:
1219 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001220 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001221
1222 case Type::LValueReference:
1223 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1224
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001225 case Type::MemberPointer:
1226 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001227
1228 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001229 case Type::Elaborated:
Douglas Gregor0915b432009-12-21 19:57:21 +00001230 case Type::QualifiedName:
Douglas Gregor0915b432009-12-21 19:57:21 +00001231 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001232 case Type::TypeOfExpr:
1233 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001234 case Type::Decltype:
1235 llvm_unreachable("type should have been unwrapped!");
1236 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001237
1238 case Type::RValueReference:
1239 // FIXME: Implement!
1240 Diag = "rvalue references";
1241 break;
Sanjiv Gupta98070572008-05-25 05:15:42 +00001242 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001243
1244 assert(Diag && "Fall through without a diagnostic?");
1245 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1246 "debug information for %0 is not yet supported");
1247 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1248 << Diag;
1249 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001250}
1251
1252/// EmitFunctionStart - Constructs the debug code for entering a function -
1253/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001254void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001255 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001256 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001257
Devang Patel934661e2010-01-14 00:36:21 +00001258 llvm::StringRef Name;
1259 llvm::StringRef LinkageName;
1260
1261 const Decl *D = GD.getDecl();
1262 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001263 // If there is a DISubprogram for this function available then use it.
1264 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1265 FI = SPCache.find(FD);
1266 if (FI != SPCache.end()) {
1267 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1268 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1269 RegionStack.push_back(SP.getNode());
Devang Patel92e25412010-01-29 18:11:03 +00001270 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel7a12ad02010-01-19 01:54:44 +00001271 return;
1272 }
1273 }
Devang Patel934661e2010-01-14 00:36:21 +00001274 Name = getFunctionName(FD);
Eli Friedman8fdc2cb2010-01-16 00:43:13 +00001275 if (!Name.empty() && Name[0] == '\01')
Devang Patel2d630102010-01-14 21:46:57 +00001276 Name = Name.substr(1);
Devang Patel934661e2010-01-14 00:36:21 +00001277 // Use mangled name as linkage name for c/c++ functions.
Devang Patel2d630102010-01-14 21:46:57 +00001278 LinkageName = CGM.getMangledName(GD);
Devang Patel934661e2010-01-14 00:36:21 +00001279 } else {
1280 // Use llvm function name as linkage name.
1281 Name = Fn->getName();
Devang Patel934661e2010-01-14 00:36:21 +00001282 LinkageName = Name;
Devang Patel757daca2010-01-19 00:25:12 +00001283 if (!Name.empty() && Name[0] == '\01')
1284 Name = Name.substr(1);
Devang Patel934661e2010-01-14 00:36:21 +00001285 }
Mike Stump11289f42009-09-09 15:08:12 +00001286
Devang Patelea84f592010-01-14 18:06:13 +00001287 // It is expected that CurLoc is set before using EmitFunctionStart.
1288 // Usually, CurLoc points to the left bracket location of compound
1289 // statement representing function body.
Chris Lattneraffb3732008-11-10 06:08:34 +00001290 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001291 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel2ac33a02009-04-08 19:47:04 +00001292 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump11289f42009-09-09 15:08:12 +00001293
Chris Lattneraffb3732008-11-10 06:08:34 +00001294 llvm::DISubprogram SP =
Devang Patel9be7b202009-07-14 21:31:22 +00001295 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stumpae2559a2009-10-23 01:52:13 +00001296 getOrCreateType(FnType, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001297 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump11289f42009-09-09 15:08:12 +00001298
Sanjiv Gupta98070572008-05-25 05:15:42 +00001299 // Push function on region stack.
Devang Patelb40f2952009-11-13 19:10:24 +00001300 RegionStack.push_back(SP.getNode());
Devang Patel92e25412010-01-29 18:11:03 +00001301 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta98070572008-05-25 05:15:42 +00001302}
1303
1304
Chris Lattneraffb3732008-11-10 06:08:34 +00001305void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001306 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001307
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001308 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001309 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00001310 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +00001311 || (SM.getInstantiationLineNumber(CurLoc) ==
1312 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001313 && SM.isFromSameFile(CurLoc, PrevLoc)))
1314 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001315
1316 // Update last state.
1317 PrevLoc = CurLoc;
1318
1319 // Get the appropriate compile unit.
Chris Lattneraffb3732008-11-10 06:08:34 +00001320 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel2ac33a02009-04-08 19:47:04 +00001321 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patel5d90d622009-10-06 18:36:08 +00001322
Devang Patelb40f2952009-11-13 19:10:24 +00001323 llvm::DIDescriptor DR(RegionStack.back());
Devang Patel5d90d622009-10-06 18:36:08 +00001324 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1325 llvm::DILocation DO(NULL);
1326 llvm::DILocation DL =
1327 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1328 DS, DO);
1329 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001330}
1331
1332/// EmitRegionStart- Constructs the debug code for entering a declarative
1333/// region - "llvm.dbg.region.start.".
Chris Lattneraffb3732008-11-10 06:08:34 +00001334void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patelb40f2952009-11-13 19:10:24 +00001335 llvm::DIDescriptor D =
1336 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1337 llvm::DIDescriptor() :
1338 llvm::DIDescriptor(RegionStack.back()));
1339 RegionStack.push_back(D.getNode());
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001340}
1341
1342/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1343/// region - "llvm.dbg.region.end."
Chris Lattneraffb3732008-11-10 06:08:34 +00001344void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001345 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1346
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001347 // Provide an region stop point.
1348 EmitStopPoint(Fn, Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001349
Sanjiv Gupta98070572008-05-25 05:15:42 +00001350 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001351}
1352
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001353/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattneraffb3732008-11-10 06:08:34 +00001354void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1355 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001356 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1357
Devang Patelafc1c1d2009-03-27 23:16:32 +00001358 // Do not emit variable debug information while generating optimized code.
1359 // The llvm optimizer and code generator are not yet ready to support
1360 // optimized code debugging.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001361 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001362 if (CGO.OptimizationLevel)
Devang Patelafc1c1d2009-03-27 23:16:32 +00001363 return;
1364
Chris Lattner362d8ae2009-05-05 04:57:08 +00001365 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump2114d7c2009-09-22 02:12:52 +00001366 QualType Type = Decl->getType();
1367 llvm::DIType Ty = getOrCreateType(Type, Unit);
1368 if (Decl->hasAttr<BlocksAttr>()) {
1369 llvm::DICompileUnit DefUnit;
1370 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1371
1372 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1373
1374 llvm::DIType FieldTy;
1375
1376 QualType FType;
1377 uint64_t FieldSize, FieldOffset;
1378 unsigned FieldAlign;
1379
1380 llvm::DIArray Elements;
1381 llvm::DIType EltTy;
1382
1383 // Build up structure for the byref. See BuildByRefType.
1384 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001385 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001386 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001387 FieldSize = CGM.getContext().getTypeSize(FType);
1388 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001389 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1390 "__isa", DefUnit,
1391 0, FieldSize, FieldAlign,
1392 FieldOffset, 0, FieldTy);
1393 EltTys.push_back(FieldTy);
1394 FieldOffset += FieldSize;
1395
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001396 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001397 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001398 FieldSize = CGM.getContext().getTypeSize(FType);
1399 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001400 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1401 "__forwarding", DefUnit,
1402 0, FieldSize, FieldAlign,
1403 FieldOffset, 0, FieldTy);
1404 EltTys.push_back(FieldTy);
1405 FieldOffset += FieldSize;
1406
Anders Carlsson88ea2452009-12-29 07:07:36 +00001407 FType = CGM.getContext().IntTy;
Mike Stump2114d7c2009-09-22 02:12:52 +00001408 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001409 FieldSize = CGM.getContext().getTypeSize(FType);
1410 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001411 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1412 "__flags", DefUnit,
1413 0, FieldSize, FieldAlign,
1414 FieldOffset, 0, FieldTy);
1415 EltTys.push_back(FieldTy);
1416 FieldOffset += FieldSize;
1417
Anders Carlsson88ea2452009-12-29 07:07:36 +00001418 FType = CGM.getContext().IntTy;
Mike Stump2114d7c2009-09-22 02:12:52 +00001419 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001420 FieldSize = CGM.getContext().getTypeSize(FType);
1421 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001422 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1423 "__size", DefUnit,
1424 0, FieldSize, FieldAlign,
1425 FieldOffset, 0, FieldTy);
1426 EltTys.push_back(FieldTy);
1427 FieldOffset += FieldSize;
1428
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001429 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump2114d7c2009-09-22 02:12:52 +00001430 if (HasCopyAndDispose) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001431 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001432 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001433 FieldSize = CGM.getContext().getTypeSize(FType);
1434 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001435 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1436 "__copy_helper", DefUnit,
1437 0, FieldSize, FieldAlign,
1438 FieldOffset, 0, FieldTy);
1439 EltTys.push_back(FieldTy);
1440 FieldOffset += FieldSize;
1441
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001442 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2114d7c2009-09-22 02:12:52 +00001443 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001444 FieldSize = CGM.getContext().getTypeSize(FType);
1445 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001446 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1447 "__destroy_helper", DefUnit,
1448 0, FieldSize, FieldAlign,
1449 FieldOffset, 0, FieldTy);
1450 EltTys.push_back(FieldTy);
1451 FieldOffset += FieldSize;
1452 }
1453
Ken Dyck160146e2010-01-27 17:10:57 +00001454 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1455 if (Align > CharUnits::fromQuantity(
1456 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump2114d7c2009-09-22 02:12:52 +00001457 unsigned AlignedOffsetInBytes
Ken Dyck160146e2010-01-27 17:10:57 +00001458 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump2114d7c2009-09-22 02:12:52 +00001459 unsigned NumPaddingBytes
Mike Stump207c680f2009-09-22 02:44:17 +00001460 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump2114d7c2009-09-22 02:12:52 +00001461
1462 if (NumPaddingBytes > 0) {
1463 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001464 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump2114d7c2009-09-22 02:12:52 +00001465 pad, ArrayType::Normal, 0);
1466 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001467 FieldSize = CGM.getContext().getTypeSize(FType);
1468 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2114d7c2009-09-22 02:12:52 +00001469 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1470 Unit, "", DefUnit,
1471 0, FieldSize, FieldAlign,
1472 FieldOffset, 0, FieldTy);
1473 EltTys.push_back(FieldTy);
1474 FieldOffset += FieldSize;
1475 }
1476 }
1477
1478 FType = Type;
1479 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001480 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck160146e2010-01-27 17:10:57 +00001481 FieldAlign = Align.getQuantity()*8;
Mike Stump2114d7c2009-09-22 02:12:52 +00001482
1483 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel58bf6e12009-11-25 17:37:31 +00001484 Decl->getName(), DefUnit,
Mike Stump2114d7c2009-09-22 02:12:52 +00001485 0, FieldSize, FieldAlign,
1486 FieldOffset, 0, FieldTy);
1487 EltTys.push_back(FieldTy);
1488 FieldOffset += FieldSize;
1489
1490 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1491
1492 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1493
1494 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1495 llvm::DICompileUnit(),
1496 0, FieldOffset, 0, 0, Flags,
1497 llvm::DIType(), Elements);
1498 }
Chris Lattner362d8ae2009-05-05 04:57:08 +00001499
Chris Lattneraffb3732008-11-10 06:08:34 +00001500 // Get location information.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001501 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001502 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner362d8ae2009-05-05 04:57:08 +00001503 unsigned Line = 0;
Eli Friedmanb05d0822009-11-16 20:33:31 +00001504 unsigned Column = 0;
1505 if (!PLoc.isInvalid()) {
Chris Lattner362d8ae2009-05-05 04:57:08 +00001506 Line = PLoc.getLine();
Eli Friedmanb05d0822009-11-16 20:33:31 +00001507 Column = PLoc.getColumn();
1508 } else {
Chris Lattner362d8ae2009-05-05 04:57:08 +00001509 Unit = llvm::DICompileUnit();
Eli Friedmanb05d0822009-11-16 20:33:31 +00001510 }
Mike Stump11289f42009-09-09 15:08:12 +00001511
Chris Lattneraffb3732008-11-10 06:08:34 +00001512 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001513 llvm::DIVariable D =
Devang Patelb40f2952009-11-13 19:10:24 +00001514 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel58bf6e12009-11-25 17:37:31 +00001515 Decl->getName(),
Chris Lattner362d8ae2009-05-05 04:57:08 +00001516 Unit, Line, Ty);
Chris Lattneraffb3732008-11-10 06:08:34 +00001517 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001518 llvm::Instruction *Call =
Devang Patelaf993bf2009-11-10 23:07:24 +00001519 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001520
Devang Patelb40f2952009-11-13 19:10:24 +00001521 llvm::DIScope DS(RegionStack.back());
Devang Patel94f798c2009-11-12 18:21:39 +00001522 llvm::DILocation DO(NULL);
Chris Lattner5e124bf2009-12-28 21:44:41 +00001523 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1524
Chris Lattner9f021fd2009-12-28 23:41:39 +00001525 Call->setMetadata("dbg", DL.getNode());
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001526}
1527
Mike Stump2e722b92009-09-30 02:43:10 +00001528/// EmitDeclare - Emit local variable declaration debug info.
1529void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1530 llvm::Value *Storage, CGBuilderTy &Builder,
1531 CodeGenFunction *CGF) {
1532 const ValueDecl *Decl = BDRE->getDecl();
1533 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1534
1535 // Do not emit variable debug information while generating optimized code.
1536 // The llvm optimizer and code generator are not yet ready to support
1537 // optimized code debugging.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001538 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruthbc55fe22009-11-12 17:24:48 +00001539 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001540 return;
1541
1542 uint64_t XOffset = 0;
1543 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1544 QualType Type = Decl->getType();
1545 llvm::DIType Ty = getOrCreateType(Type, Unit);
1546 if (Decl->hasAttr<BlocksAttr>()) {
1547 llvm::DICompileUnit DefUnit;
1548 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1549
1550 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1551
1552 llvm::DIType FieldTy;
1553
1554 QualType FType;
1555 uint64_t FieldSize, FieldOffset;
1556 unsigned FieldAlign;
1557
1558 llvm::DIArray Elements;
1559 llvm::DIType EltTy;
1560
1561 // Build up structure for the byref. See BuildByRefType.
1562 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001563 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001564 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001565 FieldSize = CGM.getContext().getTypeSize(FType);
1566 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001567 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1568 "__isa", DefUnit,
1569 0, FieldSize, FieldAlign,
1570 FieldOffset, 0, FieldTy);
1571 EltTys.push_back(FieldTy);
1572 FieldOffset += FieldSize;
1573
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001574 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001575 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001576 FieldSize = CGM.getContext().getTypeSize(FType);
1577 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001578 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1579 "__forwarding", DefUnit,
1580 0, FieldSize, FieldAlign,
1581 FieldOffset, 0, FieldTy);
1582 EltTys.push_back(FieldTy);
1583 FieldOffset += FieldSize;
1584
Anders Carlsson88ea2452009-12-29 07:07:36 +00001585 FType = CGM.getContext().IntTy;
Mike Stump2e722b92009-09-30 02:43:10 +00001586 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001587 FieldSize = CGM.getContext().getTypeSize(FType);
1588 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001589 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1590 "__flags", DefUnit,
1591 0, FieldSize, FieldAlign,
1592 FieldOffset, 0, FieldTy);
1593 EltTys.push_back(FieldTy);
1594 FieldOffset += FieldSize;
1595
Anders Carlsson88ea2452009-12-29 07:07:36 +00001596 FType = CGM.getContext().IntTy;
Mike Stump2e722b92009-09-30 02:43:10 +00001597 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001598 FieldSize = CGM.getContext().getTypeSize(FType);
1599 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001600 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1601 "__size", DefUnit,
1602 0, FieldSize, FieldAlign,
1603 FieldOffset, 0, FieldTy);
1604 EltTys.push_back(FieldTy);
1605 FieldOffset += FieldSize;
1606
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001607 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump2e722b92009-09-30 02:43:10 +00001608 if (HasCopyAndDispose) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001609 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001610 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001611 FieldSize = CGM.getContext().getTypeSize(FType);
1612 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001613 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1614 "__copy_helper", DefUnit,
1615 0, FieldSize, FieldAlign,
1616 FieldOffset, 0, FieldTy);
1617 EltTys.push_back(FieldTy);
1618 FieldOffset += FieldSize;
1619
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001620 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump2e722b92009-09-30 02:43:10 +00001621 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001622 FieldSize = CGM.getContext().getTypeSize(FType);
1623 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001624 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1625 "__destroy_helper", DefUnit,
1626 0, FieldSize, FieldAlign,
1627 FieldOffset, 0, FieldTy);
1628 EltTys.push_back(FieldTy);
1629 FieldOffset += FieldSize;
1630 }
1631
Ken Dyck160146e2010-01-27 17:10:57 +00001632 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1633 if (Align > CharUnits::fromQuantity(
1634 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump2e722b92009-09-30 02:43:10 +00001635 unsigned AlignedOffsetInBytes
Ken Dyck160146e2010-01-27 17:10:57 +00001636 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump2e722b92009-09-30 02:43:10 +00001637 unsigned NumPaddingBytes
1638 = AlignedOffsetInBytes - FieldOffset/8;
1639
1640 if (NumPaddingBytes > 0) {
1641 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001642 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump2e722b92009-09-30 02:43:10 +00001643 pad, ArrayType::Normal, 0);
1644 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001645 FieldSize = CGM.getContext().getTypeSize(FType);
1646 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump2e722b92009-09-30 02:43:10 +00001647 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1648 Unit, "", DefUnit,
1649 0, FieldSize, FieldAlign,
1650 FieldOffset, 0, FieldTy);
1651 EltTys.push_back(FieldTy);
1652 FieldOffset += FieldSize;
1653 }
1654 }
1655
1656 FType = Type;
1657 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001658 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck160146e2010-01-27 17:10:57 +00001659 FieldAlign = Align.getQuantity()*8;
Mike Stump2e722b92009-09-30 02:43:10 +00001660
1661 XOffset = FieldOffset;
1662 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel58bf6e12009-11-25 17:37:31 +00001663 Decl->getName(), DefUnit,
Mike Stump2e722b92009-09-30 02:43:10 +00001664 0, FieldSize, FieldAlign,
1665 FieldOffset, 0, FieldTy);
1666 EltTys.push_back(FieldTy);
1667 FieldOffset += FieldSize;
1668
1669 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1670
1671 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1672
1673 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1674 llvm::DICompileUnit(),
1675 0, FieldOffset, 0, 0, Flags,
1676 llvm::DIType(), Elements);
1677 }
1678
1679 // Get location information.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001680 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump2e722b92009-09-30 02:43:10 +00001681 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1682 unsigned Line = 0;
1683 if (!PLoc.isInvalid())
1684 Line = PLoc.getLine();
1685 else
1686 Unit = llvm::DICompileUnit();
1687
Ken Dyck40775002010-01-11 17:06:35 +00001688 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stump2e722b92009-09-30 02:43:10 +00001689 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattnerbf784782010-01-25 03:29:35 +00001690 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1691 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1692 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1693 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001694 if (BDRE->isByRef()) {
Chris Lattnerbf784782010-01-25 03:29:35 +00001695 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1696 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001697 // offset of __forwarding field
1698 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001699 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1700 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1701 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001702 // offset of x field
1703 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001704 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001705 }
1706
1707 // Create the descriptor for the variable.
1708 llvm::DIVariable D =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001709 DebugFactory.CreateComplexVariable(Tag,
1710 llvm::DIDescriptor(RegionStack.back()),
Devang Patel58bf6e12009-11-25 17:37:31 +00001711 Decl->getName(), Unit, Line, Ty,
Mike Stump2e722b92009-09-30 02:43:10 +00001712 addr);
1713 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001714 llvm::Instruction *Call =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001715 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001716
Devang Patelb40f2952009-11-13 19:10:24 +00001717 llvm::DIScope DS(RegionStack.back());
Devang Patel94f798c2009-11-12 18:21:39 +00001718 llvm::DILocation DO(NULL);
1719 llvm::DILocation DL =
1720 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattner5e124bf2009-12-28 21:44:41 +00001721
Chris Lattner9f021fd2009-12-28 23:41:39 +00001722 Call->setMetadata("dbg", DL.getNode());
Mike Stump2e722b92009-09-30 02:43:10 +00001723}
1724
Chris Lattneraffb3732008-11-10 06:08:34 +00001725void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1726 llvm::Value *Storage,
1727 CGBuilderTy &Builder) {
1728 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1729}
1730
Mike Stump2e722b92009-09-30 02:43:10 +00001731void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1732 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1733 CodeGenFunction *CGF) {
1734 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1735}
1736
Chris Lattneraffb3732008-11-10 06:08:34 +00001737/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1738/// variable declaration.
1739void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1740 CGBuilderTy &Builder) {
1741 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1742}
1743
1744
1745
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001746/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001747void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattneraffb3732008-11-10 06:08:34 +00001748 const VarDecl *Decl) {
Devang Patelafc1c1d2009-03-27 23:16:32 +00001749
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001750 // Create global variable debug descriptor.
Chris Lattneraffb3732008-11-10 06:08:34 +00001751 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001752 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001753 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1754 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner86d7d912008-11-24 03:54:41 +00001755
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001756 QualType T = Decl->getType();
1757 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001758
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001759 // CodeGen turns int[] into int[1] so we'll do the same here.
1760 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001761
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001762 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001763 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001764
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001765 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001766 ArrayType::Normal, 0);
1767 }
Devang Patel58bf6e12009-11-25 17:37:31 +00001768 llvm::StringRef DeclName = Decl->getName();
Devang Patel7bfc5962010-01-28 23:15:27 +00001769 DebugFactory.CreateGlobalVariable(getContextDescriptor(Decl, Unit), DeclName,
1770 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001771 getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001772 Var->hasInternalLinkage(),
1773 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001774}
1775
Devang Patelf4c205b2009-02-26 21:10:26 +00001776/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001777void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patelf4c205b2009-02-26 21:10:26 +00001778 ObjCInterfaceDecl *Decl) {
1779 // Create global variable debug descriptor.
1780 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001781 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel12f0dea2009-04-17 21:35:15 +00001782 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1783 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patelf4c205b2009-02-26 21:10:26 +00001784
Devang Patel58bf6e12009-11-25 17:37:31 +00001785 llvm::StringRef Name = Decl->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001786
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001787 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001788 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001789
Devang Patelf4c205b2009-02-26 21:10:26 +00001790 // CodeGen turns int[] into int[1] so we'll do the same here.
1791 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001792
Devang Patelf4c205b2009-02-26 21:10:26 +00001793 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001794 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001795
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001796 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001797 ArrayType::Normal, 0);
1798 }
1799
Devang Patele4f2b2a2009-10-20 18:26:30 +00001800 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001801 getOrCreateType(T, Unit),
1802 Var->hasInternalLinkage(),
1803 true/*definition*/, Var);
1804}