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