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