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