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