Skip to content

Commit 9ede895

Browse files
committed
Move HandloadedModules from SwiftPersistentExprState to SwiftASTContextFE (NFC)
This change is (somewhat surprisingly) NFC, since the list is only consumed by SwiftExpressionParser and only populated by it as well. When SwiftASTContextForExpressions is destroyed the persistent state is too.
1 parent 6e747cb commit 9ede895

File tree

5 files changed

+57
-88
lines changed

5 files changed

+57
-88
lines changed

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

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1261,9 +1261,6 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
12611261
// FIXME: We won't have to do this once the playground adds import
12621262
// statements for the things it needs itself.
12631263
if (playground) {
1264-
auto *persistent_state =
1265-
sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
1266-
12671264
Status error;
12681265
SourceModule module_info;
12691266
module_info.path.emplace_back("Swift");
@@ -1274,7 +1271,7 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
12741271
return error.ToError();
12751272
}
12761273

1277-
persistent_state->AddHandLoadedModule(ConstString("Swift"),
1274+
swift_ast_context.AddHandLoadedModule(ConstString("Swift"),
12781275
swift::ImportedModule(module));
12791276
}
12801277

@@ -1297,9 +1294,8 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
12971294
if (lldb::StackFrameSP this_frame_sp = stack_frame_wp.lock())
12981295
process_sp = this_frame_sp->CalculateProcess();
12991296
swift_ast_context.LoadImplicitModules(sc.target_sp, process_sp, exe_scope);
1300-
if (!SwiftASTContext::GetImplicitImports(swift_ast_context, sc, exe_scope,
1301-
process_sp, additional_imports,
1302-
implicit_import_error)) {
1297+
if (!swift_ast_context.GetImplicitImports(sc, process_sp, additional_imports,
1298+
implicit_import_error)) {
13031299
const char *msg = implicit_import_error.AsCString();
13041300
if (!msg)
13051301
msg = "error status positive, but import still failed";
@@ -1449,9 +1445,8 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
14491445
IRExecutionUnit::GetLLVMGlobalContextMutex());
14501446

14511447
Status auto_import_error;
1452-
if (!SwiftASTContext::CacheUserImports(swift_ast_context, sc, exe_scope,
1453-
process_sp, *source_file,
1454-
auto_import_error)) {
1448+
if (!swift_ast_context.CacheUserImports(process_sp, *source_file,
1449+
auto_import_error)) {
14551450
const char *msg = auto_import_error.AsCString();
14561451
if (!msg) {
14571452
// The import itself succeeded, but the AST context is in a

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

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,6 @@
1515

1616
#include "SwiftExpressionVariable.h"
1717

18-
#include "swift/AST/Import.h"
19-
#include "swift/AST/Module.h"
20-
21-
#include "lldb/Core/SwiftForward.h"
2218
#include "lldb/Expression/ExpressionVariable.h"
2319

2420
#include "llvm/ADT/StringMap.h"
@@ -38,10 +34,6 @@ namespace lldb_private {
3834
/// Also provides an increasing, 0-based counter for naming result
3935
/// variables.
4036
class SwiftPersistentExpressionState : public PersistentExpressionState {
41-
42-
typedef llvm::StringMap<swift::AttributedImport<swift::ImportedModule>>
43-
HandLoadedModuleSet;
44-
4537
public:
4638
class SwiftDeclMap {
4739
public:
@@ -113,28 +105,13 @@ class SwiftPersistentExpressionState : public PersistentExpressionState {
113105
const std::vector<CompilerDecl> &excluding_equivalents,
114106
std::vector<CompilerDecl> &matches);
115107

116-
// This just adds this module to the list of hand-loaded modules, it doesn't
117-
// actually load it.
118-
void AddHandLoadedModule(
119-
ConstString module_name,
120-
swift::AttributedImport<swift::ImportedModule> attributed_import) {
121-
m_hand_loaded_modules.insert_or_assign(module_name.GetStringRef(),
122-
attributed_import);
123-
}
124-
125-
/// This returns the list of hand-loaded modules.
126-
HandLoadedModuleSet GetHandLoadedModules() { return m_hand_loaded_modules; }
127-
128108
private:
129109
/// The counter used by GetNextResultName().
130110
uint32_t m_next_persistent_variable_id;
131111
/// The counter used by GetNextResultName() when is_error is true.
132112
uint32_t m_next_persistent_error_id;
133113
/// The persistent functions declared by the user.
134114
SwiftDeclMap m_swift_persistent_decls;
135-
/// These are the names of modules that we have loaded by hand into
136-
/// the Contexts we make for parsing.
137-
HandLoadedModuleSet m_hand_loaded_modules;
138115
};
139116
} // namespace lldb_private
140117

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -712,8 +712,8 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
712712
if (status.Fail() || !module_decl)
713713
return error("could not load Swift Standard Library", status.AsCString());
714714

715-
persistent_state->AddHandLoadedModule(ConstString("Swift"),
716-
swift::ImportedModule(module_decl));
715+
m_swift_ast_ctx->AddHandLoadedModule(ConstString("Swift"),
716+
swift::ImportedModule(module_decl));
717717
m_result_delegate.RegisterPersistentState(persistent_state);
718718
m_error_delegate.RegisterPersistentState(persistent_state);
719719

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

Lines changed: 12 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8226,24 +8226,18 @@ static swift::ModuleDecl *LoadOneModule(const SourceModule &module,
82268226
return swift_module;
82278227
}
82288228

8229-
bool SwiftASTContext::GetImplicitImports(
8230-
SwiftASTContext &swift_ast_context, SymbolContext &sc,
8231-
ExecutionContextScope &exe_scope, lldb::ProcessSP process_sp,
8229+
bool SwiftASTContextForExpressions::GetImplicitImports(
8230+
SymbolContext &sc, lldb::ProcessSP process_sp,
82328231
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
82338232
&modules,
82348233
Status &error) {
82358234
LLDB_SCOPED_TIMER();
8236-
if (!swift_ast_context.GetCompileUnitImports(sc, process_sp, modules,
8237-
error)) {
8235+
if (!GetCompileUnitImports(sc, process_sp, modules, error)) {
82388236
return false;
82398237
}
82408238

8241-
auto *persistent_expression_state =
8242-
sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
8243-
82448239
// Get the hand-loaded modules from the SwiftPersistentExpressionState.
8245-
for (auto &module_pair :
8246-
persistent_expression_state->GetHandLoadedModules()) {
8240+
for (auto &module_pair : m_hand_loaded_modules) {
82478241

82488242
auto &attributed_import = module_pair.second;
82498243

@@ -8257,7 +8251,7 @@ bool SwiftASTContext::GetImplicitImports(
82578251
// Otherwise, try reloading the ModuleDecl using the module name.
82588252
SourceModule module_info;
82598253
module_info.path.emplace_back(module_pair.first());
8260-
auto *module = LoadOneModule(module_info, swift_ast_context, process_sp,
8254+
auto *module = LoadOneModule(module_info, *this, process_sp,
82618255
/*import_dylibs=*/false, error);
82628256
if (!module)
82638257
return false;
@@ -8268,8 +8262,8 @@ bool SwiftASTContext::GetImplicitImports(
82688262
return true;
82698263
}
82708264

8271-
void SwiftASTContext::LoadImplicitModules(TargetSP target, ProcessSP process,
8272-
ExecutionContextScope &exe_scope) {
8265+
void SwiftASTContextForExpressions::LoadImplicitModules(
8266+
TargetSP target, ProcessSP process, ExecutionContextScope &exe_scope) {
82738267
auto load_module = [&](ConstString module_name) {
82748268
SourceModule module_info;
82758269
module_info.path.push_back(module_name);
@@ -8282,8 +8276,6 @@ void SwiftASTContext::LoadImplicitModules(TargetSP target, ProcessSP process,
82828276
return;
82838277
}
82848278

8285-
auto *persistent_expression_state =
8286-
target->GetSwiftPersistentExpressionState(exe_scope);
82878279
swift::ModuleDecl *module = GetModule(module_info, err);
82888280
if (err.Fail()) {
82898281
LOG_PRINTF(
@@ -8293,25 +8285,17 @@ void SwiftASTContext::LoadImplicitModules(TargetSP target, ProcessSP process,
82938285
return;
82948286
}
82958287

8296-
persistent_expression_state->AddHandLoadedModule(
8297-
module_name, swift::ImportedModule(module));
8288+
AddHandLoadedModule(module_name, swift::ImportedModule(module));
82988289
};
82998290

83008291
load_module(ConstString(swift::SWIFT_STRING_PROCESSING_NAME));
83018292
load_module(ConstString(swift::SWIFT_CONCURRENCY_NAME));
83028293
}
83038294

8304-
bool SwiftASTContext::CacheUserImports(SwiftASTContext &swift_ast_context,
8305-
SymbolContext &sc,
8306-
ExecutionContextScope &exe_scope,
8307-
lldb::ProcessSP process_sp,
8308-
swift::SourceFile &source_file,
8309-
Status &error) {
8295+
bool SwiftASTContextForExpressions::CacheUserImports(
8296+
lldb::ProcessSP process_sp, swift::SourceFile &source_file, Status &error) {
83108297
llvm::SmallString<1> m_description;
83118298

8312-
auto *persistent_expression_state =
8313-
sc.target_sp->GetSwiftPersistentExpressionState(exe_scope);
8314-
83158299
auto src_file_imports = source_file.getImports();
83168300

83178301
Progress progress(llvm::formatv("Caching Swift user imports from '{0}'",
@@ -8347,13 +8331,12 @@ bool SwiftASTContext::CacheUserImports(SwiftASTContext &swift_ast_context,
83478331
LOG_PRINTF(GetLog(LLDBLog::Types | LLDBLog::Expressions),
83488332
"Performing auto import on found module: %s.\n",
83498333
module_name.c_str());
8350-
if (!LoadOneModule(module_info, swift_ast_context, process_sp,
8334+
if (!LoadOneModule(module_info, *this, process_sp,
83518335
/*import_dylibs=*/true, error))
83528336
return false;
83538337

83548338
// How do we tell we are in REPL or playground mode?
8355-
persistent_expression_state->AddHandLoadedModule(module_const_str,
8356-
attributed_import);
8339+
AddHandLoadedModule(module_const_str, attributed_import);
83578340
}
83588341
}
83598342
}

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

Lines changed: 38 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
#include "lldb/Expression/DiagnosticManager.h"
2323
#include "lldb/Utility/Either.h"
2424

25+
#include "swift/AST/Import.h"
26+
#include "swift/AST/Module.h"
2527
#include "swift/Parse/ParseVersion.h"
2628
#include "swift/SymbolGraphGen/SymbolGraphOptions.h"
2729

@@ -806,30 +808,6 @@ class SwiftASTContext : public TypeSystemSwift {
806808

807809
CompilerType GetReferentType(lldb::opaque_compiler_type_t type) override;
808810

809-
/// Retrieves the modules that need to be implicitly imported in a given
810-
/// execution scope. This includes the modules imported by both the compile
811-
/// unit as well as any imports from previous expression evaluations.
812-
static bool GetImplicitImports(
813-
SwiftASTContext &swift_ast_context, SymbolContext &sc,
814-
ExecutionContextScope &exe_scope, lldb::ProcessSP process_sp,
815-
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
816-
&modules,
817-
Status &error);
818-
819-
// FIXME: the correct thing to do would be to get the modules by calling
820-
// CompilerInstance::getImplicitImportInfo, instead of loading these
821-
// modules manually. However, we currently don't have access to a
822-
// CompilerInstance, which is why this function is needed.
823-
void LoadImplicitModules(lldb::TargetSP target, lldb::ProcessSP process,
824-
ExecutionContextScope &exe_scope);
825-
/// Cache the user's imports from a SourceFile in a given execution scope such
826-
/// that they are carried over into future expression evaluations.
827-
static bool CacheUserImports(SwiftASTContext &swift_ast_context,
828-
SymbolContext &sc,
829-
ExecutionContextScope &exe_scope,
830-
lldb::ProcessSP process_sp,
831-
swift::SourceFile &source_file, Status &error);
832-
833811
/// Retrieve/import the modules imported by the compilation
834812
/// unit. Early-exists with false if there was an import failure.
835813
bool GetCompileUnitImports(
@@ -1037,6 +1015,42 @@ class SwiftASTContextForExpressions : public SwiftASTContext {
10371015

10381016
void ModulesDidLoad(ModuleList &module_list);
10391017

1018+
typedef llvm::StringMap<swift::AttributedImport<swift::ImportedModule>>
1019+
HandLoadedModuleSet;
1020+
1021+
// Insert to the list of hand-loaded modules, (no actual loading occurs).
1022+
void AddHandLoadedModule(
1023+
ConstString module_name,
1024+
swift::AttributedImport<swift::ImportedModule> attributed_import) {
1025+
m_hand_loaded_modules.insert_or_assign(module_name.GetStringRef(),
1026+
attributed_import);
1027+
}
1028+
1029+
/// Retrieves the modules that need to be implicitly imported in a given
1030+
/// execution scope. This includes the modules imported by both the compile
1031+
/// unit as well as any imports from previous expression evaluations.
1032+
bool GetImplicitImports(
1033+
SymbolContext &sc, lldb::ProcessSP process_sp,
1034+
llvm::SmallVectorImpl<swift::AttributedImport<swift::ImportedModule>>
1035+
&modules,
1036+
Status &error);
1037+
1038+
// FIXME: the correct thing to do would be to get the modules by calling
1039+
// CompilerInstance::getImplicitImportInfo, instead of loading these
1040+
// modules manually. However, we currently don't have access to a
1041+
// CompilerInstance, which is why this function is needed.
1042+
void LoadImplicitModules(lldb::TargetSP target, lldb::ProcessSP process,
1043+
ExecutionContextScope &exe_scope);
1044+
/// Cache the user's imports from a SourceFile in a given execution scope such
1045+
/// that they are carried over into future expression evaluations.
1046+
bool CacheUserImports(lldb::ProcessSP process_sp,
1047+
swift::SourceFile &source_file, Status &error);
1048+
1049+
protected:
1050+
/// These are the names of modules that we have loaded by hand into
1051+
/// the Contexts we make for parsing.
1052+
HandLoadedModuleSet m_hand_loaded_modules;
1053+
10401054
private:
10411055
std::unique_ptr<SwiftPersistentExpressionState> m_persistent_state_up;
10421056
};

0 commit comments

Comments
 (0)