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