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