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