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