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