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