Skip to content
This repository has been archived by the owner on Apr 2, 2020. It is now read-only.

[WIP/Don't touch] Use mirrors and type reconstruction for classes. #1977

Open
wants to merge 1 commit into
base: stable
Choose a base branch
from
Open
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
49 changes: 23 additions & 26 deletions source/Target/SwiftLanguageRuntime.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1543,35 +1543,32 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
return false;
address.SetRawAddress(class_metadata_ptr);

Log *log(GetLogIfAllCategoriesSet(LIBLLDB_LOG_TYPES));
auto &remote_ast = GetRemoteASTContext(scratch_ctx);
swift::remote::RemoteAddress instance_address(class_metadata_ptr);
auto metadata_address = remote_ast.getHeapMetadataForObject(instance_address);
if (!metadata_address) {
if (log) {
log->Printf("could not read heap metadata for object at %lu: %s\n",
class_metadata_ptr,
metadata_address.getFailure().render().c_str());
}

// Ask mirrors about the right type.
auto metadata = reflection_ctx->readMetadataFromInstance(class_metadata_ptr);
auto tr = reflection_ctx->readTypeFromMetadata(*metadata);
if (!tr)
return false;
}
std::string mangled;
if (auto *ntr = llvm::dyn_cast<swift::reflection::NominalTypeRef>(tr))
mangled = ntr->getMangledName();
else if (auto *bgtr =
llvm::dyn_cast<swift::reflection::BoundGenericTypeRef>(tr))
mangled = bgtr->getMangledName();
else
llvm_unreachable("Invalid typeref!");

auto instance_type =
remote_ast.getTypeForRemoteTypeMetadata(metadata_address.getValue(),
/*skipArtificial=*/true);
if (!instance_type) {
if (log) {
log->Printf("could not get type metadata from address %" PRIu64 " : %s\n",
metadata_address.getValue().getAddressData(),
instance_type.getFailure().render().c_str());
}
return false;
}
Status error;

// FIXME: Use an API to retrieve the prefix instead of hardcoding it.
ConstString mangled_with_prefix("$s" + mangled);

SwiftASTContext *swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContext>(
in_value.GetCompilerType().GetTypeSystem());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Obviously this isn't meant to be production-ready yet, but: This should be the scratch context, not in_value's module context.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed.


// The read lock must have been acquired by the caller.
class_type_or_name.SetCompilerType(
{&scratch_ctx, instance_type.getValue().getPointer()});
// TypeRef -> swift::Type conversion through type reconstruction.
auto resolved_type =
swift_ast_ctx->GetTypeFromMangledTypename(mangled_with_prefix, error);
class_type_or_name.SetCompilerType(resolved_type);
return true;
}

Expand Down