blob: 6b79b3ed43bb1cba09d1c9383c6312d1ce04fda3 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000019#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000020#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000022#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000024#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000025#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-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 Guptae8b9f5b2008-05-08 08:54:20 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000033#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000034#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000036using namespace clang;
37using namespace clang::CodeGen;
38
Anders Carlsson20f12a22009-12-06 18:00:51 +000039CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000040 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000041 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000042 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043}
44
Chris Lattner9c85ba32008-11-10 06:08:34 +000045CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000046 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000047}
48
Chris Lattner9c85ba32008-11-10 06:08:34 +000049void CGDebugInfo::setLocation(SourceLocation Loc) {
50 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000051 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000052}
53
Devang Patel33583052010-01-28 23:15:27 +000054/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000055llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000056 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-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 Patel411894b2010-02-01 22:40:08 +000064
Devang Pateleb6d79b2010-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 Patel8b90a782010-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 Patel979ec2e2009-10-06 00:35:31 +000076 return CompileUnit;
77}
78
Devang Patel9c6c3a02010-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 Patel89f05f82010-01-28 18:21:00 +000092 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000093 memcpy(StrPtr, NS.data(), NS.length());
94 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000095}
96
Devang Patel700a1cb2010-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 Patel17800552010-03-09 00:44:50 +0000131/// getOrCreateFile - Get the file debug info descriptor for the input location.
132llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000133 if (!Loc.isValid())
Devang Patel17800552010-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 Carlsson20f12a22009-12-06 18:00:51 +0000137 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000138 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-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 Patelac4d13c2010-07-27 15:17:16 +0000151 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
152 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000153
Devang Patelab699792010-05-07 18:12:35 +0000154 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000155 return F;
156
Devang Patel17800552010-03-09 00:44:50 +0000157}
Devang Patel8ab870d2010-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 Patelac4d13c2010-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 Patel17800552010-03-09 00:44:50 +0000187/// CreateCompileUnit - Create new compile unit.
188void CGDebugInfo::CreateCompileUnit() {
189
190 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000191 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000192 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
193 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000194 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000195
Douglas Gregorf6728fc2010-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 Patel6e6bc392010-07-23 23:04:28 +0000200 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000201 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000202 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000203 if (MainFileDir != ".")
204 MainFileName = MainFileDir + "/" + MainFileName;
205 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000206
Devang Patelac4d13c2010-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 Lattner515455a2009-03-25 03:28:08 +0000212 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000213 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-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 Patel8d9aefc2009-03-24 20:35:51 +0000220 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000221 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000222 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000223 } else {
224 LangTag = llvm::dwarf::DW_LANG_C89;
225 }
Devang Patel446c6192009-04-17 21:06:59 +0000226
Benjamin Kramer47daf682009-12-08 11:02:29 +0000227 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000228#ifdef CLANG_VENDOR
229 CLANG_VENDOR
230#endif
231 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000232
233 // Figure out which version of the ObjC runtime we have.
234 unsigned RuntimeVers = 0;
235 if (LO.ObjC1)
236 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000238 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000239 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000240 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000241 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000242 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000243}
244
Devang Patel65e99f22009-02-25 01:36:11 +0000245/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246/// one if necessary.
247llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000248 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000250 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000251 switch (BT->getKind()) {
252 default:
253 case BuiltinType::Void:
254 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000255 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000256 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000257 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000258 llvm::DIType::FlagFwdDecl,
259 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000260 case BuiltinType::ObjCId: {
261 // typedef struct objc_class *Class;
262 // typedef struct objc_object {
263 // Class isa;
264 // } *id;
265
266 llvm::DIType OCTy =
267 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
268 Unit, "objc_class", Unit, 0, 0, 0, 0,
269 llvm::DIType::FlagFwdDecl,
270 llvm::DIType(), llvm::DIArray());
271 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
272
273 llvm::DIType ISATy =
274 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
275 Unit, "", Unit,
276 0, Size, 0, 0, 0, OCTy);
277
278 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
279
280 llvm::DIType FieldTy =
281 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
282 "isa", Unit,
283 0,Size, 0, 0, 0, ISATy);
284 EltTys.push_back(FieldTy);
285 llvm::DIArray Elements =
286 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
287
288 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
289 Unit, "objc_object", Unit, 0, 0, 0, 0,
290 0,
291 llvm::DIType(), Elements);
292 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000293 case BuiltinType::UChar:
294 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
295 case BuiltinType::Char_S:
296 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
297 case BuiltinType::UShort:
298 case BuiltinType::UInt:
299 case BuiltinType::ULong:
300 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
301 case BuiltinType::Short:
302 case BuiltinType::Int:
303 case BuiltinType::Long:
304 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
305 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
306 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000307 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000308 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000309 }
Devang Patel05127ca2010-07-28 23:23:29 +0000310
311 switch (BT->getKind()) {
312 case BuiltinType::Long: BTName = "long int"; break;
313 case BuiltinType::LongLong: BTName = "long long int"; break;
314 case BuiltinType::ULong: BTName = "long unsigned int"; break;
315 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
316 default:
317 BTName = BT->getName(CGM.getContext().getLangOptions());
318 break;
319 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000320 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000321 uint64_t Size = CGM.getContext().getTypeSize(BT);
322 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000323 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000324
Devang Patelca80a5f2009-10-20 19:55:01 +0000325 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000326 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000327 Unit, 0, Size, Align,
328 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000329 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000330}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000331
Chris Lattnerb7003772009-04-23 06:13:01 +0000332llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000333 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000334 // Bit size, align and offset of the type.
335 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
336 if (Ty->isComplexIntegerType())
337 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Anders Carlsson20f12a22009-12-06 18:00:51 +0000339 uint64_t Size = CGM.getContext().getTypeSize(Ty);
340 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000341 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000342
Devang Patelca80a5f2009-10-20 19:55:01 +0000343 llvm::DIType DbgTy =
344 DebugFactory.CreateBasicType(Unit, "complex",
345 Unit, 0, Size, Align,
346 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000347 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000348}
349
John McCalla1805292009-09-25 01:40:47 +0000350/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000351/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000352llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000353 QualifierCollector Qc;
354 const Type *T = Qc.strip(Ty);
355
356 // Ignore these qualifiers for now.
357 Qc.removeObjCGCAttr();
358 Qc.removeAddressSpace();
359
Chris Lattner9c85ba32008-11-10 06:08:34 +0000360 // We will create one Derived type for one qualifier and recurse to handle any
361 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000362 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000363 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000364 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000365 Qc.removeConst();
366 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000368 Qc.removeVolatile();
369 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000370 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000371 Qc.removeRestrict();
372 } else {
373 assert(Qc.empty() && "Unknown type qualifier for debug info");
374 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375 }
Mike Stump1eb44332009-09-09 15:08:12 +0000376
John McCalla1805292009-09-25 01:40:47 +0000377 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
378
Daniel Dunbar3845f862008-10-31 03:54:29 +0000379 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
380 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000381 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000382 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000383 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000384 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000385}
386
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000387llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000388 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000389 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000390 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
391 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000392 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000393}
394
Chris Lattner9c85ba32008-11-10 06:08:34 +0000395llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000396 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000397 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
398 Ty->getPointeeType(), Unit);
399}
400
401llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
402 const Type *Ty,
403 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000404 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000405 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000406
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000407 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000408
409 // Size is always the size of a pointer. We can't use getTypeSize here
410 // because that does not return the correct value for references.
411 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000412 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
413 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000414
Devang Patelca80a5f2009-10-20 19:55:01 +0000415 return
Devang Pateld58562e2010-03-09 22:49:11 +0000416 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000417 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000418
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000419}
420
Mike Stump9bc093c2009-05-14 02:03:51 +0000421llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000422 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000423 if (BlockLiteralGenericSet)
424 return BlockLiteralGeneric;
425
Mike Stump9bc093c2009-05-14 02:03:51 +0000426 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
427
428 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
429
430 llvm::DIType FieldTy;
431
432 QualType FType;
433 uint64_t FieldSize, FieldOffset;
434 unsigned FieldAlign;
435
436 llvm::DIArray Elements;
437 llvm::DIType EltTy, DescTy;
438
439 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000440 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000441 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
442 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000443
Daniel Dunbarca308df2009-05-26 19:40:20 +0000444 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000445 EltTys.clear();
446
Mike Stump3d363c52009-10-02 02:30:50 +0000447 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000448 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000449
Mike Stump9bc093c2009-05-14 02:03:51 +0000450 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000451 Unit, LineNo, FieldOffset, 0, 0,
452 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Mike Stump9bc093c2009-05-14 02:03:51 +0000454 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000455 uint64_t Size = CGM.getContext().getTypeSize(Ty);
456 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Mike Stump9bc093c2009-05-14 02:03:51 +0000458 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000459 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000460 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000461
462 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000463 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000464 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000465 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000466 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
467 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000468 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000469 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000470
Anders Carlsson20f12a22009-12-06 18:00:51 +0000471 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000473 FieldSize = CGM.getContext().getTypeSize(Ty);
474 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000475 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000476 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000477 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000478 FieldOffset, 0, FieldTy);
479 EltTys.push_back(FieldTy);
480
481 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000482 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000483
484 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000485 Unit, LineNo, FieldOffset, 0, 0,
486 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Mike Stump9bc093c2009-05-14 02:03:51 +0000488 BlockLiteralGenericSet = true;
489 BlockLiteralGeneric
490 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000491 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000492 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000493 return BlockLiteralGeneric;
494}
495
Chris Lattner9c85ba32008-11-10 06:08:34 +0000496llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000497 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000498 // Typedefs are derived from some other type. If we have a typedef of a
499 // typedef, make sure to emit the whole chain.
500 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000501
Chris Lattner9c85ba32008-11-10 06:08:34 +0000502 // We don't set size information, but do specify where the typedef was
503 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000504 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000505
Devang Pateleb6d79b2010-02-01 21:34:11 +0000506 llvm::DIDescriptor TyContext
507 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
508 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000509 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000510 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000511 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000512 Ty->getDecl()->getName(), Unit,
513 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000514 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000515}
516
Chris Lattner9c85ba32008-11-10 06:08:34 +0000517llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000518 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000519 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000520
Chris Lattner9c85ba32008-11-10 06:08:34 +0000521 // Add the result type at least.
522 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000523
Chris Lattner9c85ba32008-11-10 06:08:34 +0000524 // Set up remainder of arguments if there is a prototype.
525 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000526 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000527 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
528 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
529 } else {
530 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000531 }
532
Chris Lattner9c85ba32008-11-10 06:08:34 +0000533 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000534 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000535
Devang Patelca80a5f2009-10-20 19:55:01 +0000536 llvm::DIType DbgTy =
537 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000538 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000539 0, 0, 0, 0, 0,
540 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000541 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000542}
543
Devang Patel428deb52010-01-19 00:00:59 +0000544/// CollectRecordFields - A helper function to collect debug info for
545/// record fields. This is used while creating debug info entry for a Record.
546void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000547CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000548 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
549 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000550 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
551 for (RecordDecl::field_iterator I = RD->field_begin(),
552 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000553 I != E; ++I, ++FieldNo) {
554 FieldDecl *Field = *I;
555 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000556 llvm::StringRef FieldName = Field->getName();
557
Devang Patel4835fdd2010-02-12 01:31:06 +0000558 // Ignore unnamed fields. Do not ignore unnamed records.
559 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000560 continue;
561
562 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000563 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
564 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000565 QualType FType = Field->getType();
566 uint64_t FieldSize = 0;
567 unsigned FieldAlign = 0;
568 if (!FType->isIncompleteArrayType()) {
569
570 // Bit size, align and offset of the type.
571 FieldSize = CGM.getContext().getTypeSize(FType);
572 Expr *BitWidth = Field->getBitWidth();
573 if (BitWidth)
574 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000575 FieldAlign = CGM.getContext().getTypeAlign(FType);
576 }
577
578 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
579
Devang Patel71f4ff62010-04-21 23:12:37 +0000580 unsigned Flags = 0;
581 AccessSpecifier Access = I->getAccess();
582 if (Access == clang::AS_private)
583 Flags |= llvm::DIType::FlagPrivate;
584 else if (Access == clang::AS_protected)
585 Flags |= llvm::DIType::FlagProtected;
586
Devang Patel428deb52010-01-19 00:00:59 +0000587 // Create a DW_TAG_member node to remember the offset of this field in the
588 // struct. FIXME: This is an absolutely insane way to capture this
589 // information. When we gut debug info, this should be fixed.
590 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
591 FieldName, FieldDefUnit,
592 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000593 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000594 EltTys.push_back(FieldTy);
595 }
596}
597
Devang Patela6da1922010-01-28 00:28:01 +0000598/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
599/// function type is not updated to include implicit "this" pointer. Use this
600/// routine to get a method type which includes "this" pointer.
601llvm::DIType
602CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000603 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000604 llvm::DIType FnTy
605 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
606 0),
607 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000608
609 // Static methods do not need "this" pointer argument.
610 if (Method->isStatic())
611 return FnTy;
612
Devang Patela6da1922010-01-28 00:28:01 +0000613 // Add "this" pointer.
614
Devang Patelab699792010-05-07 18:12:35 +0000615 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000616 assert (Args.getNumElements() && "Invalid number of arguments!");
617
618 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
619
620 // First element is always return type. For 'void' functions it is NULL.
621 Elts.push_back(Args.getElement(0));
622
623 // "this" pointer is always first argument.
624 ASTContext &Context = CGM.getContext();
625 QualType ThisPtr =
626 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000627 llvm::DIType ThisPtrType =
628 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000629
Devang Patelab699792010-05-07 18:12:35 +0000630 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000631 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000632
633 // Copy rest of the arguments.
634 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
635 Elts.push_back(Args.getElement(i));
636
637 llvm::DIArray EltTypeArray =
638 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
639
640 return
641 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000642 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000643 0, 0, 0, 0, 0,
644 llvm::DIType(), EltTypeArray);
645}
646
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000647/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
648/// a single member function GlobalDecl.
649llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000650CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000651 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000652 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000653 bool IsCtorOrDtor =
654 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
655
656 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000657 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000658
659 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
660 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000661 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000662 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000663 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000664
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000665 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000666 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
667 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000668
669 // Collect virtual method info.
670 llvm::DIType ContainingType;
671 unsigned Virtuality = 0;
672 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000673
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000674 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000675 if (Method->isPure())
676 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
677 else
678 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
679
680 // It doesn't make sense to give a virtual destructor a vtable index,
681 // since a single destructor has two entries in the vtable.
682 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000683 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000684 ContainingType = RecordTy;
685 }
686
687 llvm::DISubprogram SP =
688 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
689 MethodLinkageName,
690 MethodDefUnit, MethodLine,
691 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000692 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000693 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000694 Method->isImplicit(),
695 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000696
697 // Don't cache ctors or dtors since we have to emit multiple functions for
698 // a single ctor or dtor.
699 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000700 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000701
702 return SP;
703}
704
Devang Patel4125fd22010-01-19 01:54:44 +0000705/// CollectCXXMemberFunctions - A helper function to collect debug info for
706/// C++ member functions.This is used while creating debug info entry for
707/// a Record.
708void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000709CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000710 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000711 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000712 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
713 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000714 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000715
Devang Pateld5322da2010-02-09 19:09:28 +0000716 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000717 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000718
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000719 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000720 }
721}
722
Devang Patela245c5b2010-01-25 23:32:18 +0000723/// CollectCXXBases - A helper function to collect debug info for
724/// C++ base classes. This is used while creating debug info entry for
725/// a Record.
726void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000727CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000728 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000729 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000730
Devang Patel239cec62010-02-01 21:39:52 +0000731 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
732 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
733 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000734 unsigned BFlags = 0;
735 uint64_t BaseOffset;
736
737 const CXXRecordDecl *Base =
738 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
739
740 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000741 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000742 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000743 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000744 BFlags = llvm::DIType::FlagVirtual;
745 } else
746 BaseOffset = RL.getBaseClassOffset(Base);
747
748 AccessSpecifier Access = BI->getAccessSpecifier();
749 if (Access == clang::AS_private)
750 BFlags |= llvm::DIType::FlagPrivate;
751 else if (Access == clang::AS_protected)
752 BFlags |= llvm::DIType::FlagProtected;
753
754 llvm::DIType DTy =
755 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
756 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000757 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000758 BaseOffset, BFlags,
759 getOrCreateType(BI->getType(),
760 Unit));
761 EltTys.push_back(DTy);
762 }
Devang Patela245c5b2010-01-25 23:32:18 +0000763}
764
Devang Patel4ce3f202010-01-28 18:11:52 +0000765/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000766llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000767 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000768 return VTablePtrType;
769
770 ASTContext &Context = CGM.getContext();
771
772 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000773 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
774 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000775 llvm::DIType SubTy =
776 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000777 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000778 0, 0, 0, 0, 0, llvm::DIType(), SElements);
779
780 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
781 llvm::DIType vtbl_ptr_type
782 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000783 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000784 0, Size, 0, 0, 0, SubTy);
785
Devang Pateld58562e2010-03-09 22:49:11 +0000786 VTablePtrType =
787 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
788 Unit, "", Unit,
789 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000790 return VTablePtrType;
791}
792
Anders Carlsson046c2942010-04-17 20:15:18 +0000793/// getVTableName - Get vtable name for the given Class.
794llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000795 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000796 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000797
798 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000799 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000800 memcpy(StrPtr, Name.data(), Name.length());
801 return llvm::StringRef(StrPtr, Name.length());
802}
803
804
Anders Carlsson046c2942010-04-17 20:15:18 +0000805/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000806/// debug info entry in EltTys vector.
807void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000808CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000809 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000810 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000811
812 // If there is a primary base then it will hold vtable info.
813 if (RL.getPrimaryBase())
814 return;
815
816 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000817 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000818 return;
819
820 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
821 llvm::DIType VPTR
822 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000823 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000824 0, Size, 0, 0, 0,
825 getOrCreateVTablePtrType(Unit));
826 EltTys.push_back(VPTR);
827}
828
Devang Patel65e99f22009-02-25 01:36:11 +0000829/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000830llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000831 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000832 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000833
Chris Lattner9c85ba32008-11-10 06:08:34 +0000834 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000835 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000837 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000838 Tag = llvm::dwarf::DW_TAG_union_type;
839 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000840 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000841 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000842 }
843
Chris Lattner9c85ba32008-11-10 06:08:34 +0000844 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000845 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
846 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000847
Chris Lattner9c85ba32008-11-10 06:08:34 +0000848 // Records and classes and unions can all be recursive. To handle them, we
849 // first generate a debug descriptor for the struct as a forward declaration.
850 // Then (if it is a definition) we go through and get debug info for all of
851 // its members. Finally, we create a descriptor for the complete type (which
852 // may refer to the forward decl if the struct is recursive) and replace all
853 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000854 llvm::DIDescriptor FDContext =
855 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
856
857 // If this is just a forward declaration, construct an appropriately
858 // marked node and just return it.
859 if (!RD->getDefinition()) {
860 llvm::DICompositeType FwdDecl =
861 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
862 DefUnit, Line, 0, 0, 0,
863 llvm::DIType::FlagFwdDecl,
864 llvm::DIType(), llvm::DIArray());
865
866 return FwdDecl;
867 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000868
Dan Gohman86968372010-08-20 22:39:57 +0000869 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000870
Devang Patelab699792010-05-07 18:12:35 +0000871 llvm::MDNode *MN = FwdDecl;
872 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000873 // Otherwise, insert it into the TypeCache so that recursive uses will find
874 // it.
Devang Patelab699792010-05-07 18:12:35 +0000875 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000876 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000877 RegionStack.push_back(FwdDeclNode);
878 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879
880 // Convert all the elements.
881 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
882
Devang Pateld6c5a262010-02-01 21:52:22 +0000883 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000884 if (CXXDecl) {
885 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000886 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000887 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000888
889 // Collect static variables with initializers.
890 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
891 I != E; ++I)
892 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
893 if (const Expr *Init = V->getInit()) {
894 Expr::EvalResult Result;
895 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
896 llvm::ConstantInt *CI
897 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
898
899 // Create the descriptor for static variable.
900 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
901 llvm::StringRef VName = V->getName();
902 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
903 // Do not use DIGlobalVariable for enums.
904 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
905 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
906 getLineNumber(V->getLocation()),
907 VTy, true, true, CI);
908 }
909 }
910 }
911 }
912
Devang Pateld6c5a262010-02-01 21:52:22 +0000913 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000914 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000915 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000916 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000917
918 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000919 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000920 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
921 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000922 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000923 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000924 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000925 }
Mike Stump1eb44332009-09-09 15:08:12 +0000926
Chris Lattner9c85ba32008-11-10 06:08:34 +0000927 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000928 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000929
930 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000931 uint64_t Size = CGM.getContext().getTypeSize(Ty);
932 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Devang Patele4c1ea02010-03-11 20:01:48 +0000934 RegionStack.pop_back();
935 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
936 RegionMap.find(Ty->getDecl());
937 if (RI != RegionMap.end())
938 RegionMap.erase(RI);
939
Devang Patel411894b2010-02-01 22:40:08 +0000940 llvm::DIDescriptor RDContext =
941 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000942
943 llvm::StringRef RDName = RD->getName();
944 // If this is a class, include the template arguments also.
945 if (Tag == llvm::dwarf::DW_TAG_class_type)
946 RDName = getClassName(RD);
947
Devang Patel0ce73f62009-07-22 18:57:00 +0000948 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000949 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000950 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000951 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000952 llvm::DIType(), Elements,
953 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000954
955 // Now that we have a real decl for the struct, replace anything using the
956 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +0000957 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000958 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000959 return RealDecl;
960}
961
John McCallc12c5bb2010-05-15 11:32:37 +0000962/// CreateType - get objective-c object type.
963llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
964 llvm::DIFile Unit) {
965 // Ignore protocols.
966 return getOrCreateType(Ty->getBaseType(), Unit);
967}
968
Devang Patel9ca36b62009-02-26 21:10:26 +0000969/// CreateType - get objective-c interface type.
970llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000971 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000972 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000973 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000974
975 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000976 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000977 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000978 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000979
Dan Gohman45f7c782010-08-23 21:15:56 +0000980 // If this is just a forward declaration, return a special forward-declaration
981 // debug type.
982 if (ID->isForwardDecl()) {
983 llvm::DICompositeType FwdDecl =
984 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
985 DefUnit, Line, 0, 0, 0, 0,
986 llvm::DIType(), llvm::DIArray(),
987 RuntimeLang);
988 return FwdDecl;
989 }
990
Devang Patel9ca36b62009-02-26 21:10:26 +0000991 // To handle recursive interface, we
992 // first generate a debug descriptor for the struct as a forward declaration.
993 // Then (if it is a definition) we go through and get debug info for all of
994 // its members. Finally, we create a descriptor for the complete type (which
995 // may refer to the forward decl if the struct is recursive) and replace all
996 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +0000997 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Devang Patelab699792010-05-07 18:12:35 +0000999 llvm::MDNode *MN = FwdDecl;
1000 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001001 // Otherwise, insert it into the TypeCache so that recursive uses will find
1002 // it.
Devang Patelab699792010-05-07 18:12:35 +00001003 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001004 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001005 RegionStack.push_back(FwdDeclNode);
1006 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001007
1008 // Convert all the elements.
1009 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1010
Devang Pateld6c5a262010-02-01 21:52:22 +00001011 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001012 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001013 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001014 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001015 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001016 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001017 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001018 0 /* offset */, 0, SClassTy);
1019 EltTys.push_back(InhTag);
1020 }
1021
Devang Pateld6c5a262010-02-01 21:52:22 +00001022 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001023
1024 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001025 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1026 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001027 ObjCIvarDecl *Field = *I;
1028 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1029
Devang Patel73621622009-11-25 17:37:31 +00001030 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001031
Devang Patelde135022009-04-27 22:40:36 +00001032 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001033 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001034 continue;
1035
Devang Patel9ca36b62009-02-26 21:10:26 +00001036 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001037 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1038 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001039 QualType FType = Field->getType();
1040 uint64_t FieldSize = 0;
1041 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001042
Devang Patel99c20eb2009-03-20 18:24:39 +00001043 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001044
Devang Patel99c20eb2009-03-20 18:24:39 +00001045 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001046 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001047 Expr *BitWidth = Field->getBitWidth();
1048 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001049 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001050
Anders Carlsson20f12a22009-12-06 18:00:51 +00001051 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001052 }
1053
Mike Stump1eb44332009-09-09 15:08:12 +00001054 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1055
Devang Patelc20482b2009-03-19 00:23:53 +00001056 unsigned Flags = 0;
1057 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1058 Flags = llvm::DIType::FlagProtected;
1059 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1060 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001061
Devang Patel9ca36b62009-02-26 21:10:26 +00001062 // Create a DW_TAG_member node to remember the offset of this field in the
1063 // struct. FIXME: This is an absolutely insane way to capture this
1064 // information. When we gut debug info, this should be fixed.
1065 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1066 FieldName, FieldDefUnit,
1067 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001068 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001069 EltTys.push_back(FieldTy);
1070 }
Mike Stump1eb44332009-09-09 15:08:12 +00001071
Devang Patel9ca36b62009-02-26 21:10:26 +00001072 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001073 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001074
Devang Patele4c1ea02010-03-11 20:01:48 +00001075 RegionStack.pop_back();
1076 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1077 RegionMap.find(Ty->getDecl());
1078 if (RI != RegionMap.end())
1079 RegionMap.erase(RI);
1080
Devang Patel9ca36b62009-02-26 21:10:26 +00001081 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001082 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1083 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001084
Devang Patel6c1fddf2009-07-27 18:42:03 +00001085 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001086 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001087 Line, Size, Align, 0, 0, llvm::DIType(),
1088 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001089
1090 // Now that we have a real decl for the struct, replace anything using the
1091 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001092 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001093 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001094
Devang Patel9ca36b62009-02-26 21:10:26 +00001095 return RealDecl;
1096}
1097
Chris Lattner9c85ba32008-11-10 06:08:34 +00001098llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001099 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001100 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001101
1102 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1103
1104 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001105 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001106 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001107 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001108 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001109 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Chris Lattner9c85ba32008-11-10 06:08:34 +00001112 // Return a CompositeType for the enum itself.
1113 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001114 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001115
Devang Patel8ab870d2010-05-12 23:46:38 +00001116 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1117 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001118
Chris Lattner9c85ba32008-11-10 06:08:34 +00001119 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001120 uint64_t Size = 0;
1121 unsigned Align = 0;
1122 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001123 Size = CGM.getContext().getTypeSize(Ty);
1124 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001125 }
Mike Stump1eb44332009-09-09 15:08:12 +00001126
Devang Patelca80a5f2009-10-20 19:55:01 +00001127 llvm::DIType DbgTy =
1128 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001129 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001130 Size, Align, 0, 0,
1131 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001132 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001133}
1134
1135llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001136 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001137 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1138 return CreateType(RT, Unit);
1139 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1140 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001141
Chris Lattner9c85ba32008-11-10 06:08:34 +00001142 return llvm::DIType();
1143}
1144
Devang Patel70c23cd2010-02-23 22:59:39 +00001145llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001146 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001147 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1148 uint64_t NumElems = Ty->getNumElements();
1149 if (NumElems > 0)
1150 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001151
Benjamin Kramerad468862010-03-30 11:36:44 +00001152 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1153 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001154
1155 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1156 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1157
1158 return
1159 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001160 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001161 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001162 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001163}
1164
Chris Lattner9c85ba32008-11-10 06:08:34 +00001165llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001166 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001167 uint64_t Size;
1168 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001169
1170
Nuno Lopes010d5142009-01-28 00:35:17 +00001171 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001172 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001173 Size = 0;
1174 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001175 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001176 } else if (Ty->isIncompleteArrayType()) {
1177 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001178 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001179 } else {
1180 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001181 Size = CGM.getContext().getTypeSize(Ty);
1182 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001183 }
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Chris Lattner9c85ba32008-11-10 06:08:34 +00001185 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1186 // interior arrays, do we care? Why aren't nested arrays represented the
1187 // obvious/recursive way?
1188 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1189 QualType EltTy(Ty, 0);
1190 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001191 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001192 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001193 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001194 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001195 // FIXME: Verify this is right for VLAs.
1196 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1197 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001198 }
Mike Stump1eb44332009-09-09 15:08:12 +00001199
Chris Lattner9c85ba32008-11-10 06:08:34 +00001200 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001201 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001202
Devang Patelca80a5f2009-10-20 19:55:01 +00001203 llvm::DIType DbgTy =
1204 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001205 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001206 0, Size, Align, 0, 0,
1207 getOrCreateType(EltTy, Unit),
1208 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001209 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001210}
1211
Anders Carlssona031b352009-11-06 19:19:55 +00001212llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001213 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001214 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1215 Ty, Ty->getPointeeType(), Unit);
1216}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001217
Anders Carlsson20f12a22009-12-06 18:00:51 +00001218llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001219 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001220 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1221 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1222
1223 if (!Ty->getPointeeType()->isFunctionType()) {
1224 // We have a data member pointer type.
1225 return PointerDiffDITy;
1226 }
1227
1228 // We have a member function pointer type. Treat it as a struct with two
1229 // ptrdiff_t members.
1230 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1231
1232 uint64_t FieldOffset = 0;
1233 llvm::DIDescriptor ElementTypes[2];
1234
1235 // FIXME: This should probably be a function type instead.
1236 ElementTypes[0] =
1237 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001238 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001239 Info.first, Info.second, FieldOffset, 0,
1240 PointerDiffDITy);
1241 FieldOffset += Info.first;
1242
1243 ElementTypes[1] =
1244 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001245 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001246 Info.first, Info.second, FieldOffset, 0,
1247 PointerDiffDITy);
1248
1249 llvm::DIArray Elements =
1250 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1251 llvm::array_lengthof(ElementTypes));
1252
1253 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1254 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001255 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001256 0, 0, 0, llvm::DIType(), Elements);
1257}
1258
Douglas Gregor840943d2009-12-21 20:18:30 +00001259static QualType UnwrapTypeForDebugInfo(QualType T) {
1260 do {
1261 QualType LastT = T;
1262 switch (T->getTypeClass()) {
1263 default:
1264 return T;
1265 case Type::TemplateSpecialization:
1266 T = cast<TemplateSpecializationType>(T)->desugar();
1267 break;
1268 case Type::TypeOfExpr: {
1269 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1270 T = Ty->getUnderlyingExpr()->getType();
1271 break;
1272 }
1273 case Type::TypeOf:
1274 T = cast<TypeOfType>(T)->getUnderlyingType();
1275 break;
1276 case Type::Decltype:
1277 T = cast<DecltypeType>(T)->getUnderlyingType();
1278 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001279 case Type::Elaborated:
1280 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001281 break;
1282 case Type::SubstTemplateTypeParm:
1283 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1284 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001285 }
1286
1287 assert(T != LastT && "Type unwrapping failed to unwrap!");
1288 if (T == LastT)
1289 return T;
1290 } while (true);
1291
1292 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001293}
1294
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001295/// getOrCreateType - Get the type from the cache or create a new
1296/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001297llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001298 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001299 if (Ty.isNull())
1300 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001301
Douglas Gregor840943d2009-12-21 20:18:30 +00001302 // Unwrap the type as needed for debug information.
1303 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001304
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001305 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001306 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001307 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001308 if (it != TypeCache.end()) {
1309 // Verify that the debug info still exists.
1310 if (&*it->second)
1311 return llvm::DIType(cast<llvm::MDNode>(it->second));
1312 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001313
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001314 // Otherwise create the type.
1315 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001316
1317 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001318 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001319 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001320}
1321
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001322/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001323llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001324 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001325 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001326 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001327 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001328
Douglas Gregor2101a822009-12-21 19:57:21 +00001329 const char *Diag = 0;
1330
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001331 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001332 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001333#define TYPE(Class, Base)
1334#define ABSTRACT_TYPE(Class, Base)
1335#define NON_CANONICAL_TYPE(Class, Base)
1336#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1337#include "clang/AST/TypeNodes.def"
1338 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001339
Anders Carlssonbfe69952009-11-06 18:24:04 +00001340 // FIXME: Handle these.
1341 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001342 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001343
1344 case Type::Vector:
1345 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001346 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001347 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001348 case Type::ObjCObject:
1349 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001350 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001351 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1352 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1353 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1354 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001355 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001356 return CreateType(cast<BlockPointerType>(Ty), Unit);
1357 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001358 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001359 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001360 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001361 case Type::FunctionProto:
1362 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001363 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001364 case Type::ConstantArray:
1365 case Type::VariableArray:
1366 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001367 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001368
1369 case Type::LValueReference:
1370 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1371
Anders Carlsson20f12a22009-12-06 18:00:51 +00001372 case Type::MemberPointer:
1373 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001374
1375 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001376 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001377 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001378 case Type::TypeOfExpr:
1379 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001380 case Type::Decltype:
1381 llvm_unreachable("type should have been unwrapped!");
1382 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001383
1384 case Type::RValueReference:
1385 // FIXME: Implement!
1386 Diag = "rvalue references";
1387 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001388 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001389
1390 assert(Diag && "Fall through without a diagnostic?");
1391 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1392 "debug information for %0 is not yet supported");
1393 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1394 << Diag;
1395 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001396}
1397
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001398/// CreateMemberType - Create new member and increase Offset by FType's size.
1399llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1400 llvm::StringRef Name,
1401 uint64_t *Offset) {
1402 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1403 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1404 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1405 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1406 Unit, Name, Unit, 0,
1407 FieldSize, FieldAlign,
1408 *Offset, 0, FieldTy);
1409 *Offset += FieldSize;
1410 return Ty;
1411}
1412
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001413/// EmitFunctionStart - Constructs the debug code for entering a function -
1414/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001415void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001416 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001417 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001418
Devang Patel9c6c3a02010-01-14 00:36:21 +00001419 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001420 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001421
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001422 FnBeginRegionCount.push_back(RegionStack.size());
1423
Devang Patel9c6c3a02010-01-14 00:36:21 +00001424 const Decl *D = GD.getDecl();
1425 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001426 // If there is a DISubprogram for this function available then use it.
1427 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1428 FI = SPCache.find(FD);
1429 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001430 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001431 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1432 llvm::MDNode *SPN = SP;
1433 RegionStack.push_back(SPN);
1434 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001435 return;
1436 }
1437 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001438 Name = getFunctionName(FD);
1439 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001440 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001441 } else {
1442 // Use llvm function name as linkage name.
1443 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001444 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001445 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001446 if (!Name.empty() && Name[0] == '\01')
1447 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001448
Devang Patel970c6182010-04-24 00:49:16 +00001449 // It is expected that CurLoc is set before using EmitFunctionStart.
1450 // Usually, CurLoc points to the left bracket location of compound
1451 // statement representing function body.
1452 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001453 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001454
Chris Lattner9c85ba32008-11-10 06:08:34 +00001455 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001456 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001457 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001458 Fn->hasInternalLinkage(), true/*definition*/,
1459 0, 0, llvm::DIType(),
1460 D->isImplicit(),
1461 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001462
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001463 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001464 llvm::MDNode *SPN = SP;
1465 RegionStack.push_back(SPN);
1466 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001467
1468 // Clear stack used to keep track of #line directives.
1469 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001470}
1471
1472
Devang Patel4d939e62010-07-20 22:20:10 +00001473void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001474 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001475
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001476 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001477 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001478 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001479 || (SM.getInstantiationLineNumber(CurLoc) ==
1480 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001481 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001482 // New Builder may not be in sync with CGDebugInfo.
1483 if (!Builder.getCurrentDebugLocation().isUnknown())
1484 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001485
1486 // Update last state.
1487 PrevLoc = CurLoc;
1488
Chris Lattnerc6034632010-04-01 06:31:43 +00001489 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001490 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1491 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001492 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001493}
1494
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001495/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1496/// has introduced scope change.
1497void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1498 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1499 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1500 return;
1501 SourceManager &SM = CGM.getContext().getSourceManager();
1502 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1503 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1504
1505 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1506 return;
1507
1508 // If #line directive stack is empty then we are entering a new scope.
1509 if (LineDirectiveFiles.empty()) {
1510 EmitRegionStart(Builder);
1511 LineDirectiveFiles.push_back(PCLoc.getFilename());
1512 return;
1513 }
1514
1515 assert (RegionStack.size() >= LineDirectiveFiles.size()
1516 && "error handling #line regions!");
1517
1518 bool SeenThisFile = false;
1519 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1520 E = LineDirectiveFiles.end(); I != E; ++I)
1521 if (!strcmp(PPLoc.getFilename(), *I)) {
1522 SeenThisFile = true;
1523 break;
1524 }
1525
1526 // If #line for this file is seen earlier then pop out #line regions.
1527 if (SeenThisFile) {
1528 while (!LineDirectiveFiles.empty()) {
1529 const char *LastFile = LineDirectiveFiles.back();
1530 RegionStack.pop_back();
1531 LineDirectiveFiles.pop_back();
1532 if (!strcmp(PPLoc.getFilename(), LastFile))
1533 break;
1534 }
1535 return;
1536 }
1537
1538 // .. otherwise insert new #line region.
1539 EmitRegionStart(Builder);
1540 LineDirectiveFiles.push_back(PCLoc.getFilename());
1541
1542 return;
1543}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001544/// EmitRegionStart- Constructs the debug code for entering a declarative
1545/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001546void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001547 llvm::DIDescriptor D =
1548 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1549 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001550 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001551 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001552 getLineNumber(CurLoc),
1553 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001554 llvm::MDNode *DN = D;
1555 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001556}
1557
1558/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1559/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001560void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001561 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1562
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001563 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001564 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001565
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001566 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001567}
1568
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001569/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1570void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1571 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1572 unsigned RCount = FnBeginRegionCount.back();
1573 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1574
1575 // Pop all regions for this function.
1576 while (RegionStack.size() != RCount)
1577 EmitRegionEnd(Builder);
1578 FnBeginRegionCount.pop_back();
1579}
1580
Devang Patel809b9bb2010-02-10 18:49:08 +00001581// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1582// See BuildByRefType.
1583llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1584 uint64_t *XOffset) {
1585
1586 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1587
1588 QualType FType;
1589 uint64_t FieldSize, FieldOffset;
1590 unsigned FieldAlign;
1591
Devang Patel17800552010-03-09 00:44:50 +00001592 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001593 QualType Type = VD->getType();
1594
1595 FieldOffset = 0;
1596 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001597 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1598 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001599 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001600 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1601 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1602
Devang Patel809b9bb2010-02-10 18:49:08 +00001603 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1604 if (HasCopyAndDispose) {
1605 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001606 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1607 &FieldOffset));
1608 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1609 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001610 }
1611
1612 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1613 if (Align > CharUnits::fromQuantity(
1614 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1615 unsigned AlignedOffsetInBytes
1616 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1617 unsigned NumPaddingBytes
1618 = AlignedOffsetInBytes - FieldOffset/8;
1619
1620 if (NumPaddingBytes > 0) {
1621 llvm::APInt pad(32, NumPaddingBytes);
1622 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1623 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001624 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001625 }
1626 }
1627
1628 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001629 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001630 FieldSize = CGM.getContext().getTypeSize(FType);
1631 FieldAlign = Align.getQuantity()*8;
1632
1633 *XOffset = FieldOffset;
1634 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001635 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001636 0, FieldSize, FieldAlign,
1637 FieldOffset, 0, FieldTy);
1638 EltTys.push_back(FieldTy);
1639 FieldOffset += FieldSize;
1640
1641 llvm::DIArray Elements =
1642 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1643
1644 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1645
1646 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001647 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001648 0, FieldOffset, 0, 0, Flags,
1649 llvm::DIType(), Elements);
1650
1651}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001652/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001653void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001654 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001655 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1656
Devang Patel17800552010-03-09 00:44:50 +00001657 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001658 llvm::DIType Ty;
1659 uint64_t XOffset = 0;
1660 if (VD->hasAttr<BlocksAttr>())
1661 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1662 else
1663 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001664
Devang Patelf4e54a22010-05-07 23:05:55 +00001665 // If there is not any debug info for type then do not emit debug info
1666 // for this variable.
1667 if (!Ty)
1668 return;
1669
Chris Lattner9c85ba32008-11-10 06:08:34 +00001670 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001671 unsigned Line = getLineNumber(VD->getLocation());
1672 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001673
Chris Lattner9c85ba32008-11-10 06:08:34 +00001674 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001675 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001676 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001677 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001678 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001679 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001680 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001681 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001682
Chris Lattnerc6034632010-04-01 06:31:43 +00001683 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001684 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001685}
1686
Mike Stumpb1a6e682009-09-30 02:43:10 +00001687/// EmitDeclare - Emit local variable declaration debug info.
1688void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1689 llvm::Value *Storage, CGBuilderTy &Builder,
1690 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001691 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001692 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1693
Devang Patel2b594b92010-04-26 23:28:46 +00001694 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001695 return;
1696
1697 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001698 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001699 llvm::DIType Ty;
1700 if (VD->hasAttr<BlocksAttr>())
1701 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1702 else
1703 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001704
1705 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001706 unsigned Line = getLineNumber(VD->getLocation());
1707 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001708
Devang Pateld6c5a262010-02-01 21:52:22 +00001709 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001710 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001711 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1712 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1713 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1714 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001715 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001716 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1717 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001718 // offset of __forwarding field
1719 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001720 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1721 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1722 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001723 // offset of x field
1724 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001725 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001726 }
1727
1728 // Create the descriptor for the variable.
1729 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001730 DebugFactory.CreateComplexVariable(Tag,
1731 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001732 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001733 addr);
1734 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001735 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001736 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001737
Chris Lattnerc6034632010-04-01 06:31:43 +00001738 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001739 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001740}
1741
Devang Pateld6c5a262010-02-01 21:52:22 +00001742void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001743 llvm::Value *Storage,
1744 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001745 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001746}
1747
Mike Stumpb1a6e682009-09-30 02:43:10 +00001748void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1749 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1750 CodeGenFunction *CGF) {
1751 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1752}
1753
Chris Lattner9c85ba32008-11-10 06:08:34 +00001754/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1755/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001756void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001757 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001758 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001759}
1760
1761
1762
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001763/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001764void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001765 const VarDecl *D) {
1766
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001767 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001768 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001769 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001770
Devang Pateleb6d79b2010-02-01 21:34:11 +00001771 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001772 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001773
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001774 // CodeGen turns int[] into int[1] so we'll do the same here.
1775 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001776
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001777 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001778 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001779
Anders Carlsson20f12a22009-12-06 18:00:51 +00001780 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001781 ArrayType::Normal, 0);
1782 }
Devang Patel5d822f02010-04-29 17:48:37 +00001783 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001784 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001785 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001786 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001787 llvm::DIDescriptor DContext =
1788 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001789 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1790 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001791 Var->hasInternalLinkage(),
1792 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001793}
1794
Devang Patel9ca36b62009-02-26 21:10:26 +00001795/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001796void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001797 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001798 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001799 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001800 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001801
Devang Pateld6c5a262010-02-01 21:52:22 +00001802 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001803
Devang Pateld6c5a262010-02-01 21:52:22 +00001804 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001805 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Devang Patel9ca36b62009-02-26 21:10:26 +00001807 // CodeGen turns int[] into int[1] so we'll do the same here.
1808 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Devang Patel9ca36b62009-02-26 21:10:26 +00001810 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001811 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Anders Carlsson20f12a22009-12-06 18:00:51 +00001813 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001814 ArrayType::Normal, 0);
1815 }
1816
Devang Patelf6a39b72009-10-20 18:26:30 +00001817 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001818 getOrCreateType(T, Unit),
1819 Var->hasInternalLinkage(),
1820 true/*definition*/, Var);
1821}
Devang Patelabb485f2010-02-01 19:16:32 +00001822
Devang Patel25c2c8f2010-08-10 17:53:33 +00001823/// EmitGlobalVariable - Emit global variable's debug info.
1824void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1825 llvm::ConstantInt *Init,
Devang Patel8d308382010-08-10 07:24:25 +00001826 CGBuilderTy &Builder) {
1827 // Create the descriptor for the variable.
1828 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1829 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001830 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
1831 // Do not use DIGlobalVariable for enums.
1832 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1833 return;
Devang Patel8d308382010-08-10 07:24:25 +00001834 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1835 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001836 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001837}
1838
Devang Patelabb485f2010-02-01 19:16:32 +00001839/// getOrCreateNamesSpace - Return namespace descriptor for the given
1840/// namespace decl.
1841llvm::DINameSpace
1842CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1843 llvm::DIDescriptor Unit) {
1844 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1845 NameSpaceCache.find(NSDecl);
1846 if (I != NameSpaceCache.end())
1847 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1848
Devang Patel8ab870d2010-05-12 23:46:38 +00001849 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001850
1851 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001852 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001853 llvm::DINameSpace NS =
1854 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Eli Friedmana7e68452010-08-22 01:00:03 +00001855 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001856 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001857 return NS;
1858}