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