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