@@ -1805,18 +1805,128 @@ bool SwiftLanguageRuntimeImpl::IsValidErrorValue(ValueObject &in_value) {
1805
1805
1806
1806
return true ;
1807
1807
}
1808
+ // / Digs into the protocols inside a protocol composition and checks if
1809
+ // / any of them belong to the LLDB module.
1810
+ static bool
1811
+ IsDeclaredInLLDBExpr (const swift::reflection::TypeRef *typeref) {
1812
+ using swift::Demangle::Node;
1813
+
1814
+ const auto *protocol_typeref = llvm::dyn_cast_or_null<
1815
+ const swift::reflection::ProtocolCompositionTypeRef>(typeref);
1816
+
1817
+ if (!protocol_typeref)
1818
+ return false ;
1819
+
1820
+ swift::Demangle::Demangler dem;
1821
+
1822
+ NodePointer type = protocol_typeref->getDemangling (dem);
1823
+ if (!type || type->getKind () != Node::Kind::Type ||
1824
+ type->getNumChildren () == 0 )
1825
+ return false ;
1826
+
1827
+ NodePointer maybe_protocol_list = type->getFirstChild ();
1828
+ NodePointer protocol_list;
1829
+
1830
+ if (!maybe_protocol_list)
1831
+ return false ;
1832
+
1833
+ switch (maybe_protocol_list->getKind ()) {
1834
+ case Node::Kind::ProtocolList:
1835
+ protocol_list = maybe_protocol_list;
1836
+ break ;
1837
+ case Node::Kind::ProtocolListWithAnyObject:
1838
+ case Node::Kind::ProtocolListWithClass:
1839
+ if (maybe_protocol_list->getNumChildren () == 0 )
1840
+ return false ;
1841
+ protocol_list = maybe_protocol_list->getFirstChild ();
1842
+ break ;
1843
+ default :
1844
+ return false ;
1845
+ }
1846
+
1847
+ if (!protocol_list || protocol_list->getKind () != Node::Kind::ProtocolList ||
1848
+ protocol_list->getNumChildren () == 0 )
1849
+ return false ;
1850
+
1851
+ NodePointer type_list = protocol_list->getFirstChild ();
1852
+ if (!type_list || type_list->getKind () != Node::Kind::TypeList)
1853
+ return false ;
1854
+
1855
+ for (NodePointer internal_type : *type_list) {
1856
+ if (!internal_type || internal_type->getKind () != Node::Kind::Type ||
1857
+ internal_type->getNumChildren () == 0 )
1858
+ continue ;
1859
+
1860
+ NodePointer protocol = internal_type->getFirstChild ();
1861
+ if (!protocol || protocol->getKind () != Node::Kind::Protocol ||
1862
+ protocol->getNumChildren () == 0 )
1863
+ continue ;
1864
+
1865
+ NodePointer module = protocol->getFirstChild ();
1866
+ if (!module || module ->getKind () != Node::Kind::Module ||
1867
+ !module ->hasText ())
1868
+ continue ;
1869
+
1870
+ if (module ->getText ().startswith (" __lldb_expr_" ))
1871
+ return true ;
1872
+ }
1873
+ return false ;
1874
+ }
1808
1875
1809
1876
bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol (
1810
1877
ValueObject &in_value, CompilerType protocol_type,
1811
1878
SwiftASTContextForExpressions &scratch_ctx,
1812
1879
lldb::DynamicValueType use_dynamic, TypeAndOrName &class_type_or_name,
1813
1880
Address &address) {
1881
+ auto remote_ast_impl = [&](bool use_local_buffer,
1882
+ lldb::addr_t existential_address)
1883
+ -> llvm::Optional<std::pair<CompilerType, Address>> {
1884
+ swift::remote::RemoteAddress remote_existential (existential_address);
1885
+ auto &remote_ast = GetRemoteASTContext (scratch_ctx);
1886
+ auto swift_type = GetSwiftType (protocol_type);
1887
+ if (!swift_type)
1888
+ return {};
1889
+ if (use_local_buffer)
1890
+ PushLocalBuffer (existential_address,
1891
+ in_value.GetByteSize ().getValueOr (0 ));
1892
+
1893
+ auto result = remote_ast.getDynamicTypeAndAddressForExistential (
1894
+ remote_existential, swift_type);
1895
+ if (use_local_buffer)
1896
+ PopLocalBuffer ();
1897
+
1898
+ if (!result.isSuccess ())
1899
+ return {};
1900
+
1901
+ auto type_and_address = result.getValue ();
1902
+
1903
+ CompilerType type = ToCompilerType (type_and_address.InstanceType );
1904
+ Address address;
1905
+ address.SetRawAddress (type_and_address.PayloadAddress .getAddressData ());
1906
+ return {{type, address}};
1907
+ };
1908
+
1814
1909
Log *log (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES));
1815
1910
1816
1911
auto &target = m_process.GetTarget ();
1817
1912
assert (IsScratchContextLocked (target) &&
1818
1913
" Swift scratch context not locked ahead" );
1819
- auto &remote_ast = GetRemoteASTContext (scratch_ctx);
1914
+
1915
+ auto *tss =
1916
+ llvm::dyn_cast_or_null<TypeSystemSwift>(protocol_type.GetTypeSystem ());
1917
+ if (!tss) {
1918
+ if (log)
1919
+ log->Printf (" Could not get type system swift" );
1920
+ return false ;
1921
+ }
1922
+
1923
+ const swift::reflection::TypeRef *protocol_typeref =
1924
+ GetTypeRef (protocol_type, tss->GetSwiftASTContext ());
1925
+ if (!protocol_typeref) {
1926
+ if (log)
1927
+ log->Printf (" Could not get protocol typeref" );
1928
+ return false ;
1929
+ }
1820
1930
1821
1931
lldb::addr_t existential_address;
1822
1932
bool use_local_buffer = false ;
@@ -1841,29 +1951,65 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_Protocol(
1841
1951
if (log)
1842
1952
log->Printf (" existential address is 0x%llx" , existential_address);
1843
1953
1844
- if (!existential_address || existential_address == LLDB_INVALID_ADDRESS)
1954
+ if (!existential_address || existential_address == LLDB_INVALID_ADDRESS) {
1955
+ if (log)
1956
+ log->Printf (" Existential address is invalid" );
1845
1957
return false ;
1958
+ }
1959
+
1960
+ // If the protocol list contains any types declared in LLDB,
1961
+ // we use the remote AST implementation since we currently
1962
+ // don't register the metadata generated by LLDB.
1963
+ if (IsDeclaredInLLDBExpr (protocol_typeref)) {
1964
+ auto pair = remote_ast_impl (use_local_buffer, existential_address);
1965
+ if (!pair)
1966
+ return false ;
1967
+ class_type_or_name.SetCompilerType (std::get<CompilerType>(*pair));
1968
+ address = std::get<Address>(*pair);
1969
+ return true ;
1970
+ }
1846
1971
1847
1972
if (use_local_buffer)
1848
1973
PushLocalBuffer (existential_address, in_value.GetByteSize ().getValueOr (0 ));
1849
1974
1850
1975
swift::remote::RemoteAddress remote_existential (existential_address);
1851
- auto result = remote_ast.getDynamicTypeAndAddressForExistential (
1852
- remote_existential, GetSwiftType (protocol_type));
1853
1976
1977
+ auto *reflection_ctx = GetReflectionContext ();
1978
+ auto pair = reflection_ctx->projectExistentialAndUnwrapClass (
1979
+ remote_existential, *protocol_typeref);
1854
1980
if (use_local_buffer)
1855
1981
PopLocalBuffer ();
1856
1982
1857
- if (!result. isSuccess () ) {
1983
+ if (!pair ) {
1858
1984
if (log)
1859
- log->Printf (" RemoteAST failed to get dynamic type of existential" );
1985
+ log->Printf (" Runtime failed to get dynamic type of existential" );
1860
1986
return false ;
1861
1987
}
1862
1988
1863
- auto type_and_address = result.getValue ();
1864
- class_type_or_name.SetCompilerType (
1865
- ToCompilerType (type_and_address.InstanceType ));
1866
- address.SetRawAddress (type_and_address.PayloadAddress .getAddressData ());
1989
+ const swift::reflection::TypeRef *typeref;
1990
+ swift::remote::RemoteAddress out_address (nullptr );
1991
+ std::tie (typeref, out_address) = *pair;
1992
+ auto &ts = tss->GetTypeSystemSwiftTypeRef ();
1993
+ swift::Demangle::Demangler dem;
1994
+ swift::Demangle::NodePointer node = typeref->getDemangling (dem);
1995
+ class_type_or_name.SetCompilerType (ts.RemangleAsType (dem, node));
1996
+ address.SetRawAddress (out_address.getAddressData ());
1997
+
1998
+ #ifndef NDEBUG
1999
+ auto reference_pair = remote_ast_impl (use_local_buffer, existential_address);
2000
+ assert (pair.hasValue () >= reference_pair.hasValue () &&
2001
+ " RemoteAST and runtime diverge" );
2002
+
2003
+ if (reference_pair) {
2004
+ CompilerType ref_type = std::get<CompilerType>(*reference_pair);
2005
+ Address ref_address = std::get<Address>(*reference_pair);
2006
+ ConstString a = class_type_or_name.GetCompilerType ().GetMangledTypeName ();
2007
+ ConstString b = ref_type.GetMangledTypeName ();
2008
+ if (a != b)
2009
+ llvm::dbgs () << " RemoteAST and runtime diverge " << a << " != " << b
2010
+ << " \n " ;
2011
+ }
2012
+ #endif
1867
2013
return true ;
1868
2014
}
1869
2015
0 commit comments