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