Skip to content

Commit 2ea629d

Browse files
Merge pull request #1966 from adrian-prantl/69020595
Use the dynamic type of self in the expression evaluator.
2 parents cd779d2 + 4863244 commit 2ea629d

File tree

13 files changed

+235
-97
lines changed

13 files changed

+235
-97
lines changed

lldb/include/lldb/Symbol/Type.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class SymbolFileType : public std::enable_shared_from_this<SymbolFileType>,
5656
Type *operator->() { return GetType(); }
5757

5858
Type *GetType();
59+
SymbolFile &GetSymbolFile() const { return m_symbol_file; }
5960

6061
protected:
6162
SymbolFile &m_symbol_file;

lldb/include/lldb/Symbol/Variable.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ class Variable : public UserID, public std::enable_shared_from_this<Variable> {
6565

6666
lldb::ValueType GetScope() const { return m_scope; }
6767

68+
const RangeList &GetScopeRange() const { return m_scope_range; }
69+
6870
bool IsExternal() const { return m_external; }
6971

7072
bool IsArtificial() const { return m_artificial; }

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include "Plugins/TypeSystem/Swift/SwiftASTContext.h"
1616
#include "lldb/Expression/ExpressionParser.h"
1717
#include "lldb/Expression/ExpressionSourceCode.h"
18+
#include "lldb/Target/SwiftLanguageRuntime.h"
1819
#include "lldb/Target/Target.h"
1920
#include "lldb/Utility/ConstString.h"
2021
#include "lldb/Utility/Log.h"
@@ -1021,11 +1022,6 @@ GetPatternBindingForVarDecl(swift::VarDecl *var_decl,
10211022
return pattern_binding;
10221023
}
10231024

1024-
static inline swift::Type GetSwiftType(CompilerType type) {
1025-
return swift::Type(
1026-
reinterpret_cast<swift::TypeBase *>(type.GetOpaqueQualType()));
1027-
}
1028-
10291025
bool SwiftASTManipulator::AddExternalVariables(
10301026
llvm::MutableArrayRef<VariableInfo> variables) {
10311027
if (!IsValid())

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

Lines changed: 56 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -479,7 +479,7 @@ AddRequiredAliases(Block *block, lldb::StackFrameSP &stack_frame_sp,
479479
if (stack_frame_sp) {
480480
lldb::ValueObjectSP valobj_sp =
481481
stack_frame_sp->GetValueObjectForFrameVariable(self_var_sp,
482-
lldb::eNoDynamicValues);
482+
lldb::eDynamicCanRunTarget);
483483

484484
if (valobj_sp)
485485
self_type = valobj_sp->GetCompilerType();
@@ -604,7 +604,7 @@ static void AddVariableInfo(
604604
if (stack_frame_sp) {
605605
lldb::ValueObjectSP valobj_sp =
606606
stack_frame_sp->GetValueObjectForFrameVariable(variable_sp,
607-
lldb::eNoDynamicValues);
607+
lldb::eDynamicCanRunTarget);
608608

609609
if (!valobj_sp || valobj_sp->GetError().Fail()) {
610610
// Ignore the variable if we couldn't find its corresponding
@@ -628,12 +628,6 @@ static void AddVariableInfo(
628628
if (!target_type.IsValid())
629629
return;
630630

631-
// Resolve all archetypes in the variable type.
632-
if (stack_frame_sp)
633-
if (language_runtime)
634-
target_type = language_runtime->BindGenericTypeParameters(*stack_frame_sp,
635-
target_type);
636-
637631
// If we couldn't fully realize the type, then we aren't going
638632
// to get very far making a local out of it, so discard it here.
639633
Log *log(lldb_private::GetLogIfAnyCategoriesSet(LIBLLDB_LOG_TYPES |
@@ -642,7 +636,7 @@ static void AddVariableInfo(
642636
if (log)
643637
log->Printf("Discarding local %s because we couldn't fully realize it, "
644638
"our best attempt was: %s.",
645-
name_cstr, target_type.GetTypeName().AsCString("<unknown>"));
639+
name_cstr, target_type.GetDisplayTypeName().AsCString("<unknown>"));
646640
return;
647641
}
648642

@@ -656,8 +650,27 @@ static void AddVariableInfo(
656650
static_cast<void *>(swift_type.getPointer()),
657651
static_cast<void *>(ast_context.GetASTContext()), s.c_str());
658652
}
653+
// A one-off clone of variable_sp with the type replaced by target_type.
654+
auto patched_variable_sp = std::make_shared<lldb_private::Variable>(
655+
0, variable_sp->GetName().GetCString(), "",
656+
std::make_shared<lldb_private::SymbolFileType>(
657+
*variable_sp->GetType()->GetSymbolFile(),
658+
std::make_shared<lldb_private::Type>(
659+
0, variable_sp->GetType()->GetSymbolFile(),
660+
variable_sp->GetType()->GetName(), llvm::None,
661+
variable_sp->GetType()->GetSymbolContextScope(), LLDB_INVALID_UID,
662+
Type::eEncodingIsUID, variable_sp->GetType()->GetDeclaration(),
663+
target_type, lldb_private::Type::ResolveState::Full,
664+
variable_sp->GetType()->GetPayload())),
665+
variable_sp->GetScope(), variable_sp->GetSymbolContextScope(),
666+
variable_sp->GetScopeRange(),
667+
const_cast<lldb_private::Declaration *>(&variable_sp->GetDeclaration()),
668+
variable_sp->LocationExpression(), variable_sp->IsExternal(),
669+
variable_sp->IsArtificial(),
670+
variable_sp->GetLocationIsConstantValueData(),
671+
variable_sp->IsStaticMember(), variable_sp->IsConstant());
659672
SwiftASTManipulatorBase::VariableMetadataSP metadata_sp(
660-
new VariableMetadataVariable(variable_sp));
673+
new VariableMetadataVariable(patched_variable_sp));
661674
SwiftASTManipulator::VariableInfo variable_info(
662675
target_type, ast_context.GetASTContext()->getIdentifier(overridden_name),
663676
metadata_sp,
@@ -917,6 +930,23 @@ CreateMainFile(SwiftASTContextForExpressions &swift_ast_context,
917930
return {buffer_id, filename.str()};
918931
}
919932

933+
/// Determine whether this type was defined inside an LLDB expression.
934+
template <typename TypeType> bool FromLLDBModuleImpl(TypeType *type) {
935+
if (auto *decl = type->getDecl())
936+
if (auto *module = decl->getModuleContext())
937+
return module->getName().str().startswith("__lldb_expr_");
938+
return false;
939+
};
940+
941+
/// Determine whether this type was defined inside an LLDB expression.
942+
static bool FromLLDBModule(swift::TypeBase *type) {
943+
if (auto *type_alias = llvm::dyn_cast<swift::TypeAliasType>(type))
944+
return FromLLDBModuleImpl(type_alias);
945+
if (auto *nominal = llvm::dyn_cast<swift::NominalType>(type))
946+
return FromLLDBModuleImpl(nominal);
947+
return false;
948+
}
949+
920950
/// Attempt to materialize one variable.
921951
static llvm::Optional<SwiftExpressionParser::SILVariableInfo>
922952
MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
@@ -954,23 +984,7 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
954984
}
955985
}
956986
} else {
957-
CompilerType actual_type(variable.GetType());
958-
auto orig_swift_type = GetSwiftType(actual_type);
959-
auto *swift_type = orig_swift_type->mapTypeOutOfContext().getPointer();
960-
actual_type = ToCompilerType(swift_type);
961-
lldb::StackFrameSP stack_frame_sp = stack_frame_wp.lock();
962-
if (swift_type->hasTypeParameter()) {
963-
if (stack_frame_sp && stack_frame_sp->GetThread() &&
964-
stack_frame_sp->GetThread()->GetProcess()) {
965-
auto *swift_runtime = SwiftLanguageRuntime::Get(
966-
stack_frame_sp->GetThread()->GetProcess());
967-
if (swift_runtime) {
968-
actual_type = swift_runtime->BindGenericTypeParameters(
969-
*stack_frame_sp, actual_type);
970-
}
971-
}
972-
}
973-
987+
CompilerType actual_type = variable.GetType();
974988
// Desugar '$lldb_context', etc.
975989
auto transformed_type = GetSwiftType(actual_type).transform(
976990
[](swift::Type t) -> swift::Type {
@@ -981,7 +995,21 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
981995
}
982996
return t;
983997
});
984-
actual_type = ToCompilerType(transformed_type.getPointer());
998+
actual_type =
999+
ToCompilerType(transformed_type->mapTypeOutOfContext().getPointer());
1000+
// CompilerType return_ast_type =
1001+
// ToCompilerType(result_type->mapTypeOutOfContext());
1002+
auto *swift_ast_ctx =
1003+
llvm::cast<SwiftASTContext>(actual_type.GetTypeSystem());
1004+
1005+
// Currently the Swift runtime cannot resolve types that were
1006+
// defined in the expression evaluator. That's because we don't
1007+
// tell it about type metadata sections that were JIT-compiled
1008+
// by the expression evaluator. Until that is implemented, fall
1009+
// back to SwiftASTContext.
1010+
if (!FromLLDBModule(transformed_type.getPointer()))
1011+
actual_type =
1012+
swift_ast_ctx->GetTypeRefType(actual_type.GetOpaqueQualType());
9851013

9861014
if (is_result)
9871015
offset = materializer.AddResultVariable(
@@ -1009,9 +1037,6 @@ MaterializeVariable(SwiftASTManipulatorBase::VariableInfo &variable,
10091037
VariableMetadataVariable *variable_metadata =
10101038
static_cast<VariableMetadataVariable *>(variable.m_metadata.get());
10111039

1012-
// FIXME: It would be nice if we could do something like
1013-
// variable_metadata->m_variable_sp->SetType(variable.GetType())
1014-
// here.
10151040
offset = materializer.AddVariable(variable_metadata->m_variable_sp, error);
10161041

10171042
if (!error.Success()) {

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class SwiftASTContext : public TypeSystemSwift {
175175

176176
bool SupportsLanguage(lldb::LanguageType language) override;
177177

178+
SwiftASTContext *GetSwiftASTContext() override { return this; }
179+
178180
Status IsCompatible() override;
179181

180182
swift::SourceManager &GetSourceManager();

lldb/source/Plugins/TypeSystem/Swift/TypeSystemSwift.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Decl;
2727

2828
namespace lldb_private {
2929
class TypeSystemClang;
30+
class SwiftASTContext;
3031

3132
/// The implementation of lldb::Type's m_payload field for TypeSystemSwift.
3233
class TypePayloadSwift {
@@ -97,6 +98,7 @@ class TypeSystemSwift : public TypeSystem {
9798
/// \}
9899

99100
static LanguageSet GetSupportedLanguagesForTypes();
101+
virtual SwiftASTContext *GetSwiftASTContext() = 0;
100102
virtual Module *GetModule() const = 0;
101103
virtual lldb::TypeSP GetCachedType(ConstString mangled) = 0;
102104
virtual void SetCachedType(ConstString mangled,

0 commit comments

Comments
 (0)