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