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