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