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