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