1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
//! The questions about the whole tree rather than one file: what a package
//! offers, who depends on a declaration, what nothing refers to, which
//! imports are worth a look. Each is a walk over the graph's fragments —
//! every file's placed declarations and resolved edges — and answers with
//! plain data, the uncertainty counted rather than hidden.
use std::collections::{BTreeMap, BTreeSet, HashSet};
use std::path::PathBuf;
use vvv_core::{Address, LanguageId, PackageId, Parsed};
use crate::command::{Command, Context};
use crate::graph::{Candidate, Declared, Node, Structure};
use crate::{
Confidence, Consumer, Dead, DeadQuery, EngineError, Exposed, Impact, ImpactQuery, ImportSite,
ImportsQuery, ImportsReport, Placed, Reach, ReferencesQuery, Surface, SurfaceQuery,
Unreferenced,
};
impl Placed {
/// A fragment's declaration as an answer names it: with its file and line.
fn of(candidate: &Candidate, declared: &Declared) -> Self {
Self {
path: candidate.path().into(),
symbol: declared.symbol.clone(),
start: candidate
.file()
.source()
.position(declared.symbol.name_span.start),
address: declared.address.clone(),
reach: declared.reach.clone(),
}
}
}
/// What a package offers to everyone: its `pub` declarations — a re-export
/// never widens what it re-exports — plus every address a re-export offers
/// them at, with how many files take them.
impl Command for SurfaceQuery {
type Output = Surface;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let package = self.package.as_deref().map(PackageId::new);
let mut items: Vec<Exposed> = Vec::new();
let structure = Structure::of(&mut cx.graph)?;
for (ns, fragments) in structure.iter() {
for Node {
candidate,
fragment,
} in fragments
{
for declared in &fragment.declarations {
if package
.as_ref()
.is_some_and(|p| declared.address.package() != p)
{
continue;
}
if declared.reach != Reach::Everyone {
continue;
}
// Every address a re-export chain offers it at, itself first.
let addresses =
cx.graph
.aliases_of(ns, &declared.address, &declared.symbol.name)?;
let importers = fragments
.iter()
.filter(|n| n.path() != candidate.path())
.filter(|n| {
n.fragment
.imports()
.any(|e| e.address.as_ref().is_some_and(|a| addresses.contains(a)))
})
.count();
items.push(Exposed {
declaration: Placed::of(candidate, declared),
via: addresses[1..].to_vec(),
importers,
});
}
}
}
items.sort_by(|a, b| a.declaration.address.cmp(&b.declaration.address));
Ok(Surface { package, items })
}
}
/// Who would feel a change to a declaration: the modules importing it (or
/// an address that re-exports it), then the modules importing those,
/// outward, each module once at the depth it is first reached.
impl Command for ImpactQuery {
type Output = Impact;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let graph = &mut *cx.graph;
let query = ReferencesQuery::new(self.name.as_str());
let query = match &self.declared_in {
Some(file) => query.declared_in(file),
None => query,
};
let evidence = graph.references(&query)?;
let declaration =
evidence
.declarations
.first()
.ok_or_else(|| EngineError::NoSuchSymbol {
name: self.name.clone(),
kind: None,
})?;
let ns = graph
.namespace(&declaration.language)
.ok_or_else(|| EngineError::NoLayout(declaration.language.clone()))?;
let address = declaration
.address
.clone()
.ok_or_else(|| EngineError::NoLayout(declaration.language.clone()))?;
let aliases = graph.aliases_of(&ns, &address, &self.name)?;
let fragments = graph.fragments(&ns)?;
// Breadth first over module imports: what reaches an address in the
// frontier joins the next ring.
let mut consumers: Vec<Consumer> = Vec::new();
let mut seen: HashSet<Address> = HashSet::new();
let mut frontier: Vec<Address> = aliases.clone();
let declaring = address.parent().unwrap_or_else(|| address.clone());
let mut depth = 1;
while !frontier.is_empty() && depth <= 8 {
let mut next = Vec::new();
for node in &fragments {
let Some(module) = &node.fragment.module else {
continue;
};
if seen.contains(module) || module == &declaring {
continue;
}
let through = node
.fragment
.imports()
.filter_map(|e| e.address.as_ref())
.find(|a| frontier.iter().any(|f| a.starts_with(f)))
.and_then(|a| {
frontier.iter().find(|f| a.starts_with(f)).map(|f| {
if depth == 1 {
declaring.clone()
} else {
f.clone()
}
})
});
if let Some(through) = through {
seen.insert(module.clone());
consumers.push(Consumer {
module: module.clone(),
path: node.path().into(),
depth,
through,
});
next.push(module.clone());
}
}
frontier = next;
depth += 1;
}
Ok(Impact {
name: self.name,
address,
consumers,
})
}
}
/// Declarations nothing in the workspace refers to — no resolved token
/// other than the declaration's own name — with how many tokens vvv could
/// not judge and so might be a use after all.
impl Command for DeadQuery {
type Output = Dead;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let graph = &mut *cx.graph;
let mut items = Vec::new();
let languages: Vec<LanguageId> = graph
.language_ids()
.into_iter()
.filter(|id| self.language.as_ref().is_none_or(|l| l == id))
.collect();
for id in languages {
let Some(ns) = graph.namespace(&id) else {
continue;
};
let fragments = graph.fragments(&ns)?;
let mut asked: BTreeSet<(PathBuf, String)> = BTreeSet::new();
for Node {
candidate,
fragment,
} in &fragments
{
for declared in &fragment.declarations {
// Pieces of one declaration (a type and its impls) are
// one question.
if !asked.insert((candidate.path().to_path_buf(), declared.symbol.name.clone()))
{
continue;
}
let query = ReferencesQuery::new(declared.symbol.name.as_str())
.in_language(id.clone())
.declared_in(candidate.path());
let evidence = graph.references(&query)?;
let own_names: Vec<_> = fragment
.declarations
.iter()
.filter(|d| d.symbol.name == declared.symbol.name)
.map(|d| d.symbol.name_span)
.collect();
let (mut used, mut unsure) = (false, 0);
for o in &evidence.occurrences {
let is_own_name =
o.m.path == candidate.path() && own_names.contains(&o.m.span);
match o.confidence {
Confidence::Resolved if !is_own_name => used = true,
Confidence::Unresolved => unsure += 1,
_ => {}
}
}
if !used {
items.push(Unreferenced {
declaration: Placed::of(candidate, declared),
unsure,
});
}
}
}
}
items.sort_by(|a, b| {
(&a.declaration.path, a.declaration.start)
.cmp(&(&b.declaration.path, b.declaration.start))
});
Ok(Dead { items })
}
}
/// Import statements worth a look, per file or across the tree.
impl Command for ImportsQuery {
type Output = ImportsReport;
fn run(self, cx: &mut Context<'_>) -> Result<Self::Output, EngineError> {
let path = self.path.as_deref().map(|p| cx.workspace.normalize(p));
let mut report = ImportsReport {
path: path.clone().map(Into::into),
unused: Vec::new(),
unresolved: Vec::new(),
redundant: Vec::new(),
unplaced: Vec::new(),
};
let structure = Structure::of(&mut cx.graph)?;
for (ns, fragments) in structure.iter() {
for Node {
candidate,
fragment,
} in fragments
{
if path.as_deref().is_some_and(|p| p != candidate.path()) {
continue;
}
if fragment.module.is_none() {
report.unplaced.push(candidate.path().into());
continue;
}
let facts = candidate.facts()?;
let source = candidate.file().source();
let parsed = Parsed {
path: candidate.path(),
source,
facts,
};
let site = |edge: &crate::graph::Edge| ImportSite {
path: candidate.path().into(),
import: edge.import.clone(),
start: source.position(edge.import.span.start),
address: edge.address.clone(),
};
// What each statement brings in: a glob of `X` and `X`
// itself are different things.
let mut brought: BTreeMap<(Address, bool), usize> = BTreeMap::new();
for edge in fragment.imports() {
if parsed.is_group_prefix(&edge.import) {
continue;
}
if edge.address.is_none() {
report.unresolved.push(site(edge));
continue;
}
if let Some(address) = &edge.address {
let seen = brought
.entry((address.clone(), edge.import.glob))
.or_default();
*seen += 1;
if *seen > 1 {
report.redundant.push(site(edge));
continue;
}
}
// A re-export is used by its takers; a language that hides
// which names an import takes cannot be judged by tokens.
if edge.import.reexport || ns.semantics().import_scopes_names {
continue;
}
let Some(bound) = edge.import.binding() else {
continue;
};
let statement = edge
.import
.group
.as_ref()
.map_or(edge.import.span, |g| g.statement);
let used = facts
.tokens_named(bound.as_str())
.any(|(span, _)| !statement.contains(&span));
if !used {
report.unused.push(site(edge));
}
}
}
}
Ok(report)
}
}