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