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