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