1 //===-- CompilerType.cpp ----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #include "lldb/Symbol/CompilerType.h" 11 12 #include "lldb/Core/ConstString.h" 13 #include "lldb/Core/DataBufferHeap.h" 14 #include "lldb/Core/DataExtractor.h" 15 #include "lldb/Core/Debugger.h" 16 #include "lldb/Core/Scalar.h" 17 #include "lldb/Core/Stream.h" 18 #include "lldb/Core/StreamFile.h" 19 #include "lldb/Core/StreamString.h" 20 #include "lldb/Symbol/ClangASTContext.h" 21 #include "lldb/Symbol/ClangExternalASTSourceCommon.h" 22 #include "lldb/Symbol/Type.h" 23 #include "lldb/Target/ExecutionContext.h" 24 #include "lldb/Target/Process.h" 25 26 #include <iterator> 27 #include <mutex> 28 29 using namespace lldb; 30 using namespace lldb_private; 31 32 CompilerType::CompilerType (TypeSystem *type_system, 33 void* type) : 34 m_type (type), 35 m_type_system (type_system) 36 { 37 } 38 39 CompilerType::CompilerType (clang::ASTContext *ast, 40 clang::QualType qual_type) : 41 m_type (qual_type.getAsOpaquePtr()), 42 m_type_system (ClangASTContext::GetASTContext(ast)) 43 { 44 #ifdef LLDB_CONFIGURATION_DEBUG 45 if (m_type) 46 assert(m_type_system != nullptr); 47 #endif 48 } 49 50 CompilerType::~CompilerType() 51 { 52 } 53 54 //---------------------------------------------------------------------- 55 // Tests 56 //---------------------------------------------------------------------- 57 58 bool 59 CompilerType::IsAggregateType () const 60 { 61 if (IsValid()) 62 return m_type_system->IsAggregateType(m_type); 63 return false; 64 } 65 66 bool 67 CompilerType::IsArrayType (CompilerType *element_type_ptr, 68 uint64_t *size, 69 bool *is_incomplete) const 70 { 71 if (IsValid()) 72 return m_type_system->IsArrayType(m_type, element_type_ptr, size, is_incomplete); 73 74 if (element_type_ptr) 75 element_type_ptr->Clear(); 76 if (size) 77 *size = 0; 78 if (is_incomplete) 79 *is_incomplete = false; 80 return false; 81 } 82 83 bool 84 CompilerType::IsVectorType (CompilerType *element_type, 85 uint64_t *size) const 86 { 87 if (IsValid()) 88 return m_type_system->IsVectorType(m_type, element_type, size); 89 return false; 90 } 91 92 bool 93 CompilerType::IsRuntimeGeneratedType () const 94 { 95 if (IsValid()) 96 return m_type_system->IsRuntimeGeneratedType(m_type); 97 return false; 98 } 99 100 bool 101 CompilerType::IsCharType () const 102 { 103 if (IsValid()) 104 return m_type_system->IsCharType(m_type); 105 return false; 106 } 107 108 109 bool 110 CompilerType::IsCompleteType () const 111 { 112 if (IsValid()) 113 return m_type_system->IsCompleteType(m_type); 114 return false; 115 } 116 117 bool 118 CompilerType::IsConst() const 119 { 120 if (IsValid()) 121 return m_type_system->IsConst(m_type); 122 return false; 123 } 124 125 bool 126 CompilerType::IsCStringType (uint32_t &length) const 127 { 128 if (IsValid()) 129 return m_type_system->IsCStringType(m_type, length); 130 return false; 131 } 132 133 bool 134 CompilerType::IsFunctionType (bool *is_variadic_ptr) const 135 { 136 if (IsValid()) 137 return m_type_system->IsFunctionType(m_type, is_variadic_ptr); 138 return false; 139 } 140 141 // Used to detect "Homogeneous Floating-point Aggregates" 142 uint32_t 143 CompilerType::IsHomogeneousAggregate (CompilerType* base_type_ptr) const 144 { 145 if (IsValid()) 146 return m_type_system->IsHomogeneousAggregate(m_type, base_type_ptr); 147 return 0; 148 } 149 150 size_t 151 CompilerType::GetNumberOfFunctionArguments () const 152 { 153 if (IsValid()) 154 return m_type_system->GetNumberOfFunctionArguments(m_type); 155 return 0; 156 } 157 158 CompilerType 159 CompilerType::GetFunctionArgumentAtIndex (const size_t index) const 160 { 161 if (IsValid()) 162 return m_type_system->GetFunctionArgumentAtIndex(m_type, index); 163 return CompilerType(); 164 } 165 166 bool 167 CompilerType::IsFunctionPointerType () const 168 { 169 if (IsValid()) 170 return m_type_system->IsFunctionPointerType(m_type); 171 return false; 172 173 } 174 175 bool 176 CompilerType::IsIntegerType (bool &is_signed) const 177 { 178 if (IsValid()) 179 return m_type_system->IsIntegerType(m_type, is_signed); 180 return false; 181 } 182 183 bool 184 CompilerType::IsPointerType (CompilerType *pointee_type) const 185 { 186 if (IsValid()) 187 { 188 return m_type_system->IsPointerType(m_type, pointee_type); 189 } 190 if (pointee_type) 191 pointee_type->Clear(); 192 return false; 193 } 194 195 196 bool 197 CompilerType::IsPointerOrReferenceType (CompilerType *pointee_type) const 198 { 199 if (IsValid()) 200 { 201 return m_type_system->IsPointerOrReferenceType(m_type, pointee_type); 202 } 203 if (pointee_type) 204 pointee_type->Clear(); 205 return false; 206 } 207 208 209 bool 210 CompilerType::IsReferenceType (CompilerType *pointee_type, bool* is_rvalue) const 211 { 212 if (IsValid()) 213 { 214 return m_type_system->IsReferenceType(m_type, pointee_type, is_rvalue); 215 } 216 if (pointee_type) 217 pointee_type->Clear(); 218 return false; 219 } 220 221 bool 222 CompilerType::IsFloatingPointType (uint32_t &count, bool &is_complex) const 223 { 224 if (IsValid()) 225 { 226 return m_type_system->IsFloatingPointType(m_type, count, is_complex); 227 } 228 count = 0; 229 is_complex = false; 230 return false; 231 } 232 233 234 bool 235 CompilerType::IsDefined() const 236 { 237 if (IsValid()) 238 return m_type_system->IsDefined(m_type); 239 return true; 240 } 241 242 bool 243 CompilerType::IsPolymorphicClass () const 244 { 245 if (IsValid()) 246 { 247 return m_type_system->IsPolymorphicClass(m_type); 248 } 249 return false; 250 } 251 252 bool 253 CompilerType::IsPossibleDynamicType (CompilerType *dynamic_pointee_type, 254 bool check_cplusplus, 255 bool check_objc) const 256 { 257 if (IsValid()) 258 return m_type_system->IsPossibleDynamicType(m_type, dynamic_pointee_type, check_cplusplus, check_objc); 259 return false; 260 } 261 262 263 bool 264 CompilerType::IsScalarType () const 265 { 266 if (!IsValid()) 267 return false; 268 269 return m_type_system->IsScalarType(m_type); 270 } 271 272 bool 273 CompilerType::IsTypedefType () const 274 { 275 if (!IsValid()) 276 return false; 277 return m_type_system->IsTypedefType(m_type); 278 } 279 280 bool 281 CompilerType::IsVoidType () const 282 { 283 if (!IsValid()) 284 return false; 285 return m_type_system->IsVoidType(m_type); 286 } 287 288 bool 289 CompilerType::IsPointerToScalarType () const 290 { 291 if (!IsValid()) 292 return false; 293 294 return IsPointerType() && GetPointeeType().IsScalarType(); 295 } 296 297 bool 298 CompilerType::IsArrayOfScalarType () const 299 { 300 CompilerType element_type; 301 if (IsArrayType(&element_type, nullptr, nullptr)) 302 return element_type.IsScalarType(); 303 return false; 304 } 305 306 bool 307 CompilerType::IsBeingDefined () const 308 { 309 if (!IsValid()) 310 return false; 311 return m_type_system->IsBeingDefined(m_type); 312 } 313 314 //---------------------------------------------------------------------- 315 // Type Completion 316 //---------------------------------------------------------------------- 317 318 bool 319 CompilerType::GetCompleteType () const 320 { 321 if (!IsValid()) 322 return false; 323 return m_type_system->GetCompleteType(m_type); 324 } 325 326 //---------------------------------------------------------------------- 327 // AST related queries 328 //---------------------------------------------------------------------- 329 size_t 330 CompilerType::GetPointerByteSize () const 331 { 332 if (m_type_system) 333 return m_type_system->GetPointerByteSize(); 334 return 0; 335 } 336 337 ConstString 338 CompilerType::GetConstQualifiedTypeName () const 339 { 340 return GetConstTypeName (); 341 } 342 343 ConstString 344 CompilerType::GetConstTypeName () const 345 { 346 if (IsValid()) 347 { 348 ConstString type_name (GetTypeName()); 349 if (type_name) 350 return type_name; 351 } 352 return ConstString("<invalid>"); 353 } 354 355 ConstString 356 CompilerType::GetTypeName () const 357 { 358 if (IsValid()) 359 { 360 return m_type_system->GetTypeName(m_type); 361 } 362 return ConstString("<invalid>"); 363 } 364 365 ConstString 366 CompilerType::GetDisplayTypeName () const 367 { 368 return GetTypeName(); 369 } 370 371 uint32_t 372 CompilerType::GetTypeInfo (CompilerType *pointee_or_element_clang_type) const 373 { 374 if (!IsValid()) 375 return 0; 376 377 return m_type_system->GetTypeInfo(m_type, pointee_or_element_clang_type); 378 } 379 380 381 382 lldb::LanguageType 383 CompilerType::GetMinimumLanguage () 384 { 385 if (!IsValid()) 386 return lldb::eLanguageTypeC; 387 388 return m_type_system->GetMinimumLanguage(m_type); 389 } 390 391 lldb::TypeClass 392 CompilerType::GetTypeClass () const 393 { 394 if (!IsValid()) 395 return lldb::eTypeClassInvalid; 396 397 return m_type_system->GetTypeClass(m_type); 398 399 } 400 401 void 402 CompilerType::SetCompilerType (TypeSystem* type_system, void* type) 403 { 404 m_type_system = type_system; 405 m_type = type; 406 } 407 408 void 409 CompilerType::SetCompilerType (clang::ASTContext *ast, clang::QualType qual_type) 410 { 411 m_type_system = ClangASTContext::GetASTContext(ast); 412 m_type = qual_type.getAsOpaquePtr(); 413 } 414 415 unsigned 416 CompilerType::GetTypeQualifiers() const 417 { 418 if (IsValid()) 419 return m_type_system->GetTypeQualifiers(m_type); 420 return 0; 421 } 422 423 //---------------------------------------------------------------------- 424 // Creating related types 425 //---------------------------------------------------------------------- 426 427 CompilerType 428 CompilerType::GetArrayElementType (uint64_t *stride) const 429 { 430 if (IsValid()) 431 { 432 return m_type_system->GetArrayElementType(m_type, stride); 433 434 } 435 return CompilerType(); 436 } 437 438 CompilerType 439 CompilerType::GetCanonicalType () const 440 { 441 if (IsValid()) 442 return m_type_system->GetCanonicalType(m_type); 443 return CompilerType(); 444 } 445 446 CompilerType 447 CompilerType::GetFullyUnqualifiedType () const 448 { 449 if (IsValid()) 450 return m_type_system->GetFullyUnqualifiedType(m_type); 451 return CompilerType(); 452 } 453 454 455 int 456 CompilerType::GetFunctionArgumentCount () const 457 { 458 if (IsValid()) 459 { 460 return m_type_system->GetFunctionArgumentCount(m_type); 461 } 462 return -1; 463 } 464 465 CompilerType 466 CompilerType::GetFunctionArgumentTypeAtIndex (size_t idx) const 467 { 468 if (IsValid()) 469 { 470 return m_type_system->GetFunctionArgumentTypeAtIndex(m_type, idx); 471 } 472 return CompilerType(); 473 } 474 475 CompilerType 476 CompilerType::GetFunctionReturnType () const 477 { 478 if (IsValid()) 479 { 480 return m_type_system->GetFunctionReturnType(m_type); 481 } 482 return CompilerType(); 483 } 484 485 size_t 486 CompilerType::GetNumMemberFunctions () const 487 { 488 if (IsValid()) 489 { 490 return m_type_system->GetNumMemberFunctions(m_type); 491 } 492 return 0; 493 } 494 495 TypeMemberFunctionImpl 496 CompilerType::GetMemberFunctionAtIndex (size_t idx) 497 { 498 if (IsValid()) 499 { 500 return m_type_system->GetMemberFunctionAtIndex(m_type, idx); 501 } 502 return TypeMemberFunctionImpl(); 503 } 504 505 CompilerType 506 CompilerType::GetNonReferenceType () const 507 { 508 if (IsValid()) 509 return m_type_system->GetNonReferenceType(m_type); 510 return CompilerType(); 511 } 512 513 CompilerType 514 CompilerType::GetPointeeType () const 515 { 516 if (IsValid()) 517 { 518 return m_type_system->GetPointeeType(m_type); 519 } 520 return CompilerType(); 521 } 522 523 CompilerType 524 CompilerType::GetPointerType () const 525 { 526 if (IsValid()) 527 { 528 return m_type_system->GetPointerType(m_type); 529 } 530 return CompilerType(); 531 } 532 533 CompilerType 534 CompilerType::GetLValueReferenceType () const 535 { 536 if (IsValid()) 537 return m_type_system->GetLValueReferenceType(m_type); 538 else 539 return CompilerType(); 540 } 541 542 CompilerType 543 CompilerType::GetRValueReferenceType () const 544 { 545 if (IsValid()) 546 return m_type_system->GetRValueReferenceType(m_type); 547 else 548 return CompilerType(); 549 } 550 551 CompilerType 552 CompilerType::AddConstModifier () const 553 { 554 if (IsValid()) 555 return m_type_system->AddConstModifier(m_type); 556 else 557 return CompilerType(); 558 } 559 560 CompilerType 561 CompilerType::AddVolatileModifier () const 562 { 563 if (IsValid()) 564 return m_type_system->AddVolatileModifier(m_type); 565 else 566 return CompilerType(); 567 } 568 569 CompilerType 570 CompilerType::AddRestrictModifier () const 571 { 572 if (IsValid()) 573 return m_type_system->AddRestrictModifier(m_type); 574 else 575 return CompilerType(); 576 } 577 578 CompilerType 579 CompilerType::CreateTypedef (const char *name, const CompilerDeclContext &decl_ctx) const 580 { 581 if (IsValid()) 582 return m_type_system->CreateTypedef(m_type, name, decl_ctx); 583 else 584 return CompilerType(); 585 } 586 587 CompilerType 588 CompilerType::GetTypedefedType () const 589 { 590 if (IsValid()) 591 return m_type_system->GetTypedefedType(m_type); 592 else 593 return CompilerType(); 594 } 595 596 //---------------------------------------------------------------------- 597 // Create related types using the current type's AST 598 //---------------------------------------------------------------------- 599 600 CompilerType 601 CompilerType::GetBasicTypeFromAST (lldb::BasicType basic_type) const 602 { 603 if (IsValid()) 604 return m_type_system->GetBasicTypeFromAST(basic_type); 605 return CompilerType(); 606 } 607 //---------------------------------------------------------------------- 608 // Exploring the type 609 //---------------------------------------------------------------------- 610 611 uint64_t 612 CompilerType::GetBitSize (ExecutionContextScope *exe_scope) const 613 { 614 if (IsValid()) 615 { 616 return m_type_system->GetBitSize(m_type, exe_scope); 617 } 618 return 0; 619 } 620 621 uint64_t 622 CompilerType::GetByteSize (ExecutionContextScope *exe_scope) const 623 { 624 return (GetBitSize (exe_scope) + 7) / 8; 625 } 626 627 628 size_t 629 CompilerType::GetTypeBitAlign () const 630 { 631 if (IsValid()) 632 return m_type_system->GetTypeBitAlign(m_type); 633 return 0; 634 } 635 636 637 lldb::Encoding 638 CompilerType::GetEncoding (uint64_t &count) const 639 { 640 if (!IsValid()) 641 return lldb::eEncodingInvalid; 642 643 return m_type_system->GetEncoding(m_type, count); 644 } 645 646 lldb::Format 647 CompilerType::GetFormat () const 648 { 649 if (!IsValid()) 650 return lldb::eFormatDefault; 651 652 return m_type_system->GetFormat(m_type); 653 } 654 655 uint32_t 656 CompilerType::GetNumChildren (bool omit_empty_base_classes) const 657 { 658 if (!IsValid()) 659 return 0; 660 return m_type_system->GetNumChildren(m_type, omit_empty_base_classes); 661 } 662 663 lldb::BasicType 664 CompilerType::GetBasicTypeEnumeration () const 665 { 666 if (IsValid()) 667 return m_type_system->GetBasicTypeEnumeration(m_type); 668 return eBasicTypeInvalid; 669 } 670 671 void 672 CompilerType::ForEachEnumerator (std::function <bool (const CompilerType &integer_type, const ConstString &name, const llvm::APSInt &value)> const &callback) const 673 { 674 if (IsValid()) 675 return m_type_system->ForEachEnumerator (m_type, callback); 676 } 677 678 679 uint32_t 680 CompilerType::GetNumFields () const 681 { 682 if (!IsValid()) 683 return 0; 684 return m_type_system->GetNumFields(m_type); 685 } 686 687 CompilerType 688 CompilerType::GetFieldAtIndex (size_t idx, 689 std::string& name, 690 uint64_t *bit_offset_ptr, 691 uint32_t *bitfield_bit_size_ptr, 692 bool *is_bitfield_ptr) const 693 { 694 if (!IsValid()) 695 return CompilerType(); 696 return m_type_system->GetFieldAtIndex(m_type, idx, name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr); 697 } 698 699 uint32_t 700 CompilerType::GetNumDirectBaseClasses () const 701 { 702 if (IsValid()) 703 return m_type_system->GetNumDirectBaseClasses (m_type); 704 return 0; 705 } 706 707 uint32_t 708 CompilerType::GetNumVirtualBaseClasses () const 709 { 710 if (IsValid()) 711 return m_type_system->GetNumVirtualBaseClasses (m_type); 712 return 0; 713 } 714 715 CompilerType 716 CompilerType::GetDirectBaseClassAtIndex (size_t idx, uint32_t *bit_offset_ptr) const 717 { 718 if (IsValid()) 719 return m_type_system->GetDirectBaseClassAtIndex (m_type, idx, bit_offset_ptr); 720 return CompilerType(); 721 } 722 723 CompilerType 724 CompilerType::GetVirtualBaseClassAtIndex (size_t idx, uint32_t *bit_offset_ptr) const 725 { 726 if (IsValid()) 727 return m_type_system->GetVirtualBaseClassAtIndex (m_type, idx, bit_offset_ptr); 728 return CompilerType(); 729 } 730 731 uint32_t 732 CompilerType::GetIndexOfFieldWithName (const char* name, 733 CompilerType* field_clang_type_ptr, 734 uint64_t *bit_offset_ptr, 735 uint32_t *bitfield_bit_size_ptr, 736 bool *is_bitfield_ptr) const 737 { 738 unsigned count = GetNumFields(); 739 std::string field_name; 740 for (unsigned index = 0; index < count; index++) 741 { 742 CompilerType field_clang_type (GetFieldAtIndex(index, field_name, bit_offset_ptr, bitfield_bit_size_ptr, is_bitfield_ptr)); 743 if (strcmp(field_name.c_str(), name) == 0) 744 { 745 if (field_clang_type_ptr) 746 *field_clang_type_ptr = field_clang_type; 747 return index; 748 } 749 } 750 return UINT32_MAX; 751 } 752 753 754 CompilerType 755 CompilerType::GetChildCompilerTypeAtIndex (ExecutionContext *exe_ctx, 756 size_t idx, 757 bool transparent_pointers, 758 bool omit_empty_base_classes, 759 bool ignore_array_bounds, 760 std::string& child_name, 761 uint32_t &child_byte_size, 762 int32_t &child_byte_offset, 763 uint32_t &child_bitfield_bit_size, 764 uint32_t &child_bitfield_bit_offset, 765 bool &child_is_base_class, 766 bool &child_is_deref_of_parent, 767 ValueObject *valobj) const 768 { 769 if (!IsValid()) 770 return CompilerType(); 771 return m_type_system->GetChildCompilerTypeAtIndex(m_type, 772 exe_ctx, 773 idx, 774 transparent_pointers, 775 omit_empty_base_classes, 776 ignore_array_bounds, 777 child_name, 778 child_byte_size, 779 child_byte_offset, 780 child_bitfield_bit_size, 781 child_bitfield_bit_offset, 782 child_is_base_class, 783 child_is_deref_of_parent, 784 valobj); 785 } 786 787 // Look for a child member (doesn't include base classes, but it does include 788 // their members) in the type hierarchy. Returns an index path into "clang_type" 789 // on how to reach the appropriate member. 790 // 791 // class A 792 // { 793 // public: 794 // int m_a; 795 // int m_b; 796 // }; 797 // 798 // class B 799 // { 800 // }; 801 // 802 // class C : 803 // public B, 804 // public A 805 // { 806 // }; 807 // 808 // If we have a clang type that describes "class C", and we wanted to looked 809 // "m_b" in it: 810 // 811 // With omit_empty_base_classes == false we would get an integer array back with: 812 // { 1, 1 } 813 // The first index 1 is the child index for "class A" within class C 814 // The second index 1 is the child index for "m_b" within class A 815 // 816 // With omit_empty_base_classes == true we would get an integer array back with: 817 // { 0, 1 } 818 // The first index 0 is the child index for "class A" within class C (since class B doesn't have any members it doesn't count) 819 // The second index 1 is the child index for "m_b" within class A 820 821 size_t 822 CompilerType::GetIndexOfChildMemberWithName (const char *name, 823 bool omit_empty_base_classes, 824 std::vector<uint32_t>& child_indexes) const 825 { 826 if (IsValid() && name && name[0]) 827 { 828 return m_type_system->GetIndexOfChildMemberWithName(m_type, name, omit_empty_base_classes, child_indexes); 829 } 830 return 0; 831 } 832 833 size_t 834 CompilerType::GetNumTemplateArguments () const 835 { 836 if (IsValid()) 837 { 838 return m_type_system->GetNumTemplateArguments(m_type); 839 } 840 return 0; 841 } 842 843 CompilerType 844 CompilerType::GetTemplateArgument (size_t idx, 845 lldb::TemplateArgumentKind &kind) const 846 { 847 if (IsValid()) 848 { 849 return m_type_system->GetTemplateArgument(m_type, idx, kind); 850 } 851 return CompilerType(); 852 } 853 854 855 // Get the index of the child of "clang_type" whose name matches. This function 856 // doesn't descend into the children, but only looks one level deep and name 857 // matches can include base class names. 858 859 uint32_t 860 CompilerType::GetIndexOfChildWithName (const char *name, bool omit_empty_base_classes) const 861 { 862 if (IsValid() && name && name[0]) 863 { 864 return m_type_system->GetIndexOfChildWithName(m_type, name, omit_empty_base_classes); 865 } 866 return UINT32_MAX; 867 } 868 869 size_t 870 CompilerType::ConvertStringToFloatValue (const char *s, uint8_t *dst, size_t dst_size) const 871 { 872 if (IsValid()) 873 return m_type_system->ConvertStringToFloatValue(m_type, s, dst, dst_size); 874 return 0; 875 } 876 877 878 879 //---------------------------------------------------------------------- 880 // Dumping types 881 //---------------------------------------------------------------------- 882 #define DEPTH_INCREMENT 2 883 884 void 885 CompilerType::DumpValue (ExecutionContext *exe_ctx, 886 Stream *s, 887 lldb::Format format, 888 const lldb_private::DataExtractor &data, 889 lldb::offset_t data_byte_offset, 890 size_t data_byte_size, 891 uint32_t bitfield_bit_size, 892 uint32_t bitfield_bit_offset, 893 bool show_types, 894 bool show_summary, 895 bool verbose, 896 uint32_t depth) 897 { 898 if (!IsValid()) 899 return; 900 m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset, data_byte_size, bitfield_bit_size, bitfield_bit_offset, show_types, show_summary, verbose, depth); 901 } 902 903 904 905 906 bool 907 CompilerType::DumpTypeValue (Stream *s, 908 lldb::Format format, 909 const lldb_private::DataExtractor &data, 910 lldb::offset_t byte_offset, 911 size_t byte_size, 912 uint32_t bitfield_bit_size, 913 uint32_t bitfield_bit_offset, 914 ExecutionContextScope *exe_scope) 915 { 916 if (!IsValid()) 917 return false; 918 return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset, byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope); 919 } 920 921 922 923 void 924 CompilerType::DumpSummary (ExecutionContext *exe_ctx, 925 Stream *s, 926 const lldb_private::DataExtractor &data, 927 lldb::offset_t data_byte_offset, 928 size_t data_byte_size) 929 { 930 if (IsValid()) 931 m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset, data_byte_size); 932 } 933 934 void 935 CompilerType::DumpTypeDescription () const 936 { 937 if (IsValid()) 938 m_type_system->DumpTypeDescription(m_type); 939 } 940 941 void 942 CompilerType::DumpTypeDescription (Stream *s) const 943 { 944 if (IsValid()) 945 { 946 m_type_system->DumpTypeDescription(m_type, s); 947 } 948 } 949 950 bool 951 CompilerType::GetValueAsScalar (const lldb_private::DataExtractor &data, 952 lldb::offset_t data_byte_offset, 953 size_t data_byte_size, 954 Scalar &value) const 955 { 956 if (!IsValid()) 957 return false; 958 959 if (IsAggregateType ()) 960 { 961 return false; // Aggregate types don't have scalar values 962 } 963 else 964 { 965 uint64_t count = 0; 966 lldb::Encoding encoding = GetEncoding (count); 967 968 if (encoding == lldb::eEncodingInvalid || count != 1) 969 return false; 970 971 const uint64_t byte_size = GetByteSize(nullptr); 972 lldb::offset_t offset = data_byte_offset; 973 switch (encoding) 974 { 975 case lldb::eEncodingInvalid: 976 break; 977 case lldb::eEncodingVector: 978 break; 979 case lldb::eEncodingUint: 980 if (byte_size <= sizeof(unsigned long long)) 981 { 982 uint64_t uval64 = data.GetMaxU64 (&offset, byte_size); 983 if (byte_size <= sizeof(unsigned int)) 984 { 985 value = (unsigned int)uval64; 986 return true; 987 } 988 else if (byte_size <= sizeof(unsigned long)) 989 { 990 value = (unsigned long)uval64; 991 return true; 992 } 993 else if (byte_size <= sizeof(unsigned long long)) 994 { 995 value = (unsigned long long )uval64; 996 return true; 997 } 998 else 999 value.Clear(); 1000 } 1001 break; 1002 1003 case lldb::eEncodingSint: 1004 if (byte_size <= sizeof(long long)) 1005 { 1006 int64_t sval64 = data.GetMaxS64 (&offset, byte_size); 1007 if (byte_size <= sizeof(int)) 1008 { 1009 value = (int)sval64; 1010 return true; 1011 } 1012 else if (byte_size <= sizeof(long)) 1013 { 1014 value = (long)sval64; 1015 return true; 1016 } 1017 else if (byte_size <= sizeof(long long)) 1018 { 1019 value = (long long )sval64; 1020 return true; 1021 } 1022 else 1023 value.Clear(); 1024 } 1025 break; 1026 1027 case lldb::eEncodingIEEE754: 1028 if (byte_size <= sizeof(long double)) 1029 { 1030 uint32_t u32; 1031 uint64_t u64; 1032 if (byte_size == sizeof(float)) 1033 { 1034 if (sizeof(float) == sizeof(uint32_t)) 1035 { 1036 u32 = data.GetU32(&offset); 1037 value = *((float *)&u32); 1038 return true; 1039 } 1040 else if (sizeof(float) == sizeof(uint64_t)) 1041 { 1042 u64 = data.GetU64(&offset); 1043 value = *((float *)&u64); 1044 return true; 1045 } 1046 } 1047 else 1048 if (byte_size == sizeof(double)) 1049 { 1050 if (sizeof(double) == sizeof(uint32_t)) 1051 { 1052 u32 = data.GetU32(&offset); 1053 value = *((double *)&u32); 1054 return true; 1055 } 1056 else if (sizeof(double) == sizeof(uint64_t)) 1057 { 1058 u64 = data.GetU64(&offset); 1059 value = *((double *)&u64); 1060 return true; 1061 } 1062 } 1063 else 1064 if (byte_size == sizeof(long double)) 1065 { 1066 if (sizeof(long double) == sizeof(uint32_t)) 1067 { 1068 u32 = data.GetU32(&offset); 1069 value = *((long double *)&u32); 1070 return true; 1071 } 1072 else if (sizeof(long double) == sizeof(uint64_t)) 1073 { 1074 u64 = data.GetU64(&offset); 1075 value = *((long double *)&u64); 1076 return true; 1077 } 1078 } 1079 } 1080 break; 1081 } 1082 } 1083 return false; 1084 } 1085 1086 bool 1087 CompilerType::SetValueFromScalar (const Scalar &value, Stream &strm) 1088 { 1089 if (!IsValid()) 1090 return false; 1091 1092 // Aggregate types don't have scalar values 1093 if (!IsAggregateType ()) 1094 { 1095 strm.GetFlags().Set(Stream::eBinary); 1096 uint64_t count = 0; 1097 lldb::Encoding encoding = GetEncoding (count); 1098 1099 if (encoding == lldb::eEncodingInvalid || count != 1) 1100 return false; 1101 1102 const uint64_t bit_width = GetBitSize(nullptr); 1103 // This function doesn't currently handle non-byte aligned assignments 1104 if ((bit_width % 8) != 0) 1105 return false; 1106 1107 const uint64_t byte_size = (bit_width + 7 ) / 8; 1108 switch (encoding) 1109 { 1110 case lldb::eEncodingInvalid: 1111 break; 1112 case lldb::eEncodingVector: 1113 break; 1114 case lldb::eEncodingUint: 1115 switch (byte_size) 1116 { 1117 case 1: strm.PutHex8(value.UInt()); return true; 1118 case 2: strm.PutHex16(value.UInt()); return true; 1119 case 4: strm.PutHex32(value.UInt()); return true; 1120 case 8: strm.PutHex64(value.ULongLong()); return true; 1121 default: 1122 break; 1123 } 1124 break; 1125 1126 case lldb::eEncodingSint: 1127 switch (byte_size) 1128 { 1129 case 1: strm.PutHex8(value.SInt()); return true; 1130 case 2: strm.PutHex16(value.SInt()); return true; 1131 case 4: strm.PutHex32(value.SInt()); return true; 1132 case 8: strm.PutHex64(value.SLongLong()); return true; 1133 default: 1134 break; 1135 } 1136 break; 1137 1138 case lldb::eEncodingIEEE754: 1139 if (byte_size <= sizeof(long double)) 1140 { 1141 if (byte_size == sizeof(float)) 1142 { 1143 strm.PutFloat(value.Float()); 1144 return true; 1145 } 1146 else 1147 if (byte_size == sizeof(double)) 1148 { 1149 strm.PutDouble(value.Double()); 1150 return true; 1151 } 1152 else 1153 if (byte_size == sizeof(long double)) 1154 { 1155 strm.PutDouble(value.LongDouble()); 1156 return true; 1157 } 1158 } 1159 break; 1160 } 1161 } 1162 return false; 1163 } 1164 1165 bool 1166 CompilerType::ReadFromMemory (lldb_private::ExecutionContext *exe_ctx, 1167 lldb::addr_t addr, 1168 AddressType address_type, 1169 lldb_private::DataExtractor &data) 1170 { 1171 if (!IsValid()) 1172 return false; 1173 1174 // Can't convert a file address to anything valid without more 1175 // context (which Module it came from) 1176 if (address_type == eAddressTypeFile) 1177 return false; 1178 1179 if (!GetCompleteType()) 1180 return false; 1181 1182 const uint64_t byte_size = GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); 1183 if (data.GetByteSize() < byte_size) 1184 { 1185 lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0')); 1186 data.SetData(data_sp); 1187 } 1188 1189 uint8_t* dst = (uint8_t*)data.PeekData(0, byte_size); 1190 if (dst != nullptr) 1191 { 1192 if (address_type == eAddressTypeHost) 1193 { 1194 if (addr == 0) 1195 return false; 1196 // The address is an address in this process, so just copy it 1197 memcpy (dst, (uint8_t*)nullptr + addr, byte_size); 1198 return true; 1199 } 1200 else 1201 { 1202 Process *process = nullptr; 1203 if (exe_ctx) 1204 process = exe_ctx->GetProcessPtr(); 1205 if (process) 1206 { 1207 Error error; 1208 return process->ReadMemory(addr, dst, byte_size, error) == byte_size; 1209 } 1210 } 1211 } 1212 return false; 1213 } 1214 1215 bool 1216 CompilerType::WriteToMemory (lldb_private::ExecutionContext *exe_ctx, 1217 lldb::addr_t addr, 1218 AddressType address_type, 1219 StreamString &new_value) 1220 { 1221 if (!IsValid()) 1222 return false; 1223 1224 // Can't convert a file address to anything valid without more 1225 // context (which Module it came from) 1226 if (address_type == eAddressTypeFile) 1227 return false; 1228 1229 if (!GetCompleteType()) 1230 return false; 1231 1232 const uint64_t byte_size = GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); 1233 1234 if (byte_size > 0) 1235 { 1236 if (address_type == eAddressTypeHost) 1237 { 1238 // The address is an address in this process, so just copy it 1239 memcpy ((void *)addr, new_value.GetData(), byte_size); 1240 return true; 1241 } 1242 else 1243 { 1244 Process *process = nullptr; 1245 if (exe_ctx) 1246 process = exe_ctx->GetProcessPtr(); 1247 if (process) 1248 { 1249 Error error; 1250 return process->WriteMemory(addr, new_value.GetData(), byte_size, error) == byte_size; 1251 } 1252 } 1253 } 1254 return false; 1255 } 1256 1257 //clang::CXXRecordDecl * 1258 //CompilerType::GetAsCXXRecordDecl (lldb::clang_type_t opaque_clang_qual_type) 1259 //{ 1260 // if (opaque_clang_qual_type) 1261 // return clang::QualType::getFromOpaquePtr(opaque_clang_qual_type)->getAsCXXRecordDecl(); 1262 // return NULL; 1263 //} 1264 1265 bool 1266 lldb_private::operator == (const lldb_private::CompilerType &lhs, const lldb_private::CompilerType &rhs) 1267 { 1268 return lhs.GetTypeSystem() == rhs.GetTypeSystem() && lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType(); 1269 } 1270 1271 1272 bool 1273 lldb_private::operator != (const lldb_private::CompilerType &lhs, const lldb_private::CompilerType &rhs) 1274 { 1275 return lhs.GetTypeSystem() != rhs.GetTypeSystem() || lhs.GetOpaqueQualType() != rhs.GetOpaqueQualType(); 1276 } 1277 1278 1279 1280