@@ -765,7 +765,7 @@ SwiftLanguageRuntimeImpl::emplaceClangTypeInfo(
765
765
llvm::Optional<uint64_t >
766
766
SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST (
767
767
CompilerType instance_type, ValueObject *instance,
768
- ConstString member_name) {
768
+ llvm::StringRef member_name) {
769
769
auto *scratch_ctx =
770
770
llvm::cast<SwiftASTContext>(instance_type.GetTypeSystem ());
771
771
if (scratch_ctx->HasFatalErrors ())
@@ -777,7 +777,7 @@ SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST(
777
777
GetCanonicalSwiftType (instance_type).getPointer ();
778
778
779
779
// Perform the cache lookup.
780
- MemberID key{swift_type, member_name.GetCString ()};
780
+ MemberID key{swift_type, ConstString ( member_name) .GetCString ()};
781
781
auto it = m_member_offsets.find (key);
782
782
if (it != m_member_offsets.end ())
783
783
return it->second ;
@@ -818,7 +818,7 @@ SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST(
818
818
bound.GetTypeName ().AsCString ());
819
819
820
820
swift_type = GetCanonicalSwiftType (bound).getPointer ();
821
- MemberID key{swift_type, member_name.GetCString ()};
821
+ MemberID key{swift_type, ConstString ( member_name) .GetCString ()};
822
822
auto it = m_member_offsets.find (key);
823
823
if (it != m_member_offsets.end ())
824
824
return it->second ;
@@ -834,15 +834,15 @@ SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST(
834
834
835
835
// Use RemoteAST to determine the member offset.
836
836
if (safe_to_use_remote_ast) {
837
- swift::remoteAST::Result<uint64_t > result = remote_ast-> getOffsetOfMember (
838
- swift_type, optmeta, member_name. GetStringRef () );
837
+ swift::remoteAST::Result<uint64_t > result =
838
+ remote_ast-> getOffsetOfMember ( swift_type, optmeta, member_name);
839
839
if (result) {
840
840
LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
841
841
" [MemberVariableOffsetResolver] offset discovered = %" PRIu64,
842
842
(uint64_t )result.getValue ());
843
843
844
844
// Cache this result.
845
- MemberID key{swift_type, member_name.GetCString ()};
845
+ MemberID key{swift_type, ConstString ( member_name) .GetCString ()};
846
846
m_member_offsets.insert ({key, result.getValue ()});
847
847
return result.getValue ();
848
848
}
@@ -856,8 +856,10 @@ SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteAST(
856
856
}
857
857
858
858
llvm::Optional<uint64_t > SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemoteMirrors (
859
- CompilerType instance_type, ValueObject *instance, ConstString member_name,
859
+ CompilerType instance_type, ValueObject *instance, llvm::StringRef member_name,
860
860
Status *error) {
861
+ LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
862
+ " using remote mirrors" );
861
863
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
862
864
instance_type.GetTypeSystem ());
863
865
if (!ts) {
@@ -866,86 +868,66 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffsetRemote
866
868
return {};
867
869
}
868
870
869
- // Try remote mirrors.
870
- LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
871
- " [GetMemberVariableOffsetRemoteMirrors] using remote mirrors" );
872
- if (const swift::reflection::TypeInfo *type_info = GetTypeInfo (
873
- instance_type,
874
- instance ? instance->GetExecutionContextRef ().GetFrameSP ().get ()
875
- : nullptr )) {
876
- auto record_type_info =
877
- llvm::dyn_cast<swift::reflection::RecordTypeInfo>(type_info);
878
- if (record_type_info) {
879
- // Handle tuples.
880
- LLDB_LOGF (
881
- GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
882
- " [GetMemberVariableOffsetRemoteMirrors] using record type info" );
883
- if (record_type_info->getRecordKind () ==
884
- swift::reflection::RecordKind::Tuple) {
885
- unsigned tuple_idx;
886
- if (member_name.GetStringRef ().getAsInteger (10 , tuple_idx) ||
887
- tuple_idx >= record_type_info->getNumFields ()) {
888
- if (error)
889
- error->SetErrorString (" tuple index out of bounds" );
890
- return {};
891
- }
892
- return record_type_info->getFields ()[tuple_idx].Offset ;
893
- }
894
-
895
- // Handle other record types.
896
- for (auto &field : record_type_info->getFields ()) {
897
- if (ConstString (field.Name ) == member_name)
898
- return field.Offset ;
871
+ // Try the static type metadata.
872
+ auto frame = instance ? instance->GetExecutionContextRef ().GetFrameSP ().get ()
873
+ : nullptr ;
874
+ if (auto *ti = llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(
875
+ GetTypeInfo (instance_type, frame))) {
876
+ auto fields = ti->getFields ();
877
+ LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
878
+ " using record type info" );
879
+
880
+ // Handle tuples.
881
+ if (ti->getRecordKind () == swift::reflection::RecordKind::Tuple) {
882
+ unsigned tuple_idx;
883
+ if (member_name.getAsInteger (10 , tuple_idx) ||
884
+ tuple_idx >= ti->getNumFields ()) {
885
+ if (error)
886
+ error->SetErrorString (" tuple index out of bounds" );
887
+ return {};
899
888
}
889
+ return fields[tuple_idx].Offset ;
900
890
}
901
- }
902
891
903
- lldb::addr_t pointer = instance->GetPointerValue ();
904
- auto *reflection_ctx = GetReflectionContext ();
905
- if (!reflection_ctx)
906
- return {};
907
-
908
- auto find_field =
909
- [&](const swift::reflection::TypeInfo *ti) -> llvm::Optional<uint64_t > {
910
- auto class_type_info =
911
- llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti);
912
- if (class_type_info) {
913
- for (auto &field : class_type_info->getFields ()) {
914
- if (ConstString (field.Name ) == member_name)
915
- return field.Offset ;
916
- }
917
- }
918
- return {};
919
- };
920
-
921
- LLDBTypeInfoProvider provider (*this , *ts);
922
- auto md_ptr = reflection_ctx->readMetadataFromInstance (pointer);
923
- LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
924
- " [GetMemberVariableOffsetRemoteMirrors] using instance type info" );
925
- while (md_ptr && *md_ptr) {
926
- if (auto offset =
927
- find_field (reflection_ctx->getMetadataTypeInfo (*md_ptr, &provider)))
928
- return offset;
929
-
930
- // Continue with the base class.
931
- md_ptr = reflection_ctx->readSuperClassFromClassMetadata (*md_ptr);
892
+ // Handle other record types.
893
+ for (auto &field : fields)
894
+ if (StringRef (field.Name ) == member_name)
895
+ return field.Offset ;
932
896
}
933
897
934
- return {};
898
+ // Try the instance type metadata.
899
+ bool did_log = false ;
900
+ llvm::Optional<uint64_t > result;
901
+ if (instance)
902
+ ForEachSuperClassTypeInfo (
903
+ *instance, [&](const swift::reflection::RecordTypeInfo &ti) -> bool {
904
+ if (!did_log) {
905
+ did_log = true ;
906
+ LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
907
+ " using instance type info" );
908
+ }
909
+ for (auto &field : ti.getFields ())
910
+ if (StringRef (field.Name ) == member_name) {
911
+ result = field.Offset ;
912
+ return true ;
913
+ }
914
+ return false ;
915
+ });
916
+
917
+ return result;
935
918
}
936
919
937
-
938
920
llvm::Optional<uint64_t > SwiftLanguageRuntimeImpl::GetMemberVariableOffset (
939
- CompilerType instance_type, ValueObject *instance, ConstString member_name,
940
- Status *error) {
921
+ CompilerType instance_type, ValueObject *instance,
922
+ llvm::StringRef member_name, Status *error) {
941
923
llvm::Optional<uint64_t > offset;
942
924
943
925
if (!instance_type.IsValid ())
944
926
return {};
945
927
946
928
LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
947
929
" [GetMemberVariableOffset] asked to resolve offset for member %s" ,
948
- member_name.AsCString ());
930
+ member_name.str (). c_str ());
949
931
950
932
// Using the module context for RemoteAST is cheaper bit only safe
951
933
// when there is no dynamic type resolution involved.
@@ -984,16 +966,54 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffset(
984
966
if (offset) {
985
967
LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
986
968
" [GetMemberVariableOffset] offset of %s is %d" ,
987
- member_name.AsCString (), *offset);
969
+ member_name.str (). c_str (), *offset);
988
970
} else {
989
971
LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
990
972
" [GetMemberVariableOffset] failed for %s" ,
991
- member_name.AsCString ());
973
+ member_name.str (). c_str ());
992
974
if (error)
993
975
error->SetErrorStringWithFormat (" could not resolve member offset" );
994
976
}
995
977
return offset;
996
978
}
979
+
980
+ bool SwiftLanguageRuntimeImpl::ForEachSuperClassTypeInfo (
981
+ ValueObject &instance,
982
+ std::function<bool (const swift::reflection::RecordTypeInfo &rti)> fn) {
983
+ lldb::addr_t pointer = instance.GetPointerValue ();
984
+ auto *reflection_ctx = GetReflectionContext ();
985
+ if (!reflection_ctx)
986
+ return false ;
987
+
988
+ auto *ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRef>(
989
+ instance.GetCompilerType ().GetTypeSystem ());
990
+ if (!ts)
991
+ return false ;
992
+
993
+ LLDBTypeInfoProvider provider (*this , *ts);
994
+ auto md_ptr = reflection_ctx->readMetadataFromInstance (pointer);
995
+ if (!md_ptr)
996
+ return false ;
997
+
998
+ // Class object.
999
+ LLDB_LOGF (GetLogIfAllCategoriesSet (LIBLLDB_LOG_TYPES),
1000
+ " found RecordTypeInfo for instance" );
1001
+ while (md_ptr && *md_ptr) {
1002
+ auto *ti = reflection_ctx->getMetadataTypeInfo (*md_ptr, &provider);
1003
+ if (auto *class_type_info =
1004
+ llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
1005
+ if (fn (*class_type_info))
1006
+ return true ;
1007
+ }
1008
+
1009
+ // Continue with the base class.
1010
+ md_ptr = reflection_ctx->readSuperClassFromClassMetadata (*md_ptr);
1011
+ }
1012
+ return false ;
1013
+ }
1014
+
1015
+
1016
+
997
1017
bool SwiftLanguageRuntime::IsSelf (Variable &variable) {
998
1018
// A variable is self if its name if "self", and it's either a
999
1019
// function argument or a local variable and it's scope is a
0 commit comments