1 //===-- 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"
23 #include "clang/Sema/DeclSpec.h"
24 
25 #include "llvm/Support/Casting.h"
26 
27 #include "lldb/Core/Module.h"
28 #include "lldb/Core/PluginManager.h"
29 #include "lldb/Core/RegularExpression.h"
30 #include "lldb/Core/Scalar.h"
31 #include "lldb/Core/Section.h"
32 #include "lldb/Core/StreamFile.h"
33 #include "lldb/Core/StreamString.h"
34 #include "lldb/Core/Timer.h"
35 #include "lldb/Core/Value.h"
36 
37 #include "lldb/Symbol/Block.h"
38 #include "lldb/Symbol/ClangExternalASTSourceCallbacks.h"
39 #include "lldb/Symbol/CompileUnit.h"
40 #include "lldb/Symbol/LineTable.h"
41 #include "lldb/Symbol/ObjectFile.h"
42 #include "lldb/Symbol/SymbolVendor.h"
43 #include "lldb/Symbol/VariableList.h"
44 
45 #include "lldb/Target/ObjCLanguageRuntime.h"
46 
47 #include "DWARFCompileUnit.h"
48 #include "DWARFDebugAbbrev.h"
49 #include "DWARFDebugAranges.h"
50 #include "DWARFDebugInfo.h"
51 #include "DWARFDebugInfoEntry.h"
52 #include "DWARFDebugLine.h"
53 #include "DWARFDebugPubnames.h"
54 #include "DWARFDebugRanges.h"
55 #include "DWARFDIECollection.h"
56 #include "DWARFFormValue.h"
57 #include "DWARFLocationList.h"
58 #include "LogChannelDWARF.h"
59 #include "SymbolFileDWARFDebugMap.h"
60 
61 #include <map>
62 
63 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
64 
65 #ifdef ENABLE_DEBUG_PRINTF
66 #include <stdio.h>
67 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
68 #else
69 #define DEBUG_PRINTF(fmt, ...)
70 #endif
71 
72 #define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
73 
74 using namespace lldb;
75 using namespace lldb_private;
76 
77 
78 static AccessType
79 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
80 {
81     switch (dwarf_accessibility)
82     {
83         case DW_ACCESS_public:      return eAccessPublic;
84         case DW_ACCESS_private:     return eAccessPrivate;
85         case DW_ACCESS_protected:   return eAccessProtected;
86         default:                    break;
87     }
88     return eAccessNone;
89 }
90 
91 void
92 SymbolFileDWARF::Initialize()
93 {
94     LogChannelDWARF::Initialize();
95     PluginManager::RegisterPlugin (GetPluginNameStatic(),
96                                    GetPluginDescriptionStatic(),
97                                    CreateInstance);
98 }
99 
100 void
101 SymbolFileDWARF::Terminate()
102 {
103     PluginManager::UnregisterPlugin (CreateInstance);
104     LogChannelDWARF::Initialize();
105 }
106 
107 
108 const char *
109 SymbolFileDWARF::GetPluginNameStatic()
110 {
111     return "dwarf";
112 }
113 
114 const char *
115 SymbolFileDWARF::GetPluginDescriptionStatic()
116 {
117     return "DWARF and DWARF3 debug symbol file reader.";
118 }
119 
120 
121 SymbolFile*
122 SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
123 {
124     return new SymbolFileDWARF(obj_file);
125 }
126 
127 TypeList *
128 SymbolFileDWARF::GetTypeList ()
129 {
130     if (m_debug_map_symfile)
131         return m_debug_map_symfile->GetTypeList();
132     return m_obj_file->GetModule()->GetTypeList();
133 
134 }
135 
136 //----------------------------------------------------------------------
137 // Gets the first parent that is a lexical block, function or inlined
138 // subroutine, or compile unit.
139 //----------------------------------------------------------------------
140 static const DWARFDebugInfoEntry *
141 GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
142 {
143     const DWARFDebugInfoEntry *die;
144     for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
145     {
146         dw_tag_t tag = die->Tag();
147 
148         switch (tag)
149         {
150         case DW_TAG_compile_unit:
151         case DW_TAG_subprogram:
152         case DW_TAG_inlined_subroutine:
153         case DW_TAG_lexical_block:
154             return die;
155         }
156     }
157     return NULL;
158 }
159 
160 
161 SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
162     SymbolFile (objfile),
163     m_debug_map_symfile (NULL),
164     m_clang_tu_decl (NULL),
165     m_flags(),
166     m_data_debug_abbrev (),
167     m_data_debug_aranges (),
168     m_data_debug_frame (),
169     m_data_debug_info (),
170     m_data_debug_line (),
171     m_data_debug_loc (),
172     m_data_debug_ranges (),
173     m_data_debug_str (),
174     m_data_apple_names (),
175     m_data_apple_types (),
176     m_abbr(),
177     m_info(),
178     m_line(),
179     m_apple_names (this, m_data_apple_names, true),
180     m_apple_types (this, m_data_apple_types, true),
181     m_function_basename_index(),
182     m_function_fullname_index(),
183     m_function_method_index(),
184     m_function_selector_index(),
185     m_objc_class_selectors_index(),
186     m_global_index(),
187     m_type_index(),
188     m_namespace_index(),
189     m_indexed (false),
190     m_is_external_ast_source (false),
191     m_ranges(),
192     m_unique_ast_type_map ()
193 {
194 }
195 
196 SymbolFileDWARF::~SymbolFileDWARF()
197 {
198     if (m_is_external_ast_source)
199         m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
200 }
201 
202 static const ConstString &
203 GetDWARFMachOSegmentName ()
204 {
205     static ConstString g_dwarf_section_name ("__DWARF");
206     return g_dwarf_section_name;
207 }
208 
209 UniqueDWARFASTTypeMap &
210 SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
211 {
212     if (m_debug_map_symfile)
213         return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
214     return m_unique_ast_type_map;
215 }
216 
217 ClangASTContext &
218 SymbolFileDWARF::GetClangASTContext ()
219 {
220     if (m_debug_map_symfile)
221         return m_debug_map_symfile->GetClangASTContext ();
222 
223     ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
224     if (!m_is_external_ast_source)
225     {
226         m_is_external_ast_source = true;
227         llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
228             new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
229                                                  SymbolFileDWARF::CompleteObjCInterfaceDecl,
230                                                  SymbolFileDWARF::FindExternalVisibleDeclsByName,
231                                                  this));
232 
233         ast.SetExternalSource (ast_source_ap);
234     }
235     return ast;
236 }
237 
238 void
239 SymbolFileDWARF::InitializeObject()
240 {
241     // Install our external AST source callbacks so we can complete Clang types.
242     Module *module = m_obj_file->GetModule();
243     if (module)
244     {
245         const SectionList *section_list = m_obj_file->GetSectionList();
246 
247         const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
248 
249         // Memory map the DWARF mach-o segment so we have everything mmap'ed
250         // to keep our heap memory usage down.
251         if (section)
252             section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
253     }
254     get_apple_names_data();
255     get_apple_types_data();
256     m_apple_names.Initialize();
257     m_apple_types.Initialize();
258 
259 }
260 
261 bool
262 SymbolFileDWARF::SupportedVersion(uint16_t version)
263 {
264     return version == 2 || version == 3;
265 }
266 
267 uint32_t
268 SymbolFileDWARF::GetAbilities ()
269 {
270     uint32_t abilities = 0;
271     if (m_obj_file != NULL)
272     {
273         const Section* section = NULL;
274         const SectionList *section_list = m_obj_file->GetSectionList();
275         if (section_list == NULL)
276             return 0;
277 
278         uint64_t debug_abbrev_file_size = 0;
279         uint64_t debug_aranges_file_size = 0;
280         uint64_t debug_frame_file_size = 0;
281         uint64_t debug_info_file_size = 0;
282         uint64_t debug_line_file_size = 0;
283         uint64_t debug_loc_file_size = 0;
284         uint64_t debug_macinfo_file_size = 0;
285         uint64_t debug_pubnames_file_size = 0;
286         uint64_t debug_pubtypes_file_size = 0;
287         uint64_t debug_ranges_file_size = 0;
288         uint64_t debug_str_file_size = 0;
289 
290         section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
291 
292         if (section)
293             section_list = &section->GetChildren ();
294 
295         section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
296         if (section != NULL)
297         {
298             debug_info_file_size = section->GetByteSize();
299 
300             section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
301             if (section)
302                 debug_abbrev_file_size = section->GetByteSize();
303             else
304                 m_flags.Set (flagsGotDebugAbbrevData);
305 
306             section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
307             if (section)
308                 debug_aranges_file_size = section->GetByteSize();
309             else
310                 m_flags.Set (flagsGotDebugArangesData);
311 
312             section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
313             if (section)
314                 debug_frame_file_size = section->GetByteSize();
315             else
316                 m_flags.Set (flagsGotDebugFrameData);
317 
318             section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
319             if (section)
320                 debug_line_file_size = section->GetByteSize();
321             else
322                 m_flags.Set (flagsGotDebugLineData);
323 
324             section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
325             if (section)
326                 debug_loc_file_size = section->GetByteSize();
327             else
328                 m_flags.Set (flagsGotDebugLocData);
329 
330             section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
331             if (section)
332                 debug_macinfo_file_size = section->GetByteSize();
333             else
334                 m_flags.Set (flagsGotDebugMacInfoData);
335 
336             section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
337             if (section)
338                 debug_pubnames_file_size = section->GetByteSize();
339             else
340                 m_flags.Set (flagsGotDebugPubNamesData);
341 
342             section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
343             if (section)
344                 debug_pubtypes_file_size = section->GetByteSize();
345             else
346                 m_flags.Set (flagsGotDebugPubTypesData);
347 
348             section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
349             if (section)
350                 debug_ranges_file_size = section->GetByteSize();
351             else
352                 m_flags.Set (flagsGotDebugRangesData);
353 
354             section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
355             if (section)
356                 debug_str_file_size = section->GetByteSize();
357             else
358                 m_flags.Set (flagsGotDebugStrData);
359         }
360 
361         if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
362             abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
363 
364         if (debug_line_file_size > 0)
365             abilities |= LineTables;
366 
367         if (debug_aranges_file_size > 0)
368             abilities |= AddressAcceleratorTable;
369 
370         if (debug_pubnames_file_size > 0)
371             abilities |= FunctionAcceleratorTable;
372 
373         if (debug_pubtypes_file_size > 0)
374             abilities |= TypeAcceleratorTable;
375 
376         if (debug_macinfo_file_size > 0)
377             abilities |= MacroInformation;
378 
379         if (debug_frame_file_size > 0)
380             abilities |= CallFrameInformation;
381     }
382     return abilities;
383 }
384 
385 const DataExtractor&
386 SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
387 {
388     if (m_flags.IsClear (got_flag))
389     {
390         m_flags.Set (got_flag);
391         const SectionList *section_list = m_obj_file->GetSectionList();
392         if (section_list)
393         {
394             Section *section = section_list->FindSectionByType(sect_type, true).get();
395             if (section)
396             {
397                 // See if we memory mapped the DWARF segment?
398                 if (m_dwarf_data.GetByteSize())
399                 {
400                     data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
401                 }
402                 else
403                 {
404                     if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
405                         data.Clear();
406                 }
407             }
408         }
409     }
410     return data;
411 }
412 
413 const DataExtractor&
414 SymbolFileDWARF::get_debug_abbrev_data()
415 {
416     return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
417 }
418 
419 const DataExtractor&
420 SymbolFileDWARF::get_debug_aranges_data()
421 {
422     return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
423 }
424 
425 const DataExtractor&
426 SymbolFileDWARF::get_debug_frame_data()
427 {
428     return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
429 }
430 
431 const DataExtractor&
432 SymbolFileDWARF::get_debug_info_data()
433 {
434     return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
435 }
436 
437 const DataExtractor&
438 SymbolFileDWARF::get_debug_line_data()
439 {
440     return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
441 }
442 
443 const DataExtractor&
444 SymbolFileDWARF::get_debug_loc_data()
445 {
446     return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
447 }
448 
449 const DataExtractor&
450 SymbolFileDWARF::get_debug_ranges_data()
451 {
452     return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
453 }
454 
455 const DataExtractor&
456 SymbolFileDWARF::get_debug_str_data()
457 {
458     return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
459 }
460 
461 const DataExtractor&
462 SymbolFileDWARF::get_apple_names_data()
463 {
464     return GetCachedSectionData (flagsGotDebugNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
465 }
466 
467 const DataExtractor&
468 SymbolFileDWARF::get_apple_types_data()
469 {
470     return GetCachedSectionData (flagsGotDebugTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
471 }
472 
473 
474 DWARFDebugAbbrev*
475 SymbolFileDWARF::DebugAbbrev()
476 {
477     if (m_abbr.get() == NULL)
478     {
479         const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
480         if (debug_abbrev_data.GetByteSize() > 0)
481         {
482             m_abbr.reset(new DWARFDebugAbbrev());
483             if (m_abbr.get())
484                 m_abbr->Parse(debug_abbrev_data);
485         }
486     }
487     return m_abbr.get();
488 }
489 
490 const DWARFDebugAbbrev*
491 SymbolFileDWARF::DebugAbbrev() const
492 {
493     return m_abbr.get();
494 }
495 
496 
497 DWARFDebugInfo*
498 SymbolFileDWARF::DebugInfo()
499 {
500     if (m_info.get() == NULL)
501     {
502         Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
503         if (get_debug_info_data().GetByteSize() > 0)
504         {
505             m_info.reset(new DWARFDebugInfo());
506             if (m_info.get())
507             {
508                 m_info->SetDwarfData(this);
509             }
510         }
511     }
512     return m_info.get();
513 }
514 
515 const DWARFDebugInfo*
516 SymbolFileDWARF::DebugInfo() const
517 {
518     return m_info.get();
519 }
520 
521 DWARFCompileUnit*
522 SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
523 {
524     DWARFDebugInfo* info = DebugInfo();
525     if (info)
526         return info->GetCompileUnit(cu_uid).get();
527     return NULL;
528 }
529 
530 
531 DWARFDebugRanges*
532 SymbolFileDWARF::DebugRanges()
533 {
534     if (m_ranges.get() == NULL)
535     {
536         Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
537         if (get_debug_ranges_data().GetByteSize() > 0)
538         {
539             m_ranges.reset(new DWARFDebugRanges());
540             if (m_ranges.get())
541                 m_ranges->Extract(this);
542         }
543     }
544     return m_ranges.get();
545 }
546 
547 const DWARFDebugRanges*
548 SymbolFileDWARF::DebugRanges() const
549 {
550     return m_ranges.get();
551 }
552 
553 bool
554 SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
555 {
556     if (curr_cu != NULL)
557     {
558         const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
559         if (cu_die)
560         {
561             const char * cu_die_name = cu_die->GetName(this, curr_cu);
562             const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
563             LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
564             if (cu_die_name)
565             {
566                 FileSpec cu_file_spec;
567 
568                 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
569                 {
570                     // If we have a full path to the compile unit, we don't need to resolve
571                     // the file.  This can be expensive e.g. when the source files are NFS mounted.
572                     cu_file_spec.SetFile (cu_die_name, false);
573                 }
574                 else
575                 {
576                     std::string fullpath(cu_comp_dir);
577                     if (*fullpath.rbegin() != '/')
578                         fullpath += '/';
579                     fullpath += cu_die_name;
580                     cu_file_spec.SetFile (fullpath.c_str(), false);
581                 }
582 
583                 compile_unit_sp.reset(new CompileUnit(m_obj_file->GetModule(), curr_cu, cu_file_spec, curr_cu->GetOffset(), cu_language));
584                 if (compile_unit_sp.get())
585                 {
586                     curr_cu->SetUserData(compile_unit_sp.get());
587                     return true;
588                 }
589             }
590         }
591     }
592     return false;
593 }
594 
595 uint32_t
596 SymbolFileDWARF::GetNumCompileUnits()
597 {
598     DWARFDebugInfo* info = DebugInfo();
599     if (info)
600         return info->GetNumCompileUnits();
601     return 0;
602 }
603 
604 CompUnitSP
605 SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
606 {
607     CompUnitSP comp_unit;
608     DWARFDebugInfo* info = DebugInfo();
609     if (info)
610     {
611         DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
612         if (curr_cu != NULL)
613         {
614             // Our symbol vendor shouldn't be asking us to add a compile unit that
615             // has already been added to it, which this DWARF plug-in knows as it
616             // stores the lldb compile unit (CompileUnit) pointer in each
617             // DWARFCompileUnit object when it gets added.
618             assert(curr_cu->GetUserData() == NULL);
619             ParseCompileUnit(curr_cu, comp_unit);
620         }
621     }
622     return comp_unit;
623 }
624 
625 static void
626 AddRangesToBlock
627 (
628     Block& block,
629     DWARFDebugRanges::RangeList& ranges,
630     addr_t block_base_addr
631 )
632 {
633     ranges.SubtractOffset (block_base_addr);
634     size_t range_idx = 0;
635     const DWARFDebugRanges::Range *debug_range;
636     for (range_idx = 0; (debug_range = ranges.RangeAtIndex(range_idx)) != NULL; range_idx++)
637     {
638         block.AddRange(VMRange (debug_range->begin_offset, debug_range->end_offset));
639     }
640 }
641 
642 
643 Function *
644 SymbolFileDWARF::ParseCompileUnitFunction (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die)
645 {
646     DWARFDebugRanges::RangeList func_ranges;
647     const char *name = NULL;
648     const char *mangled = NULL;
649     int decl_file = 0;
650     int decl_line = 0;
651     int decl_column = 0;
652     int call_file = 0;
653     int call_line = 0;
654     int call_column = 0;
655     DWARFExpression frame_base;
656 
657     assert (die->Tag() == DW_TAG_subprogram);
658 
659     if (die->Tag() != DW_TAG_subprogram)
660         return NULL;
661 
662     if (die->GetDIENamesAndRanges(this, dwarf_cu, name, mangled, func_ranges, decl_file, decl_line, decl_column, call_file, call_line, call_column, &frame_base))
663     {
664         // Union of all ranges in the function DIE (if the function is discontiguous)
665         AddressRange func_range;
666         lldb::addr_t lowest_func_addr = func_ranges.LowestAddress(0);
667         lldb::addr_t highest_func_addr = func_ranges.HighestAddress(0);
668         if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
669         {
670             func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, m_obj_file->GetSectionList());
671             if (func_range.GetBaseAddress().IsValid())
672                 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
673         }
674 
675         if (func_range.GetBaseAddress().IsValid())
676         {
677             Mangled func_name;
678             if (mangled)
679                 func_name.SetValue(mangled, true);
680             else if (name)
681                 func_name.SetValue(name, false);
682 
683             FunctionSP func_sp;
684             std::auto_ptr<Declaration> decl_ap;
685             if (decl_file != 0 || decl_line != 0 || decl_column != 0)
686                 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
687                                                decl_line,
688                                                decl_column));
689 
690             // Supply the type _only_ if it has already been parsed
691             Type *func_type = m_die_to_type.lookup (die);
692 
693             assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
694 
695             func_range.GetBaseAddress().ResolveLinkedAddress();
696 
697             func_sp.reset(new Function (sc.comp_unit,
698                                         die->GetOffset(),       // UserID is the DIE offset
699                                         die->GetOffset(),
700                                         func_name,
701                                         func_type,
702                                         func_range));           // first address range
703 
704             if (func_sp.get() != NULL)
705             {
706                 if (frame_base.IsValid())
707                     func_sp->GetFrameBaseExpression() = frame_base;
708                 sc.comp_unit->AddFunction(func_sp);
709                 return func_sp.get();
710             }
711         }
712     }
713     return NULL;
714 }
715 
716 size_t
717 SymbolFileDWARF::ParseCompileUnitFunctions(const SymbolContext &sc)
718 {
719     assert (sc.comp_unit);
720     size_t functions_added = 0;
721     DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
722     if (dwarf_cu)
723     {
724         DWARFDIECollection function_dies;
725         const size_t num_funtions = dwarf_cu->AppendDIEsWithTag (DW_TAG_subprogram, function_dies);
726         size_t func_idx;
727         for (func_idx = 0; func_idx < num_funtions; ++func_idx)
728         {
729             const DWARFDebugInfoEntry *die = function_dies.GetDIEPtrAtIndex(func_idx);
730             if (sc.comp_unit->FindFunctionByUID (die->GetOffset()).get() == NULL)
731             {
732                 if (ParseCompileUnitFunction(sc, dwarf_cu, die))
733                     ++functions_added;
734             }
735         }
736         //FixupTypes();
737     }
738     return functions_added;
739 }
740 
741 bool
742 SymbolFileDWARF::ParseCompileUnitSupportFiles (const SymbolContext& sc, FileSpecList& support_files)
743 {
744     assert (sc.comp_unit);
745     DWARFCompileUnit* curr_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
746     assert (curr_cu);
747     const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly();
748 
749     if (cu_die)
750     {
751         const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
752         dw_offset_t stmt_list = cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
753 
754         // All file indexes in DWARF are one based and a file of index zero is
755         // supposed to be the compile unit itself.
756         support_files.Append (*sc.comp_unit);
757 
758         return DWARFDebugLine::ParseSupportFiles(get_debug_line_data(), cu_comp_dir, stmt_list, support_files);
759     }
760     return false;
761 }
762 
763 struct ParseDWARFLineTableCallbackInfo
764 {
765     LineTable* line_table;
766     const SectionList *section_list;
767     lldb::addr_t prev_sect_file_base_addr;
768     lldb::addr_t curr_sect_file_base_addr;
769     bool is_oso_for_debug_map;
770     bool prev_in_final_executable;
771     DWARFDebugLine::Row prev_row;
772     SectionSP prev_section_sp;
773     SectionSP curr_section_sp;
774 };
775 
776 //----------------------------------------------------------------------
777 // ParseStatementTableCallback
778 //----------------------------------------------------------------------
779 static void
780 ParseDWARFLineTableCallback(dw_offset_t offset, const DWARFDebugLine::State& state, void* userData)
781 {
782     LineTable* line_table = ((ParseDWARFLineTableCallbackInfo*)userData)->line_table;
783     if (state.row == DWARFDebugLine::State::StartParsingLineTable)
784     {
785         // Just started parsing the line table
786     }
787     else if (state.row == DWARFDebugLine::State::DoneParsingLineTable)
788     {
789         // Done parsing line table, nothing to do for the cleanup
790     }
791     else
792     {
793         ParseDWARFLineTableCallbackInfo* info = (ParseDWARFLineTableCallbackInfo*)userData;
794         // We have a new row, lets append it
795 
796         if (info->curr_section_sp.get() == NULL || info->curr_section_sp->ContainsFileAddress(state.address) == false)
797         {
798             info->prev_section_sp = info->curr_section_sp;
799             info->prev_sect_file_base_addr = info->curr_sect_file_base_addr;
800             // If this is an end sequence entry, then we subtract one from the
801             // address to make sure we get an address that is not the end of
802             // a section.
803             if (state.end_sequence && state.address != 0)
804                 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address - 1);
805             else
806                 info->curr_section_sp = info->section_list->FindSectionContainingFileAddress (state.address);
807 
808             if (info->curr_section_sp.get())
809                 info->curr_sect_file_base_addr = info->curr_section_sp->GetFileAddress ();
810             else
811                 info->curr_sect_file_base_addr = 0;
812         }
813         if (info->curr_section_sp.get())
814         {
815             lldb::addr_t curr_line_section_offset = state.address - info->curr_sect_file_base_addr;
816             // Check for the fancy section magic to determine if we
817 
818             if (info->is_oso_for_debug_map)
819             {
820                 // When this is a debug map object file that contains DWARF
821                 // (referenced from an N_OSO debug map nlist entry) we will have
822                 // a file address in the file range for our section from the
823                 // original .o file, and a load address in the executable that
824                 // contains the debug map.
825                 //
826                 // If the sections for the file range and load range are
827                 // different, we have a remapped section for the function and
828                 // this address is resolved. If they are the same, then the
829                 // function for this address didn't make it into the final
830                 // executable.
831                 bool curr_in_final_executable = info->curr_section_sp->GetLinkedSection () != NULL;
832 
833                 // If we are doing DWARF with debug map, then we need to carefully
834                 // add each line table entry as there may be gaps as functions
835                 // get moved around or removed.
836                 if (!info->prev_row.end_sequence && info->prev_section_sp.get())
837                 {
838                     if (info->prev_in_final_executable)
839                     {
840                         bool terminate_previous_entry = false;
841                         if (!curr_in_final_executable)
842                         {
843                             // Check for the case where the previous line entry
844                             // in a function made it into the final executable,
845                             // yet the current line entry falls in a function
846                             // that didn't. The line table used to be contiguous
847                             // through this address range but now it isn't. We
848                             // need to terminate the previous line entry so
849                             // that we can reconstruct the line range correctly
850                             // for it and to keep the line table correct.
851                             terminate_previous_entry = true;
852                         }
853                         else if (info->curr_section_sp.get() != info->prev_section_sp.get())
854                         {
855                             // Check for cases where the line entries used to be
856                             // contiguous address ranges, but now they aren't.
857                             // This can happen when order files specify the
858                             // ordering of the functions.
859                             lldb::addr_t prev_line_section_offset = info->prev_row.address - info->prev_sect_file_base_addr;
860                             Section *curr_sect = info->curr_section_sp.get();
861                             Section *prev_sect = info->prev_section_sp.get();
862                             assert (curr_sect->GetLinkedSection());
863                             assert (prev_sect->GetLinkedSection());
864                             lldb::addr_t object_file_addr_delta = state.address - info->prev_row.address;
865                             lldb::addr_t curr_linked_file_addr = curr_sect->GetLinkedFileAddress() + curr_line_section_offset;
866                             lldb::addr_t prev_linked_file_addr = prev_sect->GetLinkedFileAddress() + prev_line_section_offset;
867                             lldb::addr_t linked_file_addr_delta = curr_linked_file_addr - prev_linked_file_addr;
868                             if (object_file_addr_delta != linked_file_addr_delta)
869                                 terminate_previous_entry = true;
870                         }
871 
872                         if (terminate_previous_entry)
873                         {
874                             line_table->InsertLineEntry (info->prev_section_sp,
875                                                          state.address - info->prev_sect_file_base_addr,
876                                                          info->prev_row.line,
877                                                          info->prev_row.column,
878                                                          info->prev_row.file,
879                                                          false,                 // is_stmt
880                                                          false,                 // basic_block
881                                                          false,                 // state.prologue_end
882                                                          false,                 // state.epilogue_begin
883                                                          true);                 // end_sequence);
884                         }
885                     }
886                 }
887 
888                 if (curr_in_final_executable)
889                 {
890                     line_table->InsertLineEntry (info->curr_section_sp,
891                                                  curr_line_section_offset,
892                                                  state.line,
893                                                  state.column,
894                                                  state.file,
895                                                  state.is_stmt,
896                                                  state.basic_block,
897                                                  state.prologue_end,
898                                                  state.epilogue_begin,
899                                                  state.end_sequence);
900                     info->prev_section_sp = info->curr_section_sp;
901                 }
902                 else
903                 {
904                     // If the current address didn't make it into the final
905                     // executable, the current section will be the __text
906                     // segment in the .o file, so we need to clear this so
907                     // we can catch the next function that did make it into
908                     // the final executable.
909                     info->prev_section_sp.reset();
910                     info->curr_section_sp.reset();
911                 }
912 
913                 info->prev_in_final_executable = curr_in_final_executable;
914             }
915             else
916             {
917                 // We are not in an object file that contains DWARF for an
918                 // N_OSO, this is just a normal DWARF file. The DWARF spec
919                 // guarantees that the addresses will be in increasing order
920                 // so, since we store line tables in file address order, we
921                 // can always just append the line entry without needing to
922                 // search for the correct insertion point (we don't need to
923                 // use LineEntry::InsertLineEntry()).
924                 line_table->AppendLineEntry (info->curr_section_sp,
925                                              curr_line_section_offset,
926                                              state.line,
927                                              state.column,
928                                              state.file,
929                                              state.is_stmt,
930                                              state.basic_block,
931                                              state.prologue_end,
932                                              state.epilogue_begin,
933                                              state.end_sequence);
934             }
935         }
936 
937         info->prev_row = state;
938     }
939 }
940 
941 bool
942 SymbolFileDWARF::ParseCompileUnitLineTable (const SymbolContext &sc)
943 {
944     assert (sc.comp_unit);
945     if (sc.comp_unit->GetLineTable() != NULL)
946         return true;
947 
948     DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
949     if (dwarf_cu)
950     {
951         const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->GetCompileUnitDIEOnly();
952         const dw_offset_t cu_line_offset = dwarf_cu_die->GetAttributeValueAsUnsigned(this, dwarf_cu, DW_AT_stmt_list, DW_INVALID_OFFSET);
953         if (cu_line_offset != DW_INVALID_OFFSET)
954         {
955             std::auto_ptr<LineTable> line_table_ap(new LineTable(sc.comp_unit));
956             if (line_table_ap.get())
957             {
958                 ParseDWARFLineTableCallbackInfo info = { line_table_ap.get(), m_obj_file->GetSectionList(), 0, 0, m_debug_map_symfile != NULL, false};
959                 uint32_t offset = cu_line_offset;
960                 DWARFDebugLine::ParseStatementTable(get_debug_line_data(), &offset, ParseDWARFLineTableCallback, &info);
961                 sc.comp_unit->SetLineTable(line_table_ap.release());
962                 return true;
963             }
964         }
965     }
966     return false;
967 }
968 
969 size_t
970 SymbolFileDWARF::ParseFunctionBlocks
971 (
972     const SymbolContext& sc,
973     Block *parent_block,
974     DWARFCompileUnit* dwarf_cu,
975     const DWARFDebugInfoEntry *die,
976     addr_t subprogram_low_pc,
977     uint32_t depth
978 )
979 {
980     size_t blocks_added = 0;
981     while (die != NULL)
982     {
983         dw_tag_t tag = die->Tag();
984 
985         switch (tag)
986         {
987         case DW_TAG_inlined_subroutine:
988         case DW_TAG_subprogram:
989         case DW_TAG_lexical_block:
990             {
991                 Block *block = NULL;
992                 if (tag == DW_TAG_subprogram)
993                 {
994                     // Skip any DW_TAG_subprogram DIEs that are inside
995                     // of a normal or inlined functions. These will be
996                     // parsed on their own as separate entities.
997 
998                     if (depth > 0)
999                         break;
1000 
1001                     block = parent_block;
1002                 }
1003                 else
1004                 {
1005                     BlockSP block_sp(new Block (die->GetOffset()));
1006                     parent_block->AddChild(block_sp);
1007                     block = block_sp.get();
1008                 }
1009                 DWARFDebugRanges::RangeList ranges;
1010                 const char *name = NULL;
1011                 const char *mangled_name = NULL;
1012 
1013                 int decl_file = 0;
1014                 int decl_line = 0;
1015                 int decl_column = 0;
1016                 int call_file = 0;
1017                 int call_line = 0;
1018                 int call_column = 0;
1019                 if (die->GetDIENamesAndRanges (this,
1020                                                dwarf_cu,
1021                                                name,
1022                                                mangled_name,
1023                                                ranges,
1024                                                decl_file, decl_line, decl_column,
1025                                                call_file, call_line, call_column))
1026                 {
1027                     if (tag == DW_TAG_subprogram)
1028                     {
1029                         assert (subprogram_low_pc == LLDB_INVALID_ADDRESS);
1030                         subprogram_low_pc = ranges.LowestAddress(0);
1031                     }
1032                     else if (tag == DW_TAG_inlined_subroutine)
1033                     {
1034                         // We get called here for inlined subroutines in two ways.
1035                         // The first time is when we are making the Function object
1036                         // for this inlined concrete instance.  Since we're creating a top level block at
1037                         // here, the subprogram_low_pc will be LLDB_INVALID_ADDRESS.  So we need to
1038                         // adjust the containing address.
1039                         // The second time is when we are parsing the blocks inside the function that contains
1040                         // the inlined concrete instance.  Since these will be blocks inside the containing "real"
1041                         // function the offset will be for that function.
1042                         if (subprogram_low_pc == LLDB_INVALID_ADDRESS)
1043                         {
1044                             subprogram_low_pc = ranges.LowestAddress(0);
1045                         }
1046                     }
1047 
1048                     AddRangesToBlock (*block, ranges, subprogram_low_pc);
1049 
1050                     if (tag != DW_TAG_subprogram && (name != NULL || mangled_name != NULL))
1051                     {
1052                         std::auto_ptr<Declaration> decl_ap;
1053                         if (decl_file != 0 || decl_line != 0 || decl_column != 0)
1054                             decl_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
1055                                                           decl_line, decl_column));
1056 
1057                         std::auto_ptr<Declaration> call_ap;
1058                         if (call_file != 0 || call_line != 0 || call_column != 0)
1059                             call_ap.reset(new Declaration(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(call_file),
1060                                                           call_line, call_column));
1061 
1062                         block->SetInlinedFunctionInfo (name, mangled_name, decl_ap.get(), call_ap.get());
1063                     }
1064 
1065                     ++blocks_added;
1066 
1067                     if (die->HasChildren())
1068                     {
1069                         blocks_added += ParseFunctionBlocks (sc,
1070                                                              block,
1071                                                              dwarf_cu,
1072                                                              die->GetFirstChild(),
1073                                                              subprogram_low_pc,
1074                                                              depth + 1);
1075                     }
1076                 }
1077             }
1078             break;
1079         default:
1080             break;
1081         }
1082 
1083         // Only parse siblings of the block if we are not at depth zero. A depth
1084         // of zero indicates we are currently parsing the top level
1085         // DW_TAG_subprogram DIE
1086 
1087         if (depth == 0)
1088             die = NULL;
1089         else
1090             die = die->GetSibling();
1091     }
1092     return blocks_added;
1093 }
1094 
1095 size_t
1096 SymbolFileDWARF::ParseChildMembers
1097 (
1098     const SymbolContext& sc,
1099     DWARFCompileUnit* dwarf_cu,
1100     const DWARFDebugInfoEntry *parent_die,
1101     clang_type_t class_clang_type,
1102     const LanguageType class_language,
1103     std::vector<clang::CXXBaseSpecifier *>& base_classes,
1104     std::vector<int>& member_accessibilities,
1105     DWARFDIECollection& member_function_dies,
1106     AccessType& default_accessibility,
1107     bool &is_a_class
1108 )
1109 {
1110     if (parent_die == NULL)
1111         return 0;
1112 
1113     size_t count = 0;
1114     const DWARFDebugInfoEntry *die;
1115     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
1116     uint32_t member_idx = 0;
1117 
1118     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
1119     {
1120         dw_tag_t tag = die->Tag();
1121 
1122         switch (tag)
1123         {
1124         case DW_TAG_member:
1125             {
1126                 DWARFDebugInfoEntry::Attributes attributes;
1127                 const size_t num_attributes = die->GetAttributes (this,
1128                                                                   dwarf_cu,
1129                                                                   fixed_form_sizes,
1130                                                                   attributes);
1131                 if (num_attributes > 0)
1132                 {
1133                     Declaration decl;
1134                     //DWARFExpression location;
1135                     const char *name = NULL;
1136                     bool is_artificial = false;
1137                     lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
1138                     AccessType accessibility = eAccessNone;
1139                     //off_t member_offset = 0;
1140                     size_t byte_size = 0;
1141                     size_t bit_offset = 0;
1142                     size_t bit_size = 0;
1143                     uint32_t i;
1144                     for (i=0; i<num_attributes && !is_artificial; ++i)
1145                     {
1146                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
1147                         DWARFFormValue form_value;
1148                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1149                         {
1150                             switch (attr)
1151                             {
1152                             case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1153                             case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
1154                             case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1155                             case DW_AT_name:        name = form_value.AsCString(&get_debug_str_data()); break;
1156                             case DW_AT_type:        encoding_uid = form_value.Reference(dwarf_cu); break;
1157                             case DW_AT_bit_offset:  bit_offset = form_value.Unsigned(); break;
1158                             case DW_AT_bit_size:    bit_size = form_value.Unsigned(); break;
1159                             case DW_AT_byte_size:   byte_size = form_value.Unsigned(); break;
1160                             case DW_AT_data_member_location:
1161 //                                if (form_value.BlockData())
1162 //                                {
1163 //                                    Value initialValue(0);
1164 //                                    Value memberOffset(0);
1165 //                                    const DataExtractor& debug_info_data = get_debug_info_data();
1166 //                                    uint32_t block_length = form_value.Unsigned();
1167 //                                    uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1168 //                                    if (DWARFExpression::Evaluate(NULL, NULL, debug_info_data, NULL, NULL, block_offset, block_length, eRegisterKindDWARF, &initialValue, memberOffset, NULL))
1169 //                                    {
1170 //                                        member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1171 //                                    }
1172 //                                }
1173                                 break;
1174 
1175                             case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
1176                             case DW_AT_artificial: is_artificial = form_value.Unsigned() != 0; break;
1177                             case DW_AT_declaration:
1178                             case DW_AT_description:
1179                             case DW_AT_mutable:
1180                             case DW_AT_visibility:
1181                             default:
1182                             case DW_AT_sibling:
1183                                 break;
1184                             }
1185                         }
1186                     }
1187 
1188                     // FIXME: Make Clang ignore Objective-C accessibility for expressions
1189 
1190                     if (class_language == eLanguageTypeObjC ||
1191                         class_language == eLanguageTypeObjC_plus_plus)
1192                         accessibility = eAccessNone;
1193 
1194                     if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
1195                     {
1196                         // Not all compilers will mark the vtable pointer
1197                         // member as artificial (llvm-gcc). We can't have
1198                         // the virtual members in our classes otherwise it
1199                         // throws off all child offsets since we end up
1200                         // having and extra pointer sized member in our
1201                         // class layouts.
1202                         is_artificial = true;
1203                     }
1204 
1205                     if (is_artificial == false)
1206                     {
1207                         Type *member_type = ResolveTypeUID(encoding_uid);
1208                         if (member_type)
1209                         {
1210                             if (accessibility == eAccessNone)
1211                                 accessibility = default_accessibility;
1212                             member_accessibilities.push_back(accessibility);
1213 
1214                             GetClangASTContext().AddFieldToRecordType (class_clang_type,
1215                                                                        name,
1216                                                                        member_type->GetClangLayoutType(),
1217                                                                        accessibility,
1218                                                                        bit_size);
1219                         }
1220                         else
1221                         {
1222                             if (name)
1223                                 ReportError ("0x%8.8x: DW_TAG_member '%s' refers to type 0x%8.8x which was unable to be parsed",
1224                                              die->GetOffset(),
1225                                              name,
1226                                              encoding_uid);
1227                             else
1228                                 ReportError ("0x%8.8x: DW_TAG_member refers to type 0x%8.8x which was unable to be parsed",
1229                                              die->GetOffset(),
1230                                              encoding_uid);
1231                         }
1232                     }
1233                 }
1234                 ++member_idx;
1235             }
1236             break;
1237 
1238         case DW_TAG_subprogram:
1239             // Let the type parsing code handle this one for us.
1240             member_function_dies.Append (die);
1241             break;
1242 
1243         case DW_TAG_inheritance:
1244             {
1245                 is_a_class = true;
1246                 if (default_accessibility == eAccessNone)
1247                     default_accessibility = eAccessPrivate;
1248                 // TODO: implement DW_TAG_inheritance type parsing
1249                 DWARFDebugInfoEntry::Attributes attributes;
1250                 const size_t num_attributes = die->GetAttributes (this,
1251                                                                   dwarf_cu,
1252                                                                   fixed_form_sizes,
1253                                                                   attributes);
1254                 if (num_attributes > 0)
1255                 {
1256                     Declaration decl;
1257                     DWARFExpression location;
1258                     lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
1259                     AccessType accessibility = default_accessibility;
1260                     bool is_virtual = false;
1261                     bool is_base_of_class = true;
1262                     off_t member_offset = 0;
1263                     uint32_t i;
1264                     for (i=0; i<num_attributes; ++i)
1265                     {
1266                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
1267                         DWARFFormValue form_value;
1268                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
1269                         {
1270                             switch (attr)
1271                             {
1272                             case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1273                             case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
1274                             case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1275                             case DW_AT_type:        encoding_uid = form_value.Reference(dwarf_cu); break;
1276                             case DW_AT_data_member_location:
1277                                 if (form_value.BlockData())
1278                                 {
1279                                     Value initialValue(0);
1280                                     Value memberOffset(0);
1281                                     const DataExtractor& debug_info_data = get_debug_info_data();
1282                                     uint32_t block_length = form_value.Unsigned();
1283                                     uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
1284                                     if (DWARFExpression::Evaluate (NULL,
1285                                                                    NULL,
1286                                                                    NULL,
1287                                                                    NULL,
1288                                                                    NULL,
1289                                                                    debug_info_data,
1290                                                                    block_offset,
1291                                                                    block_length,
1292                                                                    eRegisterKindDWARF,
1293                                                                    &initialValue,
1294                                                                    memberOffset,
1295                                                                    NULL))
1296                                     {
1297                                         member_offset = memberOffset.ResolveValue(NULL, NULL).UInt();
1298                                     }
1299                                 }
1300                                 break;
1301 
1302                             case DW_AT_accessibility:
1303                                 accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
1304                                 break;
1305 
1306                             case DW_AT_virtuality: is_virtual = form_value.Unsigned() != 0; break;
1307                             default:
1308                             case DW_AT_sibling:
1309                                 break;
1310                             }
1311                         }
1312                     }
1313 
1314                     Type *base_class_type = ResolveTypeUID(encoding_uid);
1315                     assert(base_class_type);
1316 
1317                     clang_type_t base_class_clang_type = base_class_type->GetClangFullType();
1318                     assert (base_class_clang_type);
1319                     if (class_language == eLanguageTypeObjC)
1320                     {
1321                         GetClangASTContext().SetObjCSuperClass(class_clang_type, base_class_clang_type);
1322                     }
1323                     else
1324                     {
1325                         base_classes.push_back (GetClangASTContext().CreateBaseClassSpecifier (base_class_clang_type,
1326                                                                                                accessibility,
1327                                                                                                is_virtual,
1328                                                                                                is_base_of_class));
1329                     }
1330                 }
1331             }
1332             break;
1333 
1334         default:
1335             break;
1336         }
1337     }
1338     return count;
1339 }
1340 
1341 
1342 clang::DeclContext*
1343 SymbolFileDWARF::GetClangDeclContextContainingTypeUID (lldb::user_id_t type_uid)
1344 {
1345     DWARFDebugInfo* debug_info = DebugInfo();
1346     if (debug_info)
1347     {
1348         DWARFCompileUnitSP cu_sp;
1349         const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1350         if (die)
1351             return GetClangDeclContextContainingDIE (cu_sp.get(), die);
1352     }
1353     return NULL;
1354 }
1355 
1356 clang::DeclContext*
1357 SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1358 {
1359     DWARFDebugInfo* debug_info = DebugInfo();
1360     if (debug_info)
1361     {
1362         DWARFCompileUnitSP cu_sp;
1363         const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1364         if (die)
1365             return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
1366     }
1367     return NULL;
1368 }
1369 
1370 Type*
1371 SymbolFileDWARF::ResolveTypeUID (lldb::user_id_t type_uid)
1372 {
1373     DWARFDebugInfo* debug_info = DebugInfo();
1374     if (debug_info)
1375     {
1376         DWARFCompileUnitSP cu_sp;
1377         const DWARFDebugInfoEntry* type_die = debug_info->GetDIEPtr(type_uid, &cu_sp);
1378         if (type_die != NULL)
1379         {
1380             // We might be coming in in the middle of a type tree (a class
1381             // withing a class, an enum within a class), so parse any needed
1382             // parent DIEs before we get to this one...
1383             const DWARFDebugInfoEntry* parent_die = type_die->GetParent();
1384             switch (parent_die->Tag())
1385             {
1386             case DW_TAG_structure_type:
1387             case DW_TAG_union_type:
1388             case DW_TAG_class_type:
1389                 ResolveType(cu_sp.get(), parent_die);
1390                 break;
1391             }
1392             return ResolveType (cu_sp.get(), type_die);
1393         }
1394     }
1395     return NULL;
1396 }
1397 
1398 // This function is used when SymbolFileDWARFDebugMap owns a bunch of
1399 // SymbolFileDWARF objects to detect if this DWARF file is the one that
1400 // can resolve a clang_type.
1401 bool
1402 SymbolFileDWARF::HasForwardDeclForClangType (lldb::clang_type_t clang_type)
1403 {
1404     clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1405     const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1406     return die != NULL;
1407 }
1408 
1409 
1410 lldb::clang_type_t
1411 SymbolFileDWARF::ResolveClangOpaqueTypeDefinition (lldb::clang_type_t clang_type)
1412 {
1413     // We have a struct/union/class/enum that needs to be fully resolved.
1414     clang_type_t clang_type_no_qualifiers = ClangASTType::RemoveFastQualifiers(clang_type);
1415     const DWARFDebugInfoEntry* die = m_forward_decl_clang_type_to_die.lookup (clang_type_no_qualifiers);
1416     if (die == NULL)
1417     {
1418 //        if (m_debug_map_symfile)
1419 //        {
1420 //            Type *type = m_die_to_type[die];
1421 //            if (type && type->GetSymbolFile() != this)
1422 //                return type->GetClangType();
1423 //        }
1424         // We have already resolved this type...
1425         return clang_type;
1426     }
1427     // Once we start resolving this type, remove it from the forward declaration
1428     // map in case anyone child members or other types require this type to get resolved.
1429     // The type will get resolved when all of the calls to SymbolFileDWARF::ResolveClangOpaqueTypeDefinition
1430     // are done.
1431     m_forward_decl_clang_type_to_die.erase (clang_type_no_qualifiers);
1432 
1433 
1434     DWARFDebugInfo* debug_info = DebugInfo();
1435 
1436     DWARFCompileUnit *curr_cu = debug_info->GetCompileUnitContainingDIE (die->GetOffset()).get();
1437     Type *type = m_die_to_type.lookup (die);
1438 
1439     const dw_tag_t tag = die->Tag();
1440 
1441     DEBUG_PRINTF ("0x%8.8x: %s (\"%s\") - resolve forward declaration...\n",
1442                   die->GetOffset(),
1443                   DW_TAG_value_to_name(tag),
1444                   type->GetName().AsCString());
1445     assert (clang_type);
1446     DWARFDebugInfoEntry::Attributes attributes;
1447 
1448     ClangASTContext &ast = GetClangASTContext();
1449 
1450     switch (tag)
1451     {
1452     case DW_TAG_structure_type:
1453     case DW_TAG_union_type:
1454     case DW_TAG_class_type:
1455         ast.StartTagDeclarationDefinition (clang_type);
1456         if (die->HasChildren())
1457         {
1458             LanguageType class_language = eLanguageTypeUnknown;
1459             bool is_objc_class = ClangASTContext::IsObjCClassType (clang_type);
1460             if (is_objc_class)
1461                 class_language = eLanguageTypeObjC;
1462 
1463             int tag_decl_kind = -1;
1464             AccessType default_accessibility = eAccessNone;
1465             if (tag == DW_TAG_structure_type)
1466             {
1467                 tag_decl_kind = clang::TTK_Struct;
1468                 default_accessibility = eAccessPublic;
1469             }
1470             else if (tag == DW_TAG_union_type)
1471             {
1472                 tag_decl_kind = clang::TTK_Union;
1473                 default_accessibility = eAccessPublic;
1474             }
1475             else if (tag == DW_TAG_class_type)
1476             {
1477                 tag_decl_kind = clang::TTK_Class;
1478                 default_accessibility = eAccessPrivate;
1479             }
1480 
1481             SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1482             std::vector<clang::CXXBaseSpecifier *> base_classes;
1483             std::vector<int> member_accessibilities;
1484             bool is_a_class = false;
1485             // Parse members and base classes first
1486             DWARFDIECollection member_function_dies;
1487 
1488             ParseChildMembers (sc,
1489                                curr_cu,
1490                                die,
1491                                clang_type,
1492                                class_language,
1493                                base_classes,
1494                                member_accessibilities,
1495                                member_function_dies,
1496                                default_accessibility,
1497                                is_a_class);
1498 
1499             // Now parse any methods if there were any...
1500             size_t num_functions = member_function_dies.Size();
1501             if (num_functions > 0)
1502             {
1503                 for (size_t i=0; i<num_functions; ++i)
1504                 {
1505                     ResolveType(curr_cu, member_function_dies.GetDIEPtrAtIndex(i));
1506                 }
1507             }
1508 
1509             if (class_language == eLanguageTypeObjC)
1510             {
1511                 std::string class_str (ClangASTType::GetTypeNameForOpaqueQualType(clang_type));
1512                 if (!class_str.empty())
1513                 {
1514 
1515                     ConstString class_name (class_str.c_str());
1516                     DIEArray method_die_offsets;
1517                     if (m_objc_class_selectors_index.Find (class_name, method_die_offsets))
1518                     {
1519                         DWARFDebugInfo* debug_info = DebugInfo();
1520 
1521                         DWARFCompileUnit* method_cu = NULL;
1522                         const size_t num_matches = method_die_offsets.size();
1523                         for (size_t i=0; i<num_matches; ++i)
1524                         {
1525                             const dw_offset_t die_offset = method_die_offsets[i];
1526                             DWARFDebugInfoEntry *method_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &method_cu);
1527 
1528                             ResolveType (method_cu, method_die);
1529                         }
1530                     }
1531                 }
1532             }
1533 
1534             // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
1535             // need to tell the clang type it is actually a class.
1536             if (class_language != eLanguageTypeObjC)
1537             {
1538                 if (is_a_class && tag_decl_kind != clang::TTK_Class)
1539                     ast.SetTagTypeKind (clang_type, clang::TTK_Class);
1540             }
1541 
1542             // Since DW_TAG_structure_type gets used for both classes
1543             // and structures, we may need to set any DW_TAG_member
1544             // fields to have a "private" access if none was specified.
1545             // When we parsed the child members we tracked that actual
1546             // accessibility value for each DW_TAG_member in the
1547             // "member_accessibilities" array. If the value for the
1548             // member is zero, then it was set to the "default_accessibility"
1549             // which for structs was "public". Below we correct this
1550             // by setting any fields to "private" that weren't correctly
1551             // set.
1552             if (is_a_class && !member_accessibilities.empty())
1553             {
1554                 // This is a class and all members that didn't have
1555                 // their access specified are private.
1556                 ast.SetDefaultAccessForRecordFields (clang_type,
1557                                                      eAccessPrivate,
1558                                                      &member_accessibilities.front(),
1559                                                      member_accessibilities.size());
1560             }
1561 
1562             if (!base_classes.empty())
1563             {
1564                 ast.SetBaseClassesForClassType (clang_type,
1565                                                 &base_classes.front(),
1566                                                 base_classes.size());
1567 
1568                 // Clang will copy each CXXBaseSpecifier in "base_classes"
1569                 // so we have to free them all.
1570                 ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
1571                                                             base_classes.size());
1572             }
1573 
1574         }
1575         ast.CompleteTagDeclarationDefinition (clang_type);
1576         return clang_type;
1577 
1578     case DW_TAG_enumeration_type:
1579         ast.StartTagDeclarationDefinition (clang_type);
1580         if (die->HasChildren())
1581         {
1582             SymbolContext sc(GetCompUnitForDWARFCompUnit(curr_cu));
1583             ParseChildEnumerators(sc, clang_type, type->GetByteSize(), curr_cu, die);
1584         }
1585         ast.CompleteTagDeclarationDefinition (clang_type);
1586         return clang_type;
1587 
1588     default:
1589         assert(false && "not a forward clang type decl!");
1590         break;
1591     }
1592     return NULL;
1593 }
1594 
1595 Type*
1596 SymbolFileDWARF::ResolveType (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* type_die, bool assert_not_being_parsed)
1597 {
1598     if (type_die != NULL)
1599     {
1600         Type *type = m_die_to_type.lookup (type_die);
1601         if (type == NULL)
1602             type = GetTypeForDIE (curr_cu, type_die).get();
1603         if (assert_not_being_parsed)
1604             assert (type != DIE_IS_BEING_PARSED);
1605         return type;
1606     }
1607     return NULL;
1608 }
1609 
1610 CompileUnit*
1611 SymbolFileDWARF::GetCompUnitForDWARFCompUnit (DWARFCompileUnit* curr_cu, uint32_t cu_idx)
1612 {
1613     // Check if the symbol vendor already knows about this compile unit?
1614     if (curr_cu->GetUserData() == NULL)
1615     {
1616         // The symbol vendor doesn't know about this compile unit, we
1617         // need to parse and add it to the symbol vendor object.
1618         CompUnitSP dc_cu;
1619         ParseCompileUnit(curr_cu, dc_cu);
1620         if (dc_cu.get())
1621         {
1622             // Figure out the compile unit index if we weren't given one
1623             if (cu_idx == UINT32_MAX)
1624                 DebugInfo()->GetCompileUnit(curr_cu->GetOffset(), &cu_idx);
1625 
1626             m_obj_file->GetModule()->GetSymbolVendor()->SetCompileUnitAtIndex(dc_cu, cu_idx);
1627 
1628             if (m_debug_map_symfile)
1629                 m_debug_map_symfile->SetCompileUnit(this, dc_cu);
1630         }
1631     }
1632     return (CompileUnit*)curr_cu->GetUserData();
1633 }
1634 
1635 bool
1636 SymbolFileDWARF::GetFunction (DWARFCompileUnit* curr_cu, const DWARFDebugInfoEntry* func_die, SymbolContext& sc)
1637 {
1638     sc.Clear();
1639     // Check if the symbol vendor already knows about this compile unit?
1640     sc.module_sp = m_obj_file->GetModule();
1641     sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
1642 
1643     sc.function = sc.comp_unit->FindFunctionByUID (func_die->GetOffset()).get();
1644     if (sc.function == NULL)
1645         sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
1646 
1647     return sc.function != NULL;
1648 }
1649 
1650 uint32_t
1651 SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1652 {
1653     Timer scoped_timer(__PRETTY_FUNCTION__,
1654                        "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
1655                        so_addr.GetSection(),
1656                        so_addr.GetOffset(),
1657                        resolve_scope);
1658     uint32_t resolved = 0;
1659     if (resolve_scope & (   eSymbolContextCompUnit |
1660                             eSymbolContextFunction |
1661                             eSymbolContextBlock |
1662                             eSymbolContextLineEntry))
1663     {
1664         lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1665 
1666         DWARFDebugInfo* debug_info = DebugInfo();
1667         if (debug_info)
1668         {
1669             dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1670             if (cu_offset != DW_INVALID_OFFSET)
1671             {
1672                 uint32_t cu_idx;
1673                 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1674                 if (curr_cu)
1675                 {
1676                     sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
1677                     assert(sc.comp_unit != NULL);
1678                     resolved |= eSymbolContextCompUnit;
1679 
1680                     if (resolve_scope & eSymbolContextLineEntry)
1681                     {
1682                         LineTable *line_table = sc.comp_unit->GetLineTable();
1683                         if (line_table == NULL)
1684                         {
1685                             if (ParseCompileUnitLineTable(sc))
1686                                 line_table = sc.comp_unit->GetLineTable();
1687                         }
1688                         if (line_table != NULL)
1689                         {
1690                             if (so_addr.IsLinkedAddress())
1691                             {
1692                                 Address linked_addr (so_addr);
1693                                 linked_addr.ResolveLinkedAddress();
1694                                 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
1695                                 {
1696                                     resolved |= eSymbolContextLineEntry;
1697                                 }
1698                             }
1699                             else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
1700                             {
1701                                 resolved |= eSymbolContextLineEntry;
1702                             }
1703                         }
1704                     }
1705 
1706                     if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1707                     {
1708                         DWARFDebugInfoEntry *function_die = NULL;
1709                         DWARFDebugInfoEntry *block_die = NULL;
1710                         if (resolve_scope & eSymbolContextBlock)
1711                         {
1712                             curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
1713                         }
1714                         else
1715                         {
1716                             curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
1717                         }
1718 
1719                         if (function_die != NULL)
1720                         {
1721                             sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1722                             if (sc.function == NULL)
1723                                 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
1724                         }
1725 
1726                         if (sc.function != NULL)
1727                         {
1728                             resolved |= eSymbolContextFunction;
1729 
1730                             if (resolve_scope & eSymbolContextBlock)
1731                             {
1732                                 Block& block = sc.function->GetBlock (true);
1733 
1734                                 if (block_die != NULL)
1735                                     sc.block = block.FindBlockByID (block_die->GetOffset());
1736                                 else
1737                                     sc.block = block.FindBlockByID (function_die->GetOffset());
1738                                 if (sc.block)
1739                                     resolved |= eSymbolContextBlock;
1740                             }
1741                         }
1742                     }
1743                 }
1744             }
1745         }
1746     }
1747     return resolved;
1748 }
1749 
1750 
1751 
1752 uint32_t
1753 SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1754 {
1755     const uint32_t prev_size = sc_list.GetSize();
1756     if (resolve_scope & eSymbolContextCompUnit)
1757     {
1758         DWARFDebugInfo* debug_info = DebugInfo();
1759         if (debug_info)
1760         {
1761             uint32_t cu_idx;
1762             DWARFCompileUnit* curr_cu = NULL;
1763 
1764             for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
1765             {
1766                 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
1767                 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
1768                 if (check_inlines || file_spec_matches_cu_file_spec)
1769                 {
1770                     SymbolContext sc (m_obj_file->GetModule());
1771                     sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
1772                     assert(sc.comp_unit != NULL);
1773 
1774                     uint32_t file_idx = UINT32_MAX;
1775 
1776                     // If we are looking for inline functions only and we don't
1777                     // find it in the support files, we are done.
1778                     if (check_inlines)
1779                     {
1780                         file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1781                         if (file_idx == UINT32_MAX)
1782                             continue;
1783                     }
1784 
1785                     if (line != 0)
1786                     {
1787                         LineTable *line_table = sc.comp_unit->GetLineTable();
1788 
1789                         if (line_table != NULL && line != 0)
1790                         {
1791                             // We will have already looked up the file index if
1792                             // we are searching for inline entries.
1793                             if (!check_inlines)
1794                                 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1795 
1796                             if (file_idx != UINT32_MAX)
1797                             {
1798                                 uint32_t found_line;
1799                                 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1800                                 found_line = sc.line_entry.line;
1801 
1802                                 while (line_idx != UINT32_MAX)
1803                                 {
1804                                     sc.function = NULL;
1805                                     sc.block = NULL;
1806                                     if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1807                                     {
1808                                         const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1809                                         if (file_vm_addr != LLDB_INVALID_ADDRESS)
1810                                         {
1811                                             DWARFDebugInfoEntry *function_die = NULL;
1812                                             DWARFDebugInfoEntry *block_die = NULL;
1813                                             curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
1814 
1815                                             if (function_die != NULL)
1816                                             {
1817                                                 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1818                                                 if (sc.function == NULL)
1819                                                     sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
1820                                             }
1821 
1822                                             if (sc.function != NULL)
1823                                             {
1824                                                 Block& block = sc.function->GetBlock (true);
1825 
1826                                                 if (block_die != NULL)
1827                                                     sc.block = block.FindBlockByID (block_die->GetOffset());
1828                                                 else
1829                                                     sc.block = block.FindBlockByID (function_die->GetOffset());
1830                                             }
1831                                         }
1832                                     }
1833 
1834                                     sc_list.Append(sc);
1835                                     line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1836                                 }
1837                             }
1838                         }
1839                         else if (file_spec_matches_cu_file_spec && !check_inlines)
1840                         {
1841                             // only append the context if we aren't looking for inline call sites
1842                             // by file and line and if the file spec matches that of the compile unit
1843                             sc_list.Append(sc);
1844                         }
1845                     }
1846                     else if (file_spec_matches_cu_file_spec && !check_inlines)
1847                     {
1848                         // only append the context if we aren't looking for inline call sites
1849                         // by file and line and if the file spec matches that of the compile unit
1850                         sc_list.Append(sc);
1851                     }
1852 
1853                     if (!check_inlines)
1854                         break;
1855                 }
1856             }
1857         }
1858     }
1859     return sc_list.GetSize() - prev_size;
1860 }
1861 
1862 void
1863 SymbolFileDWARF::Index ()
1864 {
1865     if (m_indexed)
1866         return;
1867     m_indexed = true;
1868     Timer scoped_timer (__PRETTY_FUNCTION__,
1869                         "SymbolFileDWARF::Index (%s)",
1870                         GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1871 
1872     DWARFDebugInfo* debug_info = DebugInfo();
1873     if (debug_info)
1874     {
1875         uint32_t cu_idx = 0;
1876         const uint32_t num_compile_units = GetNumCompileUnits();
1877         for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1878         {
1879             DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1880 
1881             bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
1882 
1883             curr_cu->Index (cu_idx,
1884                             m_function_basename_index,
1885                             m_function_fullname_index,
1886                             m_function_method_index,
1887                             m_function_selector_index,
1888                             m_objc_class_selectors_index,
1889                             m_global_index,
1890                             m_type_index,
1891                             m_namespace_index);
1892 
1893             // Keep memory down by clearing DIEs if this generate function
1894             // caused them to be parsed
1895             if (clear_dies)
1896                 curr_cu->ClearDIEs (true);
1897         }
1898 
1899         m_function_basename_index.Finalize();
1900         m_function_fullname_index.Finalize();
1901         m_function_method_index.Finalize();
1902         m_function_selector_index.Finalize();
1903         m_objc_class_selectors_index.Finalize();
1904         m_global_index.Finalize();
1905         m_type_index.Finalize();
1906         m_namespace_index.Finalize();
1907 
1908 #if defined (ENABLE_DEBUG_PRINTF)
1909         StreamFile s(stdout, false);
1910         s.Printf ("DWARF index for '%s/%s':",
1911                   GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
1912                   GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1913         s.Printf("\nFunction basenames:\n");    m_function_basename_index.Dump (&s);
1914         s.Printf("\nFunction fullnames:\n");    m_function_fullname_index.Dump (&s);
1915         s.Printf("\nFunction methods:\n");      m_function_method_index.Dump (&s);
1916         s.Printf("\nFunction selectors:\n");    m_function_selector_index.Dump (&s);
1917         s.Printf("\nObjective C class selectors:\n");    m_objc_class_selectors_index.Dump (&s);
1918         s.Printf("\nGlobals and statics:\n");   m_global_index.Dump (&s);
1919         s.Printf("\nTypes:\n");                 m_type_index.Dump (&s);
1920         s.Printf("\nNamepaces:\n");             m_namespace_index.Dump (&s);
1921 #endif
1922     }
1923 }
1924 
1925 uint32_t
1926 SymbolFileDWARF::FindGlobalVariables (const ConstString &name, bool append, uint32_t max_matches, VariableList& variables)
1927 {
1928     DWARFDebugInfo* info = DebugInfo();
1929     if (info == NULL)
1930         return 0;
1931 
1932     // If we aren't appending the results to this list, then clear the list
1933     if (!append)
1934         variables.Clear();
1935 
1936     // Remember how many variables are in the list before we search in case
1937     // we are appending the results to a variable list.
1938     const uint32_t original_size = variables.GetSize();
1939 
1940     // Index the DWARF if we haven't already
1941     if (!m_indexed)
1942         Index ();
1943 
1944     SymbolContext sc;
1945     sc.module_sp = m_obj_file->GetModule();
1946     assert (sc.module_sp);
1947 
1948     DWARFCompileUnit* dwarf_cu = NULL;
1949     const DWARFDebugInfoEntry* die = NULL;
1950     DIEArray die_offsets;
1951     const size_t num_matches = m_global_index.Find (name, die_offsets);
1952     if (num_matches)
1953     {
1954         DWARFDebugInfo* debug_info = DebugInfo();
1955         for (size_t i=0; i<num_matches; ++i)
1956         {
1957             const dw_offset_t die_offset = die_offsets[i];
1958             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
1959 
1960             sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
1961             assert(sc.comp_unit != NULL);
1962 
1963             ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
1964 
1965             if (variables.GetSize() - original_size >= max_matches)
1966                 break;
1967         }
1968     }
1969 
1970     // Return the number of variable that were appended to the list
1971     return variables.GetSize() - original_size;
1972 }
1973 
1974 uint32_t
1975 SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
1976 {
1977     DWARFDebugInfo* info = DebugInfo();
1978     if (info == NULL)
1979         return 0;
1980 
1981     // If we aren't appending the results to this list, then clear the list
1982     if (!append)
1983         variables.Clear();
1984 
1985     // Remember how many variables are in the list before we search in case
1986     // we are appending the results to a variable list.
1987     const uint32_t original_size = variables.GetSize();
1988 
1989     // Index the DWARF if we haven't already
1990     if (!m_indexed)
1991         Index ();
1992 
1993     SymbolContext sc;
1994     sc.module_sp = m_obj_file->GetModule();
1995     assert (sc.module_sp);
1996 
1997     DWARFCompileUnit* dwarf_cu = NULL;
1998     const DWARFDebugInfoEntry* die = NULL;
1999     DIEArray die_offsets;
2000     const size_t num_matches = m_global_index.Find (regex, die_offsets);
2001     if (num_matches)
2002     {
2003         DWARFDebugInfo* debug_info = DebugInfo();
2004         for (size_t i=0; i<num_matches; ++i)
2005         {
2006             const dw_offset_t die_offset = die_offsets[i];
2007             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2008             sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2009             assert(sc.comp_unit != NULL);
2010 
2011             ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
2012 
2013             if (variables.GetSize() - original_size >= max_matches)
2014                 break;
2015         }
2016     }
2017 
2018     // Return the number of variable that were appended to the list
2019     return variables.GetSize() - original_size;
2020 }
2021 
2022 
2023 uint32_t
2024 SymbolFileDWARF::ResolveFunctions (const DIEArray &die_offsets,
2025                                    SymbolContextList& sc_list,
2026                                    const ConstString &name,
2027                                    uint32_t name_type_mask)
2028 {
2029     DWARFDebugInfo* info = DebugInfo();
2030     if (info == NULL)
2031         return 0;
2032 
2033     const uint32_t sc_list_initial_size = sc_list.GetSize();
2034     SymbolContext sc;
2035     sc.module_sp = m_obj_file->GetModule();
2036     assert (sc.module_sp);
2037 
2038     DWARFCompileUnit* dwarf_cu = NULL;
2039     const size_t num_matches = die_offsets.size();
2040     for (size_t i=0; i<num_matches; ++i)
2041     {
2042         const dw_offset_t die_offset = die_offsets[i];
2043         const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2044 
2045         // If we aren't doing full names,
2046         if ((name_type_mask & eFunctionNameTypeFull) == 0)
2047         {
2048             const char *name_cstr = name.GetCString();
2049             if (ObjCLanguageRuntime::IsPossibleObjCMethodName(name_cstr))
2050                 continue;
2051         }
2052 
2053 
2054         const DWARFDebugInfoEntry* inlined_die = NULL;
2055         if (die->Tag() == DW_TAG_inlined_subroutine)
2056         {
2057             // We only are looking for selectors, which disallows anything inlined
2058             if (name_type_mask == eFunctionNameTypeSelector)
2059                 continue;
2060 
2061             inlined_die = die;
2062 
2063             while ((die = die->GetParent()) != NULL)
2064             {
2065                 if (die->Tag() == DW_TAG_subprogram)
2066                     break;
2067             }
2068         }
2069         assert (die->Tag() == DW_TAG_subprogram);
2070         if (GetFunction (dwarf_cu, die, sc))
2071         {
2072             Address addr;
2073             // Parse all blocks if needed
2074             if (inlined_die)
2075             {
2076                 sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2077                 assert (sc.block != NULL);
2078                 if (sc.block->GetStartAddress (addr) == false)
2079                     addr.Clear();
2080             }
2081             else
2082             {
2083                 sc.block = NULL;
2084                 addr = sc.function->GetAddressRange().GetBaseAddress();
2085             }
2086 
2087             if (addr.IsValid())
2088             {
2089 
2090                 // We found the function, so we should find the line table
2091                 // and line table entry as well
2092                 LineTable *line_table = sc.comp_unit->GetLineTable();
2093                 if (line_table == NULL)
2094                 {
2095                     if (ParseCompileUnitLineTable(sc))
2096                         line_table = sc.comp_unit->GetLineTable();
2097                 }
2098                 if (line_table != NULL)
2099                     line_table->FindLineEntryByAddress (addr, sc.line_entry);
2100 
2101                 sc_list.Append(sc);
2102             }
2103         }
2104     }
2105     return sc_list.GetSize() - sc_list_initial_size;
2106 }
2107 
2108 void
2109 SymbolFileDWARF::FindFunctions
2110 (
2111     const ConstString &name,
2112     const NameToDIE &name_to_die,
2113     SymbolContextList& sc_list
2114 )
2115 {
2116     DWARFDebugInfo* info = DebugInfo();
2117     if (info == NULL)
2118         return;
2119 
2120     SymbolContext sc;
2121     sc.module_sp = m_obj_file->GetModule();
2122     assert (sc.module_sp);
2123 
2124     DWARFCompileUnit* dwarf_cu = NULL;
2125     const DWARFDebugInfoEntry* die = NULL;
2126     DIEArray die_offsets;
2127     const size_t num_matches = name_to_die.Find (name, die_offsets);
2128     if (num_matches)
2129     {
2130         DWARFDebugInfo* debug_info = DebugInfo();
2131         for (size_t i=0; i<num_matches; ++i)
2132         {
2133             const dw_offset_t die_offset = die_offsets[i];
2134             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2135             const DWARFDebugInfoEntry* inlined_die = NULL;
2136             if (die->Tag() == DW_TAG_inlined_subroutine)
2137             {
2138                 inlined_die = die;
2139 
2140                 while ((die = die->GetParent()) != NULL)
2141                 {
2142                     if (die->Tag() == DW_TAG_subprogram)
2143                         break;
2144                 }
2145             }
2146             assert (die->Tag() == DW_TAG_subprogram);
2147             if (GetFunction (dwarf_cu, die, sc))
2148             {
2149                 Address addr;
2150                 // Parse all blocks if needed
2151                 if (inlined_die)
2152                 {
2153                     sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2154                     assert (sc.block != NULL);
2155                     if (sc.block->GetStartAddress (addr) == false)
2156                         addr.Clear();
2157                 }
2158                 else
2159                 {
2160                     sc.block = NULL;
2161                     addr = sc.function->GetAddressRange().GetBaseAddress();
2162                 }
2163 
2164                 if (addr.IsValid())
2165                 {
2166 
2167                     // We found the function, so we should find the line table
2168                     // and line table entry as well
2169                     LineTable *line_table = sc.comp_unit->GetLineTable();
2170                     if (line_table == NULL)
2171                     {
2172                         if (ParseCompileUnitLineTable(sc))
2173                             line_table = sc.comp_unit->GetLineTable();
2174                     }
2175                     if (line_table != NULL)
2176                         line_table->FindLineEntryByAddress (addr, sc.line_entry);
2177 
2178                     sc_list.Append(sc);
2179                 }
2180             }
2181         }
2182     }
2183 }
2184 
2185 
2186 void
2187 SymbolFileDWARF::FindFunctions
2188 (
2189     const RegularExpression &regex,
2190     const NameToDIE &name_to_die,
2191     SymbolContextList& sc_list
2192 )
2193 {
2194     DWARFDebugInfo* info = DebugInfo();
2195     if (info == NULL)
2196         return;
2197 
2198     SymbolContext sc;
2199     sc.module_sp = m_obj_file->GetModule();
2200     assert (sc.module_sp);
2201 
2202     DWARFCompileUnit* dwarf_cu = NULL;
2203     const DWARFDebugInfoEntry* die = NULL;
2204     DIEArray die_offsets;
2205     const size_t num_matches = name_to_die.Find (regex, die_offsets);
2206     if (num_matches)
2207     {
2208         DWARFDebugInfo* debug_info = DebugInfo();
2209         for (size_t i=0; i<num_matches; ++i)
2210         {
2211             const dw_offset_t die_offset = die_offsets[i];
2212             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2213 
2214             const DWARFDebugInfoEntry* inlined_die = NULL;
2215             if (die->Tag() == DW_TAG_inlined_subroutine)
2216             {
2217                 inlined_die = die;
2218 
2219                 while ((die = die->GetParent()) != NULL)
2220                 {
2221                     if (die->Tag() == DW_TAG_subprogram)
2222                         break;
2223                 }
2224             }
2225             assert (die->Tag() == DW_TAG_subprogram);
2226             if (GetFunction (dwarf_cu, die, sc))
2227             {
2228                 Address addr;
2229                 // Parse all blocks if needed
2230                 if (inlined_die)
2231                 {
2232                     sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2233                     assert (sc.block != NULL);
2234                     if (sc.block->GetStartAddress (addr) == false)
2235                         addr.Clear();
2236                 }
2237                 else
2238                 {
2239                     sc.block = NULL;
2240                     addr = sc.function->GetAddressRange().GetBaseAddress();
2241                 }
2242 
2243                 if (addr.IsValid())
2244                 {
2245 
2246                     // We found the function, so we should find the line table
2247                     // and line table entry as well
2248                     LineTable *line_table = sc.comp_unit->GetLineTable();
2249                     if (line_table == NULL)
2250                     {
2251                         if (ParseCompileUnitLineTable(sc))
2252                             line_table = sc.comp_unit->GetLineTable();
2253                     }
2254                     if (line_table != NULL)
2255                         line_table->FindLineEntryByAddress (addr, sc.line_entry);
2256 
2257                     sc_list.Append(sc);
2258                 }
2259             }
2260         }
2261     }
2262 }
2263 
2264 uint32_t
2265 SymbolFileDWARF::FindFunctions (const ConstString &name,
2266                                 uint32_t name_type_mask,
2267                                 bool append,
2268                                 SymbolContextList& sc_list)
2269 {
2270     Timer scoped_timer (__PRETTY_FUNCTION__,
2271                         "SymbolFileDWARF::FindFunctions (name = '%s')",
2272                         name.AsCString());
2273 
2274     // If we aren't appending the results to this list, then clear the list
2275     if (!append)
2276         sc_list.Clear();
2277 
2278     // Remember how many sc_list are in the list before we search in case
2279     // we are appending the results to a variable list.
2280 
2281     const uint32_t original_size = sc_list.GetSize();
2282 
2283     // Index the DWARF if we haven't already
2284     if (!m_indexed)
2285         Index ();
2286 
2287     if (name_type_mask & eFunctionNameTypeBase)
2288         FindFunctions (name, m_function_basename_index, sc_list);
2289 
2290     if (name_type_mask & eFunctionNameTypeFull)
2291         FindFunctions (name, m_function_fullname_index, sc_list);
2292 
2293     if (name_type_mask & eFunctionNameTypeMethod)
2294         FindFunctions (name, m_function_method_index, sc_list);
2295 
2296     if (name_type_mask & eFunctionNameTypeSelector)
2297         FindFunctions (name, m_function_selector_index, sc_list);
2298 
2299     if (m_apple_names.IsValid())
2300     {
2301         SymbolContextList sc_list_apple;
2302         DIEArray die_offsets;
2303         const uint32_t num_matches = m_apple_names.Find(name.GetCString(), die_offsets);
2304         if (num_matches > 0)
2305             ResolveFunctions (die_offsets, sc_list_apple, name, name_type_mask);
2306         if (sc_list != sc_list_apple)
2307             assert (!"__apple_names results differ from DWARF index results");
2308     }
2309 
2310     // Return the number of variable that were appended to the list
2311     return sc_list.GetSize() - original_size;
2312 }
2313 
2314 
2315 uint32_t
2316 SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
2317 {
2318     Timer scoped_timer (__PRETTY_FUNCTION__,
2319                         "SymbolFileDWARF::FindFunctions (regex = '%s')",
2320                         regex.GetText());
2321 
2322     // If we aren't appending the results to this list, then clear the list
2323     if (!append)
2324         sc_list.Clear();
2325 
2326     // Remember how many sc_list are in the list before we search in case
2327     // we are appending the results to a variable list.
2328     uint32_t original_size = sc_list.GetSize();
2329 
2330     // Index the DWARF if we haven't already
2331     if (!m_indexed)
2332         Index ();
2333 
2334     FindFunctions (regex, m_function_basename_index, sc_list);
2335 
2336     FindFunctions (regex, m_function_fullname_index, sc_list);
2337 
2338     // Return the number of variable that were appended to the list
2339     return sc_list.GetSize() - original_size;
2340 }
2341 
2342 void
2343 SymbolFileDWARF::ReportError (const char *format, ...)
2344 {
2345     ::fprintf (stderr,
2346                "error: %s/%s ",
2347                m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2348                m_obj_file->GetFileSpec().GetFilename().GetCString());
2349 
2350     if (m_obj_file->GetModule()->GetObjectName())
2351         ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString());
2352 
2353     va_list args;
2354     va_start (args, format);
2355     vfprintf (stderr, format, args);
2356     va_end (args);
2357 }
2358 
2359 void
2360 SymbolFileDWARF::ReportWarning (const char *format, ...)
2361 {
2362     ::fprintf (stderr,
2363                "warning: %s/%s ",
2364                m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2365                m_obj_file->GetFileSpec().GetFilename().GetCString());
2366 
2367     if (m_obj_file->GetModule()->GetObjectName())
2368         ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString());
2369 
2370     va_list args;
2371     va_start (args, format);
2372     vfprintf (stderr, format, args);
2373     va_end (args);
2374 }
2375 
2376 uint32_t
2377 SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, bool append, uint32_t max_matches, TypeList& types)
2378 {
2379     DWARFDebugInfo* info = DebugInfo();
2380     if (info == NULL)
2381         return 0;
2382 
2383     // If we aren't appending the results to this list, then clear the list
2384     if (!append)
2385         types.Clear();
2386 
2387     // Index if we already haven't to make sure the compile units
2388     // get indexed and make their global DIE index list
2389     if (!m_indexed)
2390         Index ();
2391 
2392     const uint32_t initial_types_size = types.GetSize();
2393     DWARFCompileUnit* dwarf_cu = NULL;
2394     const DWARFDebugInfoEntry* die = NULL;
2395     DIEArray die_offsets;
2396     const size_t num_matches = m_type_index.Find (name, die_offsets);
2397     if (num_matches)
2398     {
2399         DWARFDebugInfo* debug_info = DebugInfo();
2400         for (size_t i=0; i<num_matches; ++i)
2401         {
2402             const dw_offset_t die_offset = die_offsets[i];
2403             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2404 
2405             Type *matching_type = ResolveType (dwarf_cu, die);
2406             if (matching_type)
2407             {
2408                 // We found a type pointer, now find the shared pointer form our type list
2409                 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
2410                 if (type_sp)
2411                 {
2412                     types.InsertUnique (type_sp);
2413                     if (types.GetSize() >= max_matches)
2414                         break;
2415                 }
2416                 else
2417                 {
2418                     ReportError ("error: can't find shared pointer for type 0x%8.8x.\n", matching_type->GetID());
2419                 }
2420             }
2421         }
2422     }
2423     return types.GetSize() - initial_types_size;
2424 }
2425 
2426 
2427 ClangNamespaceDecl
2428 SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
2429                                 const ConstString &name)
2430 {
2431     ClangNamespaceDecl namespace_decl;
2432     DWARFDebugInfo* info = DebugInfo();
2433     if (info)
2434     {
2435         // Index if we already haven't to make sure the compile units
2436         // get indexed and make their global DIE index list
2437         if (!m_indexed)
2438             Index ();
2439 
2440 
2441         DWARFCompileUnit* dwarf_cu = NULL;
2442         const DWARFDebugInfoEntry* die = NULL;
2443         DIEArray die_offsets;
2444         const size_t num_matches = m_namespace_index.Find (name, die_offsets);
2445         if (num_matches)
2446         {
2447             DWARFDebugInfo* debug_info = DebugInfo();
2448             for (size_t i=0; i<num_matches; ++i)
2449             {
2450                 const dw_offset_t die_offset = die_offsets[i];
2451                 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2452 
2453                 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
2454                 if (clang_namespace_decl)
2455                 {
2456                     namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
2457                     namespace_decl.SetNamespaceDecl (clang_namespace_decl);
2458                 }
2459             }
2460         }
2461     }
2462     return namespace_decl;
2463 }
2464 
2465 uint32_t
2466 SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
2467 {
2468     // Remember how many sc_list are in the list before we search in case
2469     // we are appending the results to a variable list.
2470     uint32_t original_size = types.GetSize();
2471 
2472     const uint32_t num_die_offsets = die_offsets.size();
2473     // Parse all of the types we found from the pubtypes matches
2474     uint32_t i;
2475     uint32_t num_matches = 0;
2476     for (i = 0; i < num_die_offsets; ++i)
2477     {
2478         Type *matching_type = ResolveTypeUID (die_offsets[i]);
2479         if (matching_type)
2480         {
2481             // We found a type pointer, now find the shared pointer form our type list
2482             TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
2483             assert (type_sp.get() != NULL);
2484             types.InsertUnique (type_sp);
2485             ++num_matches;
2486             if (num_matches >= max_matches)
2487                 break;
2488         }
2489     }
2490 
2491     // Return the number of variable that were appended to the list
2492     return types.GetSize() - original_size;
2493 }
2494 
2495 
2496 size_t
2497 SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
2498                                        clang::DeclContext *containing_decl_ctx,
2499                                        TypeSP& type_sp,
2500                                        DWARFCompileUnit* dwarf_cu,
2501                                        const DWARFDebugInfoEntry *parent_die,
2502                                        bool skip_artificial,
2503                                        bool &is_static,
2504                                        TypeList* type_list,
2505                                        std::vector<clang_type_t>& function_param_types,
2506                                        std::vector<clang::ParmVarDecl*>& function_param_decls,
2507                                        unsigned &type_quals)
2508 {
2509     if (parent_die == NULL)
2510         return 0;
2511 
2512     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2513 
2514     size_t arg_idx = 0;
2515     const DWARFDebugInfoEntry *die;
2516     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2517     {
2518         dw_tag_t tag = die->Tag();
2519         switch (tag)
2520         {
2521         case DW_TAG_formal_parameter:
2522             {
2523                 DWARFDebugInfoEntry::Attributes attributes;
2524                 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
2525                 if (num_attributes > 0)
2526                 {
2527                     const char *name = NULL;
2528                     Declaration decl;
2529                     dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
2530                     bool is_artificial = false;
2531                     // one of None, Auto, Register, Extern, Static, PrivateExtern
2532 
2533                     clang::StorageClass storage = clang::SC_None;
2534                     uint32_t i;
2535                     for (i=0; i<num_attributes; ++i)
2536                     {
2537                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
2538                         DWARFFormValue form_value;
2539                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2540                         {
2541                             switch (attr)
2542                             {
2543                             case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2544                             case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
2545                             case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2546                             case DW_AT_name:        name = form_value.AsCString(&get_debug_str_data()); break;
2547                             case DW_AT_type:        param_type_die_offset = form_value.Reference(dwarf_cu); break;
2548                             case DW_AT_artificial:  is_artificial = form_value.Unsigned() != 0; break;
2549                             case DW_AT_location:
2550     //                          if (form_value.BlockData())
2551     //                          {
2552     //                              const DataExtractor& debug_info_data = debug_info();
2553     //                              uint32_t block_length = form_value.Unsigned();
2554     //                              DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
2555     //                          }
2556     //                          else
2557     //                          {
2558     //                          }
2559     //                          break;
2560                             case DW_AT_const_value:
2561                             case DW_AT_default_value:
2562                             case DW_AT_description:
2563                             case DW_AT_endianity:
2564                             case DW_AT_is_optional:
2565                             case DW_AT_segment:
2566                             case DW_AT_variable_parameter:
2567                             default:
2568                             case DW_AT_abstract_origin:
2569                             case DW_AT_sibling:
2570                                 break;
2571                             }
2572                         }
2573                     }
2574 
2575                     bool skip = false;
2576                     if (skip_artificial)
2577                     {
2578                         if (is_artificial)
2579                         {
2580                             // In order to determine if a C++ member function is
2581                             // "const" we have to look at the const-ness of "this"...
2582                             // Ugly, but that
2583                             if (arg_idx == 0)
2584                             {
2585                                 if (containing_decl_ctx->getDeclKind() == clang::Decl::CXXRecord)
2586                                 {
2587                                     // Often times compilers omit the "this" name for the
2588                                     // specification DIEs, so we can't rely upon the name
2589                                     // being in the formal parameter DIE...
2590                                     if (name == NULL || ::strcmp(name, "this")==0)
2591                                     {
2592                                         Type *this_type = ResolveTypeUID (param_type_die_offset);
2593                                         if (this_type)
2594                                         {
2595                                             uint32_t encoding_mask = this_type->GetEncodingMask();
2596                                             if (encoding_mask & Type::eEncodingIsPointerUID)
2597                                             {
2598                                                 is_static = false;
2599 
2600                                                 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
2601                                                     type_quals |= clang::Qualifiers::Const;
2602                                                 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
2603                                                     type_quals |= clang::Qualifiers::Volatile;
2604                                             }
2605                                         }
2606                                     }
2607                                 }
2608                             }
2609                             skip = true;
2610                         }
2611                         else
2612                         {
2613 
2614                             // HACK: Objective C formal parameters "self" and "_cmd"
2615                             // are not marked as artificial in the DWARF...
2616                             CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2617                             if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
2618                             {
2619                                 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
2620                                     skip = true;
2621                             }
2622                         }
2623                     }
2624 
2625                     if (!skip)
2626                     {
2627                         Type *type = ResolveTypeUID(param_type_die_offset);
2628                         if (type)
2629                         {
2630                             function_param_types.push_back (type->GetClangForwardType());
2631 
2632                             clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, type->GetClangForwardType(), storage);
2633                             assert(param_var_decl);
2634                             function_param_decls.push_back(param_var_decl);
2635                         }
2636                     }
2637                 }
2638                 arg_idx++;
2639             }
2640             break;
2641 
2642         default:
2643             break;
2644         }
2645     }
2646     return arg_idx;
2647 }
2648 
2649 size_t
2650 SymbolFileDWARF::ParseChildEnumerators
2651 (
2652     const SymbolContext& sc,
2653     clang_type_t  enumerator_clang_type,
2654     uint32_t enumerator_byte_size,
2655     DWARFCompileUnit* dwarf_cu,
2656     const DWARFDebugInfoEntry *parent_die
2657 )
2658 {
2659     if (parent_die == NULL)
2660         return 0;
2661 
2662     size_t enumerators_added = 0;
2663     const DWARFDebugInfoEntry *die;
2664     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2665 
2666     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2667     {
2668         const dw_tag_t tag = die->Tag();
2669         if (tag == DW_TAG_enumerator)
2670         {
2671             DWARFDebugInfoEntry::Attributes attributes;
2672             const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
2673             if (num_child_attributes > 0)
2674             {
2675                 const char *name = NULL;
2676                 bool got_value = false;
2677                 int64_t enum_value = 0;
2678                 Declaration decl;
2679 
2680                 uint32_t i;
2681                 for (i=0; i<num_child_attributes; ++i)
2682                 {
2683                     const dw_attr_t attr = attributes.AttributeAtIndex(i);
2684                     DWARFFormValue form_value;
2685                     if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2686                     {
2687                         switch (attr)
2688                         {
2689                         case DW_AT_const_value:
2690                             got_value = true;
2691                             enum_value = form_value.Unsigned();
2692                             break;
2693 
2694                         case DW_AT_name:
2695                             name = form_value.AsCString(&get_debug_str_data());
2696                             break;
2697 
2698                         case DW_AT_description:
2699                         default:
2700                         case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2701                         case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
2702                         case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2703                         case DW_AT_sibling:
2704                             break;
2705                         }
2706                     }
2707                 }
2708 
2709                 if (name && name[0] && got_value)
2710                 {
2711                     GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
2712                                                                                enumerator_clang_type,
2713                                                                                decl,
2714                                                                                name,
2715                                                                                enum_value,
2716                                                                                enumerator_byte_size * 8);
2717                     ++enumerators_added;
2718                 }
2719             }
2720         }
2721     }
2722     return enumerators_added;
2723 }
2724 
2725 void
2726 SymbolFileDWARF::ParseChildArrayInfo
2727 (
2728     const SymbolContext& sc,
2729     DWARFCompileUnit* dwarf_cu,
2730     const DWARFDebugInfoEntry *parent_die,
2731     int64_t& first_index,
2732     std::vector<uint64_t>& element_orders,
2733     uint32_t& byte_stride,
2734     uint32_t& bit_stride
2735 )
2736 {
2737     if (parent_die == NULL)
2738         return;
2739 
2740     const DWARFDebugInfoEntry *die;
2741     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2742     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2743     {
2744         const dw_tag_t tag = die->Tag();
2745         switch (tag)
2746         {
2747         case DW_TAG_enumerator:
2748             {
2749                 DWARFDebugInfoEntry::Attributes attributes;
2750                 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
2751                 if (num_child_attributes > 0)
2752                 {
2753                     const char *name = NULL;
2754                     bool got_value = false;
2755                     int64_t enum_value = 0;
2756 
2757                     uint32_t i;
2758                     for (i=0; i<num_child_attributes; ++i)
2759                     {
2760                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
2761                         DWARFFormValue form_value;
2762                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2763                         {
2764                             switch (attr)
2765                             {
2766                             case DW_AT_const_value:
2767                                 got_value = true;
2768                                 enum_value = form_value.Unsigned();
2769                                 break;
2770 
2771                             case DW_AT_name:
2772                                 name = form_value.AsCString(&get_debug_str_data());
2773                                 break;
2774 
2775                             case DW_AT_description:
2776                             default:
2777                             case DW_AT_decl_file:
2778                             case DW_AT_decl_line:
2779                             case DW_AT_decl_column:
2780                             case DW_AT_sibling:
2781                                 break;
2782                             }
2783                         }
2784                     }
2785                 }
2786             }
2787             break;
2788 
2789         case DW_TAG_subrange_type:
2790             {
2791                 DWARFDebugInfoEntry::Attributes attributes;
2792                 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
2793                 if (num_child_attributes > 0)
2794                 {
2795                     const char *name = NULL;
2796                     bool got_value = false;
2797                     uint64_t byte_size = 0;
2798                     int64_t enum_value = 0;
2799                     uint64_t num_elements = 0;
2800                     uint64_t lower_bound = 0;
2801                     uint64_t upper_bound = 0;
2802                     uint32_t i;
2803                     for (i=0; i<num_child_attributes; ++i)
2804                     {
2805                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
2806                         DWARFFormValue form_value;
2807                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2808                         {
2809                             switch (attr)
2810                             {
2811                             case DW_AT_const_value:
2812                                 got_value = true;
2813                                 enum_value = form_value.Unsigned();
2814                                 break;
2815 
2816                             case DW_AT_name:
2817                                 name = form_value.AsCString(&get_debug_str_data());
2818                                 break;
2819 
2820                             case DW_AT_count:
2821                                 num_elements = form_value.Unsigned();
2822                                 break;
2823 
2824                             case DW_AT_bit_stride:
2825                                 bit_stride = form_value.Unsigned();
2826                                 break;
2827 
2828                             case DW_AT_byte_stride:
2829                                 byte_stride = form_value.Unsigned();
2830                                 break;
2831 
2832                             case DW_AT_byte_size:
2833                                 byte_size = form_value.Unsigned();
2834                                 break;
2835 
2836                             case DW_AT_lower_bound:
2837                                 lower_bound = form_value.Unsigned();
2838                                 break;
2839 
2840                             case DW_AT_upper_bound:
2841                                 upper_bound = form_value.Unsigned();
2842                                 break;
2843 
2844                             default:
2845                             case DW_AT_abstract_origin:
2846                             case DW_AT_accessibility:
2847                             case DW_AT_allocated:
2848                             case DW_AT_associated:
2849                             case DW_AT_data_location:
2850                             case DW_AT_declaration:
2851                             case DW_AT_description:
2852                             case DW_AT_sibling:
2853                             case DW_AT_threads_scaled:
2854                             case DW_AT_type:
2855                             case DW_AT_visibility:
2856                                 break;
2857                             }
2858                         }
2859                     }
2860 
2861                     if (upper_bound > lower_bound)
2862                         num_elements = upper_bound - lower_bound + 1;
2863 
2864                     if (num_elements > 0)
2865                         element_orders.push_back (num_elements);
2866                 }
2867             }
2868             break;
2869         }
2870     }
2871 }
2872 
2873 TypeSP
2874 SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
2875 {
2876     TypeSP type_sp;
2877     if (die != NULL)
2878     {
2879         assert(curr_cu != NULL);
2880         Type *type_ptr = m_die_to_type.lookup (die);
2881         if (type_ptr == NULL)
2882         {
2883             CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
2884             assert (lldb_cu);
2885             SymbolContext sc(lldb_cu);
2886             type_sp = ParseType(sc, curr_cu, die, NULL);
2887         }
2888         else if (type_ptr != DIE_IS_BEING_PARSED)
2889         {
2890             // Grab the existing type from the master types lists
2891             type_sp = GetTypeList()->FindType(type_ptr->GetID());
2892         }
2893 
2894     }
2895     return type_sp;
2896 }
2897 
2898 clang::DeclContext *
2899 SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
2900 {
2901     if (die_offset != DW_INVALID_OFFSET)
2902     {
2903         DWARFCompileUnitSP cu_sp;
2904         const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
2905         return GetClangDeclContextContainingDIE (cu_sp.get(), die);
2906     }
2907     return NULL;
2908 }
2909 
2910 clang::DeclContext *
2911 SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
2912 {
2913     if (die_offset != DW_INVALID_OFFSET)
2914     {
2915         DWARFCompileUnitSP cu_sp;
2916         const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
2917         return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
2918     }
2919     return NULL;
2920 }
2921 
2922 clang::NamespaceDecl *
2923 SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
2924 {
2925     if (die->Tag() == DW_TAG_namespace)
2926     {
2927         const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
2928         if (namespace_name)
2929         {
2930             Declaration decl;   // TODO: fill in the decl object
2931             clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextContainingDIE (curr_cu, die->GetParent()));
2932             if (namespace_decl)
2933                 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
2934             return namespace_decl;
2935         }
2936     }
2937     return NULL;
2938 }
2939 
2940 clang::DeclContext *
2941 SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
2942 {
2943     // If this DIE has a specification, or an abstract origin, then trace to those.
2944 
2945     dw_offset_t die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_specification, DW_INVALID_OFFSET);
2946     if (die_offset != DW_INVALID_OFFSET)
2947         return GetClangDeclContextForDIEOffset (sc, die_offset);
2948 
2949     die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
2950     if (die_offset != DW_INVALID_OFFSET)
2951         return GetClangDeclContextForDIEOffset (sc, die_offset);
2952 
2953     // This is the DIE we want.  Parse it, then query our map.
2954 
2955     ParseType(sc, curr_cu, die, NULL);
2956 
2957     DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die);
2958     if (pos != m_die_to_decl_ctx.end())
2959         return pos->second;
2960     else
2961         return NULL;
2962 }
2963 
2964 clang::DeclContext *
2965 SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
2966 {
2967     if (m_clang_tu_decl == NULL)
2968         m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
2969 
2970     const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
2971 
2972     if (decl_ctx_die)
2973     {
2974         DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
2975         if (pos != m_die_to_decl_ctx.end())
2976             return pos->second;
2977 
2978         switch (decl_ctx_die->Tag())
2979         {
2980         case DW_TAG_compile_unit:
2981             return m_clang_tu_decl;
2982 
2983         case DW_TAG_namespace:
2984             {
2985                 const char *namespace_name = decl_ctx_die->GetAttributeValueAsString(this, cu, DW_AT_name, NULL);
2986                 if (namespace_name)
2987                 {
2988                     Declaration decl;   // TODO: fill in the decl object
2989                     clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextContainingDIE (cu, decl_ctx_die));
2990                     if (namespace_decl)
2991                         LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, decl_ctx_die);
2992                     return namespace_decl;
2993                 }
2994             }
2995             break;
2996 
2997         case DW_TAG_structure_type:
2998         case DW_TAG_union_type:
2999         case DW_TAG_class_type:
3000             {
3001                 Type* type = ResolveType (cu, decl_ctx_die);
3002                 if (type)
3003                 {
3004                     clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
3005                     if (decl_ctx)
3006                     {
3007                         LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
3008                         if (decl_ctx)
3009                             return decl_ctx;
3010                     }
3011                 }
3012             }
3013             break;
3014 
3015         default:
3016             break;
3017         }
3018     }
3019     return m_clang_tu_decl;
3020 }
3021 
3022 
3023 const DWARFDebugInfoEntry *
3024 SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
3025 {
3026     if (cu && die)
3027     {
3028         const DWARFDebugInfoEntry * const decl_die = die;
3029 
3030         while (die != NULL)
3031         {
3032             // If this is the original DIE that we are searching for a declaration
3033             // for, then don't look in the cache as we don't want our own decl
3034             // context to be our decl context...
3035             if (decl_die != die)
3036             {
3037                 switch (die->Tag())
3038                 {
3039                     case DW_TAG_compile_unit:
3040                     case DW_TAG_namespace:
3041                     case DW_TAG_structure_type:
3042                     case DW_TAG_union_type:
3043                     case DW_TAG_class_type:
3044                         return die;
3045 
3046                     default:
3047                         break;
3048                 }
3049             }
3050 
3051             dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
3052             if (die_offset != DW_INVALID_OFFSET)
3053             {
3054                 DWARFCompileUnit *spec_cu = cu;
3055                 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
3056                 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
3057                 if (spec_die_decl_ctx_die)
3058                     return spec_die_decl_ctx_die;
3059             }
3060 
3061             die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3062             if (die_offset != DW_INVALID_OFFSET)
3063             {
3064                 DWARFCompileUnit *abs_cu = cu;
3065                 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
3066                 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
3067                 if (abs_die_decl_ctx_die)
3068                     return abs_die_decl_ctx_die;
3069             }
3070 
3071             die = die->GetParent();
3072         }
3073     }
3074     return NULL;
3075 }
3076 
3077 
3078 
3079 // This function can be used when a DIE is found that is a forward declaration
3080 // DIE and we want to try and find a type that has the complete definition.
3081 TypeSP
3082 SymbolFileDWARF::FindDefinitionTypeForDIE (
3083     DWARFCompileUnit* cu,
3084     const DWARFDebugInfoEntry *die,
3085     const ConstString &type_name
3086 )
3087 {
3088     TypeSP type_sp;
3089 
3090     if (cu == NULL || die == NULL || !type_name)
3091         return type_sp;
3092 
3093     if (!m_indexed)
3094         Index ();
3095 
3096     const dw_tag_t type_tag = die->Tag();
3097 
3098     DWARFCompileUnit* type_cu = NULL;
3099     const DWARFDebugInfoEntry* type_die = NULL;
3100     DIEArray die_offsets;
3101     const size_t num_matches = m_type_index.Find (type_name, die_offsets);
3102     if (num_matches)
3103     {
3104         DWARFDebugInfo* debug_info = DebugInfo();
3105         for (size_t i=0; i<num_matches; ++i)
3106         {
3107             const dw_offset_t die_offset = die_offsets[i];
3108             type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
3109 
3110             if (type_die != die && type_die->Tag() == type_tag)
3111             {
3112                 // Hold off on comparing parent DIE tags until
3113                 // we know what happens with stuff in namespaces
3114                 // for gcc and clang...
3115                 //DWARFDebugInfoEntry *parent_die = die->GetParent();
3116                 //DWARFDebugInfoEntry *parent_type_die = type_die->GetParent();
3117                 //if (parent_die->Tag() == parent_type_die->Tag())
3118                 {
3119                     Type *resolved_type = ResolveType (type_cu, type_die, false);
3120                     if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3121                     {
3122                         DEBUG_PRINTF ("resolved 0x%8.8x (cu 0x%8.8x) from %s to 0x%8.8x (cu 0x%8.8x)\n",
3123                                       die->GetOffset(),
3124                                       curr_cu->GetOffset(),
3125                                       m_obj_file->GetFileSpec().GetFilename().AsCString(),
3126                                       type_die->GetOffset(),
3127                                       type_cu->GetOffset());
3128 
3129                         m_die_to_type[die] = resolved_type;
3130                         type_sp = GetTypeList()->FindType(resolved_type->GetID());
3131                         if (!type_sp)
3132                         {
3133                             DEBUG_PRINTF("unable to resolve type '%s' from DIE 0x%8.8x\n", type_name.GetCString(), die->GetOffset());
3134                         }
3135                         break;
3136                     }
3137                 }
3138             }
3139         }
3140     }
3141     return type_sp;
3142 }
3143 
3144 TypeSP
3145 SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
3146 {
3147     TypeSP type_sp;
3148 
3149     if (type_is_new_ptr)
3150         *type_is_new_ptr = false;
3151 
3152     AccessType accessibility = eAccessNone;
3153     if (die != NULL)
3154     {
3155         Log *log = LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO);
3156         if (log && dwarf_cu)
3157         {
3158             StreamString s;
3159             die->DumpLocation (this, dwarf_cu, s);
3160             log->Printf ("SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
3161 
3162         }
3163 
3164         Type *type_ptr = m_die_to_type.lookup (die);
3165         TypeList* type_list = GetTypeList();
3166         if (type_ptr == NULL)
3167         {
3168             ClangASTContext &ast = GetClangASTContext();
3169             if (type_is_new_ptr)
3170                 *type_is_new_ptr = true;
3171 
3172             const dw_tag_t tag = die->Tag();
3173 
3174             bool is_forward_declaration = false;
3175             DWARFDebugInfoEntry::Attributes attributes;
3176             const char *type_name_cstr = NULL;
3177             ConstString type_name_const_str;
3178             Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
3179             size_t byte_size = 0;
3180             bool byte_size_valid = false;
3181             Declaration decl;
3182 
3183             Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
3184             clang_type_t clang_type = NULL;
3185 
3186             dw_attr_t attr;
3187 
3188             switch (tag)
3189             {
3190             case DW_TAG_base_type:
3191             case DW_TAG_pointer_type:
3192             case DW_TAG_reference_type:
3193             case DW_TAG_typedef:
3194             case DW_TAG_const_type:
3195             case DW_TAG_restrict_type:
3196             case DW_TAG_volatile_type:
3197                 {
3198                     // Set a bit that lets us know that we are currently parsing this
3199                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3200 
3201                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3202                     uint32_t encoding = 0;
3203                     lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
3204 
3205                     if (num_attributes > 0)
3206                     {
3207                         uint32_t i;
3208                         for (i=0; i<num_attributes; ++i)
3209                         {
3210                             attr = attributes.AttributeAtIndex(i);
3211                             DWARFFormValue form_value;
3212                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3213                             {
3214                                 switch (attr)
3215                                 {
3216                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3217                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3218                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3219                                 case DW_AT_name:
3220 
3221                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3222                                     // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
3223                                     // include the "&"...
3224                                     if (tag == DW_TAG_reference_type)
3225                                     {
3226                                         if (strchr (type_name_cstr, '&') == NULL)
3227                                             type_name_cstr = NULL;
3228                                     }
3229                                     if (type_name_cstr)
3230                                         type_name_const_str.SetCString(type_name_cstr);
3231                                     break;
3232                                 case DW_AT_byte_size:   byte_size = form_value.Unsigned();  byte_size_valid = true; break;
3233                                 case DW_AT_encoding:    encoding = form_value.Unsigned(); break;
3234                                 case DW_AT_type:        encoding_uid = form_value.Reference(dwarf_cu); break;
3235                                 default:
3236                                 case DW_AT_sibling:
3237                                     break;
3238                                 }
3239                             }
3240                         }
3241                     }
3242 
3243                     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);
3244 
3245                     switch (tag)
3246                     {
3247                     default:
3248                         break;
3249 
3250                     case DW_TAG_base_type:
3251                         resolve_state = Type::eResolveStateFull;
3252                         clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
3253                                                                                    encoding,
3254                                                                                    byte_size * 8);
3255                         break;
3256 
3257                     case DW_TAG_pointer_type:   encoding_data_type = Type::eEncodingIsPointerUID;           break;
3258                     case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID;   break;
3259                     case DW_TAG_typedef:        encoding_data_type = Type::eEncodingIsTypedefUID;           break;
3260                     case DW_TAG_const_type:     encoding_data_type = Type::eEncodingIsConstUID;             break;
3261                     case DW_TAG_restrict_type:  encoding_data_type = Type::eEncodingIsRestrictUID;          break;
3262                     case DW_TAG_volatile_type:  encoding_data_type = Type::eEncodingIsVolatileUID;          break;
3263                     }
3264 
3265                     if (type_name_cstr != NULL && sc.comp_unit != NULL &&
3266                         (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
3267                     {
3268                         static ConstString g_objc_type_name_id("id");
3269                         static ConstString g_objc_type_name_Class("Class");
3270                         static ConstString g_objc_type_name_selector("SEL");
3271 
3272                         if (type_name_const_str == g_objc_type_name_id)
3273                         {
3274                             clang_type = ast.GetBuiltInType_objc_id();
3275                             resolve_state = Type::eResolveStateFull;
3276 
3277                         }
3278                         else if (type_name_const_str == g_objc_type_name_Class)
3279                         {
3280                             clang_type = ast.GetBuiltInType_objc_Class();
3281                             resolve_state = Type::eResolveStateFull;
3282                         }
3283                         else if (type_name_const_str == g_objc_type_name_selector)
3284                         {
3285                             clang_type = ast.GetBuiltInType_objc_selector();
3286                             resolve_state = Type::eResolveStateFull;
3287                         }
3288                     }
3289 
3290                     type_sp.reset( new Type (die->GetOffset(),
3291                                              this,
3292                                              type_name_const_str,
3293                                              byte_size,
3294                                              NULL,
3295                                              encoding_uid,
3296                                              encoding_data_type,
3297                                              &decl,
3298                                              clang_type,
3299                                              resolve_state));
3300 
3301                     m_die_to_type[die] = type_sp.get();
3302 
3303 //                  Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
3304 //                  if (encoding_type != NULL)
3305 //                  {
3306 //                      if (encoding_type != DIE_IS_BEING_PARSED)
3307 //                          type_sp->SetEncodingType(encoding_type);
3308 //                      else
3309 //                          m_indirect_fixups.push_back(type_sp.get());
3310 //                  }
3311                 }
3312                 break;
3313 
3314             case DW_TAG_structure_type:
3315             case DW_TAG_union_type:
3316             case DW_TAG_class_type:
3317                 {
3318                     // Set a bit that lets us know that we are currently parsing this
3319                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3320 
3321                     LanguageType class_language = eLanguageTypeUnknown;
3322                     //bool struct_is_class = false;
3323                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3324                     if (num_attributes > 0)
3325                     {
3326                         uint32_t i;
3327                         for (i=0; i<num_attributes; ++i)
3328                         {
3329                             attr = attributes.AttributeAtIndex(i);
3330                             DWARFFormValue form_value;
3331                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3332                             {
3333                                 switch (attr)
3334                                 {
3335                                 case DW_AT_decl_file:
3336                                     decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
3337                                     break;
3338 
3339                                 case DW_AT_decl_line:
3340                                     decl.SetLine(form_value.Unsigned());
3341                                     break;
3342 
3343                                 case DW_AT_decl_column:
3344                                     decl.SetColumn(form_value.Unsigned());
3345                                     break;
3346 
3347                                 case DW_AT_name:
3348                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3349                                     type_name_const_str.SetCString(type_name_cstr);
3350                                     break;
3351 
3352                                 case DW_AT_byte_size:
3353                                     byte_size = form_value.Unsigned();
3354                                     byte_size_valid = true;
3355                                     break;
3356 
3357                                 case DW_AT_accessibility:
3358                                     accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3359                                     break;
3360 
3361                                 case DW_AT_declaration:
3362                                     is_forward_declaration = form_value.Unsigned() != 0;
3363                                     break;
3364 
3365                                 case DW_AT_APPLE_runtime_class:
3366                                     class_language = (LanguageType)form_value.Signed();
3367                                     break;
3368 
3369                                 case DW_AT_allocated:
3370                                 case DW_AT_associated:
3371                                 case DW_AT_data_location:
3372                                 case DW_AT_description:
3373                                 case DW_AT_start_scope:
3374                                 case DW_AT_visibility:
3375                                 default:
3376                                 case DW_AT_sibling:
3377                                     break;
3378                                 }
3379                             }
3380                         }
3381                     }
3382 
3383                     UniqueDWARFASTType unique_ast_entry;
3384                     if (decl.IsValid())
3385                     {
3386                         if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
3387                                                              this,
3388                                                              dwarf_cu,
3389                                                              die,
3390                                                              decl,
3391                                                              byte_size_valid ? byte_size : -1,
3392                                                              unique_ast_entry))
3393                         {
3394                             // We have already parsed this type or from another
3395                             // compile unit. GCC loves to use the "one definition
3396                             // rule" which can result in multiple definitions
3397                             // of the same class over and over in each compile
3398                             // unit.
3399                             type_sp = unique_ast_entry.m_type_sp;
3400                             if (type_sp)
3401                             {
3402                                 m_die_to_type[die] = type_sp.get();
3403                                 return type_sp;
3404                             }
3405                         }
3406                     }
3407 
3408                     DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3409 
3410                     int tag_decl_kind = -1;
3411                     AccessType default_accessibility = eAccessNone;
3412                     if (tag == DW_TAG_structure_type)
3413                     {
3414                         tag_decl_kind = clang::TTK_Struct;
3415                         default_accessibility = eAccessPublic;
3416                     }
3417                     else if (tag == DW_TAG_union_type)
3418                     {
3419                         tag_decl_kind = clang::TTK_Union;
3420                         default_accessibility = eAccessPublic;
3421                     }
3422                     else if (tag == DW_TAG_class_type)
3423                     {
3424                         tag_decl_kind = clang::TTK_Class;
3425                         default_accessibility = eAccessPrivate;
3426                     }
3427 
3428 
3429                     if (is_forward_declaration)
3430                     {
3431                         // We have a forward declaration to a type and we need
3432                         // to try and find a full declaration. We look in the
3433                         // current type index just in case we have a forward
3434                         // declaration followed by an actual declarations in the
3435                         // DWARF. If this fails, we need to look elsewhere...
3436 
3437                         type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
3438 
3439                         if (!type_sp && m_debug_map_symfile)
3440                         {
3441                             // We weren't able to find a full declaration in
3442                             // this DWARF, see if we have a declaration anywhere
3443                             // else...
3444                             type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
3445                         }
3446 
3447                         if (type_sp)
3448                         {
3449                             // We found a real definition for this type elsewhere
3450                             // so lets use it and cache the fact that we found
3451                             // a complete type for this die
3452                             m_die_to_type[die] = type_sp.get();
3453                             return type_sp;
3454                         }
3455                     }
3456                     assert (tag_decl_kind != -1);
3457                     bool clang_type_was_created = false;
3458                     clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3459                     if (clang_type == NULL)
3460                     {
3461                         clang_type_was_created = true;
3462                         clang_type = ast.CreateRecordType (type_name_cstr,
3463                                                            tag_decl_kind,
3464                                                            GetClangDeclContextContainingDIE (dwarf_cu, die),
3465                                                            class_language);
3466                     }
3467 
3468                     // Store a forward declaration to this class type in case any
3469                     // parameters in any class methods need it for the clang
3470                     // types for function prototypes.
3471                     LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
3472                     type_sp.reset (new Type (die->GetOffset(),
3473                                              this,
3474                                              type_name_const_str,
3475                                              byte_size,
3476                                              NULL,
3477                                              LLDB_INVALID_UID,
3478                                              Type::eEncodingIsUID,
3479                                              &decl,
3480                                              clang_type,
3481                                              Type::eResolveStateForward));
3482 
3483 
3484                     // Add our type to the unique type map so we don't
3485                     // end up creating many copies of the same type over
3486                     // and over in the ASTContext for our module
3487                     unique_ast_entry.m_type_sp = type_sp;
3488                     unique_ast_entry.m_symfile = this;
3489                     unique_ast_entry.m_cu = dwarf_cu;
3490                     unique_ast_entry.m_die = die;
3491                     unique_ast_entry.m_declaration = decl;
3492                     GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
3493                                                        unique_ast_entry);
3494 
3495                     if (die->HasChildren() == false && is_forward_declaration == false)
3496                     {
3497                         // No children for this struct/union/class, lets finish it
3498                         ast.StartTagDeclarationDefinition (clang_type);
3499                         ast.CompleteTagDeclarationDefinition (clang_type);
3500                     }
3501                     else if (clang_type_was_created)
3502                     {
3503                         // Leave this as a forward declaration until we need
3504                         // to know the details of the type. lldb_private::Type
3505                         // will automatically call the SymbolFile virtual function
3506                         // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3507                         // When the definition needs to be defined.
3508                         m_forward_decl_die_to_clang_type[die] = clang_type;
3509                         m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
3510                         ClangASTContext::SetHasExternalStorage (clang_type, true);
3511                     }
3512                 }
3513                 break;
3514 
3515             case DW_TAG_enumeration_type:
3516                 {
3517                     // Set a bit that lets us know that we are currently parsing this
3518                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3519 
3520                     lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
3521 
3522                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3523                     if (num_attributes > 0)
3524                     {
3525                         uint32_t i;
3526 
3527                         for (i=0; i<num_attributes; ++i)
3528                         {
3529                             attr = attributes.AttributeAtIndex(i);
3530                             DWARFFormValue form_value;
3531                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3532                             {
3533                                 switch (attr)
3534                                 {
3535                                 case DW_AT_decl_file:       decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3536                                 case DW_AT_decl_line:       decl.SetLine(form_value.Unsigned()); break;
3537                                 case DW_AT_decl_column:     decl.SetColumn(form_value.Unsigned()); break;
3538                                 case DW_AT_name:
3539                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3540                                     type_name_const_str.SetCString(type_name_cstr);
3541                                     break;
3542                                 case DW_AT_type:            encoding_uid = form_value.Reference(dwarf_cu); break;
3543                                 case DW_AT_byte_size:       byte_size = form_value.Unsigned(); byte_size_valid = true; break;
3544                                 case DW_AT_accessibility:   accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3545                                 case DW_AT_declaration:     is_forward_declaration = form_value.Unsigned() != 0; break;
3546                                 case DW_AT_allocated:
3547                                 case DW_AT_associated:
3548                                 case DW_AT_bit_stride:
3549                                 case DW_AT_byte_stride:
3550                                 case DW_AT_data_location:
3551                                 case DW_AT_description:
3552                                 case DW_AT_start_scope:
3553                                 case DW_AT_visibility:
3554                                 case DW_AT_specification:
3555                                 case DW_AT_abstract_origin:
3556                                 case DW_AT_sibling:
3557                                     break;
3558                                 }
3559                             }
3560                         }
3561 
3562                         DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3563 
3564                         clang_type_t enumerator_clang_type = NULL;
3565                         clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3566                         if (clang_type == NULL)
3567                         {
3568                             enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
3569                                                                                                   DW_ATE_signed,
3570                                                                                                   byte_size * 8);
3571                             clang_type = ast.CreateEnumerationType (type_name_cstr,
3572                                                                     GetClangDeclContextContainingDIE (dwarf_cu, die),
3573                                                                     decl,
3574                                                                     enumerator_clang_type);
3575                         }
3576                         else
3577                         {
3578                             enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
3579                             assert (enumerator_clang_type != NULL);
3580                         }
3581 
3582                         LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
3583 
3584                         type_sp.reset( new Type (die->GetOffset(),
3585                                                  this,
3586                                                  type_name_const_str,
3587                                                  byte_size,
3588                                                  NULL,
3589                                                  encoding_uid,
3590                                                  Type::eEncodingIsUID,
3591                                                  &decl,
3592                                                  clang_type,
3593                                                  Type::eResolveStateForward));
3594 
3595 #if LEAVE_ENUMS_FORWARD_DECLARED
3596                         // Leave this as a forward declaration until we need
3597                         // to know the details of the type. lldb_private::Type
3598                         // will automatically call the SymbolFile virtual function
3599                         // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3600                         // When the definition needs to be defined.
3601                         m_forward_decl_die_to_clang_type[die] = clang_type;
3602                         m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
3603                         ClangASTContext::SetHasExternalStorage (clang_type, true);
3604 #else
3605                         ast.StartTagDeclarationDefinition (clang_type);
3606                         if (die->HasChildren())
3607                         {
3608                             SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
3609                             ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
3610                         }
3611                         ast.CompleteTagDeclarationDefinition (clang_type);
3612 #endif
3613                     }
3614                 }
3615                 break;
3616 
3617             case DW_TAG_inlined_subroutine:
3618             case DW_TAG_subprogram:
3619             case DW_TAG_subroutine_type:
3620                 {
3621                     // Set a bit that lets us know that we are currently parsing this
3622                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3623 
3624                     const char *mangled = NULL;
3625                     dw_offset_t type_die_offset = DW_INVALID_OFFSET;
3626                     bool is_variadic = false;
3627                     bool is_inline = false;
3628                     bool is_static = false;
3629                     bool is_virtual = false;
3630                     bool is_explicit = false;
3631                     dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
3632                     dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
3633 
3634                     unsigned type_quals = 0;
3635                     clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
3636 
3637 
3638                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3639                     if (num_attributes > 0)
3640                     {
3641                         uint32_t i;
3642                         for (i=0; i<num_attributes; ++i)
3643                         {
3644                             attr = attributes.AttributeAtIndex(i);
3645                             DWARFFormValue form_value;
3646                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3647                             {
3648                                 switch (attr)
3649                                 {
3650                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3651                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3652                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3653                                 case DW_AT_name:
3654                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3655                                     type_name_const_str.SetCString(type_name_cstr);
3656                                     break;
3657 
3658                                 case DW_AT_MIPS_linkage_name:   mangled = form_value.AsCString(&get_debug_str_data()); break;
3659                                 case DW_AT_type:                type_die_offset = form_value.Reference(dwarf_cu); break;
3660                                 case DW_AT_accessibility:       accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3661                                 case DW_AT_declaration:         is_forward_declaration = form_value.Unsigned() != 0; break;
3662                                 case DW_AT_inline:              is_inline = form_value.Unsigned() != 0; break;
3663                                 case DW_AT_virtuality:          is_virtual = form_value.Unsigned() != 0;  break;
3664                                 case DW_AT_explicit:            is_explicit = form_value.Unsigned() != 0;  break;
3665 
3666                                 case DW_AT_external:
3667                                     if (form_value.Unsigned())
3668                                     {
3669                                         if (storage == clang::SC_None)
3670                                             storage = clang::SC_Extern;
3671                                         else
3672                                             storage = clang::SC_PrivateExtern;
3673                                     }
3674                                     break;
3675 
3676                                 case DW_AT_specification:
3677                                     specification_die_offset = form_value.Reference(dwarf_cu);
3678                                     break;
3679 
3680                                 case DW_AT_abstract_origin:
3681                                     abstract_origin_die_offset = form_value.Reference(dwarf_cu);
3682                                     break;
3683 
3684 
3685                                 case DW_AT_allocated:
3686                                 case DW_AT_associated:
3687                                 case DW_AT_address_class:
3688                                 case DW_AT_artificial:
3689                                 case DW_AT_calling_convention:
3690                                 case DW_AT_data_location:
3691                                 case DW_AT_elemental:
3692                                 case DW_AT_entry_pc:
3693                                 case DW_AT_frame_base:
3694                                 case DW_AT_high_pc:
3695                                 case DW_AT_low_pc:
3696                                 case DW_AT_object_pointer:
3697                                 case DW_AT_prototyped:
3698                                 case DW_AT_pure:
3699                                 case DW_AT_ranges:
3700                                 case DW_AT_recursive:
3701                                 case DW_AT_return_addr:
3702                                 case DW_AT_segment:
3703                                 case DW_AT_start_scope:
3704                                 case DW_AT_static_link:
3705                                 case DW_AT_trampoline:
3706                                 case DW_AT_visibility:
3707                                 case DW_AT_vtable_elem_location:
3708                                 case DW_AT_description:
3709                                 case DW_AT_sibling:
3710                                     break;
3711                                 }
3712                             }
3713                         }
3714                     }
3715 
3716                     DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3717 
3718                     clang_type_t return_clang_type = NULL;
3719                     Type *func_type = NULL;
3720 
3721                     if (type_die_offset != DW_INVALID_OFFSET)
3722                         func_type = ResolveTypeUID(type_die_offset);
3723 
3724                     if (func_type)
3725                         return_clang_type = func_type->GetClangLayoutType();
3726                     else
3727                         return_clang_type = ast.GetBuiltInType_void();
3728 
3729 
3730                     std::vector<clang_type_t> function_param_types;
3731                     std::vector<clang::ParmVarDecl*> function_param_decls;
3732 
3733                     // Parse the function children for the parameters
3734 
3735                     clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die);
3736                     const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
3737 
3738                     const bool is_cxx_method = containing_decl_kind == clang::Decl::CXXRecord;
3739                     // Start off static. This will be set to false in ParseChildParameters(...)
3740                     // if we find a "this" paramters as the first parameter
3741                     if (is_cxx_method)
3742                         is_static = true;
3743 
3744                     if (die->HasChildren())
3745                     {
3746                         bool skip_artificial = true;
3747                         ParseChildParameters (sc,
3748                                               containing_decl_ctx,
3749                                               type_sp,
3750                                               dwarf_cu,
3751                                               die,
3752                                               skip_artificial,
3753                                               is_static,
3754                                               type_list,
3755                                               function_param_types,
3756                                               function_param_decls,
3757                                               type_quals);
3758                     }
3759 
3760                     // clang_type will get the function prototype clang type after this call
3761                     clang_type = ast.CreateFunctionType (return_clang_type,
3762                                                          &function_param_types[0],
3763                                                          function_param_types.size(),
3764                                                          is_variadic,
3765                                                          type_quals);
3766 
3767                     if (type_name_cstr)
3768                     {
3769                         bool type_handled = false;
3770                         if (tag == DW_TAG_subprogram)
3771                         {
3772                             if (ObjCLanguageRuntime::IsPossibleObjCMethodName (type_name_cstr))
3773                             {
3774                                 // We need to find the DW_TAG_class_type or
3775                                 // DW_TAG_struct_type by name so we can add this
3776                                 // as a member function of the class.
3777                                 const char *class_name_start = type_name_cstr + 2;
3778                                 const char *class_name_end = ::strchr (class_name_start, ' ');
3779                                 SymbolContext empty_sc;
3780                                 clang_type_t class_opaque_type = NULL;
3781                                 if (class_name_start < class_name_end)
3782                                 {
3783                                     ConstString class_name (class_name_start, class_name_end - class_name_start);
3784                                     TypeList types;
3785                                     const uint32_t match_count = FindTypes (empty_sc, class_name, true, UINT32_MAX, types);
3786                                     if (match_count > 0)
3787                                     {
3788                                         for (uint32_t i=0; i<match_count; ++i)
3789                                         {
3790                                             Type *type = types.GetTypeAtIndex (i).get();
3791                                             clang_type_t type_clang_forward_type = type->GetClangForwardType();
3792                                             if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
3793                                             {
3794                                                 class_opaque_type = type_clang_forward_type;
3795                                                 break;
3796                                             }
3797                                         }
3798                                     }
3799                                 }
3800 
3801                                 if (class_opaque_type)
3802                                 {
3803                                     // If accessibility isn't set to anything valid, assume public for
3804                                     // now...
3805                                     if (accessibility == eAccessNone)
3806                                         accessibility = eAccessPublic;
3807 
3808                                     clang::ObjCMethodDecl *objc_method_decl;
3809                                     objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
3810                                                                                       type_name_cstr,
3811                                                                                       clang_type,
3812                                                                                       accessibility);
3813                                     LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
3814                                     type_handled = objc_method_decl != NULL;
3815                                 }
3816                             }
3817                             else if (is_cxx_method)
3818                             {
3819                                 // Look at the parent of this DIE and see if is is
3820                                 // a class or struct and see if this is actually a
3821                                 // C++ method
3822                                 Type *class_type = ResolveType (dwarf_cu, m_decl_ctx_to_die[containing_decl_ctx]);
3823                                 if (class_type)
3824                                 {
3825                                     if (specification_die_offset != DW_INVALID_OFFSET)
3826                                     {
3827                                         // If we have a specification, then the function type should have been
3828                                         // made with the specification and not with this die.
3829                                         DWARFCompileUnitSP spec_cu_sp;
3830                                         const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
3831                                         if (m_die_to_decl_ctx[spec_die] == NULL)
3832                                         {
3833                                             ReportWarning ("0x%8.8x: DW_AT_specification(0x%8.8x) has no decl\n",
3834                                                 die->GetOffset(),
3835                                                 specification_die_offset);
3836                                         }
3837                                         type_handled = true;
3838                                     }
3839                                     else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
3840                                     {
3841                                         DWARFCompileUnitSP abs_cu_sp;
3842                                         const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
3843                                         if (m_die_to_decl_ctx[abs_die] == NULL)
3844                                         {
3845                                             ReportWarning ("0x%8.8x: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
3846                                                 die->GetOffset(),
3847                                                 abstract_origin_die_offset);
3848                                         }
3849                                         type_handled = true;
3850                                     }
3851                                     else
3852                                     {
3853                                         clang_type_t class_opaque_type = class_type->GetClangForwardType();
3854                                         if (ClangASTContext::IsCXXClassType (class_opaque_type))
3855                                         {
3856                                             // Neither GCC 4.2 nor clang++ currently set a valid accessibility
3857                                             // in the DWARF for C++ methods... Default to public for now...
3858                                             if (accessibility == eAccessNone)
3859                                                 accessibility = eAccessPublic;
3860 
3861                                             if (!is_static && !die->HasChildren())
3862                                             {
3863                                                 // We have a C++ member function with no children (this pointer!)
3864                                                 // and clang will get mad if we try and make a function that isn't
3865                                                 // well formed in the DWARF, so we will just skip it...
3866                                                 type_handled = true;
3867                                             }
3868                                             else
3869                                             {
3870                                                 clang::CXXMethodDecl *cxx_method_decl;
3871                                                 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
3872                                                                                                 type_name_cstr,
3873                                                                                                 clang_type,
3874                                                                                                 accessibility,
3875                                                                                                 is_virtual,
3876                                                                                                 is_static,
3877                                                                                                 is_inline,
3878                                                                                                 is_explicit);
3879                                                 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
3880 
3881                                                 type_handled = cxx_method_decl != NULL;
3882                                             }
3883                                         }
3884                                     }
3885                                 }
3886                             }
3887                         }
3888 
3889                         if (!type_handled)
3890                         {
3891                             // We just have a function that isn't part of a class
3892                             clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (type_name_cstr,
3893                                                                                                 clang_type,
3894                                                                                                 storage,
3895                                                                                                 is_inline);
3896 
3897                             // Add the decl to our DIE to decl context map
3898                             assert (function_decl);
3899                             LinkDeclContextToDIE(function_decl, die);
3900                             if (!function_param_decls.empty())
3901                                 ast.SetFunctionParameters (function_decl,
3902                                                            &function_param_decls.front(),
3903                                                            function_param_decls.size());
3904                         }
3905                     }
3906                     type_sp.reset( new Type (die->GetOffset(),
3907                                              this,
3908                                              type_name_const_str,
3909                                              0,
3910                                              NULL,
3911                                              LLDB_INVALID_UID,
3912                                              Type::eEncodingIsUID,
3913                                              &decl,
3914                                              clang_type,
3915                                              Type::eResolveStateFull));
3916                     assert(type_sp.get());
3917                 }
3918                 break;
3919 
3920             case DW_TAG_array_type:
3921                 {
3922                     // Set a bit that lets us know that we are currently parsing this
3923                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3924 
3925                     lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
3926                     int64_t first_index = 0;
3927                     uint32_t byte_stride = 0;
3928                     uint32_t bit_stride = 0;
3929                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3930 
3931                     if (num_attributes > 0)
3932                     {
3933                         uint32_t i;
3934                         for (i=0; i<num_attributes; ++i)
3935                         {
3936                             attr = attributes.AttributeAtIndex(i);
3937                             DWARFFormValue form_value;
3938                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3939                             {
3940                                 switch (attr)
3941                                 {
3942                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3943                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3944                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3945                                 case DW_AT_name:
3946                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3947                                     type_name_const_str.SetCString(type_name_cstr);
3948                                     break;
3949 
3950                                 case DW_AT_type:            type_die_offset = form_value.Reference(dwarf_cu); break;
3951                                 case DW_AT_byte_size:       byte_size = form_value.Unsigned(); byte_size_valid = true; break;
3952                                 case DW_AT_byte_stride:     byte_stride = form_value.Unsigned(); break;
3953                                 case DW_AT_bit_stride:      bit_stride = form_value.Unsigned(); break;
3954                                 case DW_AT_accessibility:   accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3955                                 case DW_AT_declaration:     is_forward_declaration = form_value.Unsigned() != 0; break;
3956                                 case DW_AT_allocated:
3957                                 case DW_AT_associated:
3958                                 case DW_AT_data_location:
3959                                 case DW_AT_description:
3960                                 case DW_AT_ordering:
3961                                 case DW_AT_start_scope:
3962                                 case DW_AT_visibility:
3963                                 case DW_AT_specification:
3964                                 case DW_AT_abstract_origin:
3965                                 case DW_AT_sibling:
3966                                     break;
3967                                 }
3968                             }
3969                         }
3970 
3971                         DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3972 
3973                         Type *element_type = ResolveTypeUID(type_die_offset);
3974 
3975                         if (element_type)
3976                         {
3977                             std::vector<uint64_t> element_orders;
3978                             ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
3979                             // We have an array that claims to have no members, lets give it at least one member...
3980                             if (element_orders.empty())
3981                                 element_orders.push_back (1);
3982                             if (byte_stride == 0 && bit_stride == 0)
3983                                 byte_stride = element_type->GetByteSize();
3984                             clang_type_t array_element_type = element_type->GetClangFullType();
3985                             uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
3986                             uint64_t num_elements = 0;
3987                             std::vector<uint64_t>::const_reverse_iterator pos;
3988                             std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
3989                             for (pos = element_orders.rbegin(); pos != end; ++pos)
3990                             {
3991                                 num_elements = *pos;
3992                                 clang_type = ast.CreateArrayType (array_element_type,
3993                                                                   num_elements,
3994                                                                   num_elements * array_element_bit_stride);
3995                                 array_element_type = clang_type;
3996                                 array_element_bit_stride = array_element_bit_stride * num_elements;
3997                             }
3998                             ConstString empty_name;
3999                             type_sp.reset( new Type (die->GetOffset(),
4000                                                      this,
4001                                                      empty_name,
4002                                                      array_element_bit_stride / 8,
4003                                                      NULL,
4004                                                      type_die_offset,
4005                                                      Type::eEncodingIsUID,
4006                                                      &decl,
4007                                                      clang_type,
4008                                                      Type::eResolveStateFull));
4009                             type_sp->SetEncodingType (element_type);
4010                         }
4011                     }
4012                 }
4013                 break;
4014 
4015             case DW_TAG_ptr_to_member_type:
4016                 {
4017                     dw_offset_t type_die_offset = DW_INVALID_OFFSET;
4018                     dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
4019 
4020                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4021 
4022                     if (num_attributes > 0) {
4023                         uint32_t i;
4024                         for (i=0; i<num_attributes; ++i)
4025                         {
4026                             attr = attributes.AttributeAtIndex(i);
4027                             DWARFFormValue form_value;
4028                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4029                             {
4030                                 switch (attr)
4031                                 {
4032                                     case DW_AT_type:
4033                                         type_die_offset = form_value.Reference(dwarf_cu); break;
4034                                     case DW_AT_containing_type:
4035                                         containing_type_die_offset = form_value.Reference(dwarf_cu); break;
4036                                 }
4037                             }
4038                         }
4039 
4040                         Type *pointee_type = ResolveTypeUID(type_die_offset);
4041                         Type *class_type = ResolveTypeUID(containing_type_die_offset);
4042 
4043                         clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
4044                         clang_type_t class_clang_type = class_type->GetClangLayoutType();
4045 
4046                         clang_type = ast.CreateMemberPointerType(pointee_clang_type,
4047                                                                  class_clang_type);
4048 
4049                         byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
4050                                                                        clang_type) / 8;
4051 
4052                         type_sp.reset( new Type (die->GetOffset(),
4053                                                  this,
4054                                                  type_name_const_str,
4055                                                  byte_size,
4056                                                  NULL,
4057                                                  LLDB_INVALID_UID,
4058                                                  Type::eEncodingIsUID,
4059                                                  NULL,
4060                                                  clang_type,
4061                                                  Type::eResolveStateForward));
4062                     }
4063 
4064                     break;
4065                 }
4066             default:
4067                 assert(false && "Unhandled type tag!");
4068                 break;
4069             }
4070 
4071             if (type_sp.get())
4072             {
4073                 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4074                 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4075 
4076                 SymbolContextScope * symbol_context_scope = NULL;
4077                 if (sc_parent_tag == DW_TAG_compile_unit)
4078                 {
4079                     symbol_context_scope = sc.comp_unit;
4080                 }
4081                 else if (sc.function != NULL)
4082                 {
4083                     symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4084                     if (symbol_context_scope == NULL)
4085                         symbol_context_scope = sc.function;
4086                 }
4087 
4088                 if (symbol_context_scope != NULL)
4089                 {
4090                     type_sp->SetSymbolContextScope(symbol_context_scope);
4091                 }
4092 
4093                 // We are ready to put this type into the uniqued list up at the module level
4094                 type_list->Insert (type_sp);
4095 
4096                 m_die_to_type[die] = type_sp.get();
4097             }
4098         }
4099         else if (type_ptr != DIE_IS_BEING_PARSED)
4100         {
4101             type_sp = type_list->FindType(type_ptr->GetID());
4102         }
4103     }
4104     return type_sp;
4105 }
4106 
4107 size_t
4108 SymbolFileDWARF::ParseTypes
4109 (
4110     const SymbolContext& sc,
4111     DWARFCompileUnit* dwarf_cu,
4112     const DWARFDebugInfoEntry *die,
4113     bool parse_siblings,
4114     bool parse_children
4115 )
4116 {
4117     size_t types_added = 0;
4118     while (die != NULL)
4119     {
4120         bool type_is_new = false;
4121         if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
4122         {
4123             if (type_is_new)
4124                 ++types_added;
4125         }
4126 
4127         if (parse_children && die->HasChildren())
4128         {
4129             if (die->Tag() == DW_TAG_subprogram)
4130             {
4131                 SymbolContext child_sc(sc);
4132                 child_sc.function = sc.comp_unit->FindFunctionByUID(die->GetOffset()).get();
4133                 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
4134             }
4135             else
4136                 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
4137         }
4138 
4139         if (parse_siblings)
4140             die = die->GetSibling();
4141         else
4142             die = NULL;
4143     }
4144     return types_added;
4145 }
4146 
4147 
4148 size_t
4149 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
4150 {
4151     assert(sc.comp_unit && sc.function);
4152     size_t functions_added = 0;
4153     DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
4154     if (dwarf_cu)
4155     {
4156         dw_offset_t function_die_offset = sc.function->GetID();
4157         const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
4158         if (function_die)
4159         {
4160             ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
4161         }
4162     }
4163 
4164     return functions_added;
4165 }
4166 
4167 
4168 size_t
4169 SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
4170 {
4171     // At least a compile unit must be valid
4172     assert(sc.comp_unit);
4173     size_t types_added = 0;
4174     DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
4175     if (dwarf_cu)
4176     {
4177         if (sc.function)
4178         {
4179             dw_offset_t function_die_offset = sc.function->GetID();
4180             const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
4181             if (func_die && func_die->HasChildren())
4182             {
4183                 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
4184             }
4185         }
4186         else
4187         {
4188             const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
4189             if (dwarf_cu_die && dwarf_cu_die->HasChildren())
4190             {
4191                 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
4192             }
4193         }
4194     }
4195 
4196     return types_added;
4197 }
4198 
4199 size_t
4200 SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
4201 {
4202     if (sc.comp_unit != NULL)
4203     {
4204         DWARFDebugInfo* info = DebugInfo();
4205         if (info == NULL)
4206             return 0;
4207 
4208         uint32_t cu_idx = UINT32_MAX;
4209         DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
4210 
4211         if (dwarf_cu == NULL)
4212             return 0;
4213 
4214         if (sc.function)
4215         {
4216             const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
4217 
4218             dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
4219             assert (func_lo_pc != DW_INVALID_ADDRESS);
4220 
4221             const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
4222 
4223             // Let all blocks know they have parse all their variables
4224             sc.function->GetBlock (false).SetDidParseVariables (true, true);
4225 
4226             return num_variables;
4227         }
4228         else if (sc.comp_unit)
4229         {
4230             uint32_t vars_added = 0;
4231             VariableListSP variables (sc.comp_unit->GetVariableList(false));
4232 
4233             if (variables.get() == NULL)
4234             {
4235                 variables.reset(new VariableList());
4236                 sc.comp_unit->SetVariableList(variables);
4237 
4238                 // Index if we already haven't to make sure the compile units
4239                 // get indexed and make their global DIE index list
4240                 if (!m_indexed)
4241                     Index ();
4242 
4243                 DWARFCompileUnit* match_dwarf_cu = NULL;
4244                 const DWARFDebugInfoEntry* die = NULL;
4245                 DIEArray die_offsets;
4246                 const size_t num_matches = m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
4247                                                                                         dwarf_cu->GetNextCompileUnitOffset(),
4248                                                                                         die_offsets);
4249                 if (num_matches)
4250                 {
4251                     DWARFDebugInfo* debug_info = DebugInfo();
4252                     for (size_t i=0; i<num_matches; ++i)
4253                     {
4254                         const dw_offset_t die_offset = die_offsets[i];
4255                         die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
4256                         VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
4257                         if (var_sp)
4258                         {
4259                             variables->AddVariableIfUnique (var_sp);
4260                             ++vars_added;
4261                         }
4262                     }
4263                 }
4264             }
4265             return vars_added;
4266         }
4267     }
4268     return 0;
4269 }
4270 
4271 
4272 VariableSP
4273 SymbolFileDWARF::ParseVariableDIE
4274 (
4275     const SymbolContext& sc,
4276     DWARFCompileUnit* dwarf_cu,
4277     const DWARFDebugInfoEntry *die,
4278     const lldb::addr_t func_low_pc
4279 )
4280 {
4281 
4282     VariableSP var_sp (m_die_to_variable_sp[die]);
4283     if (var_sp)
4284         return var_sp;  // Already been parsed!
4285 
4286     const dw_tag_t tag = die->Tag();
4287     DWARFDebugInfoEntry::Attributes attributes;
4288     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4289     if (num_attributes > 0)
4290     {
4291         const char *name = NULL;
4292         const char *mangled = NULL;
4293         Declaration decl;
4294         uint32_t i;
4295         Type *var_type = NULL;
4296         DWARFExpression location;
4297         bool is_external = false;
4298         bool is_artificial = false;
4299         bool location_is_const_value_data = false;
4300         AccessType accessibility = eAccessNone;
4301 
4302         for (i=0; i<num_attributes; ++i)
4303         {
4304             dw_attr_t attr = attributes.AttributeAtIndex(i);
4305             DWARFFormValue form_value;
4306             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4307             {
4308                 switch (attr)
4309                 {
4310                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4311                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
4312                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4313                 case DW_AT_name:        name = form_value.AsCString(&get_debug_str_data()); break;
4314                 case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
4315                 case DW_AT_type:        var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break;
4316                 case DW_AT_external:    is_external = form_value.Unsigned() != 0; break;
4317                 case DW_AT_const_value:
4318                     location_is_const_value_data = true;
4319                     // Fall through...
4320                 case DW_AT_location:
4321                     {
4322                         if (form_value.BlockData())
4323                         {
4324                             const DataExtractor& debug_info_data = get_debug_info_data();
4325 
4326                             uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4327                             uint32_t block_length = form_value.Unsigned();
4328                             location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
4329                         }
4330                         else
4331                         {
4332                             const DataExtractor&    debug_loc_data = get_debug_loc_data();
4333                             const dw_offset_t debug_loc_offset = form_value.Unsigned();
4334 
4335                             size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
4336                             if (loc_list_length > 0)
4337                             {
4338                                 location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
4339                                 assert (func_low_pc != LLDB_INVALID_ADDRESS);
4340                                 location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
4341                             }
4342                         }
4343                     }
4344                     break;
4345 
4346                 case DW_AT_artificial:      is_artificial = form_value.Unsigned() != 0; break;
4347                 case DW_AT_accessibility:   accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4348                 case DW_AT_declaration:
4349                 case DW_AT_description:
4350                 case DW_AT_endianity:
4351                 case DW_AT_segment:
4352                 case DW_AT_start_scope:
4353                 case DW_AT_visibility:
4354                 default:
4355                 case DW_AT_abstract_origin:
4356                 case DW_AT_sibling:
4357                 case DW_AT_specification:
4358                     break;
4359                 }
4360             }
4361         }
4362 
4363         if (location.IsValid())
4364         {
4365             assert(var_type != DIE_IS_BEING_PARSED);
4366 
4367             ValueType scope = eValueTypeInvalid;
4368 
4369             const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4370             dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4371 
4372             if (tag == DW_TAG_formal_parameter)
4373                 scope = eValueTypeVariableArgument;
4374             else if (is_external || parent_tag == DW_TAG_compile_unit)
4375                 scope = eValueTypeVariableGlobal;
4376             else
4377                 scope = eValueTypeVariableLocal;
4378 
4379             SymbolContextScope * symbol_context_scope = NULL;
4380             if (parent_tag == DW_TAG_compile_unit)
4381             {
4382                 symbol_context_scope = sc.comp_unit;
4383             }
4384             else if (sc.function != NULL)
4385             {
4386                 symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4387                 if (symbol_context_scope == NULL)
4388                     symbol_context_scope = sc.function;
4389             }
4390 
4391             assert(symbol_context_scope != NULL);
4392             var_sp.reset (new Variable(die->GetOffset(),
4393                                        name,
4394                                        mangled,
4395                                        var_type,
4396                                        scope,
4397                                        symbol_context_scope,
4398                                        &decl,
4399                                        location,
4400                                        is_external,
4401                                        is_artificial));
4402 
4403             var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4404         }
4405     }
4406     // Cache var_sp even if NULL (the variable was just a specification or
4407     // was missing vital information to be able to be displayed in the debugger
4408     // (missing location due to optimization, etc)) so we don't re-parse
4409     // this DIE over and over later...
4410     m_die_to_variable_sp[die] = var_sp;
4411     return var_sp;
4412 }
4413 
4414 
4415 const DWARFDebugInfoEntry *
4416 SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
4417                                                    dw_offset_t spec_block_die_offset,
4418                                                    DWARFCompileUnit **result_die_cu_handle)
4419 {
4420     // Give the concrete function die specified by "func_die_offset", find the
4421     // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4422     // to "spec_block_die_offset"
4423     DWARFDebugInfo* info = DebugInfo();
4424 
4425     const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
4426     if (die)
4427     {
4428         assert (*result_die_cu_handle);
4429         return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
4430     }
4431     return NULL;
4432 }
4433 
4434 
4435 const DWARFDebugInfoEntry *
4436 SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
4437                                                   const DWARFDebugInfoEntry *die,
4438                                                   dw_offset_t spec_block_die_offset,
4439                                                   DWARFCompileUnit **result_die_cu_handle)
4440 {
4441     if (die)
4442     {
4443         switch (die->Tag())
4444         {
4445         case DW_TAG_subprogram:
4446         case DW_TAG_inlined_subroutine:
4447         case DW_TAG_lexical_block:
4448             {
4449                 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4450                 {
4451                     *result_die_cu_handle = dwarf_cu;
4452                     return die;
4453                 }
4454 
4455                 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
4456                 {
4457                     *result_die_cu_handle = dwarf_cu;
4458                     return die;
4459                 }
4460             }
4461             break;
4462         }
4463 
4464         // Give the concrete function die specified by "func_die_offset", find the
4465         // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4466         // to "spec_block_die_offset"
4467         for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
4468         {
4469             const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
4470                                                                                       child_die,
4471                                                                                       spec_block_die_offset,
4472                                                                                       result_die_cu_handle);
4473             if (result_die)
4474                 return result_die;
4475         }
4476     }
4477 
4478     *result_die_cu_handle = NULL;
4479     return NULL;
4480 }
4481 
4482 size_t
4483 SymbolFileDWARF::ParseVariables
4484 (
4485     const SymbolContext& sc,
4486     DWARFCompileUnit* dwarf_cu,
4487     const lldb::addr_t func_low_pc,
4488     const DWARFDebugInfoEntry *orig_die,
4489     bool parse_siblings,
4490     bool parse_children,
4491     VariableList* cc_variable_list
4492 )
4493 {
4494     if (orig_die == NULL)
4495         return 0;
4496 
4497     VariableListSP variable_list_sp;
4498 
4499     size_t vars_added = 0;
4500     const DWARFDebugInfoEntry *die = orig_die;
4501     while (die != NULL)
4502     {
4503         dw_tag_t tag = die->Tag();
4504 
4505         // Check to see if we have already parsed this variable or constant?
4506         if (m_die_to_variable_sp[die])
4507         {
4508             if (cc_variable_list)
4509                 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
4510         }
4511         else
4512         {
4513             // We haven't already parsed it, lets do that now.
4514             if ((tag == DW_TAG_variable) ||
4515                 (tag == DW_TAG_constant) ||
4516                 (tag == DW_TAG_formal_parameter && sc.function))
4517             {
4518                 if (variable_list_sp.get() == NULL)
4519                 {
4520                     const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
4521                     dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4522                     switch (parent_tag)
4523                     {
4524                         case DW_TAG_compile_unit:
4525                             if (sc.comp_unit != NULL)
4526                             {
4527                                 variable_list_sp = sc.comp_unit->GetVariableList(false);
4528                                 if (variable_list_sp.get() == NULL)
4529                                 {
4530                                     variable_list_sp.reset(new VariableList());
4531                                     sc.comp_unit->SetVariableList(variable_list_sp);
4532                                 }
4533                             }
4534                             else
4535                             {
4536                                 ReportError ("parent 0x%8.8x %s with no valid compile unit in symbol context for 0x%8.8x %s.\n",
4537                                          sc_parent_die->GetOffset(),
4538                                          DW_TAG_value_to_name (parent_tag),
4539                                          orig_die->GetOffset(),
4540                                          DW_TAG_value_to_name (orig_die->Tag()));
4541                             }
4542                             break;
4543 
4544                         case DW_TAG_subprogram:
4545                         case DW_TAG_inlined_subroutine:
4546                         case DW_TAG_lexical_block:
4547                             if (sc.function != NULL)
4548                             {
4549                                 // Check to see if we already have parsed the variables for the given scope
4550 
4551                                 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4552                                 if (block == NULL)
4553                                 {
4554                                     // This must be a specification or abstract origin with
4555                                     // a concrete block couterpart in the current function. We need
4556                                     // to find the concrete block so we can correctly add the
4557                                     // variable to it
4558                                     DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
4559                                     const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
4560                                                                                                                       sc_parent_die->GetOffset(),
4561                                                                                                                       &concrete_block_die_cu);
4562                                     if (concrete_block_die)
4563                                         block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die->GetOffset());
4564                                 }
4565 
4566                                 if (block != NULL)
4567                                 {
4568                                     const bool can_create = false;
4569                                     variable_list_sp = block->GetBlockVariableList (can_create);
4570                                     if (variable_list_sp.get() == NULL)
4571                                     {
4572                                         variable_list_sp.reset(new VariableList());
4573                                         block->SetVariableList(variable_list_sp);
4574                                     }
4575                                 }
4576                             }
4577                             break;
4578 
4579                         default:
4580                              ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8x %s.\n",
4581                                      orig_die->GetOffset(),
4582                                      DW_TAG_value_to_name (orig_die->Tag()));
4583                             break;
4584                     }
4585                 }
4586 
4587                 if (variable_list_sp)
4588                 {
4589                     VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
4590                     if (var_sp)
4591                     {
4592                         variable_list_sp->AddVariableIfUnique (var_sp);
4593                         if (cc_variable_list)
4594                             cc_variable_list->AddVariableIfUnique (var_sp);
4595                         ++vars_added;
4596                     }
4597                 }
4598             }
4599         }
4600 
4601         bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
4602 
4603         if (!skip_children && parse_children && die->HasChildren())
4604         {
4605             vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
4606         }
4607 
4608         if (parse_siblings)
4609             die = die->GetSibling();
4610         else
4611             die = NULL;
4612     }
4613     return vars_added;
4614 }
4615 
4616 //------------------------------------------------------------------
4617 // PluginInterface protocol
4618 //------------------------------------------------------------------
4619 const char *
4620 SymbolFileDWARF::GetPluginName()
4621 {
4622     return "SymbolFileDWARF";
4623 }
4624 
4625 const char *
4626 SymbolFileDWARF::GetShortPluginName()
4627 {
4628     return GetPluginNameStatic();
4629 }
4630 
4631 uint32_t
4632 SymbolFileDWARF::GetPluginVersion()
4633 {
4634     return 1;
4635 }
4636 
4637 void
4638 SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
4639 {
4640     SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
4641     clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
4642     if (clang_type)
4643         symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
4644 }
4645 
4646 void
4647 SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
4648 {
4649     SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
4650     clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
4651     if (clang_type)
4652         symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
4653 }
4654 
4655 void
4656 SymbolFileDWARF::DumpIndexes ()
4657 {
4658     StreamFile s(stdout, false);
4659 
4660     s.Printf ("DWARF index for (%s) '%s/%s':",
4661               GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
4662               GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
4663               GetObjectFile()->GetFileSpec().GetFilename().AsCString());
4664     s.Printf("\nFunction basenames:\n");    m_function_basename_index.Dump (&s);
4665     s.Printf("\nFunction fullnames:\n");    m_function_fullname_index.Dump (&s);
4666     s.Printf("\nFunction methods:\n");      m_function_method_index.Dump (&s);
4667     s.Printf("\nFunction selectors:\n");    m_function_selector_index.Dump (&s);
4668     s.Printf("\nObjective C class selectors:\n");    m_objc_class_selectors_index.Dump (&s);
4669     s.Printf("\nGlobals and statics:\n");   m_global_index.Dump (&s);
4670     s.Printf("\nTypes:\n");                 m_type_index.Dump (&s);
4671     s.Printf("\nNamepaces:\n");             m_namespace_index.Dump (&s);
4672 }
4673 
4674 void
4675 SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
4676                                     const char *name,
4677                                     llvm::SmallVectorImpl <clang::NamedDecl *> *results)
4678 {
4679     DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
4680 
4681     if (iter == m_decl_ctx_to_die.end())
4682         return;
4683 
4684     const DWARFDebugInfoEntry *context_die = iter->second;
4685 
4686     if (!results)
4687         return;
4688 
4689     DWARFDebugInfo* info = DebugInfo();
4690 
4691     DIEArray die_offsets;
4692 
4693     DWARFCompileUnit* dwarf_cu = NULL;
4694     const DWARFDebugInfoEntry* die = NULL;
4695     size_t num_matches = m_type_index.Find (ConstString(name), die_offsets);
4696 
4697     if (num_matches)
4698     {
4699         for (size_t i = 0; i < num_matches; ++i)
4700         {
4701             const dw_offset_t die_offset = die_offsets[i];
4702             die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
4703 
4704             if (die->GetParent() != context_die)
4705                 continue;
4706 
4707             Type *matching_type = ResolveType (dwarf_cu, die);
4708 
4709             lldb::clang_type_t type = matching_type->GetClangFullType();
4710             clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
4711 
4712             if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
4713             {
4714                 clang::TagDecl *tag_decl = tag_type->getDecl();
4715                 results->push_back(tag_decl);
4716             }
4717             else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
4718             {
4719                 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
4720                 results->push_back(typedef_decl);
4721             }
4722         }
4723     }
4724 }
4725 
4726 void
4727 SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
4728                                                  const clang::DeclContext *DC,
4729                                                  clang::DeclarationName Name,
4730                                                  llvm::SmallVectorImpl <clang::NamedDecl *> *results)
4731 {
4732     SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
4733 
4734     symbol_file_dwarf->SearchDeclContext (DC, Name.getAsString().c_str(), results);
4735 }
4736