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