18
18
#include " Plugins/ExpressionParser/Clang/ClangUtil.h"
19
19
#include " Plugins/TypeSystem/Clang/TypeSystemClang.h"
20
20
#include " Plugins/TypeSystem/Swift/SwiftDemangle.h"
21
+ #include " lldb/Core/ValueObjectMemory.h"
21
22
#include " lldb/Symbol/Variable.h"
22
23
#include " lldb/Symbol/VariableList.h"
23
24
#include " lldb/Target/ProcessStructReader.h"
@@ -909,9 +910,12 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
909
910
if (!ts)
910
911
return {};
911
912
913
+ lldb::addr_t pointer = LLDB_INVALID_ADDRESS;
912
914
ExecutionContext exe_ctx;
913
- if (valobj)
915
+ if (valobj) {
914
916
exe_ctx = valobj->GetExecutionContextRef ();
917
+ pointer = valobj->GetPointerValue ();
918
+ }
915
919
916
920
// Deal with the LLDB-only SILPackType variant.
917
921
if (auto pack_element_type = ts->GetSILPackElementAtIndex (type, idx)) {
@@ -934,6 +938,12 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
934
938
[&](const swift::reflection::FieldInfo &field,
935
939
llvm::Optional<TypeSystemSwift::TupleElement> tuple,
936
940
bool hide_existentials) -> CompilerType {
941
+ bool is_indirect_enum =
942
+ !field.Offset && field.TR &&
943
+ llvm::isa<swift::reflection::BuiltinTypeRef>(field.TR ) &&
944
+ llvm::isa<swift::reflection::ReferenceTypeInfo>(field.TI ) &&
945
+ llvm::cast<swift::reflection::ReferenceTypeInfo>(field.TI )
946
+ .getReferenceKind () == swift::reflection::ReferenceKind::Strong;
937
947
child_name = tuple ? tuple->element_name .GetStringRef ().str () : field.Name ;
938
948
child_byte_size = field.TI .getSize ();
939
949
child_byte_offset = field.Offset ;
@@ -942,13 +952,36 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
942
952
child_is_base_class = false ;
943
953
child_is_deref_of_parent = false ;
944
954
language_flags = 0 ;
955
+ if (is_indirect_enum)
956
+ language_flags |= TypeSystemSwift::LanguageFlags::eIsIndirectEnumCase;
945
957
// SwiftASTContext hardcodes the members of protocols as raw
946
958
// pointers. Remote Mirrors reports them as UnknownObject instead.
947
959
if (hide_existentials && ts->IsExistentialType (type.GetOpaqueQualType ()))
948
960
return ts->GetRawPointerType ();
949
- CompilerType result =
950
- tuple ? tuple->element_type : GetTypeFromTypeRef (*ts, field.TR );
951
- // Bug-for-bug compatibility. See comment in SwiftASTContext::GetBitSize().
961
+ CompilerType result;
962
+ if (tuple)
963
+ result = tuple->element_type ;
964
+ else if (is_indirect_enum) {
965
+ ThreadSafeReflectionContext reflection_ctx = GetReflectionContext ();
966
+ if (!reflection_ctx)
967
+ return {};
968
+ // The indirect enum field should point to a closure context.
969
+ LLDBTypeInfoProvider tip (*this , *ts);
970
+ lldb::addr_t instance = MaskMaybeBridgedPointer (m_process, pointer);
971
+ auto *ti = reflection_ctx->GetTypeInfoFromInstance (instance, &tip);
972
+ if (!ti)
973
+ return {};
974
+ auto *rti = llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti);
975
+ if (rti->getFields ().size () < 1 )
976
+ return {};
977
+ auto &field = rti->getFields ()[0 ];
978
+ auto *type_ref = field.TR ;
979
+ result = GetTypeFromTypeRef (*ts, type_ref);
980
+ child_byte_offset = field.Offset ;
981
+ } else
982
+ result = GetTypeFromTypeRef (*ts, field.TR );
983
+ // Bug-for-bug compatibility. See comment in
984
+ // SwiftASTContext::GetBitSize().
952
985
if (result.IsFunctionType ())
953
986
child_byte_size = ts->GetPointerByteSize ();
954
987
return result;
@@ -1000,7 +1033,7 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
1000
1033
child_is_base_class = false ;
1001
1034
child_is_deref_of_parent = false ;
1002
1035
language_flags = 0 ;
1003
- return ts->GetRawPointerType ();
1036
+ return ts->GetRawPointerType ();
1004
1037
}
1005
1038
}
1006
1039
return get_from_field_info (fields[idx], tuple, true );
@@ -1012,20 +1045,8 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
1012
1045
// Skip non-payload cases.
1013
1046
if (!enum_case.TR )
1014
1047
continue ;
1015
- if (i++ == idx) {
1016
- auto is_indirect = [](const swift::reflection::FieldInfo &field) {
1017
- // FIXME: This is by observation. What's the correct condition?
1018
- if (auto *tr =
1019
- llvm::dyn_cast_or_null<swift::reflection::BuiltinTypeRef>(
1020
- field.TR ))
1021
- return llvm::StringRef (tr->getMangledName ()).equals (" Bo" );
1022
- return false ;
1023
- };
1024
- auto result = get_from_field_info (enum_case, {}, true );
1025
- if (is_indirect (enum_case))
1026
- language_flags |= TypeSystemSwift::LanguageFlags::eIsIndirectEnumCase;
1027
- return result;
1028
- }
1048
+ if (i++ == idx)
1049
+ return get_from_field_info (enum_case, {}, true );
1029
1050
}
1030
1051
LLDB_LOGF (GetLog (LLDBLog::Types), " index %zu is out of bounds (%d)" , idx,
1031
1052
eti->getNumPayloadCases ());
@@ -1054,10 +1075,9 @@ CompilerType SwiftLanguageRuntimeImpl::GetChildCompilerTypeAtIndex(
1054
1075
if (!instance_ts)
1055
1076
return {};
1056
1077
1057
- // LLDBTypeInfoProvider needs to kept alive until as long as supers gets accessed.
1078
+ // LLDBTypeInfoProvider needs to be kept alive while supers gets accessed.
1058
1079
llvm::SmallVector<SuperClassType, 2 > supers;
1059
1080
LLDBTypeInfoProvider tip (*this , *instance_ts);
1060
- lldb::addr_t pointer = valobj->GetPointerValue ();
1061
1081
reflection_ctx->ForEachSuperClassType (
1062
1082
&tip, pointer, [&](SuperClassType sc) -> bool {
1063
1083
if (!found_start) {
@@ -1983,95 +2003,38 @@ bool SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_IndirectEnumCase(
1983
2003
ValueObject &in_value, lldb::DynamicValueType use_dynamic,
1984
2004
TypeAndOrName &class_type_or_name, Address &address,
1985
2005
Value::ValueType &value_type) {
1986
- static ConstString g_offset (" offset" );
1987
-
1988
- DataExtractor data;
1989
2006
Status error;
1990
- if (!(in_value.GetParent () && in_value.GetParent ()->GetData (data, error) &&
1991
- error.Success ()))
1992
- return false ;
2007
+ CompilerType child_type = in_value.GetCompilerType ();
2008
+ class_type_or_name.SetCompilerType (child_type);
1993
2009
1994
- bool has_payload;
1995
- bool is_indirect;
1996
- CompilerType payload_type;
1997
- if (!SwiftASTContext::GetSelectedEnumCase (
1998
- in_value.GetParent ()->GetCompilerType (), data, nullptr , &has_payload,
1999
- &payload_type, &is_indirect))
2000
- return false ;
2001
-
2002
- if (has_payload && is_indirect && payload_type)
2003
- class_type_or_name.SetCompilerType (payload_type);
2004
-
2005
- lldb::addr_t box_addr = in_value.GetValueAsUnsigned (LLDB_INVALID_ADDRESS);
2010
+ auto *enum_obj = in_value.GetParent ();
2011
+ lldb::addr_t box_addr = enum_obj->GetPointerValue ();
2006
2012
if (box_addr == LLDB_INVALID_ADDRESS)
2007
2013
return false ;
2008
2014
2009
- box_addr = MaskMaybeBridgedPointer (m_process, box_addr);
2015
+ box_addr =
2016
+ MaskMaybeBridgedPointer (m_process, box_addr);
2010
2017
lldb::addr_t box_location = m_process.ReadPointerFromMemory (box_addr, error);
2011
2018
if (box_location == LLDB_INVALID_ADDRESS)
2012
2019
return false ;
2013
-
2020
+
2014
2021
box_location = MaskMaybeBridgedPointer (m_process, box_location);
2015
- ProcessStructReader reader (&m_process, box_location, GetBoxMetadataType ());
2016
- uint32_t offset = reader.GetField <uint32_t >(g_offset);
2017
- lldb::addr_t box_value = box_addr + offset;
2018
-
2019
- // try to read one byte at the box value
2020
- m_process.ReadUnsignedIntegerFromMemory (box_value, 1 , 0 , error);
2021
- if (error.Fail ()) // and if that fails, then we're off in no man's land
2022
- return false ;
2023
-
2024
- Flags type_info (payload_type.GetTypeInfo ());
2025
- if (type_info.AllSet (eTypeIsSwift | eTypeIsClass)) {
2026
- lldb::addr_t old_box_value = box_value;
2027
- box_value = m_process.ReadPointerFromMemory (box_value, error);
2028
- if (box_value == LLDB_INVALID_ADDRESS)
2029
- return false ;
2030
-
2031
- DataExtractor data (&box_value, m_process.GetAddressByteSize (),
2032
- m_process.GetByteOrder (),
2033
- m_process.GetAddressByteSize ());
2034
- ValueObjectSP valobj_sp (ValueObject::CreateValueObjectFromData (
2035
- " _" , data, m_process, payload_type));
2022
+ lldb::addr_t box_value = box_addr + in_value.GetByteOffset ();
2023
+ Flags type_info (child_type.GetTypeInfo ());
2024
+ if (type_info.AllSet (eTypeIsSwift) &&
2025
+ type_info.AnySet (eTypeIsClass | eTypeIsProtocol)) {
2026
+ ExecutionContext exe_ctx = in_value.GetExecutionContextRef ();
2027
+ ValueObjectSP valobj_sp = ValueObjectMemory::Create (
2028
+ exe_ctx.GetBestExecutionContextScope (), " _" , box_value, child_type);
2036
2029
if (!valobj_sp)
2037
2030
return false ;
2038
2031
2039
- if (!GetDynamicTypeAndAddress (*valobj_sp, use_dynamic, class_type_or_name,
2040
- address, value_type))
2041
- return false ;
2042
-
2043
- address.SetRawAddress (old_box_value);
2044
- return true ;
2045
- } else if (type_info.AllSet (eTypeIsSwift | eTypeIsProtocol)) {
2046
- SwiftASTContext::ProtocolInfo protocol_info;
2047
- if (!SwiftASTContext::GetProtocolTypeInfo (payload_type, protocol_info))
2048
- return false ;
2049
- auto ptr_size = m_process.GetAddressByteSize ();
2050
- std::vector<uint8_t > buffer (ptr_size * protocol_info.m_num_storage_words ,
2051
- 0 );
2052
- for (uint32_t idx = 0 ; idx < protocol_info.m_num_storage_words ; idx++) {
2053
- lldb::addr_t word = m_process.ReadUnsignedIntegerFromMemory (
2054
- box_value + idx * ptr_size, ptr_size, 0 , error);
2055
- if (error.Fail ())
2056
- return false ;
2057
- memcpy (&buffer[idx * ptr_size], &word, ptr_size);
2058
- }
2059
- DataExtractor data (&buffer[0 ], buffer.size (), m_process.GetByteOrder (),
2060
- m_process.GetAddressByteSize ());
2061
- ValueObjectSP valobj_sp (ValueObject::CreateValueObjectFromData (
2062
- " _" , data, m_process, payload_type));
2063
- if (!valobj_sp)
2064
- return false ;
2065
-
2066
- if (!GetDynamicTypeAndAddress (*valobj_sp, use_dynamic, class_type_or_name,
2067
- address, value_type))
2068
- return false ;
2069
-
2070
- address.SetRawAddress (box_value);
2071
- return true ;
2032
+ return GetDynamicTypeAndAddress (*valobj_sp, use_dynamic, class_type_or_name,
2033
+ address, value_type);
2072
2034
} else {
2073
2035
// This is most likely a statically known type.
2074
2036
address.SetLoadAddress (box_value, &m_process.GetTarget ());
2037
+ value_type = Value::GetValueTypeFromAddressType (eAddressTypeLoad);
2075
2038
return true ;
2076
2039
}
2077
2040
}
0 commit comments