Skip to content

Commit 927f129

Browse files
committed
[lldb] Retire the Swift-specific scratch context accessors
These used to be necessary because there used to be context-specific scratch contexts, however, now there is only one global TypeSystemSwiftTypeRefForExpressions, and it manages multiple SwiftASTContexts. Therefore it is no longer necessary to have Swift-specific hooks in Target and ValueObject.
1 parent 05f909d commit 927f129

21 files changed

+274
-363
lines changed

lldb/include/lldb/Symbol/TypeSystem.h

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -612,14 +612,6 @@ class TypeSystemMap {
612612
GetTypeSystemForLanguage(lldb::LanguageType language, Target *target,
613613
bool can_create);
614614

615-
// BEGIN SWIFT
616-
llvm::Expected<lldb::TypeSystemSP>
617-
GetTypeSystemForLanguage(lldb::LanguageType language, Target *target,
618-
bool can_create, const char *compiler_options);
619-
620-
void RemoveTypeSystemsForLanguage(lldb::LanguageType language);
621-
// END SWIFT
622-
623615
/// Check all type systems in the map to see if any have forcefully completed
624616
/// types;
625617
bool GetHasForcefullyCompletedTypes() const;

lldb/include/lldb/Target/Target.h

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1248,20 +1248,14 @@ class Target : public std::enable_shared_from_this<Target>,
12481248

12491249
llvm::Expected<lldb::TypeSystemSP>
12501250
GetScratchTypeSystemForLanguage(lldb::LanguageType language,
1251-
bool create_on_demand = true,
1252-
const char *compiler_options = nullptr);
1251+
bool create_on_demand = true);
12531252

12541253
std::vector<lldb::TypeSystemSP>
12551254
GetScratchTypeSystems(bool create_on_demand = true);
12561255

12571256
PersistentExpressionState *
12581257
GetPersistentExpressionStateForLanguage(lldb::LanguageType language);
12591258

1260-
#ifdef LLDB_ENABLE_SWIFT
1261-
SwiftPersistentExpressionState *
1262-
GetSwiftPersistentExpressionState(ExecutionContextScope &exe_scope);
1263-
#endif // LLDB_ENABLE_SWIFT
1264-
12651259
const TypeSystemMap &GetTypeSystemMap();
12661260

12671261
// Creates a UserExpression for the given language, the rest of the
@@ -1294,18 +1288,7 @@ class Target : public std::enable_shared_from_this<Target>,
12941288
CreateUtilityFunction(std::string expression, std::string name,
12951289
lldb::LanguageType language, ExecutionContext &exe_ctx);
12961290

1297-
12981291
#ifdef LLDB_ENABLE_SWIFT
1299-
/// Get the lock guarding the scratch typesystem from being re-initialized.
1300-
std::shared_mutex &GetSwiftScratchContextLock() {
1301-
return m_scratch_typesystem_lock;
1302-
}
1303-
1304-
TypeSystemSwiftTypeRefForExpressionsSP
1305-
GetSwiftScratchContext(Status &error, ExecutionContextScope &exe_scope,
1306-
bool create_on_demand = true,
1307-
bool for_playground = false);
1308-
13091292
/// Return whether this is the Swift REPL.
13101293
bool IsSwiftREPL();
13111294

@@ -1719,9 +1702,6 @@ class Target : public std::enable_shared_from_this<Target>,
17191702

17201703
bool m_did_display_scratch_fallback_warning = false;
17211704

1722-
/// Guards the scratch typesystem from being re-initialized.
1723-
std::shared_mutex m_scratch_typesystem_lock;
1724-
17251705
static void ImageSearchPathsChanged(const PathMappingList &path_list,
17261706
void *baton);
17271707

