Skip to content

SwiftUserExpression: Sink initialization of scratch context into Parse() #5302

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
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
11 changes: 7 additions & 4 deletions lldb/include/lldb/Core/SwiftScratchContextReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class SharedMutex {
}
bool unlock_shared() {
std::lock_guard<std::mutex> lock(m_reader_mutex);
assert(m_readers > 0);
--m_readers;
return m_impl.unlock_shared();
}
Expand All @@ -57,7 +58,12 @@ struct ScopedSharedMutexReader {
if (m_mutex)
m_mutex->lock_shared();
}
ScopedSharedMutexReader(const ScopedSharedMutexReader&) = default;

ScopedSharedMutexReader(const ScopedSharedMutexReader &copy)
: m_mutex(copy.m_mutex) {
if (m_mutex)
m_mutex->lock_shared();
}

~ScopedSharedMutexReader() {
if (m_mutex)
Expand Down Expand Up @@ -101,9 +107,6 @@ class SwiftScratchContextReader : ScopedSharedMutexReader {
assert(m_ptr && "invalid context");
}

SwiftScratchContextReader(const SwiftScratchContextReader &copy)
: ScopedSharedMutexReader(copy.m_mutex), m_ptr(copy.m_ptr) {}

TypeSystemSwiftTypeRefForExpressions *get() {
assert(m_ptr && "invalid context");
return m_ptr;
Expand Down
45 changes: 19 additions & 26 deletions lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,9 @@ SwiftUserExpression::SwiftUserExpression(
m_type_system_helper(*m_target_wp.lock().get()),
m_result_delegate(exe_scope.CalculateTarget(), *this, false),
m_error_delegate(exe_scope.CalculateTarget(), *this, true),
m_persistent_variable_delegate(*this),
m_swift_scratch_ctx(
exe_scope.CalculateTarget()
? exe_scope.CalculateTarget()->GetSwiftScratchContext(m_err,
exe_scope)
: llvm::None) {
m_persistent_variable_delegate(*this) {
m_runs_in_playground_or_repl =
options.GetREPLEnabled() || options.GetPlaygroundTransformEnabled();
if (m_swift_scratch_ctx)
if (m_swift_scratch_ctx->get())
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
m_swift_scratch_ctx->get()->GetSwiftASTContext());
}

SwiftUserExpression::~SwiftUserExpression() {}
Expand Down Expand Up @@ -305,6 +296,23 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
return false;
}

auto exe_scope = exe_ctx.GetBestExecutionContextScope();
if (!exe_scope) {
LLDB_LOG(log, "no execution context scope");
return false;
}

ExecutionContext target_scope(*target);
m_swift_scratch_ctx = target->GetSwiftScratchContext(
m_err, /* FIXME: should be exe_scope */ *target_scope
.GetBestExecutionContextScope());
if (!m_swift_scratch_ctx) {
LLDB_LOG(log, "no scratch context", m_err.AsCString());
return false;
}
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
m_swift_scratch_ctx->get()->GetSwiftASTContext());

if (!m_swift_ast_ctx) {
LLDB_LOG(log, "no Swift AST Context");
return false;
Expand Down Expand Up @@ -397,22 +405,6 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
Callback m_callback;
};

ExecutionContextScope *exe_scope = NULL;

Process *process = exe_ctx.GetProcessPtr();

do {
exe_scope = exe_ctx.GetFramePtr();
if (exe_scope)
break;

exe_scope = process;
if (exe_scope)
break;

exe_scope = exe_ctx.GetTargetPtr();
} while (0);

auto *swift_parser =
new SwiftExpressionParser(exe_scope, *m_swift_ast_ctx, *this, m_options);
unsigned error_code = swift_parser->Parse(
Expand Down Expand Up @@ -482,6 +474,7 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
auto module =
m_execution_unit_sp->CreateJITModule(jit_module_name.GetString().data());

Process *process = exe_ctx.GetProcessPtr();
auto *swift_runtime = SwiftLanguageRuntime::Get(process);
if (module && swift_runtime) {
ModuleList modules;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,11 @@ class SwiftUserExpression : public LLVMUserExpression {
void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
};

llvm::Optional<SwiftScratchContextReader> m_swift_scratch_ctx;
SwiftASTContextForExpressions *m_swift_ast_ctx;
PersistentVariableDelegate m_persistent_variable_delegate;
std::unique_ptr<SwiftExpressionParser> m_parser;
Status m_err;
llvm::Optional<SwiftScratchContextReader> m_swift_scratch_ctx;
bool m_runs_in_playground_or_repl;
bool m_needs_object_ptr = false;
bool m_in_static_method = false;
Expand Down