Skip to content

Commit 50f15f2

Browse files
committed
[lldb] Make variadic generic types work with TypeSystemSwiftTypeRef
The missing ingredient were the reflection support for pack types, and binding pack type in BindGenericParameters(). rdar://145257088
1 parent d7572b3 commit 50f15f2

File tree

2 files changed

+51
-15
lines changed

2 files changed

+51
-15
lines changed

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -346,12 +346,20 @@ class SwiftLanguageRuntime : public LanguageRuntime {
346346
unsigned dependent_generic_param_count = 0;
347347
unsigned num_counts = 0;
348348

349-
unsigned GetNumValuePacks() { return count_for_value_pack.size(); }
350-
unsigned GetNumTypePacks() { return count_for_type_pack.size(); }
351-
unsigned GetCountForValuePack(unsigned i) {
349+
unsigned GetNumValuePacks() const { return count_for_value_pack.size(); }
350+
unsigned GetNumTypePacks() const { return count_for_type_pack.size(); }
351+
unsigned GetCountForValuePack(unsigned i) const {
352352
return count_for_value_pack[i];
353353
}
354-
unsigned GetCountForTypePack(unsigned i) { return count_for_type_pack[i]; }
354+
unsigned GetCountForTypePack(unsigned i) const { return count_for_type_pack[i]; }
355+
bool HasPacks() const { return pack_expansions.size(); }
356+
bool IsPack(unsigned depth, unsigned index) const {
357+
if (HasPacks())
358+
for (auto param : generic_params)
359+
if (param.depth == depth && param.index == index)
360+
return param.is_pack;
361+
return false;
362+
}
355363
};
356364
/// Extract the generic signature out of a mangled Swift function name.
357365
static std::optional<GenericSignature>

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

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,9 @@ CreatePackType(swift::Demangle::Demangler &dem, TypeSystemSwiftTypeRef &ts,
17671767
llvm::Expected<CompilerType>
17681768
SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
17691769
CompilerType pack_type, bool *is_indirect) {
1770+
// This mode is used only by GetDynamicTypeAndAddress_Pack(). It would be
1771+
// cleaner if we could get rid of it.
1772+
bool rewrite_indirect_packs = (is_indirect != nullptr);
17701773
swift::Demangle::Demangler dem;
17711774
Target &target = GetProcess().GetTarget();
17721775
size_t ptr_size = GetProcess().GetAddressByteSize();
@@ -1788,9 +1791,10 @@ SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
17881791
"cannot decode pack_expansion type: failed to decode generic signature "
17891792
"from function name");
17901793

1791-
auto expand_pack_type = [&](ConstString mangled_pack_type, bool indirect,
1794+
auto expand_pack_type = [&](ConstString mangled_pack_type,
1795+
bool rewrite_indirect,
17921796
swift::Mangle::ManglingFlavor flavor)
1793-
-> llvm::Expected<swift::Demangle::NodePointer> {
1797+
-> llvm::Expected<swift::Demangle::NodePointer> {
17941798
// Find pack_type in the pack_expansions.
17951799
unsigned i = 0;
17961800
SwiftLanguageRuntime::GenericSignature::PackExpansion *pack_expansion =
@@ -1923,30 +1927,31 @@ SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
19231927
// Add the substituted type to the tuple.
19241928
elements.push_back({{}, type});
19251929
}
1926-
if (indirect) {
1930+
1931+
// TODO: Could we get rid of this code path?
1932+
if (rewrite_indirect) {
19271933
// Create a tuple type with all the concrete types in the pack.
19281934
CompilerType tuple = ts->CreateTupleType(elements);
19291935
// TODO: Remove unnecessary mangling roundtrip.
19301936
// Wrap the type inside a SILPackType to mark it for GetChildAtIndex.
1931-
CompilerType sil_pack_type = ts->CreateSILPackType(tuple, indirect);
1937+
CompilerType sil_pack_type = ts->CreateSILPackType(tuple, rewrite_indirect);
19321938
swift::Demangle::NodePointer global =
19331939
dem.demangleSymbol(sil_pack_type.GetMangledTypeName().GetStringRef());
19341940
using Kind = Node::Kind;
19351941
auto *dem_sil_pack_type =
19361942
swift_demangle::ChildAtPath(global, {Kind::TypeMangling, Kind::Type});
19371943
return dem_sil_pack_type;
1938-
} else {
1939-
return CreatePackType(dem, *ts, elements);
19401944
}
1945+
return CreatePackType(dem, *ts, elements);
19411946
};
19421947

19431948
swift::Demangle::Context dem_ctx;
19441949
auto node = dem_ctx.demangleSymbolAsNode(
19451950
pack_type.GetMangledTypeName().GetStringRef());
19461951

1952+
bool indirect = false;
19471953
auto flavor =
19481954
SwiftLanguageRuntime::GetManglingFlavor(pack_type.GetMangledTypeName());
1949-
bool indirect = false;
19501955

19511956
// Expand all the pack types that appear in the incoming type,
19521957
// either at the root level or as arguments of bound generic types.
@@ -1968,13 +1973,16 @@ SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
19681973
ConstString mangled_pack_type = pack_type.GetMangledTypeName();
19691974
LLDB_LOG(GetLog(LLDBLog::Types), "decoded pack_expansion type: {0}",
19701975
mangled_pack_type);
1971-
return expand_pack_type(mangled_pack_type, indirect, flavor);
1976+
return expand_pack_type(mangled_pack_type,
1977+
rewrite_indirect_packs && indirect, flavor);
19721978
});
19731979

19741980
if (!transformed)
19751981
return transformed.takeError();
1982+
19761983
if (is_indirect)
19771984
*is_indirect = indirect;
1985+
19781986
return ts->RemangleAsType(dem, *transformed, flavor);
19791987
}
19801988

