@@ -1575,23 +1575,6 @@ bool SwiftLanguageRuntimeImpl::ForEachSuperClassType(
1575
1575
return false ;
1576
1576
1577
1577
lldb::addr_t pointer = instance.GetPointerValue ();
1578
- // Maybe this belongs into GetPointerValue, but on the other hand it
1579
- // is also nice to not hide the existence of reference storage
1580
- // types. Perhaps they should even be modelled in the ValueObject
1581
- // hierarchy. This also partially papers over the fact that
1582
- // libReflection cannot tell up how many bits to strip from
1583
- // multi-payload enum values.
1584
- auto addr_deref =
1585
- FixupPointerValue (pointer, instance_type);
1586
- pointer = addr_deref.first ;
1587
- if (addr_deref.second ) {
1588
- // This is a reference storage object.
1589
- if (!reflection_ctx->getReader ().readInteger (
1590
- swift::reflection::RemoteAddress (addr_deref.first ),
1591
- &pointer))
1592
- return false ;
1593
- }
1594
-
1595
1578
auto md_ptr = reflection_ctx->readMetadataFromInstance (pointer);
1596
1579
if (!md_ptr)
1597
1580
return false ;
@@ -1670,6 +1653,21 @@ static bool IsScratchContextLocked(TargetSP target) {
1670
1653
return target ? IsScratchContextLocked (*target) : true ;
1671
1654
}
1672
1655
1656
+ static bool IsPrivateNSClass (NodePointer node) {
1657
+ if (!node || node->getKind () != Node::Kind::Type ||
1658
+ node->getNumChildren () == 0 )
1659
+ return false ;
1660
+ NodePointer classNode = node->getFirstChild ();
1661
+ if (!classNode || classNode->getKind () != Node::Kind::Class ||
1662
+ classNode->getNumChildren () < 2 )
1663
+ return false ;
1664
+ for (NodePointer child : *classNode)
1665
+ if (child->getKind () == Node::Kind::Identifier && child->hasText ())
1666
+ return child->getText ().startswith (" __NS" ) ||
1667
+ child->getText ().startswith (" NSTaggedPointer" );
1668
+ return false ;
1669
+ }
1670
+
1673
1671
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class (
1674
1672
ValueObject &in_value, SwiftASTContextForExpressions &scratch_ctx,
1675
1673
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
@@ -1678,37 +1676,86 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
1678
1676
lldb::addr_t class_metadata_ptr = in_value.GetPointerValue (&address_type);
1679
1677
if (class_metadata_ptr == LLDB_INVALID_ADDRESS || class_metadata_ptr == 0 )
1680
1678
return false ;
1681
- address.SetRawAddress (class_metadata_ptr);
1682
1679
1680
+ CompilerType static_type = in_value.GetCompilerType ();
1681
+ auto *tss =
1682
+ llvm::dyn_cast_or_null<TypeSystemSwift>(static_type.GetTypeSystem ());
1683
+ if (!tss)
1684
+ return false ;
1685
+ address.SetRawAddress (class_metadata_ptr);
1686
+ auto &ts = tss->GetTypeSystemSwiftTypeRef ();
1687
+ // Ask the Objective-C runtime about Objective-C types.
1688
+ if (tss->IsImportedType (static_type.GetOpaqueQualType (), nullptr ))
1689
+ if (auto *objc_runtime = SwiftLanguageRuntime::GetObjCRuntime (m_process)) {
1690
+ Value::ValueType value_type;
1691
+ if (objc_runtime->GetDynamicTypeAndAddress (
1692
+ in_value, use_dynamic, class_type_or_name, address, value_type)) {
1693
+ bool found = false ;
1694
+ // Return the most specific class which we can get the typeref.
1695
+ ForEachSuperClassType (in_value, [&](SuperClassType sc) -> bool {
1696
+ if (auto *tr = sc.get_typeref ()) {
1697
+ swift::Demangle::Demangler dem;
1698
+ swift::Demangle::NodePointer node = tr->getDemangling (dem);
1699
+ // Skip private Foundation types since it's unlikely that would be
1700
+ // useful to users.
1701
+ if (IsPrivateNSClass (node))
1702
+ return false ;
1703
+ class_type_or_name.SetCompilerType (ts.RemangleAsType (dem, node));
1704
+ found = true ;
1705
+ return true ;
1706
+ }
1707
+ return false ;
1708
+ });
1709
+ return found;
1710
+ }
1711
+ return false ;
1712
+ }
1683
1713
Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1684
- auto &remote_ast = GetRemoteASTContext (scratch_ctx );
1714
+ auto *reflection_ctx = GetReflectionContext ( );
1685
1715
swift::remote::RemoteAddress instance_address (class_metadata_ptr);
1686
- auto metadata_address = remote_ast.getHeapMetadataForObject (instance_address);
1716
+ auto metadata_address =
1717
+ reflection_ctx->readMetadataFromInstance (class_metadata_ptr);
1687
1718
if (!metadata_address) {
1688
- if (log ) {
1689
- log ->Printf (" could not read heap metadata for object at %llu: %s\n " ,
1690
- class_metadata_ptr,
1691
- metadata_address.getFailure ().render ().c_str ());
1692
- }
1693
-
1719
+ if (log )
1720
+ log ->Printf (" could not read heap metadata for object at %llu\n " ,
1721
+ class_metadata_ptr);
1694
1722
return false ;
1695
1723
}
1696
1724
1697
- auto instance_type =
1698
- remote_ast.getTypeForRemoteTypeMetadata (metadata_address.getValue (),
1699
- /* skipArtificial=*/ true );
1700
- if (!instance_type) {
1701
- if (log ) {
1702
- log ->Printf (" could not get type metadata from address %" PRIu64 " : %s\n " ,
1703
- metadata_address.getValue ().getAddressData (),
1704
- instance_type.getFailure ().render ().c_str ());
1705
- }
1725
+ const auto *typeref =
1726
+ reflection_ctx->readTypeFromMetadata (*metadata_address,
1727
+ /* skipArtificial=*/ false );
1728
+ if (!typeref)
1706
1729
return false ;
1730
+ swift::Demangle::Demangler dem;
1731
+ swift::Demangle::NodePointer node = typeref->getDemangling (dem);
1732
+ class_type_or_name.SetCompilerType (ts.RemangleAsType (dem, node));
1733
+
1734
+ #ifndef NDEBUG
1735
+ auto &remote_ast = GetRemoteASTContext (scratch_ctx);
1736
+ auto remote_ast_metadata_address =
1737
+ remote_ast.getHeapMetadataForObject (instance_address);
1738
+ if (remote_ast_metadata_address) {
1739
+ auto instance_type = remote_ast.getTypeForRemoteTypeMetadata (
1740
+ remote_ast_metadata_address.getValue (),
1741
+ /* skipArtificial=*/ true );
1742
+ if (instance_type) {
1743
+ auto ref_type = ToCompilerType (instance_type.getValue ());
1744
+ ConstString a = ref_type.GetMangledTypeName ();
1745
+ ConstString b = class_type_or_name.GetCompilerType ().GetMangledTypeName ();
1746
+ if (a != b)
1747
+ llvm::dbgs () << " RemoteAST and runtime diverge " << a << " != " << b
1748
+ << " \n " ;
1749
+ } else {
1750
+ if (log ) {
1751
+ log ->Printf (
1752
+ " could not get type metadata from address %" PRIu64 " : %s\n " ,
1753
+ *metadata_address, instance_type.getFailure ().render ().c_str ());
1754
+ }
1755
+ }
1707
1756
}
1708
1757
1709
- // The read lock must have been acquired by the caller.
1710
- class_type_or_name.SetCompilerType (
1711
- {&scratch_ctx, instance_type.getValue ().getPointer ()});
1758
+ #endif
1712
1759
return true ;
1713
1760
}
1714
1761
0 commit comments