Skip to content

Commit 870b243

Browse files
Merge pull request llvm#2272 from adrian-prantl/self-resolve
Unify how local variables' types are resolved in the Swift expression…
2 parents cf1a27c + 72a325a commit 870b243

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

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

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,55 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
447447
};
448448
}; // END Anonymous namespace
449449

450+
451+
/// Returns the Swift type for a ValueObject representing a variable.
452+
/// An invalid CompilerType is returned on error.
453+
static CompilerType GetSwiftTypeForVariableValueObject(
454+
lldb::ValueObjectSP valobj_sp, lldb::StackFrameSP &stack_frame_sp,
455+
SwiftLanguageRuntime *runtime, bool use_dynamic_value) {
456+
// Check that the passed ValueObject is valid.
457+
if (!valobj_sp || valobj_sp->GetError().Fail())
458+
return CompilerType();
459+
CompilerType result = valobj_sp->GetCompilerType();
460+
if (use_dynamic_value)
461+
result = runtime->BindGenericTypeParameters(*stack_frame_sp, result);
462+
if (!result.GetTypeSystem()->SupportsLanguage(lldb::eLanguageTypeSwift))
463+
return CompilerType();
464+
return result;
465+
}
466+
467+
/// Return the type for a local variable. This function is threading a
468+
/// fine line between using dynamic type resolution to resolve generic
469+
/// types and not resolving too much: Objective-C classes can have
470+
/// more specific private implementations that LLDB can resolve, but
471+
/// SwiftASTContext cannot see because there is no header file that
472+
/// would declare them.
473+
static CompilerType ResolveVariable(
474+
lldb::VariableSP variable_sp, lldb::StackFrameSP &stack_frame_sp,
475+
SwiftLanguageRuntime * runtime, lldb::DynamicValueType use_dynamic) {
476+
lldb::ValueObjectSP valobj_sp =
477+
stack_frame_sp->GetValueObjectForFrameVariable(variable_sp,
478+
lldb::eNoDynamicValues);
479+
const bool use_dynamic_value = use_dynamic > lldb::eNoDynamicValues;
480+
481+
CompilerType var_type = GetSwiftTypeForVariableValueObject(
482+
valobj_sp, stack_frame_sp, runtime, use_dynamic_value);
483+
484+
if (!var_type.IsValid())
485+
return {};
486+
487+
// If the type can't be realized and dynamic types are allowed, fall back to
488+
// the dynamic type.
489+
if (!SwiftASTContext::IsFullyRealized(var_type) && use_dynamic_value) {
490+
var_type = GetSwiftTypeForVariableValueObject(
491+
valobj_sp->GetDynamicValue(use_dynamic), stack_frame_sp, runtime,
492+
use_dynamic_value);
493+
if (!var_type.IsValid())
494+
return {};
495+
}
496+
return var_type;
497+
}
498+
450499
static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
451500
SwiftASTContextForExpressions &swift_ast_context,
452501
SwiftASTManipulator &manipulator,
@@ -475,16 +524,10 @@ static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
475524
if (!self_var_sp)
476525
return;
477526

478-
CompilerType self_type;
479-
480-
if (stack_frame_sp) {
481-
lldb::ValueObjectSP valobj_sp =
482-
stack_frame_sp->GetValueObjectForFrameVariable(self_var_sp,
483-
use_dynamic);
484-
485-
if (valobj_sp)
486-
self_type = valobj_sp->GetCompilerType();
487-
}
527+
auto *swift_runtime =
528+
SwiftLanguageRuntime::Get(stack_frame_sp->GetThread()->GetProcess());
529+
CompilerType self_type =
530+
ResolveVariable(self_var_sp, stack_frame_sp, swift_runtime, use_dynamic);
488531

489532
if (!self_type.IsValid()) {
490533
if (Type *type = self_var_sp->GetType()) {
@@ -504,8 +547,6 @@ static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
504547
if (!imported_self_type.IsValid())
505548
return;
506549

507-
auto *swift_runtime =
508-
SwiftLanguageRuntime::Get(stack_frame_sp->GetThread()->GetProcess());
509550
auto *stack_frame = stack_frame_sp.get();
510551
imported_self_type = swift_runtime->BindGenericTypeParameters(
511552
*stack_frame, imported_self_type);
@@ -588,22 +629,6 @@ static void AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
588629
}
589630
}
590631

591-
/// Returns the Swift type for a ValueObject representing a variable.
592-
/// An invalid CompilerType is returned on error.
593-
static CompilerType GetSwiftTypeForVariableValueObject(
594-
lldb::ValueObjectSP valobj_sp, lldb::StackFrameSP &stack_frame_sp,
595-
SwiftLanguageRuntime *runtime, bool use_dynamic_value) {
596-
// Check that the passed ValueObject is valid.
597-
if (!valobj_sp || valobj_sp->GetError().Fail())
598-
return CompilerType();
599-
CompilerType result = valobj_sp->GetCompilerType();
600-
if (use_dynamic_value)
601-
result = runtime->BindGenericTypeParameters(*stack_frame_sp, result);
602-
if (!result.GetTypeSystem()->SupportsLanguage(lldb::eLanguageTypeSwift))
603-
return CompilerType();
604-
return result;
605-
}
606-
607632
/// Create a \c VariableInfo record for \c variable if there isn't
608633
/// already shadowing inner declaration in \c processed_variables.
609634
static llvm::Optional<llvm::Error> AddVariableInfo(
@@ -633,27 +658,8 @@ static llvm::Optional<llvm::Error> AddVariableInfo(
633658
if (!stack_frame_sp)
634659
return llvm::None;
635660

636-
lldb::ValueObjectSP valobj_sp =
637-
stack_frame_sp->GetValueObjectForFrameVariable(variable_sp,
638-
lldb::eNoDynamicValues);
639-
640-
const bool use_dynamic_value = use_dynamic > lldb::eNoDynamicValues;
641-
642-
CompilerType var_type = GetSwiftTypeForVariableValueObject(
643-
valobj_sp, stack_frame_sp, runtime, use_dynamic_value);
644-
645-
if (!var_type.IsValid())
646-
return {};
647-
648-
// If the type can't be realized and dynamic types are allowed, fall back to
649-
// the dynamic type.
650-
if (!SwiftASTContext::IsFullyRealized(var_type) && use_dynamic_value) {
651-
var_type = GetSwiftTypeForVariableValueObject(
652-
valobj_sp->GetDynamicValue(use_dynamic), stack_frame_sp, runtime,
653-
use_dynamic_value);
654-
if (!var_type.IsValid())
655-
return {};
656-
}
661+
CompilerType var_type =
662+
ResolveVariable(variable_sp, stack_frame_sp, runtime, use_dynamic);
657663

658664
Status error;
659665
CompilerType target_type = ast_context.ImportType(var_type, error);

0 commit comments

Comments
 (0)