1 //===-- DWARFASTParserClang.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 "DWARFASTParserClang.h"
11 #include "DWARFCompileUnit.h"
12 #include "DWARFDebugInfo.h"
13 #include "DWARFDeclContext.h"
14 #include "DWARFDefines.h"
15 #include "DWARFDIE.h"
16 #include "DWARFDIECollection.h"
17 #include "SymbolFileDWARF.h"
18 #include "SymbolFileDWARFDebugMap.h"
19 #include "UniqueDWARFASTType.h"
20 
21 #include "Plugins/Language/ObjC/ObjCLanguage.h"
22 #include "lldb/Core/Log.h"
23 #include "lldb/Core/Module.h"
24 #include "lldb/Core/StreamString.h"
25 #include "lldb/Core/Value.h"
26 #include "lldb/Host/Host.h"
27 #include "lldb/Interpreter/Args.h"
28 #include "lldb/Symbol/ClangASTImporter.h"
29 #include "lldb/Symbol/ClangExternalASTSourceCommon.h"
30 #include "lldb/Symbol/ClangUtil.h"
31 #include "lldb/Symbol/CompileUnit.h"
32 #include "lldb/Symbol/Function.h"
33 #include "lldb/Symbol/ObjectFile.h"
34 #include "lldb/Symbol/SymbolVendor.h"
35 #include "lldb/Symbol/TypeList.h"
36 #include "lldb/Symbol/TypeMap.h"
37 #include "lldb/Target/Language.h"
38 #include "lldb/Utility/LLDBAssert.h"
39 
40 #include "clang/AST/DeclCXX.h"
41 #include "clang/AST/DeclObjC.h"
42 
43 #include <map>
44 #include <vector>
45 
46 //#define ENABLE_DEBUG_PRINTF // COMMENT OUT THIS LINE PRIOR TO CHECKIN
47 
48 #ifdef ENABLE_DEBUG_PRINTF
49 #include <stdio.h>
50 #define DEBUG_PRINTF(fmt, ...) printf(fmt, __VA_ARGS__)
51 #else
52 #define DEBUG_PRINTF(fmt, ...)
53 #endif
54 
55 
56 using namespace lldb;
57 using namespace lldb_private;
58 DWARFASTParserClang::DWARFASTParserClang (ClangASTContext &ast) :
59     m_ast (ast),
60     m_die_to_decl_ctx (),
61     m_decl_ctx_to_die ()
62 {
63 }
64 
65 DWARFASTParserClang::~DWARFASTParserClang ()
66 {
67 }
68 
69 
70 static AccessType
71 DW_ACCESS_to_AccessType (uint32_t dwarf_accessibility)
72 {
73     switch (dwarf_accessibility)
74     {
75         case DW_ACCESS_public:      return eAccessPublic;
76         case DW_ACCESS_private:     return eAccessPrivate;
77         case DW_ACCESS_protected:   return eAccessProtected;
78         default:                    break;
79     }
80     return eAccessNone;
81 }
82 
83 static bool
84 DeclKindIsCXXClass (clang::Decl::Kind decl_kind)
85 {
86     switch (decl_kind)
87     {
88         case clang::Decl::CXXRecord:
89         case clang::Decl::ClassTemplateSpecialization:
90             return true;
91         default:
92             break;
93     }
94     return false;
95 }
96 
97 struct BitfieldInfo
98 {
99     uint64_t bit_size;
100     uint64_t bit_offset;
101 
102     BitfieldInfo () :
103     bit_size (LLDB_INVALID_ADDRESS),
104     bit_offset (LLDB_INVALID_ADDRESS)
105     {
106     }
107 
108     void
109     Clear()
110     {
111         bit_size = LLDB_INVALID_ADDRESS;
112         bit_offset = LLDB_INVALID_ADDRESS;
113     }
114 
115     bool IsValid ()
116     {
117         return (bit_size != LLDB_INVALID_ADDRESS) &&
118         (bit_offset != LLDB_INVALID_ADDRESS);
119     }
120 };
121 
122 
123 ClangASTImporter &
124 DWARFASTParserClang::GetClangASTImporter()
125 {
126     if (!m_clang_ast_importer_ap)
127     {
128         m_clang_ast_importer_ap.reset (new ClangASTImporter);
129     }
130     return *m_clang_ast_importer_ap;
131 }
132 
133 
134 TypeSP
135 DWARFASTParserClang::ParseTypeFromDWO (const DWARFDIE &die, Log *log)
136 {
137     ModuleSP dwo_module_sp = die.GetContainingDWOModule();
138     if (dwo_module_sp)
139     {
140         // This type comes from an external DWO module
141         std::vector<CompilerContext> dwo_context;
142         die.GetDWOContext(dwo_context);
143         TypeMap dwo_types;
144         if (dwo_module_sp->GetSymbolVendor()->FindTypes(dwo_context, true, dwo_types))
145         {
146             const size_t num_dwo_types = dwo_types.GetSize();
147             if (num_dwo_types == 1)
148             {
149                 // We found a real definition for this type elsewhere
150                 // so lets use it and cache the fact that we found
151                 // a complete type for this die
152                 TypeSP dwo_type_sp = dwo_types.GetTypeAtIndex(0);
153                 if (dwo_type_sp)
154                 {
155                     lldb_private::CompilerType dwo_type = dwo_type_sp->GetForwardCompilerType();
156 
157                     lldb_private::CompilerType type = GetClangASTImporter().CopyType (m_ast, dwo_type);
158 
159                     //printf ("copied_qual_type: ast = %p, clang_type = %p, name = '%s'\n", m_ast, copied_qual_type.getAsOpaquePtr(), external_type->GetName().GetCString());
160                     if (type)
161                     {
162                         SymbolFileDWARF *dwarf = die.GetDWARF();
163                         TypeSP type_sp (new Type (die.GetID(),
164                                                   dwarf,
165                                                   dwo_type_sp->GetName(),
166                                                   dwo_type_sp->GetByteSize(),
167                                                   NULL,
168                                                   LLDB_INVALID_UID,
169                                                   Type::eEncodingInvalid,
170                                                   &dwo_type_sp->GetDeclaration(),
171                                                   type,
172                                                   Type::eResolveStateForward));
173 
174                         dwarf->GetTypeList()->Insert(type_sp);
175                         dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
176                         clang::TagDecl *tag_decl = ClangASTContext::GetAsTagDecl(type);
177                         if (tag_decl)
178                             LinkDeclContextToDIE(tag_decl, die);
179                         else
180                         {
181                             clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(die);
182                             if (defn_decl_ctx)
183                                 LinkDeclContextToDIE(defn_decl_ctx, die);
184                         }
185                         return type_sp;
186                     }
187                 }
188             }
189         }
190     }
191     return TypeSP();
192 }
193 
194 TypeSP
195 DWARFASTParserClang::ParseTypeFromDWARF (const SymbolContext& sc,
196                                          const DWARFDIE &die,
197                                          Log *log,
198                                          bool *type_is_new_ptr)
199 {
200     TypeSP type_sp;
201 
202     if (type_is_new_ptr)
203         *type_is_new_ptr = false;
204 
205     AccessType accessibility = eAccessNone;
206     if (die)
207     {
208         SymbolFileDWARF *dwarf = die.GetDWARF();
209         if (log)
210         {
211             DWARFDIE context_die;
212             clang::DeclContext *context = GetClangDeclContextContainingDIE (die, &context_die);
213 
214             dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDWARF::ParseType (die = 0x%8.8x, decl_ctx = %p (die 0x%8.8x)) %s name = '%s')",
215                                                              die.GetOffset(),
216                                                              static_cast<void*>(context),
217                                                              context_die.GetOffset(),
218                                                              die.GetTagAsCString(),
219                                                              die.GetName());
220 
221         }
222         //
223         //        Log *log (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
224         //        if (log && dwarf_cu)
225         //        {
226         //            StreamString s;
227         //            die->DumpLocation (this, dwarf_cu, s);
228         //            dwarf->GetObjectFile()->GetModule()->LogMessage (log, "SymbolFileDwarf::%s %s", __FUNCTION__, s.GetData());
229         //
230         //        }
231 
232         Type *type_ptr = dwarf->GetDIEToType().lookup (die.GetDIE());
233         TypeList* type_list = dwarf->GetTypeList();
234         if (type_ptr == NULL)
235         {
236             if (type_is_new_ptr)
237                 *type_is_new_ptr = true;
238 
239             const dw_tag_t tag = die.Tag();
240 
241             bool is_forward_declaration = false;
242             DWARFAttributes attributes;
243             const char *type_name_cstr = NULL;
244             ConstString type_name_const_str;
245             Type::ResolveState resolve_state = Type::eResolveStateUnresolved;
246             uint64_t byte_size = 0;
247             Declaration decl;
248 
249             Type::EncodingDataType encoding_data_type = Type::eEncodingIsUID;
250             CompilerType clang_type;
251             DWARFFormValue form_value;
252 
253             dw_attr_t attr;
254 
255             switch (tag)
256             {
257                 case DW_TAG_typedef:
258                     // Try to parse a typedef from the DWO file first as modules
259                     // can contain typedef'ed structures that have no names like:
260                     //
261                     //  typedef struct { int a; } Foo;
262                     //
263                     // In this case we will have a structure with no name and a
264                     // typedef named "Foo" that points to this unnamed structure.
265                     // The name in the typedef is the only identifier for the struct,
266                     // so always try to get typedefs from DWO files if possible.
267                     //
268                     // The type_sp returned will be empty if the typedef doesn't exist
269                     // in a DWO file, so it is cheap to call this function just to check.
270                     //
271                     // If we don't do this we end up creating a TypeSP that says this
272                     // is a typedef to type 0x123 (the DW_AT_type value would be 0x123
273                     // in the DW_TAG_typedef), and this is the unnamed structure type.
274                     // We will have a hard time tracking down an unnammed structure
275                     // type in the module DWO file, so we make sure we don't get into
276                     // this situation by always resolving typedefs from the DWO file.
277                     type_sp = ParseTypeFromDWO(die, log);
278                     if (type_sp)
279                         return type_sp;
280 
281                 LLVM_FALLTHROUGH;
282                 case DW_TAG_base_type:
283                 case DW_TAG_pointer_type:
284                 case DW_TAG_reference_type:
285                 case DW_TAG_rvalue_reference_type:
286                 case DW_TAG_const_type:
287                 case DW_TAG_restrict_type:
288                 case DW_TAG_volatile_type:
289                 case DW_TAG_unspecified_type:
290                 {
291                     // Set a bit that lets us know that we are currently parsing this
292                     dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
293 
294                     const size_t num_attributes = die.GetAttributes (attributes);
295                     uint32_t encoding = 0;
296                     DWARFFormValue encoding_uid;
297 
298                     if (num_attributes > 0)
299                     {
300                         uint32_t i;
301                         for (i=0; i<num_attributes; ++i)
302                         {
303                             attr = attributes.AttributeAtIndex(i);
304                             if (attributes.ExtractFormValueAtIndex(i, form_value))
305                             {
306                                 switch (attr)
307                                 {
308                                     case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
309                                     case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
310                                     case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
311                                     case DW_AT_name:
312 
313                                         type_name_cstr = form_value.AsCString();
314                                         // Work around a bug in llvm-gcc where they give a name to a reference type which doesn't
315                                         // include the "&"...
316                                         if (tag == DW_TAG_reference_type)
317                                         {
318                                             if (strchr (type_name_cstr, '&') == NULL)
319                                                 type_name_cstr = NULL;
320                                         }
321                                         if (type_name_cstr)
322                                             type_name_const_str.SetCString(type_name_cstr);
323                                         break;
324                                     case DW_AT_byte_size:   byte_size = form_value.Unsigned(); break;
325                                     case DW_AT_encoding:    encoding = form_value.Unsigned(); break;
326                                     case DW_AT_type:        encoding_uid = form_value; break;
327                                     default:
328                                     case DW_AT_sibling:
329                                         break;
330                                 }
331                             }
332                         }
333                     }
334 
335                     DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\") type => 0x%8.8lx\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr, encoding_uid.Reference());
336 
337                     switch (tag)
338                     {
339                         default:
340                             break;
341 
342                         case DW_TAG_unspecified_type:
343                             if (strcmp(type_name_cstr, "nullptr_t") == 0 ||
344                                 strcmp(type_name_cstr, "decltype(nullptr)") == 0 )
345                             {
346                                 resolve_state = Type::eResolveStateFull;
347                                 clang_type = m_ast.GetBasicType(eBasicTypeNullPtr);
348                                 break;
349                             }
350                             // Fall through to base type below in case we can handle the type there...
351                             LLVM_FALLTHROUGH;
352 
353                         case DW_TAG_base_type:
354                             resolve_state = Type::eResolveStateFull;
355                             clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize (type_name_cstr,
356                                                                                          encoding,
357                                                                                          byte_size * 8);
358                             break;
359 
360                         case DW_TAG_pointer_type:           encoding_data_type = Type::eEncodingIsPointerUID;           break;
361                         case DW_TAG_reference_type:         encoding_data_type = Type::eEncodingIsLValueReferenceUID;   break;
362                         case DW_TAG_rvalue_reference_type:  encoding_data_type = Type::eEncodingIsRValueReferenceUID;   break;
363                         case DW_TAG_typedef:                encoding_data_type = Type::eEncodingIsTypedefUID;           break;
364                         case DW_TAG_const_type:             encoding_data_type = Type::eEncodingIsConstUID;             break;
365                         case DW_TAG_restrict_type:          encoding_data_type = Type::eEncodingIsRestrictUID;          break;
366                         case DW_TAG_volatile_type:          encoding_data_type = Type::eEncodingIsVolatileUID;          break;
367                     }
368 
369                     if (!clang_type && (encoding_data_type == Type::eEncodingIsPointerUID || encoding_data_type == Type::eEncodingIsTypedefUID) && sc.comp_unit != NULL)
370                     {
371                         bool translation_unit_is_objc = (sc.comp_unit->GetLanguage() == eLanguageTypeObjC || sc.comp_unit->GetLanguage() == eLanguageTypeObjC_plus_plus);
372 
373                         if (translation_unit_is_objc)
374                         {
375                             if (type_name_cstr != NULL)
376                             {
377                                 static ConstString g_objc_type_name_id("id");
378                                 static ConstString g_objc_type_name_Class("Class");
379                                 static ConstString g_objc_type_name_selector("SEL");
380 
381                                 if (type_name_const_str == g_objc_type_name_id)
382                                 {
383                                     if (log)
384                                         dwarf->GetObjectFile()->GetModule()->LogMessage (log,
385                                                                                          "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'id' built-in type.",
386                                                                                          die.GetOffset(),
387                                                                                          die.GetTagAsCString(),
388                                                                                          die.GetName());
389                                     clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
390                                     encoding_data_type = Type::eEncodingIsUID;
391                                     encoding_uid.Clear();
392                                     resolve_state = Type::eResolveStateFull;
393 
394                                 }
395                                 else if (type_name_const_str == g_objc_type_name_Class)
396                                 {
397                                     if (log)
398                                         dwarf->GetObjectFile()->GetModule()->LogMessage (log,
399                                                                                          "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'Class' built-in type.",
400                                                                                          die.GetOffset(),
401                                                                                          die.GetTagAsCString(),
402                                                                                          die.GetName());
403                                     clang_type = m_ast.GetBasicType(eBasicTypeObjCClass);
404                                     encoding_data_type = Type::eEncodingIsUID;
405                                     encoding_uid.Clear();
406                                     resolve_state = Type::eResolveStateFull;
407                                 }
408                                 else if (type_name_const_str == g_objc_type_name_selector)
409                                 {
410                                     if (log)
411                                         dwarf->GetObjectFile()->GetModule()->LogMessage (log,
412                                                                                          "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is Objective C 'selector' built-in type.",
413                                                                                          die.GetOffset(),
414                                                                                          die.GetTagAsCString(),
415                                                                                          die.GetName());
416                                     clang_type = m_ast.GetBasicType(eBasicTypeObjCSel);
417                                     encoding_data_type = Type::eEncodingIsUID;
418                                     encoding_uid.Clear();
419                                     resolve_state = Type::eResolveStateFull;
420                                 }
421                             }
422                             else if (encoding_data_type == Type::eEncodingIsPointerUID && encoding_uid.IsValid())
423                             {
424                                 // Clang sometimes erroneously emits id as objc_object*.  In that case we fix up the type to "id".
425 
426                                 const DWARFDIE encoding_die = dwarf->GetDIE(DIERef(encoding_uid));
427 
428                                 if (encoding_die && encoding_die.Tag() == DW_TAG_structure_type)
429                                 {
430                                     if (const char *struct_name = encoding_die.GetName())
431                                     {
432                                         if (!strcmp(struct_name, "objc_object"))
433                                         {
434                                             if (log)
435                                                 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
436                                                                                                  "SymbolFileDWARF::ParseType (die = 0x%8.8x) %s '%s' is 'objc_object*', which we overrode to 'id'.",
437                                                                                                  die.GetOffset(),
438                                                                                                  die.GetTagAsCString(),
439                                                                                                  die.GetName());
440                                             clang_type = m_ast.GetBasicType(eBasicTypeObjCID);
441                                             encoding_data_type = Type::eEncodingIsUID;
442                                             encoding_uid.Clear();
443                                             resolve_state = Type::eResolveStateFull;
444                                         }
445                                     }
446                                 }
447                             }
448                         }
449                     }
450 
451                     type_sp.reset( new Type (die.GetID(),
452                                              dwarf,
453                                              type_name_const_str,
454                                              byte_size,
455                                              NULL,
456                                              DIERef(encoding_uid).GetUID(dwarf),
457                                              encoding_data_type,
458                                              &decl,
459                                              clang_type,
460                                              resolve_state));
461 
462                     dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
463 
464                     //                  Type* encoding_type = GetUniquedTypeForDIEOffset(encoding_uid, type_sp, NULL, 0, 0, false);
465                     //                  if (encoding_type != NULL)
466                     //                  {
467                     //                      if (encoding_type != DIE_IS_BEING_PARSED)
468                     //                          type_sp->SetEncodingType(encoding_type);
469                     //                      else
470                     //                          m_indirect_fixups.push_back(type_sp.get());
471                     //                  }
472                 }
473                     break;
474 
475                 case DW_TAG_structure_type:
476                 case DW_TAG_union_type:
477                 case DW_TAG_class_type:
478                 {
479                     // Set a bit that lets us know that we are currently parsing this
480                     dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
481                     bool byte_size_valid = false;
482 
483                     LanguageType class_language = eLanguageTypeUnknown;
484                     bool is_complete_objc_class = false;
485                     //bool struct_is_class = false;
486                     const size_t num_attributes = die.GetAttributes (attributes);
487                     if (num_attributes > 0)
488                     {
489                         uint32_t i;
490                         for (i=0; i<num_attributes; ++i)
491                         {
492                             attr = attributes.AttributeAtIndex(i);
493                             if (attributes.ExtractFormValueAtIndex(i, form_value))
494                             {
495                                 switch (attr)
496                                 {
497                                     case DW_AT_decl_file:
498                                         if (die.GetCU()->DW_AT_decl_file_attributes_are_invalid())
499                                         {
500                                             // llvm-gcc outputs invalid DW_AT_decl_file attributes that always
501                                             // point to the compile unit file, so we clear this invalid value
502                                             // so that we can still unique types efficiently.
503                                             decl.SetFile(FileSpec ("<invalid>", false));
504                                         }
505                                         else
506                                             decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned()));
507                                         break;
508 
509                                     case DW_AT_decl_line:
510                                         decl.SetLine(form_value.Unsigned());
511                                         break;
512 
513                                     case DW_AT_decl_column:
514                                         decl.SetColumn(form_value.Unsigned());
515                                         break;
516 
517                                     case DW_AT_name:
518                                         type_name_cstr = form_value.AsCString();
519                                         type_name_const_str.SetCString(type_name_cstr);
520                                         break;
521 
522                                     case DW_AT_byte_size:
523                                         byte_size = form_value.Unsigned();
524                                         byte_size_valid = true;
525                                         break;
526 
527                                     case DW_AT_accessibility:
528                                         accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
529                                         break;
530 
531                                     case DW_AT_declaration:
532                                         is_forward_declaration = form_value.Boolean();
533                                         break;
534 
535                                     case DW_AT_APPLE_runtime_class:
536                                         class_language = (LanguageType)form_value.Signed();
537                                         break;
538 
539                                     case DW_AT_APPLE_objc_complete_type:
540                                         is_complete_objc_class = form_value.Signed();
541                                         break;
542 
543                                     case DW_AT_allocated:
544                                     case DW_AT_associated:
545                                     case DW_AT_data_location:
546                                     case DW_AT_description:
547                                     case DW_AT_start_scope:
548                                     case DW_AT_visibility:
549                                     default:
550                                     case DW_AT_sibling:
551                                         break;
552                                 }
553                             }
554                         }
555                     }
556 
557                     // UniqueDWARFASTType is large, so don't create a local variables on the
558                     // stack, put it on the heap. This function is often called recursively
559                     // and clang isn't good and sharing the stack space for variables in different blocks.
560                     std::unique_ptr<UniqueDWARFASTType> unique_ast_entry_ap(new UniqueDWARFASTType());
561 
562                     ConstString unique_typename(type_name_const_str);
563                     Declaration unique_decl(decl);
564 
565                     if (type_name_const_str)
566                     {
567                         LanguageType die_language = die.GetLanguage();
568                         if (Language::LanguageIsCPlusPlus(die_language))
569                         {
570                             // For C++, we rely solely upon the one definition rule that says only
571                             // one thing can exist at a given decl context. We ignore the file and
572                             // line that things are declared on.
573                             std::string qualified_name;
574                             if (die.GetQualifiedName(qualified_name))
575                                 unique_typename = ConstString(qualified_name);
576                             unique_decl.Clear();
577                         }
578 
579                         if (dwarf->GetUniqueDWARFASTTypeMap().Find(unique_typename, die, unique_decl,
580                                                                    byte_size_valid ? byte_size : -1,
581                                                                    *unique_ast_entry_ap))
582                         {
583                             type_sp = unique_ast_entry_ap->m_type_sp;
584                             if (type_sp)
585                             {
586                                 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
587                                 return type_sp;
588                             }
589                         }
590                     }
591 
592                     DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
593 
594                     int tag_decl_kind = -1;
595                     AccessType default_accessibility = eAccessNone;
596                     if (tag == DW_TAG_structure_type)
597                     {
598                         tag_decl_kind = clang::TTK_Struct;
599                         default_accessibility = eAccessPublic;
600                     }
601                     else if (tag == DW_TAG_union_type)
602                     {
603                         tag_decl_kind = clang::TTK_Union;
604                         default_accessibility = eAccessPublic;
605                     }
606                     else if (tag == DW_TAG_class_type)
607                     {
608                         tag_decl_kind = clang::TTK_Class;
609                         default_accessibility = eAccessPrivate;
610                     }
611 
612                     if (byte_size_valid && byte_size == 0 && type_name_cstr &&
613                         die.HasChildren() == false &&
614                         sc.comp_unit->GetLanguage() == eLanguageTypeObjC)
615                     {
616                         // Work around an issue with clang at the moment where
617                         // forward declarations for objective C classes are emitted
618                         // as:
619                         //  DW_TAG_structure_type [2]
620                         //  DW_AT_name( "ForwardObjcClass" )
621                         //  DW_AT_byte_size( 0x00 )
622                         //  DW_AT_decl_file( "..." )
623                         //  DW_AT_decl_line( 1 )
624                         //
625                         // Note that there is no DW_AT_declaration and there are
626                         // no children, and the byte size is zero.
627                         is_forward_declaration = true;
628                     }
629 
630                     if (class_language == eLanguageTypeObjC ||
631                         class_language == eLanguageTypeObjC_plus_plus)
632                     {
633                         if (!is_complete_objc_class && die.Supports_DW_AT_APPLE_objc_complete_type())
634                         {
635                             // We have a valid eSymbolTypeObjCClass class symbol whose
636                             // name matches the current objective C class that we
637                             // are trying to find and this DIE isn't the complete
638                             // definition (we checked is_complete_objc_class above and
639                             // know it is false), so the real definition is in here somewhere
640                             type_sp = dwarf->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
641 
642                             if (!type_sp)
643                             {
644                                 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
645                                 if (debug_map_symfile)
646                                 {
647                                     // We weren't able to find a full declaration in
648                                     // this DWARF, see if we have a declaration anywhere
649                                     // else...
650                                     type_sp = debug_map_symfile->FindCompleteObjCDefinitionTypeForDIE (die, type_name_const_str, true);
651                                 }
652                             }
653 
654                             if (type_sp)
655                             {
656                                 if (log)
657                                 {
658                                     dwarf->GetObjectFile()->GetModule()->LogMessage (log,
659                                                                                      "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is an incomplete objc type, complete type is 0x%8.8" PRIx64,
660                                                                                      static_cast<void*>(this),
661                                                                                      die.GetOffset(),
662                                                                                      DW_TAG_value_to_name(tag),
663                                                                                      type_name_cstr,
664                                                                                      type_sp->GetID());
665                                 }
666 
667                                 // We found a real definition for this type elsewhere
668                                 // so lets use it and cache the fact that we found
669                                 // a complete type for this die
670                                 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
671                                 return type_sp;
672                             }
673                         }
674                     }
675 
676 
677                     if (is_forward_declaration)
678                     {
679                         // We have a forward declaration to a type and we need
680                         // to try and find a full declaration. We look in the
681                         // current type index just in case we have a forward
682                         // declaration followed by an actual declarations in the
683                         // DWARF. If this fails, we need to look elsewhere...
684                         if (log)
685                         {
686                             dwarf->GetObjectFile()->GetModule()->LogMessage (log,
687                                                                              "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, trying to find complete type",
688                                                                              static_cast<void*>(this),
689                                                                              die.GetOffset(),
690                                                                              DW_TAG_value_to_name(tag),
691                                                                              type_name_cstr);
692                         }
693 
694                         // See if the type comes from a DWO module and if so, track down that type.
695                         type_sp = ParseTypeFromDWO(die, log);
696                         if (type_sp)
697                             return type_sp;
698 
699                         DWARFDeclContext die_decl_ctx;
700                         die.GetDWARFDeclContext(die_decl_ctx);
701 
702                         //type_sp = FindDefinitionTypeForDIE (dwarf_cu, die, type_name_const_str);
703                         type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
704 
705                         if (!type_sp)
706                         {
707                             SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
708                             if (debug_map_symfile)
709                             {
710                                 // We weren't able to find a full declaration in
711                                 // this DWARF, see if we have a declaration anywhere
712                                 // else...
713                                 type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
714                             }
715                         }
716 
717                         if (type_sp)
718                         {
719                             if (log)
720                             {
721                                 dwarf->GetObjectFile()->GetModule()->LogMessage (log,
722                                                                                  "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
723                                                                                  static_cast<void*>(this),
724                                                                                  die.GetOffset(),
725                                                                                  DW_TAG_value_to_name(tag),
726                                                                                  type_name_cstr,
727                                                                                  type_sp->GetID());
728                             }
729 
730                             // We found a real definition for this type elsewhere
731                             // so lets use it and cache the fact that we found
732                             // a complete type for this die
733                             dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
734                             clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(
735                                 dwarf->DebugInfo()->GetDIE(DIERef(type_sp->GetID(), dwarf)));
736                             if (defn_decl_ctx)
737                                 LinkDeclContextToDIE(defn_decl_ctx, die);
738                             return type_sp;
739                         }
740                     }
741                     assert (tag_decl_kind != -1);
742                     bool clang_type_was_created = false;
743                     clang_type.SetCompilerType(&m_ast, dwarf->GetForwardDeclDieToClangType().lookup (die.GetDIE()));
744                     if (!clang_type)
745                     {
746                         clang::DeclContext *decl_ctx = GetClangDeclContextContainingDIE (die, nullptr);
747                         if (accessibility == eAccessNone && decl_ctx)
748                         {
749                             // Check the decl context that contains this class/struct/union.
750                             // If it is a class we must give it an accessibility.
751                             const clang::Decl::Kind containing_decl_kind = decl_ctx->getDeclKind();
752                             if (DeclKindIsCXXClass (containing_decl_kind))
753                                 accessibility = default_accessibility;
754                         }
755 
756                         ClangASTMetadata metadata;
757                         metadata.SetUserID(die.GetID());
758                         metadata.SetIsDynamicCXXType(dwarf->ClassOrStructIsVirtual (die));
759 
760                         if (type_name_cstr && strchr (type_name_cstr, '<'))
761                         {
762                             ClangASTContext::TemplateParameterInfos template_param_infos;
763                             if (ParseTemplateParameterInfos (die, template_param_infos))
764                             {
765                                 clang::ClassTemplateDecl *class_template_decl = m_ast.ParseClassTemplateDecl (decl_ctx,
766                                                                                                               accessibility,
767                                                                                                               type_name_cstr,
768                                                                                                               tag_decl_kind,
769                                                                                                               template_param_infos);
770 
771                                 clang::ClassTemplateSpecializationDecl *class_specialization_decl = m_ast.CreateClassTemplateSpecializationDecl (decl_ctx,
772                                                                                                                                                  class_template_decl,
773                                                                                                                                                  tag_decl_kind,
774                                                                                                                                                  template_param_infos);
775                                 clang_type = m_ast.CreateClassTemplateSpecializationType (class_specialization_decl);
776                                 clang_type_was_created = true;
777 
778                                 m_ast.SetMetadata (class_template_decl, metadata);
779                                 m_ast.SetMetadata (class_specialization_decl, metadata);
780                             }
781                         }
782 
783                         if (!clang_type_was_created)
784                         {
785                             clang_type_was_created = true;
786                             clang_type = m_ast.CreateRecordType (decl_ctx,
787                                                                  accessibility,
788                                                                  type_name_cstr,
789                                                                  tag_decl_kind,
790                                                                  class_language,
791                                                                  &metadata);
792                         }
793                     }
794 
795                     // Store a forward declaration to this class type in case any
796                     // parameters in any class methods need it for the clang
797                     // types for function prototypes.
798                     LinkDeclContextToDIE(m_ast.GetDeclContextForType(clang_type), die);
799                     type_sp.reset (new Type (die.GetID(),
800                                              dwarf,
801                                              type_name_const_str,
802                                              byte_size,
803                                              NULL,
804                                              LLDB_INVALID_UID,
805                                              Type::eEncodingIsUID,
806                                              &decl,
807                                              clang_type,
808                                              Type::eResolveStateForward));
809 
810                     type_sp->SetIsCompleteObjCClass(is_complete_objc_class);
811 
812 
813                     // Add our type to the unique type map so we don't
814                     // end up creating many copies of the same type over
815                     // and over in the ASTContext for our module
816                     unique_ast_entry_ap->m_type_sp = type_sp;
817                     unique_ast_entry_ap->m_die = die;
818                     unique_ast_entry_ap->m_declaration = unique_decl;
819                     unique_ast_entry_ap->m_byte_size = byte_size;
820                     dwarf->GetUniqueDWARFASTTypeMap().Insert (unique_typename,
821                                                               *unique_ast_entry_ap);
822 
823                     if (is_forward_declaration && die.HasChildren())
824                     {
825                         // Check to see if the DIE actually has a definition, some version of GCC will
826                         // emit DIEs with DW_AT_declaration set to true, but yet still have subprogram,
827                         // members, or inheritance, so we can't trust it
828                         DWARFDIE child_die = die.GetFirstChild();
829                         while (child_die)
830                         {
831                             switch (child_die.Tag())
832                             {
833                                 case DW_TAG_inheritance:
834                                 case DW_TAG_subprogram:
835                                 case DW_TAG_member:
836                                 case DW_TAG_APPLE_property:
837                                 case DW_TAG_class_type:
838                                 case DW_TAG_structure_type:
839                                 case DW_TAG_enumeration_type:
840                                 case DW_TAG_typedef:
841                                 case DW_TAG_union_type:
842                                     child_die.Clear();
843                                     is_forward_declaration = false;
844                                     break;
845                                 default:
846                                     child_die = child_die.GetSibling();
847                                     break;
848                             }
849                         }
850                     }
851 
852                     if (!is_forward_declaration)
853                     {
854                         // Always start the definition for a class type so that
855                         // if the class has child classes or types that require
856                         // the class to be created for use as their decl contexts
857                         // the class will be ready to accept these child definitions.
858                         if (die.HasChildren() == false)
859                         {
860                             // No children for this struct/union/class, lets finish it
861                             ClangASTContext::StartTagDeclarationDefinition (clang_type);
862                             ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
863 
864                             if (tag == DW_TAG_structure_type) // this only applies in C
865                             {
866                                 clang::RecordDecl *record_decl = ClangASTContext::GetAsRecordDecl(clang_type);
867 
868                                 if (record_decl)
869                                 {
870                                     GetClangASTImporter().InsertRecordDecl(record_decl, ClangASTImporter::LayoutInfo());
871                                 }
872                             }
873                         }
874                         else if (clang_type_was_created)
875                         {
876                             // Start the definition if the class is not objective C since
877                             // the underlying decls respond to isCompleteDefinition(). Objective
878                             // C decls don't respond to isCompleteDefinition() so we can't
879                             // start the declaration definition right away. For C++ class/union/structs
880                             // we want to start the definition in case the class is needed as the
881                             // declaration context for a contained class or type without the need
882                             // to complete that type..
883 
884                             if (class_language != eLanguageTypeObjC &&
885                                 class_language != eLanguageTypeObjC_plus_plus)
886                                 ClangASTContext::StartTagDeclarationDefinition (clang_type);
887 
888                             // Leave this as a forward declaration until we need
889                             // to know the details of the type. lldb_private::Type
890                             // will automatically call the SymbolFile virtual function
891                             // "SymbolFileDWARF::CompleteType(Type *)"
892                             // When the definition needs to be defined.
893                             assert(!dwarf->GetForwardDeclClangTypeToDie().count(
894                                        ClangUtil::RemoveFastQualifiers(clang_type).GetOpaqueQualType()) &&
895                                    "Type already in the forward declaration map!");
896                             // Can't assume m_ast.GetSymbolFile() is actually a SymbolFileDWARF, it can be a
897                             // SymbolFileDWARFDebugMap for Apple binaries.
898                             dwarf->GetForwardDeclDieToClangType()[die.GetDIE()] = clang_type.GetOpaqueQualType();
899                             dwarf->GetForwardDeclClangTypeToDie()[ClangUtil::RemoveFastQualifiers(clang_type)
900                                                                       .GetOpaqueQualType()] = die.GetDIERef();
901                             m_ast.SetHasExternalStorage (clang_type.GetOpaqueQualType(), true);
902                         }
903                     }
904                 }
905                     break;
906 
907                 case DW_TAG_enumeration_type:
908                 {
909                     // Set a bit that lets us know that we are currently parsing this
910                     dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
911 
912                     DWARFFormValue encoding_form;
913 
914                     const size_t num_attributes = die.GetAttributes (attributes);
915                     if (num_attributes > 0)
916                     {
917                         uint32_t i;
918 
919                         for (i=0; i<num_attributes; ++i)
920                         {
921                             attr = attributes.AttributeAtIndex(i);
922                             if (attributes.ExtractFormValueAtIndex(i, form_value))
923                             {
924                                 switch (attr)
925                                 {
926                                     case DW_AT_decl_file:       decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
927                                     case DW_AT_decl_line:       decl.SetLine(form_value.Unsigned()); break;
928                                     case DW_AT_decl_column:     decl.SetColumn(form_value.Unsigned()); break;
929                                     case DW_AT_name:
930                                         type_name_cstr = form_value.AsCString();
931                                         type_name_const_str.SetCString(type_name_cstr);
932                                         break;
933                                     case DW_AT_type:            encoding_form = form_value; break;
934                                     case DW_AT_byte_size:       byte_size = form_value.Unsigned(); break;
935                                     case DW_AT_accessibility:   break; //accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
936                                     case DW_AT_declaration:     is_forward_declaration = form_value.Boolean(); break;
937                                     case DW_AT_allocated:
938                                     case DW_AT_associated:
939                                     case DW_AT_bit_stride:
940                                     case DW_AT_byte_stride:
941                                     case DW_AT_data_location:
942                                     case DW_AT_description:
943                                     case DW_AT_start_scope:
944                                     case DW_AT_visibility:
945                                     case DW_AT_specification:
946                                     case DW_AT_abstract_origin:
947                                     case DW_AT_sibling:
948                                         break;
949                                 }
950                             }
951                         }
952 
953                         if (is_forward_declaration)
954                         {
955                             type_sp = ParseTypeFromDWO(die, log);
956                             if (type_sp)
957                                 return type_sp;
958 
959                             DWARFDeclContext die_decl_ctx;
960                             die.GetDWARFDeclContext(die_decl_ctx);
961 
962                             type_sp = dwarf->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
963 
964                             if (!type_sp)
965                             {
966                                 SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
967                                 if (debug_map_symfile)
968                                 {
969                                     // We weren't able to find a full declaration in
970                                     // this DWARF, see if we have a declaration anywhere
971                                     // else...
972                                     type_sp = debug_map_symfile->FindDefinitionTypeForDWARFDeclContext (die_decl_ctx);
973                                 }
974                             }
975 
976                             if (type_sp)
977                             {
978                                 if (log)
979                                 {
980                                     dwarf->GetObjectFile()->GetModule()->LogMessage (log,
981                                                                                      "SymbolFileDWARF(%p) - 0x%8.8x: %s type \"%s\" is a forward declaration, complete type is 0x%8.8" PRIx64,
982                                                                                      static_cast<void*>(this),
983                                                                                      die.GetOffset(),
984                                                                                      DW_TAG_value_to_name(tag),
985                                                                                      type_name_cstr,
986                                                                                      type_sp->GetID());
987                                 }
988 
989                                 // We found a real definition for this type elsewhere
990                                 // so lets use it and cache the fact that we found
991                                 // a complete type for this die
992                                 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
993                                 clang::DeclContext *defn_decl_ctx = GetCachedClangDeclContextForDIE(dwarf->DebugInfo()->GetDIE(DIERef(type_sp->GetID(), dwarf)));
994                                 if (defn_decl_ctx)
995                                     LinkDeclContextToDIE(defn_decl_ctx, die);
996                                 return type_sp;
997                             }
998 
999                         }
1000                         DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
1001 
1002                         CompilerType enumerator_clang_type;
1003                         clang_type.SetCompilerType (&m_ast, dwarf->GetForwardDeclDieToClangType().lookup (die.GetDIE()));
1004                         if (!clang_type)
1005                         {
1006                             if (encoding_form.IsValid())
1007                             {
1008                                 Type *enumerator_type = dwarf->ResolveTypeUID(DIERef(encoding_form));
1009                                 if (enumerator_type)
1010                                     enumerator_clang_type = enumerator_type->GetFullCompilerType ();
1011                             }
1012 
1013                             if (!enumerator_clang_type)
1014                             {
1015                                 if (byte_size > 0)
1016                                 {
1017                                     enumerator_clang_type = m_ast.GetBuiltinTypeForDWARFEncodingAndBitSize(NULL,
1018                                                                                                            DW_ATE_signed,
1019                                                                                                            byte_size * 8);
1020                                 }
1021                                 else
1022                                 {
1023                                     enumerator_clang_type = m_ast.GetBasicType(eBasicTypeInt);
1024                                 }
1025                             }
1026 
1027                             clang_type = m_ast.CreateEnumerationType (type_name_cstr,
1028                                                                       GetClangDeclContextContainingDIE (die, nullptr),
1029                                                                       decl,
1030                                                                       enumerator_clang_type);
1031                         }
1032                         else
1033                         {
1034                             enumerator_clang_type = m_ast.GetEnumerationIntegerType (clang_type.GetOpaqueQualType());
1035                         }
1036 
1037                         LinkDeclContextToDIE(ClangASTContext::GetDeclContextForType(clang_type), die);
1038 
1039                         type_sp.reset( new Type (die.GetID(),
1040                                                  dwarf,
1041                                                  type_name_const_str,
1042                                                  byte_size,
1043                                                  NULL,
1044                                                  DIERef(encoding_form).GetUID(dwarf),
1045                                                  Type::eEncodingIsUID,
1046                                                  &decl,
1047                                                  clang_type,
1048                                                  Type::eResolveStateForward));
1049 
1050                         ClangASTContext::StartTagDeclarationDefinition (clang_type);
1051                         if (die.HasChildren())
1052                         {
1053                             SymbolContext cu_sc(die.GetLLDBCompileUnit());
1054                             bool is_signed = false;
1055                             enumerator_clang_type.IsIntegerType(is_signed);
1056                             ParseChildEnumerators(cu_sc, clang_type, is_signed, type_sp->GetByteSize(), die);
1057                         }
1058                         ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
1059                     }
1060                 }
1061                     break;
1062 
1063                 case DW_TAG_inlined_subroutine:
1064                 case DW_TAG_subprogram:
1065                 case DW_TAG_subroutine_type:
1066                 {
1067                     // Set a bit that lets us know that we are currently parsing this
1068                     dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
1069 
1070                     DWARFFormValue type_die_form;
1071                     bool is_variadic = false;
1072                     bool is_inline = false;
1073                     bool is_static = false;
1074                     bool is_virtual = false;
1075                     bool is_explicit = false;
1076                     bool is_artificial = false;
1077                     bool has_template_params = false;
1078                     DWARFFormValue specification_die_form;
1079                     DWARFFormValue abstract_origin_die_form;
1080                     dw_offset_t object_pointer_die_offset = DW_INVALID_OFFSET;
1081 
1082                     unsigned type_quals = 0;
1083                     clang::StorageClass storage = clang::SC_None;//, Extern, Static, PrivateExtern
1084 
1085 
1086                     const size_t num_attributes = die.GetAttributes (attributes);
1087                     if (num_attributes > 0)
1088                     {
1089                         uint32_t i;
1090                         for (i=0; i<num_attributes; ++i)
1091                         {
1092                             attr = attributes.AttributeAtIndex(i);
1093                             if (attributes.ExtractFormValueAtIndex(i, form_value))
1094                             {
1095                                 switch (attr)
1096                                 {
1097                                     case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1098                                     case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
1099                                     case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1100                                     case DW_AT_name:
1101                                         type_name_cstr = form_value.AsCString();
1102                                         type_name_const_str.SetCString(type_name_cstr);
1103                                         break;
1104 
1105                                     case DW_AT_linkage_name:
1106                                     case DW_AT_MIPS_linkage_name:   break; // mangled = form_value.AsCString(&dwarf->get_debug_str_data()); break;
1107                                     case DW_AT_type:                type_die_form = form_value; break;
1108                                     case DW_AT_accessibility:       accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
1109                                     case DW_AT_declaration:         break; // is_forward_declaration = form_value.Boolean(); break;
1110                                     case DW_AT_inline:              is_inline = form_value.Boolean(); break;
1111                                     case DW_AT_virtuality:          is_virtual = form_value.Boolean();  break;
1112                                     case DW_AT_explicit:            is_explicit = form_value.Boolean();  break;
1113                                     case DW_AT_artificial:          is_artificial = form_value.Boolean();  break;
1114 
1115 
1116                                     case DW_AT_external:
1117                                         if (form_value.Unsigned())
1118                                         {
1119                                             if (storage == clang::SC_None)
1120                                                 storage = clang::SC_Extern;
1121                                             else
1122                                                 storage = clang::SC_PrivateExtern;
1123                                         }
1124                                         break;
1125 
1126                                     case DW_AT_specification:
1127                                         specification_die_form = form_value;
1128                                         break;
1129 
1130                                     case DW_AT_abstract_origin:
1131                                         abstract_origin_die_form = form_value;
1132                                         break;
1133 
1134                                     case DW_AT_object_pointer:
1135                                         object_pointer_die_offset = form_value.Reference();
1136                                         break;
1137 
1138                                     case DW_AT_allocated:
1139                                     case DW_AT_associated:
1140                                     case DW_AT_address_class:
1141                                     case DW_AT_calling_convention:
1142                                     case DW_AT_data_location:
1143                                     case DW_AT_elemental:
1144                                     case DW_AT_entry_pc:
1145                                     case DW_AT_frame_base:
1146                                     case DW_AT_high_pc:
1147                                     case DW_AT_low_pc:
1148                                     case DW_AT_prototyped:
1149                                     case DW_AT_pure:
1150                                     case DW_AT_ranges:
1151                                     case DW_AT_recursive:
1152                                     case DW_AT_return_addr:
1153                                     case DW_AT_segment:
1154                                     case DW_AT_start_scope:
1155                                     case DW_AT_static_link:
1156                                     case DW_AT_trampoline:
1157                                     case DW_AT_visibility:
1158                                     case DW_AT_vtable_elem_location:
1159                                     case DW_AT_description:
1160                                     case DW_AT_sibling:
1161                                         break;
1162                                 }
1163                             }
1164                         }
1165                     }
1166 
1167                     std::string object_pointer_name;
1168                     if (object_pointer_die_offset != DW_INVALID_OFFSET)
1169                     {
1170                         DWARFDIE object_pointer_die = die.GetDIE (object_pointer_die_offset);
1171                         if (object_pointer_die)
1172                         {
1173                             const char *object_pointer_name_cstr = object_pointer_die.GetName();
1174                             if (object_pointer_name_cstr)
1175                                 object_pointer_name = object_pointer_name_cstr;
1176                         }
1177                     }
1178 
1179                     DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
1180 
1181                     CompilerType return_clang_type;
1182                     Type *func_type = NULL;
1183 
1184                     if (type_die_form.IsValid())
1185                         func_type = dwarf->ResolveTypeUID(DIERef(type_die_form));
1186 
1187                     if (func_type)
1188                         return_clang_type = func_type->GetForwardCompilerType ();
1189                     else
1190                         return_clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1191 
1192 
1193                     std::vector<CompilerType> function_param_types;
1194                     std::vector<clang::ParmVarDecl*> function_param_decls;
1195 
1196                     // Parse the function children for the parameters
1197 
1198                     DWARFDIE decl_ctx_die;
1199                     clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (die, &decl_ctx_die);
1200                     const clang::Decl::Kind containing_decl_kind = containing_decl_ctx->getDeclKind();
1201 
1202                     bool is_cxx_method = DeclKindIsCXXClass (containing_decl_kind);
1203                     // Start off static. This will be set to false in ParseChildParameters(...)
1204                     // if we find a "this" parameters as the first parameter
1205                     if (is_cxx_method)
1206                     {
1207                         is_static = true;
1208                     }
1209 
1210                     if (die.HasChildren())
1211                     {
1212                         bool skip_artificial = true;
1213                         ParseChildParameters (sc,
1214                                               containing_decl_ctx,
1215                                               die,
1216                                               skip_artificial,
1217                                               is_static,
1218                                               is_variadic,
1219                                               has_template_params,
1220                                               function_param_types,
1221                                               function_param_decls,
1222                                               type_quals);
1223                     }
1224 
1225                     bool ignore_containing_context = false;
1226                     // Check for templatized class member functions. If we had any DW_TAG_template_type_parameter
1227                     // or DW_TAG_template_value_parameter the DW_TAG_subprogram DIE, then we can't let this become
1228                     // a method in a class. Why? Because templatized functions are only emitted if one of the
1229                     // templatized methods is used in the current compile unit and we will end up with classes
1230                     // that may or may not include these member functions and this means one class won't match another
1231                     // class definition and it affects our ability to use a class in the clang expression parser. So
1232                     // for the greater good, we currently must not allow any template member functions in a class definition.
1233                     if (is_cxx_method && has_template_params)
1234                     {
1235                         ignore_containing_context = true;
1236                         is_cxx_method = false;
1237                     }
1238 
1239                     // clang_type will get the function prototype clang type after this call
1240                     clang_type = m_ast.CreateFunctionType (return_clang_type,
1241                                                            function_param_types.data(),
1242                                                            function_param_types.size(),
1243                                                            is_variadic,
1244                                                            type_quals);
1245 
1246 
1247                     if (type_name_cstr)
1248                     {
1249                         bool type_handled = false;
1250                         if (tag == DW_TAG_subprogram ||
1251                             tag == DW_TAG_inlined_subroutine)
1252                         {
1253                             ObjCLanguage::MethodName objc_method (type_name_cstr, true);
1254                             if (objc_method.IsValid(true))
1255                             {
1256                                 CompilerType class_opaque_type;
1257                                 ConstString class_name(objc_method.GetClassName());
1258                                 if (class_name)
1259                                 {
1260                                     TypeSP complete_objc_class_type_sp (dwarf->FindCompleteObjCDefinitionTypeForDIE (DWARFDIE(), class_name, false));
1261 
1262                                     if (complete_objc_class_type_sp)
1263                                     {
1264                                         CompilerType type_clang_forward_type = complete_objc_class_type_sp->GetForwardCompilerType ();
1265                                         if (ClangASTContext::IsObjCObjectOrInterfaceType(type_clang_forward_type))
1266                                             class_opaque_type = type_clang_forward_type;
1267                                     }
1268                                 }
1269 
1270                                 if (class_opaque_type)
1271                                 {
1272                                     // If accessibility isn't set to anything valid, assume public for
1273                                     // now...
1274                                     if (accessibility == eAccessNone)
1275                                         accessibility = eAccessPublic;
1276 
1277                                     clang::ObjCMethodDecl *objc_method_decl = m_ast.AddMethodToObjCObjectType (class_opaque_type,
1278                                                                                                                type_name_cstr,
1279                                                                                                                clang_type,
1280                                                                                                                accessibility,
1281                                                                                                                is_artificial);
1282                                     type_handled = objc_method_decl != NULL;
1283                                     if (type_handled)
1284                                     {
1285                                         LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(objc_method_decl), die);
1286                                         m_ast.SetMetadataAsUserID (objc_method_decl, die.GetID());
1287                                     }
1288                                     else
1289                                     {
1290                                         dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: invalid Objective-C method 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
1291                                                                                           die.GetOffset(),
1292                                                                                           tag,
1293                                                                                           DW_TAG_value_to_name(tag));
1294                                     }
1295                                 }
1296                             }
1297                             else if (is_cxx_method)
1298                             {
1299                                 // Look at the parent of this DIE and see if is is
1300                                 // a class or struct and see if this is actually a
1301                                 // C++ method
1302                                 Type *class_type = dwarf->ResolveType (decl_ctx_die);
1303                                 if (class_type)
1304                                 {
1305                                     bool alternate_defn = false;
1306                                     if (class_type->GetID() != decl_ctx_die.GetID() || decl_ctx_die.GetContainingDWOModuleDIE())
1307                                     {
1308                                         alternate_defn = true;
1309 
1310                                         // We uniqued the parent class of this function to another class
1311                                         // so we now need to associate all dies under "decl_ctx_die" to
1312                                         // DIEs in the DIE for "class_type"...
1313                                         SymbolFileDWARF *class_symfile = NULL;
1314                                         DWARFDIE class_type_die;
1315 
1316                                         SymbolFileDWARFDebugMap *debug_map_symfile = dwarf->GetDebugMapSymfile();
1317                                         if (debug_map_symfile)
1318                                         {
1319                                             class_symfile = debug_map_symfile->GetSymbolFileByOSOIndex(SymbolFileDWARFDebugMap::GetOSOIndexFromUserID(class_type->GetID()));
1320                                             class_type_die = class_symfile->DebugInfo()->GetDIE (DIERef(class_type->GetID(), dwarf));
1321                                         }
1322                                         else
1323                                         {
1324                                             class_symfile = dwarf;
1325                                             class_type_die = dwarf->DebugInfo()->GetDIE (DIERef(class_type->GetID(), dwarf));
1326                                         }
1327                                         if (class_type_die)
1328                                         {
1329                                             DWARFDIECollection failures;
1330 
1331                                             CopyUniqueClassMethodTypes (decl_ctx_die,
1332                                                                         class_type_die,
1333                                                                         class_type,
1334                                                                         failures);
1335 
1336                                             // FIXME do something with these failures that's smarter than
1337                                             // just dropping them on the ground.  Unfortunately classes don't
1338                                             // like having stuff added to them after their definitions are
1339                                             // complete...
1340 
1341                                             type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1342                                             if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
1343                                             {
1344                                                 type_sp = type_ptr->shared_from_this();
1345                                                 break;
1346                                             }
1347                                         }
1348                                     }
1349 
1350                                     if (specification_die_form.IsValid())
1351                                     {
1352                                         // We have a specification which we are going to base our function
1353                                         // prototype off of, so we need this type to be completed so that the
1354                                         // m_die_to_decl_ctx for the method in the specification has a valid
1355                                         // clang decl context.
1356                                         class_type->GetForwardCompilerType ();
1357                                         // If we have a specification, then the function type should have been
1358                                         // made with the specification and not with this die.
1359                                         DWARFDIE spec_die = dwarf->DebugInfo()->GetDIE(DIERef(specification_die_form));
1360                                         clang::DeclContext *spec_clang_decl_ctx = GetClangDeclContextForDIE (spec_die);
1361                                         if (spec_clang_decl_ctx)
1362                                         {
1363                                             LinkDeclContextToDIE(spec_clang_decl_ctx, die);
1364                                         }
1365                                         else
1366                                         {
1367                                             dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_specification(0x%8.8" PRIx64 ") has no decl\n",
1368                                                                                                 die.GetID(),
1369                                                                                                 specification_die_form.Reference());
1370                                         }
1371                                         type_handled = true;
1372                                     }
1373                                     else if (abstract_origin_die_form.IsValid())
1374                                     {
1375                                         // We have a specification which we are going to base our function
1376                                         // prototype off of, so we need this type to be completed so that the
1377                                         // m_die_to_decl_ctx for the method in the abstract origin has a valid
1378                                         // clang decl context.
1379                                         class_type->GetForwardCompilerType ();
1380 
1381                                         DWARFDIE abs_die = dwarf->DebugInfo()->GetDIE (DIERef(abstract_origin_die_form));
1382                                         clang::DeclContext *abs_clang_decl_ctx = GetClangDeclContextForDIE (abs_die);
1383                                         if (abs_clang_decl_ctx)
1384                                         {
1385                                             LinkDeclContextToDIE (abs_clang_decl_ctx, die);
1386                                         }
1387                                         else
1388                                         {
1389                                             dwarf->GetObjectFile()->GetModule()->ReportWarning ("0x%8.8" PRIx64 ": DW_AT_abstract_origin(0x%8.8" PRIx64 ") has no decl\n",
1390                                                                                                 die.GetID(),
1391                                                                                                 abstract_origin_die_form.Reference());
1392                                         }
1393                                         type_handled = true;
1394                                     }
1395                                     else
1396                                     {
1397                                         CompilerType class_opaque_type = class_type->GetForwardCompilerType ();
1398                                         if (ClangASTContext::IsCXXClassType(class_opaque_type))
1399                                         {
1400                                             if (class_opaque_type.IsBeingDefined () || alternate_defn)
1401                                             {
1402                                                 if (!is_static && !die.HasChildren())
1403                                                 {
1404                                                     // We have a C++ member function with no children (this pointer!)
1405                                                     // and clang will get mad if we try and make a function that isn't
1406                                                     // well formed in the DWARF, so we will just skip it...
1407                                                     type_handled = true;
1408                                                 }
1409                                                 else
1410                                                 {
1411                                                     bool add_method = true;
1412                                                     if (alternate_defn)
1413                                                     {
1414                                                         // If an alternate definition for the class exists, then add the method only if an
1415                                                         // equivalent is not already present.
1416                                                         clang::CXXRecordDecl *record_decl = m_ast.GetAsCXXRecordDecl(class_opaque_type.GetOpaqueQualType());
1417                                                         if (record_decl)
1418                                                         {
1419                                                             for (auto method_iter = record_decl->method_begin();
1420                                                                  method_iter != record_decl->method_end();
1421                                                                  method_iter++)
1422                                                             {
1423                                                                 clang::CXXMethodDecl *method_decl = *method_iter;
1424                                                                 if (method_decl->getNameInfo().getAsString() == std::string(type_name_cstr))
1425                                                                 {
1426                                                                     if (method_decl->getType() ==
1427                                                                         ClangUtil::GetQualType(clang_type))
1428                                                                     {
1429                                                                         add_method = false;
1430                                                                         LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(method_decl), die);
1431                                                                         type_handled = true;
1432 
1433                                                                         break;
1434                                                                     }
1435                                                                 }
1436                                                             }
1437                                                         }
1438                                                     }
1439 
1440                                                     if (add_method)
1441                                                     {
1442                                                         // REMOVE THE CRASH DESCRIPTION BELOW
1443                                                         Host::SetCrashDescriptionWithFormat ("SymbolFileDWARF::ParseType() is adding a method %s to class %s in DIE 0x%8.8" PRIx64 " from %s",
1444                                                                                              type_name_cstr,
1445                                                                                              class_type->GetName().GetCString(),
1446                                                                                              die.GetID(),
1447                                                                                              dwarf->GetObjectFile()->GetFileSpec().GetPath().c_str());
1448 
1449                                                         const bool is_attr_used = false;
1450                                                         // Neither GCC 4.2 nor clang++ currently set a valid accessibility
1451                                                         // in the DWARF for C++ methods... Default to public for now...
1452                                                         if (accessibility == eAccessNone)
1453                                                             accessibility = eAccessPublic;
1454 
1455                                                         clang::CXXMethodDecl *cxx_method_decl;
1456                                                         cxx_method_decl = m_ast.AddMethodToCXXRecordType (class_opaque_type.GetOpaqueQualType(),
1457                                                                                                           type_name_cstr,
1458                                                                                                           clang_type,
1459                                                                                                           accessibility,
1460                                                                                                           is_virtual,
1461                                                                                                           is_static,
1462                                                                                                           is_inline,
1463                                                                                                           is_explicit,
1464                                                                                                           is_attr_used,
1465                                                                                                           is_artificial);
1466 
1467                                                         type_handled = cxx_method_decl != NULL;
1468 
1469                                                         if (type_handled)
1470                                                         {
1471                                                             LinkDeclContextToDIE(ClangASTContext::GetAsDeclContext(cxx_method_decl), die);
1472 
1473                                                             Host::SetCrashDescription (NULL);
1474 
1475                                                             ClangASTMetadata metadata;
1476                                                             metadata.SetUserID(die.GetID());
1477 
1478                                                             if (!object_pointer_name.empty())
1479                                                             {
1480                                                                 metadata.SetObjectPtrName(object_pointer_name.c_str());
1481                                                                 if (log)
1482                                                                     log->Printf ("Setting object pointer name: %s on method object %p.\n",
1483                                                                                  object_pointer_name.c_str(),
1484                                                                                  static_cast<void*>(cxx_method_decl));
1485                                                             }
1486                                                             m_ast.SetMetadata (cxx_method_decl, metadata);
1487                                                         }
1488                                                         else
1489                                                         {
1490                                                             ignore_containing_context = true;
1491                                                         }
1492                                                     }
1493                                                 }
1494                                             }
1495                                             else
1496                                             {
1497                                                 // We were asked to parse the type for a method in a class, yet the
1498                                                 // class hasn't been asked to complete itself through the
1499                                                 // clang::ExternalASTSource protocol, so we need to just have the
1500                                                 // class complete itself and do things the right way, then our
1501                                                 // DIE should then have an entry in the dwarf->GetDIEToType() map. First
1502                                                 // we need to modify the dwarf->GetDIEToType() so it doesn't think we are
1503                                                 // trying to parse this DIE anymore...
1504                                                 dwarf->GetDIEToType()[die.GetDIE()] = NULL;
1505 
1506                                                 // Now we get the full type to force our class type to complete itself
1507                                                 // using the clang::ExternalASTSource protocol which will parse all
1508                                                 // base classes and all methods (including the method for this DIE).
1509                                                 class_type->GetFullCompilerType ();
1510 
1511                                                 // The type for this DIE should have been filled in the function call above
1512                                                 type_ptr = dwarf->GetDIEToType()[die.GetDIE()];
1513                                                 if (type_ptr && type_ptr != DIE_IS_BEING_PARSED)
1514                                                 {
1515                                                     type_sp = type_ptr->shared_from_this();
1516                                                     break;
1517                                                 }
1518 
1519                                                 // FIXME This is fixing some even uglier behavior but we really need to
1520                                                 // uniq the methods of each class as well as the class itself.
1521                                                 // <rdar://problem/11240464>
1522                                                 type_handled = true;
1523                                             }
1524                                         }
1525                                     }
1526                                 }
1527                             }
1528                         }
1529 
1530                         if (!type_handled)
1531                         {
1532                             clang::FunctionDecl *function_decl = nullptr;
1533 
1534                             if (abstract_origin_die_form.IsValid())
1535                             {
1536                                 DWARFDIE abs_die = dwarf->DebugInfo()->GetDIE (DIERef(abstract_origin_die_form));
1537 
1538                                 SymbolContext sc;
1539 
1540                                 if (dwarf->ResolveType (abs_die))
1541                                 {
1542                                     function_decl = llvm::dyn_cast_or_null<clang::FunctionDecl>(GetCachedClangDeclContextForDIE(abs_die));
1543 
1544                                     if (function_decl)
1545                                     {
1546                                         LinkDeclContextToDIE(function_decl, die);
1547                                     }
1548                                 }
1549                             }
1550 
1551                             if (!function_decl)
1552                             {
1553                                 // We just have a function that isn't part of a class
1554                                 function_decl = m_ast.CreateFunctionDeclaration (ignore_containing_context ? m_ast.GetTranslationUnitDecl() : containing_decl_ctx,
1555                                                                                                       type_name_cstr,
1556                                                                                                       clang_type,
1557                                                                                                       storage,
1558                                                                                                       is_inline);
1559 
1560                                 //                            if (template_param_infos.GetSize() > 0)
1561                                 //                            {
1562                                 //                                clang::FunctionTemplateDecl *func_template_decl = CreateFunctionTemplateDecl (containing_decl_ctx,
1563                                 //                                                                                                              function_decl,
1564                                 //                                                                                                              type_name_cstr,
1565                                 //                                                                                                              template_param_infos);
1566                                 //
1567                                 //                                CreateFunctionTemplateSpecializationInfo (function_decl,
1568                                 //                                                                          func_template_decl,
1569                                 //                                                                          template_param_infos);
1570                                 //                            }
1571                                 // Add the decl to our DIE to decl context map
1572 
1573                                 lldbassert (function_decl);
1574 
1575                                 if (function_decl)
1576                                 {
1577                                     LinkDeclContextToDIE(function_decl, die);
1578 
1579                                     if (!function_param_decls.empty())
1580                                         m_ast.SetFunctionParameters (function_decl,
1581                                                                      &function_param_decls.front(),
1582                                                                      function_param_decls.size());
1583 
1584                                     ClangASTMetadata metadata;
1585                                     metadata.SetUserID(die.GetID());
1586 
1587                                     if (!object_pointer_name.empty())
1588                                     {
1589                                         metadata.SetObjectPtrName(object_pointer_name.c_str());
1590                                         if (log)
1591                                             log->Printf ("Setting object pointer name: %s on function object %p.",
1592                                                          object_pointer_name.c_str(),
1593                                                          static_cast<void*>(function_decl));
1594                                     }
1595                                     m_ast.SetMetadata (function_decl, metadata);
1596                                 }
1597                             }
1598                         }
1599                     }
1600                     type_sp.reset( new Type (die.GetID(),
1601                                              dwarf,
1602                                              type_name_const_str,
1603                                              0,
1604                                              NULL,
1605                                              LLDB_INVALID_UID,
1606                                              Type::eEncodingIsUID,
1607                                              &decl,
1608                                              clang_type,
1609                                              Type::eResolveStateFull));
1610                     assert(type_sp.get());
1611                 }
1612                     break;
1613 
1614                 case DW_TAG_array_type:
1615                 {
1616                     // Set a bit that lets us know that we are currently parsing this
1617                     dwarf->GetDIEToType()[die.GetDIE()] = DIE_IS_BEING_PARSED;
1618 
1619                     DWARFFormValue type_die_form;
1620                     int64_t first_index = 0;
1621                     uint32_t byte_stride = 0;
1622                     uint32_t bit_stride = 0;
1623                     bool is_vector = false;
1624                     const size_t num_attributes = die.GetAttributes (attributes);
1625 
1626                     if (num_attributes > 0)
1627                     {
1628                         uint32_t i;
1629                         for (i=0; i<num_attributes; ++i)
1630                         {
1631                             attr = attributes.AttributeAtIndex(i);
1632                             if (attributes.ExtractFormValueAtIndex(i, form_value))
1633                             {
1634                                 switch (attr)
1635                                 {
1636                                     case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
1637                                     case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
1638                                     case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
1639                                     case DW_AT_name:
1640                                         type_name_cstr = form_value.AsCString();
1641                                         type_name_const_str.SetCString(type_name_cstr);
1642                                         break;
1643 
1644                                     case DW_AT_type:            type_die_form = form_value; break;
1645                                     case DW_AT_byte_size:       break; // byte_size = form_value.Unsigned(); break;
1646                                     case DW_AT_byte_stride:     byte_stride = form_value.Unsigned(); break;
1647                                     case DW_AT_bit_stride:      bit_stride = form_value.Unsigned(); break;
1648                                     case DW_AT_GNU_vector:      is_vector = form_value.Boolean(); break;
1649                                     case DW_AT_accessibility:   break; // accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned()); break;
1650                                     case DW_AT_declaration:     break; // is_forward_declaration = form_value.Boolean(); break;
1651                                     case DW_AT_allocated:
1652                                     case DW_AT_associated:
1653                                     case DW_AT_data_location:
1654                                     case DW_AT_description:
1655                                     case DW_AT_ordering:
1656                                     case DW_AT_start_scope:
1657                                     case DW_AT_visibility:
1658                                     case DW_AT_specification:
1659                                     case DW_AT_abstract_origin:
1660                                     case DW_AT_sibling:
1661                                         break;
1662                                 }
1663                             }
1664                         }
1665 
1666                         DEBUG_PRINTF ("0x%8.8" PRIx64 ": %s (\"%s\")\n", die.GetID(), DW_TAG_value_to_name(tag), type_name_cstr);
1667 
1668                         DIERef type_die_ref(type_die_form);
1669                         Type *element_type = dwarf->ResolveTypeUID(type_die_ref);
1670 
1671                         if (element_type)
1672                         {
1673                             std::vector<uint64_t> element_orders;
1674                             ParseChildArrayInfo(sc, die, first_index, element_orders, byte_stride, bit_stride);
1675                             if (byte_stride == 0 && bit_stride == 0)
1676                                 byte_stride = element_type->GetByteSize();
1677                             CompilerType array_element_type = element_type->GetForwardCompilerType ();
1678 
1679                             if (ClangASTContext::IsCXXClassType(array_element_type) && array_element_type.GetCompleteType() == false)
1680                             {
1681                                 ModuleSP module_sp = die.GetModule();
1682                                 if (module_sp)
1683                                 {
1684                                     if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang)
1685                                         module_sp->ReportError ("DWARF DW_TAG_array_type DIE at 0x%8.8x has a class/union/struct element type DIE 0x%8.8x that is a forward declaration, not a complete definition.\nTry compiling the source file with -fno-limit-debug-info or disable -gmodule",
1686                                                                 die.GetOffset(),
1687                                                                 type_die_ref.die_offset);
1688                                     else
1689                                         module_sp->ReportError ("DWARF DW_TAG_array_type DIE at 0x%8.8x has a class/union/struct element type DIE 0x%8.8x that is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
1690                                                                 die.GetOffset(),
1691                                                                 type_die_ref.die_offset,
1692                                                                 die.GetLLDBCompileUnit() ? die.GetLLDBCompileUnit()->GetPath().c_str() : "the source file");
1693                                 }
1694 
1695                                 // We have no choice other than to pretend that the element class type
1696                                 // is complete. If we don't do this, clang will crash when trying
1697                                 // to layout the class. Since we provide layout assistance, all
1698                                 // ivars in this class and other classes will be fine, this is
1699                                 // the best we can do short of crashing.
1700                                 ClangASTContext::StartTagDeclarationDefinition(array_element_type);
1701                                 ClangASTContext::CompleteTagDeclarationDefinition(array_element_type);
1702                             }
1703 
1704                             uint64_t array_element_bit_stride = byte_stride * 8 + bit_stride;
1705                             if (element_orders.size() > 0)
1706                             {
1707                                 uint64_t num_elements = 0;
1708                                 std::vector<uint64_t>::const_reverse_iterator pos;
1709                                 std::vector<uint64_t>::const_reverse_iterator end = element_orders.rend();
1710                                 for (pos = element_orders.rbegin(); pos != end; ++pos)
1711                                 {
1712                                     num_elements = *pos;
1713                                     clang_type = m_ast.CreateArrayType (array_element_type,
1714                                                                         num_elements,
1715                                                                         is_vector);
1716                                     array_element_type = clang_type;
1717                                     array_element_bit_stride = num_elements ?
1718                                     array_element_bit_stride * num_elements :
1719                                     array_element_bit_stride;
1720                                 }
1721                             }
1722                             else
1723                             {
1724                                 clang_type = m_ast.CreateArrayType (array_element_type, 0, is_vector);
1725                             }
1726                             ConstString empty_name;
1727                             type_sp.reset( new Type (die.GetID(),
1728                                                      dwarf,
1729                                                      empty_name,
1730                                                      array_element_bit_stride / 8,
1731                                                      NULL,
1732                                                      DIERef(type_die_form).GetUID(dwarf),
1733                                                      Type::eEncodingIsUID,
1734                                                      &decl,
1735                                                      clang_type,
1736                                                      Type::eResolveStateFull));
1737                             type_sp->SetEncodingType (element_type);
1738                         }
1739                     }
1740                 }
1741                     break;
1742 
1743                 case DW_TAG_ptr_to_member_type:
1744                 {
1745                     DWARFFormValue type_die_form;
1746                     DWARFFormValue containing_type_die_form;
1747 
1748                     const size_t num_attributes = die.GetAttributes (attributes);
1749 
1750                     if (num_attributes > 0) {
1751                         uint32_t i;
1752                         for (i=0; i<num_attributes; ++i)
1753                         {
1754                             attr = attributes.AttributeAtIndex(i);
1755                             if (attributes.ExtractFormValueAtIndex(i, form_value))
1756                             {
1757                                 switch (attr)
1758                                 {
1759                                     case DW_AT_type:
1760                                         type_die_form = form_value; break;
1761                                     case DW_AT_containing_type:
1762                                         containing_type_die_form = form_value; break;
1763                                 }
1764                             }
1765                         }
1766 
1767                         Type *pointee_type = dwarf->ResolveTypeUID(DIERef(type_die_form));
1768                         Type *class_type = dwarf->ResolveTypeUID(DIERef(containing_type_die_form));
1769 
1770                         CompilerType pointee_clang_type = pointee_type->GetForwardCompilerType ();
1771                         CompilerType class_clang_type = class_type->GetLayoutCompilerType ();
1772 
1773                         clang_type = ClangASTContext::CreateMemberPointerType(class_clang_type, pointee_clang_type);
1774 
1775                         byte_size = clang_type.GetByteSize(nullptr);
1776 
1777                         type_sp.reset(new Type(die.GetID(), dwarf, type_name_const_str, byte_size, NULL,
1778                                                LLDB_INVALID_UID, Type::eEncodingIsUID, NULL, clang_type,
1779                                                Type::eResolveStateForward));
1780                     }
1781 
1782                     break;
1783                 }
1784                 default:
1785                     dwarf->GetObjectFile()->GetModule()->ReportError ("{0x%8.8x}: unhandled type tag 0x%4.4x (%s), please file a bug and attach the file at the start of this error message",
1786                                                                       die.GetOffset(),
1787                                                                       tag,
1788                                                                       DW_TAG_value_to_name(tag));
1789                     break;
1790             }
1791 
1792             if (type_sp.get())
1793             {
1794                 DWARFDIE sc_parent_die = SymbolFileDWARF::GetParentSymbolContextDIE(die);
1795                 dw_tag_t sc_parent_tag = sc_parent_die.Tag();
1796 
1797                 SymbolContextScope * symbol_context_scope = NULL;
1798                 if (sc_parent_tag == DW_TAG_compile_unit)
1799                 {
1800                     symbol_context_scope = sc.comp_unit;
1801                 }
1802                 else if (sc.function != NULL && sc_parent_die)
1803                 {
1804                     symbol_context_scope = sc.function->GetBlock(true).FindBlockByID(sc_parent_die.GetID());
1805                     if (symbol_context_scope == NULL)
1806                         symbol_context_scope = sc.function;
1807                 }
1808 
1809                 if (symbol_context_scope != NULL)
1810                 {
1811                     type_sp->SetSymbolContextScope(symbol_context_scope);
1812                 }
1813 
1814                 // We are ready to put this type into the uniqued list up at the module level
1815                 type_list->Insert (type_sp);
1816 
1817                 dwarf->GetDIEToType()[die.GetDIE()] = type_sp.get();
1818             }
1819         }
1820         else if (type_ptr != DIE_IS_BEING_PARSED)
1821         {
1822             type_sp = type_ptr->shared_from_this();
1823         }
1824     }
1825     return type_sp;
1826 }
1827 
1828 // DWARF parsing functions
1829 
1830 class DWARFASTParserClang::DelayedAddObjCClassProperty
1831 {
1832 public:
1833     DelayedAddObjCClassProperty(const CompilerType     &class_opaque_type,
1834                                 const char             *property_name,
1835                                 const CompilerType     &property_opaque_type,  // The property type is only required if you don't have an ivar decl
1836                                 clang::ObjCIvarDecl    *ivar_decl,
1837                                 const char             *property_setter_name,
1838                                 const char             *property_getter_name,
1839                                 uint32_t                property_attributes,
1840                                 const ClangASTMetadata *metadata) :
1841     m_class_opaque_type     (class_opaque_type),
1842     m_property_name         (property_name),
1843     m_property_opaque_type  (property_opaque_type),
1844     m_ivar_decl             (ivar_decl),
1845     m_property_setter_name  (property_setter_name),
1846     m_property_getter_name  (property_getter_name),
1847     m_property_attributes   (property_attributes)
1848     {
1849         if (metadata != NULL)
1850         {
1851             m_metadata_ap.reset(new ClangASTMetadata());
1852             *m_metadata_ap = *metadata;
1853         }
1854     }
1855 
1856     DelayedAddObjCClassProperty (const DelayedAddObjCClassProperty &rhs)
1857     {
1858         *this = rhs;
1859     }
1860 
1861     DelayedAddObjCClassProperty& operator= (const DelayedAddObjCClassProperty &rhs)
1862     {
1863         m_class_opaque_type    = rhs.m_class_opaque_type;
1864         m_property_name        = rhs.m_property_name;
1865         m_property_opaque_type = rhs.m_property_opaque_type;
1866         m_ivar_decl            = rhs.m_ivar_decl;
1867         m_property_setter_name = rhs.m_property_setter_name;
1868         m_property_getter_name = rhs.m_property_getter_name;
1869         m_property_attributes  = rhs.m_property_attributes;
1870 
1871         if (rhs.m_metadata_ap.get())
1872         {
1873             m_metadata_ap.reset (new ClangASTMetadata());
1874             *m_metadata_ap = *rhs.m_metadata_ap;
1875         }
1876         return *this;
1877     }
1878 
1879     bool
1880     Finalize()
1881     {
1882         return ClangASTContext::AddObjCClassProperty (m_class_opaque_type,
1883                                                       m_property_name,
1884                                                       m_property_opaque_type,
1885                                                       m_ivar_decl,
1886                                                       m_property_setter_name,
1887                                                       m_property_getter_name,
1888                                                       m_property_attributes,
1889                                                       m_metadata_ap.get());
1890     }
1891 
1892 private:
1893     CompilerType            m_class_opaque_type;
1894     const char             *m_property_name;
1895     CompilerType            m_property_opaque_type;
1896     clang::ObjCIvarDecl    *m_ivar_decl;
1897     const char             *m_property_setter_name;
1898     const char             *m_property_getter_name;
1899     uint32_t                m_property_attributes;
1900     std::unique_ptr<ClangASTMetadata> m_metadata_ap;
1901 };
1902 
1903 bool
1904 DWARFASTParserClang::ParseTemplateDIE (const DWARFDIE &die,
1905                                        ClangASTContext::TemplateParameterInfos &template_param_infos)
1906 {
1907     const dw_tag_t tag = die.Tag();
1908 
1909     switch (tag)
1910     {
1911         case DW_TAG_template_type_parameter:
1912         case DW_TAG_template_value_parameter:
1913         {
1914             DWARFAttributes attributes;
1915             const size_t num_attributes = die.GetAttributes (attributes);
1916             const char *name = NULL;
1917             Type *lldb_type = NULL;
1918             CompilerType clang_type;
1919             uint64_t uval64 = 0;
1920             bool uval64_valid = false;
1921             if (num_attributes > 0)
1922             {
1923                 DWARFFormValue form_value;
1924                 for (size_t i=0; i<num_attributes; ++i)
1925                 {
1926                     const dw_attr_t attr = attributes.AttributeAtIndex(i);
1927 
1928                     switch (attr)
1929                     {
1930                         case DW_AT_name:
1931                             if (attributes.ExtractFormValueAtIndex(i, form_value))
1932                                 name = form_value.AsCString();
1933                             break;
1934 
1935                         case DW_AT_type:
1936                             if (attributes.ExtractFormValueAtIndex(i, form_value))
1937                             {
1938                                 lldb_type = die.ResolveTypeUID(DIERef(form_value));
1939                                 if (lldb_type)
1940                                     clang_type = lldb_type->GetForwardCompilerType ();
1941                             }
1942                             break;
1943 
1944                         case DW_AT_const_value:
1945                             if (attributes.ExtractFormValueAtIndex(i, form_value))
1946                             {
1947                                 uval64_valid = true;
1948                                 uval64 = form_value.Unsigned();
1949                             }
1950                             break;
1951                         default:
1952                             break;
1953                     }
1954                 }
1955 
1956                 clang::ASTContext *ast = m_ast.getASTContext();
1957                 if (!clang_type)
1958                     clang_type = m_ast.GetBasicType(eBasicTypeVoid);
1959 
1960                 if (clang_type)
1961                 {
1962                     bool is_signed = false;
1963                     if (name && name[0])
1964                         template_param_infos.names.push_back(name);
1965                     else
1966                         template_param_infos.names.push_back(NULL);
1967 
1968                     if (tag == DW_TAG_template_value_parameter &&
1969                         lldb_type != NULL &&
1970                         clang_type.IsIntegerType (is_signed) &&
1971                         uval64_valid)
1972                     {
1973                         llvm::APInt apint (lldb_type->GetByteSize() * 8, uval64, is_signed);
1974                         template_param_infos.args.push_back(
1975                             clang::TemplateArgument(*ast, llvm::APSInt(apint), ClangUtil::GetQualType(clang_type)));
1976                     }
1977                     else
1978                     {
1979                         template_param_infos.args.push_back(
1980                             clang::TemplateArgument(ClangUtil::GetQualType(clang_type)));
1981                     }
1982                 }
1983                 else
1984                 {
1985                     return false;
1986                 }
1987 
1988             }
1989         }
1990             return true;
1991 
1992         default:
1993             break;
1994     }
1995     return false;
1996 }
1997 
1998 bool
1999 DWARFASTParserClang::ParseTemplateParameterInfos (const DWARFDIE &parent_die,
2000                                                   ClangASTContext::TemplateParameterInfos &template_param_infos)
2001 {
2002 
2003     if (!parent_die)
2004         return false;
2005 
2006     Args template_parameter_names;
2007     for (DWARFDIE die = parent_die.GetFirstChild();
2008          die.IsValid();
2009          die = die.GetSibling())
2010     {
2011         const dw_tag_t tag = die.Tag();
2012 
2013         switch (tag)
2014         {
2015             case DW_TAG_template_type_parameter:
2016             case DW_TAG_template_value_parameter:
2017                 ParseTemplateDIE (die, template_param_infos);
2018                 break;
2019 
2020             default:
2021                 break;
2022         }
2023     }
2024     if (template_param_infos.args.empty())
2025         return false;
2026     return template_param_infos.args.size() == template_param_infos.names.size();
2027 }
2028 
2029 bool
2030 DWARFASTParserClang::CompleteTypeFromDWARF(const DWARFDIE &die, lldb_private::Type *type, CompilerType &clang_type)
2031 {
2032     SymbolFileDWARF *dwarf = die.GetDWARF();
2033 
2034     lldb_private::Mutex::Locker locker(dwarf->GetObjectFile()->GetModule()->GetMutex());
2035 
2036     // Disable external storage for this type so we don't get anymore
2037     // clang::ExternalASTSource queries for this type.
2038     m_ast.SetHasExternalStorage (clang_type.GetOpaqueQualType(), false);
2039 
2040     if (!die)
2041         return false;
2042 
2043     const dw_tag_t tag = die.Tag();
2044 
2045     Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO|DWARF_LOG_TYPE_COMPLETION));
2046     if (log)
2047         dwarf->GetObjectFile()->GetModule()->LogMessageVerboseBacktrace (log,
2048                                                                          "0x%8.8" PRIx64 ": %s '%s' resolving forward declaration...",
2049                                                                          die.GetID(),
2050                                                                          die.GetTagAsCString(),
2051                                                                          type->GetName().AsCString());
2052     assert (clang_type);
2053     DWARFAttributes attributes;
2054     switch (tag)
2055     {
2056         case DW_TAG_structure_type:
2057         case DW_TAG_union_type:
2058         case DW_TAG_class_type:
2059         {
2060             ClangASTImporter::LayoutInfo layout_info;
2061 
2062             {
2063                 if (die.HasChildren())
2064                 {
2065                     LanguageType class_language = eLanguageTypeUnknown;
2066                     if (ClangASTContext::IsObjCObjectOrInterfaceType(clang_type))
2067                     {
2068                         class_language = eLanguageTypeObjC;
2069                         // For objective C we don't start the definition when
2070                         // the class is created.
2071                         ClangASTContext::StartTagDeclarationDefinition (clang_type);
2072                     }
2073 
2074                     int tag_decl_kind = -1;
2075                     AccessType default_accessibility = eAccessNone;
2076                     if (tag == DW_TAG_structure_type)
2077                     {
2078                         tag_decl_kind = clang::TTK_Struct;
2079                         default_accessibility = eAccessPublic;
2080                     }
2081                     else if (tag == DW_TAG_union_type)
2082                     {
2083                         tag_decl_kind = clang::TTK_Union;
2084                         default_accessibility = eAccessPublic;
2085                     }
2086                     else if (tag == DW_TAG_class_type)
2087                     {
2088                         tag_decl_kind = clang::TTK_Class;
2089                         default_accessibility = eAccessPrivate;
2090                     }
2091 
2092                     SymbolContext sc(die.GetLLDBCompileUnit());
2093                     std::vector<clang::CXXBaseSpecifier *> base_classes;
2094                     std::vector<int> member_accessibilities;
2095                     bool is_a_class = false;
2096                     // Parse members and base classes first
2097                     DWARFDIECollection member_function_dies;
2098 
2099                     DelayedPropertyList delayed_properties;
2100                     ParseChildMembers (sc,
2101                                        die,
2102                                        clang_type,
2103                                        class_language,
2104                                        base_classes,
2105                                        member_accessibilities,
2106                                        member_function_dies,
2107                                        delayed_properties,
2108                                        default_accessibility,
2109                                        is_a_class,
2110                                        layout_info);
2111 
2112                     // Now parse any methods if there were any...
2113                     size_t num_functions = member_function_dies.Size();
2114                     if (num_functions > 0)
2115                     {
2116                         for (size_t i=0; i<num_functions; ++i)
2117                         {
2118                             dwarf->ResolveType(member_function_dies.GetDIEAtIndex(i));
2119                         }
2120                     }
2121 
2122                     if (class_language == eLanguageTypeObjC)
2123                     {
2124                         ConstString class_name (clang_type.GetTypeName());
2125                         if (class_name)
2126                         {
2127                             DIEArray method_die_offsets;
2128                             dwarf->GetObjCMethodDIEOffsets(class_name, method_die_offsets);
2129 
2130                             if (!method_die_offsets.empty())
2131                             {
2132                                 DWARFDebugInfo* debug_info = dwarf->DebugInfo();
2133 
2134                                 const size_t num_matches = method_die_offsets.size();
2135                                 for (size_t i=0; i<num_matches; ++i)
2136                                 {
2137                                     const DIERef& die_ref = method_die_offsets[i];
2138                                     DWARFDIE method_die = debug_info->GetDIE (die_ref);
2139 
2140                                     if (method_die)
2141                                         method_die.ResolveType ();
2142                                 }
2143                             }
2144 
2145                             for (DelayedPropertyList::iterator pi = delayed_properties.begin(), pe = delayed_properties.end();
2146                                  pi != pe;
2147                                  ++pi)
2148                                 pi->Finalize();
2149                         }
2150                     }
2151 
2152                     // If we have a DW_TAG_structure_type instead of a DW_TAG_class_type we
2153                     // need to tell the clang type it is actually a class.
2154                     if (class_language != eLanguageTypeObjC)
2155                     {
2156                         if (is_a_class && tag_decl_kind != clang::TTK_Class)
2157                             m_ast.SetTagTypeKind(ClangUtil::GetQualType(clang_type), clang::TTK_Class);
2158                     }
2159 
2160                     // Since DW_TAG_structure_type gets used for both classes
2161                     // and structures, we may need to set any DW_TAG_member
2162                     // fields to have a "private" access if none was specified.
2163                     // When we parsed the child members we tracked that actual
2164                     // accessibility value for each DW_TAG_member in the
2165                     // "member_accessibilities" array. If the value for the
2166                     // member is zero, then it was set to the "default_accessibility"
2167                     // which for structs was "public". Below we correct this
2168                     // by setting any fields to "private" that weren't correctly
2169                     // set.
2170                     if (is_a_class && !member_accessibilities.empty())
2171                     {
2172                         // This is a class and all members that didn't have
2173                         // their access specified are private.
2174                         m_ast.SetDefaultAccessForRecordFields (m_ast.GetAsRecordDecl(clang_type),
2175                                                                eAccessPrivate,
2176                                                                &member_accessibilities.front(),
2177                                                                member_accessibilities.size());
2178                     }
2179 
2180                     if (!base_classes.empty())
2181                     {
2182                         // Make sure all base classes refer to complete types and not
2183                         // forward declarations. If we don't do this, clang will crash
2184                         // with an assertion in the call to clang_type.SetBaseClassesForClassType()
2185                         for (auto &base_class : base_classes)
2186                         {
2187                             clang::TypeSourceInfo *type_source_info = base_class->getTypeSourceInfo();
2188                             if (type_source_info)
2189                             {
2190                                 CompilerType base_class_type (&m_ast, type_source_info->getType().getAsOpaquePtr());
2191                                 if (base_class_type.GetCompleteType() == false)
2192                                 {
2193                                     auto module = dwarf->GetObjectFile()->GetModule();
2194                                     module->ReportError (
2195                                         ":: Class '%s' has a base class '%s' which does not have a complete definition.",
2196                                         die.GetName(),
2197                                         base_class_type.GetTypeName().GetCString());
2198                                     if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang)
2199                                          module->ReportError (":: Try compiling the source file with -fno-limit-debug-info.");
2200 
2201                                     // We have no choice other than to pretend that the base class
2202                                     // is complete. If we don't do this, clang will crash when we
2203                                     // call setBases() inside of "clang_type.SetBaseClassesForClassType()"
2204                                     // below. Since we provide layout assistance, all ivars in this
2205                                     // class and other classes will be fine, this is the best we can do
2206                                     // short of crashing.
2207                                     ClangASTContext::StartTagDeclarationDefinition (base_class_type);
2208                                     ClangASTContext::CompleteTagDeclarationDefinition (base_class_type);
2209                                 }
2210                             }
2211                         }
2212                         m_ast.SetBaseClassesForClassType (clang_type.GetOpaqueQualType(),
2213                                                           &base_classes.front(),
2214                                                           base_classes.size());
2215 
2216                         // Clang will copy each CXXBaseSpecifier in "base_classes"
2217                         // so we have to free them all.
2218                         ClangASTContext::DeleteBaseClassSpecifiers (&base_classes.front(),
2219                                                                     base_classes.size());
2220                     }
2221                 }
2222             }
2223 
2224             ClangASTContext::BuildIndirectFields (clang_type);
2225             ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
2226 
2227             if (!layout_info.field_offsets.empty() ||
2228                 !layout_info.base_offsets.empty()  ||
2229                 !layout_info.vbase_offsets.empty() )
2230             {
2231                 if (type)
2232                     layout_info.bit_size = type->GetByteSize() * 8;
2233                 if (layout_info.bit_size == 0)
2234                     layout_info.bit_size = die.GetAttributeValueAsUnsigned(DW_AT_byte_size, 0) * 8;
2235 
2236                 clang::CXXRecordDecl *record_decl = m_ast.GetAsCXXRecordDecl(clang_type.GetOpaqueQualType());
2237                 if (record_decl)
2238                 {
2239                     if (log)
2240                     {
2241                         ModuleSP module_sp = dwarf->GetObjectFile()->GetModule();
2242 
2243                         if (module_sp)
2244                         {
2245                             module_sp->LogMessage (log,
2246                                                    "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) caching layout info for record_decl = %p, bit_size = %" PRIu64 ", alignment = %" PRIu64 ", field_offsets[%u], base_offsets[%u], vbase_offsets[%u])",
2247                                                    static_cast<void*>(clang_type.GetOpaqueQualType()),
2248                                                    static_cast<void*>(record_decl),
2249                                                    layout_info.bit_size,
2250                                                    layout_info.alignment,
2251                                                    static_cast<uint32_t>(layout_info.field_offsets.size()),
2252                                                    static_cast<uint32_t>(layout_info.base_offsets.size()),
2253                                                    static_cast<uint32_t>(layout_info.vbase_offsets.size()));
2254 
2255                             uint32_t idx;
2256                             {
2257                                 llvm::DenseMap<const clang::FieldDecl *, uint64_t>::const_iterator pos,
2258                                 end = layout_info.field_offsets.end();
2259                                 for (idx = 0, pos = layout_info.field_offsets.begin(); pos != end; ++pos, ++idx)
2260                                 {
2261                                     module_sp->LogMessage(log,
2262                                                           "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) field[%u] = { bit_offset=%u, name='%s' }",
2263                                                           static_cast<void *>(clang_type.GetOpaqueQualType()),
2264                                                           idx,
2265                                                           static_cast<uint32_t>(pos->second),
2266                                                           pos->first->getNameAsString().c_str());
2267                                 }
2268                             }
2269 
2270                             {
2271                                 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator base_pos,
2272                                 base_end = layout_info.base_offsets.end();
2273                                 for (idx = 0, base_pos = layout_info.base_offsets.begin(); base_pos != base_end; ++base_pos, ++idx)
2274                                 {
2275                                     module_sp->LogMessage(log,
2276                                                           "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) base[%u] = { byte_offset=%u, name='%s' }",
2277                                                           clang_type.GetOpaqueQualType(), idx, (uint32_t)base_pos->second.getQuantity(),
2278                                                           base_pos->first->getNameAsString().c_str());
2279                                 }
2280                             }
2281                             {
2282                                 llvm::DenseMap<const clang::CXXRecordDecl *, clang::CharUnits>::const_iterator vbase_pos,
2283                                 vbase_end = layout_info.vbase_offsets.end();
2284                                 for (idx = 0, vbase_pos = layout_info.vbase_offsets.begin(); vbase_pos != vbase_end; ++vbase_pos, ++idx)
2285                                 {
2286                                     module_sp->LogMessage(log,
2287                                                           "ClangASTContext::CompleteTypeFromDWARF (clang_type = %p) vbase[%u] = { byte_offset=%u, name='%s' }",
2288                                                           static_cast<void *>(clang_type.GetOpaqueQualType()), idx,
2289                                                           static_cast<uint32_t>(vbase_pos->second.getQuantity()),
2290                                                           vbase_pos->first->getNameAsString().c_str());
2291                                 }
2292                             }
2293 
2294                         }
2295                     }
2296                     GetClangASTImporter().InsertRecordDecl(record_decl, layout_info);
2297                 }
2298             }
2299         }
2300 
2301             return (bool)clang_type;
2302 
2303         case DW_TAG_enumeration_type:
2304             ClangASTContext::StartTagDeclarationDefinition (clang_type);
2305             if (die.HasChildren())
2306             {
2307                 SymbolContext sc(die.GetLLDBCompileUnit());
2308                 bool is_signed = false;
2309                 clang_type.IsIntegerType(is_signed);
2310                 ParseChildEnumerators(sc, clang_type, is_signed, type->GetByteSize(), die);
2311             }
2312             ClangASTContext::CompleteTagDeclarationDefinition (clang_type);
2313             return (bool)clang_type;
2314 
2315         default:
2316             assert(false && "not a forward clang type decl!");
2317             break;
2318     }
2319 
2320     return false;
2321 }
2322 
2323 std::vector<DWARFDIE>
2324 DWARFASTParserClang::GetDIEForDeclContext(lldb_private::CompilerDeclContext decl_context)
2325 {
2326     std::vector<DWARFDIE> result;
2327     for (auto it = m_decl_ctx_to_die.find((clang::DeclContext *)decl_context.GetOpaqueDeclContext()); it != m_decl_ctx_to_die.end(); it++)
2328         result.push_back(it->second);
2329     return result;
2330 }
2331 
2332 CompilerDecl
2333 DWARFASTParserClang::GetDeclForUIDFromDWARF (const DWARFDIE &die)
2334 {
2335     clang::Decl *clang_decl = GetClangDeclForDIE(die);
2336     if (clang_decl != nullptr)
2337         return CompilerDecl(&m_ast, clang_decl);
2338     return CompilerDecl();
2339 }
2340 
2341 CompilerDeclContext
2342 DWARFASTParserClang::GetDeclContextForUIDFromDWARF (const DWARFDIE &die)
2343 {
2344     clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (die);
2345     if (clang_decl_ctx)
2346         return CompilerDeclContext(&m_ast, clang_decl_ctx);
2347     return CompilerDeclContext();
2348 }
2349 
2350 CompilerDeclContext
2351 DWARFASTParserClang::GetDeclContextContainingUIDFromDWARF (const DWARFDIE &die)
2352 {
2353     clang::DeclContext *clang_decl_ctx = GetClangDeclContextContainingDIE (die, nullptr);
2354     if (clang_decl_ctx)
2355         return CompilerDeclContext(&m_ast, clang_decl_ctx);
2356     return CompilerDeclContext();
2357 }
2358 
2359 size_t
2360 DWARFASTParserClang::ParseChildEnumerators (const SymbolContext& sc,
2361                                             lldb_private::CompilerType &clang_type,
2362                                             bool is_signed,
2363                                             uint32_t enumerator_byte_size,
2364                                             const DWARFDIE &parent_die)
2365 {
2366     if (!parent_die)
2367         return 0;
2368 
2369     size_t enumerators_added = 0;
2370 
2371     for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
2372     {
2373         const dw_tag_t tag = die.Tag();
2374         if (tag == DW_TAG_enumerator)
2375         {
2376             DWARFAttributes attributes;
2377             const size_t num_child_attributes = die.GetAttributes(attributes);
2378             if (num_child_attributes > 0)
2379             {
2380                 const char *name = NULL;
2381                 bool got_value = false;
2382                 int64_t enum_value = 0;
2383                 Declaration decl;
2384 
2385                 uint32_t i;
2386                 for (i=0; i<num_child_attributes; ++i)
2387                 {
2388                     const dw_attr_t attr = attributes.AttributeAtIndex(i);
2389                     DWARFFormValue form_value;
2390                     if (attributes.ExtractFormValueAtIndex(i, form_value))
2391                     {
2392                         switch (attr)
2393                         {
2394                             case DW_AT_const_value:
2395                                 got_value = true;
2396                                 if (is_signed)
2397                                     enum_value = form_value.Signed();
2398                                 else
2399                                     enum_value = form_value.Unsigned();
2400                                 break;
2401 
2402                             case DW_AT_name:
2403                                 name = form_value.AsCString();
2404                                 break;
2405 
2406                             case DW_AT_description:
2407                             default:
2408                             case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2409                             case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
2410                             case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2411                             case DW_AT_sibling:
2412                                 break;
2413                         }
2414                     }
2415                 }
2416 
2417                 if (name && name[0] && got_value)
2418                 {
2419                     m_ast.AddEnumerationValueToEnumerationType (clang_type.GetOpaqueQualType(),
2420                                                                 m_ast.GetEnumerationIntegerType(clang_type.GetOpaqueQualType()),
2421                                                                 decl,
2422                                                                 name,
2423                                                                 enum_value,
2424                                                                 enumerator_byte_size * 8);
2425                     ++enumerators_added;
2426                 }
2427             }
2428         }
2429     }
2430     return enumerators_added;
2431 }
2432 
2433 #if defined(LLDB_CONFIGURATION_DEBUG) || defined(LLDB_CONFIGURATION_RELEASE)
2434 
2435 class DIEStack
2436 {
2437 public:
2438 
2439     void Push (const DWARFDIE &die)
2440     {
2441         m_dies.push_back (die);
2442     }
2443 
2444 
2445     void LogDIEs (Log *log)
2446     {
2447         StreamString log_strm;
2448         const size_t n = m_dies.size();
2449         log_strm.Printf("DIEStack[%" PRIu64 "]:\n", (uint64_t)n);
2450         for (size_t i=0; i<n; i++)
2451         {
2452             std::string qualified_name;
2453             const DWARFDIE &die = m_dies[i];
2454             die.GetQualifiedName(qualified_name);
2455             log_strm.Printf ("[%" PRIu64 "] 0x%8.8x: %s name='%s'\n",
2456                              (uint64_t)i,
2457                              die.GetOffset(),
2458                              die.GetTagAsCString(),
2459                              qualified_name.c_str());
2460         }
2461         log->PutCString(log_strm.GetData());
2462     }
2463     void Pop ()
2464     {
2465         m_dies.pop_back();
2466     }
2467 
2468     class ScopedPopper
2469     {
2470     public:
2471         ScopedPopper (DIEStack &die_stack) :
2472         m_die_stack (die_stack),
2473         m_valid (false)
2474         {
2475         }
2476 
2477         void
2478         Push (const DWARFDIE &die)
2479         {
2480             m_valid = true;
2481             m_die_stack.Push (die);
2482         }
2483 
2484         ~ScopedPopper ()
2485         {
2486             if (m_valid)
2487                 m_die_stack.Pop();
2488         }
2489 
2490 
2491 
2492     protected:
2493         DIEStack &m_die_stack;
2494         bool m_valid;
2495     };
2496 
2497 protected:
2498     typedef std::vector<DWARFDIE> Stack;
2499     Stack m_dies;
2500 };
2501 #endif
2502 
2503 Function *
2504 DWARFASTParserClang::ParseFunctionFromDWARF (const SymbolContext& sc,
2505                                              const DWARFDIE &die)
2506 {
2507     DWARFRangeList func_ranges;
2508     const char *name = NULL;
2509     const char *mangled = NULL;
2510     int decl_file = 0;
2511     int decl_line = 0;
2512     int decl_column = 0;
2513     int call_file = 0;
2514     int call_line = 0;
2515     int call_column = 0;
2516     DWARFExpression frame_base(die.GetCU());
2517 
2518     const dw_tag_t tag = die.Tag();
2519 
2520     if (tag != DW_TAG_subprogram)
2521         return NULL;
2522 
2523     if (die.GetDIENamesAndRanges (name,
2524                                   mangled,
2525                                   func_ranges,
2526                                   decl_file,
2527                                   decl_line,
2528                                   decl_column,
2529                                   call_file,
2530                                   call_line,
2531                                   call_column,
2532                                   &frame_base))
2533     {
2534 
2535         // Union of all ranges in the function DIE (if the function is discontiguous)
2536         AddressRange func_range;
2537         lldb::addr_t lowest_func_addr = func_ranges.GetMinRangeBase (0);
2538         lldb::addr_t highest_func_addr = func_ranges.GetMaxRangeEnd (0);
2539         if (lowest_func_addr != LLDB_INVALID_ADDRESS && lowest_func_addr <= highest_func_addr)
2540         {
2541             ModuleSP module_sp (die.GetModule());
2542             func_range.GetBaseAddress().ResolveAddressUsingFileSections (lowest_func_addr, module_sp->GetSectionList());
2543             if (func_range.GetBaseAddress().IsValid())
2544                 func_range.SetByteSize(highest_func_addr - lowest_func_addr);
2545         }
2546 
2547         if (func_range.GetBaseAddress().IsValid())
2548         {
2549             Mangled func_name;
2550             if (mangled)
2551                 func_name.SetValue(ConstString(mangled), true);
2552             else if (die.GetParent().Tag() == DW_TAG_compile_unit &&
2553                      Language::LanguageIsCPlusPlus(die.GetLanguage()) &&
2554                      name && strcmp(name, "main") != 0)
2555             {
2556                 // If the mangled name is not present in the DWARF, generate the demangled name
2557                 // using the decl context. We skip if the function is "main" as its name is
2558                 // never mangled.
2559                 bool is_static = false;
2560                 bool is_variadic = false;
2561                 bool has_template_params = false;
2562                 unsigned type_quals = 0;
2563                 std::vector<CompilerType> param_types;
2564                 std::vector<clang::ParmVarDecl*> param_decls;
2565                 DWARFDeclContext decl_ctx;
2566                 StreamString sstr;
2567 
2568                 die.GetDWARFDeclContext(decl_ctx);
2569                 sstr << decl_ctx.GetQualifiedName();
2570 
2571                 clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE(die, nullptr);
2572                 ParseChildParameters(sc,
2573                                      containing_decl_ctx,
2574                                      die,
2575                                      true,
2576                                      is_static,
2577                                      is_variadic,
2578                                      has_template_params,
2579                                      param_types,
2580                                      param_decls,
2581                                      type_quals);
2582                 sstr << "(";
2583                 for (size_t i = 0; i < param_types.size(); i++)
2584                 {
2585                     if (i > 0)
2586                         sstr << ", ";
2587                     sstr << param_types[i].GetTypeName();
2588                 }
2589                 if (is_variadic)
2590                     sstr << ", ...";
2591                 sstr << ")";
2592                 if (type_quals & clang::Qualifiers::Const)
2593                     sstr << " const";
2594 
2595                 func_name.SetValue(ConstString(sstr.GetData()), false);
2596             }
2597             else
2598                 func_name.SetValue(ConstString(name), false);
2599 
2600             FunctionSP func_sp;
2601             std::unique_ptr<Declaration> decl_ap;
2602             if (decl_file != 0 || decl_line != 0 || decl_column != 0)
2603                 decl_ap.reset(new Declaration (sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(decl_file),
2604                                                decl_line,
2605                                                decl_column));
2606 
2607             SymbolFileDWARF *dwarf = die.GetDWARF();
2608             // Supply the type _only_ if it has already been parsed
2609             Type *func_type = dwarf->GetDIEToType().lookup (die.GetDIE());
2610 
2611             assert(func_type == NULL || func_type != DIE_IS_BEING_PARSED);
2612 
2613             if (dwarf->FixupAddress (func_range.GetBaseAddress()))
2614             {
2615                 const user_id_t func_user_id = die.GetID();
2616                 func_sp.reset(new Function (sc.comp_unit,
2617                                             func_user_id,       // UserID is the DIE offset
2618                                             func_user_id,
2619                                             func_name,
2620                                             func_type,
2621                                             func_range));           // first address range
2622 
2623                 if (func_sp.get() != NULL)
2624                 {
2625                     if (frame_base.IsValid())
2626                         func_sp->GetFrameBaseExpression() = frame_base;
2627                     sc.comp_unit->AddFunction(func_sp);
2628                     return func_sp.get();
2629                 }
2630             }
2631         }
2632     }
2633     return NULL;
2634 }
2635 
2636 bool
2637 DWARFASTParserClang::ParseChildMembers(const SymbolContext &sc, const DWARFDIE &parent_die,
2638                                        CompilerType &class_clang_type, const LanguageType class_language,
2639                                        std::vector<clang::CXXBaseSpecifier *> &base_classes,
2640                                        std::vector<int> &member_accessibilities,
2641                                        DWARFDIECollection &member_function_dies,
2642                                        DelayedPropertyList &delayed_properties, AccessType &default_accessibility,
2643                                        bool &is_a_class, ClangASTImporter::LayoutInfo &layout_info)
2644 {
2645     if (!parent_die)
2646         return 0;
2647 
2648     // Get the parent byte size so we can verify any members will fit
2649     const uint64_t parent_byte_size = parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX) * 8;
2650     const uint64_t parent_bit_size = parent_byte_size == UINT64_MAX ? UINT64_MAX : parent_byte_size * 8;
2651 
2652     uint32_t member_idx = 0;
2653     BitfieldInfo last_field_info;
2654 
2655     ModuleSP module_sp = parent_die.GetDWARF()->GetObjectFile()->GetModule();
2656     ClangASTContext *ast = llvm::dyn_cast_or_null<ClangASTContext>(class_clang_type.GetTypeSystem());
2657     if (ast == nullptr)
2658         return 0;
2659 
2660     for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
2661     {
2662         dw_tag_t tag = die.Tag();
2663 
2664         switch (tag)
2665         {
2666             case DW_TAG_member:
2667             case DW_TAG_APPLE_property:
2668             {
2669                 DWARFAttributes attributes;
2670                 const size_t num_attributes = die.GetAttributes (attributes);
2671                 if (num_attributes > 0)
2672                 {
2673                     Declaration decl;
2674                     //DWARFExpression location;
2675                     const char *name = NULL;
2676                     const char *prop_name = NULL;
2677                     const char *prop_getter_name = NULL;
2678                     const char *prop_setter_name = NULL;
2679                     uint32_t prop_attributes = 0;
2680 
2681 
2682                     bool is_artificial = false;
2683                     DWARFFormValue encoding_form;
2684                     AccessType accessibility = eAccessNone;
2685                     uint32_t member_byte_offset = (parent_die.Tag() == DW_TAG_union_type) ? 0 : UINT32_MAX;
2686                     size_t byte_size = 0;
2687                     int64_t bit_offset = 0;
2688                     size_t bit_size = 0;
2689                     bool is_external = false; // On DW_TAG_members, this means the member is static
2690                     uint32_t i;
2691                     for (i=0; i<num_attributes && !is_artificial; ++i)
2692                     {
2693                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
2694                         DWARFFormValue form_value;
2695                         if (attributes.ExtractFormValueAtIndex(i, form_value))
2696                         {
2697                             switch (attr)
2698                             {
2699                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
2700                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
2701                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
2702                                 case DW_AT_name:        name = form_value.AsCString(); break;
2703                                 case DW_AT_type:        encoding_form = form_value; break;
2704                                 case DW_AT_bit_offset:  bit_offset = form_value.Signed(); break;
2705                                 case DW_AT_bit_size:    bit_size = form_value.Unsigned(); break;
2706                                 case DW_AT_byte_size:   byte_size = form_value.Unsigned(); break;
2707                                 case DW_AT_data_member_location:
2708                                     if (form_value.BlockData())
2709                                     {
2710                                         Value initialValue(0);
2711                                         Value memberOffset(0);
2712                                         const DWARFDataExtractor& debug_info_data = die.GetDWARF()->get_debug_info_data();
2713                                         uint32_t block_length = form_value.Unsigned();
2714                                         uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
2715                                         if (DWARFExpression::Evaluate(nullptr, // ExecutionContext *
2716                                                                       nullptr, // ClangExpressionVariableList *
2717                                                                       nullptr, // ClangExpressionDeclMap *
2718                                                                       nullptr, // RegisterContext *
2719                                                                       module_sp,
2720                                                                       debug_info_data,
2721                                                                       die.GetCU(),
2722                                                                       block_offset,
2723                                                                       block_length,
2724                                                                       eRegisterKindDWARF,
2725                                                                       &initialValue,
2726                                                                       nullptr,
2727                                                                       memberOffset,
2728                                                                       nullptr))
2729                                         {
2730                                             member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
2731                                         }
2732                                     }
2733                                     else
2734                                     {
2735                                         // With DWARF 3 and later, if the value is an integer constant,
2736                                         // this form value is the offset in bytes from the beginning
2737                                         // of the containing entity.
2738                                         member_byte_offset = form_value.Unsigned();
2739                                     }
2740                                     break;
2741 
2742                                 case DW_AT_accessibility: accessibility = DW_ACCESS_to_AccessType (form_value.Unsigned()); break;
2743                                 case DW_AT_artificial: is_artificial = form_value.Boolean(); break;
2744                                 case DW_AT_APPLE_property_name:      prop_name = form_value.AsCString();
2745                                     break;
2746                                 case DW_AT_APPLE_property_getter:    prop_getter_name = form_value.AsCString();
2747                                     break;
2748                                 case DW_AT_APPLE_property_setter:    prop_setter_name = form_value.AsCString();
2749                                     break;
2750                                 case DW_AT_APPLE_property_attribute: prop_attributes = form_value.Unsigned(); break;
2751                                 case DW_AT_external:                 is_external = form_value.Boolean(); break;
2752 
2753                                 default:
2754                                 case DW_AT_declaration:
2755                                 case DW_AT_description:
2756                                 case DW_AT_mutable:
2757                                 case DW_AT_visibility:
2758                                 case DW_AT_sibling:
2759                                     break;
2760                             }
2761                         }
2762                     }
2763 
2764                     if (prop_name)
2765                     {
2766                         ConstString fixed_getter;
2767                         ConstString fixed_setter;
2768 
2769                         // Check if the property getter/setter were provided as full
2770                         // names.  We want basenames, so we extract them.
2771 
2772                         if (prop_getter_name && prop_getter_name[0] == '-')
2773                         {
2774                             ObjCLanguage::MethodName prop_getter_method(prop_getter_name, true);
2775                             prop_getter_name = prop_getter_method.GetSelector().GetCString();
2776                         }
2777 
2778                         if (prop_setter_name && prop_setter_name[0] == '-')
2779                         {
2780                             ObjCLanguage::MethodName prop_setter_method(prop_setter_name, true);
2781                             prop_setter_name = prop_setter_method.GetSelector().GetCString();
2782                         }
2783 
2784                         // If the names haven't been provided, they need to be
2785                         // filled in.
2786 
2787                         if (!prop_getter_name)
2788                         {
2789                             prop_getter_name = prop_name;
2790                         }
2791                         if (!prop_setter_name && prop_name[0] && !(prop_attributes & DW_APPLE_PROPERTY_readonly))
2792                         {
2793                             StreamString ss;
2794 
2795                             ss.Printf("set%c%s:",
2796                                       toupper(prop_name[0]),
2797                                       &prop_name[1]);
2798 
2799                             fixed_setter.SetCString(ss.GetData());
2800                             prop_setter_name = fixed_setter.GetCString();
2801                         }
2802                     }
2803 
2804                     // Clang has a DWARF generation bug where sometimes it
2805                     // represents fields that are references with bad byte size
2806                     // and bit size/offset information such as:
2807                     //
2808                     //  DW_AT_byte_size( 0x00 )
2809                     //  DW_AT_bit_size( 0x40 )
2810                     //  DW_AT_bit_offset( 0xffffffffffffffc0 )
2811                     //
2812                     // So check the bit offset to make sure it is sane, and if
2813                     // the values are not sane, remove them. If we don't do this
2814                     // then we will end up with a crash if we try to use this
2815                     // type in an expression when clang becomes unhappy with its
2816                     // recycled debug info.
2817 
2818                     if (byte_size == 0 && bit_offset < 0)
2819                     {
2820                         bit_size = 0;
2821                         bit_offset = 0;
2822                     }
2823 
2824                     // FIXME: Make Clang ignore Objective-C accessibility for expressions
2825                     if (class_language == eLanguageTypeObjC ||
2826                         class_language == eLanguageTypeObjC_plus_plus)
2827                         accessibility = eAccessNone;
2828 
2829                     if (member_idx == 0 && !is_artificial && name && (strstr (name, "_vptr$") == name))
2830                     {
2831                         // Not all compilers will mark the vtable pointer
2832                         // member as artificial (llvm-gcc). We can't have
2833                         // the virtual members in our classes otherwise it
2834                         // throws off all child offsets since we end up
2835                         // having and extra pointer sized member in our
2836                         // class layouts.
2837                         is_artificial = true;
2838                     }
2839 
2840                     // Handle static members
2841                     if (is_external && member_byte_offset == UINT32_MAX)
2842                     {
2843                         Type *var_type = die.ResolveTypeUID(DIERef(encoding_form));
2844 
2845                         if (var_type)
2846                         {
2847                             if (accessibility == eAccessNone)
2848                                 accessibility = eAccessPublic;
2849                             ClangASTContext::AddVariableToRecordType (class_clang_type,
2850                                                                       name,
2851                                                                       var_type->GetLayoutCompilerType (),
2852                                                                       accessibility);
2853                         }
2854                         break;
2855                     }
2856 
2857                     if (is_artificial == false)
2858                     {
2859                         Type *member_type = die.ResolveTypeUID(DIERef(encoding_form));
2860 
2861                         clang::FieldDecl *field_decl = NULL;
2862                         if (tag == DW_TAG_member)
2863                         {
2864                             if (member_type)
2865                             {
2866                                 if (accessibility == eAccessNone)
2867                                     accessibility = default_accessibility;
2868                                 member_accessibilities.push_back(accessibility);
2869 
2870                                 uint64_t field_bit_offset = (member_byte_offset == UINT32_MAX ? 0 : (member_byte_offset * 8));
2871                                 if (bit_size > 0)
2872                                 {
2873 
2874                                     BitfieldInfo this_field_info;
2875                                     this_field_info.bit_offset = field_bit_offset;
2876                                     this_field_info.bit_size = bit_size;
2877 
2878                                     /////////////////////////////////////////////////////////////
2879                                     // How to locate a field given the DWARF debug information
2880                                     //
2881                                     // AT_byte_size indicates the size of the word in which the
2882                                     // bit offset must be interpreted.
2883                                     //
2884                                     // AT_data_member_location indicates the byte offset of the
2885                                     // word from the base address of the structure.
2886                                     //
2887                                     // AT_bit_offset indicates how many bits into the word
2888                                     // (according to the host endianness) the low-order bit of
2889                                     // the field starts.  AT_bit_offset can be negative.
2890                                     //
2891                                     // AT_bit_size indicates the size of the field in bits.
2892                                     /////////////////////////////////////////////////////////////
2893 
2894                                     if (byte_size == 0)
2895                                         byte_size = member_type->GetByteSize();
2896 
2897                                     ObjectFile *objfile = die.GetDWARF()->GetObjectFile();
2898                                     if (objfile->GetByteOrder() == eByteOrderLittle)
2899                                     {
2900                                         this_field_info.bit_offset += byte_size * 8;
2901                                         this_field_info.bit_offset -= (bit_offset + bit_size);
2902 
2903                                         if (this_field_info.bit_offset >= parent_bit_size)
2904                                         {
2905                                             objfile->GetModule()->ReportWarning("0x%8.8" PRIx64 ": %s bitfield named \"%s\" has invalid bit offset (0x%8.8" PRIx64 ") member will be ignored. Please file a bug against the compiler and include the preprocessed output for %s\n",
2906                                                                                 die.GetID(),
2907                                                                                 DW_TAG_value_to_name(tag),
2908                                                                                 name,
2909                                                                                 this_field_info.bit_offset,
2910                                                                                 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
2911                                             this_field_info.Clear();
2912                                             continue;
2913                                         }
2914                                     }
2915                                     else
2916                                     {
2917                                         this_field_info.bit_offset += bit_offset;
2918                                     }
2919 
2920                                     // Update the field bit offset we will report for layout
2921                                     field_bit_offset = this_field_info.bit_offset;
2922 
2923                                     // If the member to be emitted did not start on a character boundary and there is
2924                                     // empty space between the last field and this one, then we need to emit an
2925                                     // anonymous member filling up the space up to its start.  There are three cases
2926                                     // here:
2927                                     //
2928                                     // 1 If the previous member ended on a character boundary, then we can emit an
2929                                     //   anonymous member starting at the most recent character boundary.
2930                                     //
2931                                     // 2 If the previous member did not end on a character boundary and the distance
2932                                     //   from the end of the previous member to the current member is less than a
2933                                     //   word width, then we can emit an anonymous member starting right after the
2934                                     //   previous member and right before this member.
2935                                     //
2936                                     // 3 If the previous member did not end on a character boundary and the distance
2937                                     //   from the end of the previous member to the current member is greater than
2938                                     //   or equal a word width, then we act as in Case 1.
2939 
2940                                     const uint64_t character_width = 8;
2941                                     const uint64_t word_width = 32;
2942 
2943                                     // Objective-C has invalid DW_AT_bit_offset values in older versions
2944                                     // of clang, so we have to be careful and only insert unnamed bitfields
2945                                     // if we have a new enough clang.
2946                                     bool detect_unnamed_bitfields = true;
2947 
2948                                     if (class_language == eLanguageTypeObjC || class_language == eLanguageTypeObjC_plus_plus)
2949                                         detect_unnamed_bitfields = die.GetCU()->Supports_unnamed_objc_bitfields ();
2950 
2951                                     if (detect_unnamed_bitfields)
2952                                     {
2953                                         BitfieldInfo anon_field_info;
2954 
2955                                         if ((this_field_info.bit_offset % character_width) != 0) // not char aligned
2956                                         {
2957                                             uint64_t last_field_end = 0;
2958 
2959                                             if (last_field_info.IsValid())
2960                                                 last_field_end = last_field_info.bit_offset + last_field_info.bit_size;
2961 
2962                                             if (this_field_info.bit_offset != last_field_end)
2963                                             {
2964                                                 if (((last_field_end % character_width) == 0) ||                    // case 1
2965                                                     (this_field_info.bit_offset - last_field_end >= word_width))    // case 3
2966                                                 {
2967                                                     anon_field_info.bit_size = this_field_info.bit_offset % character_width;
2968                                                     anon_field_info.bit_offset = this_field_info.bit_offset - anon_field_info.bit_size;
2969                                                 }
2970                                                 else                                                                // case 2
2971                                                 {
2972                                                     anon_field_info.bit_size = this_field_info.bit_offset - last_field_end;
2973                                                     anon_field_info.bit_offset = last_field_end;
2974                                                 }
2975                                             }
2976                                         }
2977 
2978                                         if (anon_field_info.IsValid())
2979                                         {
2980                                             clang::FieldDecl *unnamed_bitfield_decl =
2981                                             ClangASTContext::AddFieldToRecordType (class_clang_type,
2982                                                                                    NULL,
2983                                                                                    m_ast.GetBuiltinTypeForEncodingAndBitSize(eEncodingSint, word_width),
2984                                                                                    accessibility,
2985                                                                                    anon_field_info.bit_size);
2986 
2987                                             layout_info.field_offsets.insert(
2988                                                                              std::make_pair(unnamed_bitfield_decl, anon_field_info.bit_offset));
2989                                         }
2990                                     }
2991                                     last_field_info = this_field_info;
2992                                 }
2993                                 else
2994                                 {
2995                                     last_field_info.Clear();
2996                                 }
2997 
2998                                 CompilerType member_clang_type = member_type->GetLayoutCompilerType ();
2999                                 if (!member_clang_type.IsCompleteType())
3000                                     member_clang_type.GetCompleteType();
3001 
3002                                 {
3003                                     // Older versions of clang emit array[0] and array[1] in the same way (<rdar://problem/12566646>).
3004                                     // If the current field is at the end of the structure, then there is definitely no room for extra
3005                                     // elements and we override the type to array[0].
3006 
3007                                     CompilerType member_array_element_type;
3008                                     uint64_t member_array_size;
3009                                     bool member_array_is_incomplete;
3010 
3011                                     if (member_clang_type.IsArrayType(&member_array_element_type,
3012                                                                       &member_array_size,
3013                                                                       &member_array_is_incomplete) &&
3014                                         !member_array_is_incomplete)
3015                                     {
3016                                         uint64_t parent_byte_size = parent_die.GetAttributeValueAsUnsigned(DW_AT_byte_size, UINT64_MAX);
3017 
3018                                         if (member_byte_offset >= parent_byte_size)
3019                                         {
3020                                             if (member_array_size != 1 && (member_array_size != 0 || member_byte_offset > parent_byte_size))
3021                                             {
3022                                                 module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which extends beyond the bounds of 0x%8.8" PRIx64,
3023                                                                         die.GetID(),
3024                                                                         name,
3025                                                                         encoding_form.Reference(),
3026                                                                         parent_die.GetID());
3027                                             }
3028 
3029                                             member_clang_type = m_ast.CreateArrayType(member_array_element_type, 0, false);
3030                                         }
3031                                     }
3032                                 }
3033 
3034                                 if (ClangASTContext::IsCXXClassType(member_clang_type) && member_clang_type.GetCompleteType() == false)
3035                                 {
3036                                     if (die.GetCU()->GetProducer() == DWARFCompileUnit::eProducerClang)
3037                                         module_sp->ReportError ("DWARF DIE at 0x%8.8x (class %s) has a member variable 0x%8.8x (%s) whose type is a forward declaration, not a complete definition.\nTry compiling the source file with -fno-limit-debug-info",
3038                                                                 parent_die.GetOffset(),
3039                                                                 parent_die.GetName(),
3040                                                                 die.GetOffset(),
3041                                                                 name);
3042                                     else
3043                                         module_sp->ReportError ("DWARF DIE at 0x%8.8x (class %s) has a member variable 0x%8.8x (%s) whose type is a forward declaration, not a complete definition.\nPlease file a bug against the compiler and include the preprocessed output for %s",
3044                                                                 parent_die.GetOffset(),
3045                                                                 parent_die.GetName(),
3046                                                                 die.GetOffset(),
3047                                                                 name,
3048                                                                 sc.comp_unit ? sc.comp_unit->GetPath().c_str() : "the source file");
3049                                     // We have no choice other than to pretend that the member class
3050                                     // is complete. If we don't do this, clang will crash when trying
3051                                     // to layout the class. Since we provide layout assistance, all
3052                                     // ivars in this class and other classes will be fine, this is
3053                                     // the best we can do short of crashing.
3054                                     ClangASTContext::StartTagDeclarationDefinition(member_clang_type);
3055                                     ClangASTContext::CompleteTagDeclarationDefinition(member_clang_type);
3056                                 }
3057 
3058                                 field_decl = ClangASTContext::AddFieldToRecordType (class_clang_type,
3059                                                                                     name,
3060                                                                                     member_clang_type,
3061                                                                                     accessibility,
3062                                                                                     bit_size);
3063 
3064                                 m_ast.SetMetadataAsUserID (field_decl, die.GetID());
3065 
3066                                 layout_info.field_offsets.insert(std::make_pair(field_decl, field_bit_offset));
3067                             }
3068                             else
3069                             {
3070                                 if (name)
3071                                     module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member '%s' refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
3072                                                             die.GetID(),
3073                                                             name,
3074                                                             encoding_form.Reference());
3075                                 else
3076                                     module_sp->ReportError ("0x%8.8" PRIx64 ": DW_TAG_member refers to type 0x%8.8" PRIx64 " which was unable to be parsed",
3077                                                             die.GetID(),
3078                                                             encoding_form.Reference());
3079                             }
3080                         }
3081 
3082                         if (prop_name != NULL && member_type)
3083                         {
3084                             clang::ObjCIvarDecl *ivar_decl = NULL;
3085 
3086                             if (field_decl)
3087                             {
3088                                 ivar_decl = clang::dyn_cast<clang::ObjCIvarDecl>(field_decl);
3089                                 assert (ivar_decl != NULL);
3090                             }
3091 
3092                             ClangASTMetadata metadata;
3093                             metadata.SetUserID (die.GetID());
3094                             delayed_properties.push_back(DelayedAddObjCClassProperty(class_clang_type,
3095                                                                                      prop_name,
3096                                                                                      member_type->GetLayoutCompilerType (),
3097                                                                                      ivar_decl,
3098                                                                                      prop_setter_name,
3099                                                                                      prop_getter_name,
3100                                                                                      prop_attributes,
3101                                                                                      &metadata));
3102 
3103                             if (ivar_decl)
3104                                 m_ast.SetMetadataAsUserID (ivar_decl, die.GetID());
3105                         }
3106                     }
3107                 }
3108                 ++member_idx;
3109             }
3110                 break;
3111 
3112             case DW_TAG_subprogram:
3113                 // Let the type parsing code handle this one for us.
3114                 member_function_dies.Append (die);
3115                 break;
3116 
3117             case DW_TAG_inheritance:
3118             {
3119                 is_a_class = true;
3120                 if (default_accessibility == eAccessNone)
3121                     default_accessibility = eAccessPrivate;
3122                 // TODO: implement DW_TAG_inheritance type parsing
3123                 DWARFAttributes attributes;
3124                 const size_t num_attributes = die.GetAttributes (attributes);
3125                 if (num_attributes > 0)
3126                 {
3127                     Declaration decl;
3128                     DWARFExpression location(die.GetCU());
3129                     DWARFFormValue encoding_form;
3130                     AccessType accessibility = default_accessibility;
3131                     bool is_virtual = false;
3132                     bool is_base_of_class = true;
3133                     off_t member_byte_offset = 0;
3134                     uint32_t i;
3135                     for (i=0; i<num_attributes; ++i)
3136                     {
3137                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
3138                         DWARFFormValue form_value;
3139                         if (attributes.ExtractFormValueAtIndex(i, form_value))
3140                         {
3141                             switch (attr)
3142                             {
3143                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3144                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3145                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3146                                 case DW_AT_type:        encoding_form = form_value; break;
3147                                 case DW_AT_data_member_location:
3148                                     if (form_value.BlockData())
3149                                     {
3150                                         Value initialValue(0);
3151                                         Value memberOffset(0);
3152                                         const DWARFDataExtractor& debug_info_data = die.GetDWARF()->get_debug_info_data();
3153                                         uint32_t block_length = form_value.Unsigned();
3154                                         uint32_t block_offset = form_value.BlockData() - debug_info_data.GetDataStart();
3155                                         if (DWARFExpression::Evaluate (nullptr,
3156                                                                        nullptr,
3157                                                                        nullptr,
3158                                                                        nullptr,
3159                                                                        module_sp,
3160                                                                        debug_info_data,
3161                                                                        die.GetCU(),
3162                                                                        block_offset,
3163                                                                        block_length,
3164                                                                        eRegisterKindDWARF,
3165                                                                        &initialValue,
3166                                                                        nullptr,
3167                                                                        memberOffset,
3168                                                                        nullptr))
3169                                         {
3170                                             member_byte_offset = memberOffset.ResolveValue(NULL).UInt();
3171                                         }
3172                                     }
3173                                     else
3174                                     {
3175                                         // With DWARF 3 and later, if the value is an integer constant,
3176                                         // this form value is the offset in bytes from the beginning
3177                                         // of the containing entity.
3178                                         member_byte_offset = form_value.Unsigned();
3179                                     }
3180                                     break;
3181 
3182                                 case DW_AT_accessibility:
3183                                     accessibility = DW_ACCESS_to_AccessType(form_value.Unsigned());
3184                                     break;
3185 
3186                                 case DW_AT_virtuality:
3187                                     is_virtual = form_value.Boolean();
3188                                     break;
3189 
3190                                 case DW_AT_sibling:
3191                                     break;
3192 
3193                                 default:
3194                                     break;
3195                             }
3196                         }
3197                     }
3198 
3199                     Type *base_class_type = die.ResolveTypeUID(DIERef(encoding_form));
3200                     if (base_class_type == NULL)
3201                     {
3202                         module_sp->ReportError("0x%8.8x: DW_TAG_inheritance failed to resolve the base class at 0x%8.8" PRIx64 " from enclosing type 0x%8.8x. \nPlease file a bug and attach the file at the start of this error message",
3203                                                die.GetOffset(),
3204                                                encoding_form.Reference(),
3205                                                parent_die.GetOffset());
3206                         break;
3207                     }
3208 
3209                     CompilerType base_class_clang_type = base_class_type->GetFullCompilerType ();
3210                     assert (base_class_clang_type);
3211                     if (class_language == eLanguageTypeObjC)
3212                     {
3213                         ast->SetObjCSuperClass(class_clang_type, base_class_clang_type);
3214                     }
3215                     else
3216                     {
3217                         base_classes.push_back (ast->CreateBaseClassSpecifier (base_class_clang_type.GetOpaqueQualType(),
3218                                                                                accessibility,
3219                                                                                is_virtual,
3220                                                                                is_base_of_class));
3221 
3222                         if (is_virtual)
3223                         {
3224                             // Do not specify any offset for virtual inheritance. The DWARF produced by clang doesn't
3225                             // give us a constant offset, but gives us a DWARF expressions that requires an actual object
3226                             // in memory. the DW_AT_data_member_location for a virtual base class looks like:
3227                             //      DW_AT_data_member_location( DW_OP_dup, DW_OP_deref, DW_OP_constu(0x00000018), DW_OP_minus, DW_OP_deref, DW_OP_plus )
3228                             // Given this, there is really no valid response we can give to clang for virtual base
3229                             // class offsets, and this should eventually be removed from LayoutRecordType() in the external
3230                             // AST source in clang.
3231                         }
3232                         else
3233                         {
3234                             layout_info.base_offsets.insert(
3235                                                             std::make_pair(ast->GetAsCXXRecordDecl(base_class_clang_type.GetOpaqueQualType()),
3236                                                                            clang::CharUnits::fromQuantity(member_byte_offset)));
3237                         }
3238                     }
3239                 }
3240             }
3241                 break;
3242 
3243             default:
3244                 break;
3245         }
3246     }
3247 
3248     return true;
3249 }
3250 
3251 
3252 size_t
3253 DWARFASTParserClang::ParseChildParameters (const SymbolContext& sc,
3254                                            clang::DeclContext *containing_decl_ctx,
3255                                            const DWARFDIE &parent_die,
3256                                            bool skip_artificial,
3257                                            bool &is_static,
3258                                            bool &is_variadic,
3259                                            bool &has_template_params,
3260                                            std::vector<CompilerType>& function_param_types,
3261                                            std::vector<clang::ParmVarDecl*>& function_param_decls,
3262                                            unsigned &type_quals)
3263 {
3264     if (!parent_die)
3265         return 0;
3266 
3267     size_t arg_idx = 0;
3268     for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
3269     {
3270         const dw_tag_t tag = die.Tag();
3271         switch (tag)
3272         {
3273             case DW_TAG_formal_parameter:
3274             {
3275                 DWARFAttributes attributes;
3276                 const size_t num_attributes = die.GetAttributes(attributes);
3277                 if (num_attributes > 0)
3278                 {
3279                     const char *name = NULL;
3280                     Declaration decl;
3281                     DWARFFormValue param_type_die_form;
3282                     bool is_artificial = false;
3283                     // one of None, Auto, Register, Extern, Static, PrivateExtern
3284 
3285                     clang::StorageClass storage = clang::SC_None;
3286                     uint32_t i;
3287                     for (i=0; i<num_attributes; ++i)
3288                     {
3289                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
3290                         DWARFFormValue form_value;
3291                         if (attributes.ExtractFormValueAtIndex(i, form_value))
3292                         {
3293                             switch (attr)
3294                             {
3295                                 case DW_AT_decl_file:   decl.SetFile(sc.comp_unit->GetSupportFiles().GetFileSpecAtIndex(form_value.Unsigned())); break;
3296                                 case DW_AT_decl_line:   decl.SetLine(form_value.Unsigned()); break;
3297                                 case DW_AT_decl_column: decl.SetColumn(form_value.Unsigned()); break;
3298                                 case DW_AT_name:        name = form_value.AsCString();
3299                                     break;
3300                                 case DW_AT_type:        param_type_die_form = form_value; break;
3301                                 case DW_AT_artificial:  is_artificial = form_value.Boolean(); break;
3302                                 case DW_AT_location:
3303                                     //                          if (form_value.BlockData())
3304                                     //                          {
3305                                     //                              const DWARFDataExtractor& debug_info_data = debug_info();
3306                                     //                              uint32_t block_length = form_value.Unsigned();
3307                                     //                              DWARFDataExtractor location(debug_info_data, form_value.BlockData() - debug_info_data.GetDataStart(), block_length);
3308                                     //                          }
3309                                     //                          else
3310                                     //                          {
3311                                     //                          }
3312                                     //                          break;
3313                                 case DW_AT_const_value:
3314                                 case DW_AT_default_value:
3315                                 case DW_AT_description:
3316                                 case DW_AT_endianity:
3317                                 case DW_AT_is_optional:
3318                                 case DW_AT_segment:
3319                                 case DW_AT_variable_parameter:
3320                                 default:
3321                                 case DW_AT_abstract_origin:
3322                                 case DW_AT_sibling:
3323                                     break;
3324                             }
3325                         }
3326                     }
3327 
3328                     bool skip = false;
3329                     if (skip_artificial)
3330                     {
3331                         if (is_artificial)
3332                         {
3333                             // In order to determine if a C++ member function is
3334                             // "const" we have to look at the const-ness of "this"...
3335                             // Ugly, but that
3336                             if (arg_idx == 0)
3337                             {
3338                                 if (DeclKindIsCXXClass(containing_decl_ctx->getDeclKind()))
3339                                 {
3340                                     // Often times compilers omit the "this" name for the
3341                                     // specification DIEs, so we can't rely upon the name
3342                                     // being in the formal parameter DIE...
3343                                     if (name == NULL || ::strcmp(name, "this")==0)
3344                                     {
3345                                         Type *this_type = die.ResolveTypeUID (DIERef(param_type_die_form));
3346                                         if (this_type)
3347                                         {
3348                                             uint32_t encoding_mask = this_type->GetEncodingMask();
3349                                             if (encoding_mask & Type::eEncodingIsPointerUID)
3350                                             {
3351                                                 is_static = false;
3352 
3353                                                 if (encoding_mask & (1u << Type::eEncodingIsConstUID))
3354                                                     type_quals |= clang::Qualifiers::Const;
3355                                                 if (encoding_mask & (1u << Type::eEncodingIsVolatileUID))
3356                                                     type_quals |= clang::Qualifiers::Volatile;
3357                                             }
3358                                         }
3359                                     }
3360                                 }
3361                             }
3362                             skip = true;
3363                         }
3364                         else
3365                         {
3366 
3367                             // HACK: Objective C formal parameters "self" and "_cmd"
3368                             // are not marked as artificial in the DWARF...
3369                             CompileUnit *comp_unit = die.GetLLDBCompileUnit();
3370                             if (comp_unit)
3371                             {
3372                                 switch (comp_unit->GetLanguage())
3373                                 {
3374                                     case eLanguageTypeObjC:
3375                                     case eLanguageTypeObjC_plus_plus:
3376                                         if (name && name[0] && (strcmp (name, "self") == 0 || strcmp (name, "_cmd") == 0))
3377                                             skip = true;
3378                                         break;
3379                                     default:
3380                                         break;
3381                                 }
3382                             }
3383                         }
3384                     }
3385 
3386                     if (!skip)
3387                     {
3388                         Type *type = die.ResolveTypeUID(DIERef(param_type_die_form));
3389                         if (type)
3390                         {
3391                             function_param_types.push_back (type->GetForwardCompilerType ());
3392 
3393                             clang::ParmVarDecl *param_var_decl = m_ast.CreateParameterDeclaration (name,
3394                                                                                                    type->GetForwardCompilerType (),
3395                                                                                                    storage);
3396                             assert(param_var_decl);
3397                             function_param_decls.push_back(param_var_decl);
3398 
3399                             m_ast.SetMetadataAsUserID (param_var_decl, die.GetID());
3400                         }
3401                     }
3402                 }
3403                 arg_idx++;
3404             }
3405                 break;
3406 
3407             case DW_TAG_unspecified_parameters:
3408                 is_variadic = true;
3409                 break;
3410 
3411             case DW_TAG_template_type_parameter:
3412             case DW_TAG_template_value_parameter:
3413                 // The one caller of this was never using the template_param_infos,
3414                 // and the local variable was taking up a large amount of stack space
3415                 // in SymbolFileDWARF::ParseType() so this was removed. If we ever need
3416                 // the template params back, we can add them back.
3417                 // ParseTemplateDIE (dwarf_cu, die, template_param_infos);
3418                 has_template_params = true;
3419                 break;
3420 
3421             default:
3422                 break;
3423         }
3424     }
3425     return arg_idx;
3426 }
3427 
3428 void
3429 DWARFASTParserClang::ParseChildArrayInfo (const SymbolContext& sc,
3430                                           const DWARFDIE &parent_die,
3431                                           int64_t& first_index,
3432                                           std::vector<uint64_t>& element_orders,
3433                                           uint32_t& byte_stride,
3434                                           uint32_t& bit_stride)
3435 {
3436     if (!parent_die)
3437         return;
3438 
3439     for (DWARFDIE die = parent_die.GetFirstChild(); die.IsValid(); die = die.GetSibling())
3440     {
3441         const dw_tag_t tag = die.Tag();
3442         switch (tag)
3443         {
3444             case DW_TAG_subrange_type:
3445             {
3446                 DWARFAttributes attributes;
3447                 const size_t num_child_attributes = die.GetAttributes(attributes);
3448                 if (num_child_attributes > 0)
3449                 {
3450                     uint64_t num_elements = 0;
3451                     uint64_t lower_bound = 0;
3452                     uint64_t upper_bound = 0;
3453                     bool upper_bound_valid = false;
3454                     uint32_t i;
3455                     for (i=0; i<num_child_attributes; ++i)
3456                     {
3457                         const dw_attr_t attr = attributes.AttributeAtIndex(i);
3458                         DWARFFormValue form_value;
3459                         if (attributes.ExtractFormValueAtIndex(i, form_value))
3460                         {
3461                             switch (attr)
3462                             {
3463                                 case DW_AT_name:
3464                                     break;
3465 
3466                                 case DW_AT_count:
3467                                     num_elements = form_value.Unsigned();
3468                                     break;
3469 
3470                                 case DW_AT_bit_stride:
3471                                     bit_stride = form_value.Unsigned();
3472                                     break;
3473 
3474                                 case DW_AT_byte_stride:
3475                                     byte_stride = form_value.Unsigned();
3476                                     break;
3477 
3478                                 case DW_AT_lower_bound:
3479                                     lower_bound = form_value.Unsigned();
3480                                     break;
3481 
3482                                 case DW_AT_upper_bound:
3483                                     upper_bound_valid = true;
3484                                     upper_bound = form_value.Unsigned();
3485                                     break;
3486 
3487                                 default:
3488                                 case DW_AT_abstract_origin:
3489                                 case DW_AT_accessibility:
3490                                 case DW_AT_allocated:
3491                                 case DW_AT_associated:
3492                                 case DW_AT_data_location:
3493                                 case DW_AT_declaration:
3494                                 case DW_AT_description:
3495                                 case DW_AT_sibling:
3496                                 case DW_AT_threads_scaled:
3497                                 case DW_AT_type:
3498                                 case DW_AT_visibility:
3499                                     break;
3500                             }
3501                         }
3502                     }
3503 
3504                     if (num_elements == 0)
3505                     {
3506                         if (upper_bound_valid && upper_bound >= lower_bound)
3507                             num_elements = upper_bound - lower_bound + 1;
3508                     }
3509 
3510                     element_orders.push_back (num_elements);
3511                 }
3512             }
3513                 break;
3514         }
3515     }
3516 }
3517 
3518 Type *
3519 DWARFASTParserClang::GetTypeForDIE (const DWARFDIE &die)
3520 {
3521     if (die)
3522     {
3523         SymbolFileDWARF *dwarf = die.GetDWARF();
3524         DWARFAttributes attributes;
3525         const size_t num_attributes = die.GetAttributes(attributes);
3526         if (num_attributes > 0)
3527         {
3528             DWARFFormValue type_die_form;
3529             for (size_t i = 0; i < num_attributes; ++i)
3530             {
3531                 dw_attr_t attr = attributes.AttributeAtIndex(i);
3532                 DWARFFormValue form_value;
3533 
3534                 if (attr == DW_AT_type && attributes.ExtractFormValueAtIndex(i, form_value))
3535                     return dwarf->ResolveTypeUID(dwarf->GetDIE (DIERef(form_value)), true);
3536             }
3537         }
3538     }
3539 
3540     return nullptr;
3541 }
3542 
3543 clang::Decl *
3544 DWARFASTParserClang::GetClangDeclForDIE (const DWARFDIE &die)
3545 {
3546     if (!die)
3547         return nullptr;
3548 
3549     switch (die.Tag())
3550     {
3551         case DW_TAG_variable:
3552         case DW_TAG_constant:
3553         case DW_TAG_formal_parameter:
3554         case DW_TAG_imported_declaration:
3555         case DW_TAG_imported_module:
3556             break;
3557         default:
3558             return nullptr;
3559     }
3560 
3561     DIEToDeclMap::iterator cache_pos = m_die_to_decl.find(die.GetDIE());
3562     if (cache_pos != m_die_to_decl.end())
3563         return cache_pos->second;
3564 
3565     if (DWARFDIE spec_die = die.GetReferencedDIE(DW_AT_specification))
3566     {
3567         clang::Decl *decl = GetClangDeclForDIE(spec_die);
3568         m_die_to_decl[die.GetDIE()] = decl;
3569         m_decl_to_die[decl].insert(die.GetDIE());
3570         return decl;
3571     }
3572 
3573     clang::Decl *decl = nullptr;
3574     switch (die.Tag())
3575     {
3576         case DW_TAG_variable:
3577         case DW_TAG_constant:
3578         case DW_TAG_formal_parameter:
3579         {
3580             SymbolFileDWARF *dwarf = die.GetDWARF();
3581             Type *type = GetTypeForDIE(die);
3582             const char *name = die.GetName();
3583             clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID()));
3584             decl = m_ast.CreateVariableDeclaration(decl_context, name,
3585                                                    ClangUtil::GetQualType(type->GetForwardCompilerType()));
3586             break;
3587         }
3588         case DW_TAG_imported_declaration:
3589         {
3590             SymbolFileDWARF *dwarf = die.GetDWARF();
3591             DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3592             if (imported_uid)
3593             {
3594                 CompilerDecl imported_decl = imported_uid.GetDecl();
3595                 if (imported_decl)
3596                 {
3597                     clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID()));
3598                     if (clang::NamedDecl *clang_imported_decl = llvm::dyn_cast<clang::NamedDecl>((clang::Decl *)imported_decl.GetOpaqueDecl()))
3599                         decl = m_ast.CreateUsingDeclaration(decl_context, clang_imported_decl);
3600                 }
3601             }
3602             break;
3603         }
3604         case DW_TAG_imported_module:
3605         {
3606             SymbolFileDWARF *dwarf = die.GetDWARF();
3607             DWARFDIE imported_uid = die.GetAttributeValueAsReferenceDIE(DW_AT_import);
3608 
3609             if (imported_uid)
3610             {
3611                 CompilerDeclContext imported_decl_ctx = imported_uid.GetDeclContext();
3612                 if (imported_decl_ctx)
3613                 {
3614                     clang::DeclContext *decl_context = ClangASTContext::DeclContextGetAsDeclContext(dwarf->GetDeclContextContainingUID(die.GetID()));
3615                     if (clang::NamespaceDecl *ns_decl = ClangASTContext::DeclContextGetAsNamespaceDecl(imported_decl_ctx))
3616                         decl = m_ast.CreateUsingDirectiveDeclaration(decl_context, ns_decl);
3617                 }
3618             }
3619             break;
3620         }
3621         default:
3622             break;
3623     }
3624 
3625     m_die_to_decl[die.GetDIE()] = decl;
3626     m_decl_to_die[decl].insert(die.GetDIE());
3627 
3628     return decl;
3629 }
3630 
3631 clang::DeclContext *
3632 DWARFASTParserClang::GetClangDeclContextForDIE (const DWARFDIE &die)
3633 {
3634     if (die)
3635     {
3636         clang::DeclContext *decl_ctx = GetCachedClangDeclContextForDIE (die);
3637         if (decl_ctx)
3638             return decl_ctx;
3639 
3640         bool try_parsing_type = true;
3641         switch (die.Tag())
3642         {
3643             case DW_TAG_compile_unit:
3644                 decl_ctx = m_ast.GetTranslationUnitDecl();
3645                 try_parsing_type = false;
3646                 break;
3647 
3648             case DW_TAG_namespace:
3649                 decl_ctx = ResolveNamespaceDIE (die);
3650                 try_parsing_type = false;
3651                 break;
3652 
3653             case DW_TAG_lexical_block:
3654                 decl_ctx = (clang::DeclContext *)ResolveBlockDIE(die);
3655                 try_parsing_type = false;
3656                 break;
3657 
3658             default:
3659                 break;
3660         }
3661 
3662         if (decl_ctx == nullptr && try_parsing_type)
3663         {
3664             Type* type = die.GetDWARF()->ResolveType (die);
3665             if (type)
3666                 decl_ctx = GetCachedClangDeclContextForDIE (die);
3667         }
3668 
3669         if (decl_ctx)
3670         {
3671             LinkDeclContextToDIE (decl_ctx, die);
3672             return decl_ctx;
3673         }
3674     }
3675     return nullptr;
3676 }
3677 
3678 clang::BlockDecl *
3679 DWARFASTParserClang::ResolveBlockDIE (const DWARFDIE &die)
3680 {
3681     if (die && die.Tag() == DW_TAG_lexical_block)
3682     {
3683         clang::BlockDecl *decl = llvm::cast_or_null<clang::BlockDecl>(m_die_to_decl_ctx[die.GetDIE()]);
3684 
3685         if (!decl)
3686         {
3687             DWARFDIE decl_context_die;
3688             clang::DeclContext *decl_context = GetClangDeclContextContainingDIE(die, &decl_context_die);
3689             decl = m_ast.CreateBlockDeclaration(decl_context);
3690 
3691             if (decl)
3692                 LinkDeclContextToDIE((clang::DeclContext *)decl, die);
3693         }
3694 
3695         return decl;
3696     }
3697     return nullptr;
3698 }
3699 
3700 clang::NamespaceDecl *
3701 DWARFASTParserClang::ResolveNamespaceDIE (const DWARFDIE &die)
3702 {
3703     if (die && die.Tag() == DW_TAG_namespace)
3704     {
3705         // See if we already parsed this namespace DIE and associated it with a
3706         // uniqued namespace declaration
3707         clang::NamespaceDecl *namespace_decl = static_cast<clang::NamespaceDecl *>(m_die_to_decl_ctx[die.GetDIE()]);
3708         if (namespace_decl)
3709             return namespace_decl;
3710         else
3711         {
3712             const char *namespace_name = die.GetName();
3713             clang::DeclContext *containing_decl_ctx = GetClangDeclContextContainingDIE (die, nullptr);
3714             namespace_decl = m_ast.GetUniqueNamespaceDeclaration (namespace_name, containing_decl_ctx);
3715             Log *log = nullptr;// (LogChannelDWARF::GetLogIfAll(DWARF_LOG_DEBUG_INFO));
3716             if (log)
3717             {
3718                 SymbolFileDWARF *dwarf = die.GetDWARF();
3719                 if (namespace_name)
3720                 {
3721                     dwarf->GetObjectFile()->GetModule()->LogMessage (log,
3722                                                                      "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace with DW_AT_name(\"%s\") => clang::NamespaceDecl *%p (original = %p)",
3723                                                                      static_cast<void*>(m_ast.getASTContext()),
3724                                                                      die.GetID(),
3725                                                                      namespace_name,
3726                                                                      static_cast<void*>(namespace_decl),
3727                                                                      static_cast<void*>(namespace_decl->getOriginalNamespace()));
3728                 }
3729                 else
3730                 {
3731                     dwarf->GetObjectFile()->GetModule()->LogMessage (log,
3732                                                                      "ASTContext => %p: 0x%8.8" PRIx64 ": DW_TAG_namespace (anonymous) => clang::NamespaceDecl *%p (original = %p)",
3733                                                                      static_cast<void*>(m_ast.getASTContext()),
3734                                                                      die.GetID(),
3735                                                                      static_cast<void*>(namespace_decl),
3736                                                                      static_cast<void*>(namespace_decl->getOriginalNamespace()));
3737                 }
3738             }
3739 
3740             if (namespace_decl)
3741                 LinkDeclContextToDIE((clang::DeclContext*)namespace_decl, die);
3742             return namespace_decl;
3743         }
3744     }
3745     return nullptr;
3746 }
3747 
3748 clang::DeclContext *
3749 DWARFASTParserClang::GetClangDeclContextContainingDIE (const DWARFDIE &die,
3750                                                        DWARFDIE *decl_ctx_die_copy)
3751 {
3752     SymbolFileDWARF *dwarf = die.GetDWARF();
3753 
3754     DWARFDIE decl_ctx_die = dwarf->GetDeclContextDIEContainingDIE (die);
3755 
3756     if (decl_ctx_die_copy)
3757         *decl_ctx_die_copy = decl_ctx_die;
3758 
3759     if (decl_ctx_die)
3760     {
3761         clang::DeclContext *clang_decl_ctx = GetClangDeclContextForDIE (decl_ctx_die);
3762         if (clang_decl_ctx)
3763             return clang_decl_ctx;
3764     }
3765     return m_ast.GetTranslationUnitDecl();
3766 }
3767 
3768 clang::DeclContext *
3769 DWARFASTParserClang::GetCachedClangDeclContextForDIE (const DWARFDIE &die)
3770 {
3771     if (die)
3772     {
3773         DIEToDeclContextMap::iterator pos = m_die_to_decl_ctx.find(die.GetDIE());
3774         if (pos != m_die_to_decl_ctx.end())
3775             return pos->second;
3776     }
3777     return nullptr;
3778 }
3779 
3780 void
3781 DWARFASTParserClang::LinkDeclContextToDIE (clang::DeclContext *decl_ctx, const DWARFDIE &die)
3782 {
3783     m_die_to_decl_ctx[die.GetDIE()] = decl_ctx;
3784     // There can be many DIEs for a single decl context
3785     //m_decl_ctx_to_die[decl_ctx].insert(die.GetDIE());
3786     m_decl_ctx_to_die.insert(std::make_pair(decl_ctx, die));
3787 }
3788 
3789 bool
3790 DWARFASTParserClang::CopyUniqueClassMethodTypes (const DWARFDIE &src_class_die,
3791                                                  const DWARFDIE &dst_class_die,
3792                                                  lldb_private::Type *class_type,
3793                                                  DWARFDIECollection &failures)
3794 {
3795     if (!class_type || !src_class_die || !dst_class_die)
3796         return false;
3797     if (src_class_die.Tag() != dst_class_die.Tag())
3798         return false;
3799 
3800     // We need to complete the class type so we can get all of the method types
3801     // parsed so we can then unique those types to their equivalent counterparts
3802     // in "dst_cu" and "dst_class_die"
3803     class_type->GetFullCompilerType ();
3804 
3805     DWARFDIE src_die;
3806     DWARFDIE dst_die;
3807     UniqueCStringMap<DWARFDIE> src_name_to_die;
3808     UniqueCStringMap<DWARFDIE> dst_name_to_die;
3809     UniqueCStringMap<DWARFDIE> src_name_to_die_artificial;
3810     UniqueCStringMap<DWARFDIE> dst_name_to_die_artificial;
3811     for (src_die = src_class_die.GetFirstChild(); src_die.IsValid(); src_die = src_die.GetSibling())
3812     {
3813         if (src_die.Tag() == DW_TAG_subprogram)
3814         {
3815             // Make sure this is a declaration and not a concrete instance by looking
3816             // for DW_AT_declaration set to 1. Sometimes concrete function instances
3817             // are placed inside the class definitions and shouldn't be included in
3818             // the list of things are are tracking here.
3819             if (src_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1)
3820             {
3821                 const char *src_name = src_die.GetMangledName ();
3822                 if (src_name)
3823                 {
3824                     ConstString src_const_name(src_name);
3825                     if (src_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3826                         src_name_to_die_artificial.Append(src_const_name.GetCString(), src_die);
3827                     else
3828                         src_name_to_die.Append(src_const_name.GetCString(), src_die);
3829                 }
3830             }
3831         }
3832     }
3833     for (dst_die = dst_class_die.GetFirstChild(); dst_die.IsValid(); dst_die = dst_die.GetSibling())
3834     {
3835         if (dst_die.Tag() == DW_TAG_subprogram)
3836         {
3837             // Make sure this is a declaration and not a concrete instance by looking
3838             // for DW_AT_declaration set to 1. Sometimes concrete function instances
3839             // are placed inside the class definitions and shouldn't be included in
3840             // the list of things are are tracking here.
3841             if (dst_die.GetAttributeValueAsUnsigned(DW_AT_declaration, 0) == 1)
3842             {
3843                 const char *dst_name =  dst_die.GetMangledName ();
3844                 if (dst_name)
3845                 {
3846                     ConstString dst_const_name(dst_name);
3847                     if ( dst_die.GetAttributeValueAsUnsigned(DW_AT_artificial, 0))
3848                         dst_name_to_die_artificial.Append(dst_const_name.GetCString(), dst_die);
3849                     else
3850                         dst_name_to_die.Append(dst_const_name.GetCString(), dst_die);
3851                 }
3852             }
3853         }
3854     }
3855     const uint32_t src_size = src_name_to_die.GetSize ();
3856     const uint32_t dst_size = dst_name_to_die.GetSize ();
3857     Log *log = nullptr; // (LogChannelDWARF::GetLogIfAny(DWARF_LOG_DEBUG_INFO | DWARF_LOG_TYPE_COMPLETION));
3858 
3859     // Is everything kosher so we can go through the members at top speed?
3860     bool fast_path = true;
3861 
3862     if (src_size != dst_size)
3863     {
3864         if (src_size != 0 && dst_size != 0)
3865         {
3866             if (log)
3867                 log->Printf("warning: trying to unique class DIE 0x%8.8x to 0x%8.8x, but they didn't have the same size (src=%d, dst=%d)",
3868                             src_class_die.GetOffset(),
3869                             dst_class_die.GetOffset(),
3870                             src_size,
3871                             dst_size);
3872         }
3873 
3874         fast_path = false;
3875     }
3876 
3877     uint32_t idx;
3878 
3879     if (fast_path)
3880     {
3881         for (idx = 0; idx < src_size; ++idx)
3882         {
3883             src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
3884             dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
3885 
3886             if (src_die.Tag() != dst_die.Tag())
3887             {
3888                 if (log)
3889                     log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) tags didn't match 0x%8.8x (%s)",
3890                                 src_class_die.GetOffset(),
3891                                 dst_class_die.GetOffset(),
3892                                 src_die.GetOffset(),
3893                                 src_die.GetTagAsCString(),
3894                                 dst_die.GetOffset(),
3895                                 dst_die.GetTagAsCString());
3896                 fast_path = false;
3897             }
3898 
3899             const char *src_name = src_die.GetMangledName ();
3900             const char *dst_name = dst_die.GetMangledName ();
3901 
3902             // Make sure the names match
3903             if (src_name == dst_name || (strcmp (src_name, dst_name) == 0))
3904                 continue;
3905 
3906             if (log)
3907                 log->Printf("warning: tried to unique class DIE 0x%8.8x to 0x%8.8x, but 0x%8.8x (%s) names didn't match 0x%8.8x (%s)",
3908                             src_class_die.GetOffset(),
3909                             dst_class_die.GetOffset(),
3910                             src_die.GetOffset(),
3911                             src_name,
3912                             dst_die.GetOffset(),
3913                             dst_name);
3914 
3915             fast_path = false;
3916         }
3917     }
3918 
3919     DWARFASTParserClang *src_dwarf_ast_parser = (DWARFASTParserClang *)src_die.GetDWARFParser();
3920     DWARFASTParserClang *dst_dwarf_ast_parser = (DWARFASTParserClang *)dst_die.GetDWARFParser();
3921 
3922     // Now do the work of linking the DeclContexts and Types.
3923     if (fast_path)
3924     {
3925         // We can do this quickly.  Just run across the tables index-for-index since
3926         // we know each node has matching names and tags.
3927         for (idx = 0; idx < src_size; ++idx)
3928         {
3929             src_die = src_name_to_die.GetValueAtIndexUnchecked (idx);
3930             dst_die = dst_name_to_die.GetValueAtIndexUnchecked (idx);
3931 
3932             clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3933             if (src_decl_ctx)
3934             {
3935                 if (log)
3936                     log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3937                                  static_cast<void*>(src_decl_ctx),
3938                                  src_die.GetOffset(), dst_die.GetOffset());
3939                 dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die);
3940             }
3941             else
3942             {
3943                 if (log)
3944                     log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found",
3945                                  src_die.GetOffset(), dst_die.GetOffset());
3946             }
3947 
3948             Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
3949             if (src_child_type)
3950             {
3951                 if (log)
3952                     log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
3953                                  static_cast<void*>(src_child_type),
3954                                  src_child_type->GetID(),
3955                                  src_die.GetOffset(), dst_die.GetOffset());
3956                 dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
3957             }
3958             else
3959             {
3960                 if (log)
3961                     log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset());
3962             }
3963         }
3964     }
3965     else
3966     {
3967         // We must do this slowly.  For each member of the destination, look
3968         // up a member in the source with the same name, check its tag, and
3969         // unique them if everything matches up.  Report failures.
3970 
3971         if (!src_name_to_die.IsEmpty() && !dst_name_to_die.IsEmpty())
3972         {
3973             src_name_to_die.Sort();
3974 
3975             for (idx = 0; idx < dst_size; ++idx)
3976             {
3977                 const char *dst_name = dst_name_to_die.GetCStringAtIndex(idx);
3978                 dst_die = dst_name_to_die.GetValueAtIndexUnchecked(idx);
3979                 src_die = src_name_to_die.Find(dst_name, DWARFDIE());
3980 
3981                 if (src_die && (src_die.Tag() == dst_die.Tag()))
3982                 {
3983                     clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
3984                     if (src_decl_ctx)
3985                     {
3986                         if (log)
3987                             log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
3988                                          static_cast<void*>(src_decl_ctx),
3989                                          src_die.GetOffset(),
3990                                          dst_die.GetOffset());
3991                         dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die);
3992                     }
3993                     else
3994                     {
3995                         if (log)
3996                             log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset());
3997                     }
3998 
3999                     Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
4000                     if (src_child_type)
4001                     {
4002                         if (log)
4003                             log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
4004                                          static_cast<void*>(src_child_type),
4005                                          src_child_type->GetID(),
4006                                          src_die.GetOffset(),
4007                                          dst_die.GetOffset());
4008                         dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
4009                     }
4010                     else
4011                     {
4012                         if (log)
4013                             log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset());
4014                     }
4015                 }
4016                 else
4017                 {
4018                     if (log)
4019                         log->Printf ("warning: couldn't find a match for 0x%8.8x", dst_die.GetOffset());
4020 
4021                     failures.Append(dst_die);
4022                 }
4023             }
4024         }
4025     }
4026 
4027     const uint32_t src_size_artificial = src_name_to_die_artificial.GetSize ();
4028     const uint32_t dst_size_artificial = dst_name_to_die_artificial.GetSize ();
4029 
4030     if (src_size_artificial && dst_size_artificial)
4031     {
4032         dst_name_to_die_artificial.Sort();
4033 
4034         for (idx = 0; idx < src_size_artificial; ++idx)
4035         {
4036             const char *src_name_artificial = src_name_to_die_artificial.GetCStringAtIndex(idx);
4037             src_die = src_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
4038             dst_die = dst_name_to_die_artificial.Find(src_name_artificial, DWARFDIE());
4039 
4040             if (dst_die)
4041             {
4042                 // Both classes have the artificial types, link them
4043                 clang::DeclContext *src_decl_ctx = src_dwarf_ast_parser->m_die_to_decl_ctx[src_die.GetDIE()];
4044                 if (src_decl_ctx)
4045                 {
4046                     if (log)
4047                         log->Printf ("uniquing decl context %p from 0x%8.8x for 0x%8.8x",
4048                                      static_cast<void*>(src_decl_ctx),
4049                                      src_die.GetOffset(), dst_die.GetOffset());
4050                     dst_dwarf_ast_parser->LinkDeclContextToDIE (src_decl_ctx, dst_die);
4051                 }
4052                 else
4053                 {
4054                     if (log)
4055                         log->Printf ("warning: tried to unique decl context from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset());
4056                 }
4057 
4058                 Type *src_child_type = dst_die.GetDWARF()->GetDIEToType()[src_die.GetDIE()];
4059                 if (src_child_type)
4060                 {
4061                     if (log)
4062                         log->Printf ("uniquing type %p (uid=0x%" PRIx64 ") from 0x%8.8x for 0x%8.8x",
4063                                      static_cast<void*>(src_child_type),
4064                                      src_child_type->GetID(),
4065                                      src_die.GetOffset(), dst_die.GetOffset());
4066                     dst_die.GetDWARF()->GetDIEToType()[dst_die.GetDIE()] = src_child_type;
4067                 }
4068                 else
4069                 {
4070                     if (log)
4071                         log->Printf ("warning: tried to unique lldb_private::Type from 0x%8.8x for 0x%8.8x, but none was found", src_die.GetOffset(), dst_die.GetOffset());
4072                 }
4073             }
4074         }
4075     }
4076 
4077     if (dst_size_artificial)
4078     {
4079         for (idx = 0; idx < dst_size_artificial; ++idx)
4080         {
4081             const char *dst_name_artificial = dst_name_to_die_artificial.GetCStringAtIndex(idx);
4082             dst_die = dst_name_to_die_artificial.GetValueAtIndexUnchecked (idx);
4083             if (log)
4084                 log->Printf ("warning: need to create artificial method for 0x%8.8x for method '%s'", dst_die.GetOffset(), dst_name_artificial);
4085 
4086             failures.Append(dst_die);
4087         }
4088     }
4089 
4090     return (failures.Size() != 0);
4091 }
4092 
4093