@@ -1604,23 +1604,6 @@ bool SwiftLanguageRuntimeImpl::ForEachSuperClassType(
1604
1604
return false ;
1605
1605
1606
1606
lldb::addr_t pointer = instance.GetPointerValue ();
1607
- // Maybe this belongs into GetPointerValue, but on the other hand it
1608
- // is also nice to not hide the existence of reference storage
1609
- // types. Perhaps they should even be modelled in the ValueObject
1610
- // hierarchy. This also partially papers over the fact that
1611
- // libReflection cannot tell up how many bits to strip from
1612
- // multi-payload enum values.
1613
- auto addr_deref =
1614
- FixupPointerValue (pointer, instance_type);
1615
- pointer = addr_deref.first ;
1616
- if (addr_deref.second ) {
1617
- // This is a reference storage object.
1618
- if (!reflection_ctx->getReader ().readInteger (
1619
- swift::reflection::RemoteAddress (addr_deref.first ),
1620
- &pointer))
1621
- return false ;
1622
- }
1623
-
1624
1607
auto md_ptr = reflection_ctx->readMetadataFromInstance (pointer);
1625
1608
if (!md_ptr)
1626
1609
return false ;
@@ -1699,6 +1682,21 @@ static bool IsScratchContextLocked(TargetSP target) {
1699
1682
return target ? IsScratchContextLocked (*target) : true ;
1700
1683
}
1701
1684
1685
+ static bool IsPrivateNSClass (NodePointer node) {
1686
+ if (!node || node->getKind () != Node::Kind::Type ||
1687
+ node->getNumChildren () == 0 )
1688
+ return false ;
1689
+ NodePointer classNode = node->getFirstChild ();
1690
+ if (!classNode || classNode->getKind () != Node::Kind::Class ||
1691
+ classNode->getNumChildren () < 2 )
1692
+ return false ;
1693
+ for (NodePointer child : *classNode)
1694
+ if (child->getKind () == Node::Kind::Identifier && child->hasText ())
1695
+ return child->getText ().startswith (" __NS" ) ||
1696
+ child->getText ().startswith (" NSTaggedPointer" );
1697
+ return false ;
1698
+ }
1699
+
1702
1700
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class (
1703
1701
ValueObject &in_value, SwiftASTContextForExpressions &scratch_ctx,
1704
1702
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
@@ -1707,37 +1705,83 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Class(
1707
1705
lldb::addr_t class_metadata_ptr = in_value.GetPointerValue (&address_type);
1708
1706
if (class_metadata_ptr == LLDB_INVALID_ADDRESS || class_metadata_ptr == 0 )
1709
1707
return false ;
1710
- address.SetRawAddress (class_metadata_ptr);
1711
1708
1709
+ CompilerType static_type = in_value.GetCompilerType ();
1710
+ auto *tss =
1711
+ llvm::dyn_cast_or_null<TypeSystemSwift>(static_type.GetTypeSystem ());
1712
+ if (!tss)
1713
+ return false ;
1714
+ address.SetRawAddress (class_metadata_ptr);
1715
+ auto &ts = tss->GetTypeSystemSwiftTypeRef ();
1716
+ // Ask the Objective-C runtime about Objective-C types.
1717
+ if (tss->IsImportedType (static_type.GetOpaqueQualType (), nullptr ))
1718
+ if (auto *objc_runtime = SwiftLanguageRuntime::GetObjCRuntime (m_process)) {
1719
+ Value::ValueType value_type;
1720
+ if (objc_runtime->GetDynamicTypeAndAddress (
1721
+ in_value, use_dynamic, class_type_or_name, address, value_type)) {
1722
+ bool found = false ;
1723
+ ForEachSuperClassType (in_value, [&](SuperClassType sc) -> bool {
1724
+ if (auto *tr = sc.get_typeref ()) {
1725
+ swift::Demangle::Demangler dem;
1726
+ swift::Demangle::NodePointer node = tr->getDemangling (dem);
1727
+ if (IsPrivateNSClass (node))
1728
+ return false ;
1729
+ class_type_or_name.SetCompilerType (ts.RemangleAsType (dem, node));
1730
+ found = true ;
1731
+ return true ;
1732
+ }
1733
+ return false ;
1734
+ });
1735
+ return found;
1736
+ }
1737
+ return false ;
1738
+ }
1712
1739
Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1713
- auto &remote_ast = GetRemoteASTContext (scratch_ctx );
1740
+ auto *reflection_ctx = GetReflectionContext ( );
1714
1741
swift::remote::RemoteAddress instance_address (class_metadata_ptr);
1715
- auto metadata_address = remote_ast.getHeapMetadataForObject (instance_address);
1742
+ auto metadata_address =
1743
+ reflection_ctx->readMetadataFromInstance (class_metadata_ptr);
1716
1744
if (!metadata_address) {
1717
- if (log ) {
1718
- log ->Printf (" could not read heap metadata for object at %llu: %s\n " ,
1719
- class_metadata_ptr,
1720
- metadata_address.getFailure ().render ().c_str ());
1721
- }
1722
-
1745
+ if (log )
1746
+ log ->Printf (" could not read heap metadata for object at %llu\n " ,
1747
+ class_metadata_ptr);
1723
1748
return false ;
1724
1749
}
1725
1750
1726
- auto instance_type =
1727
- remote_ast.getTypeForRemoteTypeMetadata (metadata_address.getValue (),
1728
- /* skipArtificial=*/ true );
1729
- if (!instance_type) {
1730
- if (log ) {
1731
- log ->Printf (" could not get type metadata from address %" PRIu64 " : %s\n " ,
1732
- metadata_address.getValue ().getAddressData (),
1733
- instance_type.getFailure ().render ().c_str ());
1734
- }
1751
+ const auto *typeref =
1752
+ reflection_ctx->readTypeFromMetadata (*metadata_address,
1753
+ /* skipArtificial=*/ false );
1754
+ if (!typeref)
1735
1755
return false ;
1756
+ swift::Demangle::Demangler dem;
1757
+ swift::Demangle::NodePointer node = typeref->getDemangling (dem);
1758
+ class_type_or_name.SetCompilerType (ts.RemangleAsType (dem, node));
1759
+
1760
+ #ifndef NDEBUG
1761
+ auto &remote_ast = GetRemoteASTContext (scratch_ctx);
1762
+ auto remote_ast_metadata_address =
1763
+ remote_ast.getHeapMetadataForObject (instance_address);
1764
+ if (remote_ast_metadata_address) {
1765
+ auto instance_type = remote_ast.getTypeForRemoteTypeMetadata (
1766
+ remote_ast_metadata_address.getValue (),
1767
+ /* skipArtificial=*/ true );
1768
+ if (instance_type) {
1769
+ auto ref_type = ToCompilerType (instance_type.getValue ());
1770
+ ConstString a = ref_type.GetMangledTypeName ();
1771
+ ConstString b = class_type_or_name.GetCompilerType ().GetMangledTypeName ();
1772
+ if (a != b)
1773
+ llvm::dbgs () << " RemoteAST and runtime diverge " << a << " != " << b
1774
+ << " \n " ;
1775
+ } else {
1776
+ if (log ) {
1777
+ log ->Printf (
1778
+ " could not get type metadata from address %" PRIu64 " : %s\n " ,
1779
+ *metadata_address, instance_type.getFailure ().render ().c_str ());
1780
+ }
1781
+ }
1736
1782
}
1737
1783
1738
- // The read lock must have been acquired by the caller.
1739
- class_type_or_name.SetCompilerType (
1740
- {&scratch_ctx, instance_type.getValue ().getPointer ()});
1784
+ #endif
1741
1785
return true ;
1742
1786
}
1743
1787
0 commit comments