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 // Get the index of the child of "clang_type" whose name matches. This function 713 // doesn't descend into the children, but only looks one level deep and name 714 // matches can include base class names. 715 716 uint32_t 717 CompilerType::GetIndexOfChildWithName(const char *name, 718 bool omit_empty_base_classes) const { 719 if (IsValid() && name && name[0]) { 720 return m_type_system->GetIndexOfChildWithName(m_type, name, 721 omit_empty_base_classes); 722 } 723 return UINT32_MAX; 724 } 725 726 // Dumping types 727 #define DEPTH_INCREMENT 2 728 729 void CompilerType::DumpValue(ExecutionContext *exe_ctx, Stream *s, 730 lldb::Format format, const DataExtractor &data, 731 lldb::offset_t data_byte_offset, 732 size_t data_byte_size, uint32_t bitfield_bit_size, 733 uint32_t bitfield_bit_offset, bool show_types, 734 bool show_summary, bool verbose, uint32_t depth) { 735 if (!IsValid()) 736 return; 737 m_type_system->DumpValue(m_type, exe_ctx, s, format, data, data_byte_offset, 738 data_byte_size, bitfield_bit_size, 739 bitfield_bit_offset, show_types, show_summary, 740 verbose, depth); 741 } 742 743 bool CompilerType::DumpTypeValue(Stream *s, lldb::Format format, 744 const DataExtractor &data, 745 lldb::offset_t byte_offset, size_t byte_size, 746 uint32_t bitfield_bit_size, 747 uint32_t bitfield_bit_offset, 748 ExecutionContextScope *exe_scope) { 749 if (!IsValid()) 750 return false; 751 return m_type_system->DumpTypeValue(m_type, s, format, data, byte_offset, 752 byte_size, bitfield_bit_size, 753 bitfield_bit_offset, exe_scope); 754 } 755 756 void CompilerType::DumpSummary(ExecutionContext *exe_ctx, Stream *s, 757 const DataExtractor &data, 758 lldb::offset_t data_byte_offset, 759 size_t data_byte_size) { 760 if (IsValid()) 761 m_type_system->DumpSummary(m_type, exe_ctx, s, data, data_byte_offset, 762 data_byte_size); 763 } 764 765 void CompilerType::DumpTypeDescription() const { 766 if (IsValid()) 767 m_type_system->DumpTypeDescription(m_type); 768 } 769 770 void CompilerType::DumpTypeDescription(Stream *s) const { 771 if (IsValid()) { 772 m_type_system->DumpTypeDescription(m_type, s); 773 } 774 } 775 776 #ifndef NDEBUG 777 LLVM_DUMP_METHOD void CompilerType::dump() const { 778 if (IsValid()) 779 m_type_system->dump(m_type); 780 else 781 llvm::errs() << "<invalid>\n"; 782 } 783 #endif 784 785 bool CompilerType::GetValueAsScalar(const lldb_private::DataExtractor &data, 786 lldb::offset_t data_byte_offset, 787 size_t data_byte_size, 788 Scalar &value) const { 789 if (!IsValid()) 790 return false; 791 792 if (IsAggregateType()) { 793 return false; // Aggregate types don't have scalar values 794 } else { 795 uint64_t count = 0; 796 lldb::Encoding encoding = GetEncoding(count); 797 798 if (encoding == lldb::eEncodingInvalid || count != 1) 799 return false; 800 801 llvm::Optional<uint64_t> byte_size = GetByteSize(nullptr); 802 if (!byte_size) 803 return false; 804 lldb::offset_t offset = data_byte_offset; 805 switch (encoding) { 806 case lldb::eEncodingInvalid: 807 break; 808 case lldb::eEncodingVector: 809 break; 810 case lldb::eEncodingUint: 811 if (*byte_size <= sizeof(unsigned long long)) { 812 uint64_t uval64 = data.GetMaxU64(&offset, *byte_size); 813 if (*byte_size <= sizeof(unsigned int)) { 814 value = (unsigned int)uval64; 815 return true; 816 } else if (*byte_size <= sizeof(unsigned long)) { 817 value = (unsigned long)uval64; 818 return true; 819 } else if (*byte_size <= sizeof(unsigned long long)) { 820 value = (unsigned long long)uval64; 821 return true; 822 } else 823 value.Clear(); 824 } 825 break; 826 827 case lldb::eEncodingSint: 828 if (*byte_size <= sizeof(long long)) { 829 int64_t sval64 = data.GetMaxS64(&offset, *byte_size); 830 if (*byte_size <= sizeof(int)) { 831 value = (int)sval64; 832 return true; 833 } else if (*byte_size <= sizeof(long)) { 834 value = (long)sval64; 835 return true; 836 } else if (*byte_size <= sizeof(long long)) { 837 value = (long long)sval64; 838 return true; 839 } else 840 value.Clear(); 841 } 842 break; 843 844 case lldb::eEncodingIEEE754: 845 if (*byte_size <= sizeof(long double)) { 846 uint32_t u32; 847 uint64_t u64; 848 if (*byte_size == sizeof(float)) { 849 if (sizeof(float) == sizeof(uint32_t)) { 850 u32 = data.GetU32(&offset); 851 value = *((float *)&u32); 852 return true; 853 } else if (sizeof(float) == sizeof(uint64_t)) { 854 u64 = data.GetU64(&offset); 855 value = *((float *)&u64); 856 return true; 857 } 858 } else if (*byte_size == sizeof(double)) { 859 if (sizeof(double) == sizeof(uint32_t)) { 860 u32 = data.GetU32(&offset); 861 value = *((double *)&u32); 862 return true; 863 } else if (sizeof(double) == sizeof(uint64_t)) { 864 u64 = data.GetU64(&offset); 865 value = *((double *)&u64); 866 return true; 867 } 868 } else if (*byte_size == sizeof(long double)) { 869 if (sizeof(long double) == sizeof(uint32_t)) { 870 u32 = data.GetU32(&offset); 871 value = *((long double *)&u32); 872 return true; 873 } else if (sizeof(long double) == sizeof(uint64_t)) { 874 u64 = data.GetU64(&offset); 875 value = *((long double *)&u64); 876 return true; 877 } 878 } 879 } 880 break; 881 } 882 } 883 return false; 884 } 885 886 bool CompilerType::SetValueFromScalar(const Scalar &value, Stream &strm) { 887 if (!IsValid()) 888 return false; 889 890 // Aggregate types don't have scalar values 891 if (!IsAggregateType()) { 892 strm.GetFlags().Set(Stream::eBinary); 893 uint64_t count = 0; 894 lldb::Encoding encoding = GetEncoding(count); 895 896 if (encoding == lldb::eEncodingInvalid || count != 1) 897 return false; 898 899 llvm::Optional<uint64_t> bit_width = GetBitSize(nullptr); 900 if (!bit_width) 901 return false; 902 903 // This function doesn't currently handle non-byte aligned assignments 904 if ((*bit_width % 8) != 0) 905 return false; 906 907 const uint64_t byte_size = (*bit_width + 7) / 8; 908 switch (encoding) { 909 case lldb::eEncodingInvalid: 910 break; 911 case lldb::eEncodingVector: 912 break; 913 case lldb::eEncodingUint: 914 switch (byte_size) { 915 case 1: 916 strm.PutHex8(value.UInt()); 917 return true; 918 case 2: 919 strm.PutHex16(value.UInt()); 920 return true; 921 case 4: 922 strm.PutHex32(value.UInt()); 923 return true; 924 case 8: 925 strm.PutHex64(value.ULongLong()); 926 return true; 927 default: 928 break; 929 } 930 break; 931 932 case lldb::eEncodingSint: 933 switch (byte_size) { 934 case 1: 935 strm.PutHex8(value.SInt()); 936 return true; 937 case 2: 938 strm.PutHex16(value.SInt()); 939 return true; 940 case 4: 941 strm.PutHex32(value.SInt()); 942 return true; 943 case 8: 944 strm.PutHex64(value.SLongLong()); 945 return true; 946 default: 947 break; 948 } 949 break; 950 951 case lldb::eEncodingIEEE754: 952 if (byte_size <= sizeof(long double)) { 953 if (byte_size == sizeof(float)) { 954 strm.PutFloat(value.Float()); 955 return true; 956 } else if (byte_size == sizeof(double)) { 957 strm.PutDouble(value.Double()); 958 return true; 959 } else if (byte_size == sizeof(long double)) { 960 strm.PutDouble(value.LongDouble()); 961 return true; 962 } 963 } 964 break; 965 } 966 } 967 return false; 968 } 969 970 bool CompilerType::ReadFromMemory(lldb_private::ExecutionContext *exe_ctx, 971 lldb::addr_t addr, AddressType address_type, 972 lldb_private::DataExtractor &data) { 973 if (!IsValid()) 974 return false; 975 976 // Can't convert a file address to anything valid without more context (which 977 // Module it came from) 978 if (address_type == eAddressTypeFile) 979 return false; 980 981 if (!GetCompleteType()) 982 return false; 983 984 auto byte_size = 985 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); 986 if (!byte_size) 987 return false; 988 989 if (data.GetByteSize() < *byte_size) { 990 lldb::DataBufferSP data_sp(new DataBufferHeap(*byte_size, '\0')); 991 data.SetData(data_sp); 992 } 993 994 uint8_t *dst = const_cast<uint8_t *>(data.PeekData(0, *byte_size)); 995 if (dst != nullptr) { 996 if (address_type == eAddressTypeHost) { 997 if (addr == 0) 998 return false; 999 // The address is an address in this process, so just copy it 1000 memcpy(dst, reinterpret_cast<uint8_t *>(addr), *byte_size); 1001 return true; 1002 } else { 1003 Process *process = nullptr; 1004 if (exe_ctx) 1005 process = exe_ctx->GetProcessPtr(); 1006 if (process) { 1007 Status error; 1008 return process->ReadMemory(addr, dst, *byte_size, error) == *byte_size; 1009 } 1010 } 1011 } 1012 return false; 1013 } 1014 1015 bool CompilerType::WriteToMemory(lldb_private::ExecutionContext *exe_ctx, 1016 lldb::addr_t addr, AddressType address_type, 1017 StreamString &new_value) { 1018 if (!IsValid()) 1019 return false; 1020 1021 // Can't convert a file address to anything valid without more context (which 1022 // Module it came from) 1023 if (address_type == eAddressTypeFile) 1024 return false; 1025 1026 if (!GetCompleteType()) 1027 return false; 1028 1029 auto byte_size = 1030 GetByteSize(exe_ctx ? exe_ctx->GetBestExecutionContextScope() : nullptr); 1031 if (!byte_size) 1032 return false; 1033 1034 if (*byte_size > 0) { 1035 if (address_type == eAddressTypeHost) { 1036 // The address is an address in this process, so just copy it 1037 memcpy((void *)addr, new_value.GetData(), *byte_size); 1038 return true; 1039 } else { 1040 Process *process = nullptr; 1041 if (exe_ctx) 1042 process = exe_ctx->GetProcessPtr(); 1043 if (process) { 1044 Status error; 1045 return process->WriteMemory(addr, new_value.GetData(), *byte_size, 1046 error) == *byte_size; 1047 } 1048 } 1049 } 1050 return false; 1051 } 1052 1053 bool lldb_private::operator==(const lldb_private::CompilerType &lhs, 1054 const lldb_private::CompilerType &rhs) { 1055 return lhs.GetTypeSystem() == rhs.GetTypeSystem() && 1056 lhs.GetOpaqueQualType() == rhs.GetOpaqueQualType(); 1057 } 1058 1059 bool lldb_private::operator!=(const lldb_private::CompilerType &lhs, 1060 const lldb_private::CompilerType &rhs) { 1061 return !(lhs == rhs); 1062 } 1063