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