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