Skip to content

Commit 9022196

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 (cherry picked from commit 50f15f2)
1 parent 863d6c9 commit 9022196

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
@@ -1777,6 +1777,9 @@ CreatePackType(swift::Demangle::Demangler &dem, TypeSystemSwiftTypeRef &ts,
17771777
llvm::Expected<CompilerType>
17781778
SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
17791779
CompilerType pack_type, bool *is_indirect) {
1780+
// This mode is used only by GetDynamicTypeAndAddress_Pack(). It would be
1781+
// cleaner if we could get rid of it.
1782+
bool rewrite_indirect_packs = (is_indirect != nullptr);
17801783
swift::Demangle::Demangler dem;
17811784
Target &target = GetProcess().GetTarget();
17821785
size_t ptr_size = GetProcess().GetAddressByteSize();
@@ -1798,9 +1801,10 @@ SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
17981801
"cannot decode pack_expansion type: failed to decode generic signature "
17991802
"from function name");
18001803

1801-
auto expand_pack_type = [&](ConstString mangled_pack_type, bool indirect,
1804+
auto expand_pack_type = [&](ConstString mangled_pack_type,
1805+
bool rewrite_indirect,
18021806
swift::Mangle::ManglingFlavor flavor)
1803-
-> llvm::Expected<swift::Demangle::NodePointer> {
1807+
-> llvm::Expected<swift::Demangle::NodePointer> {
18041808
// Find pack_type in the pack_expansions.
18051809
unsigned i = 0;
18061810
SwiftLanguageRuntime::GenericSignature::PackExpansion *pack_expansion =
@@ -1933,30 +1937,31 @@ SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
19331937
// Add the substituted type to the tuple.
19341938
elements.push_back({{}, type});
19351939
}
1936-
if (indirect) {
1940+
1941+
// TODO: Could we get rid of this code path?
1942+
if (rewrite_indirect) {
19371943
// Create a tuple type with all the concrete types in the pack.
19381944
CompilerType tuple = ts->CreateTupleType(elements);
19391945
// TODO: Remove unnecessary mangling roundtrip.
19401946
// Wrap the type inside a SILPackType to mark it for GetChildAtIndex.
1941-
CompilerType sil_pack_type = ts->CreateSILPackType(tuple, indirect);
1947+
CompilerType sil_pack_type = ts->CreateSILPackType(tuple, rewrite_indirect);
19421948
swift::Demangle::NodePointer global =
19431949
dem.demangleSymbol(sil_pack_type.GetMangledTypeName().GetStringRef());
19441950
using Kind = Node::Kind;
19451951
auto *dem_sil_pack_type =
19461952
swift_demangle::ChildAtPath(global, {Kind::TypeMangling, Kind::Type});
19471953
return dem_sil_pack_type;
1948-
} else {
1949-
return CreatePackType(dem, *ts, elements);
19501954
}
1955+
return CreatePackType(dem, *ts, elements);
19511956
};
19521957

19531958
swift::Demangle::Context dem_ctx;
19541959
auto node = dem_ctx.demangleSymbolAsNode(
19551960
pack_type.GetMangledTypeName().GetStringRef());
19561961

1962+
bool indirect = false;
19571963
auto flavor =
19581964
SwiftLanguageRuntime::GetManglingFlavor(pack_type.GetMangledTypeName());
1959-
bool indirect = false;
19601965

19611966
// Expand all the pack types that appear in the incoming type,
19621967
// either at the root level or as arguments of bound generic types.
@@ -1978,13 +1983,16 @@ SwiftLanguageRuntime::BindGenericPackType(StackFrame &frame,
19781983
ConstString mangled_pack_type = pack_type.GetMangledTypeName();
19791984
LLDB_LOG(GetLog(LLDBLog::Types), "decoded pack_expansion type: {0}",
19801985
mangled_pack_type);
1981-
return expand_pack_type(mangled_pack_type, indirect, flavor);
1986+
return expand_pack_type(mangled_pack_type,
1987+
rewrite_indirect_packs && indirect, flavor);
19821988
});
19831989

19841990
if (!transformed)
19851991
return transformed.takeError();
1992+
19861993
if (is_indirect)
19871994
*is_indirect = indirect;
1995+
19881996
return ts->RemangleAsType(dem, *transformed, flavor);
19891997
}
19901998

@@ -2636,14 +2644,19 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26362644
LLDB_SCOPED_TIMER();
26372645
using namespace swift::Demangle;
26382646

2639-
Status error;
26402647
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
26412648
if (!reflection_ctx) {
26422649
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types),
26432650
"No reflection context available.");
26442651
return ts.GetTypeFromMangledTypename(mangled_name);
26452652
}
26462653

2654+
2655+
ConstString func_name = stack_frame.GetSymbolContext(eSymbolContextFunction)
2656+
.GetFunctionName(Mangled::ePreferMangled);
2657+
// Extract the generic signature from the function symbol.
2658+
auto generic_signature =
2659+
SwiftLanguageRuntime::GetGenericSignature(func_name.GetStringRef(), ts);
26472660
Demangler dem;
26482661

26492662
NodePointer canonical = TypeSystemSwiftTypeRef::GetStaticSelfType(
@@ -2655,6 +2668,9 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26552668
ForEachGenericParameter(canonical, [&](unsigned depth, unsigned index) {
26562669
if (substitutions.count({depth, index}))
26572670
return;
2671+
// Packs will be substituted in a second pass.
2672+
if (generic_signature && generic_signature->IsPack(depth, index))
2673+
return;
26582674
StreamString mdvar_name;
26592675
mdvar_name.Printf(u8"$\u03C4_%d_%d", depth, index);
26602676

@@ -2684,7 +2700,8 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26842700
return CompilerType();
26852701
return ts.GetTypeFromMangledTypename(ConstString(mangling.result()));
26862702
};
2687-
if (substitutions.empty())
2703+
if (substitutions.empty() &&
2704+
!(generic_signature && generic_signature->HasPacks()))
26882705
return get_canonical();
26892706

26902707
// Build a TypeRef from the demangle tree.
@@ -2727,6 +2744,17 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27272744
CompilerType bound_type = scratch_ctx->RemangleAsType(dem, node, flavor);
27282745
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types), "Bound {0} -> {1}.",
27292746
mangled_name, bound_type.GetMangledTypeName());
2747+
2748+
if (generic_signature && generic_signature->HasPacks()) {
2749+
auto bound_type_or_err = BindGenericPackType(stack_frame, bound_type);
2750+
if (!bound_type_or_err) {
2751+
LLDB_LOG_ERROR(GetLog(LLDBLog::Expressions | LLDBLog::Types),
2752+
bound_type_or_err.takeError(), "{0}");
2753+
return bound_type;
2754+
}
2755+
bound_type = *bound_type_or_err;
2756+
}
2757+
27302758
return bound_type;
27312759
}
27322760

@@ -3458,7 +3486,7 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
34583486
// GetCanonicalType() returned an Expected.
34593487
return llvm::createStringError(
34603488
"could not get canonical type (possibly due to unresolved typealias)");
3461-
}
3489+
}
34623490

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

0 commit comments

Comments
 (0)