blob: 9a7e7fdfc3b8acf9bd7931af07f981c6eb823c96 [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"
17#include "clang/Basic/Builtins.h"
18#include "clang/Basic/IdentifierTable.h"
19#include "clang/Basic/LangOptions.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Basic/TargetInfo.h"
22#include "clang/Basic/Specifiers.h"
Greg Clayton7fedea22010-11-16 02:10:54 +000023#include "clang/Sema/DeclSpec.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000024
25#include "lldb/Core/Module.h"
26#include "lldb/Core/PluginManager.h"
27#include "lldb/Core/RegularExpression.h"
28#include "lldb/Core/Scalar.h"
29#include "lldb/Core/Section.h"
Greg Claytonc685f8e2010-09-15 04:15:46 +000030#include "lldb/Core/StreamFile.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000031#include "lldb/Core/Timer.h"
32#include "lldb/Core/Value.h"
33
34#include "lldb/Symbol/Block.h"
Greg Clayton6beaaa62011-01-17 03:46:26 +000035#include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000036#include "lldb/Symbol/CompileUnit.h"
37#include "lldb/Symbol/LineTable.h"
38#include "lldb/Symbol/ObjectFile.h"
39#include "lldb/Symbol/SymbolVendor.h"
40#include "lldb/Symbol/VariableList.h"
41
42#include "DWARFCompileUnit.h"
43#include "DWARFDebugAbbrev.h"
44#include "DWARFDebugAranges.h"
45#include "DWARFDebugInfo.h"
46#include "DWARFDebugInfoEntry.h"
47#include "DWARFDebugLine.h"
48#include "DWARFDebugPubnames.h"
49#include "DWARFDebugRanges.h"
50#include "DWARFDIECollection.h"
51#include "DWARFFormValue.h"
52#include "DWARFLocationList.h"
53#include "LogChannelDWARF.h"
Greg Clayton450e3f32010-10-12 02:24:53 +000054#include "SymbolFileDWARFDebugMap.h"
Chris Lattner30fdc8d2010-06-08 16:52:24 +000055
56#include <map>
57
Greg Clayton62742b12010-11-11 01:09:45 +000058//#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
Greg Claytonc93237c2010-10-01 20:48:32 +000059
60#ifdef ENABLE_DEBUG_PRINTF
61#include <stdio.h>
62#define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
63#else
64#define DEBUG_PRINTF(fmt, ...)
65#endif
66
Greg Clayton594e5ed2010-09-27 21:07:38 +000067#define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000068
69using namespace lldb;
70using namespace lldb_private;
71
72
Sean Callananc7fbf732010-08-06 00:32:49 +000073static AccessType
Greg Clayton8cf05932010-07-22 18:30:50 +000074DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
Chris Lattner30fdc8d2010-06-08 16:52:24 +000075{
76 switch (dwarf_accessibility)
77 {
Sean Callananc7fbf732010-08-06 00:32:49 +000078 case DW_ACCESS_public: return eAccessPublic;
79 case DW_ACCESS_private: return eAccessPrivate;
80 case DW_ACCESS_protected: return eAccessProtected;
Greg Clayton8cf05932010-07-22 18:30:50 +000081 default: break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000082 }
Sean Callananc7fbf732010-08-06 00:32:49 +000083 return eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +000084}
85
86void
87SymbolFileDWARF::Initialize()
88{
89 LogChannelDWARF::Initialize();
90 PluginManager::RegisterPlugin (GetPluginNameStatic(),
91 GetPluginDescriptionStatic(),
92 CreateInstance);
93}
94
95void
96SymbolFileDWARF::Terminate()
97{
98 PluginManager::UnregisterPlugin (CreateInstance);
99 LogChannelDWARF::Initialize();
100}
101
102
103const char *
104SymbolFileDWARF::GetPluginNameStatic()
105{
106 return "symbol-file.dwarf2";
107}
108
109const char *
110SymbolFileDWARF::GetPluginDescriptionStatic()
111{
112 return "DWARF and DWARF3 debug symbol file reader.";
113}
114
115
116SymbolFile*
117SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
118{
119 return new SymbolFileDWARF(obj_file);
120}
121
Greg Clayton2d95dc9b2010-11-10 04:57:04 +0000122TypeList *
123SymbolFileDWARF::GetTypeList ()
124{
125 if (m_debug_map_symfile)
126 return m_debug_map_symfile->GetTypeList();
127 return m_obj_file->GetModule()->GetTypeList();
128
129}
130
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000131//----------------------------------------------------------------------
132// Gets the first parent that is a lexical block, function or inlined
133// subroutine, or compile unit.
134//----------------------------------------------------------------------
135static const DWARFDebugInfoEntry *
136GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
137{
138 const DWARFDebugInfoEntry *die;
139 for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
140 {
141 dw_tag_t tag = die->Tag();
142
143 switch (tag)
144 {
145 case DW_TAG_compile_unit:
146 case DW_TAG_subprogram:
147 case DW_TAG_inlined_subroutine:
148 case DW_TAG_lexical_block:
149 return die;
150 }
151 }
152 return NULL;
153}
154
155
Greg Clayton450e3f32010-10-12 02:24:53 +0000156SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
157 SymbolFile (objfile),
158 m_debug_map_symfile (NULL),
Greg Clayton7a345282010-11-09 23:46:37 +0000159 m_clang_tu_decl (NULL),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000160 m_flags(),
161 m_data_debug_abbrev(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000162 m_data_debug_frame(),
163 m_data_debug_info(),
164 m_data_debug_line(),
165 m_data_debug_loc(),
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000166 m_data_debug_ranges(),
167 m_data_debug_str(),
168 m_abbr(),
169 m_aranges(),
170 m_info(),
171 m_line(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000172 m_function_basename_index(),
173 m_function_fullname_index(),
174 m_function_method_index(),
175 m_function_selector_index(),
Greg Clayton450e3f32010-10-12 02:24:53 +0000176 m_objc_class_selectors_index(),
Greg Claytonc685f8e2010-09-15 04:15:46 +0000177 m_global_index(),
Greg Clayton69b04882010-10-15 02:03:22 +0000178 m_type_index(),
179 m_namespace_index(),
Greg Clayton6beaaa62011-01-17 03:46:26 +0000180 m_indexed (false),
181 m_is_external_ast_source (false),
Greg Clayton1c9e5ac2011-02-09 19:06:17 +0000182 m_ranges(),
183 m_unique_ast_type_map ()
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000184{
185}
186
187SymbolFileDWARF::~SymbolFileDWARF()
188{
Greg Clayton6beaaa62011-01-17 03:46:26 +0000189 if (m_is_external_ast_source)
190 m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
191}
192
193static const ConstString &
194GetDWARFMachOSegmentName ()
195{
196 static ConstString g_dwarf_section_name ("__DWARF");
197 return g_dwarf_section_name;
198}
199
Greg Claytone576ab22011-02-15 00:19:15 +0000200UniqueDWARFASTTypeMap &
201SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
202{
203 if (m_debug_map_symfile)
204 return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
205 return m_unique_ast_type_map;
206}
207
Greg Clayton6beaaa62011-01-17 03:46:26 +0000208ClangASTContext &
209SymbolFileDWARF::GetClangASTContext ()
210{
211 if (m_debug_map_symfile)
212 return m_debug_map_symfile->GetClangASTContext ();
213
214 ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
215 if (!m_is_external_ast_source)
216 {
217 m_is_external_ast_source = true;
218 llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
219 new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
220 SymbolFileDWARF::CompleteObjCInterfaceDecl,
Greg Claytona2721472011-06-25 00:44:06 +0000221 SymbolFileDWARF::FindExternalVisibleDeclsByName,
Greg Clayton6beaaa62011-01-17 03:46:26 +0000222 this));
223
224 ast.SetExternalSource (ast_source_ap);
225 }
226 return ast;
227}
228
229void
230SymbolFileDWARF::InitializeObject()
231{
232 // Install our external AST source callbacks so we can complete Clang types.
233 Module *module = m_obj_file->GetModule();
234 if (module)
235 {
236 const SectionList *section_list = m_obj_file->GetSectionList();
237
238 const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
239
240 // Memory map the DWARF mach-o segment so we have everything mmap'ed
241 // to keep our heap memory usage down.
242 if (section)
243 section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
244 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000245}
246
247bool
248SymbolFileDWARF::SupportedVersion(uint16_t version)
249{
250 return version == 2 || version == 3;
251}
252
253uint32_t
254SymbolFileDWARF::GetAbilities ()
255{
256 uint32_t abilities = 0;
257 if (m_obj_file != NULL)
258 {
259 const Section* section = NULL;
260 const SectionList *section_list = m_obj_file->GetSectionList();
261 if (section_list == NULL)
262 return 0;
263
264 uint64_t debug_abbrev_file_size = 0;
265 uint64_t debug_aranges_file_size = 0;
266 uint64_t debug_frame_file_size = 0;
267 uint64_t debug_info_file_size = 0;
268 uint64_t debug_line_file_size = 0;
269 uint64_t debug_loc_file_size = 0;
270 uint64_t debug_macinfo_file_size = 0;
271 uint64_t debug_pubnames_file_size = 0;
272 uint64_t debug_pubtypes_file_size = 0;
273 uint64_t debug_ranges_file_size = 0;
274 uint64_t debug_str_file_size = 0;
275
Greg Clayton6beaaa62011-01-17 03:46:26 +0000276 section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000277
278 if (section)
Greg Clayton4ceb9982010-07-21 22:54:26 +0000279 section_list = &section->GetChildren ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000280
Greg Clayton4ceb9982010-07-21 22:54:26 +0000281 section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000282 if (section != NULL)
283 {
284 debug_info_file_size = section->GetByteSize();
285
Greg Clayton4ceb9982010-07-21 22:54:26 +0000286 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000287 if (section)
288 debug_abbrev_file_size = section->GetByteSize();
289 else
290 m_flags.Set (flagsGotDebugAbbrevData);
291
Greg Clayton4ceb9982010-07-21 22:54:26 +0000292 section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000293 if (section)
294 debug_aranges_file_size = section->GetByteSize();
295 else
296 m_flags.Set (flagsGotDebugArangesData);
297
Greg Clayton4ceb9982010-07-21 22:54:26 +0000298 section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000299 if (section)
300 debug_frame_file_size = section->GetByteSize();
301 else
302 m_flags.Set (flagsGotDebugFrameData);
303
Greg Clayton4ceb9982010-07-21 22:54:26 +0000304 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000305 if (section)
306 debug_line_file_size = section->GetByteSize();
307 else
308 m_flags.Set (flagsGotDebugLineData);
309
Greg Clayton4ceb9982010-07-21 22:54:26 +0000310 section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000311 if (section)
312 debug_loc_file_size = section->GetByteSize();
313 else
314 m_flags.Set (flagsGotDebugLocData);
315
Greg Clayton4ceb9982010-07-21 22:54:26 +0000316 section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000317 if (section)
318 debug_macinfo_file_size = section->GetByteSize();
319 else
320 m_flags.Set (flagsGotDebugMacInfoData);
321
Greg Clayton4ceb9982010-07-21 22:54:26 +0000322 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000323 if (section)
324 debug_pubnames_file_size = section->GetByteSize();
325 else
326 m_flags.Set (flagsGotDebugPubNamesData);
327
Greg Clayton4ceb9982010-07-21 22:54:26 +0000328 section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000329 if (section)
330 debug_pubtypes_file_size = section->GetByteSize();
331 else
332 m_flags.Set (flagsGotDebugPubTypesData);
333
Greg Clayton4ceb9982010-07-21 22:54:26 +0000334 section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000335 if (section)
336 debug_ranges_file_size = section->GetByteSize();
337 else
338 m_flags.Set (flagsGotDebugRangesData);
339
Greg Clayton4ceb9982010-07-21 22:54:26 +0000340 section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000341 if (section)
342 debug_str_file_size = section->GetByteSize();
343 else
344 m_flags.Set (flagsGotDebugStrData);
345 }
346
347 if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
348 abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
349
350 if (debug_line_file_size > 0)
351 abilities |= LineTables;
352
353 if (debug_aranges_file_size > 0)
354 abilities |= AddressAcceleratorTable;
355
356 if (debug_pubnames_file_size > 0)
357 abilities |= FunctionAcceleratorTable;
358
359 if (debug_pubtypes_file_size > 0)
360 abilities |= TypeAcceleratorTable;
361
362 if (debug_macinfo_file_size > 0)
363 abilities |= MacroInformation;
364
365 if (debug_frame_file_size > 0)
366 abilities |= CallFrameInformation;
367 }
368 return abilities;
369}
370
371const DataExtractor&
Greg Clayton4ceb9982010-07-21 22:54:26 +0000372SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000373{
374 if (m_flags.IsClear (got_flag))
375 {
376 m_flags.Set (got_flag);
377 const SectionList *section_list = m_obj_file->GetSectionList();
378 if (section_list)
379 {
Greg Clayton4ceb9982010-07-21 22:54:26 +0000380 Section *section = section_list->FindSectionByType(sect_type, true).get();
381 if (section)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000382 {
383 // See if we memory mapped the DWARF segment?
384 if (m_dwarf_data.GetByteSize())
385 {
386 data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
387 }
388 else
389 {
390 if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
391 data.Clear();
392 }
393 }
394 }
395 }
396 return data;
397}
398
399const DataExtractor&
400SymbolFileDWARF::get_debug_abbrev_data()
401{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000402 return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000403}
404
405const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000406SymbolFileDWARF::get_debug_frame_data()
407{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000408 return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000409}
410
411const DataExtractor&
412SymbolFileDWARF::get_debug_info_data()
413{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000414 return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000415}
416
417const DataExtractor&
418SymbolFileDWARF::get_debug_line_data()
419{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000420 return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000421}
422
423const DataExtractor&
424SymbolFileDWARF::get_debug_loc_data()
425{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000426 return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000427}
428
429const DataExtractor&
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000430SymbolFileDWARF::get_debug_ranges_data()
431{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000432 return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000433}
434
435const DataExtractor&
436SymbolFileDWARF::get_debug_str_data()
437{
Greg Clayton4ceb9982010-07-21 22:54:26 +0000438 return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000439}
440
441
442DWARFDebugAbbrev*
443SymbolFileDWARF::DebugAbbrev()
444{
445 if (m_abbr.get() == NULL)
446 {
447 const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
448 if (debug_abbrev_data.GetByteSize() > 0)
449 {
450 m_abbr.reset(new DWARFDebugAbbrev());
451 if (m_abbr.get())
452 m_abbr->Parse(debug_abbrev_data);
453 }
454 }
455 return m_abbr.get();
456}
457
458const DWARFDebugAbbrev*
459SymbolFileDWARF::DebugAbbrev() const
460{
461 return m_abbr.get();
462}
463
464DWARFDebugAranges*
465SymbolFileDWARF::DebugAranges()
466{
Greg Clayton016a95e2010-09-14 02:20:48 +0000467 // It turns out that llvm-gcc doesn't generate .debug_aranges in .o files
468 // and we are already parsing all of the DWARF because the .debug_pubnames
469 // is useless (it only mentions symbols that are externally visible), so
470 // don't use the .debug_aranges section, we should be using a debug aranges
471 // we got from SymbolFileDWARF::Index().
472
473 if (!m_indexed)
474 Index();
475
476
477// if (m_aranges.get() == NULL)
478// {
479// Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
480// m_aranges.reset(new DWARFDebugAranges());
481// if (m_aranges.get())
482// {
483// const DataExtractor &debug_aranges_data = get_debug_aranges_data();
484// if (debug_aranges_data.GetByteSize() > 0)
485// m_aranges->Extract(debug_aranges_data);
486// else
487// m_aranges->Generate(this);
488// }
489// }
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000490 return m_aranges.get();
491}
492
493const DWARFDebugAranges*
494SymbolFileDWARF::DebugAranges() const
495{
496 return m_aranges.get();
497}
498
499
500DWARFDebugInfo*
501SymbolFileDWARF::DebugInfo()
502{
503 if (m_info.get() == NULL)
504 {
505 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
506 if (get_debug_info_data().GetByteSize() > 0)
507 {
508 m_info.reset(new DWARFDebugInfo());
509 if (m_info.get())
510 {
511 m_info->SetDwarfData(this);
512 }
513 }
514 }
515 return m_info.get();
516}
517
518const DWARFDebugInfo*
519SymbolFileDWARF::DebugInfo() const
520{
521 return m_info.get();
522}
523
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000524DWARFCompileUnit*
525SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
526{
527 DWARFDebugInfo* info = DebugInfo();
528 if (info)
529 return info->GetCompileUnit(cu_uid).get();
530 return NULL;
531}
532
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000533
534DWARFDebugRanges*
535SymbolFileDWARF::DebugRanges()
536{
537 if (m_ranges.get() == NULL)
538 {
539 Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
540 if (get_debug_ranges_data().GetByteSize() > 0)
541 {
542 m_ranges.reset(new DWARFDebugRanges());
543 if (m_ranges.get())
544 m_ranges->Extract(this);
545 }
546 }
547 return m_ranges.get();
548}
549
550const DWARFDebugRanges*
551SymbolFileDWARF::DebugRanges() const
552{
553 return m_ranges.get();
554}
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000555
556bool
Greg Clayton96d7d742010-11-10 23:42:09 +0000557SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000558{
Greg Clayton96d7d742010-11-10 23:42:09 +0000559 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000560 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000561 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000562 if (cu_die)
563 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000564 const char * cu_die_name = cu_die->GetName(this, curr_cu);
565 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
566 LanguageType class_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000567 if (cu_die_name)
568 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000569 FileSpec cu_file_spec;
570
Greg Clayton7bd65b92011-02-09 23:39:34 +0000571 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000572 {
Jim Ingham0909e5f2010-09-16 00:57:33 +0000573 // If we have a full path to the compile unit, we don't need to resolve
574 // the file. This can be expensive e.g. when the source files are NFS mounted.
575 cu_file_spec.SetFile (cu_die_name, false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000576 }
577 else
578 {
579 std::string fullpath(cu_comp_dir);
580 if (*fullpath.rbegin() != '/')
581 fullpath += '/';
582 fullpath += cu_die_name;
Jim Ingham0909e5f2010-09-16 00:57:33 +0000583 cu_file_spec.SetFile (fullpath.c_str(), false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000584 }
585
Greg Clayton96d7d742010-11-10 23:42:09 +0000586 compile_unit_sp.reset(new CompileUnit(m_obj_file->GetModule(), curr_cu, cu_file_spec, curr_cu->GetOffset(), class_language));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000587 if (compile_unit_sp.get())
588 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000589 curr_cu->SetUserData(compile_unit_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000590 return true;
591 }
592 }
593 }
594 }
595 return false;
596}
597
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000598uint32_t
599SymbolFileDWARF::GetNumCompileUnits()
600{
601 DWARFDebugInfo* info = DebugInfo();
602 if (info)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000603 return info->GetNumCompileUnits();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000604 return 0;
605}
606
607CompUnitSP
608SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
609{
610 CompUnitSP comp_unit;
611 DWARFDebugInfo* info = DebugInfo();
612 if (info)
613 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000614 DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
615 if (curr_cu != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000616 {
617 // Our symbol vendor shouldn't be asking us to add a compile unit that
618 // has already been added to it, which this DWARF plug-in knows as it
619 // stores the lldb compile unit (CompileUnit) pointer in each
620 // DWARFCompileUnit object when it gets added.
Greg Clayton96d7d742010-11-10 23:42:09 +0000621 assert(curr_cu->GetUserData() == NULL);
622 ParseCompileUnit(curr_cu, comp_unit);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000623 }
624 }
625 return comp_unit;
626}
627
628static void
629AddRangesToBlock
630(
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000631 Block& block,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000632 DWARFDebugRanges::RangeList& ranges,
633 addr_t block_base_addr
634)
635{
636 ranges.SubtractOffset (block_base_addr);
637 size_t range_idx = 0;
638 const DWARFDebugRanges::Range *debug_range;
639 for (range_idx = 0; (debug_range = ranges.RangeAtIndex(range_idx)) != NULL; range_idx++)
640 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000641 block.AddRange(debug_range->begin_offset, debug_range->end_offset);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000642 }
643}
644
645
646Function *
Greg Clayton0fffff52010-09-24 05:15:53 +0000647SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000648{
649 DWARFDebugRanges::RangeList func_ranges;
650 const char *name = NULL;
651 const char *mangled = NULL;
652 int decl_file = 0;
653 int decl_line = 0;
654 int decl_column = 0;
655 int call_file = 0;
656 int call_line = 0;
657 int call_column = 0;
658 DWARFExpression frame_base;
659
Greg Claytonc93237c2010-10-01 20:48:32 +0000660 assert (die->Tag() == DW_TAG_subprogram);
661
662 if (die->Tag() != DW_TAG_subprogram)
663 return NULL;
664
665 const DWARFDebugInfoEntry *parent_die = die->GetParent();
666 switch (parent_die->Tag())
667 {
668 case DW_TAG_structure_type:
669 case DW_TAG_class_type:
670 // We have methods of a class or struct
671 {
672 Type *class_type = ResolveType (dwarf_cu, parent_die);
673 if (class_type)
Greg Claytonf4ecaa52011-02-16 23:00:21 +0000674 class_type->GetClangFullType();
Greg Claytonc93237c2010-10-01 20:48:32 +0000675 }
676 break;
677
678 default:
679 // Parse the function prototype as a type that can then be added to concrete function instance
680 ParseTypes (sc, dwarf_cu, die, false, false);
681 break;
682 }
683
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000684 //FixupTypes();
685
686 if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base))
687 {
688 // Union of all ranges in the function DIE (if the function is discontiguous)
689 AddressRange func_range;
690 lldb::addr_t lowest_func_addr = func_ranges.LowestAddress(0);
691 lldb::addr_t highest_func_addr = func_ranges.HighestAddress(0);
692 if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
693 {
694 func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
695 if (func_range.GetBaseAddress().IsValid())
696 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
697 }
698
699 if (func_range.GetBaseAddress().IsValid())
700 {
701 Mangled func_name;
702 if (mangled)
703 func_name.SetValue(mangled, true);
704 else if (name)
705 func_name.SetValue(name, false);
706
707 FunctionSP func_sp;
708 std::auto_ptr<Declaration> decl_ap;
709 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Greg Claytond7e05462010-11-14 00:22:48 +0000710 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
711 decl_line,
712 decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000713
Greg Clayton594e5ed2010-09-27 21:07:38 +0000714 Type *func_type = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000715
716 assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
717
718 func_range.GetBaseAddress().ResolveLinkedAddress();
719
720 func_sp.reset(new Function (sc.comp_unit,
721 die->GetOffset(), // UserID is the DIE offset
722 die->GetOffset(),
723 func_name,
724 func_type,
725 func_range)); // first address range
726
727 if (func_sp.get() != NULL)
728 {
729 func_sp->GetFrameBaseExpression() = frame_base;
730 sc.comp_unit->AddFunction(func_sp);
731 return func_sp.get();
732 }
733 }
734 }
735 return NULL;
736}
737
738size_t
739SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
740{
741 assert (sc.comp_unit);
742 size_t functions_added = 0;
Greg Clayton0fffff52010-09-24 05:15:53 +0000743 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000744 if (dwarf_cu)
745 {
746 DWARFDIECollection function_dies;
747 const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
748 size_t func_idx;
749 for (func_idx = 0; func_idx < num_funtions; ++func_idx)
750 {
751 const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
752 if (sc.comp_unit->FindFunctionByUID (die->GetOffset()).get() == NULL)
753 {
754 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
755 ++functions_added;
756 }
757 }
758 //FixupTypes();
759 }
760 return functions_added;
761}
762
763bool
764SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
765{
766 assert (sc.comp_unit);
Greg Clayton96d7d742010-11-10 23:42:09 +0000767 DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
768 assert (curr_cu);
769 const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000770
771 if (cu_die)
772 {
Greg Clayton96d7d742010-11-10 23:42:09 +0000773 const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
774 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 +0000775
776 // All file indexes in DWARF are one based and a file of index zero is
777 // supposed to be the compile unit itself.
778 support_files.Append (*sc.comp_unit);
779
780 return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
781 }
782 return false;
783}
784
785struct ParseDWARFLineTableCallbackInfo
786{
787 LineTable* line_table;
788 const SectionList *section_list;
789 lldb::addr_t prev_sect_file_base_addr;
790 lldb::addr_t curr_sect_file_base_addr;
791 bool is_oso_for_debug_map;
792 bool prev_in_final_executable;
793 DWARFDebugLine::Row prev_row;
794 SectionSP prev_section_sp;
795 SectionSP curr_section_sp;
796};
797
798//----------------------------------------------------------------------
799// ParseStatementTableCallback
800//----------------------------------------------------------------------
801static void
802ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
803{
804 LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
805 if (state.row == DWARFDebugLine::State::StartParsingLineTable)
806 {
807 // Just started parsing the line table
808 }
809 else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
810 {
811 // Done parsing line table, nothing to do for the cleanup
812 }
813 else
814 {
815 ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
816 // We have a new row, lets append it
817
818 if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
819 {
820 info->prev_section_sp = info->curr_section_sp;
821 info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
822 // If this is an end sequence entry, then we subtract one from the
823 // address to make sure we get an address that is not the end of
824 // a section.
825 if (state.end_sequence && state.address != 0)
826 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
827 else
828 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
829
830 if (info->curr_section_sp.get())
831 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
832 else
833 info->curr_sect_file_base_addr = 0;
834 }
835 if (info->curr_section_sp.get())
836 {
837 lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
838 // Check for the fancy section magic to determine if we
839
840 if (info->is_oso_for_debug_map)
841 {
842 // When this is a debug map object file that contains DWARF
843 // (referenced from an N_OSO debug map nlist entry) we will have
844 // a file address in the file range for our section from the
845 // original .o file, and a load address in the executable that
846 // contains the debug map.
847 //
848 // If the sections for the file range and load range are
849 // different, we have a remapped section for the function and
850 // this address is resolved. If they are the same, then the
851 // function for this address didn't make it into the final
852 // executable.
853 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
854
855 // If we are doing DWARF with debug map, then we need to carefully
856 // add each line table entry as there may be gaps as functions
857 // get moved around or removed.
858 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
859 {
860 if (info->prev_in_final_executable)
861 {
862 bool terminate_previous_entry = false;
863 if (!curr_in_final_executable)
864 {
865 // Check for the case where the previous line entry
866 // in a function made it into the final executable,
867 // yet the current line entry falls in a function
868 // that didn't. The line table used to be contiguous
869 // through this address range but now it isn't. We
870 // need to terminate the previous line entry so
871 // that we can reconstruct the line range correctly
872 // for it and to keep the line table correct.
873 terminate_previous_entry = true;
874 }
875 else if (info->curr_section_sp.get() != info->prev_section_sp.get())
876 {
877 // Check for cases where the line entries used to be
878 // contiguous address ranges, but now they aren't.
879 // This can happen when order files specify the
880 // ordering of the functions.
881 lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
882 Section *curr_sect = info->curr_section_sp.get();
883 Section *prev_sect = info->prev_section_sp.get();
884 assert (curr_sect->GetLinkedSection());
885 assert (prev_sect->GetLinkedSection());
886 lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
887 lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
888 lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
889 lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
890 if (object_file_addr_delta != linked_file_addr_delta)
891 terminate_previous_entry = true;
892 }
893
894 if (terminate_previous_entry)
895 {
896 line_table->InsertLineEntry (info->prev_section_sp,
897 state.address - info->prev_sect_file_base_addr,
898 info->prev_row.line,
899 info->prev_row.column,
900 info->prev_row.file,
901 false, // is_stmt
902 false, // basic_block
903 false, // state.prologue_end
904 false, // state.epilogue_begin
905 true); // end_sequence);
906 }
907 }
908 }
909
910 if (curr_in_final_executable)
911 {
912 line_table->InsertLineEntry (info->curr_section_sp,
913 curr_line_section_offset,
914 state.line,
915 state.column,
916 state.file,
917 state.is_stmt,
918 state.basic_block,
919 state.prologue_end,
920 state.epilogue_begin,
921 state.end_sequence);
922 info->prev_section_sp = info->curr_section_sp;
923 }
924 else
925 {
926 // If the current address didn't make it into the final
927 // executable, the current section will be the __text
928 // segment in the .o file, so we need to clear this so
929 // we can catch the next function that did make it into
930 // the final executable.
931 info->prev_section_sp.reset();
932 info->curr_section_sp.reset();
933 }
934
935 info->prev_in_final_executable = curr_in_final_executable;
936 }
937 else
938 {
939 // We are not in an object file that contains DWARF for an
940 // N_OSO, this is just a normal DWARF file. The DWARF spec
941 // guarantees that the addresses will be in increasing order
942 // so, since we store line tables in file address order, we
943 // can always just append the line entry without needing to
944 // search for the correct insertion point (we don't need to
945 // use LineEntry::InsertLineEntry()).
946 line_table->AppendLineEntry (info->curr_section_sp,
947 curr_line_section_offset,
948 state.line,
949 state.column,
950 state.file,
951 state.is_stmt,
952 state.basic_block,
953 state.prologue_end,
954 state.epilogue_begin,
955 state.end_sequence);
956 }
957 }
958
959 info->prev_row = state;
960 }
961}
962
963bool
964SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
965{
966 assert (sc.comp_unit);
967 if (sc.comp_unit->GetLineTable() != NULL)
968 return true;
969
970 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
971 if (dwarf_cu)
972 {
973 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
974 const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
975 if (cu_line_offset != DW_INVALID_OFFSET)
976 {
977 std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
978 if (line_table_ap.get())
979 {
Greg Clayton450e3f32010-10-12 02:24:53 +0000980 ParseDWARFLineTableCallbackInfo info = { line_table_ap.get(), m_obj_file->GetSectionList(), 0, 0, m_debug_map_symfile != NULL, false};
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000981 uint32_t offset = cu_line_offset;
982 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
983 sc.comp_unit->SetLineTable(line_table_ap.release());
984 return true;
985 }
986 }
987 }
988 return false;
989}
990
991size_t
992SymbolFileDWARF::ParseFunctionBlocks
993(
994 const SymbolContext& sc,
Greg Clayton0b76a2c2010-08-21 02:22:51 +0000995 Block *parent_block,
Greg Clayton0fffff52010-09-24 05:15:53 +0000996 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +0000997 const DWARFDebugInfoEntry *die,
998 addr_t subprogram_low_pc,
999 bool parse_siblings,
1000 bool parse_children
1001)
1002{
1003 size_t blocks_added = 0;
1004 while (die != NULL)
1005 {
1006 dw_tag_t tag = die->Tag();
1007
1008 switch (tag)
1009 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001010 case DW_TAG_inlined_subroutine:
Jim Inghamb0be4422010-08-12 01:20:14 +00001011 case DW_TAG_subprogram:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001012 case DW_TAG_lexical_block:
1013 {
1014 DWARFDebugRanges::RangeList ranges;
1015 const char *name = NULL;
1016 const char *mangled_name = NULL;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001017 Block *block = NULL;
1018 if (tag != DW_TAG_subprogram)
1019 {
1020 BlockSP block_sp(new Block (die->GetOffset()));
1021 parent_block->AddChild(block_sp);
1022 block = block_sp.get();
1023 }
1024 else
1025 {
1026 block = parent_block;
1027 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001028
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001029 int decl_file = 0;
1030 int decl_line = 0;
1031 int decl_column = 0;
1032 int call_file = 0;
1033 int call_line = 0;
1034 int call_column = 0;
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001035 if (die->GetDIENamesAndRanges (this,
1036 dwarf_cu,
1037 name,
1038 mangled_name,
1039 ranges,
1040 decl_file, decl_line, decl_column,
1041 call_file, call_line, call_column))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001042 {
1043 if (tag == DW_TAG_subprogram)
1044 {
1045 assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
1046 subprogram_low_pc = ranges.LowestAddress(0);
1047 }
Jim Inghamb0be4422010-08-12 01:20:14 +00001048 else if (tag == DW_TAG_inlined_subroutine)
1049 {
1050 // We get called here for inlined subroutines in two ways.
1051 // The first time is when we are making the Function object
1052 // for this inlined concrete instance. Since we're creating a top level block at
1053 // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS. So we need to
1054 // adjust the containing address.
1055 // The second time is when we are parsing the blocks inside the function that contains
1056 // the inlined concrete instance. Since these will be blocks inside the containing "real"
1057 // function the offset will be for that function.
1058 if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1059 {
1060 subprogram_low_pc = ranges.LowestAddress(0);
1061 }
1062 }
1063
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001064 AddRangesToBlock (*block, ranges, subprogram_low_pc);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001065
1066 if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1067 {
1068 std::auto_ptr<Declaration> decl_ap;
1069 if (decl_file != 0 || decl_line != 0 || decl_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001070 decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1071 decl_line, decl_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001072
1073 std::auto_ptr<Declaration> call_ap;
1074 if (call_file != 0 || call_line != 0 || call_column != 0)
Jim Inghamb0be4422010-08-12 01:20:14 +00001075 call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1076 call_line, call_column));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001077
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001078 block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001079 }
1080
1081 ++blocks_added;
1082
1083 if (parse_children && die->HasChildren())
1084 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001085 blocks_added += ParseFunctionBlocks (sc,
1086 block,
1087 dwarf_cu,
1088 die->GetFirstChild(),
1089 subprogram_low_pc,
1090 true,
1091 true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001092 }
1093 }
1094 }
1095 break;
1096 default:
1097 break;
1098 }
1099
1100 if (parse_siblings)
1101 die = die->GetSibling();
1102 else
1103 die = NULL;
1104 }
1105 return blocks_added;
1106}
1107
1108size_t
1109SymbolFileDWARF::ParseChildMembers
1110(
1111 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00001112 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001113 const DWARFDebugInfoEntry *parent_die,
Greg Clayton1be10fc2010-09-29 01:12:09 +00001114 clang_type_t class_clang_type,
Greg Clayton9e409562010-07-28 02:04:09 +00001115 const LanguageType class_language,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001116 std::vector<clang::CXXBaseSpecifier *>& base_classes,
1117 std::vector<int>& member_accessibilities,
Greg Claytonc93237c2010-10-01 20:48:32 +00001118 DWARFDIECollection& member_function_dies,
Sean Callananc7fbf732010-08-06 00:32:49 +00001119 AccessType& default_accessibility,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001120 bool &is_a_class
1121)
1122{
1123 if (parent_die == NULL)
1124 return 0;
1125
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001126 size_t count = 0;
1127 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00001128 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Greg Clayton6beaaa62011-01-17 03:46:26 +00001129 uint32_t member_idx = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00001130
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001131 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1132 {
1133 dw_tag_t tag = die->Tag();
1134
1135 switch (tag)
1136 {
1137 case DW_TAG_member:
1138 {
1139 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001140 const size_t num_attributes = die->GetAttributes (this,
1141 dwarf_cu,
1142 fixed_form_sizes,
1143 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001144 if (num_attributes > 0)
1145 {
1146 Declaration decl;
Greg Clayton73b472d2010-10-27 03:32:59 +00001147 //DWARFExpression location;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001148 const char *name = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00001149 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001150 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001151 AccessType accessibility = eAccessNone;
Greg Clayton73b472d2010-10-27 03:32:59 +00001152 //off_t member_offset = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001153 size_t byte_size = 0;
1154 size_t bit_offset = 0;
1155 size_t bit_size = 0;
1156 uint32_t i;
Greg Clayton24739922010-10-13 03:15:28 +00001157 for (i=0; i<num_attributes && !is_artificial; ++i)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001158 {
1159 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1160 DWARFFormValue form_value;
1161 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1162 {
1163 switch (attr)
1164 {
1165 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1166 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1167 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1168 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
1169 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1170 case DW_AT_bit_offset: bit_offset = form_value.Unsigned(); break;
1171 case DW_AT_bit_size: bit_size = form_value.Unsigned(); break;
1172 case DW_AT_byte_size: byte_size = form_value.Unsigned(); break;
1173 case DW_AT_data_member_location:
Greg Clayton73b472d2010-10-27 03:32:59 +00001174// if (form_value.BlockData())
1175// {
1176// Value initialValue(0);
1177// Value memberOffset(0);
1178// const DataExtractor& debug_info_data = get_debug_info_data();
1179// uint32_t block_length = form_value.Unsigned();
1180// uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1181// if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL))
1182// {
1183// member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1184// }
1185// }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001186 break;
1187
Greg Clayton8cf05932010-07-22 18:30:50 +00001188 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
Greg Clayton24739922010-10-13 03:15:28 +00001189 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001190 case DW_AT_declaration:
1191 case DW_AT_description:
1192 case DW_AT_mutable:
1193 case DW_AT_visibility:
1194 default:
1195 case DW_AT_sibling:
1196 break;
1197 }
1198 }
1199 }
Sean Callanan5a477cf2010-10-30 01:56:10 +00001200
1201 // FIXME: Make Clang ignore Objective-C accessibility for expressions
1202
1203 if (class_language == eLanguageTypeObjC ||
1204 class_language == eLanguageTypeObjC_plus_plus)
1205 accessibility = eAccessNone;
Greg Clayton6beaaa62011-01-17 03:46:26 +00001206
1207 if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1208 {
1209 // Not all compilers will mark the vtable pointer
1210 // member as artificial (llvm-gcc). We can't have
1211 // the virtual members in our classes otherwise it
1212 // throws off all child offsets since we end up
1213 // having and extra pointer sized member in our
1214 // class layouts.
1215 is_artificial = true;
1216 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001217
Greg Clayton24739922010-10-13 03:15:28 +00001218 if (is_artificial == false)
1219 {
1220 Type *member_type = ResolveTypeUID(encoding_uid);
1221 assert(member_type);
1222 if (accessibility == eAccessNone)
1223 accessibility = default_accessibility;
1224 member_accessibilities.push_back(accessibility);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001225
Greg Claytonba2d22d2010-11-13 22:57:37 +00001226 GetClangASTContext().AddFieldToRecordType (class_clang_type,
1227 name,
1228 member_type->GetClangLayoutType(),
1229 accessibility,
1230 bit_size);
Greg Clayton24739922010-10-13 03:15:28 +00001231 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001232 }
Greg Clayton6beaaa62011-01-17 03:46:26 +00001233 ++member_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001234 }
1235 break;
1236
1237 case DW_TAG_subprogram:
Greg Claytonc93237c2010-10-01 20:48:32 +00001238 // Let the type parsing code handle this one for us.
1239 member_function_dies.Append (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001240 break;
1241
1242 case DW_TAG_inheritance:
1243 {
1244 is_a_class = true;
Sean Callananc7fbf732010-08-06 00:32:49 +00001245 if (default_accessibility == eAccessNone)
1246 default_accessibility = eAccessPrivate;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001247 // TODO: implement DW_TAG_inheritance type parsing
1248 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytonba2d22d2010-11-13 22:57:37 +00001249 const size_t num_attributes = die->GetAttributes (this,
1250 dwarf_cu,
1251 fixed_form_sizes,
1252 attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001253 if (num_attributes > 0)
1254 {
1255 Declaration decl;
1256 DWARFExpression location;
1257 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
Sean Callananc7fbf732010-08-06 00:32:49 +00001258 AccessType accessibility = default_accessibility;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001259 bool is_virtual = false;
1260 bool is_base_of_class = true;
1261 off_t member_offset = 0;
1262 uint32_t i;
1263 for (i=0; i<num_attributes; ++i)
1264 {
1265 const dw_attr_t attr = attributes.AttributeAtIndex(i);
1266 DWARFFormValue form_value;
1267 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1268 {
1269 switch (attr)
1270 {
1271 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1272 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
1273 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1274 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
1275 case DW_AT_data_member_location:
1276 if (form_value.BlockData())
1277 {
1278 Value initialValue(0);
1279 Value memberOffset(0);
1280 const DataExtractor& debug_info_data = get_debug_info_data();
1281 uint32_t block_length = form_value.Unsigned();
1282 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
Greg Claytonba2d22d2010-11-13 22:57:37 +00001283 if (DWARFExpression::Evaluate (NULL,
1284 NULL,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001285 NULL,
1286 NULL,
Jason Molenda2d107dd2010-11-20 01:28:30 +00001287 NULL,
Greg Clayton1a65ae12011-01-25 23:55:37 +00001288 debug_info_data,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001289 block_offset,
1290 block_length,
1291 eRegisterKindDWARF,
1292 &initialValue,
1293 memberOffset,
1294 NULL))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001295 {
1296 member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1297 }
1298 }
1299 break;
1300
1301 case DW_AT_accessibility:
Greg Clayton8cf05932010-07-22 18:30:50 +00001302 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001303 break;
1304
1305 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1306 default:
1307 case DW_AT_sibling:
1308 break;
1309 }
1310 }
1311 }
1312
Greg Clayton526e5af2010-11-13 03:52:47 +00001313 Type *base_class_type = ResolveTypeUID(encoding_uid);
1314 assert(base_class_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001315
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001316 clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1317 assert (base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001318 if (class_language == eLanguageTypeObjC)
1319 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001320 GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
Greg Clayton9e409562010-07-28 02:04:09 +00001321 }
1322 else
1323 {
Greg Claytonf4ecaa52011-02-16 23:00:21 +00001324 base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
Greg Claytonba2d22d2010-11-13 22:57:37 +00001325 accessibility,
1326 is_virtual,
1327 is_base_of_class));
Greg Clayton9e409562010-07-28 02:04:09 +00001328 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001329 }
1330 }
1331 break;
1332
1333 default:
1334 break;
1335 }
1336 }
1337 return count;
1338}
1339
1340
1341clang::DeclContext*
1342SymbolFileDWARF::GetClangDeclContextForTypeUID (lldb::user_id_t type_uid)
1343{
1344 DWARFDebugInfo* debug_info = DebugInfo();
1345 if (debug_info)
1346 {
1347 DWARFCompileUnitSP cu_sp;
1348 const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1349 if (die)
1350 return GetClangDeclContextForDIE (cu_sp.get(), die);
1351 }
1352 return NULL;
1353}
1354
1355Type*
Greg Claytonc685f8e2010-09-15 04:15:46 +00001356SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001357{
1358 DWARFDebugInfo* debug_info = DebugInfo();
1359 if (debug_info)
1360 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00001361 DWARFCompileUnitSP cu_sp;
1362 const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001363 if (type_die != NULL)
Greg Claytonca512b32011-01-14 04:54:56 +00001364 {
1365 // We might be coming in in the middle of a type tree (a class
1366 // withing a class, an enum within a class), so parse any needed
1367 // parent DIEs before we get to this one...
1368 const DWARFDebugInfoEntry* parent_die = type_die->GetParent();
1369 switch (parent_die->Tag())
1370 {
1371 case DW_TAG_structure_type:
1372 case DW_TAG_union_type:
1373 case DW_TAG_class_type:
1374 ResolveType(cu_sp.get(), parent_die);
1375 break;
1376 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00001377 return ResolveType (cu_sp.get(), type_die);
Greg Claytonca512b32011-01-14 04:54:56 +00001378 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001379 }
1380 return NULL;
1381}
1382
Greg Clayton6beaaa62011-01-17 03:46:26 +00001383// This function is used when SymbolFileDWARFDebugMap owns a bunch of
1384// SymbolFileDWARF objects to detect if this DWARF file is the one that
1385// can resolve a clang_type.
1386bool
1387SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1388{
1389 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1390 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1391 return die != NULL;
1392}
1393
1394
Greg Clayton1be10fc2010-09-29 01:12:09 +00001395lldb::clang_type_t
1396SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1397{
1398 // We have a struct/union/class/enum that needs to be fully resolved.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001399 clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1400 const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001401 if (die == NULL)
Greg Clayton73b472d2010-10-27 03:32:59 +00001402 {
Greg Clayton6beaaa62011-01-17 03:46:26 +00001403// if (m_debug_map_symfile)
1404// {
1405// Type *type = m_die_to_type[die];
1406// if (type && type->GetSymbolFile() != this)
1407// return type->GetClangType();
1408// }
Greg Clayton73b472d2010-10-27 03:32:59 +00001409 // We have already resolved this type...
1410 return clang_type;
1411 }
1412 // Once we start resolving this type, remove it from the forward declaration
1413 // map in case anyone child members or other types require this type to get resolved.
1414 // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1415 // are done.
Greg Clayton6beaaa62011-01-17 03:46:26 +00001416 m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
Greg Clayton73b472d2010-10-27 03:32:59 +00001417
Greg Clayton1be10fc2010-09-29 01:12:09 +00001418
Greg Clayton450e3f32010-10-12 02:24:53 +00001419 DWARFDebugInfo* debug_info = DebugInfo();
1420
Greg Clayton96d7d742010-11-10 23:42:09 +00001421 DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001422 Type *type = m_die_to_type.lookup (die);
1423
1424 const dw_tag_t tag = die->Tag();
1425
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001426 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") - resolve forward declaration...\n",
1427 die->GetOffset(),
1428 DW_TAG_value_to_name(tag),
1429 type->GetName().AsCString());
Greg Clayton1be10fc2010-09-29 01:12:09 +00001430 assert (clang_type);
1431 DWARFDebugInfoEntry::Attributes attributes;
1432
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001433 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00001434
1435 switch (tag)
1436 {
1437 case DW_TAG_structure_type:
1438 case DW_TAG_union_type:
1439 case DW_TAG_class_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001440 ast.StartTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001441 if (die->HasChildren())
1442 {
1443 LanguageType class_language = eLanguageTypeUnknown;
Greg Clayton450e3f32010-10-12 02:24:53 +00001444 bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1445 if (is_objc_class)
Greg Claytonc93237c2010-10-01 20:48:32 +00001446 class_language = eLanguageTypeObjC;
1447
1448 int tag_decl_kind = -1;
1449 AccessType default_accessibility = eAccessNone;
1450 if (tag == DW_TAG_structure_type)
Greg Clayton1be10fc2010-09-29 01:12:09 +00001451 {
Greg Claytonc93237c2010-10-01 20:48:32 +00001452 tag_decl_kind = clang::TTK_Struct;
1453 default_accessibility = eAccessPublic;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001454 }
Greg Claytonc93237c2010-10-01 20:48:32 +00001455 else if (tag == DW_TAG_union_type)
1456 {
1457 tag_decl_kind = clang::TTK_Union;
1458 default_accessibility = eAccessPublic;
1459 }
1460 else if (tag == DW_TAG_class_type)
1461 {
1462 tag_decl_kind = clang::TTK_Class;
1463 default_accessibility = eAccessPrivate;
1464 }
1465
Greg Clayton96d7d742010-11-10 23:42:09 +00001466 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
Greg Claytonc93237c2010-10-01 20:48:32 +00001467 std::vector<clang::CXXBaseSpecifier *> base_classes;
1468 std::vector<int> member_accessibilities;
1469 bool is_a_class = false;
1470 // Parse members and base classes first
1471 DWARFDIECollection member_function_dies;
1472
1473 ParseChildMembers (sc,
Greg Clayton96d7d742010-11-10 23:42:09 +00001474 curr_cu,
Greg Claytonc93237c2010-10-01 20:48:32 +00001475 die,
1476 clang_type,
1477 class_language,
1478 base_classes,
1479 member_accessibilities,
1480 member_function_dies,
1481 default_accessibility,
1482 is_a_class);
1483
1484 // Now parse any methods if there were any...
1485 size_t num_functions = member_function_dies.Size();
1486 if (num_functions > 0)
1487 {
1488 for (size_t i=0; i<num_functions; ++i)
1489 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001490 ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
Greg Claytonc93237c2010-10-01 20:48:32 +00001491 }
1492 }
1493
Greg Clayton450e3f32010-10-12 02:24:53 +00001494 if (class_language == eLanguageTypeObjC)
1495 {
1496 std::string class_str (ClangASTContext::GetTypeName (clang_type));
1497 if (!class_str.empty())
1498 {
1499
1500 ConstString class_name (class_str.c_str());
1501 std::vector<NameToDIE::Info> method_die_infos;
1502 if (m_objc_class_selectors_index.Find (class_name, method_die_infos))
1503 {
1504 DWARFCompileUnit* method_cu = NULL;
1505 DWARFCompileUnit* prev_method_cu = NULL;
1506 const size_t num_objc_methods = method_die_infos.size();
1507 for (size_t i=0;i<num_objc_methods; ++i, prev_method_cu = method_cu)
1508 {
1509 method_cu = debug_info->GetCompileUnitAtIndex(method_die_infos[i].cu_idx);
1510
1511 if (method_cu != prev_method_cu)
1512 method_cu->ExtractDIEsIfNeeded (false);
1513
1514 DWARFDebugInfoEntry *method_die = method_cu->GetDIEAtIndexUnchecked(method_die_infos[i].die_idx);
1515
1516 ResolveType (method_cu, method_die);
1517 }
1518 }
1519 }
1520 }
1521
Greg Claytonc93237c2010-10-01 20:48:32 +00001522 // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
1523 // need to tell the clang type it is actually a class.
1524 if (class_language != eLanguageTypeObjC)
1525 {
1526 if (is_a_class && tag_decl_kind != clang::TTK_Class)
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001527 ast.SetTagTypeKind (clang_type, clang::TTK_Class);
Greg Claytonc93237c2010-10-01 20:48:32 +00001528 }
1529
1530 // Since DW_TAG_structure_type gets used for both classes
1531 // and structures, we may need to set any DW_TAG_member
1532 // fields to have a "private" access if none was specified.
1533 // When we parsed the child members we tracked that actual
1534 // accessibility value for each DW_TAG_member in the
1535 // "member_accessibilities" array. If the value for the
1536 // member is zero, then it was set to the "default_accessibility"
1537 // which for structs was "public". Below we correct this
1538 // by setting any fields to "private" that weren't correctly
1539 // set.
1540 if (is_a_class && !member_accessibilities.empty())
1541 {
1542 // This is a class and all members that didn't have
1543 // their access specified are private.
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001544 ast.SetDefaultAccessForRecordFields (clang_type,
1545 eAccessPrivate,
1546 &member_accessibilities.front(),
1547 member_accessibilities.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001548 }
1549
1550 if (!base_classes.empty())
1551 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001552 ast.SetBaseClassesForClassType (clang_type,
1553 &base_classes.front(),
1554 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001555
1556 // Clang will copy each CXXBaseSpecifier in "base_classes"
1557 // so we have to free them all.
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001558 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
1559 base_classes.size());
Greg Claytonc93237c2010-10-01 20:48:32 +00001560 }
1561
1562 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001563 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Claytonc93237c2010-10-01 20:48:32 +00001564 return clang_type;
Greg Clayton1be10fc2010-09-29 01:12:09 +00001565
1566 case DW_TAG_enumeration_type:
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001567 ast.StartTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001568 if (die->HasChildren())
1569 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001570 SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1571 ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001572 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00001573 ast.CompleteTagDeclarationDefinition (clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00001574 return clang_type;
1575
1576 default:
1577 assert(false && "not a forward clang type decl!");
1578 break;
1579 }
1580 return NULL;
1581}
1582
Greg Claytonc685f8e2010-09-15 04:15:46 +00001583Type*
Greg Clayton96d7d742010-11-10 23:42:09 +00001584SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
Greg Claytonc685f8e2010-09-15 04:15:46 +00001585{
1586 if (type_die != NULL)
1587 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00001588 Type *type = m_die_to_type.lookup (type_die);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001589 if (type == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001590 type = GetTypeForDIE (curr_cu, type_die).get();
Greg Clayton24739922010-10-13 03:15:28 +00001591 if (assert_not_being_parsed)
1592 assert (type != DIE_IS_BEING_PARSED);
Greg Clayton594e5ed2010-09-27 21:07:38 +00001593 return type;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001594 }
1595 return NULL;
1596}
1597
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001598CompileUnit*
Greg Clayton96d7d742010-11-10 23:42:09 +00001599SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001600{
1601 // Check if the symbol vendor already knows about this compile unit?
Greg Clayton96d7d742010-11-10 23:42:09 +00001602 if (curr_cu->GetUserData() == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001603 {
1604 // The symbol vendor doesn't know about this compile unit, we
1605 // need to parse and add it to the symbol vendor object.
1606 CompUnitSP dc_cu;
Greg Clayton96d7d742010-11-10 23:42:09 +00001607 ParseCompileUnit(curr_cu, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001608 if (dc_cu.get())
1609 {
1610 // Figure out the compile unit index if we weren't given one
Greg Clayton016a95e2010-09-14 02:20:48 +00001611 if (cu_idx == UINT32_MAX)
Greg Clayton96d7d742010-11-10 23:42:09 +00001612 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001613
1614 m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
Greg Clayton450e3f32010-10-12 02:24:53 +00001615
1616 if (m_debug_map_symfile)
1617 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001618 }
1619 }
Greg Clayton96d7d742010-11-10 23:42:09 +00001620 return (CompileUnit*)curr_cu->GetUserData();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001621}
1622
1623bool
Greg Clayton96d7d742010-11-10 23:42:09 +00001624SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001625{
1626 sc.Clear();
1627 // Check if the symbol vendor already knows about this compile unit?
1628 sc.module_sp = m_obj_file->GetModule()->GetSP();
Greg Clayton96d7d742010-11-10 23:42:09 +00001629 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001630
1631 sc.function = sc.comp_unit->FindFunctionByUID (func_die->GetOffset()).get();
1632 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001633 sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001634
1635 return sc.function != NULL;
1636}
1637
1638uint32_t
1639SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1640{
1641 Timer scoped_timer(__PRETTY_FUNCTION__,
1642 "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
1643 so_addr.GetSection(),
1644 so_addr.GetOffset(),
1645 resolve_scope);
1646 uint32_t resolved = 0;
1647 if (resolve_scope & ( eSymbolContextCompUnit |
1648 eSymbolContextFunction |
1649 eSymbolContextBlock |
1650 eSymbolContextLineEntry))
1651 {
1652 lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1653
1654 DWARFDebugAranges* debug_aranges = DebugAranges();
1655 DWARFDebugInfo* debug_info = DebugInfo();
1656 if (debug_aranges)
1657 {
1658 dw_offset_t cu_offset = debug_aranges->FindAddress(file_vm_addr);
1659 if (cu_offset != DW_INVALID_OFFSET)
1660 {
1661 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00001662 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1663 if (curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001664 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001665 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001666 assert(sc.comp_unit != NULL);
1667 resolved |= eSymbolContextCompUnit;
1668
1669 if (resolve_scope & eSymbolContextLineEntry)
1670 {
1671 LineTable *line_table = sc.comp_unit->GetLineTable();
1672 if (line_table == NULL)
1673 {
1674 if (ParseCompileUnitLineTable(sc))
1675 line_table = sc.comp_unit->GetLineTable();
1676 }
1677 if (line_table != NULL)
1678 {
1679 if (so_addr.IsLinkedAddress())
1680 {
1681 Address linked_addr (so_addr);
1682 linked_addr.ResolveLinkedAddress();
1683 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
1684 {
1685 resolved |= eSymbolContextLineEntry;
1686 }
1687 }
1688 else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
1689 {
1690 resolved |= eSymbolContextLineEntry;
1691 }
1692 }
1693 }
1694
1695 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1696 {
1697 DWARFDebugInfoEntry *function_die = NULL;
1698 DWARFDebugInfoEntry *block_die = NULL;
1699 if (resolve_scope & eSymbolContextBlock)
1700 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001701 curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001702 }
1703 else
1704 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001705 curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001706 }
1707
1708 if (function_die != NULL)
1709 {
1710 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1711 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001712 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001713 }
1714
1715 if (sc.function != NULL)
1716 {
1717 resolved |= eSymbolContextFunction;
1718
1719 if (resolve_scope & eSymbolContextBlock)
1720 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001721 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001722
1723 if (block_die != NULL)
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001724 sc.block = block.FindBlockByID (block_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001725 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001726 sc.block = block.FindBlockByID (function_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001727 if (sc.block)
1728 resolved |= eSymbolContextBlock;
1729 }
1730 }
1731 }
1732 }
1733 }
1734 }
1735 }
1736 return resolved;
1737}
1738
1739
1740
1741uint32_t
1742SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1743{
1744 const uint32_t prev_size = sc_list.GetSize();
1745 if (resolve_scope & eSymbolContextCompUnit)
1746 {
1747 DWARFDebugInfo* debug_info = DebugInfo();
1748 if (debug_info)
1749 {
1750 uint32_t cu_idx;
Greg Clayton96d7d742010-11-10 23:42:09 +00001751 DWARFCompileUnit* curr_cu = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001752
Greg Clayton96d7d742010-11-10 23:42:09 +00001753 for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001754 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001755 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001756 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
1757 if (check_inlines || file_spec_matches_cu_file_spec)
1758 {
1759 SymbolContext sc (m_obj_file->GetModule());
Greg Clayton96d7d742010-11-10 23:42:09 +00001760 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001761 assert(sc.comp_unit != NULL);
1762
1763 uint32_t file_idx = UINT32_MAX;
1764
1765 // If we are looking for inline functions only and we don't
1766 // find it in the support files, we are done.
1767 if (check_inlines)
1768 {
1769 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec);
1770 if (file_idx == UINT32_MAX)
1771 continue;
1772 }
1773
1774 if (line != 0)
1775 {
1776 LineTable *line_table = sc.comp_unit->GetLineTable();
1777
1778 if (line_table != NULL && line != 0)
1779 {
1780 // We will have already looked up the file index if
1781 // we are searching for inline entries.
1782 if (!check_inlines)
1783 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec);
1784
1785 if (file_idx != UINT32_MAX)
1786 {
1787 uint32_t found_line;
1788 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1789 found_line = sc.line_entry.line;
1790
Greg Clayton016a95e2010-09-14 02:20:48 +00001791 while (line_idx != UINT32_MAX)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001792 {
1793 sc.function = NULL;
1794 sc.block = NULL;
1795 if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1796 {
1797 const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1798 if (file_vm_addr != LLDB_INVALID_ADDRESS)
1799 {
1800 DWARFDebugInfoEntry *function_die = NULL;
1801 DWARFDebugInfoEntry *block_die = NULL;
Greg Clayton96d7d742010-11-10 23:42:09 +00001802 curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001803
1804 if (function_die != NULL)
1805 {
1806 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1807 if (sc.function == NULL)
Greg Clayton96d7d742010-11-10 23:42:09 +00001808 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001809 }
1810
1811 if (sc.function != NULL)
1812 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001813 Block& block = sc.function->GetBlock (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001814
1815 if (block_die != NULL)
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001816 sc.block = block.FindBlockByID (block_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001817 else
Greg Clayton0b76a2c2010-08-21 02:22:51 +00001818 sc.block = block.FindBlockByID (function_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001819 }
1820 }
1821 }
1822
1823 sc_list.Append(sc);
1824 line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1825 }
1826 }
1827 }
1828 else if (file_spec_matches_cu_file_spec && !check_inlines)
1829 {
1830 // only append the context if we aren't looking for inline call sites
1831 // by file and line and if the file spec matches that of the compile unit
1832 sc_list.Append(sc);
1833 }
1834 }
1835 else if (file_spec_matches_cu_file_spec && !check_inlines)
1836 {
1837 // only append the context if we aren't looking for inline call sites
1838 // by file and line and if the file spec matches that of the compile unit
1839 sc_list.Append(sc);
1840 }
1841
1842 if (!check_inlines)
1843 break;
1844 }
1845 }
1846 }
1847 }
1848 return sc_list.GetSize() - prev_size;
1849}
1850
1851void
1852SymbolFileDWARF::Index ()
1853{
1854 if (m_indexed)
1855 return;
1856 m_indexed = true;
1857 Timer scoped_timer (__PRETTY_FUNCTION__,
1858 "SymbolFileDWARF::Index (%s)",
1859 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1860
1861 DWARFDebugInfo* debug_info = DebugInfo();
1862 if (debug_info)
1863 {
Greg Clayton016a95e2010-09-14 02:20:48 +00001864 m_aranges.reset(new DWARFDebugAranges());
1865
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001866 uint32_t cu_idx = 0;
1867 const uint32_t num_compile_units = GetNumCompileUnits();
1868 for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1869 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001870 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001871
Greg Clayton96d7d742010-11-10 23:42:09 +00001872 bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001873
Greg Clayton96d7d742010-11-10 23:42:09 +00001874 curr_cu->Index (cu_idx,
Greg Clayton83c5cd92010-11-14 22:13:40 +00001875 m_function_basename_index,
1876 m_function_fullname_index,
1877 m_function_method_index,
1878 m_function_selector_index,
1879 m_objc_class_selectors_index,
1880 m_global_index,
1881 m_type_index,
1882 m_namespace_index,
1883 DebugRanges(),
1884 m_aranges.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001885
1886 // Keep memory down by clearing DIEs if this generate function
1887 // caused them to be parsed
1888 if (clear_dies)
Greg Clayton96d7d742010-11-10 23:42:09 +00001889 curr_cu->ClearDIEs (true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001890 }
1891
Greg Clayton016a95e2010-09-14 02:20:48 +00001892 m_aranges->Sort();
Greg Claytonc685f8e2010-09-15 04:15:46 +00001893
Greg Clayton24739922010-10-13 03:15:28 +00001894#if defined (ENABLE_DEBUG_PRINTF)
Greg Clayton7bd65b92011-02-09 23:39:34 +00001895 StreamFile s(stdout, false);
Greg Clayton24739922010-10-13 03:15:28 +00001896 s.Printf ("DWARF index for (%s) '%s/%s':",
1897 GetObjectFile()->GetModule()->GetArchitecture().AsCString(),
1898 GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
1899 GetObjectFile()->GetFileSpec().GetFilename().AsCString());
Greg Claytonba2d22d2010-11-13 22:57:37 +00001900 s.Printf("\nFunction basenames:\n"); m_function_basename_index.Dump (&s);
1901 s.Printf("\nFunction fullnames:\n"); m_function_fullname_index.Dump (&s);
1902 s.Printf("\nFunction methods:\n"); m_function_method_index.Dump (&s);
1903 s.Printf("\nFunction selectors:\n"); m_function_selector_index.Dump (&s);
1904 s.Printf("\nObjective C class selectors:\n"); m_objc_class_selectors_index.Dump (&s);
1905 s.Printf("\nGlobals and statics:\n"); m_global_index.Dump (&s);
Greg Clayton69b04882010-10-15 02:03:22 +00001906 s.Printf("\nTypes:\n"); m_type_index.Dump (&s);
Greg Claytonba2d22d2010-11-13 22:57:37 +00001907 s.Printf("\nNamepaces:\n"); m_namespace_index.Dump (&s);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001908#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001909 }
1910}
1911
1912uint32_t
1913SymbolFileDWARF::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
1914{
Greg Claytonc685f8e2010-09-15 04:15:46 +00001915 DWARFDebugInfo* info = DebugInfo();
1916 if (info == NULL)
1917 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001918
1919 // If we aren't appending the results to this list, then clear the list
1920 if (!append)
1921 variables.Clear();
1922
1923 // Remember how many variables are in the list before we search in case
1924 // we are appending the results to a variable list.
1925 const uint32_t original_size = variables.GetSize();
1926
1927 // Index the DWARF if we haven't already
1928 if (!m_indexed)
1929 Index ();
1930
Greg Claytonc685f8e2010-09-15 04:15:46 +00001931 SymbolContext sc;
1932 sc.module_sp = m_obj_file->GetModule()->GetSP();
1933 assert (sc.module_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001934
Greg Clayton96d7d742010-11-10 23:42:09 +00001935 DWARFCompileUnit* curr_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001936 DWARFCompileUnit* prev_cu = NULL;
1937 const DWARFDebugInfoEntry* die = NULL;
1938 std::vector<NameToDIE::Info> die_info_array;
1939 const size_t num_matches = m_global_index.Find(name, die_info_array);
Greg Clayton96d7d742010-11-10 23:42:09 +00001940 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001941 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001942 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001943
Greg Clayton96d7d742010-11-10 23:42:09 +00001944 if (curr_cu != prev_cu)
1945 curr_cu->ExtractDIEsIfNeeded (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001946
Greg Clayton96d7d742010-11-10 23:42:09 +00001947 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001948
Greg Clayton96d7d742010-11-10 23:42:09 +00001949 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001950 assert(sc.comp_unit != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001951
Greg Clayton96d7d742010-11-10 23:42:09 +00001952 ParseVariables(sc, curr_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001953
1954 if (variables.GetSize() - original_size >= max_matches)
1955 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001956 }
1957
1958 // Return the number of variable that were appended to the list
1959 return variables.GetSize() - original_size;
1960}
1961
1962uint32_t
1963SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
1964{
Greg Claytonc685f8e2010-09-15 04:15:46 +00001965 DWARFDebugInfo* info = DebugInfo();
1966 if (info == NULL)
1967 return 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001968
1969 // If we aren't appending the results to this list, then clear the list
1970 if (!append)
1971 variables.Clear();
1972
1973 // Remember how many variables are in the list before we search in case
1974 // we are appending the results to a variable list.
1975 const uint32_t original_size = variables.GetSize();
1976
1977 // Index the DWARF if we haven't already
1978 if (!m_indexed)
1979 Index ();
1980
Greg Claytonc685f8e2010-09-15 04:15:46 +00001981 SymbolContext sc;
1982 sc.module_sp = m_obj_file->GetModule()->GetSP();
1983 assert (sc.module_sp);
1984
Greg Clayton96d7d742010-11-10 23:42:09 +00001985 DWARFCompileUnit* curr_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00001986 DWARFCompileUnit* prev_cu = NULL;
1987 const DWARFDebugInfoEntry* die = NULL;
1988 std::vector<NameToDIE::Info> die_info_array;
1989 const size_t num_matches = m_global_index.Find(regex, die_info_array);
Greg Clayton96d7d742010-11-10 23:42:09 +00001990 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001991 {
Greg Clayton96d7d742010-11-10 23:42:09 +00001992 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
Greg Claytonc685f8e2010-09-15 04:15:46 +00001993
Greg Clayton96d7d742010-11-10 23:42:09 +00001994 if (curr_cu != prev_cu)
1995 curr_cu->ExtractDIEsIfNeeded (false);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001996
Greg Clayton96d7d742010-11-10 23:42:09 +00001997 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00001998
Greg Clayton96d7d742010-11-10 23:42:09 +00001999 sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002000 assert(sc.comp_unit != NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002001
Greg Clayton96d7d742010-11-10 23:42:09 +00002002 ParseVariables(sc, curr_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002003
Greg Claytonc685f8e2010-09-15 04:15:46 +00002004 if (variables.GetSize() - original_size >= max_matches)
2005 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002006 }
2007
2008 // Return the number of variable that were appended to the list
2009 return variables.GetSize() - original_size;
2010}
2011
2012
Greg Clayton0c5cd902010-06-28 21:30:43 +00002013void
2014SymbolFileDWARF::FindFunctions
2015(
2016 const ConstString &name,
Greg Claytonc685f8e2010-09-15 04:15:46 +00002017 const NameToDIE &name_to_die,
Greg Clayton0c5cd902010-06-28 21:30:43 +00002018 SymbolContextList& sc_list
2019)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002020{
Greg Claytonc685f8e2010-09-15 04:15:46 +00002021 DWARFDebugInfo* info = DebugInfo();
2022 if (info == NULL)
2023 return;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002024
Greg Claytonc685f8e2010-09-15 04:15:46 +00002025 SymbolContext sc;
2026 sc.module_sp = m_obj_file->GetModule()->GetSP();
2027 assert (sc.module_sp);
2028
Greg Clayton96d7d742010-11-10 23:42:09 +00002029 DWARFCompileUnit* curr_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002030 DWARFCompileUnit* prev_cu = NULL;
2031 const DWARFDebugInfoEntry* die = NULL;
2032 std::vector<NameToDIE::Info> die_info_array;
Greg Claytond7e05462010-11-14 00:22:48 +00002033 const size_t num_matches = name_to_die.Find (name, die_info_array);
Greg Clayton96d7d742010-11-10 23:42:09 +00002034 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002035 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002036 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002037
Greg Clayton96d7d742010-11-10 23:42:09 +00002038 if (curr_cu != prev_cu)
2039 curr_cu->ExtractDIEsIfNeeded (false);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002040
Greg Clayton96d7d742010-11-10 23:42:09 +00002041 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Greg Claytond7e05462010-11-14 00:22:48 +00002042
2043 const DWARFDebugInfoEntry* inlined_die = NULL;
2044 if (die->Tag() == DW_TAG_inlined_subroutine)
2045 {
2046 inlined_die = die;
2047
2048 while ((die = die->GetParent()) != NULL)
2049 {
2050 if (die->Tag() == DW_TAG_subprogram)
2051 break;
2052 }
2053 }
2054 assert (die->Tag() == DW_TAG_subprogram);
Greg Clayton96d7d742010-11-10 23:42:09 +00002055 if (GetFunction (curr_cu, die, sc))
Greg Claytonc685f8e2010-09-15 04:15:46 +00002056 {
Greg Claytond7e05462010-11-14 00:22:48 +00002057 Address addr;
2058 // Parse all blocks if needed
2059 if (inlined_die)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002060 {
Greg Claytond7e05462010-11-14 00:22:48 +00002061 sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2062 assert (sc.block != NULL);
2063 if (sc.block->GetStartAddress (addr) == false)
2064 addr.Clear();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002065 }
Greg Claytond7e05462010-11-14 00:22:48 +00002066 else
2067 {
2068 sc.block = NULL;
2069 addr = sc.function->GetAddressRange().GetBaseAddress();
2070 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002071
Greg Claytond7e05462010-11-14 00:22:48 +00002072 if (addr.IsValid())
2073 {
2074
2075 // We found the function, so we should find the line table
2076 // and line table entry as well
2077 LineTable *line_table = sc.comp_unit->GetLineTable();
2078 if (line_table == NULL)
2079 {
2080 if (ParseCompileUnitLineTable(sc))
2081 line_table = sc.comp_unit->GetLineTable();
2082 }
2083 if (line_table != NULL)
2084 line_table->FindLineEntryByAddress (addr, sc.line_entry);
2085
2086 sc_list.Append(sc);
2087 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002088 }
2089 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002090}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002091
Greg Claytonc685f8e2010-09-15 04:15:46 +00002092
2093void
2094SymbolFileDWARF::FindFunctions
2095(
2096 const RegularExpression &regex,
2097 const NameToDIE &name_to_die,
2098 SymbolContextList& sc_list
2099)
2100{
2101 DWARFDebugInfo* info = DebugInfo();
2102 if (info == NULL)
2103 return;
2104
2105 SymbolContext sc;
2106 sc.module_sp = m_obj_file->GetModule()->GetSP();
2107 assert (sc.module_sp);
2108
Greg Clayton96d7d742010-11-10 23:42:09 +00002109 DWARFCompileUnit* curr_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002110 DWARFCompileUnit* prev_cu = NULL;
2111 const DWARFDebugInfoEntry* die = NULL;
2112 std::vector<NameToDIE::Info> die_info_array;
2113 const size_t num_matches = name_to_die.Find(regex, die_info_array);
Greg Clayton96d7d742010-11-10 23:42:09 +00002114 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002115 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002116 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002117
Greg Clayton96d7d742010-11-10 23:42:09 +00002118 if (curr_cu != prev_cu)
2119 curr_cu->ExtractDIEsIfNeeded (false);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002120
Greg Clayton96d7d742010-11-10 23:42:09 +00002121 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Greg Claytonab843392010-12-03 17:49:14 +00002122
2123 const DWARFDebugInfoEntry* inlined_die = NULL;
2124 if (die->Tag() == DW_TAG_inlined_subroutine)
2125 {
2126 inlined_die = die;
2127
2128 while ((die = die->GetParent()) != NULL)
2129 {
2130 if (die->Tag() == DW_TAG_subprogram)
2131 break;
2132 }
2133 }
2134 assert (die->Tag() == DW_TAG_subprogram);
Greg Clayton96d7d742010-11-10 23:42:09 +00002135 if (GetFunction (curr_cu, die, sc))
Greg Claytonc685f8e2010-09-15 04:15:46 +00002136 {
Greg Claytonab843392010-12-03 17:49:14 +00002137 Address addr;
2138 // Parse all blocks if needed
2139 if (inlined_die)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002140 {
Greg Claytonab843392010-12-03 17:49:14 +00002141 sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2142 assert (sc.block != NULL);
2143 if (sc.block->GetStartAddress (addr) == false)
2144 addr.Clear();
Greg Claytonc685f8e2010-09-15 04:15:46 +00002145 }
Greg Claytonab843392010-12-03 17:49:14 +00002146 else
2147 {
2148 sc.block = NULL;
2149 addr = sc.function->GetAddressRange().GetBaseAddress();
2150 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002151
Greg Claytonab843392010-12-03 17:49:14 +00002152 if (addr.IsValid())
2153 {
2154
2155 // We found the function, so we should find the line table
2156 // and line table entry as well
2157 LineTable *line_table = sc.comp_unit->GetLineTable();
2158 if (line_table == NULL)
2159 {
2160 if (ParseCompileUnitLineTable(sc))
2161 line_table = sc.comp_unit->GetLineTable();
2162 }
2163 if (line_table != NULL)
2164 line_table->FindLineEntryByAddress (addr, sc.line_entry);
2165
2166 sc_list.Append(sc);
2167 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002168 }
2169 }
Greg Clayton0c5cd902010-06-28 21:30:43 +00002170}
2171
2172uint32_t
2173SymbolFileDWARF::FindFunctions
2174(
2175 const ConstString &name,
2176 uint32_t name_type_mask,
2177 bool append,
2178 SymbolContextList& sc_list
2179)
2180{
2181 Timer scoped_timer (__PRETTY_FUNCTION__,
2182 "SymbolFileDWARF::FindFunctions (name = '%s')",
2183 name.AsCString());
2184
Greg Clayton0c5cd902010-06-28 21:30:43 +00002185 // If we aren't appending the results to this list, then clear the list
2186 if (!append)
2187 sc_list.Clear();
2188
2189 // Remember how many sc_list are in the list before we search in case
2190 // we are appending the results to a variable list.
2191 uint32_t original_size = sc_list.GetSize();
2192
2193 // Index the DWARF if we haven't already
2194 if (!m_indexed)
2195 Index ();
2196
2197 if (name_type_mask & eFunctionNameTypeBase)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002198 FindFunctions (name, m_function_basename_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00002199
2200 if (name_type_mask & eFunctionNameTypeFull)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002201 FindFunctions (name, m_function_fullname_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00002202
2203 if (name_type_mask & eFunctionNameTypeMethod)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002204 FindFunctions (name, m_function_method_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00002205
2206 if (name_type_mask & eFunctionNameTypeSelector)
Greg Claytonc685f8e2010-09-15 04:15:46 +00002207 FindFunctions (name, m_function_selector_index, sc_list);
Greg Clayton0c5cd902010-06-28 21:30:43 +00002208
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002209 // Return the number of variable that were appended to the list
2210 return sc_list.GetSize() - original_size;
2211}
2212
2213
2214uint32_t
2215SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
2216{
2217 Timer scoped_timer (__PRETTY_FUNCTION__,
2218 "SymbolFileDWARF::FindFunctions (regex = '%s')",
2219 regex.GetText());
2220
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002221 // If we aren't appending the results to this list, then clear the list
2222 if (!append)
2223 sc_list.Clear();
2224
2225 // Remember how many sc_list are in the list before we search in case
2226 // we are appending the results to a variable list.
2227 uint32_t original_size = sc_list.GetSize();
2228
2229 // Index the DWARF if we haven't already
2230 if (!m_indexed)
2231 Index ();
2232
Greg Claytonc685f8e2010-09-15 04:15:46 +00002233 FindFunctions (regex, m_function_basename_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002234
Greg Claytonc685f8e2010-09-15 04:15:46 +00002235 FindFunctions (regex, m_function_fullname_index, sc_list);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002236
2237 // Return the number of variable that were appended to the list
2238 return sc_list.GetSize() - original_size;
2239}
2240
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002241uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002242SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, bool append, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002243{
Greg Claytonc685f8e2010-09-15 04:15:46 +00002244 DWARFDebugInfo* info = DebugInfo();
2245 if (info == NULL)
2246 return 0;
2247
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002248 // If we aren't appending the results to this list, then clear the list
2249 if (!append)
2250 types.Clear();
2251
Greg Clayton6dbd3982010-09-15 05:51:24 +00002252 // Index if we already haven't to make sure the compile units
2253 // get indexed and make their global DIE index list
2254 if (!m_indexed)
2255 Index ();
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002256
Greg Claytonc685f8e2010-09-15 04:15:46 +00002257 const uint32_t initial_types_size = types.GetSize();
Greg Clayton96d7d742010-11-10 23:42:09 +00002258 DWARFCompileUnit* curr_cu = NULL;
Greg Claytonc685f8e2010-09-15 04:15:46 +00002259 DWARFCompileUnit* prev_cu = NULL;
2260 const DWARFDebugInfoEntry* die = NULL;
2261 std::vector<NameToDIE::Info> die_info_array;
Greg Clayton69b04882010-10-15 02:03:22 +00002262 const size_t num_matches = m_type_index.Find (name, die_info_array);
Greg Clayton96d7d742010-11-10 23:42:09 +00002263 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002264 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002265 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002266
Greg Clayton96d7d742010-11-10 23:42:09 +00002267 if (curr_cu != prev_cu)
2268 curr_cu->ExtractDIEsIfNeeded (false);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002269
Greg Clayton96d7d742010-11-10 23:42:09 +00002270 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002271
Greg Clayton96d7d742010-11-10 23:42:09 +00002272 Type *matching_type = ResolveType (curr_cu, die);
Greg Claytonc685f8e2010-09-15 04:15:46 +00002273 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002274 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00002275 // We found a type pointer, now find the shared pointer form our type list
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002276 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
Greg Clayton73bf5db2011-06-17 01:22:15 +00002277 if (type_sp)
2278 {
2279 types.InsertUnique (type_sp);
2280 if (types.GetSize() >= max_matches)
2281 break;
2282 }
2283 else
2284 {
2285 fprintf (stderr, "error: can't find shared pointer for type 0x%8.8x.\n", matching_type->GetID());
2286 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002287 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002288 }
Greg Claytonc685f8e2010-09-15 04:15:46 +00002289 return types.GetSize() - initial_types_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002290}
2291
2292
Greg Clayton526e5af2010-11-13 03:52:47 +00002293ClangNamespaceDecl
Greg Clayton96d7d742010-11-10 23:42:09 +00002294SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
2295 const ConstString &name)
2296{
Greg Clayton526e5af2010-11-13 03:52:47 +00002297 ClangNamespaceDecl namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00002298 DWARFDebugInfo* info = DebugInfo();
Greg Clayton526e5af2010-11-13 03:52:47 +00002299 if (info)
Greg Clayton96d7d742010-11-10 23:42:09 +00002300 {
Greg Clayton526e5af2010-11-13 03:52:47 +00002301 // Index if we already haven't to make sure the compile units
2302 // get indexed and make their global DIE index list
2303 if (!m_indexed)
2304 Index ();
Greg Clayton96d7d742010-11-10 23:42:09 +00002305
Greg Clayton526e5af2010-11-13 03:52:47 +00002306 DWARFCompileUnit* curr_cu = NULL;
2307 DWARFCompileUnit* prev_cu = NULL;
2308 const DWARFDebugInfoEntry* die = NULL;
2309 std::vector<NameToDIE::Info> die_info_array;
2310 const size_t num_matches = m_namespace_index.Find (name, die_info_array);
2311 for (size_t i=0; i<num_matches; ++i, prev_cu = curr_cu)
2312 {
2313 curr_cu = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
2314
2315 if (curr_cu != prev_cu)
2316 curr_cu->ExtractDIEsIfNeeded (false);
Greg Clayton96d7d742010-11-10 23:42:09 +00002317
Greg Clayton526e5af2010-11-13 03:52:47 +00002318 die = curr_cu->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
2319
2320 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (curr_cu, die);
2321 if (clang_namespace_decl)
2322 {
2323 namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
2324 namespace_decl.SetNamespaceDecl (clang_namespace_decl);
2325 }
2326 }
Greg Clayton96d7d742010-11-10 23:42:09 +00002327 }
Greg Clayton526e5af2010-11-13 03:52:47 +00002328 return namespace_decl;
Greg Clayton96d7d742010-11-10 23:42:09 +00002329}
2330
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002331uint32_t
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002332SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002333{
2334 // Remember how many sc_list are in the list before we search in case
2335 // we are appending the results to a variable list.
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002336 uint32_t original_size = types.GetSize();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002337
2338 const uint32_t num_die_offsets = die_offsets.size();
2339 // Parse all of the types we found from the pubtypes matches
2340 uint32_t i;
2341 uint32_t num_matches = 0;
2342 for (i = 0; i < num_die_offsets; ++i)
2343 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002344 Type *matching_type = ResolveTypeUID (die_offsets[i]);
2345 if (matching_type)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002346 {
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002347 // We found a type pointer, now find the shared pointer form our type list
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002348 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002349 assert (type_sp.get() != NULL);
2350 types.InsertUnique (type_sp);
2351 ++num_matches;
2352 if (num_matches >= max_matches)
2353 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002354 }
2355 }
2356
2357 // Return the number of variable that were appended to the list
Greg Claytonb0b9fe62010-08-03 00:35:52 +00002358 return types.GetSize() - original_size;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002359}
2360
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002361
2362size_t
2363SymbolFileDWARF::ParseChildParameters
2364(
2365 const SymbolContext& sc,
2366 TypeSP& type_sp,
Greg Clayton0fffff52010-09-24 05:15:53 +00002367 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002368 const DWARFDebugInfoEntry *parent_die,
Greg Claytona51ed9b2010-09-23 01:09:21 +00002369 bool skip_artificial,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002370 TypeList* type_list,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002371 std::vector<clang_type_t>& function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00002372 std::vector<clang::ParmVarDecl*>& function_param_decls,
2373 unsigned &type_quals
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002374)
2375{
2376 if (parent_die == NULL)
2377 return 0;
2378
Greg Claytond88d7592010-09-15 08:33:30 +00002379 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2380
Greg Clayton7fedea22010-11-16 02:10:54 +00002381 size_t arg_idx = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002382 const DWARFDebugInfoEntry *die;
2383 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2384 {
2385 dw_tag_t tag = die->Tag();
2386 switch (tag)
2387 {
2388 case DW_TAG_formal_parameter:
2389 {
2390 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002391 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002392 if (num_attributes > 0)
2393 {
2394 const char *name = NULL;
2395 Declaration decl;
2396 dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00002397 bool is_artificial = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002398 // one of None, Auto, Register, Extern, Static, PrivateExtern
2399
Sean Callanane2ef6e32010-09-23 03:01:22 +00002400 clang::StorageClass storage = clang::SC_None;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002401 uint32_t i;
2402 for (i=0; i<num_attributes; ++i)
2403 {
2404 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2405 DWARFFormValue form_value;
2406 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2407 {
2408 switch (attr)
2409 {
2410 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2411 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2412 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2413 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
2414 case DW_AT_type: param_type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Claytona51ed9b2010-09-23 01:09:21 +00002415 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002416 case DW_AT_location:
2417 // if (form_value.BlockData())
2418 // {
2419 // const DataExtractor& debug_info_data = debug_info();
2420 // uint32_t block_length = form_value.Unsigned();
2421 // DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
2422 // }
2423 // else
2424 // {
2425 // }
2426 // break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002427 case DW_AT_const_value:
2428 case DW_AT_default_value:
2429 case DW_AT_description:
2430 case DW_AT_endianity:
2431 case DW_AT_is_optional:
2432 case DW_AT_segment:
2433 case DW_AT_variable_parameter:
2434 default:
2435 case DW_AT_abstract_origin:
2436 case DW_AT_sibling:
2437 break;
2438 }
2439 }
2440 }
Greg Claytona51ed9b2010-09-23 01:09:21 +00002441
Greg Clayton0fffff52010-09-24 05:15:53 +00002442 bool skip = false;
2443 if (skip_artificial)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002444 {
Greg Clayton0fffff52010-09-24 05:15:53 +00002445 if (is_artificial)
Greg Clayton7fedea22010-11-16 02:10:54 +00002446 {
2447 // In order to determine if a C++ member function is
2448 // "const" we have to look at the const-ness of "this"...
2449 // Ugly, but that
2450 if (arg_idx == 0)
2451 {
2452 const DWARFDebugInfoEntry *grandparent_die = parent_die->GetParent();
2453 if (grandparent_die && (grandparent_die->Tag() == DW_TAG_structure_type ||
2454 grandparent_die->Tag() == DW_TAG_class_type))
2455 {
2456 LanguageType language = sc.comp_unit->GetLanguage();
2457 if (language == eLanguageTypeObjC_plus_plus || language == eLanguageTypeC_plus_plus)
2458 {
2459 // Often times compilers omit the "this" name for the
2460 // specification DIEs, so we can't rely upon the name
2461 // being in the formal parameter DIE...
2462 if (name == NULL || ::strcmp(name, "this")==0)
2463 {
2464 Type *this_type = ResolveTypeUID (param_type_die_offset);
2465 if (this_type)
2466 {
2467 uint32_t encoding_mask = this_type->GetEncodingMask();
2468 if (encoding_mask & Type::eEncodingIsPointerUID)
2469 {
2470 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
Greg Clayton47fbf1a2010-11-16 22:09:25 +00002471 type_quals |= clang::Qualifiers::Const;
Greg Clayton7fedea22010-11-16 02:10:54 +00002472 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
Greg Clayton47fbf1a2010-11-16 22:09:25 +00002473 type_quals |= clang::Qualifiers::Volatile;
Greg Clayton7fedea22010-11-16 02:10:54 +00002474 }
2475 }
2476 }
2477 }
2478 }
2479 }
Greg Clayton0fffff52010-09-24 05:15:53 +00002480 skip = true;
Greg Clayton7fedea22010-11-16 02:10:54 +00002481 }
Greg Clayton0fffff52010-09-24 05:15:53 +00002482 else
2483 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002484
Greg Clayton0fffff52010-09-24 05:15:53 +00002485 // HACK: Objective C formal parameters "self" and "_cmd"
2486 // are not marked as artificial in the DWARF...
Greg Clayton96d7d742010-11-10 23:42:09 +00002487 CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2488 if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
Greg Clayton0fffff52010-09-24 05:15:53 +00002489 {
2490 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
2491 skip = true;
2492 }
2493 }
2494 }
2495
2496 if (!skip)
2497 {
2498 Type *type = ResolveTypeUID(param_type_die_offset);
2499 if (type)
2500 {
Greg Claytonc93237c2010-10-01 20:48:32 +00002501 function_param_types.push_back (type->GetClangForwardType());
Greg Clayton0fffff52010-09-24 05:15:53 +00002502
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002503 clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, type->GetClangForwardType(), storage);
Greg Clayton0fffff52010-09-24 05:15:53 +00002504 assert(param_var_decl);
2505 function_param_decls.push_back(param_var_decl);
2506 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002507 }
2508 }
Greg Clayton7fedea22010-11-16 02:10:54 +00002509 arg_idx++;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002510 }
2511 break;
2512
2513 default:
2514 break;
2515 }
2516 }
Greg Clayton7fedea22010-11-16 02:10:54 +00002517 return arg_idx;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002518}
2519
2520size_t
2521SymbolFileDWARF::ParseChildEnumerators
2522(
2523 const SymbolContext& sc,
Greg Clayton1be10fc2010-09-29 01:12:09 +00002524 clang_type_t enumerator_clang_type,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002525 uint32_t enumerator_byte_size,
Greg Clayton0fffff52010-09-24 05:15:53 +00002526 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002527 const DWARFDebugInfoEntry *parent_die
2528)
2529{
2530 if (parent_die == NULL)
2531 return 0;
2532
2533 size_t enumerators_added = 0;
2534 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00002535 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2536
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002537 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2538 {
2539 const dw_tag_t tag = die->Tag();
2540 if (tag == DW_TAG_enumerator)
2541 {
2542 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002543 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002544 if (num_child_attributes > 0)
2545 {
2546 const char *name = NULL;
2547 bool got_value = false;
2548 int64_t enum_value = 0;
2549 Declaration decl;
2550
2551 uint32_t i;
2552 for (i=0; i<num_child_attributes; ++i)
2553 {
2554 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2555 DWARFFormValue form_value;
2556 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2557 {
2558 switch (attr)
2559 {
2560 case DW_AT_const_value:
2561 got_value = true;
2562 enum_value = form_value.Unsigned();
2563 break;
2564
2565 case DW_AT_name:
2566 name = form_value.AsCString(&get_debug_str_data());
2567 break;
2568
2569 case DW_AT_description:
2570 default:
2571 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2572 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
2573 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2574 case DW_AT_sibling:
2575 break;
2576 }
2577 }
2578 }
2579
2580 if (name && name[0] && got_value)
2581 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002582 GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
2583 enumerator_clang_type,
2584 decl,
2585 name,
2586 enum_value,
2587 enumerator_byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002588 ++enumerators_added;
2589 }
2590 }
2591 }
2592 }
2593 return enumerators_added;
2594}
2595
2596void
2597SymbolFileDWARF::ParseChildArrayInfo
2598(
2599 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00002600 DWARFCompileUnit* dwarf_cu,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002601 const DWARFDebugInfoEntry *parent_die,
2602 int64_t& first_index,
2603 std::vector<uint64_t>& element_orders,
2604 uint32_t& byte_stride,
2605 uint32_t& bit_stride
2606)
2607{
2608 if (parent_die == NULL)
2609 return;
2610
2611 const DWARFDebugInfoEntry *die;
Greg Claytond88d7592010-09-15 08:33:30 +00002612 const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002613 for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2614 {
2615 const dw_tag_t tag = die->Tag();
2616 switch (tag)
2617 {
2618 case DW_TAG_enumerator:
2619 {
2620 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002621 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002622 if (num_child_attributes > 0)
2623 {
2624 const char *name = NULL;
2625 bool got_value = false;
2626 int64_t enum_value = 0;
2627
2628 uint32_t i;
2629 for (i=0; i<num_child_attributes; ++i)
2630 {
2631 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2632 DWARFFormValue form_value;
2633 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2634 {
2635 switch (attr)
2636 {
2637 case DW_AT_const_value:
2638 got_value = true;
2639 enum_value = form_value.Unsigned();
2640 break;
2641
2642 case DW_AT_name:
2643 name = form_value.AsCString(&get_debug_str_data());
2644 break;
2645
2646 case DW_AT_description:
2647 default:
2648 case DW_AT_decl_file:
2649 case DW_AT_decl_line:
2650 case DW_AT_decl_column:
2651 case DW_AT_sibling:
2652 break;
2653 }
2654 }
2655 }
2656 }
2657 }
2658 break;
2659
2660 case DW_TAG_subrange_type:
2661 {
2662 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00002663 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002664 if (num_child_attributes > 0)
2665 {
2666 const char *name = NULL;
2667 bool got_value = false;
2668 uint64_t byte_size = 0;
2669 int64_t enum_value = 0;
2670 uint64_t num_elements = 0;
2671 uint64_t lower_bound = 0;
2672 uint64_t upper_bound = 0;
2673 uint32_t i;
2674 for (i=0; i<num_child_attributes; ++i)
2675 {
2676 const dw_attr_t attr = attributes.AttributeAtIndex(i);
2677 DWARFFormValue form_value;
2678 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2679 {
2680 switch (attr)
2681 {
2682 case DW_AT_const_value:
2683 got_value = true;
2684 enum_value = form_value.Unsigned();
2685 break;
2686
2687 case DW_AT_name:
2688 name = form_value.AsCString(&get_debug_str_data());
2689 break;
2690
2691 case DW_AT_count:
2692 num_elements = form_value.Unsigned();
2693 break;
2694
2695 case DW_AT_bit_stride:
2696 bit_stride = form_value.Unsigned();
2697 break;
2698
2699 case DW_AT_byte_stride:
2700 byte_stride = form_value.Unsigned();
2701 break;
2702
2703 case DW_AT_byte_size:
2704 byte_size = form_value.Unsigned();
2705 break;
2706
2707 case DW_AT_lower_bound:
2708 lower_bound = form_value.Unsigned();
2709 break;
2710
2711 case DW_AT_upper_bound:
2712 upper_bound = form_value.Unsigned();
2713 break;
2714
2715 default:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002716 case DW_AT_abstract_origin:
2717 case DW_AT_accessibility:
2718 case DW_AT_allocated:
2719 case DW_AT_associated:
2720 case DW_AT_data_location:
2721 case DW_AT_declaration:
2722 case DW_AT_description:
2723 case DW_AT_sibling:
2724 case DW_AT_threads_scaled:
2725 case DW_AT_type:
2726 case DW_AT_visibility:
2727 break;
2728 }
2729 }
2730 }
2731
2732 if (upper_bound > lower_bound)
2733 num_elements = upper_bound - lower_bound + 1;
2734
2735 if (num_elements > 0)
2736 element_orders.push_back (num_elements);
2737 }
2738 }
2739 break;
2740 }
2741 }
2742}
2743
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002744TypeSP
Greg Clayton96d7d742010-11-10 23:42:09 +00002745SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002746{
2747 TypeSP type_sp;
2748 if (die != NULL)
2749 {
Greg Clayton96d7d742010-11-10 23:42:09 +00002750 assert(curr_cu != NULL);
Greg Clayton594e5ed2010-09-27 21:07:38 +00002751 Type *type_ptr = m_die_to_type.lookup (die);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002752 if (type_ptr == NULL)
2753 {
Greg Claytonca512b32011-01-14 04:54:56 +00002754 CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
2755 assert (lldb_cu);
2756 SymbolContext sc(lldb_cu);
Greg Clayton96d7d742010-11-10 23:42:09 +00002757 type_sp = ParseType(sc, curr_cu, die, NULL);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002758 }
2759 else if (type_ptr != DIE_IS_BEING_PARSED)
2760 {
2761 // Grab the existing type from the master types lists
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002762 type_sp = GetTypeList()->FindType(type_ptr->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002763 }
2764
2765 }
2766 return type_sp;
2767}
2768
2769clang::DeclContext *
2770SymbolFileDWARF::GetClangDeclContextForDIEOffset (dw_offset_t die_offset)
2771{
2772 if (die_offset != DW_INVALID_OFFSET)
2773 {
2774 DWARFCompileUnitSP cu_sp;
2775 const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
2776 return GetClangDeclContextForDIE (cu_sp.get(), die);
2777 }
2778 return NULL;
2779}
2780
2781
Greg Clayton96d7d742010-11-10 23:42:09 +00002782clang::NamespaceDecl *
2783SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
2784{
2785 if (die->Tag() == DW_TAG_namespace)
2786 {
2787 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
2788 if (namespace_name)
2789 {
2790 Declaration decl; // TODO: fill in the decl object
2791 clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextForDIE (curr_cu, die->GetParent()));
2792 if (namespace_decl)
Greg Claytona2721472011-06-25 00:44:06 +00002793 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
Greg Clayton96d7d742010-11-10 23:42:09 +00002794 return namespace_decl;
2795 }
2796 }
2797 return NULL;
2798}
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002799
2800clang::DeclContext *
Greg Clayton96d7d742010-11-10 23:42:09 +00002801SymbolFileDWARF::GetClangDeclContextForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002802{
Greg Claytonca512b32011-01-14 04:54:56 +00002803 if (m_clang_tu_decl == NULL)
2804 m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002805
Greg Claytonca512b32011-01-14 04:54:56 +00002806 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x )\n", die->GetOffset());
2807 const DWARFDebugInfoEntry * const decl_die = die;
Greg Clayton4cd17802011-01-25 06:17:32 +00002808 clang::DeclContext *decl_ctx = NULL;
2809
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002810 while (die != NULL)
2811 {
Greg Claytonca512b32011-01-14 04:54:56 +00002812 // If this is the original DIE that we are searching for a declaration
2813 // for, then don't look in the cache as we don't want our own decl
2814 // context to be our decl context...
2815 if (decl_die != die)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002816 {
Greg Claytonca512b32011-01-14 04:54:56 +00002817 DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die);
2818 if (pos != m_die_to_decl_ctx.end())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002819 {
Greg Claytonca512b32011-01-14 04:54:56 +00002820 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset());
2821 return pos->second;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002822 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002823
Greg Claytonca512b32011-01-14 04:54:56 +00002824 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) checking parent 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset());
2825
2826 switch (die->Tag())
2827 {
2828 case DW_TAG_namespace:
2829 {
2830 const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
2831 if (namespace_name)
2832 {
2833 Declaration decl; // TODO: fill in the decl object
Greg Clayton6beaaa62011-01-17 03:46:26 +00002834 clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextForDIE (curr_cu, die));
Greg Claytonca512b32011-01-14 04:54:56 +00002835 if (namespace_decl)
2836 {
2837 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset());
Greg Claytona2721472011-06-25 00:44:06 +00002838 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
Greg Claytonca512b32011-01-14 04:54:56 +00002839 }
2840 return namespace_decl;
2841 }
2842 }
2843 break;
2844
2845 case DW_TAG_structure_type:
2846 case DW_TAG_union_type:
2847 case DW_TAG_class_type:
2848 {
Greg Clayton4cd17802011-01-25 06:17:32 +00002849 Type* type = ResolveType (curr_cu, die);
Greg Claytonca512b32011-01-14 04:54:56 +00002850 pos = m_die_to_decl_ctx.find(die);
Greg Claytonca512b32011-01-14 04:54:56 +00002851 if (pos != m_die_to_decl_ctx.end())
2852 {
2853 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), die->GetOffset());
2854 return pos->second;
2855 }
Greg Clayton4cd17802011-01-25 06:17:32 +00002856 else
2857 {
2858 if (type)
2859 {
2860 decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
2861 if (decl_ctx)
2862 return decl_ctx;
2863 }
2864 }
Greg Claytonca512b32011-01-14 04:54:56 +00002865 }
2866 break;
2867
2868 default:
2869 break;
2870 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002871 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002872
Greg Clayton6beaaa62011-01-17 03:46:26 +00002873 dw_offset_t die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_specification, DW_INVALID_OFFSET);
Greg Claytonca512b32011-01-14 04:54:56 +00002874 if (die_offset != DW_INVALID_OFFSET)
2875 {
2876 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) check DW_AT_specification 0x%8.8x\n", decl_die->GetOffset(), die_offset);
2877 decl_ctx = GetClangDeclContextForDIEOffset (die_offset);
2878 if (decl_ctx != m_clang_tu_decl)
2879 return decl_ctx;
2880 }
2881
Greg Clayton6beaaa62011-01-17 03:46:26 +00002882 die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
Greg Claytonca512b32011-01-14 04:54:56 +00002883 if (die_offset != DW_INVALID_OFFSET)
2884 {
2885 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) check DW_AT_abstract_origin 0x%8.8x\n", decl_die->GetOffset(), die_offset);
2886 decl_ctx = GetClangDeclContextForDIEOffset (die_offset);
2887 if (decl_ctx != m_clang_tu_decl)
2888 return decl_ctx;
2889 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002890
2891 die = die->GetParent();
2892 }
Greg Clayton7a345282010-11-09 23:46:37 +00002893 // Right now we have only one translation unit per module...
Greg Claytonca512b32011-01-14 04:54:56 +00002894 //printf ("SymbolFileDWARF::GetClangDeclContextForDIE ( die = 0x%8.8x ) => 0x%8.8x\n", decl_die->GetOffset(), curr_cu->GetFirstDIEOffset());
Greg Clayton7a345282010-11-09 23:46:37 +00002895 return m_clang_tu_decl;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002896}
2897
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002898// This function can be used when a DIE is found that is a forward declaration
2899// DIE and we want to try and find a type that has the complete definition.
2900TypeSP
2901SymbolFileDWARF::FindDefinitionTypeForDIE (
Greg Clayton1a65ae12011-01-25 23:55:37 +00002902 DWARFCompileUnit* cu,
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002903 const DWARFDebugInfoEntry *die,
2904 const ConstString &type_name
2905)
2906{
2907 TypeSP type_sp;
2908
Greg Clayton1a65ae12011-01-25 23:55:37 +00002909 if (cu == NULL || die == NULL || !type_name)
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002910 return type_sp;
2911
Greg Clayton69974892010-12-03 21:42:06 +00002912 if (!m_indexed)
2913 Index ();
2914
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002915 const dw_tag_t type_tag = die->Tag();
2916 std::vector<NameToDIE::Info> die_info_array;
2917 const size_t num_matches = m_type_index.Find (type_name, die_info_array);
2918 if (num_matches > 0)
2919 {
2920 DWARFCompileUnit* type_cu = NULL;
Greg Clayton1a65ae12011-01-25 23:55:37 +00002921 DWARFCompileUnit* curr_cu = cu;
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002922 DWARFDebugInfo *info = DebugInfo();
2923 for (size_t i=0; i<num_matches; ++i)
2924 {
2925 type_cu = info->GetCompileUnitAtIndex (die_info_array[i].cu_idx);
2926
2927 if (type_cu != curr_cu)
2928 {
2929 type_cu->ExtractDIEsIfNeeded (false);
2930 curr_cu = type_cu;
2931 }
2932
2933 DWARFDebugInfoEntry *type_die = type_cu->GetDIEAtIndexUnchecked (die_info_array[i].die_idx);
2934
2935 if (type_die != die && type_die->Tag() == type_tag)
2936 {
2937 // Hold off on comparing parent DIE tags until
2938 // we know what happens with stuff in namespaces
2939 // for gcc and clang...
2940 //DWARFDebugInfoEntry *parent_die = die->GetParent();
2941 //DWARFDebugInfoEntry *parent_type_die = type_die->GetParent();
2942 //if (parent_die->Tag() == parent_type_die->Tag())
2943 {
2944 Type *resolved_type = ResolveType (type_cu, type_die, false);
2945 if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
2946 {
2947 DEBUG_PRINTF ("resolved 0x%8.8x (cu 0x%8.8x) from %s to 0x%8.8x (cu 0x%8.8x)\n",
2948 die->GetOffset(),
Greg Clayton96d7d742010-11-10 23:42:09 +00002949 curr_cu->GetOffset(),
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002950 m_obj_file->GetFileSpec().GetFilename().AsCString(),
2951 type_die->GetOffset(),
2952 type_cu->GetOffset());
2953
2954 m_die_to_type[die] = resolved_type;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002955 type_sp = GetTypeList()->FindType(resolved_type->GetID());
Greg Clayton40328bf2010-11-08 02:05:08 +00002956 if (!type_sp)
2957 {
2958 DEBUG_PRINTF("unable to resolve type '%s' from DIE 0x%8.8x\n", type_name.GetCString(), die->GetOffset());
2959 }
Greg Clayton2ccf8cf2010-11-07 21:02:03 +00002960 break;
2961 }
2962 }
2963 }
2964 }
2965 }
2966 return type_sp;
2967}
2968
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002969TypeSP
Greg Clayton1be10fc2010-09-29 01:12:09 +00002970SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002971{
2972 TypeSP type_sp;
2973
Greg Clayton1be10fc2010-09-29 01:12:09 +00002974 if (type_is_new_ptr)
2975 *type_is_new_ptr = false;
2976
Sean Callananc7fbf732010-08-06 00:32:49 +00002977 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002978 if (die != NULL)
2979 {
Greg Clayton594e5ed2010-09-27 21:07:38 +00002980 Type *type_ptr = m_die_to_type.lookup (die);
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002981 TypeList* type_list = GetTypeList();
Greg Clayton594e5ed2010-09-27 21:07:38 +00002982 if (type_ptr == NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002983 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00002984 ClangASTContext &ast = GetClangASTContext();
Greg Clayton1be10fc2010-09-29 01:12:09 +00002985 if (type_is_new_ptr)
2986 *type_is_new_ptr = true;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002987
Greg Clayton594e5ed2010-09-27 21:07:38 +00002988 const dw_tag_t tag = die->Tag();
2989
Chris Lattner30fdc8d2010-06-08 16:52:24 +00002990 bool is_forward_declaration = false;
2991 DWARFDebugInfoEntry::Attributes attributes;
2992 const char *type_name_cstr = NULL;
Greg Clayton24739922010-10-13 03:15:28 +00002993 ConstString type_name_const_str;
Greg Clayton526e5af2010-11-13 03:52:47 +00002994 Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
2995 size_t byte_size = 0;
Greg Clayton36909642011-03-15 04:38:20 +00002996 bool byte_size_valid = false;
Greg Clayton526e5af2010-11-13 03:52:47 +00002997 Declaration decl;
2998
Greg Clayton4957bf62010-09-30 21:49:03 +00002999 Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
Greg Clayton1be10fc2010-09-29 01:12:09 +00003000 clang_type_t clang_type = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003001
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003002 dw_attr_t attr;
3003
3004 switch (tag)
3005 {
3006 case DW_TAG_base_type:
3007 case DW_TAG_pointer_type:
3008 case DW_TAG_reference_type:
3009 case DW_TAG_typedef:
3010 case DW_TAG_const_type:
3011 case DW_TAG_restrict_type:
3012 case DW_TAG_volatile_type:
3013 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003014 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003015 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003016
Greg Claytond88d7592010-09-15 08:33:30 +00003017 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003018 uint32_t encoding = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003019 lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
3020
3021 if (num_attributes > 0)
3022 {
3023 uint32_t i;
3024 for (i=0; i<num_attributes; ++i)
3025 {
3026 attr = attributes.AttributeAtIndex(i);
3027 DWARFFormValue form_value;
3028 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3029 {
3030 switch (attr)
3031 {
3032 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3033 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3034 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3035 case DW_AT_name:
Jim Ingham337030f2011-04-15 23:41:23 +00003036
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003037 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Jim Ingham337030f2011-04-15 23:41:23 +00003038 // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
3039 // include the "&"...
3040 if (tag == DW_TAG_reference_type)
3041 {
3042 if (strchr (type_name_cstr, '&') == NULL)
3043 type_name_cstr = NULL;
3044 }
3045 if (type_name_cstr)
3046 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003047 break;
Greg Clayton36909642011-03-15 04:38:20 +00003048 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003049 case DW_AT_encoding: encoding = form_value.Unsigned(); break;
3050 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
3051 default:
3052 case DW_AT_sibling:
3053 break;
3054 }
3055 }
3056 }
3057 }
3058
Greg Claytonc93237c2010-10-01 20:48:32 +00003059 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") type => 0x%8.8x\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid);
3060
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003061 switch (tag)
3062 {
3063 default:
Greg Clayton526e5af2010-11-13 03:52:47 +00003064 break;
3065
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003066 case DW_TAG_base_type:
Greg Clayton526e5af2010-11-13 03:52:47 +00003067 resolve_state = Type::eResolveStateFull;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003068 clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
3069 encoding,
3070 byte_size * 8);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003071 break;
3072
Greg Clayton526e5af2010-11-13 03:52:47 +00003073 case DW_TAG_pointer_type: encoding_data_type = Type::eEncodingIsPointerUID; break;
3074 case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID; break;
3075 case DW_TAG_typedef: encoding_data_type = Type::eEncodingIsTypedefUID; break;
3076 case DW_TAG_const_type: encoding_data_type = Type::eEncodingIsConstUID; break;
3077 case DW_TAG_restrict_type: encoding_data_type = Type::eEncodingIsRestrictUID; break;
3078 case DW_TAG_volatile_type: encoding_data_type = Type::eEncodingIsVolatileUID; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003079 }
3080
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003081 if (type_name_cstr != NULL && sc.comp_unit != NULL &&
3082 (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
3083 {
3084 static ConstString g_objc_type_name_id("id");
3085 static ConstString g_objc_type_name_Class("Class");
3086 static ConstString g_objc_type_name_selector("SEL");
3087
Greg Clayton24739922010-10-13 03:15:28 +00003088 if (type_name_const_str == g_objc_type_name_id)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003089 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003090 clang_type = ast.GetBuiltInType_objc_id();
Greg Clayton526e5af2010-11-13 03:52:47 +00003091 resolve_state = Type::eResolveStateFull;
3092
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003093 }
Greg Clayton24739922010-10-13 03:15:28 +00003094 else if (type_name_const_str == g_objc_type_name_Class)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003095 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003096 clang_type = ast.GetBuiltInType_objc_Class();
Greg Clayton526e5af2010-11-13 03:52:47 +00003097 resolve_state = Type::eResolveStateFull;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003098 }
Greg Clayton24739922010-10-13 03:15:28 +00003099 else if (type_name_const_str == g_objc_type_name_selector)
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003100 {
Sean Callananf6c73082010-12-06 23:53:20 +00003101 clang_type = ast.GetBuiltInType_objc_selector();
Greg Clayton526e5af2010-11-13 03:52:47 +00003102 resolve_state = Type::eResolveStateFull;
Greg Claytonb0b9fe62010-08-03 00:35:52 +00003103 }
3104 }
3105
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003106 type_sp.reset( new Type (die->GetOffset(),
3107 this,
3108 type_name_const_str,
3109 byte_size,
3110 NULL,
3111 encoding_uid,
3112 encoding_data_type,
3113 &decl,
3114 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00003115 resolve_state));
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003116
Greg Clayton594e5ed2010-09-27 21:07:38 +00003117 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003118
3119// Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
3120// if (encoding_type != NULL)
3121// {
3122// if (encoding_type != DIE_IS_BEING_PARSED)
3123// type_sp->SetEncodingType(encoding_type);
3124// else
3125// m_indirect_fixups.push_back(type_sp.get());
3126// }
3127 }
3128 break;
3129
3130 case DW_TAG_structure_type:
3131 case DW_TAG_union_type:
3132 case DW_TAG_class_type:
3133 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003134 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003135 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003136
Greg Clayton9e409562010-07-28 02:04:09 +00003137 LanguageType class_language = eLanguageTypeUnknown;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003138 //bool struct_is_class = false;
Greg Claytond88d7592010-09-15 08:33:30 +00003139 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003140 if (num_attributes > 0)
3141 {
3142 uint32_t i;
3143 for (i=0; i<num_attributes; ++i)
3144 {
3145 attr = attributes.AttributeAtIndex(i);
3146 DWARFFormValue form_value;
3147 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3148 {
3149 switch (attr)
3150 {
Greg Clayton9e409562010-07-28 02:04:09 +00003151 case DW_AT_decl_file:
3152 decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
3153 break;
3154
3155 case DW_AT_decl_line:
3156 decl.SetLine(form_value.Unsigned());
3157 break;
3158
3159 case DW_AT_decl_column:
3160 decl.SetColumn(form_value.Unsigned());
3161 break;
3162
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003163 case DW_AT_name:
3164 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003165 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003166 break;
Greg Clayton9e409562010-07-28 02:04:09 +00003167
3168 case DW_AT_byte_size:
3169 byte_size = form_value.Unsigned();
Greg Clayton36909642011-03-15 04:38:20 +00003170 byte_size_valid = true;
Greg Clayton9e409562010-07-28 02:04:09 +00003171 break;
3172
3173 case DW_AT_accessibility:
3174 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3175 break;
3176
3177 case DW_AT_declaration:
Greg Clayton7a345282010-11-09 23:46:37 +00003178 is_forward_declaration = form_value.Unsigned() != 0;
Greg Clayton9e409562010-07-28 02:04:09 +00003179 break;
3180
3181 case DW_AT_APPLE_runtime_class:
3182 class_language = (LanguageType)form_value.Signed();
3183 break;
3184
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003185 case DW_AT_allocated:
3186 case DW_AT_associated:
3187 case DW_AT_data_location:
3188 case DW_AT_description:
3189 case DW_AT_start_scope:
3190 case DW_AT_visibility:
3191 default:
3192 case DW_AT_sibling:
3193 break;
3194 }
3195 }
3196 }
3197 }
3198
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003199 UniqueDWARFASTType unique_ast_entry;
3200 if (decl.IsValid())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003201 {
Greg Claytone576ab22011-02-15 00:19:15 +00003202 if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
Greg Clayton36909642011-03-15 04:38:20 +00003203 this,
3204 dwarf_cu,
Greg Claytone576ab22011-02-15 00:19:15 +00003205 die,
3206 decl,
Greg Clayton36909642011-03-15 04:38:20 +00003207 byte_size_valid ? byte_size : -1,
Greg Claytone576ab22011-02-15 00:19:15 +00003208 unique_ast_entry))
Greg Claytonc615ce42010-11-09 04:42:43 +00003209 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003210 // We have already parsed this type or from another
3211 // compile unit. GCC loves to use the "one definition
3212 // rule" which can result in multiple definitions
3213 // of the same class over and over in each compile
3214 // unit.
3215 type_sp = unique_ast_entry.m_type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00003216 if (type_sp)
3217 {
Greg Clayton4272cc72011-02-02 02:24:04 +00003218 m_die_to_type[die] = type_sp.get();
3219 return type_sp;
3220 }
3221 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003222 }
3223
3224 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3225
3226 int tag_decl_kind = -1;
3227 AccessType default_accessibility = eAccessNone;
3228 if (tag == DW_TAG_structure_type)
3229 {
3230 tag_decl_kind = clang::TTK_Struct;
3231 default_accessibility = eAccessPublic;
3232 }
3233 else if (tag == DW_TAG_union_type)
3234 {
3235 tag_decl_kind = clang::TTK_Union;
3236 default_accessibility = eAccessPublic;
3237 }
3238 else if (tag == DW_TAG_class_type)
3239 {
3240 tag_decl_kind = clang::TTK_Class;
3241 default_accessibility = eAccessPrivate;
3242 }
3243
3244
3245 if (is_forward_declaration)
3246 {
3247 // We have a forward declaration to a type and we need
3248 // to try and find a full declaration. We look in the
3249 // current type index just in case we have a forward
3250 // declaration followed by an actual declarations in the
3251 // DWARF. If this fails, we need to look elsewhere...
3252
3253 type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
3254
3255 if (!type_sp && m_debug_map_symfile)
Greg Clayton4272cc72011-02-02 02:24:04 +00003256 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003257 // We weren't able to find a full declaration in
3258 // this DWARF, see if we have a declaration anywhere
3259 // else...
3260 type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
Greg Clayton4272cc72011-02-02 02:24:04 +00003261 }
3262
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003263 if (type_sp)
Greg Clayton4272cc72011-02-02 02:24:04 +00003264 {
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003265 // We found a real definition for this type elsewhere
3266 // so lets use it and cache the fact that we found
3267 // a complete type for this die
3268 m_die_to_type[die] = type_sp.get();
3269 return type_sp;
Greg Clayton4272cc72011-02-02 02:24:04 +00003270 }
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003271 }
3272 assert (tag_decl_kind != -1);
3273 bool clang_type_was_created = false;
3274 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3275 if (clang_type == NULL)
3276 {
3277 clang_type_was_created = true;
3278 clang_type = ast.CreateRecordType (type_name_cstr,
3279 tag_decl_kind,
3280 GetClangDeclContextForDIE (dwarf_cu, die),
3281 class_language);
3282 }
3283
3284 // Store a forward declaration to this class type in case any
3285 // parameters in any class methods need it for the clang
Greg Claytona2721472011-06-25 00:44:06 +00003286 // types for function prototypes.
3287 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003288 type_sp.reset (new Type (die->GetOffset(),
3289 this,
3290 type_name_const_str,
3291 byte_size,
3292 NULL,
3293 LLDB_INVALID_UID,
3294 Type::eEncodingIsUID,
3295 &decl,
3296 clang_type,
3297 Type::eResolveStateForward));
3298
3299
3300 // Add our type to the unique type map so we don't
3301 // end up creating many copies of the same type over
3302 // and over in the ASTContext for our module
3303 unique_ast_entry.m_type_sp = type_sp;
Greg Clayton36909642011-03-15 04:38:20 +00003304 unique_ast_entry.m_symfile = this;
3305 unique_ast_entry.m_cu = dwarf_cu;
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003306 unique_ast_entry.m_die = die;
3307 unique_ast_entry.m_declaration = decl;
Greg Claytone576ab22011-02-15 00:19:15 +00003308 GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
3309 unique_ast_entry);
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003310
3311 if (die->HasChildren() == false && is_forward_declaration == false)
3312 {
3313 // No children for this struct/union/class, lets finish it
3314 ast.StartTagDeclarationDefinition (clang_type);
3315 ast.CompleteTagDeclarationDefinition (clang_type);
3316 }
3317 else if (clang_type_was_created)
3318 {
3319 // Leave this as a forward declaration until we need
3320 // to know the details of the type. lldb_private::Type
3321 // will automatically call the SymbolFile virtual function
3322 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3323 // When the definition needs to be defined.
3324 m_forward_decl_die_to_clang_type[die] = clang_type;
3325 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
3326 ClangASTContext::SetHasExternalStorage (clang_type, true);
Greg Claytonc615ce42010-11-09 04:42:43 +00003327 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003328 }
3329 break;
3330
3331 case DW_TAG_enumeration_type:
3332 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003333 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003334 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003335
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003336 lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003337
Greg Claytond88d7592010-09-15 08:33:30 +00003338 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003339 if (num_attributes > 0)
3340 {
3341 uint32_t i;
3342
3343 for (i=0; i<num_attributes; ++i)
3344 {
3345 attr = attributes.AttributeAtIndex(i);
3346 DWARFFormValue form_value;
3347 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3348 {
3349 switch (attr)
3350 {
Greg Clayton7a345282010-11-09 23:46:37 +00003351 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3352 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3353 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003354 case DW_AT_name:
3355 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003356 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003357 break;
Greg Clayton7a345282010-11-09 23:46:37 +00003358 case DW_AT_type: encoding_uid = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00003359 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Greg Clayton7a345282010-11-09 23:46:37 +00003360 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3361 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003362 case DW_AT_allocated:
3363 case DW_AT_associated:
3364 case DW_AT_bit_stride:
3365 case DW_AT_byte_stride:
3366 case DW_AT_data_location:
3367 case DW_AT_description:
3368 case DW_AT_start_scope:
3369 case DW_AT_visibility:
3370 case DW_AT_specification:
3371 case DW_AT_abstract_origin:
3372 case DW_AT_sibling:
3373 break;
3374 }
3375 }
3376 }
3377
Greg Claytonc93237c2010-10-01 20:48:32 +00003378 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3379
Greg Clayton1be10fc2010-09-29 01:12:09 +00003380 clang_type_t enumerator_clang_type = NULL;
3381 clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3382 if (clang_type == NULL)
3383 {
Greg Clayton4cd17802011-01-25 06:17:32 +00003384 if (die->GetOffset() == 0x1c436)
3385 printf("REMOVE THIS!!!\n");
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003386 enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
3387 DW_ATE_signed,
3388 byte_size * 8);
Greg Claytonca512b32011-01-14 04:54:56 +00003389 clang_type = ast.CreateEnumerationType (type_name_cstr,
Greg Clayton6beaaa62011-01-17 03:46:26 +00003390 GetClangDeclContextForDIE (dwarf_cu, die),
Greg Claytonca512b32011-01-14 04:54:56 +00003391 decl,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003392 enumerator_clang_type);
Greg Clayton1be10fc2010-09-29 01:12:09 +00003393 }
3394 else
3395 {
3396 enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
3397 assert (enumerator_clang_type != NULL);
3398 }
3399
Greg Claytona2721472011-06-25 00:44:06 +00003400 LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
3401
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003402 type_sp.reset( new Type (die->GetOffset(),
3403 this,
3404 type_name_const_str,
3405 byte_size,
3406 NULL,
3407 encoding_uid,
3408 Type::eEncodingIsUID,
3409 &decl,
3410 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00003411 Type::eResolveStateForward));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003412
Greg Clayton6beaaa62011-01-17 03:46:26 +00003413#if LEAVE_ENUMS_FORWARD_DECLARED
Greg Clayton1be10fc2010-09-29 01:12:09 +00003414 // Leave this as a forward declaration until we need
3415 // to know the details of the type. lldb_private::Type
3416 // will automatically call the SymbolFile virtual function
3417 // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3418 // When the definition needs to be defined.
3419 m_forward_decl_die_to_clang_type[die] = clang_type;
Greg Claytonc93237c2010-10-01 20:48:32 +00003420 m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
Greg Clayton6beaaa62011-01-17 03:46:26 +00003421 ClangASTContext::SetHasExternalStorage (clang_type, true);
3422#else
3423 ast.StartTagDeclarationDefinition (clang_type);
3424 if (die->HasChildren())
3425 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00003426 SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
3427 ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
Greg Clayton6beaaa62011-01-17 03:46:26 +00003428 }
3429 ast.CompleteTagDeclarationDefinition (clang_type);
3430#endif
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003431 }
3432 }
3433 break;
3434
Jim Inghamb0be4422010-08-12 01:20:14 +00003435 case DW_TAG_inlined_subroutine:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003436 case DW_TAG_subprogram:
3437 case DW_TAG_subroutine_type:
3438 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003439 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003440 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003441
3442 const char *mangled = NULL;
3443 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
Greg Claytona51ed9b2010-09-23 01:09:21 +00003444 bool is_variadic = false;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003445 bool is_inline = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00003446 bool is_static = false;
3447 bool is_virtual = false;
Greg Claytonf51de672010-10-01 02:31:07 +00003448 bool is_explicit = false;
Greg Clayton0fffff52010-09-24 05:15:53 +00003449
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003450 unsigned type_quals = 0;
Sean Callanane2ef6e32010-09-23 03:01:22 +00003451 clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003452
3453
Greg Claytond88d7592010-09-15 08:33:30 +00003454 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003455 if (num_attributes > 0)
3456 {
3457 uint32_t i;
3458 for (i=0; i<num_attributes; ++i)
3459 {
Greg Clayton1a65ae12011-01-25 23:55:37 +00003460 attr = attributes.AttributeAtIndex(i);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003461 DWARFFormValue form_value;
3462 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3463 {
3464 switch (attr)
3465 {
3466 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3467 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3468 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3469 case DW_AT_name:
3470 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003471 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003472 break;
3473
3474 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
3475 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00003476 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00003477 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Greg Clayton0fffff52010-09-24 05:15:53 +00003478 case DW_AT_inline: is_inline = form_value.Unsigned() != 0; break;
3479 case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
Greg Claytonf51de672010-10-01 02:31:07 +00003480 case DW_AT_explicit: is_explicit = form_value.Unsigned() != 0; break;
3481
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003482 case DW_AT_external:
3483 if (form_value.Unsigned())
3484 {
Sean Callanane2ef6e32010-09-23 03:01:22 +00003485 if (storage == clang::SC_None)
3486 storage = clang::SC_Extern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003487 else
Sean Callanane2ef6e32010-09-23 03:01:22 +00003488 storage = clang::SC_PrivateExtern;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003489 }
3490 break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003491
3492 case DW_AT_allocated:
3493 case DW_AT_associated:
3494 case DW_AT_address_class:
3495 case DW_AT_artificial:
3496 case DW_AT_calling_convention:
3497 case DW_AT_data_location:
3498 case DW_AT_elemental:
3499 case DW_AT_entry_pc:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003500 case DW_AT_frame_base:
3501 case DW_AT_high_pc:
3502 case DW_AT_low_pc:
3503 case DW_AT_object_pointer:
3504 case DW_AT_prototyped:
3505 case DW_AT_pure:
3506 case DW_AT_ranges:
3507 case DW_AT_recursive:
3508 case DW_AT_return_addr:
3509 case DW_AT_segment:
3510 case DW_AT_specification:
3511 case DW_AT_start_scope:
3512 case DW_AT_static_link:
3513 case DW_AT_trampoline:
3514 case DW_AT_visibility:
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003515 case DW_AT_vtable_elem_location:
3516 case DW_AT_abstract_origin:
3517 case DW_AT_description:
3518 case DW_AT_sibling:
3519 break;
3520 }
3521 }
3522 }
Greg Clayton24739922010-10-13 03:15:28 +00003523 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003524
Greg Clayton24739922010-10-13 03:15:28 +00003525 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
Greg Claytonc93237c2010-10-01 20:48:32 +00003526
Greg Clayton24739922010-10-13 03:15:28 +00003527 clang_type_t return_clang_type = NULL;
3528 Type *func_type = NULL;
3529
3530 if (type_die_offset != DW_INVALID_OFFSET)
3531 func_type = ResolveTypeUID(type_die_offset);
Greg Claytonf51de672010-10-01 02:31:07 +00003532
Greg Clayton24739922010-10-13 03:15:28 +00003533 if (func_type)
Greg Clayton526e5af2010-11-13 03:52:47 +00003534 return_clang_type = func_type->GetClangLayoutType();
Greg Clayton24739922010-10-13 03:15:28 +00003535 else
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003536 return_clang_type = ast.GetBuiltInType_void();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003537
Greg Claytonf51de672010-10-01 02:31:07 +00003538
Greg Clayton24739922010-10-13 03:15:28 +00003539 std::vector<clang_type_t> function_param_types;
3540 std::vector<clang::ParmVarDecl*> function_param_decls;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003541
Greg Clayton24739922010-10-13 03:15:28 +00003542 // Parse the function children for the parameters
3543 if (die->HasChildren())
3544 {
Greg Clayton0fffff52010-09-24 05:15:53 +00003545 bool skip_artificial = true;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003546 ParseChildParameters (sc,
3547 type_sp,
3548 dwarf_cu,
3549 die,
3550 skip_artificial,
3551 type_list,
3552 function_param_types,
Greg Clayton7fedea22010-11-16 02:10:54 +00003553 function_param_decls,
3554 type_quals);
Greg Clayton24739922010-10-13 03:15:28 +00003555 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003556
Greg Clayton24739922010-10-13 03:15:28 +00003557 // clang_type will get the function prototype clang type after this call
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003558 clang_type = ast.CreateFunctionType (return_clang_type,
3559 &function_param_types[0],
3560 function_param_types.size(),
3561 is_variadic,
3562 type_quals);
3563
Greg Clayton24739922010-10-13 03:15:28 +00003564 if (type_name_cstr)
3565 {
3566 bool type_handled = false;
3567 const DWARFDebugInfoEntry *parent_die = die->GetParent();
3568 if (tag == DW_TAG_subprogram)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003569 {
Greg Clayton24739922010-10-13 03:15:28 +00003570 if (type_name_cstr[1] == '[' && (type_name_cstr[0] == '-' || type_name_cstr[0] == '+'))
Greg Clayton0fffff52010-09-24 05:15:53 +00003571 {
Greg Clayton24739922010-10-13 03:15:28 +00003572 // We need to find the DW_TAG_class_type or
3573 // DW_TAG_struct_type by name so we can add this
3574 // as a member function of the class.
3575 const char *class_name_start = type_name_cstr + 2;
3576 const char *class_name_end = ::strchr (class_name_start, ' ');
3577 SymbolContext empty_sc;
3578 clang_type_t class_opaque_type = NULL;
3579 if (class_name_start < class_name_end)
Greg Clayton0fffff52010-09-24 05:15:53 +00003580 {
Greg Clayton24739922010-10-13 03:15:28 +00003581 ConstString class_name (class_name_start, class_name_end - class_name_start);
3582 TypeList types;
3583 const uint32_t match_count = FindTypes (empty_sc, class_name, true, UINT32_MAX, types);
3584 if (match_count > 0)
Greg Clayton0fffff52010-09-24 05:15:53 +00003585 {
Greg Clayton24739922010-10-13 03:15:28 +00003586 for (uint32_t i=0; i<match_count; ++i)
Greg Clayton0fffff52010-09-24 05:15:53 +00003587 {
Greg Clayton24739922010-10-13 03:15:28 +00003588 Type *type = types.GetTypeAtIndex (i).get();
3589 clang_type_t type_clang_forward_type = type->GetClangForwardType();
3590 if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
Greg Clayton0fffff52010-09-24 05:15:53 +00003591 {
Greg Clayton24739922010-10-13 03:15:28 +00003592 class_opaque_type = type_clang_forward_type;
3593 break;
Greg Clayton0fffff52010-09-24 05:15:53 +00003594 }
3595 }
3596 }
Greg Clayton24739922010-10-13 03:15:28 +00003597 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003598
Greg Clayton24739922010-10-13 03:15:28 +00003599 if (class_opaque_type)
3600 {
3601 // If accessibility isn't set to anything valid, assume public for
3602 // now...
3603 if (accessibility == eAccessNone)
3604 accessibility = eAccessPublic;
3605
3606 clang::ObjCMethodDecl *objc_method_decl;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003607 objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
3608 type_name_cstr,
3609 clang_type,
3610 accessibility);
Greg Clayton24739922010-10-13 03:15:28 +00003611 type_handled = objc_method_decl != NULL;
3612 }
3613 }
3614 else if (parent_die->Tag() == DW_TAG_class_type ||
3615 parent_die->Tag() == DW_TAG_structure_type)
3616 {
3617 // Look at the parent of this DIE and see if is is
3618 // a class or struct and see if this is actually a
3619 // C++ method
3620 Type *class_type = ResolveType (dwarf_cu, parent_die);
3621 if (class_type)
3622 {
3623 clang_type_t class_opaque_type = class_type->GetClangForwardType();
3624 if (ClangASTContext::IsCXXClassType (class_opaque_type))
Greg Clayton0fffff52010-09-24 05:15:53 +00003625 {
Greg Clayton24739922010-10-13 03:15:28 +00003626 // Neither GCC 4.2 nor clang++ currently set a valid accessibility
3627 // in the DWARF for C++ methods... Default to public for now...
Greg Clayton6d01ad92010-09-29 01:57:37 +00003628 if (accessibility == eAccessNone)
3629 accessibility = eAccessPublic;
Greg Clayton931180e2011-01-27 06:44:37 +00003630
3631 if (!is_static && !die->HasChildren())
3632 {
3633 // We have a C++ member function with no children (this pointer!)
3634 // and clang will get mad if we try and make a function that isn't
3635 // well formed in the DWARF, so we will just skip it...
3636 type_handled = true;
3637 }
3638 else
3639 {
3640 clang::CXXMethodDecl *cxx_method_decl;
3641 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
3642 type_name_cstr,
3643 clang_type,
3644 accessibility,
3645 is_virtual,
3646 is_static,
3647 is_inline,
3648 is_explicit);
3649 type_handled = cxx_method_decl != NULL;
3650 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003651 }
3652 }
Greg Clayton0fffff52010-09-24 05:15:53 +00003653 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003654 }
Greg Clayton24739922010-10-13 03:15:28 +00003655
3656 if (!type_handled)
3657 {
3658 // We just have a function that isn't part of a class
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003659 clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (type_name_cstr,
3660 clang_type,
3661 storage,
3662 is_inline);
Greg Clayton24739922010-10-13 03:15:28 +00003663
3664 // Add the decl to our DIE to decl context map
3665 assert (function_decl);
Greg Claytona2721472011-06-25 00:44:06 +00003666 LinkDeclContextToDIE(function_decl, die);
Greg Clayton24739922010-10-13 03:15:28 +00003667 if (!function_param_decls.empty())
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003668 ast.SetFunctionParameters (function_decl,
3669 &function_param_decls.front(),
3670 function_param_decls.size());
Greg Clayton24739922010-10-13 03:15:28 +00003671 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003672 }
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003673 type_sp.reset( new Type (die->GetOffset(),
3674 this,
3675 type_name_const_str,
3676 0,
3677 NULL,
3678 LLDB_INVALID_UID,
3679 Type::eEncodingIsUID,
3680 &decl,
3681 clang_type,
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003682 Type::eResolveStateFull));
Greg Clayton24739922010-10-13 03:15:28 +00003683 assert(type_sp.get());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003684 }
3685 break;
3686
3687 case DW_TAG_array_type:
3688 {
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003689 // Set a bit that lets us know that we are currently parsing this
Greg Clayton594e5ed2010-09-27 21:07:38 +00003690 m_die_to_type[die] = DIE_IS_BEING_PARSED;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003691
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003692 lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003693 int64_t first_index = 0;
3694 uint32_t byte_stride = 0;
3695 uint32_t bit_stride = 0;
Greg Claytond88d7592010-09-15 08:33:30 +00003696 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003697
3698 if (num_attributes > 0)
3699 {
3700 uint32_t i;
3701 for (i=0; i<num_attributes; ++i)
3702 {
3703 attr = attributes.AttributeAtIndex(i);
3704 DWARFFormValue form_value;
3705 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3706 {
3707 switch (attr)
3708 {
3709 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3710 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
3711 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3712 case DW_AT_name:
3713 type_name_cstr = form_value.AsCString(&get_debug_str_data());
Greg Clayton24739922010-10-13 03:15:28 +00003714 type_name_const_str.SetCString(type_name_cstr);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003715 break;
3716
3717 case DW_AT_type: type_die_offset = form_value.Reference(dwarf_cu); break;
Greg Clayton36909642011-03-15 04:38:20 +00003718 case DW_AT_byte_size: byte_size = form_value.Unsigned(); byte_size_valid = true; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003719 case DW_AT_byte_stride: byte_stride = form_value.Unsigned(); break;
3720 case DW_AT_bit_stride: bit_stride = form_value.Unsigned(); break;
Greg Clayton8cf05932010-07-22 18:30:50 +00003721 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Greg Clayton7a345282010-11-09 23:46:37 +00003722 case DW_AT_declaration: is_forward_declaration = form_value.Unsigned() != 0; break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003723 case DW_AT_allocated:
3724 case DW_AT_associated:
3725 case DW_AT_data_location:
3726 case DW_AT_description:
3727 case DW_AT_ordering:
3728 case DW_AT_start_scope:
3729 case DW_AT_visibility:
3730 case DW_AT_specification:
3731 case DW_AT_abstract_origin:
3732 case DW_AT_sibling:
3733 break;
3734 }
3735 }
3736 }
3737
Greg Claytonc93237c2010-10-01 20:48:32 +00003738 DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3739
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003740 Type *element_type = ResolveTypeUID(type_die_offset);
3741
3742 if (element_type)
3743 {
3744 std::vector<uint64_t> element_orders;
3745 ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
Greg Claytona134cc12010-09-13 02:37:44 +00003746 // We have an array that claims to have no members, lets give it at least one member...
3747 if (element_orders.empty())
3748 element_orders.push_back (1);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003749 if (byte_stride == 0 && bit_stride == 0)
3750 byte_stride = element_type->GetByteSize();
Greg Claytonf4ecaa52011-02-16 23:00:21 +00003751 clang_type_t array_element_type = element_type->GetClangFullType();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003752 uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
3753 uint64_t num_elements = 0;
3754 std::vector<uint64_t>::const_reverse_iterator pos;
3755 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
3756 for (pos = element_orders.rbegin(); pos != end; ++pos)
3757 {
3758 num_elements = *pos;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003759 clang_type = ast.CreateArrayType (array_element_type,
3760 num_elements,
3761 num_elements * array_element_bit_stride);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003762 array_element_type = clang_type;
3763 array_element_bit_stride = array_element_bit_stride * num_elements;
3764 }
3765 ConstString empty_name;
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003766 type_sp.reset( new Type (die->GetOffset(),
3767 this,
3768 empty_name,
3769 array_element_bit_stride / 8,
3770 NULL,
Greg Clayton526e5af2010-11-13 03:52:47 +00003771 type_die_offset,
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003772 Type::eEncodingIsUID,
3773 &decl,
3774 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00003775 Type::eResolveStateFull));
3776 type_sp->SetEncodingType (element_type);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003777 }
3778 }
3779 }
3780 break;
3781
Greg Clayton9b81a312010-06-12 01:20:30 +00003782 case DW_TAG_ptr_to_member_type:
3783 {
3784 dw_offset_t type_die_offset = DW_INVALID_OFFSET;
3785 dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
3786
Greg Claytond88d7592010-09-15 08:33:30 +00003787 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Greg Clayton9b81a312010-06-12 01:20:30 +00003788
3789 if (num_attributes > 0) {
3790 uint32_t i;
3791 for (i=0; i<num_attributes; ++i)
3792 {
3793 attr = attributes.AttributeAtIndex(i);
3794 DWARFFormValue form_value;
3795 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3796 {
3797 switch (attr)
3798 {
3799 case DW_AT_type:
3800 type_die_offset = form_value.Reference(dwarf_cu); break;
3801 case DW_AT_containing_type:
3802 containing_type_die_offset = form_value.Reference(dwarf_cu); break;
3803 }
3804 }
3805 }
3806
3807 Type *pointee_type = ResolveTypeUID(type_die_offset);
3808 Type *class_type = ResolveTypeUID(containing_type_die_offset);
3809
Greg Clayton526e5af2010-11-13 03:52:47 +00003810 clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
3811 clang_type_t class_clang_type = class_type->GetClangLayoutType();
Greg Clayton9b81a312010-06-12 01:20:30 +00003812
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003813 clang_type = ast.CreateMemberPointerType(pointee_clang_type,
3814 class_clang_type);
Greg Clayton9b81a312010-06-12 01:20:30 +00003815
Greg Clayton526e5af2010-11-13 03:52:47 +00003816 byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
3817 clang_type) / 8;
Greg Clayton9b81a312010-06-12 01:20:30 +00003818
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003819 type_sp.reset( new Type (die->GetOffset(),
3820 this,
3821 type_name_const_str,
3822 byte_size,
3823 NULL,
3824 LLDB_INVALID_UID,
3825 Type::eEncodingIsUID,
3826 NULL,
3827 clang_type,
Greg Clayton526e5af2010-11-13 03:52:47 +00003828 Type::eResolveStateForward));
Greg Clayton9b81a312010-06-12 01:20:30 +00003829 }
3830
3831 break;
3832 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003833 default:
Greg Clayton9b81a312010-06-12 01:20:30 +00003834 assert(false && "Unhandled type tag!");
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003835 break;
3836 }
3837
3838 if (type_sp.get())
3839 {
3840 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
3841 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
3842
3843 SymbolContextScope * symbol_context_scope = NULL;
3844 if (sc_parent_tag == DW_TAG_compile_unit)
3845 {
3846 symbol_context_scope = sc.comp_unit;
3847 }
3848 else if (sc.function != NULL)
3849 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003850 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003851 if (symbol_context_scope == NULL)
3852 symbol_context_scope = sc.function;
3853 }
3854
3855 if (symbol_context_scope != NULL)
3856 {
3857 type_sp->SetSymbolContextScope(symbol_context_scope);
3858 }
3859
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003860 // We are ready to put this type into the uniqued list up at the module level
3861 type_list->Insert (type_sp);
Greg Clayton450e3f32010-10-12 02:24:53 +00003862
Greg Clayton1c9e5ac2011-02-09 19:06:17 +00003863 m_die_to_type[die] = type_sp.get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003864 }
3865 }
Greg Clayton594e5ed2010-09-27 21:07:38 +00003866 else if (type_ptr != DIE_IS_BEING_PARSED)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003867 {
Greg Clayton2d95dc9b2010-11-10 04:57:04 +00003868 type_sp = type_list->FindType(type_ptr->GetID());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003869 }
3870 }
3871 return type_sp;
3872}
3873
3874size_t
Greg Clayton1be10fc2010-09-29 01:12:09 +00003875SymbolFileDWARF::ParseTypes
3876(
3877 const SymbolContext& sc,
3878 DWARFCompileUnit* dwarf_cu,
3879 const DWARFDebugInfoEntry *die,
3880 bool parse_siblings,
3881 bool parse_children
3882)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003883{
3884 size_t types_added = 0;
3885 while (die != NULL)
3886 {
3887 bool type_is_new = false;
Greg Clayton1be10fc2010-09-29 01:12:09 +00003888 if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003889 {
3890 if (type_is_new)
3891 ++types_added;
3892 }
3893
3894 if (parse_children && die->HasChildren())
3895 {
3896 if (die->Tag() == DW_TAG_subprogram)
3897 {
3898 SymbolContext child_sc(sc);
3899 child_sc.function = sc.comp_unit->FindFunctionByUID(die->GetOffset()).get();
3900 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
3901 }
3902 else
3903 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
3904 }
3905
3906 if (parse_siblings)
3907 die = die->GetSibling();
3908 else
3909 die = NULL;
3910 }
3911 return types_added;
3912}
3913
3914
3915size_t
3916SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
3917{
3918 assert(sc.comp_unit && sc.function);
3919 size_t functions_added = 0;
3920 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
3921 if (dwarf_cu)
3922 {
3923 dw_offset_t function_die_offset = sc.function->GetID();
3924 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
3925 if (function_die)
3926 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00003927 ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, false, true);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003928 }
3929 }
3930
3931 return functions_added;
3932}
3933
3934
3935size_t
3936SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
3937{
3938 // At least a compile unit must be valid
3939 assert(sc.comp_unit);
3940 size_t types_added = 0;
3941 DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
3942 if (dwarf_cu)
3943 {
3944 if (sc.function)
3945 {
3946 dw_offset_t function_die_offset = sc.function->GetID();
3947 const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
3948 if (func_die && func_die->HasChildren())
3949 {
3950 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
3951 }
3952 }
3953 else
3954 {
3955 const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
3956 if (dwarf_cu_die && dwarf_cu_die->HasChildren())
3957 {
3958 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
3959 }
3960 }
3961 }
3962
3963 return types_added;
3964}
3965
3966size_t
3967SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
3968{
3969 if (sc.comp_unit != NULL)
3970 {
Greg Clayton4b3dc102010-11-01 20:32:12 +00003971 DWARFDebugInfo* info = DebugInfo();
3972 if (info == NULL)
3973 return 0;
3974
3975 uint32_t cu_idx = UINT32_MAX;
3976 DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003977
3978 if (dwarf_cu == NULL)
3979 return 0;
3980
3981 if (sc.function)
3982 {
3983 const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
Greg Clayton016a95e2010-09-14 02:20:48 +00003984
3985 dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
3986 assert (func_lo_pc != DW_INVALID_ADDRESS);
3987
Greg Claytonc662ec82011-06-17 22:10:16 +00003988 const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
3989
3990 // Let all blocks know they have parse all their variables
3991 sc.function->GetBlock (false).SetDidParseVariables (true, true);
3992
3993 return num_variables;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00003994 }
3995 else if (sc.comp_unit)
3996 {
3997 uint32_t vars_added = 0;
3998 VariableListSP variables (sc.comp_unit->GetVariableList(false));
3999
4000 if (variables.get() == NULL)
4001 {
4002 variables.reset(new VariableList());
4003 sc.comp_unit->SetVariableList(variables);
4004
4005 // Index if we already haven't to make sure the compile units
4006 // get indexed and make their global DIE index list
4007 if (!m_indexed)
4008 Index ();
4009
Greg Claytonc685f8e2010-09-15 04:15:46 +00004010 std::vector<NameToDIE::Info> global_die_info_array;
Greg Clayton4b3dc102010-11-01 20:32:12 +00004011 const size_t num_globals = m_global_index.FindAllEntriesForCompileUnitWithIndex (cu_idx, global_die_info_array);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004012 for (size_t idx=0; idx<num_globals; ++idx)
4013 {
Greg Claytonc685f8e2010-09-15 04:15:46 +00004014 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, dwarf_cu->GetDIEAtIndexUnchecked(global_die_info_array[idx].die_idx), LLDB_INVALID_ADDRESS));
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004015 if (var_sp)
4016 {
Greg Clayton83c5cd92010-11-14 22:13:40 +00004017 variables->AddVariableIfUnique (var_sp);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004018 ++vars_added;
4019 }
4020 }
4021 }
4022 return vars_added;
4023 }
4024 }
4025 return 0;
4026}
4027
4028
4029VariableSP
4030SymbolFileDWARF::ParseVariableDIE
4031(
4032 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004033 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00004034 const DWARFDebugInfoEntry *die,
4035 const lldb::addr_t func_low_pc
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004036)
4037{
4038
Greg Clayton83c5cd92010-11-14 22:13:40 +00004039 VariableSP var_sp (m_die_to_variable_sp[die]);
4040 if (var_sp)
4041 return var_sp; // Already been parsed!
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004042
4043 const dw_tag_t tag = die->Tag();
4044 DWARFDebugInfoEntry::Attributes attributes;
Greg Claytond88d7592010-09-15 08:33:30 +00004045 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004046 if (num_attributes > 0)
4047 {
4048 const char *name = NULL;
Greg Claytona134cc12010-09-13 02:37:44 +00004049 const char *mangled = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004050 Declaration decl;
4051 uint32_t i;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004052 Type *var_type = NULL;
4053 DWARFExpression location;
4054 bool is_external = false;
4055 bool is_artificial = false;
Greg Clayton007d5be2011-05-30 00:49:24 +00004056 bool location_is_const_value_data = false;
Sean Callananc7fbf732010-08-06 00:32:49 +00004057 AccessType accessibility = eAccessNone;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004058
4059 for (i=0; i<num_attributes; ++i)
4060 {
4061 dw_attr_t attr = attributes.AttributeAtIndex(i);
4062 DWARFFormValue form_value;
4063 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4064 {
4065 switch (attr)
4066 {
4067 case DW_AT_decl_file: decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4068 case DW_AT_decl_line: decl.SetLine(form_value.Unsigned()); break;
4069 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4070 case DW_AT_name: name = form_value.AsCString(&get_debug_str_data()); break;
Greg Claytona134cc12010-09-13 02:37:44 +00004071 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
Greg Clayton594e5ed2010-09-27 21:07:38 +00004072 case DW_AT_type: var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004073 case DW_AT_external: is_external = form_value.Unsigned() != 0; break;
Greg Clayton007d5be2011-05-30 00:49:24 +00004074 case DW_AT_const_value:
4075 location_is_const_value_data = true;
4076 // Fall through...
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004077 case DW_AT_location:
4078 {
4079 if (form_value.BlockData())
4080 {
4081 const DataExtractor& debug_info_data = get_debug_info_data();
4082
4083 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4084 uint32_t block_length = form_value.Unsigned();
Greg Clayton016a95e2010-09-14 02:20:48 +00004085 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004086 }
4087 else
4088 {
4089 const DataExtractor& debug_loc_data = get_debug_loc_data();
4090 const dw_offset_t debug_loc_offset = form_value.Unsigned();
4091
4092 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
4093 if (loc_list_length > 0)
4094 {
Greg Clayton016a95e2010-09-14 02:20:48 +00004095 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
4096 assert (func_low_pc != LLDB_INVALID_ADDRESS);
4097 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004098 }
4099 }
4100 }
4101 break;
4102
4103 case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
Greg Clayton8cf05932010-07-22 18:30:50 +00004104 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004105 case DW_AT_declaration:
4106 case DW_AT_description:
4107 case DW_AT_endianity:
4108 case DW_AT_segment:
4109 case DW_AT_start_scope:
4110 case DW_AT_visibility:
4111 default:
4112 case DW_AT_abstract_origin:
4113 case DW_AT_sibling:
4114 case DW_AT_specification:
4115 break;
4116 }
4117 }
4118 }
4119
4120 if (location.IsValid())
4121 {
4122 assert(var_type != DIE_IS_BEING_PARSED);
4123
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004124 ValueType scope = eValueTypeInvalid;
4125
4126 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4127 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4128
4129 if (tag == DW_TAG_formal_parameter)
4130 scope = eValueTypeVariableArgument;
4131 else if (is_external || parent_tag == DW_TAG_compile_unit)
4132 scope = eValueTypeVariableGlobal;
4133 else
4134 scope = eValueTypeVariableLocal;
4135
4136 SymbolContextScope * symbol_context_scope = NULL;
4137 if (parent_tag == DW_TAG_compile_unit)
4138 {
4139 symbol_context_scope = sc.comp_unit;
4140 }
4141 else if (sc.function != NULL)
4142 {
Greg Clayton0b76a2c2010-08-21 02:22:51 +00004143 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004144 if (symbol_context_scope == NULL)
4145 symbol_context_scope = sc.function;
4146 }
4147
4148 assert(symbol_context_scope != NULL);
4149 var_sp.reset (new Variable(die->GetOffset(),
Greg Clayton83c5cd92010-11-14 22:13:40 +00004150 name,
4151 mangled,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004152 var_type,
4153 scope,
4154 symbol_context_scope,
4155 &decl,
4156 location,
4157 is_external,
4158 is_artificial));
Greg Clayton594e5ed2010-09-27 21:07:38 +00004159
Greg Clayton007d5be2011-05-30 00:49:24 +00004160 var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004161 }
4162 }
Greg Clayton83c5cd92010-11-14 22:13:40 +00004163 // Cache var_sp even if NULL (the variable was just a specification or
4164 // was missing vital information to be able to be displayed in the debugger
4165 // (missing location due to optimization, etc)) so we don't re-parse
4166 // this DIE over and over later...
4167 m_die_to_variable_sp[die] = var_sp;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004168 return var_sp;
4169}
4170
Greg Claytonc662ec82011-06-17 22:10:16 +00004171
4172const DWARFDebugInfoEntry *
4173SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
4174 dw_offset_t spec_block_die_offset,
4175 DWARFCompileUnit **result_die_cu_handle)
4176{
4177 // Give the concrete function die specified by "func_die_offset", find the
4178 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4179 // to "spec_block_die_offset"
4180 DWARFDebugInfo* info = DebugInfo();
4181
4182 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
4183 if (die)
4184 {
4185 assert (*result_die_cu_handle);
4186 return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
4187 }
4188 return NULL;
4189}
4190
4191
4192const DWARFDebugInfoEntry *
4193SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
4194 const DWARFDebugInfoEntry *die,
4195 dw_offset_t spec_block_die_offset,
4196 DWARFCompileUnit **result_die_cu_handle)
4197{
4198 if (die)
4199 {
4200 switch (die->Tag())
4201 {
4202 case DW_TAG_subprogram:
4203 case DW_TAG_inlined_subroutine:
4204 case DW_TAG_lexical_block:
4205 {
4206 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4207 {
4208 *result_die_cu_handle = dwarf_cu;
4209 return die;
4210 }
4211
4212 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
4213 {
4214 *result_die_cu_handle = dwarf_cu;
4215 return die;
4216 }
4217 }
4218 break;
4219 }
4220
4221 // Give the concrete function die specified by "func_die_offset", find the
4222 // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4223 // to "spec_block_die_offset"
4224 for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
4225 {
4226 const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
4227 child_die,
4228 spec_block_die_offset,
4229 result_die_cu_handle);
4230 if (result_die)
4231 return result_die;
4232 }
4233 }
4234
4235 *result_die_cu_handle = NULL;
4236 return NULL;
4237}
4238
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004239size_t
4240SymbolFileDWARF::ParseVariables
4241(
4242 const SymbolContext& sc,
Greg Clayton0fffff52010-09-24 05:15:53 +00004243 DWARFCompileUnit* dwarf_cu,
Greg Clayton016a95e2010-09-14 02:20:48 +00004244 const lldb::addr_t func_low_pc,
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004245 const DWARFDebugInfoEntry *orig_die,
4246 bool parse_siblings,
4247 bool parse_children,
4248 VariableList* cc_variable_list
4249)
4250{
4251 if (orig_die == NULL)
4252 return 0;
4253
Greg Claytonc662ec82011-06-17 22:10:16 +00004254 VariableListSP variable_list_sp;
4255
4256 size_t vars_added = 0;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004257 const DWARFDebugInfoEntry *die = orig_die;
Greg Claytonc662ec82011-06-17 22:10:16 +00004258 while (die != NULL)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004259 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004260 dw_tag_t tag = die->Tag();
4261
4262 // Check to see if we have already parsed this variable or constant?
4263 if (m_die_to_variable_sp[die])
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004264 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004265 if (cc_variable_list)
4266 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004267 }
4268 else
4269 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004270 // We haven't already parsed it, lets do that now.
4271 if ((tag == DW_TAG_variable) ||
4272 (tag == DW_TAG_constant) ||
4273 (tag == DW_TAG_formal_parameter && sc.function))
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004274 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004275 if (variable_list_sp.get() == NULL)
Greg Clayton73bf5db2011-06-17 01:22:15 +00004276 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004277 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
4278 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4279 switch (parent_tag)
4280 {
4281 case DW_TAG_compile_unit:
4282 if (sc.comp_unit != NULL)
4283 {
4284 variable_list_sp = sc.comp_unit->GetVariableList(false);
4285 if (variable_list_sp.get() == NULL)
4286 {
4287 variable_list_sp.reset(new VariableList());
4288 sc.comp_unit->SetVariableList(variable_list_sp);
4289 }
4290 }
4291 else
4292 {
4293 fprintf (stderr,
4294 "error: parent 0x%8.8x %s with no valid compile unit in symbol context for 0x%8.8x %s.\n",
4295 sc_parent_die->GetOffset(),
4296 DW_TAG_value_to_name (parent_tag),
4297 orig_die->GetOffset(),
4298 DW_TAG_value_to_name (orig_die->Tag()));
4299 }
4300 break;
4301
4302 case DW_TAG_subprogram:
4303 case DW_TAG_inlined_subroutine:
4304 case DW_TAG_lexical_block:
4305 if (sc.function != NULL)
4306 {
4307 // Check to see if we already have parsed the variables for the given scope
4308
4309 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4310 if (block == NULL)
4311 {
4312 // This must be a specification or abstract origin with
4313 // a concrete block couterpart in the current function. We need
4314 // to find the concrete block so we can correctly add the
4315 // variable to it
4316 DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
4317 const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
4318 sc_parent_die->GetOffset(),
4319 &concrete_block_die_cu);
4320 if (concrete_block_die)
4321 block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die->GetOffset());
4322 }
4323
4324 if (block != NULL)
4325 {
4326 const bool can_create = false;
4327 variable_list_sp = block->GetBlockVariableList (can_create);
4328 if (variable_list_sp.get() == NULL)
4329 {
4330 variable_list_sp.reset(new VariableList());
4331 block->SetVariableList(variable_list_sp);
4332 }
4333 }
4334 }
4335 break;
4336
4337 default:
4338 fprintf (stderr,
4339 "error: didn't find appropriate parent DIE for variable list for 0x%8.8x %s.\n",
4340 orig_die->GetOffset(),
4341 DW_TAG_value_to_name (orig_die->Tag()));
4342 break;
4343 }
Greg Clayton73bf5db2011-06-17 01:22:15 +00004344 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004345
4346 if (variable_list_sp)
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004347 {
Greg Clayton73bf5db2011-06-17 01:22:15 +00004348 VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
4349 if (var_sp)
4350 {
Greg Claytonc662ec82011-06-17 22:10:16 +00004351 variable_list_sp->AddVariableIfUnique (var_sp);
Greg Clayton73bf5db2011-06-17 01:22:15 +00004352 if (cc_variable_list)
4353 cc_variable_list->AddVariableIfUnique (var_sp);
4354 ++vars_added;
4355 }
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004356 }
4357 }
4358 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004359
4360 bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4361
4362 if (!skip_children && parse_children && die->HasChildren())
4363 {
4364 vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
4365 }
4366
4367 if (parse_siblings)
4368 die = die->GetSibling();
4369 else
4370 die = NULL;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004371 }
Greg Claytonc662ec82011-06-17 22:10:16 +00004372 return vars_added;
Chris Lattner30fdc8d2010-06-08 16:52:24 +00004373}
4374
4375//------------------------------------------------------------------
4376// PluginInterface protocol
4377//------------------------------------------------------------------
4378const char *
4379SymbolFileDWARF::GetPluginName()
4380{
4381 return "SymbolFileDWARF";
4382}
4383
4384const char *
4385SymbolFileDWARF::GetShortPluginName()
4386{
4387 return GetPluginNameStatic();
4388}
4389
4390uint32_t
4391SymbolFileDWARF::GetPluginVersion()
4392{
4393 return 1;
4394}
4395
4396void
Greg Clayton6beaaa62011-01-17 03:46:26 +00004397SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
4398{
4399 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
4400 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
4401 if (clang_type)
4402 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
4403}
4404
4405void
4406SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
4407{
4408 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
4409 clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
4410 if (clang_type)
4411 symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
4412}
4413
Greg Claytona2721472011-06-25 00:44:06 +00004414void
4415SymbolFileDWARF::SearchNamespace (const clang::NamespaceDecl *namespace_decl,
4416 const char *name,
4417 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
4418{
4419 DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find((const clang::DeclContext*)namespace_decl);
4420
4421 if (iter == m_decl_ctx_to_die.end())
4422 return;
4423
4424 const DWARFDebugInfoEntry *namespace_die = iter->second;
4425
4426 if (!results)
4427 return;
4428
4429 DWARFDebugInfo* info = DebugInfo();
4430
4431 std::vector<NameToDIE::Info> die_info_array;
4432
4433 size_t num_matches = m_type_index.Find (name, die_info_array);
4434
4435 if (num_matches)
4436 {
4437 for (int i = 0;
4438 i < num_matches;
4439 ++i)
4440 {
4441 DWARFCompileUnit* compile_unit = info->GetCompileUnitAtIndex(die_info_array[i].cu_idx);
4442 compile_unit->ExtractDIEsIfNeeded (false);
4443 const DWARFDebugInfoEntry *die = compile_unit->GetDIEAtIndexUnchecked(die_info_array[i].die_idx);
4444
4445 if (die->GetParent() != namespace_die)
4446 continue;
4447
4448 Type *matching_type = ResolveType (compile_unit, die);
4449
4450 lldb::clang_type_t type = matching_type->GetClangFullType();
4451 clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
4452
4453
4454 if (const clang::TagType *tag_type = dyn_cast<clang::TagType>(qual_type.getTypePtr()))
4455 {
4456 clang::TagDecl *tag_decl = tag_type->getDecl();
4457 results->push_back(tag_decl);
4458 }
4459 else if (const clang::TypedefType *typedef_type = dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
4460 {
4461 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4462 results->push_back(typedef_decl);
4463 }
4464 }
4465 }
4466}
4467
4468void
4469SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
4470 const clang::DeclContext *DC,
4471 clang::DeclarationName Name,
4472 llvm::SmallVectorImpl <clang::NamedDecl *> *results)
4473{
4474 SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
4475
4476 const clang::NamespaceDecl *DC_namespace = llvm::dyn_cast<clang::NamespaceDecl>(DC);
4477
4478 symbol_file_dwarf->SearchNamespace (DC_namespace, Name.getAsString().c_str(), results);
4479}