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