lldb/include/lldb/ValueObject/ValueObject.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -613,10 +613,6 @@ class ValueObject {
613613

614614
virtual bool HasSyntheticValue();
615615

616-
#ifdef LLDB_ENABLE_SWIFT
617-
TypeSystemSwiftTypeRefForExpressionsSP GetSwiftScratchContext();
618-
#endif // LLDB_ENABLE_SWIFT
619-
620616
virtual bool IsSynthetic() { return false; }
621617

622618
lldb::ValueObjectSP

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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ LLDBNameLookup::LLDBNameLookup(
143143

144144
if (!m_sc.target_sp)
145145
return;
146-
m_persistent_vars =
147-
m_sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
146+
m_persistent_vars = llvm::cast<SwiftPersistentExpressionState>(
147+
m_sc.target_sp->GetPersistentExpressionStateForLanguage(
148+
lldb::eLanguageTypeSwift));
148149
}
149150

150151
swift::SILValue LLDBNameLookup::emitLValueForVariable(
@@ -711,7 +712,8 @@ static void ResolveSpecialNames(
711712
return;
712713

713714
auto *persistent_state =
714-
sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
715+
sc.target_sp->GetPersistentExpressionStateForLanguage(
716+
lldb::eLanguageTypeSwift);
715717

716718
std::set<ConstString> resolved_names;
717719

@@ -1836,8 +1838,9 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
18361838
// Allow variables to be re-used from previous REPL statements.
18371839
if (m_sc.target_sp && (repl || !playground)) {
18381840
Status error;
1839-
auto *persistent_state =
1840-
m_sc.target_sp->GetSwiftPersistentExpressionState(*m_exe_scope);
1841+
auto *persistent_state = llvm::cast<SwiftPersistentExpressionState>(
1842+
m_sc.target_sp->GetPersistentExpressionStateForLanguage(
1843+
lldb::eLanguageTypeSwift));
18411844

18421845
llvm::SmallVector<size_t, 1> declaration_indexes;
18431846
parsed_expr->code_manipulator->FindVariableDeclarations(declaration_indexes,
@@ -2117,8 +2120,9 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
21172120
module_name = module->getName().get();
21182121
m_swift_ast_ctx.CacheModule(module_name, module);
21192122
if (m_sc.target_sp) {
2120-
auto *persistent_state =
2121-
m_sc.target_sp->GetSwiftPersistentExpressionState(*m_exe_scope);
2123+
auto *persistent_state = llvm::cast<SwiftPersistentExpressionState>(
2124+
m_sc.target_sp->GetPersistentExpressionStateForLanguage(
2125+
lldb::eLanguageTypeSwift));
21222126
persistent_state->CopyInSwiftPersistentDecls(
21232127
parsed_expr->external_lookup.GetStagedDecls());
21242128
}

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

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ lldb::REPLSP SwiftREPL::CreateInstanceFromTarget(Status &err, Target &target,
8787
}
8888

8989
// Check that we can get a type system, or we aren't going anywhere:
90-
auto type_system_or_err = target.GetScratchTypeSystemForLanguage(
91-
eLanguageTypeSwift, true, repl_options ? repl_options : "");
90+
auto type_system_or_err =
91+
target.GetScratchTypeSystemForLanguage(eLanguageTypeSwift, true);
9292
if (!type_system_or_err) {
9393
llvm::consumeError(type_system_or_err.takeError());
9494
err = Status::FromErrorString("Could not construct an expression "
@@ -260,8 +260,8 @@ lldb::REPLSP SwiftREPL::CreateInstanceFromDebugger(Status &err,
260260
// Check that we can get a type system, or we aren't
261261
// going anywhere. Remember to pass in the repl_options
262262
// in case they set up framework paths we need, etc.
263-
auto type_system_or_err = target_sp->GetScratchTypeSystemForLanguage(
264-
eLanguageTypeSwift, true, repl_options ? repl_options : "");
263+
auto type_system_or_err =
264+
target_sp->GetScratchTypeSystemForLanguage(eLanguageTypeSwift, true);
265265
if (!type_system_or_err) {
266266
llvm::consumeError(type_system_or_err.takeError());
267267
err = Status::FromErrorString("Could not construct an expression "
@@ -301,14 +301,13 @@ SwiftREPL::SwiftREPL(Target &target) : REPL(target), m_swift_ast(nullptr) {}
301301
SwiftREPL::~SwiftREPL() {}
302302

303303
Status SwiftREPL::DoInitialization() {
304-
if (m_compiler_options.empty())
305-
return Status();
306-
307304
auto type_system_or_err =
308-
m_target.GetScratchTypeSystemForLanguage(eLanguageTypeSwift, true,
309-
m_compiler_options.c_str());
305+
m_target.GetScratchTypeSystemForLanguage(eLanguageTypeSwift, true);
310306
if (!type_system_or_err)
311307
return Status::FromError(type_system_or_err.takeError());
308+
std::static_pointer_cast<TypeSystemSwiftTypeRefForExpressions>(
309+
*type_system_or_err)
310+
->SetCompilerOptions(m_compiler_options.c_str());
312311
return Status();
313312
}
314313

@@ -567,22 +566,29 @@ SwiftASTContextForExpressions *SwiftREPL::getSwiftASTContext() {
567566
// to fix this issue currently, so we need to work around it by making
568567
// our own copy of the AST and using this separate AST for completion.
569568
//----------------------------------------------------------------------
570-
if (!m_swift_ast) {
571-
auto type_system_or_err =
572-
m_target.GetScratchTypeSystemForLanguage(eLanguageTypeSwift);
573-
if (!type_system_or_err) {
574-
llvm::consumeError(type_system_or_err.takeError());
575-
return nullptr;
576-
}
577-
auto *swift_ts =
578-
llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
579-
type_system_or_err->get());
580-
auto *target_swift_ast =
581-
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
582-
swift_ts->GetSwiftASTContext(SymbolContext(
583-
m_target.shared_from_this(), m_target.GetExecutableModule())));
584-
m_swift_ast = target_swift_ast;
569+
if (m_swift_ast)
570+
return m_swift_ast;
571+
572+
auto type_system_or_err =
573+
m_target.GetScratchTypeSystemForLanguage(eLanguageTypeSwift, false);
574+
if (!type_system_or_err) {
575+
llvm::consumeError(type_system_or_err.takeError());
576+
return nullptr;
585577
}
578+
auto *swift_ts = llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
579+
type_system_or_err->get());
580+
581+
// Use the stdlib as symbol context to get a different one than the main REPL.
582+
SymbolContextList sc_list;
583+
m_target.GetImages().FindSymbolsWithNameAndType(ConstString("_swift_release"),
584+
eSymbolTypeAny, sc_list);
585+
if (!sc_list.GetSize())
586+
return nullptr;
587+
588+
auto *target_swift_ast =
589+
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
590+
swift_ts->GetSwiftASTContext(sc_list[0]));
591+
m_swift_ast = target_swift_ast;
586592
return m_swift_ast;
587593
}
588594

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

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -503,14 +503,6 @@ static llvm::Error RegisterAllVariables(
503503
return llvm::Error::success();
504504
}
505505

506-
static SwiftPersistentExpressionState *
507-
GetPersistentState(Target *target, ExecutionContext &exe_ctx) {
508-
auto exe_scope = exe_ctx.GetBestExecutionContextScope();
509-
if (!exe_scope)
510-
return nullptr;
511-
return target->GetSwiftPersistentExpressionState(*exe_scope);
512-
}
513-
514506
/// Check if we can evaluate the expression as generic.
515507
/// Currently, evaluating expression as a generic has several limitations:
516508
/// - Only self will be evaluated with unbound generics.
@@ -727,12 +719,19 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
727719

728720
exe_scope = exe_ctx.GetBestExecutionContextScope();
729721

730-
m_swift_scratch_ctx = target->GetSwiftScratchContext(
731-
m_err, *exe_scope, /*create_on_demand=*/true,
732-
m_options.GetPlaygroundTransformEnabled());
733-
if (!m_swift_scratch_ctx)
722+
auto ts_or_err = target->GetScratchTypeSystemForLanguage(
723+
lldb::eLanguageTypeSwift, /*create_on_demand=*/true);
724+
if (!ts_or_err)
734725
return error("could not create a Swift scratch context: ",
735-
m_err.AsCString());
726+
llvm::toString(ts_or_err.takeError()).c_str());
727+
m_swift_scratch_ctx =
728+
std::static_pointer_cast<TypeSystemSwiftTypeRefForExpressions>(
729+
*ts_or_err);
730+
if (!m_swift_scratch_ctx)
731+
return error("could not create a Swift scratch context: ", "unknown error");
732+
// Notify SwiftASTContext that this is a Playground.
733+
if (m_options.GetPlaygroundTransformEnabled())
734+
m_swift_scratch_ctx->SetCompilerOptions("");
736735

737736
// For playgrounds, the target triple should be used for expression
738737
// evaluation, not the current module. This requires disabling precise
@@ -757,7 +756,8 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
757756
}
758757

759758
// This may destroy the scratch context.
760-
auto *persistent_state = GetPersistentState(target, exe_ctx);
759+
auto *persistent_state =
760+
target->GetPersistentExpressionStateForLanguage(lldb::eLanguageTypeSwift);
761761
if (!persistent_state)
762762
return error("could not start parsing (no persistent data)");
763763

@@ -898,8 +898,10 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
898898
// We currently key off there being more than one external
899899
// function in the execution unit to determine whether it needs
900900
// to live in the process.
901-
GetPersistentState(exe_ctx.GetTargetPtr(), exe_ctx)
902-
->RegisterExecutionUnit(m_execution_unit_sp);
901+
if (auto *target = exe_ctx.GetTargetPtr())
902+
if (auto *state = target->GetPersistentExpressionStateForLanguage(
903+
lldb::eLanguageTypeSwift))
904+
state->RegisterExecutionUnit(m_execution_unit_sp);
903905
}
904906
}
905907

@@ -975,7 +977,8 @@ lldb::ExpressionVariableSP SwiftUserExpression::GetResultAfterDematerialization(
975977

976978
if (target_sp) {
977979
if (auto *persistent_state =
978-
target_sp->GetSwiftPersistentExpressionState(*exe_scope)) {
980+
target_sp->GetPersistentExpressionStateForLanguage(
981+
lldb::eLanguageTypeSwift)) {
979982
if (error_is_valid) {
980983
persistent_state->RemovePersistentVariable(in_result_sp);
981984
result_sp = in_error_sp;

lldb/source/Plugins/ExpressionParser/Swift/SwiftUserExpression.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ class SwiftUserExpression : public LLVMUserExpression {
189189
void DidDematerialize(lldb::ExpressionVariableSP &variable) override;
190190
};
191191

192-
TypeSystemSwiftSP m_swift_scratch_ctx;
192+
TypeSystemSwiftTypeRefForExpressionsSP m_swift_scratch_ctx;
193193
SwiftASTContextForExpressions *m_swift_ast_ctx;
194194
PersistentVariableDelegate m_persistent_variable_delegate;
195195
std::unique_ptr<SwiftExpressionParser> m_parser;

lldb/source/Plugins/Language/Swift/SwiftArray.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -330,7 +330,8 @@ SwiftArrayBufferHandler::CreateBufferHandler(ValueObject &static_valobj) {
330330

331331
// Get the type of the array elements.
332332
CompilerType argument_type;
333-
auto ts = valobj.GetSwiftScratchContext();
333+
auto ts = TypeSystemSwiftTypeRefForExpressions::GetForTarget(
334+
*valobj.GetTargetSP());
334335
if (!ts)
335336
return nullptr;
336337
auto *swift_runtime = SwiftLanguageRuntime::Get(process_sp);

lldb/source/Plugins/Language/Swift/SwiftHashedContainer.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,8 +291,9 @@ HashedCollectionConfig::StorageObjectAtAddress(
291291
// same address.
292292
Status error;
293293
ExecutionContextScope *exe_scope = exe_ctx.GetBestExecutionContextScope();
294-
auto scratch_ctx =
295-
process_sp->GetTarget().GetSwiftScratchContext(error, *exe_scope);
294+
295+
auto scratch_ctx = TypeSystemSwiftTypeRefForExpressions::GetForTarget(
296+
process_sp->GetTarget());
296297
if (!scratch_ctx)
297298
return nullptr;
298299
if (error.Fail())

lldb/source/Plugins/Language/Swift/SwiftLanguage.cpp

Lines changed: 22 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,7 +1055,8 @@ SwiftLanguage::GetHardcodedSynthetics() {
10551055
LLDB_LOGV(log, "[Matching CxxBridgedSyntheticChildProvider] - "
10561056
"Could not get the swift runtime.");
10571057

1058-
auto ts = valobj.GetSwiftScratchContext();
1058+
auto ts = TypeSystemSwiftTypeRefForExpressions::GetForTarget(
1059+
valobj.GetTargetSP());
10591060
if (!ts) {
10601061
LLDB_LOGV(log, "[Matching CxxBridgedSyntheticChildProvider] - "
10611062
"Could not get the Swift scratch context.");
@@ -1347,28 +1348,24 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13471348
size_t before = results.size();
13481349

13491350
if (exe_scope) {
1350-
Target *target = exe_scope->CalculateTarget().get();
1351-
if (target) {
1352-
const bool create_on_demand = false;
1353-
Status error;
1354-
auto scratch_ctx = target->GetSwiftScratchContext(
1355-
error, *exe_scope, create_on_demand);
1356-
if (auto frame_sp = exe_scope->CalculateStackFrame()) {
1357-
auto &sc =
1358-
frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
1359-
if (scratch_ctx)
1360-
if (SwiftASTContext *ast_ctx =
1361-
scratch_ctx->GetSwiftASTContext(sc)) {
1362-
ConstString cs_input{input};
1363-
Mangled mangled(cs_input);
1364-
if (mangled.GuessLanguage() == eLanguageTypeSwift) {
1365-
auto candidate =
1366-
ast_ctx->GetTypeFromMangledTypename(cs_input);
1367-
if (candidate.IsValid())
1368-
results.insert(candidate);
1369-
}
1351+
TargetSP target = exe_scope->CalculateTarget();
1352+
auto scratch_ctx =
1353+
TypeSystemSwiftTypeRefForExpressions::GetForTarget(target);
1354+
if (auto frame_sp = exe_scope->CalculateStackFrame()) {
1355+
auto &sc =
1356+
frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
1357+
if (scratch_ctx)
1358+
if (SwiftASTContext *ast_ctx =
1359+
scratch_ctx->GetSwiftASTContext(sc)) {
1360+
ConstString cs_input{input};
1361+
Mangled mangled(cs_input);
1362+
if (mangled.GuessLanguage() == eLanguageTypeSwift) {
1363+
auto candidate =
1364+
ast_ctx->GetTypeFromMangledTypename(cs_input);
1365+
if (candidate.IsValid())
1366+
results.insert(candidate);
13701367
}
1371-
}
1368+
}
13721369
}
13731370
}
13741371

@@ -1420,11 +1417,9 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
14201417
size_t before = results.size();
14211418

14221419
if (exe_scope) {
1423-
Target *target = exe_scope->CalculateTarget().get();
1424-
const bool create_on_demand = false;
1425-
Status error;
1426-
auto scratch_ctx = target->GetSwiftScratchContext(error, *exe_scope,
1427-
create_on_demand);
1420+
TargetSP target = exe_scope->CalculateTarget();
1421+
auto scratch_ctx =
1422+
TypeSystemSwiftTypeRefForExpressions::GetForTarget(target);
14281423
if (auto frame_sp = exe_scope->CalculateStackFrame()) {
14291424
const SymbolContext &sc =
14301425
frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);

0 commit comments

Comments
 (0)