Skip to content

[lldb] Wire-up TypeSystemSwiftTypeRef::GetNumChildren #2215

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
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
38 changes: 32 additions & 6 deletions lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwiftTypeRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1508,10 +1508,9 @@ template <> bool Equivalent<ConstString>(ConstString l, ConstString r) {
return l == r;
}

/// Version taylored to GetBitSize & friends.
template <>
bool Equivalent<llvm::Optional<uint64_t>>(llvm::Optional<uint64_t> l,
llvm::Optional<uint64_t> r) {
/// Version tailored to GetBitSize & friends.
template <typename T>
bool Equivalent(llvm::Optional<T> l, llvm::Optional<T> r) {
if (l == r)
return true;
// There are situations where SwiftASTContext incorrectly returns
Expand All @@ -1526,6 +1525,12 @@ bool Equivalent<llvm::Optional<uint64_t>>(llvm::Optional<uint64_t> l,
return false;
}

// Introduced for `GetNumChildren`.
template <typename T>
bool Equivalent(llvm::Optional<T> l, T r) {
return Equivalent(l, llvm::Optional<T>(r));
}

} // namespace
#endif

Expand Down Expand Up @@ -2066,13 +2071,34 @@ lldb::Encoding TypeSystemSwiftTypeRef::GetEncoding(opaque_compiler_type_t type,
lldb::Format TypeSystemSwiftTypeRef::GetFormat(opaque_compiler_type_t type) {
return m_swift_ast_context->GetFormat(ReconstructType(type));
}

uint32_t
TypeSystemSwiftTypeRef::GetNumChildren(opaque_compiler_type_t type,
bool omit_empty_base_classes,
const ExecutionContext *exe_ctx) {
return m_swift_ast_context->GetNumChildren(ReconstructType(type),
omit_empty_base_classes, exe_ctx);
if (exe_ctx)
if (auto *exe_scope = exe_ctx->GetBestExecutionContextScope())
if (auto *runtime =
SwiftLanguageRuntime::Get(exe_scope->CalculateProcess()))
if (auto num_children =
runtime->GetNumChildren(GetCanonicalType(type), nullptr)) {
// Use a lambda to intercept and unwrap the `Optional` return value.
return [&]() {
auto impl = [&]() { return num_children; };
VALIDATE_AND_RETURN(
impl, GetNumChildren, type,
(ReconstructType(type), omit_empty_base_classes, exe_ctx));
}().getValue();
}

LLDB_LOGF(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES),
"Using SwiftASTContext::GetNumChildren fallback for type %s",
AsMangledName(type));

return m_swift_ast_context->GetNumChildren(
ReconstructType(type), omit_empty_base_classes, exe_ctx);
}

uint32_t TypeSystemSwiftTypeRef::GetNumFields(opaque_compiler_type_t type) {
return m_swift_ast_context->GetNumFields(ReconstructType(type));
}
Expand Down
19 changes: 17 additions & 2 deletions lldb/source/Target/SwiftLanguageRuntimeDynamicTypeResolution.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,23 @@ SwiftLanguageRuntimeImpl::GetNumChildren(CompilerType type,
// Structs and Tuples.
if (auto *rti =
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
auto fields = rti->getFields();
return fields.size();
switch (rti->getRecordKind()) {
case swift::reflection::RecordKind::ThickFunction:
// There are two fields, `function` and `context`, but they're not exposed
// by lldb.
return 0;
case swift::reflection::RecordKind::OpaqueExistential:
// `OpaqueExistential` is documented as:
// An existential is a three-word buffer followed by value metadata...
// The buffer is exposed as children named `payload_data_{0,1,2}`, and
// the number of fields are increased to match.
return rti->getNumFields() + 3;
default:
return rti->getNumFields();
}
}
if (auto *eti = llvm::dyn_cast_or_null<swift::reflection::EnumTypeInfo>(ti)) {
return eti->getNumPayloadCases();
}
// FIXME: Implement more cases.
return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ 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 = (other = 10)",
"(SomeLibrary.ContainsTwoInts) container = {", "other = 10",
"(Int) simple = 1"])
self.expect("e container", substrs=["(SomeLibrary.ContainsTwoInts)", "other = 10"])
self.expect("e container.wrapped", error=True, substrs=["value of type 'ContainsTwoInts' has no member 'wrapped'"])