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::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_compiler_type) const 373 { 374 if (!IsValid()) 375 return 0; 376 377 return m_type_system->GetTypeInfo(m_type, pointee_or_element_compiler_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, lldb::opaque_compiler_type_t 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_compiler_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_compiler_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_compiler_type_ptr) 746 *field_compiler_type_ptr = field_compiler_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 CompilerType 855 CompilerType::GetTypeForFormatters () const 856 { 857 if (IsValid()) 858 return m_type_system->GetTypeForFormatters(m_type); 859 return CompilerType(); 860 } 861 862 LazyBool 863 CompilerType::ShouldPrintAsOneLiner () const 864 { 865 if (IsValid()) 866 return m_type_system->ShouldPrintAsOneLiner(m_type); 867 return eLazyBoolCalculate; 868 } 869 870 bool 871 CompilerType::IsMeaninglessWithoutDynamicResolution () const 872 { 873 if (IsValid()) 874 return m_type_system->IsMeaninglessWithoutDynamicResolution(m_type); 875 return false; 876 } 877 878 // Get the index of the child of "clang_type" whose name matches. This function 879 // doesn't descend into the children, but only looks one level deep and name 880 // matches can include base class names. 881 882 uint32_t 883 CompilerType::GetIndexOfChildWithName (const char *name, bool omit_empty_base_classes) const 884 { 885 if (IsValid() && name && name[0]) 886 { 887 return m_type_system->GetIndexOfChildWithName(m_type, name, omit_empty_base_classes); 888 } 889 return UINT32_MAX; 890 } 891 892 size_t 893 CompilerType::ConvertStringToFloatValue (const char *s, uint8_t *dst, size_t dst_size) const 894 { 895 if (IsValid()) 896 return m_type_system->ConvertStringToFloatValue(m_type, s, dst, dst_size); 897 return 0; 898 } 899 900 901 902 //---------------------------------------------------------------------- 903 // Dumping types 904 //---------------------------------------------------------------------- 905 #define DEPTH_INCREMENT 2 906 907 void 908 CompilerType::DumpValue (ExecutionContext *exe_ctx, 909 Stream *s, 910 lldb::Format format, 911 const lldb_private::DataExtractor &data, 912 lldb::offset_t data_byte_offset, 913 size_t data_byte_size, 914 uint32_t bitfield_bit_size, 915 uint32_t bitfield_bit_offset, 916 bool show_types, 917 bool show_summary, 918 bool verbose, 919 uint32_t depth) 920 { 921 if (!IsValid()) 922 return; 923 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); 924 } 925 926 927 928 929 bool 930 CompilerType::DumpTypeValue (Stream *s, 931 lldb::Format format, 932 const lldb_private::DataExtractor &data, 933 lldb::offset_t byte_offset, 934 size_t byte_size, 935 uint32_t bitfield_bit_size, 936 uint32_t bitfield_bit_offset, 937 ExecutionContextScope *exe_scope) 938 { 939 if (!IsValid()) 940 return false; 941 return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset, byte_size, bitfield_bit_size, bitfield_bit_offset, exe_scope); 942 } 943 944 945 946 void 947 CompilerType::DumpSummary (ExecutionContext *exe_ctx, 948 Stream *s, 949 const lldb_private::DataExtractor &data, 950 lldb::offset_t data_byte_offset, 951 size_t data_byte_size) 952 { 953 if (IsValid()) 954 m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset, data_byte_size); 955 } 956 957 void 958 CompilerType::DumpTypeDescription () const 959 { 960 if (IsValid()) 961 m_type_system->DumpTypeDescription(m_type); 962 } 963 964 void 965 CompilerType::DumpTypeDescription (Stream *s) const 966 { 967 if (IsValid()) 968 { 969 m_type_system->DumpTypeDescription(m_type, s); 970 } 971 } 972 973 bool 974 CompilerType::GetValueAsScalar (const lldb_private::DataExtractor &data, 975 lldb::offset_t data_byte_offset, 976 size_t data_byte_size, 977 Scalar &value) const 978 { 979 if (!IsValid()) 980 return false; 981 982 if (IsAggregateType ()) 983 { 984 return false; // Aggregate types don't have scalar values 985 } 986 else 987 { 988 uint64_t count = 0; 989 lldb::Encoding encoding = GetEncoding (count); 990 991 if (encoding == lldb::eEncodingInvalid || count != 1) 992 return false; 993 994 const uint64_t byte_size = GetByteSize(nullptr); 995 lldb::offset_t offset = data_byte_offset; 996 switch (encoding) 997 { 998 case lldb::eEncodingInvalid: 999 break; 1000 case lldb::eEncodingVector: 1001 break; 1002 case lldb::eEncodingUint: 1003 if (byte_size <= sizeof(unsigned long long)) 1004 { 1005 uint64_t uval64 = data.GetMaxU64 (&offset, byte_size); 1006 if (byte_size <= sizeof(unsigned int)) 1007 { 1008 value = (unsigned int)uval64; 1009 return true; 1010 } 1011 else if (byte_size <= sizeof(unsigned long)) 1012 { 1013 value = (unsigned long)uval64; 1014 return true; 1015 } 1016 else if (byte_size <= sizeof(unsigned long long)) 1017 { 1018 value = (unsigned long long )uval64; 1019 return true; 1020 } 1021 else 1022 value.Clear(); 1023 } 1024 break; 1025 1026 case lldb::eEncodingSint: 1027 if (byte_size <= sizeof(long long)) 1028 { 1029 int64_t sval64 = data.GetMaxS64 (&offset, byte_size); 1030 if (byte_size <= sizeof(int)) 1031 { 1032 value = (int)sval64; 1033 return true; 1034 } 1035 else if (byte_size <= sizeof(long)) 1036 { 1037 value = (long)sval64; 1038 return true; 1039 } 1040 else if (byte_size <= sizeof(long long)) 1041 { 1042 value = (long long )sval64; 1043 return true; 1044 } 1045 else 1046 value.Clear(); 1047 } 1048 break; 1049 1050 case lldb::eEncodingIEEE754: 1051 if (byte_size <= sizeof(long double)) 1052 { 1053 uint32_t u32; 1054 uint64_t u64; 1055 if (byte_size == sizeof(float)) 1056 { 1057 if (sizeof(float) == sizeof(uint32_t)) 1058 { 1059 u32 = data.GetU32(&offset); 1060 value = *((float *)&u32); 1061 return true; 1062 } 1063 else if (sizeof(float) == sizeof(uint64_t)) 1064 { 1065 u64 = data.GetU64(&offset); 1066 value = *((float *)&u64); 1067 return true; 1068 } 1069 } 1070 else 1071 if (byte_size == sizeof(double)) 1072 { 1073 if (sizeof(double) == sizeof(uint32_t)) 1074 { 1075 u32 = data.GetU32(&offset); 1076 value = *((double *)&u32); 1077 return true; 1078 } 1079 else if (sizeof(double) == sizeof(uint64_t)) 1080 { 1081 u64 = data.GetU64(&offset); 1082 value = *((double *)&u64); 1083 return true; 1084 } 1085 } 1086 else 1087 if (byte_size == sizeof(long double)) 1088 { 1089 if (sizeof(long double) == sizeof(uint32_t)) 1090 { 1091 u32 = data.GetU32(&offset); 1092 value = *((long double *)&u32); 1093 return true; 1094 } 1095 else if (sizeof(long double) == sizeof(uint64_t)) 1096 { 1097 u64 = data.GetU64(&offset); 1098 value = *((long double *)&u64); 1099 return true; 1100 } 1101 } 1102 } 1103 break; 1104 } 1105 } 1106 return false; 1107 } 1108 1109 bool 1110 CompilerType::SetValueFromScalar (const Scalar &value, Stream &strm) 1111 { 1112 if (!IsValid()) 1113 return false; 1114 1115 // Aggregate types don't have scalar values 1116 if (!IsAggregateType ()) 1117 { 1118 strm.GetFlags().Set(Stream::eBinary); 1119 uint64_t count = 0; 1120 lldb::Encoding encoding = GetEncoding (count); 1121 1122 if (encoding == lldb::eEncodingInvalid || count != 1) 1123 return false; 1124 1125 const uint64_t bit_width = GetBitSize(nullptr); 1126 // This function doesn't currently handle non-byte aligned assignments 1127 if ((bit_width % 8) != 0) 1128 return false; 1129 1130 const uint64_t byte_size = (bit_width + 7 ) / 8; 1131 switch (encoding) 1132 { 1133 case lldb::eEncodingInvalid: 1134 break; 1135 case lldb::eEncodingVector: 1136 break; 1137 case lldb::eEncodingUint: 1138 switch (byte_size) 1139 { 1140 case 1: strm.PutHex8(value.UInt()); return true; 1141 case 2: strm.PutHex16(value.UInt()); return true; 1142 case 4: strm.PutHex32(value.UInt()); return true; 1143 case 8: strm.PutHex64(value.ULongLong()); return true; 1144 default: 1145 break; 1146 } 1147 break; 1148 1149 case lldb::eEncodingSint: 1150 switch (byte_size) 1151 { 1152 case 1: strm.PutHex8(value.SInt()); return true; 1153 case 2: strm.PutHex16(value.SInt()); return true; 1154 case 4: strm.PutHex32(value.SInt()); return true; 1155 case 8: strm.PutHex64(value.SLongLong()); return true; 1156 default: 1157 break; 1158 } 1159 break; 1160 1161 case lldb::eEncodingIEEE754: 1162 if (byte_size <= sizeof(long double)) 1163 { 1164 if (byte_size == sizeof(float)) 1165 { 1166 strm.PutFloat(value.Float()); 1167 return true; 1168 } 1169 else 1170 if (byte_size == sizeof(double)) 1171 { 1172 strm.PutDouble(value.Double()); 1173 return true; 1174 } 1175 else 1176 if (byte_size == sizeof(long double)) 1177 { 1178 strm.PutDouble(value.LongDouble()); 1179 return true; 1180 } 1181 } 1182 break; 1183 } 1184 } 1185 return false; 1186 } 1187 1188 bool 1189 CompilerType::ReadFromMemory (lldb_private::ExecutionContext *exe_ctx, 1190 lldb::addr_t addr, 1191 AddressType address_type, 1192 lldb_private::DataExtractor &data) 1193 { 1194 if (!IsValid()) 1195 return false; 1196 1197 // Can't convert a file address to anything valid without more 1198 // context (which Module it came from) 1199 if (address_type == eAddressTypeFile) 1200 return false; 1201 1202 if (!GetCompleteType()) 1203 return false; 1204 1205 const uint64_t byte_size = GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); 1206 if (data.GetByteSize() < byte_size) 1207 { 1208 lldb::DataBufferSP data_sp(new DataBufferHeap (byte_size, '\0')); 1209 data.SetData(data_sp); 1210 } 1211 1212 uint8_t* dst = const_cast<uint8_t*>(data.PeekData(0, byte_size)); 1213 if (dst != nullptr) 1214 { 1215 if (address_type == eAddressTypeHost) 1216 { 1217 if (addr == 0) 1218 return false; 1219 // The address is an address in this process, so just copy it 1220 memcpy (dst, (uint8_t*)nullptr + addr, byte_size); 1221 return true; 1222 } 1223 else 1224 { 1225 Process *process = nullptr; 1226 if (exe_ctx) 1227 process = exe_ctx->GetProcessPtr(); 1228 if (process) 1229 { 1230 Error error; 1231 return process->ReadMemory(addr, dst, byte_size, error) == byte_size; 1232 } 1233 } 1234 } 1235 return false; 1236 } 1237 1238 bool 1239 CompilerType::WriteToMemory (lldb_private::ExecutionContext *exe_ctx, 1240 lldb::addr_t addr, 1241 AddressType address_type, 1242 StreamString &new_value) 1243 { 1244 if (!IsValid()) 1245 return false; 1246 1247 // Can't convert a file address to anything valid without more 1248 // context (which Module it came from) 1249 if (address_type == eAddressTypeFile) 1250 return false; 1251 1252 if (!GetCompleteType()) 1253 return false; 1254 1255 const uint64_t byte_size = GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); 1256 1257 if (byte_size > 0) 1258 { 1259 if (address_type == eAddressTypeHost) 1260 { 1261 // The address is an address in this process, so just copy it 1262 memcpy ((void *)addr, new_value.GetData(), byte_size); 1263 return true; 1264 } 1265 else 1266 { 1267 Process *process = nullptr; 1268 if (exe_ctx) 1269 process = exe_ctx->GetProcessPtr(); 1270 if (process) 1271 { 1272 Error error; 1273 return process->WriteMemory(addr, new_value.GetData(), byte_size, error) == byte_size; 1274 } 1275 } 1276 } 1277 return false; 1278 } 1279 1280 //clang::CXXRecordDecl * 1281 //CompilerType::GetAsCXXRecordDecl (lldb::opaque_compiler_type_t opaque_compiler_qual_type) 1282 //{ 1283 // if (opaque_compiler_qual_type) 1284 // return clang::QualType::getFromOpaquePtr(opaque_compiler_qual_type)->getAsCXXRecordDecl(); 1285 // return NULL; 1286 //} 1287 1288 bool 1289 lldb_private::operator == (const lldb_private::CompilerType &lhs, const lldb_private::CompilerType &rhs) 1290 { 1291 return lhs.GetTypeSystem() == rhs.GetTypeSystem() && lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType(); 1292 } 1293 1294 1295 bool 1296 lldb_private::operator != (const lldb_private::CompilerType &lhs, const lldb_private::CompilerType &rhs) 1297 { 1298 return lhs.GetTypeSystem() != rhs.GetTypeSystem() || lhs.GetOpaqueQualType() != rhs.GetOpaqueQualType(); 1299 } 1300 1301 1302 1303