Skip to content

[lldb] Add ReferenceTypeInfo support to SwiftLanguageRuntimeImpl::GetNumChildren #2224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
111 changes: 75 additions & 36 deletions lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,38 @@ llvm::Optional<uint64_t> SwiftLanguageRuntimeImpl::GetMemberVariableOffset(
return offset;
}

static CompilerType GetWeakReferent(TypeSystemSwiftTypeRef &ts,
CompilerType type) {
using namespace swift::Demangle;
Demangler dem;
auto mangled = type.GetMangledTypeName().GetStringRef();
NodePointer n = dem.demangleSymbol(mangled);
if (!n || n->getKind() != Node::Kind::Global || !n->hasChildren())
return {};
n = n->getFirstChild();
if (!n || n->getKind() != Node::Kind::TypeMangling || !n->hasChildren())
return {};
n = n->getFirstChild();
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
return {};
n = n->getFirstChild();
if (!n ||
(n->getKind() != Node::Kind::Weak &&
n->getKind() != Node::Kind::Unowned &&
n->getKind() != Node::Kind::Unmanaged) ||
!n->hasChildren())
return {};
n = n->getFirstChild();
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
return {};
// FIXME: We only need to canonicalize this node, not the entire type.
n = ts.CanonicalizeSugar(dem, n->getFirstChild());
if (!n || n->getKind() != Node::Kind::SugaredOptional || !n->hasChildren())
return {};
n = n->getFirstChild();
return ts.RemangleAsType(dem, n);
}

llvm::Optional<unsigned>
SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
ValueObject *valobj) {
Expand All @@ -1006,7 +1038,8 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
// Try the static type metadata.
auto frame =
valobj ? valobj->GetExecutionContextRef().GetFrameSP().get() : nullptr;
auto *ti = GetTypeInfo(type, frame);
const swift::reflection::TypeRef *tr = nullptr;
auto *ti = GetTypeInfo(type, frame, &tr);
// Structs and Tuples.
if (auto *rti =
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
Expand All @@ -1028,6 +1061,41 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
if (auto *eti = llvm::dyn_cast_or_null<swift::reflection::EnumTypeInfo>(ti)) {
return eti->getNumPayloadCases();
}
// Objects.
if (auto *rti =
llvm::dyn_cast_or_null<swift::reflection::ReferenceTypeInfo>(ti)) {

switch (rti->getReferenceKind()) {
case swift::reflection::ReferenceKind::Weak:
case swift::reflection::ReferenceKind::Unowned:
case swift::reflection::ReferenceKind::Unmanaged:
// Weak references are implicitly Optionals, so report the one
// child of Optional here.
if (GetWeakReferent(*ts, type))
return 1;
break;
default:
break;
}

if (!tr)
return {};

auto *reflection_ctx = GetReflectionContext();
auto &builder = reflection_ctx->getBuilder();
auto tc = swift::reflection::TypeConverter(builder);
LLDBTypeInfoProvider tip(*this, *ts);
auto *cti = tc.getClassInstanceTypeInfo(tr, 0, &tip);
if (auto *rti =
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(cti)) {
// The superclass, if any, is an extra child.
if (builder.lookupSuperclass(tr))
return rti->getNumFields() + 1;
return rti->getNumFields();
}

return {};
}
// FIXME: Implement more cases.
return {};
}
Expand Down Expand Up @@ -1062,38 +1130,6 @@ static llvm::StringRef GetBaseName(swift::Demangle::NodePointer node) {
}
}

static CompilerType GetWeakReferent(TypeSystemSwiftTypeRef &ts,
CompilerType type) {
using namespace swift::Demangle;
Demangler dem;
auto mangled = type.GetMangledTypeName().GetStringRef();
NodePointer n = dem.demangleSymbol(mangled);
if (!n || n->getKind() != Node::Kind::Global || !n->hasChildren())
return {};
n = n->getFirstChild();
if (!n || n->getKind() != Node::Kind::TypeMangling || !n->hasChildren())
return {};
n = n->getFirstChild();
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
return {};
n = n->getFirstChild();
if (!n ||
(n->getKind() != Node::Kind::Weak &&
n->getKind() != Node::Kind::Unowned &&
n->getKind() != Node::Kind::Unmanaged) ||
!n->hasChildren())
return {};
n = n->getFirstChild();
if (!n || n->getKind() != Node::Kind::Type || !n->hasChildren())
return {};
// FIXME: We only need to canonicalize this node, not the entire type.
n = ts.CanonicalizeSugar(dem, n->getFirstChild());
if (!n || n->getKind() != Node::Kind::SugaredOptional || !n->hasChildren())
return {};
n = n->getFirstChild();
return ts.RemangleAsType(dem, n);
}

static CompilerType
GetTypeFromTypeRef(TypeSystemSwiftTypeRef &ts,
const swift::reflection::TypeRef *type_ref) {
Expand Down Expand Up @@ -2508,9 +2544,9 @@ SwiftLanguageRuntimeImpl::GetTypeRef(CompilerType type,
return type_ref;
}

const swift::reflection::TypeInfo *
SwiftLanguageRuntimeImpl::GetTypeInfo(CompilerType type,
ExecutionContextScope *exe_scope) {
const swift::reflection::TypeInfo *SwiftLanguageRuntimeImpl::GetTypeInfo(
CompilerType type, ExecutionContextScope *exe_scope,
swift::reflection::TypeRef const **out_tr) {
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(type.GetTypeSystem());
if (!ts)
return nullptr;
Expand Down Expand Up @@ -2541,6 +2577,9 @@ SwiftLanguageRuntimeImpl::GetTypeInfo(CompilerType type,
if (!type_ref)
return nullptr;

if (out_tr)
*out_tr = type_ref;

auto *reflection_ctx = GetReflectionContext();
if (!reflection_ctx)
return nullptr;
Expand Down
3 changes: 2 additions & 1 deletion lldb/source/Target/SwiftLanguageRuntimeImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,8 @@ class SwiftLanguageRuntimeImpl {

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

llvm::Optional<const swift::reflection::TypeInfo *>
lookupClangTypeInfo(CompilerType clang_type);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ def cleanup():
# This test is deliberately checking what the user will see, rather than
# the structure provided by the Python API, in order to test the recovery.
self.expect("fr var", substrs=[
"(SomeLibrary.ContainsTwoInts) container = (wrapped = 0x",
", other = 10)",
"(SomeLibrary.ContainsTwoInts) container = {",
"wrapped = 0x",
"other = 10",
"(Int) simple = 1"])
self.expect("e container", substrs=["(SomeLibrary.ContainsTwoInts)", "(wrapped = 0x", ", other = 10"])
self.expect("e container", substrs=["(SomeLibrary.ContainsTwoInts)", "wrapped = 0x", "other = 10"])
self.expect("e container.wrapped", substrs=["(SomeLibrary.BoxedTwoInts)", "0x", "{}"])
self.expect("e container.wrapped.value", error=True, substrs=["value of type 'BoxedTwoInts' has no member 'value'"])