Skip to content

Commit e48e8aa

Browse files
committed
[lldb] Convert BindGenericTypeParameters to llvm::Expected
1 parent 50f15f2 commit e48e8aa

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)) {
@@ -2627,20 +2629,16 @@ CompilerType SwiftLanguageRuntime::BindGenericTypeParameters(
26272629
unbound_type.GetMangledTypeName()));
26282630
}
26292631

2630-
CompilerType
2632+
llvm::Expected<CompilerType>
26312633
SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26322634
TypeSystemSwiftTypeRef &ts,
26332635
ConstString mangled_name) {
26342636
LLDB_SCOPED_TIMER();
26352637
using namespace swift::Demangle;
26362638

26372639
ThreadSafeReflectionContext reflection_ctx = GetReflectionContext();
2638-
if (!reflection_ctx) {
2639-
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types),
2640-
"No reflection context available.");
2641-
return ts.GetTypeFromMangledTypename(mangled_name);
2642-
}
2643-
2640+
if (!reflection_ctx)
2641+
return llvm::createStringError("no reflection context");
26442642

26452643
ConstString func_name = stack_frame.GetSymbolContext(eSymbolContextFunction)
26462644
.GetFunctionName(Mangled::ePreferMangled);
@@ -2697,24 +2695,17 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
26972695
// Build a TypeRef from the demangle tree.
26982696
auto type_ref_or_err =
26992697
reflection_ctx->GetTypeRef(dem, canonical, ts.GetDescriptorFinder());
2700-
if (!type_ref_or_err) {
2701-
LLDB_LOG_ERROR(
2702-
GetLog(LLDBLog::Expressions | LLDBLog::Types),
2703-
type_ref_or_err.takeError(),
2704-
"Couldn't get type ref when binding generic type parameters: {0}");
2705-
return get_canonical();
2706-
}
2698+
if (!type_ref_or_err)
2699+
return llvm::joinErrors(
2700+
llvm::createStringError("cannot bind generic parameters"),
2701+
type_ref_or_err.takeError());
27072702

27082703
// Apply the substitutions.
27092704
auto bound_type_ref_or_err = reflection_ctx->ApplySubstitutions(
27102705
*type_ref_or_err, substitutions, ts.GetDescriptorFinder());
2711-
if (!bound_type_ref_or_err) {
2712-
LLDB_LOG_ERROR(
2713-
GetLog(LLDBLog::Expressions | LLDBLog::Types),
2714-
bound_type_ref_or_err.takeError(),
2715-
"Couldn't get type ref when binding generic type parameters: {0}");
2716-
return get_canonical();
2717-
}
2706+
if (!bound_type_ref_or_err)
2707+
return bound_type_ref_or_err.takeError();
2708+
27182709
NodePointer node = bound_type_ref_or_err->getDemangling(dem);
27192710

27202711
// Import the type into the scratch context. Subsequent conversions
@@ -2726,29 +2717,24 @@ SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27262717
// the original context as to resolve type aliases correctly.
27272718
auto &target = GetProcess().GetTarget();
27282719
auto scratch_ctx = TypeSystemSwiftTypeRefForExpressions::GetForTarget(target);
2729-
if (!scratch_ctx) {
2730-
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types),
2731-
"No scratch context available.");
2732-
return ts.GetTypeFromMangledTypename(mangled_name);
2733-
}
2720+
if (!scratch_ctx)
2721+
return llvm::createStringError("No scratch context available.");
2722+
27342723
CompilerType bound_type = scratch_ctx->RemangleAsType(dem, node, flavor);
27352724
LLDB_LOG(GetLog(LLDBLog::Expressions | LLDBLog::Types), "Bound {0} -> {1}.",
27362725
mangled_name, bound_type.GetMangledTypeName());
27372726

27382727
if (generic_signature && generic_signature->HasPacks()) {
27392728
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-
}
2729+
if (!bound_type_or_err)
2730+
return bound_type_or_err.takeError();
27452731
bound_type = *bound_type_or_err;
27462732
}
27472733

27482734
return bound_type;
27492735
}
27502736

2751-
CompilerType
2737+
llvm::Expected<CompilerType>
27522738
SwiftLanguageRuntime::BindGenericTypeParameters(StackFrame &stack_frame,
27532739
CompilerType base_type) {
27542740
// If this is a TypeRef type, bind that.
@@ -3232,7 +3218,9 @@ bool SwiftLanguageRuntime::GetDynamicTypeAndAddress(
32323218
if (!frame)
32333219
return false;
32343220

3235-
bound_type = BindGenericTypeParameters(*frame.get(), val_type);
3221+
bound_type = llvm::expectedToOptional(
3222+
BindGenericTypeParameters(*frame.get(), val_type))
3223+
.value_or(CompilerType());
32363224
if (!bound_type)
32373225
return false;
32383226
} else {
@@ -3486,7 +3474,10 @@ SwiftLanguageRuntime::GetSwiftRuntimeTypeInfo(
34863474
if (StackFrame *frame = exe_scope->CalculateStackFrame().get()) {
34873475
ExecutionContext exe_ctx;
34883476
frame->CalculateExecutionContext(exe_ctx);
3489-
type = BindGenericTypeParameters(*frame, type);
3477+
auto bound_type_or_err = BindGenericTypeParameters(*frame, type);
3478+
if (!bound_type_or_err)
3479+
return bound_type_or_err.takeError();
3480+
type = *bound_type_or_err;
34903481
}
34913482

34923483
// 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
@@ -133,7 +133,9 @@ std::optional<uint64_t> SwiftLanguageRuntime::GetMemberVariableOffsetRemoteAST(
133133
// Bind generic parameters if necessary.
134134
if (instance && swift_type->hasTypeParameter())
135135
if (auto *frame = instance->GetExecutionContextRef().GetFrameSP().get())
136-
if (auto bound = BindGenericTypeParameters(*frame, instance_type)) {
136+
if (auto bound = llvm::expectedToOptional(
137+
BindGenericTypeParameters(*frame, instance_type))
138+
.value_or(CompilerType())) {
137139
LLDB_LOGF(
138140
GetLog(LLDBLog::Types),
139141
"[MemberVariableOffsetResolver] resolved non-class type = %s",
@@ -266,7 +268,8 @@ SwiftLanguageRuntime::GetDynamicTypeAndAddress_ExistentialRemoteAST(
266268
}
267269
#endif
268270

269-
CompilerType SwiftLanguageRuntime::BindGenericTypeParametersRemoteAST(
271+
llvm::Expected<CompilerType>
272+
SwiftLanguageRuntime::BindGenericTypeParametersRemoteAST(
270273
StackFrame &stack_frame, CompilerType base_type) {
271274
LLDB_SCOPED_TIMER();
272275

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)