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