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