Skip to content

Commit 71e3bfa

Browse files
committed
Thread a SymbolContext through GetSwiftASTContext. (NFC)
1 parent c44c3e9 commit 71e3bfa

17 files changed

+195
-149
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,9 +191,9 @@ swift::BraceStmt *SwiftASTManipulatorBase::GetUserBody() {
191191

192192
SwiftASTManipulator::SwiftASTManipulator(
193193
SwiftASTContextForExpressions &swift_ast_ctx,
194-
swift::SourceFile &source_file, bool repl,
194+
swift::SourceFile &source_file, const SymbolContext &sc, bool repl,
195195
lldb::BindGenericTypes bind_generic_types)
196-
: SwiftASTManipulatorBase(source_file, repl, bind_generic_types),
196+
: SwiftASTManipulatorBase(source_file, sc, repl, bind_generic_types),
197197
m_swift_ast_ctx(swift_ast_ctx) {}
198198

199199
void SwiftASTManipulator::FindSpecialNames(
@@ -889,7 +889,7 @@ llvm::Optional<swift::Type> SwiftASTManipulator::GetSwiftTypeForVariable(
889889
// When injecting a value pack or pack count into the outer
890890
// lldb_expr function, treat it as an opaque raw pointer.
891891
if (m_bind_generic_types == lldb::eDontBind && variable.IsUnboundPack()) {
892-
auto swift_ast_ctx = type_system_swift->GetSwiftASTContext();
892+
auto swift_ast_ctx = type_system_swift->GetSwiftASTContext(&m_sc);
893893
if (swift_ast_ctx) {
894894
auto it = m_type_aliases.find("$__lldb_builtin_ptr_t");
895895
if (it == m_type_aliases.end())

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,10 @@ class SwiftASTManipulatorBase {
146146
VariableMetadataSP m_metadata;
147147
};
148148

149-
SwiftASTManipulatorBase(swift::SourceFile &source_file, bool repl,
149+
SwiftASTManipulatorBase(swift::SourceFile &source_file,
150+
const SymbolContext &sc, bool repl,
150151
lldb::BindGenericTypes bind_generic_types)
151-
: m_source_file(source_file), m_variables(), m_repl(repl),
152+
: m_source_file(source_file), m_sc(sc), m_repl(repl),
152153
m_bind_generic_types(bind_generic_types) {
153154
DoInitialization();
154155
}
@@ -168,7 +169,7 @@ class SwiftASTManipulatorBase {
168169
protected:
169170
swift::SourceFile &m_source_file;
170171
llvm::SmallVector<VariableInfo, 1> m_variables;
171-
172+
const SymbolContext &m_sc;
172173
bool m_repl = false;
173174

174175
lldb::BindGenericTypes m_bind_generic_types = lldb::eBindAuto;
@@ -196,8 +197,8 @@ class SwiftASTManipulatorBase {
196197
class SwiftASTManipulator : public SwiftASTManipulatorBase {
197198
public:
198199
SwiftASTManipulator(SwiftASTContextForExpressions &swift_ast_ctx,
199-
swift::SourceFile &source_file, bool repl,
200-
lldb::BindGenericTypes bind_generic_types);
200+
swift::SourceFile &source_file, const SymbolContext &sc,
201+
bool repl, lldb::BindGenericTypes bind_generic_types);
201202
SwiftASTContextForExpressions &GetScratchContext() { return m_swift_ast_ctx; }
202203

203204
void FindSpecialNames(llvm::SmallVectorImpl<swift::Identifier> &names,

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1416,7 +1416,8 @@ static llvm::Expected<ParsedExpression> ParseAndImport(
14161416
std::unique_ptr<SwiftASTManipulator> code_manipulator;
14171417
if (repl || !playground) {
14181418
code_manipulator = std::make_unique<SwiftASTManipulator>(
1419-
swift_ast_context, *source_file, repl, options.GetBindGenericTypes());
1419+
swift_ast_context, *source_file, sc, repl,
1420+
options.GetBindGenericTypes());
14201421

14211422
if (!playground) {
14221423
code_manipulator->RewriteResult();

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,13 +563,12 @@ void SwiftREPL::CompleteCode(const std::string &current_code,
563563
llvm::consumeError(type_system_or_err.takeError());
564564
return;
565565
}
566-
567566
auto *swift_ts =
568567
llvm::dyn_cast_or_null<TypeSystemSwiftTypeRefForExpressions>(
569568
type_system_or_err->get());
570569
auto *target_swift_ast =
571570
llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
572-
swift_ts->GetSwiftASTContext());
571+
swift_ts->GetSwiftASTContext(nullptr));
573572
m_swift_ast = target_swift_ast;
574573
}
575574
SwiftASTContextForExpressions *swift_ast = m_swift_ast;

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,8 +695,10 @@ bool SwiftUserExpression::Parse(DiagnosticManager &diagnostic_manager,
695695
return error("could not create a Swift scratch context: ",
696696
m_err.AsCString());
697697

698+
const SymbolContext *sc =
699+
&frame->GetSymbolContext(lldb::eSymbolContextFunction);
698700
m_swift_ast_ctx = llvm::dyn_cast_or_null<SwiftASTContextForExpressions>(
699-
m_swift_scratch_ctx->get()->GetSwiftASTContext());
701+
m_swift_scratch_ctx->get()->GetSwiftASTContext(sc));
700702

701703
if (!m_swift_ast_ctx)
702704
return error("could not create a Swift AST context");

lldb/source/Plugins/InstrumentationRuntime/MainThreadChecker/InstrumentationRuntimeMainThreadChecker.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,10 @@ static std::string TranslateObjCNameToSwiftName(std::string className,
9999
auto *ts = llvm::dyn_cast_or_null<TypeSystemSwift>(type_system_or_err->get());
100100
if (!ts)
101101
return "";
102-
auto *ctx = ts->GetSwiftASTContext();
102+
const SymbolContext *sc = nullptr;
103+
if (swiftFrame)
104+
sc = &swiftFrame->GetSymbolContext(eSymbolContextFunction);
105+
auto *ctx = ts->GetSwiftASTContext(sc);
103106
if (!ctx)
104107
return "";
105108
swift::ClangImporter *imp = ctx->GetClangImporter();

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

Lines changed: 71 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,10 +1326,13 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13261326
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
13271327
target->GetSwiftScratchContext(error, *exe_scope,
13281328
create_on_demand);
1329+
const SymbolContext *sc = nullptr;
1330+
if (auto frame_sp = exe_scope->CalculateStackFrame())
1331+
sc = &frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
13291332
if (maybe_scratch_ctx)
13301333
if (auto scratch_ctx = maybe_scratch_ctx->get())
13311334
if (SwiftASTContext *ast_ctx =
1332-
scratch_ctx->GetSwiftASTContext()) {
1335+
scratch_ctx->GetSwiftASTContext(sc)) {
13331336
ConstString cs_input{input};
13341337
Mangled mangled(cs_input);
13351338
if (mangled.GuessLanguage() == eLanguageTypeSwift) {
@@ -1388,73 +1391,76 @@ std::unique_ptr<Language::TypeScavenger> SwiftLanguage::GetTypeScavenger() {
13881391
llvm::Optional<SwiftScratchContextReader> maybe_scratch_ctx =
13891392
target->GetSwiftScratchContext(error, *exe_scope,
13901393
create_on_demand);
1391-
if (maybe_scratch_ctx)
1392-
if (auto scratch_ctx = maybe_scratch_ctx->get())
1393-
if (SwiftASTContext *ast_ctx =
1394-
scratch_ctx->GetSwiftASTContext()) {
1395-
auto iter = ast_ctx->GetModuleCache().begin(),
1396-
end = ast_ctx->GetModuleCache().end();
1397-
1398-
std::vector<llvm::StringRef> name_parts;
1399-
SplitDottedName(input, name_parts);
1400-
1401-
std::function<void(swift::ModuleDecl *)> lookup_func =
1402-
[&ast_ctx, input, name_parts,
1403-
&results](swift::ModuleDecl *module) -> void {
1404-
for (auto imported_module :
1405-
swift::namelookup::getAllImports(module)) {
1406-
auto module = imported_module.importedModule;
1407-
TypesOrDecls local_results;
1408-
ast_ctx->FindTypesOrDecls(input, module, local_results,
1409-
false);
1410-
llvm::Optional<TypeOrDecl> candidate;
1411-
if (local_results.empty() && name_parts.size() > 1) {
1412-
size_t idx_of_deeper = 1;
1413-
// if you're looking for Swift.Int in module Swift,
1414-
// try looking for Int
1415-
if (name_parts.front() == module->getName().str()) {
1416-
candidate = ast_ctx->FindTypeOrDecl(
1417-
name_parts[1].str().c_str(), module);
1418-
idx_of_deeper = 2;
1419-
}
1420-
// this is probably the top-level name of a nested
1421-
// type String.UTF8View
1422-
else {
1423-
candidate = ast_ctx->FindTypeOrDecl(
1424-
name_parts[0].str().c_str(), module);
1425-
}
1426-
if (candidate.has_value()) {
1427-
TypesOrDecls candidates{candidate.value()};
1428-
for (; idx_of_deeper < name_parts.size();
1429-
idx_of_deeper++) {
1430-
TypesOrDecls new_candidates;
1431-
for (auto candidate : candidates) {
1432-
ast_ctx->FindContainedTypeOrDecl(
1433-
name_parts[idx_of_deeper], candidate,
1434-
new_candidates);
1435-
}
1436-
candidates = new_candidates;
1437-
}
1394+
const SymbolContext *sc = nullptr;
1395+
if (auto frame_sp = exe_scope->CalculateStackFrame())
1396+
sc = &frame_sp->GetSymbolContext(lldb::eSymbolContextFunction);
1397+
1398+
if (maybe_scratch_ctx)
1399+
if (auto scratch_ctx = maybe_scratch_ctx->get())
1400+
if (SwiftASTContext *ast_ctx =
1401+
scratch_ctx->GetSwiftASTContext(sc)) {
1402+
auto iter = ast_ctx->GetModuleCache().begin(),
1403+
end = ast_ctx->GetModuleCache().end();
1404+
1405+
std::vector<llvm::StringRef> name_parts;
1406+
SplitDottedName(input, name_parts);
1407+
1408+
std::function<void(swift::ModuleDecl *)> lookup_func =
1409+
[&ast_ctx, input, name_parts,
1410+
&results](swift::ModuleDecl *module) -> void {
1411+
for (auto imported_module :
1412+
swift::namelookup::getAllImports(module)) {
1413+
auto module = imported_module.importedModule;
1414+
TypesOrDecls local_results;
1415+
ast_ctx->FindTypesOrDecls(input, module, local_results,
1416+
false);
1417+
llvm::Optional<TypeOrDecl> candidate;
1418+
if (local_results.empty() && name_parts.size() > 1) {
1419+
size_t idx_of_deeper = 1;
1420+
// if you're looking for Swift.Int in module Swift,
1421+
// try looking for Int
1422+
if (name_parts.front() == module->getName().str()) {
1423+
candidate = ast_ctx->FindTypeOrDecl(
1424+
name_parts[1].str().c_str(), module);
1425+
idx_of_deeper = 2;
1426+
}
1427+
// this is probably the top-level name of a nested
1428+
// type String.UTF8View
1429+
else {
1430+
candidate = ast_ctx->FindTypeOrDecl(
1431+
name_parts[0].str().c_str(), module);
1432+
}
1433+
if (candidate.has_value()) {
1434+
TypesOrDecls candidates{candidate.value()};
1435+
for (; idx_of_deeper < name_parts.size();
1436+
idx_of_deeper++) {
1437+
TypesOrDecls new_candidates;
14381438
for (auto candidate : candidates) {
1439-
if (candidate)
1440-
results.insert(candidate);
1439+
ast_ctx->FindContainedTypeOrDecl(
1440+
name_parts[idx_of_deeper], candidate,
1441+
new_candidates);
14411442
}
1443+
candidates = new_candidates;
14421444
}
1443-
} else if (local_results.size() > 0) {
1444-
for (const auto &result : local_results)
1445-
results.insert(result);
1446-
} else if (local_results.empty() && module &&
1447-
name_parts.size() == 1 &&
1448-
name_parts.front() ==
1449-
module->getName().str())
1450-
results.insert(
1451-
ToCompilerType(swift::ModuleType::get(module)));
1452-
}
1453-
};
1454-
1455-
for (; iter != end; iter++)
1456-
lookup_func(iter->second);
1457-
}
1445+
for (auto candidate : candidates) {
1446+
if (candidate)
1447+
results.insert(candidate);
1448+
}
1449+
}
1450+
} else if (local_results.size() > 0) {
1451+
for (const auto &result : local_results)
1452+
results.insert(result);
1453+
} else if (local_results.empty() && module &&
1454+
name_parts.size() == 1 &&
1455+
name_parts.front() == module->getName().str())
1456+
results.insert(
1457+
ToCompilerType(swift::ModuleType::get(module)));
1458+
}
1459+
};
1460+
1461+
for (; iter != end; iter++)
1462+
lookup_func(iter->second);
1463+
}
14581464
}
14591465

14601466
return (results.size() - before);

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntime.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1232,7 +1232,7 @@ void SwiftLanguageRuntime::FindFunctionPointersInCall(
12321232
target.GetSwiftScratchContext(error, frame);
12331233
auto scratch_ctx = maybe_swift_ast->get();
12341234
if (scratch_ctx) {
1235-
if (SwiftASTContext *swift_ast = scratch_ctx->GetSwiftASTContext()) {
1235+
if (SwiftASTContext *swift_ast = scratch_ctx->GetSwiftASTContext(&sc)) {
12361236
CompilerType function_type = swift_ast->GetTypeFromMangledTypename(
12371237
mangled_name.GetMangledName());
12381238
if (error.Success()) {
@@ -1444,7 +1444,9 @@ SwiftLanguageRuntime::CalculateErrorValue(StackFrameSP frame_sp,
14441444
auto scratch_ctx = maybe_scratch_context->get();
14451445
if (!scratch_ctx)
14461446
return error_valobj_sp;
1447-
SwiftASTContext *ast_context = scratch_ctx->GetSwiftASTContext();
1447+
1448+
const SymbolContext *sc = &frame_sp->GetSymbolContext(eSymbolContextFunction);
1449+
SwiftASTContext *ast_context = scratch_ctx->GetSwiftASTContext(sc);
14481450
if (!ast_context)
14491451
return error_valobj_sp;
14501452

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeDynamicTypeResolution.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2780,7 +2780,8 @@ SwiftLanguageRuntimeImpl::GetConcreteType(ExecutionContextScope *exe_scope,
27802780
if (!promise_sp)
27812781
return CompilerType();
27822782

2783-
return promise_sp->FulfillTypePromise();
2783+
const SymbolContext *sc = &frame->GetSymbolContext(eSymbolContextFunction);
2784+
return promise_sp->FulfillTypePromise(sc);
27842785
}
27852786

27862787
} // namespace lldb_private

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeImpl.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,13 @@ class SwiftLanguageRuntimeImpl {
291291
llvm::Optional<CompilerType> m_compiler_type;
292292

293293
public:
294-
CompilerType FulfillTypePromise(Status *error = nullptr);
294+
CompilerType FulfillTypePromise(const SymbolContext *sc,
295+
Status *error = nullptr);
295296
};
296297
typedef std::shared_ptr<MetadataPromise> MetadataPromiseSP;
297298

298-
MetadataPromiseSP GetMetadataPromise(lldb::addr_t addr,
299+
MetadataPromiseSP GetMetadataPromise(const SymbolContext *sc,
300+
lldb::addr_t addr,
299301
ValueObject &for_object);
300302
MetadataPromiseSP
301303
GetPromiseForTypeNameAndFrame(const char *type_name, StackFrame *frame);

lldb/source/Plugins/LanguageRuntime/Swift/SwiftLanguageRuntimeRemoteAST.cpp

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ ConstString SwiftLanguageRuntimeImpl::GetDynamicTypeName_ClassRemoteAST(
186186
auto scratch_ctx = maybe_scratch_ctx->get();
187187
if (!scratch_ctx)
188188
return {};
189-
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
189+
const SymbolContext *sc = nullptr;
190+
auto stack_frame_sp = in_value.GetExecutionContextRef().GetFrameSP();
191+
if (stack_frame_sp)
192+
sc = &stack_frame_sp->GetSymbolContext(eSymbolContextFunction);
193+
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext(sc);
190194
if (!swift_ast_ctx)
191195
return {};
192196

@@ -223,7 +227,12 @@ SwiftLanguageRuntimeImpl::GetDynamicTypeAndAddress_ProtocolRemoteAST(
223227
auto scratch_ctx = maybe_scratch_ctx->get();
224228
if (!scratch_ctx)
225229
return {};
226-
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
230+
231+
const SymbolContext *sc = nullptr;
232+
auto stack_frame_sp = in_value.GetExecutionContextRef().GetFrameSP();
233+
if (stack_frame_sp)
234+
sc = &stack_frame_sp->GetSymbolContext(eSymbolContextFunction);
235+
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext(sc);
227236
if (!swift_ast_ctx)
228237
return {};
229238

@@ -279,7 +288,8 @@ CompilerType SwiftLanguageRuntimeImpl::BindGenericTypeParametersRemoteAST(
279288
auto scratch_ctx = maybe_scratch_ctx->get();
280289
if (!scratch_ctx)
281290
return base_type;
282-
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
291+
292+
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext(&sc);
283293
if (!swift_ast_ctx)
284294
return base_type;
285295
base_type = swift_ast_ctx->ImportType(base_type, error);
@@ -430,8 +440,8 @@ SwiftLanguageRuntimeImpl::MetadataPromise::MetadataPromise(
430440
: m_for_object_sp(for_object.GetSP()), m_swift_runtime(runtime),
431441
m_metadata_location(location) {}
432442

433-
CompilerType
434-
SwiftLanguageRuntimeImpl::MetadataPromise::FulfillTypePromise(Status *error) {
443+
CompilerType SwiftLanguageRuntimeImpl::MetadataPromise::FulfillTypePromise(
444+
const SymbolContext *sc, Status *error) {
435445
if (error)
436446
error->Clear();
437447

@@ -456,7 +466,7 @@ SwiftLanguageRuntimeImpl::MetadataPromise::FulfillTypePromise(Status *error) {
456466
error->SetErrorString("couldn't get Swift scratch context");
457467
return CompilerType();
458468
}
459-
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
469+
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext(sc);
460470
if (!swift_ast_ctx) {
461471
error->SetErrorString("couldn't get Swift scratch context");
462472
return CompilerType();
@@ -485,7 +495,8 @@ SwiftLanguageRuntimeImpl::MetadataPromise::FulfillTypePromise(Status *error) {
485495
}
486496

487497
SwiftLanguageRuntimeImpl::MetadataPromiseSP
488-
SwiftLanguageRuntimeImpl::GetMetadataPromise(lldb::addr_t addr,
498+
SwiftLanguageRuntimeImpl::GetMetadataPromise(const SymbolContext *sc,
499+
lldb::addr_t addr,
489500
ValueObject &for_object) {
490501
llvm::Optional<SwiftScratchContextReader> maybe_swift_scratch_ctx =
491502
for_object.GetSwiftScratchContext();
@@ -494,7 +505,7 @@ SwiftLanguageRuntimeImpl::GetMetadataPromise(lldb::addr_t addr,
494505
auto scratch_ctx = maybe_swift_scratch_ctx->get();
495506
if (!scratch_ctx)
496507
return nullptr;
497-
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext();
508+
SwiftASTContext *swift_ast_ctx = scratch_ctx->GetSwiftASTContext(sc);
498509
if (!swift_ast_ctx)
499510
return nullptr;
500511
if (swift_ast_ctx->HasFatalErrors())
@@ -539,7 +550,8 @@ SwiftLanguageRuntimeImpl::GetPromiseForTypeNameAndFrame(const char *type_name,
539550
lldb::addr_t metadata_location(metadata_ptr_var_sp->GetValueAsUnsigned(0));
540551
if (metadata_location == 0 || metadata_location == LLDB_INVALID_ADDRESS)
541552
return nullptr;
542-
return GetMetadataPromise(metadata_location, *metadata_ptr_var_sp);
553+
const SymbolContext *sc = &frame->GetSymbolContext(eSymbolContextFunction);
554+
return GetMetadataPromise(sc, metadata_location, *metadata_ptr_var_sp);
543555
}
544556

545557
} // namespace lldb_private

0 commit comments

Comments
 (0)