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 #include "lldb/Target/CPPLanguageRuntime.h"
47 
48 #include "DWARFCompileUnit.h"
49 #include "DWARFDebugAbbrev.h"
50 #include "DWARFDebugAranges.h"
51 #include "DWARFDebugInfo.h"
52 #include "DWARFDebugInfoEntry.h"
53 #include "DWARFDebugLine.h"
54 #include "DWARFDebugPubnames.h"
55 #include "DWARFDebugRanges.h"
56 #include "DWARFDIECollection.h"
57 #include "DWARFFormValue.h"
58 #include "DWARFLocationList.h"
59 #include "LogChannelDWARF.h"
60 #include "SymbolFileDWARFDebugMap.h"
61 
62 #include <map>
63 
64 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
65 
66 #ifdef ENABLE_DEBUG_PRINTF
67 #include <stdio.h>
68 #define DEBUG_PRINTF(fmt, ...) printf(fmt, ## __VA_ARGS__)
69 #else
70 #define DEBUG_PRINTF(fmt, ...)
71 #endif
72 
73 #define DIE_IS_BEING_PARSED ((lldb_private::Type*)1)
74 
75 using namespace lldb;
76 using namespace lldb_private;
77 
78 
79 static AccessType
80 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
81 {
82     switch (dwarf_accessibility)
83     {
84         case DW_ACCESS_public:      return eAccessPublic;
85         case DW_ACCESS_private:     return eAccessPrivate;
86         case DW_ACCESS_protected:   return eAccessProtected;
87         default:                    break;
88     }
89     return eAccessNone;
90 }
91 
92 void
93 SymbolFileDWARF::Initialize()
94 {
95     LogChannelDWARF::Initialize();
96     PluginManager::RegisterPlugin (GetPluginNameStatic(),
97                                    GetPluginDescriptionStatic(),
98                                    CreateInstance);
99 }
100 
101 void
102 SymbolFileDWARF::Terminate()
103 {
104     PluginManager::UnregisterPlugin (CreateInstance);
105     LogChannelDWARF::Initialize();
106 }
107 
108 
109 const char *
110 SymbolFileDWARF::GetPluginNameStatic()
111 {
112     return "dwarf";
113 }
114 
115 const char *
116 SymbolFileDWARF::GetPluginDescriptionStatic()
117 {
118     return "DWARF and DWARF3 debug symbol file reader.";
119 }
120 
121 
122 SymbolFile*
123 SymbolFileDWARF::CreateInstance (ObjectFile* obj_file)
124 {
125     return new SymbolFileDWARF(obj_file);
126 }
127 
128 TypeList *
129 SymbolFileDWARF::GetTypeList ()
130 {
131     if (m_debug_map_symfile)
132         return m_debug_map_symfile->GetTypeList();
133     return m_obj_file->GetModule()->GetTypeList();
134 
135 }
136 
137 //----------------------------------------------------------------------
138 // Gets the first parent that is a lexical block, function or inlined
139 // subroutine, or compile unit.
140 //----------------------------------------------------------------------
141 static const DWARFDebugInfoEntry *
142 GetParentSymbolContextDIE(const DWARFDebugInfoEntry *child_die)
143 {
144     const DWARFDebugInfoEntry *die;
145     for (die = child_die->GetParent(); die != NULL; die = die->GetParent())
146     {
147         dw_tag_t tag = die->Tag();
148 
149         switch (tag)
150         {
151         case DW_TAG_compile_unit:
152         case DW_TAG_subprogram:
153         case DW_TAG_inlined_subroutine:
154         case DW_TAG_lexical_block:
155             return die;
156         }
157     }
158     return NULL;
159 }
160 
161 
162 SymbolFileDWARF::SymbolFileDWARF(ObjectFile* objfile) :
163     SymbolFile (objfile),
164     m_debug_map_symfile (NULL),
165     m_clang_tu_decl (NULL),
166     m_flags(),
167     m_data_debug_abbrev (),
168     m_data_debug_aranges (),
169     m_data_debug_frame (),
170     m_data_debug_info (),
171     m_data_debug_line (),
172     m_data_debug_loc (),
173     m_data_debug_ranges (),
174     m_data_debug_str (),
175     m_data_apple_names (),
176     m_data_apple_types (),
177     m_data_apple_namespaces (),
178     m_abbr(),
179     m_info(),
180     m_line(),
181     m_apple_names_ap (),
182     m_apple_types_ap (),
183     m_apple_namespaces_ap (),
184     m_function_basename_index(),
185     m_function_fullname_index(),
186     m_function_method_index(),
187     m_function_selector_index(),
188     m_objc_class_selectors_index(),
189     m_global_index(),
190     m_type_index(),
191     m_namespace_index(),
192     m_indexed (false),
193     m_is_external_ast_source (false),
194     m_ranges(),
195     m_unique_ast_type_map ()
196 {
197 }
198 
199 SymbolFileDWARF::~SymbolFileDWARF()
200 {
201     if (m_is_external_ast_source)
202         m_obj_file->GetModule()->GetClangASTContext().RemoveExternalSource ();
203 }
204 
205 static const ConstString &
206 GetDWARFMachOSegmentName ()
207 {
208     static ConstString g_dwarf_section_name ("__DWARF");
209     return g_dwarf_section_name;
210 }
211 
212 UniqueDWARFASTTypeMap &
213 SymbolFileDWARF::GetUniqueDWARFASTTypeMap ()
214 {
215     if (m_debug_map_symfile)
216         return m_debug_map_symfile->GetUniqueDWARFASTTypeMap ();
217     return m_unique_ast_type_map;
218 }
219 
220 ClangASTContext &
221 SymbolFileDWARF::GetClangASTContext ()
222 {
223     if (m_debug_map_symfile)
224         return m_debug_map_symfile->GetClangASTContext ();
225 
226     ClangASTContext &ast = m_obj_file->GetModule()->GetClangASTContext();
227     if (!m_is_external_ast_source)
228     {
229         m_is_external_ast_source = true;
230         llvm::OwningPtr<clang::ExternalASTSource> ast_source_ap (
231             new ClangExternalASTSourceCallbacks (SymbolFileDWARF::CompleteTagDecl,
232                                                  SymbolFileDWARF::CompleteObjCInterfaceDecl,
233                                                  SymbolFileDWARF::FindExternalVisibleDeclsByName,
234                                                  this));
235 
236         ast.SetExternalSource (ast_source_ap);
237     }
238     return ast;
239 }
240 
241 void
242 SymbolFileDWARF::InitializeObject()
243 {
244     // Install our external AST source callbacks so we can complete Clang types.
245     Module *module = m_obj_file->GetModule();
246     if (module)
247     {
248         const SectionList *section_list = m_obj_file->GetSectionList();
249 
250         const Section* section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
251 
252         // Memory map the DWARF mach-o segment so we have everything mmap'ed
253         // to keep our heap memory usage down.
254         if (section)
255             section->MemoryMapSectionDataFromObjectFile(m_obj_file, m_dwarf_data);
256     }
257     get_apple_names_data();
258     if (m_data_apple_names.GetByteSize() > 0)
259     {
260         m_apple_names_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_names, get_debug_str_data(), true));
261         if (!m_apple_names_ap->IsValid())
262             m_apple_names_ap.reset();
263     }
264     get_apple_types_data();
265     if (m_data_apple_types.GetByteSize() > 0)
266     {
267         m_apple_types_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_types, get_debug_str_data(), false));
268         if (!m_apple_types_ap->IsValid())
269             m_apple_types_ap.reset();
270     }
271 
272     get_apple_namespaces_data();
273     if (m_data_apple_namespaces.GetByteSize() > 0)
274     {
275         m_apple_namespaces_ap.reset (new DWARFMappedHash::MemoryTable (m_data_apple_namespaces, get_debug_str_data(), false));
276         if (!m_apple_namespaces_ap->IsValid())
277             m_apple_namespaces_ap.reset();
278     }
279 
280 }
281 
282 bool
283 SymbolFileDWARF::SupportedVersion(uint16_t version)
284 {
285     return version == 2 || version == 3;
286 }
287 
288 uint32_t
289 SymbolFileDWARF::GetAbilities ()
290 {
291     uint32_t abilities = 0;
292     if (m_obj_file != NULL)
293     {
294         const Section* section = NULL;
295         const SectionList *section_list = m_obj_file->GetSectionList();
296         if (section_list == NULL)
297             return 0;
298 
299         uint64_t debug_abbrev_file_size = 0;
300         uint64_t debug_aranges_file_size = 0;
301         uint64_t debug_frame_file_size = 0;
302         uint64_t debug_info_file_size = 0;
303         uint64_t debug_line_file_size = 0;
304         uint64_t debug_loc_file_size = 0;
305         uint64_t debug_macinfo_file_size = 0;
306         uint64_t debug_pubnames_file_size = 0;
307         uint64_t debug_pubtypes_file_size = 0;
308         uint64_t debug_ranges_file_size = 0;
309         uint64_t debug_str_file_size = 0;
310 
311         section = section_list->FindSectionByName(GetDWARFMachOSegmentName ()).get();
312 
313         if (section)
314             section_list = &section->GetChildren ();
315 
316         section = section_list->FindSectionByType (eSectionTypeDWARFDebugInfo, true).get();
317         if (section != NULL)
318         {
319             debug_info_file_size = section->GetByteSize();
320 
321             section = section_list->FindSectionByType (eSectionTypeDWARFDebugAbbrev, true).get();
322             if (section)
323                 debug_abbrev_file_size = section->GetByteSize();
324             else
325                 m_flags.Set (flagsGotDebugAbbrevData);
326 
327             section = section_list->FindSectionByType (eSectionTypeDWARFDebugAranges, true).get();
328             if (section)
329                 debug_aranges_file_size = section->GetByteSize();
330             else
331                 m_flags.Set (flagsGotDebugArangesData);
332 
333             section = section_list->FindSectionByType (eSectionTypeDWARFDebugFrame, true).get();
334             if (section)
335                 debug_frame_file_size = section->GetByteSize();
336             else
337                 m_flags.Set (flagsGotDebugFrameData);
338 
339             section = section_list->FindSectionByType (eSectionTypeDWARFDebugLine, true).get();
340             if (section)
341                 debug_line_file_size = section->GetByteSize();
342             else
343                 m_flags.Set (flagsGotDebugLineData);
344 
345             section = section_list->FindSectionByType (eSectionTypeDWARFDebugLoc, true).get();
346             if (section)
347                 debug_loc_file_size = section->GetByteSize();
348             else
349                 m_flags.Set (flagsGotDebugLocData);
350 
351             section = section_list->FindSectionByType (eSectionTypeDWARFDebugMacInfo, true).get();
352             if (section)
353                 debug_macinfo_file_size = section->GetByteSize();
354             else
355                 m_flags.Set (flagsGotDebugMacInfoData);
356 
357             section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubNames, true).get();
358             if (section)
359                 debug_pubnames_file_size = section->GetByteSize();
360             else
361                 m_flags.Set (flagsGotDebugPubNamesData);
362 
363             section = section_list->FindSectionByType (eSectionTypeDWARFDebugPubTypes, true).get();
364             if (section)
365                 debug_pubtypes_file_size = section->GetByteSize();
366             else
367                 m_flags.Set (flagsGotDebugPubTypesData);
368 
369             section = section_list->FindSectionByType (eSectionTypeDWARFDebugRanges, true).get();
370             if (section)
371                 debug_ranges_file_size = section->GetByteSize();
372             else
373                 m_flags.Set (flagsGotDebugRangesData);
374 
375             section = section_list->FindSectionByType (eSectionTypeDWARFDebugStr, true).get();
376             if (section)
377                 debug_str_file_size = section->GetByteSize();
378             else
379                 m_flags.Set (flagsGotDebugStrData);
380         }
381 
382         if (debug_abbrev_file_size > 0 && debug_info_file_size > 0)
383             abilities |= CompileUnits | Functions | Blocks | GlobalVariables | LocalVariables | VariableTypes;
384 
385         if (debug_line_file_size > 0)
386             abilities |= LineTables;
387 
388         if (debug_aranges_file_size > 0)
389             abilities |= AddressAcceleratorTable;
390 
391         if (debug_pubnames_file_size > 0)
392             abilities |= FunctionAcceleratorTable;
393 
394         if (debug_pubtypes_file_size > 0)
395             abilities |= TypeAcceleratorTable;
396 
397         if (debug_macinfo_file_size > 0)
398             abilities |= MacroInformation;
399 
400         if (debug_frame_file_size > 0)
401             abilities |= CallFrameInformation;
402     }
403     return abilities;
404 }
405 
406 const DataExtractor&
407 SymbolFileDWARF::GetCachedSectionData (uint32_t got_flag, SectionType sect_type, DataExtractor &data)
408 {
409     if (m_flags.IsClear (got_flag))
410     {
411         m_flags.Set (got_flag);
412         const SectionList *section_list = m_obj_file->GetSectionList();
413         if (section_list)
414         {
415             Section *section = section_list->FindSectionByType(sect_type, true).get();
416             if (section)
417             {
418                 // See if we memory mapped the DWARF segment?
419                 if (m_dwarf_data.GetByteSize())
420                 {
421                     data.SetData(m_dwarf_data, section->GetOffset (), section->GetByteSize());
422                 }
423                 else
424                 {
425                     if (section->ReadSectionDataFromObjectFile(m_obj_file, data) == 0)
426                         data.Clear();
427                 }
428             }
429         }
430     }
431     return data;
432 }
433 
434 const DataExtractor&
435 SymbolFileDWARF::get_debug_abbrev_data()
436 {
437     return GetCachedSectionData (flagsGotDebugAbbrevData, eSectionTypeDWARFDebugAbbrev, m_data_debug_abbrev);
438 }
439 
440 const DataExtractor&
441 SymbolFileDWARF::get_debug_aranges_data()
442 {
443     return GetCachedSectionData (flagsGotDebugArangesData, eSectionTypeDWARFDebugAranges, m_data_debug_aranges);
444 }
445 
446 const DataExtractor&
447 SymbolFileDWARF::get_debug_frame_data()
448 {
449     return GetCachedSectionData (flagsGotDebugFrameData, eSectionTypeDWARFDebugFrame, m_data_debug_frame);
450 }
451 
452 const DataExtractor&
453 SymbolFileDWARF::get_debug_info_data()
454 {
455     return GetCachedSectionData (flagsGotDebugInfoData, eSectionTypeDWARFDebugInfo, m_data_debug_info);
456 }
457 
458 const DataExtractor&
459 SymbolFileDWARF::get_debug_line_data()
460 {
461     return GetCachedSectionData (flagsGotDebugLineData, eSectionTypeDWARFDebugLine, m_data_debug_line);
462 }
463 
464 const DataExtractor&
465 SymbolFileDWARF::get_debug_loc_data()
466 {
467     return GetCachedSectionData (flagsGotDebugLocData, eSectionTypeDWARFDebugLoc, m_data_debug_loc);
468 }
469 
470 const DataExtractor&
471 SymbolFileDWARF::get_debug_ranges_data()
472 {
473     return GetCachedSectionData (flagsGotDebugRangesData, eSectionTypeDWARFDebugRanges, m_data_debug_ranges);
474 }
475 
476 const DataExtractor&
477 SymbolFileDWARF::get_debug_str_data()
478 {
479     return GetCachedSectionData (flagsGotDebugStrData, eSectionTypeDWARFDebugStr, m_data_debug_str);
480 }
481 
482 const DataExtractor&
483 SymbolFileDWARF::get_apple_names_data()
484 {
485     return GetCachedSectionData (flagsGotDebugNamesData, eSectionTypeDWARFAppleNames, m_data_apple_names);
486 }
487 
488 const DataExtractor&
489 SymbolFileDWARF::get_apple_types_data()
490 {
491     return GetCachedSectionData (flagsGotDebugTypesData, eSectionTypeDWARFAppleTypes, m_data_apple_types);
492 }
493 
494 const DataExtractor&
495 SymbolFileDWARF::get_apple_namespaces_data()
496 {
497     return GetCachedSectionData (flagsGotDebugTypesData, eSectionTypeDWARFAppleNamespaces, m_data_apple_namespaces);
498 }
499 
500 
501 DWARFDebugAbbrev*
502 SymbolFileDWARF::DebugAbbrev()
503 {
504     if (m_abbr.get() == NULL)
505     {
506         const DataExtractor &debug_abbrev_data = get_debug_abbrev_data();
507         if (debug_abbrev_data.GetByteSize() > 0)
508         {
509             m_abbr.reset(new DWARFDebugAbbrev());
510             if (m_abbr.get())
511                 m_abbr->Parse(debug_abbrev_data);
512         }
513     }
514     return m_abbr.get();
515 }
516 
517 const DWARFDebugAbbrev*
518 SymbolFileDWARF::DebugAbbrev() const
519 {
520     return m_abbr.get();
521 }
522 
523 
524 DWARFDebugInfo*
525 SymbolFileDWARF::DebugInfo()
526 {
527     if (m_info.get() == NULL)
528     {
529         Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
530         if (get_debug_info_data().GetByteSize() > 0)
531         {
532             m_info.reset(new DWARFDebugInfo());
533             if (m_info.get())
534             {
535                 m_info->SetDwarfData(this);
536             }
537         }
538     }
539     return m_info.get();
540 }
541 
542 const DWARFDebugInfo*
543 SymbolFileDWARF::DebugInfo() const
544 {
545     return m_info.get();
546 }
547 
548 DWARFCompileUnit*
549 SymbolFileDWARF::GetDWARFCompileUnitForUID(lldb::user_id_t cu_uid)
550 {
551     DWARFDebugInfo* info = DebugInfo();
552     if (info)
553         return info->GetCompileUnit(cu_uid).get();
554     return NULL;
555 }
556 
557 
558 DWARFDebugRanges*
559 SymbolFileDWARF::DebugRanges()
560 {
561     if (m_ranges.get() == NULL)
562     {
563         Timer scoped_timer(__PRETTY_FUNCTION__, "%s this = %p", __PRETTY_FUNCTION__, this);
564         if (get_debug_ranges_data().GetByteSize() > 0)
565         {
566             m_ranges.reset(new DWARFDebugRanges());
567             if (m_ranges.get())
568                 m_ranges->Extract(this);
569         }
570     }
571     return m_ranges.get();
572 }
573 
574 const DWARFDebugRanges*
575 SymbolFileDWARF::DebugRanges() const
576 {
577     return m_ranges.get();
578 }
579 
580 bool
581 SymbolFileDWARF::ParseCompileUnit (DWARFCompileUnit* curr_cu, CompUnitSP& compile_unit_sp)
582 {
583     if (curr_cu != NULL)
584     {
585         const DWARFDebugInfoEntry * cu_die = curr_cu->GetCompileUnitDIEOnly ();
586         if (cu_die)
587         {
588             const char * cu_die_name = cu_die->GetName(this, curr_cu);
589             const char * cu_comp_dir = cu_die->GetAttributeValueAsString(this, curr_cu, DW_AT_comp_dir, NULL);
590             LanguageType cu_language = (LanguageType)cu_die->GetAttributeValueAsUnsigned(this, curr_cu, DW_AT_language, 0);
591             if (cu_die_name)
592             {
593                 FileSpec cu_file_spec;
594 
595                 if (cu_die_name[0] == '/' || cu_comp_dir == NULL || cu_comp_dir[0] == '\0')
596                 {
597                     // If we have a full path to the compile unit, we don't need to resolve
598                     // the file.  This can be expensive e.g. when the source files are NFS mounted.
599                     cu_file_spec.SetFile (cu_die_name, false);
600                 }
601                 else
602                 {
603                     std::string fullpath(cu_comp_dir);
604                     if (*fullpath.rbegin() != '/')
605                         fullpath += '/';
606                     fullpath += cu_die_name;
607                     cu_file_spec.SetFile (fullpath.c_str(), false);
608                 }
609 
610                 compile_unit_sp.reset(new CompileUnit(m_obj_file->GetModule(), curr_cu, cu_file_spec, curr_cu->GetOffset(), cu_language));
611                 if (compile_unit_sp.get())
612                 {
613                     curr_cu->SetUserData(compile_unit_sp.get());
614                     return true;
615                 }
616             }
617         }
618     }
619     return false;
620 }
621 
622 uint32_t
623 SymbolFileDWARF::GetNumCompileUnits()
624 {
625     DWARFDebugInfo* info = DebugInfo();
626     if (info)
627         return info->GetNumCompileUnits();
628     return 0;
629 }
630 
631 CompUnitSP
632 SymbolFileDWARF::ParseCompileUnitAtIndex(uint32_t cu_idx)
633 {
634     CompUnitSP comp_unit;
635     DWARFDebugInfo* info = DebugInfo();
636     if (info)
637     {
638         DWARFCompileUnit* curr_cu = info->GetCompileUnitAtIndex(cu_idx);
639         if (curr_cu != NULL)
640         {
641             // Our symbol vendor shouldn't be asking us to add a compile unit that
642             // has already been added to it, which this DWARF plug-in knows as it
643             // stores the lldb compile unit (CompileUnit) pointer in each
644             // DWARFCompileUnit object when it gets added.
645             assert(curr_cu->GetUserData() == NULL);
646             ParseCompileUnit(curr_cu, comp_unit);
647         }
648     }
649     return comp_unit;
650 }
651 
652 static void
653 AddRangesToBlock (Block& block,
654                   DWARFDebugRanges::RangeList& ranges,
655                   addr_t block_base_addr)
656 {
657     const size_t num_ranges = ranges.GetSize();
658     for (size_t i = 0; i<num_ranges; ++i)
659     {
660         const DWARFDebugRanges::Range &range = ranges.GetEntryRef (i);
661         const addr_t range_base = range.GetRangeBase();
662         assert (range_base >= block_base_addr);
663         block.AddRange(Block::Range (range_base - block_base_addr, range.GetByteSize()));;
664     }
665     block.FinalizeRanges ();
666 }
667 
668 
669 Function *
670 SymbolFileDWARF::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.GetMinRangeBase (0);
693         lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (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 
742 size_t
743 SymbolFileDWARF::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 
767 bool
768 SymbolFileDWARF::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 
789 struct 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 //----------------------------------------------------------------------
805 static void
806 ParseDWARFLineTableCallback(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 
967 bool
968 SymbolFileDWARF::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 
995 size_t
996 SymbolFileDWARF::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.GetMinRangeBase(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.GetMinRangeBase(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 
1121 size_t
1122 SymbolFileDWARF::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 
1368 clang::DeclContext*
1369 SymbolFileDWARF::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 
1382 clang::DeclContext*
1383 SymbolFileDWARF::GetClangDeclContextForTypeUID (const lldb_private::SymbolContext &sc, lldb::user_id_t type_uid)
1384 {
1385     return GetClangDeclContextForDIEOffset (sc, type_uid);
1386 }
1387 
1388 Type*
1389 SymbolFileDWARF::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.
1419 bool
1420 SymbolFileDWARF::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 
1428 lldb::clang_type_t
1429 SymbolFileDWARF::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 
1613 Type*
1614 SymbolFileDWARF::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 
1628 CompileUnit*
1629 SymbolFileDWARF::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 
1653 bool
1654 SymbolFileDWARF::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.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, UINT32_MAX);
1659 
1660     sc.function = sc.comp_unit->FindFunctionByUID (func_die->GetOffset()).get();
1661     if (sc.function == NULL)
1662         sc.function = ParseCompileUnitFunction(sc, curr_cu, func_die);
1663 
1664     if (sc.function)
1665     {
1666         sc.module_sp = sc.function->CalculateSymbolContextModule();
1667         return true;
1668     }
1669 
1670     return false;
1671 }
1672 
1673 uint32_t
1674 SymbolFileDWARF::ResolveSymbolContext (const Address& so_addr, uint32_t resolve_scope, SymbolContext& sc)
1675 {
1676     Timer scoped_timer(__PRETTY_FUNCTION__,
1677                        "SymbolFileDWARF::ResolveSymbolContext (so_addr = { section = %p, offset = 0x%llx }, resolve_scope = 0x%8.8x)",
1678                        so_addr.GetSection(),
1679                        so_addr.GetOffset(),
1680                        resolve_scope);
1681     uint32_t resolved = 0;
1682     if (resolve_scope & (   eSymbolContextCompUnit |
1683                             eSymbolContextFunction |
1684                             eSymbolContextBlock |
1685                             eSymbolContextLineEntry))
1686     {
1687         lldb::addr_t file_vm_addr = so_addr.GetFileAddress();
1688 
1689         DWARFDebugInfo* debug_info = DebugInfo();
1690         if (debug_info)
1691         {
1692             dw_offset_t cu_offset = debug_info->GetCompileUnitAranges().FindAddress(file_vm_addr);
1693             if (cu_offset != DW_INVALID_OFFSET)
1694             {
1695                 uint32_t cu_idx;
1696                 DWARFCompileUnit* curr_cu = debug_info->GetCompileUnit(cu_offset, &cu_idx).get();
1697                 if (curr_cu)
1698                 {
1699                     sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
1700                     assert(sc.comp_unit != NULL);
1701                     resolved |= eSymbolContextCompUnit;
1702 
1703                     if (resolve_scope & eSymbolContextLineEntry)
1704                     {
1705                         LineTable *line_table = sc.comp_unit->GetLineTable();
1706                         if (line_table == NULL)
1707                         {
1708                             if (ParseCompileUnitLineTable(sc))
1709                                 line_table = sc.comp_unit->GetLineTable();
1710                         }
1711                         if (line_table != NULL)
1712                         {
1713                             if (so_addr.IsLinkedAddress())
1714                             {
1715                                 Address linked_addr (so_addr);
1716                                 linked_addr.ResolveLinkedAddress();
1717                                 if (line_table->FindLineEntryByAddress (linked_addr, sc.line_entry))
1718                                 {
1719                                     resolved |= eSymbolContextLineEntry;
1720                                 }
1721                             }
1722                             else if (line_table->FindLineEntryByAddress (so_addr, sc.line_entry))
1723                             {
1724                                 resolved |= eSymbolContextLineEntry;
1725                             }
1726                         }
1727                     }
1728 
1729                     if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1730                     {
1731                         DWARFDebugInfoEntry *function_die = NULL;
1732                         DWARFDebugInfoEntry *block_die = NULL;
1733                         if (resolve_scope & eSymbolContextBlock)
1734                         {
1735                             curr_cu->LookupAddress(file_vm_addr, &function_die, &block_die);
1736                         }
1737                         else
1738                         {
1739                             curr_cu->LookupAddress(file_vm_addr, &function_die, NULL);
1740                         }
1741 
1742                         if (function_die != NULL)
1743                         {
1744                             sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1745                             if (sc.function == NULL)
1746                                 sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
1747                         }
1748 
1749                         if (sc.function != NULL)
1750                         {
1751                             resolved |= eSymbolContextFunction;
1752 
1753                             if (resolve_scope & eSymbolContextBlock)
1754                             {
1755                                 Block& block = sc.function->GetBlock (true);
1756 
1757                                 if (block_die != NULL)
1758                                     sc.block = block.FindBlockByID (block_die->GetOffset());
1759                                 else
1760                                     sc.block = block.FindBlockByID (function_die->GetOffset());
1761                                 if (sc.block)
1762                                     resolved |= eSymbolContextBlock;
1763                             }
1764                         }
1765                     }
1766                 }
1767             }
1768         }
1769     }
1770     return resolved;
1771 }
1772 
1773 
1774 
1775 uint32_t
1776 SymbolFileDWARF::ResolveSymbolContext(const FileSpec& file_spec, uint32_t line, bool check_inlines, uint32_t resolve_scope, SymbolContextList& sc_list)
1777 {
1778     const uint32_t prev_size = sc_list.GetSize();
1779     if (resolve_scope & eSymbolContextCompUnit)
1780     {
1781         DWARFDebugInfo* debug_info = DebugInfo();
1782         if (debug_info)
1783         {
1784             uint32_t cu_idx;
1785             DWARFCompileUnit* curr_cu = NULL;
1786 
1787             for (cu_idx = 0; (curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx)) != NULL; ++cu_idx)
1788             {
1789                 CompileUnit *dc_cu = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
1790                 bool file_spec_matches_cu_file_spec = dc_cu != NULL && FileSpec::Compare(file_spec, *dc_cu, false) == 0;
1791                 if (check_inlines || file_spec_matches_cu_file_spec)
1792                 {
1793                     SymbolContext sc (m_obj_file->GetModule());
1794                     sc.comp_unit = GetCompUnitForDWARFCompUnit(curr_cu, cu_idx);
1795                     assert(sc.comp_unit != NULL);
1796 
1797                     uint32_t file_idx = UINT32_MAX;
1798 
1799                     // If we are looking for inline functions only and we don't
1800                     // find it in the support files, we are done.
1801                     if (check_inlines)
1802                     {
1803                         file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1804                         if (file_idx == UINT32_MAX)
1805                             continue;
1806                     }
1807 
1808                     if (line != 0)
1809                     {
1810                         LineTable *line_table = sc.comp_unit->GetLineTable();
1811 
1812                         if (line_table != NULL && line != 0)
1813                         {
1814                             // We will have already looked up the file index if
1815                             // we are searching for inline entries.
1816                             if (!check_inlines)
1817                                 file_idx = sc.comp_unit->GetSupportFiles().FindFileIndex (1, file_spec, true);
1818 
1819                             if (file_idx != UINT32_MAX)
1820                             {
1821                                 uint32_t found_line;
1822                                 uint32_t line_idx = line_table->FindLineEntryIndexByFileIndex (0, file_idx, line, false, &sc.line_entry);
1823                                 found_line = sc.line_entry.line;
1824 
1825                                 while (line_idx != UINT32_MAX)
1826                                 {
1827                                     sc.function = NULL;
1828                                     sc.block = NULL;
1829                                     if (resolve_scope & (eSymbolContextFunction | eSymbolContextBlock))
1830                                     {
1831                                         const lldb::addr_t file_vm_addr = sc.line_entry.range.GetBaseAddress().GetFileAddress();
1832                                         if (file_vm_addr != LLDB_INVALID_ADDRESS)
1833                                         {
1834                                             DWARFDebugInfoEntry *function_die = NULL;
1835                                             DWARFDebugInfoEntry *block_die = NULL;
1836                                             curr_cu->LookupAddress(file_vm_addr, &function_die, resolve_scope & eSymbolContextBlock ? &block_die : NULL);
1837 
1838                                             if (function_die != NULL)
1839                                             {
1840                                                 sc.function = sc.comp_unit->FindFunctionByUID (function_die->GetOffset()).get();
1841                                                 if (sc.function == NULL)
1842                                                     sc.function = ParseCompileUnitFunction(sc, curr_cu, function_die);
1843                                             }
1844 
1845                                             if (sc.function != NULL)
1846                                             {
1847                                                 Block& block = sc.function->GetBlock (true);
1848 
1849                                                 if (block_die != NULL)
1850                                                     sc.block = block.FindBlockByID (block_die->GetOffset());
1851                                                 else
1852                                                     sc.block = block.FindBlockByID (function_die->GetOffset());
1853                                             }
1854                                         }
1855                                     }
1856 
1857                                     sc_list.Append(sc);
1858                                     line_idx = line_table->FindLineEntryIndexByFileIndex (line_idx + 1, file_idx, found_line, true, &sc.line_entry);
1859                                 }
1860                             }
1861                         }
1862                         else if (file_spec_matches_cu_file_spec && !check_inlines)
1863                         {
1864                             // only append the context if we aren't looking for inline call sites
1865                             // by file and line and if the file spec matches that of the compile unit
1866                             sc_list.Append(sc);
1867                         }
1868                     }
1869                     else if (file_spec_matches_cu_file_spec && !check_inlines)
1870                     {
1871                         // only append the context if we aren't looking for inline call sites
1872                         // by file and line and if the file spec matches that of the compile unit
1873                         sc_list.Append(sc);
1874                     }
1875 
1876                     if (!check_inlines)
1877                         break;
1878                 }
1879             }
1880         }
1881     }
1882     return sc_list.GetSize() - prev_size;
1883 }
1884 
1885 void
1886 SymbolFileDWARF::Index ()
1887 {
1888     if (m_indexed)
1889         return;
1890     m_indexed = true;
1891     Timer scoped_timer (__PRETTY_FUNCTION__,
1892                         "SymbolFileDWARF::Index (%s)",
1893                         GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1894 
1895     DWARFDebugInfo* debug_info = DebugInfo();
1896     if (debug_info)
1897     {
1898         uint32_t cu_idx = 0;
1899         const uint32_t num_compile_units = GetNumCompileUnits();
1900         for (cu_idx = 0; cu_idx < num_compile_units; ++cu_idx)
1901         {
1902             DWARFCompileUnit* curr_cu = debug_info->GetCompileUnitAtIndex(cu_idx);
1903 
1904             bool clear_dies = curr_cu->ExtractDIEsIfNeeded (false) > 1;
1905 
1906             curr_cu->Index (cu_idx,
1907                             m_function_basename_index,
1908                             m_function_fullname_index,
1909                             m_function_method_index,
1910                             m_function_selector_index,
1911                             m_objc_class_selectors_index,
1912                             m_global_index,
1913                             m_type_index,
1914                             m_namespace_index);
1915 
1916             // Keep memory down by clearing DIEs if this generate function
1917             // caused them to be parsed
1918             if (clear_dies)
1919                 curr_cu->ClearDIEs (true);
1920         }
1921 
1922         m_function_basename_index.Finalize();
1923         m_function_fullname_index.Finalize();
1924         m_function_method_index.Finalize();
1925         m_function_selector_index.Finalize();
1926         m_objc_class_selectors_index.Finalize();
1927         m_global_index.Finalize();
1928         m_type_index.Finalize();
1929         m_namespace_index.Finalize();
1930 
1931 #if defined (ENABLE_DEBUG_PRINTF)
1932         StreamFile s(stdout, false);
1933         s.Printf ("DWARF index for '%s/%s':",
1934                   GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
1935                   GetObjectFile()->GetFileSpec().GetFilename().AsCString());
1936         s.Printf("\nFunction basenames:\n");    m_function_basename_index.Dump (&s);
1937         s.Printf("\nFunction fullnames:\n");    m_function_fullname_index.Dump (&s);
1938         s.Printf("\nFunction methods:\n");      m_function_method_index.Dump (&s);
1939         s.Printf("\nFunction selectors:\n");    m_function_selector_index.Dump (&s);
1940         s.Printf("\nObjective C class selectors:\n");    m_objc_class_selectors_index.Dump (&s);
1941         s.Printf("\nGlobals and statics:\n");   m_global_index.Dump (&s);
1942         s.Printf("\nTypes:\n");                 m_type_index.Dump (&s);
1943         s.Printf("\nNamepaces:\n");             m_namespace_index.Dump (&s);
1944 #endif
1945     }
1946 }
1947 
1948 bool
1949 SymbolFileDWARF::NamespaceDeclMatchesThisSymbolFile (const ClangNamespaceDecl *namespace_decl)
1950 {
1951     if (namespace_decl == NULL)
1952     {
1953         // Invalid namespace decl which means we aren't matching only things
1954         // in this symbol file, so return true to indicate it matches this
1955         // symbol file.
1956         return true;
1957     }
1958 
1959     clang::ASTContext *namespace_ast = namespace_decl->GetASTContext();
1960 
1961     if (namespace_ast == NULL)
1962         return true;    // No AST in the "namespace_decl", return true since it
1963                         // could then match any symbol file, including this one
1964 
1965     if (namespace_ast == GetClangASTContext().getASTContext())
1966         return true;    // The ASTs match, return true
1967 
1968     // The namespace AST was valid, and it does not match...
1969     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1970 
1971     if (log)
1972         log->Printf("Valid namespace does not match symbol file");
1973 
1974     return false;
1975 }
1976 
1977 bool
1978 SymbolFileDWARF::DIEIsInNamespace (const ClangNamespaceDecl *namespace_decl,
1979                                    DWARFCompileUnit* cu,
1980                                    const DWARFDebugInfoEntry* die)
1981 {
1982     // No namespace specified, so the answesr i
1983     if (namespace_decl == NULL)
1984         return true;
1985 
1986     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
1987 
1988     const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
1989     if (decl_ctx_die)
1990     {
1991 
1992         clang::NamespaceDecl *clang_namespace_decl = namespace_decl->GetNamespaceDecl();
1993         if (clang_namespace_decl)
1994         {
1995             if (decl_ctx_die->Tag() != DW_TAG_namespace)
1996             {
1997                 if (log)
1998                     log->Printf("Found a match, but its parent is not a namespace");
1999                 return false;
2000             }
2001 
2002             DeclContextToDIEMap::iterator pos = m_decl_ctx_to_die.find(clang_namespace_decl);
2003 
2004             if (pos == m_decl_ctx_to_die.end())
2005             {
2006                 if (log)
2007                     log->Printf("Found a match in a namespace, but its parent is not the requested namespace");
2008 
2009                 return false;
2010             }
2011 
2012             return decl_ctx_die == pos->second;
2013         }
2014         else
2015         {
2016             // We have a namespace_decl that was not NULL but it contained
2017             // a NULL "clang::NamespaceDecl", so this means the global namespace
2018             // So as long the the contained decl context DIE isn't a namespace
2019             // we should be ok.
2020             if (decl_ctx_die->Tag() != DW_TAG_namespace)
2021                 return true;
2022         }
2023     }
2024 
2025     if (log)
2026         log->Printf("Found a match, but its parent doesn't exist");
2027 
2028     return false;
2029 }
2030 uint32_t
2031 SymbolFileDWARF::FindGlobalVariables (const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, VariableList& variables)
2032 {
2033     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2034 
2035     if (log)
2036     {
2037         log->Printf ("SymbolFileDWARF::FindGlobalVariables (file=\"%s/%s\", name=\"%s\", append=%u, max_matches=%u, variables)",
2038                      m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2039                      m_obj_file->GetFileSpec().GetFilename().GetCString(),
2040                      name.GetCString(), append, max_matches);
2041     }
2042 
2043     if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2044 		return 0;
2045 
2046     DWARFDebugInfo* info = DebugInfo();
2047     if (info == NULL)
2048         return 0;
2049 
2050     // If we aren't appending the results to this list, then clear the list
2051     if (!append)
2052         variables.Clear();
2053 
2054     // Remember how many variables are in the list before we search in case
2055     // we are appending the results to a variable list.
2056     const uint32_t original_size = variables.GetSize();
2057 
2058     DIEArray die_offsets;
2059 
2060     if (m_apple_names_ap.get())
2061     {
2062         const char *name_cstr = name.GetCString();
2063         const char *base_name_start;
2064         const char *base_name_end = NULL;
2065 
2066         if (!CPPLanguageRuntime::StripNamespacesFromVariableName(name_cstr, base_name_start, base_name_end))
2067             base_name_start = name_cstr;
2068 
2069         m_apple_names_ap->FindByName (base_name_start, die_offsets);
2070     }
2071     else
2072     {
2073         // Index the DWARF if we haven't already
2074         if (!m_indexed)
2075             Index ();
2076 
2077         m_global_index.Find (name, die_offsets);
2078     }
2079 
2080     const size_t num_matches = die_offsets.size();
2081     if (num_matches)
2082     {
2083         SymbolContext sc;
2084         sc.module_sp = m_obj_file->GetModule();
2085         assert (sc.module_sp);
2086 
2087         DWARFDebugInfo* debug_info = DebugInfo();
2088         DWARFCompileUnit* dwarf_cu = NULL;
2089         const DWARFDebugInfoEntry* die = NULL;
2090         for (size_t i=0; i<num_matches; ++i)
2091         {
2092             const dw_offset_t die_offset = die_offsets[i];
2093             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2094 
2095             sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2096             assert(sc.comp_unit != NULL);
2097 
2098             if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2099                 continue;
2100 
2101             ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
2102 
2103             if (variables.GetSize() - original_size >= max_matches)
2104                 break;
2105         }
2106     }
2107 
2108     // Return the number of variable that were appended to the list
2109     return variables.GetSize() - original_size;
2110 }
2111 
2112 uint32_t
2113 SymbolFileDWARF::FindGlobalVariables(const RegularExpression& regex, bool append, uint32_t max_matches, VariableList& variables)
2114 {
2115     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2116 
2117     if (log)
2118     {
2119         log->Printf ("SymbolFileDWARF::FindGlobalVariables (file=\"%s/%s\", regex=\"%s\", append=%u, max_matches=%u, variables)",
2120                      m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2121                      m_obj_file->GetFileSpec().GetFilename().GetCString(),
2122                      regex.GetText(), append, max_matches);
2123     }
2124 
2125     DWARFDebugInfo* info = DebugInfo();
2126     if (info == NULL)
2127         return 0;
2128 
2129     // If we aren't appending the results to this list, then clear the list
2130     if (!append)
2131         variables.Clear();
2132 
2133     // Remember how many variables are in the list before we search in case
2134     // we are appending the results to a variable list.
2135     const uint32_t original_size = variables.GetSize();
2136 
2137     DIEArray die_offsets;
2138 
2139     if (m_apple_names_ap.get())
2140     {
2141         m_apple_names_ap->AppendAllDIEsThatMatchingRegex (regex, die_offsets);
2142     }
2143     else
2144     {
2145         // Index the DWARF if we haven't already
2146         if (!m_indexed)
2147             Index ();
2148 
2149         m_global_index.Find (regex, die_offsets);
2150     }
2151 
2152     SymbolContext sc;
2153     sc.module_sp = m_obj_file->GetModule();
2154     assert (sc.module_sp);
2155 
2156     DWARFCompileUnit* dwarf_cu = NULL;
2157     const DWARFDebugInfoEntry* die = NULL;
2158     const size_t num_matches = die_offsets.size();
2159     if (num_matches)
2160     {
2161         DWARFDebugInfo* debug_info = DebugInfo();
2162         for (size_t i=0; i<num_matches; ++i)
2163         {
2164             const dw_offset_t die_offset = die_offsets[i];
2165             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2166             sc.comp_unit = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
2167 
2168             ParseVariables(sc, dwarf_cu, LLDB_INVALID_ADDRESS, die, false, false, &variables);
2169 
2170             if (variables.GetSize() - original_size >= max_matches)
2171                 break;
2172         }
2173     }
2174 
2175     // Return the number of variable that were appended to the list
2176     return variables.GetSize() - original_size;
2177 }
2178 
2179 
2180 bool
2181 SymbolFileDWARF::ResolveFunction (dw_offset_t die_offset,
2182                                   DWARFCompileUnit *&dwarf_cu,
2183                                   SymbolContextList& sc_list)
2184 {
2185     const DWARFDebugInfoEntry *die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2186     return ResolveFunction (dwarf_cu, die, sc_list);
2187 }
2188 
2189 
2190 bool
2191 SymbolFileDWARF::ResolveFunction (DWARFCompileUnit *cu,
2192                                   const DWARFDebugInfoEntry *die,
2193                                   SymbolContextList& sc_list)
2194 {
2195     SymbolContext sc;
2196 
2197     if (die == NULL)
2198         return false;
2199 
2200     // If we were passed a die that is not a function, just return false...
2201     if (die->Tag() != DW_TAG_subprogram && die->Tag() != DW_TAG_inlined_subroutine)
2202         return false;
2203 
2204     const DWARFDebugInfoEntry* inlined_die = NULL;
2205     if (die->Tag() == DW_TAG_inlined_subroutine)
2206     {
2207         inlined_die = die;
2208 
2209         while ((die = die->GetParent()) != NULL)
2210         {
2211             if (die->Tag() == DW_TAG_subprogram)
2212                 break;
2213         }
2214     }
2215     assert (die->Tag() == DW_TAG_subprogram);
2216     if (GetFunction (cu, die, sc))
2217     {
2218         Address addr;
2219         // Parse all blocks if needed
2220         if (inlined_die)
2221         {
2222             sc.block = sc.function->GetBlock (true).FindBlockByID (inlined_die->GetOffset());
2223             assert (sc.block != NULL);
2224             if (sc.block->GetStartAddress (addr) == false)
2225                 addr.Clear();
2226         }
2227         else
2228         {
2229             sc.block = NULL;
2230             addr = sc.function->GetAddressRange().GetBaseAddress();
2231         }
2232 
2233         if (addr.IsValid())
2234         {
2235 
2236             // We found the function, so we should find the line table
2237             // and line table entry as well
2238             LineTable *line_table = sc.comp_unit->GetLineTable();
2239             if (line_table == NULL)
2240             {
2241                 if (ParseCompileUnitLineTable(sc))
2242                     line_table = sc.comp_unit->GetLineTable();
2243             }
2244             if (line_table != NULL)
2245                 line_table->FindLineEntryByAddress (addr, sc.line_entry);
2246 
2247             sc_list.Append(sc);
2248             return true;
2249         }
2250     }
2251 
2252     return false;
2253 }
2254 
2255 void
2256 SymbolFileDWARF::FindFunctions (const ConstString &name,
2257                                 const NameToDIE &name_to_die,
2258                                 SymbolContextList& sc_list)
2259 {
2260     DIEArray die_offsets;
2261     if (name_to_die.Find (name, die_offsets))
2262     {
2263         ParseFunctions (die_offsets, sc_list);
2264     }
2265 }
2266 
2267 
2268 void
2269 SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2270                                 const NameToDIE &name_to_die,
2271                                 SymbolContextList& sc_list)
2272 {
2273     DIEArray die_offsets;
2274     if (name_to_die.Find (regex, die_offsets))
2275     {
2276         ParseFunctions (die_offsets, sc_list);
2277     }
2278 }
2279 
2280 
2281 void
2282 SymbolFileDWARF::FindFunctions (const RegularExpression &regex,
2283                                 const DWARFMappedHash::MemoryTable &memory_table,
2284                                 SymbolContextList& sc_list)
2285 {
2286     DIEArray die_offsets;
2287     if (memory_table.AppendAllDIEsThatMatchingRegex (regex, die_offsets))
2288     {
2289         ParseFunctions (die_offsets, sc_list);
2290     }
2291 }
2292 
2293 void
2294 SymbolFileDWARF::ParseFunctions (const DIEArray &die_offsets,
2295                                  SymbolContextList& sc_list)
2296 {
2297     const size_t num_matches = die_offsets.size();
2298     if (num_matches)
2299     {
2300         SymbolContext sc;
2301 
2302         DWARFCompileUnit* dwarf_cu = NULL;
2303         for (size_t i=0; i<num_matches; ++i)
2304         {
2305             const dw_offset_t die_offset = die_offsets[i];
2306             ResolveFunction (die_offset, dwarf_cu, sc_list);
2307         }
2308     }
2309 }
2310 
2311 bool
2312 SymbolFileDWARF::FunctionDieMatchesPartialName (const DWARFDebugInfoEntry* die,
2313                                                 const DWARFCompileUnit *dwarf_cu,
2314                                                 uint32_t name_type_mask,
2315                                                 const char *partial_name,
2316                                                 const char *base_name_start,
2317                                                 const char *base_name_end)
2318 {
2319     // If we are looking only for methods, throw away all the ones that aren't in C++ classes:
2320     if (name_type_mask == eFunctionNameTypeMethod
2321         || name_type_mask == eFunctionNameTypeBase)
2322     {
2323             clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIEOffset(die->GetOffset());
2324             if (!containing_decl_ctx)
2325                 return false;
2326 
2327             bool is_cxx_method = (containing_decl_ctx->getDeclKind() == clang::Decl::CXXRecord);
2328 
2329             if (!is_cxx_method && name_type_mask == eFunctionNameTypeMethod)
2330                 return false;
2331             if (is_cxx_method && name_type_mask == eFunctionNameTypeBase)
2332                 return false;
2333     }
2334 
2335     // Now we need to check whether the name we got back for this type matches the extra specifications
2336     // that were in the name we're looking up:
2337     if (base_name_start != partial_name || *base_name_end != '\0')
2338     {
2339         // First see if the stuff to the left matches the full name.  To do that let's see if
2340         // we can pull out the mips linkage name attribute:
2341 
2342         Mangled best_name;
2343 
2344         DWARFDebugInfoEntry::Attributes attributes;
2345         die->GetAttributes(this, dwarf_cu, NULL, attributes);
2346         uint32_t idx = attributes.FindAttributeIndex(DW_AT_MIPS_linkage_name);
2347         if (idx != UINT32_MAX)
2348         {
2349             DWARFFormValue form_value;
2350             if (attributes.ExtractFormValueAtIndex(this, idx, form_value))
2351             {
2352                 const char *name = form_value.AsCString(&get_debug_str_data());
2353                 best_name.SetValue (name, true);
2354             }
2355         }
2356         if (best_name)
2357         {
2358             const char *demangled = best_name.GetDemangledName().GetCString();
2359             if (demangled)
2360             {
2361                 std::string name_no_parens(partial_name, base_name_end - partial_name);
2362                 if (strstr (demangled, name_no_parens.c_str()) == NULL)
2363                     return false;
2364             }
2365         }
2366     }
2367 
2368     return true;
2369 }
2370 
2371 uint32_t
2372 SymbolFileDWARF::FindFunctions (const ConstString &name,
2373                                 const lldb_private::ClangNamespaceDecl *namespace_decl,
2374                                 uint32_t name_type_mask,
2375                                 bool append,
2376                                 SymbolContextList& sc_list)
2377 {
2378     Timer scoped_timer (__PRETTY_FUNCTION__,
2379                         "SymbolFileDWARF::FindFunctions (name = '%s')",
2380                         name.AsCString());
2381 
2382     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2383 
2384     if (log)
2385     {
2386         log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", name=\"%s\", name_type_mask=0x%x, append=%u, sc_list)",
2387                      m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2388                      m_obj_file->GetFileSpec().GetFilename().GetCString(),
2389                      name.GetCString(), name_type_mask, append);
2390     }
2391 
2392     // If we aren't appending the results to this list, then clear the list
2393     if (!append)
2394         sc_list.Clear();
2395 
2396     if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2397 		return 0;
2398 
2399     // If name is empty then we won't find anything.
2400     if (name.IsEmpty())
2401         return 0;
2402 
2403     // Remember how many sc_list are in the list before we search in case
2404     // we are appending the results to a variable list.
2405 
2406     const uint32_t original_size = sc_list.GetSize();
2407 
2408     const char *name_cstr = name.GetCString();
2409     uint32_t effective_name_type_mask = eFunctionNameTypeNone;
2410     const char *base_name_start = name_cstr;
2411     const char *base_name_end = name_cstr + strlen(name_cstr);
2412 
2413     if (name_type_mask & eFunctionNameTypeAuto)
2414     {
2415         if (CPPLanguageRuntime::IsCPPMangledName (name_cstr))
2416             effective_name_type_mask = eFunctionNameTypeFull;
2417         else if (ObjCLanguageRuntime::IsPossibleObjCMethodName (name_cstr))
2418             effective_name_type_mask = eFunctionNameTypeFull;
2419         else
2420         {
2421             if (ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2422                 effective_name_type_mask |= eFunctionNameTypeSelector;
2423 
2424             if (CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2425                 effective_name_type_mask |= (eFunctionNameTypeMethod | eFunctionNameTypeBase);
2426         }
2427     }
2428     else
2429     {
2430         effective_name_type_mask = name_type_mask;
2431         if (effective_name_type_mask & eFunctionNameTypeMethod || name_type_mask & eFunctionNameTypeBase)
2432         {
2433             // If they've asked for a CPP method or function name and it can't be that, we don't
2434             // even need to search for CPP methods or names.
2435             if (!CPPLanguageRuntime::IsPossibleCPPCall(name_cstr, base_name_start, base_name_end))
2436             {
2437                 effective_name_type_mask &= ~(eFunctionNameTypeMethod | eFunctionNameTypeBase);
2438                 if (effective_name_type_mask == eFunctionNameTypeNone)
2439                     return 0;
2440             }
2441         }
2442 
2443         if (effective_name_type_mask & eFunctionNameTypeSelector)
2444         {
2445             if (!ObjCLanguageRuntime::IsPossibleObjCSelector(name_cstr))
2446             {
2447                 effective_name_type_mask &= ~(eFunctionNameTypeSelector);
2448                 if (effective_name_type_mask == eFunctionNameTypeNone)
2449                     return 0;
2450             }
2451         }
2452     }
2453 
2454     DWARFDebugInfo* info = DebugInfo();
2455     if (info == NULL)
2456         return 0;
2457 
2458     DWARFCompileUnit *dwarf_cu = NULL;
2459     if (m_apple_names_ap.get())
2460     {
2461         DIEArray die_offsets;
2462 
2463         uint32_t num_matches = 0;
2464 
2465         if (effective_name_type_mask & eFunctionNameTypeFull)
2466         {
2467             // If they asked for the full name, match what they typed.  At some point we may
2468             // want to canonicalize this (strip double spaces, etc.  For now, we just add all the
2469             // dies that we find by exact match.
2470             num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2471             for (uint32_t i = 0; i < num_matches; i++)
2472             {
2473                 const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2474                 if (die)
2475                 {
2476                     if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2477                         continue;
2478 
2479                     ResolveFunction (dwarf_cu, die, sc_list);
2480                 }
2481             }
2482         }
2483         else
2484         {
2485             if (effective_name_type_mask & eFunctionNameTypeSelector)
2486             {
2487                 if (namespace_decl && *namespace_decl)
2488                     return 0; // no selectors in namespaces
2489 
2490                 num_matches = m_apple_names_ap->FindByName (name_cstr, die_offsets);
2491                 // Now make sure these are actually ObjC methods.  In this case we can simply look up the name,
2492                 // and if it is an ObjC method name, we're good.
2493 
2494                 for (uint32_t i = 0; i < num_matches; i++)
2495                 {
2496                     const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2497                     if (die)
2498                     {
2499                         const char *die_name = die->GetName(this, dwarf_cu);
2500                         if (ObjCLanguageRuntime::IsPossibleObjCMethodName(die_name))
2501                             ResolveFunction (dwarf_cu, die, sc_list);
2502                     }
2503                 }
2504                 die_offsets.clear();
2505             }
2506 
2507             if (effective_name_type_mask & eFunctionNameTypeMethod
2508                 || effective_name_type_mask & eFunctionNameTypeBase)
2509             {
2510                 if ((effective_name_type_mask & eFunctionNameTypeMethod) &&
2511                     (namespace_decl && *namespace_decl))
2512                     return 0; // no methods in namespaces
2513 
2514                 // The apple_names table stores just the "base name" of C++ methods in the table.  So we have to
2515                 // extract the base name, look that up, and if there is any other information in the name we were
2516                 // passed in we have to post-filter based on that.
2517 
2518                 // FIXME: Arrange the logic above so that we don't calculate the base name twice:
2519                 std::string base_name(base_name_start, base_name_end - base_name_start);
2520                 num_matches = m_apple_names_ap->FindByName (base_name.c_str(), die_offsets);
2521 
2522                 for (uint32_t i = 0; i < num_matches; i++)
2523                 {
2524                     const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2525                     if (die)
2526                     {
2527                         if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2528                             continue;
2529 
2530                         if (!FunctionDieMatchesPartialName(die,
2531                                                            dwarf_cu,
2532                                                            effective_name_type_mask,
2533                                                            name_cstr,
2534                                                            base_name_start,
2535                                                            base_name_end))
2536                             continue;
2537 
2538                         // If we get to here, the die is good, and we should add it:
2539                         ResolveFunction (dwarf_cu, die, sc_list);
2540                     }
2541                 }
2542                 die_offsets.clear();
2543             }
2544         }
2545     }
2546     else
2547     {
2548 
2549         // Index the DWARF if we haven't already
2550         if (!m_indexed)
2551             Index ();
2552 
2553         if (name_type_mask & eFunctionNameTypeFull)
2554             FindFunctions (name, m_function_fullname_index, sc_list);
2555 
2556         std::string base_name(base_name_start, base_name_end - base_name_start);
2557         ConstString base_name_const(base_name.c_str());
2558         DIEArray die_offsets;
2559         DWARFCompileUnit *dwarf_cu = NULL;
2560 
2561         if (effective_name_type_mask & eFunctionNameTypeBase)
2562         {
2563             uint32_t num_base = m_function_basename_index.Find(base_name_const, die_offsets);
2564             for (uint32_t i = 0; i < num_base; i++)
2565             {
2566                 const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2567                 if (die)
2568                 {
2569                     if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2570                         continue;
2571 
2572                     if (!FunctionDieMatchesPartialName(die,
2573                                                        dwarf_cu,
2574                                                        effective_name_type_mask,
2575                                                        name_cstr,
2576                                                        base_name_start,
2577                                                        base_name_end))
2578                         continue;
2579 
2580                     // If we get to here, the die is good, and we should add it:
2581                     ResolveFunction (dwarf_cu, die, sc_list);
2582                 }
2583             }
2584             die_offsets.clear();
2585         }
2586 
2587         if (effective_name_type_mask & eFunctionNameTypeMethod)
2588         {
2589             if (namespace_decl && *namespace_decl)
2590                 return 0; // no methods in namespaces
2591 
2592             uint32_t num_base = m_function_method_index.Find(base_name_const, die_offsets);
2593             {
2594                 for (uint32_t i = 0; i < num_base; i++)
2595                 {
2596                     const DWARFDebugInfoEntry* die = info->GetDIEPtrWithCompileUnitHint (die_offsets[i], &dwarf_cu);
2597                     if (die)
2598                     {
2599                         if (!FunctionDieMatchesPartialName(die,
2600                                                            dwarf_cu,
2601                                                            effective_name_type_mask,
2602                                                            name_cstr,
2603                                                            base_name_start,
2604                                                            base_name_end))
2605                             continue;
2606 
2607                         // If we get to here, the die is good, and we should add it:
2608                         ResolveFunction (dwarf_cu, die, sc_list);
2609                     }
2610                 }
2611             }
2612             die_offsets.clear();
2613         }
2614 
2615         if ((effective_name_type_mask & eFunctionNameTypeSelector) && (!namespace_decl || !*namespace_decl))
2616         {
2617             FindFunctions (name, m_function_selector_index, sc_list);
2618         }
2619 
2620     }
2621 
2622     // Return the number of variable that were appended to the list
2623     return sc_list.GetSize() - original_size;
2624 }
2625 
2626 uint32_t
2627 SymbolFileDWARF::FindFunctions(const RegularExpression& regex, bool append, SymbolContextList& sc_list)
2628 {
2629     Timer scoped_timer (__PRETTY_FUNCTION__,
2630                         "SymbolFileDWARF::FindFunctions (regex = '%s')",
2631                         regex.GetText());
2632 
2633     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2634 
2635     if (log)
2636     {
2637         log->Printf ("SymbolFileDWARF::FindFunctions (file=\"%s/%s\", regex=\"%s\"append=%u, sc_list)",
2638                      m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2639                      m_obj_file->GetFileSpec().GetFilename().GetCString(),
2640                      regex.GetText(), append);
2641     }
2642 
2643 
2644     // If we aren't appending the results to this list, then clear the list
2645     if (!append)
2646         sc_list.Clear();
2647 
2648     // Remember how many sc_list are in the list before we search in case
2649     // we are appending the results to a variable list.
2650     uint32_t original_size = sc_list.GetSize();
2651 
2652     if (m_apple_names_ap.get())
2653     {
2654         FindFunctions (regex, *m_apple_names_ap, sc_list);
2655     }
2656     else
2657     {
2658         // Index the DWARF if we haven't already
2659         if (!m_indexed)
2660             Index ();
2661 
2662         FindFunctions (regex, m_function_basename_index, sc_list);
2663 
2664         FindFunctions (regex, m_function_fullname_index, sc_list);
2665     }
2666 
2667     // Return the number of variable that were appended to the list
2668     return sc_list.GetSize() - original_size;
2669 }
2670 
2671 void
2672 SymbolFileDWARF::ReportError (const char *format, ...)
2673 {
2674     ::fprintf (stderr,
2675                "error: %s/%s ",
2676                m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2677                m_obj_file->GetFileSpec().GetFilename().GetCString());
2678 
2679     if (m_obj_file->GetModule()->GetObjectName())
2680         ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString());
2681 
2682     va_list args;
2683     va_start (args, format);
2684     vfprintf (stderr, format, args);
2685     va_end (args);
2686 }
2687 
2688 void
2689 SymbolFileDWARF::ReportWarning (const char *format, ...)
2690 {
2691     ::fprintf (stderr,
2692                "warning: %s/%s ",
2693                m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2694                m_obj_file->GetFileSpec().GetFilename().GetCString());
2695 
2696     if (m_obj_file->GetModule()->GetObjectName())
2697         ::fprintf (stderr, "(%s) ", m_obj_file->GetModule()->GetObjectName().GetCString());
2698 
2699     va_list args;
2700     va_start (args, format);
2701     vfprintf (stderr, format, args);
2702     va_end (args);
2703 }
2704 
2705 uint32_t
2706 SymbolFileDWARF::FindTypes(const SymbolContext& sc, const ConstString &name, const lldb_private::ClangNamespaceDecl *namespace_decl, bool append, uint32_t max_matches, TypeList& types)
2707 {
2708     DWARFDebugInfo* info = DebugInfo();
2709     if (info == NULL)
2710         return 0;
2711 
2712     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2713 
2714     if (log)
2715     {
2716         log->Printf ("SymbolFileDWARF::FindTypes (file=\"%s/%s\", sc, name=\"%s\", append=%u, max_matches=%u, type_list)",
2717                      m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2718                      m_obj_file->GetFileSpec().GetFilename().GetCString(),
2719                      name.GetCString(), append, max_matches);
2720     }
2721 
2722     // If we aren't appending the results to this list, then clear the list
2723     if (!append)
2724         types.Clear();
2725 
2726     if (!NamespaceDeclMatchesThisSymbolFile(namespace_decl))
2727 		return 0;
2728 
2729     DIEArray die_offsets;
2730 
2731     if (m_apple_types_ap.get())
2732     {
2733         const char *name_cstr = name.GetCString();
2734         m_apple_types_ap->FindByName (name_cstr, die_offsets);
2735     }
2736     else
2737     {
2738         if (!m_indexed)
2739             Index ();
2740 
2741         m_type_index.Find (name, die_offsets);
2742     }
2743 
2744 
2745     const size_t num_matches = die_offsets.size();
2746 
2747     if (num_matches)
2748     {
2749         const uint32_t initial_types_size = types.GetSize();
2750         DWARFCompileUnit* dwarf_cu = NULL;
2751         const DWARFDebugInfoEntry* die = NULL;
2752         DWARFDebugInfo* debug_info = DebugInfo();
2753         for (size_t i=0; i<num_matches; ++i)
2754         {
2755             const dw_offset_t die_offset = die_offsets[i];
2756             die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2757 
2758             if (namespace_decl && !DIEIsInNamespace (namespace_decl, dwarf_cu, die))
2759                 continue;
2760 
2761             Type *matching_type = ResolveType (dwarf_cu, die);
2762             if (matching_type)
2763             {
2764                 // We found a type pointer, now find the shared pointer form our type list
2765                 TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
2766                 if (type_sp)
2767                 {
2768                     types.InsertUnique (type_sp);
2769                     if (types.GetSize() >= max_matches)
2770                         break;
2771                 }
2772                 else
2773                 {
2774                     ReportError ("error: can't find shared pointer for type 0x%8.8x.\n", matching_type->GetID());
2775                 }
2776             }
2777         }
2778         return types.GetSize() - initial_types_size;
2779     }
2780     return 0;
2781 }
2782 
2783 
2784 ClangNamespaceDecl
2785 SymbolFileDWARF::FindNamespace (const SymbolContext& sc,
2786                                 const ConstString &name,
2787                                 const lldb_private::ClangNamespaceDecl *parent_namespace_decl)
2788 {
2789     LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_LOOKUPS));
2790 
2791     if (log)
2792     {
2793         log->Printf ("SymbolFileDWARF::FindNamespace (file=\"%s/%s\", sc, name=\"%s\")",
2794                      m_obj_file->GetFileSpec().GetDirectory().GetCString(),
2795                      m_obj_file->GetFileSpec().GetFilename().GetCString(),
2796                      name.GetCString());
2797     }
2798 
2799     if (!NamespaceDeclMatchesThisSymbolFile(parent_namespace_decl))
2800 		return ClangNamespaceDecl();
2801 
2802     ClangNamespaceDecl namespace_decl;
2803     DWARFDebugInfo* info = DebugInfo();
2804     if (info)
2805     {
2806         DIEArray die_offsets;
2807 
2808         // Index if we already haven't to make sure the compile units
2809         // get indexed and make their global DIE index list
2810         if (m_apple_namespaces_ap.get())
2811         {
2812             const char *name_cstr = name.GetCString();
2813             m_apple_namespaces_ap->FindByName (name_cstr, die_offsets);
2814         }
2815         else
2816         {
2817             if (!m_indexed)
2818                 Index ();
2819 
2820             m_namespace_index.Find (name, die_offsets);
2821         }
2822 
2823         DWARFCompileUnit* dwarf_cu = NULL;
2824         const DWARFDebugInfoEntry* die = NULL;
2825         const size_t num_matches = die_offsets.size();
2826         if (num_matches)
2827         {
2828             DWARFDebugInfo* debug_info = DebugInfo();
2829             for (size_t i=0; i<num_matches; ++i)
2830             {
2831                 const dw_offset_t die_offset = die_offsets[i];
2832                 die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
2833 
2834                 if (parent_namespace_decl && !DIEIsInNamespace (parent_namespace_decl, dwarf_cu, die))
2835                     continue;
2836 
2837                 clang::NamespaceDecl *clang_namespace_decl = ResolveNamespaceDIE (dwarf_cu, die);
2838                 if (clang_namespace_decl)
2839                 {
2840                     namespace_decl.SetASTContext (GetClangASTContext().getASTContext());
2841                     namespace_decl.SetNamespaceDecl (clang_namespace_decl);
2842                 }
2843             }
2844         }
2845     }
2846     return namespace_decl;
2847 }
2848 
2849 uint32_t
2850 SymbolFileDWARF::FindTypes(std::vector<dw_offset_t> die_offsets, uint32_t max_matches, TypeList& types)
2851 {
2852     // Remember how many sc_list are in the list before we search in case
2853     // we are appending the results to a variable list.
2854     uint32_t original_size = types.GetSize();
2855 
2856     const uint32_t num_die_offsets = die_offsets.size();
2857     // Parse all of the types we found from the pubtypes matches
2858     uint32_t i;
2859     uint32_t num_matches = 0;
2860     for (i = 0; i < num_die_offsets; ++i)
2861     {
2862         Type *matching_type = ResolveTypeUID (die_offsets[i]);
2863         if (matching_type)
2864         {
2865             // We found a type pointer, now find the shared pointer form our type list
2866             TypeSP type_sp (GetTypeList()->FindType(matching_type->GetID()));
2867             assert (type_sp.get() != NULL);
2868             types.InsertUnique (type_sp);
2869             ++num_matches;
2870             if (num_matches >= max_matches)
2871                 break;
2872         }
2873     }
2874 
2875     // Return the number of variable that were appended to the list
2876     return types.GetSize() - original_size;
2877 }
2878 
2879 
2880 size_t
2881 SymbolFileDWARF::ParseChildParameters (const SymbolContext& sc,
2882                                        clang::DeclContext *containing_decl_ctx,
2883                                        TypeSP& type_sp,
2884                                        DWARFCompileUnit* dwarf_cu,
2885                                        const DWARFDebugInfoEntry *parent_die,
2886                                        bool skip_artificial,
2887                                        bool &is_static,
2888                                        TypeList* type_list,
2889                                        std::vector<clang_type_t>& function_param_types,
2890                                        std::vector<clang::ParmVarDecl*>& function_param_decls,
2891                                        unsigned &type_quals)
2892 {
2893     if (parent_die == NULL)
2894         return 0;
2895 
2896     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
2897 
2898     size_t arg_idx = 0;
2899     const DWARFDebugInfoEntry *die;
2900     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
2901     {
2902         dw_tag_t tag = die->Tag();
2903         switch (tag)
2904         {
2905         case DW_TAG_formal_parameter:
2906             {
2907                 DWARFDebugInfoEntry::Attributes attributes;
2908                 const size_t num_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
2909                 if (num_attributes > 0)
2910                 {
2911                     const char *name = NULL;
2912                     Declaration decl;
2913                     dw_offset_t param_type_die_offset = DW_INVALID_OFFSET;
2914                     bool is_artificial = false;
2915                     // one of None, Auto, Register, Extern, Static, PrivateExtern
2916 
2917                     clang::StorageClass storage = clang::SC_None;
2918                     uint32_t i;
2919                     for (i=0; i<num_attributes; ++i)
2920                     {
2921                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
2922                         DWARFFormValue form_value;
2923                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
2924                         {
2925                             switch (attr)
2926                             {
2927                             case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2928                             case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
2929                             case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2930                             case DW_AT_name:        name = form_value.AsCString(&get_debug_str_data()); break;
2931                             case DW_AT_type:        param_type_die_offset = form_value.Reference(dwarf_cu); break;
2932                             case DW_AT_artificial:  is_artificial = form_value.Unsigned() != 0; break;
2933                             case DW_AT_location:
2934     //                          if (form_value.BlockData())
2935     //                          {
2936     //                              const DataExtractor& debug_info_data = debug_info();
2937     //                              uint32_t block_length = form_value.Unsigned();
2938     //                              DataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
2939     //                          }
2940     //                          else
2941     //                          {
2942     //                          }
2943     //                          break;
2944                             case DW_AT_const_value:
2945                             case DW_AT_default_value:
2946                             case DW_AT_description:
2947                             case DW_AT_endianity:
2948                             case DW_AT_is_optional:
2949                             case DW_AT_segment:
2950                             case DW_AT_variable_parameter:
2951                             default:
2952                             case DW_AT_abstract_origin:
2953                             case DW_AT_sibling:
2954                                 break;
2955                             }
2956                         }
2957                     }
2958 
2959                     bool skip = false;
2960                     if (skip_artificial)
2961                     {
2962                         if (is_artificial)
2963                         {
2964                             // In order to determine if a C++ member function is
2965                             // "const" we have to look at the const-ness of "this"...
2966                             // Ugly, but that
2967                             if (arg_idx == 0)
2968                             {
2969                                 if (containing_decl_ctx->getDeclKind() == clang::Decl::CXXRecord)
2970                                 {
2971                                     // Often times compilers omit the "this" name for the
2972                                     // specification DIEs, so we can't rely upon the name
2973                                     // being in the formal parameter DIE...
2974                                     if (name == NULL || ::strcmp(name, "this")==0)
2975                                     {
2976                                         Type *this_type = ResolveTypeUID (param_type_die_offset);
2977                                         if (this_type)
2978                                         {
2979                                             uint32_t encoding_mask = this_type->GetEncodingMask();
2980                                             if (encoding_mask & Type::eEncodingIsPointerUID)
2981                                             {
2982                                                 is_static = false;
2983 
2984                                                 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
2985                                                     type_quals |= clang::Qualifiers::Const;
2986                                                 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
2987                                                     type_quals |= clang::Qualifiers::Volatile;
2988                                             }
2989                                         }
2990                                     }
2991                                 }
2992                             }
2993                             skip = true;
2994                         }
2995                         else
2996                         {
2997 
2998                             // HACK: Objective C formal parameters "self" and "_cmd"
2999                             // are not marked as artificial in the DWARF...
3000                             CompileUnit *curr_cu = GetCompUnitForDWARFCompUnit(dwarf_cu, UINT32_MAX);
3001                             if (curr_cu && (curr_cu->GetLanguage() == eLanguageTypeObjC || curr_cu->GetLanguage() == eLanguageTypeObjC_plus_plus))
3002                             {
3003                                 if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3004                                     skip = true;
3005                             }
3006                         }
3007                     }
3008 
3009                     if (!skip)
3010                     {
3011                         Type *type = ResolveTypeUID(param_type_die_offset);
3012                         if (type)
3013                         {
3014                             function_param_types.push_back (type->GetClangForwardType());
3015 
3016                             clang::ParmVarDecl *param_var_decl = GetClangASTContext().CreateParameterDeclaration (name, type->GetClangForwardType(), storage);
3017                             assert(param_var_decl);
3018                             function_param_decls.push_back(param_var_decl);
3019                         }
3020                     }
3021                 }
3022                 arg_idx++;
3023             }
3024             break;
3025 
3026         default:
3027             break;
3028         }
3029     }
3030     return arg_idx;
3031 }
3032 
3033 size_t
3034 SymbolFileDWARF::ParseChildEnumerators
3035 (
3036     const SymbolContext& sc,
3037     clang_type_t  enumerator_clang_type,
3038     uint32_t enumerator_byte_size,
3039     DWARFCompileUnit* dwarf_cu,
3040     const DWARFDebugInfoEntry *parent_die
3041 )
3042 {
3043     if (parent_die == NULL)
3044         return 0;
3045 
3046     size_t enumerators_added = 0;
3047     const DWARFDebugInfoEntry *die;
3048     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3049 
3050     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3051     {
3052         const dw_tag_t tag = die->Tag();
3053         if (tag == DW_TAG_enumerator)
3054         {
3055             DWARFDebugInfoEntry::Attributes attributes;
3056             const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
3057             if (num_child_attributes > 0)
3058             {
3059                 const char *name = NULL;
3060                 bool got_value = false;
3061                 int64_t enum_value = 0;
3062                 Declaration decl;
3063 
3064                 uint32_t i;
3065                 for (i=0; i<num_child_attributes; ++i)
3066                 {
3067                     const dw_attr_t attr = attributes.AttributeAtIndex(i);
3068                     DWARFFormValue form_value;
3069                     if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3070                     {
3071                         switch (attr)
3072                         {
3073                         case DW_AT_const_value:
3074                             got_value = true;
3075                             enum_value = form_value.Unsigned();
3076                             break;
3077 
3078                         case DW_AT_name:
3079                             name = form_value.AsCString(&get_debug_str_data());
3080                             break;
3081 
3082                         case DW_AT_description:
3083                         default:
3084                         case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3085                         case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3086                         case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3087                         case DW_AT_sibling:
3088                             break;
3089                         }
3090                     }
3091                 }
3092 
3093                 if (name && name[0] && got_value)
3094                 {
3095                     GetClangASTContext().AddEnumerationValueToEnumerationType (enumerator_clang_type,
3096                                                                                enumerator_clang_type,
3097                                                                                decl,
3098                                                                                name,
3099                                                                                enum_value,
3100                                                                                enumerator_byte_size * 8);
3101                     ++enumerators_added;
3102                 }
3103             }
3104         }
3105     }
3106     return enumerators_added;
3107 }
3108 
3109 void
3110 SymbolFileDWARF::ParseChildArrayInfo
3111 (
3112     const SymbolContext& sc,
3113     DWARFCompileUnit* dwarf_cu,
3114     const DWARFDebugInfoEntry *parent_die,
3115     int64_t& first_index,
3116     std::vector<uint64_t>& element_orders,
3117     uint32_t& byte_stride,
3118     uint32_t& bit_stride
3119 )
3120 {
3121     if (parent_die == NULL)
3122         return;
3123 
3124     const DWARFDebugInfoEntry *die;
3125     const uint8_t *fixed_form_sizes = DWARFFormValue::GetFixedFormSizesForAddressSize (dwarf_cu->GetAddressByteSize());
3126     for (die = parent_die->GetFirstChild(); die != NULL; die = die->GetSibling())
3127     {
3128         const dw_tag_t tag = die->Tag();
3129         switch (tag)
3130         {
3131         case DW_TAG_enumerator:
3132             {
3133                 DWARFDebugInfoEntry::Attributes attributes;
3134                 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
3135                 if (num_child_attributes > 0)
3136                 {
3137                     const char *name = NULL;
3138                     bool got_value = false;
3139                     int64_t enum_value = 0;
3140 
3141                     uint32_t i;
3142                     for (i=0; i<num_child_attributes; ++i)
3143                     {
3144                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
3145                         DWARFFormValue form_value;
3146                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3147                         {
3148                             switch (attr)
3149                             {
3150                             case DW_AT_const_value:
3151                                 got_value = true;
3152                                 enum_value = form_value.Unsigned();
3153                                 break;
3154 
3155                             case DW_AT_name:
3156                                 name = form_value.AsCString(&get_debug_str_data());
3157                                 break;
3158 
3159                             case DW_AT_description:
3160                             default:
3161                             case DW_AT_decl_file:
3162                             case DW_AT_decl_line:
3163                             case DW_AT_decl_column:
3164                             case DW_AT_sibling:
3165                                 break;
3166                             }
3167                         }
3168                     }
3169                 }
3170             }
3171             break;
3172 
3173         case DW_TAG_subrange_type:
3174             {
3175                 DWARFDebugInfoEntry::Attributes attributes;
3176                 const size_t num_child_attributes = die->GetAttributes(this, dwarf_cu, fixed_form_sizes, attributes);
3177                 if (num_child_attributes > 0)
3178                 {
3179                     const char *name = NULL;
3180                     bool got_value = false;
3181                     uint64_t byte_size = 0;
3182                     int64_t enum_value = 0;
3183                     uint64_t num_elements = 0;
3184                     uint64_t lower_bound = 0;
3185                     uint64_t upper_bound = 0;
3186                     uint32_t i;
3187                     for (i=0; i<num_child_attributes; ++i)
3188                     {
3189                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
3190                         DWARFFormValue form_value;
3191                         if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3192                         {
3193                             switch (attr)
3194                             {
3195                             case DW_AT_const_value:
3196                                 got_value = true;
3197                                 enum_value = form_value.Unsigned();
3198                                 break;
3199 
3200                             case DW_AT_name:
3201                                 name = form_value.AsCString(&get_debug_str_data());
3202                                 break;
3203 
3204                             case DW_AT_count:
3205                                 num_elements = form_value.Unsigned();
3206                                 break;
3207 
3208                             case DW_AT_bit_stride:
3209                                 bit_stride = form_value.Unsigned();
3210                                 break;
3211 
3212                             case DW_AT_byte_stride:
3213                                 byte_stride = form_value.Unsigned();
3214                                 break;
3215 
3216                             case DW_AT_byte_size:
3217                                 byte_size = form_value.Unsigned();
3218                                 break;
3219 
3220                             case DW_AT_lower_bound:
3221                                 lower_bound = form_value.Unsigned();
3222                                 break;
3223 
3224                             case DW_AT_upper_bound:
3225                                 upper_bound = form_value.Unsigned();
3226                                 break;
3227 
3228                             default:
3229                             case DW_AT_abstract_origin:
3230                             case DW_AT_accessibility:
3231                             case DW_AT_allocated:
3232                             case DW_AT_associated:
3233                             case DW_AT_data_location:
3234                             case DW_AT_declaration:
3235                             case DW_AT_description:
3236                             case DW_AT_sibling:
3237                             case DW_AT_threads_scaled:
3238                             case DW_AT_type:
3239                             case DW_AT_visibility:
3240                                 break;
3241                             }
3242                         }
3243                     }
3244 
3245                     if (upper_bound > lower_bound)
3246                         num_elements = upper_bound - lower_bound + 1;
3247 
3248                     if (num_elements > 0)
3249                         element_orders.push_back (num_elements);
3250                 }
3251             }
3252             break;
3253         }
3254     }
3255 }
3256 
3257 TypeSP
3258 SymbolFileDWARF::GetTypeForDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry* die)
3259 {
3260     TypeSP type_sp;
3261     if (die != NULL)
3262     {
3263         assert(curr_cu != NULL);
3264         Type *type_ptr = m_die_to_type.lookup (die);
3265         if (type_ptr == NULL)
3266         {
3267             CompileUnit* lldb_cu = GetCompUnitForDWARFCompUnit(curr_cu);
3268             assert (lldb_cu);
3269             SymbolContext sc(lldb_cu);
3270             type_sp = ParseType(sc, curr_cu, die, NULL);
3271         }
3272         else if (type_ptr != DIE_IS_BEING_PARSED)
3273         {
3274             // Grab the existing type from the master types lists
3275             type_sp = GetTypeList()->FindType(type_ptr->GetID());
3276         }
3277 
3278     }
3279     return type_sp;
3280 }
3281 
3282 clang::DeclContext *
3283 SymbolFileDWARF::GetClangDeclContextContainingDIEOffset (dw_offset_t die_offset)
3284 {
3285     if (die_offset != DW_INVALID_OFFSET)
3286     {
3287         DWARFCompileUnitSP cu_sp;
3288         const DWARFDebugInfoEntry* die = DebugInfo()->GetDIEPtr(die_offset, &cu_sp);
3289         return GetClangDeclContextContainingDIE (cu_sp.get(), die);
3290     }
3291     return NULL;
3292 }
3293 
3294 clang::DeclContext *
3295 SymbolFileDWARF::GetClangDeclContextForDIEOffset (const SymbolContext &sc, dw_offset_t die_offset)
3296 {
3297     if (die_offset != DW_INVALID_OFFSET)
3298     {
3299         DWARFDebugInfo* debug_info = DebugInfo();
3300         if (debug_info)
3301         {
3302             DWARFCompileUnitSP cu_sp;
3303             const DWARFDebugInfoEntry* die = debug_info->GetDIEPtr(die_offset, &cu_sp);
3304             if (die)
3305                 return GetClangDeclContextForDIE (sc, cu_sp.get(), die);
3306         }
3307     }
3308     return NULL;
3309 }
3310 
3311 clang::NamespaceDecl *
3312 SymbolFileDWARF::ResolveNamespaceDIE (DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3313 {
3314     if (die->Tag() == DW_TAG_namespace)
3315     {
3316         const char *namespace_name = die->GetAttributeValueAsString(this, curr_cu, DW_AT_name, NULL);
3317         if (namespace_name)
3318         {
3319             Declaration decl;   // TODO: fill in the decl object
3320             clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextContainingDIE (curr_cu, die->GetParent()));
3321             if (namespace_decl)
3322                 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3323             return namespace_decl;
3324         }
3325     }
3326     return NULL;
3327 }
3328 
3329 clang::DeclContext *
3330 SymbolFileDWARF::GetClangDeclContextForDIE (const SymbolContext &sc, DWARFCompileUnit *curr_cu, const DWARFDebugInfoEntry *die)
3331 {
3332     clang::DeclContext *clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3333     if (clang_decl_ctx)
3334         return clang_decl_ctx;
3335     // If this DIE has a specification, or an abstract origin, then trace to those.
3336 
3337     dw_offset_t die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_specification, DW_INVALID_OFFSET);
3338     if (die_offset != DW_INVALID_OFFSET)
3339         return GetClangDeclContextForDIEOffset (sc, die_offset);
3340 
3341     die_offset = die->GetAttributeValueAsReference(this, curr_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3342     if (die_offset != DW_INVALID_OFFSET)
3343         return GetClangDeclContextForDIEOffset (sc, die_offset);
3344 
3345     // This is the DIE we want.  Parse it, then query our map.
3346 
3347     ParseType(sc, curr_cu, die, NULL);
3348 
3349     clang_decl_ctx = GetCachedClangDeclContextForDIE (die);
3350 
3351     return clang_decl_ctx;
3352 }
3353 
3354 clang::DeclContext *
3355 SymbolFileDWARF::GetClangDeclContextContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
3356 {
3357     if (m_clang_tu_decl == NULL)
3358         m_clang_tu_decl = GetClangASTContext().getASTContext()->getTranslationUnitDecl();
3359 
3360     const DWARFDebugInfoEntry *decl_ctx_die = GetDeclContextDIEContainingDIE (cu, die);
3361 
3362     if (decl_ctx_die)
3363     {
3364         DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find (decl_ctx_die);
3365         if (pos != m_die_to_decl_ctx.end())
3366             return pos->second;
3367 
3368         switch (decl_ctx_die->Tag())
3369         {
3370         case DW_TAG_compile_unit:
3371             return m_clang_tu_decl;
3372 
3373         case DW_TAG_namespace:
3374             {
3375                 const char *namespace_name = decl_ctx_die->GetAttributeValueAsString(this, cu, DW_AT_name, NULL);
3376                 if (namespace_name)
3377                 {
3378                     Declaration decl;   // TODO: fill in the decl object
3379                     clang::NamespaceDecl *namespace_decl = GetClangASTContext().GetUniqueNamespaceDeclaration (namespace_name, decl, GetClangDeclContextContainingDIE (cu, decl_ctx_die));
3380                     if (namespace_decl)
3381                         LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, decl_ctx_die);
3382                     return namespace_decl;
3383                 }
3384             }
3385             break;
3386 
3387         case DW_TAG_structure_type:
3388         case DW_TAG_union_type:
3389         case DW_TAG_class_type:
3390             {
3391                 Type* type = ResolveType (cu, decl_ctx_die);
3392                 if (type)
3393                 {
3394                     clang::DeclContext *decl_ctx = ClangASTContext::GetDeclContextForType (type->GetClangForwardType ());
3395                     if (decl_ctx)
3396                     {
3397                         LinkDeclContextToDIE (decl_ctx, decl_ctx_die);
3398                         if (decl_ctx)
3399                             return decl_ctx;
3400                     }
3401                 }
3402             }
3403             break;
3404 
3405         default:
3406             break;
3407         }
3408     }
3409     return m_clang_tu_decl;
3410 }
3411 
3412 
3413 const DWARFDebugInfoEntry *
3414 SymbolFileDWARF::GetDeclContextDIEContainingDIE (DWARFCompileUnit *cu, const DWARFDebugInfoEntry *die)
3415 {
3416     if (cu && die)
3417     {
3418         const DWARFDebugInfoEntry * const decl_die = die;
3419 
3420         while (die != NULL)
3421         {
3422             // If this is the original DIE that we are searching for a declaration
3423             // for, then don't look in the cache as we don't want our own decl
3424             // context to be our decl context...
3425             if (decl_die != die)
3426             {
3427                 switch (die->Tag())
3428                 {
3429                     case DW_TAG_compile_unit:
3430                     case DW_TAG_namespace:
3431                     case DW_TAG_structure_type:
3432                     case DW_TAG_union_type:
3433                     case DW_TAG_class_type:
3434                         return die;
3435 
3436                     default:
3437                         break;
3438                 }
3439             }
3440 
3441             dw_offset_t die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_specification, DW_INVALID_OFFSET);
3442             if (die_offset != DW_INVALID_OFFSET)
3443             {
3444                 DWARFCompileUnit *spec_cu = cu;
3445                 const DWARFDebugInfoEntry *spec_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &spec_cu);
3446                 const DWARFDebugInfoEntry *spec_die_decl_ctx_die = GetDeclContextDIEContainingDIE (spec_cu, spec_die);
3447                 if (spec_die_decl_ctx_die)
3448                     return spec_die_decl_ctx_die;
3449             }
3450 
3451             die_offset = die->GetAttributeValueAsReference(this, cu, DW_AT_abstract_origin, DW_INVALID_OFFSET);
3452             if (die_offset != DW_INVALID_OFFSET)
3453             {
3454                 DWARFCompileUnit *abs_cu = cu;
3455                 const DWARFDebugInfoEntry *abs_die = DebugInfo()->GetDIEPtrWithCompileUnitHint (die_offset, &abs_cu);
3456                 const DWARFDebugInfoEntry *abs_die_decl_ctx_die = GetDeclContextDIEContainingDIE (abs_cu, abs_die);
3457                 if (abs_die_decl_ctx_die)
3458                     return abs_die_decl_ctx_die;
3459             }
3460 
3461             die = die->GetParent();
3462         }
3463     }
3464     return NULL;
3465 }
3466 
3467 
3468 
3469 // This function can be used when a DIE is found that is a forward declaration
3470 // DIE and we want to try and find a type that has the complete definition.
3471 TypeSP
3472 SymbolFileDWARF::FindDefinitionTypeForDIE (DWARFCompileUnit* cu,
3473                                            const DWARFDebugInfoEntry *die,
3474                                            const ConstString &type_name)
3475 {
3476     TypeSP type_sp;
3477 
3478     if (cu == NULL || die == NULL || !type_name)
3479         return type_sp;
3480 
3481     DIEArray die_offsets;
3482 
3483     if (m_apple_types_ap.get())
3484     {
3485         const char *name_cstr = type_name.GetCString();
3486         m_apple_types_ap->FindByName (name_cstr, die_offsets);
3487     }
3488     else
3489     {
3490         if (!m_indexed)
3491             Index ();
3492 
3493         m_type_index.Find (type_name, die_offsets);
3494     }
3495 
3496 
3497     const size_t num_matches = die_offsets.size();
3498 
3499     const dw_tag_t type_tag = die->Tag();
3500 
3501     DWARFCompileUnit* type_cu = NULL;
3502     const DWARFDebugInfoEntry* type_die = NULL;
3503     if (num_matches)
3504     {
3505         DWARFDebugInfo* debug_info = DebugInfo();
3506         for (size_t i=0; i<num_matches; ++i)
3507         {
3508             const dw_offset_t die_offset = die_offsets[i];
3509             type_die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &type_cu);
3510 
3511             if (type_die != die && type_die->Tag() == type_tag)
3512             {
3513                 // Hold off on comparing parent DIE tags until
3514                 // we know what happens with stuff in namespaces
3515                 // for gcc and clang...
3516                 //DWARFDebugInfoEntry *parent_die = die->GetParent();
3517                 //DWARFDebugInfoEntry *parent_type_die = type_die->GetParent();
3518                 //if (parent_die->Tag() == parent_type_die->Tag())
3519                 {
3520                     Type *resolved_type = ResolveType (type_cu, type_die, false);
3521                     if (resolved_type && resolved_type != DIE_IS_BEING_PARSED)
3522                     {
3523                         DEBUG_PRINTF ("resolved 0x%8.8x (cu 0x%8.8x) from %s to 0x%8.8x (cu 0x%8.8x)\n",
3524                                       die->GetOffset(),
3525                                       curr_cu->GetOffset(),
3526                                       m_obj_file->GetFileSpec().GetFilename().AsCString(),
3527                                       type_die->GetOffset(),
3528                                       type_cu->GetOffset());
3529 
3530                         m_die_to_type[die] = resolved_type;
3531                         type_sp = GetTypeList()->FindType(resolved_type->GetID());
3532                         if (!type_sp)
3533                         {
3534                             DEBUG_PRINTF("unable to resolve type '%s' from DIE 0x%8.8x\n", type_name.GetCString(), die->GetOffset());
3535                         }
3536                         break;
3537                     }
3538                 }
3539             }
3540         }
3541     }
3542     return type_sp;
3543 }
3544 
3545 TypeSP
3546 SymbolFileDWARF::ParseType (const SymbolContext& sc, DWARFCompileUnit* dwarf_cu, const DWARFDebugInfoEntry *die, bool *type_is_new_ptr)
3547 {
3548     TypeSP type_sp;
3549 
3550     if (type_is_new_ptr)
3551         *type_is_new_ptr = false;
3552 
3553     AccessType accessibility = eAccessNone;
3554     if (die != NULL)
3555     {
3556         LogSP log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3557         if (log && dwarf_cu)
3558         {
3559             StreamString s;
3560             die->DumpLocation (this, dwarf_cu, s);
3561             log->Printf ("SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
3562 
3563         }
3564 
3565         Type *type_ptr = m_die_to_type.lookup (die);
3566         TypeList* type_list = GetTypeList();
3567         if (type_ptr == NULL)
3568         {
3569             ClangASTContext &ast = GetClangASTContext();
3570             if (type_is_new_ptr)
3571                 *type_is_new_ptr = true;
3572 
3573             const dw_tag_t tag = die->Tag();
3574 
3575             bool is_forward_declaration = false;
3576             DWARFDebugInfoEntry::Attributes attributes;
3577             const char *type_name_cstr = NULL;
3578             ConstString type_name_const_str;
3579             Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
3580             size_t byte_size = 0;
3581             bool byte_size_valid = false;
3582             Declaration decl;
3583 
3584             Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
3585             clang_type_t clang_type = NULL;
3586 
3587             dw_attr_t attr;
3588 
3589             switch (tag)
3590             {
3591             case DW_TAG_base_type:
3592             case DW_TAG_pointer_type:
3593             case DW_TAG_reference_type:
3594             case DW_TAG_typedef:
3595             case DW_TAG_const_type:
3596             case DW_TAG_restrict_type:
3597             case DW_TAG_volatile_type:
3598                 {
3599                     // Set a bit that lets us know that we are currently parsing this
3600                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3601 
3602                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3603                     uint32_t encoding = 0;
3604                     lldb::user_id_t encoding_uid = LLDB_INVALID_UID;
3605 
3606                     if (num_attributes > 0)
3607                     {
3608                         uint32_t i;
3609                         for (i=0; i<num_attributes; ++i)
3610                         {
3611                             attr = attributes.AttributeAtIndex(i);
3612                             DWARFFormValue form_value;
3613                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3614                             {
3615                                 switch (attr)
3616                                 {
3617                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3618                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3619                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3620                                 case DW_AT_name:
3621 
3622                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3623                                     // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
3624                                     // include the "&"...
3625                                     if (tag == DW_TAG_reference_type)
3626                                     {
3627                                         if (strchr (type_name_cstr, '&') == NULL)
3628                                             type_name_cstr = NULL;
3629                                     }
3630                                     if (type_name_cstr)
3631                                         type_name_const_str.SetCString(type_name_cstr);
3632                                     break;
3633                                 case DW_AT_byte_size:   byte_size = form_value.Unsigned();  byte_size_valid = true; break;
3634                                 case DW_AT_encoding:    encoding = form_value.Unsigned(); break;
3635                                 case DW_AT_type:        encoding_uid = form_value.Reference(dwarf_cu); break;
3636                                 default:
3637                                 case DW_AT_sibling:
3638                                     break;
3639                                 }
3640                             }
3641                         }
3642                     }
3643 
3644                     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);
3645 
3646                     switch (tag)
3647                     {
3648                     default:
3649                         break;
3650 
3651                     case DW_TAG_base_type:
3652                         resolve_state = Type::eResolveStateFull;
3653                         clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
3654                                                                                    encoding,
3655                                                                                    byte_size * 8);
3656                         break;
3657 
3658                     case DW_TAG_pointer_type:   encoding_data_type = Type::eEncodingIsPointerUID;           break;
3659                     case DW_TAG_reference_type: encoding_data_type = Type::eEncodingIsLValueReferenceUID;   break;
3660                     case DW_TAG_typedef:        encoding_data_type = Type::eEncodingIsTypedefUID;           break;
3661                     case DW_TAG_const_type:     encoding_data_type = Type::eEncodingIsConstUID;             break;
3662                     case DW_TAG_restrict_type:  encoding_data_type = Type::eEncodingIsRestrictUID;          break;
3663                     case DW_TAG_volatile_type:  encoding_data_type = Type::eEncodingIsVolatileUID;          break;
3664                     }
3665 
3666                     if (type_name_cstr != NULL && sc.comp_unit != NULL &&
3667                         (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus))
3668                     {
3669                         static ConstString g_objc_type_name_id("id");
3670                         static ConstString g_objc_type_name_Class("Class");
3671                         static ConstString g_objc_type_name_selector("SEL");
3672 
3673                         if (type_name_const_str == g_objc_type_name_id)
3674                         {
3675                             clang_type = ast.GetBuiltInType_objc_id();
3676                             resolve_state = Type::eResolveStateFull;
3677 
3678                         }
3679                         else if (type_name_const_str == g_objc_type_name_Class)
3680                         {
3681                             clang_type = ast.GetBuiltInType_objc_Class();
3682                             resolve_state = Type::eResolveStateFull;
3683                         }
3684                         else if (type_name_const_str == g_objc_type_name_selector)
3685                         {
3686                             clang_type = ast.GetBuiltInType_objc_selector();
3687                             resolve_state = Type::eResolveStateFull;
3688                         }
3689                     }
3690 
3691                     type_sp.reset( new Type (die->GetOffset(),
3692                                              this,
3693                                              type_name_const_str,
3694                                              byte_size,
3695                                              NULL,
3696                                              encoding_uid,
3697                                              encoding_data_type,
3698                                              &decl,
3699                                              clang_type,
3700                                              resolve_state));
3701 
3702                     m_die_to_type[die] = type_sp.get();
3703 
3704 //                  Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
3705 //                  if (encoding_type != NULL)
3706 //                  {
3707 //                      if (encoding_type != DIE_IS_BEING_PARSED)
3708 //                          type_sp->SetEncodingType(encoding_type);
3709 //                      else
3710 //                          m_indirect_fixups.push_back(type_sp.get());
3711 //                  }
3712                 }
3713                 break;
3714 
3715             case DW_TAG_structure_type:
3716             case DW_TAG_union_type:
3717             case DW_TAG_class_type:
3718                 {
3719                     // Set a bit that lets us know that we are currently parsing this
3720                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3721 
3722                     LanguageType class_language = eLanguageTypeUnknown;
3723                     //bool struct_is_class = false;
3724                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3725                     if (num_attributes > 0)
3726                     {
3727                         uint32_t i;
3728                         for (i=0; i<num_attributes; ++i)
3729                         {
3730                             attr = attributes.AttributeAtIndex(i);
3731                             DWARFFormValue form_value;
3732                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3733                             {
3734                                 switch (attr)
3735                                 {
3736                                 case DW_AT_decl_file:
3737                                     decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
3738                                     break;
3739 
3740                                 case DW_AT_decl_line:
3741                                     decl.SetLine(form_value.Unsigned());
3742                                     break;
3743 
3744                                 case DW_AT_decl_column:
3745                                     decl.SetColumn(form_value.Unsigned());
3746                                     break;
3747 
3748                                 case DW_AT_name:
3749                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3750                                     type_name_const_str.SetCString(type_name_cstr);
3751                                     break;
3752 
3753                                 case DW_AT_byte_size:
3754                                     byte_size = form_value.Unsigned();
3755                                     byte_size_valid = true;
3756                                     break;
3757 
3758                                 case DW_AT_accessibility:
3759                                     accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3760                                     break;
3761 
3762                                 case DW_AT_declaration:
3763                                     is_forward_declaration = form_value.Unsigned() != 0;
3764                                     break;
3765 
3766                                 case DW_AT_APPLE_runtime_class:
3767                                     class_language = (LanguageType)form_value.Signed();
3768                                     break;
3769 
3770                                 case DW_AT_allocated:
3771                                 case DW_AT_associated:
3772                                 case DW_AT_data_location:
3773                                 case DW_AT_description:
3774                                 case DW_AT_start_scope:
3775                                 case DW_AT_visibility:
3776                                 default:
3777                                 case DW_AT_sibling:
3778                                     break;
3779                                 }
3780                             }
3781                         }
3782                     }
3783 
3784                     UniqueDWARFASTType unique_ast_entry;
3785                     if (decl.IsValid())
3786                     {
3787                         if (GetUniqueDWARFASTTypeMap().Find (type_name_const_str,
3788                                                              this,
3789                                                              dwarf_cu,
3790                                                              die,
3791                                                              decl,
3792                                                              byte_size_valid ? byte_size : -1,
3793                                                              unique_ast_entry))
3794                         {
3795                             // We have already parsed this type or from another
3796                             // compile unit. GCC loves to use the "one definition
3797                             // rule" which can result in multiple definitions
3798                             // of the same class over and over in each compile
3799                             // unit.
3800                             type_sp = unique_ast_entry.m_type_sp;
3801                             if (type_sp)
3802                             {
3803                                 m_die_to_type[die] = type_sp.get();
3804                                 return type_sp;
3805                             }
3806                         }
3807                     }
3808 
3809                     DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3810 
3811                     int tag_decl_kind = -1;
3812                     AccessType default_accessibility = eAccessNone;
3813                     if (tag == DW_TAG_structure_type)
3814                     {
3815                         tag_decl_kind = clang::TTK_Struct;
3816                         default_accessibility = eAccessPublic;
3817                     }
3818                     else if (tag == DW_TAG_union_type)
3819                     {
3820                         tag_decl_kind = clang::TTK_Union;
3821                         default_accessibility = eAccessPublic;
3822                     }
3823                     else if (tag == DW_TAG_class_type)
3824                     {
3825                         tag_decl_kind = clang::TTK_Class;
3826                         default_accessibility = eAccessPrivate;
3827                     }
3828 
3829 
3830                     if (is_forward_declaration)
3831                     {
3832                         // We have a forward declaration to a type and we need
3833                         // to try and find a full declaration. We look in the
3834                         // current type index just in case we have a forward
3835                         // declaration followed by an actual declarations in the
3836                         // DWARF. If this fails, we need to look elsewhere...
3837 
3838                         type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
3839 
3840                         if (!type_sp && m_debug_map_symfile)
3841                         {
3842                             // We weren't able to find a full declaration in
3843                             // this DWARF, see if we have a declaration anywhere
3844                             // else...
3845                             type_sp = m_debug_map_symfile->FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
3846                         }
3847 
3848                         if (type_sp)
3849                         {
3850                             // We found a real definition for this type elsewhere
3851                             // so lets use it and cache the fact that we found
3852                             // a complete type for this die
3853                             m_die_to_type[die] = type_sp.get();
3854                             return type_sp;
3855                         }
3856                     }
3857                     assert (tag_decl_kind != -1);
3858                     bool clang_type_was_created = false;
3859                     clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3860                     if (clang_type == NULL)
3861                     {
3862                         clang_type_was_created = true;
3863                         clang_type = ast.CreateRecordType (type_name_cstr,
3864                                                            tag_decl_kind,
3865                                                            GetClangDeclContextContainingDIE (dwarf_cu, die),
3866                                                            class_language);
3867                     }
3868 
3869                     // Store a forward declaration to this class type in case any
3870                     // parameters in any class methods need it for the clang
3871                     // types for function prototypes.
3872                     LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
3873                     type_sp.reset (new Type (die->GetOffset(),
3874                                              this,
3875                                              type_name_const_str,
3876                                              byte_size,
3877                                              NULL,
3878                                              LLDB_INVALID_UID,
3879                                              Type::eEncodingIsUID,
3880                                              &decl,
3881                                              clang_type,
3882                                              Type::eResolveStateForward));
3883 
3884 
3885                     // Add our type to the unique type map so we don't
3886                     // end up creating many copies of the same type over
3887                     // and over in the ASTContext for our module
3888                     unique_ast_entry.m_type_sp = type_sp;
3889                     unique_ast_entry.m_symfile = this;
3890                     unique_ast_entry.m_cu = dwarf_cu;
3891                     unique_ast_entry.m_die = die;
3892                     unique_ast_entry.m_declaration = decl;
3893                     GetUniqueDWARFASTTypeMap().Insert (type_name_const_str,
3894                                                        unique_ast_entry);
3895 
3896                     if (die->HasChildren() == false && is_forward_declaration == false)
3897                     {
3898                         // No children for this struct/union/class, lets finish it
3899                         ast.StartTagDeclarationDefinition (clang_type);
3900                         ast.CompleteTagDeclarationDefinition (clang_type);
3901                     }
3902                     else if (clang_type_was_created)
3903                     {
3904                         // Leave this as a forward declaration until we need
3905                         // to know the details of the type. lldb_private::Type
3906                         // will automatically call the SymbolFile virtual function
3907                         // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
3908                         // When the definition needs to be defined.
3909                         m_forward_decl_die_to_clang_type[die] = clang_type;
3910                         m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
3911                         ClangASTContext::SetHasExternalStorage (clang_type, true);
3912                     }
3913                 }
3914                 break;
3915 
3916             case DW_TAG_enumeration_type:
3917                 {
3918                     // Set a bit that lets us know that we are currently parsing this
3919                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
3920 
3921                     lldb::user_id_t encoding_uid = DW_INVALID_OFFSET;
3922 
3923                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
3924                     if (num_attributes > 0)
3925                     {
3926                         uint32_t i;
3927 
3928                         for (i=0; i<num_attributes; ++i)
3929                         {
3930                             attr = attributes.AttributeAtIndex(i);
3931                             DWARFFormValue form_value;
3932                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
3933                             {
3934                                 switch (attr)
3935                                 {
3936                                 case DW_AT_decl_file:       decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3937                                 case DW_AT_decl_line:       decl.SetLine(form_value.Unsigned()); break;
3938                                 case DW_AT_decl_column:     decl.SetColumn(form_value.Unsigned()); break;
3939                                 case DW_AT_name:
3940                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
3941                                     type_name_const_str.SetCString(type_name_cstr);
3942                                     break;
3943                                 case DW_AT_type:            encoding_uid = form_value.Reference(dwarf_cu); break;
3944                                 case DW_AT_byte_size:       byte_size = form_value.Unsigned(); byte_size_valid = true; break;
3945                                 case DW_AT_accessibility:   accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
3946                                 case DW_AT_declaration:     is_forward_declaration = form_value.Unsigned() != 0; break;
3947                                 case DW_AT_allocated:
3948                                 case DW_AT_associated:
3949                                 case DW_AT_bit_stride:
3950                                 case DW_AT_byte_stride:
3951                                 case DW_AT_data_location:
3952                                 case DW_AT_description:
3953                                 case DW_AT_start_scope:
3954                                 case DW_AT_visibility:
3955                                 case DW_AT_specification:
3956                                 case DW_AT_abstract_origin:
3957                                 case DW_AT_sibling:
3958                                     break;
3959                                 }
3960                             }
3961                         }
3962 
3963                         DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
3964 
3965                         clang_type_t enumerator_clang_type = NULL;
3966                         clang_type = m_forward_decl_die_to_clang_type.lookup (die);
3967                         if (clang_type == NULL)
3968                         {
3969                             enumerator_clang_type = ast.GetBuiltinTypeForDWARFEncodingAndBitSize (NULL,
3970                                                                                                   DW_ATE_signed,
3971                                                                                                   byte_size * 8);
3972                             clang_type = ast.CreateEnumerationType (type_name_cstr,
3973                                                                     GetClangDeclContextContainingDIE (dwarf_cu, die),
3974                                                                     decl,
3975                                                                     enumerator_clang_type);
3976                         }
3977                         else
3978                         {
3979                             enumerator_clang_type = ClangASTContext::GetEnumerationIntegerType (clang_type);
3980                             assert (enumerator_clang_type != NULL);
3981                         }
3982 
3983                         LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
3984 
3985                         type_sp.reset( new Type (die->GetOffset(),
3986                                                  this,
3987                                                  type_name_const_str,
3988                                                  byte_size,
3989                                                  NULL,
3990                                                  encoding_uid,
3991                                                  Type::eEncodingIsUID,
3992                                                  &decl,
3993                                                  clang_type,
3994                                                  Type::eResolveStateForward));
3995 
3996 #if LEAVE_ENUMS_FORWARD_DECLARED
3997                         // Leave this as a forward declaration until we need
3998                         // to know the details of the type. lldb_private::Type
3999                         // will automatically call the SymbolFile virtual function
4000                         // "SymbolFileDWARF::ResolveClangOpaqueTypeDefinition(Type *)"
4001                         // When the definition needs to be defined.
4002                         m_forward_decl_die_to_clang_type[die] = clang_type;
4003                         m_forward_decl_clang_type_to_die[ClangASTType::RemoveFastQualifiers (clang_type)] = die;
4004                         ClangASTContext::SetHasExternalStorage (clang_type, true);
4005 #else
4006                         ast.StartTagDeclarationDefinition (clang_type);
4007                         if (die->HasChildren())
4008                         {
4009                             SymbolContext cu_sc(GetCompUnitForDWARFCompUnit(dwarf_cu));
4010                             ParseChildEnumerators(cu_sc, clang_type, type_sp->GetByteSize(), dwarf_cu, die);
4011                         }
4012                         ast.CompleteTagDeclarationDefinition (clang_type);
4013 #endif
4014                     }
4015                 }
4016                 break;
4017 
4018             case DW_TAG_inlined_subroutine:
4019             case DW_TAG_subprogram:
4020             case DW_TAG_subroutine_type:
4021                 {
4022                     // Set a bit that lets us know that we are currently parsing this
4023                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
4024 
4025                     const char *mangled = NULL;
4026                     dw_offset_t type_die_offset = DW_INVALID_OFFSET;
4027                     bool is_variadic = false;
4028                     bool is_inline = false;
4029                     bool is_static = false;
4030                     bool is_virtual = false;
4031                     bool is_explicit = false;
4032                     dw_offset_t specification_die_offset = DW_INVALID_OFFSET;
4033                     dw_offset_t abstract_origin_die_offset = DW_INVALID_OFFSET;
4034 
4035                     unsigned type_quals = 0;
4036                     clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
4037 
4038 
4039                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4040                     if (num_attributes > 0)
4041                     {
4042                         uint32_t i;
4043                         for (i=0; i<num_attributes; ++i)
4044                         {
4045                             attr = attributes.AttributeAtIndex(i);
4046                             DWARFFormValue form_value;
4047                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4048                             {
4049                                 switch (attr)
4050                                 {
4051                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4052                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
4053                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4054                                 case DW_AT_name:
4055                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
4056                                     type_name_const_str.SetCString(type_name_cstr);
4057                                     break;
4058 
4059                                 case DW_AT_MIPS_linkage_name:   mangled = form_value.AsCString(&get_debug_str_data()); break;
4060                                 case DW_AT_type:                type_die_offset = form_value.Reference(dwarf_cu); break;
4061                                 case DW_AT_accessibility:       accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4062                                 case DW_AT_declaration:         is_forward_declaration = form_value.Unsigned() != 0; break;
4063                                 case DW_AT_inline:              is_inline = form_value.Unsigned() != 0; break;
4064                                 case DW_AT_virtuality:          is_virtual = form_value.Unsigned() != 0;  break;
4065                                 case DW_AT_explicit:            is_explicit = form_value.Unsigned() != 0;  break;
4066 
4067                                 case DW_AT_external:
4068                                     if (form_value.Unsigned())
4069                                     {
4070                                         if (storage == clang::SC_None)
4071                                             storage = clang::SC_Extern;
4072                                         else
4073                                             storage = clang::SC_PrivateExtern;
4074                                     }
4075                                     break;
4076 
4077                                 case DW_AT_specification:
4078                                     specification_die_offset = form_value.Reference(dwarf_cu);
4079                                     break;
4080 
4081                                 case DW_AT_abstract_origin:
4082                                     abstract_origin_die_offset = form_value.Reference(dwarf_cu);
4083                                     break;
4084 
4085 
4086                                 case DW_AT_allocated:
4087                                 case DW_AT_associated:
4088                                 case DW_AT_address_class:
4089                                 case DW_AT_artificial:
4090                                 case DW_AT_calling_convention:
4091                                 case DW_AT_data_location:
4092                                 case DW_AT_elemental:
4093                                 case DW_AT_entry_pc:
4094                                 case DW_AT_frame_base:
4095                                 case DW_AT_high_pc:
4096                                 case DW_AT_low_pc:
4097                                 case DW_AT_object_pointer:
4098                                 case DW_AT_prototyped:
4099                                 case DW_AT_pure:
4100                                 case DW_AT_ranges:
4101                                 case DW_AT_recursive:
4102                                 case DW_AT_return_addr:
4103                                 case DW_AT_segment:
4104                                 case DW_AT_start_scope:
4105                                 case DW_AT_static_link:
4106                                 case DW_AT_trampoline:
4107                                 case DW_AT_visibility:
4108                                 case DW_AT_vtable_elem_location:
4109                                 case DW_AT_description:
4110                                 case DW_AT_sibling:
4111                                     break;
4112                                 }
4113                             }
4114                         }
4115                     }
4116 
4117                     DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
4118 
4119                     clang_type_t return_clang_type = NULL;
4120                     Type *func_type = NULL;
4121 
4122                     if (type_die_offset != DW_INVALID_OFFSET)
4123                         func_type = ResolveTypeUID(type_die_offset);
4124 
4125                     if (func_type)
4126                         return_clang_type = func_type->GetClangLayoutType();
4127                     else
4128                         return_clang_type = ast.GetBuiltInType_void();
4129 
4130 
4131                     std::vector<clang_type_t> function_param_types;
4132                     std::vector<clang::ParmVarDecl*> function_param_decls;
4133 
4134                     // Parse the function children for the parameters
4135 
4136                     clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (dwarf_cu, die);
4137                     const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
4138 
4139                     const bool is_cxx_method = containing_decl_kind == clang::Decl::CXXRecord;
4140                     // Start off static. This will be set to false in ParseChildParameters(...)
4141                     // if we find a "this" paramters as the first parameter
4142                     if (is_cxx_method)
4143                         is_static = true;
4144 
4145                     if (die->HasChildren())
4146                     {
4147                         bool skip_artificial = true;
4148                         ParseChildParameters (sc,
4149                                               containing_decl_ctx,
4150                                               type_sp,
4151                                               dwarf_cu,
4152                                               die,
4153                                               skip_artificial,
4154                                               is_static,
4155                                               type_list,
4156                                               function_param_types,
4157                                               function_param_decls,
4158                                               type_quals);
4159                     }
4160 
4161                     // clang_type will get the function prototype clang type after this call
4162                     clang_type = ast.CreateFunctionType (return_clang_type,
4163                                                          &function_param_types[0],
4164                                                          function_param_types.size(),
4165                                                          is_variadic,
4166                                                          type_quals);
4167 
4168                     if (type_name_cstr)
4169                     {
4170                         bool type_handled = false;
4171                         if (tag == DW_TAG_subprogram)
4172                         {
4173                             if (ObjCLanguageRuntime::IsPossibleObjCMethodName (type_name_cstr))
4174                             {
4175                                 // We need to find the DW_TAG_class_type or
4176                                 // DW_TAG_struct_type by name so we can add this
4177                                 // as a member function of the class.
4178                                 const char *class_name_start = type_name_cstr + 2;
4179                                 const char *class_name_end = ::strchr (class_name_start, ' ');
4180                                 SymbolContext empty_sc;
4181                                 clang_type_t class_opaque_type = NULL;
4182                                 if (class_name_start < class_name_end)
4183                                 {
4184                                     ConstString class_name (class_name_start, class_name_end - class_name_start);
4185                                     TypeList types;
4186                                     const uint32_t match_count = FindTypes (empty_sc, class_name, NULL, true, UINT32_MAX, types);
4187                                     if (match_count > 0)
4188                                     {
4189                                         for (uint32_t i=0; i<match_count; ++i)
4190                                         {
4191                                             Type *type = types.GetTypeAtIndex (i).get();
4192                                             clang_type_t type_clang_forward_type = type->GetClangForwardType();
4193                                             if (ClangASTContext::IsObjCClassType (type_clang_forward_type))
4194                                             {
4195                                                 class_opaque_type = type_clang_forward_type;
4196                                                 break;
4197                                             }
4198                                         }
4199                                     }
4200                                 }
4201 
4202                                 if (class_opaque_type)
4203                                 {
4204                                     // If accessibility isn't set to anything valid, assume public for
4205                                     // now...
4206                                     if (accessibility == eAccessNone)
4207                                         accessibility = eAccessPublic;
4208 
4209                                     clang::ObjCMethodDecl *objc_method_decl;
4210                                     objc_method_decl = ast.AddMethodToObjCObjectType (class_opaque_type,
4211                                                                                       type_name_cstr,
4212                                                                                       clang_type,
4213                                                                                       accessibility);
4214                                     LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
4215                                     type_handled = objc_method_decl != NULL;
4216                                 }
4217                             }
4218                             else if (is_cxx_method)
4219                             {
4220                                 // Look at the parent of this DIE and see if is is
4221                                 // a class or struct and see if this is actually a
4222                                 // C++ method
4223                                 Type *class_type = ResolveType (dwarf_cu, m_decl_ctx_to_die[containing_decl_ctx]);
4224                                 if (class_type)
4225                                 {
4226                                     if (specification_die_offset != DW_INVALID_OFFSET)
4227                                     {
4228                                         // We have a specification which we are going to base our function
4229                                         // prototype off of, so we need this type to be completed so that the
4230                                         // m_die_to_decl_ctx for the method in the specification has a valid
4231                                         // clang decl context.
4232                                         class_type->GetClangFullType();
4233                                         // If we have a specification, then the function type should have been
4234                                         // made with the specification and not with this die.
4235                                         DWARFCompileUnitSP spec_cu_sp;
4236                                         const DWARFDebugInfoEntry* spec_die = DebugInfo()->GetDIEPtr(specification_die_offset, &spec_cu_sp);
4237                                         clang::DeclContext *spec_clang_decl_ctx = GetCachedClangDeclContextForDIE (spec_die);
4238                                         if (spec_clang_decl_ctx)
4239                                         {
4240                                             LinkDeclContextToDIE(spec_clang_decl_ctx, die);
4241                                         }
4242                                         else
4243                                         {
4244                                             ReportWarning ("0x%8.8x: DW_AT_specification(0x%8.8x) has no decl\n",
4245                                                            die->GetOffset(),
4246                                                            specification_die_offset);
4247                                         }
4248                                         type_handled = true;
4249                                     }
4250                                     else if (abstract_origin_die_offset != DW_INVALID_OFFSET)
4251                                     {
4252                                         // We have a specification which we are going to base our function
4253                                         // prototype off of, so we need this type to be completed so that the
4254                                         // m_die_to_decl_ctx for the method in the abstract origin has a valid
4255                                         // clang decl context.
4256                                         class_type->GetClangFullType();
4257 
4258                                         DWARFCompileUnitSP abs_cu_sp;
4259                                         const DWARFDebugInfoEntry* abs_die = DebugInfo()->GetDIEPtr(abstract_origin_die_offset, &abs_cu_sp);
4260                                         clang::DeclContext *abs_clang_decl_ctx = GetCachedClangDeclContextForDIE (abs_die);
4261                                         if (abs_clang_decl_ctx)
4262                                         {
4263                                             LinkDeclContextToDIE (abs_clang_decl_ctx, die);
4264                                         }
4265                                         else
4266                                         {
4267                                             ReportWarning ("0x%8.8x: DW_AT_abstract_origin(0x%8.8x) has no decl\n",
4268                                                            die->GetOffset(),
4269                                                            abstract_origin_die_offset);
4270                                         }
4271                                         type_handled = true;
4272                                     }
4273                                     else
4274                                     {
4275                                         clang_type_t class_opaque_type = class_type->GetClangForwardType();
4276                                         if (ClangASTContext::IsCXXClassType (class_opaque_type))
4277                                         {
4278                                             // Neither GCC 4.2 nor clang++ currently set a valid accessibility
4279                                             // in the DWARF for C++ methods... Default to public for now...
4280                                             if (accessibility == eAccessNone)
4281                                                 accessibility = eAccessPublic;
4282 
4283                                             if (!is_static && !die->HasChildren())
4284                                             {
4285                                                 // We have a C++ member function with no children (this pointer!)
4286                                                 // and clang will get mad if we try and make a function that isn't
4287                                                 // well formed in the DWARF, so we will just skip it...
4288                                                 type_handled = true;
4289                                             }
4290                                             else
4291                                             {
4292                                                 clang::CXXMethodDecl *cxx_method_decl;
4293                                                 cxx_method_decl = ast.AddMethodToCXXRecordType (class_opaque_type,
4294                                                                                                 type_name_cstr,
4295                                                                                                 clang_type,
4296                                                                                                 accessibility,
4297                                                                                                 is_virtual,
4298                                                                                                 is_static,
4299                                                                                                 is_inline,
4300                                                                                                 is_explicit);
4301                                                 LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
4302 
4303                                                 type_handled = cxx_method_decl != NULL;
4304                                             }
4305                                         }
4306                                     }
4307                                 }
4308                             }
4309                         }
4310 
4311                         if (!type_handled)
4312                         {
4313                             // We just have a function that isn't part of a class
4314                             clang::FunctionDecl *function_decl = ast.CreateFunctionDeclaration (type_name_cstr,
4315                                                                                                 clang_type,
4316                                                                                                 storage,
4317                                                                                                 is_inline);
4318 
4319                             // Add the decl to our DIE to decl context map
4320                             assert (function_decl);
4321                             LinkDeclContextToDIE(function_decl, die);
4322                             if (!function_param_decls.empty())
4323                                 ast.SetFunctionParameters (function_decl,
4324                                                            &function_param_decls.front(),
4325                                                            function_param_decls.size());
4326                         }
4327                     }
4328                     type_sp.reset( new Type (die->GetOffset(),
4329                                              this,
4330                                              type_name_const_str,
4331                                              0,
4332                                              NULL,
4333                                              LLDB_INVALID_UID,
4334                                              Type::eEncodingIsUID,
4335                                              &decl,
4336                                              clang_type,
4337                                              Type::eResolveStateFull));
4338                     assert(type_sp.get());
4339                 }
4340                 break;
4341 
4342             case DW_TAG_array_type:
4343                 {
4344                     // Set a bit that lets us know that we are currently parsing this
4345                     m_die_to_type[die] = DIE_IS_BEING_PARSED;
4346 
4347                     lldb::user_id_t type_die_offset = DW_INVALID_OFFSET;
4348                     int64_t first_index = 0;
4349                     uint32_t byte_stride = 0;
4350                     uint32_t bit_stride = 0;
4351                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4352 
4353                     if (num_attributes > 0)
4354                     {
4355                         uint32_t i;
4356                         for (i=0; i<num_attributes; ++i)
4357                         {
4358                             attr = attributes.AttributeAtIndex(i);
4359                             DWARFFormValue form_value;
4360                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4361                             {
4362                                 switch (attr)
4363                                 {
4364                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4365                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
4366                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4367                                 case DW_AT_name:
4368                                     type_name_cstr = form_value.AsCString(&get_debug_str_data());
4369                                     type_name_const_str.SetCString(type_name_cstr);
4370                                     break;
4371 
4372                                 case DW_AT_type:            type_die_offset = form_value.Reference(dwarf_cu); break;
4373                                 case DW_AT_byte_size:       byte_size = form_value.Unsigned(); byte_size_valid = true; break;
4374                                 case DW_AT_byte_stride:     byte_stride = form_value.Unsigned(); break;
4375                                 case DW_AT_bit_stride:      bit_stride = form_value.Unsigned(); break;
4376                                 case DW_AT_accessibility:   accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4377                                 case DW_AT_declaration:     is_forward_declaration = form_value.Unsigned() != 0; break;
4378                                 case DW_AT_allocated:
4379                                 case DW_AT_associated:
4380                                 case DW_AT_data_location:
4381                                 case DW_AT_description:
4382                                 case DW_AT_ordering:
4383                                 case DW_AT_start_scope:
4384                                 case DW_AT_visibility:
4385                                 case DW_AT_specification:
4386                                 case DW_AT_abstract_origin:
4387                                 case DW_AT_sibling:
4388                                     break;
4389                                 }
4390                             }
4391                         }
4392 
4393                         DEBUG_PRINTF ("0x%8.8x: %s (\"%s\")\n", die->GetOffset(), DW_TAG_value_to_name(tag), type_name_cstr);
4394 
4395                         Type *element_type = ResolveTypeUID(type_die_offset);
4396 
4397                         if (element_type)
4398                         {
4399                             std::vector<uint64_t> element_orders;
4400                             ParseChildArrayInfo(sc, dwarf_cu, die, first_index, element_orders, byte_stride, bit_stride);
4401                             // We have an array that claims to have no members, lets give it at least one member...
4402                             if (element_orders.empty())
4403                                 element_orders.push_back (1);
4404                             if (byte_stride == 0 && bit_stride == 0)
4405                                 byte_stride = element_type->GetByteSize();
4406                             clang_type_t array_element_type = element_type->GetClangFullType();
4407                             uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
4408                             uint64_t num_elements = 0;
4409                             std::vector<uint64_t>::const_reverse_iterator pos;
4410                             std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
4411                             for (pos = element_orders.rbegin(); pos != end; ++pos)
4412                             {
4413                                 num_elements = *pos;
4414                                 clang_type = ast.CreateArrayType (array_element_type,
4415                                                                   num_elements,
4416                                                                   num_elements * array_element_bit_stride);
4417                                 array_element_type = clang_type;
4418                                 array_element_bit_stride = array_element_bit_stride * num_elements;
4419                             }
4420                             ConstString empty_name;
4421                             type_sp.reset( new Type (die->GetOffset(),
4422                                                      this,
4423                                                      empty_name,
4424                                                      array_element_bit_stride / 8,
4425                                                      NULL,
4426                                                      type_die_offset,
4427                                                      Type::eEncodingIsUID,
4428                                                      &decl,
4429                                                      clang_type,
4430                                                      Type::eResolveStateFull));
4431                             type_sp->SetEncodingType (element_type);
4432                         }
4433                     }
4434                 }
4435                 break;
4436 
4437             case DW_TAG_ptr_to_member_type:
4438                 {
4439                     dw_offset_t type_die_offset = DW_INVALID_OFFSET;
4440                     dw_offset_t containing_type_die_offset = DW_INVALID_OFFSET;
4441 
4442                     const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4443 
4444                     if (num_attributes > 0) {
4445                         uint32_t i;
4446                         for (i=0; i<num_attributes; ++i)
4447                         {
4448                             attr = attributes.AttributeAtIndex(i);
4449                             DWARFFormValue form_value;
4450                             if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4451                             {
4452                                 switch (attr)
4453                                 {
4454                                     case DW_AT_type:
4455                                         type_die_offset = form_value.Reference(dwarf_cu); break;
4456                                     case DW_AT_containing_type:
4457                                         containing_type_die_offset = form_value.Reference(dwarf_cu); break;
4458                                 }
4459                             }
4460                         }
4461 
4462                         Type *pointee_type = ResolveTypeUID(type_die_offset);
4463                         Type *class_type = ResolveTypeUID(containing_type_die_offset);
4464 
4465                         clang_type_t pointee_clang_type = pointee_type->GetClangForwardType();
4466                         clang_type_t class_clang_type = class_type->GetClangLayoutType();
4467 
4468                         clang_type = ast.CreateMemberPointerType(pointee_clang_type,
4469                                                                  class_clang_type);
4470 
4471                         byte_size = ClangASTType::GetClangTypeBitWidth (ast.getASTContext(),
4472                                                                        clang_type) / 8;
4473 
4474                         type_sp.reset( new Type (die->GetOffset(),
4475                                                  this,
4476                                                  type_name_const_str,
4477                                                  byte_size,
4478                                                  NULL,
4479                                                  LLDB_INVALID_UID,
4480                                                  Type::eEncodingIsUID,
4481                                                  NULL,
4482                                                  clang_type,
4483                                                  Type::eResolveStateForward));
4484                     }
4485 
4486                     break;
4487                 }
4488             default:
4489                 assert(false && "Unhandled type tag!");
4490                 break;
4491             }
4492 
4493             if (type_sp.get())
4494             {
4495                 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4496                 dw_tag_t sc_parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4497 
4498                 SymbolContextScope * symbol_context_scope = NULL;
4499                 if (sc_parent_tag == DW_TAG_compile_unit)
4500                 {
4501                     symbol_context_scope = sc.comp_unit;
4502                 }
4503                 else if (sc.function != NULL)
4504                 {
4505                     symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4506                     if (symbol_context_scope == NULL)
4507                         symbol_context_scope = sc.function;
4508                 }
4509 
4510                 if (symbol_context_scope != NULL)
4511                 {
4512                     type_sp->SetSymbolContextScope(symbol_context_scope);
4513                 }
4514 
4515                 // We are ready to put this type into the uniqued list up at the module level
4516                 type_list->Insert (type_sp);
4517 
4518                 m_die_to_type[die] = type_sp.get();
4519             }
4520         }
4521         else if (type_ptr != DIE_IS_BEING_PARSED)
4522         {
4523             type_sp = type_list->FindType(type_ptr->GetID());
4524         }
4525     }
4526     return type_sp;
4527 }
4528 
4529 size_t
4530 SymbolFileDWARF::ParseTypes
4531 (
4532     const SymbolContext& sc,
4533     DWARFCompileUnit* dwarf_cu,
4534     const DWARFDebugInfoEntry *die,
4535     bool parse_siblings,
4536     bool parse_children
4537 )
4538 {
4539     size_t types_added = 0;
4540     while (die != NULL)
4541     {
4542         bool type_is_new = false;
4543         if (ParseType(sc, dwarf_cu, die, &type_is_new).get())
4544         {
4545             if (type_is_new)
4546                 ++types_added;
4547         }
4548 
4549         if (parse_children && die->HasChildren())
4550         {
4551             if (die->Tag() == DW_TAG_subprogram)
4552             {
4553                 SymbolContext child_sc(sc);
4554                 child_sc.function = sc.comp_unit->FindFunctionByUID(die->GetOffset()).get();
4555                 types_added += ParseTypes(child_sc, dwarf_cu, die->GetFirstChild(), true, true);
4556             }
4557             else
4558                 types_added += ParseTypes(sc, dwarf_cu, die->GetFirstChild(), true, true);
4559         }
4560 
4561         if (parse_siblings)
4562             die = die->GetSibling();
4563         else
4564             die = NULL;
4565     }
4566     return types_added;
4567 }
4568 
4569 
4570 size_t
4571 SymbolFileDWARF::ParseFunctionBlocks (const SymbolContext &sc)
4572 {
4573     assert(sc.comp_unit && sc.function);
4574     size_t functions_added = 0;
4575     DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
4576     if (dwarf_cu)
4577     {
4578         dw_offset_t function_die_offset = sc.function->GetID();
4579         const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(function_die_offset);
4580         if (function_die)
4581         {
4582             ParseFunctionBlocks(sc, &sc.function->GetBlock (false), dwarf_cu, function_die, LLDB_INVALID_ADDRESS, 0);
4583         }
4584     }
4585 
4586     return functions_added;
4587 }
4588 
4589 
4590 size_t
4591 SymbolFileDWARF::ParseTypes (const SymbolContext &sc)
4592 {
4593     // At least a compile unit must be valid
4594     assert(sc.comp_unit);
4595     size_t types_added = 0;
4596     DWARFCompileUnit* dwarf_cu = GetDWARFCompileUnitForUID(sc.comp_unit->GetID());
4597     if (dwarf_cu)
4598     {
4599         if (sc.function)
4600         {
4601             dw_offset_t function_die_offset = sc.function->GetID();
4602             const DWARFDebugInfoEntry *func_die = dwarf_cu->GetDIEPtr(function_die_offset);
4603             if (func_die && func_die->HasChildren())
4604             {
4605                 types_added = ParseTypes(sc, dwarf_cu, func_die->GetFirstChild(), true, true);
4606             }
4607         }
4608         else
4609         {
4610             const DWARFDebugInfoEntry *dwarf_cu_die = dwarf_cu->DIE();
4611             if (dwarf_cu_die && dwarf_cu_die->HasChildren())
4612             {
4613                 types_added = ParseTypes(sc, dwarf_cu, dwarf_cu_die->GetFirstChild(), true, true);
4614             }
4615         }
4616     }
4617 
4618     return types_added;
4619 }
4620 
4621 size_t
4622 SymbolFileDWARF::ParseVariablesForContext (const SymbolContext& sc)
4623 {
4624     if (sc.comp_unit != NULL)
4625     {
4626         DWARFDebugInfo* info = DebugInfo();
4627         if (info == NULL)
4628             return 0;
4629 
4630         uint32_t cu_idx = UINT32_MAX;
4631         DWARFCompileUnit* dwarf_cu = info->GetCompileUnit(sc.comp_unit->GetID(), &cu_idx).get();
4632 
4633         if (dwarf_cu == NULL)
4634             return 0;
4635 
4636         if (sc.function)
4637         {
4638             const DWARFDebugInfoEntry *function_die = dwarf_cu->GetDIEPtr(sc.function->GetID());
4639 
4640             dw_addr_t func_lo_pc = function_die->GetAttributeValueAsUnsigned (this, dwarf_cu, DW_AT_low_pc, DW_INVALID_ADDRESS);
4641             assert (func_lo_pc != DW_INVALID_ADDRESS);
4642 
4643             const size_t num_variables = ParseVariables(sc, dwarf_cu, func_lo_pc, function_die->GetFirstChild(), true, true);
4644 
4645             // Let all blocks know they have parse all their variables
4646             sc.function->GetBlock (false).SetDidParseVariables (true, true);
4647 
4648             return num_variables;
4649         }
4650         else if (sc.comp_unit)
4651         {
4652             uint32_t vars_added = 0;
4653             VariableListSP variables (sc.comp_unit->GetVariableList(false));
4654 
4655             if (variables.get() == NULL)
4656             {
4657                 variables.reset(new VariableList());
4658                 sc.comp_unit->SetVariableList(variables);
4659 
4660                 DWARFCompileUnit* match_dwarf_cu = NULL;
4661                 const DWARFDebugInfoEntry* die = NULL;
4662                 DIEArray die_offsets;
4663                 if (m_apple_names_ap.get())
4664                 {
4665                     // TODO: implement finding all items in
4666                     m_apple_names_ap->AppendAllDIEsInRange (dwarf_cu->GetOffset(),
4667                                                             dwarf_cu->GetNextCompileUnitOffset(),
4668                                                             die_offsets);
4669                 }
4670                 else
4671                 {
4672                     // Index if we already haven't to make sure the compile units
4673                     // get indexed and make their global DIE index list
4674                     if (!m_indexed)
4675                         Index ();
4676 
4677                     m_global_index.FindAllEntriesForCompileUnit (dwarf_cu->GetOffset(),
4678                                                                  dwarf_cu->GetNextCompileUnitOffset(),
4679                                                                  die_offsets);
4680                 }
4681 
4682                 const size_t num_matches = die_offsets.size();
4683                 if (num_matches)
4684                 {
4685                     DWARFDebugInfo* debug_info = DebugInfo();
4686                     for (size_t i=0; i<num_matches; ++i)
4687                     {
4688                         const dw_offset_t die_offset = die_offsets[i];
4689                         die = debug_info->GetDIEPtrWithCompileUnitHint (die_offset, &match_dwarf_cu);
4690                         VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, LLDB_INVALID_ADDRESS));
4691                         if (var_sp)
4692                         {
4693                             variables->AddVariableIfUnique (var_sp);
4694                             ++vars_added;
4695                         }
4696                     }
4697                 }
4698             }
4699             return vars_added;
4700         }
4701     }
4702     return 0;
4703 }
4704 
4705 
4706 VariableSP
4707 SymbolFileDWARF::ParseVariableDIE
4708 (
4709     const SymbolContext& sc,
4710     DWARFCompileUnit* dwarf_cu,
4711     const DWARFDebugInfoEntry *die,
4712     const lldb::addr_t func_low_pc
4713 )
4714 {
4715 
4716     VariableSP var_sp (m_die_to_variable_sp[die]);
4717     if (var_sp)
4718         return var_sp;  // Already been parsed!
4719 
4720     const dw_tag_t tag = die->Tag();
4721 
4722     if ((tag == DW_TAG_variable) ||
4723         (tag == DW_TAG_constant) ||
4724         (tag == DW_TAG_formal_parameter && sc.function))
4725     {
4726         DWARFDebugInfoEntry::Attributes attributes;
4727         const size_t num_attributes = die->GetAttributes(this, dwarf_cu, NULL, attributes);
4728         if (num_attributes > 0)
4729         {
4730             const char *name = NULL;
4731             const char *mangled = NULL;
4732             Declaration decl;
4733             uint32_t i;
4734             Type *var_type = NULL;
4735             DWARFExpression location;
4736             bool is_external = false;
4737             bool is_artificial = false;
4738             bool location_is_const_value_data = false;
4739             AccessType accessibility = eAccessNone;
4740 
4741             for (i=0; i<num_attributes; ++i)
4742             {
4743                 dw_attr_t attr = attributes.AttributeAtIndex(i);
4744                 DWARFFormValue form_value;
4745                 if (attributes.ExtractFormValueAtIndex(this, i, form_value))
4746                 {
4747                     switch (attr)
4748                     {
4749                     case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
4750                     case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
4751                     case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
4752                     case DW_AT_name:        name = form_value.AsCString(&get_debug_str_data()); break;
4753                     case DW_AT_MIPS_linkage_name: mangled = form_value.AsCString(&get_debug_str_data()); break;
4754                     case DW_AT_type:        var_type = ResolveTypeUID(form_value.Reference(dwarf_cu)); break;
4755                     case DW_AT_external:    is_external = form_value.Unsigned() != 0; break;
4756                     case DW_AT_const_value:
4757                         location_is_const_value_data = true;
4758                         // Fall through...
4759                     case DW_AT_location:
4760                         {
4761                             if (form_value.BlockData())
4762                             {
4763                                 const DataExtractor& debug_info_data = get_debug_info_data();
4764 
4765                                 uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
4766                                 uint32_t block_length = form_value.Unsigned();
4767                                 location.SetOpcodeData(get_debug_info_data(), block_offset, block_length);
4768                             }
4769                             else
4770                             {
4771                                 const DataExtractor&    debug_loc_data = get_debug_loc_data();
4772                                 const dw_offset_t debug_loc_offset = form_value.Unsigned();
4773 
4774                                 size_t loc_list_length = DWARFLocationList::Size(debug_loc_data, debug_loc_offset);
4775                                 if (loc_list_length > 0)
4776                                 {
4777                                     location.SetOpcodeData(debug_loc_data, debug_loc_offset, loc_list_length);
4778                                     assert (func_low_pc != LLDB_INVALID_ADDRESS);
4779                                     location.SetLocationListSlide (func_low_pc - dwarf_cu->GetBaseAddress());
4780                                 }
4781                             }
4782                         }
4783                         break;
4784 
4785                     case DW_AT_artificial:      is_artificial = form_value.Unsigned() != 0; break;
4786                     case DW_AT_accessibility:   accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
4787                     case DW_AT_declaration:
4788                     case DW_AT_description:
4789                     case DW_AT_endianity:
4790                     case DW_AT_segment:
4791                     case DW_AT_start_scope:
4792                     case DW_AT_visibility:
4793                     default:
4794                     case DW_AT_abstract_origin:
4795                     case DW_AT_sibling:
4796                     case DW_AT_specification:
4797                         break;
4798                     }
4799                 }
4800             }
4801 
4802             if (location.IsValid())
4803             {
4804                 assert(var_type != DIE_IS_BEING_PARSED);
4805 
4806                 ValueType scope = eValueTypeInvalid;
4807 
4808                 const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(die);
4809                 dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4810 
4811                 if (tag == DW_TAG_formal_parameter)
4812                     scope = eValueTypeVariableArgument;
4813                 else if (is_external || parent_tag == DW_TAG_compile_unit)
4814                     scope = eValueTypeVariableGlobal;
4815                 else
4816                     scope = eValueTypeVariableLocal;
4817 
4818                 SymbolContextScope * symbol_context_scope = NULL;
4819                 switch (parent_tag)
4820                 {
4821                 case DW_TAG_subprogram:
4822                 case DW_TAG_inlined_subroutine:
4823                 case DW_TAG_lexical_block:
4824                     if (sc.function)
4825                     {
4826                         symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
4827                         if (symbol_context_scope == NULL)
4828                             symbol_context_scope = sc.function;
4829                     }
4830                     break;
4831 
4832                 default:
4833                     symbol_context_scope = sc.comp_unit;
4834                     break;
4835                 }
4836 
4837                 if (symbol_context_scope)
4838                 {
4839                     var_sp.reset (new Variable(die->GetOffset(),
4840                                                name,
4841                                                mangled,
4842                                                var_type,
4843                                                scope,
4844                                                symbol_context_scope,
4845                                                &decl,
4846                                                location,
4847                                                is_external,
4848                                                is_artificial));
4849 
4850                     var_sp->SetLocationIsConstantValueData (location_is_const_value_data);
4851                 }
4852                 else
4853                 {
4854                     // Not ready to parse this variable yet. It might be a global
4855                     // or static variable that is in a function scope and the function
4856                     // in the symbol context wasn't filled in yet
4857                     return var_sp;
4858                 }
4859             }
4860         }
4861         // Cache var_sp even if NULL (the variable was just a specification or
4862         // was missing vital information to be able to be displayed in the debugger
4863         // (missing location due to optimization, etc)) so we don't re-parse
4864         // this DIE over and over later...
4865         m_die_to_variable_sp[die] = var_sp;
4866     }
4867     return var_sp;
4868 }
4869 
4870 
4871 const DWARFDebugInfoEntry *
4872 SymbolFileDWARF::FindBlockContainingSpecification (dw_offset_t func_die_offset,
4873                                                    dw_offset_t spec_block_die_offset,
4874                                                    DWARFCompileUnit **result_die_cu_handle)
4875 {
4876     // Give the concrete function die specified by "func_die_offset", find the
4877     // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4878     // to "spec_block_die_offset"
4879     DWARFDebugInfo* info = DebugInfo();
4880 
4881     const DWARFDebugInfoEntry *die = info->GetDIEPtrWithCompileUnitHint(func_die_offset, result_die_cu_handle);
4882     if (die)
4883     {
4884         assert (*result_die_cu_handle);
4885         return FindBlockContainingSpecification (*result_die_cu_handle, die, spec_block_die_offset, result_die_cu_handle);
4886     }
4887     return NULL;
4888 }
4889 
4890 
4891 const DWARFDebugInfoEntry *
4892 SymbolFileDWARF::FindBlockContainingSpecification(DWARFCompileUnit* dwarf_cu,
4893                                                   const DWARFDebugInfoEntry *die,
4894                                                   dw_offset_t spec_block_die_offset,
4895                                                   DWARFCompileUnit **result_die_cu_handle)
4896 {
4897     if (die)
4898     {
4899         switch (die->Tag())
4900         {
4901         case DW_TAG_subprogram:
4902         case DW_TAG_inlined_subroutine:
4903         case DW_TAG_lexical_block:
4904             {
4905                 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_specification, DW_INVALID_OFFSET) == spec_block_die_offset)
4906                 {
4907                     *result_die_cu_handle = dwarf_cu;
4908                     return die;
4909                 }
4910 
4911                 if (die->GetAttributeValueAsReference (this, dwarf_cu, DW_AT_abstract_origin, DW_INVALID_OFFSET) == spec_block_die_offset)
4912                 {
4913                     *result_die_cu_handle = dwarf_cu;
4914                     return die;
4915                 }
4916             }
4917             break;
4918         }
4919 
4920         // Give the concrete function die specified by "func_die_offset", find the
4921         // concrete block whose DW_AT_specification or DW_AT_abstract_origin points
4922         // to "spec_block_die_offset"
4923         for (const DWARFDebugInfoEntry *child_die = die->GetFirstChild(); child_die != NULL; child_die = child_die->GetSibling())
4924         {
4925             const DWARFDebugInfoEntry *result_die = FindBlockContainingSpecification (dwarf_cu,
4926                                                                                       child_die,
4927                                                                                       spec_block_die_offset,
4928                                                                                       result_die_cu_handle);
4929             if (result_die)
4930                 return result_die;
4931         }
4932     }
4933 
4934     *result_die_cu_handle = NULL;
4935     return NULL;
4936 }
4937 
4938 size_t
4939 SymbolFileDWARF::ParseVariables
4940 (
4941     const SymbolContext& sc,
4942     DWARFCompileUnit* dwarf_cu,
4943     const lldb::addr_t func_low_pc,
4944     const DWARFDebugInfoEntry *orig_die,
4945     bool parse_siblings,
4946     bool parse_children,
4947     VariableList* cc_variable_list
4948 )
4949 {
4950     if (orig_die == NULL)
4951         return 0;
4952 
4953     VariableListSP variable_list_sp;
4954 
4955     size_t vars_added = 0;
4956     const DWARFDebugInfoEntry *die = orig_die;
4957     while (die != NULL)
4958     {
4959         dw_tag_t tag = die->Tag();
4960 
4961         // Check to see if we have already parsed this variable or constant?
4962         if (m_die_to_variable_sp[die])
4963         {
4964             if (cc_variable_list)
4965                 cc_variable_list->AddVariableIfUnique (m_die_to_variable_sp[die]);
4966         }
4967         else
4968         {
4969             // We haven't already parsed it, lets do that now.
4970             if ((tag == DW_TAG_variable) ||
4971                 (tag == DW_TAG_constant) ||
4972                 (tag == DW_TAG_formal_parameter && sc.function))
4973             {
4974                 if (variable_list_sp.get() == NULL)
4975                 {
4976                     const DWARFDebugInfoEntry *sc_parent_die = GetParentSymbolContextDIE(orig_die);
4977                     dw_tag_t parent_tag = sc_parent_die ? sc_parent_die->Tag() : 0;
4978                     switch (parent_tag)
4979                     {
4980                         case DW_TAG_compile_unit:
4981                             if (sc.comp_unit != NULL)
4982                             {
4983                                 variable_list_sp = sc.comp_unit->GetVariableList(false);
4984                                 if (variable_list_sp.get() == NULL)
4985                                 {
4986                                     variable_list_sp.reset(new VariableList());
4987                                     sc.comp_unit->SetVariableList(variable_list_sp);
4988                                 }
4989                             }
4990                             else
4991                             {
4992                                 ReportError ("parent 0x%8.8x %s with no valid compile unit in symbol context for 0x%8.8x %s.\n",
4993                                          sc_parent_die->GetOffset(),
4994                                          DW_TAG_value_to_name (parent_tag),
4995                                          orig_die->GetOffset(),
4996                                          DW_TAG_value_to_name (orig_die->Tag()));
4997                             }
4998                             break;
4999 
5000                         case DW_TAG_subprogram:
5001                         case DW_TAG_inlined_subroutine:
5002                         case DW_TAG_lexical_block:
5003                             if (sc.function != NULL)
5004                             {
5005                                 // Check to see if we already have parsed the variables for the given scope
5006 
5007                                 Block *block = sc.function->GetBlock(true).FindBlockByID(sc_parent_die->GetOffset());
5008                                 if (block == NULL)
5009                                 {
5010                                     // This must be a specification or abstract origin with
5011                                     // a concrete block couterpart in the current function. We need
5012                                     // to find the concrete block so we can correctly add the
5013                                     // variable to it
5014                                     DWARFCompileUnit *concrete_block_die_cu = dwarf_cu;
5015                                     const DWARFDebugInfoEntry *concrete_block_die = FindBlockContainingSpecification (sc.function->GetID(),
5016                                                                                                                       sc_parent_die->GetOffset(),
5017                                                                                                                       &concrete_block_die_cu);
5018                                     if (concrete_block_die)
5019                                         block = sc.function->GetBlock(true).FindBlockByID(concrete_block_die->GetOffset());
5020                                 }
5021 
5022                                 if (block != NULL)
5023                                 {
5024                                     const bool can_create = false;
5025                                     variable_list_sp = block->GetBlockVariableList (can_create);
5026                                     if (variable_list_sp.get() == NULL)
5027                                     {
5028                                         variable_list_sp.reset(new VariableList());
5029                                         block->SetVariableList(variable_list_sp);
5030                                     }
5031                                 }
5032                             }
5033                             break;
5034 
5035                         default:
5036                              ReportError ("didn't find appropriate parent DIE for variable list for 0x%8.8x %s.\n",
5037                                      orig_die->GetOffset(),
5038                                      DW_TAG_value_to_name (orig_die->Tag()));
5039                             break;
5040                     }
5041                 }
5042 
5043                 if (variable_list_sp)
5044                 {
5045                     VariableSP var_sp (ParseVariableDIE(sc, dwarf_cu, die, func_low_pc));
5046                     if (var_sp)
5047                     {
5048                         variable_list_sp->AddVariableIfUnique (var_sp);
5049                         if (cc_variable_list)
5050                             cc_variable_list->AddVariableIfUnique (var_sp);
5051                         ++vars_added;
5052                     }
5053                 }
5054             }
5055         }
5056 
5057         bool skip_children = (sc.function == NULL && tag == DW_TAG_subprogram);
5058 
5059         if (!skip_children && parse_children && die->HasChildren())
5060         {
5061             vars_added += ParseVariables(sc, dwarf_cu, func_low_pc, die->GetFirstChild(), true, true, cc_variable_list);
5062         }
5063 
5064         if (parse_siblings)
5065             die = die->GetSibling();
5066         else
5067             die = NULL;
5068     }
5069     return vars_added;
5070 }
5071 
5072 //------------------------------------------------------------------
5073 // PluginInterface protocol
5074 //------------------------------------------------------------------
5075 const char *
5076 SymbolFileDWARF::GetPluginName()
5077 {
5078     return "SymbolFileDWARF";
5079 }
5080 
5081 const char *
5082 SymbolFileDWARF::GetShortPluginName()
5083 {
5084     return GetPluginNameStatic();
5085 }
5086 
5087 uint32_t
5088 SymbolFileDWARF::GetPluginVersion()
5089 {
5090     return 1;
5091 }
5092 
5093 void
5094 SymbolFileDWARF::CompleteTagDecl (void *baton, clang::TagDecl *decl)
5095 {
5096     SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5097     clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
5098     if (clang_type)
5099         symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
5100 }
5101 
5102 void
5103 SymbolFileDWARF::CompleteObjCInterfaceDecl (void *baton, clang::ObjCInterfaceDecl *decl)
5104 {
5105     SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5106     clang_type_t clang_type = symbol_file_dwarf->GetClangASTContext().GetTypeForDecl (decl);
5107     if (clang_type)
5108         symbol_file_dwarf->ResolveClangOpaqueTypeDefinition (clang_type);
5109 }
5110 
5111 void
5112 SymbolFileDWARF::DumpIndexes ()
5113 {
5114     StreamFile s(stdout, false);
5115 
5116     s.Printf ("DWARF index for (%s) '%s/%s':",
5117               GetObjectFile()->GetModule()->GetArchitecture().GetArchitectureName(),
5118               GetObjectFile()->GetFileSpec().GetDirectory().AsCString(),
5119               GetObjectFile()->GetFileSpec().GetFilename().AsCString());
5120     s.Printf("\nFunction basenames:\n");    m_function_basename_index.Dump (&s);
5121     s.Printf("\nFunction fullnames:\n");    m_function_fullname_index.Dump (&s);
5122     s.Printf("\nFunction methods:\n");      m_function_method_index.Dump (&s);
5123     s.Printf("\nFunction selectors:\n");    m_function_selector_index.Dump (&s);
5124     s.Printf("\nObjective C class selectors:\n");    m_objc_class_selectors_index.Dump (&s);
5125     s.Printf("\nGlobals and statics:\n");   m_global_index.Dump (&s);
5126     s.Printf("\nTypes:\n");                 m_type_index.Dump (&s);
5127     s.Printf("\nNamepaces:\n");             m_namespace_index.Dump (&s);
5128 }
5129 
5130 void
5131 SymbolFileDWARF::SearchDeclContext (const clang::DeclContext *decl_context,
5132                                     const char *name,
5133                                     llvm::SmallVectorImpl <clang::NamedDecl *> *results)
5134 {
5135     DeclContextToDIEMap::iterator iter = m_decl_ctx_to_die.find(decl_context);
5136 
5137     if (iter == m_decl_ctx_to_die.end())
5138         return;
5139 
5140     const DWARFDebugInfoEntry *context_die = iter->second;
5141 
5142     if (!results)
5143         return;
5144 
5145     DWARFDebugInfo* info = DebugInfo();
5146 
5147     DIEArray die_offsets;
5148 
5149     DWARFCompileUnit* dwarf_cu = NULL;
5150     const DWARFDebugInfoEntry* die = NULL;
5151     size_t num_matches = m_type_index.Find (ConstString(name), die_offsets);
5152 
5153     if (num_matches)
5154     {
5155         for (size_t i = 0; i < num_matches; ++i)
5156         {
5157             const dw_offset_t die_offset = die_offsets[i];
5158             die = info->GetDIEPtrWithCompileUnitHint (die_offset, &dwarf_cu);
5159 
5160             if (die->GetParent() != context_die)
5161                 continue;
5162 
5163             Type *matching_type = ResolveType (dwarf_cu, die);
5164 
5165             lldb::clang_type_t type = matching_type->GetClangFullType();
5166             clang::QualType qual_type = clang::QualType::getFromOpaquePtr(type);
5167 
5168             if (const clang::TagType *tag_type = llvm::dyn_cast<clang::TagType>(qual_type.getTypePtr()))
5169             {
5170                 clang::TagDecl *tag_decl = tag_type->getDecl();
5171                 results->push_back(tag_decl);
5172             }
5173             else if (const clang::TypedefType *typedef_type = llvm::dyn_cast<clang::TypedefType>(qual_type.getTypePtr()))
5174             {
5175                 clang::TypedefNameDecl *typedef_decl = typedef_type->getDecl();
5176                 results->push_back(typedef_decl);
5177             }
5178         }
5179     }
5180 }
5181 
5182 void
5183 SymbolFileDWARF::FindExternalVisibleDeclsByName (void *baton,
5184                                                  const clang::DeclContext *DC,
5185                                                  clang::DeclarationName Name,
5186                                                  llvm::SmallVectorImpl <clang::NamedDecl *> *results)
5187 {
5188     SymbolFileDWARF *symbol_file_dwarf = (SymbolFileDWARF *)baton;
5189 
5190     symbol_file_dwarf->SearchDeclContext (DC, Name.getAsString().c_str(), results);
5191 }
5192