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 #ifndef NDEBUG 804 LLVM_DUMP_METHOD void CompilerType::dump() const { 805 if (IsValid()) 806 m_type_system->dump(m_type); 807 else 808 llvm::errs() << "<invalid>\n"; 809 } 810 #endif 811 812 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, 813 lldb::offset_t data_byte_offset, 814 size_t data_byte_size, 815 Scalar &value) const { 816 if (!IsValid()) 817 return false; 818 819 if (IsAggregateType()) { 820 return false; // Aggregate types don't have scalar values 821 } else { 822 uint64_t count = 0; 823 lldb::Encoding encoding = GetEncoding(count); 824 825 if (encoding == lldb::eEncodingInvalid || count != 1) 826 return false; 827 828 llvm::Optional<uint64_t> byte_size = GetByteSize(nullptr); 829 if (!byte_size) 830 return false; 831 lldb::offset_t offset = data_byte_offset; 832 switch (encoding) { 833 case lldb::eEncodingInvalid: 834 break; 835 case lldb::eEncodingVector: 836 break; 837 case lldb::eEncodingUint: 838 if (*byte_size <= sizeof(unsigned long long)) { 839 uint64_t uval64 = data.GetMaxU64(&offset, *byte_size); 840 if (*byte_size <= sizeof(unsigned int)) { 841 value = (unsigned int)uval64; 842 return true; 843 } else if (*byte_size <= sizeof(unsigned long)) { 844 value = (unsigned long)uval64; 845 return true; 846 } else if (*byte_size <= sizeof(unsigned long long)) { 847 value = (unsigned long long)uval64; 848 return true; 849 } else 850 value.Clear(); 851 } 852 break; 853 854 case lldb::eEncodingSint: 855 if (*byte_size <= sizeof(long long)) { 856 int64_t sval64 = data.GetMaxS64(&offset, *byte_size); 857 if (*byte_size <= sizeof(int)) { 858 value = (int)sval64; 859 return true; 860 } else if (*byte_size <= sizeof(long)) { 861 value = (long)sval64; 862 return true; 863 } else if (*byte_size <= sizeof(long long)) { 864 value = (long long)sval64; 865 return true; 866 } else 867 value.Clear(); 868 } 869 break; 870 871 case lldb::eEncodingIEEE754: 872 if (*byte_size <= sizeof(long double)) { 873 uint32_t u32; 874 uint64_t u64; 875 if (*byte_size == sizeof(float)) { 876 if (sizeof(float) == sizeof(uint32_t)) { 877 u32 = data.GetU32(&offset); 878 value = *((float *)&u32); 879 return true; 880 } else if (sizeof(float) == sizeof(uint64_t)) { 881 u64 = data.GetU64(&offset); 882 value = *((float *)&u64); 883 return true; 884 } 885 } else if (*byte_size == sizeof(double)) { 886 if (sizeof(double) == sizeof(uint32_t)) { 887 u32 = data.GetU32(&offset); 888 value = *((double *)&u32); 889 return true; 890 } else if (sizeof(double) == sizeof(uint64_t)) { 891 u64 = data.GetU64(&offset); 892 value = *((double *)&u64); 893 return true; 894 } 895 } else if (*byte_size == sizeof(long double)) { 896 if (sizeof(long double) == sizeof(uint32_t)) { 897 u32 = data.GetU32(&offset); 898 value = *((long double *)&u32); 899 return true; 900 } else if (sizeof(long double) == sizeof(uint64_t)) { 901 u64 = data.GetU64(&offset); 902 value = *((long double *)&u64); 903 return true; 904 } 905 } 906 } 907 break; 908 } 909 } 910 return false; 911 } 912 913 bool CompilerType::SetValueFromScalar(const Scalar &value, Stream &strm) { 914 if (!IsValid()) 915 return false; 916 917 // Aggregate types don't have scalar values 918 if (!IsAggregateType()) { 919 strm.GetFlags().Set(Stream::eBinary); 920 uint64_t count = 0; 921 lldb::Encoding encoding = GetEncoding(count); 922 923 if (encoding == lldb::eEncodingInvalid || count != 1) 924 return false; 925 926 llvm::Optional<uint64_t> bit_width = GetBitSize(nullptr); 927 if (!bit_width) 928 return false; 929 930 // This function doesn't currently handle non-byte aligned assignments 931 if ((*bit_width % 8) != 0) 932 return false; 933 934 const uint64_t byte_size = (*bit_width + 7) / 8; 935 switch (encoding) { 936 case lldb::eEncodingInvalid: 937 break; 938 case lldb::eEncodingVector: 939 break; 940 case lldb::eEncodingUint: 941 switch (byte_size) { 942 case 1: 943 strm.PutHex8(value.UInt()); 944 return true; 945 case 2: 946 strm.PutHex16(value.UInt()); 947 return true; 948 case 4: 949 strm.PutHex32(value.UInt()); 950 return true; 951 case 8: 952 strm.PutHex64(value.ULongLong()); 953 return true; 954 default: 955 break; 956 } 957 break; 958 959 case lldb::eEncodingSint: 960 switch (byte_size) { 961 case 1: 962 strm.PutHex8(value.SInt()); 963 return true; 964 case 2: 965 strm.PutHex16(value.SInt()); 966 return true; 967 case 4: 968 strm.PutHex32(value.SInt()); 969 return true; 970 case 8: 971 strm.PutHex64(value.SLongLong()); 972 return true; 973 default: 974 break; 975 } 976 break; 977 978 case lldb::eEncodingIEEE754: 979 if (byte_size <= sizeof(long double)) { 980 if (byte_size == sizeof(float)) { 981 strm.PutFloat(value.Float()); 982 return true; 983 } else if (byte_size == sizeof(double)) { 984 strm.PutDouble(value.Double()); 985 return true; 986 } else if (byte_size == sizeof(long double)) { 987 strm.PutDouble(value.LongDouble()); 988 return true; 989 } 990 } 991 break; 992 } 993 } 994 return false; 995 } 996 997 bool CompilerType::ReadFromMemory(lldb_private::ExecutionContext *exe_ctx, 998 lldb::addr_t addr, AddressType address_type, 999 lldb_private::DataExtractor &data) { 1000 if (!IsValid()) 1001 return false; 1002 1003 // Can't convert a file address to anything valid without more context (which 1004 // Module it came from) 1005 if (address_type == eAddressTypeFile) 1006 return false; 1007 1008 if (!GetCompleteType()) 1009 return false; 1010 1011 auto byte_size = 1012 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); 1013 if (!byte_size) 1014 return false; 1015 1016 if (data.GetByteSize() < *byte_size) { 1017 lldb::DataBufferSP data_sp(new DataBufferHeap(*byte_size, '\0')); 1018 data.SetData(data_sp); 1019 } 1020 1021 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, *byte_size)); 1022 if (dst != nullptr) { 1023 if (address_type == eAddressTypeHost) { 1024 if (addr == 0) 1025 return false; 1026 // The address is an address in this process, so just copy it 1027 memcpy(dst, reinterpret_cast<uint8_t *>(addr), *byte_size); 1028 return true; 1029 } else { 1030 Process *process = nullptr; 1031 if (exe_ctx) 1032 process = exe_ctx->GetProcessPtr(); 1033 if (process) { 1034 Status error; 1035 return process->ReadMemory(addr, dst, *byte_size, error) == *byte_size; 1036 } 1037 } 1038 } 1039 return false; 1040 } 1041 1042 bool CompilerType::WriteToMemory(lldb_private::ExecutionContext *exe_ctx, 1043 lldb::addr_t addr, AddressType address_type, 1044 StreamString &new_value) { 1045 if (!IsValid()) 1046 return false; 1047 1048 // Can't convert a file address to anything valid without more context (which 1049 // Module it came from) 1050 if (address_type == eAddressTypeFile) 1051 return false; 1052 1053 if (!GetCompleteType()) 1054 return false; 1055 1056 auto byte_size = 1057 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : NULL); 1058 if (!byte_size) 1059 return false; 1060 1061 if (*byte_size > 0) { 1062 if (address_type == eAddressTypeHost) { 1063 // The address is an address in this process, so just copy it 1064 memcpy((void *)addr, new_value.GetData(), *byte_size); 1065 return true; 1066 } else { 1067 Process *process = nullptr; 1068 if (exe_ctx) 1069 process = exe_ctx->GetProcessPtr(); 1070 if (process) { 1071 Status error; 1072 return process->WriteMemory(addr, new_value.GetData(), *byte_size, 1073 error) == *byte_size; 1074 } 1075 } 1076 } 1077 return false; 1078 } 1079 1080 bool lldb_private::operator==(const lldb_private::CompilerType &lhs, 1081 const lldb_private::CompilerType &rhs) { 1082 return lhs.GetTypeSystem() == rhs.GetTypeSystem() && 1083 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType(); 1084 } 1085 1086 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs, 1087 const lldb_private::CompilerType &rhs) { 1088 return !(lhs == rhs); 1089 } 1090