blob: c54196cc27edb966ce8b77aded1701b996104d2f [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()),
Devang Patel7573f8b2010-03-09 21:32:27 +000041 FwdDeclCount(0), 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);
556
557 llvm::StringRef FieldName = Field->getName();
558
Devang Patel4835fdd2010-02-12 01:31:06 +0000559 // Ignore unnamed fields. Do not ignore unnamed records.
560 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000561 continue;
562
563 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000564 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
565 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000566 QualType FType = Field->getType();
567 uint64_t FieldSize = 0;
568 unsigned FieldAlign = 0;
569 if (!FType->isIncompleteArrayType()) {
570
571 // Bit size, align and offset of the type.
572 FieldSize = CGM.getContext().getTypeSize(FType);
573 Expr *BitWidth = Field->getBitWidth();
574 if (BitWidth)
575 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
576
577 FieldAlign = CGM.getContext().getTypeAlign(FType);
578 }
579
580 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
581
Devang Patel71f4ff62010-04-21 23:12:37 +0000582 unsigned Flags = 0;
583 AccessSpecifier Access = I->getAccess();
584 if (Access == clang::AS_private)
585 Flags |= llvm::DIType::FlagPrivate;
586 else if (Access == clang::AS_protected)
587 Flags |= llvm::DIType::FlagProtected;
588
Devang Patel428deb52010-01-19 00:00:59 +0000589 // Create a DW_TAG_member node to remember the offset of this field in the
590 // struct. FIXME: This is an absolutely insane way to capture this
591 // information. When we gut debug info, this should be fixed.
592 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
593 FieldName, FieldDefUnit,
594 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000595 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000596 EltTys.push_back(FieldTy);
597 }
598}
599
Devang Patela6da1922010-01-28 00:28:01 +0000600/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
601/// function type is not updated to include implicit "this" pointer. Use this
602/// routine to get a method type which includes "this" pointer.
603llvm::DIType
604CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000605 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000606 llvm::DIType FnTy
607 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
608 0),
609 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000610
611 // Static methods do not need "this" pointer argument.
612 if (Method->isStatic())
613 return FnTy;
614
Devang Patela6da1922010-01-28 00:28:01 +0000615 // Add "this" pointer.
616
Devang Patelab699792010-05-07 18:12:35 +0000617 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000618 assert (Args.getNumElements() && "Invalid number of arguments!");
619
620 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
621
622 // First element is always return type. For 'void' functions it is NULL.
623 Elts.push_back(Args.getElement(0));
624
625 // "this" pointer is always first argument.
626 ASTContext &Context = CGM.getContext();
627 QualType ThisPtr =
628 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000629 llvm::DIType ThisPtrType =
630 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000631
Devang Patelab699792010-05-07 18:12:35 +0000632 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000633 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000634
635 // Copy rest of the arguments.
636 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
637 Elts.push_back(Args.getElement(i));
638
639 llvm::DIArray EltTypeArray =
640 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
641
642 return
643 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000644 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000645 0, 0, 0, 0, 0,
646 llvm::DIType(), EltTypeArray);
647}
648
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000649/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
650/// a single member function GlobalDecl.
651llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000652CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000653 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000654 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000655 bool IsCtorOrDtor =
656 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
657
658 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000659 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000660
661 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
662 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000663 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000664 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000665 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000666
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000667 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000668 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
669 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000670
671 // Collect virtual method info.
672 llvm::DIType ContainingType;
673 unsigned Virtuality = 0;
674 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000675
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000676 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000677 if (Method->isPure())
678 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
679 else
680 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
681
682 // It doesn't make sense to give a virtual destructor a vtable index,
683 // since a single destructor has two entries in the vtable.
684 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000685 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000686 ContainingType = RecordTy;
687 }
688
689 llvm::DISubprogram SP =
690 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
691 MethodLinkageName,
692 MethodDefUnit, MethodLine,
693 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000694 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000695 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000696 Method->isImplicit(),
697 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000698
699 // Don't cache ctors or dtors since we have to emit multiple functions for
700 // a single ctor or dtor.
701 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000702 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000703
704 return SP;
705}
706
Devang Patel4125fd22010-01-19 01:54:44 +0000707/// CollectCXXMemberFunctions - A helper function to collect debug info for
708/// C++ member functions.This is used while creating debug info entry for
709/// a Record.
710void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000711CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000712 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
713 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000714 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
715 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000716 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000717
Devang Pateld5322da2010-02-09 19:09:28 +0000718 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000719 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000720
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000721 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000722 }
723}
724
Devang Patela245c5b2010-01-25 23:32:18 +0000725/// CollectCXXBases - A helper function to collect debug info for
726/// C++ base classes. This is used while creating debug info entry for
727/// a Record.
728void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000729CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000730 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
731 llvm::DICompositeType &RecordTy) {
732
Devang Patel239cec62010-02-01 21:39:52 +0000733 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
734 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
735 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000736 unsigned BFlags = 0;
737 uint64_t BaseOffset;
738
739 const CXXRecordDecl *Base =
740 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
741
742 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000743 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000744 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000745 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000746 BFlags = llvm::DIType::FlagVirtual;
747 } else
748 BaseOffset = RL.getBaseClassOffset(Base);
749
750 AccessSpecifier Access = BI->getAccessSpecifier();
751 if (Access == clang::AS_private)
752 BFlags |= llvm::DIType::FlagPrivate;
753 else if (Access == clang::AS_protected)
754 BFlags |= llvm::DIType::FlagProtected;
755
756 llvm::DIType DTy =
757 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
758 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000759 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000760 BaseOffset, BFlags,
761 getOrCreateType(BI->getType(),
762 Unit));
763 EltTys.push_back(DTy);
764 }
Devang Patela245c5b2010-01-25 23:32:18 +0000765}
766
Devang Patel4ce3f202010-01-28 18:11:52 +0000767/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000768llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000769 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000770 return VTablePtrType;
771
772 ASTContext &Context = CGM.getContext();
773
774 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000775 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
776 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000777 llvm::DIType SubTy =
778 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000779 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000780 0, 0, 0, 0, 0, llvm::DIType(), SElements);
781
782 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
783 llvm::DIType vtbl_ptr_type
784 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000785 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000786 0, Size, 0, 0, 0, SubTy);
787
Devang Pateld58562e2010-03-09 22:49:11 +0000788 VTablePtrType =
789 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
790 Unit, "", Unit,
791 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000792 return VTablePtrType;
793}
794
Anders Carlsson046c2942010-04-17 20:15:18 +0000795/// getVTableName - Get vtable name for the given Class.
796llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000797 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000798 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000799
800 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000801 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000802 memcpy(StrPtr, Name.data(), Name.length());
803 return llvm::StringRef(StrPtr, Name.length());
804}
805
806
Anders Carlsson046c2942010-04-17 20:15:18 +0000807/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000808/// debug info entry in EltTys vector.
809void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000810CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000811 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000812 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000813
814 // If there is a primary base then it will hold vtable info.
815 if (RL.getPrimaryBase())
816 return;
817
818 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000819 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000820 return;
821
822 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
823 llvm::DIType VPTR
824 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000825 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000826 0, Size, 0, 0, 0,
827 getOrCreateVTablePtrType(Unit));
828 EltTys.push_back(VPTR);
829}
830
Devang Patel65e99f22009-02-25 01:36:11 +0000831/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000832llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000833 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000834 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000835
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000837 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000838 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000839 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000840 Tag = llvm::dwarf::DW_TAG_union_type;
841 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000842 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000843 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000844 }
845
Chris Lattner9c85ba32008-11-10 06:08:34 +0000846 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000847 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
848 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattner9c85ba32008-11-10 06:08:34 +0000850 // Records and classes and unions can all be recursive. To handle them, we
851 // first generate a debug descriptor for the struct as a forward declaration.
852 // Then (if it is a definition) we go through and get debug info for all of
853 // its members. Finally, we create a descriptor for the complete type (which
854 // may refer to the forward decl if the struct is recursive) and replace all
855 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000856 llvm::DIDescriptor FDContext =
857 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
858
859 // If this is just a forward declaration, construct an appropriately
860 // marked node and just return it.
861 if (!RD->getDefinition()) {
862 llvm::DICompositeType FwdDecl =
863 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
864 DefUnit, Line, 0, 0, 0,
865 llvm::DIType::FlagFwdDecl,
866 llvm::DIType(), llvm::DIArray());
867
868 return FwdDecl;
869 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000870
Devang Pateld6c5a262010-02-01 21:52:22 +0000871 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000872 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000873 llvm::SmallString<128> FwdDeclName;
874 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000875 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000876 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000877 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000878 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Devang Patelab699792010-05-07 18:12:35 +0000880 llvm::MDNode *MN = FwdDecl;
881 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000882 // Otherwise, insert it into the TypeCache so that recursive uses will find
883 // it.
Devang Patelab699792010-05-07 18:12:35 +0000884 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000885 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000886 RegionStack.push_back(FwdDeclNode);
887 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000888
889 // Convert all the elements.
890 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
891
Devang Pateld6c5a262010-02-01 21:52:22 +0000892 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000893 if (CXXDecl) {
894 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000895 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000896 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000897 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000898 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000899 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000900 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000901
902 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000903 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000904 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
905 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000906 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000907 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000908 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000909 }
Mike Stump1eb44332009-09-09 15:08:12 +0000910
Chris Lattner9c85ba32008-11-10 06:08:34 +0000911 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000912 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000913
914 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000915 uint64_t Size = CGM.getContext().getTypeSize(Ty);
916 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000917
Devang Patele4c1ea02010-03-11 20:01:48 +0000918 RegionStack.pop_back();
919 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
920 RegionMap.find(Ty->getDecl());
921 if (RI != RegionMap.end())
922 RegionMap.erase(RI);
923
Devang Patel411894b2010-02-01 22:40:08 +0000924 llvm::DIDescriptor RDContext =
925 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000926
927 llvm::StringRef RDName = RD->getName();
928 // If this is a class, include the template arguments also.
929 if (Tag == llvm::dwarf::DW_TAG_class_type)
930 RDName = getClassName(RD);
931
Devang Patel0ce73f62009-07-22 18:57:00 +0000932 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000933 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000934 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000935 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000936 llvm::DIType(), Elements,
937 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000938
939 // Now that we have a real decl for the struct, replace anything using the
940 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000941 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000942 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000943 return RealDecl;
944}
945
John McCallc12c5bb2010-05-15 11:32:37 +0000946/// CreateType - get objective-c object type.
947llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
948 llvm::DIFile Unit) {
949 // Ignore protocols.
950 return getOrCreateType(Ty->getBaseType(), Unit);
951}
952
Devang Patel9ca36b62009-02-26 21:10:26 +0000953/// CreateType - get objective-c interface type.
954llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000955 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000956 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000957 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000958
959 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000960 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000961 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000962 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000963
Devang Patel9ca36b62009-02-26 21:10:26 +0000964 // To handle recursive interface, we
965 // first generate a debug descriptor for the struct as a forward declaration.
966 // Then (if it is a definition) we go through and get debug info for all of
967 // its members. Finally, we create a descriptor for the complete type (which
968 // may refer to the forward decl if the struct is recursive) and replace all
969 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000970 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000971 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000972 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000973 llvm::DIType(), llvm::DIArray(),
974 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000975
Devang Patel9ca36b62009-02-26 21:10:26 +0000976 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000977 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000978 return FwdDecl;
979
Devang Patelab699792010-05-07 18:12:35 +0000980 llvm::MDNode *MN = FwdDecl;
981 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000982 // Otherwise, insert it into the TypeCache so that recursive uses will find
983 // it.
Devang Patelab699792010-05-07 18:12:35 +0000984 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000985 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000986 RegionStack.push_back(FwdDeclNode);
987 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000988
989 // Convert all the elements.
990 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
991
Devang Pateld6c5a262010-02-01 21:52:22 +0000992 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000993 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000994 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000995 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000996 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000997 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000998 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000999 0 /* offset */, 0, SClassTy);
1000 EltTys.push_back(InhTag);
1001 }
1002
Devang Pateld6c5a262010-02-01 21:52:22 +00001003 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001004
1005 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001006 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1007 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001008 ObjCIvarDecl *Field = *I;
1009 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1010
Devang Patel73621622009-11-25 17:37:31 +00001011 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001012
Devang Patelde135022009-04-27 22:40:36 +00001013 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001014 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001015 continue;
1016
Devang Patel9ca36b62009-02-26 21:10:26 +00001017 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001018 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1019 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001020 QualType FType = Field->getType();
1021 uint64_t FieldSize = 0;
1022 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001023
Devang Patel99c20eb2009-03-20 18:24:39 +00001024 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Devang Patel99c20eb2009-03-20 18:24:39 +00001026 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001027 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001028 Expr *BitWidth = Field->getBitWidth();
1029 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001030 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001031
Anders Carlsson20f12a22009-12-06 18:00:51 +00001032 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001033 }
1034
Mike Stump1eb44332009-09-09 15:08:12 +00001035 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1036
Devang Patelc20482b2009-03-19 00:23:53 +00001037 unsigned Flags = 0;
1038 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1039 Flags = llvm::DIType::FlagProtected;
1040 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1041 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Devang Patel9ca36b62009-02-26 21:10:26 +00001043 // Create a DW_TAG_member node to remember the offset of this field in the
1044 // struct. FIXME: This is an absolutely insane way to capture this
1045 // information. When we gut debug info, this should be fixed.
1046 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1047 FieldName, FieldDefUnit,
1048 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001049 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001050 EltTys.push_back(FieldTy);
1051 }
Mike Stump1eb44332009-09-09 15:08:12 +00001052
Devang Patel9ca36b62009-02-26 21:10:26 +00001053 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001054 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001055
Devang Patele4c1ea02010-03-11 20:01:48 +00001056 RegionStack.pop_back();
1057 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1058 RegionMap.find(Ty->getDecl());
1059 if (RI != RegionMap.end())
1060 RegionMap.erase(RI);
1061
Devang Patel9ca36b62009-02-26 21:10:26 +00001062 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001063 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1064 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001065
Devang Patel6c1fddf2009-07-27 18:42:03 +00001066 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001067 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001068 Line, Size, Align, 0, 0, llvm::DIType(),
1069 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001070
1071 // Now that we have a real decl for the struct, replace anything using the
1072 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001073 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001074 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001075
Devang Patel9ca36b62009-02-26 21:10:26 +00001076 return RealDecl;
1077}
1078
Chris Lattner9c85ba32008-11-10 06:08:34 +00001079llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001080 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001081 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001082
1083 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1084
1085 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001086 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001087 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001088 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001089 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001090 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001091 }
Mike Stump1eb44332009-09-09 15:08:12 +00001092
Chris Lattner9c85ba32008-11-10 06:08:34 +00001093 // Return a CompositeType for the enum itself.
1094 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001095 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001096
Devang Patel8ab870d2010-05-12 23:46:38 +00001097 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1098 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Chris Lattner9c85ba32008-11-10 06:08:34 +00001100 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001101 uint64_t Size = 0;
1102 unsigned Align = 0;
1103 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001104 Size = CGM.getContext().getTypeSize(Ty);
1105 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001106 }
Mike Stump1eb44332009-09-09 15:08:12 +00001107
Devang Patelca80a5f2009-10-20 19:55:01 +00001108 llvm::DIType DbgTy =
1109 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001110 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001111 Size, Align, 0, 0,
1112 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001113 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001114}
1115
1116llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001117 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001118 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1119 return CreateType(RT, Unit);
1120 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1121 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001122
Chris Lattner9c85ba32008-11-10 06:08:34 +00001123 return llvm::DIType();
1124}
1125
Devang Patel70c23cd2010-02-23 22:59:39 +00001126llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001127 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001128 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1129 uint64_t NumElems = Ty->getNumElements();
1130 if (NumElems > 0)
1131 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001132
Benjamin Kramerad468862010-03-30 11:36:44 +00001133 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1134 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001135
1136 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1137 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1138
1139 return
1140 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001141 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001142 0, Size, Align, 0, 0,
1143 ElementTy, SubscriptArray);
1144}
1145
Chris Lattner9c85ba32008-11-10 06:08:34 +00001146llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001147 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001148 uint64_t Size;
1149 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001150
1151
Nuno Lopes010d5142009-01-28 00:35:17 +00001152 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001153 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001154 Size = 0;
1155 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001156 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001157 } else if (Ty->isIncompleteArrayType()) {
1158 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001159 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001160 } else {
1161 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001162 Size = CGM.getContext().getTypeSize(Ty);
1163 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001164 }
Mike Stump1eb44332009-09-09 15:08:12 +00001165
Chris Lattner9c85ba32008-11-10 06:08:34 +00001166 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1167 // interior arrays, do we care? Why aren't nested arrays represented the
1168 // obvious/recursive way?
1169 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1170 QualType EltTy(Ty, 0);
1171 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001172 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001173 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001174 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001175 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001176 // FIXME: Verify this is right for VLAs.
1177 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1178 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001179 }
Mike Stump1eb44332009-09-09 15:08:12 +00001180
Chris Lattner9c85ba32008-11-10 06:08:34 +00001181 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001182 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001183
Devang Patelca80a5f2009-10-20 19:55:01 +00001184 llvm::DIType DbgTy =
1185 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001186 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001187 0, Size, Align, 0, 0,
1188 getOrCreateType(EltTy, Unit),
1189 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001190 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001191}
1192
Anders Carlssona031b352009-11-06 19:19:55 +00001193llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001194 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001195 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1196 Ty, Ty->getPointeeType(), Unit);
1197}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001198
Anders Carlsson20f12a22009-12-06 18:00:51 +00001199llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001200 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001201 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1202 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1203
1204 if (!Ty->getPointeeType()->isFunctionType()) {
1205 // We have a data member pointer type.
1206 return PointerDiffDITy;
1207 }
1208
1209 // We have a member function pointer type. Treat it as a struct with two
1210 // ptrdiff_t members.
1211 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1212
1213 uint64_t FieldOffset = 0;
1214 llvm::DIDescriptor ElementTypes[2];
1215
1216 // FIXME: This should probably be a function type instead.
1217 ElementTypes[0] =
1218 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001219 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001220 Info.first, Info.second, FieldOffset, 0,
1221 PointerDiffDITy);
1222 FieldOffset += Info.first;
1223
1224 ElementTypes[1] =
1225 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001226 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001227 Info.first, Info.second, FieldOffset, 0,
1228 PointerDiffDITy);
1229
1230 llvm::DIArray Elements =
1231 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1232 llvm::array_lengthof(ElementTypes));
1233
1234 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1235 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001236 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001237 0, 0, 0, llvm::DIType(), Elements);
1238}
1239
Douglas Gregor840943d2009-12-21 20:18:30 +00001240static QualType UnwrapTypeForDebugInfo(QualType T) {
1241 do {
1242 QualType LastT = T;
1243 switch (T->getTypeClass()) {
1244 default:
1245 return T;
1246 case Type::TemplateSpecialization:
1247 T = cast<TemplateSpecializationType>(T)->desugar();
1248 break;
1249 case Type::TypeOfExpr: {
1250 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1251 T = Ty->getUnderlyingExpr()->getType();
1252 break;
1253 }
1254 case Type::TypeOf:
1255 T = cast<TypeOfType>(T)->getUnderlyingType();
1256 break;
1257 case Type::Decltype:
1258 T = cast<DecltypeType>(T)->getUnderlyingType();
1259 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001260 case Type::Elaborated:
1261 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001262 break;
1263 case Type::SubstTemplateTypeParm:
1264 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1265 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001266 }
1267
1268 assert(T != LastT && "Type unwrapping failed to unwrap!");
1269 if (T == LastT)
1270 return T;
1271 } while (true);
1272
1273 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001274}
1275
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001276/// getOrCreateType - Get the type from the cache or create a new
1277/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001278llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001279 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001280 if (Ty.isNull())
1281 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001282
Douglas Gregor840943d2009-12-21 20:18:30 +00001283 // Unwrap the type as needed for debug information.
1284 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001285
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001286 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001287 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001288 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001289 if (it != TypeCache.end()) {
1290 // Verify that the debug info still exists.
1291 if (&*it->second)
1292 return llvm::DIType(cast<llvm::MDNode>(it->second));
1293 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001294
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001295 // Otherwise create the type.
1296 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001297
1298 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001299 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001300 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001301}
1302
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001303/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001304llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001305 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001306 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001307 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001308 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001309
Douglas Gregor2101a822009-12-21 19:57:21 +00001310 const char *Diag = 0;
1311
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001312 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001313 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001314#define TYPE(Class, Base)
1315#define ABSTRACT_TYPE(Class, Base)
1316#define NON_CANONICAL_TYPE(Class, Base)
1317#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1318#include "clang/AST/TypeNodes.def"
1319 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001320
Anders Carlssonbfe69952009-11-06 18:24:04 +00001321 // FIXME: Handle these.
1322 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001323 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001324
1325 case Type::Vector:
1326 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001327 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001328 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001329 case Type::ObjCObject:
1330 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001331 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001332 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1333 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1334 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1335 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001336 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001337 return CreateType(cast<BlockPointerType>(Ty), Unit);
1338 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001339 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001340 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001341 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001342 case Type::FunctionProto:
1343 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001344 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001345 case Type::ConstantArray:
1346 case Type::VariableArray:
1347 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001348 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001349
1350 case Type::LValueReference:
1351 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1352
Anders Carlsson20f12a22009-12-06 18:00:51 +00001353 case Type::MemberPointer:
1354 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001355
1356 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001357 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001358 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001359 case Type::TypeOfExpr:
1360 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001361 case Type::Decltype:
1362 llvm_unreachable("type should have been unwrapped!");
1363 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001364
1365 case Type::RValueReference:
1366 // FIXME: Implement!
1367 Diag = "rvalue references";
1368 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001369 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001370
1371 assert(Diag && "Fall through without a diagnostic?");
1372 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1373 "debug information for %0 is not yet supported");
1374 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1375 << Diag;
1376 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001377}
1378
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001379/// CreateMemberType - Create new member and increase Offset by FType's size.
1380llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1381 llvm::StringRef Name,
1382 uint64_t *Offset) {
1383 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1384 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1385 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1386 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1387 Unit, Name, Unit, 0,
1388 FieldSize, FieldAlign,
1389 *Offset, 0, FieldTy);
1390 *Offset += FieldSize;
1391 return Ty;
1392}
1393
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001394/// EmitFunctionStart - Constructs the debug code for entering a function -
1395/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001396void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001397 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001398 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001399
Devang Patel9c6c3a02010-01-14 00:36:21 +00001400 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001401 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001402
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001403 FnBeginRegionCount.push_back(RegionStack.size());
1404
Devang Patel9c6c3a02010-01-14 00:36:21 +00001405 const Decl *D = GD.getDecl();
1406 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001407 // If there is a DISubprogram for this function available then use it.
1408 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1409 FI = SPCache.find(FD);
1410 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001411 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001412 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1413 llvm::MDNode *SPN = SP;
1414 RegionStack.push_back(SPN);
1415 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001416 return;
1417 }
1418 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001419 Name = getFunctionName(FD);
1420 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001421 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001422 } else {
1423 // Use llvm function name as linkage name.
1424 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001425 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001426 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001427 if (!Name.empty() && Name[0] == '\01')
1428 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001429
Devang Patel970c6182010-04-24 00:49:16 +00001430 // It is expected that CurLoc is set before using EmitFunctionStart.
1431 // Usually, CurLoc points to the left bracket location of compound
1432 // statement representing function body.
1433 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001434 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001435
Chris Lattner9c85ba32008-11-10 06:08:34 +00001436 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001437 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001438 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001439 Fn->hasInternalLinkage(), true/*definition*/,
1440 0, 0, llvm::DIType(),
1441 D->isImplicit(),
1442 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001443
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001444 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001445 llvm::MDNode *SPN = SP;
1446 RegionStack.push_back(SPN);
1447 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001448
1449 // Clear stack used to keep track of #line directives.
1450 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001451}
1452
1453
Devang Patel4d939e62010-07-20 22:20:10 +00001454void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001455 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001456
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001457 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001458 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001459 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001460 || (SM.getInstantiationLineNumber(CurLoc) ==
1461 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001462 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001463 // New Builder may not be in sync with CGDebugInfo.
1464 if (!Builder.getCurrentDebugLocation().isUnknown())
1465 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001466
1467 // Update last state.
1468 PrevLoc = CurLoc;
1469
Chris Lattnerc6034632010-04-01 06:31:43 +00001470 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001471 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1472 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001473 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001474}
1475
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001476/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1477/// has introduced scope change.
1478void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1479 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1480 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1481 return;
1482 SourceManager &SM = CGM.getContext().getSourceManager();
1483 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1484 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1485
1486 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1487 return;
1488
1489 // If #line directive stack is empty then we are entering a new scope.
1490 if (LineDirectiveFiles.empty()) {
1491 EmitRegionStart(Builder);
1492 LineDirectiveFiles.push_back(PCLoc.getFilename());
1493 return;
1494 }
1495
1496 assert (RegionStack.size() >= LineDirectiveFiles.size()
1497 && "error handling #line regions!");
1498
1499 bool SeenThisFile = false;
1500 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1501 E = LineDirectiveFiles.end(); I != E; ++I)
1502 if (!strcmp(PPLoc.getFilename(), *I)) {
1503 SeenThisFile = true;
1504 break;
1505 }
1506
1507 // If #line for this file is seen earlier then pop out #line regions.
1508 if (SeenThisFile) {
1509 while (!LineDirectiveFiles.empty()) {
1510 const char *LastFile = LineDirectiveFiles.back();
1511 RegionStack.pop_back();
1512 LineDirectiveFiles.pop_back();
1513 if (!strcmp(PPLoc.getFilename(), LastFile))
1514 break;
1515 }
1516 return;
1517 }
1518
1519 // .. otherwise insert new #line region.
1520 EmitRegionStart(Builder);
1521 LineDirectiveFiles.push_back(PCLoc.getFilename());
1522
1523 return;
1524}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001525/// EmitRegionStart- Constructs the debug code for entering a declarative
1526/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001527void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001528 llvm::DIDescriptor D =
1529 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1530 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001531 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001532 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001533 getLineNumber(CurLoc),
1534 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001535 llvm::MDNode *DN = D;
1536 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001537}
1538
1539/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1540/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001541void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001542 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1543
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001544 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001545 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001546
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001547 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001548}
1549
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001550/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1551void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1552 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1553 unsigned RCount = FnBeginRegionCount.back();
1554 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1555
1556 // Pop all regions for this function.
1557 while (RegionStack.size() != RCount)
1558 EmitRegionEnd(Builder);
1559 FnBeginRegionCount.pop_back();
1560}
1561
Devang Patel809b9bb2010-02-10 18:49:08 +00001562// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1563// See BuildByRefType.
1564llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1565 uint64_t *XOffset) {
1566
1567 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1568
1569 QualType FType;
1570 uint64_t FieldSize, FieldOffset;
1571 unsigned FieldAlign;
1572
Devang Patel17800552010-03-09 00:44:50 +00001573 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001574 QualType Type = VD->getType();
1575
1576 FieldOffset = 0;
1577 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001578 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1579 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001580 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001581 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1582 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1583
Devang Patel809b9bb2010-02-10 18:49:08 +00001584 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1585 if (HasCopyAndDispose) {
1586 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001587 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1588 &FieldOffset));
1589 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1590 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001591 }
1592
1593 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1594 if (Align > CharUnits::fromQuantity(
1595 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1596 unsigned AlignedOffsetInBytes
1597 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1598 unsigned NumPaddingBytes
1599 = AlignedOffsetInBytes - FieldOffset/8;
1600
1601 if (NumPaddingBytes > 0) {
1602 llvm::APInt pad(32, NumPaddingBytes);
1603 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1604 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001605 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001606 }
1607 }
1608
1609 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001610 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001611 FieldSize = CGM.getContext().getTypeSize(FType);
1612 FieldAlign = Align.getQuantity()*8;
1613
1614 *XOffset = FieldOffset;
1615 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001616 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001617 0, FieldSize, FieldAlign,
1618 FieldOffset, 0, FieldTy);
1619 EltTys.push_back(FieldTy);
1620 FieldOffset += FieldSize;
1621
1622 llvm::DIArray Elements =
1623 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1624
1625 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1626
1627 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001628 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001629 0, FieldOffset, 0, 0, Flags,
1630 llvm::DIType(), Elements);
1631
1632}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001633/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001634void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001635 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001636 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1637
Devang Patel17800552010-03-09 00:44:50 +00001638 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001639 llvm::DIType Ty;
1640 uint64_t XOffset = 0;
1641 if (VD->hasAttr<BlocksAttr>())
1642 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1643 else
1644 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001645
Devang Patelf4e54a22010-05-07 23:05:55 +00001646 // If there is not any debug info for type then do not emit debug info
1647 // for this variable.
1648 if (!Ty)
1649 return;
1650
Chris Lattner9c85ba32008-11-10 06:08:34 +00001651 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001652 unsigned Line = getLineNumber(VD->getLocation());
1653 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001654
Chris Lattner9c85ba32008-11-10 06:08:34 +00001655 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001656 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001657 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001658 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001659 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001660 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001661 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001662 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001663
Chris Lattnerc6034632010-04-01 06:31:43 +00001664 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001665 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001666}
1667
Mike Stumpb1a6e682009-09-30 02:43:10 +00001668/// EmitDeclare - Emit local variable declaration debug info.
1669void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1670 llvm::Value *Storage, CGBuilderTy &Builder,
1671 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001672 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001673 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1674
Devang Patel2b594b92010-04-26 23:28:46 +00001675 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001676 return;
1677
1678 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001679 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001680 llvm::DIType Ty;
1681 if (VD->hasAttr<BlocksAttr>())
1682 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1683 else
1684 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001685
1686 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001687 unsigned Line = getLineNumber(VD->getLocation());
1688 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001689
Devang Pateld6c5a262010-02-01 21:52:22 +00001690 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001691 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001692 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1693 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1694 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1695 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001696 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001697 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1698 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001699 // offset of __forwarding field
1700 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001701 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1702 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1703 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001704 // offset of x field
1705 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001706 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001707 }
1708
1709 // Create the descriptor for the variable.
1710 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001711 DebugFactory.CreateComplexVariable(Tag,
1712 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001713 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001714 addr);
1715 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001716 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001717 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001718
Chris Lattnerc6034632010-04-01 06:31:43 +00001719 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001720 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001721}
1722
Devang Pateld6c5a262010-02-01 21:52:22 +00001723void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001724 llvm::Value *Storage,
1725 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001726 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001727}
1728
Mike Stumpb1a6e682009-09-30 02:43:10 +00001729void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1730 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1731 CodeGenFunction *CGF) {
1732 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1733}
1734
Chris Lattner9c85ba32008-11-10 06:08:34 +00001735/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1736/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001737void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001738 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001739 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001740}
1741
1742
1743
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001744/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001745void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001746 const VarDecl *D) {
1747
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001748 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001749 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001750 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001751
Devang Pateleb6d79b2010-02-01 21:34:11 +00001752 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001753 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001754
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001755 // CodeGen turns int[] into int[1] so we'll do the same here.
1756 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001757
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001758 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001759 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001760
Anders Carlsson20f12a22009-12-06 18:00:51 +00001761 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001762 ArrayType::Normal, 0);
1763 }
Devang Patel5d822f02010-04-29 17:48:37 +00001764 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001765 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001766 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001767 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001768 llvm::DIDescriptor DContext =
1769 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001770 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1771 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001772 Var->hasInternalLinkage(),
1773 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001774}
1775
Devang Patel9ca36b62009-02-26 21:10:26 +00001776/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001777void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001778 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001779 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001780 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001781 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001782
Devang Pateld6c5a262010-02-01 21:52:22 +00001783 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001784
Devang Pateld6c5a262010-02-01 21:52:22 +00001785 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001786 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001787
Devang Patel9ca36b62009-02-26 21:10:26 +00001788 // CodeGen turns int[] into int[1] so we'll do the same here.
1789 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001790
Devang Patel9ca36b62009-02-26 21:10:26 +00001791 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001792 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001793
Anders Carlsson20f12a22009-12-06 18:00:51 +00001794 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001795 ArrayType::Normal, 0);
1796 }
1797
Devang Patelf6a39b72009-10-20 18:26:30 +00001798 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001799 getOrCreateType(T, Unit),
1800 Var->hasInternalLinkage(),
1801 true/*definition*/, Var);
1802}
Devang Patelabb485f2010-02-01 19:16:32 +00001803
1804/// getOrCreateNamesSpace - Return namespace descriptor for the given
1805/// namespace decl.
1806llvm::DINameSpace
1807CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1808 llvm::DIDescriptor Unit) {
1809 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1810 NameSpaceCache.find(NSDecl);
1811 if (I != NameSpaceCache.end())
1812 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1813
Devang Patel8ab870d2010-05-12 23:46:38 +00001814 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001815
1816 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001817 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001818 llvm::DINameSpace NS =
1819 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001820 llvm::DIFile(Unit), LineNo);
1821 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001822 return NS;
1823}