blob: 4873587a41c6aba68d1796052321c78951d3a081 [file] [log] [blame]
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Devang Patel6c018202010-07-20 20:24:18 +000019#include "clang/AST/DeclTemplate.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000020#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000022#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000024#include "clang/Basic/Version.h"
Chandler Carruth85098242010-06-15 23:19:56 +000025#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/Module.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000033#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000034#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000035#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000036using namespace clang;
37using namespace clang::CodeGen;
38
Anders Carlsson3efc6e62009-12-06 18:00:51 +000039CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel408dcf62010-03-09 00:44:50 +000040 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman196f7102010-08-20 22:02:57 +000041 BlockLiteralGenericSet(false) {
Devang Patel408dcf62010-03-09 00:44:50 +000042 CreateCompileUnit();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000043}
44
Chris Lattneraffb3732008-11-10 06:08:34 +000045CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000046 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000047}
48
Chris Lattneraffb3732008-11-10 06:08:34 +000049void CGDebugInfo::setLocation(SourceLocation Loc) {
50 if (Loc.isValid())
Anders Carlsson3efc6e62009-12-06 18:00:51 +000051 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000052}
53
Devang Patel7bfc5962010-01-28 23:15:27 +000054/// getContextDescriptor - Get context info for the decl.
Devang Patel7b7f46f2010-02-01 21:34:11 +000055llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel7bfc5962010-01-28 23:15:27 +000056 llvm::DIDescriptor &CompileUnit) {
Devang Patel7b7f46f2010-02-01 21:34:11 +000057 if (!Context)
58 return CompileUnit;
59
60 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
61 I = RegionMap.find(Context);
62 if (I != RegionMap.end())
63 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patele8fb4b72010-02-01 22:40:08 +000064
Devang Patel7b7f46f2010-02-01 21:34:11 +000065 // Check namespace.
66 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
67 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel98f21712010-05-13 23:52:37 +000068
69 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
70 if (!RDecl->isDependentType()) {
71 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
72 llvm::DIFile(CompileUnit));
73 return llvm::DIDescriptor(Ty);
74 }
75 }
Devang Patelfaf7e9a2009-10-06 00:35:31 +000076 return CompileUnit;
77}
78
Devang Patel934661e2010-01-14 00:36:21 +000079/// getFunctionName - Get function name for the given FunctionDecl. If the
80/// name is constructred on demand (e.g. C++ destructor) then the name
81/// is stored on the side.
82llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
83 assert (FD && "Invalid FunctionDecl!");
84 IdentifierInfo *FII = FD->getIdentifier();
85 if (FII)
86 return FII->getName();
87
88 // Otherwise construct human readable name for debug info.
89 std::string NS = FD->getNameAsString();
90
91 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +000092 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000093 memcpy(StrPtr, NS.data(), NS.length());
94 return llvm::StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000095}
96
Devang Patel6c018202010-07-20 20:24:18 +000097/// getClassName - Get class name including template argument list.
98llvm::StringRef
99CGDebugInfo::getClassName(RecordDecl *RD) {
100 ClassTemplateSpecializationDecl *Spec
101 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
102 if (!Spec)
103 return RD->getName();
104
105 const TemplateArgument *Args;
106 unsigned NumArgs;
107 std::string Buffer;
108 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
109 const TemplateSpecializationType *TST =
110 cast<TemplateSpecializationType>(TAW->getType());
111 Args = TST->getArgs();
112 NumArgs = TST->getNumArgs();
113 } else {
114 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
115 Args = TemplateArgs.getFlatArgumentList();
116 NumArgs = TemplateArgs.flat_size();
117 }
118 Buffer = RD->getIdentifier()->getNameStart();
119 PrintingPolicy Policy(CGM.getLangOptions());
120 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
121 NumArgs,
122 Policy);
123
124 // Copy this name on the side and use its reference.
125 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
126 memcpy(StrPtr, Buffer.data(), Buffer.length());
127 return llvm::StringRef(StrPtr, Buffer.length());
128
129}
130
Devang Patel408dcf62010-03-09 00:44:50 +0000131/// getOrCreateFile - Get the file debug info descriptor for the input location.
132llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000133 if (!Loc.isValid())
Devang Patel408dcf62010-03-09 00:44:50 +0000134 // If Location is not valid then use main input file.
135 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
136 TheCU);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000137 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +0000138 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000139
140 // Cache the results.
141 const char *fname = PLoc.getFilename();
142 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
143 DIFileCache.find(fname);
144
145 if (it != DIFileCache.end()) {
146 // Verify that the information still exists.
147 if (&*it->second)
148 return llvm::DIFile(cast<llvm::MDNode>(it->second));
149 }
150
Devang Patel6014edd2010-07-27 15:17:16 +0000151 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
152 getCurrentDirname(), TheCU);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000153
Devang Patelba4ad7f2010-05-07 18:12:35 +0000154 DIFileCache[fname] = F;
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000155 return F;
156
Devang Patel408dcf62010-03-09 00:44:50 +0000157}
Devang Patelc5ffabc2010-05-12 23:46:38 +0000158
159/// getLineNumber - Get line number for the location. If location is invalid
160/// then use current location.
161unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
162 assert (CurLoc.isValid() && "Invalid current location!");
163 SourceManager &SM = CGM.getContext().getSourceManager();
164 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
165 return PLoc.getLine();
166}
167
168/// getColumnNumber - Get column number for the location. If location is
169/// invalid then use current location.
170unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
171 assert (CurLoc.isValid() && "Invalid current location!");
172 SourceManager &SM = CGM.getContext().getSourceManager();
173 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
174 return PLoc.getColumn();
175}
176
Devang Patel6014edd2010-07-27 15:17:16 +0000177llvm::StringRef CGDebugInfo::getCurrentDirname() {
178 if (!CWDName.empty())
179 return CWDName;
180 char *CompDirnamePtr = NULL;
181 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
182 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
183 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
184 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
185}
186
Devang Patel408dcf62010-03-09 00:44:50 +0000187/// CreateCompileUnit - Create new compile unit.
188void CGDebugInfo::CreateCompileUnit() {
189
190 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000191 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000192 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
193 if (MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000194 MainFileName = "<unknown>";
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000195
Douglas Gregor65f7a3f2010-03-22 21:28:29 +0000196 // The main file name provided via the "-main-file-name" option contains just
197 // the file name itself with no path information. This file name may have had
198 // a relative path, so we look into the actual file entry for the main
199 // file to determine the real absolute path for the file.
Devang Patelcb9fe9e2010-07-23 23:04:28 +0000200 std::string MainFileDir;
Devang Patel6014edd2010-07-27 15:17:16 +0000201 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000202 MainFileDir = MainFile->getDir()->getName();
Devang Patel6014edd2010-07-27 15:17:16 +0000203 if (MainFileDir != ".")
204 MainFileName = MainFileDir + "/" + MainFileName;
205 }
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000206
Devang Patel6014edd2010-07-27 15:17:16 +0000207 // Save filename string.
208 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
209 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
210 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
211
Chris Lattner8c37df42009-03-25 03:28:08 +0000212 unsigned LangTag;
Devang Patel408dcf62010-03-09 00:44:50 +0000213 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner8c37df42009-03-25 03:28:08 +0000214 if (LO.CPlusPlus) {
215 if (LO.ObjC1)
216 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
217 else
218 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
219 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000220 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000221 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000222 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000223 } else {
224 LangTag = llvm::dwarf::DW_LANG_C89;
225 }
Devang Patel75009452009-04-17 21:06:59 +0000226
Daniel Dunbar64c222a2010-08-24 17:41:09 +0000227 std::string Producer = getClangFullVersion();
Chris Lattner5912de12009-05-02 01:00:04 +0000228
229 // Figure out which version of the ObjC runtime we have.
230 unsigned RuntimeVers = 0;
231 if (LO.ObjC1)
232 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000233
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000234 // Create new compile unit.
Devang Patel408dcf62010-03-09 00:44:50 +0000235 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel4f6e73b2010-07-27 20:49:59 +0000236 LangTag, Filename, getCurrentDirname(),
Devang Patel6014edd2010-07-27 15:17:16 +0000237 Producer, true,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000238 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000239}
240
Devang Patel410dc002009-02-25 01:36:11 +0000241/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000242/// one if necessary.
243llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel408dcf62010-03-09 00:44:50 +0000244 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000245 unsigned Encoding = 0;
Devang Patelc7f16ab2010-07-28 23:23:29 +0000246 const char *BTName = NULL;
Chris Lattneraffb3732008-11-10 06:08:34 +0000247 switch (BT->getKind()) {
248 default:
249 case BuiltinType::Void:
250 return llvm::DIType();
Devang Patela652fab2010-07-28 01:33:15 +0000251 case BuiltinType::ObjCClass:
Devang Patel222f4be2010-07-21 22:41:25 +0000252 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patela652fab2010-07-28 01:33:15 +0000253 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patel222f4be2010-07-21 22:41:25 +0000254 llvm::DIType::FlagFwdDecl,
255 llvm::DIType(), llvm::DIArray());
Devang Patela652fab2010-07-28 01:33:15 +0000256 case BuiltinType::ObjCId: {
257 // typedef struct objc_class *Class;
258 // typedef struct objc_object {
259 // Class isa;
260 // } *id;
261
262 llvm::DIType OCTy =
263 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
264 Unit, "objc_class", Unit, 0, 0, 0, 0,
265 llvm::DIType::FlagFwdDecl,
266 llvm::DIType(), llvm::DIArray());
267 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
268
269 llvm::DIType ISATy =
270 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
271 Unit, "", Unit,
272 0, Size, 0, 0, 0, OCTy);
273
274 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
275
276 llvm::DIType FieldTy =
277 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
278 "isa", Unit,
279 0,Size, 0, 0, 0, ISATy);
280 EltTys.push_back(FieldTy);
281 llvm::DIArray Elements =
282 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
283
284 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
285 Unit, "objc_object", Unit, 0, 0, 0, 0,
286 0,
287 llvm::DIType(), Elements);
288 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000289 case BuiltinType::UChar:
290 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
291 case BuiltinType::Char_S:
292 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
293 case BuiltinType::UShort:
294 case BuiltinType::UInt:
295 case BuiltinType::ULong:
296 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
297 case BuiltinType::Short:
298 case BuiltinType::Int:
299 case BuiltinType::Long:
300 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
301 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
302 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000303 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000304 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000305 }
Devang Patelc7f16ab2010-07-28 23:23:29 +0000306
307 switch (BT->getKind()) {
308 case BuiltinType::Long: BTName = "long int"; break;
309 case BuiltinType::LongLong: BTName = "long long int"; break;
310 case BuiltinType::ULong: BTName = "long unsigned int"; break;
311 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
312 default:
313 BTName = BT->getName(CGM.getContext().getLangOptions());
314 break;
315 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000316 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000317 uint64_t Size = CGM.getContext().getTypeSize(BT);
318 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattneraffb3732008-11-10 06:08:34 +0000319 uint64_t Offset = 0;
Devang Patelc7f16ab2010-07-28 23:23:29 +0000320
Devang Patele21912d2009-10-20 19:55:01 +0000321 llvm::DIType DbgTy =
Devang Patelc7f16ab2010-07-28 23:23:29 +0000322 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patele21912d2009-10-20 19:55:01 +0000323 Unit, 0, Size, Align,
324 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000325 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000326}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000327
Chris Lattner7b0344f2009-04-23 06:13:01 +0000328llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000329 llvm::DIFile Unit) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000330 // Bit size, align and offset of the type.
331 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
332 if (Ty->isComplexIntegerType())
333 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000334
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000335 uint64_t Size = CGM.getContext().getTypeSize(Ty);
336 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattner7b0344f2009-04-23 06:13:01 +0000337 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000338
Devang Patele21912d2009-10-20 19:55:01 +0000339 llvm::DIType DbgTy =
340 DebugFactory.CreateBasicType(Unit, "complex",
341 Unit, 0, Size, Align,
342 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000343 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000344}
345
John McCall0cf15512009-09-25 01:40:47 +0000346/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000347/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000348llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000349 QualifierCollector Qc;
350 const Type *T = Qc.strip(Ty);
351
352 // Ignore these qualifiers for now.
353 Qc.removeObjCGCAttr();
354 Qc.removeAddressSpace();
355
Chris Lattneraffb3732008-11-10 06:08:34 +0000356 // We will create one Derived type for one qualifier and recurse to handle any
357 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000358 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000359 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000360 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000361 Qc.removeConst();
362 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000363 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000364 Qc.removeVolatile();
365 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000366 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000367 Qc.removeRestrict();
368 } else {
369 assert(Qc.empty() && "Unknown type qualifier for debug info");
370 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000371 }
Mike Stump11289f42009-09-09 15:08:12 +0000372
John McCall0cf15512009-09-25 01:40:47 +0000373 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
374
Daniel Dunbara290ded2008-10-31 03:54:29 +0000375 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
376 // CVR derived types.
Devang Patele21912d2009-10-20 19:55:01 +0000377 llvm::DIType DbgTy =
Devang Patel93f274c2010-03-09 22:49:11 +0000378 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000379 0, 0, 0, 0, 0, FromTy);
Devang Patele21912d2009-10-20 19:55:01 +0000380 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000381}
382
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000383llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000384 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000385 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000386 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
387 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000388 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000389}
390
Chris Lattneraffb3732008-11-10 06:08:34 +0000391llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000392 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000393 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
394 Ty->getPointeeType(), Unit);
395}
396
397llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
398 const Type *Ty,
399 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000400 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000401 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000402
Sanjiv Gupta98070572008-05-25 05:15:42 +0000403 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000404
405 // Size is always the size of a pointer. We can't use getTypeSize here
406 // because that does not return the correct value for references.
407 uint64_t Size =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000408 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
409 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000410
Devang Patele21912d2009-10-20 19:55:01 +0000411 return
Devang Patel93f274c2010-03-09 22:49:11 +0000412 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000413 0, Size, Align, 0, 0, EltTy);
Anders Carlsson443f6772009-11-06 19:19:55 +0000414
Sanjiv Gupta98070572008-05-25 05:15:42 +0000415}
416
Mike Stump31f099c2009-05-14 02:03:51 +0000417llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000418 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000419 if (BlockLiteralGenericSet)
420 return BlockLiteralGeneric;
421
Mike Stump31f099c2009-05-14 02:03:51 +0000422 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
423
424 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
425
426 llvm::DIType FieldTy;
427
428 QualType FType;
429 uint64_t FieldSize, FieldOffset;
430 unsigned FieldAlign;
431
432 llvm::DIArray Elements;
433 llvm::DIType EltTy, DescTy;
434
435 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000436 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000437 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
438 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000439
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000440 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000441 EltTys.clear();
442
Mike Stump581b9ad2009-10-02 02:30:50 +0000443 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000444 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000445
Mike Stump31f099c2009-05-14 02:03:51 +0000446 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patelc5ffabc2010-05-12 23:46:38 +0000447 Unit, LineNo, FieldOffset, 0, 0,
448 Flags, llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000449
Mike Stump31f099c2009-05-14 02:03:51 +0000450 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000451 uint64_t Size = CGM.getContext().getTypeSize(Ty);
452 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000453
Mike Stump31f099c2009-05-14 02:03:51 +0000454 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000455 Unit, "", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000456 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000457
458 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000459 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000460 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000461 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000462 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
463 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000464 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000465 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000466
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000467 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000468 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000469 FieldSize = CGM.getContext().getTypeSize(Ty);
470 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump31f099c2009-05-14 02:03:51 +0000471 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000472 "__descriptor", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000473 LineNo, FieldSize, FieldAlign,
Mike Stump31f099c2009-05-14 02:03:51 +0000474 FieldOffset, 0, FieldTy);
475 EltTys.push_back(FieldTy);
476
477 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000478 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000479
480 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patelc5ffabc2010-05-12 23:46:38 +0000481 Unit, LineNo, FieldOffset, 0, 0,
482 Flags, llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000483
Mike Stump31f099c2009-05-14 02:03:51 +0000484 BlockLiteralGenericSet = true;
485 BlockLiteralGeneric
486 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000487 "", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000488 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000489 return BlockLiteralGeneric;
490}
491
Chris Lattneraffb3732008-11-10 06:08:34 +0000492llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000493 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000494 // Typedefs are derived from some other type. If we have a typedef of a
495 // typedef, make sure to emit the whole chain.
496 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000497
Chris Lattneraffb3732008-11-10 06:08:34 +0000498 // We don't set size information, but do specify where the typedef was
499 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000500 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta98070572008-05-25 05:15:42 +0000501
Devang Patel7b7f46f2010-02-01 21:34:11 +0000502 llvm::DIDescriptor TyContext
503 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
504 Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000505 llvm::DIType DbgTy =
Devang Patelbb4820d2010-01-29 22:29:31 +0000506 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Patel7b7f46f2010-02-01 21:34:11 +0000507 TyContext,
Devang Patelbb4820d2010-01-29 22:29:31 +0000508 Ty->getDecl()->getName(), Unit,
509 Line, 0, 0, 0, 0, Src);
Devang Patele21912d2009-10-20 19:55:01 +0000510 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000511}
512
Chris Lattneraffb3732008-11-10 06:08:34 +0000513llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000514 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000515 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000516
Chris Lattneraffb3732008-11-10 06:08:34 +0000517 // Add the result type at least.
518 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000519
Chris Lattneraffb3732008-11-10 06:08:34 +0000520 // Set up remainder of arguments if there is a prototype.
521 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000522 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000523 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
524 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
525 } else {
526 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000527 }
528
Chris Lattneraffb3732008-11-10 06:08:34 +0000529 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000530 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000531
Devang Patele21912d2009-10-20 19:55:01 +0000532 llvm::DIType DbgTy =
533 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000534 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000535 0, 0, 0, 0, 0,
536 llvm::DIType(), EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000537 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000538}
539
Devang Patel889ce762010-01-19 00:00:59 +0000540/// CollectRecordFields - A helper function to collect debug info for
541/// record fields. This is used while creating debug info entry for a Record.
542void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000543CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel889ce762010-01-19 00:00:59 +0000544 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
545 unsigned FieldNo = 0;
Devang Patel1c0954c2010-02-01 21:39:52 +0000546 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
547 for (RecordDecl::field_iterator I = RD->field_begin(),
548 E = RD->field_end();
Devang Patel889ce762010-01-19 00:00:59 +0000549 I != E; ++I, ++FieldNo) {
550 FieldDecl *Field = *I;
551 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel889ce762010-01-19 00:00:59 +0000552 llvm::StringRef FieldName = Field->getName();
553
Devang Patelf4df65c2010-02-12 01:31:06 +0000554 // Ignore unnamed fields. Do not ignore unnamed records.
555 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel889ce762010-01-19 00:00:59 +0000556 continue;
557
558 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000559 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
560 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel889ce762010-01-19 00:00:59 +0000561 QualType FType = Field->getType();
562 uint64_t FieldSize = 0;
563 unsigned FieldAlign = 0;
564 if (!FType->isIncompleteArrayType()) {
565
566 // Bit size, align and offset of the type.
567 FieldSize = CGM.getContext().getTypeSize(FType);
568 Expr *BitWidth = Field->getBitWidth();
569 if (BitWidth)
570 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel889ce762010-01-19 00:00:59 +0000571 FieldAlign = CGM.getContext().getTypeAlign(FType);
572 }
573
574 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
575
Devang Patelb9ab3092010-04-21 23:12:37 +0000576 unsigned Flags = 0;
577 AccessSpecifier Access = I->getAccess();
578 if (Access == clang::AS_private)
579 Flags |= llvm::DIType::FlagPrivate;
580 else if (Access == clang::AS_protected)
581 Flags |= llvm::DIType::FlagProtected;
582
Devang Patel889ce762010-01-19 00:00:59 +0000583 // Create a DW_TAG_member node to remember the offset of this field in the
584 // struct. FIXME: This is an absolutely insane way to capture this
585 // information. When we gut debug info, this should be fixed.
586 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
587 FieldName, FieldDefUnit,
588 FieldLine, FieldSize, FieldAlign,
Devang Patelb9ab3092010-04-21 23:12:37 +0000589 FieldOffset, Flags, FieldTy);
Devang Patel889ce762010-01-19 00:00:59 +0000590 EltTys.push_back(FieldTy);
591 }
592}
593
Devang Patel3d4e6d92010-01-28 00:28:01 +0000594/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
595/// function type is not updated to include implicit "this" pointer. Use this
596/// routine to get a method type which includes "this" pointer.
597llvm::DIType
598CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000599 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000600 llvm::DIType FnTy
601 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
602 0),
603 Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000604
605 // Static methods do not need "this" pointer argument.
606 if (Method->isStatic())
607 return FnTy;
608
Devang Patel3d4e6d92010-01-28 00:28:01 +0000609 // Add "this" pointer.
610
Devang Patelba4ad7f2010-05-07 18:12:35 +0000611 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000612 assert (Args.getNumElements() && "Invalid number of arguments!");
613
614 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
615
616 // First element is always return type. For 'void' functions it is NULL.
617 Elts.push_back(Args.getElement(0));
618
619 // "this" pointer is always first argument.
620 ASTContext &Context = CGM.getContext();
621 QualType ThisPtr =
622 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patelcce7e852010-02-09 17:57:50 +0000623 llvm::DIType ThisPtrType =
624 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel0a34e312010-07-13 00:24:30 +0000625
Devang Patelba4ad7f2010-05-07 18:12:35 +0000626 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patelcce7e852010-02-09 17:57:50 +0000627 Elts.push_back(ThisPtrType);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000628
629 // Copy rest of the arguments.
630 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
631 Elts.push_back(Args.getElement(i));
632
633 llvm::DIArray EltTypeArray =
634 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
635
636 return
637 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000638 Unit, "", Unit,
Devang Patel3d4e6d92010-01-28 00:28:01 +0000639 0, 0, 0, 0, 0,
640 llvm::DIType(), EltTypeArray);
641}
642
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000643/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
644/// a single member function GlobalDecl.
645llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000646CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000647 llvm::DIFile Unit,
Dan Gohman196f7102010-08-20 22:02:57 +0000648 llvm::DIType RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000649 bool IsCtorOrDtor =
650 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
651
652 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000653 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000654
655 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
656 // make sense to give a single ctor/dtor a linkage name.
Anders Carlssonea836bc2010-06-22 16:16:50 +0000657 llvm::StringRef MethodLinkageName;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000658 if (!IsCtorOrDtor)
Anders Carlssonea836bc2010-06-22 16:16:50 +0000659 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000660
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000661 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000662 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
663 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000664
665 // Collect virtual method info.
666 llvm::DIType ContainingType;
667 unsigned Virtuality = 0;
668 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000669
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000670 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000671 if (Method->isPure())
672 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
673 else
674 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
675
676 // It doesn't make sense to give a virtual destructor a vtable index,
677 // since a single destructor has two entries in the vtable.
678 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson11e51402010-04-17 20:15:18 +0000679 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000680 ContainingType = RecordTy;
681 }
682
683 llvm::DISubprogram SP =
684 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
685 MethodLinkageName,
686 MethodDefUnit, MethodLine,
687 MethodTy, /*isLocalToUnit=*/false,
Devang Patel0aabb122010-07-12 22:54:41 +0000688 /* isDefintion=*/ false,
Devang Patelb3026df2010-07-15 22:57:00 +0000689 Virtuality, VIndex, ContainingType,
Devang Patel8fd64992010-07-15 23:09:46 +0000690 Method->isImplicit(),
691 CGM.getLangOptions().Optimize);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000692
693 // Don't cache ctors or dtors since we have to emit multiple functions for
694 // a single ctor or dtor.
695 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelba4ad7f2010-05-07 18:12:35 +0000696 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000697
698 return SP;
699}
700
Devang Patel7a12ad02010-01-19 01:54:44 +0000701/// CollectCXXMemberFunctions - A helper function to collect debug info for
702/// C++ member functions.This is used while creating debug info entry for
703/// a Record.
704void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000705CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel7a12ad02010-01-19 01:54:44 +0000706 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000707 llvm::DIType RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000708 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
709 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000710 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000711
Devang Patel0ae70d12010-02-09 19:09:28 +0000712 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000713 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000714
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000715 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000716 }
717}
718
Devang Patelc54353d2010-01-25 23:32:18 +0000719/// CollectCXXBases - A helper function to collect debug info for
720/// C++ base classes. This is used while creating debug info entry for
721/// a Record.
722void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000723CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patelc54353d2010-01-25 23:32:18 +0000724 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000725 llvm::DIType RecordTy) {
Devang Patelc54353d2010-01-25 23:32:18 +0000726
Devang Patel1c0954c2010-02-01 21:39:52 +0000727 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
728 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
729 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000730 unsigned BFlags = 0;
731 uint64_t BaseOffset;
732
733 const CXXRecordDecl *Base =
734 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
735
736 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000737 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +0000738 // expression where it expects +ve number.
Anders Carlssona864caf2010-03-23 04:11:45 +0000739 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patel128aa9d2010-01-28 21:54:15 +0000740 BFlags = llvm::DIType::FlagVirtual;
741 } else
742 BaseOffset = RL.getBaseClassOffset(Base);
743
744 AccessSpecifier Access = BI->getAccessSpecifier();
745 if (Access == clang::AS_private)
746 BFlags |= llvm::DIType::FlagPrivate;
747 else if (Access == clang::AS_protected)
748 BFlags |= llvm::DIType::FlagProtected;
749
750 llvm::DIType DTy =
751 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
752 RecordTy, llvm::StringRef(),
Devang Patel93f274c2010-03-09 22:49:11 +0000753 Unit, 0, 0, 0,
Devang Patel128aa9d2010-01-28 21:54:15 +0000754 BaseOffset, BFlags,
755 getOrCreateType(BI->getType(),
756 Unit));
757 EltTys.push_back(DTy);
758 }
Devang Patelc54353d2010-01-25 23:32:18 +0000759}
760
Devang Patel84033fb2010-01-28 18:11:52 +0000761/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +0000762llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +0000763 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +0000764 return VTablePtrType;
765
766 ASTContext &Context = CGM.getContext();
767
768 /* Function type */
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000769 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
770 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel84033fb2010-01-28 18:11:52 +0000771 llvm::DIType SubTy =
772 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000773 Unit, "", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000774 0, 0, 0, 0, 0, llvm::DIType(), SElements);
775
776 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
777 llvm::DIType vtbl_ptr_type
778 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000779 Unit, "__vtbl_ptr_type", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000780 0, Size, 0, 0, 0, SubTy);
781
Devang Patel93f274c2010-03-09 22:49:11 +0000782 VTablePtrType =
783 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
784 Unit, "", Unit,
785 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel84033fb2010-01-28 18:11:52 +0000786 return VTablePtrType;
787}
788
Anders Carlsson11e51402010-04-17 20:15:18 +0000789/// getVTableName - Get vtable name for the given Class.
790llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel84033fb2010-01-28 18:11:52 +0000791 // Otherwise construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +0000792 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +0000793
794 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000795 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000796 memcpy(StrPtr, Name.data(), Name.length());
797 return llvm::StringRef(StrPtr, Name.length());
798}
799
800
Anders Carlsson11e51402010-04-17 20:15:18 +0000801/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +0000802/// debug info entry in EltTys vector.
803void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +0000804CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000805 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000806 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +0000807
808 // If there is a primary base then it will hold vtable info.
809 if (RL.getPrimaryBase())
810 return;
811
812 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +0000813 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +0000814 return;
815
816 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
817 llvm::DIType VPTR
818 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson11e51402010-04-17 20:15:18 +0000819 getVTableName(RD), Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000820 0, Size, 0, 0, 0,
821 getOrCreateVTablePtrType(Unit));
822 EltTys.push_back(VPTR);
823}
824
Devang Patel410dc002009-02-25 01:36:11 +0000825/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000826llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000827 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000828 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000829
Chris Lattneraffb3732008-11-10 06:08:34 +0000830 unsigned Tag;
Devang Patel3efd1472010-02-01 21:52:22 +0000831 if (RD->isStruct())
Chris Lattneraffb3732008-11-10 06:08:34 +0000832 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel3efd1472010-02-01 21:52:22 +0000833 else if (RD->isUnion())
Chris Lattneraffb3732008-11-10 06:08:34 +0000834 Tag = llvm::dwarf::DW_TAG_union_type;
835 else {
Devang Patel3efd1472010-02-01 21:52:22 +0000836 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattneraffb3732008-11-10 06:08:34 +0000837 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000838 }
839
Chris Lattneraffb3732008-11-10 06:08:34 +0000840 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000841 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
842 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000843
Chris Lattneraffb3732008-11-10 06:08:34 +0000844 // Records and classes and unions can all be recursive. To handle them, we
845 // first generate a debug descriptor for the struct as a forward declaration.
846 // Then (if it is a definition) we go through and get debug info for all of
847 // its members. Finally, we create a descriptor for the complete type (which
848 // may refer to the forward decl if the struct is recursive) and replace all
849 // uses of the forward declaration with the final definition.
Devang Patel8f3f76f2010-07-08 19:56:29 +0000850 llvm::DIDescriptor FDContext =
851 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
852
853 // If this is just a forward declaration, construct an appropriately
854 // marked node and just return it.
855 if (!RD->getDefinition()) {
856 llvm::DICompositeType FwdDecl =
857 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
858 DefUnit, Line, 0, 0, 0,
859 llvm::DIType::FlagFwdDecl,
860 llvm::DIType(), llvm::DIArray());
861
862 return FwdDecl;
863 }
Devang Patel3f4a77e2010-01-20 23:56:40 +0000864
Dan Gohmanb1aac332010-08-20 22:39:57 +0000865 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump11289f42009-09-09 15:08:12 +0000866
Devang Patelba4ad7f2010-05-07 18:12:35 +0000867 llvm::MDNode *MN = FwdDecl;
868 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattneraffb3732008-11-10 06:08:34 +0000869 // Otherwise, insert it into the TypeCache so that recursive uses will find
870 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000871 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +0000872 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000873 RegionStack.push_back(FwdDeclNode);
874 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +0000875
876 // Convert all the elements.
877 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
878
Devang Patel3efd1472010-02-01 21:52:22 +0000879 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +0000880 if (CXXDecl) {
881 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson11e51402010-04-17 20:15:18 +0000882 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +0000883 }
Devang Patelcaa23f02010-08-12 00:02:44 +0000884
885 // Collect static variables with initializers.
886 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
887 I != E; ++I)
888 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
889 if (const Expr *Init = V->getInit()) {
890 Expr::EvalResult Result;
891 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
892 llvm::ConstantInt *CI
893 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
894
895 // Create the descriptor for static variable.
896 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
897 llvm::StringRef VName = V->getName();
898 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
899 // Do not use DIGlobalVariable for enums.
900 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
901 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
902 getLineNumber(V->getLocation()),
903 VTy, true, true, CI);
904 }
905 }
906 }
907 }
908
Devang Patel3efd1472010-02-01 21:52:22 +0000909 CollectRecordFields(RD, Unit, EltTys);
Devang Patelabb44132010-01-28 00:54:21 +0000910 llvm::MDNode *ContainingType = NULL;
Devang Patel84033fb2010-01-28 18:11:52 +0000911 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000912 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelabb44132010-01-28 00:54:21 +0000913
914 // A class's primary base or the class itself contains the vtable.
Devang Patel3efd1472010-02-01 21:52:22 +0000915 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patelabb44132010-01-28 00:54:21 +0000916 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
917 ContainingType =
Devang Patelba4ad7f2010-05-07 18:12:35 +0000918 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patelabb44132010-01-28 00:54:21 +0000919 else if (CXXDecl->isDynamicClass())
Devang Patelba4ad7f2010-05-07 18:12:35 +0000920 ContainingType = FwdDecl;
Devang Patelc54353d2010-01-25 23:32:18 +0000921 }
Mike Stump11289f42009-09-09 15:08:12 +0000922
Chris Lattneraffb3732008-11-10 06:08:34 +0000923 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000924 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000925
926 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000927 uint64_t Size = CGM.getContext().getTypeSize(Ty);
928 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000929
Devang Patel01bb5ce2010-03-11 20:01:48 +0000930 RegionStack.pop_back();
931 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
932 RegionMap.find(Ty->getDecl());
933 if (RI != RegionMap.end())
934 RegionMap.erase(RI);
935
Devang Patele8fb4b72010-02-01 22:40:08 +0000936 llvm::DIDescriptor RDContext =
937 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel6c018202010-07-20 20:24:18 +0000938
939 llvm::StringRef RDName = RD->getName();
940 // If this is a class, include the template arguments also.
941 if (Tag == llvm::dwarf::DW_TAG_class_type)
942 RDName = getClassName(RD);
943
Devang Patel06cceef2009-07-22 18:57:00 +0000944 llvm::DICompositeType RealDecl =
Devang Patele8fb4b72010-02-01 22:40:08 +0000945 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel6c018202010-07-20 20:24:18 +0000946 RDName,
Devang Patel7bdf0962009-11-12 00:51:46 +0000947 DefUnit, Line, Size, Align, 0, 0,
Devang Patelabb44132010-01-28 00:54:21 +0000948 llvm::DIType(), Elements,
949 0, ContainingType);
Chris Lattneraffb3732008-11-10 06:08:34 +0000950
951 // Now that we have a real decl for the struct, replace anything using the
952 // old decl with the new one. This will recursively update the debug info.
Dan Gohman196f7102010-08-20 22:02:57 +0000953 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +0000954 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +0000955 return RealDecl;
956}
957
John McCall8b07ec22010-05-15 11:32:37 +0000958/// CreateType - get objective-c object type.
959llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
960 llvm::DIFile Unit) {
961 // Ignore protocols.
962 return getOrCreateType(Ty->getBaseType(), Unit);
963}
964
Devang Patelf4c205b2009-02-26 21:10:26 +0000965/// CreateType - get objective-c interface type.
966llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000967 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000968 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patelf4c205b2009-02-26 21:10:26 +0000969 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patelf4c205b2009-02-26 21:10:26 +0000970
971 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +0000972 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +0000973 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +0000974 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +0000975
Dan Gohman66427b12010-08-23 21:15:56 +0000976 // If this is just a forward declaration, return a special forward-declaration
977 // debug type.
978 if (ID->isForwardDecl()) {
979 llvm::DICompositeType FwdDecl =
980 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
981 DefUnit, Line, 0, 0, 0, 0,
982 llvm::DIType(), llvm::DIArray(),
983 RuntimeLang);
984 return FwdDecl;
985 }
986
Devang Patelf4c205b2009-02-26 21:10:26 +0000987 // To handle recursive interface, we
988 // first generate a debug descriptor for the struct as a forward declaration.
989 // Then (if it is a definition) we go through and get debug info for all of
990 // its members. Finally, we create a descriptor for the complete type (which
991 // may refer to the forward decl if the struct is recursive) and replace all
992 // uses of the forward declaration with the final definition.
Dan Gohmanb1aac332010-08-20 22:39:57 +0000993 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump11289f42009-09-09 15:08:12 +0000994
Devang Patelba4ad7f2010-05-07 18:12:35 +0000995 llvm::MDNode *MN = FwdDecl;
996 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patelf4c205b2009-02-26 21:10:26 +0000997 // Otherwise, insert it into the TypeCache so that recursive uses will find
998 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000999 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001000 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001001 RegionStack.push_back(FwdDeclNode);
1002 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001003
1004 // Convert all the elements.
1005 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1006
Devang Patel3efd1472010-02-01 21:52:22 +00001007 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +00001008 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001009 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001010 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001011 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +00001012 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Patel93f274c2010-03-09 22:49:11 +00001013 Unit, "", Unit, 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +00001014 0 /* offset */, 0, SClassTy);
1015 EltTys.push_back(InhTag);
1016 }
1017
Devang Patel3efd1472010-02-01 21:52:22 +00001018 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001019
1020 unsigned FieldNo = 0;
Devang Patel3efd1472010-02-01 21:52:22 +00001021 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1022 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001023 ObjCIvarDecl *Field = *I;
1024 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1025
Devang Patel58bf6e12009-11-25 17:37:31 +00001026 llvm::StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001027
Devang Pateldf348f12009-04-27 22:40:36 +00001028 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +00001029 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +00001030 continue;
1031
Devang Patelf4c205b2009-02-26 21:10:26 +00001032 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001033 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1034 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +00001035 QualType FType = Field->getType();
1036 uint64_t FieldSize = 0;
1037 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +00001038
Devang Patel9f804932009-03-20 18:24:39 +00001039 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001040
Devang Patel9f804932009-03-20 18:24:39 +00001041 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001042 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001043 Expr *BitWidth = Field->getBitWidth();
1044 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001045 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +00001046
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001047 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001048 }
1049
Mike Stump11289f42009-09-09 15:08:12 +00001050 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1051
Devang Patelec4bad52009-03-19 00:23:53 +00001052 unsigned Flags = 0;
1053 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1054 Flags = llvm::DIType::FlagProtected;
1055 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1056 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +00001057
Devang Patelf4c205b2009-02-26 21:10:26 +00001058 // Create a DW_TAG_member node to remember the offset of this field in the
1059 // struct. FIXME: This is an absolutely insane way to capture this
1060 // information. When we gut debug info, this should be fixed.
1061 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1062 FieldName, FieldDefUnit,
1063 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +00001064 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +00001065 EltTys.push_back(FieldTy);
1066 }
Mike Stump11289f42009-09-09 15:08:12 +00001067
Devang Patelf4c205b2009-02-26 21:10:26 +00001068 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +00001069 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +00001070
Devang Patel01bb5ce2010-03-11 20:01:48 +00001071 RegionStack.pop_back();
1072 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1073 RegionMap.find(Ty->getDecl());
1074 if (RI != RegionMap.end())
1075 RegionMap.erase(RI);
1076
Devang Patelf4c205b2009-02-26 21:10:26 +00001077 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001078 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1079 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001080
Devang Patel6a3b3fe2009-07-27 18:42:03 +00001081 llvm::DICompositeType RealDecl =
Devang Patel3efd1472010-02-01 21:52:22 +00001082 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patel7bdf0962009-11-12 00:51:46 +00001083 Line, Size, Align, 0, 0, llvm::DIType(),
1084 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +00001085
1086 // Now that we have a real decl for the struct, replace anything using the
1087 // old decl with the new one. This will recursively update the debug info.
Dan Gohman196f7102010-08-20 22:02:57 +00001088 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001089 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +00001090
Devang Patelf4c205b2009-02-26 21:10:26 +00001091 return RealDecl;
1092}
1093
Chris Lattneraffb3732008-11-10 06:08:34 +00001094llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001095 llvm::DIFile Unit) {
Devang Patel41c20972010-08-23 22:07:25 +00001096 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001097
Chris Lattneraffb3732008-11-10 06:08:34 +00001098}
1099
1100llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001101 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001102 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1103 return CreateType(RT, Unit);
1104 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1105 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001106
Chris Lattneraffb3732008-11-10 06:08:34 +00001107 return llvm::DIType();
1108}
1109
Devang Patelb4073382010-02-23 22:59:39 +00001110llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedman04831922010-08-22 01:00:03 +00001111 llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001112 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1113 uint64_t NumElems = Ty->getNumElements();
1114 if (NumElems > 0)
1115 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001116
Benjamin Kramer9e649c32010-03-30 11:36:44 +00001117 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1118 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patelb4073382010-02-23 22:59:39 +00001119
1120 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1121 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1122
1123 return
1124 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001125 Unit, "", Unit,
Devang Patelb4073382010-02-23 22:59:39 +00001126 0, Size, Align, 0, 0,
Eli Friedman04831922010-08-22 01:00:03 +00001127 ElementTy, SubscriptArray);
Devang Patelb4073382010-02-23 22:59:39 +00001128}
1129
Chris Lattneraffb3732008-11-10 06:08:34 +00001130llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001131 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001132 uint64_t Size;
1133 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001134
1135
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001136 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001137 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001138 Size = 0;
1139 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001140 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001141 } else if (Ty->isIncompleteArrayType()) {
1142 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001143 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001144 } else {
1145 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001146 Size = CGM.getContext().getTypeSize(Ty);
1147 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001148 }
Mike Stump11289f42009-09-09 15:08:12 +00001149
Chris Lattneraffb3732008-11-10 06:08:34 +00001150 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1151 // interior arrays, do we care? Why aren't nested arrays represented the
1152 // obvious/recursive way?
1153 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1154 QualType EltTy(Ty, 0);
1155 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001156 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001157 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +00001158 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +00001159 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +00001160 // FIXME: Verify this is right for VLAs.
1161 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1162 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001163 }
Mike Stump11289f42009-09-09 15:08:12 +00001164
Chris Lattneraffb3732008-11-10 06:08:34 +00001165 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +00001166 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001167
Devang Patele21912d2009-10-20 19:55:01 +00001168 llvm::DIType DbgTy =
1169 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001170 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +00001171 0, Size, Align, 0, 0,
1172 getOrCreateType(EltTy, Unit),
1173 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001174 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001175}
1176
Anders Carlsson443f6772009-11-06 19:19:55 +00001177llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001178 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001179 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1180 Ty, Ty->getPointeeType(), Unit);
1181}
Chris Lattneraffb3732008-11-10 06:08:34 +00001182
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001183llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001184 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001185 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1186 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1187
1188 if (!Ty->getPointeeType()->isFunctionType()) {
1189 // We have a data member pointer type.
1190 return PointerDiffDITy;
1191 }
1192
1193 // We have a member function pointer type. Treat it as a struct with two
1194 // ptrdiff_t members.
1195 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1196
1197 uint64_t FieldOffset = 0;
1198 llvm::DIDescriptor ElementTypes[2];
1199
1200 // FIXME: This should probably be a function type instead.
1201 ElementTypes[0] =
1202 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001203 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001204 Info.first, Info.second, FieldOffset, 0,
1205 PointerDiffDITy);
1206 FieldOffset += Info.first;
1207
1208 ElementTypes[1] =
1209 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001210 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001211 Info.first, Info.second, FieldOffset, 0,
1212 PointerDiffDITy);
1213
1214 llvm::DIArray Elements =
1215 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1216 llvm::array_lengthof(ElementTypes));
1217
1218 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1219 U, llvm::StringRef("test"),
Devang Patel93f274c2010-03-09 22:49:11 +00001220 U, 0, FieldOffset,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001221 0, 0, 0, llvm::DIType(), Elements);
1222}
1223
Devang Patel41c20972010-08-23 22:07:25 +00001224/// CreateEnumType - get enumeration type.
1225llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1226 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1227
1228 // Create DIEnumerator elements for each enumerator.
1229 for (EnumDecl::enumerator_iterator
1230 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1231 Enum != EnumEnd; ++Enum) {
1232 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1233 Enum->getInitVal().getZExtValue()));
1234 }
1235
1236 // Return a CompositeType for the enum itself.
1237 llvm::DIArray EltArray =
1238 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1239
1240 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1241 unsigned Line = getLineNumber(ED->getLocation());
1242 uint64_t Size = 0;
Devang Patel22e99c22010-08-24 18:14:06 +00001243 uint64_t Align = 0;
1244 if (!ED->getTypeForDecl()->isIncompleteType()) {
1245 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1246 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1247 }
Devang Patel41c20972010-08-23 22:07:25 +00001248 llvm::DIType DbgTy =
1249 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1250 Unit, ED->getName(), DefUnit, Line,
Devang Patel22e99c22010-08-24 18:14:06 +00001251 Size, Align, 0, 0,
Devang Patel41c20972010-08-23 22:07:25 +00001252 llvm::DIType(), EltArray);
1253 return DbgTy;
1254}
1255
Douglas Gregor0f139a12009-12-21 20:18:30 +00001256static QualType UnwrapTypeForDebugInfo(QualType T) {
1257 do {
1258 QualType LastT = T;
1259 switch (T->getTypeClass()) {
1260 default:
1261 return T;
1262 case Type::TemplateSpecialization:
1263 T = cast<TemplateSpecializationType>(T)->desugar();
1264 break;
1265 case Type::TypeOfExpr: {
1266 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1267 T = Ty->getUnderlyingExpr()->getType();
1268 break;
1269 }
1270 case Type::TypeOf:
1271 T = cast<TypeOfType>(T)->getUnderlyingType();
1272 break;
1273 case Type::Decltype:
1274 T = cast<DecltypeType>(T)->getUnderlyingType();
1275 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001276 case Type::Elaborated:
1277 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001278 break;
1279 case Type::SubstTemplateTypeParm:
1280 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1281 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001282 }
1283
1284 assert(T != LastT && "Type unwrapping failed to unwrap!");
1285 if (T == LastT)
1286 return T;
1287 } while (true);
1288
1289 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001290}
1291
Sanjiv Gupta98070572008-05-25 05:15:42 +00001292/// getOrCreateType - Get the type from the cache or create a new
1293/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001294llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001295 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001296 if (Ty.isNull())
1297 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001298
Douglas Gregor0f139a12009-12-21 20:18:30 +00001299 // Unwrap the type as needed for debug information.
1300 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001301
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001302 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001303 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001304 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001305 if (it != TypeCache.end()) {
1306 // Verify that the debug info still exists.
1307 if (&*it->second)
1308 return llvm::DIType(cast<llvm::MDNode>(it->second));
1309 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001310
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001311 // Otherwise create the type.
1312 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001313
1314 // And update the type cache.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001315 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001316 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001317}
1318
Anders Carlsson6037e782009-11-14 20:52:05 +00001319/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001320llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001321 llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001322 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001323 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001324 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001325
Douglas Gregor0915b432009-12-21 19:57:21 +00001326 const char *Diag = 0;
1327
Sanjiv Gupta98070572008-05-25 05:15:42 +00001328 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001329 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001330#define TYPE(Class, Base)
1331#define ABSTRACT_TYPE(Class, Base)
1332#define NON_CANONICAL_TYPE(Class, Base)
1333#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1334#include "clang/AST/TypeNodes.def"
1335 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001336
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001337 // FIXME: Handle these.
1338 case Type::ExtVector:
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001339 return llvm::DIType();
Devang Patelb4073382010-02-23 22:59:39 +00001340
1341 case Type::Vector:
1342 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001343 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001344 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001345 case Type::ObjCObject:
1346 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001347 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001348 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1349 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1350 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1351 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001352 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001353 return CreateType(cast<BlockPointerType>(Ty), Unit);
1354 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001355 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001356 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001357 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001358 case Type::FunctionProto:
1359 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001360 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001361 case Type::ConstantArray:
1362 case Type::VariableArray:
1363 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001364 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001365
1366 case Type::LValueReference:
1367 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1368
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001369 case Type::MemberPointer:
1370 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001371
1372 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001373 case Type::Elaborated:
Douglas Gregor0915b432009-12-21 19:57:21 +00001374 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001375 case Type::TypeOfExpr:
1376 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001377 case Type::Decltype:
1378 llvm_unreachable("type should have been unwrapped!");
1379 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001380
1381 case Type::RValueReference:
1382 // FIXME: Implement!
1383 Diag = "rvalue references";
1384 break;
Sanjiv Gupta98070572008-05-25 05:15:42 +00001385 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001386
1387 assert(Diag && "Fall through without a diagnostic?");
1388 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1389 "debug information for %0 is not yet supported");
1390 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1391 << Diag;
1392 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001393}
1394
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001395/// CreateMemberType - Create new member and increase Offset by FType's size.
1396llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1397 llvm::StringRef Name,
1398 uint64_t *Offset) {
1399 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1400 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1401 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1402 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1403 Unit, Name, Unit, 0,
1404 FieldSize, FieldAlign,
1405 *Offset, 0, FieldTy);
1406 *Offset += FieldSize;
1407 return Ty;
1408}
1409
Sanjiv Gupta98070572008-05-25 05:15:42 +00001410/// EmitFunctionStart - Constructs the debug code for entering a function -
1411/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001412void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001413 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001414 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001415
Devang Patel934661e2010-01-14 00:36:21 +00001416 llvm::StringRef Name;
Anders Carlssonea836bc2010-06-22 16:16:50 +00001417 llvm::StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001418
Devang Patel0884a602010-07-22 22:29:16 +00001419 FnBeginRegionCount.push_back(RegionStack.size());
1420
Devang Patel934661e2010-01-14 00:36:21 +00001421 const Decl *D = GD.getDecl();
1422 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001423 // If there is a DISubprogram for this function available then use it.
1424 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1425 FI = SPCache.find(FD);
1426 if (FI != SPCache.end()) {
Devang Pateld3cbaa12010-03-08 20:53:17 +00001427 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001428 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1429 llvm::MDNode *SPN = SP;
1430 RegionStack.push_back(SPN);
1431 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00001432 return;
1433 }
1434 }
Devang Patel934661e2010-01-14 00:36:21 +00001435 Name = getFunctionName(FD);
1436 // Use mangled name as linkage name for c/c++ functions.
Anders Carlssonea836bc2010-06-22 16:16:50 +00001437 LinkageName = CGM.getMangledName(GD);
Devang Patel934661e2010-01-14 00:36:21 +00001438 } else {
1439 // Use llvm function name as linkage name.
1440 Name = Fn->getName();
Anders Carlssonea836bc2010-06-22 16:16:50 +00001441 LinkageName = Name;
Devang Patel934661e2010-01-14 00:36:21 +00001442 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001443 if (!Name.empty() && Name[0] == '\01')
1444 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001445
Devang Patel84715932010-04-24 00:49:16 +00001446 // It is expected that CurLoc is set before using EmitFunctionStart.
1447 // Usually, CurLoc points to the left bracket location of compound
1448 // statement representing function body.
1449 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patelc5ffabc2010-05-12 23:46:38 +00001450 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001451
Chris Lattneraffb3732008-11-10 06:08:34 +00001452 llvm::DISubprogram SP =
Devang Patel84715932010-04-24 00:49:16 +00001453 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stumpae2559a2009-10-23 01:52:13 +00001454 getOrCreateType(FnType, Unit),
Devang Patel8fd64992010-07-15 23:09:46 +00001455 Fn->hasInternalLinkage(), true/*definition*/,
1456 0, 0, llvm::DIType(),
1457 D->isImplicit(),
1458 CGM.getLangOptions().Optimize, Fn);
Mike Stump11289f42009-09-09 15:08:12 +00001459
Sanjiv Gupta98070572008-05-25 05:15:42 +00001460 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001461 llvm::MDNode *SPN = SP;
1462 RegionStack.push_back(SPN);
1463 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel0884a602010-07-22 22:29:16 +00001464
1465 // Clear stack used to keep track of #line directives.
1466 LineDirectiveFiles.clear();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001467}
1468
1469
Devang Patel11a42a42010-07-20 22:20:10 +00001470void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001471 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001472
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001473 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001474 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00001475 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +00001476 || (SM.getInstantiationLineNumber(CurLoc) ==
1477 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001478 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patela2c048e2010-04-05 21:09:15 +00001479 // New Builder may not be in sync with CGDebugInfo.
1480 if (!Builder.getCurrentDebugLocation().isUnknown())
1481 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001482
1483 // Update last state.
1484 PrevLoc = CurLoc;
1485
Chris Lattnere675d0f2010-04-01 06:31:43 +00001486 llvm::MDNode *Scope = RegionStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00001487 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1488 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00001489 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001490}
1491
Devang Patel0884a602010-07-22 22:29:16 +00001492/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1493/// has introduced scope change.
1494void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1495 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1496 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1497 return;
1498 SourceManager &SM = CGM.getContext().getSourceManager();
1499 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1500 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1501
1502 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1503 return;
1504
1505 // If #line directive stack is empty then we are entering a new scope.
1506 if (LineDirectiveFiles.empty()) {
1507 EmitRegionStart(Builder);
1508 LineDirectiveFiles.push_back(PCLoc.getFilename());
1509 return;
1510 }
1511
1512 assert (RegionStack.size() >= LineDirectiveFiles.size()
1513 && "error handling #line regions!");
1514
1515 bool SeenThisFile = false;
1516 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1517 E = LineDirectiveFiles.end(); I != E; ++I)
1518 if (!strcmp(PPLoc.getFilename(), *I)) {
1519 SeenThisFile = true;
1520 break;
1521 }
1522
1523 // If #line for this file is seen earlier then pop out #line regions.
1524 if (SeenThisFile) {
1525 while (!LineDirectiveFiles.empty()) {
1526 const char *LastFile = LineDirectiveFiles.back();
1527 RegionStack.pop_back();
1528 LineDirectiveFiles.pop_back();
1529 if (!strcmp(PPLoc.getFilename(), LastFile))
1530 break;
1531 }
1532 return;
1533 }
1534
1535 // .. otherwise insert new #line region.
1536 EmitRegionStart(Builder);
1537 LineDirectiveFiles.push_back(PCLoc.getFilename());
1538
1539 return;
1540}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001541/// EmitRegionStart- Constructs the debug code for entering a declarative
1542/// region - "llvm.dbg.region.start.".
Devang Patel11a42a42010-07-20 22:20:10 +00001543void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patelb40f2952009-11-13 19:10:24 +00001544 llvm::DIDescriptor D =
1545 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1546 llvm::DIDescriptor() :
Devang Patel75855802010-02-16 21:41:20 +00001547 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings7d7bc562010-07-19 23:56:31 +00001548 getOrCreateFile(CurLoc),
Devang Patelc5ffabc2010-05-12 23:46:38 +00001549 getLineNumber(CurLoc),
1550 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001551 llvm::MDNode *DN = D;
1552 RegionStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001553}
1554
1555/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1556/// region - "llvm.dbg.region.end."
Devang Patel11a42a42010-07-20 22:20:10 +00001557void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001558 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1559
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001560 // Provide an region stop point.
Devang Patel11a42a42010-07-20 22:20:10 +00001561 EmitStopPoint(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001562
Sanjiv Gupta98070572008-05-25 05:15:42 +00001563 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001564}
1565
Devang Patel0884a602010-07-22 22:29:16 +00001566/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1567void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1568 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1569 unsigned RCount = FnBeginRegionCount.back();
1570 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1571
1572 // Pop all regions for this function.
1573 while (RegionStack.size() != RCount)
1574 EmitRegionEnd(Builder);
1575 FnBeginRegionCount.pop_back();
1576}
1577
Devang Patel535fdaf2010-02-10 18:49:08 +00001578// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1579// See BuildByRefType.
1580llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1581 uint64_t *XOffset) {
1582
1583 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1584
1585 QualType FType;
1586 uint64_t FieldSize, FieldOffset;
1587 unsigned FieldAlign;
1588
Devang Patel408dcf62010-03-09 00:44:50 +00001589 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001590 QualType Type = VD->getType();
1591
1592 FieldOffset = 0;
1593 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001594 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1595 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001596 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001597 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1598 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1599
Devang Patel535fdaf2010-02-10 18:49:08 +00001600 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1601 if (HasCopyAndDispose) {
1602 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001603 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1604 &FieldOffset));
1605 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1606 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001607 }
1608
1609 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1610 if (Align > CharUnits::fromQuantity(
1611 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1612 unsigned AlignedOffsetInBytes
1613 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1614 unsigned NumPaddingBytes
1615 = AlignedOffsetInBytes - FieldOffset/8;
1616
1617 if (NumPaddingBytes > 0) {
1618 llvm::APInt pad(32, NumPaddingBytes);
1619 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1620 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001621 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001622 }
1623 }
1624
1625 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001626 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00001627 FieldSize = CGM.getContext().getTypeSize(FType);
1628 FieldAlign = Align.getQuantity()*8;
1629
1630 *XOffset = FieldOffset;
1631 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001632 VD->getName(), Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001633 0, FieldSize, FieldAlign,
1634 FieldOffset, 0, FieldTy);
1635 EltTys.push_back(FieldTy);
1636 FieldOffset += FieldSize;
1637
1638 llvm::DIArray Elements =
1639 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1640
1641 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1642
1643 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001644 Unit, "", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001645 0, FieldOffset, 0, 0, Flags,
1646 llvm::DIType(), Elements);
1647
1648}
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001649/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00001650void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattneraffb3732008-11-10 06:08:34 +00001651 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001652 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1653
Devang Patel408dcf62010-03-09 00:44:50 +00001654 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001655 llvm::DIType Ty;
1656 uint64_t XOffset = 0;
1657 if (VD->hasAttr<BlocksAttr>())
1658 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1659 else
1660 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00001661
Devang Patel67eba802010-05-07 23:05:55 +00001662 // If there is not any debug info for type then do not emit debug info
1663 // for this variable.
1664 if (!Ty)
1665 return;
1666
Chris Lattneraffb3732008-11-10 06:08:34 +00001667 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001668 unsigned Line = getLineNumber(VD->getLocation());
1669 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001670
Chris Lattneraffb3732008-11-10 06:08:34 +00001671 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001672 llvm::DIVariable D =
Devang Patelb40f2952009-11-13 19:10:24 +00001673 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel1c0954c2010-02-01 21:39:52 +00001674 VD->getName(),
Devang Patel6ccba0f2010-06-05 01:14:40 +00001675 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattneraffb3732008-11-10 06:08:34 +00001676 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001677 llvm::Instruction *Call =
Devang Patelaf993bf2009-11-10 23:07:24 +00001678 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001679
Chris Lattnere675d0f2010-04-01 06:31:43 +00001680 llvm::MDNode *Scope = RegionStack.back();
Chris Lattner18a584b2010-04-02 20:21:43 +00001681 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001682}
1683
Mike Stump2e722b92009-09-30 02:43:10 +00001684/// EmitDeclare - Emit local variable declaration debug info.
1685void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1686 llvm::Value *Storage, CGBuilderTy &Builder,
1687 CodeGenFunction *CGF) {
Devang Patel3efd1472010-02-01 21:52:22 +00001688 const ValueDecl *VD = BDRE->getDecl();
Mike Stump2e722b92009-09-30 02:43:10 +00001689 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1690
Devang Patel42fb6f82010-04-26 23:28:46 +00001691 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001692 return;
1693
1694 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00001695 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001696 llvm::DIType Ty;
1697 if (VD->hasAttr<BlocksAttr>())
1698 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1699 else
1700 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00001701
1702 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001703 unsigned Line = getLineNumber(VD->getLocation());
1704 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00001705
Devang Patel3efd1472010-02-01 21:52:22 +00001706 CharUnits offset = CGF->BlockDecls[VD];
Mike Stump2e722b92009-09-30 02:43:10 +00001707 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattnerbf784782010-01-25 03:29:35 +00001708 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1709 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1710 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1711 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001712 if (BDRE->isByRef()) {
Chris Lattnerbf784782010-01-25 03:29:35 +00001713 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1714 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001715 // offset of __forwarding field
1716 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001717 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1718 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1719 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001720 // offset of x field
1721 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001722 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001723 }
1724
1725 // Create the descriptor for the variable.
1726 llvm::DIVariable D =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001727 DebugFactory.CreateComplexVariable(Tag,
1728 llvm::DIDescriptor(RegionStack.back()),
Devang Patel3efd1472010-02-01 21:52:22 +00001729 VD->getName(), Unit, Line, Ty,
Mike Stump2e722b92009-09-30 02:43:10 +00001730 addr);
1731 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001732 llvm::Instruction *Call =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001733 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattner5e124bf2009-12-28 21:44:41 +00001734
Chris Lattnere675d0f2010-04-01 06:31:43 +00001735 llvm::MDNode *Scope = RegionStack.back();
Devang Patel82bbfb52010-05-10 23:48:38 +00001736 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stump2e722b92009-09-30 02:43:10 +00001737}
1738
Devang Patel3efd1472010-02-01 21:52:22 +00001739void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattneraffb3732008-11-10 06:08:34 +00001740 llvm::Value *Storage,
1741 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001742 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001743}
1744
Mike Stump2e722b92009-09-30 02:43:10 +00001745void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1746 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1747 CodeGenFunction *CGF) {
1748 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1749}
1750
Chris Lattneraffb3732008-11-10 06:08:34 +00001751/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1752/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00001753void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattneraffb3732008-11-10 06:08:34 +00001754 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001755 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001756}
1757
1758
1759
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001760/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001761void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00001762 const VarDecl *D) {
1763
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001764 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001765 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001766 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00001767
Devang Patel7b7f46f2010-02-01 21:34:11 +00001768 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001769 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001770
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001771 // CodeGen turns int[] into int[1] so we'll do the same here.
1772 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001773
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001774 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001775 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001776
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001777 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001778 ArrayType::Normal, 0);
1779 }
Devang Pateldfcd0662010-04-29 17:48:37 +00001780 llvm::StringRef DeclName = D->getName();
Devang Patel98f21712010-05-13 23:52:37 +00001781 llvm::StringRef LinkageName;
Devang Patelec2a9ab2010-05-14 16:55:25 +00001782 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00001783 LinkageName = Var->getName();
Devang Patel7b7f46f2010-02-01 21:34:11 +00001784 llvm::DIDescriptor DContext =
1785 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel98f21712010-05-13 23:52:37 +00001786 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1787 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001788 Var->hasInternalLinkage(),
1789 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001790}
1791
Devang Patelf4c205b2009-02-26 21:10:26 +00001792/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001793void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00001794 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001795 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001796 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001797 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00001798
Devang Patel3efd1472010-02-01 21:52:22 +00001799 llvm::StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001800
Devang Patel3efd1472010-02-01 21:52:22 +00001801 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001802 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001803
Devang Patelf4c205b2009-02-26 21:10:26 +00001804 // CodeGen turns int[] into int[1] so we'll do the same here.
1805 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001806
Devang Patelf4c205b2009-02-26 21:10:26 +00001807 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001808 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001809
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001810 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001811 ArrayType::Normal, 0);
1812 }
1813
Devang Patele4f2b2a2009-10-20 18:26:30 +00001814 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001815 getOrCreateType(T, Unit),
1816 Var->hasInternalLinkage(),
1817 true/*definition*/, Var);
1818}
Devang Patel973f2eb2010-02-01 19:16:32 +00001819
Devang Pateldc866e12010-08-10 17:53:33 +00001820/// EmitGlobalVariable - Emit global variable's debug info.
1821void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1822 llvm::ConstantInt *Init,
Devang Patele03edfd2010-08-10 07:24:25 +00001823 CGBuilderTy &Builder) {
1824 // Create the descriptor for the variable.
1825 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1826 llvm::StringRef Name = VD->getName();
Devang Patel76e3b532010-08-10 18:27:15 +00001827 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel41c20972010-08-23 22:07:25 +00001828 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1829 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1830 Ty = CreateEnumType(ED, Unit);
1831 }
Devang Patel76e3b532010-08-10 18:27:15 +00001832 // Do not use DIGlobalVariable for enums.
1833 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1834 return;
Devang Patele03edfd2010-08-10 07:24:25 +00001835 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1836 getLineNumber(VD->getLocation()),
Devang Patel81665712010-08-10 20:16:57 +00001837 Ty, true, true, Init);
Devang Patele03edfd2010-08-10 07:24:25 +00001838}
1839
Devang Patel973f2eb2010-02-01 19:16:32 +00001840/// getOrCreateNamesSpace - Return namespace descriptor for the given
1841/// namespace decl.
1842llvm::DINameSpace
1843CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1844 llvm::DIDescriptor Unit) {
1845 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1846 NameSpaceCache.find(NSDecl);
1847 if (I != NameSpaceCache.end())
1848 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1849
Devang Patelc5ffabc2010-05-12 23:46:38 +00001850 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00001851
1852 llvm::DIDescriptor Context =
Devang Patel7b7f46f2010-02-01 21:34:11 +00001853 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patel973f2eb2010-02-01 19:16:32 +00001854 llvm::DINameSpace NS =
1855 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Eli Friedman04831922010-08-22 01:00:03 +00001856 llvm::DIFile(Unit), LineNo);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001857 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00001858 return NS;
1859}