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