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