Skip to content

[lldb] Make variadic generic types work with TypeSystemSwiftTypeRef #10675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2025
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,9 @@ static CompilerType GetSwiftTypeForVariableValueObject(
if (!result)
return {};
if (SwiftASTManipulator::ShouldBindGenericTypes(bind_generic_types))
result = runtime->BindGenericTypeParameters(*stack_frame_sp, result);
result = llvm::expectedToOptional(
runtime->BindGenericTypeParameters(*stack_frame_sp, result))
.value_or(CompilerType());
if (!result)
return {};
if (!result.GetTypeSystem()->SupportsLanguage(lldb::eLanguageTypeSwift))
Expand Down Expand Up @@ -606,12 +608,17 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,

auto *stack_frame = stack_frame_sp.get();
if (SwiftASTManipulator::ShouldBindGenericTypes(bind_generic_types)) {
imported_self_type = swift_runtime->BindGenericTypeParameters(
auto bound_type_or_err = swift_runtime->BindGenericTypeParameters(
*stack_frame, imported_self_type);
if (!imported_self_type)
return llvm::createStringError(
"Unable to add the aliases the expression needs because the Swift "
"expression parser couldn't bind the type parameters for self.");
if (!bound_type_or_err)
return llvm::joinErrors(
llvm::createStringError(
"Unable to add the aliases the expression needs because the "
"Swift expression parser couldn't bind the type parameters for "
"self."),
bound_type_or_err.takeError());

imported_self_type = *bound_type_or_err;
}

{
Expand Down Expand Up @@ -1224,16 +1231,17 @@ AddArchetypeTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
auto flavor = SwiftLanguageRuntime::GetManglingFlavor(type_name);
auto dependent_type = typeref_typesystem->CreateGenericTypeParamType(
info.depth, info.index, flavor);
auto bound_type =
auto bound_type_or_err =
runtime->BindGenericTypeParameters(stack_frame, dependent_type);
if (!bound_type) {
LLDB_LOG(
log,
if (!bound_type_or_err) {
LLDB_LOG_ERROR(
log, bound_type_or_err.takeError(),
"[AddArchetypeTypeAliases] Could not bind dependent generic param "
"type {0}",
"type {1}: {0}",
dependent_type.GetMangledTypeName());
continue;
}
auto bound_type = *bound_type_or_err;

LLDB_LOG(log,
"[AddArchetypeTypeAliases] Binding dependent generic param "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -868,8 +868,14 @@ std::string SwiftLanguageRuntime::GetObjectDescriptionExpr_Copy(

auto swift_ast_ctx =
static_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
if (swift_ast_ctx)
static_type = BindGenericTypeParameters(*frame_sp, static_type);
if (swift_ast_ctx) {
auto bound_type_or_err = BindGenericTypeParameters(*frame_sp, static_type);
if (!bound_type_or_err) {
LLDB_LOG_ERROR(log, bound_type_or_err.takeError(), "{0}");
return {};
}
static_type = *bound_type_or_err;
}

auto stride = 0;
auto opt_stride = static_type.GetByteStride(frame_sp.get());
Expand Down
34 changes: 23 additions & 11 deletions lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.h
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,9 @@ class SwiftLanguageRuntime : public LanguageRuntime {
Address &address, Value::ValueType &value_type,
llvm::ArrayRef<uint8_t> &local_buffer) override;

llvm::Expected<CompilerType> BindGenericPackType(StackFrame &frame,
CompilerType pack_type,
bool *indirect = nullptr);
CompilerType BindGenericTypeParameters(
CompilerType unbound_type,
std::function<CompilerType(unsigned, unsigned)> finder);
Expand Down Expand Up @@ -343,12 +346,20 @@ class SwiftLanguageRuntime : public LanguageRuntime {
unsigned dependent_generic_param_count = 0;
unsigned num_counts = 0;

unsigned GetNumValuePacks() { return count_for_value_pack.size(); }
unsigned GetNumTypePacks() { return count_for_type_pack.size(); }
unsigned GetCountForValuePack(unsigned i) {
unsigned GetNumValuePacks() const { return count_for_value_pack.size(); }
unsigned GetNumTypePacks() const { return count_for_type_pack.size(); }
unsigned GetCountForValuePack(unsigned i) const {
return count_for_value_pack[i];
}
unsigned GetCountForTypePack(unsigned i) { return count_for_type_pack[i]; }
unsigned GetCountForTypePack(unsigned i) const { return count_for_type_pack[i]; }
bool HasPacks() const { return pack_expansions.size(); }
bool IsPack(unsigned depth, unsigned index) const {
if (HasPacks())
for (auto param : generic_params)
if (param.depth == depth && param.index == index)
return param.is_pack;
return false;
}
};
/// Extract the generic signature out of a mangled Swift function name.
static std::optional<GenericSignature>
Expand All @@ -359,8 +370,8 @@ class SwiftLanguageRuntime : public LanguageRuntime {
/// version of \p base_type that replaces all generic type
/// parameters with bound generic types. If a generic type parameter
/// cannot be resolved, the input type is returned.
CompilerType BindGenericTypeParameters(StackFrame &stack_frame,
CompilerType base_type);
llvm::Expected<CompilerType>
BindGenericTypeParameters(StackFrame &stack_frame, CompilerType base_type);

bool IsStoredInlineInBuffer(CompilerType type) override;

Expand Down Expand Up @@ -580,13 +591,14 @@ class SwiftLanguageRuntime : public LanguageRuntime {
GetRemoteASTContext(SwiftASTContext &swift_ast_ctx);

/// Like \p BindGenericTypeParameters but for TypeSystemSwiftTypeRef.
CompilerType BindGenericTypeParameters(StackFrame &stack_frame,
TypeSystemSwiftTypeRef &ts,
ConstString mangled_name);
llvm::Expected<CompilerType>
BindGenericTypeParameters(StackFrame &stack_frame, TypeSystemSwiftTypeRef &ts,
ConstString mangled_name);

/// Like \p BindGenericTypeParameters but for RemoteAST.
CompilerType BindGenericTypeParametersRemoteAST(StackFrame &stack_frame,
CompilerType base_type);
llvm::Expected<CompilerType>
BindGenericTypeParametersRemoteAST(StackFrame &stack_frame,
CompilerType base_type);

bool GetDynamicTypeAndAddress_Pack(ValueObject &in_value,
CompilerType pack_type,
Expand Down
Loading