blob: 404c010c7704314e18d873395943388d4cc4640f [file] [log] [blame]
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001//===--- CGDebugInfo.cpp - Emit Debug Information for a Module ------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This coordinates the debug information generation while generating code.
11//
12//===----------------------------------------------------------------------===//
13
14#include "CGDebugInfo.h"
Mike Stumpb1a6e682009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel2ed8f002010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patel9ca36b62009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel700a1cb2010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattner3cc5c402008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson19cc4ab2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stump5a862172009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth06057ce2010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000027#include "llvm/Constants.h"
28#include "llvm/DerivedTypes.h"
29#include "llvm/Instructions.h"
30#include "llvm/Intrinsics.h"
31#include "llvm/Module.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel446c6192009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson20f12a22009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel17800552010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman4cac5b42010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel17800552010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000044}
45
Chris Lattner9c85ba32008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbar66031a52008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +000048}
49
Chris Lattner9c85ba32008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson20f12a22009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +000053}
54
Devang Patel33583052010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Pateleb6d79b2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel33583052010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Pateleb6d79b2010-02-01 21:34:11 +000058 if (!Context)
59 return CompileUnit;
60
61 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator
62 I = RegionMap.find(Context);
63 if (I != RegionMap.end())
Gabor Greif38c9b172010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patel411894b2010-02-01 22:40:08 +000065
Devang Pateleb6d79b2010-02-01 21:34:11 +000066 // Check namespace.
67 if (const NamespaceDecl *NSDecl = dyn_cast<NamespaceDecl>(Context))
68 return llvm::DIDescriptor(getOrCreateNameSpace(NSDecl, CompileUnit));
Devang Patel8b90a782010-05-13 23:52:37 +000069
70 if (const RecordDecl *RDecl = dyn_cast<RecordDecl>(Context)) {
71 if (!RDecl->isDependentType()) {
72 llvm::DIType Ty = getOrCreateType(CGM.getContext().getTypeDeclType(RDecl),
73 llvm::DIFile(CompileUnit));
74 return llvm::DIDescriptor(Ty);
75 }
76 }
Devang Patel979ec2e2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel9c6c3a02010-01-14 00:36:21 +000080/// getFunctionName - Get function name for the given FunctionDecl. If the
81/// name is constructred on demand (e.g. C++ destructor) then the name
82/// is stored on the side.
83llvm::StringRef CGDebugInfo::getFunctionName(const FunctionDecl *FD) {
84 assert (FD && "Invalid FunctionDecl!");
85 IdentifierInfo *FII = FD->getIdentifier();
86 if (FII)
87 return FII->getName();
88
89 // Otherwise construct human readable name for debug info.
90 std::string NS = FD->getNameAsString();
91
92 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer1b627dc2010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel9c6c3a02010-01-14 00:36:21 +000096}
97
David Chisnall52044a22010-09-02 18:01:51 +000098llvm::StringRef CGDebugInfo::getObjCMethodName(const ObjCMethodDecl *OMD) {
99 llvm::SmallString<256> MethodName;
100 llvm::raw_svector_ostream OS(MethodName);
101 OS << (OMD->isInstanceMethod() ? '-' : '+') << '[';
102 const DeclContext *DC = OMD->getDeclContext();
103 if (const ObjCImplementationDecl *OID = dyn_cast<const ObjCImplementationDecl>(DC)) {
104 OS << OID->getName();
105 } else if (const ObjCCategoryImplDecl *OCD = dyn_cast<const ObjCCategoryImplDecl>(DC)){
106 OS << ((NamedDecl *)OCD)->getIdentifier()->getNameStart() << '(' <<
107 OCD->getIdentifier()->getNameStart() << ')';
108 }
109 OS << ' ' << OMD->getSelector().getAsString() << ']';
110
111 char *StrPtr = DebugInfoNames.Allocate<char>(OS.tell());
112 memcpy(StrPtr, MethodName.begin(), OS.tell());
113 return llvm::StringRef(StrPtr, OS.tell());
114}
115
Devang Patel700a1cb2010-07-20 20:24:18 +0000116/// getClassName - Get class name including template argument list.
117llvm::StringRef
118CGDebugInfo::getClassName(RecordDecl *RD) {
119 ClassTemplateSpecializationDecl *Spec
120 = dyn_cast<ClassTemplateSpecializationDecl>(RD);
121 if (!Spec)
122 return RD->getName();
123
124 const TemplateArgument *Args;
125 unsigned NumArgs;
126 std::string Buffer;
127 if (TypeSourceInfo *TAW = Spec->getTypeAsWritten()) {
128 const TemplateSpecializationType *TST =
129 cast<TemplateSpecializationType>(TAW->getType());
130 Args = TST->getArgs();
131 NumArgs = TST->getNumArgs();
132 } else {
133 const TemplateArgumentList &TemplateArgs = Spec->getTemplateArgs();
134 Args = TemplateArgs.getFlatArgumentList();
135 NumArgs = TemplateArgs.flat_size();
136 }
137 Buffer = RD->getIdentifier()->getNameStart();
138 PrintingPolicy Policy(CGM.getLangOptions());
139 Buffer += TemplateSpecializationType::PrintTemplateArgumentList(Args,
140 NumArgs,
141 Policy);
142
143 // Copy this name on the side and use its reference.
144 char *StrPtr = DebugInfoNames.Allocate<char>(Buffer.length());
145 memcpy(StrPtr, Buffer.data(), Buffer.length());
146 return llvm::StringRef(StrPtr, Buffer.length());
Devang Patel700a1cb2010-07-20 20:24:18 +0000147}
148
Devang Patel17800552010-03-09 00:44:50 +0000149/// getOrCreateFile - Get the file debug info descriptor for the input location.
150llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenek9c250392010-03-30 00:27:51 +0000151 if (!Loc.isValid())
Devang Patel17800552010-03-09 00:44:50 +0000152 // If Location is not valid then use main input file.
153 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
154 TheCU);
Anders Carlsson20f12a22009-12-06 18:00:51 +0000155 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel17800552010-03-09 00:44:50 +0000156 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenek9c250392010-03-30 00:27:51 +0000157
158 // Cache the results.
159 const char *fname = PLoc.getFilename();
160 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
161 DIFileCache.find(fname);
162
163 if (it != DIFileCache.end()) {
164 // Verify that the information still exists.
165 if (&*it->second)
166 return llvm::DIFile(cast<llvm::MDNode>(it->second));
167 }
168
Devang Patelac4d13c2010-07-27 15:17:16 +0000169 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
170 getCurrentDirname(), TheCU);
Ted Kremenek9c250392010-03-30 00:27:51 +0000171
Devang Patelab699792010-05-07 18:12:35 +0000172 DIFileCache[fname] = F;
Ted Kremenek9c250392010-03-30 00:27:51 +0000173 return F;
174
Devang Patel17800552010-03-09 00:44:50 +0000175}
Devang Patel8ab870d2010-05-12 23:46:38 +0000176
177/// getLineNumber - Get line number for the location. If location is invalid
178/// then use current location.
179unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
180 assert (CurLoc.isValid() && "Invalid current location!");
181 SourceManager &SM = CGM.getContext().getSourceManager();
182 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
183 return PLoc.getLine();
184}
185
186/// getColumnNumber - Get column number for the location. If location is
187/// invalid then use current location.
188unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
189 assert (CurLoc.isValid() && "Invalid current location!");
190 SourceManager &SM = CGM.getContext().getSourceManager();
191 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
192 return PLoc.getColumn();
193}
194
Devang Patelac4d13c2010-07-27 15:17:16 +0000195llvm::StringRef CGDebugInfo::getCurrentDirname() {
196 if (!CWDName.empty())
197 return CWDName;
198 char *CompDirnamePtr = NULL;
199 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
200 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
201 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
202 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
203}
204
Devang Patel17800552010-03-09 00:44:50 +0000205/// CreateCompileUnit - Create new compile unit.
206void CGDebugInfo::CreateCompileUnit() {
207
208 // Get absolute path name.
Douglas Gregorac91b4c2010-03-18 23:46:43 +0000209 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000210 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
211 if (MainFileName.empty())
Devang Patel22fe5852010-03-12 21:04:27 +0000212 MainFileName = "<unknown>";
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000213
Douglas Gregorf6728fc2010-03-22 21:28:29 +0000214 // The main file name provided via the "-main-file-name" option contains just
215 // the file name itself with no path information. This file name may have had
216 // a relative path, so we look into the actual file entry for the main
217 // file to determine the real absolute path for the file.
Devang Patel6e6bc392010-07-23 23:04:28 +0000218 std::string MainFileDir;
Devang Patelac4d13c2010-07-27 15:17:16 +0000219 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000220 MainFileDir = MainFile->getDir()->getName();
Devang Patelac4d13c2010-07-27 15:17:16 +0000221 if (MainFileDir != ".")
222 MainFileName = MainFileDir + "/" + MainFileName;
223 }
Douglas Gregorf7ad5002010-03-19 14:49:09 +0000224
Devang Patelac4d13c2010-07-27 15:17:16 +0000225 // Save filename string.
226 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
227 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
228 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
229
Chris Lattner515455a2009-03-25 03:28:08 +0000230 unsigned LangTag;
Devang Patel17800552010-03-09 00:44:50 +0000231 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner515455a2009-03-25 03:28:08 +0000232 if (LO.CPlusPlus) {
233 if (LO.ObjC1)
234 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
235 else
236 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
237 } else if (LO.ObjC1) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000238 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner515455a2009-03-25 03:28:08 +0000239 } else if (LO.C99) {
Devang Patel8d9aefc2009-03-24 20:35:51 +0000240 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner515455a2009-03-25 03:28:08 +0000241 } else {
242 LangTag = llvm::dwarf::DW_LANG_C89;
243 }
Devang Patel446c6192009-04-17 21:06:59 +0000244
Daniel Dunbar19f19832010-08-24 17:41:09 +0000245 std::string Producer = getClangFullVersion();
Chris Lattner4c2577a2009-05-02 01:00:04 +0000246
247 // Figure out which version of the ObjC runtime we have.
248 unsigned RuntimeVers = 0;
249 if (LO.ObjC1)
250 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000252 // Create new compile unit.
Devang Patel17800552010-03-09 00:44:50 +0000253 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel58115002010-07-27 20:49:59 +0000254 LangTag, Filename, getCurrentDirname(),
Devang Patelac4d13c2010-07-27 15:17:16 +0000255 Producer, true,
Daniel Dunbarf2d8b9f2009-12-18 02:43:17 +0000256 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000257}
258
Devang Patel65e99f22009-02-25 01:36:11 +0000259/// CreateType - Get the Basic type from the cache or create a new
Chris Lattner9c85ba32008-11-10 06:08:34 +0000260/// one if necessary.
261llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel17800552010-03-09 00:44:50 +0000262 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000263 unsigned Encoding = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000264 const char *BTName = NULL;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000265 switch (BT->getKind()) {
266 default:
267 case BuiltinType::Void:
268 return llvm::DIType();
Devang Patelc8972c62010-07-28 01:33:15 +0000269 case BuiltinType::ObjCClass:
Devang Pateleaf5ee92010-07-21 22:41:25 +0000270 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patelc8972c62010-07-28 01:33:15 +0000271 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000272 llvm::DIDescriptor::FlagFwdDecl,
Devang Pateleaf5ee92010-07-21 22:41:25 +0000273 llvm::DIType(), llvm::DIArray());
Devang Patelc8972c62010-07-28 01:33:15 +0000274 case BuiltinType::ObjCId: {
275 // typedef struct objc_class *Class;
276 // typedef struct objc_object {
277 // Class isa;
278 // } *id;
279
280 llvm::DIType OCTy =
281 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
282 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000283 llvm::DIDescriptor::FlagFwdDecl,
Devang Patelc8972c62010-07-28 01:33:15 +0000284 llvm::DIType(), llvm::DIArray());
285 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
286
287 llvm::DIType ISATy =
288 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
289 Unit, "", Unit,
290 0, Size, 0, 0, 0, OCTy);
291
292 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
293
294 llvm::DIType FieldTy =
295 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
296 "isa", Unit,
297 0,Size, 0, 0, 0, ISATy);
298 EltTys.push_back(FieldTy);
299 llvm::DIArray Elements =
300 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
301
302 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
303 Unit, "objc_object", Unit, 0, 0, 0, 0,
304 0,
305 llvm::DIType(), Elements);
306 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000307 case BuiltinType::UChar:
308 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
309 case BuiltinType::Char_S:
310 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
311 case BuiltinType::UShort:
312 case BuiltinType::UInt:
313 case BuiltinType::ULong:
314 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
315 case BuiltinType::Short:
316 case BuiltinType::Int:
317 case BuiltinType::Long:
318 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
319 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
320 case BuiltinType::Float:
Devang Patel7c173cb2009-10-12 22:28:31 +0000321 case BuiltinType::LongDouble:
Chris Lattner9c85ba32008-11-10 06:08:34 +0000322 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump1eb44332009-09-09 15:08:12 +0000323 }
Devang Patel05127ca2010-07-28 23:23:29 +0000324
325 switch (BT->getKind()) {
326 case BuiltinType::Long: BTName = "long int"; break;
327 case BuiltinType::LongLong: BTName = "long long int"; break;
328 case BuiltinType::ULong: BTName = "long unsigned int"; break;
329 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
330 default:
331 BTName = BT->getName(CGM.getContext().getLangOptions());
332 break;
333 }
Chris Lattner9c85ba32008-11-10 06:08:34 +0000334 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000335 uint64_t Size = CGM.getContext().getTypeSize(BT);
336 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000337 uint64_t Offset = 0;
Devang Patel05127ca2010-07-28 23:23:29 +0000338
Devang Patelca80a5f2009-10-20 19:55:01 +0000339 llvm::DIType DbgTy =
Devang Patel05127ca2010-07-28 23:23:29 +0000340 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patelca80a5f2009-10-20 19:55:01 +0000341 Unit, 0, Size, Align,
342 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000343 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000344}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +0000345
Chris Lattnerb7003772009-04-23 06:13:01 +0000346llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000347 llvm::DIFile Unit) {
Chris Lattnerb7003772009-04-23 06:13:01 +0000348 // Bit size, align and offset of the type.
349 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
350 if (Ty->isComplexIntegerType())
351 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump1eb44332009-09-09 15:08:12 +0000352
Anders Carlsson20f12a22009-12-06 18:00:51 +0000353 uint64_t Size = CGM.getContext().getTypeSize(Ty);
354 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattnerb7003772009-04-23 06:13:01 +0000355 uint64_t Offset = 0;
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Devang Patelca80a5f2009-10-20 19:55:01 +0000357 llvm::DIType DbgTy =
358 DebugFactory.CreateBasicType(Unit, "complex",
359 Unit, 0, Size, Align,
360 Offset, /*flags*/ 0, Encoding);
Devang Patelca80a5f2009-10-20 19:55:01 +0000361 return DbgTy;
Chris Lattnerb7003772009-04-23 06:13:01 +0000362}
363
John McCalla1805292009-09-25 01:40:47 +0000364/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000365/// a new one if necessary.
Devang Patel17800552010-03-09 00:44:50 +0000366llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +0000367 QualifierCollector Qc;
368 const Type *T = Qc.strip(Ty);
369
370 // Ignore these qualifiers for now.
371 Qc.removeObjCGCAttr();
372 Qc.removeAddressSpace();
373
Chris Lattner9c85ba32008-11-10 06:08:34 +0000374 // We will create one Derived type for one qualifier and recurse to handle any
375 // additional ones.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000376 unsigned Tag;
John McCalla1805292009-09-25 01:40:47 +0000377 if (Qc.hasConst()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000378 Tag = llvm::dwarf::DW_TAG_const_type;
John McCalla1805292009-09-25 01:40:47 +0000379 Qc.removeConst();
380 } else if (Qc.hasVolatile()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000381 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCalla1805292009-09-25 01:40:47 +0000382 Qc.removeVolatile();
383 } else if (Qc.hasRestrict()) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000384 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCalla1805292009-09-25 01:40:47 +0000385 Qc.removeRestrict();
386 } else {
387 assert(Qc.empty() && "Unknown type qualifier for debug info");
388 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000389 }
Mike Stump1eb44332009-09-09 15:08:12 +0000390
John McCalla1805292009-09-25 01:40:47 +0000391 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
392
Daniel Dunbar3845f862008-10-31 03:54:29 +0000393 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
394 // CVR derived types.
Devang Patelca80a5f2009-10-20 19:55:01 +0000395 llvm::DIType DbgTy =
Devang Pateld58562e2010-03-09 22:49:11 +0000396 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000397 0, 0, 0, 0, 0, FromTy);
Devang Patelca80a5f2009-10-20 19:55:01 +0000398 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000399}
400
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000401llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000402 llvm::DIFile Unit) {
Devang Patelca80a5f2009-10-20 19:55:01 +0000403 llvm::DIType DbgTy =
Anders Carlssona031b352009-11-06 19:19:55 +0000404 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
405 Ty->getPointeeType(), Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000406 return DbgTy;
Daniel Dunbar9df4bb32009-07-14 01:20:56 +0000407}
408
Chris Lattner9c85ba32008-11-10 06:08:34 +0000409llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000410 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +0000411 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
412 Ty->getPointeeType(), Unit);
413}
414
Devang Patelc69e1cf2010-09-30 19:05:55 +0000415/// CreatePointeeType - Create PointTee type. If Pointee is a record
416/// then emit record's fwd if debug info size reduction is enabled.
417llvm::DIType CGDebugInfo::CreatePointeeType(QualType PointeeTy,
418 llvm::DIFile Unit) {
419 if (!CGM.getCodeGenOpts().LimitDebugInfo)
420 return getOrCreateType(PointeeTy, Unit);
421
422 if (const RecordType *RTy = dyn_cast<RecordType>(PointeeTy)) {
423 RecordDecl *RD = RTy->getDecl();
424 unsigned RTag;
425 if (RD->isStruct())
426 RTag = llvm::dwarf::DW_TAG_structure_type;
427 else if (RD->isUnion())
428 RTag = llvm::dwarf::DW_TAG_union_type;
429 else {
430 assert(RD->isClass() && "Unknown RecordType!");
431 RTag = llvm::dwarf::DW_TAG_class_type;
432 }
433
434 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
435 unsigned Line = getLineNumber(RD->getLocation());
436 llvm::DIDescriptor FDContext =
437 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
438
439 return
440 DebugFactory.CreateCompositeType(RTag, FDContext, RD->getName(),
441 DefUnit, Line, 0, 0, 0,
442 llvm::DIType::FlagFwdDecl,
443 llvm::DIType(), llvm::DIArray());
444 }
445 return getOrCreateType(PointeeTy, Unit);
446
447}
448
Anders Carlssona031b352009-11-06 19:19:55 +0000449llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
450 const Type *Ty,
451 QualType PointeeTy,
Devang Patel17800552010-03-09 00:44:50 +0000452 llvm::DIFile Unit) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000453 // Bit size, align and offset of the type.
Anders Carlssona031b352009-11-06 19:19:55 +0000454
455 // Size is always the size of a pointer. We can't use getTypeSize here
456 // because that does not return the correct value for references.
457 uint64_t Size =
Anders Carlsson20f12a22009-12-06 18:00:51 +0000458 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
459 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000460
Devang Patelc69e1cf2010-09-30 19:05:55 +0000461 return DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
462 0, Size, Align, 0, 0,
463 CreatePointeeType(PointeeTy, Unit));
464
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000465}
466
Mike Stump9bc093c2009-05-14 02:03:51 +0000467llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000468 llvm::DIFile Unit) {
Mike Stump9bc093c2009-05-14 02:03:51 +0000469 if (BlockLiteralGenericSet)
470 return BlockLiteralGeneric;
471
Mike Stump9bc093c2009-05-14 02:03:51 +0000472 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
473
474 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
475
476 llvm::DIType FieldTy;
477
478 QualType FType;
479 uint64_t FieldSize, FieldOffset;
480 unsigned FieldAlign;
481
482 llvm::DIArray Elements;
483 llvm::DIType EltTy, DescTy;
484
485 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000486 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000487 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
488 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000489
Daniel Dunbarca308df2009-05-26 19:40:20 +0000490 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000491 EltTys.clear();
492
Devang Patele2472482010-09-29 21:05:52 +0000493 unsigned Flags = llvm::DIDescriptor::FlagAppleBlock;
Devang Patel8ab870d2010-05-12 23:46:38 +0000494 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump3d363c52009-10-02 02:30:50 +0000495
Mike Stump9bc093c2009-05-14 02:03:51 +0000496 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patel8ab870d2010-05-12 23:46:38 +0000497 Unit, LineNo, FieldOffset, 0, 0,
498 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Mike Stump9bc093c2009-05-14 02:03:51 +0000500 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +0000501 uint64_t Size = CGM.getContext().getTypeSize(Ty);
502 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +0000503
Mike Stump9bc093c2009-05-14 02:03:51 +0000504 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000505 Unit, "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000506 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000507
508 FieldOffset = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000509 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000510 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson20f12a22009-12-06 18:00:51 +0000511 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000512 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
513 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramerd3651cc2010-04-24 20:26:20 +0000514 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +0000515 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump9bc093c2009-05-14 02:03:51 +0000516
Anders Carlsson20f12a22009-12-06 18:00:51 +0000517 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000518 FieldTy = DescTy;
Anders Carlsson20f12a22009-12-06 18:00:51 +0000519 FieldSize = CGM.getContext().getTypeSize(Ty);
520 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump9bc093c2009-05-14 02:03:51 +0000521 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000522 "__descriptor", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000523 LineNo, FieldSize, FieldAlign,
Mike Stump9bc093c2009-05-14 02:03:51 +0000524 FieldOffset, 0, FieldTy);
525 EltTys.push_back(FieldTy);
526
527 FieldOffset += FieldSize;
Daniel Dunbarca308df2009-05-26 19:40:20 +0000528 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump9bc093c2009-05-14 02:03:51 +0000529
530 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patel8ab870d2010-05-12 23:46:38 +0000531 Unit, LineNo, FieldOffset, 0, 0,
532 Flags, llvm::DIType(), Elements);
Mike Stump1eb44332009-09-09 15:08:12 +0000533
Mike Stump9bc093c2009-05-14 02:03:51 +0000534 BlockLiteralGenericSet = true;
535 BlockLiteralGeneric
536 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +0000537 "", Unit,
Devang Patel8ab870d2010-05-12 23:46:38 +0000538 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump9bc093c2009-05-14 02:03:51 +0000539 return BlockLiteralGeneric;
540}
541
Chris Lattner9c85ba32008-11-10 06:08:34 +0000542llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000543 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000544 // Typedefs are derived from some other type. If we have a typedef of a
545 // typedef, make sure to emit the whole chain.
546 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner9c85ba32008-11-10 06:08:34 +0000548 // We don't set size information, but do specify where the typedef was
549 // declared.
Devang Patel8ab870d2010-05-12 23:46:38 +0000550 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000551
Devang Pateleb6d79b2010-02-01 21:34:11 +0000552 llvm::DIDescriptor TyContext
553 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
554 Unit);
Devang Patelca80a5f2009-10-20 19:55:01 +0000555 llvm::DIType DbgTy =
Devang Pateld5289052010-01-29 22:29:31 +0000556 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Pateleb6d79b2010-02-01 21:34:11 +0000557 TyContext,
Devang Pateld5289052010-01-29 22:29:31 +0000558 Ty->getDecl()->getName(), Unit,
559 Line, 0, 0, 0, 0, Src);
Devang Patelca80a5f2009-10-20 19:55:01 +0000560 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000561}
562
Chris Lattner9c85ba32008-11-10 06:08:34 +0000563llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000564 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000565 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000566
Chris Lattner9c85ba32008-11-10 06:08:34 +0000567 // Add the result type at least.
568 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump1eb44332009-09-09 15:08:12 +0000569
Chris Lattner9c85ba32008-11-10 06:08:34 +0000570 // Set up remainder of arguments if there is a prototype.
571 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Devang Patelaf164bb2010-10-06 20:51:45 +0000572 if (isa<FunctionNoProtoType>(Ty))
573 EltTys.push_back(DebugFactory.CreateUnspecifiedParameter());
574 else if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattner9c85ba32008-11-10 06:08:34 +0000575 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
576 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000577 }
578
Chris Lattner9c85ba32008-11-10 06:08:34 +0000579 llvm::DIArray EltTypeArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +0000580 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Devang Patelca80a5f2009-10-20 19:55:01 +0000582 llvm::DIType DbgTy =
583 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000584 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +0000585 0, 0, 0, 0, 0,
586 llvm::DIType(), EltTypeArray);
Devang Patelca80a5f2009-10-20 19:55:01 +0000587 return DbgTy;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +0000588}
589
Devang Patel428deb52010-01-19 00:00:59 +0000590/// CollectRecordFields - A helper function to collect debug info for
591/// record fields. This is used while creating debug info entry for a Record.
592void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000593CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel428deb52010-01-19 00:00:59 +0000594 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
595 unsigned FieldNo = 0;
Devang Patel239cec62010-02-01 21:39:52 +0000596 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
597 for (RecordDecl::field_iterator I = RD->field_begin(),
598 E = RD->field_end();
Devang Patel428deb52010-01-19 00:00:59 +0000599 I != E; ++I, ++FieldNo) {
600 FieldDecl *Field = *I;
601 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel428deb52010-01-19 00:00:59 +0000602 llvm::StringRef FieldName = Field->getName();
603
Devang Patel4835fdd2010-02-12 01:31:06 +0000604 // Ignore unnamed fields. Do not ignore unnamed records.
605 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel428deb52010-01-19 00:00:59 +0000606 continue;
607
608 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +0000609 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
610 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel428deb52010-01-19 00:00:59 +0000611 QualType FType = Field->getType();
612 uint64_t FieldSize = 0;
613 unsigned FieldAlign = 0;
614 if (!FType->isIncompleteArrayType()) {
615
616 // Bit size, align and offset of the type.
617 FieldSize = CGM.getContext().getTypeSize(FType);
618 Expr *BitWidth = Field->getBitWidth();
619 if (BitWidth)
620 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel428deb52010-01-19 00:00:59 +0000621 FieldAlign = CGM.getContext().getTypeAlign(FType);
622 }
623
624 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
625
Devang Patel71f4ff62010-04-21 23:12:37 +0000626 unsigned Flags = 0;
627 AccessSpecifier Access = I->getAccess();
628 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000629 Flags |= llvm::DIDescriptor::FlagPrivate;
Devang Patel71f4ff62010-04-21 23:12:37 +0000630 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000631 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Patel71f4ff62010-04-21 23:12:37 +0000632
Devang Patel428deb52010-01-19 00:00:59 +0000633 // Create a DW_TAG_member node to remember the offset of this field in the
634 // struct. FIXME: This is an absolutely insane way to capture this
635 // information. When we gut debug info, this should be fixed.
636 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
637 FieldName, FieldDefUnit,
638 FieldLine, FieldSize, FieldAlign,
Devang Patel71f4ff62010-04-21 23:12:37 +0000639 FieldOffset, Flags, FieldTy);
Devang Patel428deb52010-01-19 00:00:59 +0000640 EltTys.push_back(FieldTy);
641 }
642}
643
Devang Patela6da1922010-01-28 00:28:01 +0000644/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
645/// function type is not updated to include implicit "this" pointer. Use this
646/// routine to get a method type which includes "this" pointer.
647llvm::DIType
648CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000649 llvm::DIFile Unit) {
Douglas Gregor5f970ee2010-05-04 18:18:31 +0000650 llvm::DIType FnTy
651 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
652 0),
653 Unit);
Devang Pateld774d1e2010-01-28 21:43:50 +0000654
Devang Patela6da1922010-01-28 00:28:01 +0000655 // Add "this" pointer.
656
Devang Patelab699792010-05-07 18:12:35 +0000657 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patela6da1922010-01-28 00:28:01 +0000658 assert (Args.getNumElements() && "Invalid number of arguments!");
659
660 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
661
662 // First element is always return type. For 'void' functions it is NULL.
663 Elts.push_back(Args.getElement(0));
664
Devang Patel2ed8f002010-08-27 17:47:47 +0000665 if (!Method->isStatic())
666 {
667 // "this" pointer is always first argument.
668 ASTContext &Context = CGM.getContext();
669 QualType ThisPtr =
670 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
671 llvm::DIType ThisPtrType =
672 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel769640e2010-07-13 00:24:30 +0000673
Devang Patel2ed8f002010-08-27 17:47:47 +0000674 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
675 Elts.push_back(ThisPtrType);
676 }
Devang Patela6da1922010-01-28 00:28:01 +0000677
678 // Copy rest of the arguments.
679 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
680 Elts.push_back(Args.getElement(i));
681
682 llvm::DIArray EltTypeArray =
683 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
684
685 return
686 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000687 Unit, "", Unit,
Devang Patela6da1922010-01-28 00:28:01 +0000688 0, 0, 0, 0, 0,
689 llvm::DIType(), EltTypeArray);
690}
691
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000692/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
693/// a single member function GlobalDecl.
694llvm::DISubprogram
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000695CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel17800552010-03-09 00:44:50 +0000696 llvm::DIFile Unit,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000697 llvm::DIType RecordTy) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000698 bool IsCtorOrDtor =
699 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
700
701 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patela6da1922010-01-28 00:28:01 +0000702 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000703
704 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
705 // make sense to give a single ctor/dtor a linkage name.
Anders Carlsson9a20d552010-06-22 16:16:50 +0000706 llvm::StringRef MethodLinkageName;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000707 if (!IsCtorOrDtor)
Anders Carlsson9a20d552010-06-22 16:16:50 +0000708 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000709
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000710 // Get the location for the method.
Devang Patel8ab870d2010-05-12 23:46:38 +0000711 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
712 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000713
714 // Collect virtual method info.
715 llvm::DIType ContainingType;
716 unsigned Virtuality = 0;
717 unsigned VIndex = 0;
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000718
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000719 if (Method->isVirtual()) {
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000720 if (Method->isPure())
721 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
722 else
723 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
724
725 // It doesn't make sense to give a virtual destructor a vtable index,
726 // since a single destructor has two entries in the vtable.
727 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson046c2942010-04-17 20:15:18 +0000728 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000729 ContainingType = RecordTy;
730 }
731
Devang Patele2472482010-09-29 21:05:52 +0000732 unsigned Flags = 0;
733 if (Method->isImplicit())
734 Flags |= llvm::DIDescriptor::FlagArtificial;
Devang Patel10a7a6a2010-09-29 21:46:16 +0000735 AccessSpecifier Access = Method->getAccess();
736 if (Access == clang::AS_private)
737 Flags |= llvm::DIDescriptor::FlagPrivate;
738 else if (Access == clang::AS_protected)
739 Flags |= llvm::DIDescriptor::FlagProtected;
Devang Pateld78a0192010-10-01 23:32:17 +0000740 if (const CXXConstructorDecl *CXXC = dyn_cast<CXXConstructorDecl>(Method)) {
741 if (CXXC->isExplicit())
742 Flags |= llvm::DIDescriptor::FlagExplicit;
743 } else if (const CXXConversionDecl *CXXC =
744 dyn_cast<CXXConversionDecl>(Method)) {
745 if (CXXC->isExplicit())
746 Flags |= llvm::DIDescriptor::FlagExplicit;
747 }
Devang Patel3951e712010-10-07 22:03:49 +0000748 if (Method->hasPrototype())
749 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Pateld78a0192010-10-01 23:32:17 +0000750
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000751 llvm::DISubprogram SP =
752 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
753 MethodLinkageName,
754 MethodDefUnit, MethodLine,
755 MethodTy, /*isLocalToUnit=*/false,
Devang Patela5c5bab2010-07-12 22:54:41 +0000756 /* isDefintion=*/ false,
Devang Patel40894912010-07-15 22:57:00 +0000757 Virtuality, VIndex, ContainingType,
Devang Patele2472482010-09-29 21:05:52 +0000758 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +0000759 CGM.getLangOptions().Optimize);
Anders Carlsson4433f1c2010-01-26 05:19:50 +0000760
761 // Don't cache ctors or dtors since we have to emit multiple functions for
762 // a single ctor or dtor.
763 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelab699792010-05-07 18:12:35 +0000764 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000765
766 return SP;
767}
768
Devang Patel4125fd22010-01-19 01:54:44 +0000769/// CollectCXXMemberFunctions - A helper function to collect debug info for
770/// C++ member functions.This is used while creating debug info entry for
771/// a Record.
772void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000773CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4125fd22010-01-19 01:54:44 +0000774 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000775 llvm::DIType RecordTy) {
Devang Patel239cec62010-02-01 21:39:52 +0000776 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
777 E = RD->method_end(); I != E; ++I) {
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000778 const CXXMethodDecl *Method = *I;
Anders Carlssonbea9b232010-01-26 04:40:11 +0000779
Devang Pateld5322da2010-02-09 19:09:28 +0000780 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonbea9b232010-01-26 04:40:11 +0000781 continue;
Devang Patel4125fd22010-01-19 01:54:44 +0000782
Anders Carlssond6f9a0d2010-01-26 04:49:33 +0000783 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel4125fd22010-01-19 01:54:44 +0000784 }
785}
786
Devang Patel2ed8f002010-08-27 17:47:47 +0000787/// CollectCXXFriends - A helper function to collect debug info for
788/// C++ base classes. This is used while creating debug info entry for
789/// a Record.
790void CGDebugInfo::
791CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
792 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
793 llvm::DIType RecordTy) {
794
795 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
796 BE = RD->friend_end(); BI != BE; ++BI) {
797
798 TypeSourceInfo *TInfo = (*BI)->getFriendType();
799 if(TInfo)
800 {
801 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
802
803 llvm::DIType DTy =
804 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
805 RecordTy, llvm::StringRef(),
806 Unit, 0, 0, 0,
807 0, 0, Ty);
808
809 EltTys.push_back(DTy);
810 }
811
812 }
813}
814
Devang Patela245c5b2010-01-25 23:32:18 +0000815/// CollectCXXBases - A helper function to collect debug info for
816/// C++ base classes. This is used while creating debug info entry for
817/// a Record.
818void CGDebugInfo::
Devang Patel17800552010-03-09 00:44:50 +0000819CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patela245c5b2010-01-25 23:32:18 +0000820 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman4cac5b42010-08-20 22:02:57 +0000821 llvm::DIType RecordTy) {
Devang Patela245c5b2010-01-25 23:32:18 +0000822
Devang Patel239cec62010-02-01 21:39:52 +0000823 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
824 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
825 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patelca7daed2010-01-28 21:54:15 +0000826 unsigned BFlags = 0;
827 uint64_t BaseOffset;
828
829 const CXXRecordDecl *Base =
830 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
831
832 if (BI->isVirtual()) {
Anders Carlssonbba16072010-03-11 07:15:17 +0000833 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Pateld5322da2010-02-09 19:09:28 +0000834 // expression where it expects +ve number.
Anders Carlssonaf440352010-03-23 04:11:45 +0000835 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patele2472482010-09-29 21:05:52 +0000836 BFlags = llvm::DIDescriptor::FlagVirtual;
Devang Patelca7daed2010-01-28 21:54:15 +0000837 } else
838 BaseOffset = RL.getBaseClassOffset(Base);
839
840 AccessSpecifier Access = BI->getAccessSpecifier();
841 if (Access == clang::AS_private)
Devang Patele2472482010-09-29 21:05:52 +0000842 BFlags |= llvm::DIDescriptor::FlagPrivate;
Devang Patelca7daed2010-01-28 21:54:15 +0000843 else if (Access == clang::AS_protected)
Devang Patele2472482010-09-29 21:05:52 +0000844 BFlags |= llvm::DIDescriptor::FlagProtected;
Devang Patelca7daed2010-01-28 21:54:15 +0000845
846 llvm::DIType DTy =
847 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
848 RecordTy, llvm::StringRef(),
Devang Pateld58562e2010-03-09 22:49:11 +0000849 Unit, 0, 0, 0,
Devang Patelca7daed2010-01-28 21:54:15 +0000850 BaseOffset, BFlags,
851 getOrCreateType(BI->getType(),
852 Unit));
853 EltTys.push_back(DTy);
854 }
Devang Patela245c5b2010-01-25 23:32:18 +0000855}
856
Devang Patel4ce3f202010-01-28 18:11:52 +0000857/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel17800552010-03-09 00:44:50 +0000858llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Patel0804e6e2010-03-08 20:53:17 +0000859 if (VTablePtrType.isValid())
Devang Patel4ce3f202010-01-28 18:11:52 +0000860 return VTablePtrType;
861
862 ASTContext &Context = CGM.getContext();
863
864 /* Function type */
Benjamin Kramerad468862010-03-30 11:36:44 +0000865 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
866 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel4ce3f202010-01-28 18:11:52 +0000867 llvm::DIType SubTy =
868 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000869 Unit, "", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000870 0, 0, 0, 0, 0, llvm::DIType(), SElements);
871
872 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
873 llvm::DIType vtbl_ptr_type
874 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Pateld58562e2010-03-09 22:49:11 +0000875 Unit, "__vtbl_ptr_type", Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000876 0, Size, 0, 0, 0, SubTy);
877
Devang Pateld58562e2010-03-09 22:49:11 +0000878 VTablePtrType =
879 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
880 Unit, "", Unit,
881 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel4ce3f202010-01-28 18:11:52 +0000882 return VTablePtrType;
883}
884
Anders Carlsson046c2942010-04-17 20:15:18 +0000885/// getVTableName - Get vtable name for the given Class.
886llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel4ce3f202010-01-28 18:11:52 +0000887 // Otherwise construct gdb compatible name name.
Devang Patel239cec62010-02-01 21:39:52 +0000888 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel4ce3f202010-01-28 18:11:52 +0000889
890 // Copy this name on the side and use its reference.
Devang Patel89f05f82010-01-28 18:21:00 +0000891 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel4ce3f202010-01-28 18:11:52 +0000892 memcpy(StrPtr, Name.data(), Name.length());
893 return llvm::StringRef(StrPtr, Name.length());
894}
895
896
Anders Carlsson046c2942010-04-17 20:15:18 +0000897/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel4ce3f202010-01-28 18:11:52 +0000898/// debug info entry in EltTys vector.
899void CGDebugInfo::
Anders Carlsson046c2942010-04-17 20:15:18 +0000900CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000901 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel239cec62010-02-01 21:39:52 +0000902 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel4ce3f202010-01-28 18:11:52 +0000903
904 // If there is a primary base then it will hold vtable info.
905 if (RL.getPrimaryBase())
906 return;
907
908 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel239cec62010-02-01 21:39:52 +0000909 if (!RD->isDynamicClass())
Devang Patel4ce3f202010-01-28 18:11:52 +0000910 return;
911
912 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
913 llvm::DIType VPTR
914 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson046c2942010-04-17 20:15:18 +0000915 getVTableName(RD), Unit,
Devang Patel4ce3f202010-01-28 18:11:52 +0000916 0, Size, 0, 0, 0,
917 getOrCreateVTablePtrType(Unit));
918 EltTys.push_back(VPTR);
919}
920
Devang Patelc69e1cf2010-09-30 19:05:55 +0000921/// getOrCreateRecordType - Emit record type's standalone debug info.
922llvm::DIType CGDebugInfo::getOrCreateRecordType(QualType RTy,
923 SourceLocation Loc) {
924 llvm::DIType T = getOrCreateType(RTy, getOrCreateFile(Loc));
925 DebugFactory.RecordType(T);
926 return T;
927}
928
Devang Patel65e99f22009-02-25 01:36:11 +0000929/// CreateType - get structure or union type.
Chris Lattner9c85ba32008-11-10 06:08:34 +0000930llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel17800552010-03-09 00:44:50 +0000931 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +0000932 RecordDecl *RD = Ty->getDecl();
Mike Stump1eb44332009-09-09 15:08:12 +0000933
Chris Lattner9c85ba32008-11-10 06:08:34 +0000934 unsigned Tag;
Devang Pateld6c5a262010-02-01 21:52:22 +0000935 if (RD->isStruct())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000936 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Pateld6c5a262010-02-01 21:52:22 +0000937 else if (RD->isUnion())
Chris Lattner9c85ba32008-11-10 06:08:34 +0000938 Tag = llvm::dwarf::DW_TAG_union_type;
939 else {
Devang Pateld6c5a262010-02-01 21:52:22 +0000940 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattner9c85ba32008-11-10 06:08:34 +0000941 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Guptaf58c27a2008-06-07 04:46:53 +0000942 }
943
Chris Lattner9c85ba32008-11-10 06:08:34 +0000944 // Get overall information about the record type for the debug info.
Devang Patel8ab870d2010-05-12 23:46:38 +0000945 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
946 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump1eb44332009-09-09 15:08:12 +0000947
Chris Lattner9c85ba32008-11-10 06:08:34 +0000948 // Records and classes and unions can all be recursive. To handle them, we
949 // first generate a debug descriptor for the struct as a forward declaration.
950 // Then (if it is a definition) we go through and get debug info for all of
951 // its members. Finally, we create a descriptor for the complete type (which
952 // may refer to the forward decl if the struct is recursive) and replace all
953 // uses of the forward declaration with the final definition.
Devang Patel0b897992010-07-08 19:56:29 +0000954 llvm::DIDescriptor FDContext =
955 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
956
957 // If this is just a forward declaration, construct an appropriately
958 // marked node and just return it.
959 if (!RD->getDefinition()) {
960 llvm::DICompositeType FwdDecl =
961 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
962 DefUnit, Line, 0, 0, 0,
Devang Patele2472482010-09-29 21:05:52 +0000963 llvm::DIDescriptor::FlagFwdDecl,
Devang Patel0b897992010-07-08 19:56:29 +0000964 llvm::DIType(), llvm::DIArray());
965
966 return FwdDecl;
967 }
Devang Pateld0f251b2010-01-20 23:56:40 +0000968
Dan Gohman86968372010-08-20 22:39:57 +0000969 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +0000970
Devang Patelab699792010-05-07 18:12:35 +0000971 llvm::MDNode *MN = FwdDecl;
972 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattner9c85ba32008-11-10 06:08:34 +0000973 // Otherwise, insert it into the TypeCache so that recursive uses will find
974 // it.
Devang Patelab699792010-05-07 18:12:35 +0000975 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +0000976 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +0000977 RegionStack.push_back(FwdDeclNode);
978 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +0000979
980 // Convert all the elements.
981 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
982
Devang Pateld6c5a262010-02-01 21:52:22 +0000983 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel3064afe2010-01-28 21:41:35 +0000984 if (CXXDecl) {
985 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson046c2942010-04-17 20:15:18 +0000986 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel3064afe2010-01-28 21:41:35 +0000987 }
Devang Pateldabc3e92010-08-12 00:02:44 +0000988
989 // Collect static variables with initializers.
990 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
991 I != E; ++I)
992 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
993 if (const Expr *Init = V->getInit()) {
994 Expr::EvalResult Result;
995 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
996 llvm::ConstantInt *CI
997 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
998
999 // Create the descriptor for static variable.
1000 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
1001 llvm::StringRef VName = V->getName();
1002 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
1003 // Do not use DIGlobalVariable for enums.
1004 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
1005 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
1006 getLineNumber(V->getLocation()),
1007 VTy, true, true, CI);
1008 }
1009 }
1010 }
1011 }
1012
Devang Pateld6c5a262010-02-01 21:52:22 +00001013 CollectRecordFields(RD, Unit, EltTys);
Devang Patel0ac8f312010-01-28 00:54:21 +00001014 llvm::MDNode *ContainingType = NULL;
Devang Patel4ce3f202010-01-28 18:11:52 +00001015 if (CXXDecl) {
Devang Patel4125fd22010-01-19 01:54:44 +00001016 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel2ed8f002010-08-27 17:47:47 +00001017 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel0ac8f312010-01-28 00:54:21 +00001018
1019 // A class's primary base or the class itself contains the vtable.
Devang Pateld6c5a262010-02-01 21:52:22 +00001020 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel0ac8f312010-01-28 00:54:21 +00001021 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
1022 ContainingType =
Devang Patelab699792010-05-07 18:12:35 +00001023 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patel0ac8f312010-01-28 00:54:21 +00001024 else if (CXXDecl->isDynamicClass())
Devang Patelab699792010-05-07 18:12:35 +00001025 ContainingType = FwdDecl;
Devang Patela245c5b2010-01-25 23:32:18 +00001026 }
Mike Stump1eb44332009-09-09 15:08:12 +00001027
Chris Lattner9c85ba32008-11-10 06:08:34 +00001028 llvm::DIArray Elements =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001029 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001030
1031 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001032 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1033 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001034
Devang Patele4c1ea02010-03-11 20:01:48 +00001035 RegionStack.pop_back();
1036 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1037 RegionMap.find(Ty->getDecl());
1038 if (RI != RegionMap.end())
1039 RegionMap.erase(RI);
1040
Devang Patel411894b2010-02-01 22:40:08 +00001041 llvm::DIDescriptor RDContext =
1042 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel700a1cb2010-07-20 20:24:18 +00001043
1044 llvm::StringRef RDName = RD->getName();
1045 // If this is a class, include the template arguments also.
1046 if (Tag == llvm::dwarf::DW_TAG_class_type)
1047 RDName = getClassName(RD);
1048
Devang Patel0ce73f62009-07-22 18:57:00 +00001049 llvm::DICompositeType RealDecl =
Devang Patel411894b2010-02-01 22:40:08 +00001050 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel700a1cb2010-07-20 20:24:18 +00001051 RDName,
Devang Patelab71ff52009-11-12 00:51:46 +00001052 DefUnit, Line, Size, Align, 0, 0,
Devang Patel0ac8f312010-01-28 00:54:21 +00001053 llvm::DIType(), Elements,
1054 0, ContainingType);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001055
1056 // Now that we have a real decl for the struct, replace anything using the
1057 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001058 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001059 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001060 return RealDecl;
1061}
1062
John McCallc12c5bb2010-05-15 11:32:37 +00001063/// CreateType - get objective-c object type.
1064llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1065 llvm::DIFile Unit) {
1066 // Ignore protocols.
1067 return getOrCreateType(Ty->getBaseType(), Unit);
1068}
1069
Devang Patel9ca36b62009-02-26 21:10:26 +00001070/// CreateType - get objective-c interface type.
1071llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001072 llvm::DIFile Unit) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001073 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patel9ca36b62009-02-26 21:10:26 +00001074 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel9ca36b62009-02-26 21:10:26 +00001075
1076 // Get overall information about the record type for the debug info.
Devang Patel17800552010-03-09 00:44:50 +00001077 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001078 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel17800552010-03-09 00:44:50 +00001079 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerac7c8142009-05-02 01:13:16 +00001080
Dan Gohman45f7c782010-08-23 21:15:56 +00001081 // If this is just a forward declaration, return a special forward-declaration
1082 // debug type.
1083 if (ID->isForwardDecl()) {
1084 llvm::DICompositeType FwdDecl =
1085 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1086 DefUnit, Line, 0, 0, 0, 0,
1087 llvm::DIType(), llvm::DIArray(),
1088 RuntimeLang);
1089 return FwdDecl;
1090 }
1091
Devang Patel9ca36b62009-02-26 21:10:26 +00001092 // To handle recursive interface, we
1093 // first generate a debug descriptor for the struct as a forward declaration.
1094 // Then (if it is a definition) we go through and get debug info for all of
1095 // its members. Finally, we create a descriptor for the complete type (which
1096 // may refer to the forward decl if the struct is recursive) and replace all
1097 // uses of the forward declaration with the final definition.
Dan Gohman86968372010-08-20 22:39:57 +00001098 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump1eb44332009-09-09 15:08:12 +00001099
Devang Patelab699792010-05-07 18:12:35 +00001100 llvm::MDNode *MN = FwdDecl;
1101 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patel9ca36b62009-02-26 21:10:26 +00001102 // Otherwise, insert it into the TypeCache so that recursive uses will find
1103 // it.
Devang Patelab699792010-05-07 18:12:35 +00001104 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patele4c1ea02010-03-11 20:01:48 +00001105 // Push the struct on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001106 RegionStack.push_back(FwdDeclNode);
1107 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patel9ca36b62009-02-26 21:10:26 +00001108
1109 // Convert all the elements.
1110 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1111
Devang Pateld6c5a262010-02-01 21:52:22 +00001112 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelfbe899f2009-03-10 21:30:26 +00001113 if (SClass) {
Mike Stump1eb44332009-09-09 15:08:12 +00001114 llvm::DIType SClassTy =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001115 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001116 llvm::DIType InhTag =
Devang Patelfbe899f2009-03-10 21:30:26 +00001117 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Pateld58562e2010-03-09 22:49:11 +00001118 Unit, "", Unit, 0, 0, 0,
Devang Patelfbe899f2009-03-10 21:30:26 +00001119 0 /* offset */, 0, SClassTy);
1120 EltTys.push_back(InhTag);
1121 }
1122
Devang Pateld6c5a262010-02-01 21:52:22 +00001123 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001124
1125 unsigned FieldNo = 0;
Fariborz Jahanian97477392010-10-01 00:01:53 +00001126 for (ObjCIvarDecl *Field = ID->all_declared_ivar_begin(); Field;
Fariborz Jahanianfe8fdba2010-10-11 23:55:47 +00001127 Field = Field->getNextIvar(), ++FieldNo) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001128 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1129
Devang Patel73621622009-11-25 17:37:31 +00001130 llvm::StringRef FieldName = Field->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001131
Devang Patelde135022009-04-27 22:40:36 +00001132 // Ignore unnamed fields.
Devang Patel73621622009-11-25 17:37:31 +00001133 if (FieldName.empty())
Devang Patelde135022009-04-27 22:40:36 +00001134 continue;
1135
Devang Patel9ca36b62009-02-26 21:10:26 +00001136 // Get the location for the field.
Devang Patel8ab870d2010-05-12 23:46:38 +00001137 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1138 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel99c20eb2009-03-20 18:24:39 +00001139 QualType FType = Field->getType();
1140 uint64_t FieldSize = 0;
1141 unsigned FieldAlign = 0;
Devang Patelc20482b2009-03-19 00:23:53 +00001142
Devang Patel99c20eb2009-03-20 18:24:39 +00001143 if (!FType->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001144
Devang Patel99c20eb2009-03-20 18:24:39 +00001145 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001146 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001147 Expr *BitWidth = Field->getBitWidth();
1148 if (BitWidth)
Anders Carlsson20f12a22009-12-06 18:00:51 +00001149 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman9a901bb2009-04-26 19:19:15 +00001150
Anders Carlsson20f12a22009-12-06 18:00:51 +00001151 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel99c20eb2009-03-20 18:24:39 +00001152 }
1153
Mike Stump1eb44332009-09-09 15:08:12 +00001154 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1155
Devang Patelc20482b2009-03-19 00:23:53 +00001156 unsigned Flags = 0;
1157 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
Devang Patele2472482010-09-29 21:05:52 +00001158 Flags = llvm::DIDescriptor::FlagProtected;
Devang Patelc20482b2009-03-19 00:23:53 +00001159 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
Devang Patele2472482010-09-29 21:05:52 +00001160 Flags = llvm::DIDescriptor::FlagPrivate;
Mike Stump1eb44332009-09-09 15:08:12 +00001161
Devang Patel9ca36b62009-02-26 21:10:26 +00001162 // Create a DW_TAG_member node to remember the offset of this field in the
1163 // struct. FIXME: This is an absolutely insane way to capture this
1164 // information. When we gut debug info, this should be fixed.
1165 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1166 FieldName, FieldDefUnit,
1167 FieldLine, FieldSize, FieldAlign,
Devang Patelc20482b2009-03-19 00:23:53 +00001168 FieldOffset, Flags, FieldTy);
Devang Patel9ca36b62009-02-26 21:10:26 +00001169 EltTys.push_back(FieldTy);
1170 }
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Devang Patel9ca36b62009-02-26 21:10:26 +00001172 llvm::DIArray Elements =
Jay Foadbeaaccd2009-05-21 09:52:38 +00001173 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patel9ca36b62009-02-26 21:10:26 +00001174
Devang Patele4c1ea02010-03-11 20:01:48 +00001175 RegionStack.pop_back();
1176 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1177 RegionMap.find(Ty->getDecl());
1178 if (RI != RegionMap.end())
1179 RegionMap.erase(RI);
1180
Devang Patel9ca36b62009-02-26 21:10:26 +00001181 // Bit size, align and offset of the type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001182 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1183 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump1eb44332009-09-09 15:08:12 +00001184
Devang Patel6c1fddf2009-07-27 18:42:03 +00001185 llvm::DICompositeType RealDecl =
Devang Pateld6c5a262010-02-01 21:52:22 +00001186 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patelab71ff52009-11-12 00:51:46 +00001187 Line, Size, Align, 0, 0, llvm::DIType(),
1188 Elements, RuntimeLang);
Devang Patel9ca36b62009-02-26 21:10:26 +00001189
1190 // Now that we have a real decl for the struct, replace anything using the
1191 // old decl with the new one. This will recursively update the debug info.
Dan Gohman4cac5b42010-08-20 22:02:57 +00001192 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelab699792010-05-07 18:12:35 +00001193 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patelfe09eab2009-07-13 17:03:14 +00001194
Devang Patel9ca36b62009-02-26 21:10:26 +00001195 return RealDecl;
1196}
1197
Chris Lattner9c85ba32008-11-10 06:08:34 +00001198llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001199 llvm::DIFile Unit) {
Devang Patel6237cea2010-08-23 22:07:25 +00001200 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001201
Chris Lattner9c85ba32008-11-10 06:08:34 +00001202}
1203
1204llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001205 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001206 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1207 return CreateType(RT, Unit);
1208 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1209 return CreateType(ET, Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001210
Chris Lattner9c85ba32008-11-10 06:08:34 +00001211 return llvm::DIType();
1212}
1213
Devang Patel70c23cd2010-02-23 22:59:39 +00001214llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedmana7e68452010-08-22 01:00:03 +00001215 llvm::DIFile Unit) {
Devang Patel70c23cd2010-02-23 22:59:39 +00001216 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1217 uint64_t NumElems = Ty->getNumElements();
1218 if (NumElems > 0)
1219 --NumElems;
Devang Patel70c23cd2010-02-23 22:59:39 +00001220
Benjamin Kramerad468862010-03-30 11:36:44 +00001221 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1222 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patel70c23cd2010-02-23 22:59:39 +00001223
1224 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1225 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1226
1227 return
1228 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001229 Unit, "", Unit,
Devang Patel70c23cd2010-02-23 22:59:39 +00001230 0, Size, Align, 0, 0,
Eli Friedmana7e68452010-08-22 01:00:03 +00001231 ElementTy, SubscriptArray);
Devang Patel70c23cd2010-02-23 22:59:39 +00001232}
1233
Chris Lattner9c85ba32008-11-10 06:08:34 +00001234llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001235 llvm::DIFile Unit) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001236 uint64_t Size;
1237 uint64_t Align;
Mike Stump1eb44332009-09-09 15:08:12 +00001238
1239
Nuno Lopes010d5142009-01-28 00:35:17 +00001240 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlsson835c9092009-01-05 01:23:29 +00001241 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlsson835c9092009-01-05 01:23:29 +00001242 Size = 0;
1243 Align =
Anders Carlsson20f12a22009-12-06 18:00:51 +00001244 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopes010d5142009-01-28 00:35:17 +00001245 } else if (Ty->isIncompleteArrayType()) {
1246 Size = 0;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001247 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlsson835c9092009-01-05 01:23:29 +00001248 } else {
1249 // Size and align of the whole array, not the element type.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001250 Size = CGM.getContext().getTypeSize(Ty);
1251 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlsson835c9092009-01-05 01:23:29 +00001252 }
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattner9c85ba32008-11-10 06:08:34 +00001254 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1255 // interior arrays, do we care? Why aren't nested arrays represented the
1256 // obvious/recursive way?
1257 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1258 QualType EltTy(Ty, 0);
Devang Patelcdf523c2010-10-06 18:30:00 +00001259 if (Ty->isIncompleteArrayType())
Chris Lattner9c85ba32008-11-10 06:08:34 +00001260 EltTy = Ty->getElementType();
Devang Patelcdf523c2010-10-06 18:30:00 +00001261 else {
1262 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
1263 uint64_t Upper = 0;
1264 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
1265 if (CAT->getSize().getZExtValue())
1266 Upper = CAT->getSize().getZExtValue() - 1;
1267 // FIXME: Verify this is right for VLAs.
1268 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1269 EltTy = Ty->getElementType();
1270 }
Sanjiv Gupta507de852008-06-09 10:47:41 +00001271 }
Mike Stump1eb44332009-09-09 15:08:12 +00001272
Chris Lattner9c85ba32008-11-10 06:08:34 +00001273 llvm::DIArray SubscriptArray =
Daniel Dunbarca308df2009-05-26 19:40:20 +00001274 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattner9c85ba32008-11-10 06:08:34 +00001275
Devang Patelca80a5f2009-10-20 19:55:01 +00001276 llvm::DIType DbgTy =
1277 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001278 Unit, "", Unit,
Devang Patelca80a5f2009-10-20 19:55:01 +00001279 0, Size, Align, 0, 0,
1280 getOrCreateType(EltTy, Unit),
1281 SubscriptArray);
Devang Patelca80a5f2009-10-20 19:55:01 +00001282 return DbgTy;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001283}
1284
Anders Carlssona031b352009-11-06 19:19:55 +00001285llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001286 llvm::DIFile Unit) {
Anders Carlssona031b352009-11-06 19:19:55 +00001287 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1288 Ty, Ty->getPointeeType(), Unit);
1289}
Chris Lattner9c85ba32008-11-10 06:08:34 +00001290
Anders Carlsson20f12a22009-12-06 18:00:51 +00001291llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel17800552010-03-09 00:44:50 +00001292 llvm::DIFile U) {
Anders Carlsson20f12a22009-12-06 18:00:51 +00001293 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1294 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1295
1296 if (!Ty->getPointeeType()->isFunctionType()) {
1297 // We have a data member pointer type.
1298 return PointerDiffDITy;
1299 }
1300
1301 // We have a member function pointer type. Treat it as a struct with two
1302 // ptrdiff_t members.
1303 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1304
1305 uint64_t FieldOffset = 0;
1306 llvm::DIDescriptor ElementTypes[2];
1307
1308 // FIXME: This should probably be a function type instead.
1309 ElementTypes[0] =
1310 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001311 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001312 Info.first, Info.second, FieldOffset, 0,
1313 PointerDiffDITy);
1314 FieldOffset += Info.first;
1315
1316 ElementTypes[1] =
1317 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Pateld58562e2010-03-09 22:49:11 +00001318 "ptr", U, 0,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001319 Info.first, Info.second, FieldOffset, 0,
1320 PointerDiffDITy);
1321
1322 llvm::DIArray Elements =
1323 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1324 llvm::array_lengthof(ElementTypes));
1325
1326 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1327 U, llvm::StringRef("test"),
Devang Pateld58562e2010-03-09 22:49:11 +00001328 U, 0, FieldOffset,
Anders Carlsson20f12a22009-12-06 18:00:51 +00001329 0, 0, 0, llvm::DIType(), Elements);
1330}
1331
Devang Patel6237cea2010-08-23 22:07:25 +00001332/// CreateEnumType - get enumeration type.
1333llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1334 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1335
1336 // Create DIEnumerator elements for each enumerator.
1337 for (EnumDecl::enumerator_iterator
1338 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1339 Enum != EnumEnd; ++Enum) {
1340 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1341 Enum->getInitVal().getZExtValue()));
1342 }
1343
1344 // Return a CompositeType for the enum itself.
1345 llvm::DIArray EltArray =
1346 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1347
1348 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1349 unsigned Line = getLineNumber(ED->getLocation());
1350 uint64_t Size = 0;
Devang Patelffc52e72010-08-24 18:14:06 +00001351 uint64_t Align = 0;
1352 if (!ED->getTypeForDecl()->isIncompleteType()) {
1353 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1354 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1355 }
Devang Patel6237cea2010-08-23 22:07:25 +00001356 llvm::DIType DbgTy =
1357 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1358 Unit, ED->getName(), DefUnit, Line,
Devang Patelffc52e72010-08-24 18:14:06 +00001359 Size, Align, 0, 0,
Devang Patel6237cea2010-08-23 22:07:25 +00001360 llvm::DIType(), EltArray);
1361 return DbgTy;
1362}
1363
Douglas Gregor840943d2009-12-21 20:18:30 +00001364static QualType UnwrapTypeForDebugInfo(QualType T) {
1365 do {
1366 QualType LastT = T;
1367 switch (T->getTypeClass()) {
1368 default:
1369 return T;
1370 case Type::TemplateSpecialization:
1371 T = cast<TemplateSpecializationType>(T)->desugar();
1372 break;
1373 case Type::TypeOfExpr: {
1374 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1375 T = Ty->getUnderlyingExpr()->getType();
1376 break;
1377 }
1378 case Type::TypeOf:
1379 T = cast<TypeOfType>(T)->getUnderlyingType();
1380 break;
1381 case Type::Decltype:
1382 T = cast<DecltypeType>(T)->getUnderlyingType();
1383 break;
Abramo Bagnara465d41b2010-05-11 21:36:43 +00001384 case Type::Elaborated:
1385 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor840943d2009-12-21 20:18:30 +00001386 break;
1387 case Type::SubstTemplateTypeParm:
1388 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1389 break;
Douglas Gregor840943d2009-12-21 20:18:30 +00001390 }
1391
1392 assert(T != LastT && "Type unwrapping failed to unwrap!");
1393 if (T == LastT)
1394 return T;
1395 } while (true);
1396
1397 return T;
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001398}
1399
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001400/// getOrCreateType - Get the type from the cache or create a new
1401/// one if necessary.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001402llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001403 llvm::DIFile Unit) {
Chris Lattner9c85ba32008-11-10 06:08:34 +00001404 if (Ty.isNull())
1405 return llvm::DIType();
Mike Stump1eb44332009-09-09 15:08:12 +00001406
Douglas Gregor840943d2009-12-21 20:18:30 +00001407 // Unwrap the type as needed for debug information.
1408 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson5b6117a2009-11-14 21:08:12 +00001409
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001410 // Check for existing entry.
Ted Kremenek590838b2010-03-29 18:29:57 +00001411 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001412 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar65f13c32009-09-19 20:17:48 +00001413 if (it != TypeCache.end()) {
1414 // Verify that the debug info still exists.
1415 if (&*it->second)
1416 return llvm::DIType(cast<llvm::MDNode>(it->second));
1417 }
Daniel Dunbar03faac32009-09-19 19:27:14 +00001418
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001419 // Otherwise create the type.
1420 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001421
1422 // And update the type cache.
Devang Patelab699792010-05-07 18:12:35 +00001423 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar23e81ba2009-09-19 19:27:24 +00001424 return Res;
Daniel Dunbar03faac32009-09-19 19:27:14 +00001425}
1426
Anders Carlsson0dd57c62009-11-14 20:52:05 +00001427/// CreateTypeNode - Create a new debug type node.
Daniel Dunbar03faac32009-09-19 19:27:14 +00001428llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel17800552010-03-09 00:44:50 +00001429 llvm::DIFile Unit) {
John McCalla1805292009-09-25 01:40:47 +00001430 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregora4923eb2009-11-16 21:35:15 +00001431 if (Ty.hasLocalQualifiers())
John McCalla1805292009-09-25 01:40:47 +00001432 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001433
Douglas Gregor2101a822009-12-21 19:57:21 +00001434 const char *Diag = 0;
1435
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001436 // Work out details of type.
Chris Lattner9c85ba32008-11-10 06:08:34 +00001437 switch (Ty->getTypeClass()) {
Douglas Gregor72564e72009-02-26 23:50:07 +00001438#define TYPE(Class, Base)
1439#define ABSTRACT_TYPE(Class, Base)
1440#define NON_CANONICAL_TYPE(Class, Base)
1441#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1442#include "clang/AST/TypeNodes.def"
1443 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidis1bb8a452009-08-19 01:28:17 +00001444
Anders Carlssonbfe69952009-11-06 18:24:04 +00001445 // FIXME: Handle these.
1446 case Type::ExtVector:
Anders Carlssonbfe69952009-11-06 18:24:04 +00001447 return llvm::DIType();
Devang Patel70c23cd2010-02-23 22:59:39 +00001448
1449 case Type::Vector:
1450 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbar9df4bb32009-07-14 01:20:56 +00001451 case Type::ObjCObjectPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001452 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCallc12c5bb2010-05-15 11:32:37 +00001453 case Type::ObjCObject:
1454 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump1eb44332009-09-09 15:08:12 +00001455 case Type::ObjCInterface:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001456 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1457 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1458 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1459 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump9bc093c2009-05-14 02:03:51 +00001460 case Type::BlockPointer:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001461 return CreateType(cast<BlockPointerType>(Ty), Unit);
1462 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregor72564e72009-02-26 23:50:07 +00001463 case Type::Record:
Douglas Gregor72564e72009-02-26 23:50:07 +00001464 case Type::Enum:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001465 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001466 case Type::FunctionProto:
1467 case Type::FunctionNoProto:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001468 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001469 case Type::ConstantArray:
1470 case Type::VariableArray:
1471 case Type::IncompleteArray:
Daniel Dunbar03faac32009-09-19 19:27:14 +00001472 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlssona031b352009-11-06 19:19:55 +00001473
1474 case Type::LValueReference:
1475 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1476
Anders Carlsson20f12a22009-12-06 18:00:51 +00001477 case Type::MemberPointer:
1478 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor2101a822009-12-21 19:57:21 +00001479
1480 case Type::TemplateSpecialization:
Douglas Gregor2101a822009-12-21 19:57:21 +00001481 case Type::Elaborated:
Douglas Gregor2101a822009-12-21 19:57:21 +00001482 case Type::SubstTemplateTypeParm:
Douglas Gregor2101a822009-12-21 19:57:21 +00001483 case Type::TypeOfExpr:
1484 case Type::TypeOf:
Douglas Gregor840943d2009-12-21 20:18:30 +00001485 case Type::Decltype:
1486 llvm_unreachable("type should have been unwrapped!");
1487 return llvm::DIType();
Douglas Gregor2101a822009-12-21 19:57:21 +00001488
1489 case Type::RValueReference:
1490 // FIXME: Implement!
1491 Diag = "rvalue references";
1492 break;
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001493 }
Douglas Gregor2101a822009-12-21 19:57:21 +00001494
1495 assert(Diag && "Fall through without a diagnostic?");
1496 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1497 "debug information for %0 is not yet supported");
1498 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1499 << Diag;
1500 return llvm::DIType();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001501}
1502
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001503/// CreateMemberType - Create new member and increase Offset by FType's size.
1504llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1505 llvm::StringRef Name,
1506 uint64_t *Offset) {
1507 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1508 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1509 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1510 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1511 Unit, Name, Unit, 0,
1512 FieldSize, FieldAlign,
1513 *Offset, 0, FieldTy);
1514 *Offset += FieldSize;
1515 return Ty;
1516}
1517
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001518/// EmitFunctionStart - Constructs the debug code for entering a function -
1519/// "llvm.dbg.func.start.".
Devang Patel9c6c3a02010-01-14 00:36:21 +00001520void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001521 llvm::Function *Fn,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001522 CGBuilderTy &Builder) {
Mike Stump1eb44332009-09-09 15:08:12 +00001523
Devang Patel9c6c3a02010-01-14 00:36:21 +00001524 llvm::StringRef Name;
Anders Carlsson9a20d552010-06-22 16:16:50 +00001525 llvm::StringRef LinkageName;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001526
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001527 FnBeginRegionCount.push_back(RegionStack.size());
1528
Devang Patel9c6c3a02010-01-14 00:36:21 +00001529 const Decl *D = GD.getDecl();
Devang Patel3951e712010-10-07 22:03:49 +00001530 unsigned Flags = 0;
Devang Patel0692f832010-10-11 21:58:41 +00001531 llvm::DIFile Unit = getOrCreateFile(CurLoc);
1532 llvm::DIDescriptor FDContext(Unit);
Devang Patel9c6c3a02010-01-14 00:36:21 +00001533 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel4125fd22010-01-19 01:54:44 +00001534 // If there is a DISubprogram for this function available then use it.
1535 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1536 FI = SPCache.find(FD);
1537 if (FI != SPCache.end()) {
Gabor Greif38c9b172010-09-18 13:00:17 +00001538 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelab699792010-05-07 18:12:35 +00001539 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1540 llvm::MDNode *SPN = SP;
1541 RegionStack.push_back(SPN);
1542 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel4125fd22010-01-19 01:54:44 +00001543 return;
1544 }
1545 }
Devang Patel9c6c3a02010-01-14 00:36:21 +00001546 Name = getFunctionName(FD);
1547 // Use mangled name as linkage name for c/c++ functions.
Anders Carlsson9a20d552010-06-22 16:16:50 +00001548 LinkageName = CGM.getMangledName(GD);
Devang Patel3951e712010-10-07 22:03:49 +00001549 if (FD->hasPrototype())
1550 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel0692f832010-10-11 21:58:41 +00001551 if (const NamespaceDecl *NSDecl =
1552 dyn_cast_or_null<NamespaceDecl>(FD->getDeclContext()))
1553 FDContext = getOrCreateNameSpace(NSDecl, Unit);
David Chisnall70b9b442010-09-02 17:16:32 +00001554 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnall52044a22010-09-02 18:01:51 +00001555 Name = getObjCMethodName(OMD);
David Chisnall70b9b442010-09-02 17:16:32 +00001556 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001557 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001558 } else {
1559 // Use llvm function name as linkage name.
1560 Name = Fn->getName();
Anders Carlsson9a20d552010-06-22 16:16:50 +00001561 LinkageName = Name;
Devang Patel3951e712010-10-07 22:03:49 +00001562 Flags |= llvm::DIDescriptor::FlagPrototyped;
Devang Patel9c6c3a02010-01-14 00:36:21 +00001563 }
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001564 if (!Name.empty() && Name[0] == '\01')
1565 Name = Name.substr(1);
Mike Stump1eb44332009-09-09 15:08:12 +00001566
Devang Patel970c6182010-04-24 00:49:16 +00001567 // It is expected that CurLoc is set before using EmitFunctionStart.
1568 // Usually, CurLoc points to the left bracket location of compound
1569 // statement representing function body.
Devang Patel8ab870d2010-05-12 23:46:38 +00001570 unsigned LineNo = getLineNumber(CurLoc);
Devang Patele2472482010-09-29 21:05:52 +00001571 if (D->isImplicit())
1572 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattner9c85ba32008-11-10 06:08:34 +00001573 llvm::DISubprogram SP =
Devang Patel0692f832010-10-11 21:58:41 +00001574 DebugFactory.CreateSubprogram(FDContext, Name, Name, LinkageName, Unit, LineNo,
Mike Stump91cc8152009-10-23 01:52:13 +00001575 getOrCreateType(FnType, Unit),
Devang Patel15a3d7d2010-07-15 23:09:46 +00001576 Fn->hasInternalLinkage(), true/*definition*/,
1577 0, 0, llvm::DIType(),
Devang Patele2472482010-09-29 21:05:52 +00001578 Flags,
Devang Patel15a3d7d2010-07-15 23:09:46 +00001579 CGM.getLangOptions().Optimize, Fn);
Mike Stump1eb44332009-09-09 15:08:12 +00001580
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001581 // Push function on region stack.
Devang Patelab699792010-05-07 18:12:35 +00001582 llvm::MDNode *SPN = SP;
1583 RegionStack.push_back(SPN);
1584 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001585
1586 // Clear stack used to keep track of #line directives.
1587 LineDirectiveFiles.clear();
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001588}
1589
1590
Devang Patel4d939e62010-07-20 22:20:10 +00001591void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001592 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump1eb44332009-09-09 15:08:12 +00001593
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001594 // Don't bother if things are the same as last time.
Anders Carlsson20f12a22009-12-06 18:00:51 +00001595 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +00001596 if (CurLoc == PrevLoc
Chris Lattner30fc9332009-02-04 01:06:56 +00001597 || (SM.getInstantiationLineNumber(CurLoc) ==
1598 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001599 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patel4800ea62010-04-05 21:09:15 +00001600 // New Builder may not be in sync with CGDebugInfo.
1601 if (!Builder.getCurrentDebugLocation().isUnknown())
1602 return;
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001603
1604 // Update last state.
1605 PrevLoc = CurLoc;
1606
Chris Lattnerc6034632010-04-01 06:31:43 +00001607 llvm::MDNode *Scope = RegionStack.back();
Devang Patel8ab870d2010-05-12 23:46:38 +00001608 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1609 getColumnNumber(CurLoc),
Chris Lattnere541d012010-04-02 20:21:43 +00001610 Scope));
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001611}
1612
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001613/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1614/// has introduced scope change.
1615void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1616 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1617 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1618 return;
1619 SourceManager &SM = CGM.getContext().getSourceManager();
1620 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1621 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1622
1623 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1624 return;
1625
1626 // If #line directive stack is empty then we are entering a new scope.
1627 if (LineDirectiveFiles.empty()) {
1628 EmitRegionStart(Builder);
1629 LineDirectiveFiles.push_back(PCLoc.getFilename());
1630 return;
1631 }
1632
1633 assert (RegionStack.size() >= LineDirectiveFiles.size()
1634 && "error handling #line regions!");
1635
1636 bool SeenThisFile = false;
Devang Patel424a5c62010-09-15 20:50:40 +00001637 // Chek if current file is already seen earlier.
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001638 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1639 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel424a5c62010-09-15 20:50:40 +00001640 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001641 SeenThisFile = true;
1642 break;
1643 }
1644
1645 // If #line for this file is seen earlier then pop out #line regions.
1646 if (SeenThisFile) {
1647 while (!LineDirectiveFiles.empty()) {
1648 const char *LastFile = LineDirectiveFiles.back();
1649 RegionStack.pop_back();
1650 LineDirectiveFiles.pop_back();
1651 if (!strcmp(PPLoc.getFilename(), LastFile))
1652 break;
1653 }
1654 return;
1655 }
1656
1657 // .. otherwise insert new #line region.
1658 EmitRegionStart(Builder);
1659 LineDirectiveFiles.push_back(PCLoc.getFilename());
1660
1661 return;
1662}
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001663/// EmitRegionStart- Constructs the debug code for entering a declarative
1664/// region - "llvm.dbg.region.start.".
Devang Patel4d939e62010-07-20 22:20:10 +00001665void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patel8fae0602009-11-13 19:10:24 +00001666 llvm::DIDescriptor D =
1667 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1668 llvm::DIDescriptor() :
Devang Pateld19429f2010-02-16 21:41:20 +00001669 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings257d1d32010-07-19 23:56:31 +00001670 getOrCreateFile(CurLoc),
Devang Patel8ab870d2010-05-12 23:46:38 +00001671 getLineNumber(CurLoc),
1672 getColumnNumber(CurLoc));
Devang Patelab699792010-05-07 18:12:35 +00001673 llvm::MDNode *DN = D;
1674 RegionStack.push_back(DN);
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001675}
1676
1677/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1678/// region - "llvm.dbg.region.end."
Devang Patel4d939e62010-07-20 22:20:10 +00001679void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001680 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1681
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001682 // Provide an region stop point.
Devang Patel4d939e62010-07-20 22:20:10 +00001683 EmitStopPoint(Builder);
Mike Stump1eb44332009-09-09 15:08:12 +00001684
Sanjiv Gupta1c6a38b2008-05-25 05:15:42 +00001685 RegionStack.pop_back();
Sanjiv Guptae8b9f5b2008-05-08 08:54:20 +00001686}
1687
Devang Patel5a6fbcf2010-07-22 22:29:16 +00001688/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1689void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1690 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1691 unsigned RCount = FnBeginRegionCount.back();
1692 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1693
1694 // Pop all regions for this function.
1695 while (RegionStack.size() != RCount)
1696 EmitRegionEnd(Builder);
1697 FnBeginRegionCount.pop_back();
1698}
1699
Devang Patel809b9bb2010-02-10 18:49:08 +00001700// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1701// See BuildByRefType.
1702llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1703 uint64_t *XOffset) {
1704
1705 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1706
1707 QualType FType;
1708 uint64_t FieldSize, FieldOffset;
1709 unsigned FieldAlign;
1710
Devang Patel17800552010-03-09 00:44:50 +00001711 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001712 QualType Type = VD->getType();
1713
1714 FieldOffset = 0;
1715 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001716 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1717 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001718 FType = CGM.getContext().IntTy;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001719 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1720 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1721
Devang Patel809b9bb2010-02-10 18:49:08 +00001722 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1723 if (HasCopyAndDispose) {
1724 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001725 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1726 &FieldOffset));
1727 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1728 &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001729 }
1730
1731 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1732 if (Align > CharUnits::fromQuantity(
1733 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1734 unsigned AlignedOffsetInBytes
1735 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1736 unsigned NumPaddingBytes
1737 = AlignedOffsetInBytes - FieldOffset/8;
1738
1739 if (NumPaddingBytes > 0) {
1740 llvm::APInt pad(32, NumPaddingBytes);
1741 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1742 pad, ArrayType::Normal, 0);
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001743 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel809b9bb2010-02-10 18:49:08 +00001744 }
1745 }
1746
1747 FType = Type;
Benjamin Kramer48c70f62010-04-24 20:19:58 +00001748 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel809b9bb2010-02-10 18:49:08 +00001749 FieldSize = CGM.getContext().getTypeSize(FType);
1750 FieldAlign = Align.getQuantity()*8;
1751
1752 *XOffset = FieldOffset;
1753 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Pateld58562e2010-03-09 22:49:11 +00001754 VD->getName(), Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001755 0, FieldSize, FieldAlign,
1756 FieldOffset, 0, FieldTy);
1757 EltTys.push_back(FieldTy);
1758 FieldOffset += FieldSize;
1759
1760 llvm::DIArray Elements =
1761 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1762
Devang Patele2472482010-09-29 21:05:52 +00001763 unsigned Flags = llvm::DIDescriptor::FlagBlockByrefStruct;
Devang Patel809b9bb2010-02-10 18:49:08 +00001764
1765 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Pateld58562e2010-03-09 22:49:11 +00001766 Unit, "", Unit,
Devang Patel809b9bb2010-02-10 18:49:08 +00001767 0, FieldOffset, 0, 0, Flags,
1768 llvm::DIType(), Elements);
1769
1770}
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001771/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel239cec62010-02-01 21:39:52 +00001772void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001773 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar5273f512008-10-17 01:07:56 +00001774 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1775
Devang Patel17800552010-03-09 00:44:50 +00001776 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001777 llvm::DIType Ty;
1778 uint64_t XOffset = 0;
1779 if (VD->hasAttr<BlocksAttr>())
1780 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1781 else
1782 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner650cea92009-05-05 04:57:08 +00001783
Devang Patelf4e54a22010-05-07 23:05:55 +00001784 // If there is not any debug info for type then do not emit debug info
1785 // for this variable.
1786 if (!Ty)
1787 return;
1788
Chris Lattner9c85ba32008-11-10 06:08:34 +00001789 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001790 unsigned Line = getLineNumber(VD->getLocation());
1791 unsigned Column = getColumnNumber(VD->getLocation());
Devang Patelaca745b2010-09-29 23:09:21 +00001792 unsigned Flags = 0;
1793 if (VD->isImplicit())
1794 Flags |= llvm::DIDescriptor::FlagArtificial;
Chris Lattnerc6034632010-04-01 06:31:43 +00001795 llvm::MDNode *Scope = RegionStack.back();
Devang Patelcebbedd2010-10-12 23:24:54 +00001796
1797 llvm::StringRef Name = VD->getName();
1798 if (!Name.empty()) {
1799 // Create the descriptor for the variable.
1800 llvm::DIVariable D =
1801 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1802 Name, Unit, Line, Ty,
1803 CGM.getLangOptions().Optimize, Flags);
1804
1805 // Insert an llvm.dbg.declare into the current block.
1806 llvm::Instruction *Call =
1807 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1808
1809 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1810 }
1811
1812 // If VD is an anonymous union then Storage represents value for
1813 // all union fields.
1814 if (const RecordType *RT = dyn_cast<RecordType>(VD->getType()))
1815 if (const RecordDecl *RD = dyn_cast<RecordDecl>(RT->getDecl()))
1816 if (RD->isUnion()) {
1817 for (RecordDecl::field_iterator I = RD->field_begin(),
1818 E = RD->field_end();
1819 I != E; ++I) {
1820 FieldDecl *Field = *I;
1821 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1822 llvm::StringRef FieldName = Field->getName();
1823
1824 // Ignore unnamed fields. Do not ignore unnamed records.
1825 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
1826 continue;
1827
1828 // Use VarDecl's Tag, Scope and Line number.
1829 llvm::DIVariable D =
1830 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(Scope),
1831 FieldName, Unit, Line, FieldTy,
1832 CGM.getLangOptions().Optimize, Flags);
1833
1834 // Insert an llvm.dbg.declare into the current block.
1835 llvm::Instruction *Call =
1836 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
1837
1838 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
1839 }
1840 }
Sanjiv Guptacc9b1632008-05-30 10:30:31 +00001841}
1842
Mike Stumpb1a6e682009-09-30 02:43:10 +00001843/// EmitDeclare - Emit local variable declaration debug info.
1844void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1845 llvm::Value *Storage, CGBuilderTy &Builder,
1846 CodeGenFunction *CGF) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001847 const ValueDecl *VD = BDRE->getDecl();
Mike Stumpb1a6e682009-09-30 02:43:10 +00001848 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1849
Devang Patel2b594b92010-04-26 23:28:46 +00001850 if (Builder.GetInsertBlock() == 0)
Mike Stumpb1a6e682009-09-30 02:43:10 +00001851 return;
1852
1853 uint64_t XOffset = 0;
Devang Patel17800552010-03-09 00:44:50 +00001854 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel809b9bb2010-02-10 18:49:08 +00001855 llvm::DIType Ty;
1856 if (VD->hasAttr<BlocksAttr>())
1857 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1858 else
1859 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stumpb1a6e682009-09-30 02:43:10 +00001860
1861 // Get location information.
Devang Patel8ab870d2010-05-12 23:46:38 +00001862 unsigned Line = getLineNumber(VD->getLocation());
1863 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001864
Devang Pateld6c5a262010-02-01 21:52:22 +00001865 CharUnits offset = CGF->BlockDecls[VD];
Mike Stumpb1a6e682009-09-30 02:43:10 +00001866 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattner14b1a362010-01-25 03:29:35 +00001867 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1868 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1869 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1870 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001871 if (BDRE->isByRef()) {
Chris Lattner14b1a362010-01-25 03:29:35 +00001872 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1873 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001874 // offset of __forwarding field
1875 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001876 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1877 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1878 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck199c3d62010-01-11 17:06:35 +00001879 // offset of x field
1880 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattner14b1a362010-01-25 03:29:35 +00001881 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001882 }
1883
1884 // Create the descriptor for the variable.
1885 llvm::DIVariable D =
Chris Lattner165714e2010-01-25 03:34:56 +00001886 DebugFactory.CreateComplexVariable(Tag,
1887 llvm::DIDescriptor(RegionStack.back()),
Devang Pateld6c5a262010-02-01 21:52:22 +00001888 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3475cfe2010-09-21 15:59:59 +00001889 addr.data(), addr.size());
Mike Stumpb1a6e682009-09-30 02:43:10 +00001890 // Insert an llvm.dbg.declare into the current block.
Devang Patelebf16e82009-11-11 19:10:19 +00001891 llvm::Instruction *Call =
Chris Lattner165714e2010-01-25 03:34:56 +00001892 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattnerd5b89022009-12-28 21:44:41 +00001893
Chris Lattnerc6034632010-04-01 06:31:43 +00001894 llvm::MDNode *Scope = RegionStack.back();
Devang Patelf8e10a52010-05-10 23:48:38 +00001895 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stumpb1a6e682009-09-30 02:43:10 +00001896}
1897
Devang Pateld6c5a262010-02-01 21:52:22 +00001898void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001899 llvm::Value *Storage,
1900 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001901 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001902}
1903
Mike Stumpb1a6e682009-09-30 02:43:10 +00001904void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1905 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1906 CodeGenFunction *CGF) {
1907 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1908}
1909
Chris Lattner9c85ba32008-11-10 06:08:34 +00001910/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1911/// variable declaration.
Devang Pateld6c5a262010-02-01 21:52:22 +00001912void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattner9c85ba32008-11-10 06:08:34 +00001913 CGBuilderTy &Builder) {
Devang Pateld6c5a262010-02-01 21:52:22 +00001914 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattner9c85ba32008-11-10 06:08:34 +00001915}
1916
1917
1918
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001919/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump1eb44332009-09-09 15:08:12 +00001920void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateleb6d79b2010-02-01 21:34:11 +00001921 const VarDecl *D) {
1922
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001923 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001924 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001925 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner8ec03f52008-11-24 03:54:41 +00001926
Devang Pateleb6d79b2010-02-01 21:34:11 +00001927 QualType T = D->getType();
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001928 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001929
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001930 // CodeGen turns int[] into int[1] so we'll do the same here.
1931 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001932
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001933 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001934 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001935
Anders Carlsson20f12a22009-12-06 18:00:51 +00001936 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlsson4d6e8dd2008-11-26 17:40:42 +00001937 ArrayType::Normal, 0);
1938 }
Devang Patel5d822f02010-04-29 17:48:37 +00001939 llvm::StringRef DeclName = D->getName();
Devang Patel8b90a782010-05-13 23:52:37 +00001940 llvm::StringRef LinkageName;
Devang Patel0fd3d1f2010-05-14 16:55:25 +00001941 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel8b90a782010-05-13 23:52:37 +00001942 LinkageName = Var->getName();
Devang Pateleb6d79b2010-02-01 21:34:11 +00001943 llvm::DIDescriptor DContext =
1944 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel8b90a782010-05-13 23:52:37 +00001945 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1946 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattner9c85ba32008-11-10 06:08:34 +00001947 Var->hasInternalLinkage(),
1948 true/*definition*/, Var);
Sanjiv Gupta686226b2008-06-05 08:59:10 +00001949}
1950
Devang Patel9ca36b62009-02-26 21:10:26 +00001951/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump1eb44332009-09-09 15:08:12 +00001952void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Pateld6c5a262010-02-01 21:52:22 +00001953 ObjCInterfaceDecl *ID) {
Devang Patel9ca36b62009-02-26 21:10:26 +00001954 // Create global variable debug descriptor.
Devang Patel17800552010-03-09 00:44:50 +00001955 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patel8ab870d2010-05-12 23:46:38 +00001956 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patel9ca36b62009-02-26 21:10:26 +00001957
Devang Pateld6c5a262010-02-01 21:52:22 +00001958 llvm::StringRef Name = ID->getName();
Devang Patel9ca36b62009-02-26 21:10:26 +00001959
Devang Pateld6c5a262010-02-01 21:52:22 +00001960 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patel9ca36b62009-02-26 21:10:26 +00001961 if (T->isIncompleteArrayType()) {
Mike Stump1eb44332009-09-09 15:08:12 +00001962
Devang Patel9ca36b62009-02-26 21:10:26 +00001963 // CodeGen turns int[] into int[1] so we'll do the same here.
1964 llvm::APSInt ConstVal(32);
Mike Stump1eb44332009-09-09 15:08:12 +00001965
Devang Patel9ca36b62009-02-26 21:10:26 +00001966 ConstVal = 1;
Anders Carlsson20f12a22009-12-06 18:00:51 +00001967 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump1eb44332009-09-09 15:08:12 +00001968
Anders Carlsson20f12a22009-12-06 18:00:51 +00001969 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patel9ca36b62009-02-26 21:10:26 +00001970 ArrayType::Normal, 0);
1971 }
1972
Devang Patelf6a39b72009-10-20 18:26:30 +00001973 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patel9ca36b62009-02-26 21:10:26 +00001974 getOrCreateType(T, Unit),
1975 Var->hasInternalLinkage(),
1976 true/*definition*/, Var);
1977}
Devang Patelabb485f2010-02-01 19:16:32 +00001978
Devang Patel25c2c8f2010-08-10 17:53:33 +00001979/// EmitGlobalVariable - Emit global variable's debug info.
1980void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
John McCall189d6ef2010-10-09 01:34:31 +00001981 llvm::Constant *Init) {
Devang Patel8d308382010-08-10 07:24:25 +00001982 // Create the descriptor for the variable.
1983 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1984 llvm::StringRef Name = VD->getName();
Devang Patel0317ab02010-08-10 18:27:15 +00001985 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel6237cea2010-08-23 22:07:25 +00001986 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1987 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1988 Ty = CreateEnumType(ED, Unit);
1989 }
Devang Patel0317ab02010-08-10 18:27:15 +00001990 // Do not use DIGlobalVariable for enums.
1991 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1992 return;
Devang Patel8d308382010-08-10 07:24:25 +00001993 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1994 getLineNumber(VD->getLocation()),
Devang Patel4c4acc02010-08-10 20:16:57 +00001995 Ty, true, true, Init);
Devang Patel8d308382010-08-10 07:24:25 +00001996}
1997
Devang Patelabb485f2010-02-01 19:16:32 +00001998/// getOrCreateNamesSpace - Return namespace descriptor for the given
1999/// namespace decl.
2000llvm::DINameSpace
2001CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
2002 llvm::DIDescriptor Unit) {
2003 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
2004 NameSpaceCache.find(NSDecl);
2005 if (I != NameSpaceCache.end())
2006 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
2007
Devang Patel8ab870d2010-05-12 23:46:38 +00002008 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patelabb485f2010-02-01 19:16:32 +00002009
2010 llvm::DIDescriptor Context =
Devang Pateleb6d79b2010-02-01 21:34:11 +00002011 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patelabb485f2010-02-01 19:16:32 +00002012 llvm::DINameSpace NS =
2013 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel2ed8f002010-08-27 17:47:47 +00002014 llvm::DIFile(Unit), LineNo);
Devang Patelab699792010-05-07 18:12:35 +00002015 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patelabb485f2010-02-01 19:16:32 +00002016 return NS;
2017}