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