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