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