Skip to content

Commit b0074de

Browse files
Merge pull request #10639 from adrian-prantl/cherry-pick-repltypes
[lldb] Resolve Swift-implemented Objective-C classes using Swift runtime
2 parents c39ae09 + 73a1679 commit b0074de

File tree

13 files changed

+182
-143
lines changed

13 files changed

+182
-143
lines changed

lldb/source/Plugins/ExpressionParser/Swift/SwiftREPL.cpp

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -509,13 +509,30 @@ bool SwiftREPL::PrintOneVariable(Debugger &debugger, StreamFileSP &output_sp,
509509
options.SetRevealEmptyAggregates(false);
510510
options.SetHidePointerValue(true);
511511
options.SetVariableFormatDisplayLanguage(lldb::eLanguageTypeSwift);
512-
options.SetDeclPrintingHelper([](ConstString type_name,
513-
ConstString var_name,
514-
const DumpValueObjectOptions &options,
515-
Stream &stream) -> bool {
512+
options.SetDeclPrintingHelper([&](ConstString type_name,
513+
ConstString var_name,
514+
const DumpValueObjectOptions &options,
515+
Stream &stream) -> bool {
516516
if (!type_name || !var_name)
517517
return false;
518518

519+
// Try to get the SwiftASTContext representation of the type. It
520+
// will hide Objective-C implemention details that are not
521+
// publicly declared in the SDK.
522+
if (valobj_sp) {
523+
auto static_valobj_sp = valobj_sp->GetStaticValue();
524+
auto dynamic_valobj_sp =
525+
valobj_sp->GetDynamicValue(lldb::eDynamicCanRunTarget);
526+
if (static_valobj_sp && dynamic_valobj_sp) {
527+
CompilerType static_type = static_valobj_sp->GetCompilerType();
528+
CompilerType dynamic_type = dynamic_valobj_sp->GetCompilerType();
529+
auto ts =
530+
dynamic_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
531+
if (ts &&
532+
ts->IsImportedType(dynamic_type.GetOpaqueQualType(), nullptr))
533+
type_name = static_type.GetDisplayTypeName();
534+
}
535+
}
519536
std::string type_name_str(type_name ? type_name.GetCString() : "");
520537
for (auto iter = type_name_str.find(" *"); iter != std::string::npos;
521538
iter = type_name_str.find(" *")) {

lldb/source/Plugins/Language/Swift/SwiftHashedContainer.cpp

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,9 @@ HashedCollectionConfig::RegisterSummaryProviders(
200200
AddCXXSummary(swift_category_sp, summaryProvider,
201201
m_summaryProviderName.AsCString(),
202202
m_emptyStorage_demangled, flags, false);
203+
AddCXXSummary(swift_category_sp, summaryProvider,
204+
m_summaryProviderName.AsCString(),
205+
m_nativeStorageRoot_demangled, flags, false);
203206
AddCXXSummary(swift_category_sp, summaryProvider,
204207
m_summaryProviderName.AsCString(),
205208
m_deferredBridgedStorage_demangledRegex, flags, true);
@@ -231,6 +234,9 @@ HashedCollectionConfig::RegisterSyntheticChildrenCreators(
231234
AddCXXSynthetic(swift_category_sp, creator,
232235
m_syntheticChildrenName.AsCString(),
233236
m_nativeStorage_demangledRegex, flags, true);
237+
AddCXXSynthetic(swift_category_sp, creator,
238+
m_syntheticChildrenName.AsCString(),
239+
m_nativeStorageRoot_demangled, flags, false);
234240
AddCXXSynthetic(swift_category_sp, creator,
235241
m_syntheticChildrenName.AsCString(),
236242
m_emptyStorage_demangled, flags, false);
@@ -648,18 +654,22 @@ HashedCollectionConfig::CreateHandler(ValueObject &valobj) const {
648654
return CreateNativeHandler(valobj_sp, storage_sp);
649655
}
650656

651-
ValueObjectSP variant_sp =
652-
valobj_sp->GetChildMemberWithName(g__variant, true);
653-
if (!variant_sp)
654-
return nullptr;
657+
lldb::addr_t storage_location = LLDB_INVALID_ADDRESS;
658+
if (type_name_cs == m_nativeStorageRoot_demangled)
659+
storage_location = valobj_sp->GetPointerValue();
660+
else {
661+
ValueObjectSP variant_sp =
662+
valobj_sp->GetChildMemberWithName(g__variant, true);
663+
if (!variant_sp)
664+
return nullptr;
655665

656-
ValueObjectSP bobject_sp =
657-
variant_sp->GetChildAtNamePath({g_object, g_rawValue});
658-
if (!bobject_sp)
659-
return nullptr;
666+
ValueObjectSP bobject_sp =
667+
variant_sp->GetChildAtNamePath({g_object, g_rawValue});
668+
if (!bobject_sp)
669+
return nullptr;
660670

661-
lldb::addr_t storage_location =
662-
bobject_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
671+
storage_location = bobject_sp->GetValueAsUnsigned(LLDB_INVALID_ADDRESS);
672+
}
663673
if (storage_location == LLDB_INVALID_ADDRESS)
664674
return nullptr;
665675

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 55 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -2032,21 +2032,6 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Pack(
20322032
return true;
20332033
}
20342034

2035-
static bool IsPrivateNSClass(NodePointer node) {
2036-
if (!node || node->getKind() != Node::Kind::Type ||
2037-
node->getNumChildren() == 0)
2038-
return false;
2039-
NodePointer classNode = node->getFirstChild();
2040-
if (!classNode || classNode->getKind() != Node::Kind::Class ||
2041-
classNode->getNumChildren() < 2)
2042-
return false;
2043-
for (NodePointer child : *classNode)
2044-
if (child->getKind() == Node::Kind::Identifier && child->hasText())
2045-
return child->getText().starts_with("__NS") ||
2046-
child->getText().starts_with("NSTaggedPointer");
2047-
return false;
2048-
}
2049-
20502035
CompilerType SwiftLanguageRuntime::GetDynamicTypeAndAddress_EmbeddedClass(
20512036
uint64_t instance_ptr, CompilerType class_type) {
20522037
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
@@ -2101,44 +2086,41 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
21012086
return false;
21022087

21032088
auto tss = class_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
2089+
if (!tss) {
2090+
// This could be an Objective-C type implemented in Swift. Get the
2091+
// Swift typesystem.
2092+
if (auto module_sp = in_value.GetModule()) {
2093+
auto type_system_or_err =
2094+
module_sp->GetTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2095+
if (!type_system_or_err) {
2096+
llvm::consumeError(type_system_or_err.takeError());
2097+
return false;
2098+
}
2099+
auto ts_sp = *type_system_or_err;
2100+
tss =
2101+
llvm::cast<TypeSystemSwift>(ts_sp.get())->GetTypeSystemSwiftTypeRef();
2102+
} else if (auto target_sp = in_value.GetTargetSP()) {
2103+
auto type_system_or_err =
2104+
target_sp->GetScratchTypeSystemForLanguage(lldb::eLanguageTypeSwift);
2105+
if (!type_system_or_err) {
2106+
llvm::consumeError(type_system_or_err.takeError());
2107+
return false;
2108+
}
2109+
auto ts_sp = *type_system_or_err;
2110+
tss =
2111+
llvm::cast<TypeSystemSwift>(ts_sp.get())->GetTypeSystemSwiftTypeRef();
2112+
}
2113+
}
21042114
if (!tss)
21052115
return false;
2116+
21062117
address.SetRawAddress(instance_ptr);
21072118
auto ts = tss->GetTypeSystemSwiftTypeRef();
21082119
if (!ts)
21092120
return false;
2110-
// Ask the Objective-C runtime about Objective-C types.
2111-
if (tss->IsImportedType(class_type.GetOpaqueQualType(), nullptr))
2112-
if (auto *objc_runtime =
2113-
SwiftLanguageRuntime::GetObjCRuntime(GetProcess())) {
2114-
Value::ValueType value_type;
2115-
if (objc_runtime->GetDynamicTypeAndAddress(in_value, use_dynamic,
2116-
class_type_or_name, address,
2117-
value_type, local_buffer)) {
2118-
bool found = false;
2119-
// Return the most specific class which we can get the typeref.
2120-
ForEachSuperClassType(in_value, [&](SuperClassType sc) -> bool {
2121-
if (auto *tr = sc.get_typeref()) {
2122-
swift::Demangle::Demangler dem;
2123-
swift::Demangle::NodePointer node = tr->getDemangling(dem);
2124-
// Skip private Foundation types since it's unlikely that would be
2125-
// useful to users.
2126-
if (IsPrivateNSClass(node))
2127-
return false;
2128-
class_type_or_name.SetCompilerType(ts->RemangleAsType(
2129-
dem, node, swift::Mangle::ManglingFlavor::Default));
2130-
found = true;
2131-
return true;
2132-
}
2133-
return false;
2134-
});
2135-
return found;
2136-
}
2137-
return false;
2138-
}
2139-
Log *log(GetLog(LLDBLog::Types));
2140-
// Scope reflection_ctx to minimize its lock scope.
2141-
{
2121+
2122+
auto resolve_swift = [&]() {
2123+
// Scope reflection_ctx to minimize its lock scope.
21422124
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
21432125
if (!reflection_ctx)
21442126
return false;
@@ -2182,10 +2164,17 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_Class(
21822164
return false;
21832165
}
21842166
}
2185-
2186-
LLDB_LOG(log, "dynamic type of instance_ptr {0:x} is {1}", instance_ptr,
2187-
class_type.GetMangledTypeName());
21882167
class_type_or_name.SetCompilerType(dynamic_type);
2168+
LLDB_LOG(GetLog(LLDBLog::Types),
2169+
"dynamic type of instance_ptr {0:x} is {1}", instance_ptr,
2170+
class_type.GetMangledTypeName());
2171+
return true;
2172+
};
2173+
2174+
if (!resolve_swift()) {
2175+
// When returning false here, the next compatible runtime (=
2176+
// Objective-C) will get ask to resolve this type.
2177+
return false;
21892178
}
21902179

21912180
#ifndef NDEBUG
@@ -2843,12 +2832,12 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress_IndirectEnumCase(
28432832

28442833
return GetDynamicTypeAndAddress(*valobj_sp, use_dynamic, class_type_or_name,
28452834
address, value_type, local_buffer);
2846-
} else {
2847-
// This is most likely a statically known type.
2848-
address.SetLoadAddress(box_value, &GetProcess().GetTarget());
2849-
value_type = Value::GetValueTypeFromAddressType(eAddressTypeLoad);
2850-
return true;
28512835
}
2836+
2837+
// This is most likely a statically known type.
2838+
address.SetLoadAddress(box_value, &GetProcess().GetTarget());
2839+
value_type = Value::GetValueTypeFromAddressType(eAddressTypeLoad);
2840+
return true;
28522841
}
28532842

28542843
void SwiftLanguageRuntime::DumpTyperef(CompilerType type,
@@ -3165,22 +3154,27 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress(
31653154
return false;
31663155

31673156
LLDB_SCOPED_TIMER();
3157+
CompilerType val_type(in_value.GetCompilerType());
3158+
Value::ValueType static_value_type = Value::ValueType::Invalid;
31683159

31693160
// Try to import a Clang type into Swift.
3170-
if (in_value.GetObjectRuntimeLanguage() == eLanguageTypeObjC)
3171-
return GetDynamicTypeAndAddress_ClangType(in_value, use_dynamic,
3172-
class_type_or_name, address,
3173-
value_type, local_buffer);
3161+
if (in_value.GetObjectRuntimeLanguage() == eLanguageTypeObjC) {
3162+
if (GetDynamicTypeAndAddress_ClangType(in_value, use_dynamic,
3163+
class_type_or_name, address,
3164+
value_type, local_buffer))
3165+
return true;
3166+
return GetDynamicTypeAndAddress_Class(in_value, val_type, use_dynamic,
3167+
class_type_or_name, address,
3168+
static_value_type, local_buffer);
3169+
}
31743170

31753171
if (!CouldHaveDynamicValue(in_value))
31763172
return false;
31773173

3178-
CompilerType val_type(in_value.GetCompilerType());
31793174
Flags type_info(val_type.GetTypeInfo());
31803175
if (!type_info.AnySet(eTypeIsSwift))
31813176
return false;
31823177

3183-
Value::ValueType static_value_type = Value::ValueType::Invalid;
31843178
bool success = false;
31853179
bool is_indirect_enum_case = IsIndirectEnumCase(in_value);
31863180
// Type kinds with instance metadata don't need generic type resolution.

lldb/source/Plugins/TypeSystem/Swift/SwiftASTContext.cpp

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -996,7 +996,7 @@ SwiftASTContext::SwiftASTContext()
996996
}
997997
#endif
998998

999-
SwiftASTContext::SwiftASTContext(std::string description,
999+
SwiftASTContext::SwiftASTContext(std::string description, ModuleSP module_sp,
10001000
TypeSystemSwiftTypeRefSP typeref_typesystem)
10011001
: TypeSystemSwift(), m_typeref_typesystem(typeref_typesystem),
10021002
m_compiler_invocation_ap(new swift::CompilerInvocation()),
@@ -1006,6 +1006,7 @@ SwiftASTContext::SwiftASTContext(std::string description,
10061006
"Swift AST context instantiation is disabled!");
10071007

10081008
m_description = description;
1009+
m_module = module_sp.get();
10091010

10101011
// Set the clang modules cache path.
10111012
m_compiler_invocation_ap->setClangModuleCachePath(
@@ -2441,7 +2442,8 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
24412442
// If there is a target this may be a fallback scratch context.
24422443
std::shared_ptr<SwiftASTContext> swift_ast_sp(
24432444
static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
2444-
m_description, typeref_typesystem.GetTypeSystemSwiftTypeRef())));
2445+
m_description, module.shared_from_this(),
2446+
typeref_typesystem.GetTypeSystemSwiftTypeRef())));
24452447
bool suppress_config_log = false;
24462448
auto defer_log =
24472449
llvm::make_scope_exit([swift_ast_sp, &suppress_config_log] {
@@ -2454,7 +2456,6 @@ SwiftASTContext::CreateInstance(lldb::LanguageType language, Module &module,
24542456

24552457
// This is a module AST context, mark it as such.
24562458
swift_ast_sp->m_is_scratch_context = false;
2457-
swift_ast_sp->m_module = &module;
24582459
swift_ast_sp->GetLanguageOptions().EnableAccessControl = false;
24592460
swift_ast_sp->GetLanguageOptions().EnableCXXInterop =
24602461
module.IsSwiftCxxInteropEnabled();
@@ -2791,7 +2792,8 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
27912792
return {};
27922793
}
27932794
swift_ast_sp.reset(new SwiftASTContextForExpressions(
2794-
m_description, typeref_typesystem.GetTypeSystemSwiftTypeRef()));
2795+
m_description, module_sp,
2796+
typeref_typesystem.GetTypeSystemSwiftTypeRef()));
27952797
// This is a scratch AST context, mark it as such.
27962798
swift_ast_sp->m_is_scratch_context = true;
27972799
auto &lang_opts = swift_ast_sp->GetLanguageOptions();
@@ -2806,10 +2808,10 @@ SwiftASTContext::CreateInstance(const SymbolContext &sc,
28062808
}
28072809
swift_ast_sp.reset(
28082810
static_cast<SwiftASTContext *>(new SwiftASTContextForModule(
2809-
m_description, typeref_typesystem.GetTypeSystemSwiftTypeRef())));
2811+
m_description, module_sp,
2812+
typeref_typesystem.GetTypeSystemSwiftTypeRef())));
28102813
// This is a module AST context, mark it as such.
28112814
swift_ast_sp->m_is_scratch_context = false;
2812-
swift_ast_sp->m_module = module_sp.get();
28132815
auto &lang_opts = swift_ast_sp->GetLanguageOptions();
28142816
lang_opts.EnableAccessControl = false;
28152817
lang_opts.EnableCXXInterop = ShouldEnableCXXInterop(cu);
@@ -4705,14 +4707,10 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
47054707
// that look like they might be come from Objective-C (or C) as
47064708
// Clang types. LLDB's Objective-C part is very robust against
47074709
// malformed object pointers, so this isn't very risky.
4708-
auto ts = GetTypeSystemSwiftTypeRef();
4709-
if (!ts)
4710-
return {};
4711-
Module *module = ts->GetModule();
4712-
if (!module)
4710+
if (!m_module)
47134711
return {};
47144712
auto type_system_or_err =
4715-
module->GetTypeSystemForLanguage(eLanguageTypeObjC);
4713+
m_module->GetTypeSystemForLanguage(eLanguageTypeObjC);
47164714
if (!type_system_or_err) {
47174715
llvm::consumeError(type_system_or_err.takeError());
47184716
return {};
@@ -4724,11 +4722,16 @@ CompilerType SwiftASTContext::GetAsClangType(ConstString mangled_name) {
47244722
return {};
47254723
DWARFASTParserClang *clang_ast_parser =
47264724
static_cast<DWARFASTParserClang *>(clang_ctx->GetDWARFParser());
4725+
4726+
SymbolContext sc;
4727+
m_module->CalculateSymbolContext(&sc);
47274728
CompilerType clang_type;
47284729
CompilerType imported_type = GetCompilerType(mangled_name);
4729-
if (auto ts =
4730-
imported_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>())
4731-
ts->IsImportedType(imported_type.GetOpaqueQualType(), &clang_type);
4730+
if (auto ts = imported_type.GetTypeSystem()
4731+
.dyn_cast_or_null<TypeSystemSwiftTypeRef>())
4732+
if (ts->IsImportedType(imported_type.GetOpaqueQualType(), nullptr))
4733+
if (TypeSP result = ts->LookupClangType(mangled_name, sc))
4734+
clang_type = result->GetForwardCompilerType();
47324735

47334736
// Import the Clang type into the Clang context.
47344737
if (!clang_type)
@@ -8940,8 +8943,9 @@ SwiftASTContext::GetASTVectorForModule(const Module *module) {
89408943
}
89418944

89428945
SwiftASTContextForExpressions::SwiftASTContextForExpressions(
8943-
std::string description, TypeSystemSwiftTypeRefSP typeref_typesystem)
8944-
: SwiftASTContext(std::move(description), typeref_typesystem) {
8946+
std::string description, ModuleSP module_sp,
8947+
TypeSystemSwiftTypeRefSP typeref_typesystem)
8948+
: SwiftASTContext(std::move(description), module_sp, typeref_typesystem) {
89458949
assert(llvm::isa<TypeSystemSwiftTypeRefForExpressions>(
89468950
m_typeref_typesystem.lock().get()));
89478951
}

0 commit comments

Comments
 (0)