blob: 56fc593c1b8424a6ea8abf73675edf498e50ee16 [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)
306 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
307}
308
309static const ConstString &
310GetDWARFMachOSegmentName ()
311{
312 static ConstString g_dwarf_section_name ("__DWARF");
313 return g_dwarf_section_name;
314}
315
Greg Claytone576ab22011-02-15 00:19:15 +0000316UniqueDWARFASTTypeMap &
317SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
318{
319 if (m_debug_map_symfile)
320 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
321 return m_unique_ast_type_map;
322}
323
Greg Clayton6beaaa62011-01-17 03:46:26 +0000324ClangASTContext &
325SymbolFileDWARF::GetClangASTContext ()
326{
327 if (m_debug_map_symfile)
328 return m_debug_map_symfile->GetClangASTContext ();
329
330 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
331 if (!m_is_external_ast_source)
332 {
333 m_is_external_ast_source = true;
334 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
335 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
336 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000337 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Claytoncaab74e2012-01-28 00:48:57 +0000338 SymbolFileDWARF::LayoutRecordType,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000339 this));
Greg Clayton6beaaa62011-01-17 03:46:26 +0000340 ast.SetExternalSource (ast_source_ap);
341 }
342 return ast;
343}
344
345void
346SymbolFileDWARF::InitializeObject()
347{
348 // Install our external AST source callbacks so we can complete Clang types.
349 Module *module = m_obj_file->GetModule();
350 if (module)
351 {
352 const SectionList *section_list = m_obj_file->GetSectionList();
353
354 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
355
356 // Memory map the DWARF mach-o segment so we have everything mmap'ed
357 // to keep our heap memory usage down.
358 if (section)
Greg Claytonc9660542012-02-05 02:38:54 +0000359 m_obj_file->MemoryMapSectionData(section, m_dwarf_data);
Greg Clayton6beaaa62011-01-17 03:46:26 +0000360 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000361 get_apple_names_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000362 if (m_data_apple_names.GetByteSize() > 0)
363 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000364 m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), ".apple_names"));
365 if (m_apple_names_ap->IsValid())
366 m_using_apple_tables = true;
367 else
Greg Clayton7f995132011-10-04 22:41:51 +0000368 m_apple_names_ap.reset();
369 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000370 get_apple_types_data();
Greg Clayton7f995132011-10-04 22:41:51 +0000371 if (m_data_apple_types.GetByteSize() > 0)
372 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000373 m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), ".apple_types"));
374 if (m_apple_types_ap->IsValid())
375 m_using_apple_tables = true;
376 else
Greg Clayton7f995132011-10-04 22:41:51 +0000377 m_apple_types_ap.reset();
378 }
379
380 get_apple_namespaces_data();
381 if (m_data_apple_namespaces.GetByteSize() > 0)
382 {
Greg Clayton97fbc342011-10-20 22:30:33 +0000383 m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), ".apple_namespaces"));
384 if (m_apple_namespaces_ap->IsValid())
385 m_using_apple_tables = true;
386 else
Greg Clayton7f995132011-10-04 22:41:51 +0000387 m_apple_namespaces_ap.reset();
388 }
Greg Clayton4d01ace2011-09-29 16:58:15 +0000389
Greg Clayton5009f9d2011-10-27 17:55:14 +0000390 get_apple_objc_data();
391 if (m_data_apple_objc.GetByteSize() > 0)
392 {
393 m_apple_objc_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_objc, get_debug_str_data(), ".apple_objc"));
394 if (m_apple_objc_ap->IsValid())
395 m_using_apple_tables = true;
396 else
397 m_apple_objc_ap.reset();
398 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000399}
400
401bool
402SymbolFileDWARF::SupportedVersion(uint16_t version)
403{
404 return version == 2 || version == 3;
405}
406
407uint32_t
Sean Callananbfaf54d2011-12-03 04:38:43 +0000408SymbolFileDWARF::CalculateAbilities ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409{
410 uint32_t abilities = 0;
411 if (m_obj_file != NULL)
412 {
413 const Section* section = NULL;
414 const SectionList *section_list = m_obj_file->GetSectionList();
415 if (section_list == NULL)
416 return 0;
417
418 uint64_t debug_abbrev_file_size = 0;
419 uint64_t debug_aranges_file_size = 0;
420 uint64_t debug_frame_file_size = 0;
421 uint64_t debug_info_file_size = 0;
422 uint64_t debug_line_file_size = 0;
423 uint64_t debug_loc_file_size = 0;
424 uint64_t debug_macinfo_file_size = 0;
425 uint64_t debug_pubnames_file_size = 0;
426 uint64_t debug_pubtypes_file_size = 0;
427 uint64_t debug_ranges_file_size = 0;
428 uint64_t debug_str_file_size = 0;
429
Greg Clayton6beaaa62011-01-17 03:46:26 +0000430 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000431
432 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000433 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000434
Greg Clayton4ceb9982010-07-21 22:54:26 +0000435 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000436 if (section != NULL)
437 {
438 debug_info_file_size = section->GetByteSize();
439
Greg Clayton4ceb9982010-07-21 22:54:26 +0000440 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000441 if (section)
442 debug_abbrev_file_size = section->GetByteSize();
443 else
444 m_flags.Set (flagsGotDebugAbbrevData);
445
Greg Clayton4ceb9982010-07-21 22:54:26 +0000446 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000447 if (section)
448 debug_aranges_file_size = section->GetByteSize();
449 else
450 m_flags.Set (flagsGotDebugArangesData);
451
Greg Clayton4ceb9982010-07-21 22:54:26 +0000452 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000453 if (section)
454 debug_frame_file_size = section->GetByteSize();
455 else
456 m_flags.Set (flagsGotDebugFrameData);
457
Greg Clayton4ceb9982010-07-21 22:54:26 +0000458 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000459 if (section)
460 debug_line_file_size = section->GetByteSize();
461 else
462 m_flags.Set (flagsGotDebugLineData);
463
Greg Clayton4ceb9982010-07-21 22:54:26 +0000464 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000465 if (section)
466 debug_loc_file_size = section->GetByteSize();
467 else
468 m_flags.Set (flagsGotDebugLocData);
469
Greg Clayton4ceb9982010-07-21 22:54:26 +0000470 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000471 if (section)
472 debug_macinfo_file_size = section->GetByteSize();
473 else
474 m_flags.Set (flagsGotDebugMacInfoData);
475
Greg Clayton4ceb9982010-07-21 22:54:26 +0000476 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000477 if (section)
478 debug_pubnames_file_size = section->GetByteSize();
479 else
480 m_flags.Set (flagsGotDebugPubNamesData);
481
Greg Clayton4ceb9982010-07-21 22:54:26 +0000482 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000483 if (section)
484 debug_pubtypes_file_size = section->GetByteSize();
485 else
486 m_flags.Set (flagsGotDebugPubTypesData);
487
Greg Clayton4ceb9982010-07-21 22:54:26 +0000488 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000489 if (section)
490 debug_ranges_file_size = section->GetByteSize();
491 else
492 m_flags.Set (flagsGotDebugRangesData);
493
Greg Clayton4ceb9982010-07-21 22:54:26 +0000494 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000495 if (section)
496 debug_str_file_size = section->GetByteSize();
497 else
498 m_flags.Set (flagsGotDebugStrData);
499 }
500
501 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
502 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
503
504 if (debug_line_file_size > 0)
505 abilities |= LineTables;
506
507 if (debug_aranges_file_size > 0)
508 abilities |= AddressAcceleratorTable;
509
510 if (debug_pubnames_file_size > 0)
511 abilities |= FunctionAcceleratorTable;
512
513 if (debug_pubtypes_file_size > 0)
514 abilities |= TypeAcceleratorTable;
515
516 if (debug_macinfo_file_size > 0)
517 abilities |= MacroInformation;
518
519 if (debug_frame_file_size > 0)
520 abilities |= CallFrameInformation;
521 }
522 return abilities;
523}
524
525const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000526SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000527{
528 if (m_flags.IsClear (got_flag))
529 {
530 m_flags.Set (got_flag);
531 const SectionList *section_list = m_obj_file->GetSectionList();
532 if (section_list)
533 {
Greg Clayton4ceb9982010-07-21 22:54:26 +0000534 Section *section = section_list->FindSectionByType(sect_type, true).get();
535 if (section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000536 {
537 // See if we memory mapped the DWARF segment?
538 if (m_dwarf_data.GetByteSize())
539 {
540 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
541 }
542 else
543 {
Greg Claytonc9660542012-02-05 02:38:54 +0000544 if (m_obj_file->ReadSectionData (section, data) == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000545 data.Clear();
546 }
547 }
548 }
549 }
550 return data;
551}
552
553const DataExtractor&
554SymbolFileDWARF::get_debug_abbrev_data()
555{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000556 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000557}
558
559const DataExtractor&
Greg Claytond4a2b372011-09-12 23:21:58 +0000560SymbolFileDWARF::get_debug_aranges_data()
561{
562 return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
563}
564
565const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000566SymbolFileDWARF::get_debug_frame_data()
567{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000568 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000569}
570
571const DataExtractor&
572SymbolFileDWARF::get_debug_info_data()
573{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000574 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000575}
576
577const DataExtractor&
578SymbolFileDWARF::get_debug_line_data()
579{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000580 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000581}
582
583const DataExtractor&
584SymbolFileDWARF::get_debug_loc_data()
585{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000586 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587}
588
589const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590SymbolFileDWARF::get_debug_ranges_data()
591{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000592 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000593}
594
595const DataExtractor&
596SymbolFileDWARF::get_debug_str_data()
597{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000598 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000599}
600
Greg Claytonf9eec202011-09-01 23:16:13 +0000601const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000602SymbolFileDWARF::get_apple_names_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000603{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000604 return GetCachedSectionData (flagsGotAppleNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
Greg Claytonf9eec202011-09-01 23:16:13 +0000605}
606
607const DataExtractor&
Greg Clayton17674402011-09-28 17:06:40 +0000608SymbolFileDWARF::get_apple_types_data()
Greg Claytonf9eec202011-09-01 23:16:13 +0000609{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000610 return GetCachedSectionData (flagsGotAppleTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
Greg Claytonf9eec202011-09-01 23:16:13 +0000611}
612
Greg Clayton7f995132011-10-04 22:41:51 +0000613const DataExtractor&
614SymbolFileDWARF::get_apple_namespaces_data()
615{
Greg Clayton5009f9d2011-10-27 17:55:14 +0000616 return GetCachedSectionData (flagsGotAppleNamespacesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
617}
618
619const DataExtractor&
620SymbolFileDWARF::get_apple_objc_data()
621{
622 return GetCachedSectionData (flagsGotAppleObjCData, eSectionTypeDWARFAppleObjC, m_data_apple_objc);
Greg Clayton7f995132011-10-04 22:41:51 +0000623}
624
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000625
626DWARFDebugAbbrev*
627SymbolFileDWARF::DebugAbbrev()
628{
629 if (m_abbr.get() == NULL)
630 {
631 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
632 if (debug_abbrev_data.GetByteSize() > 0)
633 {
634 m_abbr.reset(new DWARFDebugAbbrev());
635 if (m_abbr.get())
636 m_abbr->Parse(debug_abbrev_data);
637 }
638 }
639 return m_abbr.get();
640}
641
642const DWARFDebugAbbrev*
643SymbolFileDWARF::DebugAbbrev() const
644{
645 return m_abbr.get();
646}
647
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648
649DWARFDebugInfo*
650SymbolFileDWARF::DebugInfo()
651{
652 if (m_info.get() == NULL)
653 {
654 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
655 if (get_debug_info_data().GetByteSize() > 0)
656 {
657 m_info.reset(new DWARFDebugInfo());
658 if (m_info.get())
659 {
660 m_info->SetDwarfData(this);
661 }
662 }
663 }
664 return m_info.get();
665}
666
667const DWARFDebugInfo*
668SymbolFileDWARF::DebugInfo() const
669{
670 return m_info.get();
671}
672
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000673DWARFCompileUnit*
674SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
675{
676 DWARFDebugInfo* info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +0000677 if (info && UserIDMatches(cu_uid))
678 return info->GetCompileUnit((dw_offset_t)cu_uid).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000679 return NULL;
680}
681
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000682
683DWARFDebugRanges*
684SymbolFileDWARF::DebugRanges()
685{
686 if (m_ranges.get() == NULL)
687 {
688 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
689 if (get_debug_ranges_data().GetByteSize() > 0)
690 {
691 m_ranges.reset(new DWARFDebugRanges());
692 if (m_ranges.get())
693 m_ranges->Extract(this);
694 }
695 }
696 return m_ranges.get();
697}
698
699const DWARFDebugRanges*
700SymbolFileDWARF::DebugRanges() const
701{
702 return m_ranges.get();
703}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000704
705bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000706SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000707{
Greg Clayton96d7d742010-11-10 23:42:09 +0000708 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000709 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000710 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000711 if (cu_die)
712 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000713 const char * cu_die_name = cu_die->GetName(this, curr_cu);
714 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
Jim Ingham0f35ac22011-08-24 23:34:20 +0000715 LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000716 if (cu_die_name)
717 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000718 FileSpec cu_file_spec;
719
Greg Clayton7bd65b92011-02-09 23:39:34 +0000720 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000721 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000722 // If we have a full path to the compile unit, we don't need to resolve
723 // the file. This can be expensive e.g. when the source files are NFS mounted.
724 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000725 }
726 else
727 {
728 std::string fullpath(cu_comp_dir);
729 if (*fullpath.rbegin() != '/')
730 fullpath += '/';
731 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000732 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000733 }
734
Greg Clayton81c22f62011-10-19 18:09:39 +0000735 compile_unit_sp.reset(new CompileUnit (m_obj_file->GetModule(),
736 curr_cu,
737 cu_file_spec,
738 MakeUserID(curr_cu->GetOffset()),
739 cu_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000740 if (compile_unit_sp.get())
741 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000742 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000743 return true;
744 }
745 }
746 }
747 }
748 return false;
749}
750
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000751uint32_t
752SymbolFileDWARF::GetNumCompileUnits()
753{
754 DWARFDebugInfo* info = DebugInfo();
755 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000756 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000757 return 0;
758}
759
760CompUnitSP
761SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
762{
763 CompUnitSP comp_unit;
764 DWARFDebugInfo* info = DebugInfo();
765 if (info)
766 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000767 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
768 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000769 {
770 // Our symbol vendor shouldn't be asking us to add a compile unit that
771 // has already been added to it, which this DWARF plug-in knows as it
772 // stores the lldb compile unit (CompileUnit) pointer in each
773 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000774 assert(curr_cu->GetUserData() == NULL);
775 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000776 }
777 }
778 return comp_unit;
779}
780
781static void
Greg Claytonea3e7d52011-10-08 00:49:15 +0000782AddRangesToBlock (Block& block,
783 DWARFDebugRanges::RangeList& ranges,
784 addr_t block_base_addr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000785{
Greg Claytonea3e7d52011-10-08 00:49:15 +0000786 const size_t num_ranges = ranges.GetSize();
787 for (size_t i = 0; i<num_ranges; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000788 {
Greg Claytonea3e7d52011-10-08 00:49:15 +0000789 const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
790 const addr_t range_base = range.GetRangeBase();
791 assert (range_base >= block_base_addr);
792 block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000793 }
Greg Claytonea3e7d52011-10-08 00:49:15 +0000794 block.FinalizeRanges ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000795}
796
797
798Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000799SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000800{
801 DWARFDebugRanges::RangeList func_ranges;
802 const char *name = NULL;
803 const char *mangled = NULL;
804 int decl_file = 0;
805 int decl_line = 0;
806 int decl_column = 0;
807 int call_file = 0;
808 int call_line = 0;
809 int call_column = 0;
810 DWARFExpression frame_base;
811
Greg Claytonc93237c2010-10-01 20:48:32 +0000812 assert (die->Tag() == DW_TAG_subprogram);
813
814 if (die->Tag() != DW_TAG_subprogram)
815 return NULL;
816
Greg Clayton3c2e3ae2012-02-06 06:42:51 +0000817 if (die->GetDIENamesAndRanges (this,
818 dwarf_cu,
819 name,
820 mangled,
821 func_ranges,
822 decl_file,
823 decl_line,
824 decl_column,
825 call_file,
826 call_line,
827 call_column,
828 &frame_base))
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000829 {
830 // Union of all ranges in the function DIE (if the function is discontiguous)
831 AddressRange func_range;
Greg Claytonea3e7d52011-10-08 00:49:15 +0000832 lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
833 lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000834 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
835 {
836 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
837 if (func_range.GetBaseAddress().IsValid())
838 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
839 }
840
841 if (func_range.GetBaseAddress().IsValid())
842 {
843 Mangled func_name;
844 if (mangled)
845 func_name.SetValue(mangled, true);
846 else if (name)
847 func_name.SetValue(name, false);
848
849 FunctionSP func_sp;
850 std::auto_ptr<Declaration> decl_ap;
851 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000852 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
853 decl_line,
854 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000855
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000856 // Supply the type _only_ if it has already been parsed
Greg Clayton594e5ed2010-09-27 21:07:38 +0000857 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000858
859 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
860
861 func_range.GetBaseAddress().ResolveLinkedAddress();
862
Greg Clayton81c22f62011-10-19 18:09:39 +0000863 const user_id_t func_user_id = MakeUserID(die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000864 func_sp.reset(new Function (sc.comp_unit,
Greg Clayton81c22f62011-10-19 18:09:39 +0000865 func_user_id, // UserID is the DIE offset
866 func_user_id,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000867 func_name,
868 func_type,
869 func_range)); // first address range
870
871 if (func_sp.get() != NULL)
872 {
Greg Clayton0bd4e1b2011-09-30 20:52:25 +0000873 if (frame_base.IsValid())
874 func_sp->GetFrameBaseExpression() = frame_base;
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000875 sc.comp_unit->AddFunction(func_sp);
876 return func_sp.get();
877 }
878 }
879 }
880 return NULL;
881}
882
883size_t
884SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
885{
886 assert (sc.comp_unit);
887 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000888 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000889 if (dwarf_cu)
890 {
891 DWARFDIECollection function_dies;
892 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
893 size_t func_idx;
894 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
895 {
896 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
Greg Clayton81c22f62011-10-19 18:09:39 +0000897 if (sc.comp_unit->FindFunctionByUID (MakeUserID(die->GetOffset())).get() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000898 {
899 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
900 ++functions_added;
901 }
902 }
903 //FixupTypes();
904 }
905 return functions_added;
906}
907
908bool
909SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
910{
911 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000912 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
913 assert (curr_cu);
914 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000915
916 if (cu_die)
917 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000918 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
919 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 +0000920
921 // All file indexes in DWARF are one based and a file of index zero is
922 // supposed to be the compile unit itself.
923 support_files.Append (*sc.comp_unit);
924
925 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
926 }
927 return false;
928}
929
930struct ParseDWARFLineTableCallbackInfo
931{
932 LineTable* line_table;
933 const SectionList *section_list;
934 lldb::addr_t prev_sect_file_base_addr;
935 lldb::addr_t curr_sect_file_base_addr;
936 bool is_oso_for_debug_map;
937 bool prev_in_final_executable;
938 DWARFDebugLine::Row prev_row;
939 SectionSP prev_section_sp;
940 SectionSP curr_section_sp;
941};
942
943//----------------------------------------------------------------------
944// ParseStatementTableCallback
945//----------------------------------------------------------------------
946static void
947ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
948{
949 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
950 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
951 {
952 // Just started parsing the line table
953 }
954 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
955 {
956 // Done parsing line table, nothing to do for the cleanup
957 }
958 else
959 {
960 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
961 // We have a new row, lets append it
962
963 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
964 {
965 info->prev_section_sp = info->curr_section_sp;
966 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
967 // If this is an end sequence entry, then we subtract one from the
968 // address to make sure we get an address that is not the end of
969 // a section.
970 if (state.end_sequence && state.address != 0)
971 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
972 else
973 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
974
975 if (info->curr_section_sp.get())
976 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
977 else
978 info->curr_sect_file_base_addr = 0;
979 }
980 if (info->curr_section_sp.get())
981 {
982 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
983 // Check for the fancy section magic to determine if we
984
985 if (info->is_oso_for_debug_map)
986 {
987 // When this is a debug map object file that contains DWARF
988 // (referenced from an N_OSO debug map nlist entry) we will have
989 // a file address in the file range for our section from the
990 // original .o file, and a load address in the executable that
991 // contains the debug map.
992 //
993 // If the sections for the file range and load range are
994 // different, we have a remapped section for the function and
995 // this address is resolved. If they are the same, then the
996 // function for this address didn't make it into the final
997 // executable.
998 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
999
1000 // If we are doing DWARF with debug map, then we need to carefully
1001 // add each line table entry as there may be gaps as functions
1002 // get moved around or removed.
1003 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
1004 {
1005 if (info->prev_in_final_executable)
1006 {
1007 bool terminate_previous_entry = false;
1008 if (!curr_in_final_executable)
1009 {
1010 // Check for the case where the previous line entry
1011 // in a function made it into the final executable,
1012 // yet the current line entry falls in a function
1013 // that didn't. The line table used to be contiguous
1014 // through this address range but now it isn't. We
1015 // need to terminate the previous line entry so
1016 // that we can reconstruct the line range correctly
1017 // for it and to keep the line table correct.
1018 terminate_previous_entry = true;
1019 }
1020 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
1021 {
1022 // Check for cases where the line entries used to be
1023 // contiguous address ranges, but now they aren't.
1024 // This can happen when order files specify the
1025 // ordering of the functions.
1026 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
1027 Section *curr_sect = info->curr_section_sp.get();
1028 Section *prev_sect = info->prev_section_sp.get();
1029 assert (curr_sect->GetLinkedSection());
1030 assert (prev_sect->GetLinkedSection());
1031 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
1032 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
1033 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
1034 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
1035 if (object_file_addr_delta != linked_file_addr_delta)
1036 terminate_previous_entry = true;
1037 }
1038
1039 if (terminate_previous_entry)
1040 {
1041 line_table->InsertLineEntry (info->prev_section_sp,
1042 state.address - info->prev_sect_file_base_addr,
1043 info->prev_row.line,
1044 info->prev_row.column,
1045 info->prev_row.file,
1046 false, // is_stmt
1047 false, // basic_block
1048 false, // state.prologue_end
1049 false, // state.epilogue_begin
1050 true); // end_sequence);
1051 }
1052 }
1053 }
1054
1055 if (curr_in_final_executable)
1056 {
1057 line_table->InsertLineEntry (info->curr_section_sp,
1058 curr_line_section_offset,
1059 state.line,
1060 state.column,
1061 state.file,
1062 state.is_stmt,
1063 state.basic_block,
1064 state.prologue_end,
1065 state.epilogue_begin,
1066 state.end_sequence);
1067 info->prev_section_sp = info->curr_section_sp;
1068 }
1069 else
1070 {
1071 // If the current address didn't make it into the final
1072 // executable, the current section will be the __text
1073 // segment in the .o file, so we need to clear this so
1074 // we can catch the next function that did make it into
1075 // the final executable.
1076 info->prev_section_sp.reset();
1077 info->curr_section_sp.reset();
1078 }
1079
1080 info->prev_in_final_executable = curr_in_final_executable;
1081 }
1082 else
1083 {
1084 // We are not in an object file that contains DWARF for an
1085 // N_OSO, this is just a normal DWARF file. The DWARF spec
1086 // guarantees that the addresses will be in increasing order
1087 // so, since we store line tables in file address order, we
1088 // can always just append the line entry without needing to
1089 // search for the correct insertion point (we don't need to
1090 // use LineEntry::InsertLineEntry()).
1091 line_table->AppendLineEntry (info->curr_section_sp,
1092 curr_line_section_offset,
1093 state.line,
1094 state.column,
1095 state.file,
1096 state.is_stmt,
1097 state.basic_block,
1098 state.prologue_end,
1099 state.epilogue_begin,
1100 state.end_sequence);
1101 }
1102 }
1103
1104 info->prev_row = state;
1105 }
1106}
1107
1108bool
1109SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
1110{
1111 assert (sc.comp_unit);
1112 if (sc.comp_unit->GetLineTable() != NULL)
1113 return true;
1114
1115 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
1116 if (dwarf_cu)
1117 {
1118 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
Greg Clayton129d12c2011-11-28 03:29:03 +00001119 if (dwarf_cu_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001121 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
1122 if (cu_line_offset != DW_INVALID_OFFSET)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001123 {
Greg Clayton129d12c2011-11-28 03:29:03 +00001124 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
1125 if (line_table_ap.get())
1126 {
1127 ParseDWARFLineTableCallbackInfo info = {
1128 line_table_ap.get(),
1129 m_obj_file->GetSectionList(),
1130 0,
1131 0,
1132 m_debug_map_symfile != NULL,
1133 false,
1134 DWARFDebugLine::Row(),
1135 SectionSP(),
1136 SectionSP()
1137 };
1138 uint32_t offset = cu_line_offset;
1139 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
1140 sc.comp_unit->SetLineTable(line_table_ap.release());
1141 return true;
1142 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001143 }
1144 }
1145 }
1146 return false;
1147}
1148
1149size_t
1150SymbolFileDWARF::ParseFunctionBlocks
1151(
1152 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001153 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +00001154 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001155 const DWARFDebugInfoEntry *die,
1156 addr_t subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001157 uint32_t depth
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158)
1159{
1160 size_t blocks_added = 0;
1161 while (die != NULL)
1162 {
1163 dw_tag_t tag = die->Tag();
1164
1165 switch (tag)
1166 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001167 case DW_TAG_inlined_subroutine:
Greg Claytonb4d37332011-08-12 16:22:48 +00001168 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001169 case DW_TAG_lexical_block:
1170 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001171 Block *block = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001172 if (tag == DW_TAG_subprogram)
1173 {
1174 // Skip any DW_TAG_subprogram DIEs that are inside
1175 // of a normal or inlined functions. These will be
1176 // parsed on their own as separate entities.
1177
1178 if (depth > 0)
1179 break;
1180
1181 block = parent_block;
1182 }
1183 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001184 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001185 BlockSP block_sp(new Block (MakeUserID(die->GetOffset())));
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001186 parent_block->AddChild(block_sp);
1187 block = block_sp.get();
1188 }
Greg Claytondd7feaf2011-08-12 17:54:33 +00001189 DWARFDebugRanges::RangeList ranges;
1190 const char *name = NULL;
1191 const char *mangled_name = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001192
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001193 int decl_file = 0;
1194 int decl_line = 0;
1195 int decl_column = 0;
1196 int call_file = 0;
1197 int call_line = 0;
1198 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001199 if (die->GetDIENamesAndRanges (this,
1200 dwarf_cu,
1201 name,
1202 mangled_name,
1203 ranges,
1204 decl_file, decl_line, decl_column,
1205 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001206 {
1207 if (tag == DW_TAG_subprogram)
1208 {
1209 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
Greg Claytonea3e7d52011-10-08 00:49:15 +00001210 subprogram_low_pc = ranges.GetMinRangeBase(0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001211 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001212 else if (tag == DW_TAG_inlined_subroutine)
1213 {
1214 // We get called here for inlined subroutines in two ways.
1215 // The first time is when we are making the Function object
1216 // for this inlined concrete instance. Since we're creating a top level block at
1217 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1218 // adjust the containing address.
1219 // The second time is when we are parsing the blocks inside the function that contains
1220 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1221 // function the offset will be for that function.
1222 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1223 {
Greg Claytonea3e7d52011-10-08 00:49:15 +00001224 subprogram_low_pc = ranges.GetMinRangeBase(0);
Jim Inghamb0be4422010-08-12 01:20:14 +00001225 }
1226 }
1227
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001228 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001229
1230 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1231 {
1232 std::auto_ptr<Declaration> decl_ap;
1233 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001234 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1235 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001236
1237 std::auto_ptr<Declaration> call_ap;
1238 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001239 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1240 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001241
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001242 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001243 }
1244
1245 ++blocks_added;
1246
Greg Claytondd7feaf2011-08-12 17:54:33 +00001247 if (die->HasChildren())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001248 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001249 blocks_added += ParseFunctionBlocks (sc,
1250 block,
1251 dwarf_cu,
1252 die->GetFirstChild(),
1253 subprogram_low_pc,
Greg Claytondd7feaf2011-08-12 17:54:33 +00001254 depth + 1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001255 }
1256 }
1257 }
1258 break;
1259 default:
1260 break;
1261 }
1262
Greg Claytondd7feaf2011-08-12 17:54:33 +00001263 // Only parse siblings of the block if we are not at depth zero. A depth
1264 // of zero indicates we are currently parsing the top level
1265 // DW_TAG_subprogram DIE
1266
1267 if (depth == 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001268 die = NULL;
Greg Claytondd7feaf2011-08-12 17:54:33 +00001269 else
1270 die = die->GetSibling();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001271 }
1272 return blocks_added;
1273}
1274
Greg Claytonf0705c82011-10-22 03:33:13 +00001275bool
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001276SymbolFileDWARF::ParseTemplateDIE (DWARFCompileUnit* dwarf_cu,
1277 const DWARFDebugInfoEntry *die,
1278 ClangASTContext::TemplateParameterInfos &template_param_infos)
1279{
1280 const dw_tag_t tag = die->Tag();
1281
1282 switch (tag)
1283 {
1284 case DW_TAG_template_type_parameter:
1285 case DW_TAG_template_value_parameter:
1286 {
1287 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1288
1289 DWARFDebugInfoEntry::Attributes attributes;
1290 const size_t num_attributes = die->GetAttributes (this,
1291 dwarf_cu,
1292 fixed_form_sizes,
1293 attributes);
1294 const char *name = NULL;
1295 Type *lldb_type = NULL;
1296 clang_type_t clang_type = NULL;
1297 uint64_t uval64 = 0;
1298 bool uval64_valid = false;
1299 if (num_attributes > 0)
1300 {
1301 DWARFFormValue form_value;
1302 for (size_t i=0; i<num_attributes; ++i)
1303 {
1304 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1305
1306 switch (attr)
1307 {
1308 case DW_AT_name:
1309 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1310 name = form_value.AsCString(&get_debug_str_data());
1311 break;
1312
1313 case DW_AT_type:
1314 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1315 {
1316 const dw_offset_t type_die_offset = form_value.Reference(dwarf_cu);
1317 lldb_type = ResolveTypeUID(type_die_offset);
1318 if (lldb_type)
1319 clang_type = lldb_type->GetClangForwardType();
1320 }
1321 break;
1322
1323 case DW_AT_const_value:
1324 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1325 {
1326 uval64_valid = true;
1327 uval64 = form_value.Unsigned();
1328 }
1329 break;
1330 default:
1331 break;
1332 }
1333 }
1334
1335 if (name && lldb_type && clang_type)
1336 {
1337 bool is_signed = false;
1338 template_param_infos.names.push_back(name);
1339 clang::QualType clang_qual_type (clang::QualType::getFromOpaquePtr (clang_type));
1340 if (tag == DW_TAG_template_value_parameter && ClangASTContext::IsIntegerType (clang_type, is_signed) && uval64_valid)
1341 {
1342 llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1343 template_param_infos.args.push_back (clang::TemplateArgument (llvm::APSInt(apint), clang_qual_type));
1344 }
1345 else
1346 {
1347 template_param_infos.args.push_back (clang::TemplateArgument (clang_qual_type));
1348 }
1349 }
1350 else
1351 {
1352 return false;
1353 }
1354
1355 }
1356 }
1357 return true;
1358
1359 default:
1360 break;
1361 }
1362 return false;
1363}
1364
1365bool
Greg Claytonf0705c82011-10-22 03:33:13 +00001366SymbolFileDWARF::ParseTemplateParameterInfos (DWARFCompileUnit* dwarf_cu,
1367 const DWARFDebugInfoEntry *parent_die,
1368 ClangASTContext::TemplateParameterInfos &template_param_infos)
1369{
1370
1371 if (parent_die == NULL)
1372 return NULL;
1373
Greg Claytonf0705c82011-10-22 03:33:13 +00001374 Args template_parameter_names;
1375 for (const DWARFDebugInfoEntry *die = parent_die->GetFirstChild();
1376 die != NULL;
1377 die = die->GetSibling())
1378 {
1379 const dw_tag_t tag = die->Tag();
1380
1381 switch (tag)
1382 {
1383 case DW_TAG_template_type_parameter:
1384 case DW_TAG_template_value_parameter:
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00001385 ParseTemplateDIE (dwarf_cu, die, template_param_infos);
Greg Claytonf0705c82011-10-22 03:33:13 +00001386 break;
1387
1388 default:
1389 break;
1390 }
1391 }
1392 if (template_param_infos.args.empty())
1393 return false;
1394 return template_param_infos.args.size() == template_param_infos.names.size();
1395}
1396
1397clang::ClassTemplateDecl *
1398SymbolFileDWARF::ParseClassTemplateDecl (clang::DeclContext *decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001399 lldb::AccessType access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001400 const char *parent_name,
1401 int tag_decl_kind,
1402 const ClangASTContext::TemplateParameterInfos &template_param_infos)
1403{
1404 if (template_param_infos.IsValid())
1405 {
1406 std::string template_basename(parent_name);
1407 template_basename.erase (template_basename.find('<'));
1408 ClangASTContext &ast = GetClangASTContext();
1409
1410 return ast.CreateClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00001411 access_type,
Greg Claytonf0705c82011-10-22 03:33:13 +00001412 template_basename.c_str(),
1413 tag_decl_kind,
1414 template_param_infos);
1415 }
1416 return NULL;
1417}
1418
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001419size_t
1420SymbolFileDWARF::ParseChildMembers
1421(
1422 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001423 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001424 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001425 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001426 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001427 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1428 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001429 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001430 AccessType& default_accessibility,
Greg Claytoncaab74e2012-01-28 00:48:57 +00001431 bool &is_a_class,
1432 LayoutInfo &layout_info
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001433)
1434{
1435 if (parent_die == NULL)
1436 return 0;
1437
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001438 size_t count = 0;
1439 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001440 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001441 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001442
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001443 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1444 {
1445 dw_tag_t tag = die->Tag();
1446
1447 switch (tag)
1448 {
1449 case DW_TAG_member:
1450 {
1451 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001452 const size_t num_attributes = die->GetAttributes (this,
1453 dwarf_cu,
1454 fixed_form_sizes,
1455 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001456 if (num_attributes > 0)
1457 {
1458 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001459 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001460 const char *name = NULL;
Jim Inghame3ae82a2011-11-12 01:36:43 +00001461 const char *prop_name = NULL;
1462 const char *prop_getter_name = NULL;
1463 const char *prop_setter_name = NULL;
1464 uint32_t prop_attributes = 0;
1465
1466
Greg Clayton24739922010-10-13 03:15:28 +00001467 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001468 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001469 AccessType accessibility = eAccessNone;
Greg Claytoncaab74e2012-01-28 00:48:57 +00001470 uint32_t member_byte_offset = UINT32_MAX;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001471 size_t byte_size = 0;
1472 size_t bit_offset = 0;
1473 size_t bit_size = 0;
1474 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001475 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001476 {
1477 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1478 DWARFFormValue form_value;
1479 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1480 {
1481 switch (attr)
1482 {
1483 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1484 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1485 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1486 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1487 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1488 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1489 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1490 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1491 case DW_AT_data_member_location:
Greg Claytoncaab74e2012-01-28 00:48:57 +00001492 if (form_value.BlockData())
1493 {
1494 Value initialValue(0);
1495 Value memberOffset(0);
1496 const DataExtractor& debug_info_data = get_debug_info_data();
1497 uint32_t block_length = form_value.Unsigned();
1498 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1499 if (DWARFExpression::Evaluate(NULL, // ExecutionContext *
1500 NULL, // clang::ASTContext *
1501 NULL, // ClangExpressionVariableList *
1502 NULL, // ClangExpressionDeclMap *
1503 NULL, // RegisterContext *
1504 debug_info_data,
1505 block_offset,
1506 block_length,
1507 eRegisterKindDWARF,
1508 &initialValue,
1509 memberOffset,
1510 NULL))
1511 {
1512 member_byte_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1513 }
1514 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001515 break;
1516
Greg Clayton8cf05932010-07-22 18:30:50 +00001517 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton24739922010-10-13 03:15:28 +00001518 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001519 case DW_AT_declaration:
1520 case DW_AT_description:
1521 case DW_AT_mutable:
1522 case DW_AT_visibility:
Jim Inghame3ae82a2011-11-12 01:36:43 +00001523
1524 case DW_AT_APPLE_property_name: prop_name = form_value.AsCString(&get_debug_str_data()); break;
1525 case DW_AT_APPLE_property_getter: prop_getter_name = form_value.AsCString(&get_debug_str_data()); break;
1526 case DW_AT_APPLE_property_setter: prop_setter_name = form_value.AsCString(&get_debug_str_data()); break;
1527 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
1528
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001529 default:
1530 case DW_AT_sibling:
1531 break;
1532 }
1533 }
1534 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001535
Greg Clayton44953932011-10-25 01:25:35 +00001536 // Clang has a DWARF generation bug where sometimes it
1537 // represents fields that are references with bad byte size
1538 // and bit size/offset information such as:
1539 //
1540 // DW_AT_byte_size( 0x00 )
1541 // DW_AT_bit_size( 0x40 )
1542 // DW_AT_bit_offset( 0xffffffffffffffc0 )
1543 //
1544 // So check the bit offset to make sure it is sane, and if
1545 // the values are not sane, remove them. If we don't do this
1546 // then we will end up with a crash if we try to use this
1547 // type in an expression when clang becomes unhappy with its
1548 // recycled debug info.
Sean Callanan5a477cf2010-10-30 01:56:10 +00001549
Greg Clayton44953932011-10-25 01:25:35 +00001550 if (bit_offset > 128)
1551 {
1552 bit_size = 0;
1553 bit_offset = 0;
1554 }
1555
1556 // FIXME: Make Clang ignore Objective-C accessibility for expressions
Sean Callanan5a477cf2010-10-30 01:56:10 +00001557 if (class_language == eLanguageTypeObjC ||
1558 class_language == eLanguageTypeObjC_plus_plus)
1559 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001560
1561 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1562 {
1563 // Not all compilers will mark the vtable pointer
1564 // member as artificial (llvm-gcc). We can't have
1565 // the virtual members in our classes otherwise it
1566 // throws off all child offsets since we end up
1567 // having and extra pointer sized member in our
1568 // class layouts.
1569 is_artificial = true;
1570 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001571
Greg Clayton24739922010-10-13 03:15:28 +00001572 if (is_artificial == false)
1573 {
1574 Type *member_type = ResolveTypeUID(encoding_uid);
Jim Inghame3ae82a2011-11-12 01:36:43 +00001575 clang::FieldDecl *field_decl = NULL;
Greg Claytond16e1e52011-07-12 17:06:17 +00001576 if (member_type)
1577 {
1578 if (accessibility == eAccessNone)
1579 accessibility = default_accessibility;
1580 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001581
Jim Inghame3ae82a2011-11-12 01:36:43 +00001582 field_decl = GetClangASTContext().AddFieldToRecordType (class_clang_type,
Greg Clayton42ce2f32011-12-12 21:50:19 +00001583 name,
1584 member_type->GetClangLayoutType(),
1585 accessibility,
1586 bit_size);
Greg Claytond16e1e52011-07-12 17:06:17 +00001587 }
1588 else
1589 {
1590 if (name)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001591 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member '%s' refers to type 0x%8.8llx which was unable to be parsed",
1592 MakeUserID(die->GetOffset()),
1593 name,
1594 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001595 else
Greg Claytone38a5ed2012-01-05 03:57:59 +00001596 GetObjectFile()->GetModule()->ReportError ("0x%8.8llx: DW_TAG_member refers to type 0x%8.8llx which was unable to be parsed",
1597 MakeUserID(die->GetOffset()),
1598 encoding_uid);
Greg Claytond16e1e52011-07-12 17:06:17 +00001599 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001600
Sean Callanan5b26f272012-02-04 08:49:35 +00001601 if (member_byte_offset != UINT32_MAX || bit_size != 0)
Greg Claytoncaab74e2012-01-28 00:48:57 +00001602 {
Sean Callanan5b26f272012-02-04 08:49:35 +00001603 /////////////////////////////////////////////////////////////
1604 // How to locate a field given the DWARF debug information
1605 //
1606 // AT_byte_size indicates the size of the word in which the
1607 // bit offset must be interpreted.
1608 //
1609 // AT_data_member_location indicates the byte offset of the
1610 // word from the base address of the structure.
1611 //
1612 // AT_bit_offset indicates how many bits into the word
1613 // (according to the host endianness) the low-order bit of
1614 // the field starts. AT_bit_offset can be negative.
1615 //
1616 // AT_bit_size indicates the size of the field in bits.
1617 /////////////////////////////////////////////////////////////
1618
1619 ByteOrder object_endian = GetObjectFile()->GetModule()->GetArchitecture().GetDefaultEndian();
1620
1621 uint64_t total_bit_offset = 0;
1622
1623 total_bit_offset += (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
1624
1625 if (object_endian == eByteOrderLittle)
1626 {
1627 total_bit_offset += byte_size * 8;
1628 total_bit_offset -= (bit_offset + bit_size);
1629 }
1630 else
1631 {
1632 total_bit_offset += bit_offset;
1633 }
1634
1635 layout_info.field_offsets.insert(std::make_pair(field_decl, total_bit_offset));
Greg Claytoncaab74e2012-01-28 00:48:57 +00001636 }
Jim Inghame3ae82a2011-11-12 01:36:43 +00001637 if (prop_name != NULL)
1638 {
1639
1640 clang::ObjCIvarDecl *ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
1641 assert (ivar_decl != NULL);
1642
1643
1644 GetClangASTContext().AddObjCClassProperty (class_clang_type,
1645 prop_name,
1646 0,
1647 ivar_decl,
1648 prop_setter_name,
1649 prop_getter_name,
1650 prop_attributes);
1651 }
Greg Clayton24739922010-10-13 03:15:28 +00001652 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001653 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001654 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001655 }
1656 break;
1657
1658 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001659 // Let the type parsing code handle this one for us.
1660 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001661 break;
1662
1663 case DW_TAG_inheritance:
1664 {
1665 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001666 if (default_accessibility == eAccessNone)
1667 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001668 // TODO: implement DW_TAG_inheritance type parsing
1669 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001670 const size_t num_attributes = die->GetAttributes (this,
1671 dwarf_cu,
1672 fixed_form_sizes,
1673 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001674 if (num_attributes > 0)
1675 {
1676 Declaration decl;
1677 DWARFExpression location;
1678 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001679 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001680 bool is_virtual = false;
1681 bool is_base_of_class = true;
1682 off_t member_offset = 0;
1683 uint32_t i;
1684 for (i=0; i<num_attributes; ++i)
1685 {
1686 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1687 DWARFFormValue form_value;
1688 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1689 {
1690 switch (attr)
1691 {
1692 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1693 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1694 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1695 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1696 case DW_AT_data_member_location:
1697 if (form_value.BlockData())
1698 {
1699 Value initialValue(0);
1700 Value memberOffset(0);
1701 const DataExtractor& debug_info_data = get_debug_info_data();
1702 uint32_t block_length = form_value.Unsigned();
1703 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001704 if (DWARFExpression::Evaluate (NULL,
1705 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001706 NULL,
1707 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001708 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001709 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001710 block_offset,
1711 block_length,
1712 eRegisterKindDWARF,
1713 &initialValue,
1714 memberOffset,
1715 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001716 {
1717 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1718 }
1719 }
1720 break;
1721
1722 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001723 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001724 break;
1725
1726 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1727 default:
1728 case DW_AT_sibling:
1729 break;
1730 }
1731 }
1732 }
1733
Greg Clayton526e5af2010-11-13 03:52:47 +00001734 Type *base_class_type = ResolveTypeUID(encoding_uid);
1735 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001736
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001737 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1738 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001739 if (class_language == eLanguageTypeObjC)
1740 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001741 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001742 }
1743 else
1744 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001745 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001746 accessibility,
1747 is_virtual,
1748 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001749 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001750 }
1751 }
1752 break;
1753
1754 default:
1755 break;
1756 }
1757 }
1758 return count;
1759}
1760
1761
1762clang::DeclContext*
Sean Callanan72e49402011-08-05 23:43:37 +00001763SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001764{
1765 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton81c22f62011-10-19 18:09:39 +00001766 if (debug_info && UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001767 {
1768 DWARFCompileUnitSP cu_sp;
1769 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1770 if (die)
Greg Claytoncb5860a2011-10-13 23:49:28 +00001771 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Sean Callanan72e49402011-08-05 23:43:37 +00001772 }
1773 return NULL;
1774}
1775
1776clang::DeclContext*
1777SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1778{
Greg Clayton81c22f62011-10-19 18:09:39 +00001779 if (UserIDMatches(type_uid))
1780 return GetClangDeclContextForDIEOffset (sc, type_uid);
1781 return NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001782}
1783
1784Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001785SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001786{
Greg Clayton81c22f62011-10-19 18:09:39 +00001787 if (UserIDMatches(type_uid))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001788 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001789 DWARFDebugInfo* debug_info = DebugInfo();
1790 if (debug_info)
Greg Claytonca512b32011-01-14 04:54:56 +00001791 {
Greg Clayton81c22f62011-10-19 18:09:39 +00001792 DWARFCompileUnitSP cu_sp;
1793 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Greg Claytoncab36a32011-12-08 05:16:30 +00001794 const bool assert_not_being_parsed = true;
1795 return ResolveTypeUID (cu_sp.get(), type_die, assert_not_being_parsed);
Greg Claytonca512b32011-01-14 04:54:56 +00001796 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001797 }
1798 return NULL;
1799}
1800
Greg Claytoncab36a32011-12-08 05:16:30 +00001801Type*
1802SymbolFileDWARF::ResolveTypeUID (DWARFCompileUnit* cu, const DWARFDebugInfoEntry* die, bool assert_not_being_parsed)
Sean Callanan5b26f272012-02-04 08:49:35 +00001803{
Greg Claytoncab36a32011-12-08 05:16:30 +00001804 if (die != NULL)
1805 {
Greg Clayton3bffb082011-12-10 02:15:28 +00001806 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1807 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001808 GetObjectFile()->GetModule()->LogMessage (log.get(),
1809 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s'",
1810 die->GetOffset(),
1811 DW_TAG_value_to_name(die->Tag()),
1812 die->GetName(this, cu));
Greg Clayton3bffb082011-12-10 02:15:28 +00001813
Greg Claytoncab36a32011-12-08 05:16:30 +00001814 // We might be coming in in the middle of a type tree (a class
1815 // withing a class, an enum within a class), so parse any needed
1816 // parent DIEs before we get to this one...
1817 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1818 switch (decl_ctx_die->Tag())
1819 {
1820 case DW_TAG_structure_type:
1821 case DW_TAG_union_type:
1822 case DW_TAG_class_type:
1823 {
1824 // Get the type, which could be a forward declaration
Greg Clayton3bffb082011-12-10 02:15:28 +00001825 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001826 GetObjectFile()->GetModule()->LogMessage (log.get(),
1827 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent forward type for 0x%8.8x",
1828 die->GetOffset(),
1829 DW_TAG_value_to_name(die->Tag()),
1830 die->GetName(this, cu),
1831 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001832
Greg Claytoncab36a32011-12-08 05:16:30 +00001833 Type *parent_type = ResolveTypeUID (cu, decl_ctx_die, assert_not_being_parsed);
Sean Callanan5b26f272012-02-04 08:49:35 +00001834 if (child_requires_parent_class_union_or_struct_to_be_completed(die->Tag()))
Greg Clayton3bffb082011-12-10 02:15:28 +00001835 {
1836 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001837 GetObjectFile()->GetModule()->LogMessage (log.get(),
1838 "SymbolFileDWARF::ResolveTypeUID (die = 0x%8.8x) %s '%s' resolve parent full type for 0x%8.8x since die is a function",
1839 die->GetOffset(),
1840 DW_TAG_value_to_name(die->Tag()),
1841 die->GetName(this, cu),
1842 decl_ctx_die->GetOffset());
Greg Clayton3bffb082011-12-10 02:15:28 +00001843 // Ask the type to complete itself if it already hasn't since if we
1844 // want a function (method or static) from a class, the class must
1845 // create itself and add it's own methods and class functions.
1846 if (parent_type)
1847 parent_type->GetClangFullType();
1848 }
Greg Claytoncab36a32011-12-08 05:16:30 +00001849 }
1850 break;
1851
1852 default:
1853 break;
1854 }
1855 return ResolveType (cu, die);
1856 }
1857 return NULL;
1858}
1859
Greg Clayton6beaaa62011-01-17 03:46:26 +00001860// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1861// SymbolFileDWARF objects to detect if this DWARF file is the one that
1862// can resolve a clang_type.
1863bool
1864SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1865{
1866 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1867 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1868 return die != NULL;
1869}
1870
1871
Greg Clayton1be10fc2010-09-29 01:12:09 +00001872lldb::clang_type_t
1873SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1874{
1875 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001876 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1877 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001878 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001879 {
1880 // We have already resolved this type...
1881 return clang_type;
1882 }
1883 // Once we start resolving this type, remove it from the forward declaration
1884 // map in case anyone child members or other types require this type to get resolved.
1885 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1886 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001887 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001888
Greg Clayton1be10fc2010-09-29 01:12:09 +00001889
Greg Clayton85ae2e12011-10-18 23:36:41 +00001890 // Disable external storage for this type so we don't get anymore
1891 // clang::ExternalASTSource queries for this type.
1892 ClangASTContext::SetHasExternalStorage (clang_type, false);
1893
Greg Clayton450e3f32010-10-12 02:24:53 +00001894 DWARFDebugInfo* debug_info = DebugInfo();
1895
Greg Clayton96d7d742010-11-10 23:42:09 +00001896 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001897 Type *type = m_die_to_type.lookup (die);
1898
1899 const dw_tag_t tag = die->Tag();
1900
Greg Clayton3bffb082011-12-10 02:15:28 +00001901 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
1902 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00001903 GetObjectFile()->GetModule()->LogMessage (log.get(),
1904 "0x%8.8llx: %s '%s' resolving forward declaration...\n",
1905 MakeUserID(die->GetOffset()),
1906 DW_TAG_value_to_name(tag),
1907 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001908 assert (clang_type);
1909 DWARFDebugInfoEntry::Attributes attributes;
1910
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001911 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001912
1913 switch (tag)
1914 {
1915 case DW_TAG_structure_type:
1916 case DW_TAG_union_type:
1917 case DW_TAG_class_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001918 ast.StartTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001919 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001920 LayoutInfo layout_info;
1921 if (die->HasChildren())
Greg Clayton1be10fc2010-09-29 01:12:09 +00001922 {
Greg Claytonc93237c2010-10-01 20:48:32 +00001923
Greg Claytoncaab74e2012-01-28 00:48:57 +00001924 LanguageType class_language = eLanguageTypeUnknown;
1925 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1926 if (is_objc_class)
1927 class_language = eLanguageTypeObjC;
Greg Claytonc93237c2010-10-01 20:48:32 +00001928
Greg Claytoncaab74e2012-01-28 00:48:57 +00001929 int tag_decl_kind = -1;
1930 AccessType default_accessibility = eAccessNone;
1931 if (tag == DW_TAG_structure_type)
Greg Claytonc93237c2010-10-01 20:48:32 +00001932 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001933 tag_decl_kind = clang::TTK_Struct;
1934 default_accessibility = eAccessPublic;
Greg Claytonc93237c2010-10-01 20:48:32 +00001935 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00001936 else if (tag == DW_TAG_union_type)
Greg Clayton450e3f32010-10-12 02:24:53 +00001937 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001938 tag_decl_kind = clang::TTK_Union;
1939 default_accessibility = eAccessPublic;
1940 }
1941 else if (tag == DW_TAG_class_type)
1942 {
1943 tag_decl_kind = clang::TTK_Class;
1944 default_accessibility = eAccessPrivate;
1945 }
1946
1947 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1948 std::vector<clang::CXXBaseSpecifier *> base_classes;
1949 std::vector<int> member_accessibilities;
1950 bool is_a_class = false;
1951 // Parse members and base classes first
1952 DWARFDIECollection member_function_dies;
1953
1954 ParseChildMembers (sc,
1955 curr_cu,
1956 die,
1957 clang_type,
1958 class_language,
1959 base_classes,
1960 member_accessibilities,
1961 member_function_dies,
1962 default_accessibility,
1963 is_a_class,
1964 layout_info);
1965
1966 // Now parse any methods if there were any...
1967 size_t num_functions = member_function_dies.Size();
1968 if (num_functions > 0)
1969 {
1970 for (size_t i=0; i<num_functions; ++i)
1971 {
1972 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
1973 }
1974 }
Greg Clayton450e3f32010-10-12 02:24:53 +00001975
Greg Claytoncaab74e2012-01-28 00:48:57 +00001976 if (class_language == eLanguageTypeObjC)
1977 {
1978 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type));
1979 if (!class_str.empty())
Greg Clayton5009f9d2011-10-27 17:55:14 +00001980 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001981
1982 DIEArray method_die_offsets;
1983 if (m_using_apple_tables)
Greg Claytond4a2b372011-09-12 23:21:58 +00001984 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00001985 if (m_apple_objc_ap.get())
1986 m_apple_objc_ap->FindByName(class_str.c_str(), method_die_offsets);
1987 }
1988 else
1989 {
1990 if (!m_indexed)
1991 Index ();
Greg Clayton450e3f32010-10-12 02:24:53 +00001992
Greg Claytoncaab74e2012-01-28 00:48:57 +00001993 ConstString class_name (class_str.c_str());
1994 m_objc_class_selectors_index.Find (class_name, method_die_offsets);
1995 }
1996
1997 if (!method_die_offsets.empty())
1998 {
1999 DWARFDebugInfo* debug_info = DebugInfo();
2000
2001 DWARFCompileUnit* method_cu = NULL;
2002 const size_t num_matches = method_die_offsets.size();
2003 for (size_t i=0; i<num_matches; ++i)
Greg Clayton95d87902011-11-11 03:16:25 +00002004 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002005 const dw_offset_t die_offset = method_die_offsets[i];
2006 DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
2007
2008 if (method_die)
2009 ResolveType (method_cu, method_die);
2010 else
Greg Clayton95d87902011-11-11 03:16:25 +00002011 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002012 if (m_using_apple_tables)
2013 {
2014 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_objc accelerator table had bad die 0x%8.8x for '%s')\n",
2015 die_offset, class_str.c_str());
2016 }
2017 }
2018 }
Greg Clayton450e3f32010-10-12 02:24:53 +00002019 }
2020 }
2021 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002022
2023 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2024 // need to tell the clang type it is actually a class.
2025 if (class_language != eLanguageTypeObjC)
2026 {
2027 if (is_a_class && tag_decl_kind != clang::TTK_Class)
2028 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
2029 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002030
Greg Claytoncaab74e2012-01-28 00:48:57 +00002031 // Since DW_TAG_structure_type gets used for both classes
2032 // and structures, we may need to set any DW_TAG_member
2033 // fields to have a "private" access if none was specified.
2034 // When we parsed the child members we tracked that actual
2035 // accessibility value for each DW_TAG_member in the
2036 // "member_accessibilities" array. If the value for the
2037 // member is zero, then it was set to the "default_accessibility"
2038 // which for structs was "public". Below we correct this
2039 // by setting any fields to "private" that weren't correctly
2040 // set.
2041 if (is_a_class && !member_accessibilities.empty())
2042 {
2043 // This is a class and all members that didn't have
2044 // their access specified are private.
2045 ast.SetDefaultAccessForRecordFields (clang_type,
2046 eAccessPrivate,
2047 &member_accessibilities.front(),
2048 member_accessibilities.size());
2049 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002050
Greg Claytoncaab74e2012-01-28 00:48:57 +00002051 if (!base_classes.empty())
2052 {
2053 ast.SetBaseClassesForClassType (clang_type,
2054 &base_classes.front(),
2055 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00002056
Greg Claytoncaab74e2012-01-28 00:48:57 +00002057 // Clang will copy each CXXBaseSpecifier in "base_classes"
2058 // so we have to free them all.
2059 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2060 base_classes.size());
2061 }
2062
Greg Claytonc93237c2010-10-01 20:48:32 +00002063 }
Sean Callanan5b26f272012-02-04 08:49:35 +00002064
Greg Claytoncaab74e2012-01-28 00:48:57 +00002065 if (!layout_info.field_offsets.empty())
2066 {
2067 if (type)
2068 layout_info.bit_size = type->GetByteSize() * 8;
2069 if (layout_info.bit_size == 0)
2070 layout_info.bit_size = die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_byte_size, 0) * 8;
2071 clang::QualType qual_type(clang::QualType::getFromOpaquePtr(clang_type));
2072 const clang::RecordType *record_type = clang::dyn_cast<clang::RecordType>(qual_type.getTypePtr());
2073 if (record_type)
2074 {
2075 const clang::RecordDecl *record_decl = record_type->getDecl();
2076
2077 if (log)
Greg Clayton821ac6d2012-01-28 02:22:27 +00002078 {
Greg Claytoncaab74e2012-01-28 00:48:57 +00002079 GetObjectFile()->GetModule()->LogMessage (log.get(),
Greg Clayton821ac6d2012-01-28 02:22:27 +00002080 "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 +00002081 clang_type,
2082 record_decl,
2083 layout_info.bit_size,
2084 layout_info.alignment,
2085 (uint32_t)layout_info.field_offsets.size());
2086
Greg Clayton821ac6d2012-01-28 02:22:27 +00002087 llvm::DenseMap <const clang::FieldDecl *, uint64_t>::const_iterator pos, end = layout_info.field_offsets.end();
2088 for (pos = layout_info.field_offsets.begin(); pos != end; ++pos)
2089 {
2090 GetObjectFile()->GetModule()->LogMessage (log.get(),
2091 "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (clang_type = %p) field = { bit_offset=%u, name='%s' }",
2092 clang_type,
2093 (uint32_t)pos->second,
2094 pos->first->getNameAsString().c_str());
2095 }
2096 }
Greg Claytoncaab74e2012-01-28 00:48:57 +00002097 m_record_decl_to_layout_map.insert(std::make_pair(record_decl, layout_info));
2098 }
2099 }
Greg Claytonc93237c2010-10-01 20:48:32 +00002100 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002101 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00002102 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00002103
2104 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002105 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002106 if (die->HasChildren())
2107 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002108 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
2109 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002110 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002111 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00002112 return clang_type;
2113
2114 default:
2115 assert(false && "not a forward clang type decl!");
2116 break;
2117 }
2118 return NULL;
2119}
2120
Greg Claytonc685f8e2010-09-15 04:15:46 +00002121Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00002122SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002123{
2124 if (type_die != NULL)
2125 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002126 Type *type = m_die_to_type.lookup (type_die);
Greg Claytoncab36a32011-12-08 05:16:30 +00002127
Greg Claytonc685f8e2010-09-15 04:15:46 +00002128 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002129 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Claytoncab36a32011-12-08 05:16:30 +00002130
Greg Clayton24739922010-10-13 03:15:28 +00002131 if (assert_not_being_parsed)
Jim Inghamc3549282012-01-11 02:21:12 +00002132 {
2133 if (type != DIE_IS_BEING_PARSED)
2134 return type;
2135
2136 GetObjectFile()->GetModule()->ReportError ("Parsing a die that is being parsed die: 0x%8.8x: %s %s",
2137 type_die->GetOffset(),
2138 DW_TAG_value_to_name(type_die->Tag()),
2139 type_die->GetName(this, curr_cu));
2140
2141 }
2142 else
2143 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002144 }
2145 return NULL;
2146}
2147
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002148CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00002149SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002150{
2151 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002152 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002153 {
2154 // The symbol vendor doesn't know about this compile unit, we
2155 // need to parse and add it to the symbol vendor object.
2156 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00002157 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002158 if (dc_cu.get())
2159 {
2160 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00002161 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00002162 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002163
2164 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00002165
2166 if (m_debug_map_symfile)
2167 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002168 }
2169 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002170 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002171}
2172
2173bool
Greg Clayton96d7d742010-11-10 23:42:09 +00002174SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002175{
2176 sc.Clear();
2177 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00002178 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002179
Greg Clayton81c22f62011-10-19 18:09:39 +00002180 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(func_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002181 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002182 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Jim Ingham4cda6e02011-10-07 22:23:45 +00002183
2184 if (sc.function)
2185 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00002186 sc.module_sp = sc.function->CalculateSymbolContextModule()->shared_from_this();
Jim Ingham4cda6e02011-10-07 22:23:45 +00002187 return true;
2188 }
2189
2190 return false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002191}
2192
2193uint32_t
2194SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
2195{
2196 Timer scoped_timer(__PRETTY_FUNCTION__,
2197 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
2198 so_addr.GetSection(),
2199 so_addr.GetOffset(),
2200 resolve_scope);
2201 uint32_t resolved = 0;
2202 if (resolve_scope & ( eSymbolContextCompUnit |
2203 eSymbolContextFunction |
2204 eSymbolContextBlock |
2205 eSymbolContextLineEntry))
2206 {
2207 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
2208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209 DWARFDebugInfo* debug_info = DebugInfo();
Greg Claytond4a2b372011-09-12 23:21:58 +00002210 if (debug_info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002211 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002212 dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002213 if (cu_offset != DW_INVALID_OFFSET)
2214 {
2215 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002216 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
2217 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002218 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002219 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002220 assert(sc.comp_unit != NULL);
2221 resolved |= eSymbolContextCompUnit;
2222
2223 if (resolve_scope & eSymbolContextLineEntry)
2224 {
2225 LineTable *line_table = sc.comp_unit->GetLineTable();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002226 if (line_table != NULL)
2227 {
2228 if (so_addr.IsLinkedAddress())
2229 {
2230 Address linked_addr (so_addr);
2231 linked_addr.ResolveLinkedAddress();
2232 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
2233 {
2234 resolved |= eSymbolContextLineEntry;
2235 }
2236 }
2237 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
2238 {
2239 resolved |= eSymbolContextLineEntry;
2240 }
2241 }
2242 }
2243
2244 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2245 {
2246 DWARFDebugInfoEntry *function_die = NULL;
2247 DWARFDebugInfoEntry *block_die = NULL;
2248 if (resolve_scope & eSymbolContextBlock)
2249 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002250 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002251 }
2252 else
2253 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002254 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002255 }
2256
2257 if (function_die != NULL)
2258 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002259 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002260 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002261 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002262 }
2263
2264 if (sc.function != NULL)
2265 {
2266 resolved |= eSymbolContextFunction;
2267
2268 if (resolve_scope & eSymbolContextBlock)
2269 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002270 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002271
2272 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002273 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002275 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002276 if (sc.block)
2277 resolved |= eSymbolContextBlock;
2278 }
2279 }
2280 }
2281 }
2282 }
2283 }
2284 }
2285 return resolved;
2286}
2287
2288
2289
2290uint32_t
2291SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
2292{
2293 const uint32_t prev_size = sc_list.GetSize();
2294 if (resolve_scope & eSymbolContextCompUnit)
2295 {
2296 DWARFDebugInfo* debug_info = DebugInfo();
2297 if (debug_info)
2298 {
2299 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00002300 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002301
Greg Clayton96d7d742010-11-10 23:42:09 +00002302 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002303 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002304 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002305 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
2306 if (check_inlines || file_spec_matches_cu_file_spec)
2307 {
2308 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00002309 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002310 assert(sc.comp_unit != NULL);
2311
2312 uint32_t file_idx = UINT32_MAX;
2313
2314 // If we are looking for inline functions only and we don't
2315 // find it in the support files, we are done.
2316 if (check_inlines)
2317 {
Jim Ingham87df91b2011-09-23 00:54:11 +00002318 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002319 if (file_idx == UINT32_MAX)
2320 continue;
2321 }
2322
2323 if (line != 0)
2324 {
2325 LineTable *line_table = sc.comp_unit->GetLineTable();
2326
2327 if (line_table != NULL && line != 0)
2328 {
2329 // We will have already looked up the file index if
2330 // we are searching for inline entries.
2331 if (!check_inlines)
Jim Ingham87df91b2011-09-23 00:54:11 +00002332 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333
2334 if (file_idx != UINT32_MAX)
2335 {
2336 uint32_t found_line;
2337 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
2338 found_line = sc.line_entry.line;
2339
Greg Clayton016a95e2010-09-14 02:20:48 +00002340 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002341 {
2342 sc.function = NULL;
2343 sc.block = NULL;
2344 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
2345 {
2346 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
2347 if (file_vm_addr != LLDB_INVALID_ADDRESS)
2348 {
2349 DWARFDebugInfoEntry *function_die = NULL;
2350 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00002351 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002352
2353 if (function_die != NULL)
2354 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002355 sc.function = sc.comp_unit->FindFunctionByUID (MakeUserID(function_die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002356 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00002357 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002358 }
2359
2360 if (sc.function != NULL)
2361 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00002362 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002363
2364 if (block_die != NULL)
Greg Clayton81c22f62011-10-19 18:09:39 +00002365 sc.block = block.FindBlockByID (MakeUserID(block_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002366 else
Greg Clayton81c22f62011-10-19 18:09:39 +00002367 sc.block = block.FindBlockByID (MakeUserID(function_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002368 }
2369 }
2370 }
2371
2372 sc_list.Append(sc);
2373 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
2374 }
2375 }
2376 }
2377 else if (file_spec_matches_cu_file_spec && !check_inlines)
2378 {
2379 // only append the context if we aren't looking for inline call sites
2380 // by file and line and if the file spec matches that of the compile unit
2381 sc_list.Append(sc);
2382 }
2383 }
2384 else if (file_spec_matches_cu_file_spec && !check_inlines)
2385 {
2386 // only append the context if we aren't looking for inline call sites
2387 // by file and line and if the file spec matches that of the compile unit
2388 sc_list.Append(sc);
2389 }
2390
2391 if (!check_inlines)
2392 break;
2393 }
2394 }
2395 }
2396 }
2397 return sc_list.GetSize() - prev_size;
2398}
2399
2400void
2401SymbolFileDWARF::Index ()
2402{
2403 if (m_indexed)
2404 return;
2405 m_indexed = true;
2406 Timer scoped_timer (__PRETTY_FUNCTION__,
2407 "SymbolFileDWARF::Index (%s)",
2408 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
2409
2410 DWARFDebugInfo* debug_info = DebugInfo();
2411 if (debug_info)
2412 {
2413 uint32_t cu_idx = 0;
2414 const uint32_t num_compile_units = GetNumCompileUnits();
2415 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
2416 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002417 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002418
Greg Clayton96d7d742010-11-10 23:42:09 +00002419 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002420
Greg Clayton96d7d742010-11-10 23:42:09 +00002421 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00002422 m_function_basename_index,
2423 m_function_fullname_index,
2424 m_function_method_index,
2425 m_function_selector_index,
2426 m_objc_class_selectors_index,
2427 m_global_index,
2428 m_type_index,
Greg Claytond4a2b372011-09-12 23:21:58 +00002429 m_namespace_index);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002430
2431 // Keep memory down by clearing DIEs if this generate function
2432 // caused them to be parsed
2433 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00002434 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002435 }
2436
Greg Claytond4a2b372011-09-12 23:21:58 +00002437 m_function_basename_index.Finalize();
2438 m_function_fullname_index.Finalize();
2439 m_function_method_index.Finalize();
2440 m_function_selector_index.Finalize();
2441 m_objc_class_selectors_index.Finalize();
2442 m_global_index.Finalize();
2443 m_type_index.Finalize();
2444 m_namespace_index.Finalize();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002445
Greg Clayton24739922010-10-13 03:15:28 +00002446#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00002447 StreamFile s(stdout, false);
Greg Claytonf9eec202011-09-01 23:16:13 +00002448 s.Printf ("DWARF index for '%s/%s':",
Greg Clayton24739922010-10-13 03:15:28 +00002449 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
2450 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00002451 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
2452 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
2453 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
2454 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
2455 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
2456 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00002457 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00002458 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002459#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002460 }
2461}
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002462
2463bool
2464SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
2465{
2466 if (namespace_decl == NULL)
2467 {
2468 // Invalid namespace decl which means we aren't matching only things
2469 // in this symbol file, so return true to indicate it matches this
2470 // symbol file.
2471 return true;
2472 }
2473
2474 clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
2475
2476 if (namespace_ast == NULL)
2477 return true; // No AST in the "namespace_decl", return true since it
2478 // could then match any symbol file, including this one
2479
2480 if (namespace_ast == GetClangASTContext().getASTContext())
2481 return true; // The ASTs match, return true
2482
2483 // The namespace AST was valid, and it does not match...
Sean Callananc41e68b2011-10-13 21:08:11 +00002484 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2485
2486 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002487 GetObjectFile()->GetModule()->LogMessage(log.get(), "Valid namespace does not match symbol file");
Sean Callananc41e68b2011-10-13 21:08:11 +00002488
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002489 return false;
2490}
2491
Greg Clayton2506a7a2011-10-12 23:34:26 +00002492bool
2493SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
2494 DWARFCompileUnit* cu,
2495 const DWARFDebugInfoEntry* die)
2496{
2497 // No namespace specified, so the answesr i
2498 if (namespace_decl == NULL)
2499 return true;
Sean Callananebe60672011-10-13 21:50:33 +00002500
2501 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002502
Greg Clayton2506a7a2011-10-12 23:34:26 +00002503 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2504 if (decl_ctx_die)
2505 {
Greg Claytonbfe3dd42011-10-13 00:00:53 +00002506
Greg Clayton2506a7a2011-10-12 23:34:26 +00002507 clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
2508 if (clang_namespace_decl)
2509 {
2510 if (decl_ctx_die->Tag() != DW_TAG_namespace)
Sean Callananebe60672011-10-13 21:50:33 +00002511 {
2512 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002513 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent is not a namespace");
Greg Clayton2506a7a2011-10-12 23:34:26 +00002514 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002515 }
2516
Greg Clayton2506a7a2011-10-12 23:34:26 +00002517 DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
2518
2519 if (pos == m_decl_ctx_to_die.end())
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 in a namespace, but its parent is not the requested namespace");
Sean Callananebe60672011-10-13 21:50:33 +00002523
Greg Clayton2506a7a2011-10-12 23:34:26 +00002524 return false;
Sean Callananebe60672011-10-13 21:50:33 +00002525 }
Greg Clayton2506a7a2011-10-12 23:34:26 +00002526
Greg Claytoncb5860a2011-10-13 23:49:28 +00002527 return pos->second.count (decl_ctx_die);
Greg Clayton2506a7a2011-10-12 23:34:26 +00002528 }
2529 else
2530 {
2531 // We have a namespace_decl that was not NULL but it contained
2532 // a NULL "clang::NamespaceDecl", so this means the global namespace
2533 // So as long the the contained decl context DIE isn't a namespace
2534 // we should be ok.
2535 if (decl_ctx_die->Tag() != DW_TAG_namespace)
2536 return true;
2537 }
2538 }
Sean Callananebe60672011-10-13 21:50:33 +00002539
2540 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00002541 GetObjectFile()->GetModule()->LogMessage(log.get(), "Found a match, but its parent doesn't exist");
Sean Callananebe60672011-10-13 21:50:33 +00002542
Greg Clayton2506a7a2011-10-12 23:34:26 +00002543 return false;
2544}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002545uint32_t
Sean Callanan213fdb82011-10-13 01:49:10 +00002546SymbolFileDWARF::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 +00002547{
Greg Clayton21f2a492011-10-06 00:09:08 +00002548 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2549
2550 if (log)
2551 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002552 GetObjectFile()->GetModule()->LogMessage (log.get(),
2553 "SymbolFileDWARF::FindGlobalVariables (name=\"%s\", namespace_decl=%p, append=%u, max_matches=%u, variables)",
2554 name.GetCString(),
2555 namespace_decl,
2556 append,
2557 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002558 }
Sean Callanan213fdb82011-10-13 01:49:10 +00002559
2560 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2561 return 0;
2562
Greg Claytonc685f8e2010-09-15 04:15:46 +00002563 DWARFDebugInfo* info = DebugInfo();
2564 if (info == NULL)
2565 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002566
2567 // If we aren't appending the results to this list, then clear the list
2568 if (!append)
2569 variables.Clear();
2570
2571 // Remember how many variables are in the list before we search in case
2572 // we are appending the results to a variable list.
2573 const uint32_t original_size = variables.GetSize();
2574
Greg Claytond4a2b372011-09-12 23:21:58 +00002575 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002576
Greg Clayton97fbc342011-10-20 22:30:33 +00002577 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002578 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002579 if (m_apple_names_ap.get())
2580 {
2581 const char *name_cstr = name.GetCString();
2582 const char *base_name_start;
2583 const char *base_name_end = NULL;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002584
Greg Clayton97fbc342011-10-20 22:30:33 +00002585 if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2586 base_name_start = name_cstr;
2587
2588 m_apple_names_ap->FindByName (base_name_start, die_offsets);
2589 }
Greg Clayton7f995132011-10-04 22:41:51 +00002590 }
2591 else
2592 {
2593 // Index the DWARF if we haven't already
2594 if (!m_indexed)
2595 Index ();
2596
2597 m_global_index.Find (name, die_offsets);
2598 }
2599
2600 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002601 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002602 {
Greg Clayton7f995132011-10-04 22:41:51 +00002603 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +00002604 sc.module_sp = m_obj_file->GetModule()->shared_from_this();
Greg Clayton7f995132011-10-04 22:41:51 +00002605 assert (sc.module_sp);
2606
Greg Claytond4a2b372011-09-12 23:21:58 +00002607 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton7f995132011-10-04 22:41:51 +00002608 DWARFCompileUnit* dwarf_cu = NULL;
2609 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002610 for (size_t i=0; i<num_matches; ++i)
2611 {
2612 const dw_offset_t die_offset = die_offsets[i];
2613 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002614
Greg Clayton95d87902011-11-11 03:16:25 +00002615 if (die)
2616 {
2617 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2618 assert(sc.comp_unit != NULL);
2619
2620 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2621 continue;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002622
Greg Clayton95d87902011-11-11 03:16:25 +00002623 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002624
Greg Clayton95d87902011-11-11 03:16:25 +00002625 if (variables.GetSize() - original_size >= max_matches)
2626 break;
2627 }
2628 else
2629 {
2630 if (m_using_apple_tables)
2631 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002632 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')\n",
2633 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00002634 }
2635 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002636 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002637 }
2638
2639 // Return the number of variable that were appended to the list
2640 return variables.GetSize() - original_size;
2641}
2642
2643uint32_t
2644SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2645{
Greg Clayton21f2a492011-10-06 00:09:08 +00002646 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2647
2648 if (log)
2649 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002650 GetObjectFile()->GetModule()->LogMessage (log.get(),
2651 "SymbolFileDWARF::FindGlobalVariables (regex=\"%s\", append=%u, max_matches=%u, variables)",
2652 regex.GetText(),
2653 append,
2654 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00002655 }
2656
Greg Claytonc685f8e2010-09-15 04:15:46 +00002657 DWARFDebugInfo* info = DebugInfo();
2658 if (info == NULL)
2659 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002660
2661 // If we aren't appending the results to this list, then clear the list
2662 if (!append)
2663 variables.Clear();
2664
2665 // Remember how many variables are in the list before we search in case
2666 // we are appending the results to a variable list.
2667 const uint32_t original_size = variables.GetSize();
2668
Greg Clayton7f995132011-10-04 22:41:51 +00002669 DIEArray die_offsets;
2670
Greg Clayton97fbc342011-10-20 22:30:33 +00002671 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00002672 {
Greg Clayton97fbc342011-10-20 22:30:33 +00002673 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00002674 {
2675 DWARFMappedHash::DIEInfoArray hash_data_array;
2676 if (m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
2677 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
2678 }
Greg Clayton7f995132011-10-04 22:41:51 +00002679 }
2680 else
2681 {
2682 // Index the DWARF if we haven't already
2683 if (!m_indexed)
2684 Index ();
2685
2686 m_global_index.Find (regex, die_offsets);
2687 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002688
Greg Claytonc685f8e2010-09-15 04:15:46 +00002689 SymbolContext sc;
Greg Claytone1cd1be2012-01-29 20:56:30 +00002690 sc.module_sp = m_obj_file->GetModule()->shared_from_this();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002691 assert (sc.module_sp);
2692
Greg Claytond4a2b372011-09-12 23:21:58 +00002693 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002694 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00002695 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002696 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002697 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002698 DWARFDebugInfo* debug_info = DebugInfo();
2699 for (size_t i=0; i<num_matches; ++i)
2700 {
2701 const dw_offset_t die_offset = die_offsets[i];
2702 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00002703
2704 if (die)
2705 {
2706 sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002707
Greg Clayton95d87902011-11-11 03:16:25 +00002708 ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002709
Greg Clayton95d87902011-11-11 03:16:25 +00002710 if (variables.GetSize() - original_size >= max_matches)
2711 break;
2712 }
2713 else
2714 {
2715 if (m_using_apple_tables)
2716 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002717 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for regex '%s')\n",
2718 die_offset, regex.GetText());
Greg Clayton95d87902011-11-11 03:16:25 +00002719 }
2720 }
Greg Claytond4a2b372011-09-12 23:21:58 +00002721 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002722 }
2723
2724 // Return the number of variable that were appended to the list
2725 return variables.GetSize() - original_size;
2726}
2727
Greg Claytonaa044962011-10-13 00:59:38 +00002728
Jim Ingham4cda6e02011-10-07 22:23:45 +00002729bool
2730SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2731 DWARFCompileUnit *&dwarf_cu,
2732 SymbolContextList& sc_list)
Greg Clayton9e315582011-09-02 04:03:59 +00002733{
Greg Claytonaa044962011-10-13 00:59:38 +00002734 const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2735 return ResolveFunction (dwarf_cu, die, sc_list);
2736}
2737
2738
2739bool
2740SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2741 const DWARFDebugInfoEntry *die,
2742 SymbolContextList& sc_list)
2743{
Greg Clayton9e315582011-09-02 04:03:59 +00002744 SymbolContext sc;
Greg Claytonaa044962011-10-13 00:59:38 +00002745
2746 if (die == NULL)
2747 return false;
2748
Jim Ingham4cda6e02011-10-07 22:23:45 +00002749 // If we were passed a die that is not a function, just return false...
2750 if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2751 return false;
2752
2753 const DWARFDebugInfoEntry* inlined_die = NULL;
2754 if (die->Tag() == DW_TAG_inlined_subroutine)
Greg Clayton9e315582011-09-02 04:03:59 +00002755 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002756 inlined_die = die;
Greg Clayton9e315582011-09-02 04:03:59 +00002757
Jim Ingham4cda6e02011-10-07 22:23:45 +00002758 while ((die = die->GetParent()) != NULL)
Greg Clayton2bc22f82011-09-30 03:20:47 +00002759 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002760 if (die->Tag() == DW_TAG_subprogram)
2761 break;
Greg Clayton9e315582011-09-02 04:03:59 +00002762 }
2763 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00002764 assert (die->Tag() == DW_TAG_subprogram);
Greg Claytonaa044962011-10-13 00:59:38 +00002765 if (GetFunction (cu, die, sc))
Jim Ingham4cda6e02011-10-07 22:23:45 +00002766 {
2767 Address addr;
2768 // Parse all blocks if needed
2769 if (inlined_die)
2770 {
Greg Clayton81c22f62011-10-19 18:09:39 +00002771 sc.block = sc.function->GetBlock (true).FindBlockByID (MakeUserID(inlined_die->GetOffset()));
Jim Ingham4cda6e02011-10-07 22:23:45 +00002772 assert (sc.block != NULL);
2773 if (sc.block->GetStartAddress (addr) == false)
2774 addr.Clear();
2775 }
2776 else
2777 {
2778 sc.block = NULL;
2779 addr = sc.function->GetAddressRange().GetBaseAddress();
2780 }
2781
2782 if (addr.IsValid())
2783 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00002784 sc_list.Append(sc);
Greg Claytonaa044962011-10-13 00:59:38 +00002785 return true;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002786 }
2787 }
2788
Greg Claytonaa044962011-10-13 00:59:38 +00002789 return false;
Greg Clayton9e315582011-09-02 04:03:59 +00002790}
2791
Greg Clayton7f995132011-10-04 22:41:51 +00002792void
2793SymbolFileDWARF::FindFunctions (const ConstString &name,
2794 const NameToDIE &name_to_die,
2795 SymbolContextList& sc_list)
2796{
Greg Claytond4a2b372011-09-12 23:21:58 +00002797 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00002798 if (name_to_die.Find (name, die_offsets))
2799 {
2800 ParseFunctions (die_offsets, sc_list);
2801 }
2802}
2803
2804
2805void
2806SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2807 const NameToDIE &name_to_die,
2808 SymbolContextList& sc_list)
2809{
2810 DIEArray die_offsets;
2811 if (name_to_die.Find (regex, die_offsets))
2812 {
2813 ParseFunctions (die_offsets, sc_list);
2814 }
2815}
2816
2817
2818void
2819SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2820 const DWARFMappedHash::MemoryTable &memory_table,
2821 SymbolContextList& sc_list)
2822{
2823 DIEArray die_offsets;
Greg Claytond1767f02011-12-08 02:13:16 +00002824 DWARFMappedHash::DIEInfoArray hash_data_array;
2825 if (memory_table.AppendAllDIEsThatMatchingRegex (regex, hash_data_array))
Greg Clayton7f995132011-10-04 22:41:51 +00002826 {
Greg Claytond1767f02011-12-08 02:13:16 +00002827 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
Greg Clayton7f995132011-10-04 22:41:51 +00002828 ParseFunctions (die_offsets, sc_list);
2829 }
2830}
2831
2832void
2833SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2834 SymbolContextList& sc_list)
2835{
2836 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00002837 if (num_matches)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002838 {
Greg Clayton7f995132011-10-04 22:41:51 +00002839 SymbolContext sc;
Greg Clayton7f995132011-10-04 22:41:51 +00002840
2841 DWARFCompileUnit* dwarf_cu = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00002842 for (size_t i=0; i<num_matches; ++i)
Greg Claytond7e05462010-11-14 00:22:48 +00002843 {
Greg Claytond4a2b372011-09-12 23:21:58 +00002844 const dw_offset_t die_offset = die_offsets[i];
Jim Ingham4cda6e02011-10-07 22:23:45 +00002845 ResolveFunction (die_offset, dwarf_cu, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002846 }
2847 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002848}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002849
Jim Ingham4cda6e02011-10-07 22:23:45 +00002850bool
2851SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2852 const DWARFCompileUnit *dwarf_cu,
2853 uint32_t name_type_mask,
2854 const char *partial_name,
2855 const char *base_name_start,
2856 const char *base_name_end)
2857{
2858 // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2859 if (name_type_mask == eFunctionNameTypeMethod
2860 || name_type_mask == eFunctionNameTypeBase)
2861 {
Greg Claytonf0705c82011-10-22 03:33:13 +00002862 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2863 if (!containing_decl_ctx)
2864 return false;
2865
2866 bool is_cxx_method = DeclKindIsCXXClass(containing_decl_ctx->getDeclKind());
2867
2868 if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2869 return false;
2870 if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2871 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002872 }
2873
2874 // Now we need to check whether the name we got back for this type matches the extra specifications
2875 // that were in the name we're looking up:
2876 if (base_name_start != partial_name || *base_name_end != '\0')
2877 {
2878 // First see if the stuff to the left matches the full name. To do that let's see if
2879 // we can pull out the mips linkage name attribute:
2880
2881 Mangled best_name;
2882
2883 DWARFDebugInfoEntry::Attributes attributes;
2884 die->GetAttributes(this, dwarf_cu, NULL, attributes);
2885 uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2886 if (idx != UINT32_MAX)
2887 {
2888 DWARFFormValue form_value;
2889 if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2890 {
2891 const char *name = form_value.AsCString(&get_debug_str_data());
2892 best_name.SetValue (name, true);
2893 }
2894 }
2895 if (best_name)
2896 {
2897 const char *demangled = best_name.GetDemangledName().GetCString();
2898 if (demangled)
2899 {
2900 std::string name_no_parens(partial_name, base_name_end - partial_name);
2901 if (strstr (demangled, name_no_parens.c_str()) == NULL)
Jim Ingham4cda6e02011-10-07 22:23:45 +00002902 return false;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002903 }
2904 }
2905 }
2906
2907 return true;
2908}
Greg Claytonc685f8e2010-09-15 04:15:46 +00002909
Greg Clayton0c5cd902010-06-28 21:30:43 +00002910uint32_t
Greg Clayton2bc22f82011-09-30 03:20:47 +00002911SymbolFileDWARF::FindFunctions (const ConstString &name,
Sean Callanan213fdb82011-10-13 01:49:10 +00002912 const lldb_private::ClangNamespaceDecl *namespace_decl,
Sean Callanan9df05fb2012-02-10 22:52:19 +00002913 uint32_t name_type_mask,
2914 bool include_inlines,
Greg Clayton2bc22f82011-09-30 03:20:47 +00002915 bool append,
2916 SymbolContextList& sc_list)
Greg Clayton0c5cd902010-06-28 21:30:43 +00002917{
2918 Timer scoped_timer (__PRETTY_FUNCTION__,
2919 "SymbolFileDWARF::FindFunctions (name = '%s')",
2920 name.AsCString());
2921
Greg Clayton21f2a492011-10-06 00:09:08 +00002922 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2923
2924 if (log)
2925 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00002926 GetObjectFile()->GetModule()->LogMessage (log.get(),
2927 "SymbolFileDWARF::FindFunctions (name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2928 name.GetCString(),
2929 name_type_mask,
2930 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00002931 }
2932
Greg Clayton0c5cd902010-06-28 21:30:43 +00002933 // If we aren't appending the results to this list, then clear the list
2934 if (!append)
2935 sc_list.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00002936
2937 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2938 return 0;
Jim Ingham4cda6e02011-10-07 22:23:45 +00002939
2940 // If name is empty then we won't find anything.
2941 if (name.IsEmpty())
2942 return 0;
Greg Clayton0c5cd902010-06-28 21:30:43 +00002943
2944 // Remember how many sc_list are in the list before we search in case
2945 // we are appending the results to a variable list.
Greg Clayton9e315582011-09-02 04:03:59 +00002946
Greg Clayton9e315582011-09-02 04:03:59 +00002947 const uint32_t original_size = sc_list.GetSize();
Greg Clayton0c5cd902010-06-28 21:30:43 +00002948
Jim Ingham4cda6e02011-10-07 22:23:45 +00002949 const char *name_cstr = name.GetCString();
2950 uint32_t effective_name_type_mask = eFunctionNameTypeNone;
2951 const char *base_name_start = name_cstr;
2952 const char *base_name_end = name_cstr + strlen(name_cstr);
2953
2954 if (name_type_mask & eFunctionNameTypeAuto)
2955 {
2956 if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
2957 effective_name_type_mask = eFunctionNameTypeFull;
2958 else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
2959 effective_name_type_mask = eFunctionNameTypeFull;
2960 else
2961 {
2962 if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2963 effective_name_type_mask |= eFunctionNameTypeSelector;
2964
2965 if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2966 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
2967 }
2968 }
2969 else
2970 {
2971 effective_name_type_mask = name_type_mask;
2972 if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
2973 {
2974 // If they've asked for a CPP method or function name and it can't be that, we don't
2975 // even need to search for CPP methods or names.
2976 if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2977 {
2978 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
2979 if (effective_name_type_mask == eFunctionNameTypeNone)
2980 return 0;
2981 }
2982 }
2983
2984 if (effective_name_type_mask & eFunctionNameTypeSelector)
2985 {
2986 if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2987 {
2988 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
2989 if (effective_name_type_mask == eFunctionNameTypeNone)
2990 return 0;
2991 }
2992 }
2993 }
2994
2995 DWARFDebugInfo* info = DebugInfo();
2996 if (info == NULL)
2997 return 0;
2998
Greg Claytonaa044962011-10-13 00:59:38 +00002999 DWARFCompileUnit *dwarf_cu = NULL;
Greg Clayton97fbc342011-10-20 22:30:33 +00003000 if (m_using_apple_tables)
Greg Clayton4d01ace2011-09-29 16:58:15 +00003001 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003002 if (m_apple_names_ap.get())
Jim Ingham4cda6e02011-10-07 22:23:45 +00003003 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003004
3005 DIEArray die_offsets;
3006
3007 uint32_t num_matches = 0;
3008
3009 if (effective_name_type_mask & eFunctionNameTypeFull)
Greg Claytonaa044962011-10-13 00:59:38 +00003010 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003011 // If they asked for the full name, match what they typed. At some point we may
3012 // want to canonicalize this (strip double spaces, etc. For now, we just add all the
3013 // dies that we find by exact match.
Jim Ingham4cda6e02011-10-07 22:23:45 +00003014 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003015 for (uint32_t i = 0; i < num_matches; i++)
3016 {
Greg Clayton95d87902011-11-11 03:16:25 +00003017 const dw_offset_t die_offset = die_offsets[i];
3018 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytonaa044962011-10-13 00:59:38 +00003019 if (die)
3020 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003021 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3022 continue;
3023
Sean Callanan9df05fb2012-02-10 22:52:19 +00003024 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3025 continue;
3026
Greg Claytonaa044962011-10-13 00:59:38 +00003027 ResolveFunction (dwarf_cu, die, sc_list);
3028 }
Greg Clayton95d87902011-11-11 03:16:25 +00003029 else
3030 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003031 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3032 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003033 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003034 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003035 }
3036 else
3037 {
3038 if (effective_name_type_mask & eFunctionNameTypeSelector)
3039 {
3040 if (namespace_decl && *namespace_decl)
3041 return 0; // no selectors in namespaces
3042
3043 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
3044 // Now make sure these are actually ObjC methods. In this case we can simply look up the name,
3045 // and if it is an ObjC method name, we're good.
3046
3047 for (uint32_t i = 0; i < num_matches; i++)
3048 {
Greg Clayton95d87902011-11-11 03:16:25 +00003049 const dw_offset_t die_offset = die_offsets[i];
3050 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003051 if (die)
3052 {
3053 const char *die_name = die->GetName(this, dwarf_cu);
3054 if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
Sean Callanan9df05fb2012-02-10 22:52:19 +00003055 {
3056 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3057 continue;
3058
Greg Clayton97fbc342011-10-20 22:30:33 +00003059 ResolveFunction (dwarf_cu, die, sc_list);
Sean Callanan9df05fb2012-02-10 22:52:19 +00003060 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003061 }
Greg Clayton95d87902011-11-11 03:16:25 +00003062 else
3063 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003064 GetObjectFile()->GetModule()->ReportError ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3065 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003066 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003067 }
3068 die_offsets.clear();
3069 }
3070
3071 if (effective_name_type_mask & eFunctionNameTypeMethod
3072 || effective_name_type_mask & eFunctionNameTypeBase)
3073 {
3074 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
3075 (namespace_decl && *namespace_decl))
3076 return 0; // no methods in namespaces
3077
3078 // The apple_names table stores just the "base name" of C++ methods in the table. So we have to
3079 // extract the base name, look that up, and if there is any other information in the name we were
3080 // passed in we have to post-filter based on that.
3081
3082 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
3083 std::string base_name(base_name_start, base_name_end - base_name_start);
3084 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
3085
3086 for (uint32_t i = 0; i < num_matches; i++)
3087 {
Greg Clayton95d87902011-11-11 03:16:25 +00003088 const dw_offset_t die_offset = die_offsets[i];
3089 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Clayton97fbc342011-10-20 22:30:33 +00003090 if (die)
3091 {
3092 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3093 continue;
3094
3095 if (!FunctionDieMatchesPartialName(die,
3096 dwarf_cu,
3097 effective_name_type_mask,
3098 name_cstr,
3099 base_name_start,
3100 base_name_end))
3101 continue;
Sean Callanan9df05fb2012-02-10 22:52:19 +00003102
3103 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3104 continue;
Greg Clayton97fbc342011-10-20 22:30:33 +00003105
3106 // If we get to here, the die is good, and we should add it:
3107 ResolveFunction (dwarf_cu, die, sc_list);
3108 }
Greg Clayton95d87902011-11-11 03:16:25 +00003109 else
3110 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003111 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_names accelerator table had bad die 0x%8.8x for '%s')",
3112 die_offset, name_cstr);
Greg Clayton95d87902011-11-11 03:16:25 +00003113 }
Greg Clayton97fbc342011-10-20 22:30:33 +00003114 }
3115 die_offsets.clear();
3116 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003117 }
3118 }
Greg Clayton7f995132011-10-04 22:41:51 +00003119 }
3120 else
3121 {
3122
3123 // Index the DWARF if we haven't already
3124 if (!m_indexed)
3125 Index ();
3126
Greg Clayton7f995132011-10-04 22:41:51 +00003127 if (name_type_mask & eFunctionNameTypeFull)
3128 FindFunctions (name, m_function_fullname_index, sc_list);
3129
Jim Ingham4cda6e02011-10-07 22:23:45 +00003130 std::string base_name(base_name_start, base_name_end - base_name_start);
3131 ConstString base_name_const(base_name.c_str());
3132 DIEArray die_offsets;
3133 DWARFCompileUnit *dwarf_cu = NULL;
3134
3135 if (effective_name_type_mask & eFunctionNameTypeBase)
3136 {
3137 uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
Greg Claytonaa044962011-10-13 00:59:38 +00003138 for (uint32_t i = 0; i < num_base; i++)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003139 {
Greg Claytonaa044962011-10-13 00:59:38 +00003140 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3141 if (die)
3142 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003143 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3144 continue;
3145
Greg Claytonaa044962011-10-13 00:59:38 +00003146 if (!FunctionDieMatchesPartialName(die,
3147 dwarf_cu,
3148 effective_name_type_mask,
3149 name_cstr,
3150 base_name_start,
3151 base_name_end))
3152 continue;
Jim Ingham4cda6e02011-10-07 22:23:45 +00003153
Sean Callanan9df05fb2012-02-10 22:52:19 +00003154 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3155 continue;
3156
Greg Claytonaa044962011-10-13 00:59:38 +00003157 // If we get to here, the die is good, and we should add it:
3158 ResolveFunction (dwarf_cu, die, sc_list);
3159 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003160 }
3161 die_offsets.clear();
3162 }
3163
Jim Inghamea8005a2011-10-11 01:18:11 +00003164 if (effective_name_type_mask & eFunctionNameTypeMethod)
Jim Ingham4cda6e02011-10-07 22:23:45 +00003165 {
Sean Callanan213fdb82011-10-13 01:49:10 +00003166 if (namespace_decl && *namespace_decl)
3167 return 0; // no methods in namespaces
3168
Jim Ingham4cda6e02011-10-07 22:23:45 +00003169 uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
3170 {
Greg Claytonaa044962011-10-13 00:59:38 +00003171 for (uint32_t i = 0; i < num_base; i++)
3172 {
3173 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
3174 if (die)
3175 {
3176 if (!FunctionDieMatchesPartialName(die,
3177 dwarf_cu,
3178 effective_name_type_mask,
3179 name_cstr,
3180 base_name_start,
3181 base_name_end))
3182 continue;
3183
Sean Callanan9df05fb2012-02-10 22:52:19 +00003184 if (!include_inlines && die->Tag() == DW_TAG_inlined_subroutine)
3185 continue;
3186
Greg Claytonaa044962011-10-13 00:59:38 +00003187 // If we get to here, the die is good, and we should add it:
3188 ResolveFunction (dwarf_cu, die, sc_list);
3189 }
3190 }
Jim Ingham4cda6e02011-10-07 22:23:45 +00003191 }
3192 die_offsets.clear();
3193 }
Greg Clayton7f995132011-10-04 22:41:51 +00003194
Sean Callanan213fdb82011-10-13 01:49:10 +00003195 if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
Jim Ingham4cda6e02011-10-07 22:23:45 +00003196 {
Greg Clayton7f995132011-10-04 22:41:51 +00003197 FindFunctions (name, m_function_selector_index, sc_list);
Jim Ingham4cda6e02011-10-07 22:23:45 +00003198 }
3199
Greg Clayton4d01ace2011-09-29 16:58:15 +00003200 }
3201
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003202 // Return the number of variable that were appended to the list
3203 return sc_list.GetSize() - original_size;
3204}
3205
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003206uint32_t
Sean Callanan9df05fb2012-02-10 22:52:19 +00003207SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool include_inlines, bool append, SymbolContextList& sc_list)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003208{
3209 Timer scoped_timer (__PRETTY_FUNCTION__,
3210 "SymbolFileDWARF::FindFunctions (regex = '%s')",
3211 regex.GetText());
3212
Greg Clayton21f2a492011-10-06 00:09:08 +00003213 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3214
3215 if (log)
3216 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003217 GetObjectFile()->GetModule()->LogMessage (log.get(),
3218 "SymbolFileDWARF::FindFunctions (regex=\"%s\", append=%u, sc_list)",
3219 regex.GetText(),
3220 append);
Greg Clayton21f2a492011-10-06 00:09:08 +00003221 }
3222
3223
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003224 // If we aren't appending the results to this list, then clear the list
3225 if (!append)
3226 sc_list.Clear();
3227
3228 // Remember how many sc_list are in the list before we search in case
3229 // we are appending the results to a variable list.
3230 uint32_t original_size = sc_list.GetSize();
3231
Greg Clayton97fbc342011-10-20 22:30:33 +00003232 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003233 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003234 if (m_apple_names_ap.get())
3235 FindFunctions (regex, *m_apple_names_ap, sc_list);
Greg Clayton7f995132011-10-04 22:41:51 +00003236 }
3237 else
3238 {
Jim Ingham4cda6e02011-10-07 22:23:45 +00003239 // Index the DWARF if we haven't already
Greg Clayton7f995132011-10-04 22:41:51 +00003240 if (!m_indexed)
3241 Index ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003242
Greg Clayton7f995132011-10-04 22:41:51 +00003243 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003244
Greg Clayton7f995132011-10-04 22:41:51 +00003245 FindFunctions (regex, m_function_fullname_index, sc_list);
3246 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003247
3248 // Return the number of variable that were appended to the list
3249 return sc_list.GetSize() - original_size;
3250}
Jim Ingham318c9f22011-08-26 19:44:13 +00003251
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003252uint32_t
Greg Claytond1767f02011-12-08 02:13:16 +00003253SymbolFileDWARF::FindTypes (const SymbolContext& sc,
3254 const ConstString &name,
3255 const lldb_private::ClangNamespaceDecl *namespace_decl,
3256 bool append,
3257 uint32_t max_matches,
3258 TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003259{
Greg Claytonc685f8e2010-09-15 04:15:46 +00003260 DWARFDebugInfo* info = DebugInfo();
3261 if (info == NULL)
3262 return 0;
3263
Greg Clayton21f2a492011-10-06 00:09:08 +00003264 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3265
3266 if (log)
3267 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003268 GetObjectFile()->GetModule()->LogMessage (log.get(),
3269 "SymbolFileDWARF::FindTypes (sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
3270 name.GetCString(),
3271 append,
3272 max_matches);
Greg Clayton21f2a492011-10-06 00:09:08 +00003273 }
3274
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003275 // If we aren't appending the results to this list, then clear the list
3276 if (!append)
3277 types.Clear();
Sean Callanan213fdb82011-10-13 01:49:10 +00003278
3279 if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
3280 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003281
Greg Claytond4a2b372011-09-12 23:21:58 +00003282 DIEArray die_offsets;
Greg Clayton7f995132011-10-04 22:41:51 +00003283
Greg Clayton97fbc342011-10-20 22:30:33 +00003284 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003285 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003286 if (m_apple_types_ap.get())
3287 {
3288 const char *name_cstr = name.GetCString();
3289 m_apple_types_ap->FindByName (name_cstr, die_offsets);
3290 }
Greg Clayton7f995132011-10-04 22:41:51 +00003291 }
3292 else
3293 {
3294 if (!m_indexed)
3295 Index ();
3296
3297 m_type_index.Find (name, die_offsets);
3298 }
3299
Greg Clayton7f995132011-10-04 22:41:51 +00003300 const size_t num_matches = die_offsets.size();
3301
Greg Claytond4a2b372011-09-12 23:21:58 +00003302 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003303 {
Greg Clayton7f995132011-10-04 22:41:51 +00003304 const uint32_t initial_types_size = types.GetSize();
3305 DWARFCompileUnit* dwarf_cu = NULL;
3306 const DWARFDebugInfoEntry* die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00003307 DWARFDebugInfo* debug_info = DebugInfo();
3308 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003309 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003310 const dw_offset_t die_offset = die_offsets[i];
3311 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
3312
Greg Clayton95d87902011-11-11 03:16:25 +00003313 if (die)
Greg Clayton73bf5db2011-06-17 01:22:15 +00003314 {
Greg Clayton95d87902011-11-11 03:16:25 +00003315 if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
3316 continue;
3317
3318 Type *matching_type = ResolveType (dwarf_cu, die);
3319 if (matching_type)
3320 {
3321 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003322 types.InsertUnique (matching_type->shared_from_this());
Greg Clayton95d87902011-11-11 03:16:25 +00003323 if (types.GetSize() >= max_matches)
3324 break;
3325 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00003326 }
Greg Clayton95d87902011-11-11 03:16:25 +00003327 else
3328 {
3329 if (m_using_apple_tables)
3330 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003331 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
3332 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003333 }
3334 }
3335
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003336 }
Greg Clayton7f995132011-10-04 22:41:51 +00003337 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003338 }
Greg Clayton7f995132011-10-04 22:41:51 +00003339 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003340}
3341
3342
Greg Clayton526e5af2010-11-13 03:52:47 +00003343ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00003344SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
Sean Callanan213fdb82011-10-13 01:49:10 +00003345 const ConstString &name,
3346 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003347{
Greg Clayton21f2a492011-10-06 00:09:08 +00003348 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
3349
3350 if (log)
3351 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003352 GetObjectFile()->GetModule()->LogMessage (log.get(),
3353 "SymbolFileDWARF::FindNamespace (sc, name=\"%s\")",
3354 name.GetCString());
Greg Clayton21f2a492011-10-06 00:09:08 +00003355 }
Sean Callanan213fdb82011-10-13 01:49:10 +00003356
3357 if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
3358 return ClangNamespaceDecl();
Greg Clayton21f2a492011-10-06 00:09:08 +00003359
Greg Clayton526e5af2010-11-13 03:52:47 +00003360 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003361 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00003362 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00003363 {
Greg Clayton7f995132011-10-04 22:41:51 +00003364 DIEArray die_offsets;
3365
Greg Clayton526e5af2010-11-13 03:52:47 +00003366 // Index if we already haven't to make sure the compile units
3367 // get indexed and make their global DIE index list
Greg Clayton97fbc342011-10-20 22:30:33 +00003368 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00003369 {
Greg Clayton97fbc342011-10-20 22:30:33 +00003370 if (m_apple_namespaces_ap.get())
3371 {
3372 const char *name_cstr = name.GetCString();
3373 m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
3374 }
Greg Clayton7f995132011-10-04 22:41:51 +00003375 }
3376 else
3377 {
3378 if (!m_indexed)
3379 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00003380
Greg Clayton7f995132011-10-04 22:41:51 +00003381 m_namespace_index.Find (name, die_offsets);
3382 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003383
3384 DWARFCompileUnit* dwarf_cu = NULL;
Greg Clayton526e5af2010-11-13 03:52:47 +00003385 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00003386 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00003387 if (num_matches)
Greg Clayton526e5af2010-11-13 03:52:47 +00003388 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003389 DWARFDebugInfo* debug_info = DebugInfo();
3390 for (size_t i=0; i<num_matches; ++i)
Greg Clayton526e5af2010-11-13 03:52:47 +00003391 {
Greg Claytond4a2b372011-09-12 23:21:58 +00003392 const dw_offset_t die_offset = die_offsets[i];
3393 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Sean Callanan213fdb82011-10-13 01:49:10 +00003394
Greg Clayton95d87902011-11-11 03:16:25 +00003395 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00003396 {
Greg Clayton95d87902011-11-11 03:16:25 +00003397 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
3398 continue;
3399
3400 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
3401 if (clang_namespace_decl)
3402 {
3403 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
3404 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
Greg Claytond1767f02011-12-08 02:13:16 +00003405 break;
Greg Clayton95d87902011-11-11 03:16:25 +00003406 }
Greg Claytond4a2b372011-09-12 23:21:58 +00003407 }
Greg Clayton95d87902011-11-11 03:16:25 +00003408 else
3409 {
3410 if (m_using_apple_tables)
3411 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003412 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_namespaces accelerator table had bad die 0x%8.8x for '%s')\n",
3413 die_offset, name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00003414 }
3415 }
3416
Greg Clayton526e5af2010-11-13 03:52:47 +00003417 }
3418 }
Greg Clayton96d7d742010-11-10 23:42:09 +00003419 }
Greg Clayton526e5af2010-11-13 03:52:47 +00003420 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003421}
3422
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003423uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003424SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003425{
3426 // Remember how many sc_list are in the list before we search in case
3427 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003428 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003429
3430 const uint32_t num_die_offsets = die_offsets.size();
3431 // Parse all of the types we found from the pubtypes matches
3432 uint32_t i;
3433 uint32_t num_matches = 0;
3434 for (i = 0; i < num_die_offsets; ++i)
3435 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003436 Type *matching_type = ResolveTypeUID (die_offsets[i]);
3437 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003438 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003439 // We found a type pointer, now find the shared pointer form our type list
Greg Claytone1cd1be2012-01-29 20:56:30 +00003440 types.InsertUnique (matching_type->shared_from_this());
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003441 ++num_matches;
3442 if (num_matches >= max_matches)
3443 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003444 }
3445 }
3446
3447 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003448 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003449}
3450
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003451
3452size_t
Greg Clayton5113dc82011-08-12 06:47:54 +00003453SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
3454 clang::DeclContext *containing_decl_ctx,
Greg Clayton5113dc82011-08-12 06:47:54 +00003455 DWARFCompileUnit* dwarf_cu,
3456 const DWARFDebugInfoEntry *parent_die,
3457 bool skip_artificial,
3458 bool &is_static,
3459 TypeList* type_list,
3460 std::vector<clang_type_t>& function_param_types,
3461 std::vector<clang::ParmVarDecl*>& function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003462 unsigned &type_quals,
3463 ClangASTContext::TemplateParameterInfos &template_param_infos)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003464{
3465 if (parent_die == NULL)
3466 return 0;
3467
Greg Claytond88d7592010-09-15 08:33:30 +00003468 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3469
Greg Clayton7fedea22010-11-16 02:10:54 +00003470 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003471 const DWARFDebugInfoEntry *die;
3472 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3473 {
3474 dw_tag_t tag = die->Tag();
3475 switch (tag)
3476 {
3477 case DW_TAG_formal_parameter:
3478 {
3479 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003480 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003481 if (num_attributes > 0)
3482 {
3483 const char *name = NULL;
3484 Declaration decl;
3485 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003486 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003487 // one of None, Auto, Register, Extern, Static, PrivateExtern
3488
Sean Callanane2ef6e32010-09-23 03:01:22 +00003489 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003490 uint32_t i;
3491 for (i=0; i<num_attributes; ++i)
3492 {
3493 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3494 DWARFFormValue form_value;
3495 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3496 {
3497 switch (attr)
3498 {
3499 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3500 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3501 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3502 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
3503 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003504 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003505 case DW_AT_location:
3506 // if (form_value.BlockData())
3507 // {
3508 // const DataExtractor& debug_info_data = debug_info();
3509 // uint32_t block_length = form_value.Unsigned();
3510 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3511 // }
3512 // else
3513 // {
3514 // }
3515 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003516 case DW_AT_const_value:
3517 case DW_AT_default_value:
3518 case DW_AT_description:
3519 case DW_AT_endianity:
3520 case DW_AT_is_optional:
3521 case DW_AT_segment:
3522 case DW_AT_variable_parameter:
3523 default:
3524 case DW_AT_abstract_origin:
3525 case DW_AT_sibling:
3526 break;
3527 }
3528 }
3529 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00003530
Greg Clayton0fffff52010-09-24 05:15:53 +00003531 bool skip = false;
3532 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003533 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003534 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00003535 {
3536 // In order to determine if a C++ member function is
3537 // "const" we have to look at the const-ness of "this"...
3538 // Ugly, but that
3539 if (arg_idx == 0)
3540 {
Greg Claytonf0705c82011-10-22 03:33:13 +00003541 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
Sean Callanan763d72a2011-08-02 22:21:50 +00003542 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003543 // Often times compilers omit the "this" name for the
3544 // specification DIEs, so we can't rely upon the name
3545 // being in the formal parameter DIE...
3546 if (name == NULL || ::strcmp(name, "this")==0)
Greg Clayton7fedea22010-11-16 02:10:54 +00003547 {
Greg Clayton5113dc82011-08-12 06:47:54 +00003548 Type *this_type = ResolveTypeUID (param_type_die_offset);
3549 if (this_type)
3550 {
3551 uint32_t encoding_mask = this_type->GetEncodingMask();
3552 if (encoding_mask & Type::eEncodingIsPointerUID)
3553 {
3554 is_static = false;
3555
3556 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3557 type_quals |= clang::Qualifiers::Const;
3558 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3559 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00003560 }
3561 }
3562 }
3563 }
3564 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003565 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00003566 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003567 else
3568 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003569
Greg Clayton0fffff52010-09-24 05:15:53 +00003570 // HACK: Objective C formal parameters "self" and "_cmd"
3571 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00003572 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3573 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00003574 {
3575 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3576 skip = true;
3577 }
3578 }
3579 }
3580
3581 if (!skip)
3582 {
3583 Type *type = ResolveTypeUID(param_type_die_offset);
3584 if (type)
3585 {
Greg Claytonc93237c2010-10-01 20:48:32 +00003586 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00003587
Greg Clayton42ce2f32011-12-12 21:50:19 +00003588 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name,
3589 type->GetClangForwardType(),
3590 storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00003591 assert(param_var_decl);
3592 function_param_decls.push_back(param_var_decl);
3593 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003594 }
3595 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003596 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003597 }
3598 break;
3599
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00003600 case DW_TAG_template_type_parameter:
3601 case DW_TAG_template_value_parameter:
3602 ParseTemplateDIE (dwarf_cu, die,template_param_infos);
3603 break;
3604
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003605 default:
3606 break;
3607 }
3608 }
Greg Clayton7fedea22010-11-16 02:10:54 +00003609 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003610}
3611
3612size_t
3613SymbolFileDWARF::ParseChildEnumerators
3614(
3615 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00003616 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003617 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00003618 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003619 const DWARFDebugInfoEntry *parent_die
3620)
3621{
3622 if (parent_die == NULL)
3623 return 0;
3624
3625 size_t enumerators_added = 0;
3626 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003627 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3628
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003629 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3630 {
3631 const dw_tag_t tag = die->Tag();
3632 if (tag == DW_TAG_enumerator)
3633 {
3634 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003635 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003636 if (num_child_attributes > 0)
3637 {
3638 const char *name = NULL;
3639 bool got_value = false;
3640 int64_t enum_value = 0;
3641 Declaration decl;
3642
3643 uint32_t i;
3644 for (i=0; i<num_child_attributes; ++i)
3645 {
3646 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3647 DWARFFormValue form_value;
3648 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3649 {
3650 switch (attr)
3651 {
3652 case DW_AT_const_value:
3653 got_value = true;
3654 enum_value = form_value.Unsigned();
3655 break;
3656
3657 case DW_AT_name:
3658 name = form_value.AsCString(&get_debug_str_data());
3659 break;
3660
3661 case DW_AT_description:
3662 default:
3663 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3664 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3665 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3666 case DW_AT_sibling:
3667 break;
3668 }
3669 }
3670 }
3671
3672 if (name && name[0] && got_value)
3673 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003674 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3675 enumerator_clang_type,
3676 decl,
3677 name,
3678 enum_value,
3679 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003680 ++enumerators_added;
3681 }
3682 }
3683 }
3684 }
3685 return enumerators_added;
3686}
3687
3688void
3689SymbolFileDWARF::ParseChildArrayInfo
3690(
3691 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00003692 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003693 const DWARFDebugInfoEntry *parent_die,
3694 int64_t& first_index,
3695 std::vector<uint64_t>& element_orders,
3696 uint32_t& byte_stride,
3697 uint32_t& bit_stride
3698)
3699{
3700 if (parent_die == NULL)
3701 return;
3702
3703 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00003704 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003705 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3706 {
3707 const dw_tag_t tag = die->Tag();
3708 switch (tag)
3709 {
3710 case DW_TAG_enumerator:
3711 {
3712 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00003713 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003714 if (num_child_attributes > 0)
3715 {
3716 const char *name = NULL;
3717 bool got_value = false;
3718 int64_t enum_value = 0;
3719
3720 uint32_t i;
3721 for (i=0; i<num_child_attributes; ++i)
3722 {
3723 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3724 DWARFFormValue form_value;
3725 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3726 {
3727 switch (attr)
3728 {
3729 case DW_AT_const_value:
3730 got_value = true;
3731 enum_value = form_value.Unsigned();
3732 break;
3733
3734 case DW_AT_name:
3735 name = form_value.AsCString(&get_debug_str_data());
3736 break;
3737
3738 case DW_AT_description:
3739 default:
3740 case DW_AT_decl_file:
3741 case DW_AT_decl_line:
3742 case DW_AT_decl_column:
3743 case DW_AT_sibling:
3744 break;
3745 }
3746 }
3747 }
3748 }
3749 }
3750 break;
3751
3752 case DW_TAG_subrange_type:
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 uint64_t byte_size = 0;
3761 int64_t enum_value = 0;
3762 uint64_t num_elements = 0;
3763 uint64_t lower_bound = 0;
3764 uint64_t upper_bound = 0;
3765 uint32_t i;
3766 for (i=0; i<num_child_attributes; ++i)
3767 {
3768 const dw_attr_t attr = attributes.AttributeAtIndex(i);
3769 DWARFFormValue form_value;
3770 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3771 {
3772 switch (attr)
3773 {
3774 case DW_AT_const_value:
3775 got_value = true;
3776 enum_value = form_value.Unsigned();
3777 break;
3778
3779 case DW_AT_name:
3780 name = form_value.AsCString(&get_debug_str_data());
3781 break;
3782
3783 case DW_AT_count:
3784 num_elements = form_value.Unsigned();
3785 break;
3786
3787 case DW_AT_bit_stride:
3788 bit_stride = form_value.Unsigned();
3789 break;
3790
3791 case DW_AT_byte_stride:
3792 byte_stride = form_value.Unsigned();
3793 break;
3794
3795 case DW_AT_byte_size:
3796 byte_size = form_value.Unsigned();
3797 break;
3798
3799 case DW_AT_lower_bound:
3800 lower_bound = form_value.Unsigned();
3801 break;
3802
3803 case DW_AT_upper_bound:
3804 upper_bound = form_value.Unsigned();
3805 break;
3806
3807 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003808 case DW_AT_abstract_origin:
3809 case DW_AT_accessibility:
3810 case DW_AT_allocated:
3811 case DW_AT_associated:
3812 case DW_AT_data_location:
3813 case DW_AT_declaration:
3814 case DW_AT_description:
3815 case DW_AT_sibling:
3816 case DW_AT_threads_scaled:
3817 case DW_AT_type:
3818 case DW_AT_visibility:
3819 break;
3820 }
3821 }
3822 }
3823
3824 if (upper_bound > lower_bound)
3825 num_elements = upper_bound - lower_bound + 1;
3826
3827 if (num_elements > 0)
3828 element_orders.push_back (num_elements);
3829 }
3830 }
3831 break;
3832 }
3833 }
3834}
3835
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003836TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00003837SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003838{
3839 TypeSP type_sp;
3840 if (die != NULL)
3841 {
Greg Clayton96d7d742010-11-10 23:42:09 +00003842 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00003843 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003844 if (type_ptr == NULL)
3845 {
Greg Claytonca512b32011-01-14 04:54:56 +00003846 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3847 assert (lldb_cu);
3848 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00003849 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003850 }
3851 else if (type_ptr != DIE_IS_BEING_PARSED)
3852 {
3853 // Grab the existing type from the master types lists
Greg Claytone1cd1be2012-01-29 20:56:30 +00003854 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003855 }
3856
3857 }
3858 return type_sp;
3859}
3860
3861clang::DeclContext *
Sean Callanan72e49402011-08-05 23:43:37 +00003862SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003863{
3864 if (die_offset != DW_INVALID_OFFSET)
3865 {
3866 DWARFCompileUnitSP cu_sp;
3867 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003868 return GetClangDeclContextContainingDIE (cu_sp.get(), die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003869 }
3870 return NULL;
3871}
3872
Sean Callanan72e49402011-08-05 23:43:37 +00003873clang::DeclContext *
3874SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3875{
3876 if (die_offset != DW_INVALID_OFFSET)
3877 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00003878 DWARFDebugInfo* debug_info = DebugInfo();
3879 if (debug_info)
3880 {
3881 DWARFCompileUnitSP cu_sp;
3882 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3883 if (die)
3884 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3885 }
Sean Callanan72e49402011-08-05 23:43:37 +00003886 }
3887 return NULL;
3888}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003889
Greg Clayton96d7d742010-11-10 23:42:09 +00003890clang::NamespaceDecl *
3891SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3892{
Greg Clayton030a2042011-10-14 21:34:45 +00003893 if (die && die->Tag() == DW_TAG_namespace)
Greg Clayton96d7d742010-11-10 23:42:09 +00003894 {
Greg Clayton030a2042011-10-14 21:34:45 +00003895 // See if we already parsed this namespace DIE and associated it with a
3896 // uniqued namespace declaration
3897 clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die]);
3898 if (namespace_decl)
Greg Clayton96d7d742010-11-10 23:42:09 +00003899 return namespace_decl;
Greg Clayton030a2042011-10-14 21:34:45 +00003900 else
3901 {
3902 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
Greg Clayton9d3d6882011-10-31 23:51:19 +00003903 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (curr_cu, die, NULL);
3904 namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3905 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3906 if (log)
Greg Clayton030a2042011-10-14 21:34:45 +00003907 {
Greg Clayton9d3d6882011-10-31 23:51:19 +00003908 if (namespace_name)
Greg Clayton030a2042011-10-14 21:34:45 +00003909 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003910 GetObjectFile()->GetModule()->LogMessage (log.get(),
3911 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3912 GetClangASTContext().getASTContext(),
3913 MakeUserID(die->GetOffset()),
3914 namespace_name,
3915 namespace_decl,
3916 namespace_decl->getOriginalNamespace());
Greg Clayton030a2042011-10-14 21:34:45 +00003917 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003918 else
3919 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00003920 GetObjectFile()->GetModule()->LogMessage (log.get(),
3921 "ASTContext => %p: 0x%8.8llx: DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
3922 GetClangASTContext().getASTContext(),
3923 MakeUserID(die->GetOffset()),
3924 namespace_decl,
3925 namespace_decl->getOriginalNamespace());
Greg Clayton9d3d6882011-10-31 23:51:19 +00003926 }
Greg Clayton030a2042011-10-14 21:34:45 +00003927 }
Greg Clayton9d3d6882011-10-31 23:51:19 +00003928
3929 if (namespace_decl)
3930 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3931 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00003932 }
3933 }
3934 return NULL;
3935}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003936
3937clang::DeclContext *
Greg Claytoncab36a32011-12-08 05:16:30 +00003938SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
Sean Callanan72e49402011-08-05 23:43:37 +00003939{
Greg Clayton5cf58b92011-10-05 22:22:08 +00003940 clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3941 if (clang_decl_ctx)
3942 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003943 // If this DIE has a specification, or an abstract origin, then trace to those.
3944
Greg Claytoncab36a32011-12-08 05:16:30 +00003945 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003946 if (die_offset != DW_INVALID_OFFSET)
3947 return GetClangDeclContextForDIEOffset (sc, die_offset);
3948
Greg Claytoncab36a32011-12-08 05:16:30 +00003949 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Sean Callanan72e49402011-08-05 23:43:37 +00003950 if (die_offset != DW_INVALID_OFFSET)
3951 return GetClangDeclContextForDIEOffset (sc, die_offset);
3952
Greg Clayton3bffb082011-12-10 02:15:28 +00003953 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3954 if (log)
Greg Claytone38a5ed2012-01-05 03:57:59 +00003955 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 +00003956 // This is the DIE we want. Parse it, then query our map.
Greg Claytoncab36a32011-12-08 05:16:30 +00003957 bool assert_not_being_parsed = true;
3958 ResolveTypeUID (cu, die, assert_not_being_parsed);
3959
Greg Clayton5cf58b92011-10-05 22:22:08 +00003960 clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3961
3962 return clang_decl_ctx;
Sean Callanan72e49402011-08-05 23:43:37 +00003963}
3964
3965clang::DeclContext *
Greg Claytoncb5860a2011-10-13 23:49:28 +00003966SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die, const DWARFDebugInfoEntry **decl_ctx_die_copy)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003967{
Greg Claytonca512b32011-01-14 04:54:56 +00003968 if (m_clang_tu_decl == NULL)
3969 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003970
Greg Clayton2bc22f82011-09-30 03:20:47 +00003971 const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
Greg Claytoncb5860a2011-10-13 23:49:28 +00003972
3973 if (decl_ctx_die_copy)
3974 *decl_ctx_die_copy = decl_ctx_die;
Greg Clayton2bc22f82011-09-30 03:20:47 +00003975
3976 if (decl_ctx_die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003977 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00003978
Greg Clayton2bc22f82011-09-30 03:20:47 +00003979 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
3980 if (pos != m_die_to_decl_ctx.end())
3981 return pos->second;
3982
3983 switch (decl_ctx_die->Tag())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003984 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00003985 case DW_TAG_compile_unit:
3986 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003987
Greg Clayton2bc22f82011-09-30 03:20:47 +00003988 case DW_TAG_namespace:
Greg Clayton030a2042011-10-14 21:34:45 +00003989 return ResolveNamespaceDIE (cu, decl_ctx_die);
Greg Clayton2bc22f82011-09-30 03:20:47 +00003990 break;
3991
3992 case DW_TAG_structure_type:
3993 case DW_TAG_union_type:
3994 case DW_TAG_class_type:
3995 {
3996 Type* type = ResolveType (cu, decl_ctx_die);
3997 if (type)
3998 {
3999 clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
4000 if (decl_ctx)
Greg Claytonca512b32011-01-14 04:54:56 +00004001 {
Greg Clayton2bc22f82011-09-30 03:20:47 +00004002 LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
4003 if (decl_ctx)
4004 return decl_ctx;
Greg Claytonca512b32011-01-14 04:54:56 +00004005 }
4006 }
Greg Claytonca512b32011-01-14 04:54:56 +00004007 }
Greg Clayton2bc22f82011-09-30 03:20:47 +00004008 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004009
Greg Clayton2bc22f82011-09-30 03:20:47 +00004010 default:
4011 break;
Greg Claytonca512b32011-01-14 04:54:56 +00004012 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004013 }
Greg Clayton7a345282010-11-09 23:46:37 +00004014 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004015}
4016
Greg Clayton2bc22f82011-09-30 03:20:47 +00004017
4018const DWARFDebugInfoEntry *
4019SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
4020{
4021 if (cu && die)
4022 {
4023 const DWARFDebugInfoEntry * const decl_die = die;
4024
4025 while (die != NULL)
4026 {
4027 // If this is the original DIE that we are searching for a declaration
4028 // for, then don't look in the cache as we don't want our own decl
4029 // context to be our decl context...
4030 if (decl_die != die)
4031 {
4032 switch (die->Tag())
4033 {
4034 case DW_TAG_compile_unit:
4035 case DW_TAG_namespace:
4036 case DW_TAG_structure_type:
4037 case DW_TAG_union_type:
4038 case DW_TAG_class_type:
4039 return die;
4040
4041 default:
4042 break;
4043 }
4044 }
4045
4046 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
4047 if (die_offset != DW_INVALID_OFFSET)
4048 {
4049 DWARFCompileUnit *spec_cu = cu;
4050 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
4051 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
4052 if (spec_die_decl_ctx_die)
4053 return spec_die_decl_ctx_die;
4054 }
4055
4056 die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
4057 if (die_offset != DW_INVALID_OFFSET)
4058 {
4059 DWARFCompileUnit *abs_cu = cu;
4060 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
4061 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
4062 if (abs_die_decl_ctx_die)
4063 return abs_die_decl_ctx_die;
4064 }
4065
4066 die = die->GetParent();
4067 }
4068 }
4069 return NULL;
4070}
4071
4072
Greg Clayton901c5ca2011-12-03 04:40:03 +00004073Symbol *
4074SymbolFileDWARF::GetObjCClassSymbol (const ConstString &objc_class_name)
4075{
4076 Symbol *objc_class_symbol = NULL;
4077 if (m_obj_file)
4078 {
4079 Symtab *symtab = m_obj_file->GetSymtab();
4080 if (symtab)
4081 {
4082 objc_class_symbol = symtab->FindFirstSymbolWithNameAndType (objc_class_name,
4083 eSymbolTypeObjCClass,
4084 Symtab::eDebugNo,
4085 Symtab::eVisibilityAny);
4086 }
4087 }
4088 return objc_class_symbol;
4089}
4090
Greg Claytonc7f03b62012-01-12 04:33:28 +00004091// Some compilers don't emit the DW_AT_APPLE_objc_complete_type attribute. If they don't
4092// then we can end up looking through all class types for a complete type and never find
4093// the full definition. We need to know if this attribute is supported, so we determine
4094// this here and cache th result. We also need to worry about the debug map DWARF file
4095// if we are doing darwin DWARF in .o file debugging.
4096bool
4097SymbolFileDWARF::Supports_DW_AT_APPLE_objc_complete_type (DWARFCompileUnit *cu)
4098{
4099 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolCalculate)
4100 {
4101 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolNo;
4102 if (cu && cu->Supports_DW_AT_APPLE_objc_complete_type())
4103 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4104 else
4105 {
4106 DWARFDebugInfo* debug_info = DebugInfo();
4107 const uint32_t num_compile_units = GetNumCompileUnits();
4108 for (uint32_t cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
4109 {
4110 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
4111 if (curr_cu != cu && curr_cu->Supports_DW_AT_APPLE_objc_complete_type())
4112 {
4113 m_supports_DW_AT_APPLE_objc_complete_type = eLazyBoolYes;
4114 break;
4115 }
4116 }
4117 }
4118 if (m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolNo && m_debug_map_symfile)
4119 return m_debug_map_symfile->Supports_DW_AT_APPLE_objc_complete_type (this);
4120 }
4121 return m_supports_DW_AT_APPLE_objc_complete_type == eLazyBoolYes;
4122}
Greg Clayton901c5ca2011-12-03 04:40:03 +00004123
4124// This function can be used when a DIE is found that is a forward declaration
4125// DIE and we want to try and find a type that has the complete definition.
4126TypeSP
Greg Claytonc7f03b62012-01-12 04:33:28 +00004127SymbolFileDWARF::FindCompleteObjCDefinitionTypeForDIE (const DWARFDebugInfoEntry *die,
4128 const ConstString &type_name,
4129 bool must_be_implementation)
Greg Clayton901c5ca2011-12-03 04:40:03 +00004130{
4131
4132 TypeSP type_sp;
4133
Greg Claytonc7f03b62012-01-12 04:33:28 +00004134 if (!type_name || (must_be_implementation && !GetObjCClassSymbol (type_name)))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004135 return type_sp;
4136
4137 DIEArray die_offsets;
4138
4139 if (m_using_apple_tables)
4140 {
4141 if (m_apple_types_ap.get())
4142 {
4143 const char *name_cstr = type_name.GetCString();
Greg Clayton68221ec2012-01-18 20:58:12 +00004144 m_apple_types_ap->FindCompleteObjCClassByName (name_cstr, die_offsets, must_be_implementation);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004145 }
4146 }
4147 else
4148 {
4149 if (!m_indexed)
4150 Index ();
4151
4152 m_type_index.Find (type_name, die_offsets);
4153 }
4154
Greg Clayton901c5ca2011-12-03 04:40:03 +00004155 const size_t num_matches = die_offsets.size();
4156
Greg Clayton901c5ca2011-12-03 04:40:03 +00004157 DWARFCompileUnit* type_cu = NULL;
4158 const DWARFDebugInfoEntry* type_die = NULL;
4159 if (num_matches)
4160 {
4161 DWARFDebugInfo* debug_info = DebugInfo();
4162 for (size_t i=0; i<num_matches; ++i)
4163 {
4164 const dw_offset_t die_offset = die_offsets[i];
4165 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
4166
4167 if (type_die)
4168 {
4169 bool try_resolving_type = false;
4170
4171 // Don't try and resolve the DIE we are looking for with the DIE itself!
4172 if (type_die != die)
4173 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004174 switch (type_die->Tag())
Greg Clayton901c5ca2011-12-03 04:40:03 +00004175 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004176 case DW_TAG_class_type:
4177 case DW_TAG_structure_type:
4178 try_resolving_type = true;
4179 break;
4180 default:
4181 break;
Greg Clayton901c5ca2011-12-03 04:40:03 +00004182 }
4183 }
4184
4185 if (try_resolving_type)
4186 {
Sean Callanana9bc0652012-01-19 02:17:40 +00004187 if (must_be_implementation && type_cu->Supports_DW_AT_APPLE_objc_complete_type())
Greg Claytonc7f03b62012-01-12 04:33:28 +00004188 try_resolving_type = type_die->GetAttributeValueAsUnsigned (this, type_cu, DW_AT_APPLE_objc_complete_type, 0);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004189
4190 if (try_resolving_type)
4191 {
4192 Type *resolved_type = ResolveType (type_cu, type_die, false);
4193 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4194 {
4195 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4196 MakeUserID(die->GetOffset()),
4197 MakeUserID(curr_cu->GetOffset()),
4198 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4199 MakeUserID(type_die->GetOffset()),
4200 MakeUserID(type_cu->GetOffset()));
4201
Greg Claytonc7f03b62012-01-12 04:33:28 +00004202 if (die)
4203 m_die_to_type[die] = resolved_type;
Greg Claytone1cd1be2012-01-29 20:56:30 +00004204 type_sp = resolved_type->shared_from_this();
Greg Clayton901c5ca2011-12-03 04:40:03 +00004205 break;
4206 }
4207 }
4208 }
4209 }
4210 else
4211 {
4212 if (m_using_apple_tables)
4213 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004214 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4215 die_offset, type_name.GetCString());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004216 }
4217 }
4218
4219 }
4220 }
4221 return type_sp;
4222}
4223
Greg Clayton80c26302012-02-05 06:12:47 +00004224//----------------------------------------------------------------------
4225// This function helps to ensure that the declaration contexts match for
4226// two different DIEs. Often times debug information will refer to a
4227// forward declaration of a type (the equivalent of "struct my_struct;".
4228// There will often be a declaration of that type elsewhere that has the
4229// full definition. When we go looking for the full type "my_struct", we
4230// will find one or more matches in the accelerator tables and we will
4231// then need to make sure the type was in the same declaration context
4232// as the original DIE. This function can efficiently compare two DIEs
4233// and will return true when the declaration context matches, and false
4234// when they don't.
4235//----------------------------------------------------------------------
Greg Clayton890ff562012-02-02 05:48:16 +00004236bool
4237SymbolFileDWARF::DIEDeclContextsMatch (DWARFCompileUnit* cu1, const DWARFDebugInfoEntry *die1,
4238 DWARFCompileUnit* cu2, const DWARFDebugInfoEntry *die2)
4239{
4240 assert (die1 != die2);
4241 DWARFDIECollection decl_ctx_1;
4242 DWARFDIECollection decl_ctx_2;
Greg Clayton80c26302012-02-05 06:12:47 +00004243 //The declaration DIE stack is a stack of the declaration context
4244 // DIEs all the way back to the compile unit. If a type "T" is
4245 // declared inside a class "B", and class "B" is declared inside
4246 // a class "A" and class "A" is in a namespace "lldb", and the
4247 // namespace is in a compile unit, there will be a stack of DIEs:
4248 //
4249 // [0] DW_TAG_class_type for "B"
4250 // [1] DW_TAG_class_type for "A"
4251 // [2] DW_TAG_namespace for "lldb"
4252 // [3] DW_TAG_compile_unit for the source file.
4253 //
4254 // We grab both contexts and make sure that everything matches
4255 // all the way back to the compiler unit.
4256
4257 // First lets grab the decl contexts for both DIEs
Greg Clayton890ff562012-02-02 05:48:16 +00004258 die1->GetDeclContextDIEs (this, cu1, decl_ctx_1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004259 die2->GetDeclContextDIEs (this, cu2, decl_ctx_2);
Greg Clayton80c26302012-02-05 06:12:47 +00004260 // Make sure the context arrays have the same size, otherwise
4261 // we are done
Greg Clayton890ff562012-02-02 05:48:16 +00004262 const size_t count1 = decl_ctx_1.Size();
4263 const size_t count2 = decl_ctx_2.Size();
4264 if (count1 != count2)
4265 return false;
Greg Clayton80c26302012-02-05 06:12:47 +00004266
4267 // Make sure the DW_TAG values match all the way back up the the
4268 // compile unit. If they don't, then we are done.
Greg Clayton890ff562012-02-02 05:48:16 +00004269 const DWARFDebugInfoEntry *decl_ctx_die1;
4270 const DWARFDebugInfoEntry *decl_ctx_die2;
4271 size_t i;
4272 for (i=0; i<count1; i++)
4273 {
4274 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4275 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4276 if (decl_ctx_die1->Tag() != decl_ctx_die2->Tag())
4277 return false;
4278 }
Greg Clayton890ff562012-02-02 05:48:16 +00004279#if defined LLDB_CONFIGURATION_DEBUG
Greg Clayton80c26302012-02-05 06:12:47 +00004280
4281 // Make sure the top item in the decl context die array is always
4282 // DW_TAG_compile_unit. If it isn't then something went wrong in
4283 // the DWARFDebugInfoEntry::GetDeclContextDIEs() function...
Greg Clayton890ff562012-02-02 05:48:16 +00004284 assert (decl_ctx_1.GetDIEPtrAtIndex (count1 - 1)->Tag() == DW_TAG_compile_unit);
Greg Clayton80c26302012-02-05 06:12:47 +00004285
Greg Clayton890ff562012-02-02 05:48:16 +00004286#endif
4287 // Always skip the compile unit when comparing by only iterating up to
Greg Clayton80c26302012-02-05 06:12:47 +00004288 // "count - 1". Here we compare the names as we go.
Greg Clayton890ff562012-02-02 05:48:16 +00004289 for (i=0; i<count1 - 1; i++)
4290 {
4291 decl_ctx_die1 = decl_ctx_1.GetDIEPtrAtIndex (i);
4292 decl_ctx_die2 = decl_ctx_2.GetDIEPtrAtIndex (i);
4293 const char *name1 = decl_ctx_die1->GetName(this, cu1);
Sean Callanan5b26f272012-02-04 08:49:35 +00004294 const char *name2 = decl_ctx_die2->GetName(this, cu2);
Greg Clayton890ff562012-02-02 05:48:16 +00004295 // If the string was from a DW_FORM_strp, then the pointer will often
4296 // be the same!
Greg Clayton5569e642012-02-06 01:44:54 +00004297 if (name1 == name2)
4298 continue;
4299
4300 // Name pointers are not equal, so only compare the strings
4301 // if both are not NULL.
4302 if (name1 && name2)
Greg Clayton890ff562012-02-02 05:48:16 +00004303 {
Greg Clayton5569e642012-02-06 01:44:54 +00004304 // If the strings don't compare, we are done...
4305 if (strcmp(name1, name2) != 0)
Greg Clayton890ff562012-02-02 05:48:16 +00004306 return false;
Greg Clayton5569e642012-02-06 01:44:54 +00004307 }
4308 else
4309 {
4310 // One name was NULL while the other wasn't
4311 return false;
Greg Clayton890ff562012-02-02 05:48:16 +00004312 }
4313 }
Greg Clayton80c26302012-02-05 06:12:47 +00004314 // We made it through all of the checks and the declaration contexts
4315 // are equal.
Greg Clayton890ff562012-02-02 05:48:16 +00004316 return true;
4317}
Greg Clayton220a0072011-12-09 08:48:30 +00004318
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004319// This function can be used when a DIE is found that is a forward declaration
4320// DIE and we want to try and find a type that has the complete definition.
4321TypeSP
Greg Clayton7f995132011-10-04 22:41:51 +00004322SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
4323 const DWARFDebugInfoEntry *die,
4324 const ConstString &type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004325{
4326 TypeSP type_sp;
4327
Greg Clayton1a65ae12011-01-25 23:55:37 +00004328 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004329 return type_sp;
4330
Greg Clayton7f995132011-10-04 22:41:51 +00004331 DIEArray die_offsets;
4332
Greg Clayton97fbc342011-10-20 22:30:33 +00004333 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00004334 {
Greg Clayton97fbc342011-10-20 22:30:33 +00004335 if (m_apple_types_ap.get())
4336 {
Greg Claytond1767f02011-12-08 02:13:16 +00004337 if (m_apple_types_ap->GetHeader().header_data.atoms.size() > 1)
4338 {
Greg Claytonae920b62012-01-06 00:17:16 +00004339 m_apple_types_ap->FindByNameAndTag (type_name.GetCString(), die->Tag(), die_offsets);
Greg Claytond1767f02011-12-08 02:13:16 +00004340 }
4341 else
4342 {
4343 m_apple_types_ap->FindByName (type_name.GetCString(), die_offsets);
4344 }
Greg Clayton97fbc342011-10-20 22:30:33 +00004345 }
Greg Clayton7f995132011-10-04 22:41:51 +00004346 }
4347 else
4348 {
4349 if (!m_indexed)
4350 Index ();
4351
4352 m_type_index.Find (type_name, die_offsets);
4353 }
Greg Clayton7f995132011-10-04 22:41:51 +00004354
4355 const size_t num_matches = die_offsets.size();
Greg Clayton69974892010-12-03 21:42:06 +00004356
Greg Clayton934cb052011-12-03 00:27:05 +00004357 const dw_tag_t die_tag = die->Tag();
Greg Claytond4a2b372011-09-12 23:21:58 +00004358
4359 DWARFCompileUnit* type_cu = NULL;
4360 const DWARFDebugInfoEntry* type_die = NULL;
Greg Claytond4a2b372011-09-12 23:21:58 +00004361 if (num_matches)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004362 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004363 DWARFDebugInfo* debug_info = DebugInfo();
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004364 for (size_t i=0; i<num_matches; ++i)
4365 {
Greg Claytond4a2b372011-09-12 23:21:58 +00004366 const dw_offset_t die_offset = die_offsets[i];
4367 type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004368
Greg Clayton95d87902011-11-11 03:16:25 +00004369 if (type_die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004370 {
Greg Clayton934cb052011-12-03 00:27:05 +00004371 bool try_resolving_type = false;
4372
4373 // Don't try and resolve the DIE we are looking for with the DIE itself!
4374 if (type_die != die)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004375 {
Greg Clayton934cb052011-12-03 00:27:05 +00004376 const dw_tag_t type_die_tag = type_die->Tag();
4377 // Make sure the tags match
4378 if (type_die_tag == die_tag)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004379 {
Greg Clayton934cb052011-12-03 00:27:05 +00004380 // The tags match, lets try resolving this type
4381 try_resolving_type = true;
4382 }
4383 else
4384 {
4385 // The tags don't match, but we need to watch our for a
4386 // forward declaration for a struct and ("struct foo")
4387 // ends up being a class ("class foo { ... };") or
4388 // vice versa.
4389 switch (type_die_tag)
Greg Clayton95d87902011-11-11 03:16:25 +00004390 {
Greg Clayton934cb052011-12-03 00:27:05 +00004391 case DW_TAG_class_type:
4392 // We had a "class foo", see if we ended up with a "struct foo { ... };"
4393 try_resolving_type = (die_tag == DW_TAG_structure_type);
4394 break;
4395 case DW_TAG_structure_type:
4396 // We had a "struct foo", see if we ended up with a "class foo { ... };"
4397 try_resolving_type = (die_tag == DW_TAG_class_type);
4398 break;
4399 default:
4400 // Tags don't match, don't event try to resolve
4401 // using this type whose name matches....
Greg Clayton95d87902011-11-11 03:16:25 +00004402 break;
4403 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004404 }
4405 }
Greg Clayton934cb052011-12-03 00:27:05 +00004406
4407 if (try_resolving_type)
4408 {
Greg Clayton890ff562012-02-02 05:48:16 +00004409 // Make sure the decl contexts match all the way up
4410 if (DIEDeclContextsMatch(cu, die, type_cu, type_die))
Greg Clayton934cb052011-12-03 00:27:05 +00004411 {
Greg Clayton890ff562012-02-02 05:48:16 +00004412 Type *resolved_type = ResolveType (type_cu, type_die, false);
4413 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
4414 {
4415 DEBUG_PRINTF ("resolved 0x%8.8llx (cu 0x%8.8llx) from %s to 0x%8.8llx (cu 0x%8.8llx)\n",
4416 MakeUserID(die->GetOffset()),
4417 MakeUserID(curr_cu->GetOffset()),
4418 m_obj_file->GetFileSpec().GetFilename().AsCString(),
4419 MakeUserID(type_die->GetOffset()),
4420 MakeUserID(type_cu->GetOffset()));
4421
4422 m_die_to_type[die] = resolved_type;
Greg Claytonff7692a2012-02-02 18:16:59 +00004423 type_sp = resolved_type->shared_from_this();
Greg Clayton890ff562012-02-02 05:48:16 +00004424 break;
4425 }
Greg Clayton934cb052011-12-03 00:27:05 +00004426 }
4427 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004428 }
Greg Clayton95d87902011-11-11 03:16:25 +00004429 else
4430 {
4431 if (m_using_apple_tables)
4432 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004433 GetObjectFile()->GetModule()->ReportErrorIfModifyDetected ("the DWARF debug information has been modified (.apple_types accelerator table had bad die 0x%8.8x for '%s')\n",
4434 die_offset, type_name.GetCString());
Greg Clayton95d87902011-11-11 03:16:25 +00004435 }
4436 }
4437
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00004438 }
4439 }
4440 return type_sp;
4441}
4442
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004443TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00004444SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004445{
4446 TypeSP type_sp;
4447
Greg Clayton1be10fc2010-09-29 01:12:09 +00004448 if (type_is_new_ptr)
4449 *type_is_new_ptr = false;
Greg Clayton1fba87112012-02-09 20:03:49 +00004450
4451#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4452 static DIEStack g_die_stack;
4453 DIEStack::ScopedPopper scoped_die_logger(g_die_stack);
4454#endif
Greg Clayton1be10fc2010-09-29 01:12:09 +00004455
Sean Callananc7fbf732010-08-06 00:32:49 +00004456 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004457 if (die != NULL)
4458 {
Greg Clayton21f2a492011-10-06 00:09:08 +00004459 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
Greg Clayton3bffb082011-12-10 02:15:28 +00004460 if (log)
Sean Callanan5b26f272012-02-04 08:49:35 +00004461 {
4462 const DWARFDebugInfoEntry *context_die;
4463 clang::DeclContext *context = GetClangDeclContextContainingDIE (dwarf_cu, die, &context_die);
4464
Greg Clayton1fba87112012-02-09 20:03:49 +00004465 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 +00004466 die->GetOffset(),
4467 context,
4468 context_die->GetOffset(),
Greg Clayton3bffb082011-12-10 02:15:28 +00004469 DW_TAG_value_to_name(die->Tag()),
Greg Clayton1fba87112012-02-09 20:03:49 +00004470 die->GetName(this, dwarf_cu));
4471
4472#if defined(LLDB_CONFIGURATION_DEBUG) or defined(LLDB_CONFIGURATION_RELEASE)
4473 scoped_die_logger.Push (dwarf_cu, die);
4474 g_die_stack.LogDIEs(log.get(), this);
4475#endif
Sean Callanan5b26f272012-02-04 08:49:35 +00004476 }
Greg Clayton3bffb082011-12-10 02:15:28 +00004477//
4478// LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
4479// if (log && dwarf_cu)
4480// {
4481// StreamString s;
4482// die->DumpLocation (this, dwarf_cu, s);
Greg Claytone38a5ed2012-01-05 03:57:59 +00004483// GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
Greg Clayton3bffb082011-12-10 02:15:28 +00004484//
4485// }
Jim Ingham16746d12011-08-25 23:21:43 +00004486
Greg Clayton594e5ed2010-09-27 21:07:38 +00004487 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004488 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00004489 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004490 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004491 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00004492 if (type_is_new_ptr)
4493 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004494
Greg Clayton594e5ed2010-09-27 21:07:38 +00004495 const dw_tag_t tag = die->Tag();
4496
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004497 bool is_forward_declaration = false;
4498 DWARFDebugInfoEntry::Attributes attributes;
4499 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00004500 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00004501 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
4502 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00004503 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00004504 Declaration decl;
4505
Greg Clayton4957bf62010-09-30 21:49:03 +00004506 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00004507 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004508
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004509 dw_attr_t attr;
4510
4511 switch (tag)
4512 {
4513 case DW_TAG_base_type:
4514 case DW_TAG_pointer_type:
4515 case DW_TAG_reference_type:
4516 case DW_TAG_typedef:
4517 case DW_TAG_const_type:
4518 case DW_TAG_restrict_type:
4519 case DW_TAG_volatile_type:
Greg Claytond4436412011-10-28 21:00:00 +00004520 case DW_TAG_unspecified_type:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004521 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004522 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004523 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004524
Greg Claytond88d7592010-09-15 08:33:30 +00004525 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004526 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004527 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
4528
4529 if (num_attributes > 0)
4530 {
4531 uint32_t i;
4532 for (i=0; i<num_attributes; ++i)
4533 {
4534 attr = attributes.AttributeAtIndex(i);
4535 DWARFFormValue form_value;
4536 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4537 {
4538 switch (attr)
4539 {
4540 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4541 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4542 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4543 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00004544
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004545 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00004546 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
4547 // include the "&"...
4548 if (tag == DW_TAG_reference_type)
4549 {
4550 if (strchr (type_name_cstr, '&') == NULL)
4551 type_name_cstr = NULL;
4552 }
4553 if (type_name_cstr)
4554 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004555 break;
Greg Clayton36909642011-03-15 04:38:20 +00004556 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004557 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
4558 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
4559 default:
4560 case DW_AT_sibling:
4561 break;
4562 }
4563 }
4564 }
4565 }
4566
Greg Clayton81c22f62011-10-19 18:09:39 +00004567 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 +00004568
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004569 switch (tag)
4570 {
4571 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00004572 break;
4573
Greg Claytond4436412011-10-28 21:00:00 +00004574 case DW_TAG_unspecified_type:
4575 if (strcmp(type_name_cstr, "nullptr_t") == 0)
4576 {
4577 resolve_state = Type::eResolveStateFull;
4578 clang_type = ast.getASTContext()->NullPtrTy.getAsOpaquePtr();
4579 break;
4580 }
4581 // Fall through to base type below in case we can handle the type there...
4582
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004583 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00004584 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004585 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
4586 encoding,
4587 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004588 break;
4589
Greg Clayton526e5af2010-11-13 03:52:47 +00004590 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
4591 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
4592 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
4593 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
4594 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
4595 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004596 }
4597
Greg Claytonc7f03b62012-01-12 04:33:28 +00004598 if (clang_type == NULL && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004599 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004600 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
4601 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004602 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004603 static ConstString g_objc_type_name_id("id");
4604 static ConstString g_objc_type_name_Class("Class");
4605 static ConstString g_objc_type_name_selector("SEL");
4606
4607 if (type_name_const_str == g_objc_type_name_id)
4608 {
4609 if (log)
4610 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
4611 die->GetOffset(),
4612 DW_TAG_value_to_name(die->Tag()),
4613 die->GetName(this, dwarf_cu));
4614 clang_type = ast.GetBuiltInType_objc_id();
4615 encoding_data_type = Type::eEncodingIsUID;
4616 encoding_uid = LLDB_INVALID_UID;
4617 resolve_state = Type::eResolveStateFull;
Greg Clayton526e5af2010-11-13 03:52:47 +00004618
Greg Claytonc7f03b62012-01-12 04:33:28 +00004619 }
4620 else if (type_name_const_str == g_objc_type_name_Class)
4621 {
4622 if (log)
4623 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
4624 die->GetOffset(),
4625 DW_TAG_value_to_name(die->Tag()),
4626 die->GetName(this, dwarf_cu));
4627 clang_type = ast.GetBuiltInType_objc_Class();
4628 encoding_data_type = Type::eEncodingIsUID;
4629 encoding_uid = LLDB_INVALID_UID;
4630 resolve_state = Type::eResolveStateFull;
4631 }
4632 else if (type_name_const_str == g_objc_type_name_selector)
4633 {
4634 if (log)
4635 GetObjectFile()->GetModule()->LogMessage (log.get(), "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
4636 die->GetOffset(),
4637 DW_TAG_value_to_name(die->Tag()),
4638 die->GetName(this, dwarf_cu));
4639 clang_type = ast.GetBuiltInType_objc_selector();
4640 encoding_data_type = Type::eEncodingIsUID;
4641 encoding_uid = LLDB_INVALID_UID;
4642 resolve_state = Type::eResolveStateFull;
4643 }
Greg Claytonb0b9fe62010-08-03 00:35:52 +00004644 }
4645 }
4646
Greg Clayton81c22f62011-10-19 18:09:39 +00004647 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004648 this,
4649 type_name_const_str,
4650 byte_size,
4651 NULL,
4652 encoding_uid,
4653 encoding_data_type,
4654 &decl,
4655 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00004656 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00004657
Greg Clayton594e5ed2010-09-27 21:07:38 +00004658 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004659
4660// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
4661// if (encoding_type != NULL)
4662// {
4663// if (encoding_type != DIE_IS_BEING_PARSED)
4664// type_sp->SetEncodingType(encoding_type);
4665// else
4666// m_indirect_fixups.push_back(type_sp.get());
4667// }
4668 }
4669 break;
4670
4671 case DW_TAG_structure_type:
4672 case DW_TAG_union_type:
4673 case DW_TAG_class_type:
4674 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004675 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00004676 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004677
Greg Clayton9e409562010-07-28 02:04:09 +00004678 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton18774842011-11-29 23:40:34 +00004679 bool is_complete_objc_class = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004680 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00004681 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004682 if (num_attributes > 0)
4683 {
4684 uint32_t i;
4685 for (i=0; i<num_attributes; ++i)
4686 {
4687 attr = attributes.AttributeAtIndex(i);
4688 DWARFFormValue form_value;
4689 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4690 {
4691 switch (attr)
4692 {
Greg Clayton9e409562010-07-28 02:04:09 +00004693 case DW_AT_decl_file:
Greg Claytonc7f03b62012-01-12 04:33:28 +00004694 if (dwarf_cu->DW_AT_decl_file_attributes_are_invalid())
4695 {
4696 // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
4697 // point to the compile unit file, so we clear this invalid value
4698 // so that we can still unique types efficiently.
4699 decl.SetFile(FileSpec ("<invalid>", false));
4700 }
4701 else
4702 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
Greg Clayton9e409562010-07-28 02:04:09 +00004703 break;
4704
4705 case DW_AT_decl_line:
4706 decl.SetLine(form_value.Unsigned());
4707 break;
4708
4709 case DW_AT_decl_column:
4710 decl.SetColumn(form_value.Unsigned());
4711 break;
4712
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004713 case DW_AT_name:
4714 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00004715 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004716 break;
Greg Clayton9e409562010-07-28 02:04:09 +00004717
4718 case DW_AT_byte_size:
4719 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00004720 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00004721 break;
4722
4723 case DW_AT_accessibility:
4724 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
4725 break;
4726
4727 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00004728 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00004729 break;
4730
4731 case DW_AT_APPLE_runtime_class:
4732 class_language = (LanguageType)form_value.Signed();
4733 break;
4734
Greg Clayton18774842011-11-29 23:40:34 +00004735 case DW_AT_APPLE_objc_complete_type:
4736 is_complete_objc_class = form_value.Signed();
4737 break;
4738
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004739 case DW_AT_allocated:
4740 case DW_AT_associated:
4741 case DW_AT_data_location:
4742 case DW_AT_description:
4743 case DW_AT_start_scope:
4744 case DW_AT_visibility:
4745 default:
4746 case DW_AT_sibling:
4747 break;
4748 }
4749 }
4750 }
4751 }
4752
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004753 UniqueDWARFASTType unique_ast_entry;
Sean Callanan8e8072d2012-02-07 21:13:38 +00004754
4755 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
4756 this,
4757 dwarf_cu,
4758 die,
4759 decl,
4760 byte_size_valid ? byte_size : -1,
4761 unique_ast_entry))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004762 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00004763 // We have already parsed this type or from another
4764 // compile unit. GCC loves to use the "one definition
4765 // rule" which can result in multiple definitions
4766 // of the same class over and over in each compile
4767 // unit.
4768 type_sp = unique_ast_entry.m_type_sp;
4769 if (type_sp)
Greg Claytonc615ce42010-11-09 04:42:43 +00004770 {
Sean Callanan8e8072d2012-02-07 21:13:38 +00004771 m_die_to_type[die] = type_sp.get();
4772 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004773 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004774 }
4775
Greg Clayton81c22f62011-10-19 18:09:39 +00004776 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 +00004777
4778 int tag_decl_kind = -1;
4779 AccessType default_accessibility = eAccessNone;
4780 if (tag == DW_TAG_structure_type)
4781 {
4782 tag_decl_kind = clang::TTK_Struct;
4783 default_accessibility = eAccessPublic;
4784 }
4785 else if (tag == DW_TAG_union_type)
4786 {
4787 tag_decl_kind = clang::TTK_Union;
4788 default_accessibility = eAccessPublic;
4789 }
4790 else if (tag == DW_TAG_class_type)
4791 {
4792 tag_decl_kind = clang::TTK_Class;
4793 default_accessibility = eAccessPrivate;
4794 }
Greg Clayton3a5f29a2011-11-30 02:48:28 +00004795
4796 if (byte_size_valid && byte_size == 0 && type_name_cstr &&
4797 die->HasChildren() == false &&
4798 sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
4799 {
4800 // Work around an issue with clang at the moment where
4801 // forward declarations for objective C classes are emitted
4802 // as:
4803 // DW_TAG_structure_type [2]
4804 // DW_AT_name( "ForwardObjcClass" )
4805 // DW_AT_byte_size( 0x00 )
4806 // DW_AT_decl_file( "..." )
4807 // DW_AT_decl_line( 1 )
4808 //
4809 // Note that there is no DW_AT_declaration and there are
4810 // no children, and the byte size is zero.
4811 is_forward_declaration = true;
4812 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004813
Greg Clayton18774842011-11-29 23:40:34 +00004814 if (class_language == eLanguageTypeObjC)
4815 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00004816 if (!is_complete_objc_class && Supports_DW_AT_APPLE_objc_complete_type(dwarf_cu))
Greg Clayton901c5ca2011-12-03 04:40:03 +00004817 {
4818 // We have a valid eSymbolTypeObjCClass class symbol whose
4819 // name matches the current objective C class that we
4820 // are trying to find and this DIE isn't the complete
4821 // definition (we checked is_complete_objc_class above and
4822 // know it is false), so the real definition is in here somewhere
Greg Claytonc7f03b62012-01-12 04:33:28 +00004823 type_sp = FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004824
Greg Clayton901c5ca2011-12-03 04:40:03 +00004825 if (!type_sp && m_debug_map_symfile)
4826 {
4827 // We weren't able to find a full declaration in
4828 // this DWARF, see if we have a declaration anywhere
4829 // else...
Greg Claytonc7f03b62012-01-12 04:33:28 +00004830 type_sp = m_debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
Greg Clayton901c5ca2011-12-03 04:40:03 +00004831 }
4832
4833 if (type_sp)
4834 {
4835 if (log)
4836 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004837 GetObjectFile()->GetModule()->LogMessage (log.get(),
4838 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8llx",
4839 this,
4840 die->GetOffset(),
4841 DW_TAG_value_to_name(tag),
4842 type_name_cstr,
4843 type_sp->GetID());
Greg Clayton901c5ca2011-12-03 04:40:03 +00004844 }
4845
4846 // We found a real definition for this type elsewhere
4847 // so lets use it and cache the fact that we found
4848 // a complete type for this die
4849 m_die_to_type[die] = type_sp.get();
4850 return type_sp;
4851 }
4852 }
4853 }
4854
4855
4856 if (is_forward_declaration)
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004857 {
4858 // We have a forward declaration to a type and we need
4859 // to try and find a full declaration. We look in the
4860 // current type index just in case we have a forward
4861 // declaration followed by an actual declarations in the
4862 // DWARF. If this fails, we need to look elsewhere...
Greg Claytonc982b3d2011-11-28 01:45:00 +00004863 if (log)
4864 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004865 GetObjectFile()->GetModule()->LogMessage (log.get(),
4866 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
4867 this,
4868 die->GetOffset(),
4869 DW_TAG_value_to_name(tag),
4870 type_name_cstr);
Greg Claytonc982b3d2011-11-28 01:45:00 +00004871 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004872
4873 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
4874
4875 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00004876 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004877 // We weren't able to find a full declaration in
4878 // this DWARF, see if we have a declaration anywhere
4879 // else...
4880 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00004881 }
4882
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004883 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00004884 {
Greg Claytonc982b3d2011-11-28 01:45:00 +00004885 if (log)
4886 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00004887 GetObjectFile()->GetModule()->LogMessage (log.get(),
4888 "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8llx",
4889 this,
4890 die->GetOffset(),
4891 DW_TAG_value_to_name(tag),
4892 type_name_cstr,
4893 type_sp->GetID());
Greg Claytonc982b3d2011-11-28 01:45:00 +00004894 }
4895
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004896 // We found a real definition for this type elsewhere
4897 // so lets use it and cache the fact that we found
4898 // a complete type for this die
4899 m_die_to_type[die] = type_sp.get();
4900 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00004901 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004902 }
4903 assert (tag_decl_kind != -1);
4904 bool clang_type_was_created = false;
4905 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
4906 if (clang_type == NULL)
4907 {
Sean Callanan4d04c6a2012-02-09 22:54:11 +00004908 const DWARFDebugInfoEntry *decl_ctx_die;
4909
4910 clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton55561e92011-10-26 03:31:36 +00004911 if (accessibility == eAccessNone && decl_ctx)
4912 {
4913 // Check the decl context that contains this class/struct/union.
4914 // If it is a class we must give it an accessability.
4915 const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
4916 if (DeclKindIsCXXClass (containing_decl_kind))
4917 accessibility = default_accessibility;
4918 }
4919
Greg Claytonf0705c82011-10-22 03:33:13 +00004920 if (type_name_cstr && strchr (type_name_cstr, '<'))
4921 {
4922 ClangASTContext::TemplateParameterInfos template_param_infos;
4923 if (ParseTemplateParameterInfos (dwarf_cu, die, template_param_infos))
4924 {
4925 clang::ClassTemplateDecl *class_template_decl = ParseClassTemplateDecl (decl_ctx,
Greg Clayton55561e92011-10-26 03:31:36 +00004926 accessibility,
Greg Claytonf0705c82011-10-22 03:33:13 +00004927 type_name_cstr,
4928 tag_decl_kind,
4929 template_param_infos);
4930
4931 clang::ClassTemplateSpecializationDecl *class_specialization_decl = ast.CreateClassTemplateSpecializationDecl (decl_ctx,
4932 class_template_decl,
4933 tag_decl_kind,
4934 template_param_infos);
4935 clang_type = ast.CreateClassTemplateSpecializationType (class_specialization_decl);
4936 clang_type_was_created = true;
4937 }
4938 }
4939
4940 if (!clang_type_was_created)
4941 {
4942 clang_type_was_created = true;
Greg Clayton55561e92011-10-26 03:31:36 +00004943 clang_type = ast.CreateRecordType (decl_ctx,
4944 accessibility,
4945 type_name_cstr,
Greg Claytonf0705c82011-10-22 03:33:13 +00004946 tag_decl_kind,
Greg Claytonf0705c82011-10-22 03:33:13 +00004947 class_language);
4948 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004949 }
4950
4951 // Store a forward declaration to this class type in case any
4952 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00004953 // types for function prototypes.
4954 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton81c22f62011-10-19 18:09:39 +00004955 type_sp.reset (new Type (MakeUserID(die->GetOffset()),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004956 this,
4957 type_name_const_str,
4958 byte_size,
4959 NULL,
4960 LLDB_INVALID_UID,
4961 Type::eEncodingIsUID,
4962 &decl,
4963 clang_type,
4964 Type::eResolveStateForward));
Sean Callanan72772842012-02-22 23:57:45 +00004965
4966 type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004967
4968
4969 // Add our type to the unique type map so we don't
4970 // end up creating many copies of the same type over
4971 // and over in the ASTContext for our module
4972 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00004973 unique_ast_entry.m_symfile = this;
4974 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004975 unique_ast_entry.m_die = die;
4976 unique_ast_entry.m_declaration = decl;
Sean Callanan59700592012-02-13 22:30:16 +00004977 unique_ast_entry.m_byte_size = byte_size;
Greg Claytone576ab22011-02-15 00:19:15 +00004978 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
4979 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00004980
Sean Callanan12014a02011-12-08 23:45:45 +00004981 if (!is_forward_declaration)
4982 {
4983 if (die->HasChildren() == false)
4984 {
4985 // No children for this struct/union/class, lets finish it
4986 ast.StartTagDeclarationDefinition (clang_type);
4987 ast.CompleteTagDeclarationDefinition (clang_type);
4988 }
4989 else if (clang_type_was_created)
4990 {
4991 // Leave this as a forward declaration until we need
4992 // to know the details of the type. lldb_private::Type
4993 // will automatically call the SymbolFile virtual function
4994 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
4995 // When the definition needs to be defined.
4996 m_forward_decl_die_to_clang_type[die] = clang_type;
4997 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
4998 ClangASTContext::SetHasExternalStorage (clang_type, true);
4999 }
Greg Claytonc615ce42010-11-09 04:42:43 +00005000 }
Greg Claytoncab36a32011-12-08 05:16:30 +00005001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005002 }
5003 break;
5004
5005 case DW_TAG_enumeration_type:
5006 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005007 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005008 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005009
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005010 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005011
Greg Claytond88d7592010-09-15 08:33:30 +00005012 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005013 if (num_attributes > 0)
5014 {
5015 uint32_t i;
5016
5017 for (i=0; i<num_attributes; ++i)
5018 {
5019 attr = attributes.AttributeAtIndex(i);
5020 DWARFFormValue form_value;
5021 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5022 {
5023 switch (attr)
5024 {
Greg Clayton7a345282010-11-09 23:46:37 +00005025 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5026 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5027 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005028 case DW_AT_name:
5029 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005030 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005031 break;
Greg Clayton7a345282010-11-09 23:46:37 +00005032 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005033 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00005034 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5035 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005036 case DW_AT_allocated:
5037 case DW_AT_associated:
5038 case DW_AT_bit_stride:
5039 case DW_AT_byte_stride:
5040 case DW_AT_data_location:
5041 case DW_AT_description:
5042 case DW_AT_start_scope:
5043 case DW_AT_visibility:
5044 case DW_AT_specification:
5045 case DW_AT_abstract_origin:
5046 case DW_AT_sibling:
5047 break;
5048 }
5049 }
5050 }
5051
Greg Clayton81c22f62011-10-19 18:09:39 +00005052 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 +00005053
Greg Clayton1be10fc2010-09-29 01:12:09 +00005054 clang_type_t enumerator_clang_type = NULL;
5055 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
5056 if (clang_type == NULL)
5057 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005058 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
5059 DW_ATE_signed,
5060 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00005061 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Claytoncb5860a2011-10-13 23:49:28 +00005062 GetClangDeclContextContainingDIE (dwarf_cu, die, NULL),
Greg Claytonca512b32011-01-14 04:54:56 +00005063 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005064 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00005065 }
5066 else
5067 {
5068 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
5069 assert (enumerator_clang_type != NULL);
5070 }
5071
Greg Claytona2721472011-06-25 00:44:06 +00005072 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
5073
Greg Clayton81c22f62011-10-19 18:09:39 +00005074 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005075 this,
5076 type_name_const_str,
5077 byte_size,
5078 NULL,
5079 encoding_uid,
5080 Type::eEncodingIsUID,
5081 &decl,
5082 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005083 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005084
Greg Clayton6beaaa62011-01-17 03:46:26 +00005085 ast.StartTagDeclarationDefinition (clang_type);
5086 if (die->HasChildren())
5087 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005088 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
5089 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00005090 }
5091 ast.CompleteTagDeclarationDefinition (clang_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005092 }
5093 }
5094 break;
5095
Jim Inghamb0be4422010-08-12 01:20:14 +00005096 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005097 case DW_TAG_subprogram:
5098 case DW_TAG_subroutine_type:
5099 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005100 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005101 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005102
5103 const char *mangled = NULL;
5104 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00005105 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005106 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00005107 bool is_static = false;
5108 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00005109 bool is_explicit = false;
Sean Callanandbb58392011-11-02 01:38:59 +00005110 bool is_artificial = false;
Greg Clayton72da3972011-08-16 18:40:23 +00005111 dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
5112 dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
Greg Clayton0fffff52010-09-24 05:15:53 +00005113
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005114 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00005115 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005116
5117
Greg Claytond88d7592010-09-15 08:33:30 +00005118 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005119 if (num_attributes > 0)
5120 {
5121 uint32_t i;
5122 for (i=0; i<num_attributes; ++i)
5123 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00005124 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005125 DWARFFormValue form_value;
5126 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5127 {
5128 switch (attr)
5129 {
5130 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5131 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5132 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5133 case DW_AT_name:
5134 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005135 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005136 break;
5137
5138 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
5139 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005140 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005141 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00005142 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
5143 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00005144 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
Sean Callanandbb58392011-11-02 01:38:59 +00005145 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5146
Greg Claytonf51de672010-10-01 02:31:07 +00005147
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005148 case DW_AT_external:
5149 if (form_value.Unsigned())
5150 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00005151 if (storage == clang::SC_None)
5152 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005153 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00005154 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005155 }
5156 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005157
Greg Clayton72da3972011-08-16 18:40:23 +00005158 case DW_AT_specification:
5159 specification_die_offset = form_value.Reference(dwarf_cu);
5160 break;
5161
5162 case DW_AT_abstract_origin:
5163 abstract_origin_die_offset = form_value.Reference(dwarf_cu);
5164 break;
5165
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005166 case DW_AT_allocated:
5167 case DW_AT_associated:
5168 case DW_AT_address_class:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005169 case DW_AT_calling_convention:
5170 case DW_AT_data_location:
5171 case DW_AT_elemental:
5172 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005173 case DW_AT_frame_base:
5174 case DW_AT_high_pc:
5175 case DW_AT_low_pc:
5176 case DW_AT_object_pointer:
5177 case DW_AT_prototyped:
5178 case DW_AT_pure:
5179 case DW_AT_ranges:
5180 case DW_AT_recursive:
5181 case DW_AT_return_addr:
5182 case DW_AT_segment:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005183 case DW_AT_start_scope:
5184 case DW_AT_static_link:
5185 case DW_AT_trampoline:
5186 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005187 case DW_AT_vtable_elem_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005188 case DW_AT_description:
5189 case DW_AT_sibling:
5190 break;
5191 }
5192 }
5193 }
Greg Clayton24739922010-10-13 03:15:28 +00005194 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005195
Greg Clayton81c22f62011-10-19 18:09:39 +00005196 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 +00005197
Greg Clayton24739922010-10-13 03:15:28 +00005198 clang_type_t return_clang_type = NULL;
5199 Type *func_type = NULL;
5200
5201 if (type_die_offset != DW_INVALID_OFFSET)
5202 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00005203
Greg Clayton24739922010-10-13 03:15:28 +00005204 if (func_type)
Greg Clayton42ce2f32011-12-12 21:50:19 +00005205 return_clang_type = func_type->GetClangForwardType();
Greg Clayton24739922010-10-13 03:15:28 +00005206 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005207 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005208
Greg Claytonf51de672010-10-01 02:31:07 +00005209
Greg Clayton24739922010-10-13 03:15:28 +00005210 std::vector<clang_type_t> function_param_types;
5211 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005212
Greg Clayton24739922010-10-13 03:15:28 +00005213 // Parse the function children for the parameters
Sean Callanan763d72a2011-08-02 22:21:50 +00005214
Greg Claytoncb5860a2011-10-13 23:49:28 +00005215 const DWARFDebugInfoEntry *decl_ctx_die = NULL;
5216 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die, &decl_ctx_die);
Greg Clayton5113dc82011-08-12 06:47:54 +00005217 const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
5218
Greg Claytonf0705c82011-10-22 03:33:13 +00005219 const bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
Greg Clayton5113dc82011-08-12 06:47:54 +00005220 // Start off static. This will be set to false in ParseChildParameters(...)
5221 // if we find a "this" paramters as the first parameter
5222 if (is_cxx_method)
Sean Callanan763d72a2011-08-02 22:21:50 +00005223 is_static = true;
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005224 ClangASTContext::TemplateParameterInfos template_param_infos;
5225
Greg Clayton24739922010-10-13 03:15:28 +00005226 if (die->HasChildren())
5227 {
Greg Clayton0fffff52010-09-24 05:15:53 +00005228 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005229 ParseChildParameters (sc,
Greg Clayton5113dc82011-08-12 06:47:54 +00005230 containing_decl_ctx,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005231 dwarf_cu,
5232 die,
Sean Callanan763d72a2011-08-02 22:21:50 +00005233 skip_artificial,
5234 is_static,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005235 type_list,
5236 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00005237 function_param_decls,
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005238 type_quals,
5239 template_param_infos);
Greg Clayton24739922010-10-13 03:15:28 +00005240 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005241
Greg Clayton24739922010-10-13 03:15:28 +00005242 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005243 clang_type = ast.CreateFunctionType (return_clang_type,
5244 &function_param_types[0],
5245 function_param_types.size(),
5246 is_variadic,
5247 type_quals);
5248
Greg Clayton24739922010-10-13 03:15:28 +00005249 if (type_name_cstr)
5250 {
5251 bool type_handled = false;
Greg Clayton24739922010-10-13 03:15:28 +00005252 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005253 {
Greg Clayton7c810422012-01-18 23:40:49 +00005254 ConstString class_name;
Greg Claytone42ae842012-01-19 03:24:53 +00005255 ConstString class_name_no_category;
5256 if (ObjCLanguageRuntime::ParseMethodName (type_name_cstr, &class_name, NULL, NULL, &class_name_no_category))
Greg Clayton0fffff52010-09-24 05:15:53 +00005257 {
Greg Claytone42ae842012-01-19 03:24:53 +00005258 // Use the class name with no category if there is one
5259 if (class_name_no_category)
5260 class_name = class_name_no_category;
5261
Greg Clayton24739922010-10-13 03:15:28 +00005262 SymbolContext empty_sc;
5263 clang_type_t class_opaque_type = NULL;
Greg Clayton7c810422012-01-18 23:40:49 +00005264 if (class_name)
Greg Clayton0fffff52010-09-24 05:15:53 +00005265 {
Greg Clayton24739922010-10-13 03:15:28 +00005266 TypeList types;
Greg Clayton278a16b2012-01-19 00:52:59 +00005267 TypeSP complete_objc_class_type_sp (FindCompleteObjCDefinitionTypeForDIE (NULL, class_name, false));
Greg Claytonc7f03b62012-01-12 04:33:28 +00005268
5269 if (complete_objc_class_type_sp)
Greg Clayton0fffff52010-09-24 05:15:53 +00005270 {
Greg Claytonc7f03b62012-01-12 04:33:28 +00005271 clang_type_t type_clang_forward_type = complete_objc_class_type_sp->GetClangForwardType();
5272 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
5273 class_opaque_type = type_clang_forward_type;
Greg Clayton0fffff52010-09-24 05:15:53 +00005274 }
Greg Clayton24739922010-10-13 03:15:28 +00005275 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005276
Greg Clayton24739922010-10-13 03:15:28 +00005277 if (class_opaque_type)
5278 {
5279 // If accessibility isn't set to anything valid, assume public for
5280 // now...
5281 if (accessibility == eAccessNone)
5282 accessibility = eAccessPublic;
5283
5284 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005285 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
5286 type_name_cstr,
5287 clang_type,
5288 accessibility);
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005289 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
Greg Clayton24739922010-10-13 03:15:28 +00005290 type_handled = objc_method_decl != NULL;
5291 }
5292 }
Greg Clayton5113dc82011-08-12 06:47:54 +00005293 else if (is_cxx_method)
Greg Clayton24739922010-10-13 03:15:28 +00005294 {
5295 // Look at the parent of this DIE and see if is is
5296 // a class or struct and see if this is actually a
5297 // C++ method
Greg Claytoncb5860a2011-10-13 23:49:28 +00005298 Type *class_type = ResolveType (dwarf_cu, decl_ctx_die);
Greg Clayton24739922010-10-13 03:15:28 +00005299 if (class_type)
5300 {
Greg Clayton72da3972011-08-16 18:40:23 +00005301 if (specification_die_offset != DW_INVALID_OFFSET)
Greg Clayton0fffff52010-09-24 05:15:53 +00005302 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005303 // We have a specification which we are going to base our function
5304 // prototype off of, so we need this type to be completed so that the
5305 // m_die_to_decl_ctx for the method in the specification has a valid
5306 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005307 class_type->GetClangForwardType();
Greg Clayton72da3972011-08-16 18:40:23 +00005308 // If we have a specification, then the function type should have been
5309 // made with the specification and not with this die.
5310 DWARFCompileUnitSP spec_cu_sp;
5311 const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005312 clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die);
5313 if (spec_clang_decl_ctx)
5314 {
5315 LinkDeclContextToDIE(spec_clang_decl_ctx, die);
5316 }
5317 else
Jim Inghamc1663042011-09-29 22:12:35 +00005318 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005319 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_specification(0x%8.8x) has no decl\n",
5320 MakeUserID(die->GetOffset()),
5321 specification_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005322 }
Greg Clayton72da3972011-08-16 18:40:23 +00005323 type_handled = true;
5324 }
5325 else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
5326 {
Greg Clayton5cf58b92011-10-05 22:22:08 +00005327 // We have a specification which we are going to base our function
5328 // prototype off of, so we need this type to be completed so that the
5329 // m_die_to_decl_ctx for the method in the abstract origin has a valid
5330 // clang decl context.
Greg Clayton8eb732e2011-12-13 04:34:06 +00005331 class_type->GetClangForwardType();
Greg Clayton5cf58b92011-10-05 22:22:08 +00005332
Greg Clayton72da3972011-08-16 18:40:23 +00005333 DWARFCompileUnitSP abs_cu_sp;
5334 const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
Greg Clayton5cf58b92011-10-05 22:22:08 +00005335 clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die);
5336 if (abs_clang_decl_ctx)
5337 {
5338 LinkDeclContextToDIE (abs_clang_decl_ctx, die);
5339 }
5340 else
Jim Inghamc1663042011-09-29 22:12:35 +00005341 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005342 GetObjectFile()->GetModule()->ReportWarning ("0x%8.8llx: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
5343 MakeUserID(die->GetOffset()),
5344 abstract_origin_die_offset);
Jim Inghamc1663042011-09-29 22:12:35 +00005345 }
Greg Clayton72da3972011-08-16 18:40:23 +00005346 type_handled = true;
5347 }
5348 else
5349 {
5350 clang_type_t class_opaque_type = class_type->GetClangForwardType();
5351 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton931180e2011-01-27 06:44:37 +00005352 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005353 if (ClangASTContext::IsBeingDefined (class_opaque_type))
Greg Clayton72da3972011-08-16 18:40:23 +00005354 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005355 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
5356 // in the DWARF for C++ methods... Default to public for now...
5357 if (accessibility == eAccessNone)
5358 accessibility = eAccessPublic;
5359
5360 if (!is_static && !die->HasChildren())
5361 {
5362 // We have a C++ member function with no children (this pointer!)
5363 // and clang will get mad if we try and make a function that isn't
5364 // well formed in the DWARF, so we will just skip it...
5365 type_handled = true;
5366 }
5367 else
5368 {
5369 clang::CXXMethodDecl *cxx_method_decl;
5370 // REMOVE THE CRASH DESCRIPTION BELOW
Greg Clayton81c22f62011-10-19 18:09:39 +00005371 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 +00005372 type_name_cstr,
5373 class_type->GetName().GetCString(),
Greg Clayton81c22f62011-10-19 18:09:39 +00005374 MakeUserID(die->GetOffset()),
Greg Clayton20568dd2011-10-13 23:13:20 +00005375 m_obj_file->GetFileSpec().GetDirectory().GetCString(),
5376 m_obj_file->GetFileSpec().GetFilename().GetCString());
5377
Sean Callananc1b732d2011-11-01 18:07:13 +00005378 const bool is_attr_used = false;
5379
Greg Clayton20568dd2011-10-13 23:13:20 +00005380 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
5381 type_name_cstr,
5382 clang_type,
5383 accessibility,
5384 is_virtual,
5385 is_static,
5386 is_inline,
Sean Callananc1b732d2011-11-01 18:07:13 +00005387 is_explicit,
Sean Callanandbb58392011-11-02 01:38:59 +00005388 is_attr_used,
5389 is_artificial);
Greg Clayton20568dd2011-10-13 23:13:20 +00005390 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
5391
Greg Claytonf49e65a2011-11-10 18:31:53 +00005392 Host::SetCrashDescription (NULL);
5393
Greg Clayton20568dd2011-10-13 23:13:20 +00005394 type_handled = cxx_method_decl != NULL;
5395 }
Greg Clayton72da3972011-08-16 18:40:23 +00005396 }
5397 else
5398 {
Greg Clayton20568dd2011-10-13 23:13:20 +00005399 // We were asked to parse the type for a method in a class, yet the
5400 // class hasn't been asked to complete itself through the
5401 // clang::ExternalASTSource protocol, so we need to just have the
5402 // class complete itself and do things the right way, then our
5403 // DIE should then have an entry in the m_die_to_type map. First
5404 // we need to modify the m_die_to_type so it doesn't think we are
5405 // trying to parse this DIE anymore...
5406 m_die_to_type[die] = NULL;
5407
5408 // Now we get the full type to force our class type to complete itself
5409 // using the clang::ExternalASTSource protocol which will parse all
5410 // base classes and all methods (including the method for this DIE).
5411 class_type->GetClangFullType();
Greg Clayton2c5f0e92011-08-04 21:02:57 +00005412
Greg Clayton20568dd2011-10-13 23:13:20 +00005413 // The type for this DIE should have been filled in the function call above
5414 type_ptr = m_die_to_type[die];
5415 if (type_ptr)
5416 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005417 type_sp = type_ptr->shared_from_this();
Greg Clayton20568dd2011-10-13 23:13:20 +00005418 break;
5419 }
Greg Clayton72da3972011-08-16 18:40:23 +00005420 }
Greg Clayton931180e2011-01-27 06:44:37 +00005421 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005422 }
5423 }
Greg Clayton0fffff52010-09-24 05:15:53 +00005424 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005425 }
Greg Clayton24739922010-10-13 03:15:28 +00005426
5427 if (!type_handled)
5428 {
5429 // We just have a function that isn't part of a class
Greg Clayton147e1fa2011-10-14 22:47:18 +00005430 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (containing_decl_ctx,
5431 type_name_cstr,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005432 clang_type,
5433 storage,
5434 is_inline);
Greg Clayton3c2e3ae2012-02-06 06:42:51 +00005435
5436// if (template_param_infos.GetSize() > 0)
5437// {
5438// clang::FunctionTemplateDecl *func_template_decl = ast.CreateFunctionTemplateDecl (containing_decl_ctx,
5439// function_decl,
5440// type_name_cstr,
5441// template_param_infos);
5442//
5443// ast.CreateFunctionTemplateSpecializationInfo (function_decl,
5444// func_template_decl,
5445// template_param_infos);
5446// }
Greg Clayton24739922010-10-13 03:15:28 +00005447 // Add the decl to our DIE to decl context map
5448 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00005449 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00005450 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005451 ast.SetFunctionParameters (function_decl,
5452 &function_param_decls.front(),
5453 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00005454 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005455 }
Greg Clayton81c22f62011-10-19 18:09:39 +00005456 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005457 this,
5458 type_name_const_str,
5459 0,
5460 NULL,
5461 LLDB_INVALID_UID,
5462 Type::eEncodingIsUID,
5463 &decl,
5464 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005465 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00005466 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005467 }
5468 break;
5469
5470 case DW_TAG_array_type:
5471 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005472 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00005473 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005474
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005475 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005476 int64_t first_index = 0;
5477 uint32_t byte_stride = 0;
5478 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00005479 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005480
5481 if (num_attributes > 0)
5482 {
5483 uint32_t i;
5484 for (i=0; i<num_attributes; ++i)
5485 {
5486 attr = attributes.AttributeAtIndex(i);
5487 DWARFFormValue form_value;
5488 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5489 {
5490 switch (attr)
5491 {
5492 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5493 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5494 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5495 case DW_AT_name:
5496 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00005497 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005498 break;
5499
5500 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00005501 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005502 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
5503 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00005504 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00005505 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005506 case DW_AT_allocated:
5507 case DW_AT_associated:
5508 case DW_AT_data_location:
5509 case DW_AT_description:
5510 case DW_AT_ordering:
5511 case DW_AT_start_scope:
5512 case DW_AT_visibility:
5513 case DW_AT_specification:
5514 case DW_AT_abstract_origin:
5515 case DW_AT_sibling:
5516 break;
5517 }
5518 }
5519 }
5520
Greg Clayton81c22f62011-10-19 18:09:39 +00005521 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 +00005522
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005523 Type *element_type = ResolveTypeUID(type_die_offset);
5524
5525 if (element_type)
5526 {
5527 std::vector<uint64_t> element_orders;
5528 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00005529 // We have an array that claims to have no members, lets give it at least one member...
5530 if (element_orders.empty())
5531 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005532 if (byte_stride == 0 && bit_stride == 0)
5533 byte_stride = element_type->GetByteSize();
Greg Clayton42ce2f32011-12-12 21:50:19 +00005534 clang_type_t array_element_type = element_type->GetClangForwardType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005535 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
5536 uint64_t num_elements = 0;
5537 std::vector<uint64_t>::const_reverse_iterator pos;
5538 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
5539 for (pos = element_orders.rbegin(); pos != end; ++pos)
5540 {
5541 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005542 clang_type = ast.CreateArrayType (array_element_type,
5543 num_elements,
5544 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005545 array_element_type = clang_type;
5546 array_element_bit_stride = array_element_bit_stride * num_elements;
5547 }
5548 ConstString empty_name;
Greg Clayton81c22f62011-10-19 18:09:39 +00005549 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005550 this,
5551 empty_name,
5552 array_element_bit_stride / 8,
5553 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00005554 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005555 Type::eEncodingIsUID,
5556 &decl,
5557 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005558 Type::eResolveStateFull));
5559 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005560 }
5561 }
5562 }
5563 break;
5564
Greg Clayton9b81a312010-06-12 01:20:30 +00005565 case DW_TAG_ptr_to_member_type:
5566 {
5567 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
5568 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
5569
Greg Claytond88d7592010-09-15 08:33:30 +00005570 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00005571
5572 if (num_attributes > 0) {
5573 uint32_t i;
5574 for (i=0; i<num_attributes; ++i)
5575 {
5576 attr = attributes.AttributeAtIndex(i);
5577 DWARFFormValue form_value;
5578 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
5579 {
5580 switch (attr)
5581 {
5582 case DW_AT_type:
5583 type_die_offset = form_value.Reference(dwarf_cu); break;
5584 case DW_AT_containing_type:
5585 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
5586 }
5587 }
5588 }
5589
5590 Type *pointee_type = ResolveTypeUID(type_die_offset);
5591 Type *class_type = ResolveTypeUID(containing_type_die_offset);
5592
Greg Clayton526e5af2010-11-13 03:52:47 +00005593 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
5594 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00005595
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005596 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
5597 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00005598
Greg Clayton526e5af2010-11-13 03:52:47 +00005599 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
5600 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00005601
Greg Clayton81c22f62011-10-19 18:09:39 +00005602 type_sp.reset( new Type (MakeUserID(die->GetOffset()),
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00005603 this,
5604 type_name_const_str,
5605 byte_size,
5606 NULL,
5607 LLDB_INVALID_UID,
5608 Type::eEncodingIsUID,
5609 NULL,
5610 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00005611 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00005612 }
5613
5614 break;
5615 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005616 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00005617 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005618 break;
5619 }
5620
5621 if (type_sp.get())
5622 {
5623 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5624 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
5625
5626 SymbolContextScope * symbol_context_scope = NULL;
5627 if (sc_parent_tag == DW_TAG_compile_unit)
5628 {
5629 symbol_context_scope = sc.comp_unit;
5630 }
5631 else if (sc.function != NULL)
5632 {
Greg Clayton81c22f62011-10-19 18:09:39 +00005633 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005634 if (symbol_context_scope == NULL)
5635 symbol_context_scope = sc.function;
5636 }
5637
5638 if (symbol_context_scope != NULL)
5639 {
5640 type_sp->SetSymbolContextScope(symbol_context_scope);
5641 }
5642
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005643 // We are ready to put this type into the uniqued list up at the module level
5644 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00005645
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00005646 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005647 }
5648 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00005649 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005650 {
Greg Claytone1cd1be2012-01-29 20:56:30 +00005651 type_sp = type_ptr->shared_from_this();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005652 }
5653 }
5654 return type_sp;
5655}
5656
5657size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00005658SymbolFileDWARF::ParseTypes
5659(
5660 const SymbolContext& sc,
5661 DWARFCompileUnit* dwarf_cu,
5662 const DWARFDebugInfoEntry *die,
5663 bool parse_siblings,
5664 bool parse_children
5665)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005666{
5667 size_t types_added = 0;
5668 while (die != NULL)
5669 {
5670 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00005671 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005672 {
5673 if (type_is_new)
5674 ++types_added;
5675 }
5676
5677 if (parse_children && die->HasChildren())
5678 {
5679 if (die->Tag() == DW_TAG_subprogram)
5680 {
5681 SymbolContext child_sc(sc);
Greg Clayton81c22f62011-10-19 18:09:39 +00005682 child_sc.function = sc.comp_unit->FindFunctionByUID(MakeUserID(die->GetOffset())).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005683 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
5684 }
5685 else
5686 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
5687 }
5688
5689 if (parse_siblings)
5690 die = die->GetSibling();
5691 else
5692 die = NULL;
5693 }
5694 return types_added;
5695}
5696
5697
5698size_t
5699SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
5700{
5701 assert(sc.comp_unit && sc.function);
5702 size_t functions_added = 0;
5703 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5704 if (dwarf_cu)
5705 {
5706 dw_offset_t function_die_offset = sc.function->GetID();
5707 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
5708 if (function_die)
5709 {
Greg Claytondd7feaf2011-08-12 17:54:33 +00005710 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005711 }
5712 }
5713
5714 return functions_added;
5715}
5716
5717
5718size_t
5719SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
5720{
5721 // At least a compile unit must be valid
5722 assert(sc.comp_unit);
5723 size_t types_added = 0;
5724 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
5725 if (dwarf_cu)
5726 {
5727 if (sc.function)
5728 {
5729 dw_offset_t function_die_offset = sc.function->GetID();
5730 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
5731 if (func_die && func_die->HasChildren())
5732 {
5733 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
5734 }
5735 }
5736 else
5737 {
5738 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
5739 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
5740 {
5741 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
5742 }
5743 }
5744 }
5745
5746 return types_added;
5747}
5748
5749size_t
5750SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
5751{
5752 if (sc.comp_unit != NULL)
5753 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00005754 DWARFDebugInfo* info = DebugInfo();
5755 if (info == NULL)
5756 return 0;
5757
5758 uint32_t cu_idx = UINT32_MAX;
5759 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005760
5761 if (dwarf_cu == NULL)
5762 return 0;
5763
5764 if (sc.function)
5765 {
5766 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00005767
5768 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 +00005769 if (func_lo_pc != DW_INVALID_ADDRESS)
5770 {
5771 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
Greg Claytonc662ec82011-06-17 22:10:16 +00005772
Greg Claytone38a5ed2012-01-05 03:57:59 +00005773 // Let all blocks know they have parse all their variables
5774 sc.function->GetBlock (false).SetDidParseVariables (true, true);
5775 return num_variables;
5776 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005777 }
5778 else if (sc.comp_unit)
5779 {
5780 uint32_t vars_added = 0;
5781 VariableListSP variables (sc.comp_unit->GetVariableList(false));
5782
5783 if (variables.get() == NULL)
5784 {
5785 variables.reset(new VariableList());
5786 sc.comp_unit->SetVariableList(variables);
5787
Greg Claytond4a2b372011-09-12 23:21:58 +00005788 DWARFCompileUnit* match_dwarf_cu = NULL;
5789 const DWARFDebugInfoEntry* die = NULL;
5790 DIEArray die_offsets;
Greg Clayton97fbc342011-10-20 22:30:33 +00005791 if (m_using_apple_tables)
Greg Clayton7f995132011-10-04 22:41:51 +00005792 {
Greg Clayton97fbc342011-10-20 22:30:33 +00005793 if (m_apple_names_ap.get())
Greg Claytond1767f02011-12-08 02:13:16 +00005794 {
5795 DWARFMappedHash::DIEInfoArray hash_data_array;
5796 if (m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
5797 dwarf_cu->GetNextCompileUnitOffset(),
5798 hash_data_array))
5799 {
5800 DWARFMappedHash::ExtractDIEArray (hash_data_array, die_offsets);
5801 }
5802 }
Greg Clayton7f995132011-10-04 22:41:51 +00005803 }
5804 else
5805 {
5806 // Index if we already haven't to make sure the compile units
5807 // get indexed and make their global DIE index list
5808 if (!m_indexed)
5809 Index ();
5810
5811 m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
5812 dwarf_cu->GetNextCompileUnitOffset(),
5813 die_offsets);
5814 }
5815
5816 const size_t num_matches = die_offsets.size();
Greg Claytond4a2b372011-09-12 23:21:58 +00005817 if (num_matches)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005818 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005819 DWARFDebugInfo* debug_info = DebugInfo();
5820 for (size_t i=0; i<num_matches; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005821 {
Greg Claytond4a2b372011-09-12 23:21:58 +00005822 const dw_offset_t die_offset = die_offsets[i];
5823 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
Greg Clayton95d87902011-11-11 03:16:25 +00005824 if (die)
Greg Claytond4a2b372011-09-12 23:21:58 +00005825 {
Greg Clayton95d87902011-11-11 03:16:25 +00005826 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
5827 if (var_sp)
5828 {
5829 variables->AddVariableIfUnique (var_sp);
5830 ++vars_added;
5831 }
Greg Claytond4a2b372011-09-12 23:21:58 +00005832 }
Greg Clayton95d87902011-11-11 03:16:25 +00005833 else
5834 {
5835 if (m_using_apple_tables)
5836 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00005837 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 +00005838 }
5839 }
5840
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005841 }
5842 }
5843 }
5844 return vars_added;
5845 }
5846 }
5847 return 0;
5848}
5849
5850
5851VariableSP
5852SymbolFileDWARF::ParseVariableDIE
5853(
5854 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00005855 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00005856 const DWARFDebugInfoEntry *die,
5857 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005858)
5859{
5860
Greg Clayton83c5cd92010-11-14 22:13:40 +00005861 VariableSP var_sp (m_die_to_variable_sp[die]);
5862 if (var_sp)
5863 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005864
5865 const dw_tag_t tag = die->Tag();
Greg Clayton7f995132011-10-04 22:41:51 +00005866
5867 if ((tag == DW_TAG_variable) ||
5868 (tag == DW_TAG_constant) ||
5869 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005870 {
Greg Clayton7f995132011-10-04 22:41:51 +00005871 DWARFDebugInfoEntry::Attributes attributes;
5872 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
5873 if (num_attributes > 0)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005874 {
Greg Clayton7f995132011-10-04 22:41:51 +00005875 const char *name = NULL;
5876 const char *mangled = NULL;
5877 Declaration decl;
5878 uint32_t i;
Greg Claytond1767f02011-12-08 02:13:16 +00005879 lldb::user_id_t type_uid = LLDB_INVALID_UID;
Greg Clayton7f995132011-10-04 22:41:51 +00005880 DWARFExpression location;
5881 bool is_external = false;
5882 bool is_artificial = false;
5883 bool location_is_const_value_data = false;
5884 AccessType accessibility = eAccessNone;
5885
5886 for (i=0; i<num_attributes; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005887 {
Greg Clayton7f995132011-10-04 22:41:51 +00005888 dw_attr_t attr = attributes.AttributeAtIndex(i);
5889 DWARFFormValue form_value;
5890 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005891 {
Greg Clayton7f995132011-10-04 22:41:51 +00005892 switch (attr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005893 {
Greg Clayton7f995132011-10-04 22:41:51 +00005894 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
5895 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
5896 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
5897 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
5898 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytond1767f02011-12-08 02:13:16 +00005899 case DW_AT_type: type_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton7f995132011-10-04 22:41:51 +00005900 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
5901 case DW_AT_const_value:
5902 location_is_const_value_data = true;
5903 // Fall through...
5904 case DW_AT_location:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005905 {
Greg Clayton7f995132011-10-04 22:41:51 +00005906 if (form_value.BlockData())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005907 {
Greg Clayton7f995132011-10-04 22:41:51 +00005908 const DataExtractor& debug_info_data = get_debug_info_data();
5909
5910 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
5911 uint32_t block_length = form_value.Unsigned();
5912 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
5913 }
5914 else
5915 {
5916 const DataExtractor& debug_loc_data = get_debug_loc_data();
5917 const dw_offset_t debug_loc_offset = form_value.Unsigned();
5918
5919 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
5920 if (loc_list_length > 0)
5921 {
5922 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
5923 assert (func_low_pc != LLDB_INVALID_ADDRESS);
5924 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
5925 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005926 }
5927 }
Greg Clayton7f995132011-10-04 22:41:51 +00005928 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005929
Greg Clayton7f995132011-10-04 22:41:51 +00005930 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
5931 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
5932 case DW_AT_declaration:
5933 case DW_AT_description:
5934 case DW_AT_endianity:
5935 case DW_AT_segment:
5936 case DW_AT_start_scope:
5937 case DW_AT_visibility:
5938 default:
5939 case DW_AT_abstract_origin:
5940 case DW_AT_sibling:
5941 case DW_AT_specification:
5942 break;
5943 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005944 }
5945 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005946
Greg Clayton7f995132011-10-04 22:41:51 +00005947 if (location.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00005948 {
Greg Clayton7f995132011-10-04 22:41:51 +00005949 ValueType scope = eValueTypeInvalid;
5950
5951 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
5952 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005953 SymbolContextScope * symbol_context_scope = NULL;
Greg Clayton7f995132011-10-04 22:41:51 +00005954
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005955 // DWARF doesn't specify if a DW_TAG_variable is a local, global
5956 // or static variable, so we have to do a little digging by
5957 // looking at the location of a varaible to see if it contains
5958 // a DW_OP_addr opcode _somewhere_ in the definition. I say
5959 // somewhere because clang likes to combine small global variables
5960 // into the same symbol and have locations like:
5961 // DW_OP_addr(0x1000), DW_OP_constu(2), DW_OP_plus
5962 // So if we don't have a DW_TAG_formal_parameter, we can look at
5963 // the location to see if it contains a DW_OP_addr opcode, and
5964 // then we can correctly classify our variables.
Greg Clayton7f995132011-10-04 22:41:51 +00005965 if (tag == DW_TAG_formal_parameter)
5966 scope = eValueTypeVariableArgument;
Greg Claytond1767f02011-12-08 02:13:16 +00005967 else
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005968 {
Greg Clayton96c09682012-01-04 22:56:43 +00005969 bool op_error = false;
Greg Claytond1767f02011-12-08 02:13:16 +00005970 // Check if the location has a DW_OP_addr with any address value...
5971 addr_t location_has_op_addr = false;
5972 if (!location_is_const_value_data)
Greg Clayton96c09682012-01-04 22:56:43 +00005973 {
5974 location_has_op_addr = location.LocationContains_DW_OP_addr (LLDB_INVALID_ADDRESS, op_error);
5975 if (op_error)
5976 {
5977 StreamString strm;
5978 location.DumpLocationForAddress (&strm, eDescriptionLevelFull, 0, 0, NULL);
Greg Claytone38a5ed2012-01-05 03:57:59 +00005979 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 +00005980 }
5981 }
Greg Claytond1767f02011-12-08 02:13:16 +00005982
5983 if (location_has_op_addr)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005984 {
Greg Claytond1767f02011-12-08 02:13:16 +00005985 if (is_external)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005986 {
Greg Claytond1767f02011-12-08 02:13:16 +00005987 scope = eValueTypeVariableGlobal;
5988
5989 if (m_debug_map_symfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00005990 {
Greg Claytond1767f02011-12-08 02:13:16 +00005991 // When leaving the DWARF in the .o files on darwin,
5992 // when we have a global variable that wasn't initialized,
5993 // the .o file might not have allocated a virtual
5994 // address for the global variable. In this case it will
5995 // have created a symbol for the global variable
5996 // that is undefined and external and the value will
5997 // be the byte size of the variable. When we do the
5998 // address map in SymbolFileDWARFDebugMap we rely on
5999 // having an address, we need to do some magic here
6000 // so we can get the correct address for our global
6001 // variable. The address for all of these entries
6002 // will be zero, and there will be an undefined symbol
6003 // in this object file, and the executable will have
6004 // a matching symbol with a good address. So here we
6005 // dig up the correct address and replace it in the
6006 // location for the variable, and set the variable's
6007 // symbol context scope to be that of the main executable
6008 // so the file address will resolve correctly.
Greg Clayton96c09682012-01-04 22:56:43 +00006009 if (location.LocationContains_DW_OP_addr (0, op_error))
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006010 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006011
Greg Claytond1767f02011-12-08 02:13:16 +00006012 // we have a possible uninitialized extern global
6013 Symtab *symtab = m_obj_file->GetSymtab();
6014 if (symtab)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006015 {
Greg Claytond1767f02011-12-08 02:13:16 +00006016 ConstString const_name(name);
6017 Symbol *undefined_symbol = symtab->FindFirstSymbolWithNameAndType (const_name,
6018 eSymbolTypeUndefined,
6019 Symtab::eDebugNo,
6020 Symtab::eVisibilityExtern);
6021
6022 if (undefined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006023 {
Greg Claytond1767f02011-12-08 02:13:16 +00006024 ObjectFile *debug_map_objfile = m_debug_map_symfile->GetObjectFile();
6025 if (debug_map_objfile)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006026 {
Greg Claytond1767f02011-12-08 02:13:16 +00006027 Symtab *debug_map_symtab = debug_map_objfile->GetSymtab();
6028 Symbol *defined_symbol = debug_map_symtab->FindFirstSymbolWithNameAndType (const_name,
6029 eSymbolTypeData,
6030 Symtab::eDebugYes,
6031 Symtab::eVisibilityExtern);
6032 if (defined_symbol)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006033 {
Greg Claytond1767f02011-12-08 02:13:16 +00006034 const AddressRange *defined_range = defined_symbol->GetAddressRangePtr();
6035 if (defined_range)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006036 {
Greg Claytond1767f02011-12-08 02:13:16 +00006037 const addr_t defined_addr = defined_range->GetBaseAddress().GetFileAddress();
6038 if (defined_addr != LLDB_INVALID_ADDRESS)
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006039 {
Greg Claytond1767f02011-12-08 02:13:16 +00006040 if (location.Update_DW_OP_addr (defined_addr))
6041 {
6042 symbol_context_scope = defined_symbol;
6043 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006044 }
6045 }
6046 }
6047 }
6048 }
6049 }
6050 }
6051 }
6052 }
Greg Claytond1767f02011-12-08 02:13:16 +00006053 else
6054 {
6055 scope = eValueTypeVariableStatic;
6056 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006057 }
6058 else
Greg Claytond1767f02011-12-08 02:13:16 +00006059 {
6060 scope = eValueTypeVariableLocal;
6061 }
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006062 }
Greg Clayton7f995132011-10-04 22:41:51 +00006063
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006064 if (symbol_context_scope == NULL)
Greg Clayton7f995132011-10-04 22:41:51 +00006065 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006066 switch (parent_tag)
Greg Clayton5cf58b92011-10-05 22:22:08 +00006067 {
Greg Clayton2fc93ea2011-11-13 04:15:56 +00006068 case DW_TAG_subprogram:
6069 case DW_TAG_inlined_subroutine:
6070 case DW_TAG_lexical_block:
6071 if (sc.function)
6072 {
6073 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
6074 if (symbol_context_scope == NULL)
6075 symbol_context_scope = sc.function;
6076 }
6077 break;
6078
6079 default:
6080 symbol_context_scope = sc.comp_unit;
6081 break;
Greg Clayton5cf58b92011-10-05 22:22:08 +00006082 }
Greg Clayton7f995132011-10-04 22:41:51 +00006083 }
6084
Greg Clayton5cf58b92011-10-05 22:22:08 +00006085 if (symbol_context_scope)
6086 {
Greg Clayton81c22f62011-10-19 18:09:39 +00006087 var_sp.reset (new Variable (MakeUserID(die->GetOffset()),
6088 name,
6089 mangled,
Greg Claytond1767f02011-12-08 02:13:16 +00006090 SymbolFileTypeSP (new SymbolFileType(*this, type_uid)),
Greg Clayton81c22f62011-10-19 18:09:39 +00006091 scope,
6092 symbol_context_scope,
6093 &decl,
6094 location,
6095 is_external,
6096 is_artificial));
Greg Clayton5cf58b92011-10-05 22:22:08 +00006097
6098 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
6099 }
6100 else
6101 {
6102 // Not ready to parse this variable yet. It might be a global
6103 // or static variable that is in a function scope and the function
6104 // in the symbol context wasn't filled in yet
6105 return var_sp;
6106 }
Greg Clayton7f995132011-10-04 22:41:51 +00006107 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006108 }
Greg Clayton7f995132011-10-04 22:41:51 +00006109 // Cache var_sp even if NULL (the variable was just a specification or
6110 // was missing vital information to be able to be displayed in the debugger
6111 // (missing location due to optimization, etc)) so we don't re-parse
6112 // this DIE over and over later...
6113 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006114 }
6115 return var_sp;
6116}
6117
Greg Claytonc662ec82011-06-17 22:10:16 +00006118
6119const DWARFDebugInfoEntry *
6120SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
6121 dw_offset_t spec_block_die_offset,
6122 DWARFCompileUnit **result_die_cu_handle)
6123{
6124 // Give the concrete function die specified by "func_die_offset", find the
6125 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6126 // to "spec_block_die_offset"
6127 DWARFDebugInfo* info = DebugInfo();
6128
6129 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
6130 if (die)
6131 {
6132 assert (*result_die_cu_handle);
6133 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
6134 }
6135 return NULL;
6136}
6137
6138
6139const DWARFDebugInfoEntry *
6140SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
6141 const DWARFDebugInfoEntry *die,
6142 dw_offset_t spec_block_die_offset,
6143 DWARFCompileUnit **result_die_cu_handle)
6144{
6145 if (die)
6146 {
6147 switch (die->Tag())
6148 {
6149 case DW_TAG_subprogram:
6150 case DW_TAG_inlined_subroutine:
6151 case DW_TAG_lexical_block:
6152 {
6153 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
6154 {
6155 *result_die_cu_handle = dwarf_cu;
6156 return die;
6157 }
6158
6159 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
6160 {
6161 *result_die_cu_handle = dwarf_cu;
6162 return die;
6163 }
6164 }
6165 break;
6166 }
6167
6168 // Give the concrete function die specified by "func_die_offset", find the
6169 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
6170 // to "spec_block_die_offset"
6171 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
6172 {
6173 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
6174 child_die,
6175 spec_block_die_offset,
6176 result_die_cu_handle);
6177 if (result_die)
6178 return result_die;
6179 }
6180 }
6181
6182 *result_die_cu_handle = NULL;
6183 return NULL;
6184}
6185
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006186size_t
6187SymbolFileDWARF::ParseVariables
6188(
6189 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00006190 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00006191 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006192 const DWARFDebugInfoEntry *orig_die,
6193 bool parse_siblings,
6194 bool parse_children,
6195 VariableList* cc_variable_list
6196)
6197{
6198 if (orig_die == NULL)
6199 return 0;
6200
Greg Claytonc662ec82011-06-17 22:10:16 +00006201 VariableListSP variable_list_sp;
6202
6203 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006204 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00006205 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006206 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006207 dw_tag_t tag = die->Tag();
6208
6209 // Check to see if we have already parsed this variable or constant?
6210 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006211 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006212 if (cc_variable_list)
6213 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006214 }
6215 else
6216 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006217 // We haven't already parsed it, lets do that now.
6218 if ((tag == DW_TAG_variable) ||
6219 (tag == DW_TAG_constant) ||
6220 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006221 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006222 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00006223 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006224 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
6225 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
6226 switch (parent_tag)
6227 {
6228 case DW_TAG_compile_unit:
6229 if (sc.comp_unit != NULL)
6230 {
6231 variable_list_sp = sc.comp_unit->GetVariableList(false);
6232 if (variable_list_sp.get() == NULL)
6233 {
6234 variable_list_sp.reset(new VariableList());
6235 sc.comp_unit->SetVariableList(variable_list_sp);
6236 }
6237 }
6238 else
6239 {
Greg Claytone38a5ed2012-01-05 03:57:59 +00006240 GetObjectFile()->GetModule()->ReportError ("parent 0x%8.8llx %s with no valid compile unit in symbol context for 0x%8.8llx %s.\n",
6241 MakeUserID(sc_parent_die->GetOffset()),
6242 DW_TAG_value_to_name (parent_tag),
6243 MakeUserID(orig_die->GetOffset()),
6244 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006245 }
6246 break;
6247
6248 case DW_TAG_subprogram:
6249 case DW_TAG_inlined_subroutine:
6250 case DW_TAG_lexical_block:
6251 if (sc.function != NULL)
6252 {
6253 // Check to see if we already have parsed the variables for the given scope
6254
Greg Clayton81c22f62011-10-19 18:09:39 +00006255 Block *block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(sc_parent_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006256 if (block == NULL)
6257 {
6258 // This must be a specification or abstract origin with
6259 // a concrete block couterpart in the current function. We need
6260 // to find the concrete block so we can correctly add the
6261 // variable to it
6262 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
6263 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
6264 sc_parent_die->GetOffset(),
6265 &concrete_block_die_cu);
6266 if (concrete_block_die)
Greg Clayton81c22f62011-10-19 18:09:39 +00006267 block = sc.function->GetBlock(true).FindBlockByID(MakeUserID(concrete_block_die->GetOffset()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006268 }
6269
6270 if (block != NULL)
6271 {
6272 const bool can_create = false;
6273 variable_list_sp = block->GetBlockVariableList (can_create);
6274 if (variable_list_sp.get() == NULL)
6275 {
6276 variable_list_sp.reset(new VariableList());
6277 block->SetVariableList(variable_list_sp);
6278 }
6279 }
6280 }
6281 break;
6282
6283 default:
Greg Claytone38a5ed2012-01-05 03:57:59 +00006284 GetObjectFile()->GetModule()->ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8llx %s.\n",
6285 MakeUserID(orig_die->GetOffset()),
6286 DW_TAG_value_to_name (orig_die->Tag()));
Greg Claytonc662ec82011-06-17 22:10:16 +00006287 break;
6288 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00006289 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006290
6291 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006292 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00006293 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
6294 if (var_sp)
6295 {
Greg Claytonc662ec82011-06-17 22:10:16 +00006296 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00006297 if (cc_variable_list)
6298 cc_variable_list->AddVariableIfUnique (var_sp);
6299 ++vars_added;
6300 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006301 }
6302 }
6303 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006304
6305 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
6306
6307 if (!skip_children && parse_children && die->HasChildren())
6308 {
6309 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
6310 }
6311
6312 if (parse_siblings)
6313 die = die->GetSibling();
6314 else
6315 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006316 }
Greg Claytonc662ec82011-06-17 22:10:16 +00006317 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00006318}
6319
6320//------------------------------------------------------------------
6321// PluginInterface protocol
6322//------------------------------------------------------------------
6323const char *
6324SymbolFileDWARF::GetPluginName()
6325{
6326 return "SymbolFileDWARF";
6327}
6328
6329const char *
6330SymbolFileDWARF::GetShortPluginName()
6331{
6332 return GetPluginNameStatic();
6333}
6334
6335uint32_t
6336SymbolFileDWARF::GetPluginVersion()
6337{
6338 return 1;
6339}
6340
6341void
Greg Clayton6beaaa62011-01-17 03:46:26 +00006342SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
6343{
6344 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6345 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6346 if (clang_type)
6347 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6348}
6349
6350void
6351SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
6352{
6353 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6354 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
6355 if (clang_type)
6356 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
6357}
6358
Greg Claytona2721472011-06-25 00:44:06 +00006359void
Sean Callanancc427fa2011-07-30 02:42:06 +00006360SymbolFileDWARF::DumpIndexes ()
6361{
6362 StreamFile s(stdout, false);
6363
6364 s.Printf ("DWARF index for (%s) '%s/%s':",
6365 GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
6366 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
6367 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
6368 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
6369 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
6370 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
6371 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
6372 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
6373 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
6374 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
6375 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
6376}
6377
6378void
6379SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
6380 const char *name,
6381 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
Greg Claytona2721472011-06-25 00:44:06 +00006382{
Sean Callanancc427fa2011-07-30 02:42:06 +00006383 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
Greg Claytona2721472011-06-25 00:44:06 +00006384
6385 if (iter == m_decl_ctx_to_die.end())
6386 return;
6387
Greg Claytoncb5860a2011-10-13 23:49:28 +00006388 for (DIEPointerSet::iterator pos = iter->second.begin(), end = iter->second.end(); pos != end; ++pos)
Greg Claytona2721472011-06-25 00:44:06 +00006389 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006390 const DWARFDebugInfoEntry *context_die = *pos;
6391
6392 if (!results)
6393 return;
6394
6395 DWARFDebugInfo* info = DebugInfo();
6396
6397 DIEArray die_offsets;
6398
6399 DWARFCompileUnit* dwarf_cu = NULL;
6400 const DWARFDebugInfoEntry* die = NULL;
Greg Clayton220a0072011-12-09 08:48:30 +00006401
6402 if (m_using_apple_tables)
6403 {
6404 if (m_apple_types_ap.get())
6405 m_apple_types_ap->FindByName (name, die_offsets);
6406 }
6407 else
6408 {
6409 if (!m_indexed)
6410 Index ();
6411
6412 m_type_index.Find (ConstString(name), die_offsets);
6413 }
6414
Greg Clayton220a0072011-12-09 08:48:30 +00006415 const size_t num_matches = die_offsets.size();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006416
6417 if (num_matches)
Greg Claytona2721472011-06-25 00:44:06 +00006418 {
Greg Claytoncb5860a2011-10-13 23:49:28 +00006419 for (size_t i = 0; i < num_matches; ++i)
6420 {
6421 const dw_offset_t die_offset = die_offsets[i];
6422 die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
Greg Claytond4a2b372011-09-12 23:21:58 +00006423
Greg Claytoncb5860a2011-10-13 23:49:28 +00006424 if (die->GetParent() != context_die)
6425 continue;
6426
6427 Type *matching_type = ResolveType (dwarf_cu, die);
6428
Greg Clayton42ce2f32011-12-12 21:50:19 +00006429 lldb::clang_type_t type = matching_type->GetClangForwardType();
Greg Claytoncb5860a2011-10-13 23:49:28 +00006430 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
6431
6432 if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
6433 {
6434 clang::TagDecl *tag_decl = tag_type->getDecl();
6435 results->push_back(tag_decl);
6436 }
6437 else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
6438 {
6439 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
6440 results->push_back(typedef_decl);
6441 }
Greg Claytona2721472011-06-25 00:44:06 +00006442 }
6443 }
6444 }
6445}
6446
6447void
6448SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
Greg Clayton85ae2e12011-10-18 23:36:41 +00006449 const clang::DeclContext *decl_context,
6450 clang::DeclarationName decl_name,
Greg Claytona2721472011-06-25 00:44:06 +00006451 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
6452{
Greg Clayton85ae2e12011-10-18 23:36:41 +00006453
6454 switch (decl_context->getDeclKind())
6455 {
6456 case clang::Decl::Namespace:
6457 case clang::Decl::TranslationUnit:
6458 {
6459 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6460 symbol_file_dwarf->SearchDeclContext (decl_context, decl_name.getAsString().c_str(), results);
6461 }
6462 break;
6463 default:
6464 break;
6465 }
Greg Claytona2721472011-06-25 00:44:06 +00006466}
Greg Claytoncaab74e2012-01-28 00:48:57 +00006467
6468bool
6469SymbolFileDWARF::LayoutRecordType (void *baton,
6470 const clang::RecordDecl *record_decl,
6471 uint64_t &size,
6472 uint64_t &alignment,
6473 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6474 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6475 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6476{
6477 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
6478 return symbol_file_dwarf->LayoutRecordType (record_decl, size, alignment, field_offsets, base_offsets, vbase_offsets);
6479}
6480
6481
6482bool
6483SymbolFileDWARF::LayoutRecordType (const clang::RecordDecl *record_decl,
6484 uint64_t &bit_size,
6485 uint64_t &alignment,
6486 llvm::DenseMap <const clang::FieldDecl *, uint64_t> &field_offsets,
6487 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &base_offsets,
6488 llvm::DenseMap <const clang::CXXRecordDecl *, clang::CharUnits> &vbase_offsets)
6489{
6490 LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
6491 RecordDeclToLayoutMap::iterator pos = m_record_decl_to_layout_map.find (record_decl);
6492 bool success = false;
6493 base_offsets.clear();
6494 vbase_offsets.clear();
6495 if (pos != m_record_decl_to_layout_map.end())
6496 {
6497 bit_size = pos->second.bit_size;
6498 alignment = pos->second.alignment;
6499 field_offsets.swap(pos->second.field_offsets);
6500 m_record_decl_to_layout_map.erase(pos);
6501 success = true;
6502 }
6503 else
6504 {
6505 bit_size = 0;
6506 alignment = 0;
6507 field_offsets.clear();
6508 }
6509
6510 if (log)
6511 GetObjectFile()->GetModule()->LogMessage (log.get(),
6512 "SymbolFileDWARF::LayoutRecordType (record_decl = %p, bit_size = %llu, alignment = %llu, field_offsets[%u],base_offsets[%u], vbase_offsets[%u]) success = %i",
6513 record_decl,
6514 bit_size,
6515 alignment,
6516 (uint32_t)field_offsets.size(),
6517 (uint32_t)base_offsets.size(),
6518 (uint32_t)vbase_offsets.size(),
6519 success);
6520 return success;
6521}
6522
6523
6524