Skip to content

Commit 8ce32ab

Browse files
committed
[lldb] Convert BindGenericTypeParameters to llvm::Expected
(cherry picked from commit e48e8aa)
1 parent 9022196 commit 8ce32ab

File tree

7 files changed

+77
-60
lines changed

7 files changed

+77
-60
lines changed

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

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,9 @@ static CompilerType GetSwiftTypeForVariableValueObject(
470470
if (!result)
471471
return {};
472472
if (SwiftASTManipulator::ShouldBindGenericTypes(bind_generic_types))
473-
result = runtime->BindGenericTypeParameters(*stack_frame_sp, result);
473+
result = llvm::expectedToOptional(
474+
runtime->BindGenericTypeParameters(*stack_frame_sp, result))
475+
.value_or(CompilerType());
474476
if (!result)
475477
return {};
476478
if (!result.GetTypeSystem()->SupportsLanguage(lldb::eLanguageTypeSwift))
@@ -606,12 +608,17 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
606608

607609
auto *stack_frame = stack_frame_sp.get();
608610
if (SwiftASTManipulator::ShouldBindGenericTypes(bind_generic_types)) {
609-
imported_self_type = swift_runtime->BindGenericTypeParameters(
611+
auto bound_type_or_err = swift_runtime->BindGenericTypeParameters(
610612
*stack_frame, imported_self_type);
611-
if (!imported_self_type)
612-
return llvm::createStringError(
613-
"Unable to add the aliases the expression needs because the Swift "
614-
"expression parser couldn't bind the type parameters for self.");
613+
if (!bound_type_or_err)
614+
return llvm::joinErrors(
615+
llvm::createStringError(
616+
"Unable to add the aliases the expression needs because the "
617+
"Swift expression parser couldn't bind the type parameters for "
618+
"self."),
619+
bound_type_or_err.takeError());
620+
621+
imported_self_type = *bound_type_or_err;
615622
}
616623

617624
{
@@ -1224,16 +1231,17 @@ AddArchetypeTypeAliases(std::unique_ptr<SwiftASTManipulator> &code_manipulator,
12241231
auto flavor = SwiftLanguageRuntime::GetManglingFlavor(type_name);
12251232
auto dependent_type = typeref_typesystem->CreateGenericTypeParamType(
12261233
info.depth, info.index, flavor);
1227-
auto bound_type =
1234+
auto bound_type_or_err =
12281235
runtime->BindGenericTypeParameters(stack_frame, dependent_type);
1229-
if (!bound_type) {
1230-
LLDB_LOG(
1231-
log,
1236+
if (!bound_type_or_err) {
1237+
LLDB_LOG_ERROR(
1238+
log, bound_type_or_err.takeError(),
12321239
"[AddArchetypeTypeAliases] Could not bind dependent generic param "
1233-
"type {0}",
1240+
"type {1}: {0}",
12341241
dependent_type.GetMangledTypeName());
12351242
continue;
12361243
}
1244+
auto bound_type = *bound_type_or_err;
12371245

12381246
LLDB_LOG(log,
12391247
"[AddArchetypeTypeAliases] Binding dependent generic param "

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -868,8 +868,14 @@ std::string SwiftLanguageRuntime::GetObjectDescriptionExpr_Copy(
868868

869869
auto swift_ast_ctx =
870870
static_type.GetTypeSystem().dyn_cast_or_null<TypeSystemSwift>();
871-
if (swift_ast_ctx)
872-
static_type = BindGenericTypeParameters(*frame_sp, static_type);
871+
if (swift_ast_ctx) {
872+
auto bound_type_or_err = BindGenericTypeParameters(*frame_sp, static_type);
873+
if (!bound_type_or_err) {
874+
LLDB_LOG_ERROR(log, bound_type_or_err.takeError(), "{0}");
875+
return {};
876+
}
877+
static_type = *bound_type_or_err;
878+
}
873879

874880
auto stride = 0;
875881
auto opt_stride = static_type.GetByteStride(frame_sp.get());

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

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -370,8 +370,8 @@ class SwiftLanguageRuntime : public LanguageRuntime {
370370
/// version of \p base_type that replaces all generic type
371371
/// parameters with bound generic types. If a generic type parameter
372372
/// cannot be resolved, the input type is returned.
373-
CompilerType BindGenericTypeParameters(StackFrame &stack_frame,
374-
CompilerType base_type);
373+
llvm::Expected<CompilerType>
374+
BindGenericTypeParameters(StackFrame &stack_frame, CompilerType base_type);
375375

376376
bool IsStoredInlineInBuffer(CompilerType type) override;
377377

@@ -591,13 +591,14 @@ class SwiftLanguageRuntime : public LanguageRuntime {
591591
GetRemoteASTContext(SwiftASTContext &swift_ast_ctx);
592592

593593
/// Like \p BindGenericTypeParameters but for TypeSystemSwiftTypeRef.
594-
CompilerType BindGenericTypeParameters(StackFrame &stack_frame,
595-
TypeSystemSwiftTypeRef &ts,
596-
ConstString mangled_name);
594+
llvm::Expected<CompilerType>
595+
BindGenericTypeParameters(StackFrame &stack_frame, TypeSystemSwiftTypeRef &ts,
596+
ConstString mangled_name);
597597

598598
/// Like \p BindGenericTypeParameters but for RemoteAST.
599-
CompilerType BindGenericTypeParametersRemoteAST(StackFrame &stack_frame,
600-
CompilerType base_type);
599+
llvm::Expected<CompilerType>
600+
BindGenericTypeParametersRemoteAST(StackFrame &stack_frame,
601+
CompilerType base_type);
601602

602603
bool GetDynamicTypeAndAddress_Pack(ValueObject &in_value,
603604
CompilerType pack_type,

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

Lines changed: 26 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -498,8 +498,10 @@ SwiftLanguageRuntime::GetMemberVariableOffsetRemoteMirrors(
498498
auto frame = instance ? instance->GetExecutionContextRef().GetFrameSP().get()
499499
: nullptr;
500500
auto ti_or_err = GetSwiftRuntimeTypeInfo(instance_type, frame);
501-
if (!ti_or_err)
501+
if (!ti_or_err) {
502502
LLDB_LOG_ERRORV(GetLog(LLDBLog::Types), ti_or_err.takeError(), "{0}");
503+
return {};
504+
}
503505
auto *ti = &*ti_or_err;
504506
if (auto *rti =
505507
llvm::dyn_cast_or_null<swift::reflection::RecordTypeInfo>(ti)) {
@@ -2637,20 +2639,16 @@ CompilerType SwiftLanguageRuntime::BindGenericTypeParameters(
26372639
unbound_type.GetMangledTypeName()));
26382640
}
26392641

2640-
CompilerType
2642+
llvm::Expected<CompilerType>
26412643
SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26422644
TypeSystemSwiftTypeRef &ts,
26432645
ConstString mangled_name) {
26442646
LLDB_SCOPED_TIMER();
26452647
using namespace swift::Demangle;
26462648

26472649
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
2648-
if (!reflection_ctx) {
2649-
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types),
2650-
"No reflection context available.");
2651-
return ts.GetTypeFromMangledTypename(mangled_name);
2652-
}
2653-
2650+
if (!reflection_ctx)
2651+
return llvm::createStringError("no reflection context");
26542652

26552653
ConstString func_name = stack_frame.GetSymbolContext(eSymbolContextFunction)
26562654
.GetFunctionName(Mangled::ePreferMangled);
@@ -2707,24 +2705,17 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27072705
// Build a TypeRef from the demangle tree.
27082706
auto type_ref_or_err =
27092707
reflection_ctx->GetTypeRef(dem, canonical, ts.GetDescriptorFinder());
2710-
if (!type_ref_or_err) {
2711-
LLDB_LOG_ERROR(
2712-
GetLog(LLDBLog::Expressions | LLDBLog::Types),
2713-
type_ref_or_err.takeError(),
2714-
"Couldn't get type ref when binding generic type parameters: {0}");
2715-
return get_canonical();
2716-
}
2708+
if (!type_ref_or_err)
2709+
return llvm::joinErrors(
2710+
llvm::createStringError("cannot bind generic parameters"),
2711+
type_ref_or_err.takeError());
27172712

27182713
// Apply the substitutions.
27192714
auto bound_type_ref_or_err = reflection_ctx->ApplySubstitutions(
27202715
*type_ref_or_err, substitutions, ts.GetDescriptorFinder());
2721-
if (!bound_type_ref_or_err) {
2722-
LLDB_LOG_ERROR(
2723-
GetLog(LLDBLog::Expressions | LLDBLog::Types),
2724-
bound_type_ref_or_err.takeError(),
2725-
"Couldn't get type ref when binding generic type parameters: {0}");
2726-
return get_canonical();
2727-
}
2716+
if (!bound_type_ref_or_err)
2717+
return bound_type_ref_or_err.takeError();
2718+
27282719
NodePointer node = bound_type_ref_or_err->getDemangling(dem);
27292720

27302721
// Import the type into the scratch context. Subsequent conversions
@@ -2736,29 +2727,24 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27362727
// the original context as to resolve type aliases correctly.
27372728
auto &target = GetProcess().GetTarget();
27382729
auto scratch_ctx = TypeSystemSwiftTypeRefForExpressions::GetForTarget(target);
2739-
if (!scratch_ctx) {
2740-
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types),
2741-
"No scratch context available.");
2742-
return ts.GetTypeFromMangledTypename(mangled_name);
2743-
}
2730+
if (!scratch_ctx)
2731+
return llvm::createStringError("No scratch context available.");
2732+
27442733
CompilerType bound_type = scratch_ctx->RemangleAsType(dem, node, flavor);
27452734
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types), "Bound {0} -> {1}.",
27462735
mangled_name, bound_type.GetMangledTypeName());
27472736

27482737
if (generic_signature && generic_signature->HasPacks()) {
27492738
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-
}
2739+
if (!bound_type_or_err)
2740+
return bound_type_or_err.takeError();
27552741
bound_type = *bound_type_or_err;
27562742
}
27572743

27582744
return bound_type;
27592745
}
27602746

2761-
CompilerType
2747+
llvm::Expected<CompilerType>
27622748
SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27632749
CompilerType base_type) {
27642750
// If this is a TypeRef type, bind that.
@@ -3241,7 +3227,9 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress(
32413227
if (!frame)
32423228
return false;
32433229

3244-
bound_type = BindGenericTypeParameters(*frame.get(), val_type);
3230+
bound_type = llvm::expectedToOptional(
3231+
BindGenericTypeParameters(*frame.get(), val_type))
3232+
.value_or(CompilerType());
32453233
if (!bound_type)
32463234
return false;
32473235
} else {
@@ -3495,7 +3483,10 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
34953483
if (StackFrame *frame = exe_scope->CalculateStackFrame().get()) {
34963484
ExecutionContext exe_ctx;
34973485
frame->CalculateExecutionContext(exe_ctx);
3498-
type = BindGenericTypeParameters(*frame, type);
3486+
auto bound_type_or_err = BindGenericTypeParameters(*frame, type);
3487+
if (!bound_type_or_err)
3488+
return bound_type_or_err.takeError();
3489+
type = *bound_type_or_err;
34993490
}
35003491

35013492
// BindGenericTypeParameters imports the type into the scratch

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

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -694,9 +694,15 @@ void SwiftLanguageRuntime::GetGenericParameterNamesForFunction(
694694
break;
695695
CompilerType generic_type =
696696
ts->CreateGenericTypeParamType(depth, index, flavor);
697-
CompilerType bound_type =
697+
llvm::Expected<CompilerType> bound_type_or_err =
698698
runtime->BindGenericTypeParameters(*frame, generic_type);
699-
type_name = bound_type.GetDisplayTypeName();
699+
if (!bound_type_or_err) {
700+
LLDB_LOG_ERROR(GetLog(LLDBLog::Expressions | LLDBLog::Types),
701+
bound_type_or_err.takeError(), "{0}");
702+
break;
703+
}
704+
705+
type_name = bound_type_or_err->GetDisplayTypeName();
700706
break;
701707
}
702708

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,9 @@ std::optional<uint64_t> SwiftLanguageRuntime::GetMemberVariableOffsetRemoteAST(
132132
// Bind generic parameters if necessary.
133133
if (instance && swift_type->hasTypeParameter())
134134
if (auto *frame = instance->GetExecutionContextRef().GetFrameSP().get())
135-
if (auto bound = BindGenericTypeParameters(*frame, instance_type)) {
135+
if (auto bound = llvm::expectedToOptional(
136+
BindGenericTypeParameters(*frame, instance_type))
137+
.value_or(CompilerType())) {
136138
LLDB_LOGF(
137139
GetLog(LLDBLog::Types),
138140
"[MemberVariableOffsetResolver] resolved non-class type = %s",
@@ -265,7 +267,8 @@ SwiftLanguageRuntime::GetDynamicTypeAndAddress_ExistentialRemoteAST(
265267
}
266268
#endif
267269

268-
CompilerType SwiftLanguageRuntime::BindGenericTypeParametersRemoteAST(
270+
llvm::Expected<CompilerType>
271+
SwiftLanguageRuntime::BindGenericTypeParametersRemoteAST(
269272
StackFrame &stack_frame, CompilerType base_type) {
270273
LLDB_SCOPED_TIMER();
271274

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5863,7 +5863,9 @@ BindGenericTypeParameters(CompilerType type, ExecutionContextScope *exe_scope) {
58635863
return type;
58645864
ExecutionContext exe_ctx;
58655865
exe_scope->CalculateExecutionContext(exe_ctx);
5866-
if (auto bound = runtime->BindGenericTypeParameters(*frame, type))
5866+
if (auto bound = llvm::expectedToOptional(
5867+
runtime->BindGenericTypeParameters(*frame, type))
5868+
.value_or(CompilerType()))
58675869
return bound;
58685870
return type;
58695871
}

0 commit comments

Comments
 (0)