blob: 8b501c79bf4bd74c6d143567321d949ffb6e935f [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"
60#include "DWARFDIECollection.h"
61#include "DWARFFormValue.h"
62#include "DWARFLocationList.h"
63#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000064#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000065
66#include <map>
67
Greg Clayton62742b12010-11-11 01:09:45 +000068//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000069
70#ifdef ENABLE_DEBUG_PRINTF
71#include <stdio.h>
72#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
73#else
74#define DEBUG_PRINTF(fmt, ...)
75#endif
76
Greg Clayton594e5ed2010-09-27 21:07:38 +000077#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000078
79using namespace lldb;
80using namespace lldb_private;
81
Greg Clayton3bffb082011-12-10 02:15:28 +000082static inline bool
Sean Callanan5b26f272012-02-04 08:49:35 +000083child_requires_parent_class_union_or_struct_to_be_completed (dw_tag_t tag)
Greg Clayton3bffb082011-12-10 02:15:28 +000084{
Sean Callanan5b26f272012-02-04 08:49:35 +000085 switch (tag)
86 {
87 default:
88 break;
89 case DW_TAG_subprogram:
90 case DW_TAG_inlined_subroutine:
91 case DW_TAG_class_type:
92 case DW_TAG_structure_type:
93 case DW_TAG_union_type:
94 return true;
95 }
96 return false;
Greg Clayton3bffb082011-12-10 02:15:28 +000097}
Chris Lattner30fdc8d2010-06-08 16:52:24 +000098
Sean Callananc7fbf732010-08-06 00:32:49 +000099static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +0000100DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000101{
102 switch (dwarf_accessibility)
103 {
Sean Callananc7fbf732010-08-06 00:32:49 +0000104 case DW_ACCESS_public: return eAccessPublic;
105 case DW_ACCESS_private: return eAccessPrivate;
106 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +0000107 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000108 }
Sean Callananc7fbf732010-08-06 00:32:49 +0000109 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000110}
111
Greg Clayton1fba87112012-02-09 20:03:49 +0000112#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
113
114class DIEStack
115{
116public:
117
118 void Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
119 {
120 m_dies.push_back (DIEInfo(cu, die));
121 }
122
123
124 void LogDIEs (Log *log, SymbolFileDWARF *dwarf)
125 {
126 StreamString log_strm;
127 const size_t n = m_dies.size();
128 log_strm.Printf("DIEStack[%zu]:\n", n);
129 for (size_t i=0; i<n; i++)
130 {
131 DWARFCompileUnit *cu = m_dies[i].cu;
132 const DWARFDebugInfoEntry *die = m_dies[i].die;
133 std::string qualified_name;
134 die->GetQualifiedName(dwarf, cu, qualified_name);
135 log_strm.Printf ("[%zu] 0x%8.8x: %s name='%s'\n",
136 i,
137 die->GetOffset(),
138 DW_TAG_value_to_name(die->Tag()),
139 qualified_name.c_str());
140 }
141 log->PutCString(log_strm.GetData());
142 }
143 void Pop ()
144 {
145 m_dies.pop_back();
146 }
147
148 class ScopedPopper
149 {
150 public:
151 ScopedPopper (DIEStack &die_stack) :
152 m_die_stack (die_stack),
153 m_valid (false)
154 {
155 }
156
157 void
158 Push (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
159 {
160 m_valid = true;
161 m_die_stack.Push (cu, die);
162 }
163
164 ~ScopedPopper ()
165 {
166 if (m_valid)
167 m_die_stack.Pop();
168 }
169
170
171
172 protected:
173 DIEStack &m_die_stack;
174 bool m_valid;
175 };
176
177protected:
178 struct DIEInfo {
179 DIEInfo (DWARFCompileUnit *c, const DWARFDebugInfoEntry *d) :
180 cu(c),
181 die(d)
182 {
183 }
184 DWARFCompileUnit *cu;
185 const DWARFDebugInfoEntry *die;
186 };
187 typedef std::vector<DIEInfo> Stack;
188 Stack m_dies;
189};
190#endif
191
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000192void
193SymbolFileDWARF::Initialize()
194{
195 LogChannelDWARF::Initialize();
196 PluginManager::RegisterPlugin (GetPluginNameStatic(),
197 GetPluginDescriptionStatic(),
198 CreateInstance);
199}
200
201void
202SymbolFileDWARF::Terminate()
203{
204 PluginManager::UnregisterPlugin (CreateInstance);
205 LogChannelDWARF::Initialize();
206}
207
208
209const char *
210SymbolFileDWARF::GetPluginNameStatic()
211{
Greg Claytond4a2b372011-09-12 23:21:58 +0000212 return "dwarf";
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000213}
214
215const char *
216SymbolFileDWARF::GetPluginDescriptionStatic()
217{
218 return "DWARF and DWARF3 debug symbol file reader.";
219}
220
221
222SymbolFile*
223SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
224{
225 return new SymbolFileDWARF(obj_file);
226}
227
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000228TypeList *
229SymbolFileDWARF::GetTypeList ()
230{
231 if (m_debug_map_symfile)
232 return m_debug_map_symfile->GetTypeList();
233 return m_obj_file->GetModule()->GetTypeList();
234
235}
236
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000237//----------------------------------------------------------------------
238// Gets the first parent that is a lexical block, function or inlined
239// subroutine, or compile unit.
240//----------------------------------------------------------------------
241static const DWARFDebugInfoEntry *
242GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
243{
244 const DWARFDebugInfoEntry *die;
245 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
246 {
247 dw_tag_t tag = die->Tag();
248
249 switch (tag)
250 {
251 case DW_TAG_compile_unit:
252 case DW_TAG_subprogram:
253 case DW_TAG_inlined_subroutine:
254 case DW_TAG_lexical_block:
255 return die;
256 }
257 }
258 return NULL;
259}
260
261
Greg Clayton450e3f32010-10-12 02:24:53 +0000262SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
263 SymbolFile (objfile),
Greg Clayton81c22f62011-10-19 18:09:39 +0000264 UserID (0), // Used by SymbolFileDWARFDebugMap to when this class parses .o files to contain the .o file index/ID
Greg Clayton450e3f32010-10-12 02:24:53 +0000265 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000266 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000267 m_flags(),
Greg Claytond4a2b372011-09-12 23:21:58 +0000268 m_data_debug_abbrev (),
269 m_data_debug_aranges (),
270 m_data_debug_frame (),
271 m_data_debug_info (),
272 m_data_debug_line (),
273 m_data_debug_loc (),
274 m_data_debug_ranges (),
275 m_data_debug_str (),
Greg Clayton17674402011-09-28 17:06:40 +0000276 m_data_apple_names (),
277 m_data_apple_types (),
Greg Clayton7f995132011-10-04 22:41:51 +0000278 m_data_apple_namespaces (),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000279 m_abbr(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280 m_info(),
281 m_line(),
Greg Clayton7f995132011-10-04 22:41:51 +0000282 m_apple_names_ap (),
283 m_apple_types_ap (),
284 m_apple_namespaces_ap (),
Greg Clayton5009f9d2011-10-27 17:55:14 +0000285 m_apple_objc_ap (),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000286 m_function_basename_index(),
287 m_function_fullname_index(),
288 m_function_method_index(),
289 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000290 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000291 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000292 m_type_index(),
293 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000294 m_indexed (false),
295 m_is_external_ast_source (false),
Greg Clayton97fbc342011-10-20 22:30:33 +0000296 m_using_apple_tables (false),
Greg Claytonc7f03b62012-01-12 04:33:28 +0000297 m_supports_DW_AT_APPLE_objc_complete_type (eLazyBoolCalculate),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000298 m_ranges(),
299 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000300{
301}
302
303SymbolFileDWARF::~SymbolFileDWARF()
304{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000305 if (m_is_external_ast_source)
Greg Claytone72dfb32012-02-24 01:59:29 +0000306 {
307 ModuleSP module_sp (m_obj_file->GetModule());
308 if (module_sp)
309 module_sp->GetClangASTContext().RemoveExternalSource ();
310 }
Greg Clayton6beaaa62011-01-17 03:46:26 +0000311}
312
313static const ConstString &
314GetDWARFMachOSegmentName ()
315{
316 static ConstString g_dwarf_section_name ("__DWARF");
317 return g_dwarf_section_name;
318}
319
Greg Claytone576ab22011-02-15 00:19:15 +0000320UniqueDWARFASTTypeMap &
321SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
322{
323 if (m_debug_map_symfile)
324 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
325 return m_unique_ast_type_map;
326}
327
Greg Clayton6beaaa62011-01-17 03:46:26 +0000328ClangASTContext &
329SymbolFileDWARF::GetClangASTContext ()
330{
331 if (m_debug_map_symfile)
332 return m_debug_map_symfile->GetClangASTContext ();
333
334 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
335 if (!m_is_external_ast_source)
336 {
337 m_is_external_ast_source = true;
338 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
339 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
340 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000341 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000342 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000343 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000344 ast.SetExternalSource (ast_source_ap);
345 }
346 return ast;
347}
348
349void
350SymbolFileDWARF::InitializeObject()
351{
352 // Install our external AST source callbacks so we can complete Clang types.
Greg Claytone72dfb32012-02-24 01:59:29 +0000353 ModuleSP module_sp (m_obj_file->GetModule());
354 if (module_sp)
Greg Clayton6beaaa62011-01-17 03:46:26 +0000355 {
356 const SectionList *section_list = m_obj_file->GetSectionList();
357
358 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
359
360 // Memory map the DWARF mach-o segment so we have everything mmap'ed
361 // to keep our heap memory usage down.
362 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000363 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000364 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000365 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000366 if (m_data_apple_names.GetByteSize() > 0)
367 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000368 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
369 if (m_apple_names_ap->IsValid())
370 m_using_apple_tables = true;
371 else
Greg Clayton7f995132011-10-04 22:41:51 +0000372 m_apple_names_ap.reset();
373 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000374 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000375 if (m_data_apple_types.GetByteSize() > 0)
376 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000377 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
378 if (m_apple_types_ap->IsValid())
379 m_using_apple_tables = true;
380 else
Greg Clayton7f995132011-10-04 22:41:51 +0000381 m_apple_types_ap.reset();
382 }
383
384 get_apple_namespaces_data();
385 if (m_data_apple_namespaces.GetByteSize() > 0)
386 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000387 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
388 if (m_apple_namespaces_ap->IsValid())
389 m_using_apple_tables = true;
390 else
Greg Clayton7f995132011-10-04 22:41:51 +0000391 m_apple_namespaces_ap.reset();
392 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000393
Greg Clayton5009f9d2011-10-27 17:55:14 +0000394 get_apple_objc_data();
395 if (m_data_apple_objc.GetByteSize() > 0)
396 {
397 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
398 if (m_apple_objc_ap->IsValid())
399 m_using_apple_tables = true;
400 else
401 m_apple_objc_ap.reset();
402 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
405bool
406SymbolFileDWARF::SupportedVersion(uint16_t version)
407{
408 return version == 2 || version == 3;
409}
410
411uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000412SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000413{
414 uint32_t abilities = 0;
415 if (m_obj_file != NULL)
416 {
417 const Section* section = NULL;
418 const SectionList *section_list = m_obj_file->GetSectionList();
419 if (section_list == NULL)
420 return 0;
421
422 uint64_t debug_abbrev_file_size = 0;
423 uint64_t debug_aranges_file_size = 0;
424 uint64_t debug_frame_file_size = 0;
425 uint64_t debug_info_file_size = 0;
426 uint64_t debug_line_file_size = 0;
427 uint64_t debug_loc_file_size = 0;
428 uint64_t debug_macinfo_file_size = 0;
429 uint64_t debug_pubnames_file_size = 0;
430 uint64_t debug_pubtypes_file_size = 0;
431 uint64_t debug_ranges_file_size = 0;
432 uint64_t debug_str_file_size = 0;
433
Greg Clayton6beaaa62011-01-17 03:46:26 +0000434 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000435
436 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000437 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000438
Greg Clayton4ceb9982010-07-21 22:54:26 +0000439 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000440 if (section != NULL)
441 {
442 debug_info_file_size = section->GetByteSize();
443
Greg Clayton4ceb9982010-07-21 22:54:26 +0000444 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000445 if (section)
446 debug_abbrev_file_size = section->GetByteSize();
447 else
448 m_flags.Set (flagsGotDebugAbbrevData);
449
Greg Clayton4ceb9982010-07-21 22:54:26 +0000450 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000451 if (section)
452 debug_aranges_file_size = section->GetByteSize();
453 else
454 m_flags.Set (flagsGotDebugArangesData);
455
Greg Clayton4ceb9982010-07-21 22:54:26 +0000456 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000457 if (section)
458 debug_frame_file_size = section->GetByteSize();
459 else
460 m_flags.Set (flagsGotDebugFrameData);
461
Greg Clayton4ceb9982010-07-21 22:54:26 +0000462 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000463 if (section)
464 debug_line_file_size = section->GetByteSize();
465 else
466 m_flags.Set (flagsGotDebugLineData);
467
Greg Clayton4ceb9982010-07-21 22:54:26 +0000468 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000469 if (section)
470 debug_loc_file_size = section->GetByteSize();
471 else
472 m_flags.Set (flagsGotDebugLocData);
473
Greg Clayton4ceb9982010-07-21 22:54:26 +0000474 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000475 if (section)
476 debug_macinfo_file_size = section->GetByteSize();
477 else
478 m_flags.Set (flagsGotDebugMacInfoData);
479
Greg Clayton4ceb9982010-07-21 22:54:26 +0000480 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000481 if (section)
482 debug_pubnames_file_size = section->GetByteSize();
483 else
484 m_flags.Set (flagsGotDebugPubNamesData);
485
Greg Clayton4ceb9982010-07-21 22:54:26 +0000486 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000487 if (section)
488 debug_pubtypes_file_size = section->GetByteSize();
489 else
490 m_flags.Set (flagsGotDebugPubTypesData);
491
Greg Clayton4ceb9982010-07-21 22:54:26 +0000492 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000493 if (section)
494 debug_ranges_file_size = section->GetByteSize();
495 else
496 m_flags.Set (flagsGotDebugRangesData);
497
Greg Clayton4ceb9982010-07-21 22:54:26 +0000498 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000499 if (section)
500 debug_str_file_size = section->GetByteSize();
501 else
502 m_flags.Set (flagsGotDebugStrData);
503 }
504
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;
510
511 if (debug_aranges_file_size > 0)
512 abilities |= AddressAcceleratorTable;
513
514 if (debug_pubnames_file_size > 0)
515 abilities |= FunctionAcceleratorTable;
516
517 if (debug_pubtypes_file_size > 0)
518 abilities |= TypeAcceleratorTable;
519
520 if (debug_macinfo_file_size > 0)
521 abilities |= MacroInformation;
522
523 if (debug_frame_file_size > 0)
524 abilities |= CallFrameInformation;
525 }
526 return abilities;
527}
528
529const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000530SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000531{
532 if (m_flags.IsClear (got_flag))
533 {
534 m_flags.Set (got_flag);
535 const SectionList *section_list = m_obj_file->GetSectionList();
536 if (section_list)
537 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000538 SectionSP section_sp (section_list->FindSectionByType(sect_type, true));
539 if (section_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000540 {
541 // See if we memory mapped the DWARF segment?
542 if (m_dwarf_data.GetByteSize())
543 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000544 data.SetData(m_dwarf_data, section_sp->GetOffset (), section_sp->GetByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 }
546 else
547 {
Greg Claytone72dfb32012-02-24 01:59:29 +0000548 if (m_obj_file->ReadSectionData (section_sp.get(), data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000549 data.Clear();
550 }
551 }
552 }
553 }
554 return data;
555}
556
557const DataExtractor&
558SymbolFileDWARF::get_debug_abbrev_data()
559{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000560 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000561}
562
563const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000564SymbolFileDWARF::get_debug_aranges_data()
565{
566 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
567}
568
569const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000570SymbolFileDWARF::get_debug_frame_data()
571{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000572 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000573}
574
575const DataExtractor&
576SymbolFileDWARF::get_debug_info_data()
577{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000578 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000579}
580
581const DataExtractor&
582SymbolFileDWARF::get_debug_line_data()
583{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000584 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000585}
586
587const DataExtractor&
588SymbolFileDWARF::get_debug_loc_data()
589{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000590 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000591}
592
593const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000594SymbolFileDWARF::get_debug_ranges_data()
595{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000596 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000597}
598
599const DataExtractor&
600SymbolFileDWARF::get_debug_str_data()
601{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000602 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603}
604
Greg Claytonf9eec202011-09-01 23:16:13 +0000605const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000606SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000607{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000608 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000609}
610
611const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000612SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000613{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000614 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000615}
616
Greg Clayton7f995132011-10-04 22:41:51 +0000617const DataExtractor&
618SymbolFileDWARF::get_apple_namespaces_data()
619{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000620 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
621}
622
623const DataExtractor&
624SymbolFileDWARF::get_apple_objc_data()
625{
626 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000627}
628
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000629
630DWARFDebugAbbrev*
631SymbolFileDWARF::DebugAbbrev()
632{
633 if (m_abbr.get() == NULL)
634 {
635 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
636 if (debug_abbrev_data.GetByteSize() > 0)
637 {
638 m_abbr.reset(new DWARFDebugAbbrev());
639 if (m_abbr.get())
640 m_abbr->Parse(debug_abbrev_data);
641 }
642 }
643 return m_abbr.get();
644}
645
646const DWARFDebugAbbrev*
647SymbolFileDWARF::DebugAbbrev() const
648{
649 return m_abbr.get();
650}
651
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000652
653DWARFDebugInfo*
654SymbolFileDWARF::DebugInfo()
655{
656 if (m_info.get() == NULL)
657 {
658 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
659 if (get_debug_info_data().GetByteSize() > 0)
660 {
661 m_info.reset(new DWARFDebugInfo());
662 if (m_info.get())
663 {
664 m_info->SetDwarfData(this);
665 }
666 }
667 }
668 return m_info.get();
669}
670
671const DWARFDebugInfo*
672SymbolFileDWARF::DebugInfo() const
673{
674 return m_info.get();
675}
676
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000677DWARFCompileUnit*
678SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
679{
680 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000681 if (info && UserIDMatches(cu_uid))
682 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000683 return NULL;
684}
685
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000686
687DWARFDebugRanges*
688SymbolFileDWARF::DebugRanges()
689{
690 if (m_ranges.get() == NULL)
691 {
692 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
693 if (get_debug_ranges_data().GetByteSize() > 0)
694 {
695 m_ranges.reset(new DWARFDebugRanges());
696 if (m_ranges.get())
697 m_ranges->Extract(this);
698 }
699 }
700 return m_ranges.get();
701}
702
703const DWARFDebugRanges*
704SymbolFileDWARF::DebugRanges() const
705{
706 return m_ranges.get();
707}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000708
709bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000710SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711{
Greg Clayton96d7d742010-11-10 23:42:09 +0000712 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000714 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715 if (cu_die)
716 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000717 const char * cu_die_name = cu_die->GetName(this, curr_cu);
718 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
Jim Ingham0f35ac22011-08-24 23:34:20 +0000719 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000720 if (cu_die_name)
721 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000722 FileSpec cu_file_spec;
723
Greg Clayton7bd65b92011-02-09 23:39:34 +0000724 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000726 // If we have a full path to the compile unit, we don't need to resolve
727 // the file. This can be expensive e.g. when the source files are NFS mounted.
728 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000729 }
730 else
731 {
732 std::string fullpath(cu_comp_dir);
733 if (*fullpath.rbegin() != '/')
734 fullpath += '/';
735 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000736 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000737 }
738
Greg Clayton81c22f62011-10-19 18:09:39 +0000739 compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
740 curr_cu,
741 cu_file_spec,
742 MakeUserID(curr_cu->GetOffset()),
743 cu_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 if (compile_unit_sp.get())
745 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000746 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000747 return true;
748 }
749 }
750 }
751 }
752 return false;
753}
754
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000755uint32_t
756SymbolFileDWARF::GetNumCompileUnits()
757{
758 DWARFDebugInfo* info = DebugInfo();
759 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000760 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000761 return 0;
762}
763
764CompUnitSP
765SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
766{
767 CompUnitSP comp_unit;
768 DWARFDebugInfo* info = DebugInfo();
769 if (info)
770 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000771 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
772 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000773 {
774 // Our symbol vendor shouldn't be asking us to add a compile unit that
775 // has already been added to it, which this DWARF plug-in knows as it
776 // stores the lldb compile unit (CompileUnit) pointer in each
777 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000778 assert(curr_cu->GetUserData() == NULL);
779 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000780 }
781 }
782 return comp_unit;
783}
784
785static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000786AddRangesToBlock (Block& block,
787 DWARFDebugRanges::RangeList& ranges,
788 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000789{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000790 const size_t num_ranges = ranges.GetSize();
791 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000792 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000793 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
794 const addr_t range_base = range.GetRangeBase();
795 assert (range_base >= block_base_addr);
796 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000797 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000798 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000799}
800
801
802Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000803SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000804{
805 DWARFDebugRanges::RangeList func_ranges;
806 const char *name = NULL;
807 const char *mangled = NULL;
808 int decl_file = 0;
809 int decl_line = 0;
810 int decl_column = 0;
811 int call_file = 0;
812 int call_line = 0;
813 int call_column = 0;
814 DWARFExpression frame_base;
815
Greg Claytonc93237c2010-10-01 20:48:32 +0000816 assert (die->Tag() == DW_TAG_subprogram);
817
818 if (die->Tag() != DW_TAG_subprogram)
819 return NULL;
820
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000821 if (die->GetDIENamesAndRanges (this,
822 dwarf_cu,
823 name,
824 mangled,
825 func_ranges,
826 decl_file,
827 decl_line,
828 decl_column,
829 call_file,
830 call_line,
831 call_column,
832 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000833 {
834 // Union of all ranges in the function DIE (if the function is discontiguous)
835 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000836 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
837 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000838 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
839 {
840 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
841 if (func_range.GetBaseAddress().IsValid())
842 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
843 }
844
845 if (func_range.GetBaseAddress().IsValid())
846 {
847 Mangled func_name;
848 if (mangled)
849 func_name.SetValue(mangled, true);
850 else if (name)
851 func_name.SetValue(name, false);
852
853 FunctionSP func_sp;
854 std::auto_ptr<Declaration> decl_ap;
855 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000856 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
857 decl_line,
858 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000859
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000860 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000861 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000862
863 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
864
865 func_range.GetBaseAddress().ResolveLinkedAddress();
866
Greg Clayton81c22f62011-10-19 18:09:39 +0000867 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000868 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000869 func_user_id, // UserID is the DIE offset
870 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000871 func_name,
872 func_type,
873 func_range)); // first address range
874
875 if (func_sp.get() != NULL)
876 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000877 if (frame_base.IsValid())
878 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000879 sc.comp_unit->AddFunction(func_sp);
880 return func_sp.get();
881 }
882 }
883 }
884 return NULL;
885}
886
887size_t
888SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
889{
890 assert (sc.comp_unit);
891 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000892 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000893 if (dwarf_cu)
894 {
895 DWARFDIECollection function_dies;
896 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
897 size_t func_idx;
898 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
899 {
900 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000901 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000902 {
903 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
904 ++functions_added;
905 }
906 }
907 //FixupTypes();
908 }
909 return functions_added;
910}
911
912bool
913SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
914{
915 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000916 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
917 assert (curr_cu);
918 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000919
920 if (cu_die)
921 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000922 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
923 dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000924
925 // All file indexes in DWARF are one based and a file of index zero is
926 // supposed to be the compile unit itself.
927 support_files.Append (*sc.comp_unit);
928
929 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
930 }
931 return false;
932}
933
934struct ParseDWARFLineTableCallbackInfo
935{
936 LineTable* line_table;
937 const SectionList *section_list;
938 lldb::addr_t prev_sect_file_base_addr;
939 lldb::addr_t curr_sect_file_base_addr;
940 bool is_oso_for_debug_map;
941 bool prev_in_final_executable;
942 DWARFDebugLine::Row prev_row;
943 SectionSP prev_section_sp;
944 SectionSP curr_section_sp;
945};
946
947//----------------------------------------------------------------------
948// ParseStatementTableCallback
949//----------------------------------------------------------------------
950static void
951ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
952{
953 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
954 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
955 {
956 // Just started parsing the line table
957 }
958 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
959 {
960 // Done parsing line table, nothing to do for the cleanup
961 }
962 else
963 {
964 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
965 // We have a new row, lets append it
966
967 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
968 {
969 info->prev_section_sp = info->curr_section_sp;
970 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
971 // If this is an end sequence entry, then we subtract one from the
972 // address to make sure we get an address that is not the end of
973 // a section.
974 if (state.end_sequence && state.address != 0)
975 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
976 else
977 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
978
979 if (info->curr_section_sp.get())
980 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
981 else
982 info->curr_sect_file_base_addr = 0;
983 }
984 if (info->curr_section_sp.get())
985 {
986 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
987 // Check for the fancy section magic to determine if we
988
989 if (info->is_oso_for_debug_map)
990 {
991 // When this is a debug map object file that contains DWARF
992 // (referenced from an N_OSO debug map nlist entry) we will have
993 // a file address in the file range for our section from the
994 // original .o file, and a load address in the executable that
995 // contains the debug map.
996 //
997 // If the sections for the file range and load range are
998 // different, we have a remapped section for the function and
999 // this address is resolved. If they are the same, then the
1000 // function for this address didn't make it into the final
1001 // executable.
1002 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
1003
1004 // If we are doing DWARF with debug map, then we need to carefully
1005 // add each line table entry as there may be gaps as functions
1006 // get moved around or removed.
1007 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1008 {
1009 if (info->prev_in_final_executable)
1010 {
1011 bool terminate_previous_entry = false;
1012 if (!curr_in_final_executable)
1013 {
1014 // Check for the case where the previous line entry
1015 // in a function made it into the final executable,
1016 // yet the current line entry falls in a function
1017 // that didn't. The line table used to be contiguous
1018 // through this address range but now it isn't. We
1019 // need to terminate the previous line entry so
1020 // that we can reconstruct the line range correctly
1021 // for it and to keep the line table correct.
1022 terminate_previous_entry = true;
1023 }
1024 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1025 {
1026 // Check for cases where the line entries used to be
1027 // contiguous address ranges, but now they aren't.
1028 // This can happen when order files specify the
1029 // ordering of the functions.
1030 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1031 Section *curr_sect = info->curr_section_sp.get();
1032 Section *prev_sect = info->prev_section_sp.get();
1033 assert (curr_sect->GetLinkedSection());
1034 assert (prev_sect->GetLinkedSection());
1035 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1036 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1037 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1038 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1039 if (object_file_addr_delta != linked_file_addr_delta)
1040 terminate_previous_entry = true;
1041 }
1042
1043 if (terminate_previous_entry)
1044 {
1045 line_table->InsertLineEntry (info->prev_section_sp,
1046 state.address - info->prev_sect_file_base_addr,
1047 info->prev_row.line,
1048 info->prev_row.column,
1049 info->prev_row.file,
1050 false, // is_stmt
1051 false, // basic_block
1052 false, // state.prologue_end
1053 false, // state.epilogue_begin
1054 true); // end_sequence);
1055 }
1056 }
1057 }
1058
1059 if (curr_in_final_executable)
1060 {
1061 line_table->InsertLineEntry (info->curr_section_sp,
1062 curr_line_section_offset,
1063 state.line,
1064 state.column,
1065 state.file,
1066 state.is_stmt,
1067 state.basic_block,
1068 state.prologue_end,
1069 state.epilogue_begin,
1070 state.end_sequence);
1071 info->prev_section_sp = info->curr_section_sp;
1072 }
1073 else
1074 {
1075 // If the current address didn't make it into the final
1076 // executable, the current section will be the __text
1077 // segment in the .o file, so we need to clear this so
1078 // we can catch the next function that did make it into
1079 // the final executable.
1080 info->prev_section_sp.reset();
1081 info->curr_section_sp.reset();
1082 }
1083
1084 info->prev_in_final_executable = curr_in_final_executable;
1085 }
1086 else
1087 {
1088 // We are not in an object file that contains DWARF for an
1089 // N_OSO, this is just a normal DWARF file. The DWARF spec
1090 // guarantees that the addresses will be in increasing order
1091 // so, since we store line tables in file address order, we
1092 // can always just append the line entry without needing to
1093 // search for the correct insertion point (we don't need to
1094 // use LineEntry::InsertLineEntry()).
1095 line_table->AppendLineEntry (info->curr_section_sp,
1096 curr_line_section_offset,
1097 state.line,
1098 state.column,
1099 state.file,
1100 state.is_stmt,
1101 state.basic_block,
1102 state.prologue_end,
1103 state.epilogue_begin,
1104 state.end_sequence);
1105 }
1106 }
1107
1108 info->prev_row = state;
1109 }
1110}
1111
1112bool
1113SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1114{
1115 assert (sc.comp_unit);
1116 if (sc.comp_unit->GetLineTable() != NULL)
1117 return true;
1118
1119 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1120 if (dwarf_cu)
1121 {
1122 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001123 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001124 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001125 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1126 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001127 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001128 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1129 if (line_table_ap.get())
1130 {
1131 ParseDWARFLineTableCallbackInfo info = {
1132 line_table_ap.get(),
1133 m_obj_file->GetSectionList(),
1134 0,
1135 0,
1136 m_debug_map_symfile != NULL,
1137 false,
1138 DWARFDebugLine::Row(),
1139 SectionSP(),
1140 SectionSP()
1141 };
1142 uint32_t offset = cu_line_offset;
1143 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1144 sc.comp_unit->SetLineTable(line_table_ap.release());
1145 return true;
1146 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001147 }
1148 }
1149 }
1150 return false;
1151}
1152
1153size_t
1154SymbolFileDWARF::ParseFunctionBlocks
1155(
1156 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001157 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001158 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001159 const DWARFDebugInfoEntry *die,
1160 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001161 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001162)
1163{
1164 size_t blocks_added = 0;
1165 while (die != NULL)
1166 {
1167 dw_tag_t tag = die->Tag();
1168
1169 switch (tag)
1170 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001171 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001172 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001173 case DW_TAG_lexical_block:
1174 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001175 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001176 if (tag == DW_TAG_subprogram)
1177 {
1178 // Skip any DW_TAG_subprogram DIEs that are inside
1179 // of a normal or inlined functions. These will be
1180 // parsed on their own as separate entities.
1181
1182 if (depth > 0)
1183 break;
1184
1185 block = parent_block;
1186 }
1187 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001188 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001189 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001190 parent_block->AddChild(block_sp);
1191 block = block_sp.get();
1192 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001193 DWARFDebugRanges::RangeList ranges;
1194 const char *name = NULL;
1195 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001196
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001197 int decl_file = 0;
1198 int decl_line = 0;
1199 int decl_column = 0;
1200 int call_file = 0;
1201 int call_line = 0;
1202 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001203 if (die->GetDIENamesAndRanges (this,
1204 dwarf_cu,
1205 name,
1206 mangled_name,
1207 ranges,
1208 decl_file, decl_line, decl_column,
1209 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001210 {
1211 if (tag == DW_TAG_subprogram)
1212 {
1213 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001214 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001215 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001216 else if (tag == DW_TAG_inlined_subroutine)
1217 {
1218 // We get called here for inlined subroutines in two ways.
1219 // The first time is when we are making the Function object
1220 // for this inlined concrete instance. Since we're creating a top level block at
1221 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1222 // adjust the containing address.
1223 // The second time is when we are parsing the blocks inside the function that contains
1224 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1225 // function the offset will be for that function.
1226 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1227 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001228 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001229 }
1230 }
1231
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001232 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001233
1234 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1235 {
1236 std::auto_ptr<Declaration> decl_ap;
1237 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001238 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1239 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240
1241 std::auto_ptr<Declaration> call_ap;
1242 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001243 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1244 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001245
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001246 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247 }
1248
1249 ++blocks_added;
1250
Greg Claytondd7feaf2011-08-12 17:54:33 +00001251 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001252 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001253 blocks_added += ParseFunctionBlocks (sc,
1254 block,
1255 dwarf_cu,
1256 die->GetFirstChild(),
1257 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001258 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 }
1260 }
1261 }
1262 break;
1263 default:
1264 break;
1265 }
1266
Greg Claytondd7feaf2011-08-12 17:54:33 +00001267 // Only parse siblings of the block if we are not at depth zero. A depth
1268 // of zero indicates we are currently parsing the top level
1269 // DW_TAG_subprogram DIE
1270
1271 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001272 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001273 else
1274 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001275 }
1276 return blocks_added;
1277}
1278
Greg Claytonf0705c82011-10-22 03:33:13 +00001279bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001280SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1281 const DWARFDebugInfoEntry *die,
1282 ClangASTContext::TemplateParameterInfos &template_param_infos)
1283{
1284 const dw_tag_t tag = die->Tag();
1285
1286 switch (tag)
1287 {
1288 case DW_TAG_template_type_parameter:
1289 case DW_TAG_template_value_parameter:
1290 {
1291 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1292
1293 DWARFDebugInfoEntry::Attributes attributes;
1294 const size_t num_attributes = die->GetAttributes (this,
1295 dwarf_cu,
1296 fixed_form_sizes,
1297 attributes);
1298 const char *name = NULL;
1299 Type *lldb_type = NULL;
1300 clang_type_t clang_type = NULL;
1301 uint64_t uval64 = 0;
1302 bool uval64_valid = false;
1303 if (num_attributes > 0)
1304 {
1305 DWARFFormValue form_value;
1306 for (size_t i=0; i<num_attributes; ++i)
1307 {
1308 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1309
1310 switch (attr)
1311 {
1312 case DW_AT_name:
1313 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1314 name = form_value.AsCString(&get_debug_str_data());
1315 break;
1316
1317 case DW_AT_type:
1318 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1319 {
1320 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1321 lldb_type = ResolveTypeUID(type_die_offset);
1322 if (lldb_type)
1323 clang_type = lldb_type->GetClangForwardType();
1324 }
1325 break;
1326
1327 case DW_AT_const_value:
1328 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1329 {
1330 uval64_valid = true;
1331 uval64 = form_value.Unsigned();
1332 }
1333 break;
1334 default:
1335 break;
1336 }
1337 }
1338
1339 if (name && lldb_type && clang_type)
1340 {
1341 bool is_signed = false;
1342 template_param_infos.names.push_back(name);
1343 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1344 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1345 {
1346 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1347 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1348 }
1349 else
1350 {
1351 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1352 }
1353 }
1354 else
1355 {
1356 return false;
1357 }
1358
1359 }
1360 }
1361 return true;
1362
1363 default:
1364 break;
1365 }
1366 return false;
1367}
1368
1369bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001370SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1371 const DWARFDebugInfoEntry *parent_die,
1372 ClangASTContext::TemplateParameterInfos &template_param_infos)
1373{
1374
1375 if (parent_die == NULL)
1376 return NULL;
1377
Greg Claytonf0705c82011-10-22 03:33:13 +00001378 Args template_parameter_names;
1379 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1380 die != NULL;
1381 die = die->GetSibling())
1382 {
1383 const dw_tag_t tag = die->Tag();
1384
1385 switch (tag)
1386 {
1387 case DW_TAG_template_type_parameter:
1388 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001389 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001390 break;
1391
1392 default:
1393 break;
1394 }
1395 }
1396 if (template_param_infos.args.empty())
1397 return false;
1398 return template_param_infos.args.size() == template_param_infos.names.size();
1399}
1400
1401clang::ClassTemplateDecl *
1402SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001403 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001404 const char *parent_name,
1405 int tag_decl_kind,
1406 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1407{
1408 if (template_param_infos.IsValid())
1409 {
1410 std::string template_basename(parent_name);
1411 template_basename.erase (template_basename.find('<'));
1412 ClangASTContext &ast = GetClangASTContext();
1413
1414 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001415 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001416 template_basename.c_str(),
1417 tag_decl_kind,
1418 template_param_infos);
1419 }
1420 return NULL;
1421}
1422
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001423size_t
1424SymbolFileDWARF::ParseChildMembers
1425(
1426 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001427 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001428 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001429 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001430 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001431 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1432 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001433 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001434 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001435 bool &is_a_class,
1436 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001437)
1438{
1439 if (parent_die == NULL)
1440 return 0;
1441
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001442 size_t count = 0;
1443 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001444 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001445 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001446
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001447 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1448 {
1449 dw_tag_t tag = die->Tag();
1450
1451 switch (tag)
1452 {
1453 case DW_TAG_member:
1454 {
1455 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001456 const size_t num_attributes = die->GetAttributes (this,
1457 dwarf_cu,
1458 fixed_form_sizes,
1459 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 if (num_attributes > 0)
1461 {
1462 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001463 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001464 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001465 const char *prop_name = NULL;
1466 const char *prop_getter_name = NULL;
1467 const char *prop_setter_name = NULL;
1468 uint32_t prop_attributes = 0;
1469
1470
Greg Clayton24739922010-10-13 03:15:28 +00001471 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001472 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001473 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001474 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001475 size_t byte_size = 0;
1476 size_t bit_offset = 0;
1477 size_t bit_size = 0;
1478 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001479 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001480 {
1481 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1482 DWARFFormValue form_value;
1483 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1484 {
1485 switch (attr)
1486 {
1487 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1488 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1489 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1490 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1491 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1492 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1493 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1494 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1495 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001496 if (form_value.BlockData())
1497 {
1498 Value initialValue(0);
1499 Value memberOffset(0);
1500 const DataExtractor& debug_info_data = get_debug_info_data();
1501 uint32_t block_length = form_value.Unsigned();
1502 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1503 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1504 NULL, // clang::ASTContext *
1505 NULL, // ClangExpressionVariableList *
1506 NULL, // ClangExpressionDeclMap *
1507 NULL, // RegisterContext *
1508 debug_info_data,
1509 block_offset,
1510 block_length,
1511 eRegisterKindDWARF,
1512 &initialValue,
1513 memberOffset,
1514 NULL))
1515 {
1516 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1517 }
1518 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519 break;
1520
Greg Clayton8cf05932010-07-22 18:30:50 +00001521 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton1b02c172012-03-14 21:00:47 +00001522 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001523 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1524 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1525 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1526 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1527
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001528 default:
Greg Clayton1b02c172012-03-14 21:00:47 +00001529 case DW_AT_declaration:
1530 case DW_AT_description:
1531 case DW_AT_mutable:
1532 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001533 case DW_AT_sibling:
1534 break;
1535 }
1536 }
1537 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001538
Greg Clayton44953932011-10-25 01:25:35 +00001539 // Clang has a DWARF generation bug where sometimes it
1540 // represents fields that are references with bad byte size
1541 // and bit size/offset information such as:
1542 //
1543 // DW_AT_byte_size( 0x00 )
1544 // DW_AT_bit_size( 0x40 )
1545 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1546 //
1547 // So check the bit offset to make sure it is sane, and if
1548 // the values are not sane, remove them. If we don't do this
1549 // then we will end up with a crash if we try to use this
1550 // type in an expression when clang becomes unhappy with its
1551 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001552
Greg Clayton44953932011-10-25 01:25:35 +00001553 if (bit_offset > 128)
1554 {
1555 bit_size = 0;
1556 bit_offset = 0;
1557 }
1558
1559 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001560 if (class_language == eLanguageTypeObjC ||
1561 class_language == eLanguageTypeObjC_plus_plus)
1562 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001563
1564 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1565 {
1566 // Not all compilers will mark the vtable pointer
1567 // member as artificial (llvm-gcc). We can't have
1568 // the virtual members in our classes otherwise it
1569 // throws off all child offsets since we end up
1570 // having and extra pointer sized member in our
1571 // class layouts.
1572 is_artificial = true;
1573 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001574
Greg Clayton24739922010-10-13 03:15:28 +00001575 if (is_artificial == false)
1576 {
1577 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001578 clang::FieldDecl *field_decl = NULL;
Greg Claytond16e1e52011-07-12 17:06:17 +00001579 if (member_type)
1580 {
1581 if (accessibility == eAccessNone)
1582 accessibility = default_accessibility;
1583 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001584
Jim Inghame3ae82a2011-11-12 01:36:43 +00001585 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
Greg Clayton42ce2f32011-12-12 21:50:19 +00001586 name,
1587 member_type->GetClangLayoutType(),
1588 accessibility,
1589 bit_size);
Greg Claytond16e1e52011-07-12 17:06:17 +00001590 }
1591 else
1592 {
1593 if (name)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001594 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1595 MakeUserID(die->GetOffset()),
1596 name,
1597 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001598 else
Greg Claytone38a5ed2012-01-05 03:57:59 +00001599 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1600 MakeUserID(die->GetOffset()),
1601 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001602 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001603
Sean Callanan5b26f272012-02-04 08:49:35 +00001604 if (member_byte_offset != UINT32_MAX || bit_size != 0)
Greg Claytoncaab74e2012-01-28 00:48:57 +00001605 {
Sean Callanan5b26f272012-02-04 08:49:35 +00001606 /////////////////////////////////////////////////////////////
1607 // How to locate a field given the DWARF debug information
1608 //
1609 // AT_byte_size indicates the size of the word in which the
1610 // bit offset must be interpreted.
1611 //
1612 // AT_data_member_location indicates the byte offset of the
1613 // word from the base address of the structure.
1614 //
1615 // AT_bit_offset indicates how many bits into the word
1616 // (according to the host endianness) the low-order bit of
1617 // the field starts. AT_bit_offset can be negative.
1618 //
1619 // AT_bit_size indicates the size of the field in bits.
1620 /////////////////////////////////////////////////////////////
1621
1622 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1623
1624 uint64_t total_bit_offset = 0;
1625
1626 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1627
1628 if (object_endian == eByteOrderLittle)
1629 {
1630 total_bit_offset += byte_size * 8;
1631 total_bit_offset -= (bit_offset + bit_size);
1632 }
1633 else
1634 {
1635 total_bit_offset += bit_offset;
1636 }
1637
1638 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
Greg Claytoncaab74e2012-01-28 00:48:57 +00001639 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001640 if (prop_name != NULL)
1641 {
1642
1643 clang::ObjCIvarDecl *ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1644 assert (ivar_decl != NULL);
1645
1646
1647 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1648 prop_name,
1649 0,
1650 ivar_decl,
1651 prop_setter_name,
1652 prop_getter_name,
1653 prop_attributes);
1654 }
Greg Clayton24739922010-10-13 03:15:28 +00001655 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001656 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001657 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001658 }
1659 break;
1660
1661 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001662 // Let the type parsing code handle this one for us.
1663 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664 break;
1665
1666 case DW_TAG_inheritance:
1667 {
1668 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001669 if (default_accessibility == eAccessNone)
1670 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001671 // TODO: implement DW_TAG_inheritance type parsing
1672 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001673 const size_t num_attributes = die->GetAttributes (this,
1674 dwarf_cu,
1675 fixed_form_sizes,
1676 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001677 if (num_attributes > 0)
1678 {
1679 Declaration decl;
1680 DWARFExpression location;
1681 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001682 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001683 bool is_virtual = false;
1684 bool is_base_of_class = true;
1685 off_t member_offset = 0;
1686 uint32_t i;
1687 for (i=0; i<num_attributes; ++i)
1688 {
1689 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1690 DWARFFormValue form_value;
1691 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1692 {
1693 switch (attr)
1694 {
1695 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1696 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1697 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1698 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1699 case DW_AT_data_member_location:
1700 if (form_value.BlockData())
1701 {
1702 Value initialValue(0);
1703 Value memberOffset(0);
1704 const DataExtractor& debug_info_data = get_debug_info_data();
1705 uint32_t block_length = form_value.Unsigned();
1706 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001707 if (DWARFExpression::Evaluate (NULL,
1708 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001709 NULL,
1710 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001711 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001712 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001713 block_offset,
1714 block_length,
1715 eRegisterKindDWARF,
1716 &initialValue,
1717 memberOffset,
1718 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001719 {
1720 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1721 }
1722 }
1723 break;
1724
1725 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001726 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727 break;
1728
1729 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1730 default:
1731 case DW_AT_sibling:
1732 break;
1733 }
1734 }
1735 }
1736
Greg Clayton526e5af2010-11-13 03:52:47 +00001737 Type *base_class_type = ResolveTypeUID(encoding_uid);
1738 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001739
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001740 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1741 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001742 if (class_language == eLanguageTypeObjC)
1743 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001744 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001745 }
1746 else
1747 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001748 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001749 accessibility,
1750 is_virtual,
1751 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001752 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001753 }
1754 }
1755 break;
1756
1757 default:
1758 break;
1759 }
1760 }
1761 return count;
1762}
1763
1764
1765clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001766SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001767{
1768 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001769 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001770 {
1771 DWARFCompileUnitSP cu_sp;
1772 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1773 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001774 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001775 }
1776 return NULL;
1777}
1778
1779clang::DeclContext*
1780SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1781{
Greg Clayton81c22f62011-10-19 18:09:39 +00001782 if (UserIDMatches(type_uid))
1783 return GetClangDeclContextForDIEOffset (sc, type_uid);
1784 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001785}
1786
1787Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001788SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001789{
Greg Clayton81c22f62011-10-19 18:09:39 +00001790 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001791 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001792 DWARFDebugInfo* debug_info = DebugInfo();
1793 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001794 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001795 DWARFCompileUnitSP cu_sp;
1796 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001797 const bool assert_not_being_parsed = true;
1798 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001799 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001800 }
1801 return NULL;
1802}
1803
Greg Claytoncab36a32011-12-08 05:16:30 +00001804Type*
1805SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001806{
Greg Claytoncab36a32011-12-08 05:16:30 +00001807 if (die != NULL)
1808 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001809 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1810 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001811 GetObjectFile()->GetModule()->LogMessage (log.get(),
1812 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1813 die->GetOffset(),
1814 DW_TAG_value_to_name(die->Tag()),
1815 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001816
Greg Claytoncab36a32011-12-08 05:16:30 +00001817 // We might be coming in in the middle of a type tree (a class
1818 // withing a class, an enum within a class), so parse any needed
1819 // parent DIEs before we get to this one...
1820 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1821 switch (decl_ctx_die->Tag())
1822 {
1823 case DW_TAG_structure_type:
1824 case DW_TAG_union_type:
1825 case DW_TAG_class_type:
1826 {
1827 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001828 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001829 GetObjectFile()->GetModule()->LogMessage (log.get(),
1830 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1831 die->GetOffset(),
1832 DW_TAG_value_to_name(die->Tag()),
1833 die->GetName(this, cu),
1834 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001835
Greg Claytoncab36a32011-12-08 05:16:30 +00001836 Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
Sean Callanan5b26f272012-02-04 08:49:35 +00001837 if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
Greg Clayton3bffb082011-12-10 02:15:28 +00001838 {
1839 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001840 GetObjectFile()->GetModule()->LogMessage (log.get(),
1841 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1842 die->GetOffset(),
1843 DW_TAG_value_to_name(die->Tag()),
1844 die->GetName(this, cu),
1845 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001846 // Ask the type to complete itself if it already hasn't since if we
1847 // want a function (method or static) from a class, the class must
1848 // create itself and add it's own methods and class functions.
1849 if (parent_type)
1850 parent_type->GetClangFullType();
1851 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001852 }
1853 break;
1854
1855 default:
1856 break;
1857 }
1858 return ResolveType (cu, die);
1859 }
1860 return NULL;
1861}
1862
Greg Clayton6beaaa62011-01-17 03:46:26 +00001863// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1864// SymbolFileDWARF objects to detect if this DWARF file is the one that
1865// can resolve a clang_type.
1866bool
1867SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1868{
1869 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1870 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1871 return die != NULL;
1872}
1873
1874
Greg Clayton1be10fc2010-09-29 01:12:09 +00001875lldb::clang_type_t
1876SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1877{
1878 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001879 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1880 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001881 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001882 {
1883 // We have already resolved this type...
1884 return clang_type;
1885 }
1886 // Once we start resolving this type, remove it from the forward declaration
1887 // map in case anyone child members or other types require this type to get resolved.
1888 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1889 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001890 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001891
Greg Clayton1be10fc2010-09-29 01:12:09 +00001892
Greg Clayton85ae2e12011-10-18 23:36:41 +00001893 // Disable external storage for this type so we don't get anymore
1894 // clang::ExternalASTSource queries for this type.
1895 ClangASTContext::SetHasExternalStorage (clang_type, false);
1896
Greg Clayton450e3f32010-10-12 02:24:53 +00001897 DWARFDebugInfo* debug_info = DebugInfo();
1898
Greg Clayton96d7d742010-11-10 23:42:09 +00001899 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001900 Type *type = m_die_to_type.lookup (die);
1901
1902 const dw_tag_t tag = die->Tag();
1903
Greg Clayton3bffb082011-12-10 02:15:28 +00001904 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1905 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001906 GetObjectFile()->GetModule()->LogMessage (log.get(),
1907 "0x%8.8llx: %s '%s' resolving forward declaration...\n",
1908 MakeUserID(die->GetOffset()),
1909 DW_TAG_value_to_name(tag),
1910 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001911 assert (clang_type);
1912 DWARFDebugInfoEntry::Attributes attributes;
1913
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001914 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001915
1916 switch (tag)
1917 {
1918 case DW_TAG_structure_type:
1919 case DW_TAG_union_type:
1920 case DW_TAG_class_type:
Greg Claytonc93237c2010-10-01 20:48:32 +00001921 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001922 LayoutInfo layout_info;
Sean Callanan77a1fd32012-02-27 20:07:01 +00001923
1924 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001925 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00001926 if (die->HasChildren())
Greg Claytonc93237c2010-10-01 20:48:32 +00001927 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001928
Sean Callanan77a1fd32012-02-27 20:07:01 +00001929 LanguageType class_language = eLanguageTypeUnknown;
1930 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1931 if (is_objc_class)
1932 class_language = eLanguageTypeObjC;
1933
1934 int tag_decl_kind = -1;
1935 AccessType default_accessibility = eAccessNone;
1936 if (tag == DW_TAG_structure_type)
1937 {
1938 tag_decl_kind = clang::TTK_Struct;
1939 default_accessibility = eAccessPublic;
1940 }
1941 else if (tag == DW_TAG_union_type)
1942 {
1943 tag_decl_kind = clang::TTK_Union;
1944 default_accessibility = eAccessPublic;
1945 }
1946 else if (tag == DW_TAG_class_type)
1947 {
1948 tag_decl_kind = clang::TTK_Class;
1949 default_accessibility = eAccessPrivate;
1950 }
1951
1952 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1953 std::vector<clang::CXXBaseSpecifier *> base_classes;
1954 std::vector<int> member_accessibilities;
1955 bool is_a_class = false;
1956 // Parse members and base classes first
1957 DWARFDIECollection member_function_dies;
1958
1959 ParseChildMembers (sc,
1960 curr_cu,
1961 die,
1962 clang_type,
1963 class_language,
1964 base_classes,
1965 member_accessibilities,
1966 member_function_dies,
1967 default_accessibility,
1968 is_a_class,
1969 layout_info);
1970
1971 // Now parse any methods if there were any...
1972 size_t num_functions = member_function_dies.Size();
1973 if (num_functions > 0)
1974 {
1975 for (size_t i=0; i<num_functions; ++i)
Greg Claytond4a2b372011-09-12 23:21:58 +00001976 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00001977 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytoncaab74e2012-01-28 00:48:57 +00001978 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00001979 }
1980
1981 if (class_language == eLanguageTypeObjC)
1982 {
1983 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type));
1984 if (!class_str.empty())
Greg Claytoncaab74e2012-01-28 00:48:57 +00001985 {
Greg Clayton450e3f32010-10-12 02:24:53 +00001986
Sean Callanan77a1fd32012-02-27 20:07:01 +00001987 DIEArray method_die_offsets;
1988 if (m_using_apple_tables)
Greg Clayton95d87902011-11-11 03:16:25 +00001989 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00001990 if (m_apple_objc_ap.get())
1991 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
1992 }
1993 else
1994 {
1995 if (!m_indexed)
1996 Index ();
Greg Claytoncaab74e2012-01-28 00:48:57 +00001997
Sean Callanan77a1fd32012-02-27 20:07:01 +00001998 ConstString class_name (class_str.c_str());
1999 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
2000 }
2001
2002 if (!method_die_offsets.empty())
2003 {
2004 DWARFDebugInfo* debug_info = DebugInfo();
2005
2006 DWARFCompileUnit* method_cu = NULL;
2007 const size_t num_matches = method_die_offsets.size();
2008 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002009 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002010 const dw_offset_t die_offset = method_die_offsets[i];
2011 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2012
2013 if (method_die)
2014 ResolveType (method_cu, method_die);
2015 else
Greg Claytoncaab74e2012-01-28 00:48:57 +00002016 {
Sean Callanan77a1fd32012-02-27 20:07:01 +00002017 if (m_using_apple_tables)
2018 {
2019 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2020 die_offset, class_str.c_str());
2021 }
2022 }
2023 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002024 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002025 }
2026 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002027
2028 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2029 // need to tell the clang type it is actually a class.
2030 if (class_language != eLanguageTypeObjC)
2031 {
2032 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2033 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2034 }
2035
2036 // Since DW_TAG_structure_type gets used for both classes
2037 // and structures, we may need to set any DW_TAG_member
2038 // fields to have a "private" access if none was specified.
2039 // When we parsed the child members we tracked that actual
2040 // accessibility value for each DW_TAG_member in the
2041 // "member_accessibilities" array. If the value for the
2042 // member is zero, then it was set to the "default_accessibility"
2043 // which for structs was "public". Below we correct this
2044 // by setting any fields to "private" that weren't correctly
2045 // set.
2046 if (is_a_class && !member_accessibilities.empty())
2047 {
2048 // This is a class and all members that didn't have
2049 // their access specified are private.
2050 ast.SetDefaultAccessForRecordFields (clang_type,
2051 eAccessPrivate,
2052 &member_accessibilities.front(),
2053 member_accessibilities.size());
2054 }
2055
2056 if (!base_classes.empty())
2057 {
2058 ast.SetBaseClassesForClassType (clang_type,
2059 &base_classes.front(),
2060 base_classes.size());
2061
2062 // Clang will copy each CXXBaseSpecifier in "base_classes"
2063 // so we have to free them all.
2064 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2065 base_classes.size());
2066 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002067 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002068 }
Sean Callanane8c0cfb2012-03-02 01:03:45 +00002069
2070 ast.BuildIndirectFields (clang_type);
2071
Sean Callanan77a1fd32012-02-27 20:07:01 +00002072 ast.CompleteTagDeclarationDefinition (clang_type);
Sean Callanan5b26f272012-02-04 08:49:35 +00002073
Greg Claytoncaab74e2012-01-28 00:48:57 +00002074 if (!layout_info.field_offsets.empty())
2075 {
2076 if (type)
2077 layout_info.bit_size = type->GetByteSize() * 8;
2078 if (layout_info.bit_size == 0)
2079 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_byte_size, 0) * 8;
2080 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2081 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2082 if (record_type)
2083 {
2084 const clang::RecordDecl *record_decl = record_type->getDecl();
2085
2086 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002087 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002088 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002089 "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 +00002090 clang_type,
2091 record_decl,
2092 layout_info.bit_size,
2093 layout_info.alignment,
2094 (uint32_t)layout_info.field_offsets.size());
Sean Callanan77a1fd32012-02-27 20:07:01 +00002095
Greg Clayton821ac6d2012-01-28 02:22:27 +00002096 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2097 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2098 {
2099 GetObjectFile()->GetModule()->LogMessage (log.get(),
2100 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2101 clang_type,
2102 (uint32_t)pos->second,
2103 pos->first->getNameAsString().c_str());
2104 }
2105 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002106 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2107 }
2108 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002109 }
Sean Callanan77a1fd32012-02-27 20:07:01 +00002110
Greg Claytonc93237c2010-10-01 20:48:32 +00002111 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002112
2113 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002114 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002115 if (die->HasChildren())
2116 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002117 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
2118 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002119 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002120 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002121 return clang_type;
2122
2123 default:
2124 assert(false && "not a forward clang type decl!");
2125 break;
2126 }
2127 return NULL;
2128}
2129
Greg Claytonc685f8e2010-09-15 04:15:46 +00002130Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00002131SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002132{
2133 if (type_die != NULL)
2134 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002135 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002136
Greg Claytonc685f8e2010-09-15 04:15:46 +00002137 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002138 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002139
Greg Clayton24739922010-10-13 03:15:28 +00002140 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002141 {
2142 if (type != DIE_IS_BEING_PARSED)
2143 return type;
2144
2145 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
2146 type_die->GetOffset(),
2147 DW_TAG_value_to_name(type_die->Tag()),
2148 type_die->GetName(this, curr_cu));
2149
2150 }
2151 else
2152 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002153 }
2154 return NULL;
2155}
2156
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002157CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00002158SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002159{
2160 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002161 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002162 {
2163 // The symbol vendor doesn't know about this compile unit, we
2164 // need to parse and add it to the symbol vendor object.
2165 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00002166 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002167 if (dc_cu.get())
2168 {
2169 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00002170 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00002171 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002172
2173 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00002174
2175 if (m_debug_map_symfile)
2176 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002177 }
2178 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002179 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002180}
2181
2182bool
Greg Clayton96d7d742010-11-10 23:42:09 +00002183SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002184{
2185 sc.Clear();
2186 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002187 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002188
Greg Clayton81c22f62011-10-19 18:09:39 +00002189 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002190 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002191 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002192
2193 if (sc.function)
2194 {
Greg Claytone72dfb32012-02-24 01:59:29 +00002195 sc.module_sp = sc.function->CalculateSymbolContextModule();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002196 return true;
2197 }
2198
2199 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002200}
2201
2202uint32_t
2203SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2204{
2205 Timer scoped_timer(__PRETTY_FUNCTION__,
2206 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
Greg Claytone72dfb32012-02-24 01:59:29 +00002207 so_addr.GetSection().get(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002208 so_addr.GetOffset(),
2209 resolve_scope);
2210 uint32_t resolved = 0;
2211 if (resolve_scope & ( eSymbolContextCompUnit |
2212 eSymbolContextFunction |
2213 eSymbolContextBlock |
2214 eSymbolContextLineEntry))
2215 {
2216 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2217
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002219 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002220 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002221 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002222 if (cu_offset != DW_INVALID_OFFSET)
2223 {
2224 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002225 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2226 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002227 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002228 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002229 assert(sc.comp_unit != NULL);
2230 resolved |= eSymbolContextCompUnit;
2231
2232 if (resolve_scope & eSymbolContextLineEntry)
2233 {
2234 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002235 if (line_table != NULL)
2236 {
2237 if (so_addr.IsLinkedAddress())
2238 {
2239 Address linked_addr (so_addr);
2240 linked_addr.ResolveLinkedAddress();
2241 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2242 {
2243 resolved |= eSymbolContextLineEntry;
2244 }
2245 }
2246 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2247 {
2248 resolved |= eSymbolContextLineEntry;
2249 }
2250 }
2251 }
2252
2253 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2254 {
2255 DWARFDebugInfoEntry *function_die = NULL;
2256 DWARFDebugInfoEntry *block_die = NULL;
2257 if (resolve_scope & eSymbolContextBlock)
2258 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002259 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 }
2261 else
2262 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002263 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002264 }
2265
2266 if (function_die != NULL)
2267 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002268 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002269 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002270 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271 }
2272
2273 if (sc.function != NULL)
2274 {
2275 resolved |= eSymbolContextFunction;
2276
2277 if (resolve_scope & eSymbolContextBlock)
2278 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002279 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002280
2281 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002282 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002283 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002284 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002285 if (sc.block)
2286 resolved |= eSymbolContextBlock;
2287 }
2288 }
2289 }
2290 }
2291 }
2292 }
2293 }
2294 return resolved;
2295}
2296
2297
2298
2299uint32_t
2300SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2301{
2302 const uint32_t prev_size = sc_list.GetSize();
2303 if (resolve_scope & eSymbolContextCompUnit)
2304 {
2305 DWARFDebugInfo* debug_info = DebugInfo();
2306 if (debug_info)
2307 {
2308 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002309 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002310
Greg Clayton96d7d742010-11-10 23:42:09 +00002311 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002312 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002313 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002314 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2315 if (check_inlines || file_spec_matches_cu_file_spec)
2316 {
2317 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00002318 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002319 assert(sc.comp_unit != NULL);
2320
2321 uint32_t file_idx = UINT32_MAX;
2322
2323 // If we are looking for inline functions only and we don't
2324 // find it in the support files, we are done.
2325 if (check_inlines)
2326 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002327 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002328 if (file_idx == UINT32_MAX)
2329 continue;
2330 }
2331
2332 if (line != 0)
2333 {
2334 LineTable *line_table = sc.comp_unit->GetLineTable();
2335
2336 if (line_table != NULL && line != 0)
2337 {
2338 // We will have already looked up the file index if
2339 // we are searching for inline entries.
2340 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002341 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002342
2343 if (file_idx != UINT32_MAX)
2344 {
2345 uint32_t found_line;
2346 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2347 found_line = sc.line_entry.line;
2348
Greg Clayton016a95e2010-09-14 02:20:48 +00002349 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002350 {
2351 sc.function = NULL;
2352 sc.block = NULL;
2353 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2354 {
2355 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2356 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2357 {
2358 DWARFDebugInfoEntry *function_die = NULL;
2359 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00002360 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002361
2362 if (function_die != NULL)
2363 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002364 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002365 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002366 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002367 }
2368
2369 if (sc.function != NULL)
2370 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002371 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002372
2373 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002374 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002375 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002376 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002377 }
2378 }
2379 }
2380
2381 sc_list.Append(sc);
2382 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2383 }
2384 }
2385 }
2386 else if (file_spec_matches_cu_file_spec && !check_inlines)
2387 {
2388 // only append the context if we aren't looking for inline call sites
2389 // by file and line and if the file spec matches that of the compile unit
2390 sc_list.Append(sc);
2391 }
2392 }
2393 else if (file_spec_matches_cu_file_spec && !check_inlines)
2394 {
2395 // only append the context if we aren't looking for inline call sites
2396 // by file and line and if the file spec matches that of the compile unit
2397 sc_list.Append(sc);
2398 }
2399
2400 if (!check_inlines)
2401 break;
2402 }
2403 }
2404 }
2405 }
2406 return sc_list.GetSize() - prev_size;
2407}
2408
2409void
2410SymbolFileDWARF::Index ()
2411{
2412 if (m_indexed)
2413 return;
2414 m_indexed = true;
2415 Timer scoped_timer (__PRETTY_FUNCTION__,
2416 "SymbolFileDWARF::Index (%s)",
2417 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2418
2419 DWARFDebugInfo* debug_info = DebugInfo();
2420 if (debug_info)
2421 {
2422 uint32_t cu_idx = 0;
2423 const uint32_t num_compile_units = GetNumCompileUnits();
2424 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2425 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002426 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002427
Greg Clayton96d7d742010-11-10 23:42:09 +00002428 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002429
Greg Clayton96d7d742010-11-10 23:42:09 +00002430 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00002431 m_function_basename_index,
2432 m_function_fullname_index,
2433 m_function_method_index,
2434 m_function_selector_index,
2435 m_objc_class_selectors_index,
2436 m_global_index,
2437 m_type_index,
Greg Claytond4a2b372011-09-12 23:21:58 +00002438 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002439
2440 // Keep memory down by clearing DIEs if this generate function
2441 // caused them to be parsed
2442 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00002443 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002444 }
2445
Greg Claytond4a2b372011-09-12 23:21:58 +00002446 m_function_basename_index.Finalize();
2447 m_function_fullname_index.Finalize();
2448 m_function_method_index.Finalize();
2449 m_function_selector_index.Finalize();
2450 m_objc_class_selectors_index.Finalize();
2451 m_global_index.Finalize();
2452 m_type_index.Finalize();
2453 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002454
Greg Clayton24739922010-10-13 03:15:28 +00002455#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002456 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002457 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002458 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2459 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002460 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2461 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2462 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2463 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2464 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2465 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002466 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002467 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002468#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002469 }
2470}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002471
2472bool
2473SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2474{
2475 if (namespace_decl == NULL)
2476 {
2477 // Invalid namespace decl which means we aren't matching only things
2478 // in this symbol file, so return true to indicate it matches this
2479 // symbol file.
2480 return true;
2481 }
2482
2483 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2484
2485 if (namespace_ast == NULL)
2486 return true; // No AST in the "namespace_decl", return true since it
2487 // could then match any symbol file, including this one
2488
2489 if (namespace_ast == GetClangASTContext().getASTContext())
2490 return true; // The ASTs match, return true
2491
2492 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002493 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2494
2495 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002496 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002497
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002498 return false;
2499}
2500
Greg Clayton2506a7a2011-10-12 23:34:26 +00002501bool
2502SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2503 DWARFCompileUnit* cu,
2504 const DWARFDebugInfoEntry* die)
2505{
2506 // No namespace specified, so the answesr i
2507 if (namespace_decl == NULL)
2508 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002509
2510 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002511
Greg Clayton2506a7a2011-10-12 23:34:26 +00002512 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2513 if (decl_ctx_die)
2514 {
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002515
Greg Clayton2506a7a2011-10-12 23:34:26 +00002516 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
2517 if (clang_namespace_decl)
2518 {
2519 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002520 {
2521 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002522 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002523 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002524 }
2525
Greg Clayton2506a7a2011-10-12 23:34:26 +00002526 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
2527
2528 if (pos == m_decl_ctx_to_die.end())
Sean Callananebe60672011-10-13 21:50:33 +00002529 {
2530 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002531 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match in a namespace, but its parent is not the requested namespace");
Sean Callananebe60672011-10-13 21:50:33 +00002532
Greg Clayton2506a7a2011-10-12 23:34:26 +00002533 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002534 }
Greg Clayton2506a7a2011-10-12 23:34:26 +00002535
Greg Claytoncb5860a2011-10-13 23:49:28 +00002536 return pos->second.count (decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002537 }
2538 else
2539 {
2540 // We have a namespace_decl that was not NULL but it contained
2541 // a NULL "clang::NamespaceDecl", so this means the global namespace
2542 // So as long the the contained decl context DIE isn't a namespace
2543 // we should be ok.
2544 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2545 return true;
2546 }
2547 }
Sean Callananebe60672011-10-13 21:50:33 +00002548
2549 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002550 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002551
Greg Clayton2506a7a2011-10-12 23:34:26 +00002552 return false;
2553}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002554uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002555SymbolFileDWARF::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 +00002556{
Greg Clayton21f2a492011-10-06 00:09:08 +00002557 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2558
2559 if (log)
2560 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002561 GetObjectFile()->GetModule()->LogMessage (log.get(),
2562 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2563 name.GetCString(),
2564 namespace_decl,
2565 append,
2566 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002567 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002568
2569 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2570 return 0;
2571
Greg Claytonc685f8e2010-09-15 04:15:46 +00002572 DWARFDebugInfo* info = DebugInfo();
2573 if (info == NULL)
2574 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002575
2576 // If we aren't appending the results to this list, then clear the list
2577 if (!append)
2578 variables.Clear();
2579
2580 // Remember how many variables are in the list before we search in case
2581 // we are appending the results to a variable list.
2582 const uint32_t original_size = variables.GetSize();
2583
Greg Claytond4a2b372011-09-12 23:21:58 +00002584 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002585
Greg Clayton97fbc342011-10-20 22:30:33 +00002586 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002587 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002588 if (m_apple_names_ap.get())
2589 {
2590 const char *name_cstr = name.GetCString();
2591 const char *base_name_start;
2592 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002593
Greg Clayton97fbc342011-10-20 22:30:33 +00002594 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2595 base_name_start = name_cstr;
2596
2597 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2598 }
Greg Clayton7f995132011-10-04 22:41:51 +00002599 }
2600 else
2601 {
2602 // Index the DWARF if we haven't already
2603 if (!m_indexed)
2604 Index ();
2605
2606 m_global_index.Find (name, die_offsets);
2607 }
2608
2609 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002610 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002611 {
Greg Clayton7f995132011-10-04 22:41:51 +00002612 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002613 sc.module_sp = m_obj_file->GetModule();
Greg Clayton7f995132011-10-04 22:41:51 +00002614 assert (sc.module_sp);
2615
Greg Claytond4a2b372011-09-12 23:21:58 +00002616 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002617 DWARFCompileUnit* dwarf_cu = NULL;
2618 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002619 for (size_t i=0; i<num_matches; ++i)
2620 {
2621 const dw_offset_t die_offset = die_offsets[i];
2622 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002623
Greg Clayton95d87902011-11-11 03:16:25 +00002624 if (die)
2625 {
2626 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2627 assert(sc.comp_unit != NULL);
2628
2629 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2630 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002631
Greg Clayton95d87902011-11-11 03:16:25 +00002632 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002633
Greg Clayton95d87902011-11-11 03:16:25 +00002634 if (variables.GetSize() - original_size >= max_matches)
2635 break;
2636 }
2637 else
2638 {
2639 if (m_using_apple_tables)
2640 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002641 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2642 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002643 }
2644 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002645 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002646 }
2647
2648 // Return the number of variable that were appended to the list
2649 return variables.GetSize() - original_size;
2650}
2651
2652uint32_t
2653SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2654{
Greg Clayton21f2a492011-10-06 00:09:08 +00002655 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2656
2657 if (log)
2658 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002659 GetObjectFile()->GetModule()->LogMessage (log.get(),
2660 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2661 regex.GetText(),
2662 append,
2663 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002664 }
2665
Greg Claytonc685f8e2010-09-15 04:15:46 +00002666 DWARFDebugInfo* info = DebugInfo();
2667 if (info == NULL)
2668 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002669
2670 // If we aren't appending the results to this list, then clear the list
2671 if (!append)
2672 variables.Clear();
2673
2674 // Remember how many variables are in the list before we search in case
2675 // we are appending the results to a variable list.
2676 const uint32_t original_size = variables.GetSize();
2677
Greg Clayton7f995132011-10-04 22:41:51 +00002678 DIEArray die_offsets;
2679
Greg Clayton97fbc342011-10-20 22:30:33 +00002680 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002681 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002682 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002683 {
2684 DWARFMappedHash::DIEInfoArray hash_data_array;
2685 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2686 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2687 }
Greg Clayton7f995132011-10-04 22:41:51 +00002688 }
2689 else
2690 {
2691 // Index the DWARF if we haven't already
2692 if (!m_indexed)
2693 Index ();
2694
2695 m_global_index.Find (regex, die_offsets);
2696 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002697
Greg Claytonc685f8e2010-09-15 04:15:46 +00002698 SymbolContext sc;
Greg Claytone72dfb32012-02-24 01:59:29 +00002699 sc.module_sp = m_obj_file->GetModule();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002700 assert (sc.module_sp);
2701
Greg Claytond4a2b372011-09-12 23:21:58 +00002702 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002703 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002704 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002705 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002706 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002707 DWARFDebugInfo* debug_info = DebugInfo();
2708 for (size_t i=0; i<num_matches; ++i)
2709 {
2710 const dw_offset_t die_offset = die_offsets[i];
2711 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002712
2713 if (die)
2714 {
2715 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002716
Greg Clayton95d87902011-11-11 03:16:25 +00002717 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002718
Greg Clayton95d87902011-11-11 03:16:25 +00002719 if (variables.GetSize() - original_size >= max_matches)
2720 break;
2721 }
2722 else
2723 {
2724 if (m_using_apple_tables)
2725 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002726 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2727 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002728 }
2729 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002730 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002731 }
2732
2733 // Return the number of variable that were appended to the list
2734 return variables.GetSize() - original_size;
2735}
2736
Greg Claytonaa044962011-10-13 00:59:38 +00002737
Jim Ingham4cda6e02011-10-07 22:23:45 +00002738bool
2739SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2740 DWARFCompileUnit *&dwarf_cu,
2741 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002742{
Greg Claytonaa044962011-10-13 00:59:38 +00002743 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2744 return ResolveFunction (dwarf_cu, die, sc_list);
2745}
2746
2747
2748bool
2749SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2750 const DWARFDebugInfoEntry *die,
2751 SymbolContextList& sc_list)
2752{
Greg Clayton9e315582011-09-02 04:03:59 +00002753 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002754
2755 if (die == NULL)
2756 return false;
2757
Jim Ingham4cda6e02011-10-07 22:23:45 +00002758 // If we were passed a die that is not a function, just return false...
2759 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2760 return false;
2761
2762 const DWARFDebugInfoEntry* inlined_die = NULL;
2763 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002764 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002765 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002766
Jim Ingham4cda6e02011-10-07 22:23:45 +00002767 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002768 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002769 if (die->Tag() == DW_TAG_subprogram)
2770 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002771 }
2772 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002773 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002774 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002775 {
2776 Address addr;
2777 // Parse all blocks if needed
2778 if (inlined_die)
2779 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002780 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002781 assert (sc.block != NULL);
2782 if (sc.block->GetStartAddress (addr) == false)
2783 addr.Clear();
2784 }
2785 else
2786 {
2787 sc.block = NULL;
2788 addr = sc.function->GetAddressRange().GetBaseAddress();
2789 }
2790
2791 if (addr.IsValid())
2792 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002793 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002794 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002795 }
2796 }
2797
Greg Claytonaa044962011-10-13 00:59:38 +00002798 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002799}
2800
Greg Clayton7f995132011-10-04 22:41:51 +00002801void
2802SymbolFileDWARF::FindFunctions (const ConstString &name,
2803 const NameToDIE &name_to_die,
2804 SymbolContextList& sc_list)
2805{
Greg Claytond4a2b372011-09-12 23:21:58 +00002806 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002807 if (name_to_die.Find (name, die_offsets))
2808 {
2809 ParseFunctions (die_offsets, sc_list);
2810 }
2811}
2812
2813
2814void
2815SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2816 const NameToDIE &name_to_die,
2817 SymbolContextList& sc_list)
2818{
2819 DIEArray die_offsets;
2820 if (name_to_die.Find (regex, die_offsets))
2821 {
2822 ParseFunctions (die_offsets, sc_list);
2823 }
2824}
2825
2826
2827void
2828SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2829 const DWARFMappedHash::MemoryTable &memory_table,
2830 SymbolContextList& sc_list)
2831{
2832 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002833 DWARFMappedHash::DIEInfoArray hash_data_array;
2834 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002835 {
Greg Claytond1767f02011-12-08 02:13:16 +00002836 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002837 ParseFunctions (die_offsets, sc_list);
2838 }
2839}
2840
2841void
2842SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2843 SymbolContextList& sc_list)
2844{
2845 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002846 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002847 {
Greg Clayton7f995132011-10-04 22:41:51 +00002848 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002849
2850 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002851 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002852 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002853 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002854 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002855 }
2856 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002857}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002858
Jim Ingham4cda6e02011-10-07 22:23:45 +00002859bool
2860SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2861 const DWARFCompileUnit *dwarf_cu,
2862 uint32_t name_type_mask,
2863 const char *partial_name,
2864 const char *base_name_start,
2865 const char *base_name_end)
2866{
2867 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2868 if (name_type_mask == eFunctionNameTypeMethod
2869 || name_type_mask == eFunctionNameTypeBase)
2870 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002871 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2872 if (!containing_decl_ctx)
2873 return false;
2874
2875 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2876
2877 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2878 return false;
2879 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2880 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002881 }
2882
2883 // Now we need to check whether the name we got back for this type matches the extra specifications
2884 // that were in the name we're looking up:
2885 if (base_name_start != partial_name || *base_name_end != '\0')
2886 {
2887 // First see if the stuff to the left matches the full name. To do that let's see if
2888 // we can pull out the mips linkage name attribute:
2889
2890 Mangled best_name;
2891
2892 DWARFDebugInfoEntry::Attributes attributes;
2893 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2894 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2895 if (idx != UINT32_MAX)
2896 {
2897 DWARFFormValue form_value;
2898 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2899 {
2900 const char *name = form_value.AsCString(&get_debug_str_data());
2901 best_name.SetValue (name, true);
2902 }
2903 }
2904 if (best_name)
2905 {
2906 const char *demangled = best_name.GetDemangledName().GetCString();
2907 if (demangled)
2908 {
2909 std::string name_no_parens(partial_name, base_name_end - partial_name);
Jim Ingham85c13d72012-03-02 02:24:42 +00002910 const char *partial_in_demangled = strstr (demangled, name_no_parens.c_str());
2911 if (partial_in_demangled == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002912 return false;
Jim Ingham85c13d72012-03-02 02:24:42 +00002913 else
2914 {
2915 // Sort out the case where our name is something like "Process::Destroy" and the match is
2916 // "SBProcess::Destroy" - that shouldn't be a match. We should really always match on
2917 // namespace boundaries...
2918
2919 if (partial_name[0] == ':' && partial_name[1] == ':')
2920 {
2921 // The partial name was already on a namespace boundary so all matches are good.
2922 return true;
2923 }
2924 else if (partial_in_demangled == demangled)
2925 {
2926 // They both start the same, so this is an good match.
2927 return true;
2928 }
2929 else
2930 {
2931 if (partial_in_demangled - demangled == 1)
2932 {
2933 // Only one character difference, can't be a namespace boundary...
2934 return false;
2935 }
2936 else if (*(partial_in_demangled - 1) == ':' && *(partial_in_demangled - 2) == ':')
2937 {
2938 // We are on a namespace boundary, so this is also good.
2939 return true;
2940 }
2941 else
2942 return false;
2943 }
2944 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002945 }
2946 }
2947 }
2948
2949 return true;
2950}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002951
Greg Clayton0c5cd902010-06-28 21:30:43 +00002952uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002953SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00002954 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002955 uint32_t name_type_mask,
2956 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002957 bool append,
2958 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002959{
2960 Timer scoped_timer (__PRETTY_FUNCTION__,
2961 "SymbolFileDWARF::FindFunctions (name = '%s')",
2962 name.AsCString());
2963
Greg Clayton21f2a492011-10-06 00:09:08 +00002964 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2965
2966 if (log)
2967 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002968 GetObjectFile()->GetModule()->LogMessage (log.get(),
2969 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2970 name.GetCString(),
2971 name_type_mask,
2972 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002973 }
2974
Greg Clayton0c5cd902010-06-28 21:30:43 +00002975 // If we aren't appending the results to this list, then clear the list
2976 if (!append)
2977 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002978
2979 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2980 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002981
2982 // If name is empty then we won't find anything.
2983 if (name.IsEmpty())
2984 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002985
2986 // Remember how many sc_list are in the list before we search in case
2987 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002988
Greg Clayton9e315582011-09-02 04:03:59 +00002989 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00002990
Jim Ingham4cda6e02011-10-07 22:23:45 +00002991 const char *name_cstr = name.GetCString();
2992 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
2993 const char *base_name_start = name_cstr;
2994 const char *base_name_end = name_cstr + strlen(name_cstr);
2995
2996 if (name_type_mask & eFunctionNameTypeAuto)
2997 {
2998 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
2999 effective_name_type_mask = eFunctionNameTypeFull;
3000 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
3001 effective_name_type_mask = eFunctionNameTypeFull;
3002 else
3003 {
3004 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3005 effective_name_type_mask |= eFunctionNameTypeSelector;
3006
3007 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3008 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
3009 }
3010 }
3011 else
3012 {
3013 effective_name_type_mask = name_type_mask;
3014 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
3015 {
3016 // If they've asked for a CPP method or function name and it can't be that, we don't
3017 // even need to search for CPP methods or names.
3018 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
3019 {
3020 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
3021 if (effective_name_type_mask == eFunctionNameTypeNone)
3022 return 0;
3023 }
3024 }
3025
3026 if (effective_name_type_mask & eFunctionNameTypeSelector)
3027 {
3028 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
3029 {
3030 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
3031 if (effective_name_type_mask == eFunctionNameTypeNone)
3032 return 0;
3033 }
3034 }
3035 }
3036
3037 DWARFDebugInfo* info = DebugInfo();
3038 if (info == NULL)
3039 return 0;
3040
Greg Claytonaa044962011-10-13 00:59:38 +00003041 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003042 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003043 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003044 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003045 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003046
3047 DIEArray die_offsets;
3048
3049 uint32_t num_matches = 0;
3050
3051 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003052 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003053 // If they asked for the full name, match what they typed. At some point we may
3054 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3055 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003056 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003057 for (uint32_t i = 0; i < num_matches; i++)
3058 {
Greg Clayton95d87902011-11-11 03:16:25 +00003059 const dw_offset_t die_offset = die_offsets[i];
3060 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003061 if (die)
3062 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003063 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3064 continue;
3065
Sean Callanan9df05fb2012-02-10 22:52:19 +00003066 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3067 continue;
3068
Greg Claytonaa044962011-10-13 00:59:38 +00003069 ResolveFunction (dwarf_cu, die, sc_list);
3070 }
Greg Clayton95d87902011-11-11 03:16:25 +00003071 else
3072 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003073 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3074 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003075 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003076 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003077 }
3078 else
3079 {
3080 if (effective_name_type_mask & eFunctionNameTypeSelector)
3081 {
3082 if (namespace_decl && *namespace_decl)
3083 return 0; // no selectors in namespaces
3084
3085 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3086 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3087 // and if it is an ObjC method name, we're good.
3088
3089 for (uint32_t i = 0; i < num_matches; i++)
3090 {
Greg Clayton95d87902011-11-11 03:16:25 +00003091 const dw_offset_t die_offset = die_offsets[i];
3092 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003093 if (die)
3094 {
3095 const char *die_name = die->GetName(this, dwarf_cu);
3096 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003097 {
3098 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3099 continue;
3100
Greg Clayton97fbc342011-10-20 22:30:33 +00003101 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003102 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003103 }
Greg Clayton95d87902011-11-11 03:16:25 +00003104 else
3105 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003106 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3107 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003108 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003109 }
3110 die_offsets.clear();
3111 }
3112
3113 if (effective_name_type_mask & eFunctionNameTypeMethod
3114 || effective_name_type_mask & eFunctionNameTypeBase)
3115 {
3116 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3117 (namespace_decl && *namespace_decl))
3118 return 0; // no methods in namespaces
3119
3120 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3121 // extract the base name, look that up, and if there is any other information in the name we were
3122 // passed in we have to post-filter based on that.
3123
3124 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3125 std::string base_name(base_name_start, base_name_end - base_name_start);
3126 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3127
3128 for (uint32_t i = 0; i < num_matches; i++)
3129 {
Greg Clayton95d87902011-11-11 03:16:25 +00003130 const dw_offset_t die_offset = die_offsets[i];
3131 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003132 if (die)
3133 {
3134 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3135 continue;
3136
3137 if (!FunctionDieMatchesPartialName(die,
3138 dwarf_cu,
3139 effective_name_type_mask,
3140 name_cstr,
3141 base_name_start,
3142 base_name_end))
3143 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003144
3145 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3146 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003147
3148 // If we get to here, the die is good, and we should add it:
3149 ResolveFunction (dwarf_cu, die, sc_list);
3150 }
Greg Clayton95d87902011-11-11 03:16:25 +00003151 else
3152 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003153 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3154 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003155 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003156 }
3157 die_offsets.clear();
3158 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003159 }
3160 }
Greg Clayton7f995132011-10-04 22:41:51 +00003161 }
3162 else
3163 {
3164
3165 // Index the DWARF if we haven't already
3166 if (!m_indexed)
3167 Index ();
3168
Greg Clayton7f995132011-10-04 22:41:51 +00003169 if (name_type_mask & eFunctionNameTypeFull)
3170 FindFunctions (name, m_function_fullname_index, sc_list);
3171
Jim Ingham4cda6e02011-10-07 22:23:45 +00003172 std::string base_name(base_name_start, base_name_end - base_name_start);
3173 ConstString base_name_const(base_name.c_str());
3174 DIEArray die_offsets;
3175 DWARFCompileUnit *dwarf_cu = NULL;
3176
3177 if (effective_name_type_mask & eFunctionNameTypeBase)
3178 {
3179 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003180 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003181 {
Greg Claytonaa044962011-10-13 00:59:38 +00003182 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3183 if (die)
3184 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003185 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3186 continue;
3187
Greg Claytonaa044962011-10-13 00:59:38 +00003188 if (!FunctionDieMatchesPartialName(die,
3189 dwarf_cu,
3190 effective_name_type_mask,
3191 name_cstr,
3192 base_name_start,
3193 base_name_end))
3194 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003195
Sean Callanan9df05fb2012-02-10 22:52:19 +00003196 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3197 continue;
3198
Greg Claytonaa044962011-10-13 00:59:38 +00003199 // If we get to here, the die is good, and we should add it:
3200 ResolveFunction (dwarf_cu, die, sc_list);
3201 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003202 }
3203 die_offsets.clear();
3204 }
3205
Jim Inghamea8005a2011-10-11 01:18:11 +00003206 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003207 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003208 if (namespace_decl && *namespace_decl)
3209 return 0; // no methods in namespaces
3210
Jim Ingham4cda6e02011-10-07 22:23:45 +00003211 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3212 {
Greg Claytonaa044962011-10-13 00:59:38 +00003213 for (uint32_t i = 0; i < num_base; i++)
3214 {
3215 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3216 if (die)
3217 {
3218 if (!FunctionDieMatchesPartialName(die,
3219 dwarf_cu,
3220 effective_name_type_mask,
3221 name_cstr,
3222 base_name_start,
3223 base_name_end))
3224 continue;
3225
Sean Callanan9df05fb2012-02-10 22:52:19 +00003226 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3227 continue;
3228
Greg Claytonaa044962011-10-13 00:59:38 +00003229 // If we get to here, the die is good, and we should add it:
3230 ResolveFunction (dwarf_cu, die, sc_list);
3231 }
3232 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003233 }
3234 die_offsets.clear();
3235 }
Greg Clayton7f995132011-10-04 22:41:51 +00003236
Sean Callanan213fdb82011-10-13 01:49:10 +00003237 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003238 {
Greg Clayton7f995132011-10-04 22:41:51 +00003239 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003240 }
3241
Greg Clayton4d01ace2011-09-29 16:58:15 +00003242 }
3243
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003244 // Return the number of variable that were appended to the list
3245 return sc_list.GetSize() - original_size;
3246}
3247
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003248uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003249SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003250{
3251 Timer scoped_timer (__PRETTY_FUNCTION__,
3252 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3253 regex.GetText());
3254
Greg Clayton21f2a492011-10-06 00:09:08 +00003255 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3256
3257 if (log)
3258 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003259 GetObjectFile()->GetModule()->LogMessage (log.get(),
3260 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3261 regex.GetText(),
3262 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003263 }
3264
3265
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003266 // If we aren't appending the results to this list, then clear the list
3267 if (!append)
3268 sc_list.Clear();
3269
3270 // Remember how many sc_list are in the list before we search in case
3271 // we are appending the results to a variable list.
3272 uint32_t original_size = sc_list.GetSize();
3273
Greg Clayton97fbc342011-10-20 22:30:33 +00003274 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003275 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003276 if (m_apple_names_ap.get())
3277 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003278 }
3279 else
3280 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003281 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003282 if (!m_indexed)
3283 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003284
Greg Clayton7f995132011-10-04 22:41:51 +00003285 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003286
Greg Clayton7f995132011-10-04 22:41:51 +00003287 FindFunctions (regex, m_function_fullname_index, sc_list);
3288 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003289
3290 // Return the number of variable that were appended to the list
3291 return sc_list.GetSize() - original_size;
3292}
Jim Ingham318c9f22011-08-26 19:44:13 +00003293
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003294uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003295SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3296 const ConstString &name,
3297 const lldb_private::ClangNamespaceDecl *namespace_decl,
3298 bool append,
3299 uint32_t max_matches,
3300 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003301{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003302 DWARFDebugInfo* info = DebugInfo();
3303 if (info == NULL)
3304 return 0;
3305
Greg Clayton21f2a492011-10-06 00:09:08 +00003306 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3307
3308 if (log)
3309 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003310 GetObjectFile()->GetModule()->LogMessage (log.get(),
3311 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
3312 name.GetCString(),
3313 append,
3314 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003315 }
3316
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003317 // If we aren't appending the results to this list, then clear the list
3318 if (!append)
3319 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003320
3321 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3322 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003323
Greg Claytond4a2b372011-09-12 23:21:58 +00003324 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003325
Greg Clayton97fbc342011-10-20 22:30:33 +00003326 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003327 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003328 if (m_apple_types_ap.get())
3329 {
3330 const char *name_cstr = name.GetCString();
3331 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3332 }
Greg Clayton7f995132011-10-04 22:41:51 +00003333 }
3334 else
3335 {
3336 if (!m_indexed)
3337 Index ();
3338
3339 m_type_index.Find (name, die_offsets);
3340 }
3341
Greg Clayton7f995132011-10-04 22:41:51 +00003342 const size_t num_matches = die_offsets.size();
3343
Greg Claytond4a2b372011-09-12 23:21:58 +00003344 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003345 {
Greg Clayton7f995132011-10-04 22:41:51 +00003346 const uint32_t initial_types_size = types.GetSize();
3347 DWARFCompileUnit* dwarf_cu = NULL;
3348 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003349 DWARFDebugInfo* debug_info = DebugInfo();
3350 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003351 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003352 const dw_offset_t die_offset = die_offsets[i];
3353 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3354
Greg Clayton95d87902011-11-11 03:16:25 +00003355 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003356 {
Greg Clayton95d87902011-11-11 03:16:25 +00003357 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3358 continue;
3359
3360 Type *matching_type = ResolveType (dwarf_cu, die);
3361 if (matching_type)
3362 {
3363 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003364 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003365 if (types.GetSize() >= max_matches)
3366 break;
3367 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003368 }
Greg Clayton95d87902011-11-11 03:16:25 +00003369 else
3370 {
3371 if (m_using_apple_tables)
3372 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003373 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3374 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003375 }
3376 }
3377
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003378 }
Greg Clayton7f995132011-10-04 22:41:51 +00003379 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003380 }
Greg Clayton7f995132011-10-04 22:41:51 +00003381 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003382}
3383
3384
Greg Clayton526e5af2010-11-13 03:52:47 +00003385ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003386SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003387 const ConstString &name,
3388 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003389{
Greg Clayton21f2a492011-10-06 00:09:08 +00003390 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3391
3392 if (log)
3393 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003394 GetObjectFile()->GetModule()->LogMessage (log.get(),
3395 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3396 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003397 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003398
3399 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3400 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003401
Greg Clayton526e5af2010-11-13 03:52:47 +00003402 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003403 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003404 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003405 {
Greg Clayton7f995132011-10-04 22:41:51 +00003406 DIEArray die_offsets;
3407
Greg Clayton526e5af2010-11-13 03:52:47 +00003408 // Index if we already haven't to make sure the compile units
3409 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003410 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003411 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003412 if (m_apple_namespaces_ap.get())
3413 {
3414 const char *name_cstr = name.GetCString();
3415 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3416 }
Greg Clayton7f995132011-10-04 22:41:51 +00003417 }
3418 else
3419 {
3420 if (!m_indexed)
3421 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003422
Greg Clayton7f995132011-10-04 22:41:51 +00003423 m_namespace_index.Find (name, die_offsets);
3424 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003425
3426 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003427 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003428 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003429 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003430 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003431 DWARFDebugInfo* debug_info = DebugInfo();
3432 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003433 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003434 const dw_offset_t die_offset = die_offsets[i];
3435 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003436
Greg Clayton95d87902011-11-11 03:16:25 +00003437 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003438 {
Greg Clayton95d87902011-11-11 03:16:25 +00003439 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3440 continue;
3441
3442 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3443 if (clang_namespace_decl)
3444 {
3445 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3446 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003447 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003448 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003449 }
Greg Clayton95d87902011-11-11 03:16:25 +00003450 else
3451 {
3452 if (m_using_apple_tables)
3453 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003454 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3455 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003456 }
3457 }
3458
Greg Clayton526e5af2010-11-13 03:52:47 +00003459 }
3460 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003461 }
Greg Clayton526e5af2010-11-13 03:52:47 +00003462 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003463}
3464
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003465uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003466SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003467{
3468 // Remember how many sc_list are in the list before we search in case
3469 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003470 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003471
3472 const uint32_t num_die_offsets = die_offsets.size();
3473 // Parse all of the types we found from the pubtypes matches
3474 uint32_t i;
3475 uint32_t num_matches = 0;
3476 for (i = 0; i < num_die_offsets; ++i)
3477 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003478 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3479 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003480 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003481 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003482 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003483 ++num_matches;
3484 if (num_matches >= max_matches)
3485 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003486 }
3487 }
3488
3489 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003490 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003491}
3492
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003493
3494size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003495SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3496 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003497 DWARFCompileUnit* dwarf_cu,
3498 const DWARFDebugInfoEntry *parent_die,
3499 bool skip_artificial,
3500 bool &is_static,
3501 TypeList* type_list,
3502 std::vector<clang_type_t>& function_param_types,
3503 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003504 unsigned &type_quals,
3505 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003506{
3507 if (parent_die == NULL)
3508 return 0;
3509
Greg Claytond88d7592010-09-15 08:33:30 +00003510 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3511
Greg Clayton7fedea22010-11-16 02:10:54 +00003512 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003513 const DWARFDebugInfoEntry *die;
3514 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3515 {
3516 dw_tag_t tag = die->Tag();
3517 switch (tag)
3518 {
3519 case DW_TAG_formal_parameter:
3520 {
3521 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003522 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003523 if (num_attributes > 0)
3524 {
3525 const char *name = NULL;
3526 Declaration decl;
3527 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003528 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003529 // one of None, Auto, Register, Extern, Static, PrivateExtern
3530
Sean Callanane2ef6e32010-09-23 03:01:22 +00003531 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003532 uint32_t i;
3533 for (i=0; i<num_attributes; ++i)
3534 {
3535 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3536 DWARFFormValue form_value;
3537 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3538 {
3539 switch (attr)
3540 {
3541 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3542 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3543 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3544 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3545 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003546 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003547 case DW_AT_location:
3548 // if (form_value.BlockData())
3549 // {
3550 // const DataExtractor& debug_info_data = debug_info();
3551 // uint32_t block_length = form_value.Unsigned();
3552 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3553 // }
3554 // else
3555 // {
3556 // }
3557 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003558 case DW_AT_const_value:
3559 case DW_AT_default_value:
3560 case DW_AT_description:
3561 case DW_AT_endianity:
3562 case DW_AT_is_optional:
3563 case DW_AT_segment:
3564 case DW_AT_variable_parameter:
3565 default:
3566 case DW_AT_abstract_origin:
3567 case DW_AT_sibling:
3568 break;
3569 }
3570 }
3571 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003572
Greg Clayton0fffff52010-09-24 05:15:53 +00003573 bool skip = false;
3574 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003575 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003576 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003577 {
3578 // In order to determine if a C++ member function is
3579 // "const" we have to look at the const-ness of "this"...
3580 // Ugly, but that
3581 if (arg_idx == 0)
3582 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003583 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003584 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003585 // Often times compilers omit the "this" name for the
3586 // specification DIEs, so we can't rely upon the name
3587 // being in the formal parameter DIE...
3588 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003589 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003590 Type *this_type = ResolveTypeUID (param_type_die_offset);
3591 if (this_type)
3592 {
3593 uint32_t encoding_mask = this_type->GetEncodingMask();
3594 if (encoding_mask & Type::eEncodingIsPointerUID)
3595 {
3596 is_static = false;
3597
3598 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3599 type_quals |= clang::Qualifiers::Const;
3600 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3601 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003602 }
3603 }
3604 }
3605 }
3606 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003607 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003608 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003609 else
3610 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003611
Greg Clayton0fffff52010-09-24 05:15:53 +00003612 // HACK: Objective C formal parameters "self" and "_cmd"
3613 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00003614 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3615 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00003616 {
3617 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3618 skip = true;
3619 }
3620 }
3621 }
3622
3623 if (!skip)
3624 {
3625 Type *type = ResolveTypeUID(param_type_die_offset);
3626 if (type)
3627 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003628 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003629
Greg Clayton42ce2f32011-12-12 21:50:19 +00003630 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3631 type->GetClangForwardType(),
3632 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003633 assert(param_var_decl);
3634 function_param_decls.push_back(param_var_decl);
3635 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003636 }
3637 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003638 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003639 }
3640 break;
3641
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003642 case DW_TAG_template_type_parameter:
3643 case DW_TAG_template_value_parameter:
3644 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3645 break;
3646
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003647 default:
3648 break;
3649 }
3650 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003651 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003652}
3653
3654size_t
3655SymbolFileDWARF::ParseChildEnumerators
3656(
3657 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003658 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003659 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003660 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003661 const DWARFDebugInfoEntry *parent_die
3662)
3663{
3664 if (parent_die == NULL)
3665 return 0;
3666
3667 size_t enumerators_added = 0;
3668 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003669 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3670
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003671 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3672 {
3673 const dw_tag_t tag = die->Tag();
3674 if (tag == DW_TAG_enumerator)
3675 {
3676 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003677 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003678 if (num_child_attributes > 0)
3679 {
3680 const char *name = NULL;
3681 bool got_value = false;
3682 int64_t enum_value = 0;
3683 Declaration decl;
3684
3685 uint32_t i;
3686 for (i=0; i<num_child_attributes; ++i)
3687 {
3688 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3689 DWARFFormValue form_value;
3690 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3691 {
3692 switch (attr)
3693 {
3694 case DW_AT_const_value:
3695 got_value = true;
3696 enum_value = form_value.Unsigned();
3697 break;
3698
3699 case DW_AT_name:
3700 name = form_value.AsCString(&get_debug_str_data());
3701 break;
3702
3703 case DW_AT_description:
3704 default:
3705 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3706 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3707 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3708 case DW_AT_sibling:
3709 break;
3710 }
3711 }
3712 }
3713
3714 if (name && name[0] && got_value)
3715 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003716 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3717 enumerator_clang_type,
3718 decl,
3719 name,
3720 enum_value,
3721 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003722 ++enumerators_added;
3723 }
3724 }
3725 }
3726 }
3727 return enumerators_added;
3728}
3729
3730void
3731SymbolFileDWARF::ParseChildArrayInfo
3732(
3733 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003734 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003735 const DWARFDebugInfoEntry *parent_die,
3736 int64_t& first_index,
3737 std::vector<uint64_t>& element_orders,
3738 uint32_t& byte_stride,
3739 uint32_t& bit_stride
3740)
3741{
3742 if (parent_die == NULL)
3743 return;
3744
3745 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003746 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003747 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3748 {
3749 const dw_tag_t tag = die->Tag();
3750 switch (tag)
3751 {
3752 case DW_TAG_enumerator:
3753 {
3754 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003755 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003756 if (num_child_attributes > 0)
3757 {
3758 const char *name = NULL;
3759 bool got_value = false;
3760 int64_t enum_value = 0;
3761
3762 uint32_t i;
3763 for (i=0; i<num_child_attributes; ++i)
3764 {
3765 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3766 DWARFFormValue form_value;
3767 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3768 {
3769 switch (attr)
3770 {
3771 case DW_AT_const_value:
3772 got_value = true;
3773 enum_value = form_value.Unsigned();
3774 break;
3775
3776 case DW_AT_name:
3777 name = form_value.AsCString(&get_debug_str_data());
3778 break;
3779
3780 case DW_AT_description:
3781 default:
3782 case DW_AT_decl_file:
3783 case DW_AT_decl_line:
3784 case DW_AT_decl_column:
3785 case DW_AT_sibling:
3786 break;
3787 }
3788 }
3789 }
3790 }
3791 }
3792 break;
3793
3794 case DW_TAG_subrange_type:
3795 {
3796 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003797 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003798 if (num_child_attributes > 0)
3799 {
3800 const char *name = NULL;
3801 bool got_value = false;
3802 uint64_t byte_size = 0;
3803 int64_t enum_value = 0;
3804 uint64_t num_elements = 0;
3805 uint64_t lower_bound = 0;
3806 uint64_t upper_bound = 0;
3807 uint32_t i;
3808 for (i=0; i<num_child_attributes; ++i)
3809 {
3810 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3811 DWARFFormValue form_value;
3812 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3813 {
3814 switch (attr)
3815 {
3816 case DW_AT_const_value:
3817 got_value = true;
3818 enum_value = form_value.Unsigned();
3819 break;
3820
3821 case DW_AT_name:
3822 name = form_value.AsCString(&get_debug_str_data());
3823 break;
3824
3825 case DW_AT_count:
3826 num_elements = form_value.Unsigned();
3827 break;
3828
3829 case DW_AT_bit_stride:
3830 bit_stride = form_value.Unsigned();
3831 break;
3832
3833 case DW_AT_byte_stride:
3834 byte_stride = form_value.Unsigned();
3835 break;
3836
3837 case DW_AT_byte_size:
3838 byte_size = form_value.Unsigned();
3839 break;
3840
3841 case DW_AT_lower_bound:
3842 lower_bound = form_value.Unsigned();
3843 break;
3844
3845 case DW_AT_upper_bound:
3846 upper_bound = form_value.Unsigned();
3847 break;
3848
3849 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850 case DW_AT_abstract_origin:
3851 case DW_AT_accessibility:
3852 case DW_AT_allocated:
3853 case DW_AT_associated:
3854 case DW_AT_data_location:
3855 case DW_AT_declaration:
3856 case DW_AT_description:
3857 case DW_AT_sibling:
3858 case DW_AT_threads_scaled:
3859 case DW_AT_type:
3860 case DW_AT_visibility:
3861 break;
3862 }
3863 }
3864 }
3865
3866 if (upper_bound > lower_bound)
3867 num_elements = upper_bound - lower_bound + 1;
3868
3869 if (num_elements > 0)
3870 element_orders.push_back (num_elements);
3871 }
3872 }
3873 break;
3874 }
3875 }
3876}
3877
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003878TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00003879SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003880{
3881 TypeSP type_sp;
3882 if (die != NULL)
3883 {
Greg Clayton96d7d742010-11-10 23:42:09 +00003884 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003885 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003886 if (type_ptr == NULL)
3887 {
Greg Claytonca512b32011-01-14 04:54:56 +00003888 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3889 assert (lldb_cu);
3890 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00003891 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003892 }
3893 else if (type_ptr != DIE_IS_BEING_PARSED)
3894 {
3895 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003896 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003897 }
3898
3899 }
3900 return type_sp;
3901}
3902
3903clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003904SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003905{
3906 if (die_offset != DW_INVALID_OFFSET)
3907 {
3908 DWARFCompileUnitSP cu_sp;
3909 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003910 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003911 }
3912 return NULL;
3913}
3914
Sean Callanan72e49402011-08-05 23:43:37 +00003915clang::DeclContext *
3916SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3917{
3918 if (die_offset != DW_INVALID_OFFSET)
3919 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00003920 DWARFDebugInfo* debug_info = DebugInfo();
3921 if (debug_info)
3922 {
3923 DWARFCompileUnitSP cu_sp;
3924 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3925 if (die)
3926 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3927 }
Sean Callanan72e49402011-08-05 23:43:37 +00003928 }
3929 return NULL;
3930}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003931
Greg Clayton96d7d742010-11-10 23:42:09 +00003932clang::NamespaceDecl *
3933SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3934{
Greg Clayton030a2042011-10-14 21:34:45 +00003935 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00003936 {
Greg Clayton030a2042011-10-14 21:34:45 +00003937 // See if we already parsed this namespace DIE and associated it with a
3938 // uniqued namespace declaration
3939 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
3940 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003941 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00003942 else
3943 {
3944 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00003945 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (curr_cu, die, NULL);
3946 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3947 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3948 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00003949 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00003950 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00003951 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003952 GetObjectFile()->GetModule()->LogMessage (log.get(),
3953 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3954 GetClangASTContext().getASTContext(),
3955 MakeUserID(die->GetOffset()),
3956 namespace_name,
3957 namespace_decl,
3958 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00003959 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003960 else
3961 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003962 GetObjectFile()->GetModule()->LogMessage (log.get(),
3963 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
3964 GetClangASTContext().getASTContext(),
3965 MakeUserID(die->GetOffset()),
3966 namespace_decl,
3967 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00003968 }
Greg Clayton030a2042011-10-14 21:34:45 +00003969 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003970
3971 if (namespace_decl)
3972 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3973 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003974 }
3975 }
3976 return NULL;
3977}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003978
3979clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00003980SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00003981{
Greg Clayton5cf58b92011-10-05 22:22:08 +00003982 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3983 if (clang_decl_ctx)
3984 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003985 // If this DIE has a specification, or an abstract origin, then trace to those.
3986
Greg Claytoncab36a32011-12-08 05:16:30 +00003987 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003988 if (die_offset != DW_INVALID_OFFSET)
3989 return GetClangDeclContextForDIEOffset (sc, die_offset);
3990
Greg Claytoncab36a32011-12-08 05:16:30 +00003991 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003992 if (die_offset != DW_INVALID_OFFSET)
3993 return GetClangDeclContextForDIEOffset (sc, die_offset);
3994
Greg Clayton3bffb082011-12-10 02:15:28 +00003995 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3996 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003997 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 +00003998 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00003999 bool assert_not_being_parsed = true;
4000 ResolveTypeUID (cu, die, assert_not_being_parsed);
4001
Greg Clayton5cf58b92011-10-05 22:22:08 +00004002 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
4003
4004 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00004005}
4006
4007clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00004008SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004009{
Greg Claytonca512b32011-01-14 04:54:56 +00004010 if (m_clang_tu_decl == NULL)
4011 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004012
Greg Clayton2bc22f82011-09-30 03:20:47 +00004013 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00004014
4015 if (decl_ctx_die_copy)
4016 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00004017
4018 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004019 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00004020
Greg Clayton2bc22f82011-09-30 03:20:47 +00004021 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
4022 if (pos != m_die_to_decl_ctx.end())
4023 return pos->second;
4024
4025 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004026 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004027 case DW_TAG_compile_unit:
4028 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004029
Greg Clayton2bc22f82011-09-30 03:20:47 +00004030 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00004031 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00004032 break;
4033
4034 case DW_TAG_structure_type:
4035 case DW_TAG_union_type:
4036 case DW_TAG_class_type:
4037 {
4038 Type* type = ResolveType (cu, decl_ctx_die);
4039 if (type)
4040 {
4041 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4042 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004043 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004044 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4045 if (decl_ctx)
4046 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004047 }
4048 }
Greg Claytonca512b32011-01-14 04:54:56 +00004049 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004050 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004051
Greg Clayton2bc22f82011-09-30 03:20:47 +00004052 default:
4053 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004054 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004055 }
Greg Clayton7a345282010-11-09 23:46:37 +00004056 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004057}
4058
Greg Clayton2bc22f82011-09-30 03:20:47 +00004059
4060const DWARFDebugInfoEntry *
4061SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4062{
4063 if (cu && die)
4064 {
4065 const DWARFDebugInfoEntry * const decl_die = die;
4066
4067 while (die != NULL)
4068 {
4069 // If this is the original DIE that we are searching for a declaration
4070 // for, then don't look in the cache as we don't want our own decl
4071 // context to be our decl context...
4072 if (decl_die != die)
4073 {
4074 switch (die->Tag())
4075 {
4076 case DW_TAG_compile_unit:
4077 case DW_TAG_namespace:
4078 case DW_TAG_structure_type:
4079 case DW_TAG_union_type:
4080 case DW_TAG_class_type:
4081 return die;
4082
4083 default:
4084 break;
4085 }
4086 }
4087
4088 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4089 if (die_offset != DW_INVALID_OFFSET)
4090 {
4091 DWARFCompileUnit *spec_cu = cu;
4092 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4093 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4094 if (spec_die_decl_ctx_die)
4095 return spec_die_decl_ctx_die;
4096 }
4097
4098 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4099 if (die_offset != DW_INVALID_OFFSET)
4100 {
4101 DWARFCompileUnit *abs_cu = cu;
4102 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4103 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4104 if (abs_die_decl_ctx_die)
4105 return abs_die_decl_ctx_die;
4106 }
4107
4108 die = die->GetParent();
4109 }
4110 }
4111 return NULL;
4112}
4113
4114
Greg Clayton901c5ca2011-12-03 04:40:03 +00004115Symbol *
4116SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4117{
4118 Symbol *objc_class_symbol = NULL;
4119 if (m_obj_file)
4120 {
4121 Symtab *symtab = m_obj_file->GetSymtab();
4122 if (symtab)
4123 {
4124 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4125 eSymbolTypeObjCClass,
4126 Symtab::eDebugNo,
4127 Symtab::eVisibilityAny);
4128 }
4129 }
4130 return objc_class_symbol;
4131}
4132
Greg Claytonc7f03b62012-01-12 04:33:28 +00004133// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4134// then we can end up looking through all class types for a complete type and never find
4135// the full definition. We need to know if this attribute is supported, so we determine
4136// this here and cache th result. We also need to worry about the debug map DWARF file
4137// if we are doing darwin DWARF in .o file debugging.
4138bool
4139SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4140{
4141 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4142 {
4143 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4144 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4145 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4146 else
4147 {
4148 DWARFDebugInfo* debug_info = DebugInfo();
4149 const uint32_t num_compile_units = GetNumCompileUnits();
4150 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4151 {
4152 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4153 if (curr_cu != cu && curr_cu->Supports_DW_AT_APPLE_objc_complete_type())
4154 {
4155 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4156 break;
4157 }
4158 }
4159 }
4160 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
4161 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4162 }
4163 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4164}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004165
4166// This function can be used when a DIE is found that is a forward declaration
4167// DIE and we want to try and find a type that has the complete definition.
4168TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004169SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4170 const ConstString &type_name,
4171 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004172{
4173
4174 TypeSP type_sp;
4175
Greg Claytonc7f03b62012-01-12 04:33:28 +00004176 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004177 return type_sp;
4178
4179 DIEArray die_offsets;
4180
4181 if (m_using_apple_tables)
4182 {
4183 if (m_apple_types_ap.get())
4184 {
4185 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004186 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004187 }
4188 }
4189 else
4190 {
4191 if (!m_indexed)
4192 Index ();
4193
4194 m_type_index.Find (type_name, die_offsets);
4195 }
4196
Greg Clayton901c5ca2011-12-03 04:40:03 +00004197 const size_t num_matches = die_offsets.size();
4198
Greg Clayton901c5ca2011-12-03 04:40:03 +00004199 DWARFCompileUnit* type_cu = NULL;
4200 const DWARFDebugInfoEntry* type_die = NULL;
4201 if (num_matches)
4202 {
4203 DWARFDebugInfo* debug_info = DebugInfo();
4204 for (size_t i=0; i<num_matches; ++i)
4205 {
4206 const dw_offset_t die_offset = die_offsets[i];
4207 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4208
4209 if (type_die)
4210 {
4211 bool try_resolving_type = false;
4212
4213 // Don't try and resolve the DIE we are looking for with the DIE itself!
4214 if (type_die != die)
4215 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004216 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004217 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004218 case DW_TAG_class_type:
4219 case DW_TAG_structure_type:
4220 try_resolving_type = true;
4221 break;
4222 default:
4223 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004224 }
4225 }
4226
4227 if (try_resolving_type)
4228 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004229 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004230 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004231
4232 if (try_resolving_type)
4233 {
4234 Type *resolved_type = ResolveType (type_cu, type_die, false);
4235 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4236 {
4237 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4238 MakeUserID(die->GetOffset()),
4239 MakeUserID(curr_cu->GetOffset()),
4240 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4241 MakeUserID(type_die->GetOffset()),
4242 MakeUserID(type_cu->GetOffset()));
4243
Greg Claytonc7f03b62012-01-12 04:33:28 +00004244 if (die)
4245 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004246 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004247 break;
4248 }
4249 }
4250 }
4251 }
4252 else
4253 {
4254 if (m_using_apple_tables)
4255 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004256 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4257 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004258 }
4259 }
4260
4261 }
4262 }
4263 return type_sp;
4264}
4265
Greg Clayton80c26302012-02-05 06:12:47 +00004266//----------------------------------------------------------------------
4267// This function helps to ensure that the declaration contexts match for
4268// two different DIEs. Often times debug information will refer to a
4269// forward declaration of a type (the equivalent of "struct my_struct;".
4270// There will often be a declaration of that type elsewhere that has the
4271// full definition. When we go looking for the full type "my_struct", we
4272// will find one or more matches in the accelerator tables and we will
4273// then need to make sure the type was in the same declaration context
4274// as the original DIE. This function can efficiently compare two DIEs
4275// and will return true when the declaration context matches, and false
4276// when they don't.
4277//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004278bool
4279SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4280 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4281{
4282 assert (die1 != die2);
4283 DWARFDIECollection decl_ctx_1;
4284 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004285 //The declaration DIE stack is a stack of the declaration context
4286 // DIEs all the way back to the compile unit. If a type "T" is
4287 // declared inside a class "B", and class "B" is declared inside
4288 // a class "A" and class "A" is in a namespace "lldb", and the
4289 // namespace is in a compile unit, there will be a stack of DIEs:
4290 //
4291 // [0] DW_TAG_class_type for "B"
4292 // [1] DW_TAG_class_type for "A"
4293 // [2] DW_TAG_namespace for "lldb"
4294 // [3] DW_TAG_compile_unit for the source file.
4295 //
4296 // We grab both contexts and make sure that everything matches
4297 // all the way back to the compiler unit.
4298
4299 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004300 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004301 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004302 // Make sure the context arrays have the same size, otherwise
4303 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004304 const size_t count1 = decl_ctx_1.Size();
4305 const size_t count2 = decl_ctx_2.Size();
4306 if (count1 != count2)
4307 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004308
4309 // Make sure the DW_TAG values match all the way back up the the
4310 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004311 const DWARFDebugInfoEntry *decl_ctx_die1;
4312 const DWARFDebugInfoEntry *decl_ctx_die2;
4313 size_t i;
4314 for (i=0; i<count1; i++)
4315 {
4316 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4317 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4318 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4319 return false;
4320 }
Greg Clayton890ff562012-02-02 05:48:16 +00004321#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004322
4323 // Make sure the top item in the decl context die array is always
4324 // DW_TAG_compile_unit. If it isn't then something went wrong in
4325 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004326 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004327
Greg Clayton890ff562012-02-02 05:48:16 +00004328#endif
4329 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004330 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004331 for (i=0; i<count1 - 1; i++)
4332 {
4333 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4334 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4335 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004336 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004337 // If the string was from a DW_FORM_strp, then the pointer will often
4338 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004339 if (name1 == name2)
4340 continue;
4341
4342 // Name pointers are not equal, so only compare the strings
4343 // if both are not NULL.
4344 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004345 {
Greg Clayton5569e642012-02-06 01:44:54 +00004346 // If the strings don't compare, we are done...
4347 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004348 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004349 }
4350 else
4351 {
4352 // One name was NULL while the other wasn't
4353 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004354 }
4355 }
Greg Clayton80c26302012-02-05 06:12:47 +00004356 // We made it through all of the checks and the declaration contexts
4357 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004358 return true;
4359}
Greg Clayton220a0072011-12-09 08:48:30 +00004360
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004361// This function can be used when a DIE is found that is a forward declaration
4362// DIE and we want to try and find a type that has the complete definition.
4363TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00004364SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
4365 const DWARFDebugInfoEntry *die,
4366 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004367{
4368 TypeSP type_sp;
4369
Greg Clayton1a65ae12011-01-25 23:55:37 +00004370 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004371 return type_sp;
4372
Greg Clayton7f995132011-10-04 22:41:51 +00004373 DIEArray die_offsets;
4374
Greg Clayton97fbc342011-10-20 22:30:33 +00004375 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004376 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004377 if (m_apple_types_ap.get())
4378 {
Greg Claytond1767f02011-12-08 02:13:16 +00004379 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4380 {
Greg Claytonae920b62012-01-06 00:17:16 +00004381 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004382 }
4383 else
4384 {
4385 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4386 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004387 }
Greg Clayton7f995132011-10-04 22:41:51 +00004388 }
4389 else
4390 {
4391 if (!m_indexed)
4392 Index ();
4393
4394 m_type_index.Find (type_name, die_offsets);
4395 }
Greg Clayton7f995132011-10-04 22:41:51 +00004396
4397 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004398
Greg Clayton934cb052011-12-03 00:27:05 +00004399 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004400
4401 DWARFCompileUnit* type_cu = NULL;
4402 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004403 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004404 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004405 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004406 for (size_t i=0; i<num_matches; ++i)
4407 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004408 const dw_offset_t die_offset = die_offsets[i];
4409 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004410
Greg Clayton95d87902011-11-11 03:16:25 +00004411 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004412 {
Greg Clayton934cb052011-12-03 00:27:05 +00004413 bool try_resolving_type = false;
4414
4415 // Don't try and resolve the DIE we are looking for with the DIE itself!
4416 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004417 {
Greg Clayton934cb052011-12-03 00:27:05 +00004418 const dw_tag_t type_die_tag = type_die->Tag();
4419 // Make sure the tags match
4420 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004421 {
Greg Clayton934cb052011-12-03 00:27:05 +00004422 // The tags match, lets try resolving this type
4423 try_resolving_type = true;
4424 }
4425 else
4426 {
4427 // The tags don't match, but we need to watch our for a
4428 // forward declaration for a struct and ("struct foo")
4429 // ends up being a class ("class foo { ... };") or
4430 // vice versa.
4431 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004432 {
Greg Clayton934cb052011-12-03 00:27:05 +00004433 case DW_TAG_class_type:
4434 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4435 try_resolving_type = (die_tag == DW_TAG_structure_type);
4436 break;
4437 case DW_TAG_structure_type:
4438 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4439 try_resolving_type = (die_tag == DW_TAG_class_type);
4440 break;
4441 default:
4442 // Tags don't match, don't event try to resolve
4443 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004444 break;
4445 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004446 }
4447 }
Greg Clayton934cb052011-12-03 00:27:05 +00004448
4449 if (try_resolving_type)
4450 {
Greg Clayton890ff562012-02-02 05:48:16 +00004451 // Make sure the decl contexts match all the way up
4452 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004453 {
Greg Clayton890ff562012-02-02 05:48:16 +00004454 Type *resolved_type = ResolveType (type_cu, type_die, false);
4455 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4456 {
4457 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4458 MakeUserID(die->GetOffset()),
4459 MakeUserID(curr_cu->GetOffset()),
4460 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4461 MakeUserID(type_die->GetOffset()),
4462 MakeUserID(type_cu->GetOffset()));
4463
4464 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004465 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004466 break;
4467 }
Greg Clayton934cb052011-12-03 00:27:05 +00004468 }
4469 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004470 }
Greg Clayton95d87902011-11-11 03:16:25 +00004471 else
4472 {
4473 if (m_using_apple_tables)
4474 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004475 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4476 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004477 }
4478 }
4479
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004480 }
4481 }
4482 return type_sp;
4483}
4484
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004485TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004486SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004487{
4488 TypeSP type_sp;
4489
Greg Clayton1be10fc2010-09-29 01:12:09 +00004490 if (type_is_new_ptr)
4491 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00004492
4493#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4494 static DIEStack g_die_stack;
4495 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
4496#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00004497
Sean Callananc7fbf732010-08-06 00:32:49 +00004498 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004499 if (die != NULL)
4500 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004501 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004502 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00004503 {
4504 const DWARFDebugInfoEntry *context_die;
4505 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
4506
Greg Clayton1fba87112012-02-09 20:03:49 +00004507 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 +00004508 die->GetOffset(),
4509 context,
4510 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00004511 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00004512 die->GetName(this, dwarf_cu));
4513
4514#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4515 scoped_die_logger.Push (dwarf_cu, die);
4516 g_die_stack.LogDIEs(log.get(), this);
4517#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00004518 }
Greg Clayton3bffb082011-12-10 02:15:28 +00004519//
4520// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4521// if (log && dwarf_cu)
4522// {
4523// StreamString s;
4524// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004525// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004526//
4527// }
Jim Ingham16746d12011-08-25 23:21:43 +00004528
Greg Clayton594e5ed2010-09-27 21:07:38 +00004529 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004530 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004531 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004532 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004533 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004534 if (type_is_new_ptr)
4535 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004536
Greg Clayton594e5ed2010-09-27 21:07:38 +00004537 const dw_tag_t tag = die->Tag();
4538
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004539 bool is_forward_declaration = false;
4540 DWARFDebugInfoEntry::Attributes attributes;
4541 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004542 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004543 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4544 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004545 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004546 Declaration decl;
4547
Greg Clayton4957bf62010-09-30 21:49:03 +00004548 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004549 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004550
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004551 dw_attr_t attr;
4552
4553 switch (tag)
4554 {
4555 case DW_TAG_base_type:
4556 case DW_TAG_pointer_type:
4557 case DW_TAG_reference_type:
4558 case DW_TAG_typedef:
4559 case DW_TAG_const_type:
4560 case DW_TAG_restrict_type:
4561 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004562 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004563 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004564 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004565 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004566
Greg Claytond88d7592010-09-15 08:33:30 +00004567 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004568 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004569 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4570
4571 if (num_attributes > 0)
4572 {
4573 uint32_t i;
4574 for (i=0; i<num_attributes; ++i)
4575 {
4576 attr = attributes.AttributeAtIndex(i);
4577 DWARFFormValue form_value;
4578 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4579 {
4580 switch (attr)
4581 {
4582 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4583 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4584 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4585 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004586
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004587 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004588 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4589 // include the "&"...
4590 if (tag == DW_TAG_reference_type)
4591 {
4592 if (strchr (type_name_cstr, '&') == NULL)
4593 type_name_cstr = NULL;
4594 }
4595 if (type_name_cstr)
4596 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004597 break;
Greg Clayton36909642011-03-15 04:38:20 +00004598 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004599 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4600 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4601 default:
4602 case DW_AT_sibling:
4603 break;
4604 }
4605 }
4606 }
4607 }
4608
Greg Clayton81c22f62011-10-19 18:09:39 +00004609 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 +00004610
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004611 switch (tag)
4612 {
4613 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00004614 break;
4615
Greg Claytond4436412011-10-28 21:00:00 +00004616 case DW_TAG_unspecified_type:
4617 if (strcmp(type_name_cstr, "nullptr_t") == 0)
4618 {
4619 resolve_state = Type::eResolveStateFull;
4620 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
4621 break;
4622 }
4623 // Fall through to base type below in case we can handle the type there...
4624
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004625 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00004626 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004627 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
4628 encoding,
4629 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004630 break;
4631
Greg Clayton526e5af2010-11-13 03:52:47 +00004632 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
4633 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
4634 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
4635 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
4636 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
4637 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004638 }
4639
Greg Claytonc7f03b62012-01-12 04:33:28 +00004640 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004641 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004642 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
4643 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004644 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004645 static ConstString g_objc_type_name_id("id");
4646 static ConstString g_objc_type_name_Class("Class");
4647 static ConstString g_objc_type_name_selector("SEL");
4648
4649 if (type_name_const_str == g_objc_type_name_id)
4650 {
4651 if (log)
4652 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
4653 die->GetOffset(),
4654 DW_TAG_value_to_name(die->Tag()),
4655 die->GetName(this, dwarf_cu));
4656 clang_type = ast.GetBuiltInType_objc_id();
4657 encoding_data_type = Type::eEncodingIsUID;
4658 encoding_uid = LLDB_INVALID_UID;
4659 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00004660
Greg Claytonc7f03b62012-01-12 04:33:28 +00004661 }
4662 else if (type_name_const_str == g_objc_type_name_Class)
4663 {
4664 if (log)
4665 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
4666 die->GetOffset(),
4667 DW_TAG_value_to_name(die->Tag()),
4668 die->GetName(this, dwarf_cu));
4669 clang_type = ast.GetBuiltInType_objc_Class();
4670 encoding_data_type = Type::eEncodingIsUID;
4671 encoding_uid = LLDB_INVALID_UID;
4672 resolve_state = Type::eResolveStateFull;
4673 }
4674 else if (type_name_const_str == g_objc_type_name_selector)
4675 {
4676 if (log)
4677 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
4678 die->GetOffset(),
4679 DW_TAG_value_to_name(die->Tag()),
4680 die->GetName(this, dwarf_cu));
4681 clang_type = ast.GetBuiltInType_objc_selector();
4682 encoding_data_type = Type::eEncodingIsUID;
4683 encoding_uid = LLDB_INVALID_UID;
4684 resolve_state = Type::eResolveStateFull;
4685 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004686 }
4687 }
4688
Greg Clayton81c22f62011-10-19 18:09:39 +00004689 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004690 this,
4691 type_name_const_str,
4692 byte_size,
4693 NULL,
4694 encoding_uid,
4695 encoding_data_type,
4696 &decl,
4697 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004698 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004699
Greg Clayton594e5ed2010-09-27 21:07:38 +00004700 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004701
4702// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
4703// if (encoding_type != NULL)
4704// {
4705// if (encoding_type != DIE_IS_BEING_PARSED)
4706// type_sp->SetEncodingType(encoding_type);
4707// else
4708// m_indirect_fixups.push_back(type_sp.get());
4709// }
4710 }
4711 break;
4712
4713 case DW_TAG_structure_type:
4714 case DW_TAG_union_type:
4715 case DW_TAG_class_type:
4716 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004717 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004718 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004719
Greg Clayton9e409562010-07-28 02:04:09 +00004720 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00004721 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004722 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00004723 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004724 if (num_attributes > 0)
4725 {
4726 uint32_t i;
4727 for (i=0; i<num_attributes; ++i)
4728 {
4729 attr = attributes.AttributeAtIndex(i);
4730 DWARFFormValue form_value;
4731 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4732 {
4733 switch (attr)
4734 {
Greg Clayton9e409562010-07-28 02:04:09 +00004735 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00004736 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
4737 {
4738 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
4739 // point to the compile unit file, so we clear this invalid value
4740 // so that we can still unique types efficiently.
4741 decl.SetFile(FileSpec ("<invalid>", false));
4742 }
4743 else
4744 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00004745 break;
4746
4747 case DW_AT_decl_line:
4748 decl.SetLine(form_value.Unsigned());
4749 break;
4750
4751 case DW_AT_decl_column:
4752 decl.SetColumn(form_value.Unsigned());
4753 break;
4754
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004755 case DW_AT_name:
4756 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004757 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004758 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004759
4760 case DW_AT_byte_size:
4761 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00004762 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004763 break;
4764
4765 case DW_AT_accessibility:
4766 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
4767 break;
4768
4769 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00004770 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00004771 break;
4772
4773 case DW_AT_APPLE_runtime_class:
4774 class_language = (LanguageType)form_value.Signed();
4775 break;
4776
Greg Clayton18774842011-11-29 23:40:34 +00004777 case DW_AT_APPLE_objc_complete_type:
4778 is_complete_objc_class = form_value.Signed();
4779 break;
4780
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004781 case DW_AT_allocated:
4782 case DW_AT_associated:
4783 case DW_AT_data_location:
4784 case DW_AT_description:
4785 case DW_AT_start_scope:
4786 case DW_AT_visibility:
4787 default:
4788 case DW_AT_sibling:
4789 break;
4790 }
4791 }
4792 }
4793 }
4794
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004795 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00004796
Greg Clayton123edca2012-03-02 00:07:15 +00004797 // Only try and unique the type if it has a name.
4798 if (type_name_const_str &&
4799 GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Sean Callanan8e8072d2012-02-07 21:13:38 +00004800 this,
4801 dwarf_cu,
4802 die,
4803 decl,
4804 byte_size_valid ? byte_size : -1,
4805 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004806 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00004807 // We have already parsed this type or from another
4808 // compile unit. GCC loves to use the "one definition
4809 // rule" which can result in multiple definitions
4810 // of the same class over and over in each compile
4811 // unit.
4812 type_sp = unique_ast_entry.m_type_sp;
4813 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00004814 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00004815 m_die_to_type[die] = type_sp.get();
4816 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004817 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004818 }
4819
Greg Clayton81c22f62011-10-19 18:09:39 +00004820 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 +00004821
4822 int tag_decl_kind = -1;
4823 AccessType default_accessibility = eAccessNone;
4824 if (tag == DW_TAG_structure_type)
4825 {
4826 tag_decl_kind = clang::TTK_Struct;
4827 default_accessibility = eAccessPublic;
4828 }
4829 else if (tag == DW_TAG_union_type)
4830 {
4831 tag_decl_kind = clang::TTK_Union;
4832 default_accessibility = eAccessPublic;
4833 }
4834 else if (tag == DW_TAG_class_type)
4835 {
4836 tag_decl_kind = clang::TTK_Class;
4837 default_accessibility = eAccessPrivate;
4838 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00004839
4840 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
4841 die->HasChildren() == false &&
4842 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
4843 {
4844 // Work around an issue with clang at the moment where
4845 // forward declarations for objective C classes are emitted
4846 // as:
4847 // DW_TAG_structure_type [2]
4848 // DW_AT_name( "ForwardObjcClass" )
4849 // DW_AT_byte_size( 0x00 )
4850 // DW_AT_decl_file( "..." )
4851 // DW_AT_decl_line( 1 )
4852 //
4853 // Note that there is no DW_AT_declaration and there are
4854 // no children, and the byte size is zero.
4855 is_forward_declaration = true;
4856 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004857
Greg Clayton18774842011-11-29 23:40:34 +00004858 if (class_language == eLanguageTypeObjC)
4859 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004860 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004861 {
4862 // We have a valid eSymbolTypeObjCClass class symbol whose
4863 // name matches the current objective C class that we
4864 // are trying to find and this DIE isn't the complete
4865 // definition (we checked is_complete_objc_class above and
4866 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00004867 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004868
Greg Clayton901c5ca2011-12-03 04:40:03 +00004869 if (!type_sp && m_debug_map_symfile)
4870 {
4871 // We weren't able to find a full declaration in
4872 // this DWARF, see if we have a declaration anywhere
4873 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00004874 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004875 }
4876
4877 if (type_sp)
4878 {
4879 if (log)
4880 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004881 GetObjectFile()->GetModule()->LogMessage (log.get(),
4882 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
4883 this,
4884 die->GetOffset(),
4885 DW_TAG_value_to_name(tag),
4886 type_name_cstr,
4887 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004888 }
4889
4890 // We found a real definition for this type elsewhere
4891 // so lets use it and cache the fact that we found
4892 // a complete type for this die
4893 m_die_to_type[die] = type_sp.get();
4894 return type_sp;
4895 }
4896 }
4897 }
4898
4899
4900 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004901 {
4902 // We have a forward declaration to a type and we need
4903 // to try and find a full declaration. We look in the
4904 // current type index just in case we have a forward
4905 // declaration followed by an actual declarations in the
4906 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00004907 if (log)
4908 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004909 GetObjectFile()->GetModule()->LogMessage (log.get(),
4910 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
4911 this,
4912 die->GetOffset(),
4913 DW_TAG_value_to_name(tag),
4914 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00004915 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004916
4917 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
4918
4919 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00004920 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004921 // We weren't able to find a full declaration in
4922 // this DWARF, see if we have a declaration anywhere
4923 // else...
4924 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00004925 }
4926
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004927 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00004928 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00004929 if (log)
4930 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004931 GetObjectFile()->GetModule()->LogMessage (log.get(),
4932 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
4933 this,
4934 die->GetOffset(),
4935 DW_TAG_value_to_name(tag),
4936 type_name_cstr,
4937 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00004938 }
4939
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004940 // We found a real definition for this type elsewhere
4941 // so lets use it and cache the fact that we found
4942 // a complete type for this die
4943 m_die_to_type[die] = type_sp.get();
4944 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004945 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004946 }
4947 assert (tag_decl_kind != -1);
4948 bool clang_type_was_created = false;
4949 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4950 if (clang_type == NULL)
4951 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00004952 const DWARFDebugInfoEntry *decl_ctx_die;
4953
4954 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00004955 if (accessibility == eAccessNone && decl_ctx)
4956 {
4957 // Check the decl context that contains this class/struct/union.
4958 // If it is a class we must give it an accessability.
4959 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
4960 if (DeclKindIsCXXClass (containing_decl_kind))
4961 accessibility = default_accessibility;
4962 }
4963
Greg Claytonf0705c82011-10-22 03:33:13 +00004964 if (type_name_cstr && strchr (type_name_cstr, '<'))
4965 {
4966 ClangASTContext::TemplateParameterInfos template_param_infos;
4967 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
4968 {
4969 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00004970 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00004971 type_name_cstr,
4972 tag_decl_kind,
4973 template_param_infos);
4974
4975 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
4976 class_template_decl,
4977 tag_decl_kind,
4978 template_param_infos);
4979 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
4980 clang_type_was_created = true;
4981 }
4982 }
4983
4984 if (!clang_type_was_created)
4985 {
4986 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00004987 clang_type = ast.CreateRecordType (decl_ctx,
4988 accessibility,
4989 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00004990 tag_decl_kind,
Greg Claytonf0705c82011-10-22 03:33:13 +00004991 class_language);
4992 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004993 }
4994
4995 // Store a forward declaration to this class type in case any
4996 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00004997 // types for function prototypes.
4998 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00004999 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005000 this,
5001 type_name_const_str,
5002 byte_size,
5003 NULL,
5004 LLDB_INVALID_UID,
5005 Type::eEncodingIsUID,
5006 &decl,
5007 clang_type,
5008 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00005009
5010 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005011
5012
5013 // Add our type to the unique type map so we don't
5014 // end up creating many copies of the same type over
5015 // and over in the ASTContext for our module
5016 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00005017 unique_ast_entry.m_symfile = this;
5018 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005019 unique_ast_entry.m_die = die;
5020 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00005021 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00005022 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
5023 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005024
Sean Callanan12014a02011-12-08 23:45:45 +00005025 if (!is_forward_declaration)
5026 {
5027 if (die->HasChildren() == false)
5028 {
5029 // No children for this struct/union/class, lets finish it
5030 ast.StartTagDeclarationDefinition (clang_type);
5031 ast.CompleteTagDeclarationDefinition (clang_type);
5032 }
5033 else if (clang_type_was_created)
5034 {
5035 // Leave this as a forward declaration until we need
5036 // to know the details of the type. lldb_private::Type
5037 // will automatically call the SymbolFile virtual function
5038 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
5039 // When the definition needs to be defined.
5040 m_forward_decl_die_to_clang_type[die] = clang_type;
5041 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
5042 ClangASTContext::SetHasExternalStorage (clang_type, true);
5043 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005044 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005045
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005046 }
5047 break;
5048
5049 case DW_TAG_enumeration_type:
5050 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005051 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005052 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005053
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005054 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005055
Greg Claytond88d7592010-09-15 08:33:30 +00005056 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005057 if (num_attributes > 0)
5058 {
5059 uint32_t i;
5060
5061 for (i=0; i<num_attributes; ++i)
5062 {
5063 attr = attributes.AttributeAtIndex(i);
5064 DWARFFormValue form_value;
5065 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5066 {
5067 switch (attr)
5068 {
Greg Clayton7a345282010-11-09 23:46:37 +00005069 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5070 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5071 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005072 case DW_AT_name:
5073 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005074 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005075 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005076 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005077 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00005078 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5079 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005080 case DW_AT_allocated:
5081 case DW_AT_associated:
5082 case DW_AT_bit_stride:
5083 case DW_AT_byte_stride:
5084 case DW_AT_data_location:
5085 case DW_AT_description:
5086 case DW_AT_start_scope:
5087 case DW_AT_visibility:
5088 case DW_AT_specification:
5089 case DW_AT_abstract_origin:
5090 case DW_AT_sibling:
5091 break;
5092 }
5093 }
5094 }
5095
Greg Clayton81c22f62011-10-19 18:09:39 +00005096 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 +00005097
Greg Clayton1be10fc2010-09-29 01:12:09 +00005098 clang_type_t enumerator_clang_type = NULL;
5099 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5100 if (clang_type == NULL)
5101 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005102 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5103 DW_ATE_signed,
5104 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005105 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005106 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005107 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005108 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005109 }
5110 else
5111 {
5112 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5113 assert (enumerator_clang_type != NULL);
5114 }
5115
Greg Claytona2721472011-06-25 00:44:06 +00005116 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5117
Greg Clayton81c22f62011-10-19 18:09:39 +00005118 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005119 this,
5120 type_name_const_str,
5121 byte_size,
5122 NULL,
5123 encoding_uid,
5124 Type::eEncodingIsUID,
5125 &decl,
5126 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005127 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005128
Greg Clayton6beaaa62011-01-17 03:46:26 +00005129 ast.StartTagDeclarationDefinition (clang_type);
5130 if (die->HasChildren())
5131 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005132 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5133 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005134 }
5135 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005136 }
5137 }
5138 break;
5139
Jim Inghamb0be4422010-08-12 01:20:14 +00005140 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005141 case DW_TAG_subprogram:
5142 case DW_TAG_subroutine_type:
5143 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005144 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005145 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005146
5147 const char *mangled = NULL;
5148 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005149 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005150 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005151 bool is_static = false;
5152 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005153 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005154 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005155 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5156 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005157
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005158 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005159 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005160
5161
Greg Claytond88d7592010-09-15 08:33:30 +00005162 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005163 if (num_attributes > 0)
5164 {
5165 uint32_t i;
5166 for (i=0; i<num_attributes; ++i)
5167 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005168 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005169 DWARFFormValue form_value;
5170 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5171 {
5172 switch (attr)
5173 {
5174 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5175 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5176 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5177 case DW_AT_name:
5178 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005179 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005180 break;
5181
5182 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
5183 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005184 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005185 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005186 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5187 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005188 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005189 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5190
Greg Claytonf51de672010-10-01 02:31:07 +00005191
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005192 case DW_AT_external:
5193 if (form_value.Unsigned())
5194 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005195 if (storage == clang::SC_None)
5196 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005197 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005198 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005199 }
5200 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005201
Greg Clayton72da3972011-08-16 18:40:23 +00005202 case DW_AT_specification:
5203 specification_die_offset = form_value.Reference(dwarf_cu);
5204 break;
5205
5206 case DW_AT_abstract_origin:
5207 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5208 break;
5209
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005210 case DW_AT_allocated:
5211 case DW_AT_associated:
5212 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005213 case DW_AT_calling_convention:
5214 case DW_AT_data_location:
5215 case DW_AT_elemental:
5216 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005217 case DW_AT_frame_base:
5218 case DW_AT_high_pc:
5219 case DW_AT_low_pc:
5220 case DW_AT_object_pointer:
5221 case DW_AT_prototyped:
5222 case DW_AT_pure:
5223 case DW_AT_ranges:
5224 case DW_AT_recursive:
5225 case DW_AT_return_addr:
5226 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005227 case DW_AT_start_scope:
5228 case DW_AT_static_link:
5229 case DW_AT_trampoline:
5230 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005231 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005232 case DW_AT_description:
5233 case DW_AT_sibling:
5234 break;
5235 }
5236 }
5237 }
Greg Clayton24739922010-10-13 03:15:28 +00005238 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005239
Greg Clayton81c22f62011-10-19 18:09:39 +00005240 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 +00005241
Greg Clayton24739922010-10-13 03:15:28 +00005242 clang_type_t return_clang_type = NULL;
5243 Type *func_type = NULL;
5244
5245 if (type_die_offset != DW_INVALID_OFFSET)
5246 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005247
Greg Clayton24739922010-10-13 03:15:28 +00005248 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005249 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005250 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005251 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005252
Greg Claytonf51de672010-10-01 02:31:07 +00005253
Greg Clayton24739922010-10-13 03:15:28 +00005254 std::vector<clang_type_t> function_param_types;
5255 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005256
Greg Clayton24739922010-10-13 03:15:28 +00005257 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005258
Greg Claytoncb5860a2011-10-13 23:49:28 +00005259 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5260 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005261 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5262
Greg Claytonf0705c82011-10-22 03:33:13 +00005263 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005264 // Start off static. This will be set to false in ParseChildParameters(...)
5265 // if we find a "this" paramters as the first parameter
5266 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005267 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005268 ClangASTContext::TemplateParameterInfos template_param_infos;
5269
Greg Clayton24739922010-10-13 03:15:28 +00005270 if (die->HasChildren())
5271 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005272 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005273 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005274 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005275 dwarf_cu,
5276 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005277 skip_artificial,
5278 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005279 type_list,
5280 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005281 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005282 type_quals,
5283 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005284 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005285
Greg Clayton24739922010-10-13 03:15:28 +00005286 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005287 clang_type = ast.CreateFunctionType (return_clang_type,
5288 &function_param_types[0],
5289 function_param_types.size(),
5290 is_variadic,
5291 type_quals);
5292
Greg Clayton24739922010-10-13 03:15:28 +00005293 if (type_name_cstr)
5294 {
5295 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005296 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005297 {
Greg Clayton7c810422012-01-18 23:40:49 +00005298 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005299 ConstString class_name_no_category;
5300 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005301 {
Greg Claytone42ae842012-01-19 03:24:53 +00005302 // Use the class name with no category if there is one
5303 if (class_name_no_category)
5304 class_name = class_name_no_category;
5305
Greg Clayton24739922010-10-13 03:15:28 +00005306 SymbolContext empty_sc;
5307 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005308 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005309 {
Greg Clayton24739922010-10-13 03:15:28 +00005310 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005311 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005312
5313 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005314 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005315 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5316 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5317 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005318 }
Greg Clayton24739922010-10-13 03:15:28 +00005319 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005320
Greg Clayton24739922010-10-13 03:15:28 +00005321 if (class_opaque_type)
5322 {
5323 // If accessibility isn't set to anything valid, assume public for
5324 // now...
5325 if (accessibility == eAccessNone)
5326 accessibility = eAccessPublic;
5327
5328 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005329 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5330 type_name_cstr,
5331 clang_type,
5332 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005333 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00005334 type_handled = objc_method_decl != NULL;
5335 }
5336 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005337 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005338 {
5339 // Look at the parent of this DIE and see if is is
5340 // a class or struct and see if this is actually a
5341 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005342 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005343 if (class_type)
5344 {
Greg Clayton72da3972011-08-16 18:40:23 +00005345 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005346 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005347 // We have a specification which we are going to base our function
5348 // prototype off of, so we need this type to be completed so that the
5349 // m_die_to_decl_ctx for the method in the specification has a valid
5350 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005351 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005352 // If we have a specification, then the function type should have been
5353 // made with the specification and not with this die.
5354 DWARFCompileUnitSP spec_cu_sp;
5355 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005356 clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die);
5357 if (spec_clang_decl_ctx)
5358 {
5359 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5360 }
5361 else
Jim Inghamc1663042011-09-29 22:12:35 +00005362 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005363 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5364 MakeUserID(die->GetOffset()),
5365 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005366 }
Greg Clayton72da3972011-08-16 18:40:23 +00005367 type_handled = true;
5368 }
5369 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5370 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005371 // We have a specification which we are going to base our function
5372 // prototype off of, so we need this type to be completed so that the
5373 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5374 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005375 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005376
Greg Clayton72da3972011-08-16 18:40:23 +00005377 DWARFCompileUnitSP abs_cu_sp;
5378 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005379 clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die);
5380 if (abs_clang_decl_ctx)
5381 {
5382 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5383 }
5384 else
Jim Inghamc1663042011-09-29 22:12:35 +00005385 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005386 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5387 MakeUserID(die->GetOffset()),
5388 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005389 }
Greg Clayton72da3972011-08-16 18:40:23 +00005390 type_handled = true;
5391 }
5392 else
5393 {
5394 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5395 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005396 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005397 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005398 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005399 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5400 // in the DWARF for C++ methods... Default to public for now...
5401 if (accessibility == eAccessNone)
5402 accessibility = eAccessPublic;
5403
5404 if (!is_static && !die->HasChildren())
5405 {
5406 // We have a C++ member function with no children (this pointer!)
5407 // and clang will get mad if we try and make a function that isn't
5408 // well formed in the DWARF, so we will just skip it...
5409 type_handled = true;
5410 }
5411 else
5412 {
5413 clang::CXXMethodDecl *cxx_method_decl;
5414 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005415 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 +00005416 type_name_cstr,
5417 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005418 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005419 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5420 m_obj_file->GetFileSpec().GetFilename().GetCString());
5421
Sean Callananc1b732d2011-11-01 18:07:13 +00005422 const bool is_attr_used = false;
5423
Greg Clayton20568dd2011-10-13 23:13:20 +00005424 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5425 type_name_cstr,
5426 clang_type,
5427 accessibility,
5428 is_virtual,
5429 is_static,
5430 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005431 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005432 is_attr_used,
5433 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005434 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5435
Greg Claytonf49e65a2011-11-10 18:31:53 +00005436 Host::SetCrashDescription (NULL);
5437
Greg Clayton20568dd2011-10-13 23:13:20 +00005438 type_handled = cxx_method_decl != NULL;
5439 }
Greg Clayton72da3972011-08-16 18:40:23 +00005440 }
5441 else
5442 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005443 // We were asked to parse the type for a method in a class, yet the
5444 // class hasn't been asked to complete itself through the
5445 // clang::ExternalASTSource protocol, so we need to just have the
5446 // class complete itself and do things the right way, then our
5447 // DIE should then have an entry in the m_die_to_type map. First
5448 // we need to modify the m_die_to_type so it doesn't think we are
5449 // trying to parse this DIE anymore...
5450 m_die_to_type[die] = NULL;
5451
5452 // Now we get the full type to force our class type to complete itself
5453 // using the clang::ExternalASTSource protocol which will parse all
5454 // base classes and all methods (including the method for this DIE).
5455 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005456
Greg Clayton20568dd2011-10-13 23:13:20 +00005457 // The type for this DIE should have been filled in the function call above
5458 type_ptr = m_die_to_type[die];
5459 if (type_ptr)
5460 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005461 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00005462 break;
5463 }
Greg Clayton72da3972011-08-16 18:40:23 +00005464 }
Greg Clayton931180e2011-01-27 06:44:37 +00005465 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005466 }
5467 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005468 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005469 }
Greg Clayton24739922010-10-13 03:15:28 +00005470
5471 if (!type_handled)
5472 {
5473 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005474 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5475 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005476 clang_type,
5477 storage,
5478 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005479
5480// if (template_param_infos.GetSize() > 0)
5481// {
5482// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
5483// function_decl,
5484// type_name_cstr,
5485// template_param_infos);
5486//
5487// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
5488// func_template_decl,
5489// template_param_infos);
5490// }
Greg Clayton24739922010-10-13 03:15:28 +00005491 // Add the decl to our DIE to decl context map
5492 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005493 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005494 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005495 ast.SetFunctionParameters (function_decl,
5496 &function_param_decls.front(),
5497 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00005498 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005499 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005500 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005501 this,
5502 type_name_const_str,
5503 0,
5504 NULL,
5505 LLDB_INVALID_UID,
5506 Type::eEncodingIsUID,
5507 &decl,
5508 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005509 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005510 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005511 }
5512 break;
5513
5514 case DW_TAG_array_type:
5515 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005516 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005517 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005518
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005519 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005520 int64_t first_index = 0;
5521 uint32_t byte_stride = 0;
5522 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005523 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005524
5525 if (num_attributes > 0)
5526 {
5527 uint32_t i;
5528 for (i=0; i<num_attributes; ++i)
5529 {
5530 attr = attributes.AttributeAtIndex(i);
5531 DWARFFormValue form_value;
5532 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5533 {
5534 switch (attr)
5535 {
5536 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5537 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5538 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5539 case DW_AT_name:
5540 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005541 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005542 break;
5543
5544 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005545 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005546 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5547 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005548 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005549 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005550 case DW_AT_allocated:
5551 case DW_AT_associated:
5552 case DW_AT_data_location:
5553 case DW_AT_description:
5554 case DW_AT_ordering:
5555 case DW_AT_start_scope:
5556 case DW_AT_visibility:
5557 case DW_AT_specification:
5558 case DW_AT_abstract_origin:
5559 case DW_AT_sibling:
5560 break;
5561 }
5562 }
5563 }
5564
Greg Clayton81c22f62011-10-19 18:09:39 +00005565 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 +00005566
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005567 Type *element_type = ResolveTypeUID(type_die_offset);
5568
5569 if (element_type)
5570 {
5571 std::vector<uint64_t> element_orders;
5572 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005573 // We have an array that claims to have no members, lets give it at least one member...
5574 if (element_orders.empty())
5575 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005576 if (byte_stride == 0 && bit_stride == 0)
5577 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00005578 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005579 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
5580 uint64_t num_elements = 0;
5581 std::vector<uint64_t>::const_reverse_iterator pos;
5582 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
5583 for (pos = element_orders.rbegin(); pos != end; ++pos)
5584 {
5585 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005586 clang_type = ast.CreateArrayType (array_element_type,
5587 num_elements,
5588 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005589 array_element_type = clang_type;
5590 array_element_bit_stride = array_element_bit_stride * num_elements;
5591 }
5592 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00005593 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005594 this,
5595 empty_name,
5596 array_element_bit_stride / 8,
5597 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00005598 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005599 Type::eEncodingIsUID,
5600 &decl,
5601 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005602 Type::eResolveStateFull));
5603 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005604 }
5605 }
5606 }
5607 break;
5608
Greg Clayton9b81a312010-06-12 01:20:30 +00005609 case DW_TAG_ptr_to_member_type:
5610 {
5611 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
5612 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
5613
Greg Claytond88d7592010-09-15 08:33:30 +00005614 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00005615
5616 if (num_attributes > 0) {
5617 uint32_t i;
5618 for (i=0; i<num_attributes; ++i)
5619 {
5620 attr = attributes.AttributeAtIndex(i);
5621 DWARFFormValue form_value;
5622 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5623 {
5624 switch (attr)
5625 {
5626 case DW_AT_type:
5627 type_die_offset = form_value.Reference(dwarf_cu); break;
5628 case DW_AT_containing_type:
5629 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
5630 }
5631 }
5632 }
5633
5634 Type *pointee_type = ResolveTypeUID(type_die_offset);
5635 Type *class_type = ResolveTypeUID(containing_type_die_offset);
5636
Greg Clayton526e5af2010-11-13 03:52:47 +00005637 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
5638 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00005639
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005640 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
5641 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00005642
Greg Clayton526e5af2010-11-13 03:52:47 +00005643 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
5644 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00005645
Greg Clayton81c22f62011-10-19 18:09:39 +00005646 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005647 this,
5648 type_name_const_str,
5649 byte_size,
5650 NULL,
5651 LLDB_INVALID_UID,
5652 Type::eEncodingIsUID,
5653 NULL,
5654 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005655 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00005656 }
5657
5658 break;
5659 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005660 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00005661 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005662 break;
5663 }
5664
5665 if (type_sp.get())
5666 {
5667 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5668 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5669
5670 SymbolContextScope * symbol_context_scope = NULL;
5671 if (sc_parent_tag == DW_TAG_compile_unit)
5672 {
5673 symbol_context_scope = sc.comp_unit;
5674 }
5675 else if (sc.function != NULL)
5676 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005677 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005678 if (symbol_context_scope == NULL)
5679 symbol_context_scope = sc.function;
5680 }
5681
5682 if (symbol_context_scope != NULL)
5683 {
5684 type_sp->SetSymbolContextScope(symbol_context_scope);
5685 }
5686
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005687 // We are ready to put this type into the uniqued list up at the module level
5688 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00005689
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005690 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005691 }
5692 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00005693 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005694 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005695 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005696 }
5697 }
5698 return type_sp;
5699}
5700
5701size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00005702SymbolFileDWARF::ParseTypes
5703(
5704 const SymbolContext& sc,
5705 DWARFCompileUnit* dwarf_cu,
5706 const DWARFDebugInfoEntry *die,
5707 bool parse_siblings,
5708 bool parse_children
5709)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005710{
5711 size_t types_added = 0;
5712 while (die != NULL)
5713 {
5714 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005715 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005716 {
5717 if (type_is_new)
5718 ++types_added;
5719 }
5720
5721 if (parse_children && die->HasChildren())
5722 {
5723 if (die->Tag() == DW_TAG_subprogram)
5724 {
5725 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00005726 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005727 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
5728 }
5729 else
5730 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
5731 }
5732
5733 if (parse_siblings)
5734 die = die->GetSibling();
5735 else
5736 die = NULL;
5737 }
5738 return types_added;
5739}
5740
5741
5742size_t
5743SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
5744{
5745 assert(sc.comp_unit && sc.function);
5746 size_t functions_added = 0;
5747 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5748 if (dwarf_cu)
5749 {
5750 dw_offset_t function_die_offset = sc.function->GetID();
5751 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
5752 if (function_die)
5753 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00005754 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005755 }
5756 }
5757
5758 return functions_added;
5759}
5760
5761
5762size_t
5763SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
5764{
5765 // At least a compile unit must be valid
5766 assert(sc.comp_unit);
5767 size_t types_added = 0;
5768 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5769 if (dwarf_cu)
5770 {
5771 if (sc.function)
5772 {
5773 dw_offset_t function_die_offset = sc.function->GetID();
5774 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
5775 if (func_die && func_die->HasChildren())
5776 {
5777 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
5778 }
5779 }
5780 else
5781 {
5782 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
5783 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
5784 {
5785 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
5786 }
5787 }
5788 }
5789
5790 return types_added;
5791}
5792
5793size_t
5794SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
5795{
5796 if (sc.comp_unit != NULL)
5797 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00005798 DWARFDebugInfo* info = DebugInfo();
5799 if (info == NULL)
5800 return 0;
5801
5802 uint32_t cu_idx = UINT32_MAX;
5803 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005804
5805 if (dwarf_cu == NULL)
5806 return 0;
5807
5808 if (sc.function)
5809 {
5810 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00005811
5812 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 +00005813 if (func_lo_pc != DW_INVALID_ADDRESS)
5814 {
5815 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00005816
Greg Claytone38a5ed2012-01-05 03:57:59 +00005817 // Let all blocks know they have parse all their variables
5818 sc.function->GetBlock (false).SetDidParseVariables (true, true);
5819 return num_variables;
5820 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005821 }
5822 else if (sc.comp_unit)
5823 {
5824 uint32_t vars_added = 0;
5825 VariableListSP variables (sc.comp_unit->GetVariableList(false));
5826
5827 if (variables.get() == NULL)
5828 {
5829 variables.reset(new VariableList());
5830 sc.comp_unit->SetVariableList(variables);
5831
Greg Claytond4a2b372011-09-12 23:21:58 +00005832 DWARFCompileUnit* match_dwarf_cu = NULL;
5833 const DWARFDebugInfoEntry* die = NULL;
5834 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00005835 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00005836 {
Greg Clayton97fbc342011-10-20 22:30:33 +00005837 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00005838 {
5839 DWARFMappedHash::DIEInfoArray hash_data_array;
5840 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
5841 dwarf_cu->GetNextCompileUnitOffset(),
5842 hash_data_array))
5843 {
5844 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
5845 }
5846 }
Greg Clayton7f995132011-10-04 22:41:51 +00005847 }
5848 else
5849 {
5850 // Index if we already haven't to make sure the compile units
5851 // get indexed and make their global DIE index list
5852 if (!m_indexed)
5853 Index ();
5854
5855 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
5856 dwarf_cu->GetNextCompileUnitOffset(),
5857 die_offsets);
5858 }
5859
5860 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00005861 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005862 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005863 DWARFDebugInfo* debug_info = DebugInfo();
5864 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005865 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005866 const dw_offset_t die_offset = die_offsets[i];
5867 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00005868 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00005869 {
Greg Clayton95d87902011-11-11 03:16:25 +00005870 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
5871 if (var_sp)
5872 {
5873 variables->AddVariableIfUnique (var_sp);
5874 ++vars_added;
5875 }
Greg Claytond4a2b372011-09-12 23:21:58 +00005876 }
Greg Clayton95d87902011-11-11 03:16:25 +00005877 else
5878 {
5879 if (m_using_apple_tables)
5880 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005881 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 +00005882 }
5883 }
5884
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005885 }
5886 }
5887 }
5888 return vars_added;
5889 }
5890 }
5891 return 0;
5892}
5893
5894
5895VariableSP
5896SymbolFileDWARF::ParseVariableDIE
5897(
5898 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005899 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005900 const DWARFDebugInfoEntry *die,
5901 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005902)
5903{
5904
Greg Clayton83c5cd92010-11-14 22:13:40 +00005905 VariableSP var_sp (m_die_to_variable_sp[die]);
5906 if (var_sp)
5907 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005908
5909 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00005910
5911 if ((tag == DW_TAG_variable) ||
5912 (tag == DW_TAG_constant) ||
5913 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005914 {
Greg Clayton7f995132011-10-04 22:41:51 +00005915 DWARFDebugInfoEntry::Attributes attributes;
5916 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
5917 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005918 {
Greg Clayton7f995132011-10-04 22:41:51 +00005919 const char *name = NULL;
5920 const char *mangled = NULL;
5921 Declaration decl;
5922 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00005923 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00005924 DWARFExpression location;
5925 bool is_external = false;
5926 bool is_artificial = false;
5927 bool location_is_const_value_data = false;
5928 AccessType accessibility = eAccessNone;
5929
5930 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005931 {
Greg Clayton7f995132011-10-04 22:41:51 +00005932 dw_attr_t attr = attributes.AttributeAtIndex(i);
5933 DWARFFormValue form_value;
5934 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005935 {
Greg Clayton7f995132011-10-04 22:41:51 +00005936 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005937 {
Greg Clayton7f995132011-10-04 22:41:51 +00005938 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5939 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5940 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5941 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
5942 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00005943 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00005944 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
5945 case DW_AT_const_value:
5946 location_is_const_value_data = true;
5947 // Fall through...
5948 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005949 {
Greg Clayton7f995132011-10-04 22:41:51 +00005950 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005951 {
Greg Clayton7f995132011-10-04 22:41:51 +00005952 const DataExtractor& debug_info_data = get_debug_info_data();
5953
5954 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
5955 uint32_t block_length = form_value.Unsigned();
5956 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
5957 }
5958 else
5959 {
5960 const DataExtractor& debug_loc_data = get_debug_loc_data();
5961 const dw_offset_t debug_loc_offset = form_value.Unsigned();
5962
5963 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
5964 if (loc_list_length > 0)
5965 {
5966 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
5967 assert (func_low_pc != LLDB_INVALID_ADDRESS);
5968 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
5969 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005970 }
5971 }
Greg Clayton7f995132011-10-04 22:41:51 +00005972 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005973
Greg Clayton7f995132011-10-04 22:41:51 +00005974 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5975 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5976 case DW_AT_declaration:
5977 case DW_AT_description:
5978 case DW_AT_endianity:
5979 case DW_AT_segment:
5980 case DW_AT_start_scope:
5981 case DW_AT_visibility:
5982 default:
5983 case DW_AT_abstract_origin:
5984 case DW_AT_sibling:
5985 case DW_AT_specification:
5986 break;
5987 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005988 }
5989 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005990
Greg Clayton7f995132011-10-04 22:41:51 +00005991 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005992 {
Greg Clayton7f995132011-10-04 22:41:51 +00005993 ValueType scope = eValueTypeInvalid;
5994
5995 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5996 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005997 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00005998
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005999 // DWARF doesn't specify if a DW_TAG_variable is a local, global
6000 // or static variable, so we have to do a little digging by
6001 // looking at the location of a varaible to see if it contains
6002 // a DW_OP_addr opcode _somewhere_ in the definition. I say
6003 // somewhere because clang likes to combine small global variables
6004 // into the same symbol and have locations like:
6005 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
6006 // So if we don't have a DW_TAG_formal_parameter, we can look at
6007 // the location to see if it contains a DW_OP_addr opcode, and
6008 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00006009 if (tag == DW_TAG_formal_parameter)
6010 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00006011 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006012 {
Greg Clayton96c09682012-01-04 22:56:43 +00006013 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00006014 // Check if the location has a DW_OP_addr with any address value...
6015 addr_t location_has_op_addr = false;
6016 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00006017 {
6018 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
6019 if (op_error)
6020 {
6021 StreamString strm;
6022 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00006023 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 +00006024 }
6025 }
Greg Claytond1767f02011-12-08 02:13:16 +00006026
6027 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006028 {
Greg Claytond1767f02011-12-08 02:13:16 +00006029 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006030 {
Greg Claytond1767f02011-12-08 02:13:16 +00006031 scope = eValueTypeVariableGlobal;
6032
6033 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006034 {
Greg Claytond1767f02011-12-08 02:13:16 +00006035 // When leaving the DWARF in the .o files on darwin,
6036 // when we have a global variable that wasn't initialized,
6037 // the .o file might not have allocated a virtual
6038 // address for the global variable. In this case it will
6039 // have created a symbol for the global variable
6040 // that is undefined and external and the value will
6041 // be the byte size of the variable. When we do the
6042 // address map in SymbolFileDWARFDebugMap we rely on
6043 // having an address, we need to do some magic here
6044 // so we can get the correct address for our global
6045 // variable. The address for all of these entries
6046 // will be zero, and there will be an undefined symbol
6047 // in this object file, and the executable will have
6048 // a matching symbol with a good address. So here we
6049 // dig up the correct address and replace it in the
6050 // location for the variable, and set the variable's
6051 // symbol context scope to be that of the main executable
6052 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006053 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006054 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006055
Greg Claytond1767f02011-12-08 02:13:16 +00006056 // we have a possible uninitialized extern global
6057 Symtab *symtab = m_obj_file->GetSymtab();
6058 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006059 {
Greg Claytond1767f02011-12-08 02:13:16 +00006060 ConstString const_name(name);
6061 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6062 eSymbolTypeUndefined,
6063 Symtab::eDebugNo,
6064 Symtab::eVisibilityExtern);
6065
6066 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006067 {
Greg Claytond1767f02011-12-08 02:13:16 +00006068 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6069 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006070 {
Greg Claytond1767f02011-12-08 02:13:16 +00006071 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6072 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6073 eSymbolTypeData,
6074 Symtab::eDebugYes,
6075 Symtab::eVisibilityExtern);
6076 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006077 {
Greg Claytone7612132012-03-07 21:03:09 +00006078 if (defined_symbol->ValueIsAddress())
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006079 {
Greg Claytone7612132012-03-07 21:03:09 +00006080 const addr_t defined_addr = defined_symbol->GetAddress().GetFileAddress();
Greg Claytond1767f02011-12-08 02:13:16 +00006081 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006082 {
Greg Claytond1767f02011-12-08 02:13:16 +00006083 if (location.Update_DW_OP_addr (defined_addr))
6084 {
6085 symbol_context_scope = defined_symbol;
6086 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006087 }
6088 }
6089 }
6090 }
6091 }
6092 }
6093 }
6094 }
6095 }
Greg Claytond1767f02011-12-08 02:13:16 +00006096 else
6097 {
6098 scope = eValueTypeVariableStatic;
6099 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006100 }
6101 else
Greg Claytond1767f02011-12-08 02:13:16 +00006102 {
6103 scope = eValueTypeVariableLocal;
6104 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006105 }
Greg Clayton7f995132011-10-04 22:41:51 +00006106
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006107 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006108 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006109 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006110 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006111 case DW_TAG_subprogram:
6112 case DW_TAG_inlined_subroutine:
6113 case DW_TAG_lexical_block:
6114 if (sc.function)
6115 {
6116 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6117 if (symbol_context_scope == NULL)
6118 symbol_context_scope = sc.function;
6119 }
6120 break;
6121
6122 default:
6123 symbol_context_scope = sc.comp_unit;
6124 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006125 }
Greg Clayton7f995132011-10-04 22:41:51 +00006126 }
6127
Greg Clayton5cf58b92011-10-05 22:22:08 +00006128 if (symbol_context_scope)
6129 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006130 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6131 name,
6132 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006133 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006134 scope,
6135 symbol_context_scope,
6136 &decl,
6137 location,
6138 is_external,
6139 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006140
6141 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6142 }
6143 else
6144 {
6145 // Not ready to parse this variable yet. It might be a global
6146 // or static variable that is in a function scope and the function
6147 // in the symbol context wasn't filled in yet
6148 return var_sp;
6149 }
Greg Clayton7f995132011-10-04 22:41:51 +00006150 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006151 }
Greg Clayton7f995132011-10-04 22:41:51 +00006152 // Cache var_sp even if NULL (the variable was just a specification or
6153 // was missing vital information to be able to be displayed in the debugger
6154 // (missing location due to optimization, etc)) so we don't re-parse
6155 // this DIE over and over later...
6156 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006157 }
6158 return var_sp;
6159}
6160
Greg Claytonc662ec82011-06-17 22:10:16 +00006161
6162const DWARFDebugInfoEntry *
6163SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6164 dw_offset_t spec_block_die_offset,
6165 DWARFCompileUnit **result_die_cu_handle)
6166{
6167 // Give the concrete function die specified by "func_die_offset", find the
6168 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6169 // to "spec_block_die_offset"
6170 DWARFDebugInfo* info = DebugInfo();
6171
6172 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6173 if (die)
6174 {
6175 assert (*result_die_cu_handle);
6176 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6177 }
6178 return NULL;
6179}
6180
6181
6182const DWARFDebugInfoEntry *
6183SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6184 const DWARFDebugInfoEntry *die,
6185 dw_offset_t spec_block_die_offset,
6186 DWARFCompileUnit **result_die_cu_handle)
6187{
6188 if (die)
6189 {
6190 switch (die->Tag())
6191 {
6192 case DW_TAG_subprogram:
6193 case DW_TAG_inlined_subroutine:
6194 case DW_TAG_lexical_block:
6195 {
6196 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6197 {
6198 *result_die_cu_handle = dwarf_cu;
6199 return die;
6200 }
6201
6202 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6203 {
6204 *result_die_cu_handle = dwarf_cu;
6205 return die;
6206 }
6207 }
6208 break;
6209 }
6210
6211 // Give the concrete function die specified by "func_die_offset", find the
6212 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6213 // to "spec_block_die_offset"
6214 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6215 {
6216 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6217 child_die,
6218 spec_block_die_offset,
6219 result_die_cu_handle);
6220 if (result_die)
6221 return result_die;
6222 }
6223 }
6224
6225 *result_die_cu_handle = NULL;
6226 return NULL;
6227}
6228
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006229size_t
6230SymbolFileDWARF::ParseVariables
6231(
6232 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006233 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006234 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006235 const DWARFDebugInfoEntry *orig_die,
6236 bool parse_siblings,
6237 bool parse_children,
6238 VariableList* cc_variable_list
6239)
6240{
6241 if (orig_die == NULL)
6242 return 0;
6243
Greg Claytonc662ec82011-06-17 22:10:16 +00006244 VariableListSP variable_list_sp;
6245
6246 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006247 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006248 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006249 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006250 dw_tag_t tag = die->Tag();
6251
6252 // Check to see if we have already parsed this variable or constant?
6253 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006254 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006255 if (cc_variable_list)
6256 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006257 }
6258 else
6259 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006260 // We haven't already parsed it, lets do that now.
6261 if ((tag == DW_TAG_variable) ||
6262 (tag == DW_TAG_constant) ||
6263 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006264 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006265 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006266 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006267 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6268 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6269 switch (parent_tag)
6270 {
6271 case DW_TAG_compile_unit:
6272 if (sc.comp_unit != NULL)
6273 {
6274 variable_list_sp = sc.comp_unit->GetVariableList(false);
6275 if (variable_list_sp.get() == NULL)
6276 {
6277 variable_list_sp.reset(new VariableList());
6278 sc.comp_unit->SetVariableList(variable_list_sp);
6279 }
6280 }
6281 else
6282 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006283 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6284 MakeUserID(sc_parent_die->GetOffset()),
6285 DW_TAG_value_to_name (parent_tag),
6286 MakeUserID(orig_die->GetOffset()),
6287 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006288 }
6289 break;
6290
6291 case DW_TAG_subprogram:
6292 case DW_TAG_inlined_subroutine:
6293 case DW_TAG_lexical_block:
6294 if (sc.function != NULL)
6295 {
6296 // Check to see if we already have parsed the variables for the given scope
6297
Greg Clayton81c22f62011-10-19 18:09:39 +00006298 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006299 if (block == NULL)
6300 {
6301 // This must be a specification or abstract origin with
6302 // a concrete block couterpart in the current function. We need
6303 // to find the concrete block so we can correctly add the
6304 // variable to it
6305 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6306 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6307 sc_parent_die->GetOffset(),
6308 &concrete_block_die_cu);
6309 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006310 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006311 }
6312
6313 if (block != NULL)
6314 {
6315 const bool can_create = false;
6316 variable_list_sp = block->GetBlockVariableList (can_create);
6317 if (variable_list_sp.get() == NULL)
6318 {
6319 variable_list_sp.reset(new VariableList());
6320 block->SetVariableList(variable_list_sp);
6321 }
6322 }
6323 }
6324 break;
6325
6326 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006327 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6328 MakeUserID(orig_die->GetOffset()),
6329 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006330 break;
6331 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006332 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006333
6334 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006335 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006336 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6337 if (var_sp)
6338 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006339 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006340 if (cc_variable_list)
6341 cc_variable_list->AddVariableIfUnique (var_sp);
6342 ++vars_added;
6343 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006344 }
6345 }
6346 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006347
6348 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6349
6350 if (!skip_children && parse_children && die->HasChildren())
6351 {
6352 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6353 }
6354
6355 if (parse_siblings)
6356 die = die->GetSibling();
6357 else
6358 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006359 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006360 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006361}
6362
6363//------------------------------------------------------------------
6364// PluginInterface protocol
6365//------------------------------------------------------------------
6366const char *
6367SymbolFileDWARF::GetPluginName()
6368{
6369 return "SymbolFileDWARF";
6370}
6371
6372const char *
6373SymbolFileDWARF::GetShortPluginName()
6374{
6375 return GetPluginNameStatic();
6376}
6377
6378uint32_t
6379SymbolFileDWARF::GetPluginVersion()
6380{
6381 return 1;
6382}
6383
6384void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006385SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6386{
6387 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6388 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6389 if (clang_type)
6390 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6391}
6392
6393void
6394SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6395{
6396 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6397 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6398 if (clang_type)
6399 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6400}
6401
Greg Claytona2721472011-06-25 00:44:06 +00006402void
Sean Callanancc427fa2011-07-30 02:42:06 +00006403SymbolFileDWARF::DumpIndexes ()
6404{
6405 StreamFile s(stdout, false);
6406
6407 s.Printf ("DWARF index for (%s) '%s/%s':",
6408 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6409 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6410 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6411 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6412 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6413 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6414 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6415 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6416 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6417 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6418 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6419}
6420
6421void
6422SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6423 const char *name,
6424 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006425{
Sean Callanancc427fa2011-07-30 02:42:06 +00006426 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006427
6428 if (iter == m_decl_ctx_to_die.end())
6429 return;
6430
Greg Claytoncb5860a2011-10-13 23:49:28 +00006431 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006432 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006433 const DWARFDebugInfoEntry *context_die = *pos;
6434
6435 if (!results)
6436 return;
6437
6438 DWARFDebugInfo* info = DebugInfo();
6439
6440 DIEArray die_offsets;
6441
6442 DWARFCompileUnit* dwarf_cu = NULL;
6443 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00006444
6445 if (m_using_apple_tables)
6446 {
6447 if (m_apple_types_ap.get())
6448 m_apple_types_ap->FindByName (name, die_offsets);
6449 }
6450 else
6451 {
6452 if (!m_indexed)
6453 Index ();
6454
6455 m_type_index.Find (ConstString(name), die_offsets);
6456 }
6457
Greg Clayton220a0072011-12-09 08:48:30 +00006458 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006459
6460 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006461 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006462 for (size_t i = 0; i < num_matches; ++i)
6463 {
6464 const dw_offset_t die_offset = die_offsets[i];
6465 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006466
Greg Claytoncb5860a2011-10-13 23:49:28 +00006467 if (die->GetParent() != context_die)
6468 continue;
6469
6470 Type *matching_type = ResolveType (dwarf_cu, die);
6471
Greg Clayton42ce2f32011-12-12 21:50:19 +00006472 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006473 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6474
6475 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6476 {
6477 clang::TagDecl *tag_decl = tag_type->getDecl();
6478 results->push_back(tag_decl);
6479 }
6480 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6481 {
6482 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6483 results->push_back(typedef_decl);
6484 }
Greg Claytona2721472011-06-25 00:44:06 +00006485 }
6486 }
6487 }
6488}
6489
6490void
6491SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006492 const clang::DeclContext *decl_context,
6493 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006494 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6495{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006496
6497 switch (decl_context->getDeclKind())
6498 {
6499 case clang::Decl::Namespace:
6500 case clang::Decl::TranslationUnit:
6501 {
6502 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6503 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6504 }
6505 break;
6506 default:
6507 break;
6508 }
Greg Claytona2721472011-06-25 00:44:06 +00006509}
Greg Claytoncaab74e2012-01-28 00:48:57 +00006510
6511bool
6512SymbolFileDWARF::LayoutRecordType (void *baton,
6513 const clang::RecordDecl *record_decl,
6514 uint64_t &size,
6515 uint64_t &alignment,
6516 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6517 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6518 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6519{
6520 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6521 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
6522}
6523
6524
6525bool
6526SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
6527 uint64_t &bit_size,
6528 uint64_t &alignment,
6529 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6530 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6531 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6532{
6533 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
6534 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
6535 bool success = false;
6536 base_offsets.clear();
6537 vbase_offsets.clear();
6538 if (pos != m_record_decl_to_layout_map.end())
6539 {
6540 bit_size = pos->second.bit_size;
6541 alignment = pos->second.alignment;
6542 field_offsets.swap(pos->second.field_offsets);
6543 m_record_decl_to_layout_map.erase(pos);
6544 success = true;
6545 }
6546 else
6547 {
6548 bit_size = 0;
6549 alignment = 0;
6550 field_offsets.clear();
6551 }
6552
6553 if (log)
6554 GetObjectFile()->GetModule()->LogMessage (log.get(),
6555 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
6556 record_decl,
6557 bit_size,
6558 alignment,
6559 (uint32_t)field_offsets.size(),
6560 (uint32_t)base_offsets.size(),
6561 (uint32_t)vbase_offsets.size(),
6562 success);
6563 return success;
6564}
6565
6566
6567