blob: 56689f58136ef4cc2615e060d6de4d63398c0968 [file] [log] [blame]
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001//===-- SymbolFileDWARF.cpp ------------------------------------*- C++ -*-===//
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#include "SymbolFileDWARF.h"
11
12// Other libraries and framework includes
13#include "clang/AST/ASTConsumer.h"
14#include "clang/AST/ASTContext.h"
15#include "clang/AST/Decl.h"
16#include "clang/AST/DeclGroup.h"
Jim Inghame3ae82a2011-11-12 01:36:43 +000017#include "clang/AST/DeclObjC.h"
Greg Clayton3c2e3ae2012-02-06 06:42:51 +000018#include "clang/AST/DeclTemplate.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000019#include "clang/Basic/Builtins.h"
20#include "clang/Basic/IdentifierTable.h"
21#include "clang/Basic/LangOptions.h"
22#include "clang/Basic/SourceManager.h"
23#include "clang/Basic/TargetInfo.h"
24#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000025#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000026
Sean Callanancc427fa2011-07-30 02:42:06 +000027#include "llvm/Support/Casting.h"
28
Chris Lattner30fdc8d2010-06-08 16:52:24 +000029#include "lldb/Core/Module.h"
30#include "lldb/Core/PluginManager.h"
31#include "lldb/Core/RegularExpression.h"
32#include "lldb/Core/Scalar.h"
33#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000034#include "lldb/Core/StreamFile.h"
Jim Ingham318c9f22011-08-26 19:44:13 +000035#include "lldb/Core/StreamString.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Core/Timer.h"
37#include "lldb/Core/Value.h"
38
Greg Clayton20568dd2011-10-13 23:13:20 +000039#include "lldb/Host/Host.h"
40
Chris Lattner30fdc8d2010-06-08 16:52:24 +000041#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000042#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000043#include "lldb/Symbol/CompileUnit.h"
44#include "lldb/Symbol/LineTable.h"
45#include "lldb/Symbol/ObjectFile.h"
46#include "lldb/Symbol/SymbolVendor.h"
47#include "lldb/Symbol/VariableList.h"
48
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000049#include "lldb/Target/ObjCLanguageRuntime.h"
Jim Ingham4cda6e02011-10-07 22:23:45 +000050#include "lldb/Target/CPPLanguageRuntime.h"
Jim Inghamb7f6b2f2011-09-08 22:13:49 +000051
Chris Lattner30fdc8d2010-06-08 16:52:24 +000052#include "DWARFCompileUnit.h"
53#include "DWARFDebugAbbrev.h"
54#include "DWARFDebugAranges.h"
55#include "DWARFDebugInfo.h"
56#include "DWARFDebugInfoEntry.h"
57#include "DWARFDebugLine.h"
58#include "DWARFDebugPubnames.h"
59#include "DWARFDebugRanges.h"
Greg Claytona8022fa2012-04-24 21:22:41 +000060#include "DWARFDeclContext.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000061#include "DWARFDIECollection.h"
62#include "DWARFFormValue.h"
63#include "DWARFLocationList.h"
64#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000065#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000066
67#include <map>
68
Greg Clayton62742b12010-11-11 01:09:45 +000069//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000070
71#ifdef ENABLE_DEBUG_PRINTF
72#include <stdio.h>
73#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
74#else
75#define DEBUG_PRINTF(fmt, ...)
76#endif
77
Greg Clayton594e5ed2010-09-27 21:07:38 +000078#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000079
80using namespace lldb;
81using namespace lldb_private;
82
Greg Clayton219cf312012-03-30 00:51:13 +000083//static inline bool
84//child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
85//{
86// switch (tag)
87// {
88// default:
89// break;
90// case DW_TAG_subprogram:
91// case DW_TAG_inlined_subroutine:
92// case DW_TAG_class_type:
93// case DW_TAG_structure_type:
94// case DW_TAG_union_type:
95// return true;
96// }
97// return false;
98//}
99//
Sean Callananc7fbf732010-08-06 00:32:49 +0000100static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000101DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000102{
103 switch (dwarf_accessibility)
104 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000105 case DW_ACCESS_public: return eAccessPublic;
106 case DW_ACCESS_private: return eAccessPrivate;
107 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000108 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000109 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000110 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000111}
112
Greg Clayton1fba87112012-02-09 20:03:49 +0000113#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
114
115class DIEStack
116{
117public:
118
119 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
120 {
121 m_dies.push_back (DIEInfo(cu, die));
122 }
123
124
125 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
126 {
127 StreamString log_strm;
128 const size_t n = m_dies.size();
Greg Clayton43e0af02012-09-18 18:04:04 +0000129 log_strm.Printf("DIEStack[%llu]:\n", (uint64_t)n);
Greg Clayton1fba87112012-02-09 20:03:49 +0000130 for (size_t i=0; i<n; i++)
131 {
132 DWARFCompileUnit *cu = m_dies[i].cu;
133 const DWARFDebugInfoEntry *die = m_dies[i].die;
134 std::string qualified_name;
135 die->GetQualifiedName(dwarf, cu, qualified_name);
Greg Clayton43e0af02012-09-18 18:04:04 +0000136 log_strm.Printf ("[%llu] 0x%8.8x: %s name='%s'\n",
137 (uint64_t)i,
Greg Clayton1fba87112012-02-09 20:03:49 +0000138 die->GetOffset(),
139 DW_TAG_value_to_name(die->Tag()),
140 qualified_name.c_str());
141 }
142 log->PutCString(log_strm.GetData());
143 }
144 void Pop ()
145 {
146 m_dies.pop_back();
147 }
148
149 class ScopedPopper
150 {
151 public:
152 ScopedPopper (DIEStack &die_stack) :
153 m_die_stack (die_stack),
154 m_valid (false)
155 {
156 }
157
158 void
159 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
160 {
161 m_valid = true;
162 m_die_stack.Push (cu, die);
163 }
164
165 ~ScopedPopper ()
166 {
167 if (m_valid)
168 m_die_stack.Pop();
169 }
170
171
172
173 protected:
174 DIEStack &m_die_stack;
175 bool m_valid;
176 };
177
178protected:
179 struct DIEInfo {
180 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
181 cu(c),
182 die(d)
183 {
184 }
185 DWARFCompileUnit *cu;
186 const DWARFDebugInfoEntry *die;
187 };
188 typedef std::vector<DIEInfo> Stack;
189 Stack m_dies;
190};
191#endif
192
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000193void
194SymbolFileDWARF::Initialize()
195{
196 LogChannelDWARF::Initialize();
197 PluginManager::RegisterPlugin (GetPluginNameStatic(),
198 GetPluginDescriptionStatic(),
199 CreateInstance);
200}
201
202void
203SymbolFileDWARF::Terminate()
204{
205 PluginManager::UnregisterPlugin (CreateInstance);
206 LogChannelDWARF::Initialize();
207}
208
209
210const char *
211SymbolFileDWARF::GetPluginNameStatic()
212{
Greg Claytond4a2b372011-09-12 23:21:58 +0000213 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000214}
215
216const char *
217SymbolFileDWARF::GetPluginDescriptionStatic()
218{
219 return "DWARF and DWARF3 debug symbol file reader.";
220}
221
222
223SymbolFile*
224SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
225{
226 return new SymbolFileDWARF(obj_file);
227}
228
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000229TypeList *
230SymbolFileDWARF::GetTypeList ()
231{
Greg Clayton1f746072012-08-29 21:13:06 +0000232 if (GetDebugMapSymfile ())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000233 return m_debug_map_symfile->GetTypeList();
234 return m_obj_file->GetModule()->GetTypeList();
235
236}
237
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000238//----------------------------------------------------------------------
239// Gets the first parent that is a lexical block, function or inlined
240// subroutine, or compile unit.
241//----------------------------------------------------------------------
242static const DWARFDebugInfoEntry *
243GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
244{
245 const DWARFDebugInfoEntry *die;
246 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
247 {
248 dw_tag_t tag = die->Tag();
249
250 switch (tag)
251 {
252 case DW_TAG_compile_unit:
253 case DW_TAG_subprogram:
254 case DW_TAG_inlined_subroutine:
255 case DW_TAG_lexical_block:
256 return die;
257 }
258 }
259 return NULL;
260}
261
262
Greg Clayton450e3f32010-10-12 02:24:53 +0000263SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
264 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000265 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton1f746072012-08-29 21:13:06 +0000266 m_debug_map_module_wp (),
Greg Clayton450e3f32010-10-12 02:24:53 +0000267 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000268 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000269 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000270 m_data_debug_abbrev (),
271 m_data_debug_aranges (),
272 m_data_debug_frame (),
273 m_data_debug_info (),
274 m_data_debug_line (),
275 m_data_debug_loc (),
276 m_data_debug_ranges (),
277 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000278 m_data_apple_names (),
279 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000280 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000281 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 m_info(),
283 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000284 m_apple_names_ap (),
285 m_apple_types_ap (),
286 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000287 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000288 m_function_basename_index(),
289 m_function_fullname_index(),
290 m_function_method_index(),
291 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000292 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000293 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000294 m_type_index(),
295 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000296 m_indexed (false),
297 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000298 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000299 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000300 m_ranges(),
301 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000302{
303}
304
305SymbolFileDWARF::~SymbolFileDWARF()
306{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000307 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000308 {
309 ModuleSP module_sp (m_obj_file->GetModule());
310 if (module_sp)
311 module_sp->GetClangASTContext().RemoveExternalSource ();
312 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000313}
314
315static const ConstString &
316GetDWARFMachOSegmentName ()
317{
318 static ConstString g_dwarf_section_name ("__DWARF");
319 return g_dwarf_section_name;
320}
321
Greg Claytone576ab22011-02-15 00:19:15 +0000322UniqueDWARFASTTypeMap &
323SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
324{
Greg Clayton1f746072012-08-29 21:13:06 +0000325 if (GetDebugMapSymfile ())
Greg Claytone576ab22011-02-15 00:19:15 +0000326 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
327 return m_unique_ast_type_map;
328}
329
Greg Clayton6beaaa62011-01-17 03:46:26 +0000330ClangASTContext &
331SymbolFileDWARF::GetClangASTContext ()
332{
Greg Clayton1f746072012-08-29 21:13:06 +0000333 if (GetDebugMapSymfile ())
Greg Clayton6beaaa62011-01-17 03:46:26 +0000334 return m_debug_map_symfile->GetClangASTContext ();
335
336 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
337 if (!m_is_external_ast_source)
338 {
339 m_is_external_ast_source = true;
340 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
341 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
342 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000343 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000344 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000345 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000346 ast.SetExternalSource (ast_source_ap);
347 }
348 return ast;
349}
350
351void
352SymbolFileDWARF::InitializeObject()
353{
354 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000355 ModuleSP module_sp (m_obj_file->GetModule());
356 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000357 {
358 const SectionList *section_list = m_obj_file->GetSectionList();
359
360 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
361
362 // Memory map the DWARF mach-o segment so we have everything mmap'ed
363 // to keep our heap memory usage down.
364 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000365 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000366 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000367 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000368 if (m_data_apple_names.GetByteSize() > 0)
369 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000370 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
371 if (m_apple_names_ap->IsValid())
372 m_using_apple_tables = true;
373 else
Greg Clayton7f995132011-10-04 22:41:51 +0000374 m_apple_names_ap.reset();
375 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000376 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000377 if (m_data_apple_types.GetByteSize() > 0)
378 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000379 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
380 if (m_apple_types_ap->IsValid())
381 m_using_apple_tables = true;
382 else
Greg Clayton7f995132011-10-04 22:41:51 +0000383 m_apple_types_ap.reset();
384 }
385
386 get_apple_namespaces_data();
387 if (m_data_apple_namespaces.GetByteSize() > 0)
388 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000389 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
390 if (m_apple_namespaces_ap->IsValid())
391 m_using_apple_tables = true;
392 else
Greg Clayton7f995132011-10-04 22:41:51 +0000393 m_apple_namespaces_ap.reset();
394 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000395
Greg Clayton5009f9d2011-10-27 17:55:14 +0000396 get_apple_objc_data();
397 if (m_data_apple_objc.GetByteSize() > 0)
398 {
399 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
400 if (m_apple_objc_ap->IsValid())
401 m_using_apple_tables = true;
402 else
403 m_apple_objc_ap.reset();
404 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000405}
406
407bool
408SymbolFileDWARF::SupportedVersion(uint16_t version)
409{
410 return version == 2 || version == 3;
411}
412
413uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000414SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415{
416 uint32_t abilities = 0;
417 if (m_obj_file != NULL)
418 {
419 const Section* section = NULL;
420 const SectionList *section_list = m_obj_file->GetSectionList();
421 if (section_list == NULL)
422 return 0;
423
424 uint64_t debug_abbrev_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000425 uint64_t debug_info_file_size = 0;
426 uint64_t debug_line_file_size = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427
Greg Clayton6beaaa62011-01-17 03:46:26 +0000428 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000429
430 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000431 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000432
Greg Clayton4ceb9982010-07-21 22:54:26 +0000433 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434 if (section != NULL)
435 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000436 debug_info_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000437
Greg Clayton4ceb9982010-07-21 22:54:26 +0000438 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000440 debug_abbrev_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 else
442 m_flags.Set (flagsGotDebugAbbrevData);
443
Greg Clayton4ceb9982010-07-21 22:54:26 +0000444 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000445 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000446 m_flags.Set (flagsGotDebugArangesData);
447
Greg Clayton4ceb9982010-07-21 22:54:26 +0000448 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000449 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000450 m_flags.Set (flagsGotDebugFrameData);
451
Greg Clayton4ceb9982010-07-21 22:54:26 +0000452 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 if (section)
Greg Clayton47037bc2012-03-27 02:40:46 +0000454 debug_line_file_size = section->GetFileSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000455 else
456 m_flags.Set (flagsGotDebugLineData);
457
Greg Clayton4ceb9982010-07-21 22:54:26 +0000458 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000459 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000460 m_flags.Set (flagsGotDebugLocData);
461
Greg Clayton4ceb9982010-07-21 22:54:26 +0000462 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000463 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000464 m_flags.Set (flagsGotDebugMacInfoData);
465
Greg Clayton4ceb9982010-07-21 22:54:26 +0000466 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000467 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000468 m_flags.Set (flagsGotDebugPubNamesData);
469
Greg Clayton4ceb9982010-07-21 22:54:26 +0000470 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000471 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000472 m_flags.Set (flagsGotDebugPubTypesData);
473
Greg Clayton4ceb9982010-07-21 22:54:26 +0000474 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000475 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000476 m_flags.Set (flagsGotDebugRangesData);
477
Greg Clayton4ceb9982010-07-21 22:54:26 +0000478 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Greg Clayton23f59502012-07-17 03:23:13 +0000479 if (!section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000480 m_flags.Set (flagsGotDebugStrData);
481 }
Greg Clayton6c596612012-05-18 21:47:20 +0000482 else
483 {
484 const char *symfile_dir_cstr = m_obj_file->GetFileSpec().GetDirectory().GetCString();
485 if (symfile_dir_cstr)
486 {
487 if (strcasestr(symfile_dir_cstr, ".dsym"))
488 {
489 if (m_obj_file->GetType() == ObjectFile::eTypeDebugInfo)
490 {
491 // We have a dSYM file that didn't have a any debug info.
492 // If the string table has a size of 1, then it was made from
493 // an executable with no debug info, or from an executable that
494 // was stripped.
495 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
496 if (section && section->GetFileSize() == 1)
497 {
498 m_obj_file->GetModule()->ReportWarning ("empty dSYM file detected, dSYM was created with an executable with no debug info.");
499 }
500 }
501 }
502 }
503 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000504
505 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
506 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
507
508 if (debug_line_file_size > 0)
509 abilities |= LineTables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000510 }
511 return abilities;
512}
513
514const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000515SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000516{
517 if (m_flags.IsClear (got_flag))
518 {
519 m_flags.Set (got_flag);
520 const SectionList *section_list = m_obj_file->GetSectionList();
521 if (section_list)
522 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000523 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
524 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000525 {
526 // See if we memory mapped the DWARF segment?
527 if (m_dwarf_data.GetByteSize())
528 {
Greg Clayton47037bc2012-03-27 02:40:46 +0000529 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetFileSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000530 }
531 else
532 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000533 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000534 data.Clear();
535 }
536 }
537 }
538 }
539 return data;
540}
541
542const DataExtractor&
543SymbolFileDWARF::get_debug_abbrev_data()
544{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000545 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000546}
547
548const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000549SymbolFileDWARF::get_debug_aranges_data()
550{
551 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
552}
553
554const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555SymbolFileDWARF::get_debug_frame_data()
556{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000557 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558}
559
560const DataExtractor&
561SymbolFileDWARF::get_debug_info_data()
562{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000563 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000564}
565
566const DataExtractor&
567SymbolFileDWARF::get_debug_line_data()
568{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000569 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570}
571
572const DataExtractor&
573SymbolFileDWARF::get_debug_loc_data()
574{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000575 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576}
577
578const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579SymbolFileDWARF::get_debug_ranges_data()
580{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000581 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000582}
583
584const DataExtractor&
585SymbolFileDWARF::get_debug_str_data()
586{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000587 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000588}
589
Greg Claytonf9eec202011-09-01 23:16:13 +0000590const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000591SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000592{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000593 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000594}
595
596const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000597SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000598{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000599 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000600}
601
Greg Clayton7f995132011-10-04 22:41:51 +0000602const DataExtractor&
603SymbolFileDWARF::get_apple_namespaces_data()
604{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000605 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
606}
607
608const DataExtractor&
609SymbolFileDWARF::get_apple_objc_data()
610{
611 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000612}
613
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000614
615DWARFDebugAbbrev*
616SymbolFileDWARF::DebugAbbrev()
617{
618 if (m_abbr.get() == NULL)
619 {
620 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
621 if (debug_abbrev_data.GetByteSize() > 0)
622 {
623 m_abbr.reset(new DWARFDebugAbbrev());
624 if (m_abbr.get())
625 m_abbr->Parse(debug_abbrev_data);
626 }
627 }
628 return m_abbr.get();
629}
630
631const DWARFDebugAbbrev*
632SymbolFileDWARF::DebugAbbrev() const
633{
634 return m_abbr.get();
635}
636
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000637
638DWARFDebugInfo*
639SymbolFileDWARF::DebugInfo()
640{
641 if (m_info.get() == NULL)
642 {
643 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
644 if (get_debug_info_data().GetByteSize() > 0)
645 {
646 m_info.reset(new DWARFDebugInfo());
647 if (m_info.get())
648 {
649 m_info->SetDwarfData(this);
650 }
651 }
652 }
653 return m_info.get();
654}
655
656const DWARFDebugInfo*
657SymbolFileDWARF::DebugInfo() const
658{
659 return m_info.get();
660}
661
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000662DWARFCompileUnit*
Greg Clayton1f746072012-08-29 21:13:06 +0000663SymbolFileDWARF::GetDWARFCompileUnit(lldb_private::CompileUnit *comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000664{
665 DWARFDebugInfo* info = DebugInfo();
Greg Clayton1f746072012-08-29 21:13:06 +0000666 if (info)
667 {
668 if (GetDebugMapSymfile ())
669 {
670 // The debug map symbol file made the compile units for this DWARF
671 // file which is .o file with DWARF in it, and we should have
672 // only 1 compile unit which is at offset zero in the DWARF.
673 // TODO: modify to support LTO .o files where each .o file might
674 // have multiple DW_TAG_compile_unit tags.
675 return info->GetCompileUnit(0).get();
676 }
677 else
678 {
679 // Just a normal DWARF file whose user ID for the compile unit is
680 // the DWARF offset itself
681 return info->GetCompileUnit((dw_offset_t)comp_unit->GetID()).get();
682 }
683 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 return NULL;
685}
686
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000687
688DWARFDebugRanges*
689SymbolFileDWARF::DebugRanges()
690{
691 if (m_ranges.get() == NULL)
692 {
693 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
694 if (get_debug_ranges_data().GetByteSize() > 0)
695 {
696 m_ranges.reset(new DWARFDebugRanges());
697 if (m_ranges.get())
698 m_ranges->Extract(this);
699 }
700 }
701 return m_ranges.get();
702}
703
704const DWARFDebugRanges*
705SymbolFileDWARF::DebugRanges() const
706{
707 return m_ranges.get();
708}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709
Greg Clayton53eb1c22012-04-02 22:59:12 +0000710lldb::CompUnitSP
711SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000712{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000713 CompUnitSP cu_sp;
714 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000716 CompileUnit *comp_unit = (CompileUnit*)dwarf_cu->GetUserData();
717 if (comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000718 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000719 // We already parsed this compile unit, had out a shared pointer to it
720 cu_sp = comp_unit->shared_from_this();
721 }
722 else
723 {
Greg Clayton1f746072012-08-29 21:13:06 +0000724 if (GetDebugMapSymfile ())
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
Greg Clayton1f746072012-08-29 21:13:06 +0000726 // Let the debug map create the compile unit
727 cu_sp = m_debug_map_symfile->GetCompileUnit(this);
728 dwarf_cu->SetUserData(cu_sp.get());
729 }
730 else
731 {
732 ModuleSP module_sp (m_obj_file->GetModule());
733 if (module_sp)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000734 {
Greg Clayton1f746072012-08-29 21:13:06 +0000735 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly ();
736 if (cu_die)
Greg Clayton53eb1c22012-04-02 22:59:12 +0000737 {
Greg Clayton1f746072012-08-29 21:13:06 +0000738 const char * cu_die_name = cu_die->GetName(this, dwarf_cu);
739 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
740 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
741 if (cu_die_name)
742 {
743 std::string ramapped_file;
744 FileSpec cu_file_spec;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000745
Greg Clayton1f746072012-08-29 21:13:06 +0000746 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Greg Clayton53eb1c22012-04-02 22:59:12 +0000747 {
Greg Clayton1f746072012-08-29 21:13:06 +0000748 // If we have a full path to the compile unit, we don't need to resolve
749 // the file. This can be expensive e.g. when the source files are NFS mounted.
750 if (module_sp->RemapSourceFile(cu_die_name, ramapped_file))
751 cu_file_spec.SetFile (ramapped_file.c_str(), false);
752 else
753 cu_file_spec.SetFile (cu_die_name, false);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000754 }
755 else
756 {
Greg Clayton1f746072012-08-29 21:13:06 +0000757 std::string fullpath(cu_comp_dir);
758 if (*fullpath.rbegin() != '/')
759 fullpath += '/';
760 fullpath += cu_die_name;
761 if (module_sp->RemapSourceFile (fullpath.c_str(), ramapped_file))
762 cu_file_spec.SetFile (ramapped_file.c_str(), false);
763 else
764 cu_file_spec.SetFile (fullpath.c_str(), false);
765 }
766
767 cu_sp.reset(new CompileUnit (module_sp,
768 dwarf_cu,
769 cu_file_spec,
770 MakeUserID(dwarf_cu->GetOffset()),
771 cu_language));
772 if (cu_sp)
773 {
774 dwarf_cu->SetUserData(cu_sp.get());
775
Greg Clayton53eb1c22012-04-02 22:59:12 +0000776 // Figure out the compile unit index if we weren't given one
777 if (cu_idx == UINT32_MAX)
778 DebugInfo()->GetCompileUnit(dwarf_cu->GetOffset(), &cu_idx);
779
780 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(cu_idx, cu_sp);
781 }
782 }
783 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000784 }
785 }
786 }
787 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000788 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789}
790
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000791uint32_t
792SymbolFileDWARF::GetNumCompileUnits()
793{
794 DWARFDebugInfo* info = DebugInfo();
795 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000796 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 return 0;
798}
799
800CompUnitSP
801SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
802{
Greg Clayton53eb1c22012-04-02 22:59:12 +0000803 CompUnitSP cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804 DWARFDebugInfo* info = DebugInfo();
805 if (info)
806 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000807 DWARFCompileUnit* dwarf_cu = info->GetCompileUnitAtIndex(cu_idx);
808 if (dwarf_cu)
809 cu_sp = ParseCompileUnit(dwarf_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000810 }
Greg Clayton53eb1c22012-04-02 22:59:12 +0000811 return cu_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000812}
813
814static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000815AddRangesToBlock (Block& block,
816 DWARFDebugRanges::RangeList& ranges,
817 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000818{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000819 const size_t num_ranges = ranges.GetSize();
820 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000821 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000822 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
823 const addr_t range_base = range.GetRangeBase();
824 assert (range_base >= block_base_addr);
825 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000826 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000827 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000828}
829
830
831Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000832SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833{
834 DWARFDebugRanges::RangeList func_ranges;
835 const char *name = NULL;
836 const char *mangled = NULL;
837 int decl_file = 0;
838 int decl_line = 0;
839 int decl_column = 0;
840 int call_file = 0;
841 int call_line = 0;
842 int call_column = 0;
843 DWARFExpression frame_base;
844
Greg Claytonc93237c2010-10-01 20:48:32 +0000845 assert (die->Tag() == DW_TAG_subprogram);
846
847 if (die->Tag() != DW_TAG_subprogram)
848 return NULL;
849
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000850 if (die->GetDIENamesAndRanges (this,
851 dwarf_cu,
852 name,
853 mangled,
854 func_ranges,
855 decl_file,
856 decl_line,
857 decl_column,
858 call_file,
859 call_line,
860 call_column,
861 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862 {
863 // Union of all ranges in the function DIE (if the function is discontiguous)
864 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000865 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
866 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
868 {
869 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
870 if (func_range.GetBaseAddress().IsValid())
871 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
872 }
873
874 if (func_range.GetBaseAddress().IsValid())
875 {
876 Mangled func_name;
877 if (mangled)
Greg Clayton037520e2012-07-18 23:18:10 +0000878 func_name.SetValue(ConstString(mangled), true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 else if (name)
Greg Clayton037520e2012-07-18 23:18:10 +0000880 func_name.SetValue(ConstString(name), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000881
882 FunctionSP func_sp;
883 std::auto_ptr<Declaration> decl_ap;
884 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000885 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
886 decl_line,
887 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000888
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000889 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000890 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000891
892 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
893
894 func_range.GetBaseAddress().ResolveLinkedAddress();
895
Greg Clayton81c22f62011-10-19 18:09:39 +0000896 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000897 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000898 func_user_id, // UserID is the DIE offset
899 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000900 func_name,
901 func_type,
902 func_range)); // first address range
903
904 if (func_sp.get() != NULL)
905 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000906 if (frame_base.IsValid())
907 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000908 sc.comp_unit->AddFunction(func_sp);
909 return func_sp.get();
910 }
911 }
912 }
913 return NULL;
914}
915
Greg Clayton1f746072012-08-29 21:13:06 +0000916lldb::LanguageType
917SymbolFileDWARF::ParseCompileUnitLanguage (const SymbolContext& sc)
918{
919 assert (sc.comp_unit);
920 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
921 if (dwarf_cu)
922 {
923 const DWARFDebugInfoEntry *die = dwarf_cu->GetCompileUnitDIEOnly();
Jim Ingham28eb5712012-10-12 17:34:26 +0000924 if (die)
925 {
926 const uint32_t language = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_language, 0);
927 if (language)
928 return (lldb::LanguageType)language;
929 }
Greg Clayton1f746072012-08-29 21:13:06 +0000930 }
931 return eLanguageTypeUnknown;
932}
933
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000934size_t
935SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
936{
937 assert (sc.comp_unit);
938 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +0000939 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000940 if (dwarf_cu)
941 {
942 DWARFDIECollection function_dies;
Greg Clayton1f746072012-08-29 21:13:06 +0000943 const size_t num_functions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000944 size_t func_idx;
Greg Clayton1f746072012-08-29 21:13:06 +0000945 for (func_idx = 0; func_idx < num_functions; ++func_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000946 {
947 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000948 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000949 {
950 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
951 ++functions_added;
952 }
953 }
954 //FixupTypes();
955 }
956 return functions_added;
957}
958
959bool
960SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
961{
962 assert (sc.comp_unit);
Greg Clayton1f746072012-08-29 21:13:06 +0000963 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Greg Clayton53eb1c22012-04-02 22:59:12 +0000964 assert (dwarf_cu);
965 const DWARFDebugInfoEntry * cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000966
967 if (cu_die)
968 {
Greg Clayton53eb1c22012-04-02 22:59:12 +0000969 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_comp_dir, NULL);
970 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000971
972 // All file indexes in DWARF are one based and a file of index zero is
973 // supposed to be the compile unit itself.
974 support_files.Append (*sc.comp_unit);
975
Greg Claytond804d282012-03-15 21:01:31 +0000976 return DWARFDebugLine::ParseSupportFiles(sc.comp_unit->GetModule(), get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000977 }
978 return false;
979}
980
981struct ParseDWARFLineTableCallbackInfo
982{
983 LineTable* line_table;
984 const SectionList *section_list;
985 lldb::addr_t prev_sect_file_base_addr;
986 lldb::addr_t curr_sect_file_base_addr;
987 bool is_oso_for_debug_map;
988 bool prev_in_final_executable;
989 DWARFDebugLine::Row prev_row;
990 SectionSP prev_section_sp;
991 SectionSP curr_section_sp;
992};
993
994//----------------------------------------------------------------------
995// ParseStatementTableCallback
996//----------------------------------------------------------------------
997static void
998ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
999{
1000 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
1001 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
1002 {
1003 // Just started parsing the line table
1004 }
1005 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
1006 {
1007 // Done parsing line table, nothing to do for the cleanup
1008 }
1009 else
1010 {
1011 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
1012 // We have a new row, lets append it
1013
1014 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
1015 {
1016 info->prev_section_sp = info->curr_section_sp;
1017 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
1018 // If this is an end sequence entry, then we subtract one from the
1019 // address to make sure we get an address that is not the end of
1020 // a section.
1021 if (state.end_sequence && state.address != 0)
1022 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
1023 else
1024 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
1025
1026 if (info->curr_section_sp.get())
1027 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
1028 else
1029 info->curr_sect_file_base_addr = 0;
1030 }
1031 if (info->curr_section_sp.get())
1032 {
1033 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
1034 // Check for the fancy section magic to determine if we
1035
1036 if (info->is_oso_for_debug_map)
1037 {
1038 // When this is a debug map object file that contains DWARF
1039 // (referenced from an N_OSO debug map nlist entry) we will have
1040 // a file address in the file range for our section from the
1041 // original .o file, and a load address in the executable that
1042 // contains the debug map.
1043 //
1044 // If the sections for the file range and load range are
1045 // different, we have a remapped section for the function and
1046 // this address is resolved. If they are the same, then the
1047 // function for this address didn't make it into the final
1048 // executable.
Jim Inghamf94e1792012-08-11 00:35:26 +00001049 bool curr_in_final_executable = (bool) info->curr_section_sp->GetLinkedSection ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001050
1051 // If we are doing DWARF with debug map, then we need to carefully
1052 // add each line table entry as there may be gaps as functions
1053 // get moved around or removed.
1054 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1055 {
1056 if (info->prev_in_final_executable)
1057 {
1058 bool terminate_previous_entry = false;
1059 if (!curr_in_final_executable)
1060 {
1061 // Check for the case where the previous line entry
1062 // in a function made it into the final executable,
1063 // yet the current line entry falls in a function
1064 // that didn't. The line table used to be contiguous
1065 // through this address range but now it isn't. We
1066 // need to terminate the previous line entry so
1067 // that we can reconstruct the line range correctly
1068 // for it and to keep the line table correct.
1069 terminate_previous_entry = true;
1070 }
1071 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1072 {
1073 // Check for cases where the line entries used to be
1074 // contiguous address ranges, but now they aren't.
1075 // This can happen when order files specify the
1076 // ordering of the functions.
1077 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1078 Section *curr_sect = info->curr_section_sp.get();
1079 Section *prev_sect = info->prev_section_sp.get();
1080 assert (curr_sect->GetLinkedSection());
1081 assert (prev_sect->GetLinkedSection());
1082 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1083 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1084 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1085 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1086 if (object_file_addr_delta != linked_file_addr_delta)
1087 terminate_previous_entry = true;
1088 }
1089
1090 if (terminate_previous_entry)
1091 {
1092 line_table->InsertLineEntry (info->prev_section_sp,
1093 state.address - info->prev_sect_file_base_addr,
1094 info->prev_row.line,
1095 info->prev_row.column,
1096 info->prev_row.file,
1097 false, // is_stmt
1098 false, // basic_block
1099 false, // state.prologue_end
1100 false, // state.epilogue_begin
1101 true); // end_sequence);
1102 }
1103 }
1104 }
1105
1106 if (curr_in_final_executable)
1107 {
1108 line_table->InsertLineEntry (info->curr_section_sp,
1109 curr_line_section_offset,
1110 state.line,
1111 state.column,
1112 state.file,
1113 state.is_stmt,
1114 state.basic_block,
1115 state.prologue_end,
1116 state.epilogue_begin,
1117 state.end_sequence);
1118 info->prev_section_sp = info->curr_section_sp;
1119 }
1120 else
1121 {
1122 // If the current address didn't make it into the final
1123 // executable, the current section will be the __text
1124 // segment in the .o file, so we need to clear this so
1125 // we can catch the next function that did make it into
1126 // the final executable.
1127 info->prev_section_sp.reset();
1128 info->curr_section_sp.reset();
1129 }
1130
1131 info->prev_in_final_executable = curr_in_final_executable;
1132 }
1133 else
1134 {
1135 // We are not in an object file that contains DWARF for an
1136 // N_OSO, this is just a normal DWARF file. The DWARF spec
1137 // guarantees that the addresses will be in increasing order
1138 // so, since we store line tables in file address order, we
1139 // can always just append the line entry without needing to
1140 // search for the correct insertion point (we don't need to
1141 // use LineEntry::InsertLineEntry()).
1142 line_table->AppendLineEntry (info->curr_section_sp,
1143 curr_line_section_offset,
1144 state.line,
1145 state.column,
1146 state.file,
1147 state.is_stmt,
1148 state.basic_block,
1149 state.prologue_end,
1150 state.epilogue_begin,
1151 state.end_sequence);
1152 }
1153 }
1154
1155 info->prev_row = state;
1156 }
1157}
1158
1159bool
1160SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1161{
1162 assert (sc.comp_unit);
1163 if (sc.comp_unit->GetLineTable() != NULL)
1164 return true;
1165
Greg Clayton1f746072012-08-29 21:13:06 +00001166 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 if (dwarf_cu)
1168 {
1169 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001170 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001172 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1173 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001174 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001175 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1176 if (line_table_ap.get())
1177 {
1178 ParseDWARFLineTableCallbackInfo info = {
1179 line_table_ap.get(),
1180 m_obj_file->GetSectionList(),
1181 0,
1182 0,
Greg Clayton1f746072012-08-29 21:13:06 +00001183 GetDebugMapSymfile () != NULL,
Greg Clayton129d12c2011-11-28 03:29:03 +00001184 false,
1185 DWARFDebugLine::Row(),
1186 SectionSP(),
1187 SectionSP()
1188 };
1189 uint32_t offset = cu_line_offset;
1190 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1191 sc.comp_unit->SetLineTable(line_table_ap.release());
1192 return true;
1193 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001194 }
1195 }
1196 }
1197 return false;
1198}
1199
1200size_t
1201SymbolFileDWARF::ParseFunctionBlocks
1202(
1203 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001204 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001205 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 const DWARFDebugInfoEntry *die,
1207 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001208 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001209)
1210{
1211 size_t blocks_added = 0;
1212 while (die != NULL)
1213 {
1214 dw_tag_t tag = die->Tag();
1215
1216 switch (tag)
1217 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001218 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001219 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001220 case DW_TAG_lexical_block:
1221 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001222 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001223 if (tag == DW_TAG_subprogram)
1224 {
1225 // Skip any DW_TAG_subprogram DIEs that are inside
1226 // of a normal or inlined functions. These will be
1227 // parsed on their own as separate entities.
1228
1229 if (depth > 0)
1230 break;
1231
1232 block = parent_block;
1233 }
1234 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001235 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001236 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001237 parent_block->AddChild(block_sp);
1238 block = block_sp.get();
1239 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001240 DWARFDebugRanges::RangeList ranges;
1241 const char *name = NULL;
1242 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001244 int decl_file = 0;
1245 int decl_line = 0;
1246 int decl_column = 0;
1247 int call_file = 0;
1248 int call_line = 0;
1249 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001250 if (die->GetDIENamesAndRanges (this,
1251 dwarf_cu,
1252 name,
1253 mangled_name,
1254 ranges,
1255 decl_file, decl_line, decl_column,
1256 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001257 {
1258 if (tag == DW_TAG_subprogram)
1259 {
1260 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001261 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001262 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001263 else if (tag == DW_TAG_inlined_subroutine)
1264 {
1265 // We get called here for inlined subroutines in two ways.
1266 // The first time is when we are making the Function object
1267 // for this inlined concrete instance. Since we're creating a top level block at
1268 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1269 // adjust the containing address.
1270 // The second time is when we are parsing the blocks inside the function that contains
1271 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1272 // function the offset will be for that function.
1273 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1274 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001275 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001276 }
1277 }
1278
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001279 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001280
1281 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1282 {
1283 std::auto_ptr<Declaration> decl_ap;
1284 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001285 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1286 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001287
1288 std::auto_ptr<Declaration> call_ap;
1289 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001290 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1291 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001292
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001293 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001294 }
1295
1296 ++blocks_added;
1297
Greg Claytondd7feaf2011-08-12 17:54:33 +00001298 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001299 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001300 blocks_added += ParseFunctionBlocks (sc,
1301 block,
1302 dwarf_cu,
1303 die->GetFirstChild(),
1304 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001305 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001306 }
1307 }
1308 }
1309 break;
1310 default:
1311 break;
1312 }
1313
Greg Claytondd7feaf2011-08-12 17:54:33 +00001314 // Only parse siblings of the block if we are not at depth zero. A depth
1315 // of zero indicates we are currently parsing the top level
1316 // DW_TAG_subprogram DIE
1317
1318 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001319 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001320 else
1321 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001322 }
1323 return blocks_added;
1324}
1325
Greg Claytonf0705c82011-10-22 03:33:13 +00001326bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001327SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1328 const DWARFDebugInfoEntry *die,
1329 ClangASTContext::TemplateParameterInfos &template_param_infos)
1330{
1331 const dw_tag_t tag = die->Tag();
1332
1333 switch (tag)
1334 {
1335 case DW_TAG_template_type_parameter:
1336 case DW_TAG_template_value_parameter:
1337 {
1338 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1339
1340 DWARFDebugInfoEntry::Attributes attributes;
1341 const size_t num_attributes = die->GetAttributes (this,
1342 dwarf_cu,
1343 fixed_form_sizes,
1344 attributes);
1345 const char *name = NULL;
1346 Type *lldb_type = NULL;
1347 clang_type_t clang_type = NULL;
1348 uint64_t uval64 = 0;
1349 bool uval64_valid = false;
1350 if (num_attributes > 0)
1351 {
1352 DWARFFormValue form_value;
1353 for (size_t i=0; i<num_attributes; ++i)
1354 {
1355 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1356
1357 switch (attr)
1358 {
1359 case DW_AT_name:
1360 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1361 name = form_value.AsCString(&get_debug_str_data());
1362 break;
1363
1364 case DW_AT_type:
1365 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1366 {
1367 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1368 lldb_type = ResolveTypeUID(type_die_offset);
1369 if (lldb_type)
1370 clang_type = lldb_type->GetClangForwardType();
1371 }
1372 break;
1373
1374 case DW_AT_const_value:
1375 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1376 {
1377 uval64_valid = true;
1378 uval64 = form_value.Unsigned();
1379 }
1380 break;
1381 default:
1382 break;
1383 }
1384 }
1385
1386 if (name && lldb_type && clang_type)
1387 {
1388 bool is_signed = false;
1389 template_param_infos.names.push_back(name);
1390 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1391 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1392 {
1393 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
Sean Callanan3d654b32012-09-24 22:25:51 +00001394 template_param_infos.args.push_back (clang::TemplateArgument (*GetClangASTContext().getASTContext(),
1395 llvm::APSInt(apint),
1396 clang_qual_type));
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001397 }
1398 else
1399 {
1400 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1401 }
1402 }
1403 else
1404 {
1405 return false;
1406 }
1407
1408 }
1409 }
1410 return true;
1411
1412 default:
1413 break;
1414 }
1415 return false;
1416}
1417
1418bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001419SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1420 const DWARFDebugInfoEntry *parent_die,
1421 ClangASTContext::TemplateParameterInfos &template_param_infos)
1422{
1423
1424 if (parent_die == NULL)
Filipe Cabecinhas52008c62012-05-23 16:24:11 +00001425 return false;
Greg Claytonf0705c82011-10-22 03:33:13 +00001426
Greg Claytonf0705c82011-10-22 03:33:13 +00001427 Args template_parameter_names;
1428 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1429 die != NULL;
1430 die = die->GetSibling())
1431 {
1432 const dw_tag_t tag = die->Tag();
1433
1434 switch (tag)
1435 {
1436 case DW_TAG_template_type_parameter:
1437 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001438 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001439 break;
1440
1441 default:
1442 break;
1443 }
1444 }
1445 if (template_param_infos.args.empty())
1446 return false;
1447 return template_param_infos.args.size() == template_param_infos.names.size();
1448}
1449
1450clang::ClassTemplateDecl *
1451SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001452 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001453 const char *parent_name,
1454 int tag_decl_kind,
1455 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1456{
1457 if (template_param_infos.IsValid())
1458 {
1459 std::string template_basename(parent_name);
1460 template_basename.erase (template_basename.find('<'));
1461 ClangASTContext &ast = GetClangASTContext();
1462
1463 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001464 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001465 template_basename.c_str(),
1466 tag_decl_kind,
1467 template_param_infos);
1468 }
1469 return NULL;
1470}
1471
Sean Callanana0b80ab2012-06-04 22:28:05 +00001472class SymbolFileDWARF::DelayedAddObjCClassProperty
1473{
1474public:
1475 DelayedAddObjCClassProperty
1476 (
1477 clang::ASTContext *ast,
1478 lldb::clang_type_t class_opaque_type,
1479 const char *property_name,
1480 lldb::clang_type_t property_opaque_type, // The property type is only required if you don't have an ivar decl
1481 clang::ObjCIvarDecl *ivar_decl,
1482 const char *property_setter_name,
1483 const char *property_getter_name,
1484 uint32_t property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001485 const ClangASTMetadata *metadata
Sean Callanana0b80ab2012-06-04 22:28:05 +00001486 ) :
1487 m_ast (ast),
1488 m_class_opaque_type (class_opaque_type),
1489 m_property_name (property_name),
1490 m_property_opaque_type (property_opaque_type),
1491 m_ivar_decl (ivar_decl),
1492 m_property_setter_name (property_setter_name),
1493 m_property_getter_name (property_getter_name),
Jim Ingham379397632012-10-27 02:54:13 +00001494 m_property_attributes (property_attributes)
Sean Callanana0b80ab2012-06-04 22:28:05 +00001495 {
Jim Ingham379397632012-10-27 02:54:13 +00001496 if (metadata != NULL)
1497 {
1498 m_metadata_ap.reset(new ClangASTMetadata());
1499 *(m_metadata_ap.get()) = *metadata;
1500 }
1501 }
1502
1503 DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1504 {
1505 m_ast = rhs.m_ast;
1506 m_class_opaque_type = rhs.m_class_opaque_type;
1507 m_property_name = rhs.m_property_name;
1508 m_property_opaque_type = rhs.m_property_opaque_type;
1509 m_ivar_decl = rhs.m_ivar_decl;
1510 m_property_setter_name = rhs.m_property_setter_name;
1511 m_property_getter_name = rhs.m_property_getter_name;
1512 m_property_attributes = rhs.m_property_attributes;
1513
1514 if (rhs.m_metadata_ap.get())
1515 {
1516 m_metadata_ap.reset (new ClangASTMetadata());
1517 *(m_metadata_ap.get()) = *(rhs.m_metadata_ap.get());
1518 }
1519
Sean Callanana0b80ab2012-06-04 22:28:05 +00001520 }
1521
1522 bool Finalize() const
1523 {
1524 return ClangASTContext::AddObjCClassProperty(m_ast,
1525 m_class_opaque_type,
1526 m_property_name,
1527 m_property_opaque_type,
1528 m_ivar_decl,
1529 m_property_setter_name,
1530 m_property_getter_name,
1531 m_property_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001532 m_metadata_ap.get());
Sean Callanana0b80ab2012-06-04 22:28:05 +00001533 }
1534private:
1535 clang::ASTContext *m_ast;
1536 lldb::clang_type_t m_class_opaque_type;
1537 const char *m_property_name;
1538 lldb::clang_type_t m_property_opaque_type;
1539 clang::ObjCIvarDecl *m_ivar_decl;
1540 const char *m_property_setter_name;
1541 const char *m_property_getter_name;
1542 uint32_t m_property_attributes;
Jim Ingham379397632012-10-27 02:54:13 +00001543 std::auto_ptr<ClangASTMetadata> m_metadata_ap;
Sean Callanana0b80ab2012-06-04 22:28:05 +00001544};
1545
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001546size_t
1547SymbolFileDWARF::ParseChildMembers
1548(
1549 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001550 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001551 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001552 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001553 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001554 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1555 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001556 DWARFDIECollection& member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00001557 DelayedPropertyList& delayed_properties,
Sean Callananc7fbf732010-08-06 00:32:49 +00001558 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001559 bool &is_a_class,
1560 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001561)
1562{
1563 if (parent_die == NULL)
1564 return 0;
1565
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001566 size_t count = 0;
1567 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001568 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001569 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001570
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1572 {
1573 dw_tag_t tag = die->Tag();
1574
1575 switch (tag)
1576 {
1577 case DW_TAG_member:
Sean Callanan3d654b32012-09-24 22:25:51 +00001578 case DW_TAG_APPLE_property:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001579 {
1580 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001581 const size_t num_attributes = die->GetAttributes (this,
1582 dwarf_cu,
1583 fixed_form_sizes,
1584 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001585 if (num_attributes > 0)
1586 {
1587 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001588 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001589 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001590 const char *prop_name = NULL;
1591 const char *prop_getter_name = NULL;
1592 const char *prop_setter_name = NULL;
1593 uint32_t prop_attributes = 0;
1594
1595
Greg Clayton24739922010-10-13 03:15:28 +00001596 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001597 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001598 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001599 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001600 size_t byte_size = 0;
1601 size_t bit_offset = 0;
1602 size_t bit_size = 0;
1603 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001604 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001605 {
1606 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1607 DWARFFormValue form_value;
1608 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1609 {
1610 switch (attr)
1611 {
1612 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1613 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1614 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1615 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1616 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1617 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1618 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1619 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1620 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001621 if (form_value.BlockData())
1622 {
1623 Value initialValue(0);
1624 Value memberOffset(0);
1625 const DataExtractor& debug_info_data = get_debug_info_data();
1626 uint32_t block_length = form_value.Unsigned();
1627 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1628 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1629 NULL, // clang::ASTContext *
1630 NULL, // ClangExpressionVariableList *
1631 NULL, // ClangExpressionDeclMap *
1632 NULL, // RegisterContext *
1633 debug_info_data,
1634 block_offset,
1635 block_length,
1636 eRegisterKindDWARF,
1637 &initialValue,
1638 memberOffset,
1639 NULL))
1640 {
1641 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1642 }
1643 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001644 break;
1645
Greg Clayton8cf05932010-07-22 18:30:50 +00001646 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001647 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001648 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1649 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1650 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1651 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1652
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001654 case DW_AT_declaration:
1655 case DW_AT_description:
1656 case DW_AT_mutable:
1657 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658 case DW_AT_sibling:
1659 break;
1660 }
1661 }
1662 }
Sean Callanana6582262012-04-05 00:12:52 +00001663
1664 if (prop_name)
Sean Callanan751aac62012-03-29 19:07:06 +00001665 {
Sean Callanana6582262012-04-05 00:12:52 +00001666 ConstString fixed_getter;
1667 ConstString fixed_setter;
1668
1669 // Check if the property getter/setter were provided as full
1670 // names. We want basenames, so we extract them.
1671
1672 if (prop_getter_name && prop_getter_name[0] == '-')
1673 {
1674 ObjCLanguageRuntime::ParseMethodName (prop_getter_name,
1675 NULL,
1676 &fixed_getter,
1677 NULL,
1678 NULL);
1679 prop_getter_name = fixed_getter.GetCString();
1680 }
1681
1682 if (prop_setter_name && prop_setter_name[0] == '-')
1683 {
1684 ObjCLanguageRuntime::ParseMethodName (prop_setter_name,
1685 NULL,
1686 &fixed_setter,
1687 NULL,
1688 NULL);
1689 prop_setter_name = fixed_setter.GetCString();
1690 }
1691
1692 // If the names haven't been provided, they need to be
1693 // filled in.
1694
1695 if (!prop_getter_name)
1696 {
1697 prop_getter_name = prop_name;
1698 }
1699 if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
1700 {
1701 StreamString ss;
1702
1703 ss.Printf("set%c%s:",
1704 toupper(prop_name[0]),
1705 &prop_name[1]);
1706
1707 fixed_setter.SetCString(ss.GetData());
1708 prop_setter_name = fixed_setter.GetCString();
1709 }
Sean Callanan751aac62012-03-29 19:07:06 +00001710 }
1711
1712 // Clang has a DWARF generation bug where sometimes it
Greg Clayton44953932011-10-25 01:25:35 +00001713 // represents fields that are references with bad byte size
1714 // and bit size/offset information such as:
1715 //
1716 // DW_AT_byte_size( 0x00 )
1717 // DW_AT_bit_size( 0x40 )
1718 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1719 //
1720 // So check the bit offset to make sure it is sane, and if
1721 // the values are not sane, remove them. If we don't do this
1722 // then we will end up with a crash if we try to use this
1723 // type in an expression when clang becomes unhappy with its
1724 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001725
Greg Clayton44953932011-10-25 01:25:35 +00001726 if (bit_offset > 128)
1727 {
1728 bit_size = 0;
1729 bit_offset = 0;
1730 }
1731
1732 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001733 if (class_language == eLanguageTypeObjC ||
1734 class_language == eLanguageTypeObjC_plus_plus)
1735 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001736
1737 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1738 {
1739 // Not all compilers will mark the vtable pointer
1740 // member as artificial (llvm-gcc). We can't have
1741 // the virtual members in our classes otherwise it
1742 // throws off all child offsets since we end up
1743 // having and extra pointer sized member in our
1744 // class layouts.
1745 is_artificial = true;
1746 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001747
Greg Clayton24739922010-10-13 03:15:28 +00001748 if (is_artificial == false)
1749 {
1750 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001751 clang::FieldDecl *field_decl = NULL;
Sean Callanan751aac62012-03-29 19:07:06 +00001752 if (tag == DW_TAG_member)
Greg Claytond16e1e52011-07-12 17:06:17 +00001753 {
Sean Callanan751aac62012-03-29 19:07:06 +00001754 if (member_type)
1755 {
1756 if (accessibility == eAccessNone)
1757 accessibility = default_accessibility;
1758 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001759
Sean Callanan751aac62012-03-29 19:07:06 +00001760 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
1761 name,
1762 member_type->GetClangLayoutType(),
1763 accessibility,
1764 bit_size);
Sean Callanan60217122012-04-13 00:10:03 +00001765
Jim Ingham379397632012-10-27 02:54:13 +00001766 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)field_decl, MakeUserID(die->GetOffset()));
Sean Callanan5b26f272012-02-04 08:49:35 +00001767 }
1768 else
1769 {
Sean Callanan751aac62012-03-29 19:07:06 +00001770 if (name)
1771 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1772 MakeUserID(die->GetOffset()),
1773 name,
1774 encoding_uid);
1775 else
1776 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1777 MakeUserID(die->GetOffset()),
1778 encoding_uid);
Sean Callanan5b26f272012-02-04 08:49:35 +00001779 }
Sean Callanan751aac62012-03-29 19:07:06 +00001780
1781 if (member_byte_offset != UINT32_MAX || bit_size != 0)
1782 {
1783 /////////////////////////////////////////////////////////////
1784 // How to locate a field given the DWARF debug information
1785 //
1786 // AT_byte_size indicates the size of the word in which the
1787 // bit offset must be interpreted.
1788 //
1789 // AT_data_member_location indicates the byte offset of the
1790 // word from the base address of the structure.
1791 //
1792 // AT_bit_offset indicates how many bits into the word
1793 // (according to the host endianness) the low-order bit of
1794 // the field starts. AT_bit_offset can be negative.
1795 //
1796 // AT_bit_size indicates the size of the field in bits.
1797 /////////////////////////////////////////////////////////////
Sean Callanan5b26f272012-02-04 08:49:35 +00001798
Sean Callanan751aac62012-03-29 19:07:06 +00001799 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1800
1801 uint64_t total_bit_offset = 0;
1802
1803 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1804
1805 if (object_endian == eByteOrderLittle)
1806 {
1807 total_bit_offset += byte_size * 8;
1808 total_bit_offset -= (bit_offset + bit_size);
1809 }
1810 else
1811 {
1812 total_bit_offset += bit_offset;
1813 }
1814
1815 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
1816 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001817 }
Sean Callanan751aac62012-03-29 19:07:06 +00001818
Jim Inghame3ae82a2011-11-12 01:36:43 +00001819 if (prop_name != NULL)
1820 {
Sean Callanan751aac62012-03-29 19:07:06 +00001821 clang::ObjCIvarDecl *ivar_decl = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001822
Sean Callanan751aac62012-03-29 19:07:06 +00001823 if (field_decl)
1824 {
1825 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1826 assert (ivar_decl != NULL);
1827 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001828
Jim Ingham379397632012-10-27 02:54:13 +00001829 ClangASTMetadata metadata;
1830 metadata.SetUserID (MakeUserID(die->GetOffset()));
Sean Callanana0b80ab2012-06-04 22:28:05 +00001831 delayed_properties.push_back(DelayedAddObjCClassProperty(GetClangASTContext().getASTContext(),
1832 class_clang_type,
1833 prop_name,
1834 member_type->GetClangLayoutType(),
1835 ivar_decl,
1836 prop_setter_name,
1837 prop_getter_name,
1838 prop_attributes,
Jim Ingham379397632012-10-27 02:54:13 +00001839 &metadata));
Sean Callanan60217122012-04-13 00:10:03 +00001840
Sean Callananad880762012-04-18 01:06:17 +00001841 if (ivar_decl)
Jim Ingham379397632012-10-27 02:54:13 +00001842 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)ivar_decl, MakeUserID(die->GetOffset()));
Jim Inghame3ae82a2011-11-12 01:36:43 +00001843 }
Greg Clayton24739922010-10-13 03:15:28 +00001844 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001845 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001846 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001847 }
1848 break;
1849
1850 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001851 // Let the type parsing code handle this one for us.
1852 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001853 break;
1854
1855 case DW_TAG_inheritance:
1856 {
1857 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001858 if (default_accessibility == eAccessNone)
1859 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001860 // TODO: implement DW_TAG_inheritance type parsing
1861 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001862 const size_t num_attributes = die->GetAttributes (this,
1863 dwarf_cu,
1864 fixed_form_sizes,
1865 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866 if (num_attributes > 0)
1867 {
1868 Declaration decl;
1869 DWARFExpression location;
1870 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001871 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001872 bool is_virtual = false;
1873 bool is_base_of_class = true;
Greg Clayton23f59502012-07-17 03:23:13 +00001874 //off_t member_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001875 uint32_t i;
1876 for (i=0; i<num_attributes; ++i)
1877 {
1878 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1879 DWARFFormValue form_value;
1880 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1881 {
1882 switch (attr)
1883 {
1884 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1885 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1886 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1887 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00001888// case DW_AT_data_member_location:
1889// if (form_value.BlockData())
1890// {
1891// Value initialValue(0);
1892// Value memberOffset(0);
1893// const DataExtractor& debug_info_data = get_debug_info_data();
1894// uint32_t block_length = form_value.Unsigned();
1895// uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1896// if (DWARFExpression::Evaluate (NULL,
1897// NULL,
1898// NULL,
1899// NULL,
1900// NULL,
1901// debug_info_data,
1902// block_offset,
1903// block_length,
1904// eRegisterKindDWARF,
1905// &initialValue,
1906// memberOffset,
1907// NULL))
1908// {
1909// member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1910// }
1911// }
1912// break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001913
1914 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001915 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001916 break;
1917
1918 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1919 default:
1920 case DW_AT_sibling:
1921 break;
1922 }
1923 }
1924 }
1925
Greg Clayton526e5af2010-11-13 03:52:47 +00001926 Type *base_class_type = ResolveTypeUID(encoding_uid);
1927 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001928
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001929 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1930 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001931 if (class_language == eLanguageTypeObjC)
1932 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001933 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001934 }
1935 else
1936 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001937 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001938 accessibility,
1939 is_virtual,
1940 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001941 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001942 }
1943 }
1944 break;
1945
1946 default:
1947 break;
1948 }
1949 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00001950
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951 return count;
1952}
1953
1954
1955clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001956SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001957{
1958 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001959 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001960 {
1961 DWARFCompileUnitSP cu_sp;
1962 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1963 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001964 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001965 }
1966 return NULL;
1967}
1968
1969clang::DeclContext*
1970SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1971{
Greg Clayton81c22f62011-10-19 18:09:39 +00001972 if (UserIDMatches(type_uid))
1973 return GetClangDeclContextForDIEOffset (sc, type_uid);
1974 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001975}
1976
1977Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001978SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001979{
Greg Clayton81c22f62011-10-19 18:09:39 +00001980 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001981 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001982 DWARFDebugInfo* debug_info = DebugInfo();
1983 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001984 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001985 DWARFCompileUnitSP cu_sp;
1986 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001987 const bool assert_not_being_parsed = true;
1988 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001989 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001990 }
1991 return NULL;
1992}
1993
Greg Claytoncab36a32011-12-08 05:16:30 +00001994Type*
1995SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001996{
Greg Claytoncab36a32011-12-08 05:16:30 +00001997 if (die != NULL)
1998 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001999 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
2000 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002001 GetObjectFile()->GetModule()->LogMessage (log.get(),
2002 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
2003 die->GetOffset(),
2004 DW_TAG_value_to_name(die->Tag()),
2005 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00002006
Greg Claytoncab36a32011-12-08 05:16:30 +00002007 // We might be coming in in the middle of a type tree (a class
2008 // withing a class, an enum within a class), so parse any needed
2009 // parent DIEs before we get to this one...
2010 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2011 switch (decl_ctx_die->Tag())
2012 {
2013 case DW_TAG_structure_type:
2014 case DW_TAG_union_type:
2015 case DW_TAG_class_type:
2016 {
2017 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00002018 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002019 GetObjectFile()->GetModule()->LogMessage (log.get(),
2020 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
2021 die->GetOffset(),
2022 DW_TAG_value_to_name(die->Tag()),
2023 die->GetName(this, cu),
2024 decl_ctx_die->GetOffset());
Greg Clayton219cf312012-03-30 00:51:13 +00002025//
2026// Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
2027// if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
2028// {
2029// if (log)
2030// GetObjectFile()->GetModule()->LogMessage (log.get(),
2031// "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
2032// die->GetOffset(),
2033// DW_TAG_value_to_name(die->Tag()),
2034// die->GetName(this, cu),
2035// decl_ctx_die->GetOffset());
2036// // Ask the type to complete itself if it already hasn't since if we
2037// // want a function (method or static) from a class, the class must
2038// // create itself and add it's own methods and class functions.
2039// if (parent_type)
2040// parent_type->GetClangFullType();
2041// }
Greg Claytoncab36a32011-12-08 05:16:30 +00002042 }
2043 break;
2044
2045 default:
2046 break;
2047 }
2048 return ResolveType (cu, die);
2049 }
2050 return NULL;
2051}
2052
Greg Clayton6beaaa62011-01-17 03:46:26 +00002053// This function is used when SymbolFileDWARFDebugMap owns a bunch of
2054// SymbolFileDWARF objects to detect if this DWARF file is the one that
2055// can resolve a clang_type.
2056bool
2057SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
2058{
2059 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2060 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
2061 return die != NULL;
2062}
2063
2064
Greg Clayton1be10fc2010-09-29 01:12:09 +00002065lldb::clang_type_t
2066SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
2067{
2068 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002069 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
2070 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002071 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00002072 {
2073 // We have already resolved this type...
2074 return clang_type;
2075 }
2076 // Once we start resolving this type, remove it from the forward declaration
2077 // map in case anyone child members or other types require this type to get resolved.
2078 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
2079 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00002080 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00002081
Greg Clayton1be10fc2010-09-29 01:12:09 +00002082
Greg Clayton85ae2e12011-10-18 23:36:41 +00002083 // Disable external storage for this type so we don't get anymore
2084 // clang::ExternalASTSource queries for this type.
2085 ClangASTContext::SetHasExternalStorage (clang_type, false);
2086
Greg Clayton450e3f32010-10-12 02:24:53 +00002087 DWARFDebugInfo* debug_info = DebugInfo();
2088
Greg Clayton53eb1c22012-04-02 22:59:12 +00002089 DWARFCompileUnit *dwarf_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002090 Type *type = m_die_to_type.lookup (die);
2091
2092 const dw_tag_t tag = die->Tag();
2093
Greg Claytonbaf95da2012-03-30 23:50:54 +00002094 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
Greg Clayton3bffb082011-12-10 02:15:28 +00002095 if (log)
Greg Claytonbaf95da2012-03-30 23:50:54 +00002096 {
Greg Claytond61c0fc2012-04-23 22:55:20 +00002097 GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log.get(),
2098 "0x%8.8llx: %s '%s' resolving forward declaration...",
2099 MakeUserID(die->GetOffset()),
2100 DW_TAG_value_to_name(tag),
2101 type->GetName().AsCString());
Greg Claytonbaf95da2012-03-30 23:50:54 +00002102
Greg Claytonbaf95da2012-03-30 23:50:54 +00002103 }
Greg Clayton1be10fc2010-09-29 01:12:09 +00002104 assert (clang_type);
2105 DWARFDebugInfoEntry::Attributes attributes;
2106
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002107 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002108
2109 switch (tag)
2110 {
2111 case DW_TAG_structure_type:
2112 case DW_TAG_union_type:
2113 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00002114 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002115 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002116
Greg Clayton1be10fc2010-09-29 01:12:09 +00002117 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002118 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00002119 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002120
Sean Callanan77a1fd32012-02-27 20:07:01 +00002121 LanguageType class_language = eLanguageTypeUnknown;
2122 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
2123 if (is_objc_class)
Greg Clayton219cf312012-03-30 00:51:13 +00002124 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002125 class_language = eLanguageTypeObjC;
Greg Clayton219cf312012-03-30 00:51:13 +00002126 // For objective C we don't start the definition when
2127 // the class is created.
2128 ast.StartTagDeclarationDefinition (clang_type);
2129 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002130
2131 int tag_decl_kind = -1;
2132 AccessType default_accessibility = eAccessNone;
2133 if (tag == DW_TAG_structure_type)
2134 {
2135 tag_decl_kind = clang::TTK_Struct;
2136 default_accessibility = eAccessPublic;
2137 }
2138 else if (tag == DW_TAG_union_type)
2139 {
2140 tag_decl_kind = clang::TTK_Union;
2141 default_accessibility = eAccessPublic;
2142 }
2143 else if (tag == DW_TAG_class_type)
2144 {
2145 tag_decl_kind = clang::TTK_Class;
2146 default_accessibility = eAccessPrivate;
2147 }
2148
Greg Clayton53eb1c22012-04-02 22:59:12 +00002149 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002150 std::vector<clang::CXXBaseSpecifier *> base_classes;
2151 std::vector<int> member_accessibilities;
2152 bool is_a_class = false;
2153 // Parse members and base classes first
2154 DWARFDIECollection member_function_dies;
Sean Callanana0b80ab2012-06-04 22:28:05 +00002155
2156 DelayedPropertyList delayed_properties;
Sean Callanan77a1fd32012-02-27 20:07:01 +00002157
2158 ParseChildMembers (sc,
Greg Clayton53eb1c22012-04-02 22:59:12 +00002159 dwarf_cu,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002160 die,
2161 clang_type,
2162 class_language,
2163 base_classes,
2164 member_accessibilities,
2165 member_function_dies,
Sean Callanana0b80ab2012-06-04 22:28:05 +00002166 delayed_properties,
Sean Callanan77a1fd32012-02-27 20:07:01 +00002167 default_accessibility,
2168 is_a_class,
2169 layout_info);
2170
2171 // Now parse any methods if there were any...
2172 size_t num_functions = member_function_dies.Size();
2173 if (num_functions > 0)
2174 {
2175 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002176 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002177 ResolveType(dwarf_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00002178 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002179 }
2180
2181 if (class_language == eLanguageTypeObjC)
2182 {
Greg Clayton84db9102012-03-26 23:03:23 +00002183 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(ast.getASTContext(), clang_type));
Sean Callanan77a1fd32012-02-27 20:07:01 +00002184 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00002185 {
Greg Clayton450e3f32010-10-12 02:24:53 +00002186
Sean Callanan77a1fd32012-02-27 20:07:01 +00002187 DIEArray method_die_offsets;
2188 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00002189 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002190 if (m_apple_objc_ap.get())
2191 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
2192 }
2193 else
2194 {
2195 if (!m_indexed)
2196 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00002197
Sean Callanan77a1fd32012-02-27 20:07:01 +00002198 ConstString class_name (class_str.c_str());
2199 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2200 }
2201
2202 if (!method_die_offsets.empty())
2203 {
2204 DWARFDebugInfo* debug_info = DebugInfo();
2205
2206 DWARFCompileUnit* method_cu = NULL;
2207 const size_t num_matches = method_die_offsets.size();
2208 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002209 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002210 const dw_offset_t die_offset = method_die_offsets[i];
2211 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2212
2213 if (method_die)
2214 ResolveType (method_cu, method_die);
2215 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002216 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002217 if (m_using_apple_tables)
2218 {
2219 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2220 die_offset, class_str.c_str());
2221 }
2222 }
2223 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002224 }
Sean Callanana0b80ab2012-06-04 22:28:05 +00002225
2226 for (DelayedPropertyList::const_iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2227 pi != pe;
2228 ++pi)
2229 pi->Finalize();
Greg Clayton450e3f32010-10-12 02:24:53 +00002230 }
2231 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002232
2233 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2234 // need to tell the clang type it is actually a class.
2235 if (class_language != eLanguageTypeObjC)
2236 {
2237 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2238 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2239 }
2240
2241 // Since DW_TAG_structure_type gets used for both classes
2242 // and structures, we may need to set any DW_TAG_member
2243 // fields to have a "private" access if none was specified.
2244 // When we parsed the child members we tracked that actual
2245 // accessibility value for each DW_TAG_member in the
2246 // "member_accessibilities" array. If the value for the
2247 // member is zero, then it was set to the "default_accessibility"
2248 // which for structs was "public". Below we correct this
2249 // by setting any fields to "private" that weren't correctly
2250 // set.
2251 if (is_a_class && !member_accessibilities.empty())
2252 {
2253 // This is a class and all members that didn't have
2254 // their access specified are private.
2255 ast.SetDefaultAccessForRecordFields (clang_type,
2256 eAccessPrivate,
2257 &member_accessibilities.front(),
2258 member_accessibilities.size());
2259 }
2260
2261 if (!base_classes.empty())
2262 {
2263 ast.SetBaseClassesForClassType (clang_type,
2264 &base_classes.front(),
2265 base_classes.size());
2266
2267 // Clang will copy each CXXBaseSpecifier in "base_classes"
2268 // so we have to free them all.
2269 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2270 base_classes.size());
2271 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002272 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002273 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002274
2275 ast.BuildIndirectFields (clang_type);
2276
Sean Callanan77a1fd32012-02-27 20:07:01 +00002277 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002278
Greg Claytoncaab74e2012-01-28 00:48:57 +00002279 if (!layout_info.field_offsets.empty())
2280 {
2281 if (type)
2282 layout_info.bit_size = type->GetByteSize() * 8;
2283 if (layout_info.bit_size == 0)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002284 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_byte_size, 0) * 8;
Greg Claytoncaab74e2012-01-28 00:48:57 +00002285 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2286 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2287 if (record_type)
2288 {
2289 const clang::RecordDecl *record_decl = record_type->getDecl();
2290
2291 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002292 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002293 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002294 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) caching layout info for record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u], base_offsets[0], vbase_offsets[0])",
Greg Claytoncaab74e2012-01-28 00:48:57 +00002295 clang_type,
2296 record_decl,
2297 layout_info.bit_size,
2298 layout_info.alignment,
2299 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002300
Greg Clayton821ac6d2012-01-28 02:22:27 +00002301 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2302 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2303 {
2304 GetObjectFile()->GetModule()->LogMessage (log.get(),
2305 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2306 clang_type,
2307 (uint32_t)pos->second,
2308 pos->first->getNameAsString().c_str());
2309 }
2310 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002311 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2312 }
2313 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002314 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002315
Greg Claytonc93237c2010-10-01 20:48:32 +00002316 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002317
2318 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002319 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002320 if (die->HasChildren())
2321 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002322 SymbolContext sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
2323 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), dwarf_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002324 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002325 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002326 return clang_type;
2327
2328 default:
2329 assert(false && "not a forward clang type decl!");
2330 break;
2331 }
2332 return NULL;
2333}
2334
Greg Claytonc685f8e2010-09-15 04:15:46 +00002335Type*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002336SymbolFileDWARF::ResolveType (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002337{
2338 if (type_die != NULL)
2339 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002340 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002341
Greg Claytonc685f8e2010-09-15 04:15:46 +00002342 if (type == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002343 type = GetTypeForDIE (dwarf_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002344
Greg Clayton24739922010-10-13 03:15:28 +00002345 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002346 {
2347 if (type != DIE_IS_BEING_PARSED)
2348 return type;
2349
2350 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
Greg Clayton53eb1c22012-04-02 22:59:12 +00002351 type_die->GetOffset(),
2352 DW_TAG_value_to_name(type_die->Tag()),
2353 type_die->GetName(this, dwarf_cu));
Jim Inghamc3549282012-01-11 02:21:12 +00002354
2355 }
2356 else
2357 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002358 }
2359 return NULL;
2360}
2361
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002362CompileUnit*
Greg Clayton53eb1c22012-04-02 22:59:12 +00002363SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* dwarf_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002364{
2365 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002366 if (dwarf_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002367 {
2368 // The symbol vendor doesn't know about this compile unit, we
2369 // need to parse and add it to the symbol vendor object.
Greg Clayton53eb1c22012-04-02 22:59:12 +00002370 return ParseCompileUnit(dwarf_cu, cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002371 }
Greg Clayton53eb1c22012-04-02 22:59:12 +00002372 return (CompileUnit*)dwarf_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002373}
2374
2375bool
Greg Clayton53eb1c22012-04-02 22:59:12 +00002376SymbolFileDWARF::GetFunction (DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002377{
2378 sc.Clear();
2379 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton53eb1c22012-04-02 22:59:12 +00002380 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002381
Greg Clayton81c22f62011-10-19 18:09:39 +00002382 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002383 if (sc.function == NULL)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002384 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002385
2386 if (sc.function)
2387 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002388 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002389 return true;
2390 }
2391
2392 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002393}
2394
2395uint32_t
2396SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2397{
2398 Timer scoped_timer(__PRETTY_FUNCTION__,
2399 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002400 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002401 so_addr.GetOffset(),
2402 resolve_scope);
2403 uint32_t resolved = 0;
2404 if (resolve_scope & ( eSymbolContextCompUnit |
2405 eSymbolContextFunction |
2406 eSymbolContextBlock |
2407 eSymbolContextLineEntry))
2408 {
2409 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2410
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002411 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002412 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002413 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002414 const dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002415 if (cu_offset != DW_INVALID_OFFSET)
2416 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002417 uint32_t cu_idx = DW_INVALID_INDEX;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002418 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2419 if (dwarf_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002420 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002421 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002422 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002423 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002424 resolved |= eSymbolContextCompUnit;
2425
2426 if (resolve_scope & eSymbolContextLineEntry)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002427 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002428 LineTable *line_table = sc.comp_unit->GetLineTable();
2429 if (line_table != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002430 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002431 if (so_addr.IsLinkedAddress())
2432 {
2433 Address linked_addr (so_addr);
2434 linked_addr.ResolveLinkedAddress();
2435 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2436 {
2437 resolved |= eSymbolContextLineEntry;
2438 }
2439 }
2440 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002441 {
2442 resolved |= eSymbolContextLineEntry;
2443 }
2444 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002445 }
2446
2447 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2448 {
2449 DWARFDebugInfoEntry *function_die = NULL;
2450 DWARFDebugInfoEntry *block_die = NULL;
2451 if (resolve_scope & eSymbolContextBlock)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002452 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002453 dwarf_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
2454 }
2455 else
2456 {
2457 dwarf_cu->LookupAddress(file_vm_addr, &function_die, NULL);
2458 }
2459
2460 if (function_die != NULL)
2461 {
2462 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2463 if (sc.function == NULL)
2464 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2465 }
2466 else
2467 {
2468 // We might have had a compile unit that had discontiguous
2469 // address ranges where the gaps are symbols that don't have
2470 // any debug info. Discontiguous compile unit address ranges
2471 // should only happen when there aren't other functions from
2472 // other compile units in these gaps. This helps keep the size
2473 // of the aranges down.
2474 sc.comp_unit = NULL;
2475 resolved &= ~eSymbolContextCompUnit;
2476 }
2477
2478 if (sc.function != NULL)
2479 {
2480 resolved |= eSymbolContextFunction;
2481
2482 if (resolve_scope & eSymbolContextBlock)
2483 {
2484 Block& block = sc.function->GetBlock (true);
2485
2486 if (block_die != NULL)
2487 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2488 else
2489 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2490 if (sc.block)
2491 resolved |= eSymbolContextBlock;
2492 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002493 }
2494 }
2495 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002496 else
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002497 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002498 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8x: compile unit %u failed to create a valid lldb_private::CompileUnit class.",
2499 cu_offset,
2500 cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002501 }
2502 }
2503 }
2504 }
2505 }
2506 return resolved;
2507}
2508
2509
2510
2511uint32_t
2512SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2513{
2514 const uint32_t prev_size = sc_list.GetSize();
2515 if (resolve_scope & eSymbolContextCompUnit)
2516 {
2517 DWARFDebugInfo* debug_info = DebugInfo();
2518 if (debug_info)
2519 {
2520 uint32_t cu_idx;
Greg Clayton53eb1c22012-04-02 22:59:12 +00002521 DWARFCompileUnit* dwarf_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002522
Greg Clayton53eb1c22012-04-02 22:59:12 +00002523 for (cu_idx = 0; (dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002524 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002525 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton1f746072012-08-29 21:13:06 +00002526 const bool full_match = file_spec.GetDirectory();
2527 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Equal(file_spec, *dc_cu, full_match);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002528 if (check_inlines || file_spec_matches_cu_file_spec)
2529 {
2530 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton53eb1c22012-04-02 22:59:12 +00002531 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, cu_idx);
Greg Clayton526a4ae2012-05-16 22:09:01 +00002532 if (sc.comp_unit)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002533 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002534 uint32_t file_idx = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002535
Greg Clayton526a4ae2012-05-16 22:09:01 +00002536 // If we are looking for inline functions only and we don't
2537 // find it in the support files, we are done.
2538 if (check_inlines)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002539 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002540 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
2541 if (file_idx == UINT32_MAX)
2542 continue;
2543 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002544
Greg Clayton526a4ae2012-05-16 22:09:01 +00002545 if (line != 0)
2546 {
2547 LineTable *line_table = sc.comp_unit->GetLineTable();
2548
2549 if (line_table != NULL && line != 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002550 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002551 // We will have already looked up the file index if
2552 // we are searching for inline entries.
2553 if (!check_inlines)
2554 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002555
Greg Clayton526a4ae2012-05-16 22:09:01 +00002556 if (file_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002557 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002558 uint32_t found_line;
2559 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2560 found_line = sc.line_entry.line;
2561
2562 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002563 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002564 sc.function = NULL;
2565 sc.block = NULL;
2566 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002567 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002568 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2569 if (file_vm_addr != LLDB_INVALID_ADDRESS)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002570 {
Greg Clayton526a4ae2012-05-16 22:09:01 +00002571 DWARFDebugInfoEntry *function_die = NULL;
2572 DWARFDebugInfoEntry *block_die = NULL;
2573 dwarf_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002574
Greg Clayton526a4ae2012-05-16 22:09:01 +00002575 if (function_die != NULL)
2576 {
2577 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
2578 if (sc.function == NULL)
2579 sc.function = ParseCompileUnitFunction(sc, dwarf_cu, function_die);
2580 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002581
Greg Clayton526a4ae2012-05-16 22:09:01 +00002582 if (sc.function != NULL)
2583 {
2584 Block& block = sc.function->GetBlock (true);
2585
2586 if (block_die != NULL)
2587 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
2588 else
2589 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
2590 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002591 }
2592 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002593
Greg Clayton526a4ae2012-05-16 22:09:01 +00002594 sc_list.Append(sc);
2595 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2596 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002597 }
2598 }
Greg Clayton526a4ae2012-05-16 22:09:01 +00002599 else if (file_spec_matches_cu_file_spec && !check_inlines)
2600 {
2601 // only append the context if we aren't looking for inline call sites
2602 // by file and line and if the file spec matches that of the compile unit
2603 sc_list.Append(sc);
2604 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002605 }
2606 else if (file_spec_matches_cu_file_spec && !check_inlines)
2607 {
2608 // only append the context if we aren't looking for inline call sites
2609 // by file and line and if the file spec matches that of the compile unit
2610 sc_list.Append(sc);
2611 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002612
Greg Clayton526a4ae2012-05-16 22:09:01 +00002613 if (!check_inlines)
2614 break;
2615 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002616 }
2617 }
2618 }
2619 }
2620 return sc_list.GetSize() - prev_size;
2621}
2622
2623void
2624SymbolFileDWARF::Index ()
2625{
2626 if (m_indexed)
2627 return;
2628 m_indexed = true;
2629 Timer scoped_timer (__PRETTY_FUNCTION__,
2630 "SymbolFileDWARF::Index (%s)",
2631 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2632
2633 DWARFDebugInfo* debug_info = DebugInfo();
2634 if (debug_info)
2635 {
2636 uint32_t cu_idx = 0;
2637 const uint32_t num_compile_units = GetNumCompileUnits();
2638 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2639 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00002640 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002641
Greg Clayton53eb1c22012-04-02 22:59:12 +00002642 bool clear_dies = dwarf_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002643
Greg Clayton53eb1c22012-04-02 22:59:12 +00002644 dwarf_cu->Index (cu_idx,
2645 m_function_basename_index,
2646 m_function_fullname_index,
2647 m_function_method_index,
2648 m_function_selector_index,
2649 m_objc_class_selectors_index,
2650 m_global_index,
2651 m_type_index,
2652 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002653
2654 // Keep memory down by clearing DIEs if this generate function
2655 // caused them to be parsed
2656 if (clear_dies)
Greg Clayton53eb1c22012-04-02 22:59:12 +00002657 dwarf_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002658 }
2659
Greg Claytond4a2b372011-09-12 23:21:58 +00002660 m_function_basename_index.Finalize();
2661 m_function_fullname_index.Finalize();
2662 m_function_method_index.Finalize();
2663 m_function_selector_index.Finalize();
2664 m_objc_class_selectors_index.Finalize();
2665 m_global_index.Finalize();
2666 m_type_index.Finalize();
2667 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002668
Greg Clayton24739922010-10-13 03:15:28 +00002669#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002670 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002671 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002672 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2673 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002674 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2675 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2676 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2677 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2678 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2679 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002680 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002681 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002682#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002683 }
2684}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002685
2686bool
2687SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2688{
2689 if (namespace_decl == NULL)
2690 {
2691 // Invalid namespace decl which means we aren't matching only things
2692 // in this symbol file, so return true to indicate it matches this
2693 // symbol file.
2694 return true;
2695 }
2696
2697 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2698
2699 if (namespace_ast == NULL)
2700 return true; // No AST in the "namespace_decl", return true since it
2701 // could then match any symbol file, including this one
2702
2703 if (namespace_ast == GetClangASTContext().getASTContext())
2704 return true; // The ASTs match, return true
2705
2706 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002707 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2708
2709 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002710 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002711
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002712 return false;
2713}
2714
Greg Clayton2506a7a2011-10-12 23:34:26 +00002715bool
2716SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2717 DWARFCompileUnit* cu,
2718 const DWARFDebugInfoEntry* die)
2719{
2720 // No namespace specified, so the answesr i
2721 if (namespace_decl == NULL)
2722 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002723
2724 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002725
Greg Clayton437a1352012-04-09 22:43:43 +00002726 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
2727 clang::DeclContext *die_clang_decl_ctx = GetClangDeclContextContainingDIE (cu, die, &decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002728 if (decl_ctx_die)
Greg Clayton437a1352012-04-09 22:43:43 +00002729 {
Greg Clayton2506a7a2011-10-12 23:34:26 +00002730 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
Greg Clayton437a1352012-04-09 22:43:43 +00002731
Greg Clayton2506a7a2011-10-12 23:34:26 +00002732 if (clang_namespace_decl)
2733 {
2734 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002735 {
2736 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002737 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002738 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002739 }
2740
Greg Clayton437a1352012-04-09 22:43:43 +00002741 if (clang_namespace_decl == die_clang_decl_ctx)
2742 return true;
2743 else
Greg Clayton2506a7a2011-10-12 23:34:26 +00002744 return false;
Greg Clayton2506a7a2011-10-12 23:34:26 +00002745 }
2746 else
2747 {
2748 // We have a namespace_decl that was not NULL but it contained
2749 // a NULL "clang::NamespaceDecl", so this means the global namespace
2750 // So as long the the contained decl context DIE isn't a namespace
2751 // we should be ok.
2752 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2753 return true;
2754 }
2755 }
Sean Callananebe60672011-10-13 21:50:33 +00002756
2757 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002758 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002759
Greg Clayton2506a7a2011-10-12 23:34:26 +00002760 return false;
2761}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002762uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002763SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002764{
Greg Clayton21f2a492011-10-06 00:09:08 +00002765 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2766
2767 if (log)
2768 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002769 GetObjectFile()->GetModule()->LogMessage (log.get(),
2770 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2771 name.GetCString(),
2772 namespace_decl,
2773 append,
2774 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002775 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002776
2777 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2778 return 0;
2779
Greg Claytonc685f8e2010-09-15 04:15:46 +00002780 DWARFDebugInfo* info = DebugInfo();
2781 if (info == NULL)
2782 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002783
2784 // If we aren't appending the results to this list, then clear the list
2785 if (!append)
2786 variables.Clear();
2787
2788 // Remember how many variables are in the list before we search in case
2789 // we are appending the results to a variable list.
2790 const uint32_t original_size = variables.GetSize();
2791
Greg Claytond4a2b372011-09-12 23:21:58 +00002792 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002793
Greg Clayton97fbc342011-10-20 22:30:33 +00002794 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002795 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002796 if (m_apple_names_ap.get())
2797 {
2798 const char *name_cstr = name.GetCString();
2799 const char *base_name_start;
2800 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002801
Greg Clayton97fbc342011-10-20 22:30:33 +00002802 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2803 base_name_start = name_cstr;
2804
2805 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2806 }
Greg Clayton7f995132011-10-04 22:41:51 +00002807 }
2808 else
2809 {
2810 // Index the DWARF if we haven't already
2811 if (!m_indexed)
2812 Index ();
2813
2814 m_global_index.Find (name, die_offsets);
2815 }
2816
Greg Clayton437a1352012-04-09 22:43:43 +00002817 const size_t num_die_matches = die_offsets.size();
2818 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002819 {
Greg Clayton7f995132011-10-04 22:41:51 +00002820 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002821 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002822 assert (sc.module_sp);
2823
Greg Claytond4a2b372011-09-12 23:21:58 +00002824 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002825 DWARFCompileUnit* dwarf_cu = NULL;
2826 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton437a1352012-04-09 22:43:43 +00002827 bool done = false;
2828 for (size_t i=0; i<num_die_matches && !done; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00002829 {
2830 const dw_offset_t die_offset = die_offsets[i];
2831 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002832
Greg Clayton95d87902011-11-11 03:16:25 +00002833 if (die)
2834 {
Greg Clayton437a1352012-04-09 22:43:43 +00002835 switch (die->Tag())
2836 {
2837 default:
2838 case DW_TAG_subprogram:
2839 case DW_TAG_inlined_subroutine:
2840 case DW_TAG_try_block:
2841 case DW_TAG_catch_block:
2842 break;
2843
2844 case DW_TAG_variable:
2845 {
2846 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Greg Clayton437a1352012-04-09 22:43:43 +00002847
2848 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2849 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002850
Greg Clayton437a1352012-04-09 22:43:43 +00002851 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002852
Greg Clayton437a1352012-04-09 22:43:43 +00002853 if (variables.GetSize() - original_size >= max_matches)
2854 done = true;
2855 }
2856 break;
2857 }
Greg Clayton95d87902011-11-11 03:16:25 +00002858 }
2859 else
2860 {
2861 if (m_using_apple_tables)
2862 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002863 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2864 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002865 }
2866 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002867 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002868 }
2869
2870 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00002871 const uint32_t num_matches = variables.GetSize() - original_size;
2872 if (log && num_matches > 0)
2873 {
2874 GetObjectFile()->GetModule()->LogMessage (log.get(),
2875 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables) => %u",
2876 name.GetCString(),
2877 namespace_decl,
2878 append,
2879 max_matches,
2880 num_matches);
2881 }
2882 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002883}
2884
2885uint32_t
2886SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2887{
Greg Clayton21f2a492011-10-06 00:09:08 +00002888 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2889
2890 if (log)
2891 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002892 GetObjectFile()->GetModule()->LogMessage (log.get(),
2893 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2894 regex.GetText(),
2895 append,
2896 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002897 }
2898
Greg Claytonc685f8e2010-09-15 04:15:46 +00002899 DWARFDebugInfo* info = DebugInfo();
2900 if (info == NULL)
2901 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002902
2903 // If we aren't appending the results to this list, then clear the list
2904 if (!append)
2905 variables.Clear();
2906
2907 // Remember how many variables are in the list before we search in case
2908 // we are appending the results to a variable list.
2909 const uint32_t original_size = variables.GetSize();
2910
Greg Clayton7f995132011-10-04 22:41:51 +00002911 DIEArray die_offsets;
2912
Greg Clayton97fbc342011-10-20 22:30:33 +00002913 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002914 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002915 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002916 {
2917 DWARFMappedHash::DIEInfoArray hash_data_array;
2918 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2919 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2920 }
Greg Clayton7f995132011-10-04 22:41:51 +00002921 }
2922 else
2923 {
2924 // Index the DWARF if we haven't already
2925 if (!m_indexed)
2926 Index ();
2927
2928 m_global_index.Find (regex, die_offsets);
2929 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002930
Greg Claytonc685f8e2010-09-15 04:15:46 +00002931 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002932 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002933 assert (sc.module_sp);
2934
Greg Claytond4a2b372011-09-12 23:21:58 +00002935 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002936 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002937 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002938 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002939 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002940 DWARFDebugInfo* debug_info = DebugInfo();
2941 for (size_t i=0; i<num_matches; ++i)
2942 {
2943 const dw_offset_t die_offset = die_offsets[i];
2944 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002945
2946 if (die)
2947 {
2948 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002949
Greg Clayton95d87902011-11-11 03:16:25 +00002950 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002951
Greg Clayton95d87902011-11-11 03:16:25 +00002952 if (variables.GetSize() - original_size >= max_matches)
2953 break;
2954 }
2955 else
2956 {
2957 if (m_using_apple_tables)
2958 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002959 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2960 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002961 }
2962 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002963 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002964 }
2965
2966 // Return the number of variable that were appended to the list
2967 return variables.GetSize() - original_size;
2968}
2969
Greg Claytonaa044962011-10-13 00:59:38 +00002970
Jim Ingham4cda6e02011-10-07 22:23:45 +00002971bool
2972SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2973 DWARFCompileUnit *&dwarf_cu,
2974 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002975{
Greg Claytonaa044962011-10-13 00:59:38 +00002976 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2977 return ResolveFunction (dwarf_cu, die, sc_list);
2978}
2979
2980
2981bool
2982SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2983 const DWARFDebugInfoEntry *die,
2984 SymbolContextList& sc_list)
2985{
Greg Clayton9e315582011-09-02 04:03:59 +00002986 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002987
2988 if (die == NULL)
2989 return false;
2990
Jim Ingham4cda6e02011-10-07 22:23:45 +00002991 // If we were passed a die that is not a function, just return false...
2992 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2993 return false;
2994
2995 const DWARFDebugInfoEntry* inlined_die = NULL;
2996 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002997 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002998 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002999
Jim Ingham4cda6e02011-10-07 22:23:45 +00003000 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00003001 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003002 if (die->Tag() == DW_TAG_subprogram)
3003 break;
Greg Clayton9e315582011-09-02 04:03:59 +00003004 }
3005 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003006 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00003007 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003008 {
3009 Address addr;
3010 // Parse all blocks if needed
3011 if (inlined_die)
3012 {
Greg Clayton81c22f62011-10-19 18:09:39 +00003013 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00003014 assert (sc.block != NULL);
3015 if (sc.block->GetStartAddress (addr) == false)
3016 addr.Clear();
3017 }
3018 else
3019 {
3020 sc.block = NULL;
3021 addr = sc.function->GetAddressRange().GetBaseAddress();
3022 }
3023
3024 if (addr.IsValid())
3025 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003026 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00003027 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003028 }
3029 }
3030
Greg Claytonaa044962011-10-13 00:59:38 +00003031 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00003032}
3033
Greg Clayton7f995132011-10-04 22:41:51 +00003034void
3035SymbolFileDWARF::FindFunctions (const ConstString &name,
3036 const NameToDIE &name_to_die,
3037 SymbolContextList& sc_list)
3038{
Greg Claytond4a2b372011-09-12 23:21:58 +00003039 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003040 if (name_to_die.Find (name, die_offsets))
3041 {
3042 ParseFunctions (die_offsets, sc_list);
3043 }
3044}
3045
3046
3047void
3048SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3049 const NameToDIE &name_to_die,
3050 SymbolContextList& sc_list)
3051{
3052 DIEArray die_offsets;
3053 if (name_to_die.Find (regex, die_offsets))
3054 {
3055 ParseFunctions (die_offsets, sc_list);
3056 }
3057}
3058
3059
3060void
3061SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
3062 const DWARFMappedHash::MemoryTable &memory_table,
3063 SymbolContextList& sc_list)
3064{
3065 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00003066 DWARFMappedHash::DIEInfoArray hash_data_array;
3067 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00003068 {
Greg Claytond1767f02011-12-08 02:13:16 +00003069 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00003070 ParseFunctions (die_offsets, sc_list);
3071 }
3072}
3073
3074void
3075SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
3076 SymbolContextList& sc_list)
3077{
3078 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003079 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00003080 {
Greg Clayton7f995132011-10-04 22:41:51 +00003081 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00003082
3083 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003084 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00003085 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003086 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00003087 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003088 }
3089 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00003090}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003091
Jim Ingham4cda6e02011-10-07 22:23:45 +00003092bool
3093SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
3094 const DWARFCompileUnit *dwarf_cu,
3095 uint32_t name_type_mask,
3096 const char *partial_name,
3097 const char *base_name_start,
3098 const char *base_name_end)
3099{
3100 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
3101 if (name_type_mask == eFunctionNameTypeMethod
3102 || name_type_mask == eFunctionNameTypeBase)
3103 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003104 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
3105 if (!containing_decl_ctx)
3106 return false;
3107
3108 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
3109
3110 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
3111 return false;
3112 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
3113 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003114 }
3115
3116 // Now we need to check whether the name we got back for this type matches the extra specifications
3117 // that were in the name we're looking up:
3118 if (base_name_start != partial_name || *base_name_end != '\0')
3119 {
3120 // First see if the stuff to the left matches the full name. To do that let's see if
3121 // we can pull out the mips linkage name attribute:
3122
3123 Mangled best_name;
3124
3125 DWARFDebugInfoEntry::Attributes attributes;
3126 die->GetAttributes(this, dwarf_cu, NULL, attributes);
3127 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
3128 if (idx != UINT32_MAX)
3129 {
3130 DWARFFormValue form_value;
3131 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
3132 {
3133 const char *name = form_value.AsCString(&get_debug_str_data());
Greg Clayton037520e2012-07-18 23:18:10 +00003134 best_name.SetValue (ConstString(name), true);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003135 }
3136 }
3137 if (best_name)
3138 {
3139 const char *demangled = best_name.GetDemangledName().GetCString();
3140 if (demangled)
3141 {
3142 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00003143 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
3144 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003145 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00003146 else
3147 {
3148 // Sort out the case where our name is something like "Process::Destroy" and the match is
3149 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
3150 // namespace boundaries...
3151
3152 if (partial_name[0] == ':' && partial_name[1] == ':')
3153 {
3154 // The partial name was already on a namespace boundary so all matches are good.
3155 return true;
3156 }
3157 else if (partial_in_demangled == demangled)
3158 {
3159 // They both start the same, so this is an good match.
3160 return true;
3161 }
3162 else
3163 {
3164 if (partial_in_demangled - demangled == 1)
3165 {
3166 // Only one character difference, can't be a namespace boundary...
3167 return false;
3168 }
3169 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
3170 {
3171 // We are on a namespace boundary, so this is also good.
3172 return true;
3173 }
3174 else
3175 return false;
3176 }
3177 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003178 }
3179 }
3180 }
3181
3182 return true;
3183}
Greg Claytonc685f8e2010-09-15 04:15:46 +00003184
Greg Clayton0c5cd902010-06-28 21:30:43 +00003185uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00003186SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00003187 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00003188 uint32_t name_type_mask,
3189 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00003190 bool append,
3191 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00003192{
3193 Timer scoped_timer (__PRETTY_FUNCTION__,
3194 "SymbolFileDWARF::FindFunctions (name = '%s')",
3195 name.AsCString());
3196
Greg Clayton21f2a492011-10-06 00:09:08 +00003197 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3198
3199 if (log)
3200 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003201 GetObjectFile()->GetModule()->LogMessage (log.get(),
3202 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
3203 name.GetCString(),
3204 name_type_mask,
3205 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003206 }
3207
Greg Clayton0c5cd902010-06-28 21:30:43 +00003208 // If we aren't appending the results to this list, then clear the list
3209 if (!append)
3210 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003211
3212 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3213 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003214
3215 // If name is empty then we won't find anything.
3216 if (name.IsEmpty())
3217 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00003218
3219 // Remember how many sc_list are in the list before we search in case
3220 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00003221
Greg Clayton9e315582011-09-02 04:03:59 +00003222 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00003223
Jim Ingham4cda6e02011-10-07 22:23:45 +00003224 const char *name_cstr = name.GetCString();
3225 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
3226 const char *base_name_start = name_cstr;
3227 const char *base_name_end = name_cstr + strlen(name_cstr);
3228
3229 if (name_type_mask & eFunctionNameTypeAuto)
3230 {
3231 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
3232 effective_name_type_mask = eFunctionNameTypeFull;
3233 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3234 effective_name_type_mask = eFunctionNameTypeFull;
3235 else
3236 {
3237 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3238 effective_name_type_mask |= eFunctionNameTypeSelector;
3239
3240 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3241 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3242 }
3243 }
3244 else
3245 {
3246 effective_name_type_mask = name_type_mask;
3247 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3248 {
3249 // If they've asked for a CPP method or function name and it can't be that, we don't
3250 // even need to search for CPP methods or names.
3251 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3252 {
3253 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3254 if (effective_name_type_mask == eFunctionNameTypeNone)
3255 return 0;
3256 }
3257 }
3258
3259 if (effective_name_type_mask & eFunctionNameTypeSelector)
3260 {
3261 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3262 {
3263 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3264 if (effective_name_type_mask == eFunctionNameTypeNone)
3265 return 0;
3266 }
3267 }
3268 }
3269
3270 DWARFDebugInfo* info = DebugInfo();
3271 if (info == NULL)
3272 return 0;
3273
Greg Claytonaa044962011-10-13 00:59:38 +00003274 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003275 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003276 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003277 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003278 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003279
3280 DIEArray die_offsets;
3281
3282 uint32_t num_matches = 0;
3283
3284 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003285 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003286 // If they asked for the full name, match what they typed. At some point we may
3287 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3288 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003289 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003290 for (uint32_t i = 0; i < num_matches; i++)
3291 {
Greg Clayton95d87902011-11-11 03:16:25 +00003292 const dw_offset_t die_offset = die_offsets[i];
3293 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003294 if (die)
3295 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003296 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3297 continue;
3298
Sean Callanan9df05fb2012-02-10 22:52:19 +00003299 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3300 continue;
3301
Greg Claytonaa044962011-10-13 00:59:38 +00003302 ResolveFunction (dwarf_cu, die, sc_list);
3303 }
Greg Clayton95d87902011-11-11 03:16:25 +00003304 else
3305 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003306 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3307 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003308 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003309 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003310 }
3311 else
3312 {
3313 if (effective_name_type_mask & eFunctionNameTypeSelector)
3314 {
3315 if (namespace_decl && *namespace_decl)
3316 return 0; // no selectors in namespaces
3317
3318 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3319 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3320 // and if it is an ObjC method name, we're good.
3321
3322 for (uint32_t i = 0; i < num_matches; i++)
3323 {
Greg Clayton95d87902011-11-11 03:16:25 +00003324 const dw_offset_t die_offset = die_offsets[i];
3325 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003326 if (die)
3327 {
3328 const char *die_name = die->GetName(this, dwarf_cu);
3329 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003330 {
3331 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3332 continue;
3333
Greg Clayton97fbc342011-10-20 22:30:33 +00003334 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003335 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003336 }
Greg Clayton95d87902011-11-11 03:16:25 +00003337 else
3338 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003339 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3340 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003341 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003342 }
3343 die_offsets.clear();
3344 }
3345
3346 if (effective_name_type_mask & eFunctionNameTypeMethod
3347 || effective_name_type_mask & eFunctionNameTypeBase)
3348 {
3349 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3350 (namespace_decl && *namespace_decl))
3351 return 0; // no methods in namespaces
3352
3353 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3354 // extract the base name, look that up, and if there is any other information in the name we were
3355 // passed in we have to post-filter based on that.
3356
3357 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3358 std::string base_name(base_name_start, base_name_end - base_name_start);
3359 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3360
3361 for (uint32_t i = 0; i < num_matches; i++)
3362 {
Greg Clayton95d87902011-11-11 03:16:25 +00003363 const dw_offset_t die_offset = die_offsets[i];
3364 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003365 if (die)
3366 {
3367 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3368 continue;
3369
3370 if (!FunctionDieMatchesPartialName(die,
3371 dwarf_cu,
3372 effective_name_type_mask,
3373 name_cstr,
3374 base_name_start,
3375 base_name_end))
3376 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003377
3378 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3379 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003380
3381 // If we get to here, the die is good, and we should add it:
3382 ResolveFunction (dwarf_cu, die, sc_list);
3383 }
Greg Clayton95d87902011-11-11 03:16:25 +00003384 else
3385 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003386 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3387 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003388 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003389 }
3390 die_offsets.clear();
3391 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003392 }
3393 }
Greg Clayton7f995132011-10-04 22:41:51 +00003394 }
3395 else
3396 {
3397
3398 // Index the DWARF if we haven't already
3399 if (!m_indexed)
3400 Index ();
3401
Greg Clayton7f995132011-10-04 22:41:51 +00003402 if (name_type_mask & eFunctionNameTypeFull)
3403 FindFunctions (name, m_function_fullname_index, sc_list);
3404
Jim Ingham4cda6e02011-10-07 22:23:45 +00003405 std::string base_name(base_name_start, base_name_end - base_name_start);
3406 ConstString base_name_const(base_name.c_str());
3407 DIEArray die_offsets;
3408 DWARFCompileUnit *dwarf_cu = NULL;
3409
3410 if (effective_name_type_mask & eFunctionNameTypeBase)
3411 {
3412 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003413 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003414 {
Greg Claytonaa044962011-10-13 00:59:38 +00003415 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3416 if (die)
3417 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003418 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3419 continue;
3420
Greg Claytonaa044962011-10-13 00:59:38 +00003421 if (!FunctionDieMatchesPartialName(die,
3422 dwarf_cu,
3423 effective_name_type_mask,
3424 name_cstr,
3425 base_name_start,
3426 base_name_end))
3427 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003428
Sean Callanan9df05fb2012-02-10 22:52:19 +00003429 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3430 continue;
3431
Greg Claytonaa044962011-10-13 00:59:38 +00003432 // If we get to here, the die is good, and we should add it:
3433 ResolveFunction (dwarf_cu, die, sc_list);
3434 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003435 }
3436 die_offsets.clear();
3437 }
3438
Jim Inghamea8005a2011-10-11 01:18:11 +00003439 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003440 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003441 if (namespace_decl && *namespace_decl)
3442 return 0; // no methods in namespaces
3443
Jim Ingham4cda6e02011-10-07 22:23:45 +00003444 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3445 {
Greg Claytonaa044962011-10-13 00:59:38 +00003446 for (uint32_t i = 0; i < num_base; i++)
3447 {
3448 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3449 if (die)
3450 {
3451 if (!FunctionDieMatchesPartialName(die,
3452 dwarf_cu,
3453 effective_name_type_mask,
3454 name_cstr,
3455 base_name_start,
3456 base_name_end))
3457 continue;
3458
Sean Callanan9df05fb2012-02-10 22:52:19 +00003459 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3460 continue;
3461
Greg Claytonaa044962011-10-13 00:59:38 +00003462 // If we get to here, the die is good, and we should add it:
3463 ResolveFunction (dwarf_cu, die, sc_list);
3464 }
3465 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003466 }
3467 die_offsets.clear();
3468 }
Greg Clayton7f995132011-10-04 22:41:51 +00003469
Sean Callanan213fdb82011-10-13 01:49:10 +00003470 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003471 {
Greg Clayton7f995132011-10-04 22:41:51 +00003472 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003473 }
3474
Greg Clayton4d01ace2011-09-29 16:58:15 +00003475 }
3476
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003477 // Return the number of variable that were appended to the list
Greg Clayton437a1352012-04-09 22:43:43 +00003478 const uint32_t num_matches = sc_list.GetSize() - original_size;
3479
3480 if (log && num_matches > 0)
3481 {
3482 GetObjectFile()->GetModule()->LogMessage (log.get(),
3483 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list) => %u",
3484 name.GetCString(),
3485 name_type_mask,
3486 append,
3487 num_matches);
3488 }
3489 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003490}
3491
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003492uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003493SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003494{
3495 Timer scoped_timer (__PRETTY_FUNCTION__,
3496 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3497 regex.GetText());
3498
Greg Clayton21f2a492011-10-06 00:09:08 +00003499 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3500
3501 if (log)
3502 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003503 GetObjectFile()->GetModule()->LogMessage (log.get(),
3504 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3505 regex.GetText(),
3506 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003507 }
3508
3509
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003510 // If we aren't appending the results to this list, then clear the list
3511 if (!append)
3512 sc_list.Clear();
3513
3514 // Remember how many sc_list are in the list before we search in case
3515 // we are appending the results to a variable list.
3516 uint32_t original_size = sc_list.GetSize();
3517
Greg Clayton97fbc342011-10-20 22:30:33 +00003518 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003519 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003520 if (m_apple_names_ap.get())
3521 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003522 }
3523 else
3524 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003525 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003526 if (!m_indexed)
3527 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003528
Greg Clayton7f995132011-10-04 22:41:51 +00003529 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003530
Greg Clayton7f995132011-10-04 22:41:51 +00003531 FindFunctions (regex, m_function_fullname_index, sc_list);
3532 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003533
3534 // Return the number of variable that were appended to the list
3535 return sc_list.GetSize() - original_size;
3536}
Jim Ingham318c9f22011-08-26 19:44:13 +00003537
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003538uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003539SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3540 const ConstString &name,
3541 const lldb_private::ClangNamespaceDecl *namespace_decl,
3542 bool append,
3543 uint32_t max_matches,
3544 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003545{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003546 DWARFDebugInfo* info = DebugInfo();
3547 if (info == NULL)
3548 return 0;
3549
Greg Clayton21f2a492011-10-06 00:09:08 +00003550 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3551
3552 if (log)
3553 {
Greg Clayton437a1352012-04-09 22:43:43 +00003554 if (namespace_decl)
3555 {
3556 GetObjectFile()->GetModule()->LogMessage (log.get(),
3557 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list)",
3558 name.GetCString(),
3559 namespace_decl->GetNamespaceDecl(),
3560 namespace_decl->GetQualifiedName().c_str(),
3561 append,
3562 max_matches);
3563 }
3564 else
3565 {
3566 GetObjectFile()->GetModule()->LogMessage (log.get(),
3567 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list)",
3568 name.GetCString(),
3569 append,
3570 max_matches);
3571 }
Greg Clayton21f2a492011-10-06 00:09:08 +00003572 }
3573
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003574 // If we aren't appending the results to this list, then clear the list
3575 if (!append)
3576 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003577
3578 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3579 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003580
Greg Claytond4a2b372011-09-12 23:21:58 +00003581 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003582
Greg Clayton97fbc342011-10-20 22:30:33 +00003583 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003584 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003585 if (m_apple_types_ap.get())
3586 {
3587 const char *name_cstr = name.GetCString();
3588 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3589 }
Greg Clayton7f995132011-10-04 22:41:51 +00003590 }
3591 else
3592 {
3593 if (!m_indexed)
3594 Index ();
3595
3596 m_type_index.Find (name, die_offsets);
3597 }
3598
Greg Clayton437a1352012-04-09 22:43:43 +00003599 const size_t num_die_matches = die_offsets.size();
Greg Clayton7f995132011-10-04 22:41:51 +00003600
Greg Clayton437a1352012-04-09 22:43:43 +00003601 if (num_die_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003602 {
Greg Clayton7f995132011-10-04 22:41:51 +00003603 const uint32_t initial_types_size = types.GetSize();
3604 DWARFCompileUnit* dwarf_cu = NULL;
3605 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003606 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton437a1352012-04-09 22:43:43 +00003607 for (size_t i=0; i<num_die_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003608 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003609 const dw_offset_t die_offset = die_offsets[i];
3610 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3611
Greg Clayton95d87902011-11-11 03:16:25 +00003612 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003613 {
Greg Clayton95d87902011-11-11 03:16:25 +00003614 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3615 continue;
3616
3617 Type *matching_type = ResolveType (dwarf_cu, die);
3618 if (matching_type)
3619 {
3620 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003621 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003622 if (types.GetSize() >= max_matches)
3623 break;
3624 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003625 }
Greg Clayton95d87902011-11-11 03:16:25 +00003626 else
3627 {
3628 if (m_using_apple_tables)
3629 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003630 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3631 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003632 }
3633 }
3634
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003635 }
Greg Clayton437a1352012-04-09 22:43:43 +00003636 const uint32_t num_matches = types.GetSize() - initial_types_size;
3637 if (log && num_matches)
3638 {
3639 if (namespace_decl)
3640 {
3641 GetObjectFile()->GetModule()->LogMessage (log.get(),
3642 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(%p) \"%s\", append=%u, max_matches=%u, type_list) => %u",
3643 name.GetCString(),
3644 namespace_decl->GetNamespaceDecl(),
3645 namespace_decl->GetQualifiedName().c_str(),
3646 append,
3647 max_matches,
3648 num_matches);
3649 }
3650 else
3651 {
3652 GetObjectFile()->GetModule()->LogMessage (log.get(),
3653 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", clang::NamespaceDecl(NULL), append=%u, max_matches=%u, type_list) => %u",
3654 name.GetCString(),
3655 append,
3656 max_matches,
3657 num_matches);
3658 }
3659 }
3660 return num_matches;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661 }
Greg Clayton7f995132011-10-04 22:41:51 +00003662 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003663}
3664
3665
Greg Clayton526e5af2010-11-13 03:52:47 +00003666ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003667SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003668 const ConstString &name,
3669 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003670{
Greg Clayton21f2a492011-10-06 00:09:08 +00003671 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3672
3673 if (log)
3674 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003675 GetObjectFile()->GetModule()->LogMessage (log.get(),
3676 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3677 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003678 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003679
3680 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3681 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003682
Greg Clayton526e5af2010-11-13 03:52:47 +00003683 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003684 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003685 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003686 {
Greg Clayton7f995132011-10-04 22:41:51 +00003687 DIEArray die_offsets;
3688
Greg Clayton526e5af2010-11-13 03:52:47 +00003689 // Index if we already haven't to make sure the compile units
3690 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003691 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003692 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003693 if (m_apple_namespaces_ap.get())
3694 {
3695 const char *name_cstr = name.GetCString();
3696 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3697 }
Greg Clayton7f995132011-10-04 22:41:51 +00003698 }
3699 else
3700 {
3701 if (!m_indexed)
3702 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003703
Greg Clayton7f995132011-10-04 22:41:51 +00003704 m_namespace_index.Find (name, die_offsets);
3705 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003706
3707 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003708 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003709 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003710 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003711 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003712 DWARFDebugInfo* debug_info = DebugInfo();
3713 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003714 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003715 const dw_offset_t die_offset = die_offsets[i];
3716 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003717
Greg Clayton95d87902011-11-11 03:16:25 +00003718 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003719 {
Greg Clayton95d87902011-11-11 03:16:25 +00003720 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3721 continue;
3722
3723 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3724 if (clang_namespace_decl)
3725 {
3726 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3727 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003728 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003729 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003730 }
Greg Clayton95d87902011-11-11 03:16:25 +00003731 else
3732 {
3733 if (m_using_apple_tables)
3734 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003735 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3736 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003737 }
3738 }
3739
Greg Clayton526e5af2010-11-13 03:52:47 +00003740 }
3741 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003742 }
Greg Clayton437a1352012-04-09 22:43:43 +00003743 if (log && namespace_decl.GetNamespaceDecl())
3744 {
3745 GetObjectFile()->GetModule()->LogMessage (log.get(),
3746 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\") => clang::NamespaceDecl(%p) \"%s\"",
3747 name.GetCString(),
3748 namespace_decl.GetNamespaceDecl(),
3749 namespace_decl.GetQualifiedName().c_str());
3750 }
3751
Greg Clayton526e5af2010-11-13 03:52:47 +00003752 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003753}
3754
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003755uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003756SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003757{
3758 // Remember how many sc_list are in the list before we search in case
3759 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003760 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003761
3762 const uint32_t num_die_offsets = die_offsets.size();
3763 // Parse all of the types we found from the pubtypes matches
3764 uint32_t i;
3765 uint32_t num_matches = 0;
3766 for (i = 0; i < num_die_offsets; ++i)
3767 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003768 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3769 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003770 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003771 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003772 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003773 ++num_matches;
3774 if (num_matches >= max_matches)
3775 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003776 }
3777 }
3778
3779 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003780 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003781}
3782
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003783
3784size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003785SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3786 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003787 DWARFCompileUnit* dwarf_cu,
3788 const DWARFDebugInfoEntry *parent_die,
3789 bool skip_artificial,
3790 bool &is_static,
3791 TypeList* type_list,
3792 std::vector<clang_type_t>& function_param_types,
3793 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003794 unsigned &type_quals,
3795 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003796{
3797 if (parent_die == NULL)
3798 return 0;
3799
Greg Claytond88d7592010-09-15 08:33:30 +00003800 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3801
Greg Clayton7fedea22010-11-16 02:10:54 +00003802 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003803 const DWARFDebugInfoEntry *die;
3804 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3805 {
3806 dw_tag_t tag = die->Tag();
3807 switch (tag)
3808 {
3809 case DW_TAG_formal_parameter:
3810 {
3811 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003812 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003813 if (num_attributes > 0)
3814 {
3815 const char *name = NULL;
3816 Declaration decl;
3817 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003818 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003819 // one of None, Auto, Register, Extern, Static, PrivateExtern
3820
Sean Callanane2ef6e32010-09-23 03:01:22 +00003821 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003822 uint32_t i;
3823 for (i=0; i<num_attributes; ++i)
3824 {
3825 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3826 DWARFFormValue form_value;
3827 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3828 {
3829 switch (attr)
3830 {
3831 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3832 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3833 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3834 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3835 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003836 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003837 case DW_AT_location:
3838 // if (form_value.BlockData())
3839 // {
3840 // const DataExtractor& debug_info_data = debug_info();
3841 // uint32_t block_length = form_value.Unsigned();
3842 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3843 // }
3844 // else
3845 // {
3846 // }
3847 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003848 case DW_AT_const_value:
3849 case DW_AT_default_value:
3850 case DW_AT_description:
3851 case DW_AT_endianity:
3852 case DW_AT_is_optional:
3853 case DW_AT_segment:
3854 case DW_AT_variable_parameter:
3855 default:
3856 case DW_AT_abstract_origin:
3857 case DW_AT_sibling:
3858 break;
3859 }
3860 }
3861 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003862
Greg Clayton0fffff52010-09-24 05:15:53 +00003863 bool skip = false;
3864 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003865 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003866 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003867 {
3868 // In order to determine if a C++ member function is
3869 // "const" we have to look at the const-ness of "this"...
3870 // Ugly, but that
3871 if (arg_idx == 0)
3872 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003873 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003874 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003875 // Often times compilers omit the "this" name for the
3876 // specification DIEs, so we can't rely upon the name
3877 // being in the formal parameter DIE...
3878 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003879 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003880 Type *this_type = ResolveTypeUID (param_type_die_offset);
3881 if (this_type)
3882 {
3883 uint32_t encoding_mask = this_type->GetEncodingMask();
3884 if (encoding_mask & Type::eEncodingIsPointerUID)
3885 {
3886 is_static = false;
3887
3888 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3889 type_quals |= clang::Qualifiers::Const;
3890 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3891 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003892 }
3893 }
3894 }
3895 }
3896 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003897 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003898 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003899 else
3900 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003901
Greg Clayton0fffff52010-09-24 05:15:53 +00003902 // HACK: Objective C formal parameters "self" and "_cmd"
3903 // are not marked as artificial in the DWARF...
Greg Clayton53eb1c22012-04-02 22:59:12 +00003904 CompileUnit *comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3905 if (comp_unit)
Greg Clayton0fffff52010-09-24 05:15:53 +00003906 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00003907 switch (comp_unit->GetLanguage())
3908 {
3909 case eLanguageTypeObjC:
3910 case eLanguageTypeObjC_plus_plus:
3911 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3912 skip = true;
3913 break;
3914 default:
3915 break;
3916 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003917 }
3918 }
3919 }
3920
3921 if (!skip)
3922 {
3923 Type *type = ResolveTypeUID(param_type_die_offset);
3924 if (type)
3925 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003926 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003927
Greg Clayton42ce2f32011-12-12 21:50:19 +00003928 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3929 type->GetClangForwardType(),
3930 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003931 assert(param_var_decl);
3932 function_param_decls.push_back(param_var_decl);
Sean Callanan60217122012-04-13 00:10:03 +00003933
Jim Ingham379397632012-10-27 02:54:13 +00003934 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)param_var_decl, MakeUserID(die->GetOffset()));
Greg Clayton0fffff52010-09-24 05:15:53 +00003935 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936 }
3937 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003938 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003939 }
3940 break;
3941
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003942 case DW_TAG_template_type_parameter:
3943 case DW_TAG_template_value_parameter:
3944 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3945 break;
3946
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003947 default:
3948 break;
3949 }
3950 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003951 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003952}
3953
3954size_t
3955SymbolFileDWARF::ParseChildEnumerators
3956(
3957 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003958 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003959 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003960 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003961 const DWARFDebugInfoEntry *parent_die
3962)
3963{
3964 if (parent_die == NULL)
3965 return 0;
3966
3967 size_t enumerators_added = 0;
3968 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003969 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3970
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003971 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3972 {
3973 const dw_tag_t tag = die->Tag();
3974 if (tag == DW_TAG_enumerator)
3975 {
3976 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003977 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003978 if (num_child_attributes > 0)
3979 {
3980 const char *name = NULL;
3981 bool got_value = false;
3982 int64_t enum_value = 0;
3983 Declaration decl;
3984
3985 uint32_t i;
3986 for (i=0; i<num_child_attributes; ++i)
3987 {
3988 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3989 DWARFFormValue form_value;
3990 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3991 {
3992 switch (attr)
3993 {
3994 case DW_AT_const_value:
3995 got_value = true;
3996 enum_value = form_value.Unsigned();
3997 break;
3998
3999 case DW_AT_name:
4000 name = form_value.AsCString(&get_debug_str_data());
4001 break;
4002
4003 case DW_AT_description:
4004 default:
4005 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4006 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4007 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4008 case DW_AT_sibling:
4009 break;
4010 }
4011 }
4012 }
4013
4014 if (name && name[0] && got_value)
4015 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004016 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
4017 enumerator_clang_type,
4018 decl,
4019 name,
4020 enum_value,
4021 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004022 ++enumerators_added;
4023 }
4024 }
4025 }
4026 }
4027 return enumerators_added;
4028}
4029
4030void
4031SymbolFileDWARF::ParseChildArrayInfo
4032(
4033 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004034 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004035 const DWARFDebugInfoEntry *parent_die,
4036 int64_t& first_index,
4037 std::vector<uint64_t>& element_orders,
4038 uint32_t& byte_stride,
4039 uint32_t& bit_stride
4040)
4041{
4042 if (parent_die == NULL)
4043 return;
4044
4045 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00004046 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004047 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
4048 {
4049 const dw_tag_t tag = die->Tag();
4050 switch (tag)
4051 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004052 case DW_TAG_subrange_type:
4053 {
4054 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004055 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004056 if (num_child_attributes > 0)
4057 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004058 uint64_t num_elements = 0;
4059 uint64_t lower_bound = 0;
4060 uint64_t upper_bound = 0;
4061 uint32_t i;
4062 for (i=0; i<num_child_attributes; ++i)
4063 {
4064 const dw_attr_t attr = attributes.AttributeAtIndex(i);
4065 DWARFFormValue form_value;
4066 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4067 {
4068 switch (attr)
4069 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004070 case DW_AT_name:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004071 break;
4072
4073 case DW_AT_count:
4074 num_elements = form_value.Unsigned();
4075 break;
4076
4077 case DW_AT_bit_stride:
4078 bit_stride = form_value.Unsigned();
4079 break;
4080
4081 case DW_AT_byte_stride:
4082 byte_stride = form_value.Unsigned();
4083 break;
4084
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004085 case DW_AT_lower_bound:
4086 lower_bound = form_value.Unsigned();
4087 break;
4088
4089 case DW_AT_upper_bound:
4090 upper_bound = form_value.Unsigned();
4091 break;
4092
4093 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004094 case DW_AT_abstract_origin:
4095 case DW_AT_accessibility:
4096 case DW_AT_allocated:
4097 case DW_AT_associated:
4098 case DW_AT_data_location:
4099 case DW_AT_declaration:
4100 case DW_AT_description:
4101 case DW_AT_sibling:
4102 case DW_AT_threads_scaled:
4103 case DW_AT_type:
4104 case DW_AT_visibility:
4105 break;
4106 }
4107 }
4108 }
4109
4110 if (upper_bound > lower_bound)
4111 num_elements = upper_bound - lower_bound + 1;
4112
Sean Callananec979ba2012-10-22 23:56:48 +00004113 element_orders.push_back (num_elements);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004114 }
4115 }
4116 break;
4117 }
4118 }
4119}
4120
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004121TypeSP
Greg Clayton53eb1c22012-04-02 22:59:12 +00004122SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004123{
4124 TypeSP type_sp;
4125 if (die != NULL)
4126 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004127 assert(dwarf_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00004128 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004129 if (type_ptr == NULL)
4130 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004131 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(dwarf_cu);
Greg Claytonca512b32011-01-14 04:54:56 +00004132 assert (lldb_cu);
4133 SymbolContext sc(lldb_cu);
Greg Clayton53eb1c22012-04-02 22:59:12 +00004134 type_sp = ParseType(sc, dwarf_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004135 }
4136 else if (type_ptr != DIE_IS_BEING_PARSED)
4137 {
4138 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00004139 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004140 }
4141
4142 }
4143 return type_sp;
4144}
4145
4146clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00004147SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004148{
4149 if (die_offset != DW_INVALID_OFFSET)
4150 {
4151 DWARFCompileUnitSP cu_sp;
4152 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004153 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004154 }
4155 return NULL;
4156}
4157
Sean Callanan72e49402011-08-05 23:43:37 +00004158clang::DeclContext *
4159SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
4160{
4161 if (die_offset != DW_INVALID_OFFSET)
4162 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00004163 DWARFDebugInfo* debug_info = DebugInfo();
4164 if (debug_info)
4165 {
4166 DWARFCompileUnitSP cu_sp;
4167 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
4168 if (die)
4169 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
4170 }
Sean Callanan72e49402011-08-05 23:43:37 +00004171 }
4172 return NULL;
4173}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004174
Greg Clayton96d7d742010-11-10 23:42:09 +00004175clang::NamespaceDecl *
Greg Clayton53eb1c22012-04-02 22:59:12 +00004176SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *dwarf_cu, const DWARFDebugInfoEntry *die)
Greg Clayton96d7d742010-11-10 23:42:09 +00004177{
Greg Clayton030a2042011-10-14 21:34:45 +00004178 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00004179 {
Greg Clayton030a2042011-10-14 21:34:45 +00004180 // See if we already parsed this namespace DIE and associated it with a
4181 // uniqued namespace declaration
4182 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
4183 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00004184 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00004185 else
4186 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004187 const char *namespace_name = die->GetAttributeValueAsString(this, dwarf_cu, DW_AT_name, NULL);
4188 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00004189 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
4190 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4191 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00004192 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00004193 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00004194 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004195 GetObjectFile()->GetModule()->LogMessage (log.get(),
4196 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
4197 GetClangASTContext().getASTContext(),
4198 MakeUserID(die->GetOffset()),
4199 namespace_name,
4200 namespace_decl,
4201 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00004202 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004203 else
4204 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004205 GetObjectFile()->GetModule()->LogMessage (log.get(),
4206 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
4207 GetClangASTContext().getASTContext(),
4208 MakeUserID(die->GetOffset()),
4209 namespace_decl,
4210 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00004211 }
Greg Clayton030a2042011-10-14 21:34:45 +00004212 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00004213
4214 if (namespace_decl)
4215 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
4216 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00004217 }
4218 }
4219 return NULL;
4220}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004221
4222clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00004223SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00004224{
Greg Clayton5cf58b92011-10-05 22:22:08 +00004225 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4226 if (clang_decl_ctx)
4227 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004228 // If this DIE has a specification, or an abstract origin, then trace to those.
4229
Greg Claytoncab36a32011-12-08 05:16:30 +00004230 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004231 if (die_offset != DW_INVALID_OFFSET)
4232 return GetClangDeclContextForDIEOffset (sc, die_offset);
4233
Greg Claytoncab36a32011-12-08 05:16:30 +00004234 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00004235 if (die_offset != DW_INVALID_OFFSET)
4236 return GetClangDeclContextForDIEOffset (sc, die_offset);
4237
Greg Clayton3bffb082011-12-10 02:15:28 +00004238 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4239 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00004240 GetObjectFile()->GetModule()->LogMessage(log.get(), "SymbolFileDWARF::GetClangDeclContextForDIE (die = 0x%8.8x) %s '%s'", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), die->GetName(this, cu));
Sean Callanan72e49402011-08-05 23:43:37 +00004241 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00004242 bool assert_not_being_parsed = true;
4243 ResolveTypeUID (cu, die, assert_not_being_parsed);
4244
Greg Clayton5cf58b92011-10-05 22:22:08 +00004245 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4246
4247 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004248}
4249
4250clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004251SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004252{
Greg Claytonca512b32011-01-14 04:54:56 +00004253 if (m_clang_tu_decl == NULL)
4254 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004255
Greg Clayton2bc22f82011-09-30 03:20:47 +00004256 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004257
4258 if (decl_ctx_die_copy)
4259 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004260
4261 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004262 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004263
Greg Clayton2bc22f82011-09-30 03:20:47 +00004264 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4265 if (pos != m_die_to_decl_ctx.end())
4266 return pos->second;
4267
4268 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004269 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004270 case DW_TAG_compile_unit:
4271 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004272
Greg Clayton2bc22f82011-09-30 03:20:47 +00004273 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004274 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004275 break;
4276
4277 case DW_TAG_structure_type:
4278 case DW_TAG_union_type:
4279 case DW_TAG_class_type:
4280 {
4281 Type* type = ResolveType (cu, decl_ctx_die);
4282 if (type)
4283 {
4284 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4285 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004286 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004287 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4288 if (decl_ctx)
4289 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004290 }
4291 }
Greg Claytonca512b32011-01-14 04:54:56 +00004292 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004293 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004294
Greg Clayton2bc22f82011-09-30 03:20:47 +00004295 default:
4296 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004297 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004298 }
Greg Clayton7a345282010-11-09 23:46:37 +00004299 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004300}
4301
Greg Clayton2bc22f82011-09-30 03:20:47 +00004302
4303const DWARFDebugInfoEntry *
4304SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4305{
4306 if (cu && die)
4307 {
4308 const DWARFDebugInfoEntry * const decl_die = die;
4309
4310 while (die != NULL)
4311 {
4312 // If this is the original DIE that we are searching for a declaration
4313 // for, then don't look in the cache as we don't want our own decl
4314 // context to be our decl context...
4315 if (decl_die != die)
4316 {
4317 switch (die->Tag())
4318 {
4319 case DW_TAG_compile_unit:
4320 case DW_TAG_namespace:
4321 case DW_TAG_structure_type:
4322 case DW_TAG_union_type:
4323 case DW_TAG_class_type:
4324 return die;
4325
4326 default:
4327 break;
4328 }
4329 }
4330
4331 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4332 if (die_offset != DW_INVALID_OFFSET)
4333 {
4334 DWARFCompileUnit *spec_cu = cu;
4335 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4336 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4337 if (spec_die_decl_ctx_die)
4338 return spec_die_decl_ctx_die;
4339 }
4340
4341 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4342 if (die_offset != DW_INVALID_OFFSET)
4343 {
4344 DWARFCompileUnit *abs_cu = cu;
4345 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4346 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4347 if (abs_die_decl_ctx_die)
4348 return abs_die_decl_ctx_die;
4349 }
4350
4351 die = die->GetParent();
4352 }
4353 }
4354 return NULL;
4355}
4356
4357
Greg Clayton901c5ca2011-12-03 04:40:03 +00004358Symbol *
4359SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4360{
4361 Symbol *objc_class_symbol = NULL;
4362 if (m_obj_file)
4363 {
4364 Symtab *symtab = m_obj_file->GetSymtab();
4365 if (symtab)
4366 {
4367 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4368 eSymbolTypeObjCClass,
4369 Symtab::eDebugNo,
4370 Symtab::eVisibilityAny);
4371 }
4372 }
4373 return objc_class_symbol;
4374}
4375
Greg Claytonc7f03b62012-01-12 04:33:28 +00004376// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4377// then we can end up looking through all class types for a complete type and never find
4378// the full definition. We need to know if this attribute is supported, so we determine
4379// this here and cache th result. We also need to worry about the debug map DWARF file
4380// if we are doing darwin DWARF in .o file debugging.
4381bool
4382SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4383{
4384 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4385 {
4386 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4387 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4388 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4389 else
4390 {
4391 DWARFDebugInfo* debug_info = DebugInfo();
4392 const uint32_t num_compile_units = GetNumCompileUnits();
4393 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4394 {
Greg Clayton53eb1c22012-04-02 22:59:12 +00004395 DWARFCompileUnit* dwarf_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4396 if (dwarf_cu != cu && dwarf_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004397 {
4398 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4399 break;
4400 }
4401 }
4402 }
Greg Clayton1f746072012-08-29 21:13:06 +00004403 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && GetDebugMapSymfile ())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004404 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4405 }
4406 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4407}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004408
4409// This function can be used when a DIE is found that is a forward declaration
4410// DIE and we want to try and find a type that has the complete definition.
4411TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004412SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4413 const ConstString &type_name,
4414 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004415{
4416
4417 TypeSP type_sp;
4418
Greg Claytonc7f03b62012-01-12 04:33:28 +00004419 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004420 return type_sp;
4421
4422 DIEArray die_offsets;
4423
4424 if (m_using_apple_tables)
4425 {
4426 if (m_apple_types_ap.get())
4427 {
4428 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004429 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004430 }
4431 }
4432 else
4433 {
4434 if (!m_indexed)
4435 Index ();
4436
4437 m_type_index.Find (type_name, die_offsets);
4438 }
4439
Greg Clayton901c5ca2011-12-03 04:40:03 +00004440 const size_t num_matches = die_offsets.size();
4441
Greg Clayton901c5ca2011-12-03 04:40:03 +00004442 DWARFCompileUnit* type_cu = NULL;
4443 const DWARFDebugInfoEntry* type_die = NULL;
4444 if (num_matches)
4445 {
4446 DWARFDebugInfo* debug_info = DebugInfo();
4447 for (size_t i=0; i<num_matches; ++i)
4448 {
4449 const dw_offset_t die_offset = die_offsets[i];
4450 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4451
4452 if (type_die)
4453 {
4454 bool try_resolving_type = false;
4455
4456 // Don't try and resolve the DIE we are looking for with the DIE itself!
4457 if (type_die != die)
4458 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004459 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004460 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004461 case DW_TAG_class_type:
4462 case DW_TAG_structure_type:
4463 try_resolving_type = true;
4464 break;
4465 default:
4466 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004467 }
4468 }
4469
4470 if (try_resolving_type)
4471 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004472 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004473 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004474
4475 if (try_resolving_type)
4476 {
4477 Type *resolved_type = ResolveType (type_cu, type_die, false);
4478 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4479 {
4480 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4481 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004482 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton901c5ca2011-12-03 04:40:03 +00004483 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4484 MakeUserID(type_die->GetOffset()),
4485 MakeUserID(type_cu->GetOffset()));
4486
Greg Claytonc7f03b62012-01-12 04:33:28 +00004487 if (die)
4488 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004489 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004490 break;
4491 }
4492 }
4493 }
4494 }
4495 else
4496 {
4497 if (m_using_apple_tables)
4498 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004499 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4500 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004501 }
4502 }
4503
4504 }
4505 }
4506 return type_sp;
4507}
4508
Greg Claytona8022fa2012-04-24 21:22:41 +00004509
Greg Clayton80c26302012-02-05 06:12:47 +00004510//----------------------------------------------------------------------
4511// This function helps to ensure that the declaration contexts match for
4512// two different DIEs. Often times debug information will refer to a
4513// forward declaration of a type (the equivalent of "struct my_struct;".
4514// There will often be a declaration of that type elsewhere that has the
4515// full definition. When we go looking for the full type "my_struct", we
4516// will find one or more matches in the accelerator tables and we will
4517// then need to make sure the type was in the same declaration context
4518// as the original DIE. This function can efficiently compare two DIEs
4519// and will return true when the declaration context matches, and false
4520// when they don't.
4521//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004522bool
4523SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4524 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4525{
Greg Claytona8022fa2012-04-24 21:22:41 +00004526 if (die1 == die2)
4527 return true;
4528
4529#if defined (LLDB_CONFIGURATION_DEBUG)
4530 // You can't and shouldn't call this function with a compile unit from
4531 // two different SymbolFileDWARF instances.
4532 assert (DebugInfo()->ContainsCompileUnit (cu1));
4533 assert (DebugInfo()->ContainsCompileUnit (cu2));
4534#endif
4535
Greg Clayton890ff562012-02-02 05:48:16 +00004536 DWARFDIECollection decl_ctx_1;
4537 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004538 //The declaration DIE stack is a stack of the declaration context
4539 // DIEs all the way back to the compile unit. If a type "T" is
4540 // declared inside a class "B", and class "B" is declared inside
4541 // a class "A" and class "A" is in a namespace "lldb", and the
4542 // namespace is in a compile unit, there will be a stack of DIEs:
4543 //
4544 // [0] DW_TAG_class_type for "B"
4545 // [1] DW_TAG_class_type for "A"
4546 // [2] DW_TAG_namespace for "lldb"
4547 // [3] DW_TAG_compile_unit for the source file.
4548 //
4549 // We grab both contexts and make sure that everything matches
4550 // all the way back to the compiler unit.
4551
4552 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004553 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004554 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004555 // Make sure the context arrays have the same size, otherwise
4556 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004557 const size_t count1 = decl_ctx_1.Size();
4558 const size_t count2 = decl_ctx_2.Size();
4559 if (count1 != count2)
4560 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004561
4562 // Make sure the DW_TAG values match all the way back up the the
4563 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004564 const DWARFDebugInfoEntry *decl_ctx_die1;
4565 const DWARFDebugInfoEntry *decl_ctx_die2;
4566 size_t i;
4567 for (i=0; i<count1; i++)
4568 {
4569 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4570 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4571 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4572 return false;
4573 }
Greg Clayton890ff562012-02-02 05:48:16 +00004574#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004575
4576 // Make sure the top item in the decl context die array is always
4577 // DW_TAG_compile_unit. If it isn't then something went wrong in
4578 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004579 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004580
Greg Clayton890ff562012-02-02 05:48:16 +00004581#endif
4582 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004583 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004584 for (i=0; i<count1 - 1; i++)
4585 {
4586 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4587 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4588 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004589 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004590 // If the string was from a DW_FORM_strp, then the pointer will often
4591 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004592 if (name1 == name2)
4593 continue;
4594
4595 // Name pointers are not equal, so only compare the strings
4596 // if both are not NULL.
4597 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004598 {
Greg Clayton5569e642012-02-06 01:44:54 +00004599 // If the strings don't compare, we are done...
4600 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004601 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004602 }
4603 else
4604 {
4605 // One name was NULL while the other wasn't
4606 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004607 }
4608 }
Greg Clayton80c26302012-02-05 06:12:47 +00004609 // We made it through all of the checks and the declaration contexts
4610 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004611 return true;
4612}
Greg Clayton220a0072011-12-09 08:48:30 +00004613
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004614// This function can be used when a DIE is found that is a forward declaration
4615// DIE and we want to try and find a type that has the complete definition.
Greg Claytona8022fa2012-04-24 21:22:41 +00004616// "cu" and "die" must be from this SymbolFileDWARF
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004617TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004618SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
Greg Clayton7f995132011-10-04 22:41:51 +00004619 const DWARFDebugInfoEntry *die,
4620 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004621{
4622 TypeSP type_sp;
4623
Greg Claytona8022fa2012-04-24 21:22:41 +00004624#if defined (LLDB_CONFIGURATION_DEBUG)
4625 // You can't and shouldn't call this function with a compile unit from
4626 // another SymbolFileDWARF instance.
4627 assert (DebugInfo()->ContainsCompileUnit (cu));
4628#endif
4629
Greg Clayton1a65ae12011-01-25 23:55:37 +00004630 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004631 return type_sp;
4632
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004633 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4634 if (log)
4635 {
4636 std::string qualified_name;
4637 die->GetQualifiedName(this, cu, qualified_name);
4638 GetObjectFile()->GetModule()->LogMessage (log.get(),
4639 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x (%s), name='%s')",
4640 die->GetOffset(),
4641 qualified_name.c_str(),
4642 type_name.GetCString());
4643 }
4644
Greg Clayton7f995132011-10-04 22:41:51 +00004645 DIEArray die_offsets;
4646
Greg Clayton97fbc342011-10-20 22:30:33 +00004647 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004648 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004649 if (m_apple_types_ap.get())
4650 {
Greg Claytond1767f02011-12-08 02:13:16 +00004651 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4652 {
Greg Claytonae920b62012-01-06 00:17:16 +00004653 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004654 }
4655 else
4656 {
4657 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4658 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004659 }
Greg Clayton7f995132011-10-04 22:41:51 +00004660 }
4661 else
4662 {
4663 if (!m_indexed)
4664 Index ();
4665
4666 m_type_index.Find (type_name, die_offsets);
4667 }
Greg Clayton7f995132011-10-04 22:41:51 +00004668
4669 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004670
Greg Clayton934cb052011-12-03 00:27:05 +00004671 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004672
4673 DWARFCompileUnit* type_cu = NULL;
4674 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004675 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004676 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004677 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004678 for (size_t i=0; i<num_matches; ++i)
4679 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004680 const dw_offset_t die_offset = die_offsets[i];
4681 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004682
Greg Clayton95d87902011-11-11 03:16:25 +00004683 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004684 {
Greg Clayton934cb052011-12-03 00:27:05 +00004685 bool try_resolving_type = false;
4686
4687 // Don't try and resolve the DIE we are looking for with the DIE itself!
4688 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004689 {
Greg Clayton934cb052011-12-03 00:27:05 +00004690 const dw_tag_t type_die_tag = type_die->Tag();
4691 // Make sure the tags match
4692 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004693 {
Greg Clayton934cb052011-12-03 00:27:05 +00004694 // The tags match, lets try resolving this type
4695 try_resolving_type = true;
4696 }
4697 else
4698 {
4699 // The tags don't match, but we need to watch our for a
4700 // forward declaration for a struct and ("struct foo")
4701 // ends up being a class ("class foo { ... };") or
4702 // vice versa.
4703 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004704 {
Greg Clayton934cb052011-12-03 00:27:05 +00004705 case DW_TAG_class_type:
4706 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4707 try_resolving_type = (die_tag == DW_TAG_structure_type);
4708 break;
4709 case DW_TAG_structure_type:
4710 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4711 try_resolving_type = (die_tag == DW_TAG_class_type);
4712 break;
4713 default:
4714 // Tags don't match, don't event try to resolve
4715 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004716 break;
4717 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004718 }
4719 }
Greg Clayton934cb052011-12-03 00:27:05 +00004720
4721 if (try_resolving_type)
4722 {
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004723 if (log)
4724 {
4725 std::string qualified_name;
4726 type_die->GetQualifiedName(this, cu, qualified_name);
4727 GetObjectFile()->GetModule()->LogMessage (log.get(),
4728 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') trying die=0x%8.8x (%s)",
4729 die->GetOffset(),
4730 type_name.GetCString(),
4731 type_die->GetOffset(),
4732 qualified_name.c_str());
4733 }
4734
Greg Clayton890ff562012-02-02 05:48:16 +00004735 // Make sure the decl contexts match all the way up
4736 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004737 {
Greg Clayton890ff562012-02-02 05:48:16 +00004738 Type *resolved_type = ResolveType (type_cu, type_die, false);
4739 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4740 {
4741 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4742 MakeUserID(die->GetOffset()),
Greg Clayton53eb1c22012-04-02 22:59:12 +00004743 MakeUserID(dwarf_cu->GetOffset()),
Greg Clayton890ff562012-02-02 05:48:16 +00004744 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4745 MakeUserID(type_die->GetOffset()),
4746 MakeUserID(type_cu->GetOffset()));
4747
4748 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004749 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004750 break;
4751 }
Greg Clayton934cb052011-12-03 00:27:05 +00004752 }
4753 }
Greg Clayton9bbddbf2012-04-20 20:35:47 +00004754 else
4755 {
4756 if (log)
4757 {
4758 std::string qualified_name;
4759 type_die->GetQualifiedName(this, cu, qualified_name);
4760 GetObjectFile()->GetModule()->LogMessage (log.get(),
4761 "SymbolFileDWARF::FindDefinitionTypeForDIE(die=0x%8.8x, name='%s') ignoring die=0x%8.8x (%s)",
4762 die->GetOffset(),
4763 type_name.GetCString(),
4764 type_die->GetOffset(),
4765 qualified_name.c_str());
4766 }
4767 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004768 }
Greg Clayton95d87902011-11-11 03:16:25 +00004769 else
4770 {
4771 if (m_using_apple_tables)
4772 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004773 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4774 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004775 }
4776 }
4777
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004778 }
4779 }
4780 return type_sp;
4781}
4782
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004783TypeSP
Greg Claytona8022fa2012-04-24 21:22:41 +00004784SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext (const DWARFDeclContext &dwarf_decl_ctx)
4785{
4786 TypeSP type_sp;
4787
4788 const uint32_t dwarf_decl_ctx_count = dwarf_decl_ctx.GetSize();
4789 if (dwarf_decl_ctx_count > 0)
4790 {
4791 const ConstString type_name(dwarf_decl_ctx[0].name);
4792 const dw_tag_t tag = dwarf_decl_ctx[0].tag;
4793
4794 if (type_name)
4795 {
4796 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_TYPE_COMPLETION|DWARF_LOG_LOOKUPS));
4797 if (log)
4798 {
4799 GetObjectFile()->GetModule()->LogMessage (log.get(),
4800 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s')",
4801 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4802 dwarf_decl_ctx.GetQualifiedName());
4803 }
4804
4805 DIEArray die_offsets;
4806
4807 if (m_using_apple_tables)
4808 {
4809 if (m_apple_types_ap.get())
4810 {
4811 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4812 {
4813 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), tag, die_offsets);
4814 }
4815 else
4816 {
4817 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4818 }
4819 }
4820 }
4821 else
4822 {
4823 if (!m_indexed)
4824 Index ();
4825
4826 m_type_index.Find (type_name, die_offsets);
4827 }
4828
4829 const size_t num_matches = die_offsets.size();
4830
4831
4832 DWARFCompileUnit* type_cu = NULL;
4833 const DWARFDebugInfoEntry* type_die = NULL;
4834 if (num_matches)
4835 {
4836 DWARFDebugInfo* debug_info = DebugInfo();
4837 for (size_t i=0; i<num_matches; ++i)
4838 {
4839 const dw_offset_t die_offset = die_offsets[i];
4840 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4841
4842 if (type_die)
4843 {
4844 bool try_resolving_type = false;
4845
4846 // Don't try and resolve the DIE we are looking for with the DIE itself!
4847 const dw_tag_t type_tag = type_die->Tag();
4848 // Make sure the tags match
4849 if (type_tag == tag)
4850 {
4851 // The tags match, lets try resolving this type
4852 try_resolving_type = true;
4853 }
4854 else
4855 {
4856 // The tags don't match, but we need to watch our for a
4857 // forward declaration for a struct and ("struct foo")
4858 // ends up being a class ("class foo { ... };") or
4859 // vice versa.
4860 switch (type_tag)
4861 {
4862 case DW_TAG_class_type:
4863 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4864 try_resolving_type = (tag == DW_TAG_structure_type);
4865 break;
4866 case DW_TAG_structure_type:
4867 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4868 try_resolving_type = (tag == DW_TAG_class_type);
4869 break;
4870 default:
4871 // Tags don't match, don't event try to resolve
4872 // using this type whose name matches....
4873 break;
4874 }
4875 }
4876
4877 if (try_resolving_type)
4878 {
4879 DWARFDeclContext type_dwarf_decl_ctx;
4880 type_die->GetDWARFDeclContext (this, type_cu, type_dwarf_decl_ctx);
4881
4882 if (log)
4883 {
4884 GetObjectFile()->GetModule()->LogMessage (log.get(),
4885 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') trying die=0x%8.8x (%s)",
4886 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4887 dwarf_decl_ctx.GetQualifiedName(),
4888 type_die->GetOffset(),
4889 type_dwarf_decl_ctx.GetQualifiedName());
4890 }
4891
4892 // Make sure the decl contexts match all the way up
4893 if (dwarf_decl_ctx == type_dwarf_decl_ctx)
4894 {
4895 Type *resolved_type = ResolveType (type_cu, type_die, false);
4896 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4897 {
4898 type_sp = resolved_type->shared_from_this();
4899 break;
4900 }
4901 }
4902 }
4903 else
4904 {
4905 if (log)
4906 {
4907 std::string qualified_name;
4908 type_die->GetQualifiedName(this, type_cu, qualified_name);
4909 GetObjectFile()->GetModule()->LogMessage (log.get(),
4910 "SymbolFileDWARF::FindDefinitionTypeForDWARFDeclContext(tag=%s, qualified-name='%s') ignoring die=0x%8.8x (%s)",
4911 DW_TAG_value_to_name(dwarf_decl_ctx[0].tag),
4912 dwarf_decl_ctx.GetQualifiedName(),
4913 type_die->GetOffset(),
4914 qualified_name.c_str());
4915 }
4916 }
4917 }
4918 else
4919 {
4920 if (m_using_apple_tables)
4921 {
4922 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4923 die_offset, type_name.GetCString());
4924 }
4925 }
4926
4927 }
4928 }
4929 }
4930 }
4931 return type_sp;
4932}
4933
Greg Clayton4116e932012-05-15 02:33:01 +00004934bool
4935SymbolFileDWARF::CopyUniqueClassMethodTypes (Type *class_type,
4936 DWARFCompileUnit* src_cu,
4937 const DWARFDebugInfoEntry *src_class_die,
4938 DWARFCompileUnit* dst_cu,
4939 const DWARFDebugInfoEntry *dst_class_die)
4940{
4941 if (!class_type || !src_cu || !src_class_die || !dst_cu || !dst_class_die)
4942 return false;
4943 if (src_class_die->Tag() != dst_class_die->Tag())
4944 return false;
4945
4946 // We need to complete the class type so we can get all of the method types
4947 // parsed so we can then unique those types to their equivalent counterparts
4948 // in "dst_cu" and "dst_class_die"
4949 class_type->GetClangFullType();
4950
4951 const DWARFDebugInfoEntry *src_die;
4952 const DWARFDebugInfoEntry *dst_die;
4953 UniqueCStringMap<const DWARFDebugInfoEntry *> src_name_to_die;
4954 UniqueCStringMap<const DWARFDebugInfoEntry *> dst_name_to_die;
4955 for (src_die = src_class_die->GetFirstChild(); src_die != NULL; src_die = src_die->GetSibling())
4956 {
4957 if (src_die->Tag() == DW_TAG_subprogram)
4958 {
4959 const char *src_name = src_die->GetMangledName (this, src_cu);
4960 if (src_name)
4961 src_name_to_die.Append(ConstString(src_name).GetCString(), src_die);
4962 }
4963 }
4964 for (dst_die = dst_class_die->GetFirstChild(); dst_die != NULL; dst_die = dst_die->GetSibling())
4965 {
4966 if (dst_die->Tag() == DW_TAG_subprogram)
4967 {
4968 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
4969 if (dst_name)
4970 dst_name_to_die.Append(ConstString(dst_name).GetCString(), dst_die);
4971 }
4972 }
4973 const uint32_t src_size = src_name_to_die.GetSize ();
4974 const uint32_t dst_size = dst_name_to_die.GetSize ();
4975 LogSP log (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
4976
4977 if (src_size && dst_size)
4978 {
Sean Callanan210b8152012-07-07 00:29:33 +00004979 if (src_size != dst_size)
4980 {
4981 if (log)
4982 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
4983 src_class_die->GetOffset(),
4984 dst_class_die->GetOffset(),
4985 src_size,
4986 dst_size);
4987
4988 return false;
4989 }
4990
Greg Clayton4116e932012-05-15 02:33:01 +00004991 uint32_t idx;
4992 for (idx = 0; idx < src_size; ++idx)
4993 {
4994 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
4995 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
4996
4997 if (src_die->Tag() != dst_die->Tag())
4998 {
4999 if (log)
5000 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
5001 src_class_die->GetOffset(),
5002 dst_class_die->GetOffset(),
5003 src_die->GetOffset(),
5004 DW_TAG_value_to_name(src_die->Tag()),
5005 dst_die->GetOffset(),
5006 DW_TAG_value_to_name(src_die->Tag()));
5007 return false;
5008 }
5009
5010 const char *src_name = src_die->GetMangledName (this, src_cu);
5011 const char *dst_name = dst_die->GetMangledName (this, dst_cu);
5012
5013 // Make sure the names match
5014 if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
5015 continue;
5016
5017 if (log)
5018 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
5019 src_class_die->GetOffset(),
5020 dst_class_die->GetOffset(),
5021 src_die->GetOffset(),
5022 src_name,
5023 dst_die->GetOffset(),
5024 dst_name);
5025
5026 return false;
5027 }
5028
5029 for (idx = 0; idx < src_size; ++idx)
5030 {
5031 src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
5032 dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
5033
5034 clang::DeclContext *src_decl_ctx = m_die_to_decl_ctx[src_die];
5035 if (src_decl_ctx)
5036 {
5037 if (log)
5038 log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x\n", src_decl_ctx, src_die->GetOffset(), dst_die->GetOffset());
5039 LinkDeclContextToDIE (src_decl_ctx, dst_die);
5040 }
5041 else
5042 {
5043 if (log)
5044 log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found\n", src_die->GetOffset(), dst_die->GetOffset());
5045 }
5046
5047 Type *src_child_type = m_die_to_type[src_die];
5048 if (src_child_type)
5049 {
5050 if (log)
5051 log->Printf ("uniquing type %p (uid=0x%llx) from 0x%8.8x for 0x%8.8x\n", src_child_type, src_child_type->GetID(), src_die->GetOffset(), dst_die->GetOffset());
5052 m_die_to_type[dst_die] = src_child_type;
5053 }
5054 else
5055 {
5056 if (log)
5057 log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found\n", src_die->GetOffset(), dst_die->GetOffset());
5058 }
5059 }
5060 return true;
5061 }
5062 else
5063 {
5064 if (log)
5065 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x has %u methods and 0x%8.8x has %u",
5066 src_class_die->GetOffset(),
5067 dst_class_die->GetOffset(),
5068 src_die->GetOffset(),
5069 src_size,
5070 dst_die->GetOffset(),
5071 dst_size);
5072 }
5073 return false;
5074}
Greg Claytona8022fa2012-04-24 21:22:41 +00005075
5076TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00005077SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005078{
5079 TypeSP type_sp;
5080
Greg Clayton1be10fc2010-09-29 01:12:09 +00005081 if (type_is_new_ptr)
5082 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00005083
5084#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5085 static DIEStack g_die_stack;
5086 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
5087#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00005088
Sean Callananc7fbf732010-08-06 00:32:49 +00005089 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005090 if (die != NULL)
5091 {
Greg Clayton21f2a492011-10-06 00:09:08 +00005092 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00005093 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00005094 {
5095 const DWARFDebugInfoEntry *context_die;
5096 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
5097
Greg Clayton1fba87112012-02-09 20:03:49 +00005098 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
Sean Callanan5b26f272012-02-04 08:49:35 +00005099 die->GetOffset(),
5100 context,
5101 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00005102 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00005103 die->GetName(this, dwarf_cu));
5104
5105#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
5106 scoped_die_logger.Push (dwarf_cu, die);
5107 g_die_stack.LogDIEs(log.get(), this);
5108#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00005109 }
Greg Clayton3bffb082011-12-10 02:15:28 +00005110//
5111// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
5112// if (log && dwarf_cu)
5113// {
5114// StreamString s;
5115// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005116// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00005117//
5118// }
Jim Ingham16746d12011-08-25 23:21:43 +00005119
Greg Clayton594e5ed2010-09-27 21:07:38 +00005120 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005121 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00005122 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005123 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005124 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00005125 if (type_is_new_ptr)
5126 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005127
Greg Clayton594e5ed2010-09-27 21:07:38 +00005128 const dw_tag_t tag = die->Tag();
5129
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005130 bool is_forward_declaration = false;
5131 DWARFDebugInfoEntry::Attributes attributes;
5132 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00005133 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00005134 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
5135 size_t byte_size = 0;
5136 Declaration decl;
5137
Greg Clayton4957bf62010-09-30 21:49:03 +00005138 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005139 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005140
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005141 dw_attr_t attr;
5142
5143 switch (tag)
5144 {
5145 case DW_TAG_base_type:
5146 case DW_TAG_pointer_type:
5147 case DW_TAG_reference_type:
Sean Callanance8af862012-05-21 23:31:51 +00005148 case DW_TAG_rvalue_reference_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005149 case DW_TAG_typedef:
5150 case DW_TAG_const_type:
5151 case DW_TAG_restrict_type:
5152 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00005153 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005154 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005155 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005156 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005157
Greg Claytond88d7592010-09-15 08:33:30 +00005158 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005159 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005160 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
5161
5162 if (num_attributes > 0)
5163 {
5164 uint32_t i;
5165 for (i=0; i<num_attributes; ++i)
5166 {
5167 attr = attributes.AttributeAtIndex(i);
5168 DWARFFormValue form_value;
5169 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5170 {
5171 switch (attr)
5172 {
5173 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5174 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5175 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5176 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00005177
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005178 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00005179 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
5180 // include the "&"...
5181 if (tag == DW_TAG_reference_type)
5182 {
5183 if (strchr (type_name_cstr, '&') == NULL)
5184 type_name_cstr = NULL;
5185 }
5186 if (type_name_cstr)
5187 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005188 break;
Greg Clayton23f59502012-07-17 03:23:13 +00005189 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005190 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
5191 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
5192 default:
5193 case DW_AT_sibling:
5194 break;
5195 }
5196 }
5197 }
5198 }
5199
Greg Clayton81c22f62011-10-19 18:09:39 +00005200 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\") type => 0x%8.8x\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
Greg Claytonc93237c2010-10-01 20:48:32 +00005201
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005202 switch (tag)
5203 {
5204 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00005205 break;
5206
Greg Claytond4436412011-10-28 21:00:00 +00005207 case DW_TAG_unspecified_type:
5208 if (strcmp(type_name_cstr, "nullptr_t") == 0)
5209 {
5210 resolve_state = Type::eResolveStateFull;
5211 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
5212 break;
5213 }
5214 // Fall through to base type below in case we can handle the type there...
5215
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005216 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00005217 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005218 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
5219 encoding,
5220 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005221 break;
5222
Sean Callanance8af862012-05-21 23:31:51 +00005223 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
5224 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
5225 case DW_TAG_rvalue_reference_type: encoding_data_type = Type::eEncodingIsRValueReferenceUID; break;
5226 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
5227 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
5228 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
5229 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005230 }
5231
Greg Claytonc7f03b62012-01-12 04:33:28 +00005232 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005233 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005234 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
5235 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005236 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005237 static ConstString g_objc_type_name_id("id");
5238 static ConstString g_objc_type_name_Class("Class");
5239 static ConstString g_objc_type_name_selector("SEL");
5240
5241 if (type_name_const_str == g_objc_type_name_id)
5242 {
5243 if (log)
5244 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
5245 die->GetOffset(),
5246 DW_TAG_value_to_name(die->Tag()),
5247 die->GetName(this, dwarf_cu));
5248 clang_type = ast.GetBuiltInType_objc_id();
5249 encoding_data_type = Type::eEncodingIsUID;
5250 encoding_uid = LLDB_INVALID_UID;
5251 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00005252
Greg Claytonc7f03b62012-01-12 04:33:28 +00005253 }
5254 else if (type_name_const_str == g_objc_type_name_Class)
5255 {
5256 if (log)
5257 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
5258 die->GetOffset(),
5259 DW_TAG_value_to_name(die->Tag()),
5260 die->GetName(this, dwarf_cu));
5261 clang_type = ast.GetBuiltInType_objc_Class();
5262 encoding_data_type = Type::eEncodingIsUID;
5263 encoding_uid = LLDB_INVALID_UID;
5264 resolve_state = Type::eResolveStateFull;
5265 }
5266 else if (type_name_const_str == g_objc_type_name_selector)
5267 {
5268 if (log)
5269 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
5270 die->GetOffset(),
5271 DW_TAG_value_to_name(die->Tag()),
5272 die->GetName(this, dwarf_cu));
5273 clang_type = ast.GetBuiltInType_objc_selector();
5274 encoding_data_type = Type::eEncodingIsUID;
5275 encoding_uid = LLDB_INVALID_UID;
5276 resolve_state = Type::eResolveStateFull;
5277 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00005278 }
5279 }
5280
Greg Clayton81c22f62011-10-19 18:09:39 +00005281 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005282 this,
5283 type_name_const_str,
5284 byte_size,
5285 NULL,
5286 encoding_uid,
5287 encoding_data_type,
5288 &decl,
5289 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005290 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005291
Greg Clayton594e5ed2010-09-27 21:07:38 +00005292 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005293
5294// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
5295// if (encoding_type != NULL)
5296// {
5297// if (encoding_type != DIE_IS_BEING_PARSED)
5298// type_sp->SetEncodingType(encoding_type);
5299// else
5300// m_indirect_fixups.push_back(type_sp.get());
5301// }
5302 }
5303 break;
5304
5305 case DW_TAG_structure_type:
5306 case DW_TAG_union_type:
5307 case DW_TAG_class_type:
5308 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005309 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005310 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Greg Clayton23f59502012-07-17 03:23:13 +00005311 bool byte_size_valid = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005312
Greg Clayton9e409562010-07-28 02:04:09 +00005313 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00005314 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005315 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00005316 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005317 if (num_attributes > 0)
5318 {
5319 uint32_t i;
5320 for (i=0; i<num_attributes; ++i)
5321 {
5322 attr = attributes.AttributeAtIndex(i);
5323 DWARFFormValue form_value;
5324 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5325 {
5326 switch (attr)
5327 {
Greg Clayton9e409562010-07-28 02:04:09 +00005328 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00005329 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
5330 {
5331 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
5332 // point to the compile unit file, so we clear this invalid value
5333 // so that we can still unique types efficiently.
5334 decl.SetFile(FileSpec ("<invalid>", false));
5335 }
5336 else
5337 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00005338 break;
5339
5340 case DW_AT_decl_line:
5341 decl.SetLine(form_value.Unsigned());
5342 break;
5343
5344 case DW_AT_decl_column:
5345 decl.SetColumn(form_value.Unsigned());
5346 break;
5347
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005348 case DW_AT_name:
5349 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005350 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005351 break;
Greg Clayton9e409562010-07-28 02:04:09 +00005352
5353 case DW_AT_byte_size:
5354 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00005355 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00005356 break;
5357
5358 case DW_AT_accessibility:
5359 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
5360 break;
5361
5362 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00005363 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00005364 break;
5365
5366 case DW_AT_APPLE_runtime_class:
5367 class_language = (LanguageType)form_value.Signed();
5368 break;
5369
Greg Clayton18774842011-11-29 23:40:34 +00005370 case DW_AT_APPLE_objc_complete_type:
5371 is_complete_objc_class = form_value.Signed();
5372 break;
5373
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005374 case DW_AT_allocated:
5375 case DW_AT_associated:
5376 case DW_AT_data_location:
5377 case DW_AT_description:
5378 case DW_AT_start_scope:
5379 case DW_AT_visibility:
5380 default:
5381 case DW_AT_sibling:
5382 break;
5383 }
5384 }
5385 }
5386 }
5387
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005388 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00005389
Greg Clayton123edca2012-03-02 00:07:15 +00005390 // Only try and unique the type if it has a name.
5391 if (type_name_const_str &&
5392 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00005393 this,
5394 dwarf_cu,
5395 die,
5396 decl,
5397 byte_size_valid ? byte_size : -1,
5398 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005399 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005400 // We have already parsed this type or from another
5401 // compile unit. GCC loves to use the "one definition
5402 // rule" which can result in multiple definitions
5403 // of the same class over and over in each compile
5404 // unit.
5405 type_sp = unique_ast_entry.m_type_sp;
5406 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00005407 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00005408 m_die_to_type[die] = type_sp.get();
5409 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005410 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005411 }
5412
Greg Clayton81c22f62011-10-19 18:09:39 +00005413 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005414
5415 int tag_decl_kind = -1;
5416 AccessType default_accessibility = eAccessNone;
5417 if (tag == DW_TAG_structure_type)
5418 {
5419 tag_decl_kind = clang::TTK_Struct;
5420 default_accessibility = eAccessPublic;
5421 }
5422 else if (tag == DW_TAG_union_type)
5423 {
5424 tag_decl_kind = clang::TTK_Union;
5425 default_accessibility = eAccessPublic;
5426 }
5427 else if (tag == DW_TAG_class_type)
5428 {
5429 tag_decl_kind = clang::TTK_Class;
5430 default_accessibility = eAccessPrivate;
5431 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00005432
5433 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
5434 die->HasChildren() == false &&
5435 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
5436 {
5437 // Work around an issue with clang at the moment where
5438 // forward declarations for objective C classes are emitted
5439 // as:
5440 // DW_TAG_structure_type [2]
5441 // DW_AT_name( "ForwardObjcClass" )
5442 // DW_AT_byte_size( 0x00 )
5443 // DW_AT_decl_file( "..." )
5444 // DW_AT_decl_line( 1 )
5445 //
5446 // Note that there is no DW_AT_declaration and there are
5447 // no children, and the byte size is zero.
5448 is_forward_declaration = true;
5449 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005450
Sean Callanan7457f8d2012-04-25 01:03:57 +00005451 if (class_language == eLanguageTypeObjC ||
5452 class_language == eLanguageTypeObjC_plus_plus)
Greg Clayton18774842011-11-29 23:40:34 +00005453 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005454 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00005455 {
5456 // We have a valid eSymbolTypeObjCClass class symbol whose
5457 // name matches the current objective C class that we
5458 // are trying to find and this DIE isn't the complete
5459 // definition (we checked is_complete_objc_class above and
5460 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00005461 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005462
Greg Clayton1f746072012-08-29 21:13:06 +00005463 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton901c5ca2011-12-03 04:40:03 +00005464 {
5465 // We weren't able to find a full declaration in
5466 // this DWARF, see if we have a declaration anywhere
5467 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00005468 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00005469 }
5470
5471 if (type_sp)
5472 {
5473 if (log)
5474 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005475 GetObjectFile()->GetModule()->LogMessage (log.get(),
5476 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
5477 this,
5478 die->GetOffset(),
5479 DW_TAG_value_to_name(tag),
5480 type_name_cstr,
5481 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00005482 }
5483
5484 // We found a real definition for this type elsewhere
5485 // so lets use it and cache the fact that we found
5486 // a complete type for this die
5487 m_die_to_type[die] = type_sp.get();
5488 return type_sp;
5489 }
5490 }
5491 }
5492
5493
5494 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005495 {
5496 // We have a forward declaration to a type and we need
5497 // to try and find a full declaration. We look in the
5498 // current type index just in case we have a forward
5499 // declaration followed by an actual declarations in the
5500 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00005501 if (log)
5502 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005503 GetObjectFile()->GetModule()->LogMessage (log.get(),
5504 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
5505 this,
5506 die->GetOffset(),
5507 DW_TAG_value_to_name(tag),
5508 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00005509 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005510
Greg Claytona8022fa2012-04-24 21:22:41 +00005511 DWARFDeclContext die_decl_ctx;
5512 die->GetDWARFDeclContext(this, dwarf_cu, die_decl_ctx);
5513
5514 //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
5515 type_sp = FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005516
Greg Clayton1f746072012-08-29 21:13:06 +00005517 if (!type_sp && GetDebugMapSymfile ())
Greg Clayton4272cc72011-02-02 02:24:04 +00005518 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005519 // We weren't able to find a full declaration in
5520 // this DWARF, see if we have a declaration anywhere
5521 // else...
Greg Claytona8022fa2012-04-24 21:22:41 +00005522 type_sp = m_debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
Greg Clayton4272cc72011-02-02 02:24:04 +00005523 }
5524
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005525 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00005526 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00005527 if (log)
5528 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005529 GetObjectFile()->GetModule()->LogMessage (log.get(),
5530 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
5531 this,
5532 die->GetOffset(),
5533 DW_TAG_value_to_name(tag),
5534 type_name_cstr,
5535 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00005536 }
5537
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005538 // We found a real definition for this type elsewhere
5539 // so lets use it and cache the fact that we found
5540 // a complete type for this die
5541 m_die_to_type[die] = type_sp.get();
5542 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00005543 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005544 }
5545 assert (tag_decl_kind != -1);
5546 bool clang_type_was_created = false;
5547 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5548 if (clang_type == NULL)
5549 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00005550 const DWARFDebugInfoEntry *decl_ctx_die;
5551
5552 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00005553 if (accessibility == eAccessNone && decl_ctx)
5554 {
5555 // Check the decl context that contains this class/struct/union.
5556 // If it is a class we must give it an accessability.
5557 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
5558 if (DeclKindIsCXXClass (containing_decl_kind))
5559 accessibility = default_accessibility;
5560 }
5561
Greg Claytonf0705c82011-10-22 03:33:13 +00005562 if (type_name_cstr && strchr (type_name_cstr, '<'))
5563 {
5564 ClangASTContext::TemplateParameterInfos template_param_infos;
5565 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
5566 {
5567 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00005568 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00005569 type_name_cstr,
5570 tag_decl_kind,
5571 template_param_infos);
5572
5573 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
5574 class_template_decl,
5575 tag_decl_kind,
5576 template_param_infos);
5577 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
5578 clang_type_was_created = true;
Sean Callanan60217122012-04-13 00:10:03 +00005579
Jim Ingham379397632012-10-27 02:54:13 +00005580 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)class_template_decl, MakeUserID(die->GetOffset()));
5581 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)class_specialization_decl, MakeUserID(die->GetOffset()));
Greg Claytonf0705c82011-10-22 03:33:13 +00005582 }
5583 }
5584
5585 if (!clang_type_was_created)
5586 {
5587 clang_type_was_created = true;
Jim Ingham379397632012-10-27 02:54:13 +00005588 ClangASTMetadata metadata;
5589 metadata.SetUserID(MakeUserID(die->GetOffset()));
Greg Clayton55561e92011-10-26 03:31:36 +00005590 clang_type = ast.CreateRecordType (decl_ctx,
5591 accessibility,
5592 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00005593 tag_decl_kind,
Sean Callanan60217122012-04-13 00:10:03 +00005594 class_language,
Jim Ingham379397632012-10-27 02:54:13 +00005595 &metadata);
Greg Claytonf0705c82011-10-22 03:33:13 +00005596 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005597 }
5598
5599 // Store a forward declaration to this class type in case any
5600 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00005601 // types for function prototypes.
5602 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00005603 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005604 this,
5605 type_name_const_str,
5606 byte_size,
5607 NULL,
5608 LLDB_INVALID_UID,
5609 Type::eEncodingIsUID,
5610 &decl,
5611 clang_type,
5612 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005613
5614 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005615
5616
5617 // Add our type to the unique type map so we don't
5618 // end up creating many copies of the same type over
5619 // and over in the ASTContext for our module
5620 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005621 unique_ast_entry.m_symfile = this;
5622 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005623 unique_ast_entry.m_die = die;
5624 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005625 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005626 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5627 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005628
Sean Callanan12014a02011-12-08 23:45:45 +00005629 if (!is_forward_declaration)
Greg Clayton219cf312012-03-30 00:51:13 +00005630 {
5631 // Always start the definition for a class type so that
5632 // if the class has child classes or types that require
5633 // the class to be created for use as their decl contexts
5634 // the class will be ready to accept these child definitions.
Sean Callanan12014a02011-12-08 23:45:45 +00005635 if (die->HasChildren() == false)
5636 {
5637 // No children for this struct/union/class, lets finish it
5638 ast.StartTagDeclarationDefinition (clang_type);
5639 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan433cd222012-10-19 01:37:25 +00005640
5641 if (tag == DW_TAG_structure_type) // this only applies in C
5642 {
5643 clang::QualType qual_type = clang::QualType::getFromOpaquePtr (clang_type);
5644 const clang::RecordType *record_type = qual_type->getAs<clang::RecordType> ();
5645
5646 if (record_type)
5647 {
5648 clang::RecordDecl *record_decl = record_type->getDecl();
5649
5650 if (record_decl)
5651 {
5652 LayoutInfo layout_info;
5653
5654 layout_info.alignment = 0;
5655 layout_info.bit_size = 0;
5656
5657 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
5658 }
5659 }
5660 }
Sean Callanan12014a02011-12-08 23:45:45 +00005661 }
5662 else if (clang_type_was_created)
5663 {
Greg Clayton219cf312012-03-30 00:51:13 +00005664 // Start the definition if the class is not objective C since
5665 // the underlying decls respond to isCompleteDefinition(). Objective
5666 // C decls dont' respond to isCompleteDefinition() so we can't
5667 // start the declaration definition right away. For C++ classs/union/structs
5668 // we want to start the definition in case the class is needed as the
5669 // declaration context for a contained class or type without the need
5670 // to complete that type..
5671
Sean Callanan7457f8d2012-04-25 01:03:57 +00005672 if (class_language != eLanguageTypeObjC &&
5673 class_language != eLanguageTypeObjC_plus_plus)
Greg Clayton219cf312012-03-30 00:51:13 +00005674 ast.StartTagDeclarationDefinition (clang_type);
5675
Sean Callanan12014a02011-12-08 23:45:45 +00005676 // Leave this as a forward declaration until we need
5677 // to know the details of the type. lldb_private::Type
5678 // will automatically call the SymbolFile virtual function
5679 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5680 // When the definition needs to be defined.
5681 m_forward_decl_die_to_clang_type[die] = clang_type;
5682 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5683 ClangASTContext::SetHasExternalStorage (clang_type, true);
5684 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005685 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005686
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005687 }
5688 break;
5689
5690 case DW_TAG_enumeration_type:
5691 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005692 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005693 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005695 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005696
Greg Claytond88d7592010-09-15 08:33:30 +00005697 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005698 if (num_attributes > 0)
5699 {
5700 uint32_t i;
5701
5702 for (i=0; i<num_attributes; ++i)
5703 {
5704 attr = attributes.AttributeAtIndex(i);
5705 DWARFFormValue form_value;
5706 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5707 {
5708 switch (attr)
5709 {
Greg Clayton7a345282010-11-09 23:46:37 +00005710 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5711 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5712 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005713 case DW_AT_name:
5714 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005715 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005716 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005717 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00005718 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
5719 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5720 case DW_AT_declaration: break; //is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005721 case DW_AT_allocated:
5722 case DW_AT_associated:
5723 case DW_AT_bit_stride:
5724 case DW_AT_byte_stride:
5725 case DW_AT_data_location:
5726 case DW_AT_description:
5727 case DW_AT_start_scope:
5728 case DW_AT_visibility:
5729 case DW_AT_specification:
5730 case DW_AT_abstract_origin:
5731 case DW_AT_sibling:
5732 break;
5733 }
5734 }
5735 }
5736
Greg Clayton81c22f62011-10-19 18:09:39 +00005737 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005738
Greg Clayton1be10fc2010-09-29 01:12:09 +00005739 clang_type_t enumerator_clang_type = NULL;
5740 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5741 if (clang_type == NULL)
5742 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005743 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5744 DW_ATE_signed,
5745 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005746 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005747 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005748 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005749 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005750 }
5751 else
5752 {
5753 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5754 assert (enumerator_clang_type != NULL);
5755 }
5756
Greg Claytona2721472011-06-25 00:44:06 +00005757 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5758
Greg Clayton81c22f62011-10-19 18:09:39 +00005759 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005760 this,
5761 type_name_const_str,
5762 byte_size,
5763 NULL,
5764 encoding_uid,
5765 Type::eEncodingIsUID,
5766 &decl,
5767 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005768 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005769
Greg Clayton6beaaa62011-01-17 03:46:26 +00005770 ast.StartTagDeclarationDefinition (clang_type);
5771 if (die->HasChildren())
5772 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005773 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5774 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005775 }
5776 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005777 }
5778 }
5779 break;
5780
Jim Inghamb0be4422010-08-12 01:20:14 +00005781 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005782 case DW_TAG_subprogram:
5783 case DW_TAG_subroutine_type:
5784 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005785 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005786 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005787
Greg Clayton23f59502012-07-17 03:23:13 +00005788 //const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005789 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005790 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005791 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005792 bool is_static = false;
5793 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005794 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005795 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005796 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5797 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Jim Ingham379397632012-10-27 02:54:13 +00005798 dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005799
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005800 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005801 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005802
5803
Greg Claytond88d7592010-09-15 08:33:30 +00005804 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005805 if (num_attributes > 0)
5806 {
5807 uint32_t i;
5808 for (i=0; i<num_attributes; ++i)
5809 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005810 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005811 DWARFFormValue form_value;
5812 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5813 {
5814 switch (attr)
5815 {
5816 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5817 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5818 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5819 case DW_AT_name:
5820 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005821 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005822 break;
5823
Greg Clayton23f59502012-07-17 03:23:13 +00005824 case DW_AT_MIPS_linkage_name: break; // mangled = form_value.AsCString(&get_debug_str_data()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005825 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005826 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton23f59502012-07-17 03:23:13 +00005827 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005828 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5829 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005830 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005831 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5832
Greg Claytonf51de672010-10-01 02:31:07 +00005833
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005834 case DW_AT_external:
5835 if (form_value.Unsigned())
5836 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005837 if (storage == clang::SC_None)
5838 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005839 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005840 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005841 }
5842 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005843
Greg Clayton72da3972011-08-16 18:40:23 +00005844 case DW_AT_specification:
5845 specification_die_offset = form_value.Reference(dwarf_cu);
5846 break;
5847
5848 case DW_AT_abstract_origin:
5849 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5850 break;
5851
Jim Ingham379397632012-10-27 02:54:13 +00005852 case DW_AT_object_pointer:
5853 object_pointer_die_offset = form_value.Reference(dwarf_cu);
5854 break;
5855
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005856 case DW_AT_allocated:
5857 case DW_AT_associated:
5858 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005859 case DW_AT_calling_convention:
5860 case DW_AT_data_location:
5861 case DW_AT_elemental:
5862 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005863 case DW_AT_frame_base:
5864 case DW_AT_high_pc:
5865 case DW_AT_low_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005866 case DW_AT_prototyped:
5867 case DW_AT_pure:
5868 case DW_AT_ranges:
5869 case DW_AT_recursive:
5870 case DW_AT_return_addr:
5871 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005872 case DW_AT_start_scope:
5873 case DW_AT_static_link:
5874 case DW_AT_trampoline:
5875 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005876 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005877 case DW_AT_description:
5878 case DW_AT_sibling:
5879 break;
5880 }
5881 }
5882 }
Greg Clayton24739922010-10-13 03:15:28 +00005883 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005884
Jim Ingham379397632012-10-27 02:54:13 +00005885 std::string object_pointer_name;
5886 if (object_pointer_die_offset != DW_INVALID_OFFSET)
5887 {
5888 // Get the name from the object pointer die
5889 StreamString s;
5890 if (DWARFDebugInfoEntry::GetName (this, dwarf_cu, object_pointer_die_offset, s))
5891 {
5892 object_pointer_name.assign(s.GetData());
5893 }
5894 }
5895
Greg Clayton81c22f62011-10-19 18:09:39 +00005896 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00005897
Greg Clayton24739922010-10-13 03:15:28 +00005898 clang_type_t return_clang_type = NULL;
5899 Type *func_type = NULL;
5900
5901 if (type_die_offset != DW_INVALID_OFFSET)
5902 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005903
Greg Clayton24739922010-10-13 03:15:28 +00005904 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005905 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005906 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005907 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005908
Greg Claytonf51de672010-10-01 02:31:07 +00005909
Greg Clayton24739922010-10-13 03:15:28 +00005910 std::vector<clang_type_t> function_param_types;
5911 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005912
Greg Clayton24739922010-10-13 03:15:28 +00005913 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005914
Greg Claytoncb5860a2011-10-13 23:49:28 +00005915 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5916 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005917 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5918
Greg Claytonf0705c82011-10-22 03:33:13 +00005919 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005920 // Start off static. This will be set to false in ParseChildParameters(...)
5921 // if we find a "this" paramters as the first parameter
5922 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005923 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005924 ClangASTContext::TemplateParameterInfos template_param_infos;
5925
Greg Clayton24739922010-10-13 03:15:28 +00005926 if (die->HasChildren())
5927 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005928 bool skip_artificial = true;
Jim Ingham379397632012-10-27 02:54:13 +00005929 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005930 containing_decl_ctx,
Jim Ingham379397632012-10-27 02:54:13 +00005931 dwarf_cu,
5932 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005933 skip_artificial,
5934 is_static,
Jim Ingham379397632012-10-27 02:54:13 +00005935 type_list,
5936 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005937 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005938 type_quals,
5939 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005940 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005941
Greg Clayton24739922010-10-13 03:15:28 +00005942 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005943 clang_type = ast.CreateFunctionType (return_clang_type,
Greg Clayton9cb25c42012-10-30 17:02:18 +00005944 function_param_types.data(),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005945 function_param_types.size(),
5946 is_variadic,
5947 type_quals);
5948
Greg Clayton24739922010-10-13 03:15:28 +00005949 if (type_name_cstr)
5950 {
5951 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005952 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005953 {
Greg Clayton7c810422012-01-18 23:40:49 +00005954 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005955 ConstString class_name_no_category;
5956 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005957 {
Greg Claytone42ae842012-01-19 03:24:53 +00005958 // Use the class name with no category if there is one
5959 if (class_name_no_category)
5960 class_name = class_name_no_category;
5961
Greg Clayton24739922010-10-13 03:15:28 +00005962 SymbolContext empty_sc;
5963 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005964 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005965 {
Greg Clayton24739922010-10-13 03:15:28 +00005966 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005967 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005968
5969 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005970 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005971 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5972 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5973 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005974 }
Greg Clayton24739922010-10-13 03:15:28 +00005975 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005976
Greg Clayton24739922010-10-13 03:15:28 +00005977 if (class_opaque_type)
5978 {
5979 // If accessibility isn't set to anything valid, assume public for
5980 // now...
5981 if (accessibility == eAccessNone)
5982 accessibility = eAccessPublic;
5983
Sean Callanan464a5b52012-10-04 22:06:29 +00005984 clang::ObjCMethodDecl *objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5985 type_name_cstr,
5986 clang_type,
5987 accessibility);
Greg Clayton24739922010-10-13 03:15:28 +00005988 type_handled = objc_method_decl != NULL;
Sean Callanan464a5b52012-10-04 22:06:29 +00005989 if (type_handled)
5990 {
5991 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Jim Ingham379397632012-10-27 02:54:13 +00005992 GetClangASTContext().SetMetadataAsUserID ((uintptr_t)objc_method_decl, MakeUserID(die->GetOffset()));
Sean Callanan464a5b52012-10-04 22:06:29 +00005993 }
Greg Clayton24739922010-10-13 03:15:28 +00005994 }
5995 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005996 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005997 {
5998 // Look at the parent of this DIE and see if is is
5999 // a class or struct and see if this is actually a
6000 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00006001 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00006002 if (class_type)
6003 {
Greg Clayton4116e932012-05-15 02:33:01 +00006004 if (class_type->GetID() != MakeUserID(decl_ctx_die->GetOffset()))
6005 {
6006 // We uniqued the parent class of this function to another class
6007 // so we now need to associate all dies under "decl_ctx_die" to
6008 // DIEs in the DIE for "class_type"...
6009 DWARFCompileUnitSP class_type_cu_sp;
6010 const DWARFDebugInfoEntry *class_type_die = DebugInfo()->GetDIEPtr(class_type->GetID(), &class_type_cu_sp);
6011 if (class_type_die)
6012 {
6013 if (CopyUniqueClassMethodTypes (class_type,
6014 class_type_cu_sp.get(),
6015 class_type_die,
6016 dwarf_cu,
6017 decl_ctx_die))
6018 {
6019 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006020 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton4116e932012-05-15 02:33:01 +00006021 {
6022 type_sp = type_ptr->shared_from_this();
6023 break;
6024 }
6025 }
6026 }
6027 }
6028
Greg Clayton72da3972011-08-16 18:40:23 +00006029 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00006030 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006031 // We have a specification which we are going to base our function
6032 // prototype off of, so we need this type to be completed so that the
6033 // m_die_to_decl_ctx for the method in the specification has a valid
6034 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006035 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00006036 // If we have a specification, then the function type should have been
6037 // made with the specification and not with this die.
6038 DWARFCompileUnitSP spec_cu_sp;
6039 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006040 clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, spec_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006041 if (spec_clang_decl_ctx)
6042 {
6043 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
6044 }
6045 else
Jim Inghamc1663042011-09-29 22:12:35 +00006046 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006047 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
6048 MakeUserID(die->GetOffset()),
6049 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006050 }
Greg Clayton72da3972011-08-16 18:40:23 +00006051 type_handled = true;
6052 }
6053 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
6054 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00006055 // We have a specification which we are going to base our function
6056 // prototype off of, so we need this type to be completed so that the
6057 // m_die_to_decl_ctx for the method in the abstract origin has a valid
6058 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00006059 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00006060
Greg Clayton72da3972011-08-16 18:40:23 +00006061 DWARFCompileUnitSP abs_cu_sp;
6062 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Eric Christopher6cc6e602012-03-25 19:37:33 +00006063 clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (sc, dwarf_cu, abs_die);
Greg Clayton5cf58b92011-10-05 22:22:08 +00006064 if (abs_clang_decl_ctx)
6065 {
6066 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
6067 }
6068 else
Jim Inghamc1663042011-09-29 22:12:35 +00006069 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006070 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
6071 MakeUserID(die->GetOffset()),
6072 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00006073 }
Greg Clayton72da3972011-08-16 18:40:23 +00006074 type_handled = true;
6075 }
6076 else
6077 {
6078 clang_type_t class_opaque_type = class_type->GetClangForwardType();
6079 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00006080 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006081 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00006082 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006083 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
6084 // in the DWARF for C++ methods... Default to public for now...
6085 if (accessibility == eAccessNone)
6086 accessibility = eAccessPublic;
6087
6088 if (!is_static && !die->HasChildren())
6089 {
6090 // We have a C++ member function with no children (this pointer!)
6091 // and clang will get mad if we try and make a function that isn't
6092 // well formed in the DWARF, so we will just skip it...
6093 type_handled = true;
6094 }
6095 else
6096 {
6097 clang::CXXMethodDecl *cxx_method_decl;
6098 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00006099 Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8llx from %s/%s",
Greg Clayton20568dd2011-10-13 23:13:20 +00006100 type_name_cstr,
6101 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00006102 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00006103 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
6104 m_obj_file->GetFileSpec().GetFilename().GetCString());
6105
Sean Callananc1b732d2011-11-01 18:07:13 +00006106 const bool is_attr_used = false;
6107
Greg Clayton20568dd2011-10-13 23:13:20 +00006108 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
6109 type_name_cstr,
6110 clang_type,
6111 accessibility,
6112 is_virtual,
6113 is_static,
6114 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00006115 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00006116 is_attr_used,
6117 is_artificial);
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006118
Greg Clayton20568dd2011-10-13 23:13:20 +00006119 type_handled = cxx_method_decl != NULL;
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006120
6121 if (type_handled)
Jim Ingham379397632012-10-27 02:54:13 +00006122 {
Sean Callanan4ac7ec72012-10-31 02:01:58 +00006123 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
6124
6125 Host::SetCrashDescription (NULL);
6126
6127
6128 ClangASTMetadata metadata;
6129 metadata.SetUserID(MakeUserID(die->GetOffset()));
6130
6131 if (!object_pointer_name.empty())
6132 {
6133 metadata.SetObjectPtrName(object_pointer_name.c_str());
6134 if (log)
6135 log->Printf ("Setting object pointer name: %s on method object 0x%ld.\n",
6136 object_pointer_name.c_str(),
6137 (uintptr_t) cxx_method_decl);
6138 }
6139 GetClangASTContext().SetMetadata ((uintptr_t)cxx_method_decl, metadata);
Jim Ingham379397632012-10-27 02:54:13 +00006140 }
Greg Clayton20568dd2011-10-13 23:13:20 +00006141 }
Greg Clayton72da3972011-08-16 18:40:23 +00006142 }
6143 else
6144 {
Greg Clayton20568dd2011-10-13 23:13:20 +00006145 // We were asked to parse the type for a method in a class, yet the
6146 // class hasn't been asked to complete itself through the
6147 // clang::ExternalASTSource protocol, so we need to just have the
6148 // class complete itself and do things the right way, then our
6149 // DIE should then have an entry in the m_die_to_type map. First
6150 // we need to modify the m_die_to_type so it doesn't think we are
6151 // trying to parse this DIE anymore...
6152 m_die_to_type[die] = NULL;
6153
6154 // Now we get the full type to force our class type to complete itself
6155 // using the clang::ExternalASTSource protocol which will parse all
6156 // base classes and all methods (including the method for this DIE).
6157 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00006158
Greg Clayton20568dd2011-10-13 23:13:20 +00006159 // The type for this DIE should have been filled in the function call above
6160 type_ptr = m_die_to_type[die];
Greg Claytone5476db2012-06-01 20:32:35 +00006161 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
Greg Clayton20568dd2011-10-13 23:13:20 +00006162 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006163 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00006164 break;
6165 }
Sean Callanan02eee4d2012-04-12 23:10:00 +00006166
6167 // FIXME This is fixing some even uglier behavior but we really need to
6168 // uniq the methods of each class as well as the class itself.
6169 // <rdar://problem/11240464>
6170 type_handled = true;
Greg Clayton72da3972011-08-16 18:40:23 +00006171 }
Greg Clayton931180e2011-01-27 06:44:37 +00006172 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006173 }
6174 }
Greg Clayton0fffff52010-09-24 05:15:53 +00006175 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006176 }
Greg Clayton24739922010-10-13 03:15:28 +00006177
6178 if (!type_handled)
6179 {
6180 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00006181 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
6182 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006183 clang_type,
6184 storage,
6185 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00006186
6187// if (template_param_infos.GetSize() > 0)
6188// {
6189// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
6190// function_decl,
6191// type_name_cstr,
6192// template_param_infos);
6193//
6194// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
6195// func_template_decl,
6196// template_param_infos);
6197// }
Greg Clayton24739922010-10-13 03:15:28 +00006198 // Add the decl to our DIE to decl context map
6199 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00006200 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00006201 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006202 ast.SetFunctionParameters (function_decl,
6203 &function_param_decls.front(),
6204 function_param_decls.size());
Sean Callanan60217122012-04-13 00:10:03 +00006205
Jim Ingham379397632012-10-27 02:54:13 +00006206 ClangASTMetadata metadata;
6207 metadata.SetUserID(MakeUserID(die->GetOffset()));
6208
6209 if (!object_pointer_name.empty())
6210 {
6211 metadata.SetObjectPtrName(object_pointer_name.c_str());
Jim Ingham2cffda3f2012-10-30 23:34:07 +00006212 if (log)
6213 log->Printf ("Setting object pointer name: %s on function object 0x%ld.\n",
6214 object_pointer_name.c_str(),
6215 (uintptr_t) function_decl);
Jim Ingham379397632012-10-27 02:54:13 +00006216 }
6217 GetClangASTContext().SetMetadata ((uintptr_t)function_decl, metadata);
Greg Clayton24739922010-10-13 03:15:28 +00006218 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006219 }
Greg Clayton81c22f62011-10-19 18:09:39 +00006220 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006221 this,
6222 type_name_const_str,
6223 0,
6224 NULL,
6225 LLDB_INVALID_UID,
6226 Type::eEncodingIsUID,
6227 &decl,
6228 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006229 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00006230 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006231 }
6232 break;
6233
6234 case DW_TAG_array_type:
6235 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006236 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00006237 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006238
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006239 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006240 int64_t first_index = 0;
6241 uint32_t byte_stride = 0;
6242 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00006243 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006244
6245 if (num_attributes > 0)
6246 {
6247 uint32_t i;
6248 for (i=0; i<num_attributes; ++i)
6249 {
6250 attr = attributes.AttributeAtIndex(i);
6251 DWARFFormValue form_value;
6252 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6253 {
6254 switch (attr)
6255 {
6256 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6257 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6258 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6259 case DW_AT_name:
6260 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00006261 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006262 break;
6263
6264 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006265 case DW_AT_byte_size: break; // byte_size = form_value.Unsigned(); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006266 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
6267 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton23f59502012-07-17 03:23:13 +00006268 case DW_AT_accessibility: break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
6269 case DW_AT_declaration: break; // is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006270 case DW_AT_allocated:
6271 case DW_AT_associated:
6272 case DW_AT_data_location:
6273 case DW_AT_description:
6274 case DW_AT_ordering:
6275 case DW_AT_start_scope:
6276 case DW_AT_visibility:
6277 case DW_AT_specification:
6278 case DW_AT_abstract_origin:
6279 case DW_AT_sibling:
6280 break;
6281 }
6282 }
6283 }
6284
Greg Clayton81c22f62011-10-19 18:09:39 +00006285 DEBUG_PRINTF ("0x%8.8llx: %s (\"%s\")\n", MakeUserID(die->GetOffset()), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00006286
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006287 Type *element_type = ResolveTypeUID(type_die_offset);
6288
6289 if (element_type)
6290 {
6291 std::vector<uint64_t> element_orders;
6292 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
6293 if (byte_stride == 0 && bit_stride == 0)
6294 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00006295 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006296 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
6297 uint64_t num_elements = 0;
6298 std::vector<uint64_t>::const_reverse_iterator pos;
6299 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
6300 for (pos = element_orders.rbegin(); pos != end; ++pos)
6301 {
6302 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006303 clang_type = ast.CreateArrayType (array_element_type,
6304 num_elements,
6305 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006306 array_element_type = clang_type;
6307 array_element_bit_stride = array_element_bit_stride * num_elements;
6308 }
6309 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00006310 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006311 this,
6312 empty_name,
6313 array_element_bit_stride / 8,
6314 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00006315 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006316 Type::eEncodingIsUID,
6317 &decl,
6318 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006319 Type::eResolveStateFull));
6320 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006321 }
6322 }
6323 }
6324 break;
6325
Greg Clayton9b81a312010-06-12 01:20:30 +00006326 case DW_TAG_ptr_to_member_type:
6327 {
6328 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
6329 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
6330
Greg Claytond88d7592010-09-15 08:33:30 +00006331 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00006332
6333 if (num_attributes > 0) {
6334 uint32_t i;
6335 for (i=0; i<num_attributes; ++i)
6336 {
6337 attr = attributes.AttributeAtIndex(i);
6338 DWARFFormValue form_value;
6339 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
6340 {
6341 switch (attr)
6342 {
6343 case DW_AT_type:
6344 type_die_offset = form_value.Reference(dwarf_cu); break;
6345 case DW_AT_containing_type:
6346 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
6347 }
6348 }
6349 }
6350
6351 Type *pointee_type = ResolveTypeUID(type_die_offset);
6352 Type *class_type = ResolveTypeUID(containing_type_die_offset);
6353
Greg Clayton526e5af2010-11-13 03:52:47 +00006354 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
6355 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00006356
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006357 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
6358 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00006359
Greg Clayton526e5af2010-11-13 03:52:47 +00006360 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
6361 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00006362
Greg Clayton81c22f62011-10-19 18:09:39 +00006363 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00006364 this,
6365 type_name_const_str,
6366 byte_size,
6367 NULL,
6368 LLDB_INVALID_UID,
6369 Type::eEncodingIsUID,
6370 NULL,
6371 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00006372 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00006373 }
6374
6375 break;
6376 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006377 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00006378 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006379 break;
6380 }
6381
6382 if (type_sp.get())
6383 {
6384 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6385 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6386
6387 SymbolContextScope * symbol_context_scope = NULL;
6388 if (sc_parent_tag == DW_TAG_compile_unit)
6389 {
6390 symbol_context_scope = sc.comp_unit;
6391 }
6392 else if (sc.function != NULL)
6393 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006394 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006395 if (symbol_context_scope == NULL)
6396 symbol_context_scope = sc.function;
6397 }
6398
6399 if (symbol_context_scope != NULL)
6400 {
6401 type_sp->SetSymbolContextScope(symbol_context_scope);
6402 }
6403
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006404 // We are ready to put this type into the uniqued list up at the module level
6405 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00006406
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00006407 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006408 }
6409 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00006410 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006411 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00006412 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006413 }
6414 }
6415 return type_sp;
6416}
6417
6418size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00006419SymbolFileDWARF::ParseTypes
6420(
6421 const SymbolContext& sc,
6422 DWARFCompileUnit* dwarf_cu,
6423 const DWARFDebugInfoEntry *die,
6424 bool parse_siblings,
6425 bool parse_children
6426)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006427{
6428 size_t types_added = 0;
6429 while (die != NULL)
6430 {
6431 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00006432 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006433 {
6434 if (type_is_new)
6435 ++types_added;
6436 }
6437
6438 if (parse_children && die->HasChildren())
6439 {
6440 if (die->Tag() == DW_TAG_subprogram)
6441 {
6442 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00006443 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006444 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
6445 }
6446 else
6447 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
6448 }
6449
6450 if (parse_siblings)
6451 die = die->GetSibling();
6452 else
6453 die = NULL;
6454 }
6455 return types_added;
6456}
6457
6458
6459size_t
6460SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
6461{
6462 assert(sc.comp_unit && sc.function);
6463 size_t functions_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006464 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006465 if (dwarf_cu)
6466 {
6467 dw_offset_t function_die_offset = sc.function->GetID();
6468 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
6469 if (function_die)
6470 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00006471 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006472 }
6473 }
6474
6475 return functions_added;
6476}
6477
6478
6479size_t
6480SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
6481{
6482 // At least a compile unit must be valid
6483 assert(sc.comp_unit);
6484 size_t types_added = 0;
Greg Clayton1f746072012-08-29 21:13:06 +00006485 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnit(sc.comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006486 if (dwarf_cu)
6487 {
6488 if (sc.function)
6489 {
6490 dw_offset_t function_die_offset = sc.function->GetID();
6491 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
6492 if (func_die && func_die->HasChildren())
6493 {
6494 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
6495 }
6496 }
6497 else
6498 {
6499 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
6500 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
6501 {
6502 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
6503 }
6504 }
6505 }
6506
6507 return types_added;
6508}
6509
6510size_t
6511SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
6512{
6513 if (sc.comp_unit != NULL)
6514 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00006515 DWARFDebugInfo* info = DebugInfo();
6516 if (info == NULL)
6517 return 0;
6518
6519 uint32_t cu_idx = UINT32_MAX;
6520 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006521
6522 if (dwarf_cu == NULL)
6523 return 0;
6524
6525 if (sc.function)
6526 {
6527 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00006528
6529 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006530 if (func_lo_pc != DW_INVALID_ADDRESS)
6531 {
6532 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00006533
Greg Claytone38a5ed2012-01-05 03:57:59 +00006534 // Let all blocks know they have parse all their variables
6535 sc.function->GetBlock (false).SetDidParseVariables (true, true);
6536 return num_variables;
6537 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006538 }
6539 else if (sc.comp_unit)
6540 {
6541 uint32_t vars_added = 0;
6542 VariableListSP variables (sc.comp_unit->GetVariableList(false));
6543
6544 if (variables.get() == NULL)
6545 {
6546 variables.reset(new VariableList());
6547 sc.comp_unit->SetVariableList(variables);
6548
Greg Claytond4a2b372011-09-12 23:21:58 +00006549 DWARFCompileUnit* match_dwarf_cu = NULL;
6550 const DWARFDebugInfoEntry* die = NULL;
6551 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00006552 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00006553 {
Greg Clayton97fbc342011-10-20 22:30:33 +00006554 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00006555 {
6556 DWARFMappedHash::DIEInfoArray hash_data_array;
6557 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
6558 dwarf_cu->GetNextCompileUnitOffset(),
6559 hash_data_array))
6560 {
6561 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
6562 }
6563 }
Greg Clayton7f995132011-10-04 22:41:51 +00006564 }
6565 else
6566 {
6567 // Index if we already haven't to make sure the compile units
6568 // get indexed and make their global DIE index list
6569 if (!m_indexed)
6570 Index ();
6571
6572 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
6573 dwarf_cu->GetNextCompileUnitOffset(),
6574 die_offsets);
6575 }
6576
6577 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00006578 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006579 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006580 DWARFDebugInfo* debug_info = DebugInfo();
6581 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006582 {
Greg Claytond4a2b372011-09-12 23:21:58 +00006583 const dw_offset_t die_offset = die_offsets[i];
6584 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00006585 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00006586 {
Greg Clayton95d87902011-11-11 03:16:25 +00006587 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
6588 if (var_sp)
6589 {
6590 variables->AddVariableIfUnique (var_sp);
6591 ++vars_added;
6592 }
Greg Claytond4a2b372011-09-12 23:21:58 +00006593 }
Greg Clayton95d87902011-11-11 03:16:25 +00006594 else
6595 {
6596 if (m_using_apple_tables)
6597 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006598 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x)\n", die_offset);
Greg Clayton95d87902011-11-11 03:16:25 +00006599 }
6600 }
6601
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006602 }
6603 }
6604 }
6605 return vars_added;
6606 }
6607 }
6608 return 0;
6609}
6610
6611
6612VariableSP
6613SymbolFileDWARF::ParseVariableDIE
6614(
6615 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006616 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006617 const DWARFDebugInfoEntry *die,
6618 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006619)
6620{
6621
Greg Clayton83c5cd92010-11-14 22:13:40 +00006622 VariableSP var_sp (m_die_to_variable_sp[die]);
6623 if (var_sp)
6624 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006625
6626 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00006627
6628 if ((tag == DW_TAG_variable) ||
6629 (tag == DW_TAG_constant) ||
6630 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006631 {
Greg Clayton7f995132011-10-04 22:41:51 +00006632 DWARFDebugInfoEntry::Attributes attributes;
6633 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
6634 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006635 {
Greg Clayton7f995132011-10-04 22:41:51 +00006636 const char *name = NULL;
6637 const char *mangled = NULL;
6638 Declaration decl;
6639 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00006640 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00006641 DWARFExpression location;
6642 bool is_external = false;
6643 bool is_artificial = false;
6644 bool location_is_const_value_data = false;
Greg Clayton23f59502012-07-17 03:23:13 +00006645 //AccessType accessibility = eAccessNone;
Greg Clayton7f995132011-10-04 22:41:51 +00006646
6647 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006648 {
Greg Clayton7f995132011-10-04 22:41:51 +00006649 dw_attr_t attr = attributes.AttributeAtIndex(i);
6650 DWARFFormValue form_value;
6651 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006652 {
Greg Clayton7f995132011-10-04 22:41:51 +00006653 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006654 {
Greg Clayton7f995132011-10-04 22:41:51 +00006655 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
6656 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
6657 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
6658 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
6659 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00006660 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006661 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
6662 case DW_AT_const_value:
6663 location_is_const_value_data = true;
6664 // Fall through...
6665 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006666 {
Greg Clayton7f995132011-10-04 22:41:51 +00006667 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006668 {
Greg Clayton7f995132011-10-04 22:41:51 +00006669 const DataExtractor& debug_info_data = get_debug_info_data();
6670
6671 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
6672 uint32_t block_length = form_value.Unsigned();
Greg Clayton7f4c7432012-08-11 00:49:14 +00006673 location.CopyOpcodeData(get_debug_info_data(), block_offset, block_length);
Greg Clayton7f995132011-10-04 22:41:51 +00006674 }
6675 else
6676 {
6677 const DataExtractor& debug_loc_data = get_debug_loc_data();
6678 const dw_offset_t debug_loc_offset = form_value.Unsigned();
6679
6680 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
6681 if (loc_list_length > 0)
6682 {
Greg Clayton7f4c7432012-08-11 00:49:14 +00006683 location.CopyOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
Greg Clayton7f995132011-10-04 22:41:51 +00006684 assert (func_low_pc != LLDB_INVALID_ADDRESS);
6685 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
6686 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006687 }
6688 }
Greg Clayton7f995132011-10-04 22:41:51 +00006689 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006690
Greg Clayton7f995132011-10-04 22:41:51 +00006691 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Greg Clayton23f59502012-07-17 03:23:13 +00006692 case DW_AT_accessibility: break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7f995132011-10-04 22:41:51 +00006693 case DW_AT_declaration:
6694 case DW_AT_description:
6695 case DW_AT_endianity:
6696 case DW_AT_segment:
6697 case DW_AT_start_scope:
6698 case DW_AT_visibility:
6699 default:
6700 case DW_AT_abstract_origin:
6701 case DW_AT_sibling:
6702 case DW_AT_specification:
6703 break;
6704 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006705 }
6706 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006707
Greg Clayton7f995132011-10-04 22:41:51 +00006708 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006709 {
Greg Clayton7f995132011-10-04 22:41:51 +00006710 ValueType scope = eValueTypeInvalid;
6711
6712 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
6713 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006714 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00006715
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006716 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6717 // or static variable, so we have to do a little digging by
6718 // looking at the location of a varaible to see if it contains
6719 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6720 // somewhere because clang likes to combine small global variables
6721 // into the same symbol and have locations like:
6722 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6723 // So if we don't have a DW_TAG_formal_parameter, we can look at
6724 // the location to see if it contains a DW_OP_addr opcode, and
6725 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006726 if (tag == DW_TAG_formal_parameter)
6727 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006728 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006729 {
Greg Clayton96c09682012-01-04 22:56:43 +00006730 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006731 // Check if the location has a DW_OP_addr with any address value...
6732 addr_t location_has_op_addr = false;
6733 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006734 {
6735 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6736 if (op_error)
6737 {
6738 StreamString strm;
6739 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006740 GetObjectFile()->GetModule()->ReportError ("0x%8.8x: %s has an invalid location: %s", die->GetOffset(), DW_TAG_value_to_name(die->Tag()), strm.GetString().c_str());
Greg Clayton96c09682012-01-04 22:56:43 +00006741 }
6742 }
Greg Claytond1767f02011-12-08 02:13:16 +00006743
6744 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006745 {
Greg Claytond1767f02011-12-08 02:13:16 +00006746 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006747 {
Greg Claytond1767f02011-12-08 02:13:16 +00006748 scope = eValueTypeVariableGlobal;
6749
Greg Clayton1f746072012-08-29 21:13:06 +00006750 if (GetDebugMapSymfile ())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006751 {
Greg Claytond1767f02011-12-08 02:13:16 +00006752 // When leaving the DWARF in the .o files on darwin,
6753 // when we have a global variable that wasn't initialized,
6754 // the .o file might not have allocated a virtual
6755 // address for the global variable. In this case it will
6756 // have created a symbol for the global variable
6757 // that is undefined and external and the value will
6758 // be the byte size of the variable. When we do the
6759 // address map in SymbolFileDWARFDebugMap we rely on
6760 // having an address, we need to do some magic here
6761 // so we can get the correct address for our global
6762 // variable. The address for all of these entries
6763 // will be zero, and there will be an undefined symbol
6764 // in this object file, and the executable will have
6765 // a matching symbol with a good address. So here we
6766 // dig up the correct address and replace it in the
6767 // location for the variable, and set the variable's
6768 // symbol context scope to be that of the main executable
6769 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006770 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006771 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006772
Greg Claytond1767f02011-12-08 02:13:16 +00006773 // we have a possible uninitialized extern global
6774 Symtab *symtab = m_obj_file->GetSymtab();
6775 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006776 {
Greg Claytond1767f02011-12-08 02:13:16 +00006777 ConstString const_name(name);
6778 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6779 eSymbolTypeUndefined,
6780 Symtab::eDebugNo,
6781 Symtab::eVisibilityExtern);
6782
6783 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006784 {
Greg Claytond1767f02011-12-08 02:13:16 +00006785 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6786 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006787 {
Greg Claytond1767f02011-12-08 02:13:16 +00006788 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6789 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6790 eSymbolTypeData,
6791 Symtab::eDebugYes,
6792 Symtab::eVisibilityExtern);
6793 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006794 {
Greg Claytone7612132012-03-07 21:03:09 +00006795 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006796 {
Greg Claytone7612132012-03-07 21:03:09 +00006797 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006798 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006799 {
Greg Claytond1767f02011-12-08 02:13:16 +00006800 if (location.Update_DW_OP_addr (defined_addr))
6801 {
6802 symbol_context_scope = defined_symbol;
6803 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006804 }
6805 }
6806 }
6807 }
6808 }
6809 }
6810 }
6811 }
6812 }
Greg Claytond1767f02011-12-08 02:13:16 +00006813 else
6814 {
6815 scope = eValueTypeVariableStatic;
6816 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006817 }
6818 else
Greg Claytond1767f02011-12-08 02:13:16 +00006819 {
6820 scope = eValueTypeVariableLocal;
6821 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006822 }
Greg Clayton7f995132011-10-04 22:41:51 +00006823
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006824 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006825 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006826 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006827 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006828 case DW_TAG_subprogram:
6829 case DW_TAG_inlined_subroutine:
6830 case DW_TAG_lexical_block:
6831 if (sc.function)
6832 {
6833 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6834 if (symbol_context_scope == NULL)
6835 symbol_context_scope = sc.function;
6836 }
6837 break;
6838
6839 default:
6840 symbol_context_scope = sc.comp_unit;
6841 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006842 }
Greg Clayton7f995132011-10-04 22:41:51 +00006843 }
6844
Greg Clayton5cf58b92011-10-05 22:22:08 +00006845 if (symbol_context_scope)
6846 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006847 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6848 name,
6849 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006850 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006851 scope,
6852 symbol_context_scope,
6853 &decl,
6854 location,
6855 is_external,
6856 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006857
6858 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6859 }
6860 else
6861 {
6862 // Not ready to parse this variable yet. It might be a global
6863 // or static variable that is in a function scope and the function
6864 // in the symbol context wasn't filled in yet
6865 return var_sp;
6866 }
Greg Clayton7f995132011-10-04 22:41:51 +00006867 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006868 }
Greg Clayton7f995132011-10-04 22:41:51 +00006869 // Cache var_sp even if NULL (the variable was just a specification or
6870 // was missing vital information to be able to be displayed in the debugger
6871 // (missing location due to optimization, etc)) so we don't re-parse
6872 // this DIE over and over later...
6873 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006874 }
6875 return var_sp;
6876}
6877
Greg Claytonc662ec82011-06-17 22:10:16 +00006878
6879const DWARFDebugInfoEntry *
6880SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6881 dw_offset_t spec_block_die_offset,
6882 DWARFCompileUnit **result_die_cu_handle)
6883{
6884 // Give the concrete function die specified by "func_die_offset", find the
6885 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6886 // to "spec_block_die_offset"
6887 DWARFDebugInfo* info = DebugInfo();
6888
6889 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6890 if (die)
6891 {
6892 assert (*result_die_cu_handle);
6893 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6894 }
6895 return NULL;
6896}
6897
6898
6899const DWARFDebugInfoEntry *
6900SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6901 const DWARFDebugInfoEntry *die,
6902 dw_offset_t spec_block_die_offset,
6903 DWARFCompileUnit **result_die_cu_handle)
6904{
6905 if (die)
6906 {
6907 switch (die->Tag())
6908 {
6909 case DW_TAG_subprogram:
6910 case DW_TAG_inlined_subroutine:
6911 case DW_TAG_lexical_block:
6912 {
6913 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6914 {
6915 *result_die_cu_handle = dwarf_cu;
6916 return die;
6917 }
6918
6919 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6920 {
6921 *result_die_cu_handle = dwarf_cu;
6922 return die;
6923 }
6924 }
6925 break;
6926 }
6927
6928 // Give the concrete function die specified by "func_die_offset", find the
6929 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6930 // to "spec_block_die_offset"
6931 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6932 {
6933 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6934 child_die,
6935 spec_block_die_offset,
6936 result_die_cu_handle);
6937 if (result_die)
6938 return result_die;
6939 }
6940 }
6941
6942 *result_die_cu_handle = NULL;
6943 return NULL;
6944}
6945
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006946size_t
6947SymbolFileDWARF::ParseVariables
6948(
6949 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006950 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006951 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006952 const DWARFDebugInfoEntry *orig_die,
6953 bool parse_siblings,
6954 bool parse_children,
6955 VariableList* cc_variable_list
6956)
6957{
6958 if (orig_die == NULL)
6959 return 0;
6960
Greg Claytonc662ec82011-06-17 22:10:16 +00006961 VariableListSP variable_list_sp;
6962
6963 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006964 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006965 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006966 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006967 dw_tag_t tag = die->Tag();
6968
6969 // Check to see if we have already parsed this variable or constant?
6970 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006971 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006972 if (cc_variable_list)
6973 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006974 }
6975 else
6976 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006977 // We haven't already parsed it, lets do that now.
6978 if ((tag == DW_TAG_variable) ||
6979 (tag == DW_TAG_constant) ||
6980 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006981 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006982 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006983 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006984 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6985 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6986 switch (parent_tag)
6987 {
6988 case DW_TAG_compile_unit:
6989 if (sc.comp_unit != NULL)
6990 {
6991 variable_list_sp = sc.comp_unit->GetVariableList(false);
6992 if (variable_list_sp.get() == NULL)
6993 {
6994 variable_list_sp.reset(new VariableList());
6995 sc.comp_unit->SetVariableList(variable_list_sp);
6996 }
6997 }
6998 else
6999 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00007000 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
7001 MakeUserID(sc_parent_die->GetOffset()),
7002 DW_TAG_value_to_name (parent_tag),
7003 MakeUserID(orig_die->GetOffset()),
7004 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007005 }
7006 break;
7007
7008 case DW_TAG_subprogram:
7009 case DW_TAG_inlined_subroutine:
7010 case DW_TAG_lexical_block:
7011 if (sc.function != NULL)
7012 {
7013 // Check to see if we already have parsed the variables for the given scope
7014
Greg Clayton81c22f62011-10-19 18:09:39 +00007015 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007016 if (block == NULL)
7017 {
7018 // This must be a specification or abstract origin with
7019 // a concrete block couterpart in the current function. We need
7020 // to find the concrete block so we can correctly add the
7021 // variable to it
7022 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
7023 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
7024 sc_parent_die->GetOffset(),
7025 &concrete_block_die_cu);
7026 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00007027 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007028 }
7029
7030 if (block != NULL)
7031 {
7032 const bool can_create = false;
7033 variable_list_sp = block->GetBlockVariableList (can_create);
7034 if (variable_list_sp.get() == NULL)
7035 {
7036 variable_list_sp.reset(new VariableList());
7037 block->SetVariableList(variable_list_sp);
7038 }
7039 }
7040 }
7041 break;
7042
7043 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00007044 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
7045 MakeUserID(orig_die->GetOffset()),
7046 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00007047 break;
7048 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00007049 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007050
7051 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007052 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00007053 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
7054 if (var_sp)
7055 {
Greg Claytonc662ec82011-06-17 22:10:16 +00007056 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00007057 if (cc_variable_list)
7058 cc_variable_list->AddVariableIfUnique (var_sp);
7059 ++vars_added;
7060 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007061 }
7062 }
7063 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007064
7065 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
7066
7067 if (!skip_children && parse_children && die->HasChildren())
7068 {
7069 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
7070 }
7071
7072 if (parse_siblings)
7073 die = die->GetSibling();
7074 else
7075 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007076 }
Greg Claytonc662ec82011-06-17 22:10:16 +00007077 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00007078}
7079
7080//------------------------------------------------------------------
7081// PluginInterface protocol
7082//------------------------------------------------------------------
7083const char *
7084SymbolFileDWARF::GetPluginName()
7085{
7086 return "SymbolFileDWARF";
7087}
7088
7089const char *
7090SymbolFileDWARF::GetShortPluginName()
7091{
7092 return GetPluginNameStatic();
7093}
7094
7095uint32_t
7096SymbolFileDWARF::GetPluginVersion()
7097{
7098 return 1;
7099}
7100
7101void
Greg Clayton6beaaa62011-01-17 03:46:26 +00007102SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
7103{
7104 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7105 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7106 if (clang_type)
7107 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7108}
7109
7110void
7111SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
7112{
7113 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7114 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
7115 if (clang_type)
7116 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
7117}
7118
Greg Claytona2721472011-06-25 00:44:06 +00007119void
Sean Callanancc427fa2011-07-30 02:42:06 +00007120SymbolFileDWARF::DumpIndexes ()
7121{
7122 StreamFile s(stdout, false);
7123
7124 s.Printf ("DWARF index for (%s) '%s/%s':",
7125 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
7126 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
7127 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
7128 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
7129 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
7130 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
7131 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
7132 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
7133 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
7134 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
7135 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
7136}
7137
7138void
7139SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
7140 const char *name,
7141 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00007142{
Sean Callanancc427fa2011-07-30 02:42:06 +00007143 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00007144
7145 if (iter == m_decl_ctx_to_die.end())
7146 return;
7147
Greg Claytoncb5860a2011-10-13 23:49:28 +00007148 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00007149 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007150 const DWARFDebugInfoEntry *context_die = *pos;
7151
7152 if (!results)
7153 return;
7154
7155 DWARFDebugInfo* info = DebugInfo();
7156
7157 DIEArray die_offsets;
7158
7159 DWARFCompileUnit* dwarf_cu = NULL;
7160 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00007161
7162 if (m_using_apple_tables)
7163 {
7164 if (m_apple_types_ap.get())
7165 m_apple_types_ap->FindByName (name, die_offsets);
7166 }
7167 else
7168 {
7169 if (!m_indexed)
7170 Index ();
7171
7172 m_type_index.Find (ConstString(name), die_offsets);
7173 }
7174
Greg Clayton220a0072011-12-09 08:48:30 +00007175 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007176
7177 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00007178 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00007179 for (size_t i = 0; i < num_matches; ++i)
7180 {
7181 const dw_offset_t die_offset = die_offsets[i];
7182 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00007183
Greg Claytoncb5860a2011-10-13 23:49:28 +00007184 if (die->GetParent() != context_die)
7185 continue;
7186
7187 Type *matching_type = ResolveType (dwarf_cu, die);
7188
Greg Clayton42ce2f32011-12-12 21:50:19 +00007189 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00007190 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
7191
7192 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
7193 {
7194 clang::TagDecl *tag_decl = tag_type->getDecl();
7195 results->push_back(tag_decl);
7196 }
7197 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
7198 {
7199 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
7200 results->push_back(typedef_decl);
7201 }
Greg Claytona2721472011-06-25 00:44:06 +00007202 }
7203 }
7204 }
7205}
7206
7207void
7208SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00007209 const clang::DeclContext *decl_context,
7210 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00007211 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
7212{
Greg Clayton85ae2e12011-10-18 23:36:41 +00007213
7214 switch (decl_context->getDeclKind())
7215 {
7216 case clang::Decl::Namespace:
7217 case clang::Decl::TranslationUnit:
7218 {
7219 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7220 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
7221 }
7222 break;
7223 default:
7224 break;
7225 }
Greg Claytona2721472011-06-25 00:44:06 +00007226}
Greg Claytoncaab74e2012-01-28 00:48:57 +00007227
7228bool
7229SymbolFileDWARF::LayoutRecordType (void *baton,
7230 const clang::RecordDecl *record_decl,
7231 uint64_t &size,
7232 uint64_t &alignment,
7233 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7234 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7235 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7236{
7237 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
7238 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
7239}
7240
7241
7242bool
7243SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
7244 uint64_t &bit_size,
7245 uint64_t &alignment,
7246 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
7247 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
7248 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
7249{
7250 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
7251 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
7252 bool success = false;
7253 base_offsets.clear();
7254 vbase_offsets.clear();
7255 if (pos != m_record_decl_to_layout_map.end())
7256 {
7257 bit_size = pos->second.bit_size;
7258 alignment = pos->second.alignment;
7259 field_offsets.swap(pos->second.field_offsets);
7260 m_record_decl_to_layout_map.erase(pos);
7261 success = true;
7262 }
7263 else
7264 {
7265 bit_size = 0;
7266 alignment = 0;
7267 field_offsets.clear();
7268 }
7269
7270 if (log)
7271 GetObjectFile()->GetModule()->LogMessage (log.get(),
7272 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
7273 record_decl,
7274 bit_size,
7275 alignment,
7276 (uint32_t)field_offsets.size(),
7277 (uint32_t)base_offsets.size(),
7278 (uint32_t)vbase_offsets.size(),
7279 success);
7280 return success;
7281}
7282
7283
Greg Clayton1f746072012-08-29 21:13:06 +00007284SymbolFileDWARFDebugMap *
7285SymbolFileDWARF::GetDebugMapSymfile ()
7286{
7287 if (m_debug_map_symfile == NULL && !m_debug_map_module_wp.expired())
7288 {
7289 lldb::ModuleSP module_sp (m_debug_map_module_wp.lock());
7290 if (module_sp)
7291 {
7292 SymbolVendor *sym_vendor = module_sp->GetSymbolVendor();
7293 if (sym_vendor)
7294 m_debug_map_symfile = (SymbolFileDWARFDebugMap *)sym_vendor->GetSymbolFile();
7295 }
7296 }
7297 return m_debug_map_symfile;
7298}
7299
Greg Claytoncaab74e2012-01-28 00:48:57 +00007300