@@ -2626,14 +2634,19 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26262634
LLDB_SCOPED_TIMER();
26272635
using namespace swift::Demangle;
26282636

2629-
Status error;
26302637
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
26312638
if (!reflection_ctx) {
26322639
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types),
26332640
"No reflection context available.");
26342641
return ts.GetTypeFromMangledTypename(mangled_name);
26352642
}
26362643

2644+
2645+
ConstString func_name = stack_frame.GetSymbolContext(eSymbolContextFunction)
2646+
.GetFunctionName(Mangled::ePreferMangled);
2647+
// Extract the generic signature from the function symbol.
2648+
auto generic_signature =
2649+
SwiftLanguageRuntime::GetGenericSignature(func_name.GetStringRef(), ts);
26372650
Demangler dem;
26382651

26392652
NodePointer canonical = TypeSystemSwiftTypeRef::GetStaticSelfType(
@@ -2645,6 +2658,9 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26452658
ForEachGenericParameter(canonical, [&](unsigned depth, unsigned index) {
26462659
if (substitutions.count({depth, index}))
26472660
return;
2661+
// Packs will be substituted in a second pass.
2662+
if (generic_signature && generic_signature->IsPack(depth, index))
2663+
return;
26482664
StreamString mdvar_name;
26492665
mdvar_name.Printf(u8"$\u03C4_%d_%d", depth, index);
26502666

@@ -2674,7 +2690,8 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26742690
return CompilerType();
26752691
return ts.GetTypeFromMangledTypename(ConstString(mangling.result()));
26762692
};
2677-
if (substitutions.empty())
2693+
if (substitutions.empty() &&
2694+
!(generic_signature && generic_signature->HasPacks()))
26782695
return get_canonical();
26792696

26802697
// Build a TypeRef from the demangle tree.
@@ -2717,6 +2734,17 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27172734
CompilerType bound_type = scratch_ctx->RemangleAsType(dem, node, flavor);
27182735
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types), "Bound {0} -> {1}.",
27192736
mangled_name, bound_type.GetMangledTypeName());
2737+
2738+
if (generic_signature && generic_signature->HasPacks()) {
2739+
auto bound_type_or_err = BindGenericPackType(stack_frame, bound_type);
2740+
if (!bound_type_or_err) {
2741+
LLDB_LOG_ERROR(GetLog(LLDBLog::Expressions | LLDBLog::Types),
2742+
bound_type_or_err.takeError(), "{0}");
2743+
return bound_type;
2744+
}
2745+
bound_type = *bound_type_or_err;
2746+
}
2747+
27202748
return bound_type;
27212749
}
27222750

@@ -3449,7 +3477,7 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
34493477
// GetCanonicalType() returned an Expected.
34503478
return llvm::createStringError(
34513479
"could not get canonical type (possibly due to unresolved typealias)");
3452-
}
3480+
}
34533481

34543482
// Resolve all generic type parameters in the type for the current
34553483
// frame. Generic parameter binding has to happen in the scratch

0 commit comments

Comments
 (0)