Skip to content

Commit 6e747cb

Browse files
committed
Store CompilerDecls in SwiftPersistenExpressionState::SwiftDeclMap (NFC)
A CompilerDecl is a pair of TypeSystem and Decl *. This is in preparation for a later patch.
1 parent 8ca10dc commit 6e747cb

File tree

6 files changed

+67
-48
lines changed

6 files changed

+67
-48
lines changed

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

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ static CompilerType ImportType(SwiftASTContextForExpressions &target_context,
125125
return target_context.ImportType(source_type, error);
126126
}
127127

128+
static CompilerDecl GetCompilerDecl(swift::Decl *decl) {
129+
return {SwiftASTContext::GetSwiftASTContext(&decl->getASTContext()), decl};
130+
}
131+
128132
namespace {
129133
class LLDBNameLookup : public swift::SILDebuggerClient {
130134
public:
@@ -219,7 +223,7 @@ class LLDBExprNameLookup : public LLDBNameLookup {
219223
// kind lldb explicitly wants to globalize.
220224
if (shouldGlobalize(value_decl->getBaseName().getIdentifier(),
221225
value_decl->getKind()))
222-
m_staged_decls.AddDecl(value_decl, false, {});
226+
m_staged_decls.AddDecl(GetCompilerDecl(value_decl), false, {});
223227
}
224228
}
225229

@@ -253,11 +257,11 @@ class LLDBExprNameLookup : public LLDBNameLookup {
253257
"[LLDBExprNameLookup::lookupAdditions (%u)] Searching for %s", count,
254258
NameStr.empty() ? "<anonymous>" : NameStr.str().c_str());
255259

256-
std::vector<swift::ValueDecl *> results;
260+
std::vector<CompilerDecl> results;
257261

258262
for (auto *alias : m_type_aliases) {
259263
if (alias->getName().str() == NameStr) {
260-
results.push_back(alias);
264+
results.push_back(GetCompilerDecl(alias));
261265
break;
262266
}
263267
}
@@ -292,13 +296,18 @@ class LLDBExprNameLookup : public LLDBNameLookup {
292296

293297
size_t num_external_results = RV.size();
294298
if (!is_debugger_variable && num_external_results > 0) {
295-
std::vector<swift::ValueDecl *> persistent_results;
299+
std::vector<CompilerDecl> persistent_results;
296300
m_persistent_vars->GetSwiftPersistentDecls(NameStr, {},
297301
persistent_results);
298302

299-
size_t num_persistent_results = persistent_results.size();
300-
for (size_t idx = 0; idx < num_persistent_results; idx++) {
301-
swift::ValueDecl *value_decl = persistent_results[idx];
303+
for (CompilerDecl & decl : persistent_results) {
304+
if (decl.GetTypeSystem() !=
305+
SwiftASTContext::GetSwiftASTContext(&DC->getASTContext())) {
306+
LLDB_LOG(m_log, "ignoring persistent result from other context");
307+
continue;
308+
}
309+
auto *value_decl =
310+
static_cast<swift::ValueDecl *>(decl.GetOpaqueDecl());
302311
if (!value_decl)
303312
continue;
304313
swift::DeclName value_decl_name = value_decl->getName();
@@ -332,15 +341,15 @@ class LLDBExprNameLookup : public LLDBNameLookup {
332341
}
333342
}
334343
if (!skip_it)
335-
results.push_back(value_decl);
344+
results.push_back(decl);
336345
}
337346
} else {
338347
m_persistent_vars->GetSwiftPersistentDecls(NameStr, results, results);
339348
}
340349
}
341350

342-
for (size_t idx = 0; idx < results.size(); idx++) {
343-
swift::ValueDecl *value_decl = results[idx];
351+
for (CompilerDecl &decl : results) {
352+
auto *value_decl = static_cast<swift::ValueDecl *>(decl.GetOpaqueDecl());
344353
// No import required.
345354
assert(&DC->getASTContext() == &value_decl->getASTContext());
346355
RV.push_back(swift::LookupResultEntry(value_decl));
@@ -405,26 +414,28 @@ class LLDBREPLNameLookup : public LLDBNameLookup {
405414
NameStr.empty() ? "<anonymous>" : NameStr.str().c_str());
406415

407416
// Find decls that come from the current compilation.
408-
std::vector<swift::ValueDecl *> current_compilation_results;
417+
std::vector<CompilerDecl> current_compilation_results;
409418
for (auto result : RV) {
410419
auto result_decl = result.getValueDecl();
411420
auto result_decl_context = result_decl->getDeclContext();
412421
if (result_decl_context->isChildContextOf(DC) || result_decl_context == DC)
413-
current_compilation_results.push_back(result_decl);
422+
current_compilation_results.push_back(GetCompilerDecl(result_decl));
414423
}
415424

416425
// Find persistent decls, excluding decls that are equivalent to
417426
// decls from the current compilation. This makes the decls from
418427
// the current compilation take precedence.
419-
std::vector<swift::ValueDecl *> persistent_decl_results;
428+
std::vector<CompilerDecl> persistent_decl_results;
420429
m_persistent_vars->GetSwiftPersistentDecls(
421430
NameStr, current_compilation_results, persistent_decl_results);
422431

423432
// Append the persistent decls that we found to the result vector.
424433
for (auto result : persistent_decl_results) {
425434
// No import required.
426-
assert(&DC->getASTContext() == &result->getASTContext());
427-
RV.push_back(swift::LookupResultEntry(result));
435+
auto *result_decl =
436+
static_cast<swift::ValueDecl *>(result.GetOpaqueDecl());
437+
assert(&DC->getASTContext() == &result_decl->getASTContext());
438+
RV.push_back(swift::LookupResultEntry(result_decl));
428439
}
429440

430441
return !persistent_decl_results.empty();
@@ -1836,16 +1847,15 @@ SwiftExpressionParser::Parse(DiagnosticManager &diagnostic_manager,
18361847
persistent_variable));
18371848

18381849
// This is only exercised by the PlaygroundsREPL tests.
1839-
persistent_state->RegisterSwiftPersistentDecl(decl);
1850+
persistent_state->RegisterSwiftPersistentDecl(GetCompilerDecl(decl));
18401851
}
18411852

18421853
if (repl) {
18431854
llvm::SmallVector<swift::ValueDecl *, 1> non_variables;
18441855
parsed_expr->code_manipulator->FindNonVariableDeclarations(non_variables);
18451856

1846-
for (swift::ValueDecl *decl : non_variables) {
1847-
persistent_state->RegisterSwiftPersistentDecl(decl);
1848-
}
1857+
for (swift::ValueDecl *decl : non_variables)
1858+
persistent_state->RegisterSwiftPersistentDecl(GetCompilerDecl(decl));
18491859
}
18501860
}
18511861

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -617,7 +617,7 @@ Status SwiftExpressionSourceCode::GetText(
617617
status.SetErrorString("no persistent state");
618618
return status;
619619
}
620-
std::vector<swift::ValueDecl *> persistent_results;
620+
std::vector<CompilerDecl> persistent_results;
621621
// Check if we have already declared the playground stub debug functions
622622
persistent_state->GetSwiftPersistentDecls("__builtin_log_with_id", {},
623623
persistent_results);

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

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ SwiftPersistentExpressionState::GetCompilerTypeFromPersistentDecl(
8989
}
9090

