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 GetClangForwardType().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 GetClangForwardType().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 = GetClangForwardType().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 GetClangForwardType().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 = GetClangLayoutType().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 GetClangForwardType().GetNumChildren(omit_empty_base_classes); 359 } 360 361 bool 362 Type::IsAggregateType () 363 { 364 return GetClangForwardType().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 GetClangForwardType().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 GetClangForwardType().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->GetClangForwardType(); 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->GetClangForwardType()); 516 break; 517 518 case eEncodingIsRestrictUID: 519 m_clang_type = ClangASTContext::AddRestrictModifier(encoding_type->GetClangForwardType()); 520 break; 521 522 case eEncodingIsVolatileUID: 523 m_clang_type = ClangASTContext::AddVolatileModifier(encoding_type->GetClangForwardType()); 524 break; 525 526 case eEncodingIsTypedefUID: 527 m_clang_type = ClangASTContext::CreateTypedefType (encoding_type->GetClangForwardType(), 528 GetName().AsCString(), 529 GetSymbolFile()->GetClangDeclContextContainingTypeUID(GetID())); 530 m_name.Clear(); 531 break; 532 533 case eEncodingIsPointerUID: 534 m_clang_type = encoding_type->GetClangForwardType().GetPointerType(); 535 break; 536 537 case eEncodingIsLValueReferenceUID: 538 m_clang_type = ClangASTContext::GetLValueReferenceType(encoding_type->GetClangForwardType()); 539 break; 540 541 case eEncodingIsRValueReferenceUID: 542 m_clang_type = ClangASTContext::GetRValueReferenceType(encoding_type->GetClangForwardType()); 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()->GetClangDeclContextContainingTypeUID(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->ResolveClangOpaqueTypeDefinition (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::GetClangFullType () 660 { 661 ResolveClangType(eResolveStateFull); 662 return m_clang_type; 663 } 664 665 CompilerType 666 Type::GetClangLayoutType () 667 { 668 ResolveClangType(eResolveStateLayout); 669 return m_clang_type; 670 } 671 672 CompilerType 673 Type::GetClangForwardType () 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 // if (a.getQualType() == b.getQualType()) 697 // return 0; 698 } 699 700 701 #if 0 // START REMOVE 702 // Move this into CompilerType 703 void * 704 Type::CreateClangPointerType (Type *type) 705 { 706 assert(type); 707 return GetClangASTContext().CreatePointerType(type->GetClangForwardType()); 708 } 709 710 void * 711 Type::CreateClangTypedefType (Type *typedef_type, Type *base_type) 712 { 713 assert(typedef_type && base_type); 714 return GetClangASTContext().CreateTypedefType (typedef_type->GetName().AsCString(), 715 base_type->GetClangForwardType(), 716 typedef_type->GetSymbolFile()->GetClangDeclContextContainingTypeUID(typedef_type->GetID())); 717 } 718 719 void * 720 Type::CreateClangLValueReferenceType (Type *type) 721 { 722 assert(type); 723 return GetClangASTContext().CreateLValueReferenceType(type->GetClangForwardType()); 724 } 725 726 void * 727 Type::CreateClangRValueReferenceType (Type *type) 728 { 729 assert(type); 730 return GetClangASTContext().CreateRValueReferenceType (type->GetClangForwardType()); 731 } 732 #endif // END REMOVE 733 734 bool 735 Type::IsRealObjCClass() 736 { 737 // For now we are just skipping ObjC classes that get made by hand from the runtime, because 738 // those don't have any information. We could extend this to only return true for "full 739 // definitions" if we can figure that out. 740 741 if (ClangASTContext::IsObjCObjectOrInterfaceType(m_clang_type) && GetByteSize() != 0) 742 return true; 743 else 744 return false; 745 } 746 747 ConstString 748 Type::GetQualifiedName () 749 { 750 return GetClangForwardType().GetConstTypeName(); 751 } 752 753 754 bool 755 Type::GetTypeScopeAndBasename (const char* &name_cstr, 756 std::string &scope, 757 std::string &basename, 758 TypeClass &type_class) 759 { 760 // Protect against null c string. 761 762 type_class = eTypeClassAny; 763 764 if (name_cstr && name_cstr[0]) 765 { 766 llvm::StringRef name_strref(name_cstr); 767 if (name_strref.startswith("struct ")) 768 { 769 name_cstr += 7; 770 type_class = eTypeClassStruct; 771 } 772 else if (name_strref.startswith("class ")) 773 { 774 name_cstr += 6; 775 type_class = eTypeClassClass; 776 } 777 else if (name_strref.startswith("union ")) 778 { 779 name_cstr += 6; 780 type_class = eTypeClassUnion; 781 } 782 else if (name_strref.startswith("enum ")) 783 { 784 name_cstr += 5; 785 type_class = eTypeClassEnumeration; 786 } 787 else if (name_strref.startswith("typedef ")) 788 { 789 name_cstr += 8; 790 type_class = eTypeClassTypedef; 791 } 792 const char *basename_cstr = name_cstr; 793 const char* namespace_separator = ::strstr (basename_cstr, "::"); 794 if (namespace_separator) 795 { 796 const char* template_arg_char = ::strchr (basename_cstr, '<'); 797 while (namespace_separator != nullptr) 798 { 799 if (template_arg_char && namespace_separator > template_arg_char) // but namespace'd template arguments are still good to go 800 break; 801 basename_cstr = namespace_separator + 2; 802 namespace_separator = strstr(basename_cstr, "::"); 803 } 804 if (basename_cstr > name_cstr) 805 { 806 scope.assign (name_cstr, basename_cstr - name_cstr); 807 basename.assign (basename_cstr); 808 return true; 809 } 810 } 811 } 812 return false; 813 } 814 815 816 ModuleSP 817 Type::GetModule() 818 { 819 if (m_symbol_file) 820 return m_symbol_file->GetObjectFile()->GetModule(); 821 return ModuleSP(); 822 } 823 824 825 TypeAndOrName::TypeAndOrName () : m_type_pair(), m_type_name() 826 { 827 828 } 829 830 TypeAndOrName::TypeAndOrName (TypeSP &in_type_sp) : m_type_pair(in_type_sp) 831 { 832 if (in_type_sp) 833 m_type_name = in_type_sp->GetName(); 834 } 835 836 TypeAndOrName::TypeAndOrName (const char *in_type_str) : m_type_name(in_type_str) 837 { 838 } 839 840 TypeAndOrName::TypeAndOrName (const TypeAndOrName &rhs) : m_type_pair (rhs.m_type_pair), m_type_name (rhs.m_type_name) 841 { 842 843 } 844 845 TypeAndOrName::TypeAndOrName (ConstString &in_type_const_string) : m_type_name (in_type_const_string) 846 { 847 } 848 849 TypeAndOrName & 850 TypeAndOrName::operator= (const TypeAndOrName &rhs) 851 { 852 if (this != &rhs) 853 { 854 m_type_name = rhs.m_type_name; 855 m_type_pair = rhs.m_type_pair; 856 } 857 return *this; 858 } 859 860 bool 861 TypeAndOrName::operator==(const TypeAndOrName &other) const 862 { 863 if (m_type_pair != other.m_type_pair) 864 return false; 865 if (m_type_name != other.m_type_name) 866 return false; 867 return true; 868 } 869 870 bool 871 TypeAndOrName::operator!=(const TypeAndOrName &other) const 872 { 873 if (m_type_pair != other.m_type_pair) 874 return true; 875 if (m_type_name != other.m_type_name) 876 return true; 877 return false; 878 } 879 880 ConstString 881 TypeAndOrName::GetName () const 882 { 883 if (m_type_name) 884 return m_type_name; 885 if (m_type_pair) 886 return m_type_pair.GetName(); 887 return ConstString("<invalid>"); 888 } 889 890 void 891 TypeAndOrName::SetName (const ConstString &type_name) 892 { 893 m_type_name = type_name; 894 } 895 896 void 897 TypeAndOrName::SetName (const char *type_name_cstr) 898 { 899 m_type_name.SetCString (type_name_cstr); 900 } 901 902 void 903 TypeAndOrName::SetTypeSP (lldb::TypeSP type_sp) 904 { 905 m_type_pair.SetType(type_sp); 906 if (m_type_pair) 907 m_type_name = m_type_pair.GetName(); 908 } 909 910 void 911 TypeAndOrName::SetCompilerType (CompilerType clang_type) 912 { 913 m_type_pair.SetType(clang_type); 914 if (m_type_pair) 915 m_type_name = m_type_pair.GetName(); 916 } 917 918 bool 919 TypeAndOrName::IsEmpty() const 920 { 921 if ((bool)m_type_name || (bool)m_type_pair) 922 return false; 923 else 924 return true; 925 } 926 927 void 928 TypeAndOrName::Clear () 929 { 930 m_type_name.Clear(); 931 m_type_pair.Clear(); 932 } 933 934 bool 935 TypeAndOrName::HasName () const 936 { 937 return (bool)m_type_name; 938 } 939 940 bool 941 TypeAndOrName::HasTypeSP () const 942 { 943 return m_type_pair.GetTypeSP().get() != nullptr; 944 } 945 946 bool 947 TypeAndOrName::HasCompilerType () const 948 { 949 return m_type_pair.GetCompilerType().IsValid(); 950 } 951 952 953 TypeImpl::TypeImpl() : 954 m_module_wp(), 955 m_static_type(), 956 m_dynamic_type() 957 { 958 } 959 960 TypeImpl::TypeImpl(const TypeImpl& rhs) : 961 m_module_wp (rhs.m_module_wp), 962 m_static_type(rhs.m_static_type), 963 m_dynamic_type(rhs.m_dynamic_type) 964 { 965 } 966 967 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp) : 968 m_module_wp (), 969 m_static_type(), 970 m_dynamic_type() 971 { 972 SetType (type_sp); 973 } 974 975 TypeImpl::TypeImpl (const CompilerType &clang_type) : 976 m_module_wp (), 977 m_static_type(), 978 m_dynamic_type() 979 { 980 SetType (clang_type); 981 } 982 983 TypeImpl::TypeImpl (const lldb::TypeSP &type_sp, const CompilerType &dynamic) : 984 m_module_wp (), 985 m_static_type (type_sp), 986 m_dynamic_type(dynamic) 987 { 988 SetType (type_sp, dynamic); 989 } 990 991 TypeImpl::TypeImpl (const CompilerType &static_type, const CompilerType &dynamic_type) : 992 m_module_wp (), 993 m_static_type (), 994 m_dynamic_type() 995 { 996 SetType (static_type, dynamic_type); 997 } 998 999 TypeImpl::TypeImpl (const TypePair &pair, const CompilerType &dynamic) : 1000 m_module_wp (), 1001 m_static_type (), 1002 m_dynamic_type() 1003 { 1004 SetType (pair, dynamic); 1005 } 1006 1007 void 1008 TypeImpl::SetType (const lldb::TypeSP &type_sp) 1009 { 1010 m_static_type.SetType(type_sp); 1011 if (type_sp) 1012 m_module_wp = type_sp->GetModule(); 1013 else 1014 m_module_wp = lldb::ModuleWP(); 1015 } 1016 1017 void 1018 TypeImpl::SetType (const CompilerType &clang_type) 1019 { 1020 m_module_wp = lldb::ModuleWP(); 1021 m_static_type.SetType (clang_type); 1022 } 1023 1024 void 1025 TypeImpl::SetType (const lldb::TypeSP &type_sp, const CompilerType &dynamic) 1026 { 1027 SetType (type_sp); 1028 m_dynamic_type = dynamic; 1029 } 1030 1031 void 1032 TypeImpl::SetType (const CompilerType &clang_type, const CompilerType &dynamic) 1033 { 1034 m_module_wp = lldb::ModuleWP(); 1035 m_static_type.SetType (clang_type); 1036 m_dynamic_type = dynamic; 1037 } 1038 1039 void 1040 TypeImpl::SetType (const TypePair &pair, const CompilerType &dynamic) 1041 { 1042 m_module_wp = pair.GetModule(); 1043 m_static_type = pair; 1044 m_dynamic_type = dynamic; 1045 } 1046 1047 TypeImpl& 1048 TypeImpl::operator = (const TypeImpl& rhs) 1049 { 1050 if (rhs != *this) 1051 { 1052 m_module_wp = rhs.m_module_wp; 1053 m_static_type = rhs.m_static_type; 1054 m_dynamic_type = rhs.m_dynamic_type; 1055 } 1056 return *this; 1057 } 1058 1059 bool 1060 TypeImpl::CheckModule (lldb::ModuleSP &module_sp) const 1061 { 1062 // Check if we have a module for this type. If we do and the shared pointer is 1063 // can be successfully initialized with m_module_wp, return true. Else return false 1064 // if we didn't have a module, or if we had a module and it has been deleted. Any 1065 // functions doing anything with a TypeSP in this TypeImpl class should call this 1066 // function and only do anything with the ivars if this function returns true. If 1067 // we have a module, the "module_sp" will be filled in with a strong reference to the 1068 // module so that the module will at least stay around long enough for the type 1069 // query to succeed. 1070 module_sp = m_module_wp.lock(); 1071 if (!module_sp) 1072 { 1073 lldb::ModuleWP empty_module_wp; 1074 // If either call to "std::weak_ptr::owner_before(...) value returns true, this 1075 // indicates that m_module_wp once contained (possibly still does) a reference 1076 // to a valid shared pointer. This helps us know if we had a valid reference to 1077 // a section which is now invalid because the module it was in was deleted 1078 if (empty_module_wp.owner_before(m_module_wp) || m_module_wp.owner_before(empty_module_wp)) 1079 { 1080 // m_module_wp had a valid reference to a module, but all strong references 1081 // have been released and the module has been deleted 1082 return false; 1083 } 1084 } 1085 // We either successfully locked the module, or didn't have one to begin with 1086 return true; 1087 } 1088 1089 bool 1090 TypeImpl::operator == (const TypeImpl& rhs) const 1091 { 1092 return m_static_type == rhs.m_static_type && m_dynamic_type == rhs.m_dynamic_type; 1093 } 1094 1095 bool 1096 TypeImpl::operator != (const TypeImpl& rhs) const 1097 { 1098 return m_static_type != rhs.m_static_type || m_dynamic_type != rhs.m_dynamic_type; 1099 } 1100 1101 bool 1102 TypeImpl::IsValid() const 1103 { 1104 // just a name is not valid 1105 ModuleSP module_sp; 1106 if (CheckModule (module_sp)) 1107 return m_static_type.IsValid() || m_dynamic_type.IsValid(); 1108 return false; 1109 } 1110 1111 TypeImpl::operator bool () const 1112 { 1113 return IsValid(); 1114 } 1115 1116 void 1117 TypeImpl::Clear() 1118 { 1119 m_module_wp = lldb::ModuleWP(); 1120 m_static_type.Clear(); 1121 m_dynamic_type.Clear(); 1122 } 1123 1124 ConstString 1125 TypeImpl::GetName () const 1126 { 1127 ModuleSP module_sp; 1128 if (CheckModule (module_sp)) 1129 { 1130 if (m_dynamic_type) 1131 return m_dynamic_type.GetTypeName(); 1132 return m_static_type.GetName (); 1133 } 1134 return ConstString(); 1135 } 1136 1137 ConstString 1138 TypeImpl::GetDisplayTypeName () const 1139 { 1140 ModuleSP module_sp; 1141 if (CheckModule (module_sp)) 1142 { 1143 if (m_dynamic_type) 1144 return m_dynamic_type.GetDisplayTypeName(); 1145 return m_static_type.GetDisplayTypeName(); 1146 } 1147 return ConstString(); 1148 } 1149 1150 TypeImpl 1151 TypeImpl::GetPointerType () const 1152 { 1153 ModuleSP module_sp; 1154 if (CheckModule (module_sp)) 1155 { 1156 if (m_dynamic_type.IsValid()) 1157 { 1158 return TypeImpl(m_static_type.GetPointerType(), m_dynamic_type.GetPointerType()); 1159 } 1160 return TypeImpl(m_static_type.GetPointerType()); 1161 } 1162 return TypeImpl(); 1163 } 1164 1165 TypeImpl 1166 TypeImpl::GetPointeeType () const 1167 { 1168 ModuleSP module_sp; 1169 if (CheckModule (module_sp)) 1170 { 1171 if (m_dynamic_type.IsValid()) 1172 { 1173 return TypeImpl(m_static_type.GetPointeeType(), m_dynamic_type.GetPointeeType()); 1174 } 1175 return TypeImpl(m_static_type.GetPointeeType()); 1176 } 1177 return TypeImpl(); 1178 } 1179 1180 TypeImpl 1181 TypeImpl::GetReferenceType () const 1182 { 1183 ModuleSP module_sp; 1184 if (CheckModule (module_sp)) 1185 { 1186 if (m_dynamic_type.IsValid()) 1187 { 1188 return TypeImpl(m_static_type.GetReferenceType(), ClangASTContext::GetLValueReferenceType(m_dynamic_type)); 1189 } 1190 return TypeImpl(m_static_type.GetReferenceType()); 1191 } 1192 return TypeImpl(); 1193 } 1194 1195 TypeImpl 1196 TypeImpl::GetTypedefedType () const 1197 { 1198 ModuleSP module_sp; 1199 if (CheckModule (module_sp)) 1200 { 1201 if (m_dynamic_type.IsValid()) 1202 { 1203 return TypeImpl(m_static_type.GetTypedefedType(), m_dynamic_type.GetTypedefedType()); 1204 } 1205 return TypeImpl(m_static_type.GetTypedefedType()); 1206 } 1207 return TypeImpl(); 1208 } 1209 1210 TypeImpl 1211 TypeImpl::GetDereferencedType () const 1212 { 1213 ModuleSP module_sp; 1214 if (CheckModule (module_sp)) 1215 { 1216 if (m_dynamic_type.IsValid()) 1217 { 1218 return TypeImpl(m_static_type.GetDereferencedType(), m_dynamic_type.GetNonReferenceType()); 1219 } 1220 return TypeImpl(m_static_type.GetDereferencedType()); 1221 } 1222 return TypeImpl(); 1223 } 1224 1225 TypeImpl 1226 TypeImpl::GetUnqualifiedType() const 1227 { 1228 ModuleSP module_sp; 1229 if (CheckModule (module_sp)) 1230 { 1231 if (m_dynamic_type.IsValid()) 1232 { 1233 return TypeImpl(m_static_type.GetUnqualifiedType(), m_dynamic_type.GetFullyUnqualifiedType()); 1234 } 1235 return TypeImpl(m_static_type.GetUnqualifiedType()); 1236 } 1237 return TypeImpl(); 1238 } 1239 1240 TypeImpl 1241 TypeImpl::GetCanonicalType() const 1242 { 1243 ModuleSP module_sp; 1244 if (CheckModule (module_sp)) 1245 { 1246 if (m_dynamic_type.IsValid()) 1247 { 1248 return TypeImpl(m_static_type.GetCanonicalType(), m_dynamic_type.GetCanonicalType()); 1249 } 1250 return TypeImpl(m_static_type.GetCanonicalType()); 1251 } 1252 return TypeImpl(); 1253 } 1254 1255 CompilerType 1256 TypeImpl::GetCompilerType (bool prefer_dynamic) 1257 { 1258 ModuleSP module_sp; 1259 if (CheckModule (module_sp)) 1260 { 1261 if (prefer_dynamic) 1262 { 1263 if (m_dynamic_type.IsValid()) 1264 return m_dynamic_type; 1265 } 1266 return m_static_type.GetCompilerType(); 1267 } 1268 return CompilerType(); 1269 } 1270 1271 TypeSystem * 1272 TypeImpl::GetTypeSystem (bool prefer_dynamic) 1273 { 1274 ModuleSP module_sp; 1275 if (CheckModule (module_sp)) 1276 { 1277 if (prefer_dynamic) 1278 { 1279 if (m_dynamic_type.IsValid()) 1280 return m_dynamic_type.GetTypeSystem(); 1281 } 1282 return m_static_type.GetCompilerType().GetTypeSystem(); 1283 } 1284 return NULL; 1285 } 1286 1287 bool 1288 TypeImpl::GetDescription (lldb_private::Stream &strm, 1289 lldb::DescriptionLevel description_level) 1290 { 1291 ModuleSP module_sp; 1292 if (CheckModule (module_sp)) 1293 { 1294 if (m_dynamic_type.IsValid()) 1295 { 1296 strm.Printf("Dynamic:\n"); 1297 m_dynamic_type.DumpTypeDescription(&strm); 1298 strm.Printf("\nStatic:\n"); 1299 } 1300 m_static_type.GetCompilerType().DumpTypeDescription(&strm); 1301 } 1302 else 1303 { 1304 strm.PutCString("Invalid TypeImpl module for type has been deleted\n"); 1305 } 1306 return true; 1307 } 1308 1309 TypeMemberFunctionImpl& 1310 TypeMemberFunctionImpl::operator = (const TypeMemberFunctionImpl& rhs) 1311 { 1312 if (this != &rhs) 1313 { 1314 m_type = rhs.m_type; 1315 m_objc_method_decl = rhs.m_objc_method_decl; 1316 m_name = rhs.m_name; 1317 m_kind = rhs.m_kind; 1318 } 1319 return *this; 1320 } 1321 1322 bool 1323 TypeMemberFunctionImpl::IsValid () 1324 { 1325 return m_type.IsValid() && m_kind != lldb::eMemberFunctionKindUnknown; 1326 } 1327 1328 ConstString 1329 TypeMemberFunctionImpl::GetName () const 1330 { 1331 return m_name; 1332 } 1333 1334 CompilerType 1335 TypeMemberFunctionImpl::GetType () const 1336 { 1337 return m_type; 1338 } 1339 1340 lldb::MemberFunctionKind 1341 TypeMemberFunctionImpl::GetKind () const 1342 { 1343 return m_kind; 1344 } 1345 1346 std::string 1347 TypeMemberFunctionImpl::GetPrintableTypeName () 1348 { 1349 if (m_type) 1350 return m_type.GetTypeName().AsCString("<unknown>"); 1351 if (m_objc_method_decl) 1352 { 1353 if (m_objc_method_decl->getClassInterface()) 1354 { 1355 return m_objc_method_decl->getClassInterface()->getName(); 1356 } 1357 } 1358 return "<unknown>"; 1359 } 1360 1361 bool 1362 TypeMemberFunctionImpl::GetDescription (Stream& stream) 1363 { 1364 switch (m_kind) { 1365 case lldb::eMemberFunctionKindUnknown: 1366 return false; 1367 case lldb::eMemberFunctionKindConstructor: 1368 stream.Printf("constructor for %s", GetPrintableTypeName().c_str()); 1369 break; 1370 case lldb::eMemberFunctionKindDestructor: 1371 stream.Printf("destructor for %s", GetPrintableTypeName().c_str()); 1372 break; 1373 case lldb::eMemberFunctionKindInstanceMethod: 1374 stream.Printf("instance method %s of type %s", 1375 m_name.AsCString(), 1376 GetPrintableTypeName().c_str()); 1377 break; 1378 case lldb::eMemberFunctionKindStaticMethod: 1379 stream.Printf("static method %s of type %s", 1380 m_name.AsCString(), 1381 GetPrintableTypeName().c_str()); 1382 break; 1383 } 1384 return true; 1385 } 1386 1387 CompilerType 1388 TypeMemberFunctionImpl::GetReturnType () const 1389 { 1390 if (m_type) 1391 return m_type.GetFunctionReturnType(); 1392 if (m_objc_method_decl) 1393 return CompilerType(&m_objc_method_decl->getASTContext(), m_objc_method_decl->getReturnType()); 1394 return CompilerType(); 1395 } 1396 1397 size_t 1398 TypeMemberFunctionImpl::GetNumArguments () const 1399 { 1400 if (m_type) 1401 return m_type.GetNumberOfFunctionArguments(); 1402 if (m_objc_method_decl) 1403 return m_objc_method_decl->param_size(); 1404 return 0; 1405 } 1406 1407 CompilerType 1408 TypeMemberFunctionImpl::GetArgumentAtIndex (size_t idx) const 1409 { 1410 if (m_type) 1411 return m_type.GetFunctionArgumentAtIndex (idx); 1412 if (m_objc_method_decl) 1413 { 1414 if (idx < m_objc_method_decl->param_size()) 1415 return CompilerType(&m_objc_method_decl->getASTContext(), m_objc_method_decl->parameters()[idx]->getOriginalType()); 1416 } 1417 return CompilerType(); 1418 } 1419 1420 TypeEnumMemberImpl::TypeEnumMemberImpl (const clang::EnumConstantDecl* enum_member_decl, 1421 const lldb_private::CompilerType& integer_type) : 1422 m_integer_type_sp(), 1423 m_name(), 1424 m_value(), 1425 m_valid(false) 1426 1427 { 1428 if (enum_member_decl) 1429 { 1430 m_integer_type_sp.reset(new TypeImpl(integer_type)); 1431 m_name = ConstString(enum_member_decl->getNameAsString().c_str()); 1432 m_value = enum_member_decl->getInitVal(); 1433 m_valid = true; 1434 } 1435 } 1436