blob: 7a7b4e01920da48d0503ce14335f325644e920a0 [file] [log] [blame]
Sanjiv Gupta15cb6692008-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 Stump2e722b92009-09-30 02:43:10 +000015#include "CodeGenFunction.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000016#include "CodeGenModule.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000017#include "clang/AST/ASTContext.h"
Devang Patel96b7f552010-08-27 17:47:47 +000018#include "clang/AST/DeclFriend.h"
Devang Patelf4c205b2009-02-26 21:10:26 +000019#include "clang/AST/DeclObjC.h"
Devang Patel6c018202010-07-20 20:24:18 +000020#include "clang/AST/DeclTemplate.h"
Chris Lattnercd2523b2008-11-11 07:01:36 +000021#include "clang/AST/Expr.h"
Anders Carlsson15b73de2009-07-18 19:43:29 +000022#include "clang/AST/RecordLayout.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000023#include "clang/Basic/SourceManager.h"
24#include "clang/Basic/FileManager.h"
Mike Stumpc3844be2009-09-15 21:48:34 +000025#include "clang/Basic/Version.h"
Chandler Carruth85098242010-06-15 23:19:56 +000026#include "clang/Frontend/CodeGenOptions.h"
Sanjiv Gupta15cb6692008-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 Gupta15cb6692008-05-08 08:54:20 +000032#include "llvm/ADT/StringExtras.h"
33#include "llvm/ADT/SmallVector.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000034#include "llvm/Support/Dwarf.h"
Devang Patel75009452009-04-17 21:06:59 +000035#include "llvm/System/Path.h"
Sanjiv Gupta98070572008-05-25 05:15:42 +000036#include "llvm/Target/TargetMachine.h"
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000037using namespace clang;
38using namespace clang::CodeGen;
39
Anders Carlsson3efc6e62009-12-06 18:00:51 +000040CGDebugInfo::CGDebugInfo(CodeGenModule &CGM)
Devang Patel408dcf62010-03-09 00:44:50 +000041 : CGM(CGM), DebugFactory(CGM.getModule()),
Dan Gohman196f7102010-08-20 22:02:57 +000042 BlockLiteralGenericSet(false) {
Devang Patel408dcf62010-03-09 00:44:50 +000043 CreateCompileUnit();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000044}
45
Chris Lattneraffb3732008-11-10 06:08:34 +000046CGDebugInfo::~CGDebugInfo() {
Daniel Dunbarb9fd9022008-10-17 16:15:48 +000047 assert(RegionStack.empty() && "Region stack mismatch, stack not empty!");
Sanjiv Gupta15cb6692008-05-08 08:54:20 +000048}
49
Chris Lattneraffb3732008-11-10 06:08:34 +000050void CGDebugInfo::setLocation(SourceLocation Loc) {
51 if (Loc.isValid())
Anders Carlsson3efc6e62009-12-06 18:00:51 +000052 CurLoc = CGM.getContext().getSourceManager().getInstantiationLoc(Loc);
Sanjiv Gupta98070572008-05-25 05:15:42 +000053}
54
Devang Patel7bfc5962010-01-28 23:15:27 +000055/// getContextDescriptor - Get context info for the decl.
Devang Patel7b7f46f2010-02-01 21:34:11 +000056llvm::DIDescriptor CGDebugInfo::getContextDescriptor(const Decl *Context,
Devang Patel7bfc5962010-01-28 23:15:27 +000057 llvm::DIDescriptor &CompileUnit) {
Devang Patel7b7f46f2010-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 Greifbf986082010-09-18 13:00:17 +000064 return llvm::DIDescriptor(dyn_cast_or_null<llvm::MDNode>(&*I->second));
Devang Patele8fb4b72010-02-01 22:40:08 +000065
Devang Patel7b7f46f2010-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 Patel98f21712010-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 Patelfaf7e9a2009-10-06 00:35:31 +000077 return CompileUnit;
78}
79
Devang Patel934661e2010-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 Patel0d61eeb2010-01-28 18:21:00 +000093 char *StrPtr = DebugInfoNames.Allocate<char>(NS.length());
Benjamin Kramer8f8f4052010-01-23 18:16:07 +000094 memcpy(StrPtr, NS.data(), NS.length());
95 return llvm::StringRef(StrPtr, NS.length());
Devang Patel934661e2010-01-14 00:36:21 +000096}
97
David Chisnallcf607442010-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 Patel6c018202010-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());
147
148}
149
Devang Patel408dcf62010-03-09 00:44:50 +0000150/// getOrCreateFile - Get the file debug info descriptor for the input location.
151llvm::DIFile CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000152 if (!Loc.isValid())
Devang Patel408dcf62010-03-09 00:44:50 +0000153 // If Location is not valid then use main input file.
154 return DebugFactory.CreateFile(TheCU.getFilename(), TheCU.getDirectory(),
155 TheCU);
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000156 SourceManager &SM = CGM.getContext().getSourceManager();
Devang Patel408dcf62010-03-09 00:44:50 +0000157 PresumedLoc PLoc = SM.getPresumedLoc(Loc);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000158
159 // Cache the results.
160 const char *fname = PLoc.getFilename();
161 llvm::DenseMap<const char *, llvm::WeakVH>::iterator it =
162 DIFileCache.find(fname);
163
164 if (it != DIFileCache.end()) {
165 // Verify that the information still exists.
166 if (&*it->second)
167 return llvm::DIFile(cast<llvm::MDNode>(it->second));
168 }
169
Devang Patel6014edd2010-07-27 15:17:16 +0000170 llvm::DIFile F = DebugFactory.CreateFile(PLoc.getFilename(),
171 getCurrentDirname(), TheCU);
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000172
Devang Patelba4ad7f2010-05-07 18:12:35 +0000173 DIFileCache[fname] = F;
Ted Kremenekdbb8cd12010-03-30 00:27:51 +0000174 return F;
175
Devang Patel408dcf62010-03-09 00:44:50 +0000176}
Devang Patelc5ffabc2010-05-12 23:46:38 +0000177
178/// getLineNumber - Get line number for the location. If location is invalid
179/// then use current location.
180unsigned CGDebugInfo::getLineNumber(SourceLocation Loc) {
181 assert (CurLoc.isValid() && "Invalid current location!");
182 SourceManager &SM = CGM.getContext().getSourceManager();
183 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
184 return PLoc.getLine();
185}
186
187/// getColumnNumber - Get column number for the location. If location is
188/// invalid then use current location.
189unsigned CGDebugInfo::getColumnNumber(SourceLocation Loc) {
190 assert (CurLoc.isValid() && "Invalid current location!");
191 SourceManager &SM = CGM.getContext().getSourceManager();
192 PresumedLoc PLoc = SM.getPresumedLoc(Loc.isValid() ? Loc : CurLoc);
193 return PLoc.getColumn();
194}
195
Devang Patel6014edd2010-07-27 15:17:16 +0000196llvm::StringRef CGDebugInfo::getCurrentDirname() {
197 if (!CWDName.empty())
198 return CWDName;
199 char *CompDirnamePtr = NULL;
200 llvm::sys::Path CWD = llvm::sys::Path::GetCurrentDirectory();
201 CompDirnamePtr = DebugInfoNames.Allocate<char>(CWD.size());
202 memcpy(CompDirnamePtr, CWD.c_str(), CWD.size());
203 return CWDName = llvm::StringRef(CompDirnamePtr, CWD.size());
204}
205
Devang Patel408dcf62010-03-09 00:44:50 +0000206/// CreateCompileUnit - Create new compile unit.
207void CGDebugInfo::CreateCompileUnit() {
208
209 // Get absolute path name.
Douglas Gregorc6b5a3d2010-03-18 23:46:43 +0000210 SourceManager &SM = CGM.getContext().getSourceManager();
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000211 std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
212 if (MainFileName.empty())
Devang Patela42d3ea2010-03-12 21:04:27 +0000213 MainFileName = "<unknown>";
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000214
Douglas Gregor65f7a3f2010-03-22 21:28:29 +0000215 // The main file name provided via the "-main-file-name" option contains just
216 // the file name itself with no path information. This file name may have had
217 // a relative path, so we look into the actual file entry for the main
218 // file to determine the real absolute path for the file.
Devang Patelcb9fe9e2010-07-23 23:04:28 +0000219 std::string MainFileDir;
Devang Patel6014edd2010-07-27 15:17:16 +0000220 if (const FileEntry *MainFile = SM.getFileEntryForID(SM.getMainFileID())) {
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000221 MainFileDir = MainFile->getDir()->getName();
Devang Patel6014edd2010-07-27 15:17:16 +0000222 if (MainFileDir != ".")
223 MainFileName = MainFileDir + "/" + MainFileName;
224 }
Douglas Gregorfc7a4812010-03-19 14:49:09 +0000225
Devang Patel6014edd2010-07-27 15:17:16 +0000226 // Save filename string.
227 char *FilenamePtr = DebugInfoNames.Allocate<char>(MainFileName.length());
228 memcpy(FilenamePtr, MainFileName.c_str(), MainFileName.length());
229 llvm::StringRef Filename(FilenamePtr, MainFileName.length());
230
Chris Lattner8c37df42009-03-25 03:28:08 +0000231 unsigned LangTag;
Devang Patel408dcf62010-03-09 00:44:50 +0000232 const LangOptions &LO = CGM.getLangOptions();
Chris Lattner8c37df42009-03-25 03:28:08 +0000233 if (LO.CPlusPlus) {
234 if (LO.ObjC1)
235 LangTag = llvm::dwarf::DW_LANG_ObjC_plus_plus;
236 else
237 LangTag = llvm::dwarf::DW_LANG_C_plus_plus;
238 } else if (LO.ObjC1) {
Devang Patel94406c92009-03-24 20:35:51 +0000239 LangTag = llvm::dwarf::DW_LANG_ObjC;
Chris Lattner8c37df42009-03-25 03:28:08 +0000240 } else if (LO.C99) {
Devang Patel94406c92009-03-24 20:35:51 +0000241 LangTag = llvm::dwarf::DW_LANG_C99;
Chris Lattner8c37df42009-03-25 03:28:08 +0000242 } else {
243 LangTag = llvm::dwarf::DW_LANG_C89;
244 }
Devang Patel75009452009-04-17 21:06:59 +0000245
Daniel Dunbar64c222a2010-08-24 17:41:09 +0000246 std::string Producer = getClangFullVersion();
Chris Lattner5912de12009-05-02 01:00:04 +0000247
248 // Figure out which version of the ObjC runtime we have.
249 unsigned RuntimeVers = 0;
250 if (LO.ObjC1)
251 RuntimeVers = LO.ObjCNonFragileABI ? 2 : 1;
Mike Stump11289f42009-09-09 15:08:12 +0000252
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000253 // Create new compile unit.
Devang Patel408dcf62010-03-09 00:44:50 +0000254 TheCU = DebugFactory.CreateCompileUnit(
Devang Patel4f6e73b2010-07-27 20:49:59 +0000255 LangTag, Filename, getCurrentDirname(),
Devang Patel6014edd2010-07-27 15:17:16 +0000256 Producer, true,
Daniel Dunbar24c7f5e2009-12-18 02:43:17 +0000257 LO.Optimize, CGM.getCodeGenOpts().DwarfDebugFlags, RuntimeVers);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000258}
259
Devang Patel410dc002009-02-25 01:36:11 +0000260/// CreateType - Get the Basic type from the cache or create a new
Chris Lattneraffb3732008-11-10 06:08:34 +0000261/// one if necessary.
262llvm::DIType CGDebugInfo::CreateType(const BuiltinType *BT,
Devang Patel408dcf62010-03-09 00:44:50 +0000263 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000264 unsigned Encoding = 0;
Devang Patelc7f16ab2010-07-28 23:23:29 +0000265 const char *BTName = NULL;
Chris Lattneraffb3732008-11-10 06:08:34 +0000266 switch (BT->getKind()) {
267 default:
268 case BuiltinType::Void:
269 return llvm::DIType();
Devang Patela652fab2010-07-28 01:33:15 +0000270 case BuiltinType::ObjCClass:
Devang Patel222f4be2010-07-21 22:41:25 +0000271 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patela652fab2010-07-28 01:33:15 +0000272 Unit, "objc_class", Unit, 0, 0, 0, 0,
Devang Patel222f4be2010-07-21 22:41:25 +0000273 llvm::DIType::FlagFwdDecl,
274 llvm::DIType(), llvm::DIArray());
Devang Patela652fab2010-07-28 01:33:15 +0000275 case BuiltinType::ObjCId: {
276 // typedef struct objc_class *Class;
277 // typedef struct objc_object {
278 // Class isa;
279 // } *id;
280
281 llvm::DIType OCTy =
282 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
283 Unit, "objc_class", Unit, 0, 0, 0, 0,
284 llvm::DIType::FlagFwdDecl,
285 llvm::DIType(), llvm::DIArray());
286 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
287
288 llvm::DIType ISATy =
289 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
290 Unit, "", Unit,
291 0, Size, 0, 0, 0, OCTy);
292
293 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
294
295 llvm::DIType FieldTy =
296 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
297 "isa", Unit,
298 0,Size, 0, 0, 0, ISATy);
299 EltTys.push_back(FieldTy);
300 llvm::DIArray Elements =
301 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
302
303 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
304 Unit, "objc_object", Unit, 0, 0, 0, 0,
305 0,
306 llvm::DIType(), Elements);
307 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000308 case BuiltinType::UChar:
309 case BuiltinType::Char_U: Encoding = llvm::dwarf::DW_ATE_unsigned_char; break;
310 case BuiltinType::Char_S:
311 case BuiltinType::SChar: Encoding = llvm::dwarf::DW_ATE_signed_char; break;
312 case BuiltinType::UShort:
313 case BuiltinType::UInt:
314 case BuiltinType::ULong:
315 case BuiltinType::ULongLong: Encoding = llvm::dwarf::DW_ATE_unsigned; break;
316 case BuiltinType::Short:
317 case BuiltinType::Int:
318 case BuiltinType::Long:
319 case BuiltinType::LongLong: Encoding = llvm::dwarf::DW_ATE_signed; break;
320 case BuiltinType::Bool: Encoding = llvm::dwarf::DW_ATE_boolean; break;
321 case BuiltinType::Float:
Devang Patel551e1122009-10-12 22:28:31 +0000322 case BuiltinType::LongDouble:
Chris Lattneraffb3732008-11-10 06:08:34 +0000323 case BuiltinType::Double: Encoding = llvm::dwarf::DW_ATE_float; break;
Mike Stump11289f42009-09-09 15:08:12 +0000324 }
Devang Patelc7f16ab2010-07-28 23:23:29 +0000325
326 switch (BT->getKind()) {
327 case BuiltinType::Long: BTName = "long int"; break;
328 case BuiltinType::LongLong: BTName = "long long int"; break;
329 case BuiltinType::ULong: BTName = "long unsigned int"; break;
330 case BuiltinType::ULongLong: BTName = "long long unsigned int"; break;
331 default:
332 BTName = BT->getName(CGM.getContext().getLangOptions());
333 break;
334 }
Chris Lattneraffb3732008-11-10 06:08:34 +0000335 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000336 uint64_t Size = CGM.getContext().getTypeSize(BT);
337 uint64_t Align = CGM.getContext().getTypeAlign(BT);
Chris Lattneraffb3732008-11-10 06:08:34 +0000338 uint64_t Offset = 0;
Devang Patelc7f16ab2010-07-28 23:23:29 +0000339
Devang Patele21912d2009-10-20 19:55:01 +0000340 llvm::DIType DbgTy =
Devang Patelc7f16ab2010-07-28 23:23:29 +0000341 DebugFactory.CreateBasicType(Unit, BTName,
Devang Patele21912d2009-10-20 19:55:01 +0000342 Unit, 0, Size, Align,
343 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000344 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +0000345}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +0000346
Chris Lattner7b0344f2009-04-23 06:13:01 +0000347llvm::DIType CGDebugInfo::CreateType(const ComplexType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000348 llvm::DIFile Unit) {
Chris Lattner7b0344f2009-04-23 06:13:01 +0000349 // Bit size, align and offset of the type.
350 unsigned Encoding = llvm::dwarf::DW_ATE_complex_float;
351 if (Ty->isComplexIntegerType())
352 Encoding = llvm::dwarf::DW_ATE_lo_user;
Mike Stump11289f42009-09-09 15:08:12 +0000353
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000354 uint64_t Size = CGM.getContext().getTypeSize(Ty);
355 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Chris Lattner7b0344f2009-04-23 06:13:01 +0000356 uint64_t Offset = 0;
Mike Stump11289f42009-09-09 15:08:12 +0000357
Devang Patele21912d2009-10-20 19:55:01 +0000358 llvm::DIType DbgTy =
359 DebugFactory.CreateBasicType(Unit, "complex",
360 Unit, 0, Size, Align,
361 Offset, /*flags*/ 0, Encoding);
Devang Patele21912d2009-10-20 19:55:01 +0000362 return DbgTy;
Chris Lattner7b0344f2009-04-23 06:13:01 +0000363}
364
John McCall0cf15512009-09-25 01:40:47 +0000365/// CreateCVRType - Get the qualified type from the cache or create
Sanjiv Gupta19292422008-06-07 04:46:53 +0000366/// a new one if necessary.
Devang Patel408dcf62010-03-09 00:44:50 +0000367llvm::DIType CGDebugInfo::CreateQualifiedType(QualType Ty, llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +0000368 QualifierCollector Qc;
369 const Type *T = Qc.strip(Ty);
370
371 // Ignore these qualifiers for now.
372 Qc.removeObjCGCAttr();
373 Qc.removeAddressSpace();
374
Chris Lattneraffb3732008-11-10 06:08:34 +0000375 // We will create one Derived type for one qualifier and recurse to handle any
376 // additional ones.
Chris Lattneraffb3732008-11-10 06:08:34 +0000377 unsigned Tag;
John McCall0cf15512009-09-25 01:40:47 +0000378 if (Qc.hasConst()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000379 Tag = llvm::dwarf::DW_TAG_const_type;
John McCall0cf15512009-09-25 01:40:47 +0000380 Qc.removeConst();
381 } else if (Qc.hasVolatile()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000382 Tag = llvm::dwarf::DW_TAG_volatile_type;
John McCall0cf15512009-09-25 01:40:47 +0000383 Qc.removeVolatile();
384 } else if (Qc.hasRestrict()) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000385 Tag = llvm::dwarf::DW_TAG_restrict_type;
John McCall0cf15512009-09-25 01:40:47 +0000386 Qc.removeRestrict();
387 } else {
388 assert(Qc.empty() && "Unknown type qualifier for debug info");
389 return getOrCreateType(QualType(T, 0), Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +0000390 }
Mike Stump11289f42009-09-09 15:08:12 +0000391
John McCall0cf15512009-09-25 01:40:47 +0000392 llvm::DIType FromTy = getOrCreateType(Qc.apply(T), Unit);
393
Daniel Dunbara290ded2008-10-31 03:54:29 +0000394 // No need to fill in the Name, Line, Size, Alignment, Offset in case of
395 // CVR derived types.
Devang Patele21912d2009-10-20 19:55:01 +0000396 llvm::DIType DbgTy =
Devang Patel93f274c2010-03-09 22:49:11 +0000397 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000398 0, 0, 0, 0, 0, FromTy);
Devang Patele21912d2009-10-20 19:55:01 +0000399 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000400}
401
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000402llvm::DIType CGDebugInfo::CreateType(const ObjCObjectPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000403 llvm::DIFile Unit) {
Devang Patele21912d2009-10-20 19:55:01 +0000404 llvm::DIType DbgTy =
Anders Carlsson443f6772009-11-06 19:19:55 +0000405 CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
406 Ty->getPointeeType(), Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000407 return DbgTy;
Daniel Dunbarf5c79702009-07-14 01:20:56 +0000408}
409
Chris Lattneraffb3732008-11-10 06:08:34 +0000410llvm::DIType CGDebugInfo::CreateType(const PointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000411 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000412 return CreatePointerLikeType(llvm::dwarf::DW_TAG_pointer_type, Ty,
413 Ty->getPointeeType(), Unit);
414}
415
416llvm::DIType CGDebugInfo::CreatePointerLikeType(unsigned Tag,
417 const Type *Ty,
418 QualType PointeeTy,
Devang Patel408dcf62010-03-09 00:44:50 +0000419 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +0000420 llvm::DIType EltTy = getOrCreateType(PointeeTy, Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000421
Sanjiv Gupta98070572008-05-25 05:15:42 +0000422 // Bit size, align and offset of the type.
Anders Carlsson443f6772009-11-06 19:19:55 +0000423
424 // Size is always the size of a pointer. We can't use getTypeSize here
425 // because that does not return the correct value for references.
426 uint64_t Size =
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000427 CGM.getContext().Target.getPointerWidth(PointeeTy.getAddressSpace());
428 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000429
Devang Patele21912d2009-10-20 19:55:01 +0000430 return
Devang Patel93f274c2010-03-09 22:49:11 +0000431 DebugFactory.CreateDerivedType(Tag, Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000432 0, Size, Align, 0, 0, EltTy);
Anders Carlsson443f6772009-11-06 19:19:55 +0000433
Sanjiv Gupta98070572008-05-25 05:15:42 +0000434}
435
Mike Stump31f099c2009-05-14 02:03:51 +0000436llvm::DIType CGDebugInfo::CreateType(const BlockPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000437 llvm::DIFile Unit) {
Mike Stump31f099c2009-05-14 02:03:51 +0000438 if (BlockLiteralGenericSet)
439 return BlockLiteralGeneric;
440
Mike Stump31f099c2009-05-14 02:03:51 +0000441 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
442
443 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
444
445 llvm::DIType FieldTy;
446
447 QualType FType;
448 uint64_t FieldSize, FieldOffset;
449 unsigned FieldAlign;
450
451 llvm::DIArray Elements;
452 llvm::DIType EltTy, DescTy;
453
454 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000455 FType = CGM.getContext().UnsignedLongTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000456 EltTys.push_back(CreateMemberType(Unit, FType, "reserved", &FieldOffset));
457 EltTys.push_back(CreateMemberType(Unit, FType, "Size", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000458
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000459 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000460 EltTys.clear();
461
Mike Stump581b9ad2009-10-02 02:30:50 +0000462 unsigned Flags = llvm::DIType::FlagAppleBlock;
Devang Patelc5ffabc2010-05-12 23:46:38 +0000463 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump581b9ad2009-10-02 02:30:50 +0000464
Mike Stump31f099c2009-05-14 02:03:51 +0000465 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_descriptor",
Devang Patelc5ffabc2010-05-12 23:46:38 +0000466 Unit, LineNo, FieldOffset, 0, 0,
467 Flags, llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000468
Mike Stump31f099c2009-05-14 02:03:51 +0000469 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000470 uint64_t Size = CGM.getContext().getTypeSize(Ty);
471 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000472
Mike Stump31f099c2009-05-14 02:03:51 +0000473 DescTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000474 Unit, "", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000475 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000476
477 FieldOffset = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000478 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000479 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000480 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000481 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
482 EltTys.push_back(CreateMemberType(Unit, FType, "__reserved", &FieldOffset));
Benjamin Kramer20f2d432010-04-24 20:26:20 +0000483 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +0000484 EltTys.push_back(CreateMemberType(Unit, FType, "__FuncPtr", &FieldOffset));
Mike Stump31f099c2009-05-14 02:03:51 +0000485
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000486 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000487 FieldTy = DescTy;
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000488 FieldSize = CGM.getContext().getTypeSize(Ty);
489 FieldAlign = CGM.getContext().getTypeAlign(Ty);
Mike Stump31f099c2009-05-14 02:03:51 +0000490 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000491 "__descriptor", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000492 LineNo, FieldSize, FieldAlign,
Mike Stump31f099c2009-05-14 02:03:51 +0000493 FieldOffset, 0, FieldTy);
494 EltTys.push_back(FieldTy);
495
496 FieldOffset += FieldSize;
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000497 Elements = DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump31f099c2009-05-14 02:03:51 +0000498
499 EltTy = DebugFactory.CreateCompositeType(Tag, Unit, "__block_literal_generic",
Devang Patelc5ffabc2010-05-12 23:46:38 +0000500 Unit, LineNo, FieldOffset, 0, 0,
501 Flags, llvm::DIType(), Elements);
Mike Stump11289f42009-09-09 15:08:12 +0000502
Mike Stump31f099c2009-05-14 02:03:51 +0000503 BlockLiteralGenericSet = true;
504 BlockLiteralGeneric
505 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +0000506 "", Unit,
Devang Patelc5ffabc2010-05-12 23:46:38 +0000507 LineNo, Size, Align, 0, 0, EltTy);
Mike Stump31f099c2009-05-14 02:03:51 +0000508 return BlockLiteralGeneric;
509}
510
Chris Lattneraffb3732008-11-10 06:08:34 +0000511llvm::DIType CGDebugInfo::CreateType(const TypedefType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000512 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000513 // Typedefs are derived from some other type. If we have a typedef of a
514 // typedef, make sure to emit the whole chain.
515 llvm::DIType Src = getOrCreateType(Ty->getDecl()->getUnderlyingType(), Unit);
Mike Stump11289f42009-09-09 15:08:12 +0000516
Chris Lattneraffb3732008-11-10 06:08:34 +0000517 // We don't set size information, but do specify where the typedef was
518 // declared.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000519 unsigned Line = getLineNumber(Ty->getDecl()->getLocation());
Sanjiv Gupta98070572008-05-25 05:15:42 +0000520
Devang Patel7b7f46f2010-02-01 21:34:11 +0000521 llvm::DIDescriptor TyContext
522 = getContextDescriptor(dyn_cast<Decl>(Ty->getDecl()->getDeclContext()),
523 Unit);
Devang Patele21912d2009-10-20 19:55:01 +0000524 llvm::DIType DbgTy =
Devang Patelbb4820d2010-01-29 22:29:31 +0000525 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_typedef,
Devang Patel7b7f46f2010-02-01 21:34:11 +0000526 TyContext,
Devang Patelbb4820d2010-01-29 22:29:31 +0000527 Ty->getDecl()->getName(), Unit,
528 Line, 0, 0, 0, 0, Src);
Devang Patele21912d2009-10-20 19:55:01 +0000529 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000530}
531
Chris Lattneraffb3732008-11-10 06:08:34 +0000532llvm::DIType CGDebugInfo::CreateType(const FunctionType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000533 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000534 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000535
Chris Lattneraffb3732008-11-10 06:08:34 +0000536 // Add the result type at least.
537 EltTys.push_back(getOrCreateType(Ty->getResultType(), Unit));
Mike Stump11289f42009-09-09 15:08:12 +0000538
Chris Lattneraffb3732008-11-10 06:08:34 +0000539 // Set up remainder of arguments if there is a prototype.
540 // FIXME: IF NOT, HOW IS THIS REPRESENTED? llvm-gcc doesn't represent '...'!
Douglas Gregordeaad8c2009-02-26 23:50:07 +0000541 if (const FunctionProtoType *FTP = dyn_cast<FunctionProtoType>(Ty)) {
Chris Lattneraffb3732008-11-10 06:08:34 +0000542 for (unsigned i = 0, e = FTP->getNumArgs(); i != e; ++i)
543 EltTys.push_back(getOrCreateType(FTP->getArgType(i), Unit));
544 } else {
545 // FIXME: Handle () case in C. llvm-gcc doesn't do it either.
Sanjiv Gupta98070572008-05-25 05:15:42 +0000546 }
547
Chris Lattneraffb3732008-11-10 06:08:34 +0000548 llvm::DIArray EltTypeArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000549 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Mike Stump11289f42009-09-09 15:08:12 +0000550
Devang Patele21912d2009-10-20 19:55:01 +0000551 llvm::DIType DbgTy =
552 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000553 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +0000554 0, 0, 0, 0, 0,
555 llvm::DIType(), EltTypeArray);
Devang Patele21912d2009-10-20 19:55:01 +0000556 return DbgTy;
Sanjiv Gupta98070572008-05-25 05:15:42 +0000557}
558
Devang Patel889ce762010-01-19 00:00:59 +0000559/// CollectRecordFields - A helper function to collect debug info for
560/// record fields. This is used while creating debug info entry for a Record.
561void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000562CollectRecordFields(const RecordDecl *RD, llvm::DIFile Unit,
Devang Patel889ce762010-01-19 00:00:59 +0000563 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
564 unsigned FieldNo = 0;
Devang Patel1c0954c2010-02-01 21:39:52 +0000565 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
566 for (RecordDecl::field_iterator I = RD->field_begin(),
567 E = RD->field_end();
Devang Patel889ce762010-01-19 00:00:59 +0000568 I != E; ++I, ++FieldNo) {
569 FieldDecl *Field = *I;
570 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
Devang Patel889ce762010-01-19 00:00:59 +0000571 llvm::StringRef FieldName = Field->getName();
572
Devang Patelf4df65c2010-02-12 01:31:06 +0000573 // Ignore unnamed fields. Do not ignore unnamed records.
574 if (FieldName.empty() && !isa<RecordType>(Field->getType()))
Devang Patel889ce762010-01-19 00:00:59 +0000575 continue;
576
577 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000578 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
579 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel889ce762010-01-19 00:00:59 +0000580 QualType FType = Field->getType();
581 uint64_t FieldSize = 0;
582 unsigned FieldAlign = 0;
583 if (!FType->isIncompleteArrayType()) {
584
585 // Bit size, align and offset of the type.
586 FieldSize = CGM.getContext().getTypeSize(FType);
587 Expr *BitWidth = Field->getBitWidth();
588 if (BitWidth)
589 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Devang Patel889ce762010-01-19 00:00:59 +0000590 FieldAlign = CGM.getContext().getTypeAlign(FType);
591 }
592
593 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
594
Devang Patelb9ab3092010-04-21 23:12:37 +0000595 unsigned Flags = 0;
596 AccessSpecifier Access = I->getAccess();
597 if (Access == clang::AS_private)
598 Flags |= llvm::DIType::FlagPrivate;
599 else if (Access == clang::AS_protected)
600 Flags |= llvm::DIType::FlagProtected;
601
Devang Patel889ce762010-01-19 00:00:59 +0000602 // Create a DW_TAG_member node to remember the offset of this field in the
603 // struct. FIXME: This is an absolutely insane way to capture this
604 // information. When we gut debug info, this should be fixed.
605 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
606 FieldName, FieldDefUnit,
607 FieldLine, FieldSize, FieldAlign,
Devang Patelb9ab3092010-04-21 23:12:37 +0000608 FieldOffset, Flags, FieldTy);
Devang Patel889ce762010-01-19 00:00:59 +0000609 EltTys.push_back(FieldTy);
610 }
611}
612
Devang Patel3d4e6d92010-01-28 00:28:01 +0000613/// getOrCreateMethodType - CXXMethodDecl's type is a FunctionType. This
614/// function type is not updated to include implicit "this" pointer. Use this
615/// routine to get a method type which includes "this" pointer.
616llvm::DIType
617CGDebugInfo::getOrCreateMethodType(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000618 llvm::DIFile Unit) {
Douglas Gregorc8be9522010-05-04 18:18:31 +0000619 llvm::DIType FnTy
620 = getOrCreateType(QualType(Method->getType()->getAs<FunctionProtoType>(),
621 0),
622 Unit);
Devang Patel4c3e7e92010-01-28 21:43:50 +0000623
Devang Patel3d4e6d92010-01-28 00:28:01 +0000624 // Add "this" pointer.
625
Devang Patelba4ad7f2010-05-07 18:12:35 +0000626 llvm::DIArray Args = llvm::DICompositeType(FnTy).getTypeArray();
Devang Patel3d4e6d92010-01-28 00:28:01 +0000627 assert (Args.getNumElements() && "Invalid number of arguments!");
628
629 llvm::SmallVector<llvm::DIDescriptor, 16> Elts;
630
631 // First element is always return type. For 'void' functions it is NULL.
632 Elts.push_back(Args.getElement(0));
633
Devang Patel96b7f552010-08-27 17:47:47 +0000634 if (!Method->isStatic())
635 {
636 // "this" pointer is always first argument.
637 ASTContext &Context = CGM.getContext();
638 QualType ThisPtr =
639 Context.getPointerType(Context.getTagDeclType(Method->getParent()));
640 llvm::DIType ThisPtrType =
641 DebugFactory.CreateArtificialType(getOrCreateType(ThisPtr, Unit));
Devang Patel0a34e312010-07-13 00:24:30 +0000642
Devang Patel96b7f552010-08-27 17:47:47 +0000643 TypeCache[ThisPtr.getAsOpaquePtr()] = ThisPtrType;
644 Elts.push_back(ThisPtrType);
645 }
Devang Patel3d4e6d92010-01-28 00:28:01 +0000646
647 // Copy rest of the arguments.
648 for (unsigned i = 1, e = Args.getNumElements(); i != e; ++i)
649 Elts.push_back(Args.getElement(i));
650
651 llvm::DIArray EltTypeArray =
652 DebugFactory.GetOrCreateArray(Elts.data(), Elts.size());
653
654 return
655 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000656 Unit, "", Unit,
Devang Patel3d4e6d92010-01-28 00:28:01 +0000657 0, 0, 0, 0, 0,
658 llvm::DIType(), EltTypeArray);
659}
660
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000661/// CreateCXXMemberFunction - A helper function to create a DISubprogram for
662/// a single member function GlobalDecl.
663llvm::DISubprogram
Anders Carlsson17ed0492010-01-26 05:19:50 +0000664CGDebugInfo::CreateCXXMemberFunction(const CXXMethodDecl *Method,
Devang Patel408dcf62010-03-09 00:44:50 +0000665 llvm::DIFile Unit,
Dan Gohman196f7102010-08-20 22:02:57 +0000666 llvm::DIType RecordTy) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000667 bool IsCtorOrDtor =
668 isa<CXXConstructorDecl>(Method) || isa<CXXDestructorDecl>(Method);
669
670 llvm::StringRef MethodName = getFunctionName(Method);
Devang Patel3d4e6d92010-01-28 00:28:01 +0000671 llvm::DIType MethodTy = getOrCreateMethodType(Method, Unit);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000672
673 // Since a single ctor/dtor corresponds to multiple functions, it doesn't
674 // make sense to give a single ctor/dtor a linkage name.
Anders Carlssonea836bc2010-06-22 16:16:50 +0000675 llvm::StringRef MethodLinkageName;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000676 if (!IsCtorOrDtor)
Anders Carlssonea836bc2010-06-22 16:16:50 +0000677 MethodLinkageName = CGM.getMangledName(Method);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000678
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000679 // Get the location for the method.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000680 llvm::DIFile MethodDefUnit = getOrCreateFile(Method->getLocation());
681 unsigned MethodLine = getLineNumber(Method->getLocation());
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000682
683 // Collect virtual method info.
684 llvm::DIType ContainingType;
685 unsigned Virtuality = 0;
686 unsigned VIndex = 0;
Anders Carlsson17ed0492010-01-26 05:19:50 +0000687
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000688 if (Method->isVirtual()) {
Anders Carlsson17ed0492010-01-26 05:19:50 +0000689 if (Method->isPure())
690 Virtuality = llvm::dwarf::DW_VIRTUALITY_pure_virtual;
691 else
692 Virtuality = llvm::dwarf::DW_VIRTUALITY_virtual;
693
694 // It doesn't make sense to give a virtual destructor a vtable index,
695 // since a single destructor has two entries in the vtable.
696 if (!isa<CXXDestructorDecl>(Method))
Anders Carlsson11e51402010-04-17 20:15:18 +0000697 VIndex = CGM.getVTables().getMethodVTableIndex(Method);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000698 ContainingType = RecordTy;
699 }
700
701 llvm::DISubprogram SP =
702 DebugFactory.CreateSubprogram(RecordTy , MethodName, MethodName,
703 MethodLinkageName,
704 MethodDefUnit, MethodLine,
705 MethodTy, /*isLocalToUnit=*/false,
Devang Patel0aabb122010-07-12 22:54:41 +0000706 /* isDefintion=*/ false,
Devang Patelb3026df2010-07-15 22:57:00 +0000707 Virtuality, VIndex, ContainingType,
Devang Patel8fd64992010-07-15 23:09:46 +0000708 Method->isImplicit(),
709 CGM.getLangOptions().Optimize);
Anders Carlsson17ed0492010-01-26 05:19:50 +0000710
711 // Don't cache ctors or dtors since we have to emit multiple functions for
712 // a single ctor or dtor.
713 if (!IsCtorOrDtor && Method->isThisDeclarationADefinition())
Devang Patelba4ad7f2010-05-07 18:12:35 +0000714 SPCache[Method] = llvm::WeakVH(SP);
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000715
716 return SP;
717}
718
Devang Patel7a12ad02010-01-19 01:54:44 +0000719/// CollectCXXMemberFunctions - A helper function to collect debug info for
720/// C++ member functions.This is used while creating debug info entry for
721/// a Record.
722void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000723CollectCXXMemberFunctions(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel7a12ad02010-01-19 01:54:44 +0000724 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000725 llvm::DIType RecordTy) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000726 for(CXXRecordDecl::method_iterator I = RD->method_begin(),
727 E = RD->method_end(); I != E; ++I) {
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000728 const CXXMethodDecl *Method = *I;
Anders Carlssonc1821152010-01-26 04:40:11 +0000729
Devang Patel0ae70d12010-02-09 19:09:28 +0000730 if (Method->isImplicit() && !Method->isUsed())
Anders Carlssonc1821152010-01-26 04:40:11 +0000731 continue;
Devang Patel7a12ad02010-01-19 01:54:44 +0000732
Anders Carlssonb85f0ab2010-01-26 04:49:33 +0000733 EltTys.push_back(CreateCXXMemberFunction(Method, Unit, RecordTy));
Devang Patel7a12ad02010-01-19 01:54:44 +0000734 }
735}
736
Devang Patel96b7f552010-08-27 17:47:47 +0000737/// CollectCXXFriends - A helper function to collect debug info for
738/// C++ base classes. This is used while creating debug info entry for
739/// a Record.
740void CGDebugInfo::
741CollectCXXFriends(const CXXRecordDecl *RD, llvm::DIFile Unit,
742 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
743 llvm::DIType RecordTy) {
744
745 for (CXXRecordDecl::friend_iterator BI = RD->friend_begin(),
746 BE = RD->friend_end(); BI != BE; ++BI) {
747
748 TypeSourceInfo *TInfo = (*BI)->getFriendType();
749 if(TInfo)
750 {
751 llvm::DIType Ty = getOrCreateType(TInfo->getType(), Unit);
752
753 llvm::DIType DTy =
754 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_friend,
755 RecordTy, llvm::StringRef(),
756 Unit, 0, 0, 0,
757 0, 0, Ty);
758
759 EltTys.push_back(DTy);
760 }
761
762 }
763}
764
Devang Patelc54353d2010-01-25 23:32:18 +0000765/// CollectCXXBases - A helper function to collect debug info for
766/// C++ base classes. This is used while creating debug info entry for
767/// a Record.
768void CGDebugInfo::
Devang Patel408dcf62010-03-09 00:44:50 +0000769CollectCXXBases(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patelc54353d2010-01-25 23:32:18 +0000770 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys,
Dan Gohman196f7102010-08-20 22:02:57 +0000771 llvm::DIType RecordTy) {
Devang Patelc54353d2010-01-25 23:32:18 +0000772
Devang Patel1c0954c2010-02-01 21:39:52 +0000773 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
774 for (CXXRecordDecl::base_class_const_iterator BI = RD->bases_begin(),
775 BE = RD->bases_end(); BI != BE; ++BI) {
Devang Patel128aa9d2010-01-28 21:54:15 +0000776 unsigned BFlags = 0;
777 uint64_t BaseOffset;
778
779 const CXXRecordDecl *Base =
780 cast<CXXRecordDecl>(BI->getType()->getAs<RecordType>()->getDecl());
781
782 if (BI->isVirtual()) {
Anders Carlsson4cbe83c2010-03-11 07:15:17 +0000783 // virtual base offset offset is -ve. The code generator emits dwarf
Devang Patel0ae70d12010-02-09 19:09:28 +0000784 // expression where it expects +ve number.
Anders Carlssona864caf2010-03-23 04:11:45 +0000785 BaseOffset = 0 - CGM.getVTables().getVirtualBaseOffsetOffset(RD, Base);
Devang Patel128aa9d2010-01-28 21:54:15 +0000786 BFlags = llvm::DIType::FlagVirtual;
787 } else
788 BaseOffset = RL.getBaseClassOffset(Base);
789
790 AccessSpecifier Access = BI->getAccessSpecifier();
791 if (Access == clang::AS_private)
792 BFlags |= llvm::DIType::FlagPrivate;
793 else if (Access == clang::AS_protected)
794 BFlags |= llvm::DIType::FlagProtected;
795
796 llvm::DIType DTy =
797 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
798 RecordTy, llvm::StringRef(),
Devang Patel93f274c2010-03-09 22:49:11 +0000799 Unit, 0, 0, 0,
Devang Patel128aa9d2010-01-28 21:54:15 +0000800 BaseOffset, BFlags,
801 getOrCreateType(BI->getType(),
802 Unit));
803 EltTys.push_back(DTy);
804 }
Devang Patelc54353d2010-01-25 23:32:18 +0000805}
806
Devang Patel84033fb2010-01-28 18:11:52 +0000807/// getOrCreateVTablePtrType - Return debug info descriptor for vtable.
Devang Patel408dcf62010-03-09 00:44:50 +0000808llvm::DIType CGDebugInfo::getOrCreateVTablePtrType(llvm::DIFile Unit) {
Devang Pateld3cbaa12010-03-08 20:53:17 +0000809 if (VTablePtrType.isValid())
Devang Patel84033fb2010-01-28 18:11:52 +0000810 return VTablePtrType;
811
812 ASTContext &Context = CGM.getContext();
813
814 /* Function type */
Benjamin Kramer9e649c32010-03-30 11:36:44 +0000815 llvm::DIDescriptor STy = getOrCreateType(Context.IntTy, Unit);
816 llvm::DIArray SElements = DebugFactory.GetOrCreateArray(&STy, 1);
Devang Patel84033fb2010-01-28 18:11:52 +0000817 llvm::DIType SubTy =
818 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_subroutine_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000819 Unit, "", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000820 0, 0, 0, 0, 0, llvm::DIType(), SElements);
821
822 unsigned Size = Context.getTypeSize(Context.VoidPtrTy);
823 llvm::DIType vtbl_ptr_type
824 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
Devang Patel93f274c2010-03-09 22:49:11 +0000825 Unit, "__vtbl_ptr_type", Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000826 0, Size, 0, 0, 0, SubTy);
827
Devang Patel93f274c2010-03-09 22:49:11 +0000828 VTablePtrType =
829 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_pointer_type,
830 Unit, "", Unit,
831 0, Size, 0, 0, 0, vtbl_ptr_type);
Devang Patel84033fb2010-01-28 18:11:52 +0000832 return VTablePtrType;
833}
834
Anders Carlsson11e51402010-04-17 20:15:18 +0000835/// getVTableName - Get vtable name for the given Class.
836llvm::StringRef CGDebugInfo::getVTableName(const CXXRecordDecl *RD) {
Devang Patel84033fb2010-01-28 18:11:52 +0000837 // Otherwise construct gdb compatible name name.
Devang Patel1c0954c2010-02-01 21:39:52 +0000838 std::string Name = "_vptr$" + RD->getNameAsString();
Devang Patel84033fb2010-01-28 18:11:52 +0000839
840 // Copy this name on the side and use its reference.
Devang Patel0d61eeb2010-01-28 18:21:00 +0000841 char *StrPtr = DebugInfoNames.Allocate<char>(Name.length());
Devang Patel84033fb2010-01-28 18:11:52 +0000842 memcpy(StrPtr, Name.data(), Name.length());
843 return llvm::StringRef(StrPtr, Name.length());
844}
845
846
Anders Carlsson11e51402010-04-17 20:15:18 +0000847/// CollectVTableInfo - If the C++ class has vtable info then insert appropriate
Devang Patel84033fb2010-01-28 18:11:52 +0000848/// debug info entry in EltTys vector.
849void CGDebugInfo::
Anders Carlsson11e51402010-04-17 20:15:18 +0000850CollectVTableInfo(const CXXRecordDecl *RD, llvm::DIFile Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000851 llvm::SmallVectorImpl<llvm::DIDescriptor> &EltTys) {
Devang Patel1c0954c2010-02-01 21:39:52 +0000852 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patel84033fb2010-01-28 18:11:52 +0000853
854 // If there is a primary base then it will hold vtable info.
855 if (RL.getPrimaryBase())
856 return;
857
858 // If this class is not dynamic then there is not any vtable info to collect.
Devang Patel1c0954c2010-02-01 21:39:52 +0000859 if (!RD->isDynamicClass())
Devang Patel84033fb2010-01-28 18:11:52 +0000860 return;
861
862 unsigned Size = CGM.getContext().getTypeSize(CGM.getContext().VoidPtrTy);
863 llvm::DIType VPTR
864 = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Anders Carlsson11e51402010-04-17 20:15:18 +0000865 getVTableName(RD), Unit,
Devang Patel84033fb2010-01-28 18:11:52 +0000866 0, Size, 0, 0, 0,
867 getOrCreateVTablePtrType(Unit));
868 EltTys.push_back(VPTR);
869}
870
Devang Patel410dc002009-02-25 01:36:11 +0000871/// CreateType - get structure or union type.
Chris Lattneraffb3732008-11-10 06:08:34 +0000872llvm::DIType CGDebugInfo::CreateType(const RecordType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +0000873 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +0000874 RecordDecl *RD = Ty->getDecl();
Mike Stump11289f42009-09-09 15:08:12 +0000875
Chris Lattneraffb3732008-11-10 06:08:34 +0000876 unsigned Tag;
Devang Patel3efd1472010-02-01 21:52:22 +0000877 if (RD->isStruct())
Chris Lattneraffb3732008-11-10 06:08:34 +0000878 Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patel3efd1472010-02-01 21:52:22 +0000879 else if (RD->isUnion())
Chris Lattneraffb3732008-11-10 06:08:34 +0000880 Tag = llvm::dwarf::DW_TAG_union_type;
881 else {
Devang Patel3efd1472010-02-01 21:52:22 +0000882 assert(RD->isClass() && "Unknown RecordType!");
Chris Lattneraffb3732008-11-10 06:08:34 +0000883 Tag = llvm::dwarf::DW_TAG_class_type;
Sanjiv Gupta19292422008-06-07 04:46:53 +0000884 }
885
Chris Lattneraffb3732008-11-10 06:08:34 +0000886 // Get overall information about the record type for the debug info.
Devang Patelc5ffabc2010-05-12 23:46:38 +0000887 llvm::DIFile DefUnit = getOrCreateFile(RD->getLocation());
888 unsigned Line = getLineNumber(RD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +0000889
Chris Lattneraffb3732008-11-10 06:08:34 +0000890 // Records and classes and unions can all be recursive. To handle them, we
891 // first generate a debug descriptor for the struct as a forward declaration.
892 // Then (if it is a definition) we go through and get debug info for all of
893 // its members. Finally, we create a descriptor for the complete type (which
894 // may refer to the forward decl if the struct is recursive) and replace all
895 // uses of the forward declaration with the final definition.
Devang Patel8f3f76f2010-07-08 19:56:29 +0000896 llvm::DIDescriptor FDContext =
897 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
898
899 // If this is just a forward declaration, construct an appropriately
900 // marked node and just return it.
901 if (!RD->getDefinition()) {
902 llvm::DICompositeType FwdDecl =
903 DebugFactory.CreateCompositeType(Tag, FDContext, RD->getName(),
904 DefUnit, Line, 0, 0, 0,
905 llvm::DIType::FlagFwdDecl,
906 llvm::DIType(), llvm::DIArray());
907
908 return FwdDecl;
909 }
Devang Patel3f4a77e2010-01-20 23:56:40 +0000910
Dan Gohmanb1aac332010-08-20 22:39:57 +0000911 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump11289f42009-09-09 15:08:12 +0000912
Devang Patelba4ad7f2010-05-07 18:12:35 +0000913 llvm::MDNode *MN = FwdDecl;
914 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Chris Lattneraffb3732008-11-10 06:08:34 +0000915 // Otherwise, insert it into the TypeCache so that recursive uses will find
916 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000917 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +0000918 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +0000919 RegionStack.push_back(FwdDeclNode);
920 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +0000921
922 // Convert all the elements.
923 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
924
Devang Patel3efd1472010-02-01 21:52:22 +0000925 const CXXRecordDecl *CXXDecl = dyn_cast<CXXRecordDecl>(RD);
Devang Patel946edc12010-01-28 21:41:35 +0000926 if (CXXDecl) {
927 CollectCXXBases(CXXDecl, Unit, EltTys, FwdDecl);
Anders Carlsson11e51402010-04-17 20:15:18 +0000928 CollectVTableInfo(CXXDecl, Unit, EltTys);
Devang Patel946edc12010-01-28 21:41:35 +0000929 }
Devang Patelcaa23f02010-08-12 00:02:44 +0000930
931 // Collect static variables with initializers.
932 for (RecordDecl::decl_iterator I = RD->decls_begin(), E = RD->decls_end();
933 I != E; ++I)
934 if (const VarDecl *V = dyn_cast<VarDecl>(*I)) {
935 if (const Expr *Init = V->getInit()) {
936 Expr::EvalResult Result;
937 if (Init->Evaluate(Result, CGM.getContext()) && Result.Val.isInt()) {
938 llvm::ConstantInt *CI
939 = llvm::ConstantInt::get(CGM.getLLVMContext(), Result.Val.getInt());
940
941 // Create the descriptor for static variable.
942 llvm::DIFile VUnit = getOrCreateFile(V->getLocation());
943 llvm::StringRef VName = V->getName();
944 llvm::DIType VTy = getOrCreateType(V->getType(), VUnit);
945 // Do not use DIGlobalVariable for enums.
946 if (VTy.getTag() != llvm::dwarf::DW_TAG_enumeration_type) {
947 DebugFactory.CreateGlobalVariable(FwdDecl, VName, VName, VName, VUnit,
948 getLineNumber(V->getLocation()),
949 VTy, true, true, CI);
950 }
951 }
952 }
953 }
954
Devang Patel3efd1472010-02-01 21:52:22 +0000955 CollectRecordFields(RD, Unit, EltTys);
Devang Patelabb44132010-01-28 00:54:21 +0000956 llvm::MDNode *ContainingType = NULL;
Devang Patel84033fb2010-01-28 18:11:52 +0000957 if (CXXDecl) {
Devang Patel7a12ad02010-01-19 01:54:44 +0000958 CollectCXXMemberFunctions(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patel96b7f552010-08-27 17:47:47 +0000959 CollectCXXFriends(CXXDecl, Unit, EltTys, FwdDecl);
Devang Patelabb44132010-01-28 00:54:21 +0000960
961 // A class's primary base or the class itself contains the vtable.
Devang Patel3efd1472010-02-01 21:52:22 +0000962 const ASTRecordLayout &RL = CGM.getContext().getASTRecordLayout(RD);
Devang Patelabb44132010-01-28 00:54:21 +0000963 if (const CXXRecordDecl *PBase = RL.getPrimaryBase())
964 ContainingType =
Devang Patelba4ad7f2010-05-07 18:12:35 +0000965 getOrCreateType(QualType(PBase->getTypeForDecl(), 0), Unit);
Devang Patelabb44132010-01-28 00:54:21 +0000966 else if (CXXDecl->isDynamicClass())
Devang Patelba4ad7f2010-05-07 18:12:35 +0000967 ContainingType = FwdDecl;
Devang Patelc54353d2010-01-25 23:32:18 +0000968 }
Mike Stump11289f42009-09-09 15:08:12 +0000969
Chris Lattneraffb3732008-11-10 06:08:34 +0000970 llvm::DIArray Elements =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +0000971 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Chris Lattneraffb3732008-11-10 06:08:34 +0000972
973 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +0000974 uint64_t Size = CGM.getContext().getTypeSize(Ty);
975 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +0000976
Devang Patel01bb5ce2010-03-11 20:01:48 +0000977 RegionStack.pop_back();
978 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
979 RegionMap.find(Ty->getDecl());
980 if (RI != RegionMap.end())
981 RegionMap.erase(RI);
982
Devang Patele8fb4b72010-02-01 22:40:08 +0000983 llvm::DIDescriptor RDContext =
984 getContextDescriptor(dyn_cast<Decl>(RD->getDeclContext()), Unit);
Devang Patel6c018202010-07-20 20:24:18 +0000985
986 llvm::StringRef RDName = RD->getName();
987 // If this is a class, include the template arguments also.
988 if (Tag == llvm::dwarf::DW_TAG_class_type)
989 RDName = getClassName(RD);
990
Devang Patel06cceef2009-07-22 18:57:00 +0000991 llvm::DICompositeType RealDecl =
Devang Patele8fb4b72010-02-01 22:40:08 +0000992 DebugFactory.CreateCompositeType(Tag, RDContext,
Devang Patel6c018202010-07-20 20:24:18 +0000993 RDName,
Devang Patel7bdf0962009-11-12 00:51:46 +0000994 DefUnit, Line, Size, Align, 0, 0,
Devang Patelabb44132010-01-28 00:54:21 +0000995 llvm::DIType(), Elements,
996 0, ContainingType);
Chris Lattneraffb3732008-11-10 06:08:34 +0000997
998 // Now that we have a real decl for the struct, replace anything using the
999 // old decl with the new one. This will recursively update the debug info.
Dan Gohman196f7102010-08-20 22:02:57 +00001000 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001001 RegionMap[RD] = llvm::WeakVH(RealDecl);
Chris Lattneraffb3732008-11-10 06:08:34 +00001002 return RealDecl;
1003}
1004
John McCall8b07ec22010-05-15 11:32:37 +00001005/// CreateType - get objective-c object type.
1006llvm::DIType CGDebugInfo::CreateType(const ObjCObjectType *Ty,
1007 llvm::DIFile Unit) {
1008 // Ignore protocols.
1009 return getOrCreateType(Ty->getBaseType(), Unit);
1010}
1011
Devang Patelf4c205b2009-02-26 21:10:26 +00001012/// CreateType - get objective-c interface type.
1013llvm::DIType CGDebugInfo::CreateType(const ObjCInterfaceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001014 llvm::DIFile Unit) {
Devang Patel3efd1472010-02-01 21:52:22 +00001015 ObjCInterfaceDecl *ID = Ty->getDecl();
Devang Patelf4c205b2009-02-26 21:10:26 +00001016 unsigned Tag = llvm::dwarf::DW_TAG_structure_type;
Devang Patelf4c205b2009-02-26 21:10:26 +00001017
1018 // Get overall information about the record type for the debug info.
Devang Patel408dcf62010-03-09 00:44:50 +00001019 llvm::DIFile DefUnit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001020 unsigned Line = getLineNumber(ID->getLocation());
Devang Patel408dcf62010-03-09 00:44:50 +00001021 unsigned RuntimeLang = TheCU.getLanguage();
Chris Lattnerc6ad2582009-05-02 01:13:16 +00001022
Dan Gohman66427b12010-08-23 21:15:56 +00001023 // If this is just a forward declaration, return a special forward-declaration
1024 // debug type.
1025 if (ID->isForwardDecl()) {
1026 llvm::DICompositeType FwdDecl =
1027 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(),
1028 DefUnit, Line, 0, 0, 0, 0,
1029 llvm::DIType(), llvm::DIArray(),
1030 RuntimeLang);
1031 return FwdDecl;
1032 }
1033
Devang Patelf4c205b2009-02-26 21:10:26 +00001034 // To handle recursive interface, we
1035 // first generate a debug descriptor for the struct as a forward declaration.
1036 // Then (if it is a definition) we go through and get debug info for all of
1037 // its members. Finally, we create a descriptor for the complete type (which
1038 // may refer to the forward decl if the struct is recursive) and replace all
1039 // uses of the forward declaration with the final definition.
Dan Gohmanb1aac332010-08-20 22:39:57 +00001040 llvm::DIType FwdDecl = DebugFactory.CreateTemporaryType();
Mike Stump11289f42009-09-09 15:08:12 +00001041
Devang Patelba4ad7f2010-05-07 18:12:35 +00001042 llvm::MDNode *MN = FwdDecl;
1043 llvm::TrackingVH<llvm::MDNode> FwdDeclNode = MN;
Devang Patelf4c205b2009-02-26 21:10:26 +00001044 // Otherwise, insert it into the TypeCache so that recursive uses will find
1045 // it.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001046 TypeCache[QualType(Ty, 0).getAsOpaquePtr()] = FwdDecl;
Devang Patel01bb5ce2010-03-11 20:01:48 +00001047 // Push the struct on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001048 RegionStack.push_back(FwdDeclNode);
1049 RegionMap[Ty->getDecl()] = llvm::WeakVH(FwdDecl);
Devang Patelf4c205b2009-02-26 21:10:26 +00001050
1051 // Convert all the elements.
1052 llvm::SmallVector<llvm::DIDescriptor, 16> EltTys;
1053
Devang Patel3efd1472010-02-01 21:52:22 +00001054 ObjCInterfaceDecl *SClass = ID->getSuperClass();
Devang Patelc0f58ea2009-03-10 21:30:26 +00001055 if (SClass) {
Mike Stump11289f42009-09-09 15:08:12 +00001056 llvm::DIType SClassTy =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001057 getOrCreateType(CGM.getContext().getObjCInterfaceType(SClass), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001058 llvm::DIType InhTag =
Devang Patelc0f58ea2009-03-10 21:30:26 +00001059 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_inheritance,
Devang Patel93f274c2010-03-09 22:49:11 +00001060 Unit, "", Unit, 0, 0, 0,
Devang Patelc0f58ea2009-03-10 21:30:26 +00001061 0 /* offset */, 0, SClassTy);
1062 EltTys.push_back(InhTag);
1063 }
1064
Devang Patel3efd1472010-02-01 21:52:22 +00001065 const ASTRecordLayout &RL = CGM.getContext().getASTObjCInterfaceLayout(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001066
1067 unsigned FieldNo = 0;
Devang Patel3efd1472010-02-01 21:52:22 +00001068 for (ObjCInterfaceDecl::ivar_iterator I = ID->ivar_begin(),
1069 E = ID->ivar_end(); I != E; ++I, ++FieldNo) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001070 ObjCIvarDecl *Field = *I;
1071 llvm::DIType FieldTy = getOrCreateType(Field->getType(), Unit);
1072
Devang Patel58bf6e12009-11-25 17:37:31 +00001073 llvm::StringRef FieldName = Field->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001074
Devang Pateldf348f12009-04-27 22:40:36 +00001075 // Ignore unnamed fields.
Devang Patel58bf6e12009-11-25 17:37:31 +00001076 if (FieldName.empty())
Devang Pateldf348f12009-04-27 22:40:36 +00001077 continue;
1078
Devang Patelf4c205b2009-02-26 21:10:26 +00001079 // Get the location for the field.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001080 llvm::DIFile FieldDefUnit = getOrCreateFile(Field->getLocation());
1081 unsigned FieldLine = getLineNumber(Field->getLocation());
Devang Patel9f804932009-03-20 18:24:39 +00001082 QualType FType = Field->getType();
1083 uint64_t FieldSize = 0;
1084 unsigned FieldAlign = 0;
Devang Patelec4bad52009-03-19 00:23:53 +00001085
Devang Patel9f804932009-03-20 18:24:39 +00001086 if (!FType->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001087
Devang Patel9f804932009-03-20 18:24:39 +00001088 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001089 FieldSize = CGM.getContext().getTypeSize(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001090 Expr *BitWidth = Field->getBitWidth();
1091 if (BitWidth)
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001092 FieldSize = BitWidth->EvaluateAsInt(CGM.getContext()).getZExtValue();
Eli Friedman1c4a1752009-04-26 19:19:15 +00001093
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001094 FieldAlign = CGM.getContext().getTypeAlign(FType);
Devang Patel9f804932009-03-20 18:24:39 +00001095 }
1096
Mike Stump11289f42009-09-09 15:08:12 +00001097 uint64_t FieldOffset = RL.getFieldOffset(FieldNo);
1098
Devang Patelec4bad52009-03-19 00:23:53 +00001099 unsigned Flags = 0;
1100 if (Field->getAccessControl() == ObjCIvarDecl::Protected)
1101 Flags = llvm::DIType::FlagProtected;
1102 else if (Field->getAccessControl() == ObjCIvarDecl::Private)
1103 Flags = llvm::DIType::FlagPrivate;
Mike Stump11289f42009-09-09 15:08:12 +00001104
Devang Patelf4c205b2009-02-26 21:10:26 +00001105 // Create a DW_TAG_member node to remember the offset of this field in the
1106 // struct. FIXME: This is an absolutely insane way to capture this
1107 // information. When we gut debug info, this should be fixed.
1108 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
1109 FieldName, FieldDefUnit,
1110 FieldLine, FieldSize, FieldAlign,
Devang Patelec4bad52009-03-19 00:23:53 +00001111 FieldOffset, Flags, FieldTy);
Devang Patelf4c205b2009-02-26 21:10:26 +00001112 EltTys.push_back(FieldTy);
1113 }
Mike Stump11289f42009-09-09 15:08:12 +00001114
Devang Patelf4c205b2009-02-26 21:10:26 +00001115 llvm::DIArray Elements =
Jay Foad7d0479f2009-05-21 09:52:38 +00001116 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
Devang Patelf4c205b2009-02-26 21:10:26 +00001117
Devang Patel01bb5ce2010-03-11 20:01:48 +00001118 RegionStack.pop_back();
1119 llvm::DenseMap<const Decl *, llvm::WeakVH>::iterator RI =
1120 RegionMap.find(Ty->getDecl());
1121 if (RI != RegionMap.end())
1122 RegionMap.erase(RI);
1123
Devang Patelf4c205b2009-02-26 21:10:26 +00001124 // Bit size, align and offset of the type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001125 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1126 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
Mike Stump11289f42009-09-09 15:08:12 +00001127
Devang Patel6a3b3fe2009-07-27 18:42:03 +00001128 llvm::DICompositeType RealDecl =
Devang Patel3efd1472010-02-01 21:52:22 +00001129 DebugFactory.CreateCompositeType(Tag, Unit, ID->getName(), DefUnit,
Devang Patel7bdf0962009-11-12 00:51:46 +00001130 Line, Size, Align, 0, 0, llvm::DIType(),
1131 Elements, RuntimeLang);
Devang Patelf4c205b2009-02-26 21:10:26 +00001132
1133 // Now that we have a real decl for the struct, replace anything using the
1134 // old decl with the new one. This will recursively update the debug info.
Dan Gohman196f7102010-08-20 22:02:57 +00001135 llvm::DIType(FwdDeclNode).replaceAllUsesWith(RealDecl);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001136 RegionMap[ID] = llvm::WeakVH(RealDecl);
Devang Patel9c3a0182009-07-13 17:03:14 +00001137
Devang Patelf4c205b2009-02-26 21:10:26 +00001138 return RealDecl;
1139}
1140
Chris Lattneraffb3732008-11-10 06:08:34 +00001141llvm::DIType CGDebugInfo::CreateType(const EnumType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001142 llvm::DIFile Unit) {
Devang Patel41c20972010-08-23 22:07:25 +00001143 return CreateEnumType(Ty->getDecl(), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001144
Chris Lattneraffb3732008-11-10 06:08:34 +00001145}
1146
1147llvm::DIType CGDebugInfo::CreateType(const TagType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001148 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001149 if (const RecordType *RT = dyn_cast<RecordType>(Ty))
1150 return CreateType(RT, Unit);
1151 else if (const EnumType *ET = dyn_cast<EnumType>(Ty))
1152 return CreateType(ET, Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001153
Chris Lattneraffb3732008-11-10 06:08:34 +00001154 return llvm::DIType();
1155}
1156
Devang Patelb4073382010-02-23 22:59:39 +00001157llvm::DIType CGDebugInfo::CreateType(const VectorType *Ty,
Eli Friedman04831922010-08-22 01:00:03 +00001158 llvm::DIFile Unit) {
Devang Patelb4073382010-02-23 22:59:39 +00001159 llvm::DIType ElementTy = getOrCreateType(Ty->getElementType(), Unit);
1160 uint64_t NumElems = Ty->getNumElements();
1161 if (NumElems > 0)
1162 --NumElems;
Devang Patelb4073382010-02-23 22:59:39 +00001163
Benjamin Kramer9e649c32010-03-30 11:36:44 +00001164 llvm::DIDescriptor Subscript = DebugFactory.GetOrCreateSubrange(0, NumElems);
1165 llvm::DIArray SubscriptArray = DebugFactory.GetOrCreateArray(&Subscript, 1);
Devang Patelb4073382010-02-23 22:59:39 +00001166
1167 uint64_t Size = CGM.getContext().getTypeSize(Ty);
1168 uint64_t Align = CGM.getContext().getTypeAlign(Ty);
1169
1170 return
1171 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_vector_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001172 Unit, "", Unit,
Devang Patelb4073382010-02-23 22:59:39 +00001173 0, Size, Align, 0, 0,
Eli Friedman04831922010-08-22 01:00:03 +00001174 ElementTy, SubscriptArray);
Devang Patelb4073382010-02-23 22:59:39 +00001175}
1176
Chris Lattneraffb3732008-11-10 06:08:34 +00001177llvm::DIType CGDebugInfo::CreateType(const ArrayType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001178 llvm::DIFile Unit) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001179 uint64_t Size;
1180 uint64_t Align;
Mike Stump11289f42009-09-09 15:08:12 +00001181
1182
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001183 // FIXME: make getTypeAlign() aware of VLAs and incomplete array types
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001184 if (const VariableArrayType *VAT = dyn_cast<VariableArrayType>(Ty)) {
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001185 Size = 0;
1186 Align =
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001187 CGM.getContext().getTypeAlign(CGM.getContext().getBaseElementType(VAT));
Nuno Lopesbb537dc2009-01-28 00:35:17 +00001188 } else if (Ty->isIncompleteArrayType()) {
1189 Size = 0;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001190 Align = CGM.getContext().getTypeAlign(Ty->getElementType());
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001191 } else {
1192 // Size and align of the whole array, not the element type.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001193 Size = CGM.getContext().getTypeSize(Ty);
1194 Align = CGM.getContext().getTypeAlign(Ty);
Anders Carlssond8cd7b62009-01-05 01:23:29 +00001195 }
Mike Stump11289f42009-09-09 15:08:12 +00001196
Chris Lattneraffb3732008-11-10 06:08:34 +00001197 // Add the dimensions of the array. FIXME: This loses CV qualifiers from
1198 // interior arrays, do we care? Why aren't nested arrays represented the
1199 // obvious/recursive way?
1200 llvm::SmallVector<llvm::DIDescriptor, 8> Subscripts;
1201 QualType EltTy(Ty, 0);
1202 while ((Ty = dyn_cast<ArrayType>(EltTy))) {
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001203 uint64_t Upper = 0;
Mike Stump11289f42009-09-09 15:08:12 +00001204 if (const ConstantArrayType *CAT = dyn_cast<ConstantArrayType>(Ty))
Devang Pateld4bbb082009-08-14 20:57:45 +00001205 if (CAT->getSize().getZExtValue())
Mike Stump11289f42009-09-09 15:08:12 +00001206 Upper = CAT->getSize().getZExtValue() - 1;
Chris Lattneraffb3732008-11-10 06:08:34 +00001207 // FIXME: Verify this is right for VLAs.
1208 Subscripts.push_back(DebugFactory.GetOrCreateSubrange(0, Upper));
1209 EltTy = Ty->getElementType();
Sanjiv Gupta224e8ea2008-06-09 10:47:41 +00001210 }
Mike Stump11289f42009-09-09 15:08:12 +00001211
Chris Lattneraffb3732008-11-10 06:08:34 +00001212 llvm::DIArray SubscriptArray =
Daniel Dunbarbee70bd2009-05-26 19:40:20 +00001213 DebugFactory.GetOrCreateArray(Subscripts.data(), Subscripts.size());
Chris Lattneraffb3732008-11-10 06:08:34 +00001214
Devang Patele21912d2009-10-20 19:55:01 +00001215 llvm::DIType DbgTy =
1216 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_array_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001217 Unit, "", Unit,
Devang Patele21912d2009-10-20 19:55:01 +00001218 0, Size, Align, 0, 0,
1219 getOrCreateType(EltTy, Unit),
1220 SubscriptArray);
Devang Patele21912d2009-10-20 19:55:01 +00001221 return DbgTy;
Chris Lattneraffb3732008-11-10 06:08:34 +00001222}
1223
Anders Carlsson443f6772009-11-06 19:19:55 +00001224llvm::DIType CGDebugInfo::CreateType(const LValueReferenceType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001225 llvm::DIFile Unit) {
Anders Carlsson443f6772009-11-06 19:19:55 +00001226 return CreatePointerLikeType(llvm::dwarf::DW_TAG_reference_type,
1227 Ty, Ty->getPointeeType(), Unit);
1228}
Chris Lattneraffb3732008-11-10 06:08:34 +00001229
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001230llvm::DIType CGDebugInfo::CreateType(const MemberPointerType *Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001231 llvm::DIFile U) {
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001232 QualType PointerDiffTy = CGM.getContext().getPointerDiffType();
1233 llvm::DIType PointerDiffDITy = getOrCreateType(PointerDiffTy, U);
1234
1235 if (!Ty->getPointeeType()->isFunctionType()) {
1236 // We have a data member pointer type.
1237 return PointerDiffDITy;
1238 }
1239
1240 // We have a member function pointer type. Treat it as a struct with two
1241 // ptrdiff_t members.
1242 std::pair<uint64_t, unsigned> Info = CGM.getContext().getTypeInfo(Ty);
1243
1244 uint64_t FieldOffset = 0;
1245 llvm::DIDescriptor ElementTypes[2];
1246
1247 // FIXME: This should probably be a function type instead.
1248 ElementTypes[0] =
1249 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001250 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001251 Info.first, Info.second, FieldOffset, 0,
1252 PointerDiffDITy);
1253 FieldOffset += Info.first;
1254
1255 ElementTypes[1] =
1256 DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, U,
Devang Patel93f274c2010-03-09 22:49:11 +00001257 "ptr", U, 0,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001258 Info.first, Info.second, FieldOffset, 0,
1259 PointerDiffDITy);
1260
1261 llvm::DIArray Elements =
1262 DebugFactory.GetOrCreateArray(&ElementTypes[0],
1263 llvm::array_lengthof(ElementTypes));
1264
1265 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
1266 U, llvm::StringRef("test"),
Devang Patel93f274c2010-03-09 22:49:11 +00001267 U, 0, FieldOffset,
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001268 0, 0, 0, llvm::DIType(), Elements);
1269}
1270
Devang Patel41c20972010-08-23 22:07:25 +00001271/// CreateEnumType - get enumeration type.
1272llvm::DIType CGDebugInfo::CreateEnumType(const EnumDecl *ED, llvm::DIFile Unit){
1273 llvm::SmallVector<llvm::DIDescriptor, 32> Enumerators;
1274
1275 // Create DIEnumerator elements for each enumerator.
1276 for (EnumDecl::enumerator_iterator
1277 Enum = ED->enumerator_begin(), EnumEnd = ED->enumerator_end();
1278 Enum != EnumEnd; ++Enum) {
1279 Enumerators.push_back(DebugFactory.CreateEnumerator(Enum->getName(),
1280 Enum->getInitVal().getZExtValue()));
1281 }
1282
1283 // Return a CompositeType for the enum itself.
1284 llvm::DIArray EltArray =
1285 DebugFactory.GetOrCreateArray(Enumerators.data(), Enumerators.size());
1286
1287 llvm::DIFile DefUnit = getOrCreateFile(ED->getLocation());
1288 unsigned Line = getLineNumber(ED->getLocation());
1289 uint64_t Size = 0;
Devang Patel22e99c22010-08-24 18:14:06 +00001290 uint64_t Align = 0;
1291 if (!ED->getTypeForDecl()->isIncompleteType()) {
1292 Size = CGM.getContext().getTypeSize(ED->getTypeForDecl());
1293 Align = CGM.getContext().getTypeAlign(ED->getTypeForDecl());
1294 }
Devang Patel41c20972010-08-23 22:07:25 +00001295 llvm::DIType DbgTy =
1296 DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_enumeration_type,
1297 Unit, ED->getName(), DefUnit, Line,
Devang Patel22e99c22010-08-24 18:14:06 +00001298 Size, Align, 0, 0,
Devang Patel41c20972010-08-23 22:07:25 +00001299 llvm::DIType(), EltArray);
1300 return DbgTy;
1301}
1302
Douglas Gregor0f139a12009-12-21 20:18:30 +00001303static QualType UnwrapTypeForDebugInfo(QualType T) {
1304 do {
1305 QualType LastT = T;
1306 switch (T->getTypeClass()) {
1307 default:
1308 return T;
1309 case Type::TemplateSpecialization:
1310 T = cast<TemplateSpecializationType>(T)->desugar();
1311 break;
1312 case Type::TypeOfExpr: {
1313 TypeOfExprType *Ty = cast<TypeOfExprType>(T);
1314 T = Ty->getUnderlyingExpr()->getType();
1315 break;
1316 }
1317 case Type::TypeOf:
1318 T = cast<TypeOfType>(T)->getUnderlyingType();
1319 break;
1320 case Type::Decltype:
1321 T = cast<DecltypeType>(T)->getUnderlyingType();
1322 break;
Abramo Bagnara6150c882010-05-11 21:36:43 +00001323 case Type::Elaborated:
1324 T = cast<ElaboratedType>(T)->getNamedType();
Douglas Gregor0f139a12009-12-21 20:18:30 +00001325 break;
1326 case Type::SubstTemplateTypeParm:
1327 T = cast<SubstTemplateTypeParmType>(T)->getReplacementType();
1328 break;
Douglas Gregor0f139a12009-12-21 20:18:30 +00001329 }
1330
1331 assert(T != LastT && "Type unwrapping failed to unwrap!");
1332 if (T == LastT)
1333 return T;
1334 } while (true);
1335
1336 return T;
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001337}
1338
Sanjiv Gupta98070572008-05-25 05:15:42 +00001339/// getOrCreateType - Get the type from the cache or create a new
1340/// one if necessary.
Chris Lattneraffb3732008-11-10 06:08:34 +00001341llvm::DIType CGDebugInfo::getOrCreateType(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001342 llvm::DIFile Unit) {
Chris Lattneraffb3732008-11-10 06:08:34 +00001343 if (Ty.isNull())
1344 return llvm::DIType();
Mike Stump11289f42009-09-09 15:08:12 +00001345
Douglas Gregor0f139a12009-12-21 20:18:30 +00001346 // Unwrap the type as needed for debug information.
1347 Ty = UnwrapTypeForDebugInfo(Ty);
Anders Carlsson0acee6e2009-11-14 21:08:12 +00001348
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001349 // Check for existing entry.
Ted Kremenek23c29f02010-03-29 18:29:57 +00001350 llvm::DenseMap<void *, llvm::WeakVH>::iterator it =
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001351 TypeCache.find(Ty.getAsOpaquePtr());
Daniel Dunbar99961382009-09-19 20:17:48 +00001352 if (it != TypeCache.end()) {
1353 // Verify that the debug info still exists.
1354 if (&*it->second)
1355 return llvm::DIType(cast<llvm::MDNode>(it->second));
1356 }
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001357
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001358 // Otherwise create the type.
1359 llvm::DIType Res = CreateTypeNode(Ty, Unit);
Anders Carlsson6037e782009-11-14 20:52:05 +00001360
1361 // And update the type cache.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001362 TypeCache[Ty.getAsOpaquePtr()] = Res;
Daniel Dunbar1cbaae52009-09-19 19:27:24 +00001363 return Res;
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001364}
1365
Anders Carlsson6037e782009-11-14 20:52:05 +00001366/// CreateTypeNode - Create a new debug type node.
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001367llvm::DIType CGDebugInfo::CreateTypeNode(QualType Ty,
Devang Patel408dcf62010-03-09 00:44:50 +00001368 llvm::DIFile Unit) {
John McCall0cf15512009-09-25 01:40:47 +00001369 // Handle qualifiers, which recursively handles what they refer to.
Douglas Gregor1b8fe5b72009-11-16 21:35:15 +00001370 if (Ty.hasLocalQualifiers())
John McCall0cf15512009-09-25 01:40:47 +00001371 return CreateQualifiedType(Ty, Unit);
Sanjiv Gupta98070572008-05-25 05:15:42 +00001372
Douglas Gregor0915b432009-12-21 19:57:21 +00001373 const char *Diag = 0;
1374
Sanjiv Gupta98070572008-05-25 05:15:42 +00001375 // Work out details of type.
Chris Lattneraffb3732008-11-10 06:08:34 +00001376 switch (Ty->getTypeClass()) {
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001377#define TYPE(Class, Base)
1378#define ABSTRACT_TYPE(Class, Base)
1379#define NON_CANONICAL_TYPE(Class, Base)
1380#define DEPENDENT_TYPE(Class, Base) case Type::Class:
1381#include "clang/AST/TypeNodes.def"
1382 assert(false && "Dependent types cannot show up in debug information");
Argyrios Kyrtzidise9189262009-08-19 01:28:17 +00001383
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001384 // FIXME: Handle these.
1385 case Type::ExtVector:
Anders Carlsson25ed5c22009-11-06 18:24:04 +00001386 return llvm::DIType();
Devang Patelb4073382010-02-23 22:59:39 +00001387
1388 case Type::Vector:
1389 return CreateType(cast<VectorType>(Ty), Unit);
Daniel Dunbarf5c79702009-07-14 01:20:56 +00001390 case Type::ObjCObjectPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001391 return CreateType(cast<ObjCObjectPointerType>(Ty), Unit);
John McCall8b07ec22010-05-15 11:32:37 +00001392 case Type::ObjCObject:
1393 return CreateType(cast<ObjCObjectType>(Ty), Unit);
Mike Stump11289f42009-09-09 15:08:12 +00001394 case Type::ObjCInterface:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001395 return CreateType(cast<ObjCInterfaceType>(Ty), Unit);
1396 case Type::Builtin: return CreateType(cast<BuiltinType>(Ty), Unit);
1397 case Type::Complex: return CreateType(cast<ComplexType>(Ty), Unit);
1398 case Type::Pointer: return CreateType(cast<PointerType>(Ty), Unit);
Mike Stump31f099c2009-05-14 02:03:51 +00001399 case Type::BlockPointer:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001400 return CreateType(cast<BlockPointerType>(Ty), Unit);
1401 case Type::Typedef: return CreateType(cast<TypedefType>(Ty), Unit);
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001402 case Type::Record:
Douglas Gregordeaad8c2009-02-26 23:50:07 +00001403 case Type::Enum:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001404 return CreateType(cast<TagType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001405 case Type::FunctionProto:
1406 case Type::FunctionNoProto:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001407 return CreateType(cast<FunctionType>(Ty), Unit);
Chris Lattneraffb3732008-11-10 06:08:34 +00001408 case Type::ConstantArray:
1409 case Type::VariableArray:
1410 case Type::IncompleteArray:
Daniel Dunbarde870bd2009-09-19 19:27:14 +00001411 return CreateType(cast<ArrayType>(Ty), Unit);
Anders Carlsson443f6772009-11-06 19:19:55 +00001412
1413 case Type::LValueReference:
1414 return CreateType(cast<LValueReferenceType>(Ty), Unit);
1415
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001416 case Type::MemberPointer:
1417 return CreateType(cast<MemberPointerType>(Ty), Unit);
Douglas Gregor0915b432009-12-21 19:57:21 +00001418
1419 case Type::TemplateSpecialization:
Douglas Gregor0915b432009-12-21 19:57:21 +00001420 case Type::Elaborated:
Douglas Gregor0915b432009-12-21 19:57:21 +00001421 case Type::SubstTemplateTypeParm:
Douglas Gregor0915b432009-12-21 19:57:21 +00001422 case Type::TypeOfExpr:
1423 case Type::TypeOf:
Douglas Gregor0f139a12009-12-21 20:18:30 +00001424 case Type::Decltype:
1425 llvm_unreachable("type should have been unwrapped!");
1426 return llvm::DIType();
Douglas Gregor0915b432009-12-21 19:57:21 +00001427
1428 case Type::RValueReference:
1429 // FIXME: Implement!
1430 Diag = "rvalue references";
1431 break;
Sanjiv Gupta98070572008-05-25 05:15:42 +00001432 }
Douglas Gregor0915b432009-12-21 19:57:21 +00001433
1434 assert(Diag && "Fall through without a diagnostic?");
1435 unsigned DiagID = CGM.getDiags().getCustomDiagID(Diagnostic::Error,
1436 "debug information for %0 is not yet supported");
1437 CGM.getDiags().Report(FullSourceLoc(), DiagID)
1438 << Diag;
1439 return llvm::DIType();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001440}
1441
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001442/// CreateMemberType - Create new member and increase Offset by FType's size.
1443llvm::DIType CGDebugInfo::CreateMemberType(llvm::DIFile Unit, QualType FType,
1444 llvm::StringRef Name,
1445 uint64_t *Offset) {
1446 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
1447 uint64_t FieldSize = CGM.getContext().getTypeSize(FType);
1448 unsigned FieldAlign = CGM.getContext().getTypeAlign(FType);
1449 llvm::DIType Ty = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member,
1450 Unit, Name, Unit, 0,
1451 FieldSize, FieldAlign,
1452 *Offset, 0, FieldTy);
1453 *Offset += FieldSize;
1454 return Ty;
1455}
1456
Sanjiv Gupta98070572008-05-25 05:15:42 +00001457/// EmitFunctionStart - Constructs the debug code for entering a function -
1458/// "llvm.dbg.func.start.".
Devang Patel934661e2010-01-14 00:36:21 +00001459void CGDebugInfo::EmitFunctionStart(GlobalDecl GD, QualType FnType,
Sanjiv Gupta98070572008-05-25 05:15:42 +00001460 llvm::Function *Fn,
Chris Lattneraffb3732008-11-10 06:08:34 +00001461 CGBuilderTy &Builder) {
Mike Stump11289f42009-09-09 15:08:12 +00001462
Devang Patel934661e2010-01-14 00:36:21 +00001463 llvm::StringRef Name;
Anders Carlssonea836bc2010-06-22 16:16:50 +00001464 llvm::StringRef LinkageName;
Devang Patel934661e2010-01-14 00:36:21 +00001465
Devang Patel0884a602010-07-22 22:29:16 +00001466 FnBeginRegionCount.push_back(RegionStack.size());
1467
Devang Patel934661e2010-01-14 00:36:21 +00001468 const Decl *D = GD.getDecl();
1469 if (const FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
Devang Patel7a12ad02010-01-19 01:54:44 +00001470 // If there is a DISubprogram for this function available then use it.
1471 llvm::DenseMap<const FunctionDecl *, llvm::WeakVH>::iterator
1472 FI = SPCache.find(FD);
1473 if (FI != SPCache.end()) {
Gabor Greifbf986082010-09-18 13:00:17 +00001474 llvm::DIDescriptor SP(dyn_cast_or_null<llvm::MDNode>(&*FI->second));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001475 if (SP.isSubprogram() && llvm::DISubprogram(SP).isDefinition()) {
1476 llvm::MDNode *SPN = SP;
1477 RegionStack.push_back(SPN);
1478 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel7a12ad02010-01-19 01:54:44 +00001479 return;
1480 }
1481 }
Devang Patel934661e2010-01-14 00:36:21 +00001482 Name = getFunctionName(FD);
1483 // Use mangled name as linkage name for c/c++ functions.
Anders Carlssonea836bc2010-06-22 16:16:50 +00001484 LinkageName = CGM.getMangledName(GD);
David Chisnall6bf98ff2010-09-02 17:16:32 +00001485 } else if (const ObjCMethodDecl *OMD = dyn_cast<ObjCMethodDecl>(D)) {
David Chisnallcf607442010-09-02 18:01:51 +00001486 Name = getObjCMethodName(OMD);
David Chisnall6bf98ff2010-09-02 17:16:32 +00001487 LinkageName = Name;
Devang Patel934661e2010-01-14 00:36:21 +00001488 } else {
1489 // Use llvm function name as linkage name.
1490 Name = Fn->getName();
Anders Carlssonea836bc2010-06-22 16:16:50 +00001491 LinkageName = Name;
Devang Patel934661e2010-01-14 00:36:21 +00001492 }
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001493 if (!Name.empty() && Name[0] == '\01')
1494 Name = Name.substr(1);
Mike Stump11289f42009-09-09 15:08:12 +00001495
Devang Patel84715932010-04-24 00:49:16 +00001496 // It is expected that CurLoc is set before using EmitFunctionStart.
1497 // Usually, CurLoc points to the left bracket location of compound
1498 // statement representing function body.
1499 llvm::DIFile Unit = getOrCreateFile(CurLoc);
Devang Patelc5ffabc2010-05-12 23:46:38 +00001500 unsigned LineNo = getLineNumber(CurLoc);
Mike Stump11289f42009-09-09 15:08:12 +00001501
Chris Lattneraffb3732008-11-10 06:08:34 +00001502 llvm::DISubprogram SP =
Devang Patel84715932010-04-24 00:49:16 +00001503 DebugFactory.CreateSubprogram(Unit, Name, Name, LinkageName, Unit, LineNo,
Mike Stumpae2559a2009-10-23 01:52:13 +00001504 getOrCreateType(FnType, Unit),
Devang Patel8fd64992010-07-15 23:09:46 +00001505 Fn->hasInternalLinkage(), true/*definition*/,
1506 0, 0, llvm::DIType(),
1507 D->isImplicit(),
1508 CGM.getLangOptions().Optimize, Fn);
Mike Stump11289f42009-09-09 15:08:12 +00001509
Sanjiv Gupta98070572008-05-25 05:15:42 +00001510 // Push function on region stack.
Devang Patelba4ad7f2010-05-07 18:12:35 +00001511 llvm::MDNode *SPN = SP;
1512 RegionStack.push_back(SPN);
1513 RegionMap[D] = llvm::WeakVH(SP);
Devang Patel0884a602010-07-22 22:29:16 +00001514
1515 // Clear stack used to keep track of #line directives.
1516 LineDirectiveFiles.clear();
Sanjiv Gupta98070572008-05-25 05:15:42 +00001517}
1518
1519
Devang Patel11a42a42010-07-20 22:20:10 +00001520void CGDebugInfo::EmitStopPoint(CGBuilderTy &Builder) {
Sanjiv Gupta98070572008-05-25 05:15:42 +00001521 if (CurLoc.isInvalid() || CurLoc.isMacroID()) return;
Mike Stump11289f42009-09-09 15:08:12 +00001522
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001523 // Don't bother if things are the same as last time.
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001524 SourceManager &SM = CGM.getContext().getSourceManager();
Mike Stump11289f42009-09-09 15:08:12 +00001525 if (CurLoc == PrevLoc
Chris Lattner88ea93e2009-02-04 01:06:56 +00001526 || (SM.getInstantiationLineNumber(CurLoc) ==
1527 SM.getInstantiationLineNumber(PrevLoc)
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001528 && SM.isFromSameFile(CurLoc, PrevLoc)))
Devang Patela2c048e2010-04-05 21:09:15 +00001529 // New Builder may not be in sync with CGDebugInfo.
1530 if (!Builder.getCurrentDebugLocation().isUnknown())
1531 return;
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001532
1533 // Update last state.
1534 PrevLoc = CurLoc;
1535
Chris Lattnere675d0f2010-04-01 06:31:43 +00001536 llvm::MDNode *Scope = RegionStack.back();
Devang Patelc5ffabc2010-05-12 23:46:38 +00001537 Builder.SetCurrentDebugLocation(llvm::DebugLoc::get(getLineNumber(CurLoc),
1538 getColumnNumber(CurLoc),
Chris Lattner18a584b2010-04-02 20:21:43 +00001539 Scope));
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001540}
1541
Devang Patel0884a602010-07-22 22:29:16 +00001542/// UpdateLineDirectiveRegion - Update region stack only if #line directive
1543/// has introduced scope change.
1544void CGDebugInfo::UpdateLineDirectiveRegion(CGBuilderTy &Builder) {
1545 if (CurLoc.isInvalid() || CurLoc.isMacroID() ||
1546 PrevLoc.isInvalid() || PrevLoc.isMacroID())
1547 return;
1548 SourceManager &SM = CGM.getContext().getSourceManager();
1549 PresumedLoc PCLoc = SM.getPresumedLoc(CurLoc);
1550 PresumedLoc PPLoc = SM.getPresumedLoc(PrevLoc);
1551
1552 if (!strcmp(PPLoc.getFilename(), PCLoc.getFilename()))
1553 return;
1554
1555 // If #line directive stack is empty then we are entering a new scope.
1556 if (LineDirectiveFiles.empty()) {
1557 EmitRegionStart(Builder);
1558 LineDirectiveFiles.push_back(PCLoc.getFilename());
1559 return;
1560 }
1561
1562 assert (RegionStack.size() >= LineDirectiveFiles.size()
1563 && "error handling #line regions!");
1564
1565 bool SeenThisFile = false;
Devang Patel28b52862010-09-15 20:50:40 +00001566 // Chek if current file is already seen earlier.
Devang Patel0884a602010-07-22 22:29:16 +00001567 for(std::vector<const char *>::iterator I = LineDirectiveFiles.begin(),
1568 E = LineDirectiveFiles.end(); I != E; ++I)
Devang Patel28b52862010-09-15 20:50:40 +00001569 if (!strcmp(PCLoc.getFilename(), *I)) {
Devang Patel0884a602010-07-22 22:29:16 +00001570 SeenThisFile = true;
1571 break;
1572 }
1573
1574 // If #line for this file is seen earlier then pop out #line regions.
1575 if (SeenThisFile) {
1576 while (!LineDirectiveFiles.empty()) {
1577 const char *LastFile = LineDirectiveFiles.back();
1578 RegionStack.pop_back();
1579 LineDirectiveFiles.pop_back();
1580 if (!strcmp(PPLoc.getFilename(), LastFile))
1581 break;
1582 }
1583 return;
1584 }
1585
1586 // .. otherwise insert new #line region.
1587 EmitRegionStart(Builder);
1588 LineDirectiveFiles.push_back(PCLoc.getFilename());
1589
1590 return;
1591}
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001592/// EmitRegionStart- Constructs the debug code for entering a declarative
1593/// region - "llvm.dbg.region.start.".
Devang Patel11a42a42010-07-20 22:20:10 +00001594void CGDebugInfo::EmitRegionStart(CGBuilderTy &Builder) {
Devang Patelb40f2952009-11-13 19:10:24 +00001595 llvm::DIDescriptor D =
1596 DebugFactory.CreateLexicalBlock(RegionStack.empty() ?
1597 llvm::DIDescriptor() :
Devang Patel75855802010-02-16 21:41:20 +00001598 llvm::DIDescriptor(RegionStack.back()),
Stuart Hastings7d7bc562010-07-19 23:56:31 +00001599 getOrCreateFile(CurLoc),
Devang Patelc5ffabc2010-05-12 23:46:38 +00001600 getLineNumber(CurLoc),
1601 getColumnNumber(CurLoc));
Devang Patelba4ad7f2010-05-07 18:12:35 +00001602 llvm::MDNode *DN = D;
1603 RegionStack.push_back(DN);
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001604}
1605
1606/// EmitRegionEnd - Constructs the debug code for exiting a declarative
1607/// region - "llvm.dbg.region.end."
Devang Patel11a42a42010-07-20 22:20:10 +00001608void CGDebugInfo::EmitRegionEnd(CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001609 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1610
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001611 // Provide an region stop point.
Devang Patel11a42a42010-07-20 22:20:10 +00001612 EmitStopPoint(Builder);
Mike Stump11289f42009-09-09 15:08:12 +00001613
Sanjiv Gupta98070572008-05-25 05:15:42 +00001614 RegionStack.pop_back();
Sanjiv Gupta15cb6692008-05-08 08:54:20 +00001615}
1616
Devang Patel0884a602010-07-22 22:29:16 +00001617/// EmitFunctionEnd - Constructs the debug code for exiting a function.
1618void CGDebugInfo::EmitFunctionEnd(CGBuilderTy &Builder) {
1619 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1620 unsigned RCount = FnBeginRegionCount.back();
1621 assert(RCount <= RegionStack.size() && "Region stack mismatch");
1622
1623 // Pop all regions for this function.
1624 while (RegionStack.size() != RCount)
1625 EmitRegionEnd(Builder);
1626 FnBeginRegionCount.pop_back();
1627}
1628
Devang Patel535fdaf2010-02-10 18:49:08 +00001629// EmitTypeForVarWithBlocksAttr - Build up structure info for the byref.
1630// See BuildByRefType.
1631llvm::DIType CGDebugInfo::EmitTypeForVarWithBlocksAttr(const ValueDecl *VD,
1632 uint64_t *XOffset) {
1633
1634 llvm::SmallVector<llvm::DIDescriptor, 5> EltTys;
1635
1636 QualType FType;
1637 uint64_t FieldSize, FieldOffset;
1638 unsigned FieldAlign;
1639
Devang Patel408dcf62010-03-09 00:44:50 +00001640 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001641 QualType Type = VD->getType();
1642
1643 FieldOffset = 0;
1644 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001645 EltTys.push_back(CreateMemberType(Unit, FType, "__isa", &FieldOffset));
1646 EltTys.push_back(CreateMemberType(Unit, FType, "__forwarding", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001647 FType = CGM.getContext().IntTy;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001648 EltTys.push_back(CreateMemberType(Unit, FType, "__flags", &FieldOffset));
1649 EltTys.push_back(CreateMemberType(Unit, FType, "__size", &FieldOffset));
1650
Devang Patel535fdaf2010-02-10 18:49:08 +00001651 bool HasCopyAndDispose = CGM.BlockRequiresCopying(Type);
1652 if (HasCopyAndDispose) {
1653 FType = CGM.getContext().getPointerType(CGM.getContext().VoidTy);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001654 EltTys.push_back(CreateMemberType(Unit, FType, "__copy_helper",
1655 &FieldOffset));
1656 EltTys.push_back(CreateMemberType(Unit, FType, "__destroy_helper",
1657 &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001658 }
1659
1660 CharUnits Align = CGM.getContext().getDeclAlign(VD);
1661 if (Align > CharUnits::fromQuantity(
1662 CGM.getContext().Target.getPointerAlign(0) / 8)) {
1663 unsigned AlignedOffsetInBytes
1664 = llvm::RoundUpToAlignment(FieldOffset/8, Align.getQuantity());
1665 unsigned NumPaddingBytes
1666 = AlignedOffsetInBytes - FieldOffset/8;
1667
1668 if (NumPaddingBytes > 0) {
1669 llvm::APInt pad(32, NumPaddingBytes);
1670 FType = CGM.getContext().getConstantArrayType(CGM.getContext().CharTy,
1671 pad, ArrayType::Normal, 0);
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001672 EltTys.push_back(CreateMemberType(Unit, FType, "", &FieldOffset));
Devang Patel535fdaf2010-02-10 18:49:08 +00001673 }
1674 }
1675
1676 FType = Type;
Benjamin Kramerbbb5dea2010-04-24 20:19:58 +00001677 llvm::DIType FieldTy = CGDebugInfo::getOrCreateType(FType, Unit);
Devang Patel535fdaf2010-02-10 18:49:08 +00001678 FieldSize = CGM.getContext().getTypeSize(FType);
1679 FieldAlign = Align.getQuantity()*8;
1680
1681 *XOffset = FieldOffset;
1682 FieldTy = DebugFactory.CreateDerivedType(llvm::dwarf::DW_TAG_member, Unit,
Devang Patel93f274c2010-03-09 22:49:11 +00001683 VD->getName(), Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001684 0, FieldSize, FieldAlign,
1685 FieldOffset, 0, FieldTy);
1686 EltTys.push_back(FieldTy);
1687 FieldOffset += FieldSize;
1688
1689 llvm::DIArray Elements =
1690 DebugFactory.GetOrCreateArray(EltTys.data(), EltTys.size());
1691
1692 unsigned Flags = llvm::DIType::FlagBlockByrefStruct;
1693
1694 return DebugFactory.CreateCompositeType(llvm::dwarf::DW_TAG_structure_type,
Devang Patel93f274c2010-03-09 22:49:11 +00001695 Unit, "", Unit,
Devang Patel535fdaf2010-02-10 18:49:08 +00001696 0, FieldOffset, 0, 0, Flags,
1697 llvm::DIType(), Elements);
1698
1699}
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001700/// EmitDeclare - Emit local variable declaration debug info.
Devang Patel1c0954c2010-02-01 21:39:52 +00001701void CGDebugInfo::EmitDeclare(const VarDecl *VD, unsigned Tag,
Chris Lattneraffb3732008-11-10 06:08:34 +00001702 llvm::Value *Storage, CGBuilderTy &Builder) {
Daniel Dunbar380827c2008-10-17 01:07:56 +00001703 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1704
Devang Patel408dcf62010-03-09 00:44:50 +00001705 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001706 llvm::DIType Ty;
1707 uint64_t XOffset = 0;
1708 if (VD->hasAttr<BlocksAttr>())
1709 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1710 else
1711 Ty = getOrCreateType(VD->getType(), Unit);
Chris Lattner362d8ae2009-05-05 04:57:08 +00001712
Devang Patel67eba802010-05-07 23:05:55 +00001713 // If there is not any debug info for type then do not emit debug info
1714 // for this variable.
1715 if (!Ty)
1716 return;
1717
Chris Lattneraffb3732008-11-10 06:08:34 +00001718 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001719 unsigned Line = getLineNumber(VD->getLocation());
1720 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump11289f42009-09-09 15:08:12 +00001721
Chris Lattneraffb3732008-11-10 06:08:34 +00001722 // Create the descriptor for the variable.
Mike Stump11289f42009-09-09 15:08:12 +00001723 llvm::DIVariable D =
Devang Patelb40f2952009-11-13 19:10:24 +00001724 DebugFactory.CreateVariable(Tag, llvm::DIDescriptor(RegionStack.back()),
Devang Patel1c0954c2010-02-01 21:39:52 +00001725 VD->getName(),
Devang Patel6ccba0f2010-06-05 01:14:40 +00001726 Unit, Line, Ty, CGM.getLangOptions().Optimize);
Chris Lattneraffb3732008-11-10 06:08:34 +00001727 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001728 llvm::Instruction *Call =
Devang Patelaf993bf2009-11-10 23:07:24 +00001729 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Devang Patel94f798c2009-11-12 18:21:39 +00001730
Chris Lattnere675d0f2010-04-01 06:31:43 +00001731 llvm::MDNode *Scope = RegionStack.back();
Chris Lattner18a584b2010-04-02 20:21:43 +00001732 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Sanjiv Gupta18de6242008-05-30 10:30:31 +00001733}
1734
Mike Stump2e722b92009-09-30 02:43:10 +00001735/// EmitDeclare - Emit local variable declaration debug info.
1736void CGDebugInfo::EmitDeclare(const BlockDeclRefExpr *BDRE, unsigned Tag,
1737 llvm::Value *Storage, CGBuilderTy &Builder,
1738 CodeGenFunction *CGF) {
Devang Patel3efd1472010-02-01 21:52:22 +00001739 const ValueDecl *VD = BDRE->getDecl();
Mike Stump2e722b92009-09-30 02:43:10 +00001740 assert(!RegionStack.empty() && "Region stack mismatch, stack empty!");
1741
Devang Patel42fb6f82010-04-26 23:28:46 +00001742 if (Builder.GetInsertBlock() == 0)
Mike Stump2e722b92009-09-30 02:43:10 +00001743 return;
1744
1745 uint64_t XOffset = 0;
Devang Patel408dcf62010-03-09 00:44:50 +00001746 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
Devang Patel535fdaf2010-02-10 18:49:08 +00001747 llvm::DIType Ty;
1748 if (VD->hasAttr<BlocksAttr>())
1749 Ty = EmitTypeForVarWithBlocksAttr(VD, &XOffset);
1750 else
1751 Ty = getOrCreateType(VD->getType(), Unit);
Mike Stump2e722b92009-09-30 02:43:10 +00001752
1753 // Get location information.
Devang Patelc5ffabc2010-05-12 23:46:38 +00001754 unsigned Line = getLineNumber(VD->getLocation());
1755 unsigned Column = getColumnNumber(VD->getLocation());
Mike Stump2e722b92009-09-30 02:43:10 +00001756
Devang Patel3efd1472010-02-01 21:52:22 +00001757 CharUnits offset = CGF->BlockDecls[VD];
Mike Stump2e722b92009-09-30 02:43:10 +00001758 llvm::SmallVector<llvm::Value *, 9> addr;
Chris Lattnerbf784782010-01-25 03:29:35 +00001759 const llvm::Type *Int64Ty = llvm::Type::getInt64Ty(CGM.getLLVMContext());
1760 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1761 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
1762 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001763 if (BDRE->isByRef()) {
Chris Lattnerbf784782010-01-25 03:29:35 +00001764 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1765 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001766 // offset of __forwarding field
1767 offset = CharUnits::fromQuantity(CGF->LLVMPointerWidth/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001768 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
1769 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpDeref));
1770 addr.push_back(llvm::ConstantInt::get(Int64Ty, llvm::DIFactory::OpPlus));
Ken Dyck40775002010-01-11 17:06:35 +00001771 // offset of x field
1772 offset = CharUnits::fromQuantity(XOffset/8);
Chris Lattnerbf784782010-01-25 03:29:35 +00001773 addr.push_back(llvm::ConstantInt::get(Int64Ty, offset.getQuantity()));
Mike Stump2e722b92009-09-30 02:43:10 +00001774 }
1775
1776 // Create the descriptor for the variable.
1777 llvm::DIVariable D =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001778 DebugFactory.CreateComplexVariable(Tag,
1779 llvm::DIDescriptor(RegionStack.back()),
Devang Patel3efd1472010-02-01 21:52:22 +00001780 VD->getName(), Unit, Line, Ty,
Benjamin Kramer3e0c5272010-09-21 15:59:59 +00001781 addr.data(), addr.size());
Mike Stump2e722b92009-09-30 02:43:10 +00001782 // Insert an llvm.dbg.declare into the current block.
Devang Patel53485152009-11-11 19:10:19 +00001783 llvm::Instruction *Call =
Chris Lattner83b0dd12010-01-25 03:34:56 +00001784 DebugFactory.InsertDeclare(Storage, D, Builder.GetInsertBlock());
Chris Lattner5e124bf2009-12-28 21:44:41 +00001785
Chris Lattnere675d0f2010-04-01 06:31:43 +00001786 llvm::MDNode *Scope = RegionStack.back();
Devang Patel82bbfb52010-05-10 23:48:38 +00001787 Call->setDebugLoc(llvm::DebugLoc::get(Line, Column, Scope));
Mike Stump2e722b92009-09-30 02:43:10 +00001788}
1789
Devang Patel3efd1472010-02-01 21:52:22 +00001790void CGDebugInfo::EmitDeclareOfAutoVariable(const VarDecl *VD,
Chris Lattneraffb3732008-11-10 06:08:34 +00001791 llvm::Value *Storage,
1792 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001793 EmitDeclare(VD, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001794}
1795
Mike Stump2e722b92009-09-30 02:43:10 +00001796void CGDebugInfo::EmitDeclareOfBlockDeclRefVariable(
1797 const BlockDeclRefExpr *BDRE, llvm::Value *Storage, CGBuilderTy &Builder,
1798 CodeGenFunction *CGF) {
1799 EmitDeclare(BDRE, llvm::dwarf::DW_TAG_auto_variable, Storage, Builder, CGF);
1800}
1801
Chris Lattneraffb3732008-11-10 06:08:34 +00001802/// EmitDeclareOfArgVariable - Emit call to llvm.dbg.declare for an argument
1803/// variable declaration.
Devang Patel3efd1472010-02-01 21:52:22 +00001804void CGDebugInfo::EmitDeclareOfArgVariable(const VarDecl *VD, llvm::Value *AI,
Chris Lattneraffb3732008-11-10 06:08:34 +00001805 CGBuilderTy &Builder) {
Devang Patel3efd1472010-02-01 21:52:22 +00001806 EmitDeclare(VD, llvm::dwarf::DW_TAG_arg_variable, AI, Builder);
Chris Lattneraffb3732008-11-10 06:08:34 +00001807}
1808
1809
1810
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001811/// EmitGlobalVariable - Emit information about a global variable.
Mike Stump11289f42009-09-09 15:08:12 +00001812void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel7b7f46f2010-02-01 21:34:11 +00001813 const VarDecl *D) {
1814
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001815 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001816 llvm::DIFile Unit = getOrCreateFile(D->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001817 unsigned LineNo = getLineNumber(D->getLocation());
Chris Lattner86d7d912008-11-24 03:54:41 +00001818
Devang Patel7b7f46f2010-02-01 21:34:11 +00001819 QualType T = D->getType();
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001820 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001821
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001822 // CodeGen turns int[] into int[1] so we'll do the same here.
1823 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001824
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001825 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001826 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001827
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001828 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Anders Carlssonf7a9a922008-11-26 17:40:42 +00001829 ArrayType::Normal, 0);
1830 }
Devang Pateldfcd0662010-04-29 17:48:37 +00001831 llvm::StringRef DeclName = D->getName();
Devang Patel98f21712010-05-13 23:52:37 +00001832 llvm::StringRef LinkageName;
Devang Patelec2a9ab2010-05-14 16:55:25 +00001833 if (D->getDeclContext() && !isa<FunctionDecl>(D->getDeclContext()))
Devang Patel98f21712010-05-13 23:52:37 +00001834 LinkageName = Var->getName();
Devang Patel7b7f46f2010-02-01 21:34:11 +00001835 llvm::DIDescriptor DContext =
1836 getContextDescriptor(dyn_cast<Decl>(D->getDeclContext()), Unit);
Devang Patel98f21712010-05-13 23:52:37 +00001837 DebugFactory.CreateGlobalVariable(DContext, DeclName, DeclName, LinkageName,
1838 Unit, LineNo, getOrCreateType(T, Unit),
Chris Lattneraffb3732008-11-10 06:08:34 +00001839 Var->hasInternalLinkage(),
1840 true/*definition*/, Var);
Sanjiv Gupta158143a2008-06-05 08:59:10 +00001841}
1842
Devang Patelf4c205b2009-02-26 21:10:26 +00001843/// EmitGlobalVariable - Emit information about an objective-c interface.
Mike Stump11289f42009-09-09 15:08:12 +00001844void CGDebugInfo::EmitGlobalVariable(llvm::GlobalVariable *Var,
Devang Patel3efd1472010-02-01 21:52:22 +00001845 ObjCInterfaceDecl *ID) {
Devang Patelf4c205b2009-02-26 21:10:26 +00001846 // Create global variable debug descriptor.
Devang Patel408dcf62010-03-09 00:44:50 +00001847 llvm::DIFile Unit = getOrCreateFile(ID->getLocation());
Devang Patelc5ffabc2010-05-12 23:46:38 +00001848 unsigned LineNo = getLineNumber(ID->getLocation());
Devang Patelf4c205b2009-02-26 21:10:26 +00001849
Devang Patel3efd1472010-02-01 21:52:22 +00001850 llvm::StringRef Name = ID->getName();
Devang Patelf4c205b2009-02-26 21:10:26 +00001851
Devang Patel3efd1472010-02-01 21:52:22 +00001852 QualType T = CGM.getContext().getObjCInterfaceType(ID);
Devang Patelf4c205b2009-02-26 21:10:26 +00001853 if (T->isIncompleteArrayType()) {
Mike Stump11289f42009-09-09 15:08:12 +00001854
Devang Patelf4c205b2009-02-26 21:10:26 +00001855 // CodeGen turns int[] into int[1] so we'll do the same here.
1856 llvm::APSInt ConstVal(32);
Mike Stump11289f42009-09-09 15:08:12 +00001857
Devang Patelf4c205b2009-02-26 21:10:26 +00001858 ConstVal = 1;
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001859 QualType ET = CGM.getContext().getAsArrayType(T)->getElementType();
Mike Stump11289f42009-09-09 15:08:12 +00001860
Anders Carlsson3efc6e62009-12-06 18:00:51 +00001861 T = CGM.getContext().getConstantArrayType(ET, ConstVal,
Devang Patelf4c205b2009-02-26 21:10:26 +00001862 ArrayType::Normal, 0);
1863 }
1864
Devang Patele4f2b2a2009-10-20 18:26:30 +00001865 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit, LineNo,
Devang Patelf4c205b2009-02-26 21:10:26 +00001866 getOrCreateType(T, Unit),
1867 Var->hasInternalLinkage(),
1868 true/*definition*/, Var);
1869}
Devang Patel973f2eb2010-02-01 19:16:32 +00001870
Devang Pateldc866e12010-08-10 17:53:33 +00001871/// EmitGlobalVariable - Emit global variable's debug info.
1872void CGDebugInfo::EmitGlobalVariable(const ValueDecl *VD,
1873 llvm::ConstantInt *Init,
Devang Patele03edfd2010-08-10 07:24:25 +00001874 CGBuilderTy &Builder) {
1875 // Create the descriptor for the variable.
1876 llvm::DIFile Unit = getOrCreateFile(VD->getLocation());
1877 llvm::StringRef Name = VD->getName();
Devang Patel76e3b532010-08-10 18:27:15 +00001878 llvm::DIType Ty = getOrCreateType(VD->getType(), Unit);
Devang Patel41c20972010-08-23 22:07:25 +00001879 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(VD)) {
1880 if (const EnumDecl *ED = dyn_cast<EnumDecl>(ECD->getDeclContext()))
1881 Ty = CreateEnumType(ED, Unit);
1882 }
Devang Patel76e3b532010-08-10 18:27:15 +00001883 // Do not use DIGlobalVariable for enums.
1884 if (Ty.getTag() == llvm::dwarf::DW_TAG_enumeration_type)
1885 return;
Devang Patele03edfd2010-08-10 07:24:25 +00001886 DebugFactory.CreateGlobalVariable(Unit, Name, Name, Name, Unit,
1887 getLineNumber(VD->getLocation()),
Devang Patel81665712010-08-10 20:16:57 +00001888 Ty, true, true, Init);
Devang Patele03edfd2010-08-10 07:24:25 +00001889}
1890
Devang Patel973f2eb2010-02-01 19:16:32 +00001891/// getOrCreateNamesSpace - Return namespace descriptor for the given
1892/// namespace decl.
1893llvm::DINameSpace
1894CGDebugInfo::getOrCreateNameSpace(const NamespaceDecl *NSDecl,
1895 llvm::DIDescriptor Unit) {
1896 llvm::DenseMap<const NamespaceDecl *, llvm::WeakVH>::iterator I =
1897 NameSpaceCache.find(NSDecl);
1898 if (I != NameSpaceCache.end())
1899 return llvm::DINameSpace(cast<llvm::MDNode>(I->second));
1900
Devang Patelc5ffabc2010-05-12 23:46:38 +00001901 unsigned LineNo = getLineNumber(NSDecl->getLocation());
Devang Patel973f2eb2010-02-01 19:16:32 +00001902
1903 llvm::DIDescriptor Context =
Devang Patel7b7f46f2010-02-01 21:34:11 +00001904 getContextDescriptor(dyn_cast<Decl>(NSDecl->getDeclContext()), Unit);
Devang Patel973f2eb2010-02-01 19:16:32 +00001905 llvm::DINameSpace NS =
1906 DebugFactory.CreateNameSpace(Context, NSDecl->getName(),
Devang Patel96b7f552010-08-27 17:47:47 +00001907 llvm::DIFile(Unit), LineNo);
Devang Patelba4ad7f2010-05-07 18:12:35 +00001908 NameSpaceCache[NSDecl] = llvm::WeakVH(NS);
Devang Patel973f2eb2010-02-01 19:16:32 +00001909 return NS;
1910}