9191
bool SwiftPersistentExpressionState::SwiftDeclMap::DeclsAreEquivalent(
92-
swift::Decl *lhs_decl, swift::Decl *rhs_decl) {
92+
CompilerDecl lhs, CompilerDecl rhs) {
93+
if (lhs.GetTypeSystem() != rhs.GetTypeSystem())
94+
return false;
95+
auto *lhs_decl = static_cast<swift::Decl *>(lhs.GetOpaqueDecl());
96+
auto *rhs_decl = static_cast<swift::Decl *>(rhs.GetOpaqueDecl());
9397
swift::DeclKind lhs_kind = lhs_decl->getKind();
9498
swift::DeclKind rhs_kind = rhs_decl->getKind();
9599
if (lhs_kind != rhs_kind)
@@ -129,45 +133,45 @@ bool SwiftPersistentExpressionState::SwiftDeclMap::DeclsAreEquivalent(
129133
}
130134

131135
void SwiftPersistentExpressionState::SwiftDeclMap::AddDecl(
132-
swift::ValueDecl *value_decl, bool check_existing, llvm::StringRef alias) {
133-
llvm::StringRef name;
136+
CompilerDecl decl, bool check_existing, llvm::StringRef alias) {
137+
auto *value_decl = static_cast<swift::ValueDecl *>(decl.GetOpaqueDecl());
134138

139+
llvm::StringRef name;
135140
if (alias.empty())
136141
name = value_decl->getBaseName().getIdentifier().str();
137142
else
138143
name = alias;
139144

140145
auto it = m_swift_decls.find(name);
141146
if (it == m_swift_decls.end()) {
142-
m_swift_decls.insert({name, {value_decl}});
147+
m_swift_decls.insert({name, {decl}});
143148
return;
144149
}
145150

146-
llvm::SmallVectorImpl<swift::ValueDecl *> &decls = it->second;
151+
llvm::SmallVectorImpl<CompilerDecl> &decls = it->second;
147152
if (check_existing)
148153
decls.erase(std::remove_if(decls.begin(), decls.end(),
149-
[&value_decl](swift::ValueDecl *cur_decl) {
150-
return DeclsAreEquivalent(cur_decl,
151-
value_decl);
154+
[&decl](CompilerDecl cur_decl) {
155+
return DeclsAreEquivalent(cur_decl, decl);
152156
}),
153157
decls.end());
154158

155-
decls.push_back(value_decl);
159+
decls.push_back(decl);
156160
}
157161

158162
bool SwiftPersistentExpressionState::SwiftDeclMap::FindMatchingDecls(
159163
llvm::StringRef name,
160-
const std::vector<swift::ValueDecl *> &excluding_equivalents,
161-
std::vector<swift::ValueDecl *> &matches) {
164+
const std::vector<CompilerDecl> &excluding_equivalents,
165+
std::vector<CompilerDecl> &matches) {
162166
auto it = m_swift_decls.find(name);
163167
if (it == m_swift_decls.end())
164168
return false;
165-
llvm::SmallVectorImpl<swift::ValueDecl *> &decls = it->second;
169+
llvm::SmallVectorImpl<CompilerDecl> &decls = it->second;
166170

167171
size_t start_num_items = matches.size();
168172
for (auto &cur_decl : decls)
169173
if (std::none_of(excluding_equivalents.begin(), excluding_equivalents.end(),
170-
[&](swift::ValueDecl *decl) {
174+
[&](CompilerDecl decl) {
171175
return DeclsAreEquivalent(cur_decl, decl);
172176
}))
173177
matches.push_back(cur_decl);
@@ -183,12 +187,12 @@ void SwiftPersistentExpressionState::SwiftDeclMap::CopyDeclsTo(
183187
}
184188

185189
void SwiftPersistentExpressionState::RegisterSwiftPersistentDecl(
186-
swift::ValueDecl *value_decl) {
190+
CompilerDecl value_decl) {
187191
m_swift_persistent_decls.AddDecl(value_decl, true, {});
188192
}
189193

190194
void SwiftPersistentExpressionState::RegisterSwiftPersistentDeclAlias(
191-
swift::ValueDecl *value_decl, llvm::StringRef name) {
195+
CompilerDecl value_decl, llvm::StringRef name) {
192196
m_swift_persistent_decls.AddDecl(value_decl, true, name);
193197
}
194198

@@ -199,8 +203,8 @@ void SwiftPersistentExpressionState::CopyInSwiftPersistentDecls(
199203

200204
bool SwiftPersistentExpressionState::GetSwiftPersistentDecls(
201205
llvm::StringRef name,
202-
const std::vector<swift::ValueDecl *> &excluding_equivalents,
203-
std::vector<swift::ValueDecl *> &matches) {
206+
const std::vector<CompilerDecl> &excluding_equivalents,
207+
std::vector<CompilerDecl> &matches) {
204208
return m_swift_persistent_decls.FindMatchingDecls(name, excluding_equivalents,
205209
matches);
206210
}

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,22 +45,23 @@ class SwiftPersistentExpressionState : public PersistentExpressionState {
4545
public:
4646
class SwiftDeclMap {
4747
public:
48-
void AddDecl(swift::ValueDecl *decl, bool check_existing,
48+
void AddDecl(CompilerDecl value_decl, bool check_existing,
4949
llvm::StringRef name);
5050

5151
/// Find decls matching `name`, excluding decls that are equivalent to
5252
/// decls in `excluding_equivalents`, and put the results in `matches`.
5353
/// Return true if there are any results.
5454
bool FindMatchingDecls(
5555
llvm::StringRef name,
56-
const std::vector<swift::ValueDecl *> &excluding_equivalents,
57-
std::vector<swift::ValueDecl *> &matches);
56+
const std::vector<CompilerDecl> &excluding_equivalents,
57+
std::vector<CompilerDecl> &matches);
5858

5959
void CopyDeclsTo(SwiftDeclMap &target_map);
60-
static bool DeclsAreEquivalent(swift::Decl *lhs, swift::Decl *rhs);
60+
static bool DeclsAreEquivalent(CompilerDecl lhs, CompilerDecl rhs);
6161

6262
private:
63-
llvm::StringMap<llvm::SmallVector<swift::ValueDecl *, 1>> m_swift_decls;
63+
/// Each decl also stores the context it comes from.
64+
llvm::StringMap<llvm::SmallVector<CompilerDecl, 1>> m_swift_decls;
6465
};
6566

6667
//----------------------------------------------------------------------
@@ -97,9 +98,9 @@ class SwiftPersistentExpressionState : public PersistentExpressionState {
9798
llvm::Optional<CompilerType>
9899
GetCompilerTypeFromPersistentDecl(ConstString type_name) override;
99100

100-
void RegisterSwiftPersistentDecl(swift::ValueDecl *value_decl);
101+
void RegisterSwiftPersistentDecl(CompilerDecl value_decl);
101102

102-
void RegisterSwiftPersistentDeclAlias(swift::ValueDecl *value_decl,
103+
void RegisterSwiftPersistentDeclAlias(CompilerDecl value_decl,
103104
llvm::StringRef name);
104105

105106
void CopyInSwiftPersistentDecls(SwiftDeclMap &source_map);
@@ -109,8 +110,8 @@ class SwiftPersistentExpressionState : public PersistentExpressionState {
109110
/// if there are any results.
110111
bool GetSwiftPersistentDecls(
111112
llvm::StringRef name,
112-
const std::vector<swift::ValueDecl *> &excluding_equivalents,
113-
std::vector<swift::ValueDecl *> &matches);
113+
const std::vector<CompilerDecl> &excluding_equivalents,
114+
std::vector<CompilerDecl> &matches);
114115

115116
// This just adds this module to the list of hand-loaded modules, it doesn't
116117
// actually load it.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,11 @@ class EntityREPLResultVariable : public Materializer::Entity {
186186

187187
if (m_swift_decl) {
188188
llvm::cast<SwiftPersistentExpressionState>(persistent_state)
189-
->RegisterSwiftPersistentDeclAlias(m_swift_decl, name.GetStringRef());
189+
->RegisterSwiftPersistentDeclAlias(
190+
{SwiftASTContext::GetSwiftASTContext(
191+
&m_swift_decl->getASTContext()),
192+
m_swift_decl},
193+
name.GetStringRef());
190194
}
191195

192196
return;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1506,7 +1506,7 @@ void SwiftLanguageRuntime::RegisterGlobalError(Target &target, ConstString name,
15061506
if (!persistent_state)
15071507
return;
15081508

1509-
persistent_state->RegisterSwiftPersistentDecl(var_decl);
1509+
persistent_state->RegisterSwiftPersistentDecl({ast_context, var_decl});
15101510

15111511
ConstString mangled_name;
15121512

0 commit comments

Comments
 (0)