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