blob: 939b6f768dbb7153e6d44dcc0b444de6ddd48ecf [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 Patel2ed8f002010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson20f12a22009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048}
49
Chris Lattner9c85ba32008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053}
54
Devang Patel33583052010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
59 return CompileUnit;
60
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
64 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
68 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
72 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
73 llvm::DIFile(CompileUnit));
74 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel979ec2e2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
Devang Patel700a1cb2010-07-20 20:24:18 +000098/// getClassName - Get class name including template argument list.
99llvm::StringRef
100CGDebugInfo::getClassName(RecordDecl *RD) {
101 ClassTemplateSpecializationDecl *Spec
102 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
103 if (!Spec)
104 return RD->getName();
105
106 const TemplateArgument *Args;
107 unsigned NumArgs;
108 std::string Buffer;
109 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
110 const TemplateSpecializationType *TST =
111 cast<TemplateSpecializationType>(TAW->getType());
112 Args = TST->getArgs();
113 NumArgs = TST->getNumArgs();
114 } else {
115 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
116 Args = TemplateArgs.getFlatArgumentList();
117 NumArgs = TemplateArgs.flat_size();
118 }
119 Buffer = RD->getIdentifier()->getNameStart();
120 PrintingPolicy Policy(CGM.getLangOptions());
121 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
122 NumArgs,
123 Policy);
124
125 // Copy this name on the side and use its reference.
126 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
127 memcpy(StrPtr, Buffer.data(), Buffer.length());
128 return llvm::StringRef(StrPtr, Buffer.length());
129
130}
131
Devang Patel17800552010-03-09 00:44:50 +0000132/// getOrCreateFile - Get the file debug info descriptor for the input location.
133llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000134 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000135 // If Location is not valid then use main input file.
136 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
137 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000138 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000139 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000140
141 // Cache the results.
142 const char *fname = PLoc.getFilename();
143 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
144 DIFileCache.find(fname);
145
146 if (it != DIFileCache.end()) {
147 // Verify that the information still exists.
148 if (&*it->second)
149 return llvm::DIFile(cast<llvm::MDNode>(it->second));
150 }
151
Devang Patelac4d13c2010-07-27 15:17:16 +0000152 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
153 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000154
Devang Patelab699792010-05-07 18:12:35 +0000155 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000156 return F;
157
Devang Patel17800552010-03-09 00:44:50 +0000158}
Devang Patel8ab870d2010-05-12 23:46:38 +0000159
160/// getLineNumber - Get line number for the location. If location is invalid
161/// then use current location.
162unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
163 assert (CurLoc.isValid() && "Invalid current location!");
164 SourceManager &SM = CGM.getContext().getSourceManager();
165 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
166 return PLoc.getLine();
167}
168
169/// getColumnNumber - Get column number for the location. If location is
170/// invalid then use current location.
171unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
172 assert (CurLoc.isValid() && "Invalid current location!");
173 SourceManager &SM = CGM.getContext().getSourceManager();
174 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
175 return PLoc.getColumn();
176}
177
Devang Patelac4d13c2010-07-27 15:17:16 +0000178llvm::StringRef CGDebugInfo::getCurrentDirname() {
179 if (!CWDName.empty())
180 return CWDName;
181 char *CompDirnamePtr = NULL;
182 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
183 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
184 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
185 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
186}
187
Devang Patel17800552010-03-09 00:44:50 +0000188/// CreateCompileUnit - Create new compile unit.
189void CGDebugInfo::CreateCompileUnit() {
190
191 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000192 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000193 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
194 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000195 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000196
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000197 // The main file name provided via the "-main-file-name" option contains just
198 // the file name itself with no path information. This file name may have had
199 // a relative path, so we look into the actual file entry for the main
200 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000201 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000202 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000203 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000204 if (MainFileDir != ".")
205 MainFileName = MainFileDir + "/" + MainFileName;
206 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000207
Devang Patelac4d13c2010-07-27 15:17:16 +0000208 // Save filename string.
209 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
210 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
211 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
212
Chris Lattner515455a2009-03-25 03:28:08 +0000213 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000214 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000215 if (LO.CPlusPlus) {
216 if (LO.ObjC1)
217 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
218 else
219 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
220 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000221 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000222 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000223 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000224 } else {
225 LangTag = llvm::dwarf::DW_LANG_C89;
226 }
Devang Patel446c6192009-04-17 21:06:59 +0000227
Daniel Dunbar19f19832010-08-24 17:41:09 +0000228 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000229
230 // Figure out which version of the ObjC runtime we have.
231 unsigned RuntimeVers = 0;
232 if (LO.ObjC1)
233 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000234
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000235 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000236 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000237 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000238 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000239 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000240}
241
Devang Patel65e99f22009-02-25 01:36:11 +0000242/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000243/// one if necessary.
244llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000245 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000247 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000248 switch (BT->getKind()) {
249 default:
250 case BuiltinType::Void:
251 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000252 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000253 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000254 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000255 llvm::DIType::FlagFwdDecl,
256 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000257 case BuiltinType::ObjCId: {
258 // typedef struct objc_class *Class;
259 // typedef struct objc_object {
260 // Class isa;
261 // } *id;
262
263 llvm::DIType OCTy =
264 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
265 Unit, "objc_class", Unit, 0, 0, 0, 0,
266 llvm::DIType::FlagFwdDecl,
267 llvm::DIType(), llvm::DIArray());
268 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
269
270 llvm::DIType ISATy =
271 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
272 Unit, "", Unit,
273 0, Size, 0, 0, 0, OCTy);
274
275 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
276
277 llvm::DIType FieldTy =
278 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
279 "isa", Unit,
280 0,Size, 0, 0, 0, ISATy);
281 EltTys.push_back(FieldTy);
282 llvm::DIArray Elements =
283 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
284
285 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
286 Unit, "objc_object", Unit, 0, 0, 0, 0,
287 0,
288 llvm::DIType(), Elements);
289 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000290 case BuiltinType::UChar:
291 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
292 case BuiltinType::Char_S:
293 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
294 case BuiltinType::UShort:
295 case BuiltinType::UInt:
296 case BuiltinType::ULong:
297 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
298 case BuiltinType::Short:
299 case BuiltinType::Int:
300 case BuiltinType::Long:
301 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
302 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
303 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000304 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000305 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000306 }
Devang Patel05127ca2010-07-28 23:23:29 +0000307
308 switch (BT->getKind()) {
309 case BuiltinType::Long: BTName = "long int"; break;
310 case BuiltinType::LongLong: BTName = "long long int"; break;
311 case BuiltinType::ULong: BTName = "long unsigned int"; break;
312 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
313 default:
314 BTName = BT->getName(CGM.getContext().getLangOptions());
315 break;
316 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000317 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000318 uint64_t Size = CGM.getContext().getTypeSize(BT);
319 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000320 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000321
Devang Patelca80a5f2009-10-20 19:55:01 +0000322 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000323 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000324 Unit, 0, Size, Align,
325 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000326 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000327}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000328
Chris Lattnerb7003772009-04-23 06:13:01 +0000329llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000330 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000331 // Bit size, align and offset of the type.
332 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
333 if (Ty->isComplexIntegerType())
334 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000335
Anders Carlsson20f12a22009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(Ty);
337 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000338 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000339
Devang Patelca80a5f2009-10-20 19:55:01 +0000340 llvm::DIType DbgTy =
341 DebugFactory.CreateBasicType(Unit, "complex",
342 Unit, 0, Size, Align,
343 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000344 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000345}
346
John McCalla1805292009-09-25 01:40:47 +0000347/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000348/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000349llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000350 QualifierCollector Qc;
351 const Type *T = Qc.strip(Ty);
352
353 // Ignore these qualifiers for now.
354 Qc.removeObjCGCAttr();
355 Qc.removeAddressSpace();
356
Chris Lattner9c85ba32008-11-10 06:08:34 +0000357 // We will create one Derived type for one qualifier and recurse to handle any
358 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000359 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000360 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000361 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000362 Qc.removeConst();
363 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000364 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000365 Qc.removeVolatile();
366 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000367 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000368 Qc.removeRestrict();
369 } else {
370 assert(Qc.empty() && "Unknown type qualifier for debug info");
371 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000372 }
Mike Stump1eb44332009-09-09 15:08:12 +0000373
John McCalla1805292009-09-25 01:40:47 +0000374 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
375
Daniel Dunbar3845f862008-10-31 03:54:29 +0000376 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
377 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000378 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000379 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000380 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000381 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000382}
383
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000384llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000385 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000386 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000387 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
388 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000389 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000390}
391
Chris Lattner9c85ba32008-11-10 06:08:34 +0000392llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000393 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000394 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
395 Ty->getPointeeType(), Unit);
396}
397
398llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
399 const Type *Ty,
400 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000401 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000402 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000403
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000404 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000405
406 // Size is always the size of a pointer. We can't use getTypeSize here
407 // because that does not return the correct value for references.
408 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000409 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
410 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000411
Devang Patelca80a5f2009-10-20 19:55:01 +0000412 return
Devang Pateld58562e2010-03-09 22:49:11 +0000413 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000414 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000415
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000416}
417
Mike Stump9bc093c2009-05-14 02:03:51 +0000418llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000419 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000420 if (BlockLiteralGenericSet)
421 return BlockLiteralGeneric;
422
Mike Stump9bc093c2009-05-14 02:03:51 +0000423 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
424
425 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
426
427 llvm::DIType FieldTy;
428
429 QualType FType;
430 uint64_t FieldSize, FieldOffset;
431 unsigned FieldAlign;
432
433 llvm::DIArray Elements;
434 llvm::DIType EltTy, DescTy;
435
436 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000437 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000438 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
439 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000440
Daniel Dunbarca308df2009-05-26 19:40:20 +0000441 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000442 EltTys.clear();
443
Mike Stump3d363c52009-10-02 02:30:50 +0000444 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000445 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000446
Mike Stump9bc093c2009-05-14 02:03:51 +0000447 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000448 Unit, LineNo, FieldOffset, 0, 0,
449 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000450
Mike Stump9bc093c2009-05-14 02:03:51 +0000451 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000452 uint64_t Size = CGM.getContext().getTypeSize(Ty);
453 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Mike Stump9bc093c2009-05-14 02:03:51 +0000455 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000456 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000457 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000458
459 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000460 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000461 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000462 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000463 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
464 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000465 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000466 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000467
Anders Carlsson20f12a22009-12-06 18:00:51 +0000468 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000469 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000470 FieldSize = CGM.getContext().getTypeSize(Ty);
471 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000473 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000474 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000475 FieldOffset, 0, FieldTy);
476 EltTys.push_back(FieldTy);
477
478 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000479 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000480
481 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000482 Unit, LineNo, FieldOffset, 0, 0,
483 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000484
Mike Stump9bc093c2009-05-14 02:03:51 +0000485 BlockLiteralGenericSet = true;
486 BlockLiteralGeneric
487 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000488 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000489 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000490 return BlockLiteralGeneric;
491}
492
Chris Lattner9c85ba32008-11-10 06:08:34 +0000493llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000494 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000495 // Typedefs are derived from some other type. If we have a typedef of a
496 // typedef, make sure to emit the whole chain.
497 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000498
Chris Lattner9c85ba32008-11-10 06:08:34 +0000499 // We don't set size information, but do specify where the typedef was
500 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000501 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000502
Devang Pateleb6d79b2010-02-01 21:34:11 +0000503 llvm::DIDescriptor TyContext
504 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
505 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000506 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000507 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000508 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000509 Ty->getDecl()->getName(), Unit,
510 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000511 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000512}
513
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000515 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000516 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000517
Chris Lattner9c85ba32008-11-10 06:08:34 +0000518 // Add the result type at least.
519 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner9c85ba32008-11-10 06:08:34 +0000521 // Set up remainder of arguments if there is a prototype.
522 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000523 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000524 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
525 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
526 } else {
527 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000528 }
529
Chris Lattner9c85ba32008-11-10 06:08:34 +0000530 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000531 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Devang Patelca80a5f2009-10-20 19:55:01 +0000533 llvm::DIType DbgTy =
534 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000535 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000536 0, 0, 0, 0, 0,
537 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000538 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000539}
540
Devang Patel428deb52010-01-19 00:00:59 +0000541/// CollectRecordFields - A helper function to collect debug info for
542/// record fields. This is used while creating debug info entry for a Record.
543void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000544CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000545 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
546 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000547 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
548 for (RecordDecl::field_iterator I = RD->field_begin(),
549 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000550 I != E; ++I, ++FieldNo) {
551 FieldDecl *Field = *I;
552 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000553 llvm::StringRef FieldName = Field->getName();
554
Devang Patel4835fdd2010-02-12 01:31:06 +0000555 // Ignore unnamed fields. Do not ignore unnamed records.
556 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000557 continue;
558
559 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000560 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
561 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000562 QualType FType = Field->getType();
563 uint64_t FieldSize = 0;
564 unsigned FieldAlign = 0;
565 if (!FType->isIncompleteArrayType()) {
566
567 // Bit size, align and offset of the type.
568 FieldSize = CGM.getContext().getTypeSize(FType);
569 Expr *BitWidth = Field->getBitWidth();
570 if (BitWidth)
571 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000572 FieldAlign = CGM.getContext().getTypeAlign(FType);
573 }
574
575 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
576
Devang Patel71f4ff62010-04-21 23:12:37 +0000577 unsigned Flags = 0;
578 AccessSpecifier Access = I->getAccess();
579 if (Access == clang::AS_private)
580 Flags |= llvm::DIType::FlagPrivate;
581 else if (Access == clang::AS_protected)
582 Flags |= llvm::DIType::FlagProtected;
583
Devang Patel428deb52010-01-19 00:00:59 +0000584 // Create a DW_TAG_member node to remember the offset of this field in the
585 // struct. FIXME: This is an absolutely insane way to capture this
586 // information. When we gut debug info, this should be fixed.
587 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
588 FieldName, FieldDefUnit,
589 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000590 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000591 EltTys.push_back(FieldTy);
592 }
593}
594
Devang Patela6da1922010-01-28 00:28:01 +0000595/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
596/// function type is not updated to include implicit "this" pointer. Use this
597/// routine to get a method type which includes "this" pointer.
598llvm::DIType
599CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000600 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000601 llvm::DIType FnTy
602 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
603 0),
604 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000605
Devang Patel2ed8f002010-08-27 17:47:47 +0000606 unsigned BFlags=0;
607 AccessSpecifier Access = Method->getAccess();
608 if (Access == clang::AS_private)
609 BFlags |= llvm::DIType::FlagPrivate;
610 else if (Access == clang::AS_protected)
611 BFlags |= llvm::DIType::FlagProtected;
Devang Pateld774d1e2010-01-28 21:43:50 +0000612
Devang Patela6da1922010-01-28 00:28:01 +0000613 // Add "this" pointer.
614
Devang Patelab699792010-05-07 18:12:35 +0000615 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000616 assert (Args.getNumElements() && "Invalid number of arguments!");
617
618 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
619
620 // First element is always return type. For 'void' functions it is NULL.
621 Elts.push_back(Args.getElement(0));
622
Devang Patel2ed8f002010-08-27 17:47:47 +0000623 if (!Method->isStatic())
624 {
625 // "this" pointer is always first argument.
626 ASTContext &Context = CGM.getContext();
627 QualType ThisPtr =
628 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
629 llvm::DIType ThisPtrType =
630 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000631
Devang Patel2ed8f002010-08-27 17:47:47 +0000632 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
633 Elts.push_back(ThisPtrType);
634 }
Devang Patela6da1922010-01-28 00:28:01 +0000635
636 // Copy rest of the arguments.
637 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
638 Elts.push_back(Args.getElement(i));
639
640 llvm::DIArray EltTypeArray =
641 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
642
643 return
644 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000645 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000646 0, 0, 0, 0, 0,
647 llvm::DIType(), EltTypeArray);
648}
649
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000650/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
651/// a single member function GlobalDecl.
652llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000653CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000654 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000655 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000656 bool IsCtorOrDtor =
657 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
658
659 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000660 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000661
662 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
663 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000664 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000665 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000666 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000667
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000668 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000669 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
670 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000671
672 // Collect virtual method info.
673 llvm::DIType ContainingType;
674 unsigned Virtuality = 0;
675 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000676
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000677 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000678 if (Method->isPure())
679 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
680 else
681 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
682
683 // It doesn't make sense to give a virtual destructor a vtable index,
684 // since a single destructor has two entries in the vtable.
685 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000686 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000687 ContainingType = RecordTy;
688 }
689
690 llvm::DISubprogram SP =
691 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
692 MethodLinkageName,
693 MethodDefUnit, MethodLine,
694 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000695 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000696 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000697 Method->isImplicit(),
698 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000699
700 // Don't cache ctors or dtors since we have to emit multiple functions for
701 // a single ctor or dtor.
702 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000703 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000704
705 return SP;
706}
707
Devang Patel4125fd22010-01-19 01:54:44 +0000708/// CollectCXXMemberFunctions - A helper function to collect debug info for
709/// C++ member functions.This is used while creating debug info entry for
710/// a Record.
711void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000712CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000713 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000714 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000715 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
716 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000717 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000718
Devang Pateld5322da2010-02-09 19:09:28 +0000719 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000720 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000721
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000722 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000723 }
724}
725
Devang Patel2ed8f002010-08-27 17:47:47 +0000726/// CollectCXXFriends - A helper function to collect debug info for
727/// C++ base classes. This is used while creating debug info entry for
728/// a Record.
729void CGDebugInfo::
730CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
731 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
732 llvm::DIType RecordTy) {
733
734 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
735 BE = RD->friend_end(); BI != BE; ++BI) {
736
737 TypeSourceInfo *TInfo = (*BI)->getFriendType();
738 if(TInfo)
739 {
740 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
741
742 llvm::DIType DTy =
743 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
744 RecordTy, llvm::StringRef(),
745 Unit, 0, 0, 0,
746 0, 0, Ty);
747
748 EltTys.push_back(DTy);
749 }
750
751 }
752}
753
Devang Patela245c5b2010-01-25 23:32:18 +0000754/// CollectCXXBases - A helper function to collect debug info for
755/// C++ base classes. This is used while creating debug info entry for
756/// a Record.
757void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000758CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000759 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000760 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000761
Devang Patel239cec62010-02-01 21:39:52 +0000762 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
763 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
764 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000765 unsigned BFlags = 0;
766 uint64_t BaseOffset;
767
768 const CXXRecordDecl *Base =
769 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
770
771 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000772 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000773 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000774 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000775 BFlags = llvm::DIType::FlagVirtual;
776 } else
777 BaseOffset = RL.getBaseClassOffset(Base);
778
779 AccessSpecifier Access = BI->getAccessSpecifier();
780 if (Access == clang::AS_private)
781 BFlags |= llvm::DIType::FlagPrivate;
782 else if (Access == clang::AS_protected)
783 BFlags |= llvm::DIType::FlagProtected;
784
785 llvm::DIType DTy =
786 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
787 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000788 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000789 BaseOffset, BFlags,
790 getOrCreateType(BI->getType(),
791 Unit));
792 EltTys.push_back(DTy);
793 }
Devang Patela245c5b2010-01-25 23:32:18 +0000794}
795
Devang Patel4ce3f202010-01-28 18:11:52 +0000796/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000797llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000798 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000799 return VTablePtrType;
800
801 ASTContext &Context = CGM.getContext();
802
803 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000804 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
805 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000806 llvm::DIType SubTy =
807 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000808 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000809 0, 0, 0, 0, 0, llvm::DIType(), SElements);
810
811 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
812 llvm::DIType vtbl_ptr_type
813 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000814 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000815 0, Size, 0, 0, 0, SubTy);
816
Devang Pateld58562e2010-03-09 22:49:11 +0000817 VTablePtrType =
818 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
819 Unit, "", Unit,
820 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000821 return VTablePtrType;
822}
823
Anders Carlsson046c2942010-04-17 20:15:18 +0000824/// getVTableName - Get vtable name for the given Class.
825llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000826 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000827 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000828
829 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000830 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000831 memcpy(StrPtr, Name.data(), Name.length());
832 return llvm::StringRef(StrPtr, Name.length());
833}
834
835
Anders Carlsson046c2942010-04-17 20:15:18 +0000836/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000837/// debug info entry in EltTys vector.
838void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000839CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000840 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000841 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000842
843 // If there is a primary base then it will hold vtable info.
844 if (RL.getPrimaryBase())
845 return;
846
847 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000848 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000849 return;
850
851 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
852 llvm::DIType VPTR
853 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000854 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000855 0, Size, 0, 0, 0,
856 getOrCreateVTablePtrType(Unit));
857 EltTys.push_back(VPTR);
858}
859
Devang Patel65e99f22009-02-25 01:36:11 +0000860/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000861llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000862 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000863 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattner9c85ba32008-11-10 06:08:34 +0000865 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000866 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000867 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000868 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000869 Tag = llvm::dwarf::DW_TAG_union_type;
870 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000871 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000872 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000873 }
874
Chris Lattner9c85ba32008-11-10 06:08:34 +0000875 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000876 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
877 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner9c85ba32008-11-10 06:08:34 +0000879 // Records and classes and unions can all be recursive. To handle them, we
880 // first generate a debug descriptor for the struct as a forward declaration.
881 // Then (if it is a definition) we go through and get debug info for all of
882 // its members. Finally, we create a descriptor for the complete type (which
883 // may refer to the forward decl if the struct is recursive) and replace all
884 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000885 llvm::DIDescriptor FDContext =
886 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
887
888 // If this is just a forward declaration, construct an appropriately
889 // marked node and just return it.
890 if (!RD->getDefinition()) {
891 llvm::DICompositeType FwdDecl =
892 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
893 DefUnit, Line, 0, 0, 0,
894 llvm::DIType::FlagFwdDecl,
895 llvm::DIType(), llvm::DIArray());
896
897 return FwdDecl;
898 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000899
Dan Gohman86968372010-08-20 22:39:57 +0000900 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000901
Devang Patelab699792010-05-07 18:12:35 +0000902 llvm::MDNode *MN = FwdDecl;
903 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000904 // Otherwise, insert it into the TypeCache so that recursive uses will find
905 // it.
Devang Patelab699792010-05-07 18:12:35 +0000906 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000907 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000908 RegionStack.push_back(FwdDeclNode);
909 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000910
911 // Convert all the elements.
912 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
913
Devang Pateld6c5a262010-02-01 21:52:22 +0000914 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000915 if (CXXDecl) {
916 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000917 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000918 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000919
920 // Collect static variables with initializers.
921 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
922 I != E; ++I)
923 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
924 if (const Expr *Init = V->getInit()) {
925 Expr::EvalResult Result;
926 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
927 llvm::ConstantInt *CI
928 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
929
930 // Create the descriptor for static variable.
931 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
932 llvm::StringRef VName = V->getName();
933 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
934 // Do not use DIGlobalVariable for enums.
935 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
936 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
937 getLineNumber(V->getLocation()),
938 VTy, true, true, CI);
939 }
940 }
941 }
942 }
943
Devang Pateld6c5a262010-02-01 21:52:22 +0000944 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000945 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000946 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000947 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +0000948 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000949
950 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000951 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000952 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
953 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000954 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000955 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000956 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000957 }
Mike Stump1eb44332009-09-09 15:08:12 +0000958
Chris Lattner9c85ba32008-11-10 06:08:34 +0000959 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000960 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000961
962 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000963 uint64_t Size = CGM.getContext().getTypeSize(Ty);
964 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Devang Patele4c1ea02010-03-11 20:01:48 +0000966 RegionStack.pop_back();
967 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
968 RegionMap.find(Ty->getDecl());
969 if (RI != RegionMap.end())
970 RegionMap.erase(RI);
971
Devang Patel411894b2010-02-01 22:40:08 +0000972 llvm::DIDescriptor RDContext =
973 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000974
975 llvm::StringRef RDName = RD->getName();
976 // If this is a class, include the template arguments also.
977 if (Tag == llvm::dwarf::DW_TAG_class_type)
978 RDName = getClassName(RD);
979
Devang Patel0ce73f62009-07-22 18:57:00 +0000980 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000981 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000982 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000983 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000984 llvm::DIType(), Elements,
985 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000986
987 // Now that we have a real decl for the struct, replace anything using the
988 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +0000989 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000990 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000991 return RealDecl;
992}
993
John McCallc12c5bb2010-05-15 11:32:37 +0000994/// CreateType - get objective-c object type.
995llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
996 llvm::DIFile Unit) {
997 // Ignore protocols.
998 return getOrCreateType(Ty->getBaseType(), Unit);
999}
1000
Devang Patel9ca36b62009-02-26 21:10:26 +00001001/// CreateType - get objective-c interface type.
1002llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001003 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001004 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001005 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001006
1007 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001008 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001009 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001010 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001011
Dan Gohman45f7c782010-08-23 21:15:56 +00001012 // If this is just a forward declaration, return a special forward-declaration
1013 // debug type.
1014 if (ID->isForwardDecl()) {
1015 llvm::DICompositeType FwdDecl =
1016 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1017 DefUnit, Line, 0, 0, 0, 0,
1018 llvm::DIType(), llvm::DIArray(),
1019 RuntimeLang);
1020 return FwdDecl;
1021 }
1022
Devang Patel9ca36b62009-02-26 21:10:26 +00001023 // To handle recursive interface, we
1024 // first generate a debug descriptor for the struct as a forward declaration.
1025 // Then (if it is a definition) we go through and get debug info for all of
1026 // its members. Finally, we create a descriptor for the complete type (which
1027 // may refer to the forward decl if the struct is recursive) and replace all
1028 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001029 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001030
Devang Patelab699792010-05-07 18:12:35 +00001031 llvm::MDNode *MN = FwdDecl;
1032 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001033 // Otherwise, insert it into the TypeCache so that recursive uses will find
1034 // it.
Devang Patelab699792010-05-07 18:12:35 +00001035 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001036 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001037 RegionStack.push_back(FwdDeclNode);
1038 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001039
1040 // Convert all the elements.
1041 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1042
Devang Pateld6c5a262010-02-01 21:52:22 +00001043 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001044 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001045 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001046 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001047 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001048 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001049 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001050 0 /* offset */, 0, SClassTy);
1051 EltTys.push_back(InhTag);
1052 }
1053
Devang Pateld6c5a262010-02-01 21:52:22 +00001054 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001055
1056 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +00001057 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1058 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001059 ObjCIvarDecl *Field = *I;
1060 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1061
Devang Patel73621622009-11-25 17:37:31 +00001062 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001063
Devang Patelde135022009-04-27 22:40:36 +00001064 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001065 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001066 continue;
1067
Devang Patel9ca36b62009-02-26 21:10:26 +00001068 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001069 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1070 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001071 QualType FType = Field->getType();
1072 uint64_t FieldSize = 0;
1073 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001074
Devang Patel99c20eb2009-03-20 18:24:39 +00001075 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001076
Devang Patel99c20eb2009-03-20 18:24:39 +00001077 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001078 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001079 Expr *BitWidth = Field->getBitWidth();
1080 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001081 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001082
Anders Carlsson20f12a22009-12-06 18:00:51 +00001083 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001084 }
1085
Mike Stump1eb44332009-09-09 15:08:12 +00001086 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1087
Devang Patelc20482b2009-03-19 00:23:53 +00001088 unsigned Flags = 0;
1089 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1090 Flags = llvm::DIType::FlagProtected;
1091 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1092 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001093
Devang Patel9ca36b62009-02-26 21:10:26 +00001094 // Create a DW_TAG_member node to remember the offset of this field in the
1095 // struct. FIXME: This is an absolutely insane way to capture this
1096 // information. When we gut debug info, this should be fixed.
1097 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1098 FieldName, FieldDefUnit,
1099 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001100 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001101 EltTys.push_back(FieldTy);
1102 }
Mike Stump1eb44332009-09-09 15:08:12 +00001103
Devang Patel9ca36b62009-02-26 21:10:26 +00001104 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001105 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001106
Devang Patele4c1ea02010-03-11 20:01:48 +00001107 RegionStack.pop_back();
1108 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1109 RegionMap.find(Ty->getDecl());
1110 if (RI != RegionMap.end())
1111 RegionMap.erase(RI);
1112
Devang Patel9ca36b62009-02-26 21:10:26 +00001113 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001114 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1115 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001116
Devang Patel6c1fddf2009-07-27 18:42:03 +00001117 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001118 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001119 Line, Size, Align, 0, 0, llvm::DIType(),
1120 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001121
1122 // Now that we have a real decl for the struct, replace anything using the
1123 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001124 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001125 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001126
Devang Patel9ca36b62009-02-26 21:10:26 +00001127 return RealDecl;
1128}
1129
Chris Lattner9c85ba32008-11-10 06:08:34 +00001130llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001131 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001132 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001133
Chris Lattner9c85ba32008-11-10 06:08:34 +00001134}
1135
1136llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001137 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001138 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1139 return CreateType(RT, Unit);
1140 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1141 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001142
Chris Lattner9c85ba32008-11-10 06:08:34 +00001143 return llvm::DIType();
1144}
1145
Devang Patel70c23cd2010-02-23 22:59:39 +00001146llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001147 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001148 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1149 uint64_t NumElems = Ty->getNumElements();
1150 if (NumElems > 0)
1151 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001152
Benjamin Kramerad468862010-03-30 11:36:44 +00001153 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1154 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001155
1156 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1157 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1158
1159 return
1160 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001161 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001162 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001163 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001164}
1165
Chris Lattner9c85ba32008-11-10 06:08:34 +00001166llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001167 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001168 uint64_t Size;
1169 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001170
1171
Nuno Lopes010d5142009-01-28 00:35:17 +00001172 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001173 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001174 Size = 0;
1175 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001176 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001177 } else if (Ty->isIncompleteArrayType()) {
1178 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001179 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001180 } else {
1181 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001182 Size = CGM.getContext().getTypeSize(Ty);
1183 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001184 }
Mike Stump1eb44332009-09-09 15:08:12 +00001185
Chris Lattner9c85ba32008-11-10 06:08:34 +00001186 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1187 // interior arrays, do we care? Why aren't nested arrays represented the
1188 // obvious/recursive way?
1189 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1190 QualType EltTy(Ty, 0);
1191 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001192 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001193 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001194 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001195 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001196 // FIXME: Verify this is right for VLAs.
1197 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1198 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001199 }
Mike Stump1eb44332009-09-09 15:08:12 +00001200
Chris Lattner9c85ba32008-11-10 06:08:34 +00001201 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001202 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001203
Devang Patelca80a5f2009-10-20 19:55:01 +00001204 llvm::DIType DbgTy =
1205 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001206 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001207 0, Size, Align, 0, 0,
1208 getOrCreateType(EltTy, Unit),
1209 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001210 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001211}
1212
Anders Carlssona031b352009-11-06 19:19:55 +00001213llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001214 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001215 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1216 Ty, Ty->getPointeeType(), Unit);
1217}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001218
Anders Carlsson20f12a22009-12-06 18:00:51 +00001219llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001220 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001221 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1222 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1223
1224 if (!Ty->getPointeeType()->isFunctionType()) {
1225 // We have a data member pointer type.
1226 return PointerDiffDITy;
1227 }
1228
1229 // We have a member function pointer type. Treat it as a struct with two
1230 // ptrdiff_t members.
1231 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1232
1233 uint64_t FieldOffset = 0;
1234 llvm::DIDescriptor ElementTypes[2];
1235
1236 // FIXME: This should probably be a function type instead.
1237 ElementTypes[0] =
1238 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001239 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001240 Info.first, Info.second, FieldOffset, 0,
1241 PointerDiffDITy);
1242 FieldOffset += Info.first;
1243
1244 ElementTypes[1] =
1245 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001246 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001247 Info.first, Info.second, FieldOffset, 0,
1248 PointerDiffDITy);
1249
1250 llvm::DIArray Elements =
1251 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1252 llvm::array_lengthof(ElementTypes));
1253
1254 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1255 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001256 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001257 0, 0, 0, llvm::DIType(), Elements);
1258}
1259
Devang Patel6237cea2010-08-23 22:07:25 +00001260/// CreateEnumType - get enumeration type.
1261llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1262 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1263
1264 // Create DIEnumerator elements for each enumerator.
1265 for (EnumDecl::enumerator_iterator
1266 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1267 Enum != EnumEnd; ++Enum) {
1268 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1269 Enum->getInitVal().getZExtValue()));
1270 }
1271
1272 // Return a CompositeType for the enum itself.
1273 llvm::DIArray EltArray =
1274 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1275
1276 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1277 unsigned Line = getLineNumber(ED->getLocation());
1278 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001279 uint64_t Align = 0;
1280 if (!ED->getTypeForDecl()->isIncompleteType()) {
1281 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1282 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1283 }
Devang Patel6237cea2010-08-23 22:07:25 +00001284 llvm::DIType DbgTy =
1285 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1286 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001287 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001288 llvm::DIType(), EltArray);
1289 return DbgTy;
1290}
1291
Douglas Gregor840943d2009-12-21 20:18:30 +00001292static QualType UnwrapTypeForDebugInfo(QualType T) {
1293 do {
1294 QualType LastT = T;
1295 switch (T->getTypeClass()) {
1296 default:
1297 return T;
1298 case Type::TemplateSpecialization:
1299 T = cast<TemplateSpecializationType>(T)->desugar();
1300 break;
1301 case Type::TypeOfExpr: {
1302 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1303 T = Ty->getUnderlyingExpr()->getType();
1304 break;
1305 }
1306 case Type::TypeOf:
1307 T = cast<TypeOfType>(T)->getUnderlyingType();
1308 break;
1309 case Type::Decltype:
1310 T = cast<DecltypeType>(T)->getUnderlyingType();
1311 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001312 case Type::Elaborated:
1313 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001314 break;
1315 case Type::SubstTemplateTypeParm:
1316 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1317 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001318 }
1319
1320 assert(T != LastT && "Type unwrapping failed to unwrap!");
1321 if (T == LastT)
1322 return T;
1323 } while (true);
1324
1325 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001326}
1327
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001328/// getOrCreateType - Get the type from the cache or create a new
1329/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001330llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001331 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001332 if (Ty.isNull())
1333 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Douglas Gregor840943d2009-12-21 20:18:30 +00001335 // Unwrap the type as needed for debug information.
1336 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001337
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001338 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001339 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001340 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001341 if (it != TypeCache.end()) {
1342 // Verify that the debug info still exists.
1343 if (&*it->second)
1344 return llvm::DIType(cast<llvm::MDNode>(it->second));
1345 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001346
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001347 // Otherwise create the type.
1348 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001349
1350 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001351 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001352 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001353}
1354
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001355/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001356llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001357 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001358 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001359 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001360 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001361
Douglas Gregor2101a822009-12-21 19:57:21 +00001362 const char *Diag = 0;
1363
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001364 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001365 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001366#define TYPE(Class, Base)
1367#define ABSTRACT_TYPE(Class, Base)
1368#define NON_CANONICAL_TYPE(Class, Base)
1369#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1370#include "clang/AST/TypeNodes.def"
1371 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001372
Anders Carlssonbfe69952009-11-06 18:24:04 +00001373 // FIXME: Handle these.
1374 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001375 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001376
1377 case Type::Vector:
1378 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001379 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001380 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001381 case Type::ObjCObject:
1382 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001383 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001384 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1385 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1386 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1387 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001388 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001389 return CreateType(cast<BlockPointerType>(Ty), Unit);
1390 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001391 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001392 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001393 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001394 case Type::FunctionProto:
1395 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001396 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001397 case Type::ConstantArray:
1398 case Type::VariableArray:
1399 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001400 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001401
1402 case Type::LValueReference:
1403 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1404
Anders Carlsson20f12a22009-12-06 18:00:51 +00001405 case Type::MemberPointer:
1406 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001407
1408 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001409 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001410 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001411 case Type::TypeOfExpr:
1412 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001413 case Type::Decltype:
1414 llvm_unreachable("type should have been unwrapped!");
1415 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001416
1417 case Type::RValueReference:
1418 // FIXME: Implement!
1419 Diag = "rvalue references";
1420 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001421 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001422
1423 assert(Diag && "Fall through without a diagnostic?");
1424 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1425 "debug information for %0 is not yet supported");
1426 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1427 << Diag;
1428 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001429}
1430
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001431/// CreateMemberType - Create new member and increase Offset by FType's size.
1432llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1433 llvm::StringRef Name,
1434 uint64_t *Offset) {
1435 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1436 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1437 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1438 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1439 Unit, Name, Unit, 0,
1440 FieldSize, FieldAlign,
1441 *Offset, 0, FieldTy);
1442 *Offset += FieldSize;
1443 return Ty;
1444}
1445
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001446/// EmitFunctionStart - Constructs the debug code for entering a function -
1447/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001448void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001449 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001450 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001451
Devang Patel9c6c3a02010-01-14 00:36:21 +00001452 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001453 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001454
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001455 FnBeginRegionCount.push_back(RegionStack.size());
1456
Devang Patel9c6c3a02010-01-14 00:36:21 +00001457 const Decl *D = GD.getDecl();
1458 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001459 // If there is a DISubprogram for this function available then use it.
1460 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1461 FI = SPCache.find(FD);
1462 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001463 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001464 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1465 llvm::MDNode *SPN = SP;
1466 RegionStack.push_back(SPN);
1467 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001468 return;
1469 }
1470 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001471 Name = getFunctionName(FD);
1472 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001473 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001474 } else {
1475 // Use llvm function name as linkage name.
1476 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001477 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001478 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001479 if (!Name.empty() && Name[0] == '\01')
1480 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001481
Devang Patel970c6182010-04-24 00:49:16 +00001482 // It is expected that CurLoc is set before using EmitFunctionStart.
1483 // Usually, CurLoc points to the left bracket location of compound
1484 // statement representing function body.
1485 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001486 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001487
Chris Lattner9c85ba32008-11-10 06:08:34 +00001488 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001489 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001490 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001491 Fn->hasInternalLinkage(), true/*definition*/,
1492 0, 0, llvm::DIType(),
1493 D->isImplicit(),
1494 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001495
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001496 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001497 llvm::MDNode *SPN = SP;
1498 RegionStack.push_back(SPN);
1499 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001500
1501 // Clear stack used to keep track of #line directives.
1502 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001503}
1504
1505
Devang Patel4d939e62010-07-20 22:20:10 +00001506void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001507 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001508
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001509 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001510 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001511 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001512 || (SM.getInstantiationLineNumber(CurLoc) ==
1513 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001514 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001515 // New Builder may not be in sync with CGDebugInfo.
1516 if (!Builder.getCurrentDebugLocation().isUnknown())
1517 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001518
1519 // Update last state.
1520 PrevLoc = CurLoc;
1521
Chris Lattnerc6034632010-04-01 06:31:43 +00001522 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001523 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1524 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001525 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001526}
1527
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001528/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1529/// has introduced scope change.
1530void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1531 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1532 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1533 return;
1534 SourceManager &SM = CGM.getContext().getSourceManager();
1535 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1536 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1537
1538 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1539 return;
1540
1541 // If #line directive stack is empty then we are entering a new scope.
1542 if (LineDirectiveFiles.empty()) {
1543 EmitRegionStart(Builder);
1544 LineDirectiveFiles.push_back(PCLoc.getFilename());
1545 return;
1546 }
1547
1548 assert (RegionStack.size() >= LineDirectiveFiles.size()
1549 && "error handling #line regions!");
1550
1551 bool SeenThisFile = false;
1552 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1553 E = LineDirectiveFiles.end(); I != E; ++I)
1554 if (!strcmp(PPLoc.getFilename(), *I)) {
1555 SeenThisFile = true;
1556 break;
1557 }
1558
1559 // If #line for this file is seen earlier then pop out #line regions.
1560 if (SeenThisFile) {
1561 while (!LineDirectiveFiles.empty()) {
1562 const char *LastFile = LineDirectiveFiles.back();
1563 RegionStack.pop_back();
1564 LineDirectiveFiles.pop_back();
1565 if (!strcmp(PPLoc.getFilename(), LastFile))
1566 break;
1567 }
1568 return;
1569 }
1570
1571 // .. otherwise insert new #line region.
1572 EmitRegionStart(Builder);
1573 LineDirectiveFiles.push_back(PCLoc.getFilename());
1574
1575 return;
1576}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001577/// EmitRegionStart- Constructs the debug code for entering a declarative
1578/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001579void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001580 llvm::DIDescriptor D =
1581 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1582 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001583 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001584 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001585 getLineNumber(CurLoc),
1586 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001587 llvm::MDNode *DN = D;
1588 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001589}
1590
1591/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1592/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001593void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001594 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1595
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001596 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001597 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001598
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001599 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001600}
1601
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001602/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1603void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1604 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1605 unsigned RCount = FnBeginRegionCount.back();
1606 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1607
1608 // Pop all regions for this function.
1609 while (RegionStack.size() != RCount)
1610 EmitRegionEnd(Builder);
1611 FnBeginRegionCount.pop_back();
1612}
1613
Devang Patel809b9bb2010-02-10 18:49:08 +00001614// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1615// See BuildByRefType.
1616llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1617 uint64_t *XOffset) {
1618
1619 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1620
1621 QualType FType;
1622 uint64_t FieldSize, FieldOffset;
1623 unsigned FieldAlign;
1624
Devang Patel17800552010-03-09 00:44:50 +00001625 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001626 QualType Type = VD->getType();
1627
1628 FieldOffset = 0;
1629 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001630 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1631 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001632 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001633 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1634 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1635
Devang Patel809b9bb2010-02-10 18:49:08 +00001636 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1637 if (HasCopyAndDispose) {
1638 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001639 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1640 &FieldOffset));
1641 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1642 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001643 }
1644
1645 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1646 if (Align > CharUnits::fromQuantity(
1647 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1648 unsigned AlignedOffsetInBytes
1649 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1650 unsigned NumPaddingBytes
1651 = AlignedOffsetInBytes - FieldOffset/8;
1652
1653 if (NumPaddingBytes > 0) {
1654 llvm::APInt pad(32, NumPaddingBytes);
1655 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1656 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001657 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001658 }
1659 }
1660
1661 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001662 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001663 FieldSize = CGM.getContext().getTypeSize(FType);
1664 FieldAlign = Align.getQuantity()*8;
1665
1666 *XOffset = FieldOffset;
1667 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001668 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001669 0, FieldSize, FieldAlign,
1670 FieldOffset, 0, FieldTy);
1671 EltTys.push_back(FieldTy);
1672 FieldOffset += FieldSize;
1673
1674 llvm::DIArray Elements =
1675 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1676
1677 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1678
1679 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001680 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001681 0, FieldOffset, 0, 0, Flags,
1682 llvm::DIType(), Elements);
1683
1684}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001685/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001686void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001687 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001688 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1689
Devang Patel17800552010-03-09 00:44:50 +00001690 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001691 llvm::DIType Ty;
1692 uint64_t XOffset = 0;
1693 if (VD->hasAttr<BlocksAttr>())
1694 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1695 else
1696 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001697
Devang Patelf4e54a22010-05-07 23:05:55 +00001698 // If there is not any debug info for type then do not emit debug info
1699 // for this variable.
1700 if (!Ty)
1701 return;
1702
Chris Lattner9c85ba32008-11-10 06:08:34 +00001703 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001704 unsigned Line = getLineNumber(VD->getLocation());
1705 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001706
Chris Lattner9c85ba32008-11-10 06:08:34 +00001707 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001708 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001709 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001710 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001711 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001712 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001713 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001714 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001715
Chris Lattnerc6034632010-04-01 06:31:43 +00001716 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001717 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001718}
1719
Mike Stumpb1a6e682009-09-30 02:43:10 +00001720/// EmitDeclare - Emit local variable declaration debug info.
1721void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1722 llvm::Value *Storage, CGBuilderTy &Builder,
1723 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001724 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001725 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1726
Devang Patel2b594b92010-04-26 23:28:46 +00001727 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001728 return;
1729
1730 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001731 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001732 llvm::DIType Ty;
1733 if (VD->hasAttr<BlocksAttr>())
1734 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1735 else
1736 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001737
1738 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001739 unsigned Line = getLineNumber(VD->getLocation());
1740 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001741
Devang Pateld6c5a262010-02-01 21:52:22 +00001742 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001743 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001744 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1745 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1746 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1747 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001748 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001749 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1750 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001751 // offset of __forwarding field
1752 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001753 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1754 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1755 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001756 // offset of x field
1757 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001758 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001759 }
1760
1761 // Create the descriptor for the variable.
1762 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001763 DebugFactory.CreateComplexVariable(Tag,
1764 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001765 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001766 addr);
1767 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001768 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001769 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001770
Chris Lattnerc6034632010-04-01 06:31:43 +00001771 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001772 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001773}
1774
Devang Pateld6c5a262010-02-01 21:52:22 +00001775void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001776 llvm::Value *Storage,
1777 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001778 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001779}
1780
Mike Stumpb1a6e682009-09-30 02:43:10 +00001781void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1782 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1783 CodeGenFunction *CGF) {
1784 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1785}
1786
Chris Lattner9c85ba32008-11-10 06:08:34 +00001787/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1788/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001789void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001790 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001791 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001792}
1793
1794
1795
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001796/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001797void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001798 const VarDecl *D) {
1799
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001800 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001801 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001802 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001803
Devang Pateleb6d79b2010-02-01 21:34:11 +00001804 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001805 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001806
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001807 // CodeGen turns int[] into int[1] so we'll do the same here.
1808 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001809
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001810 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001811 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001812
Anders Carlsson20f12a22009-12-06 18:00:51 +00001813 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001814 ArrayType::Normal, 0);
1815 }
Devang Patel5d822f02010-04-29 17:48:37 +00001816 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001817 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001818 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001819 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001820 llvm::DIDescriptor DContext =
1821 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001822 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1823 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001824 Var->hasInternalLinkage(),
1825 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001826}
1827
Devang Patel9ca36b62009-02-26 21:10:26 +00001828/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001829void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001830 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001831 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001832 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001833 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001834
Devang Pateld6c5a262010-02-01 21:52:22 +00001835 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001836
Devang Pateld6c5a262010-02-01 21:52:22 +00001837 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001838 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001839
Devang Patel9ca36b62009-02-26 21:10:26 +00001840 // CodeGen turns int[] into int[1] so we'll do the same here.
1841 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001842
Devang Patel9ca36b62009-02-26 21:10:26 +00001843 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001844 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001845
Anders Carlsson20f12a22009-12-06 18:00:51 +00001846 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001847 ArrayType::Normal, 0);
1848 }
1849
Devang Patelf6a39b72009-10-20 18:26:30 +00001850 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001851 getOrCreateType(T, Unit),
1852 Var->hasInternalLinkage(),
1853 true/*definition*/, Var);
1854}
Devang Patelabb485f2010-02-01 19:16:32 +00001855
Devang Patel25c2c8f2010-08-10 17:53:33 +00001856/// EmitGlobalVariable - Emit global variable's debug info.
1857void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1858 llvm::ConstantInt *Init,
Devang Patel8d308382010-08-10 07:24:25 +00001859 CGBuilderTy &Builder) {
1860 // Create the descriptor for the variable.
1861 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1862 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001863 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001864 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1865 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1866 Ty = CreateEnumType(ED, Unit);
1867 }
Devang Patel0317ab02010-08-10 18:27:15 +00001868 // Do not use DIGlobalVariable for enums.
1869 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1870 return;
Devang Patel8d308382010-08-10 07:24:25 +00001871 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1872 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001873 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001874}
1875
Devang Patelabb485f2010-02-01 19:16:32 +00001876/// getOrCreateNamesSpace - Return namespace descriptor for the given
1877/// namespace decl.
1878llvm::DINameSpace
1879CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1880 llvm::DIDescriptor Unit) {
1881 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1882 NameSpaceCache.find(NSDecl);
1883 if (I != NameSpaceCache.end())
1884 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1885
Devang Patel8ab870d2010-05-12 23:46:38 +00001886 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001887
1888 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001889 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001890 llvm::DINameSpace NS =
1891 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00001892 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00001893 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001894 return NS;
1895}