blob: 1bdc91016b0cb7a81fa3d0c32281a3df7c8becd5 [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000018#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000019#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000020#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000021#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000022#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000024#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000025#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000026#include "llvm/Constants.h"
27#include "llvm/DerivedTypes.h"
28#include "llvm/Instructions.h"
29#include "llvm/Intrinsics.h"
30#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000031#include "llvm/ADT/StringExtras.h"
32#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000033#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000034#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000035#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000036using namespace clang;
37using namespace clang::CodeGen;
38
Anders Carlsson20f12a22009-12-06 18:00:51 +000039CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000040 : CGM(CGM), DebugFactory(CGM.getModule()),
Devang Patel7573f8b2010-03-09 21:32:27 +000041 FwdDeclCount(0), BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000042 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000043}
44
Chris Lattner9c85ba32008-11-10 06:08:34 +000045CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000046 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000047}
48
Chris Lattner9c85ba32008-11-10 06:08:34 +000049void CGDebugInfo::setLocation(SourceLocation Loc) {
50 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000051 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000052}
53
Devang Patel33583052010-01-28 23:15:27 +000054/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000055llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000056 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000057 if (!Context)
58 return CompileUnit;
59
60 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
61 I = RegionMap.find(Context);
62 if (I != RegionMap.end())
63 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(I->second));
Devang Patel411894b2010-02-01 22:40:08 +000064
Devang Pateleb6d79b2010-02-01 21:34:11 +000065 // Check namespace.
66 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
67 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000068
69 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
70 if (!RDecl->isDependentType()) {
71 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
72 llvm::DIFile(CompileUnit));
73 return llvm::DIDescriptor(Ty);
74 }
75 }
Devang Patel979ec2e2009-10-06 00:35:31 +000076 return CompileUnit;
77}
78
Devang Patel9c6c3a02010-01-14 00:36:21 +000079/// getFunctionName - Get function name for the given FunctionDecl. If the
80/// name is constructred on demand (e.g. C++ destructor) then the name
81/// is stored on the side.
82llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
83 assert (FD && "Invalid FunctionDecl!");
84 IdentifierInfo *FII = FD->getIdentifier();
85 if (FII)
86 return FII->getName();
87
88 // Otherwise construct human readable name for debug info.
89 std::string NS = FD->getNameAsString();
90
91 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000092 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000093 memcpy(StrPtr, NS.data(), NS.length());
94 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000095}
96
Devang Patel700a1cb2010-07-20 20:24:18 +000097/// getClassName - Get class name including template argument list.
98llvm::StringRef
99CGDebugInfo::getClassName(RecordDecl *RD) {
100 ClassTemplateSpecializationDecl *Spec
101 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
102 if (!Spec)
103 return RD->getName();
104
105 const TemplateArgument *Args;
106 unsigned NumArgs;
107 std::string Buffer;
108 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
109 const TemplateSpecializationType *TST =
110 cast<TemplateSpecializationType>(TAW->getType());
111 Args = TST->getArgs();
112 NumArgs = TST->getNumArgs();
113 } else {
114 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
115 Args = TemplateArgs.getFlatArgumentList();
116 NumArgs = TemplateArgs.flat_size();
117 }
118 Buffer = RD->getIdentifier()->getNameStart();
119 PrintingPolicy Policy(CGM.getLangOptions());
120 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
121 NumArgs,
122 Policy);
123
124 // Copy this name on the side and use its reference.
125 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
126 memcpy(StrPtr, Buffer.data(), Buffer.length());
127 return llvm::StringRef(StrPtr, Buffer.length());
128
129}
130
Devang Patel17800552010-03-09 00:44:50 +0000131/// getOrCreateFile - Get the file debug info descriptor for the input location.
132llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000133 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000134 // If Location is not valid then use main input file.
135 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
136 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000137 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000138 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000139
140 // Cache the results.
141 const char *fname = PLoc.getFilename();
142 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
143 DIFileCache.find(fname);
144
145 if (it != DIFileCache.end()) {
146 // Verify that the information still exists.
147 if (&*it->second)
148 return llvm::DIFile(cast<llvm::MDNode>(it->second));
149 }
150
Devang Patelac4d13c2010-07-27 15:17:16 +0000151 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
152 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000153
Devang Patelab699792010-05-07 18:12:35 +0000154 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000155 return F;
156
Devang Patel17800552010-03-09 00:44:50 +0000157}
Devang Patel8ab870d2010-05-12 23:46:38 +0000158
159/// getLineNumber - Get line number for the location. If location is invalid
160/// then use current location.
161unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
162 assert (CurLoc.isValid() && "Invalid current location!");
163 SourceManager &SM = CGM.getContext().getSourceManager();
164 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
165 return PLoc.getLine();
166}
167
168/// getColumnNumber - Get column number for the location. If location is
169/// invalid then use current location.
170unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
171 assert (CurLoc.isValid() && "Invalid current location!");
172 SourceManager &SM = CGM.getContext().getSourceManager();
173 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
174 return PLoc.getColumn();
175}
176
Devang Patelac4d13c2010-07-27 15:17:16 +0000177llvm::StringRef CGDebugInfo::getCurrentDirname() {
178 if (!CWDName.empty())
179 return CWDName;
180 char *CompDirnamePtr = NULL;
181 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
182 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
183 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
184 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
185}
186
Devang Patel17800552010-03-09 00:44:50 +0000187/// CreateCompileUnit - Create new compile unit.
188void CGDebugInfo::CreateCompileUnit() {
189
190 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000191 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000192 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
193 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000194 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000195
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000196 // The main file name provided via the "-main-file-name" option contains just
197 // the file name itself with no path information. This file name may have had
198 // a relative path, so we look into the actual file entry for the main
199 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000200 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000201 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000202 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000203 if (MainFileDir != ".")
204 MainFileName = MainFileDir + "/" + MainFileName;
205 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000206
Devang Patelac4d13c2010-07-27 15:17:16 +0000207 // Save filename string.
208 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
209 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
210 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
211
Chris Lattner515455a2009-03-25 03:28:08 +0000212 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000213 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000214 if (LO.CPlusPlus) {
215 if (LO.ObjC1)
216 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
217 else
218 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
219 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000220 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000221 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000222 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000223 } else {
224 LangTag = llvm::dwarf::DW_LANG_C89;
225 }
Devang Patel446c6192009-04-17 21:06:59 +0000226
Benjamin Kramer47daf682009-12-08 11:02:29 +0000227 const char *Producer =
Mike Stumpd8945d62009-10-09 18:38:12 +0000228#ifdef CLANG_VENDOR
229 CLANG_VENDOR
230#endif
231 "clang " CLANG_VERSION_STRING;
Chris Lattner4c2577a2009-05-02 01:00:04 +0000232
233 // Figure out which version of the ObjC runtime we have.
234 unsigned RuntimeVers = 0;
235 if (LO.ObjC1)
236 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000237
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000238 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000239 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000240 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000241 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000242 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000243}
244
Devang Patel65e99f22009-02-25 01:36:11 +0000245/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000246/// one if necessary.
247llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000248 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000249 unsigned Encoding = 0;
250 switch (BT->getKind()) {
251 default:
252 case BuiltinType::Void:
253 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000254 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000255 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000256 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000257 llvm::DIType::FlagFwdDecl,
258 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000259 case BuiltinType::ObjCId: {
260 // typedef struct objc_class *Class;
261 // typedef struct objc_object {
262 // Class isa;
263 // } *id;
264
265 llvm::DIType OCTy =
266 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
267 Unit, "objc_class", Unit, 0, 0, 0, 0,
268 llvm::DIType::FlagFwdDecl,
269 llvm::DIType(), llvm::DIArray());
270 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
271
272 llvm::DIType ISATy =
273 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
274 Unit, "", Unit,
275 0, Size, 0, 0, 0, OCTy);
276
277 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
278
279 llvm::DIType FieldTy =
280 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
281 "isa", Unit,
282 0,Size, 0, 0, 0, ISATy);
283 EltTys.push_back(FieldTy);
284 llvm::DIArray Elements =
285 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
286
287 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
288 Unit, "objc_object", Unit, 0, 0, 0, 0,
289 0,
290 llvm::DIType(), Elements);
291 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000292 case BuiltinType::UChar:
293 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
294 case BuiltinType::Char_S:
295 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
296 case BuiltinType::UShort:
297 case BuiltinType::UInt:
298 case BuiltinType::ULong:
299 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
300 case BuiltinType::Short:
301 case BuiltinType::Int:
302 case BuiltinType::Long:
303 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
304 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
305 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000306 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000307 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000308 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000309 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000310 uint64_t Size = CGM.getContext().getTypeSize(BT);
311 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000312 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000313
Devang Patelca80a5f2009-10-20 19:55:01 +0000314 llvm::DIType DbgTy =
315 DebugFactory.CreateBasicType(Unit,
Anders Carlsson20f12a22009-12-06 18:00:51 +0000316 BT->getName(CGM.getContext().getLangOptions()),
Devang Patelca80a5f2009-10-20 19:55:01 +0000317 Unit, 0, Size, Align,
318 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000319 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000320}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000321
Chris Lattnerb7003772009-04-23 06:13:01 +0000322llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000323 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000324 // Bit size, align and offset of the type.
325 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
326 if (Ty->isComplexIntegerType())
327 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000328
Anders Carlsson20f12a22009-12-06 18:00:51 +0000329 uint64_t Size = CGM.getContext().getTypeSize(Ty);
330 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000331 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Devang Patelca80a5f2009-10-20 19:55:01 +0000333 llvm::DIType DbgTy =
334 DebugFactory.CreateBasicType(Unit, "complex",
335 Unit, 0, Size, Align,
336 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000337 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000338}
339
John McCalla1805292009-09-25 01:40:47 +0000340/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000341/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000342llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000343 QualifierCollector Qc;
344 const Type *T = Qc.strip(Ty);
345
346 // Ignore these qualifiers for now.
347 Qc.removeObjCGCAttr();
348 Qc.removeAddressSpace();
349
Chris Lattner9c85ba32008-11-10 06:08:34 +0000350 // We will create one Derived type for one qualifier and recurse to handle any
351 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000352 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000353 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000354 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000355 Qc.removeConst();
356 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000357 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000358 Qc.removeVolatile();
359 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000360 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000361 Qc.removeRestrict();
362 } else {
363 assert(Qc.empty() && "Unknown type qualifier for debug info");
364 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000365 }
Mike Stump1eb44332009-09-09 15:08:12 +0000366
John McCalla1805292009-09-25 01:40:47 +0000367 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
368
Daniel Dunbar3845f862008-10-31 03:54:29 +0000369 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
370 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000371 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000372 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000373 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000374 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000375}
376
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000377llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000378 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000379 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000380 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
381 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000382 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000383}
384
Chris Lattner9c85ba32008-11-10 06:08:34 +0000385llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000386 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000387 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
388 Ty->getPointeeType(), Unit);
389}
390
391llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
392 const Type *Ty,
393 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000394 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000395 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000396
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000397 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000398
399 // Size is always the size of a pointer. We can't use getTypeSize here
400 // because that does not return the correct value for references.
401 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000402 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
403 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000404
Devang Patelca80a5f2009-10-20 19:55:01 +0000405 return
Devang Pateld58562e2010-03-09 22:49:11 +0000406 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000407 0, Size, Align, 0, 0, EltTy);
Anders Carlssona031b352009-11-06 19:19:55 +0000408
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000409}
410
Mike Stump9bc093c2009-05-14 02:03:51 +0000411llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000412 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000413 if (BlockLiteralGenericSet)
414 return BlockLiteralGeneric;
415
Mike Stump9bc093c2009-05-14 02:03:51 +0000416 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
417
418 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
419
420 llvm::DIType FieldTy;
421
422 QualType FType;
423 uint64_t FieldSize, FieldOffset;
424 unsigned FieldAlign;
425
426 llvm::DIArray Elements;
427 llvm::DIType EltTy, DescTy;
428
429 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000430 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000431 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
432 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000433
Daniel Dunbarca308df2009-05-26 19:40:20 +0000434 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000435 EltTys.clear();
436
Mike Stump3d363c52009-10-02 02:30:50 +0000437 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000438 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000439
Mike Stump9bc093c2009-05-14 02:03:51 +0000440 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000441 Unit, LineNo, FieldOffset, 0, 0,
442 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000443
Mike Stump9bc093c2009-05-14 02:03:51 +0000444 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000445 uint64_t Size = CGM.getContext().getTypeSize(Ty);
446 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000447
Mike Stump9bc093c2009-05-14 02:03:51 +0000448 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000449 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000450 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000451
452 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000453 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000454 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000455 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000456 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
457 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000458 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000459 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000460
Anders Carlsson20f12a22009-12-06 18:00:51 +0000461 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000462 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000463 FieldSize = CGM.getContext().getTypeSize(Ty);
464 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000465 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000466 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000467 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000468 FieldOffset, 0, FieldTy);
469 EltTys.push_back(FieldTy);
470
471 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000472 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000473
474 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000475 Unit, LineNo, FieldOffset, 0, 0,
476 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000477
Mike Stump9bc093c2009-05-14 02:03:51 +0000478 BlockLiteralGenericSet = true;
479 BlockLiteralGeneric
480 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000481 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000482 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000483 return BlockLiteralGeneric;
484}
485
Chris Lattner9c85ba32008-11-10 06:08:34 +0000486llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000487 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000488 // Typedefs are derived from some other type. If we have a typedef of a
489 // typedef, make sure to emit the whole chain.
490 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000491
Chris Lattner9c85ba32008-11-10 06:08:34 +0000492 // We don't set size information, but do specify where the typedef was
493 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000494 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000495
Devang Pateleb6d79b2010-02-01 21:34:11 +0000496 llvm::DIDescriptor TyContext
497 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
498 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000499 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000500 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000501 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000502 Ty->getDecl()->getName(), Unit,
503 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000504 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000505}
506
Chris Lattner9c85ba32008-11-10 06:08:34 +0000507llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000508 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000509 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000510
Chris Lattner9c85ba32008-11-10 06:08:34 +0000511 // Add the result type at least.
512 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000513
Chris Lattner9c85ba32008-11-10 06:08:34 +0000514 // Set up remainder of arguments if there is a prototype.
515 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregor72564e72009-02-26 23:50:07 +0000516 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000517 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
518 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
519 } else {
520 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000521 }
522
Chris Lattner9c85ba32008-11-10 06:08:34 +0000523 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000524 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000525
Devang Patelca80a5f2009-10-20 19:55:01 +0000526 llvm::DIType DbgTy =
527 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000528 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000529 0, 0, 0, 0, 0,
530 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000531 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000532}
533
Devang Patel428deb52010-01-19 00:00:59 +0000534/// CollectRecordFields - A helper function to collect debug info for
535/// record fields. This is used while creating debug info entry for a Record.
536void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000537CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000538 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
539 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000540 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
541 for (RecordDecl::field_iterator I = RD->field_begin(),
542 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000543 I != E; ++I, ++FieldNo) {
544 FieldDecl *Field = *I;
545 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
546
547 llvm::StringRef FieldName = Field->getName();
548
Devang Patel4835fdd2010-02-12 01:31:06 +0000549 // Ignore unnamed fields. Do not ignore unnamed records.
550 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000551 continue;
552
553 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000554 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
555 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000556 QualType FType = Field->getType();
557 uint64_t FieldSize = 0;
558 unsigned FieldAlign = 0;
559 if (!FType->isIncompleteArrayType()) {
560
561 // Bit size, align and offset of the type.
562 FieldSize = CGM.getContext().getTypeSize(FType);
563 Expr *BitWidth = Field->getBitWidth();
564 if (BitWidth)
565 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
566
567 FieldAlign = CGM.getContext().getTypeAlign(FType);
568 }
569
570 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
571
Devang Patel71f4ff62010-04-21 23:12:37 +0000572 unsigned Flags = 0;
573 AccessSpecifier Access = I->getAccess();
574 if (Access == clang::AS_private)
575 Flags |= llvm::DIType::FlagPrivate;
576 else if (Access == clang::AS_protected)
577 Flags |= llvm::DIType::FlagProtected;
578
Devang Patel428deb52010-01-19 00:00:59 +0000579 // Create a DW_TAG_member node to remember the offset of this field in the
580 // struct. FIXME: This is an absolutely insane way to capture this
581 // information. When we gut debug info, this should be fixed.
582 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
583 FieldName, FieldDefUnit,
584 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000585 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000586 EltTys.push_back(FieldTy);
587 }
588}
589
Devang Patela6da1922010-01-28 00:28:01 +0000590/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
591/// function type is not updated to include implicit "this" pointer. Use this
592/// routine to get a method type which includes "this" pointer.
593llvm::DIType
594CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000595 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000596 llvm::DIType FnTy
597 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
598 0),
599 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000600
601 // Static methods do not need "this" pointer argument.
602 if (Method->isStatic())
603 return FnTy;
604
Devang Patela6da1922010-01-28 00:28:01 +0000605 // Add "this" pointer.
606
Devang Patelab699792010-05-07 18:12:35 +0000607 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000608 assert (Args.getNumElements() && "Invalid number of arguments!");
609
610 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
611
612 // First element is always return type. For 'void' functions it is NULL.
613 Elts.push_back(Args.getElement(0));
614
615 // "this" pointer is always first argument.
616 ASTContext &Context = CGM.getContext();
617 QualType ThisPtr =
618 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
Devang Patel337472d2010-02-09 17:57:50 +0000619 llvm::DIType ThisPtrType =
620 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000621
Devang Patelab699792010-05-07 18:12:35 +0000622 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
Devang Patel337472d2010-02-09 17:57:50 +0000623 Elts.push_back(ThisPtrType);
Devang Patela6da1922010-01-28 00:28:01 +0000624
625 // Copy rest of the arguments.
626 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
627 Elts.push_back(Args.getElement(i));
628
629 llvm::DIArray EltTypeArray =
630 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
631
632 return
633 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000634 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000635 0, 0, 0, 0, 0,
636 llvm::DIType(), EltTypeArray);
637}
638
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000639/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
640/// a single member function GlobalDecl.
641llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000642CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000643 llvm::DIFile Unit,
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000644 llvm::DICompositeType &RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000645 bool IsCtorOrDtor =
646 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
647
648 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000649 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000650
651 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
652 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000653 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000654 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000655 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000656
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000657 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000658 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
659 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000660
661 // Collect virtual method info.
662 llvm::DIType ContainingType;
663 unsigned Virtuality = 0;
664 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000665
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000666 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000667 if (Method->isPure())
668 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
669 else
670 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
671
672 // It doesn't make sense to give a virtual destructor a vtable index,
673 // since a single destructor has two entries in the vtable.
674 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000675 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000676 ContainingType = RecordTy;
677 }
678
679 llvm::DISubprogram SP =
680 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
681 MethodLinkageName,
682 MethodDefUnit, MethodLine,
683 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000684 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000685 Virtuality, VIndex, ContainingType,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000686 Method->isImplicit(),
687 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000688
689 // Don't cache ctors or dtors since we have to emit multiple functions for
690 // a single ctor or dtor.
691 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000692 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000693
694 return SP;
695}
696
Devang Patel4125fd22010-01-19 01:54:44 +0000697/// CollectCXXMemberFunctions - A helper function to collect debug info for
698/// C++ member functions.This is used while creating debug info entry for
699/// a Record.
700void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000701CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000702 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
703 llvm::DICompositeType &RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000704 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
705 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000706 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000707
Devang Pateld5322da2010-02-09 19:09:28 +0000708 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000709 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000710
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000711 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000712 }
713}
714
Devang Patela245c5b2010-01-25 23:32:18 +0000715/// CollectCXXBases - A helper function to collect debug info for
716/// C++ base classes. This is used while creating debug info entry for
717/// a Record.
718void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000719CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000720 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
721 llvm::DICompositeType &RecordTy) {
722
Devang Patel239cec62010-02-01 21:39:52 +0000723 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
724 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
725 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000726 unsigned BFlags = 0;
727 uint64_t BaseOffset;
728
729 const CXXRecordDecl *Base =
730 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
731
732 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000733 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000734 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000735 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patelca7daed2010-01-28 21:54:15 +0000736 BFlags = llvm::DIType::FlagVirtual;
737 } else
738 BaseOffset = RL.getBaseClassOffset(Base);
739
740 AccessSpecifier Access = BI->getAccessSpecifier();
741 if (Access == clang::AS_private)
742 BFlags |= llvm::DIType::FlagPrivate;
743 else if (Access == clang::AS_protected)
744 BFlags |= llvm::DIType::FlagProtected;
745
746 llvm::DIType DTy =
747 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
748 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000749 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000750 BaseOffset, BFlags,
751 getOrCreateType(BI->getType(),
752 Unit));
753 EltTys.push_back(DTy);
754 }
Devang Patela245c5b2010-01-25 23:32:18 +0000755}
756
Devang Patel4ce3f202010-01-28 18:11:52 +0000757/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000758llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000759 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000760 return VTablePtrType;
761
762 ASTContext &Context = CGM.getContext();
763
764 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000765 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
766 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000767 llvm::DIType SubTy =
768 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000769 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000770 0, 0, 0, 0, 0, llvm::DIType(), SElements);
771
772 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
773 llvm::DIType vtbl_ptr_type
774 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000775 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000776 0, Size, 0, 0, 0, SubTy);
777
Devang Pateld58562e2010-03-09 22:49:11 +0000778 VTablePtrType =
779 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
780 Unit, "", Unit,
781 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000782 return VTablePtrType;
783}
784
Anders Carlsson046c2942010-04-17 20:15:18 +0000785/// getVTableName - Get vtable name for the given Class.
786llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000787 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000788 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000789
790 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000791 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000792 memcpy(StrPtr, Name.data(), Name.length());
793 return llvm::StringRef(StrPtr, Name.length());
794}
795
796
Anders Carlsson046c2942010-04-17 20:15:18 +0000797/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000798/// debug info entry in EltTys vector.
799void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000800CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000801 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000802 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000803
804 // If there is a primary base then it will hold vtable info.
805 if (RL.getPrimaryBase())
806 return;
807
808 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000809 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000810 return;
811
812 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
813 llvm::DIType VPTR
814 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000815 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000816 0, Size, 0, 0, 0,
817 getOrCreateVTablePtrType(Unit));
818 EltTys.push_back(VPTR);
819}
820
Devang Patel65e99f22009-02-25 01:36:11 +0000821/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000822llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000823 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000824 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000825
Chris Lattner9c85ba32008-11-10 06:08:34 +0000826 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000827 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000828 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000829 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000830 Tag = llvm::dwarf::DW_TAG_union_type;
831 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000832 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000833 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000834 }
835
Chris Lattner9c85ba32008-11-10 06:08:34 +0000836 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000837 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
838 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000839
Chris Lattner9c85ba32008-11-10 06:08:34 +0000840 // Records and classes and unions can all be recursive. To handle them, we
841 // first generate a debug descriptor for the struct as a forward declaration.
842 // Then (if it is a definition) we go through and get debug info for all of
843 // its members. Finally, we create a descriptor for the complete type (which
844 // may refer to the forward decl if the struct is recursive) and replace all
845 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000846 llvm::DIDescriptor FDContext =
847 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
848
849 // If this is just a forward declaration, construct an appropriately
850 // marked node and just return it.
851 if (!RD->getDefinition()) {
852 llvm::DICompositeType FwdDecl =
853 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
854 DefUnit, Line, 0, 0, 0,
855 llvm::DIType::FlagFwdDecl,
856 llvm::DIType(), llvm::DIArray());
857
858 return FwdDecl;
859 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000860
Devang Pateld6c5a262010-02-01 21:52:22 +0000861 // A RD->getName() is not unique. However, the debug info descriptors
Devang Patelce78c972010-02-01 22:51:29 +0000862 // are uniqued so use type name to ensure uniquness.
Benjamin Kramerfea3d4d2010-03-13 12:06:51 +0000863 llvm::SmallString<128> FwdDeclName;
864 llvm::raw_svector_ostream(FwdDeclName) << "fwd.type." << FwdDeclCount++;
Devang Patel0ce73f62009-07-22 18:57:00 +0000865 llvm::DICompositeType FwdDecl =
Devang Patel7573f8b2010-03-09 21:32:27 +0000866 DebugFactory.CreateCompositeType(Tag, FDContext, FwdDeclName,
Devang Patelab71ff52009-11-12 00:51:46 +0000867 DefUnit, Line, 0, 0, 0, 0,
Chris Lattner9c85ba32008-11-10 06:08:34 +0000868 llvm::DIType(), llvm::DIArray());
Mike Stump1eb44332009-09-09 15:08:12 +0000869
Devang Patelab699792010-05-07 18:12:35 +0000870 llvm::MDNode *MN = FwdDecl;
871 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000872 // Otherwise, insert it into the TypeCache so that recursive uses will find
873 // it.
Devang Patelab699792010-05-07 18:12:35 +0000874 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000875 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000876 RegionStack.push_back(FwdDeclNode);
877 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000878
879 // Convert all the elements.
880 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
881
Devang Pateld6c5a262010-02-01 21:52:22 +0000882 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000883 if (CXXDecl) {
884 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000885 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000886 }
Devang Pateld6c5a262010-02-01 21:52:22 +0000887 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +0000888 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +0000889 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +0000890 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +0000891
892 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +0000893 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +0000894 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
895 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +0000896 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +0000897 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +0000898 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +0000899 }
Mike Stump1eb44332009-09-09 15:08:12 +0000900
Chris Lattner9c85ba32008-11-10 06:08:34 +0000901 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000902 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +0000903
904 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000905 uint64_t Size = CGM.getContext().getTypeSize(Ty);
906 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Devang Patele4c1ea02010-03-11 20:01:48 +0000908 RegionStack.pop_back();
909 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
910 RegionMap.find(Ty->getDecl());
911 if (RI != RegionMap.end())
912 RegionMap.erase(RI);
913
Devang Patel411894b2010-02-01 22:40:08 +0000914 llvm::DIDescriptor RDContext =
915 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +0000916
917 llvm::StringRef RDName = RD->getName();
918 // If this is a class, include the template arguments also.
919 if (Tag == llvm::dwarf::DW_TAG_class_type)
920 RDName = getClassName(RD);
921
Devang Patel0ce73f62009-07-22 18:57:00 +0000922 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +0000923 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +0000924 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +0000925 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +0000926 llvm::DIType(), Elements,
927 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000928
929 // Now that we have a real decl for the struct, replace anything using the
930 // old decl with the new one. This will recursively update the debug info.
Eli Friedman14d63652009-11-16 21:04:30 +0000931 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +0000932 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000933 return RealDecl;
934}
935
John McCallc12c5bb2010-05-15 11:32:37 +0000936/// CreateType - get objective-c object type.
937llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
938 llvm::DIFile Unit) {
939 // Ignore protocols.
940 return getOrCreateType(Ty->getBaseType(), Unit);
941}
942
Devang Patel9ca36b62009-02-26 21:10:26 +0000943/// CreateType - get objective-c interface type.
944llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000945 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000946 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +0000947 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +0000948
949 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +0000950 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +0000951 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +0000952 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +0000953
Devang Patel9ca36b62009-02-26 21:10:26 +0000954 // To handle recursive interface, we
955 // first generate a debug descriptor for the struct as a forward declaration.
956 // Then (if it is a definition) we go through and get debug info for all of
957 // its members. Finally, we create a descriptor for the complete type (which
958 // may refer to the forward decl if the struct is recursive) and replace all
959 // uses of the forward declaration with the final definition.
Devang Patel6c1fddf2009-07-27 18:42:03 +0000960 llvm::DICompositeType FwdDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +0000961 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
Devang Patelab71ff52009-11-12 00:51:46 +0000962 DefUnit, Line, 0, 0, 0, 0,
Chris Lattnerac7c8142009-05-02 01:13:16 +0000963 llvm::DIType(), llvm::DIArray(),
964 RuntimeLang);
Mike Stump1eb44332009-09-09 15:08:12 +0000965
Devang Patel9ca36b62009-02-26 21:10:26 +0000966 // If this is just a forward declaration, return it.
Devang Pateld6c5a262010-02-01 21:52:22 +0000967 if (ID->isForwardDecl())
Devang Patel9ca36b62009-02-26 21:10:26 +0000968 return FwdDecl;
969
Devang Patelab699792010-05-07 18:12:35 +0000970 llvm::MDNode *MN = FwdDecl;
971 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +0000972 // Otherwise, insert it into the TypeCache so that recursive uses will find
973 // it.
Devang Patelab699792010-05-07 18:12:35 +0000974 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000975 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000976 RegionStack.push_back(FwdDeclNode);
977 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +0000978
979 // Convert all the elements.
980 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
981
Devang Pateld6c5a262010-02-01 21:52:22 +0000982 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +0000983 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +0000984 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000985 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000986 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +0000987 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +0000988 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +0000989 0 /* offset */, 0, SClassTy);
990 EltTys.push_back(InhTag);
991 }
992
Devang Pateld6c5a262010-02-01 21:52:22 +0000993 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +0000994
995 unsigned FieldNo = 0;
Devang Pateld6c5a262010-02-01 21:52:22 +0000996 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
997 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +0000998 ObjCIvarDecl *Field = *I;
999 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1000
Devang Patel73621622009-11-25 17:37:31 +00001001 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001002
Devang Patelde135022009-04-27 22:40:36 +00001003 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001004 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001005 continue;
1006
Devang Patel9ca36b62009-02-26 21:10:26 +00001007 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001008 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1009 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001010 QualType FType = Field->getType();
1011 uint64_t FieldSize = 0;
1012 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001013
Devang Patel99c20eb2009-03-20 18:24:39 +00001014 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001015
Devang Patel99c20eb2009-03-20 18:24:39 +00001016 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001017 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001018 Expr *BitWidth = Field->getBitWidth();
1019 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001020 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001021
Anders Carlsson20f12a22009-12-06 18:00:51 +00001022 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001023 }
1024
Mike Stump1eb44332009-09-09 15:08:12 +00001025 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1026
Devang Patelc20482b2009-03-19 00:23:53 +00001027 unsigned Flags = 0;
1028 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1029 Flags = llvm::DIType::FlagProtected;
1030 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1031 Flags = llvm::DIType::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001032
Devang Patel9ca36b62009-02-26 21:10:26 +00001033 // Create a DW_TAG_member node to remember the offset of this field in the
1034 // struct. FIXME: This is an absolutely insane way to capture this
1035 // information. When we gut debug info, this should be fixed.
1036 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1037 FieldName, FieldDefUnit,
1038 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001039 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001040 EltTys.push_back(FieldTy);
1041 }
Mike Stump1eb44332009-09-09 15:08:12 +00001042
Devang Patel9ca36b62009-02-26 21:10:26 +00001043 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001044 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001045
Devang Patele4c1ea02010-03-11 20:01:48 +00001046 RegionStack.pop_back();
1047 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1048 RegionMap.find(Ty->getDecl());
1049 if (RI != RegionMap.end())
1050 RegionMap.erase(RI);
1051
Devang Patel9ca36b62009-02-26 21:10:26 +00001052 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001053 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1054 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Devang Patel6c1fddf2009-07-27 18:42:03 +00001056 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001057 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001058 Line, Size, Align, 0, 0, llvm::DIType(),
1059 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001060
1061 // Now that we have a real decl for the struct, replace anything using the
1062 // old decl with the new one. This will recursively update the debug info.
Devang Patelffffb032009-11-16 20:09:38 +00001063 llvm::DIDerivedType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001064 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001065
Devang Patel9ca36b62009-02-26 21:10:26 +00001066 return RealDecl;
1067}
1068
Chris Lattner9c85ba32008-11-10 06:08:34 +00001069llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001070 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001071 EnumDecl *ED = Ty->getDecl();
Chris Lattner9c85ba32008-11-10 06:08:34 +00001072
1073 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1074
1075 // Create DIEnumerator elements for each enumerator.
Mike Stump1eb44332009-09-09 15:08:12 +00001076 for (EnumDecl::enumerator_iterator
Devang Pateld6c5a262010-02-01 21:52:22 +00001077 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
Douglas Gregor44b43212008-12-11 16:49:14 +00001078 Enum != EnumEnd; ++Enum) {
Devang Patel73621622009-11-25 17:37:31 +00001079 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
Douglas Gregor44b43212008-12-11 16:49:14 +00001080 Enum->getInitVal().getZExtValue()));
Chris Lattner9c85ba32008-11-10 06:08:34 +00001081 }
Mike Stump1eb44332009-09-09 15:08:12 +00001082
Chris Lattner9c85ba32008-11-10 06:08:34 +00001083 // Return a CompositeType for the enum itself.
1084 llvm::DIArray EltArray =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001085 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001086
Devang Patel8ab870d2010-05-12 23:46:38 +00001087 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1088 unsigned Line = getLineNumber(ED->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001089
Chris Lattner9c85ba32008-11-10 06:08:34 +00001090 // Size and align of the type.
Eli Friedman3189e4b2009-05-04 04:39:55 +00001091 uint64_t Size = 0;
1092 unsigned Align = 0;
1093 if (!Ty->isIncompleteType()) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001094 Size = CGM.getContext().getTypeSize(Ty);
1095 Align = CGM.getContext().getTypeAlign(Ty);
Eli Friedman3189e4b2009-05-04 04:39:55 +00001096 }
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Devang Patelca80a5f2009-10-20 19:55:01 +00001098 llvm::DIType DbgTy =
1099 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
Devang Pateld6c5a262010-02-01 21:52:22 +00001100 Unit, ED->getName(), DefUnit, Line,
Devang Patelca80a5f2009-10-20 19:55:01 +00001101 Size, Align, 0, 0,
1102 llvm::DIType(), EltArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001103 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001104}
1105
1106llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001107 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001108 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1109 return CreateType(RT, Unit);
1110 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1111 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001112
Chris Lattner9c85ba32008-11-10 06:08:34 +00001113 return llvm::DIType();
1114}
1115
Devang Patel70c23cd2010-02-23 22:59:39 +00001116llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001117 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001118 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1119 uint64_t NumElems = Ty->getNumElements();
1120 if (NumElems > 0)
1121 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001122
Benjamin Kramerad468862010-03-30 11:36:44 +00001123 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1124 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001125
1126 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1127 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1128
1129 return
1130 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001131 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001132 0, Size, Align, 0, 0,
1133 ElementTy, SubscriptArray);
1134}
1135
Chris Lattner9c85ba32008-11-10 06:08:34 +00001136llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001137 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001138 uint64_t Size;
1139 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001140
1141
Nuno Lopes010d5142009-01-28 00:35:17 +00001142 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001143 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001144 Size = 0;
1145 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001146 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001147 } else if (Ty->isIncompleteArrayType()) {
1148 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001149 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001150 } else {
1151 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001152 Size = CGM.getContext().getTypeSize(Ty);
1153 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001154 }
Mike Stump1eb44332009-09-09 15:08:12 +00001155
Chris Lattner9c85ba32008-11-10 06:08:34 +00001156 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1157 // interior arrays, do we care? Why aren't nested arrays represented the
1158 // obvious/recursive way?
1159 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1160 QualType EltTy(Ty, 0);
1161 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta507de852008-06-09 10:47:41 +00001162 uint64_t Upper = 0;
Mike Stump1eb44332009-09-09 15:08:12 +00001163 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Patel5a6bfe32009-08-14 20:57:45 +00001164 if (CAT->getSize().getZExtValue())
Mike Stump1eb44332009-09-09 15:08:12 +00001165 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001166 // FIXME: Verify this is right for VLAs.
1167 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1168 EltTy = Ty->getElementType();
Sanjiv Gupta507de852008-06-09 10:47:41 +00001169 }
Mike Stump1eb44332009-09-09 15:08:12 +00001170
Chris Lattner9c85ba32008-11-10 06:08:34 +00001171 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001172 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001173
Devang Patelca80a5f2009-10-20 19:55:01 +00001174 llvm::DIType DbgTy =
1175 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001176 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001177 0, Size, Align, 0, 0,
1178 getOrCreateType(EltTy, Unit),
1179 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001180 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001181}
1182
Anders Carlssona031b352009-11-06 19:19:55 +00001183llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001184 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001185 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1186 Ty, Ty->getPointeeType(), Unit);
1187}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001188
Anders Carlsson20f12a22009-12-06 18:00:51 +00001189llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001190 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001191 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1192 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1193
1194 if (!Ty->getPointeeType()->isFunctionType()) {
1195 // We have a data member pointer type.
1196 return PointerDiffDITy;
1197 }
1198
1199 // We have a member function pointer type. Treat it as a struct with two
1200 // ptrdiff_t members.
1201 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1202
1203 uint64_t FieldOffset = 0;
1204 llvm::DIDescriptor ElementTypes[2];
1205
1206 // FIXME: This should probably be a function type instead.
1207 ElementTypes[0] =
1208 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001209 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001210 Info.first, Info.second, FieldOffset, 0,
1211 PointerDiffDITy);
1212 FieldOffset += Info.first;
1213
1214 ElementTypes[1] =
1215 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001216 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001217 Info.first, Info.second, FieldOffset, 0,
1218 PointerDiffDITy);
1219
1220 llvm::DIArray Elements =
1221 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1222 llvm::array_lengthof(ElementTypes));
1223
1224 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1225 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001226 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001227 0, 0, 0, llvm::DIType(), Elements);
1228}
1229
Douglas Gregor840943d2009-12-21 20:18:30 +00001230static QualType UnwrapTypeForDebugInfo(QualType T) {
1231 do {
1232 QualType LastT = T;
1233 switch (T->getTypeClass()) {
1234 default:
1235 return T;
1236 case Type::TemplateSpecialization:
1237 T = cast<TemplateSpecializationType>(T)->desugar();
1238 break;
1239 case Type::TypeOfExpr: {
1240 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1241 T = Ty->getUnderlyingExpr()->getType();
1242 break;
1243 }
1244 case Type::TypeOf:
1245 T = cast<TypeOfType>(T)->getUnderlyingType();
1246 break;
1247 case Type::Decltype:
1248 T = cast<DecltypeType>(T)->getUnderlyingType();
1249 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001250 case Type::Elaborated:
1251 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001252 break;
1253 case Type::SubstTemplateTypeParm:
1254 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1255 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001256 }
1257
1258 assert(T != LastT && "Type unwrapping failed to unwrap!");
1259 if (T == LastT)
1260 return T;
1261 } while (true);
1262
1263 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001264}
1265
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001266/// getOrCreateType - Get the type from the cache or create a new
1267/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001268llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001269 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001270 if (Ty.isNull())
1271 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Douglas Gregor840943d2009-12-21 20:18:30 +00001273 // Unwrap the type as needed for debug information.
1274 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001275
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001276 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001277 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001278 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001279 if (it != TypeCache.end()) {
1280 // Verify that the debug info still exists.
1281 if (&*it->second)
1282 return llvm::DIType(cast<llvm::MDNode>(it->second));
1283 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001284
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001285 // Otherwise create the type.
1286 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001287
1288 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001289 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001290 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001291}
1292
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001293/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001294llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001295 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001296 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001297 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001298 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001299
Douglas Gregor2101a822009-12-21 19:57:21 +00001300 const char *Diag = 0;
1301
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001302 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001303 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001304#define TYPE(Class, Base)
1305#define ABSTRACT_TYPE(Class, Base)
1306#define NON_CANONICAL_TYPE(Class, Base)
1307#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1308#include "clang/AST/TypeNodes.def"
1309 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001310
Anders Carlssonbfe69952009-11-06 18:24:04 +00001311 // FIXME: Handle these.
1312 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001313 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001314
1315 case Type::Vector:
1316 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001317 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001318 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001319 case Type::ObjCObject:
1320 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001321 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001322 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1323 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1324 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1325 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001326 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001327 return CreateType(cast<BlockPointerType>(Ty), Unit);
1328 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001329 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001330 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001331 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001332 case Type::FunctionProto:
1333 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001334 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001335 case Type::ConstantArray:
1336 case Type::VariableArray:
1337 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001338 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001339
1340 case Type::LValueReference:
1341 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1342
Anders Carlsson20f12a22009-12-06 18:00:51 +00001343 case Type::MemberPointer:
1344 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001345
1346 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001347 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001348 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001349 case Type::TypeOfExpr:
1350 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001351 case Type::Decltype:
1352 llvm_unreachable("type should have been unwrapped!");
1353 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001354
1355 case Type::RValueReference:
1356 // FIXME: Implement!
1357 Diag = "rvalue references";
1358 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001359 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001360
1361 assert(Diag && "Fall through without a diagnostic?");
1362 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1363 "debug information for %0 is not yet supported");
1364 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1365 << Diag;
1366 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001367}
1368
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001369/// CreateMemberType - Create new member and increase Offset by FType's size.
1370llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1371 llvm::StringRef Name,
1372 uint64_t *Offset) {
1373 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1374 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1375 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1376 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1377 Unit, Name, Unit, 0,
1378 FieldSize, FieldAlign,
1379 *Offset, 0, FieldTy);
1380 *Offset += FieldSize;
1381 return Ty;
1382}
1383
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001384/// EmitFunctionStart - Constructs the debug code for entering a function -
1385/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001386void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001387 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001388 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001389
Devang Patel9c6c3a02010-01-14 00:36:21 +00001390 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001391 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001392
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001393 FnBeginRegionCount.push_back(RegionStack.size());
1394
Devang Patel9c6c3a02010-01-14 00:36:21 +00001395 const Decl *D = GD.getDecl();
1396 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001397 // If there is a DISubprogram for this function available then use it.
1398 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1399 FI = SPCache.find(FD);
1400 if (FI != SPCache.end()) {
Devang Patel0804e6e2010-03-08 20:53:17 +00001401 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001402 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1403 llvm::MDNode *SPN = SP;
1404 RegionStack.push_back(SPN);
1405 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001406 return;
1407 }
1408 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001409 Name = getFunctionName(FD);
1410 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001411 LinkageName = CGM.getMangledName(GD);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001412 } else {
1413 // Use llvm function name as linkage name.
1414 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001415 LinkageName = Name;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001416 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001417 if (!Name.empty() && Name[0] == '\01')
1418 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001419
Devang Patel970c6182010-04-24 00:49:16 +00001420 // It is expected that CurLoc is set before using EmitFunctionStart.
1421 // Usually, CurLoc points to the left bracket location of compound
1422 // statement representing function body.
1423 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patel8ab870d2010-05-12 23:46:38 +00001424 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump1eb44332009-09-09 15:08:12 +00001425
Chris Lattner9c85ba32008-11-10 06:08:34 +00001426 llvm::DISubprogram SP =
Devang Patel970c6182010-04-24 00:49:16 +00001427 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001428 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001429 Fn->hasInternalLinkage(), true/*definition*/,
1430 0, 0, llvm::DIType(),
1431 D->isImplicit(),
1432 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001433
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001434 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001435 llvm::MDNode *SPN = SP;
1436 RegionStack.push_back(SPN);
1437 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001438
1439 // Clear stack used to keep track of #line directives.
1440 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001441}
1442
1443
Devang Patel4d939e62010-07-20 22:20:10 +00001444void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001445 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001446
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001447 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001448 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001449 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001450 || (SM.getInstantiationLineNumber(CurLoc) ==
1451 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001452 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001453 // New Builder may not be in sync with CGDebugInfo.
1454 if (!Builder.getCurrentDebugLocation().isUnknown())
1455 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001456
1457 // Update last state.
1458 PrevLoc = CurLoc;
1459
Chris Lattnerc6034632010-04-01 06:31:43 +00001460 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001461 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1462 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001463 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001464}
1465
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001466/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1467/// has introduced scope change.
1468void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1469 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1470 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1471 return;
1472 SourceManager &SM = CGM.getContext().getSourceManager();
1473 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1474 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1475
1476 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1477 return;
1478
1479 // If #line directive stack is empty then we are entering a new scope.
1480 if (LineDirectiveFiles.empty()) {
1481 EmitRegionStart(Builder);
1482 LineDirectiveFiles.push_back(PCLoc.getFilename());
1483 return;
1484 }
1485
1486 assert (RegionStack.size() >= LineDirectiveFiles.size()
1487 && "error handling #line regions!");
1488
1489 bool SeenThisFile = false;
1490 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1491 E = LineDirectiveFiles.end(); I != E; ++I)
1492 if (!strcmp(PPLoc.getFilename(), *I)) {
1493 SeenThisFile = true;
1494 break;
1495 }
1496
1497 // If #line for this file is seen earlier then pop out #line regions.
1498 if (SeenThisFile) {
1499 while (!LineDirectiveFiles.empty()) {
1500 const char *LastFile = LineDirectiveFiles.back();
1501 RegionStack.pop_back();
1502 LineDirectiveFiles.pop_back();
1503 if (!strcmp(PPLoc.getFilename(), LastFile))
1504 break;
1505 }
1506 return;
1507 }
1508
1509 // .. otherwise insert new #line region.
1510 EmitRegionStart(Builder);
1511 LineDirectiveFiles.push_back(PCLoc.getFilename());
1512
1513 return;
1514}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001515/// EmitRegionStart- Constructs the debug code for entering a declarative
1516/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001517void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001518 llvm::DIDescriptor D =
1519 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1520 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001521 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001522 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001523 getLineNumber(CurLoc),
1524 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001525 llvm::MDNode *DN = D;
1526 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001527}
1528
1529/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1530/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001531void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001532 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1533
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001534 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001535 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001536
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001537 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001538}
1539
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001540/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1541void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1542 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1543 unsigned RCount = FnBeginRegionCount.back();
1544 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1545
1546 // Pop all regions for this function.
1547 while (RegionStack.size() != RCount)
1548 EmitRegionEnd(Builder);
1549 FnBeginRegionCount.pop_back();
1550}
1551
Devang Patel809b9bb2010-02-10 18:49:08 +00001552// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1553// See BuildByRefType.
1554llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1555 uint64_t *XOffset) {
1556
1557 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1558
1559 QualType FType;
1560 uint64_t FieldSize, FieldOffset;
1561 unsigned FieldAlign;
1562
Devang Patel17800552010-03-09 00:44:50 +00001563 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001564 QualType Type = VD->getType();
1565
1566 FieldOffset = 0;
1567 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001568 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1569 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001570 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001571 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1572 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1573
Devang Patel809b9bb2010-02-10 18:49:08 +00001574 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1575 if (HasCopyAndDispose) {
1576 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001577 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1578 &FieldOffset));
1579 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1580 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001581 }
1582
1583 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1584 if (Align > CharUnits::fromQuantity(
1585 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1586 unsigned AlignedOffsetInBytes
1587 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1588 unsigned NumPaddingBytes
1589 = AlignedOffsetInBytes - FieldOffset/8;
1590
1591 if (NumPaddingBytes > 0) {
1592 llvm::APInt pad(32, NumPaddingBytes);
1593 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1594 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001595 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001596 }
1597 }
1598
1599 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001600 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001601 FieldSize = CGM.getContext().getTypeSize(FType);
1602 FieldAlign = Align.getQuantity()*8;
1603
1604 *XOffset = FieldOffset;
1605 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001606 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001607 0, FieldSize, FieldAlign,
1608 FieldOffset, 0, FieldTy);
1609 EltTys.push_back(FieldTy);
1610 FieldOffset += FieldSize;
1611
1612 llvm::DIArray Elements =
1613 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1614
1615 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1616
1617 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001618 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001619 0, FieldOffset, 0, 0, Flags,
1620 llvm::DIType(), Elements);
1621
1622}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001623/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001624void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001625 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001626 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1627
Devang Patel17800552010-03-09 00:44:50 +00001628 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001629 llvm::DIType Ty;
1630 uint64_t XOffset = 0;
1631 if (VD->hasAttr<BlocksAttr>())
1632 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1633 else
1634 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001635
Devang Patelf4e54a22010-05-07 23:05:55 +00001636 // If there is not any debug info for type then do not emit debug info
1637 // for this variable.
1638 if (!Ty)
1639 return;
1640
Chris Lattner9c85ba32008-11-10 06:08:34 +00001641 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001642 unsigned Line = getLineNumber(VD->getLocation());
1643 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +00001644
Chris Lattner9c85ba32008-11-10 06:08:34 +00001645 // Create the descriptor for the variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001646 llvm::DIVariable D =
Devang Patel8fae0602009-11-13 19:10:24 +00001647 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel239cec62010-02-01 21:39:52 +00001648 VD->getName(),
Devang Patel44eeeba2010-06-05 01:14:40 +00001649 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001650 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001651 llvm::Instruction *Call =
Devang Patela0203802009-11-10 23:07:24 +00001652 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel23908b82009-11-12 18:21:39 +00001653
Chris Lattnerc6034632010-04-01 06:31:43 +00001654 llvm::MDNode *Scope = RegionStack.back();
Chris Lattnere541d012010-04-02 20:21:43 +00001655 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001656}
1657
Mike Stumpb1a6e682009-09-30 02:43:10 +00001658/// EmitDeclare - Emit local variable declaration debug info.
1659void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1660 llvm::Value *Storage, CGBuilderTy &Builder,
1661 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001662 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001663 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1664
Devang Patel2b594b92010-04-26 23:28:46 +00001665 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001666 return;
1667
1668 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001669 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001670 llvm::DIType Ty;
1671 if (VD->hasAttr<BlocksAttr>())
1672 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1673 else
1674 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001675
1676 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001677 unsigned Line = getLineNumber(VD->getLocation());
1678 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001679
Devang Pateld6c5a262010-02-01 21:52:22 +00001680 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001681 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001682 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1683 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1684 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1685 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001686 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001687 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1688 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001689 // offset of __forwarding field
1690 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001691 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1692 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1693 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001694 // offset of x field
1695 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001696 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001697 }
1698
1699 // Create the descriptor for the variable.
1700 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001701 DebugFactory.CreateComplexVariable(Tag,
1702 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001703 VD->getName(), Unit, Line, Ty,
Mike Stumpb1a6e682009-09-30 02:43:10 +00001704 addr);
1705 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001706 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001707 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001708
Chris Lattnerc6034632010-04-01 06:31:43 +00001709 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001710 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001711}
1712
Devang Pateld6c5a262010-02-01 21:52:22 +00001713void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001714 llvm::Value *Storage,
1715 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001716 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001717}
1718
Mike Stumpb1a6e682009-09-30 02:43:10 +00001719void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1720 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1721 CodeGenFunction *CGF) {
1722 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1723}
1724
Chris Lattner9c85ba32008-11-10 06:08:34 +00001725/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1726/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001727void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001728 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001729 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001730}
1731
1732
1733
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001734/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001735void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001736 const VarDecl *D) {
1737
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001738 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001739 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001740 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001741
Devang Pateleb6d79b2010-02-01 21:34:11 +00001742 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001743 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001744
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001745 // CodeGen turns int[] into int[1] so we'll do the same here.
1746 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001747
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001748 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001749 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001750
Anders Carlsson20f12a22009-12-06 18:00:51 +00001751 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001752 ArrayType::Normal, 0);
1753 }
Devang Patel5d822f02010-04-29 17:48:37 +00001754 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001755 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001756 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001757 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001758 llvm::DIDescriptor DContext =
1759 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001760 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1761 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001762 Var->hasInternalLinkage(),
1763 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001764}
1765
Devang Patel9ca36b62009-02-26 21:10:26 +00001766/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001767void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001768 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001769 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001770 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001771 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001772
Devang Pateld6c5a262010-02-01 21:52:22 +00001773 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001774
Devang Pateld6c5a262010-02-01 21:52:22 +00001775 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001776 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001777
Devang Patel9ca36b62009-02-26 21:10:26 +00001778 // CodeGen turns int[] into int[1] so we'll do the same here.
1779 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001780
Devang Patel9ca36b62009-02-26 21:10:26 +00001781 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001782 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001783
Anders Carlsson20f12a22009-12-06 18:00:51 +00001784 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001785 ArrayType::Normal, 0);
1786 }
1787
Devang Patelf6a39b72009-10-20 18:26:30 +00001788 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001789 getOrCreateType(T, Unit),
1790 Var->hasInternalLinkage(),
1791 true/*definition*/, Var);
1792}
Devang Patelabb485f2010-02-01 19:16:32 +00001793
1794/// getOrCreateNamesSpace - Return namespace descriptor for the given
1795/// namespace decl.
1796llvm::DINameSpace
1797CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1798 llvm::DIDescriptor Unit) {
1799 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1800 NameSpaceCache.find(NSDecl);
1801 if (I != NameSpaceCache.end())
1802 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1803
Devang Patel8ab870d2010-05-12 23:46:38 +00001804 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00001805
1806 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00001807 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00001808 llvm::DINameSpace NS =
1809 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patelab699792010-05-07 18:12:35 +00001810 llvm::DIFile(Unit), LineNo);
1811 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00001812 return NS;
1813}