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