From 08d133bacfad77be852ef39cf0e20f451b650bf1 Mon Sep 17 00:00:00 2001 From: Davide Italiano Date: Mon, 9 Sep 2019 15:41:23 -0700 Subject: [PATCH] [SwiftLanguageRuntime] Use mirrors and type reconstruction for classes. --- source/Target/SwiftLanguageRuntime.cpp | 49 ++++++++++++-------------- 1 file changed, 23 insertions(+), 26 deletions(-) diff --git a/source/Target/SwiftLanguageRuntime.cpp b/source/Target/SwiftLanguageRuntime.cpp index 7a95d4db8313..9630ac130152 100644 --- a/source/Target/SwiftLanguageRuntime.cpp +++ b/source/Target/SwiftLanguageRuntime.cpp @@ -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(tr)) + mangled = ntr->getMangledName(); + else if (auto *bgtr = + llvm::dyn_cast(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( + in_value.GetCompilerType().GetTypeSystem()); - // 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; }