1 //===-- Type.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 // C Includes 11 #include <stdio.h> 12 13 // C++ Includes 14 // Other libraries and framework includes 15 // Project includes 16 #include "lldb/Core/DataExtractor.h" 17 #include "lldb/Core/DataBufferHeap.h" 18 #include "lldb/Core/Module.h" 19 #include "lldb/Core/Scalar.h" 20 #include "lldb/Core/StreamString.h" 21 22 #include "lldb/Symbol/CompilerType.h" 23 #include "lldb/Symbol/ObjectFile.h" 24 #include "lldb/Symbol/SymbolContextScope.h" 25 #include "lldb/Symbol/SymbolFile.h" 26 #include "lldb/Symbol/SymbolVendor.h" 27 #include "lldb/Symbol/Type.h" 28 #include "lldb/Symbol/TypeList.h" 29 #include "lldb/Symbol/TypeSystem.h" 30 31 #include "lldb/Target/ExecutionContext.h" 32 #include "lldb/Target/Process.h" 33 #include "lldb/Target/Target.h" 34 35 #include "llvm/ADT/StringRef.h" 36 37 #include "clang/AST/Decl.h" 38 #include "clang/AST/DeclObjC.h" 39 40 using namespace lldb; 41 using namespace lldb_private; 42 43 void 44 CompilerContext::Dump() const 45 { 46 switch (type) 47 { 48 case CompilerContextKind::Invalid: printf("Invalid"); break; 49 case CompilerContextKind::TranslationUnit: printf("TranslationUnit"); break; 50 case CompilerContextKind::Module: printf("Module"); break; 51 case CompilerContextKind::Namespace: printf("Namespace"); break; 52 case CompilerContextKind::Class: printf("Class"); break; 53 case CompilerContextKind::Structure: printf("Structure"); break; 54 case CompilerContextKind::Union: printf("Union"); break; 55 case CompilerContextKind::Function: printf("Function"); break; 56 case CompilerContextKind::Variable: printf("Variable"); break; 57 case CompilerContextKind::Enumeration: printf("Enumeration"); break; 58 case CompilerContextKind::Typedef: printf("Typedef"); break; 59 default: printf("???(%u)", type); 60 } 61 printf("(\"%s\")\n", name.GetCString()); 62 } 63 64 class TypeAppendVisitor 65 { 66 public: 67 TypeAppendVisitor(TypeListImpl &type_list) : 68 m_type_list(type_list) 69 { 70 } 71 72 bool 73 operator() (const lldb::TypeSP& type) 74 { 75 m_type_list.Append(TypeImplSP(new TypeImpl(type))); 76 return true; 77 } 78 79 private: 80 TypeListImpl &m_type_list; 81 }; 82 83 void 84 TypeListImpl::Append (const lldb_private::TypeList &type_list) 85 { 86 TypeAppendVisitor cb(*this); 87 type_list.ForEach(cb); 88 } 89 90 SymbolFileType::SymbolFileType (SymbolFile &symbol_file, const lldb::TypeSP &type_sp) : 91 UserID (type_sp ? type_sp->GetID() : LLDB_INVALID_UID), 92 m_symbol_file (symbol_file), 93 m_type_sp (type_sp) 94 { 95 } 96 97 98 Type * 99 SymbolFileType::GetType () 100 { 101 if (!m_type_sp) 102 { 103 Type *resolved_type = m_symbol_file.ResolveTypeUID (GetID()); 104 if (resolved_type) 105 m_type_sp = resolved_type->shared_from_this(); 106 } 107 return m_type_sp.get(); 108 } 109 110 111 Type::Type 112 ( 113 lldb::user_id_t uid, 114 SymbolFile* symbol_file, 115 const ConstString &name, 116 uint64_t byte_size, 117 SymbolContextScope *context, 118 user_id_t encoding_uid, 119 EncodingDataType encoding_uid_type, 120 const Declaration& decl, 121 const CompilerType &compiler_type, 122 ResolveState compiler_type_resolve_state 123 ) : 124 std::enable_shared_from_this<Type> (), 125 UserID (uid), 126 m_name (name), 127 m_symbol_file (symbol_file), 128 m_context (context), 129 m_encoding_type (nullptr), 130 m_encoding_uid (encoding_uid), 131 m_encoding_uid_type (encoding_uid_type), 132 m_byte_size (byte_size), 133 m_decl (decl), 134 m_compiler_type (compiler_type) 135 { 136 m_flags.compiler_type_resolve_state = (compiler_type ? compiler_type_resolve_state : eResolveStateUnresolved); 137 m_flags.is_complete_objc_class = false; 138 } 139 140 Type::Type () : 141 std::enable_shared_from_this<Type> (), 142 UserID (0), 143 m_name ("<INVALID TYPE>"), 144 m_symbol_file (nullptr), 145 m_context (nullptr), 146 m_encoding_type (nullptr), 147 m_encoding_uid (LLDB_INVALID_UID), 148 m_encoding_uid_type (eEncodingInvalid), 149 m_byte_size (0), 150 m_decl (), 151 m_compiler_type () 152 { 153 m_flags.compiler_type_resolve_state = eResolveStateUnresolved; 154 m_flags.is_complete_objc_class = false; 155 } 156 157 158 Type::Type (const Type &rhs) : 159 std::enable_shared_from_this<Type> (rhs), 160 UserID (rhs), 161 m_name (rhs.m_name), 162 m_symbol_file (rhs.m_symbol_file), 163 m_context (rhs.m_context), 164 m_encoding_type (rhs.m_encoding_type), 165 m_encoding_uid (rhs.m_encoding_uid), 166 m_encoding_uid_type (rhs.m_encoding_uid_type), 167 m_byte_size (rhs.m_byte_size), 168 m_decl (rhs.m_decl), 169 m_compiler_type (rhs.m_compiler_type), 170 m_flags (rhs.m_flags) 171 { 172 } 173 174 const Type& 175 Type::operator= (const Type& rhs) 176 { 177 if (this != &rhs) 178 { 179 } 180 return *this; 181 } 182 183 184 void 185 Type::GetDescription (Stream *s, lldb::DescriptionLevel level, bool show_name) 186 { 187 *s << "id = " << (const UserID&)*this; 188 189 // Call the name accessor to make sure we resolve the type name 190 if (show_name) 191 { 192 const ConstString &type_name = GetName(); 193 if (type_name) 194 { 195 *s << ", name = \"" << type_name << '"'; 196 ConstString qualified_type_name (GetQualifiedName()); 197 if (qualified_type_name != type_name) 198 { 199 *s << ", qualified = \"" << qualified_type_name << '"'; 200 } 201 } 202 } 203 204 // Call the get byte size accesor so we resolve our byte size 205 if (GetByteSize()) 206 s->Printf(", byte-size = %" PRIu64, m_byte_size); 207 bool show_fullpaths = (level == lldb::eDescriptionLevelVerbose); 208 m_decl.Dump(s, show_fullpaths); 209 210 if (m_compiler_type.IsValid()) 211 { 212 *s << ", compiler_type = \""; 213 GetForwardCompilerType ().DumpTypeDescription(s); 214 *s << '"'; 215 } 216 else if (m_encoding_uid != LLDB_INVALID_UID) 217 { 218 s->Printf(", type_uid = 0x%8.8" PRIx64, m_encoding_uid); 219 switch (m_encoding_uid_type) 220 { 221 case eEncodingInvalid: break; 222 case eEncodingIsUID: s->PutCString(" (unresolved type)"); break; 223 case eEncodingIsConstUID: s->PutCString(" (unresolved const type)"); break; 224 case eEncodingIsRestrictUID: s->PutCString(" (unresolved restrict type)"); break; 225 case eEncodingIsVolatileUID: s->PutCString(" (unresolved volatile type)"); break; 226 case eEncodingIsTypedefUID: s->PutCString(" (unresolved typedef)"); break; 227 case eEncodingIsPointerUID: s->PutCString(" (unresolved pointer)"); break; 228 case eEncodingIsLValueReferenceUID: s->PutCString(" (unresolved L value reference)"); break; 229 case eEncodingIsRValueReferenceUID: s->PutCString(" (unresolved R value reference)"); break; 230 case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break; 231 } 232 } 233 } 234 235 236 void 237 Type::Dump (Stream *s, bool show_context) 238 { 239 s->Printf("%p: ", static_cast<void*>(this)); 240 s->Indent(); 241 *s << "Type" << static_cast<const UserID&>(*this) << ' '; 242 if (m_name) 243 *s << ", name = \"" << m_name << "\""; 244 245 if (m_byte_size != 0) 246 s->Printf(", size = %" PRIu64, m_byte_size); 247 248 if (show_context && m_context != nullptr) 249 { 250 s->PutCString(", context = ( "); 251 m_context->DumpSymbolContext(s); 252 s->PutCString(" )"); 253 } 254 255 bool show_fullpaths = false; 256 m_decl.Dump (s,show_fullpaths); 257 258 if (m_compiler_type.IsValid()) 259 { 260 *s << ", compiler_type = " << m_compiler_type.GetOpaqueQualType() << ' '; 261 GetForwardCompilerType ().DumpTypeDescription (s); 262 } 263 else if (m_encoding_uid != LLDB_INVALID_UID) 264 { 265 *s << ", type_data = " << (uint64_t)m_encoding_uid; 266 switch (m_encoding_uid_type) 267 { 268 case eEncodingInvalid: break; 269 case eEncodingIsUID: s->PutCString(" (unresolved type)"); break; 270 case eEncodingIsConstUID: s->PutCString(" (unresolved const type)"); break; 271 case eEncodingIsRestrictUID: s->PutCString(" (unresolved restrict type)"); break; 272 case eEncodingIsVolatileUID: s->PutCString(" (unresolved volatile type)"); break; 273 case eEncodingIsTypedefUID: s->PutCString(" (unresolved typedef)"); break; 274 case eEncodingIsPointerUID: s->PutCString(" (unresolved pointer)"); break; 275 case eEncodingIsLValueReferenceUID: s->PutCString(" (unresolved L value reference)"); break; 276 case eEncodingIsRValueReferenceUID: s->PutCString(" (unresolved R value reference)"); break; 277 case eEncodingIsSyntheticUID: s->PutCString(" (synthetic type)"); break; 278 } 279 } 280 281 // 282 // if (m_access) 283 // s->Printf(", access = %u", m_access); 284 s->EOL(); 285 } 286 287 const ConstString & 288 Type::GetName() 289 { 290 if (!m_name) 291 m_name = GetForwardCompilerType ().GetConstTypeName(); 292 return m_name; 293 } 294 295 void 296 Type::DumpTypeName(Stream *s) 297 { 298 GetName().Dump(s, "<invalid-type-name>"); 299 } 300 301 302 void 303 Type::DumpValue 304 ( 305 ExecutionContext *exe_ctx, 306 Stream *s, 307 const DataExtractor &data, 308 uint32_t data_byte_offset, 309 bool show_types, 310 bool show_summary, 311 bool verbose, 312 lldb::Format format 313 ) 314 { 315 if (ResolveClangType(eResolveStateForward)) 316 { 317 if (show_types) 318 { 319 s->PutChar('('); 320 if (verbose) 321 s->Printf("Type{0x%8.8" PRIx64 "} ", GetID()); 322 DumpTypeName (s); 323 s->PutCString(") "); 324 } 325 326 GetForwardCompilerType ().DumpValue (exe_ctx, 327 s, 328 format == lldb::eFormatDefault ? GetFormat() : format, 329 data, 330 data_byte_offset, 331 GetByteSize(), 332 0, // Bitfield bit size 333 0, // Bitfield bit offset 334 show_types, 335 show_summary, 336 verbose, 337 0); 338 } 339 } 340 341 Type * 342 Type::GetEncodingType () 343 { 344 if (m_encoding_type == nullptr && m_encoding_uid != LLDB_INVALID_UID) 345 m_encoding_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 346 return m_encoding_type; 347 } 348 349 350 351 uint64_t 352 Type::GetByteSize() 353 { 354 if (m_byte_size == 0) 355 { 356 switch (m_encoding_uid_type) 357 { 358 case eEncodingInvalid: 359 case eEncodingIsSyntheticUID: 360 break; 361 case eEncodingIsUID: 362 case eEncodingIsConstUID: 363 case eEncodingIsRestrictUID: 364 case eEncodingIsVolatileUID: 365 case eEncodingIsTypedefUID: 366 { 367 Type *encoding_type = GetEncodingType (); 368 if (encoding_type) 369 m_byte_size = encoding_type->GetByteSize(); 370 if (m_byte_size == 0) 371 m_byte_size = GetLayoutCompilerType ().GetByteSize(nullptr); 372 } 373 break; 374 375 // If we are a pointer or reference, then this is just a pointer size; 376 case eEncodingIsPointerUID: 377 case eEncodingIsLValueReferenceUID: 378 case eEncodingIsRValueReferenceUID: 379 { 380 ArchSpec arch; 381 if (m_symbol_file->GetObjectFile()->GetArchitecture(arch)) 382 m_byte_size = arch.GetAddressByteSize(); 383 } 384 break; 385 } 386 } 387 return m_byte_size; 388 } 389 390 391 uint32_t 392 Type::GetNumChildren (bool omit_empty_base_classes) 393 { 394 return GetForwardCompilerType ().GetNumChildren(omit_empty_base_classes); 395 } 396 397 bool 398 Type::IsAggregateType () 399 { 400 return GetForwardCompilerType ().IsAggregateType(); 401 } 402 403 lldb::TypeSP 404 Type::GetTypedefType() 405 { 406 lldb::TypeSP type_sp; 407 if (IsTypedef()) 408 { 409 Type *typedef_type = m_symbol_file->ResolveTypeUID(m_encoding_uid); 410 if (typedef_type) 411 type_sp = typedef_type->shared_from_this(); 412 } 413 return type_sp; 414 } 415 416 417 418 lldb::Format 419 Type::GetFormat () 420 { 421 return GetForwardCompilerType ().GetFormat(); 422 } 423 424 425 426 lldb::Encoding 427 Type::GetEncoding (uint64_t &count) 428 { 429 // Make sure we resolve our type if it already hasn't been. 430 return GetForwardCompilerType ().GetEncoding(count); 431 } 432 433 bool 434 Type::DumpValueInMemory 435 ( 436 ExecutionContext *exe_ctx, 437 Stream *s, 438 lldb::addr_t address, 439 AddressType address_type, 440 bool show_types, 441 bool show_summary, 442 bool verbose 443 ) 444 { 445 if (address != LLDB_INVALID_ADDRESS) 446 { 447 DataExtractor data; 448 Target *target = nullptr; 449 if (exe_ctx) 450 target = exe_ctx->GetTargetPtr(); 451 if (target) 452 data.SetByteOrder (target->GetArchitecture().GetByteOrder()); 453 if (ReadFromMemory (exe_ctx, address, address_type, data)) 454 { 455 DumpValue(exe_ctx, s, data, 0, show_types, show_summary, verbose); 456 return true; 457 } 458 } 459 return false; 460 } 461 462 463 bool 464 Type::ReadFromMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data) 465 { 466 if (address_type == eAddressTypeFile) 467 { 468 // Can't convert a file address to anything valid without more 469 // context (which Module it came from) 470 return false; 471 } 472 473 const uint64_t byte_size = GetByteSize(); 474 if (data.GetByteSize() < byte_size) 475 { 476 lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0')); 477 data.SetData(data_sp); 478 } 479 480 uint8_t* dst = const_cast<uint8_t*>(data.PeekData(0, byte_size)); 481 if (dst != nullptr) 482 { 483 if (address_type == eAddressTypeHost) 484 { 485 // The address is an address in this process, so just copy it 486 if (addr == 0) 487 return false; 488 memcpy (dst, (uint8_t*)nullptr + addr, byte_size); 489 return true; 490 } 491 else 492 { 493 if (exe_ctx) 494 { 495 Process *process = exe_ctx->GetProcessPtr(); 496 if (process) 497 { 498 Error error; 499 return exe_ctx->GetProcessPtr()->ReadMemory(addr, dst, byte_size, error) == byte_size; 500 } 501 } 502 } 503 } 504 return false; 505 } 506 507 508 bool 509 Type::WriteToMemory (ExecutionContext *exe_ctx, lldb::addr_t addr, AddressType address_type, DataExtractor &data) 510 { 511 return false; 512 } 513 514 515 TypeList* 516 Type::GetTypeList() 517 { 518 return GetSymbolFile()->GetTypeList(); 519 } 520 521 const Declaration & 522 Type::GetDeclaration () const 523 { 524 return m_decl; 525 } 526 527 bool 528 Type::ResolveClangType (ResolveState compiler_type_resolve_state) 529 { 530 // TODO: This needs to consider the correct type system to use. 531 Type *encoding_type = nullptr; 532 if (!m_compiler_type.IsValid()) 533 { 534 encoding_type = GetEncodingType(); 535 if (encoding_type) 536 { 537 switch (m_encoding_uid_type) 538 { 539 case eEncodingIsUID: 540 { 541 CompilerType encoding_compiler_type = encoding_type->GetForwardCompilerType (); 542 if (encoding_compiler_type.IsValid()) 543 { 544 m_compiler_type = encoding_compiler_type; 545 m_flags.compiler_type_resolve_state = encoding_type->m_flags.compiler_type_resolve_state; 546 } 547 } 548 break; 549 550 case eEncodingIsConstUID: 551 m_compiler_type = encoding_type->GetForwardCompilerType ().AddConstModifier(); 552 break; 553 554 case eEncodingIsRestrictUID: 555 m_compiler_type = encoding_type->GetForwardCompilerType ().AddRestrictModifier(); 556 break; 557 558 case eEncodingIsVolatileUID: 559 m_compiler_type = encoding_type->GetForwardCompilerType ().AddVolatileModifier(); 560 break; 561 562 case eEncodingIsTypedefUID: 563 m_compiler_type = encoding_type->GetForwardCompilerType ().CreateTypedef(GetName().AsCString(), 564 GetSymbolFile()->GetDeclContextContainingUID(GetID())); 565 m_name.Clear(); 566 break; 567 568 case eEncodingIsPointerUID: 569 m_compiler_type = encoding_type->GetForwardCompilerType ().GetPointerType(); 570 break; 571 572 case eEncodingIsLValueReferenceUID: 573 m_compiler_type = encoding_type->GetForwardCompilerType ().GetLValueReferenceType(); 574 break; 575 576 case eEncodingIsRValueReferenceUID: 577 m_compiler_type = encoding_type->GetForwardCompilerType ().GetRValueReferenceType(); 578 break; 579 580 default: 581 assert(!"Unhandled encoding_data_type."); 582 break; 583 } 584 } 585 else 586 { 587 // We have no encoding type, return void? 588 TypeSystem *type_system = m_symbol_file->GetTypeSystemForLanguage(eLanguageTypeC); 589 CompilerType void_compiler_type = type_system->GetBasicTypeFromAST(eBasicTypeVoid); 590 switch (m_encoding_uid_type) 591 { 592 case eEncodingIsUID: 593 m_compiler_type = void_compiler_type; 594 break; 595 596 case eEncodingIsConstUID: 597 m_compiler_type = void_compiler_type.AddConstModifier(); 598 break; 599 600 case eEncodingIsRestrictUID: 601 m_compiler_type = void_compiler_type.AddRestrictModifier(); 602 break; 603 604 case eEncodingIsVolatileUID: 605 m_compiler_type = void_compiler_type.AddVolatileModifier(); 606 break; 607 608 case eEncodingIsTypedefUID: 609 m_compiler_type = void_compiler_type.CreateTypedef(GetName().AsCString(), 610 GetSymbolFile()->GetDeclContextContainingUID(GetID())); 611 break; 612 613 case eEncodingIsPointerUID: 614 m_compiler_type = void_compiler_type.GetPointerType (); 615 break; 616 617 case eEncodingIsLValueReferenceUID: 618 m_compiler_type = void_compiler_type.GetLValueReferenceType(); 619 break; 620 621 case eEncodingIsRValueReferenceUID: 622 m_compiler_type = void_compiler_type.GetRValueReferenceType(); 623 break; 624 625 default: 626 assert(!"Unhandled encoding_data_type."); 627 break; 628 } 629 } 630 631 // When we have a EncodingUID, our "m_flags.compiler_type_resolve_state" is set to eResolveStateUnresolved 632 // so we need to update it to say that we now have a forward declaration since that is what we created 633 // above. 634 if (m_compiler_type.IsValid()) 635 m_flags.compiler_type_resolve_state = eResolveStateForward; 636 637 } 638 639 // Check if we have a forward reference to a class/struct/union/enum? 640 if (compiler_type_resolve_state == eResolveStateLayout || compiler_type_resolve_state == eResolveStateFull) 641 { 642 // Check if we have a forward reference to a class/struct/union/enum? 643 if (m_compiler_type.IsValid() && m_flags.compiler_type_resolve_state < compiler_type_resolve_state) 644 { 645 m_flags.compiler_type_resolve_state = eResolveStateFull; 646 if (!m_compiler_type.IsDefined ()) 647 { 648 // We have a forward declaration, we need to resolve it to a complete definition. 649 m_symbol_file->CompleteType (m_compiler_type); 650 } 651 } 652 } 653 654 // If we have an encoding type, then we need to make sure it is 655 // resolved appropriately. 656 if (m_encoding_uid != LLDB_INVALID_UID) 657 { 658 if (encoding_type == nullptr) 659 encoding_type = GetEncodingType(); 660 if (encoding_type) 661 { 662 ResolveState encoding_compiler_type_resolve_state = compiler_type_resolve_state; 663 664 if (compiler_type_resolve_state == eResolveStateLayout) 665 { 666 switch (m_encoding_uid_type) 667 { 668 case eEncodingIsPointerUID: 669 case eEncodingIsLValueReferenceUID: 670 case eEncodingIsRValueReferenceUID: 671 encoding_compiler_type_resolve_state = eResolveStateForward; 672 break; 673 default: 674 break; 675 } 676 } 677 encoding_type->ResolveClangType (encoding_compiler_type_resolve_state); 678 } 679 } 680 return m_compiler_type.IsValid(); 681 } 682 uint32_t 683 Type::GetEncodingMask () 684 { 685 uint32_t encoding_mask = 1u << m_encoding_uid_type; 686 Type *encoding_type = GetEncodingType(); 687 assert (encoding_type != this); 688 if (encoding_type) 689 encoding_mask |= encoding_type->GetEncodingMask (); 690 return encoding_mask; 691 } 692 693 CompilerType 694 Type::GetFullCompilerType () 695 { 696 ResolveClangType(eResolveStateFull); 697 return m_compiler_type; 698 } 699 700 CompilerType 701 Type::GetLayoutCompilerType () 702 { 703 ResolveClangType(eResolveStateLayout); 704 return m_compiler_type; 705 } 706 707 CompilerType 708 Type::GetForwardCompilerType () 709 { 710 ResolveClangType (eResolveStateForward); 711 return m_compiler_type; 712 } 713 714 int 715 Type::Compare(const Type &a, const Type &b) 716 { 717 // Just compare the UID values for now... 718 lldb::user_id_t a_uid = a.GetID(); 719 lldb::user_id_t b_uid = b.GetID(); 720 if (a_uid < b_uid) 721 return -1; 722 if (a_uid > b_uid) 723 return 1; 724 return 0; 725 } 726 727 ConstString 728 Type::GetQualifiedName () 729 { 730 return GetForwardCompilerType ().GetConstTypeName(); 731 } 732 733 bool 734 Type::GetTypeScopeAndBasename (const char* &name_cstr, 735 std::string &scope, 736 std::string &basename, 737 TypeClass &type_class) 738 { 739 // Protect against null c string. 740 741 type_class = eTypeClassAny; 742 743 if (name_cstr && name_cstr[0]) 744 { 745 llvm::StringRef name_strref(name_cstr); 746 if (name_strref.startswith("struct ")) 747 { 748 name_cstr += 7; 749 type_class = eTypeClassStruct; 750 } 751 else if (name_strref.startswith("class ")) 752 { 753 name_cstr += 6; 754 type_class = eTypeClassClass; 755 } 756 else if (name_strref.startswith("union ")) 757 { 758 name_cstr += 6; 759 type_class = eTypeClassUnion; 760 } 761 else if (name_strref.startswith("enum ")) 762 { 763 name_cstr += 5; 764 type_class = eTypeClassEnumeration; 765 } 766 else if (name_strref.startswith("typedef ")) 767 { 768 name_cstr += 8; 769 type_class = eTypeClassTypedef; 770 } 771 const char *basename_cstr = name_cstr; 772 const char* namespace_separator = ::strstr (basename_cstr, "::"); 773 if (namespace_separator) 774 { 775 const char* template_arg_char = ::strchr (basename_cstr, '<'); 776 while (namespace_separator != nullptr) 777 { 778 if (template_arg_char && namespace_separator > template_arg_char) // but namespace'd template arguments are still good to go 779 break; 780 basename_cstr = namespace_separator + 2; 781 namespace_separator = strstr(basename_cstr, "::"); 782 } 783 if (basename_cstr > name_cstr) 784 { 785 scope.assign (name_cstr, basename_cstr - name_cstr); 786 basename.assign (basename_cstr); 787 return true; 788 } 789 } 790 } 791 return false; 792 } 793 794 795 ModuleSP 796 Type::GetModule() 797 { 798 if (m_symbol_file) 799 return m_symbol_file->GetObjectFile()->GetModule(); 800 return ModuleSP(); 801 } 802 803 804 TypeAndOrName::TypeAndOrName () : m_type_pair(), m_type_name() 805 { 806 807 } 808 809 TypeAndOrName::TypeAndOrName (TypeSP &in_type_sp) : m_type_pair(in_type_sp) 810 { 811 if (in_type_sp) 812 m_type_name = in_type_sp->GetName(); 813 } 814 815 TypeAndOrName::TypeAndOrName (const char *in_type_str) : m_type_name(in_type_str) 816 { 817 } 818 819 TypeAndOrName::TypeAndOrName (const TypeAndOrName &rhs) : m_type_pair (rhs.m_type_pair), m_type_name (rhs.m_type_name) 820 { 821 822 } 823 824 TypeAndOrName::TypeAndOrName (ConstString &in_type_const_string) : m_type_name (in_type_const_string) 825 { 826 } 827 828 TypeAndOrName & 829 TypeAndOrName::operator= (const TypeAndOrName &rhs) 830 { 831 if (this != &rhs) 832 { 833 m_type_name = rhs.m_type_name; 834 m_type_pair = rhs.m_type_pair; 835 } 836 return *this; 837 } 838 839 bool 840 TypeAndOrName::operator==(const TypeAndOrName &other) const 841 { 842 if (m_type_pair != other.m_type_pair) 843 return false; 844 if (m_type_name != other.m_type_name) 845 return false; 846 return true; 847 } 848 849 bool 850 TypeAndOrName::operator!=(const TypeAndOrName &other) const 851 { 852 if (m_type_pair != other.m_type_pair) 853 return true; 854 if (m_type_name != other.m_type_name) 855 return true; 856 return false; 857 } 858 859 ConstString 860 TypeAndOrName::GetName () const 861 { 862 if (m_type_name) 863 return m_type_name; 864 if (m_type_pair) 865 return m_type_pair.GetName(); 866 return ConstString("<invalid>"); 867 } 868 869 void 870 TypeAndOrName::SetName (const ConstString &type_name) 871 { 872 m_type_name = type_name; 873 } 874 875 void 876 TypeAndOrName::SetName (const char *type_name_cstr) 877 { 878 m_type_name.SetCString (type_name_cstr); 879 } 880 881 void 882 TypeAndOrName::SetTypeSP (lldb::TypeSP type_sp) 883 { 884 m_type_pair.SetType(type_sp); 885 if (m_type_pair) 886 m_type_name = m_type_pair.GetName(); 887 } 888 889 void 890 TypeAndOrName::SetCompilerType (CompilerType compiler_type) 891 { 892 m_type_pair.SetType(compiler_type); 893 if (m_type_pair) 894 m_type_name = m_type_pair.GetName(); 895 } 896 897 bool 898 TypeAndOrName::IsEmpty() const 899 { 900 if ((bool)m_type_name || (bool)m_type_pair) 901 return false; 902 else 903 return true; 904 } 905 906 void 907 TypeAndOrName::Clear () 908 { 909 m_type_name.Clear(); 910 m_type_pair.Clear(); 911 } 912 913 bool 914 TypeAndOrName::HasName () const 915 { 916 return (bool)m_type_name; 917 } 918 919 bool 920 TypeAndOrName::HasTypeSP () const 921 { 922 return m_type_pair.GetTypeSP().get() != nullptr; 923 } 924 925 bool 926 TypeAndOrName::HasCompilerType () const 927 { 928 return m_type_pair.GetCompilerType().IsValid(); 929 } 930 931 932 TypeImpl::TypeImpl() : 933 m_module_wp(), 934 m_static_type(), 935 m_dynamic_type() 936 { 937 } 938 939 TypeImpl::TypeImpl(const TypeImpl& rhs) : 940 m_module_wp (rhs.m_module_wp), 941 m_static_type(rhs.m_static_type), 942 m_dynamic_type(rhs.m_dynamic_type) 943 { 944 } 945 946 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp) : 947 m_module_wp (), 948 m_static_type(), 949 m_dynamic_type() 950 { 951 SetType (type_sp); 952 } 953 954 TypeImpl::TypeImpl (const CompilerType &compiler_type) : 955 m_module_wp (), 956 m_static_type(), 957 m_dynamic_type() 958 { 959 SetType (compiler_type); 960 } 961 962 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp, const CompilerType &dynamic) : 963 m_module_wp (), 964 m_static_type (type_sp), 965 m_dynamic_type(dynamic) 966 { 967 SetType (type_sp, dynamic); 968 } 969 970 TypeImpl::TypeImpl (const CompilerType &static_type, const CompilerType &dynamic_type) : 971 m_module_wp (), 972 m_static_type (), 973 m_dynamic_type() 974 { 975 SetType (static_type, dynamic_type); 976 } 977 978 TypeImpl::TypeImpl (const TypePair &pair, const CompilerType &dynamic) : 979 m_module_wp (), 980 m_static_type (), 981 m_dynamic_type() 982 { 983 SetType (pair, dynamic); 984 } 985 986 void 987 TypeImpl::SetType (const lldb::TypeSP &type_sp) 988 { 989 m_static_type.SetType(type_sp); 990 if (type_sp) 991 m_module_wp = type_sp->GetModule(); 992 else 993 m_module_wp = lldb::ModuleWP(); 994 } 995 996 void 997 TypeImpl::SetType (const CompilerType &compiler_type) 998 { 999 m_module_wp = lldb::ModuleWP(); 1000 m_static_type.SetType (compiler_type); 1001 } 1002 1003 void 1004 TypeImpl::SetType (const lldb::TypeSP &type_sp, const CompilerType &dynamic) 1005 { 1006 SetType (type_sp); 1007 m_dynamic_type = dynamic; 1008 } 1009 1010 void 1011 TypeImpl::SetType (const CompilerType &compiler_type, const CompilerType &dynamic) 1012 { 1013 m_module_wp = lldb::ModuleWP(); 1014 m_static_type.SetType (compiler_type); 1015 m_dynamic_type = dynamic; 1016 } 1017 1018 void 1019 TypeImpl::SetType (const TypePair &pair, const CompilerType &dynamic) 1020 { 1021 m_module_wp = pair.GetModule(); 1022 m_static_type = pair; 1023 m_dynamic_type = dynamic; 1024 } 1025 1026 TypeImpl& 1027 TypeImpl::operator = (const TypeImpl& rhs) 1028 { 1029 if (rhs != *this) 1030 { 1031 m_module_wp = rhs.m_module_wp; 1032 m_static_type = rhs.m_static_type; 1033 m_dynamic_type = rhs.m_dynamic_type; 1034 } 1035 return *this; 1036 } 1037 1038 bool 1039 TypeImpl::CheckModule (lldb::ModuleSP &module_sp) const 1040 { 1041 // Check if we have a module for this type. If we do and the shared pointer is 1042 // can be successfully initialized with m_module_wp, return true. Else return false 1043 // if we didn't have a module, or if we had a module and it has been deleted. Any 1044 // functions doing anything with a TypeSP in this TypeImpl class should call this 1045 // function and only do anything with the ivars if this function returns true. If 1046 // we have a module, the "module_sp" will be filled in with a strong reference to the 1047 // module so that the module will at least stay around long enough for the type 1048 // query to succeed. 1049 module_sp = m_module_wp.lock(); 1050 if (!module_sp) 1051 { 1052 lldb::ModuleWP empty_module_wp; 1053 // If either call to "std::weak_ptr::owner_before(...) value returns true, this 1054 // indicates that m_module_wp once contained (possibly still does) a reference 1055 // to a valid shared pointer. This helps us know if we had a valid reference to 1056 // a section which is now invalid because the module it was in was deleted 1057 if (empty_module_wp.owner_before(m_module_wp) || m_module_wp.owner_before(empty_module_wp)) 1058 { 1059 // m_module_wp had a valid reference to a module, but all strong references 1060 // have been released and the module has been deleted 1061 return false; 1062 } 1063 } 1064 // We either successfully locked the module, or didn't have one to begin with 1065 return true; 1066 } 1067 1068 bool 1069 TypeImpl::operator == (const TypeImpl& rhs) const 1070 { 1071 return m_static_type == rhs.m_static_type && m_dynamic_type == rhs.m_dynamic_type; 1072 } 1073 1074 bool 1075 TypeImpl::operator != (const TypeImpl& rhs) const 1076 { 1077 return m_static_type != rhs.m_static_type || m_dynamic_type != rhs.m_dynamic_type; 1078 } 1079 1080 bool 1081 TypeImpl::IsValid() const 1082 { 1083 // just a name is not valid 1084 ModuleSP module_sp; 1085 if (CheckModule (module_sp)) 1086 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 1087 return false; 1088 } 1089 1090 TypeImpl::operator bool () const 1091 { 1092 return IsValid(); 1093 } 1094 1095 void 1096 TypeImpl::Clear() 1097 { 1098 m_module_wp = lldb::ModuleWP(); 1099 m_static_type.Clear(); 1100 m_dynamic_type.Clear(); 1101 } 1102 1103 ConstString 1104 TypeImpl::GetName () const 1105 { 1106 ModuleSP module_sp; 1107 if (CheckModule (module_sp)) 1108 { 1109 if (m_dynamic_type) 1110 return m_dynamic_type.GetTypeName(); 1111 return m_static_type.GetName (); 1112 } 1113 return ConstString(); 1114 } 1115 1116 ConstString 1117 TypeImpl::GetDisplayTypeName () const 1118 { 1119 ModuleSP module_sp; 1120 if (CheckModule (module_sp)) 1121 { 1122 if (m_dynamic_type) 1123 return m_dynamic_type.GetDisplayTypeName(); 1124 return m_static_type.GetDisplayTypeName(); 1125 } 1126 return ConstString(); 1127 } 1128 1129 TypeImpl 1130 TypeImpl::GetPointerType () const 1131 { 1132 ModuleSP module_sp; 1133 if (CheckModule (module_sp)) 1134 { 1135 if (m_dynamic_type.IsValid()) 1136 { 1137 return TypeImpl(m_static_type.GetPointerType(), m_dynamic_type.GetPointerType()); 1138 } 1139 return TypeImpl(m_static_type.GetPointerType()); 1140 } 1141 return TypeImpl(); 1142 } 1143 1144 TypeImpl 1145 TypeImpl::GetPointeeType () const 1146 { 1147 ModuleSP module_sp; 1148 if (CheckModule (module_sp)) 1149 { 1150 if (m_dynamic_type.IsValid()) 1151 { 1152 return TypeImpl(m_static_type.GetPointeeType(), m_dynamic_type.GetPointeeType()); 1153 } 1154 return TypeImpl(m_static_type.GetPointeeType()); 1155 } 1156 return TypeImpl(); 1157 } 1158 1159 TypeImpl 1160 TypeImpl::GetReferenceType () const 1161 { 1162 ModuleSP module_sp; 1163 if (CheckModule (module_sp)) 1164 { 1165 if (m_dynamic_type.IsValid()) 1166 { 1167 return TypeImpl(m_static_type.GetReferenceType(), m_dynamic_type.GetLValueReferenceType()); 1168 } 1169 return TypeImpl(m_static_type.GetReferenceType()); 1170 } 1171 return TypeImpl(); 1172 } 1173 1174 TypeImpl 1175 TypeImpl::GetTypedefedType () const 1176 { 1177 ModuleSP module_sp; 1178 if (CheckModule (module_sp)) 1179 { 1180 if (m_dynamic_type.IsValid()) 1181 { 1182 return TypeImpl(m_static_type.GetTypedefedType(), m_dynamic_type.GetTypedefedType()); 1183 } 1184 return TypeImpl(m_static_type.GetTypedefedType()); 1185 } 1186 return TypeImpl(); 1187 } 1188 1189 TypeImpl 1190 TypeImpl::GetDereferencedType () const 1191 { 1192 ModuleSP module_sp; 1193 if (CheckModule (module_sp)) 1194 { 1195 if (m_dynamic_type.IsValid()) 1196 { 1197 return TypeImpl(m_static_type.GetDereferencedType(), m_dynamic_type.GetNonReferenceType()); 1198 } 1199 return TypeImpl(m_static_type.GetDereferencedType()); 1200 } 1201 return TypeImpl(); 1202 } 1203 1204 TypeImpl 1205 TypeImpl::GetUnqualifiedType() const 1206 { 1207 ModuleSP module_sp; 1208 if (CheckModule (module_sp)) 1209 { 1210 if (m_dynamic_type.IsValid()) 1211 { 1212 return TypeImpl(m_static_type.GetUnqualifiedType(), m_dynamic_type.GetFullyUnqualifiedType()); 1213 } 1214 return TypeImpl(m_static_type.GetUnqualifiedType()); 1215 } 1216 return TypeImpl(); 1217 } 1218 1219 TypeImpl 1220 TypeImpl::GetCanonicalType() const 1221 { 1222 ModuleSP module_sp; 1223 if (CheckModule (module_sp)) 1224 { 1225 if (m_dynamic_type.IsValid()) 1226 { 1227 return TypeImpl(m_static_type.GetCanonicalType(), m_dynamic_type.GetCanonicalType()); 1228 } 1229 return TypeImpl(m_static_type.GetCanonicalType()); 1230 } 1231 return TypeImpl(); 1232 } 1233 1234 CompilerType 1235 TypeImpl::GetCompilerType (bool prefer_dynamic) 1236 { 1237 ModuleSP module_sp; 1238 if (CheckModule (module_sp)) 1239 { 1240 if (prefer_dynamic) 1241 { 1242 if (m_dynamic_type.IsValid()) 1243 return m_dynamic_type; 1244 } 1245 return m_static_type.GetCompilerType(); 1246 } 1247 return CompilerType(); 1248 } 1249 1250 TypeSystem * 1251 TypeImpl::GetTypeSystem (bool prefer_dynamic) 1252 { 1253 ModuleSP module_sp; 1254 if (CheckModule (module_sp)) 1255 { 1256 if (prefer_dynamic) 1257 { 1258 if (m_dynamic_type.IsValid()) 1259 return m_dynamic_type.GetTypeSystem(); 1260 } 1261 return m_static_type.GetCompilerType().GetTypeSystem(); 1262 } 1263 return NULL; 1264 } 1265 1266 bool 1267 TypeImpl::GetDescription (lldb_private::Stream &strm, 1268 lldb::DescriptionLevel description_level) 1269 { 1270 ModuleSP module_sp; 1271 if (CheckModule (module_sp)) 1272 { 1273 if (m_dynamic_type.IsValid()) 1274 { 1275 strm.Printf("Dynamic:\n"); 1276 m_dynamic_type.DumpTypeDescription(&strm); 1277 strm.Printf("\nStatic:\n"); 1278 } 1279 m_static_type.GetCompilerType().DumpTypeDescription(&strm); 1280 } 1281 else 1282 { 1283 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1284 } 1285 return true; 1286 } 1287 1288 bool 1289 TypeMemberFunctionImpl::IsValid () 1290 { 1291 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1292 } 1293 1294 ConstString 1295 TypeMemberFunctionImpl::GetName () const 1296 { 1297 return m_name; 1298 } 1299 1300 ConstString 1301 TypeMemberFunctionImpl::GetMangledName () const 1302 { 1303 return m_decl.GetMangledName(); 1304 } 1305 1306 CompilerType 1307 TypeMemberFunctionImpl::GetType () const 1308 { 1309 return m_type; 1310 } 1311 1312 lldb::MemberFunctionKind 1313 TypeMemberFunctionImpl::GetKind () const 1314 { 1315 return m_kind; 1316 } 1317 1318 bool 1319 TypeMemberFunctionImpl::GetDescription (Stream& stream) 1320 { 1321 switch (m_kind) { 1322 case lldb::eMemberFunctionKindUnknown: 1323 return false; 1324 case lldb::eMemberFunctionKindConstructor: 1325 stream.Printf("constructor for %s", m_type.GetTypeName().AsCString("<unknown>")); 1326 break; 1327 case lldb::eMemberFunctionKindDestructor: 1328 stream.Printf("destructor for %s", m_type.GetTypeName().AsCString("<unknown>")); 1329 break; 1330 case lldb::eMemberFunctionKindInstanceMethod: 1331 stream.Printf("instance method %s of type %s", 1332 m_name.AsCString(), 1333 m_decl.GetDeclContext().GetName().AsCString()); 1334 break; 1335 case lldb::eMemberFunctionKindStaticMethod: 1336 stream.Printf("static method %s of type %s", 1337 m_name.AsCString(), 1338 m_decl.GetDeclContext().GetName().AsCString()); 1339 break; 1340 } 1341 return true; 1342 } 1343 1344 CompilerType 1345 TypeMemberFunctionImpl::GetReturnType () const 1346 { 1347 if (m_type) 1348 return m_type.GetFunctionReturnType(); 1349 return m_decl.GetFunctionReturnType(); 1350 } 1351 1352 size_t 1353 TypeMemberFunctionImpl::GetNumArguments () const 1354 { 1355 if (m_type) 1356 return m_type.GetNumberOfFunctionArguments(); 1357 else 1358 return m_decl.GetNumFunctionArguments(); 1359 } 1360 1361 CompilerType 1362 TypeMemberFunctionImpl::GetArgumentAtIndex (size_t idx) const 1363 { 1364 if (m_type) 1365 return m_type.GetFunctionArgumentAtIndex (idx); 1366 else 1367 return m_decl.GetFunctionArgumentType(idx); 1368 } 1369 1370 TypeEnumMemberImpl::TypeEnumMemberImpl (const lldb::TypeImplSP &integer_type_sp, 1371 const ConstString &name, 1372 const llvm::APSInt &value) : 1373 m_integer_type_sp(integer_type_sp), 1374 m_name(name), 1375 m_value(value), 1376 m_valid((bool)name && (bool)integer_type_sp) 1377 1378 { 1379 } 1380