blob: 36b3a8390f41b11e966e4b89129726344375153f [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000019#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000020#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000021#include "clang/Basic/SourceManager.h"
22#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000023#include "clang/Basic/Version.h"
Chandler Carruth2811ccf2009-11-12 17:24:48 +000024#include "clang/CodeGen/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000025#include "llvm/Constants.h"
26#include "llvm/DerivedTypes.h"
27#include "llvm/Instructions.h"
28#include "llvm/Intrinsics.h"
29#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000030#include "llvm/ADT/StringExtras.h"
31#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000032#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000033#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000035using namespace clang;
36using namespace clang::CodeGen;
37
Anders Carlsson20f12a22009-12-06 18:00:51 +000038CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
39 : CGM(CGM), isMainCompileUnitCreated(false), DebugFactory(CGM.getModule()),
Mike Stump9bc093c2009-05-14 02:03:51 +000040 BlockLiteralGenericSet(false) {
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000041}
42
Chris Lattner9c85ba32008-11-10 06:08:34 +000043CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000044 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000045}
46
Chris Lattner9c85ba32008-11-10 06:08:34 +000047void CGDebugInfo::setLocation(SourceLocation Loc) {
48 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000049 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000050}
51
Devang Patel33583052010-01-28 23:15:27 +000052/// getContextDescriptor - Get context info for the decl.
Devang Patel3dd96a12010-01-29 18:11:03 +000053llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *D,
Devang Patel33583052010-01-28 23:15:27 +000054 llvm::DIDescriptor &CompileUnit) {
Devang Patel3dd96a12010-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 Patelabb485f2010-02-01 19:16:32 +000060 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Parent))
61 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel979ec2e2009-10-06 00:35:31 +000062 }
63 return CompileUnit;
64}
65
Devang Patel9c6c3a02010-01-14 00:36:21 +000066/// getFunctionName - Get function name for the given FunctionDecl. If the
67/// name is constructred on demand (e.g. C++ destructor) then the name
68/// is stored on the side.
69llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
70 assert (FD && "Invalid FunctionDecl!");
71 IdentifierInfo *FII = FD->getIdentifier();
72 if (FII)
73 return FII->getName();
74
75 // Otherwise construct human readable name for debug info.
76 std::string NS = FD->getNameAsString();
77
78 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000079 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000080 memcpy(StrPtr, NS.data(), NS.length());
81 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000082}
83
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000084/// getOrCreateCompileUnit - Get the compile unit from the cache or create a new
Daniel Dunbar25f51dd2008-10-24 08:38:36 +000085/// one if necessary. This returns null for invalid source locations.
Chris Lattner9c85ba32008-11-10 06:08:34 +000086llvm::DICompileUnit CGDebugInfo::getOrCreateCompileUnit(SourceLocation Loc) {
Devang Patel446c6192009-04-17 21:06:59 +000087 // Get source file information.
88 const char *FileName = "<unknown>";
Anders Carlsson20f12a22009-12-06 18:00:51 +000089 SourceManager &SM = CGM.getContext().getSourceManager();
Chris Lattneradb1a6f2009-04-19 06:50:29 +000090 unsigned FID = 0;
Daniel Dunbar831570c2009-01-22 00:09:25 +000091 if (Loc.isValid()) {
Devang Patel446c6192009-04-17 21:06:59 +000092 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
93 FileName = PLoc.getFilename();
94 FID = PLoc.getIncludeLoc().getRawEncoding();
Daniel Dunbar831570c2009-01-22 00:09:25 +000095 }
Mike Stump1eb44332009-09-09 15:08:12 +000096
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000097 // See if this compile unit has been used before.
Devang Patel446c6192009-04-17 21:06:59 +000098 llvm::DICompileUnit &Unit = CompileUnitCache[FID];
Chris Lattner9c85ba32008-11-10 06:08:34 +000099 if (!Unit.isNull()) return Unit;
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000100
Devang Patel446c6192009-04-17 21:06:59 +0000101 // Get absolute path name.
102 llvm::sys::Path AbsFileName(FileName);
Benjamin Kramer47daf682009-12-08 11:02:29 +0000103 AbsFileName.makeAbsolute();
Devang Patel446c6192009-04-17 21:06:59 +0000104
Devang Patel72240d72009-06-26 18:32:22 +0000105 // See if thie compile unit is representing main source file. Each source
106 // file has corresponding compile unit. There is only one main source
107 // file at a time.
108 bool isMain = false;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000109 const LangOptions &LO = CGM.getLangOptions();
110 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Devang Patel72240d72009-06-26 18:32:22 +0000111 if (isMainCompileUnitCreated == false) {
Daniel Dunbar7d065d02009-11-29 02:38:34 +0000112 if (!CGO.MainFileName.empty()) {
113 if (AbsFileName.getLast() == CGO.MainFileName)
Devang Patel72240d72009-06-26 18:32:22 +0000114 isMain = true;
115 } else {
116 if (Loc.isValid() && SM.isFromMainFile(Loc))
117 isMain = true;
118 }
119 if (isMain)
120 isMainCompileUnitCreated = true;
Devang Patel446c6192009-04-17 21:06:59 +0000121 }
Daniel Dunbarc9abc042009-04-08 05:11:16 +0000122
Chris Lattner515455a2009-03-25 03:28:08 +0000123 unsigned LangTag;
124 if (LO.CPlusPlus) {
125 if (LO.ObjC1)
126 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
127 else
128 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
129 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000130 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000131 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000132 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000133 } else {
134 LangTag = llvm::dwarf::DW_LANG_C89;
135 }
Devang Patel446c6192009-04-17 21:06:59 +0000136
Benjamin Kramer47daf682009-12-08 11:02:29 +0000137 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000138#ifdef CLANG_VENDOR
139 CLANG_VENDOR
140#endif
141 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000142
143 // Figure out which version of the ObjC runtime we have.
144 unsigned RuntimeVers = 0;
145 if (LO.ObjC1)
146 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000147
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000148 // Create new compile unit.
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000149 return Unit = DebugFactory.CreateCompileUnit(
150 LangTag, AbsFileName.getLast(), AbsFileName.getDirname(), Producer, isMain,
151 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000152}
153
Devang Patel65e99f22009-02-25 01:36:11 +0000154/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000155/// one if necessary.
156llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel65e99f22009-02-25 01:36:11 +0000157 llvm::DICompileUnit Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000158 unsigned Encoding = 0;
159 switch (BT->getKind()) {
160 default:
161 case BuiltinType::Void:
162 return llvm::DIType();
163 case BuiltinType::UChar:
164 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
165 case BuiltinType::Char_S:
166 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
167 case BuiltinType::UShort:
168 case BuiltinType::UInt:
169 case BuiltinType::ULong:
170 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
171 case BuiltinType::Short:
172 case BuiltinType::Int:
173 case BuiltinType::Long:
174 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
175 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
176 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000177 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000178 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000179 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000180 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000181 uint64_t Size = CGM.getContext().getTypeSize(BT);
182 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000183 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Devang Patelca80a5f2009-10-20 19:55:01 +0000185 llvm::DIType DbgTy =
186 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000187 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000188 Unit, 0, Size, Align,
189 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000190 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000191}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000192
Chris Lattnerb7003772009-04-23 06:13:01 +0000193llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
194 llvm::DICompileUnit Unit) {
195 // Bit size, align and offset of the type.
196 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
197 if (Ty->isComplexIntegerType())
198 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000199
Anders Carlsson20f12a22009-12-06 18:00:51 +0000200 uint64_t Size = CGM.getContext().getTypeSize(Ty);
201 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000202 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Devang Patelca80a5f2009-10-20 19:55:01 +0000204 llvm::DIType DbgTy =
205 DebugFactory.CreateBasicType(Unit, "complex",
206 Unit, 0, Size, Align,
207 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000208 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000209}
210
John McCalla1805292009-09-25 01:40:47 +0000211/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000212/// a new one if necessary.
John McCalla1805292009-09-25 01:40:47 +0000213llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DICompileUnit Unit) {
214 QualifierCollector Qc;
215 const Type *T = Qc.strip(Ty);
216
217 // Ignore these qualifiers for now.
218 Qc.removeObjCGCAttr();
219 Qc.removeAddressSpace();
220
Chris Lattner9c85ba32008-11-10 06:08:34 +0000221 // We will create one Derived type for one qualifier and recurse to handle any
222 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000223 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000224 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000225 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000226 Qc.removeConst();
227 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000228 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000229 Qc.removeVolatile();
230 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000231 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000232 Qc.removeRestrict();
233 } else {
234 assert(Qc.empty() && "Unknown type qualifier for debug info");
235 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000236 }
Mike Stump1eb44332009-09-09 15:08:12 +0000237
John McCalla1805292009-09-25 01:40:47 +0000238 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
239
Daniel Dunbar3845f862008-10-31 03:54:29 +0000240 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
241 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000242 llvm::DIType DbgTy =
243 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
244 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000245 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000246}
247
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000248llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
249 llvm::DICompileUnit Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000250 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000251 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
252 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000253 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000254}
255
Chris Lattner9c85ba32008-11-10 06:08:34 +0000256llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
257 llvm::DICompileUnit Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000258 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
259 Ty->getPointeeType(), Unit);
260}
261
262llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
263 const Type *Ty,
264 QualType PointeeTy,
265 llvm::DICompileUnit Unit) {
266 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000267
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000268 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000269
270 // Size is always the size of a pointer. We can't use getTypeSize here
271 // because that does not return the correct value for references.
272 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000273 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
274 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Devang Patelca80a5f2009-10-20 19:55:01 +0000276 return
Anders Carlssona031b352009-11-06 19:19:55 +0000277 DebugFactory.CreateDerivedType(Tag, Unit, "", llvm::DICompileUnit(),
Devang Patelca80a5f2009-10-20 19:55:01 +0000278 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000279
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000280}
281
Mike Stump9bc093c2009-05-14 02:03:51 +0000282llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
283 llvm::DICompileUnit Unit) {
284 if (BlockLiteralGenericSet)
285 return BlockLiteralGeneric;
286
287 llvm::DICompileUnit DefUnit;
288 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
289
290 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
291
292 llvm::DIType FieldTy;
293
294 QualType FType;
295 uint64_t FieldSize, FieldOffset;
296 unsigned FieldAlign;
297
298 llvm::DIArray Elements;
299 llvm::DIType EltTy, DescTy;
300
301 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000302 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000303 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000304 FieldSize = CGM.getContext().getTypeSize(FType);
305 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000306 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
307 "reserved", DefUnit,
308 0, FieldSize, FieldAlign,
309 FieldOffset, 0, FieldTy);
310 EltTys.push_back(FieldTy);
311
312 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000313 FType = CGM.getContext().UnsignedLongTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000314 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000315 FieldSize = CGM.getContext().getTypeSize(FType);
316 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000317 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
318 "Size", DefUnit,
319 0, FieldSize, FieldAlign,
320 FieldOffset, 0, FieldTy);
321 EltTys.push_back(FieldTy);
322
323 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000324 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000325 EltTys.clear();
326
Mike Stump3d363c52009-10-02 02:30:50 +0000327 unsigned Flags = llvm::DIType::FlagAppleBlock;
328
Mike Stump9bc093c2009-05-14 02:03:51 +0000329 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Mike Stump3d363c52009-10-02 02:30:50 +0000330 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000331 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Mike Stump9bc093c2009-05-14 02:03:51 +0000333 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000334 uint64_t Size = CGM.getContext().getTypeSize(Ty);
335 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000336
Mike Stump9bc093c2009-05-14 02:03:51 +0000337 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
338 Unit, "", llvm::DICompileUnit(),
339 0, Size, Align, 0, 0, EltTy);
340
341 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000342 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000343 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000344 FieldSize = CGM.getContext().getTypeSize(FType);
345 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000346 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
347 "__isa", DefUnit,
348 0, FieldSize, FieldAlign,
349 FieldOffset, 0, FieldTy);
350 EltTys.push_back(FieldTy);
351
352 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000353 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000354 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000355 FieldSize = CGM.getContext().getTypeSize(FType);
356 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000357 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
358 "__flags", DefUnit,
359 0, FieldSize, FieldAlign,
360 FieldOffset, 0, FieldTy);
361 EltTys.push_back(FieldTy);
362
363 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000364 FType = CGM.getContext().IntTy;
Mike Stump9bc093c2009-05-14 02:03:51 +0000365 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000366 FieldSize = CGM.getContext().getTypeSize(FType);
367 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000368 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
369 "__reserved", DefUnit,
370 0, FieldSize, FieldAlign,
371 FieldOffset, 0, FieldTy);
372 EltTys.push_back(FieldTy);
373
374 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000375 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000376 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000377 FieldSize = CGM.getContext().getTypeSize(FType);
378 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump9bc093c2009-05-14 02:03:51 +0000379 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
380 "__FuncPtr", DefUnit,
381 0, FieldSize, FieldAlign,
382 FieldOffset, 0, FieldTy);
383 EltTys.push_back(FieldTy);
384
385 FieldOffset += FieldSize;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000386 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000387 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000388 FieldSize = CGM.getContext().getTypeSize(Ty);
389 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000390 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
391 "__descriptor", DefUnit,
392 0, FieldSize, FieldAlign,
393 FieldOffset, 0, FieldTy);
394 EltTys.push_back(FieldTy);
395
396 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000397 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000398
399 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Mike Stump944e7052009-10-02 02:23:37 +0000400 DefUnit, 0, FieldOffset, 0, 0, Flags,
Mike Stump9bc093c2009-05-14 02:03:51 +0000401 llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000402
Mike Stump9bc093c2009-05-14 02:03:51 +0000403 BlockLiteralGenericSet = true;
404 BlockLiteralGeneric
405 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
406 "", llvm::DICompileUnit(),
407 0, Size, Align, 0, 0, EltTy);
408 return BlockLiteralGeneric;
409}
410
Chris Lattner9c85ba32008-11-10 06:08:34 +0000411llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
412 llvm::DICompileUnit Unit) {
413 // Typedefs are derived from some other type. If we have a typedef of a
414 // typedef, make sure to emit the whole chain.
415 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Chris Lattner9c85ba32008-11-10 06:08:34 +0000417 // We don't set size information, but do specify where the typedef was
418 // declared.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000419 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Pateld5289052010-01-29 22:29:31 +0000420 PresumedLoc PLoc = SM.getPresumedLoc(Ty->getDecl()->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000421 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000422
Devang Patelca80a5f2009-10-20 19:55:01 +0000423 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000424 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
425 getContextDescriptor(Ty->getDecl(), Unit),
426 Ty->getDecl()->getName(), Unit,
427 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000428 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000429}
430
Chris Lattner9c85ba32008-11-10 06:08:34 +0000431llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
432 llvm::DICompileUnit Unit) {
433 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000434
Chris Lattner9c85ba32008-11-10 06:08:34 +0000435 // Add the result type at least.
436 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000437
Chris Lattner9c85ba32008-11-10 06:08:34 +0000438 // Set up remainder of arguments if there is a prototype.
439 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000440 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000441 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
442 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
443 } else {
444 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000445 }
446
Chris Lattner9c85ba32008-11-10 06:08:34 +0000447 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000448 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Devang Patelca80a5f2009-10-20 19:55:01 +0000450 llvm::DIType DbgTy =
451 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
452 Unit, "", llvm::DICompileUnit(),
453 0, 0, 0, 0, 0,
454 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000455 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000456}
457
Devang Patel428deb52010-01-19 00:00:59 +0000458/// CollectRecordFields - A helper function to collect debug info for
459/// record fields. This is used while creating debug info entry for a Record.
460void CGDebugInfo::
461CollectRecordFields(const RecordDecl *Decl,
462 llvm::DICompileUnit Unit,
463 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
464 unsigned FieldNo = 0;
465 SourceManager &SM = CGM.getContext().getSourceManager();
466 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
467 for (RecordDecl::field_iterator I = Decl->field_begin(),
468 E = Decl->field_end();
469 I != E; ++I, ++FieldNo) {
470 FieldDecl *Field = *I;
471 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
472
473 llvm::StringRef FieldName = Field->getName();
474
475 // Ignore unnamed fields.
476 if (FieldName.empty())
477 continue;
478
479 // Get the location for the field.
480 SourceLocation FieldDefLoc = Field->getLocation();
481 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
482 llvm::DICompileUnit FieldDefUnit;
483 unsigned FieldLine = 0;
484
485 if (!PLoc.isInvalid()) {
486 FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
487 FieldLine = PLoc.getLine();
488 }
489
490 QualType FType = Field->getType();
491 uint64_t FieldSize = 0;
492 unsigned FieldAlign = 0;
493 if (!FType->isIncompleteArrayType()) {
494
495 // Bit size, align and offset of the type.
496 FieldSize = CGM.getContext().getTypeSize(FType);
497 Expr *BitWidth = Field->getBitWidth();
498 if (BitWidth)
499 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
500
501 FieldAlign = CGM.getContext().getTypeAlign(FType);
502 }
503
504 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
505
506 // Create a DW_TAG_member node to remember the offset of this field in the
507 // struct. FIXME: This is an absolutely insane way to capture this
508 // information. When we gut debug info, this should be fixed.
509 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
510 FieldName, FieldDefUnit,
511 FieldLine, FieldSize, FieldAlign,
512 FieldOffset, 0, FieldTy);
513 EltTys.push_back(FieldTy);
514 }
515}
516
Devang Patela6da1922010-01-28 00:28:01 +0000517/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
518/// function type is not updated to include implicit "this" pointer. Use this
519/// routine to get a method type which includes "this" pointer.
520llvm::DIType
521CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
522 llvm::DICompileUnit Unit) {
523 llvm::DIType FnTy = getOrCreateType(Method->getType(), Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000524
525 // Static methods do not need "this" pointer argument.
526 if (Method->isStatic())
527 return FnTy;
528
Devang Patela6da1922010-01-28 00:28:01 +0000529 // Add "this" pointer.
530
531 llvm::DIArray Args = llvm::DICompositeType(FnTy.getNode()).getTypeArray();
532 assert (Args.getNumElements() && "Invalid number of arguments!");
533
534 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
535
536 // First element is always return type. For 'void' functions it is NULL.
537 Elts.push_back(Args.getElement(0));
538
539 // "this" pointer is always first argument.
540 ASTContext &Context = CGM.getContext();
541 QualType ThisPtr =
542 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
543 Elts.push_back(getOrCreateType(ThisPtr, Unit));
544
545 // Copy rest of the arguments.
546 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
547 Elts.push_back(Args.getElement(i));
548
549 llvm::DIArray EltTypeArray =
550 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
551
552 return
553 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
554 Unit, "", llvm::DICompileUnit(),
555 0, 0, 0, 0, 0,
556 llvm::DIType(), EltTypeArray);
557}
558
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000559/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
560/// a single member function GlobalDecl.
561llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000562CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000563 llvm::DICompileUnit Unit,
564 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000565 bool IsCtorOrDtor =
566 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
567
568 llvm::StringRef MethodName = getFunctionName(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000569 llvm::StringRef MethodLinkageName;
Devang Patela6da1922010-01-28 00:28:01 +0000570 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000571
572 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
573 // make sense to give a single ctor/dtor a linkage name.
574 if (!IsCtorOrDtor)
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000575 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000576
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000577 SourceManager &SM = CGM.getContext().getSourceManager();
578
579 // Get the location for the method.
580 SourceLocation MethodDefLoc = Method->getLocation();
581 PresumedLoc PLoc = SM.getPresumedLoc(MethodDefLoc);
582 llvm::DICompileUnit MethodDefUnit;
583 unsigned MethodLine = 0;
584
585 if (!PLoc.isInvalid()) {
586 MethodDefUnit = getOrCreateCompileUnit(MethodDefLoc);
587 MethodLine = PLoc.getLine();
588 }
589
590 // Collect virtual method info.
591 llvm::DIType ContainingType;
592 unsigned Virtuality = 0;
593 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000594
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000595 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000596 if (Method->isPure())
597 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
598 else
599 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
600
601 // It doesn't make sense to give a virtual destructor a vtable index,
602 // since a single destructor has two entries in the vtable.
603 if (!isa<CXXDestructorDecl>(Method))
604 VIndex = CGM.getVtableInfo().getMethodVtableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000605 ContainingType = RecordTy;
606 }
607
608 llvm::DISubprogram SP =
609 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
610 MethodLinkageName,
611 MethodDefUnit, MethodLine,
612 MethodTy, /*isLocalToUnit=*/false,
613 Method->isThisDeclarationADefinition(),
614 Virtuality, VIndex, ContainingType);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000615
616 // Don't cache ctors or dtors since we have to emit multiple functions for
617 // a single ctor or dtor.
618 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
619 SPCache[Method] = llvm::WeakVH(SP.getNode());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000620
621 return SP;
622}
623
Devang Patel4125fd22010-01-19 01:54:44 +0000624/// CollectCXXMemberFunctions - A helper function to collect debug info for
625/// C++ member functions.This is used while creating debug info entry for
626/// a Record.
627void CGDebugInfo::
628CollectCXXMemberFunctions(const CXXRecordDecl *Decl,
629 llvm::DICompileUnit Unit,
630 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
631 llvm::DICompositeType &RecordTy) {
Devang Patel4125fd22010-01-19 01:54:44 +0000632 for(CXXRecordDecl::method_iterator I = Decl->method_begin(),
633 E = Decl->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000634 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000635
636 if (Method->isImplicit())
637 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000638
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000639 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000640 }
641}
642
Devang Patela245c5b2010-01-25 23:32:18 +0000643/// CollectCXXBases - A helper function to collect debug info for
644/// C++ base classes. This is used while creating debug info entry for
645/// a Record.
646void CGDebugInfo::
647CollectCXXBases(const CXXRecordDecl *Decl,
648 llvm::DICompileUnit Unit,
649 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
650 llvm::DICompositeType &RecordTy) {
651
Devang Patelca7daed2010-01-28 21:54:15 +0000652 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
653 for (CXXRecordDecl::base_class_const_iterator BI = Decl->bases_begin(),
654 BE = Decl->bases_end(); BI != BE; ++BI) {
655 unsigned BFlags = 0;
656 uint64_t BaseOffset;
657
658 const CXXRecordDecl *Base =
659 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
660
661 if (BI->isVirtual()) {
662 BaseOffset = RL.getVBaseClassOffset(Base);
663 BFlags = llvm::DIType::FlagVirtual;
664 } else
665 BaseOffset = RL.getBaseClassOffset(Base);
666
667 AccessSpecifier Access = BI->getAccessSpecifier();
668 if (Access == clang::AS_private)
669 BFlags |= llvm::DIType::FlagPrivate;
670 else if (Access == clang::AS_protected)
671 BFlags |= llvm::DIType::FlagProtected;
672
673 llvm::DIType DTy =
674 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
675 RecordTy, llvm::StringRef(),
676 llvm::DICompileUnit(), 0, 0, 0,
677 BaseOffset, BFlags,
678 getOrCreateType(BI->getType(),
679 Unit));
680 EltTys.push_back(DTy);
681 }
Devang Patela245c5b2010-01-25 23:32:18 +0000682}
683
Devang Patel4ce3f202010-01-28 18:11:52 +0000684/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
685llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DICompileUnit Unit) {
686 if (!VTablePtrType.isNull())
687 return VTablePtrType;
688
689 ASTContext &Context = CGM.getContext();
690
691 /* Function type */
692 llvm::SmallVector<llvm::DIDescriptor, 16> STys;
693 STys.push_back(getOrCreateType(Context.IntTy, Unit));
694 llvm::DIArray SElements =
695 DebugFactory.GetOrCreateArray(STys.data(), STys.size());
696 llvm::DIType SubTy =
697 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
698 Unit, "", llvm::DICompileUnit(),
699 0, 0, 0, 0, 0, llvm::DIType(), SElements);
700
701 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
702 llvm::DIType vtbl_ptr_type
703 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
704 Unit, "__vtbl_ptr_type", llvm::DICompileUnit(),
705 0, Size, 0, 0, 0, SubTy);
706
707 VTablePtrType = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
708 Unit, "", llvm::DICompileUnit(),
709 0, Size, 0, 0, 0, vtbl_ptr_type);
710 return VTablePtrType;
711}
712
713/// getVtableName - Get vtable name for the given Class.
714llvm::StringRef CGDebugInfo::getVtableName(const CXXRecordDecl *Decl) {
715 // Otherwise construct gdb compatible name name.
716 std::string Name = "_vptr$" + Decl->getNameAsString();
717
718 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000719 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000720 memcpy(StrPtr, Name.data(), Name.length());
721 return llvm::StringRef(StrPtr, Name.length());
722}
723
724
725/// CollectVtableInfo - If the C++ class has vtable info then insert appropriate
726/// debug info entry in EltTys vector.
727void CGDebugInfo::
728CollectVtableInfo(const CXXRecordDecl *Decl,
729 llvm::DICompileUnit Unit,
730 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
731 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
732
733 // If there is a primary base then it will hold vtable info.
734 if (RL.getPrimaryBase())
735 return;
736
737 // If this class is not dynamic then there is not any vtable info to collect.
738 if (!Decl->isDynamicClass())
739 return;
740
741 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
742 llvm::DIType VPTR
743 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
744 getVtableName(Decl), llvm::DICompileUnit(),
745 0, Size, 0, 0, 0,
746 getOrCreateVTablePtrType(Unit));
747 EltTys.push_back(VPTR);
748}
749
Devang Patel65e99f22009-02-25 01:36:11 +0000750/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000751llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
752 llvm::DICompileUnit Unit) {
Douglas Gregora4c46df2008-12-11 17:59:21 +0000753 RecordDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000754
Chris Lattner9c85ba32008-11-10 06:08:34 +0000755 unsigned Tag;
756 if (Decl->isStruct())
757 Tag = llvm::dwarf::DW_TAG_structure_type;
758 else if (Decl->isUnion())
759 Tag = llvm::dwarf::DW_TAG_union_type;
760 else {
761 assert(Decl->isClass() && "Unknown RecordType!");
762 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000763 }
764
Anders Carlsson20f12a22009-12-06 18:00:51 +0000765 SourceManager &SM = CGM.getContext().getSourceManager();
Sanjiv Gupta507de852008-06-09 10:47:41 +0000766
Chris Lattner9c85ba32008-11-10 06:08:34 +0000767 // Get overall information about the record type for the debug info.
Devang Patel4f6fa232009-04-17 21:35:15 +0000768 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattnerd37d9b52009-05-05 05:16:17 +0000769 llvm::DICompileUnit DefUnit;
770 unsigned Line = 0;
771 if (!PLoc.isInvalid()) {
772 DefUnit = getOrCreateCompileUnit(Decl->getLocation());
773 Line = PLoc.getLine();
774 }
Mike Stump1eb44332009-09-09 15:08:12 +0000775
Chris Lattner9c85ba32008-11-10 06:08:34 +0000776 // Records and classes and unions can all be recursive. To handle them, we
777 // first generate a debug descriptor for the struct as a forward declaration.
778 // Then (if it is a definition) we go through and get debug info for all of
779 // its members. Finally, we create a descriptor for the complete type (which
780 // may refer to the forward decl if the struct is recursive) and replace all
781 // uses of the forward declaration with the final definition.
Devang Pateld0f251b2010-01-20 23:56:40 +0000782
783 // A Decl->getName() is not unique. However, the debug info descriptors
784 // are uniqued. The debug info descriptor describing record's context is
785 // necessary to keep two Decl's descriptor unique if their name match.
786 // FIXME : Use RecordDecl's DeclContext's descriptor. As a temp. step
787 // use type's name in FwdDecl.
788 std::string STy = QualType(Ty, 0).getAsString();
Devang Patel0ce73f62009-07-22 18:57:00 +0000789 llvm::DICompositeType FwdDecl =
Devang Pateld0f251b2010-01-20 23:56:40 +0000790 DebugFactory.CreateCompositeType(Tag, Unit, STy.c_str(),
Devang Patelab71ff52009-11-12 00:51:46 +0000791 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000792 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000793
Chris Lattner9c85ba32008-11-10 06:08:34 +0000794 // If this is just a forward declaration, return it.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000795 if (!Decl->getDefinition(CGM.getContext()))
Chris Lattner9c85ba32008-11-10 06:08:34 +0000796 return FwdDecl;
Sanjiv Gupta507de852008-06-09 10:47:41 +0000797
Eli Friedman14d63652009-11-16 21:04:30 +0000798 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000799 // Otherwise, insert it into the TypeCache so that recursive uses will find
800 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000801 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Chris Lattner9c85ba32008-11-10 06:08:34 +0000802
803 // Convert all the elements.
804 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
805
Devang Patel4ce3f202010-01-28 18:11:52 +0000806 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(Decl);
Devang Patel3064afe2010-01-28 21:41:35 +0000807 if (CXXDecl) {
808 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel4ce3f202010-01-28 18:11:52 +0000809 CollectVtableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000810 }
Devang Patel428deb52010-01-19 00:00:59 +0000811 CollectRecordFields(Decl, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000812 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000813 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000814 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000815
816 // A class's primary base or the class itself contains the vtable.
817 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(Decl);
818 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
819 ContainingType =
820 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit).getNode();
821 else if (CXXDecl->isDynamicClass())
822 ContainingType = FwdDecl.getNode();
Devang Patela245c5b2010-01-25 23:32:18 +0000823 }
Mike Stump1eb44332009-09-09 15:08:12 +0000824
Chris Lattner9c85ba32008-11-10 06:08:34 +0000825 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000826 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000827
828 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000829 uint64_t Size = CGM.getContext().getTypeSize(Ty);
830 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000831
Devang Patel0ce73f62009-07-22 18:57:00 +0000832 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000833 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000834 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000835 llvm::DIType(), Elements,
836 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000837
838 // Now that we have a real decl for the struct, replace anything using the
839 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000840 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000841
Chris Lattner9c85ba32008-11-10 06:08:34 +0000842 return RealDecl;
843}
844
Devang Patel9ca36b62009-02-26 21:10:26 +0000845/// CreateType - get objective-c interface type.
846llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
847 llvm::DICompileUnit Unit) {
848 ObjCInterfaceDecl *Decl = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Devang Patel9ca36b62009-02-26 21:10:26 +0000850 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000851 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel9ca36b62009-02-26 21:10:26 +0000852
853 // Get overall information about the record type for the debug info.
Devang Patel9ca36b62009-02-26 21:10:26 +0000854 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(Decl->getLocation());
Devang Patel4f6fa232009-04-17 21:35:15 +0000855 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
856 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
857
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Daniel Dunbard86d3362009-05-18 20:51:58 +0000859 unsigned RuntimeLang = DefUnit.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000860
Devang Patel9ca36b62009-02-26 21:10:26 +0000861 // To handle recursive interface, we
862 // first generate a debug descriptor for the struct as a forward declaration.
863 // Then (if it is a definition) we go through and get debug info for all of
864 // its members. Finally, we create a descriptor for the complete type (which
865 // may refer to the forward decl if the struct is recursive) and replace all
866 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000867 llvm::DICompositeType FwdDecl =
Devang Patel73621622009-11-25 17:37:31 +0000868 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000869 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000870 llvm::DIType(), llvm::DIArray(),
871 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000872
Devang Patel9ca36b62009-02-26 21:10:26 +0000873 // If this is just a forward declaration, return it.
874 if (Decl->isForwardDecl())
875 return FwdDecl;
876
Devang Patelffffb032009-11-16 20:09:38 +0000877 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000878 // Otherwise, insert it into the TypeCache so that recursive uses will find
879 // it.
Daniel Dunbar23e81ba2009-09-19 19:27:24 +0000880 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl.getNode();
Devang Patel9ca36b62009-02-26 21:10:26 +0000881
882 // Convert all the elements.
883 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
884
Devang Patelfbe899f2009-03-10 21:30:26 +0000885 ObjCInterfaceDecl *SClass = Decl->getSuperClass();
886 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000887 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000888 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000889 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000890 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Chris Lattner9e55b8a2009-05-05 05:05:36 +0000891 Unit, "", llvm::DICompileUnit(), 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000892 0 /* offset */, 0, SClassTy);
893 EltTys.push_back(InhTag);
894 }
895
Anders Carlsson20f12a22009-12-06 18:00:51 +0000896 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000897
898 unsigned FieldNo = 0;
899 for (ObjCInterfaceDecl::ivar_iterator I = Decl->ivar_begin(),
900 E = Decl->ivar_end(); I != E; ++I, ++FieldNo) {
901 ObjCIvarDecl *Field = *I;
902 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
903
Devang Patel73621622009-11-25 17:37:31 +0000904 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +0000905
Devang Patelde135022009-04-27 22:40:36 +0000906 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +0000907 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +0000908 continue;
909
Devang Patel9ca36b62009-02-26 21:10:26 +0000910 // Get the location for the field.
911 SourceLocation FieldDefLoc = Field->getLocation();
912 llvm::DICompileUnit FieldDefUnit = getOrCreateCompileUnit(FieldDefLoc);
Devang Patel4f6fa232009-04-17 21:35:15 +0000913 PresumedLoc PLoc = SM.getPresumedLoc(FieldDefLoc);
914 unsigned FieldLine = PLoc.isInvalid() ? 0 : PLoc.getLine();
915
Mike Stump1eb44332009-09-09 15:08:12 +0000916
Devang Patel99c20eb2009-03-20 18:24:39 +0000917 QualType FType = Field->getType();
918 uint64_t FieldSize = 0;
919 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +0000920
Devang Patel99c20eb2009-03-20 18:24:39 +0000921 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +0000922
Devang Patel99c20eb2009-03-20 18:24:39 +0000923 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000924 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000925 Expr *BitWidth = Field->getBitWidth();
926 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +0000927 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +0000928
Anders Carlsson20f12a22009-12-06 18:00:51 +0000929 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +0000930 }
931
Mike Stump1eb44332009-09-09 15:08:12 +0000932 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
933
Devang Patelc20482b2009-03-19 00:23:53 +0000934 unsigned Flags = 0;
935 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
936 Flags = llvm::DIType::FlagProtected;
937 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
938 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Devang Patel9ca36b62009-02-26 21:10:26 +0000940 // Create a DW_TAG_member node to remember the offset of this field in the
941 // struct. FIXME: This is an absolutely insane way to capture this
942 // information. When we gut debug info, this should be fixed.
943 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
944 FieldName, FieldDefUnit,
945 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +0000946 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +0000947 EltTys.push_back(FieldTy);
948 }
Mike Stump1eb44332009-09-09 15:08:12 +0000949
Devang Patel9ca36b62009-02-26 21:10:26 +0000950 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000951 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +0000952
953 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000954 uint64_t Size = CGM.getContext().getTypeSize(Ty);
955 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000956
Devang Patel6c1fddf2009-07-27 18:42:03 +0000957 llvm::DICompositeType RealDecl =
Devang Patel73621622009-11-25 17:37:31 +0000958 DebugFactory.CreateCompositeType(Tag, Unit, Decl->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +0000959 Line, Size, Align, 0, 0, llvm::DIType(),
960 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +0000961
962 // Now that we have a real decl for the struct, replace anything using the
963 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +0000964 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +0000965
Devang Patel9ca36b62009-02-26 21:10:26 +0000966 return RealDecl;
967}
968
Chris Lattner9c85ba32008-11-10 06:08:34 +0000969llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
970 llvm::DICompileUnit Unit) {
971 EnumDecl *Decl = Ty->getDecl();
972
973 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
974
975 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +0000976 for (EnumDecl::enumerator_iterator
Argyrios Kyrtzidis17945a02009-06-30 02:36:12 +0000977 Enum = Decl->enumerator_begin(), EnumEnd = Decl->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +0000978 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +0000979 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +0000980 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +0000981 }
Mike Stump1eb44332009-09-09 15:08:12 +0000982
Chris Lattner9c85ba32008-11-10 06:08:34 +0000983 // Return a CompositeType for the enum itself.
984 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +0000985 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986
Chris Lattner9c85ba32008-11-10 06:08:34 +0000987 SourceLocation DefLoc = Decl->getLocation();
988 llvm::DICompileUnit DefUnit = getOrCreateCompileUnit(DefLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000989 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +0000990 PresumedLoc PLoc = SM.getPresumedLoc(DefLoc);
991 unsigned Line = PLoc.isInvalid() ? 0 : PLoc.getLine();
992
Mike Stump1eb44332009-09-09 15:08:12 +0000993
Chris Lattner9c85ba32008-11-10 06:08:34 +0000994 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +0000995 uint64_t Size = 0;
996 unsigned Align = 0;
997 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +0000998 Size = CGM.getContext().getTypeSize(Ty);
999 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001000 }
Mike Stump1eb44332009-09-09 15:08:12 +00001001
Devang Patelca80a5f2009-10-20 19:55:01 +00001002 llvm::DIType DbgTy =
1003 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Patel73621622009-11-25 17:37:31 +00001004 Unit, Decl->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001005 Size, Align, 0, 0,
1006 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001007 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001008}
1009
1010llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
1011 llvm::DICompileUnit Unit) {
1012 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1013 return CreateType(RT, Unit);
1014 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1015 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001016
Chris Lattner9c85ba32008-11-10 06:08:34 +00001017 return llvm::DIType();
1018}
1019
1020llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
1021 llvm::DICompileUnit Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001022 uint64_t Size;
1023 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001024
1025
Nuno Lopes010d5142009-01-28 00:35:17 +00001026 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001027 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001028 Size = 0;
1029 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001030 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001031 } else if (Ty->isIncompleteArrayType()) {
1032 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001033 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001034 } else {
1035 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001036 Size = CGM.getContext().getTypeSize(Ty);
1037 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001038 }
Mike Stump1eb44332009-09-09 15:08:12 +00001039
Chris Lattner9c85ba32008-11-10 06:08:34 +00001040 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1041 // interior arrays, do we care? Why aren't nested arrays represented the
1042 // obvious/recursive way?
1043 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1044 QualType EltTy(Ty, 0);
1045 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001046 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001047 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001048 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001049 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001050 // FIXME: Verify this is right for VLAs.
1051 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1052 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001053 }
Mike Stump1eb44332009-09-09 15:08:12 +00001054
Chris Lattner9c85ba32008-11-10 06:08:34 +00001055 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001056 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001057
Devang Patelca80a5f2009-10-20 19:55:01 +00001058 llvm::DIType DbgTy =
1059 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
1060 Unit, "", llvm::DICompileUnit(),
1061 0, Size, Align, 0, 0,
1062 getOrCreateType(EltTy, Unit),
1063 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001064 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001065}
1066
Anders Carlssona031b352009-11-06 19:19:55 +00001067llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
1068 llvm::DICompileUnit Unit) {
1069 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1070 Ty, Ty->getPointeeType(), Unit);
1071}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072
Anders Carlsson20f12a22009-12-06 18:00:51 +00001073llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
1074 llvm::DICompileUnit U) {
1075 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1076 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1077
1078 if (!Ty->getPointeeType()->isFunctionType()) {
1079 // We have a data member pointer type.
1080 return PointerDiffDITy;
1081 }
1082
1083 // We have a member function pointer type. Treat it as a struct with two
1084 // ptrdiff_t members.
1085 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1086
1087 uint64_t FieldOffset = 0;
1088 llvm::DIDescriptor ElementTypes[2];
1089
1090 // FIXME: This should probably be a function type instead.
1091 ElementTypes[0] =
1092 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1093 "ptr", llvm::DICompileUnit(), 0,
1094 Info.first, Info.second, FieldOffset, 0,
1095 PointerDiffDITy);
1096 FieldOffset += Info.first;
1097
1098 ElementTypes[1] =
1099 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
1100 "ptr", llvm::DICompileUnit(), 0,
1101 Info.first, Info.second, FieldOffset, 0,
1102 PointerDiffDITy);
1103
1104 llvm::DIArray Elements =
1105 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1106 llvm::array_lengthof(ElementTypes));
1107
1108 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1109 U, llvm::StringRef("test"),
1110 llvm::DICompileUnit(), 0, FieldOffset,
1111 0, 0, 0, llvm::DIType(), Elements);
1112}
1113
Douglas Gregor840943d2009-12-21 20:18:30 +00001114static QualType UnwrapTypeForDebugInfo(QualType T) {
1115 do {
1116 QualType LastT = T;
1117 switch (T->getTypeClass()) {
1118 default:
1119 return T;
1120 case Type::TemplateSpecialization:
1121 T = cast<TemplateSpecializationType>(T)->desugar();
1122 break;
1123 case Type::TypeOfExpr: {
1124 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1125 T = Ty->getUnderlyingExpr()->getType();
1126 break;
1127 }
1128 case Type::TypeOf:
1129 T = cast<TypeOfType>(T)->getUnderlyingType();
1130 break;
1131 case Type::Decltype:
1132 T = cast<DecltypeType>(T)->getUnderlyingType();
1133 break;
1134 case Type::QualifiedName:
1135 T = cast<QualifiedNameType>(T)->getNamedType();
1136 break;
1137 case Type::SubstTemplateTypeParm:
1138 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1139 break;
1140 case Type::Elaborated:
1141 T = cast<ElaboratedType>(T)->getUnderlyingType();
1142 break;
1143 }
1144
1145 assert(T != LastT && "Type unwrapping failed to unwrap!");
1146 if (T == LastT)
1147 return T;
1148 } while (true);
1149
1150 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001151}
1152
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001153/// getOrCreateType - Get the type from the cache or create a new
1154/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001155llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
1156 llvm::DICompileUnit Unit) {
1157 if (Ty.isNull())
1158 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001159
Douglas Gregor840943d2009-12-21 20:18:30 +00001160 // Unwrap the type as needed for debug information.
1161 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001162
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001163 // Check for existing entry.
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001164 std::map<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001165 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001166 if (it != TypeCache.end()) {
1167 // Verify that the debug info still exists.
1168 if (&*it->second)
1169 return llvm::DIType(cast<llvm::MDNode>(it->second));
1170 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001171
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001172 // Otherwise create the type.
1173 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001174
1175 // And update the type cache.
1176 TypeCache[Ty.getAsOpaquePtr()] = Res.getNode();
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001177 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001178}
1179
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001180/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001181llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
1182 llvm::DICompileUnit Unit) {
John McCalla1805292009-09-25 01:40:47 +00001183 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001184 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001185 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001186
Douglas Gregor2101a822009-12-21 19:57:21 +00001187 const char *Diag = 0;
1188
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001189 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001190 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001191#define TYPE(Class, Base)
1192#define ABSTRACT_TYPE(Class, Base)
1193#define NON_CANONICAL_TYPE(Class, Base)
1194#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1195#include "clang/AST/TypeNodes.def"
1196 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001197
Anders Carlssonbfe69952009-11-06 18:24:04 +00001198 // FIXME: Handle these.
1199 case Type::ExtVector:
1200 case Type::Vector:
1201 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001202
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001203 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001204 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001205 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001206 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1207 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1208 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1209 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001210 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001211 return CreateType(cast<BlockPointerType>(Ty), Unit);
1212 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001213 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001214 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001215 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001216 case Type::FunctionProto:
1217 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001218 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001219 case Type::ConstantArray:
1220 case Type::VariableArray:
1221 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001222 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001223
1224 case Type::LValueReference:
1225 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1226
Anders Carlsson20f12a22009-12-06 18:00:51 +00001227 case Type::MemberPointer:
1228 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001229
1230 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001231 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001232 case Type::QualifiedName:
Douglas Gregor2101a822009-12-21 19:57:21 +00001233 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001234 case Type::TypeOfExpr:
1235 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001236 case Type::Decltype:
1237 llvm_unreachable("type should have been unwrapped!");
1238 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001239
1240 case Type::RValueReference:
1241 // FIXME: Implement!
1242 Diag = "rvalue references";
1243 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001244 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001245
1246 assert(Diag && "Fall through without a diagnostic?");
1247 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1248 "debug information for %0 is not yet supported");
1249 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1250 << Diag;
1251 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001252}
1253
1254/// EmitFunctionStart - Constructs the debug code for entering a function -
1255/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001256void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001257 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001258 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001259
Devang Patel9c6c3a02010-01-14 00:36:21 +00001260 llvm::StringRef Name;
1261 llvm::StringRef LinkageName;
1262
1263 const Decl *D = GD.getDecl();
1264 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001265 // If there is a DISubprogram for this function available then use it.
1266 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1267 FI = SPCache.find(FD);
1268 if (FI != SPCache.end()) {
1269 llvm::DISubprogram SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
1270 if (!SP.isNull() && SP.isSubprogram() && SP.isDefinition()) {
1271 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001272 RegionMap[D] = llvm::WeakVH(SP.getNode());
Devang Patel4125fd22010-01-19 01:54:44 +00001273 return;
1274 }
1275 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001276 Name = getFunctionName(FD);
Eli Friedman3364e622010-01-16 00:43:13 +00001277 if (!Name.empty() && Name[0] == '\01')
Devang Patelaa97d702010-01-14 21:46:57 +00001278 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001279 // Use mangled name as linkage name for c/c++ functions.
Devang Patelaa97d702010-01-14 21:46:57 +00001280 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001281 } else {
1282 // Use llvm function name as linkage name.
1283 Name = Fn->getName();
Devang Patel9c6c3a02010-01-14 00:36:21 +00001284 LinkageName = Name;
Devang Patel17584202010-01-19 00:25:12 +00001285 if (!Name.empty() && Name[0] == '\01')
1286 Name = Name.substr(1);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001287 }
Mike Stump1eb44332009-09-09 15:08:12 +00001288
Devang Patel98a200b2010-01-14 18:06:13 +00001289 // It is expected that CurLoc is set before using EmitFunctionStart.
1290 // Usually, CurLoc points to the left bracket location of compound
1291 // statement representing function body.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001292 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001293 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel0f78fea2009-04-08 19:47:04 +00001294 unsigned LineNo = SM.getPresumedLoc(CurLoc).getLine();
Mike Stump1eb44332009-09-09 15:08:12 +00001295
Chris Lattner9c85ba32008-11-10 06:08:34 +00001296 llvm::DISubprogram SP =
Devang Patel6dba4322009-07-14 21:31:22 +00001297 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001298 getOrCreateType(FnType, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001299 Fn->hasInternalLinkage(), true/*definition*/);
Mike Stump1eb44332009-09-09 15:08:12 +00001300
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001301 // Push function on region stack.
Devang Patel8fae0602009-11-13 19:10:24 +00001302 RegionStack.push_back(SP.getNode());
Devang Patel3dd96a12010-01-29 18:11:03 +00001303 RegionMap[D] = llvm::WeakVH(SP.getNode());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001304}
1305
1306
Chris Lattner9c85ba32008-11-10 06:08:34 +00001307void CGDebugInfo::EmitStopPoint(llvm::Function *Fn, CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001308 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001309
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001310 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001311 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001312 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001313 || (SM.getInstantiationLineNumber(CurLoc) ==
1314 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001315 && SM.isFromSameFile(CurLoc, PrevLoc)))
1316 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001317
1318 // Update last state.
1319 PrevLoc = CurLoc;
1320
1321 // Get the appropriate compile unit.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001322 llvm::DICompileUnit Unit = getOrCreateCompileUnit(CurLoc);
Devang Patel0f78fea2009-04-08 19:47:04 +00001323 PresumedLoc PLoc = SM.getPresumedLoc(CurLoc);
Devang Patelbbd9fa42009-10-06 18:36:08 +00001324
Devang Patel8fae0602009-11-13 19:10:24 +00001325 llvm::DIDescriptor DR(RegionStack.back());
Devang Patelbbd9fa42009-10-06 18:36:08 +00001326 llvm::DIScope DS = llvm::DIScope(DR.getNode());
1327 llvm::DILocation DO(NULL);
1328 llvm::DILocation DL =
1329 DebugFactory.CreateLocation(PLoc.getLine(), PLoc.getColumn(),
1330 DS, DO);
1331 Builder.SetCurrentDebugLocation(DL.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001332}
1333
1334/// EmitRegionStart- Constructs the debug code for entering a declarative
1335/// region - "llvm.dbg.region.start.".
Chris Lattner9c85ba32008-11-10 06:08:34 +00001336void CGDebugInfo::EmitRegionStart(llvm::Function *Fn, CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001337 llvm::DIDescriptor D =
1338 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1339 llvm::DIDescriptor() :
1340 llvm::DIDescriptor(RegionStack.back()));
1341 RegionStack.push_back(D.getNode());
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001342}
1343
1344/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1345/// region - "llvm.dbg.region.end."
Chris Lattner9c85ba32008-11-10 06:08:34 +00001346void CGDebugInfo::EmitRegionEnd(llvm::Function *Fn, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001347 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1348
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001349 // Provide an region stop point.
1350 EmitStopPoint(Fn, Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001351
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001352 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001353}
1354
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001355/// EmitDeclare - Emit local variable declaration debug info.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001356void CGDebugInfo::EmitDeclare(const VarDecl *Decl, unsigned Tag,
1357 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001358 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1359
Devang Patel07739032009-03-27 23:16:32 +00001360 // Do not emit variable debug information while generating optimized code.
1361 // The llvm optimizer and code generator are not yet ready to support
1362 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001363 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001364 if (CGO.OptimizationLevel)
Devang Patel07739032009-03-27 23:16:32 +00001365 return;
1366
Chris Lattner650cea92009-05-05 04:57:08 +00001367 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Mike Stump39605b42009-09-22 02:12:52 +00001368 QualType Type = Decl->getType();
1369 llvm::DIType Ty = getOrCreateType(Type, Unit);
1370 if (Decl->hasAttr<BlocksAttr>()) {
1371 llvm::DICompileUnit DefUnit;
1372 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1373
1374 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1375
1376 llvm::DIType FieldTy;
1377
1378 QualType FType;
1379 uint64_t FieldSize, FieldOffset;
1380 unsigned FieldAlign;
1381
1382 llvm::DIArray Elements;
1383 llvm::DIType EltTy;
1384
1385 // Build up structure for the byref. See BuildByRefType.
1386 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001387 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001388 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001389 FieldSize = CGM.getContext().getTypeSize(FType);
1390 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001391 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1392 "__isa", DefUnit,
1393 0, FieldSize, FieldAlign,
1394 FieldOffset, 0, FieldTy);
1395 EltTys.push_back(FieldTy);
1396 FieldOffset += FieldSize;
1397
Anders Carlsson20f12a22009-12-06 18:00:51 +00001398 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001399 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001400 FieldSize = CGM.getContext().getTypeSize(FType);
1401 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001402 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1403 "__forwarding", DefUnit,
1404 0, FieldSize, FieldAlign,
1405 FieldOffset, 0, FieldTy);
1406 EltTys.push_back(FieldTy);
1407 FieldOffset += FieldSize;
1408
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001409 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001410 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001411 FieldSize = CGM.getContext().getTypeSize(FType);
1412 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001413 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1414 "__flags", DefUnit,
1415 0, FieldSize, FieldAlign,
1416 FieldOffset, 0, FieldTy);
1417 EltTys.push_back(FieldTy);
1418 FieldOffset += FieldSize;
1419
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001420 FType = CGM.getContext().IntTy;
Mike Stump39605b42009-09-22 02:12:52 +00001421 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001422 FieldSize = CGM.getContext().getTypeSize(FType);
1423 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001424 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1425 "__size", DefUnit,
1426 0, FieldSize, FieldAlign,
1427 FieldOffset, 0, FieldTy);
1428 EltTys.push_back(FieldTy);
1429 FieldOffset += FieldSize;
1430
Anders Carlsson20f12a22009-12-06 18:00:51 +00001431 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stump39605b42009-09-22 02:12:52 +00001432 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001433 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001434 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001435 FieldSize = CGM.getContext().getTypeSize(FType);
1436 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001437 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1438 "__copy_helper", DefUnit,
1439 0, FieldSize, FieldAlign,
1440 FieldOffset, 0, FieldTy);
1441 EltTys.push_back(FieldTy);
1442 FieldOffset += FieldSize;
1443
Anders Carlsson20f12a22009-12-06 18:00:51 +00001444 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump39605b42009-09-22 02:12:52 +00001445 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001446 FieldSize = CGM.getContext().getTypeSize(FType);
1447 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001448 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1449 "__destroy_helper", DefUnit,
1450 0, FieldSize, FieldAlign,
1451 FieldOffset, 0, FieldTy);
1452 EltTys.push_back(FieldTy);
1453 FieldOffset += FieldSize;
1454 }
1455
Ken Dyck8b752f12010-01-27 17:10:57 +00001456 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1457 if (Align > CharUnits::fromQuantity(
1458 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stump39605b42009-09-22 02:12:52 +00001459 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001460 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stump39605b42009-09-22 02:12:52 +00001461 unsigned NumPaddingBytes
Mike Stumpfd47b312009-09-22 02:44:17 +00001462 = AlignedOffsetInBytes - FieldOffset/8;
Mike Stump39605b42009-09-22 02:12:52 +00001463
1464 if (NumPaddingBytes > 0) {
1465 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001466 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stump39605b42009-09-22 02:12:52 +00001467 pad, ArrayType::Normal, 0);
1468 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001469 FieldSize = CGM.getContext().getTypeSize(FType);
1470 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stump39605b42009-09-22 02:12:52 +00001471 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1472 Unit, "", DefUnit,
1473 0, FieldSize, FieldAlign,
1474 FieldOffset, 0, FieldTy);
1475 EltTys.push_back(FieldTy);
1476 FieldOffset += FieldSize;
1477 }
1478 }
1479
1480 FType = Type;
1481 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001482 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001483 FieldAlign = Align.getQuantity()*8;
Mike Stump39605b42009-09-22 02:12:52 +00001484
1485 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001486 Decl->getName(), DefUnit,
Mike Stump39605b42009-09-22 02:12:52 +00001487 0, FieldSize, FieldAlign,
1488 FieldOffset, 0, FieldTy);
1489 EltTys.push_back(FieldTy);
1490 FieldOffset += FieldSize;
1491
1492 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1493
1494 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1495
1496 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1497 llvm::DICompileUnit(),
1498 0, FieldOffset, 0, 0, Flags,
1499 llvm::DIType(), Elements);
1500 }
Chris Lattner650cea92009-05-05 04:57:08 +00001501
Chris Lattner9c85ba32008-11-10 06:08:34 +00001502 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001503 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001504 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
Chris Lattner650cea92009-05-05 04:57:08 +00001505 unsigned Line = 0;
Eli Friedman1468ac72009-11-16 20:33:31 +00001506 unsigned Column = 0;
1507 if (!PLoc.isInvalid()) {
Chris Lattner650cea92009-05-05 04:57:08 +00001508 Line = PLoc.getLine();
Eli Friedman1468ac72009-11-16 20:33:31 +00001509 Column = PLoc.getColumn();
1510 } else {
Chris Lattner650cea92009-05-05 04:57:08 +00001511 Unit = llvm::DICompileUnit();
Eli Friedman1468ac72009-11-16 20:33:31 +00001512 }
Mike Stump1eb44332009-09-09 15:08:12 +00001513
Chris Lattner9c85ba32008-11-10 06:08:34 +00001514 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001515 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001516 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001517 Decl->getName(),
Chris Lattner650cea92009-05-05 04:57:08 +00001518 Unit, Line, Ty);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001519 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001520 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001521 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001522
Devang Patel8fae0602009-11-13 19:10:24 +00001523 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001524 llvm::DILocation DO(NULL);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001525 llvm::DILocation DL = DebugFactory.CreateLocation(Line, Column, DS, DO);
1526
Chris Lattner23e92c02009-12-28 23:41:39 +00001527 Call->setMetadata("dbg", DL.getNode());
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001528}
1529
Mike Stumpb1a6e682009-09-30 02:43:10 +00001530/// EmitDeclare - Emit local variable declaration debug info.
1531void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1532 llvm::Value *Storage, CGBuilderTy &Builder,
1533 CodeGenFunction *CGF) {
1534 const ValueDecl *Decl = BDRE->getDecl();
1535 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1536
1537 // Do not emit variable debug information while generating optimized code.
1538 // The llvm optimizer and code generator are not yet ready to support
1539 // optimized code debugging.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001540 const CodeGenOptions &CGO = CGM.getCodeGenOpts();
Chandler Carruth2811ccf2009-11-12 17:24:48 +00001541 if (CGO.OptimizationLevel || Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001542 return;
1543
1544 uint64_t XOffset = 0;
1545 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
1546 QualType Type = Decl->getType();
1547 llvm::DIType Ty = getOrCreateType(Type, Unit);
1548 if (Decl->hasAttr<BlocksAttr>()) {
1549 llvm::DICompileUnit DefUnit;
1550 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
1551
1552 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1553
1554 llvm::DIType FieldTy;
1555
1556 QualType FType;
1557 uint64_t FieldSize, FieldOffset;
1558 unsigned FieldAlign;
1559
1560 llvm::DIArray Elements;
1561 llvm::DIType EltTy;
1562
1563 // Build up structure for the byref. See BuildByRefType.
1564 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001565 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001566 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001567 FieldSize = CGM.getContext().getTypeSize(FType);
1568 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001569 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1570 "__isa", DefUnit,
1571 0, FieldSize, FieldAlign,
1572 FieldOffset, 0, FieldTy);
1573 EltTys.push_back(FieldTy);
1574 FieldOffset += FieldSize;
1575
Anders Carlsson20f12a22009-12-06 18:00:51 +00001576 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001577 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001578 FieldSize = CGM.getContext().getTypeSize(FType);
1579 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001580 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1581 "__forwarding", DefUnit,
1582 0, FieldSize, FieldAlign,
1583 FieldOffset, 0, FieldTy);
1584 EltTys.push_back(FieldTy);
1585 FieldOffset += FieldSize;
1586
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001587 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001588 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001589 FieldSize = CGM.getContext().getTypeSize(FType);
1590 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001591 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1592 "__flags", DefUnit,
1593 0, FieldSize, FieldAlign,
1594 FieldOffset, 0, FieldTy);
1595 EltTys.push_back(FieldTy);
1596 FieldOffset += FieldSize;
1597
Anders Carlssonf5f7d862009-12-29 07:07:36 +00001598 FType = CGM.getContext().IntTy;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001599 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001600 FieldSize = CGM.getContext().getTypeSize(FType);
1601 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001602 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1603 "__size", DefUnit,
1604 0, FieldSize, FieldAlign,
1605 FieldOffset, 0, FieldTy);
1606 EltTys.push_back(FieldTy);
1607 FieldOffset += FieldSize;
1608
Anders Carlsson20f12a22009-12-06 18:00:51 +00001609 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001610 if (HasCopyAndDispose) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001611 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001612 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001613 FieldSize = CGM.getContext().getTypeSize(FType);
1614 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001615 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1616 "__copy_helper", DefUnit,
1617 0, FieldSize, FieldAlign,
1618 FieldOffset, 0, FieldTy);
1619 EltTys.push_back(FieldTy);
1620 FieldOffset += FieldSize;
1621
Anders Carlsson20f12a22009-12-06 18:00:51 +00001622 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001623 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001624 FieldSize = CGM.getContext().getTypeSize(FType);
1625 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001626 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1627 "__destroy_helper", DefUnit,
1628 0, FieldSize, FieldAlign,
1629 FieldOffset, 0, FieldTy);
1630 EltTys.push_back(FieldTy);
1631 FieldOffset += FieldSize;
1632 }
1633
Ken Dyck8b752f12010-01-27 17:10:57 +00001634 CharUnits Align = CGM.getContext().getDeclAlign(Decl);
1635 if (Align > CharUnits::fromQuantity(
1636 CGM.getContext().Target.getPointerAlign(0) / 8)) {
Mike Stumpb1a6e682009-09-30 02:43:10 +00001637 unsigned AlignedOffsetInBytes
Ken Dyck8b752f12010-01-27 17:10:57 +00001638 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001639 unsigned NumPaddingBytes
1640 = AlignedOffsetInBytes - FieldOffset/8;
1641
1642 if (NumPaddingBytes > 0) {
1643 llvm::APInt pad(32, NumPaddingBytes);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001644 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001645 pad, ArrayType::Normal, 0);
1646 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001647 FieldSize = CGM.getContext().getTypeSize(FType);
1648 FieldAlign = CGM.getContext().getTypeAlign(FType);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001649 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1650 Unit, "", DefUnit,
1651 0, FieldSize, FieldAlign,
1652 FieldOffset, 0, FieldTy);
1653 EltTys.push_back(FieldTy);
1654 FieldOffset += FieldSize;
1655 }
1656 }
1657
1658 FType = Type;
1659 FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Anders Carlsson20f12a22009-12-06 18:00:51 +00001660 FieldSize = CGM.getContext().getTypeSize(FType);
Ken Dyck8b752f12010-01-27 17:10:57 +00001661 FieldAlign = Align.getQuantity()*8;
Mike Stumpb1a6e682009-09-30 02:43:10 +00001662
1663 XOffset = FieldOffset;
1664 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel73621622009-11-25 17:37:31 +00001665 Decl->getName(), DefUnit,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001666 0, FieldSize, FieldAlign,
1667 FieldOffset, 0, FieldTy);
1668 EltTys.push_back(FieldTy);
1669 FieldOffset += FieldSize;
1670
1671 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1672
1673 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1674
1675 Ty = DebugFactory.CreateCompositeType(Tag, Unit, "",
1676 llvm::DICompileUnit(),
1677 0, FieldOffset, 0, 0, Flags,
1678 llvm::DIType(), Elements);
1679 }
1680
1681 // Get location information.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001682 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001683 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1684 unsigned Line = 0;
1685 if (!PLoc.isInvalid())
1686 Line = PLoc.getLine();
1687 else
1688 Unit = llvm::DICompileUnit();
1689
Ken Dyck199c3d62010-01-11 17:06:35 +00001690 CharUnits offset = CGF->BlockDecls[Decl];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001691 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001692 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1693 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1694 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1695 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001696 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001697 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1698 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001699 // offset of __forwarding field
1700 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001701 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1702 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1703 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001704 // offset of x field
1705 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001706 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001707 }
1708
1709 // Create the descriptor for the variable.
1710 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001711 DebugFactory.CreateComplexVariable(Tag,
1712 llvm::DIDescriptor(RegionStack.back()),
Devang Patel73621622009-11-25 17:37:31 +00001713 Decl->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001714 addr);
1715 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001716 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001717 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001718
Devang Patel8fae0602009-11-13 19:10:24 +00001719 llvm::DIScope DS(RegionStack.back());
Devang Patel23908b82009-11-12 18:21:39 +00001720 llvm::DILocation DO(NULL);
1721 llvm::DILocation DL =
1722 DebugFactory.CreateLocation(Line, PLoc.getColumn(), DS, DO);
Chris Lattnerd5b89022009-12-28 21:44:41 +00001723
Chris Lattner23e92c02009-12-28 23:41:39 +00001724 Call->setMetadata("dbg", DL.getNode());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001725}
1726
Chris Lattner9c85ba32008-11-10 06:08:34 +00001727void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *Decl,
1728 llvm::Value *Storage,
1729 CGBuilderTy &Builder) {
1730 EmitDeclare(Decl, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
1731}
1732
Mike Stumpb1a6e682009-09-30 02:43:10 +00001733void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1734 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1735 CodeGenFunction *CGF) {
1736 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1737}
1738
Chris Lattner9c85ba32008-11-10 06:08:34 +00001739/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1740/// variable declaration.
1741void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *Decl, llvm::Value *AI,
1742 CGBuilderTy &Builder) {
1743 EmitDeclare(Decl, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
1744}
1745
1746
1747
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001748/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001749void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001750 const VarDecl *Decl) {
Devang Patel07739032009-03-27 23:16:32 +00001751
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001752 // Create global variable debug descriptor.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001753 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001754 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001755 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1756 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Chris Lattner8ec03f52008-11-24 03:54:41 +00001757
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001758 QualType T = Decl->getType();
1759 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001761 // CodeGen turns int[] into int[1] so we'll do the same here.
1762 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001763
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001764 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001765 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001766
Anders Carlsson20f12a22009-12-06 18:00:51 +00001767 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001768 ArrayType::Normal, 0);
1769 }
Devang Patel73621622009-11-25 17:37:31 +00001770 llvm::StringRef DeclName = Decl->getName();
Devang Patel33583052010-01-28 23:15:27 +00001771 DebugFactory.CreateGlobalVariable(getContextDescriptor(Decl, Unit), DeclName,
1772 DeclName, llvm::StringRef(), Unit, LineNo,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001773 getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001774 Var->hasInternalLinkage(),
1775 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001776}
1777
Devang Patel9ca36b62009-02-26 21:10:26 +00001778/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001779void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel9ca36b62009-02-26 21:10:26 +00001780 ObjCInterfaceDecl *Decl) {
1781 // Create global variable debug descriptor.
1782 llvm::DICompileUnit Unit = getOrCreateCompileUnit(Decl->getLocation());
Anders Carlsson20f12a22009-12-06 18:00:51 +00001783 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel4f6fa232009-04-17 21:35:15 +00001784 PresumedLoc PLoc = SM.getPresumedLoc(Decl->getLocation());
1785 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
Devang Patel9ca36b62009-02-26 21:10:26 +00001786
Devang Patel73621622009-11-25 17:37:31 +00001787 llvm::StringRef Name = Decl->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001788
Anders Carlsson20f12a22009-12-06 18:00:51 +00001789 QualType T = CGM.getContext().getObjCInterfaceType(Decl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001790 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001791
Devang Patel9ca36b62009-02-26 21:10:26 +00001792 // CodeGen turns int[] into int[1] so we'll do the same here.
1793 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001794
Devang Patel9ca36b62009-02-26 21:10:26 +00001795 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001796 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001797
Anders Carlsson20f12a22009-12-06 18:00:51 +00001798 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001799 ArrayType::Normal, 0);
1800 }
1801
Devang Patelf6a39b72009-10-20 18:26:30 +00001802 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001803 getOrCreateType(T, Unit),
1804 Var->hasInternalLinkage(),
1805 true/*definition*/, Var);
1806}
Devang Patelabb485f2010-02-01 19:16:32 +00001807
1808/// getOrCreateNamesSpace - Return namespace descriptor for the given
1809/// namespace decl.
1810llvm::DINameSpace
1811CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1812 llvm::DIDescriptor Unit) {
1813 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1814 NameSpaceCache.find(NSDecl);
1815 if (I != NameSpaceCache.end())
1816 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1817
1818 SourceManager &SM = CGM.getContext().getSourceManager();
1819 PresumedLoc PLoc = SM.getPresumedLoc(NSDecl->getLocation());
1820 unsigned LineNo = PLoc.isInvalid() ? 0 : PLoc.getLine();
1821
1822 llvm::DIDescriptor Context =
1823 getContextDescriptor(NSDecl, Unit);
1824 llvm::DINameSpace NS =
1825 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
1826 llvm::DICompileUnit(Unit.getNode()), LineNo);
1827 NameSpaceCache[NSDecl] = llvm::WeakVH(NS.getNode());
1828 return NS;
1829}