Skip to content

Commit af7c46a

Browse files
committed
[lldb] Add ReferenceTypeInfo support to SwiftLanguageRuntimeImpl::GetNumChildren
1 parent e9480ca commit af7c46a

File tree

2 files changed

+73
-37
lines changed

2 files changed

+73
-37
lines changed

lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 71 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -997,6 +997,38 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffset(
997997
return offset;
998998
}
999999

1000+
static CompilerType GetWeakReferent(TypeSystemSwiftTypeRef &ts,
1001+
CompilerType type) {
1002+
using namespace swift::Demangle;
1003+
Demangler dem;
1004+
auto mangled = type.GetMangledTypeName().GetStringRef();
1005+
NodePointer n = dem.demangleSymbol(mangled);
1006+
if (!n || n->getKind() != Node::Kind::Global || !n->hasChildren())
1007+
return {};
1008+
n = n->getFirstChild();
1009+
if (!n || n->getKind() != Node::Kind::TypeMangling || !n->hasChildren())
1010+
return {};
1011+
n = n->getFirstChild();
1012+
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
1013+
return {};
1014+
n = n->getFirstChild();
1015+
if (!n ||
1016+
(n->getKind() != Node::Kind::Weak &&
1017+
n->getKind() != Node::Kind::Unowned &&
1018+
n->getKind() != Node::Kind::Unmanaged) ||
1019+
!n->hasChildren())
1020+
return {};
1021+
n = n->getFirstChild();
1022+
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
1023+
return {};
1024+
// FIXME: We only need to canonicalize this node, not the entire type.
1025+
n = ts.CanonicalizeSugar(dem, n->getFirstChild());
1026+
if (!n || n->getKind() != Node::Kind::SugaredOptional || !n->hasChildren())
1027+
return {};
1028+
n = n->getFirstChild();
1029+
return ts.RemangleAsType(dem, n);
1030+
}
1031+
10001032
llvm::Optional<unsigned>
10011033
SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
10021034
ValueObject *valobj) {
@@ -1008,7 +1040,8 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
10081040
// Try the static type metadata.
10091041
auto frame =
10101042
valobj ? valobj->GetExecutionContextRef().GetFrameSP().get() : nullptr;
1011-
auto *ti = GetTypeInfo(type, frame);
1043+
const swift::reflection::TypeRef *tr = nullptr;
1044+
auto *ti = GetTypeInfo(type, frame, &tr);
10121045
// Structs and Tuples.
10131046
if (auto *rti =
10141047
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
@@ -1030,6 +1063,37 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
10301063
if (auto *eti = llvm::dyn_cast_or_null<swift::reflection::EnumTypeInfo>(ti)) {
10311064
return eti->getNumPayloadCases();
10321065
}
1066+
// Objects.
1067+
if (auto *rti =
1068+
llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {
1069+
1070+
switch (rti->getReferenceKind()) {
1071+
case swift::reflection::ReferenceKind::Weak:
1072+
case swift::reflection::ReferenceKind::Unowned:
1073+
case swift::reflection::ReferenceKind::Unmanaged:
1074+
// Weak references are implicitly Optionals, so report the one
1075+
// child of Optional here.
1076+
if (GetWeakReferent(*ts, type))
1077+
return 1;
1078+
break;
1079+
default:
1080+
break;
1081+
}
1082+
1083+
if (tr)
1084+
return {};
1085+
1086+
auto *reflection_ctx = GetReflectionContext();
1087+
auto tc = swift::reflection::TypeConverter(reflection_ctx->getBuilder());
1088+
LLDBTypeInfoProvider tip(*this, *ts);
1089+
auto *cti = tc.getClassInstanceTypeInfo(tr, 0, &tip);
1090+
if (auto *rti =
1091+
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(cti)) {
1092+
return rti->getNumFields();
1093+
}
1094+
1095+
return {};
1096+
}
10331097
// FIXME: Implement more cases.
10341098
return {};
10351099
}
@@ -1064,38 +1128,6 @@ static llvm::StringRef GetBaseName(swift::Demangle::NodePointer node) {
10641128
}
10651129
}
10661130

1067-
static CompilerType GetWeakReferent(TypeSystemSwiftTypeRef &ts,
1068-
CompilerType type) {
1069-
using namespace swift::Demangle;
1070-
Demangler dem;
1071-
auto mangled = type.GetMangledTypeName().GetStringRef();
1072-
NodePointer n = dem.demangleSymbol(mangled);
1073-
if (!n || n->getKind() != Node::Kind::Global || !n->hasChildren())
1074-
return {};
1075-
n = n->getFirstChild();
1076-
if (!n || n->getKind() != Node::Kind::TypeMangling || !n->hasChildren())
1077-
return {};
1078-
n = n->getFirstChild();
1079-
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
1080-
return {};
1081-
n = n->getFirstChild();
1082-
if (!n ||
1083-
(n->getKind() != Node::Kind::Weak &&
1084-
n->getKind() != Node::Kind::Unowned &&
1085-
n->getKind() != Node::Kind::Unmanaged) ||
1086-
!n->hasChildren())
1087-
return {};
1088-
n = n->getFirstChild();
1089-
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
1090-
return {};
1091-
// FIXME: We only need to canonicalize this node, not the entire type.
1092-
n = ts.CanonicalizeSugar(dem, n->getFirstChild());
1093-
if (!n || n->getKind() != Node::Kind::SugaredOptional || !n->hasChildren())
1094-
return {};
1095-
n = n->getFirstChild();
1096-
return ts.RemangleAsType(dem, n);
1097-
}
1098-
10991131
static CompilerType
11001132
GetTypeFromTypeRef(TypeSystemSwiftTypeRef &ts,
11011133
const swift::reflection::TypeRef *type_ref) {
@@ -2510,9 +2542,9 @@ SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
25102542
return type_ref;
25112543
}
25122544

2513-
const swift::reflection::TypeInfo *
2514-
SwiftLanguageRuntimeImpl::GetTypeInfo(CompilerType type,
2515-
ExecutionContextScope *exe_scope) {
2545+
const swift::reflection::TypeInfo *SwiftLanguageRuntimeImpl::GetTypeInfo(
2546+
CompilerType type, ExecutionContextScope *exe_scope,
2547+
swift::reflection::TypeRef const **out_tr) {
25162548
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(type.GetTypeSystem());
25172549
if (!ts)
25182550
return nullptr;
@@ -2543,6 +2575,9 @@ SwiftLanguageRuntimeImpl::GetTypeInfo(CompilerType type,
25432575
if (!type_ref)
25442576
return nullptr;
25452577

2578+
if (out_tr)
2579+
*out_tr = type_ref;
2580+
25462581
auto *reflection_ctx = GetReflectionContext();
25472582
if (!reflection_ctx)
25482583
return nullptr;

lldb/source/Target/SwiftLanguageRuntimeImpl.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class SwiftLanguageRuntimeImpl {
7878

7979
/// Ask Remote Mirrors for the type info about a Swift type.
8080
const swift::reflection::TypeInfo *
81-
GetTypeInfo(CompilerType type, ExecutionContextScope *exe_scope);
81+
GetTypeInfo(CompilerType type, ExecutionContextScope *exe_scope,
82+
swift::reflection::TypeRef const **out_tr = nullptr);
8283

8384
llvm::Optional<const swift::reflection::TypeInfo *>
8485
lookupClangTypeInfo(CompilerType clang_type);

0 commit comments

Comments
 (0)