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