vlog_semantic (new crate): single-file symbol tables and reference
resolution -- the layer that powers go-to-definition, references,
hover and document outline, and later the semantic lint rules.
Two-pass bind-then-resolve (Verilog forward references), 15 symbol
kinds, scope tree, and a three-state Resolution (Resolved /
ExternalCandidate carrying import paths for the future workspace
index / NotFound). scan.rs recovers name spans the AST lacks (95k
corpus spans verified; handshake filed and approved to move
name_span into the parser, which deletes the module). Corpus
baseline: 79.5% in-file resolution, 0.9% unresolved on clean files,
zero panics including tolerant-parse trees.
parallel-files (vfmt): rayon-parallel per-file formatting -- first
dependency, exact-pinned with written justification. 8 MiB worker
stacks discharge the recursion-limit handshake (sabotage-verified);
single-install panic hook discharges the cli-polish handshake;
deterministic input-order output (one stdout/stderr hash across 14
corpus runs at 4 thread counts); --threads flag; hand-rolled
.gitignore subset with 16/16 differential agreement against git
check-ignore. 3.6x wall-clock on the 860-file corpus.
Dev: 2 Opus agents; QA: 2 Sonnet agents; reviews in
docs/features/{vlog-semantic,parallel-files}/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
115 lines
4.0 KiB
Rust
115 lines
4.0 KiB
Rust
//! Pass two: turn every pending use into a [`Resolution`].
|
|
//!
|
|
//! Everything is bound before anything is resolved, so a name used before it
|
|
//! is declared resolves exactly like one used after — which is what Verilog
|
|
//! requires at module scope and what an editor needs mid-edit.
|
|
|
|
use crate::bind::{Pending, Target};
|
|
use crate::model::{Resolution, Scope, ScopeId, SymbolId, SymbolKind};
|
|
|
|
/// Resolve one pending use against the finished scope tree.
|
|
pub(crate) fn resolve(
|
|
scopes: &[Scope],
|
|
symbols: &[crate::model::Symbol],
|
|
p: &Pending,
|
|
) -> Resolution {
|
|
match &p.target {
|
|
Target::Lexical => lexical(scopes, p.scope, &p.name),
|
|
Target::Package => named_at_file_scope(scopes, symbols, &p.name, SymbolKind::Package),
|
|
Target::Module => named_at_file_scope(scopes, symbols, &p.name, SymbolKind::Module),
|
|
Target::PackageMember(pkg) => {
|
|
match owning_scope(scopes, symbols, pkg, SymbolKind::Package) {
|
|
Some(scope) => match scopes[scope.0 as usize].lookup_local(&p.name) {
|
|
Some(id) => Resolution::Resolved(id),
|
|
None => Resolution::NotFound,
|
|
},
|
|
None => Resolution::ExternalCandidate { paths: vec![format!("{pkg}::{}", p.name)] },
|
|
}
|
|
}
|
|
Target::Port(module) => match owning_scope(scopes, symbols, module, SymbolKind::Module) {
|
|
Some(scope) => match scopes[scope.0 as usize].lookup_local(&p.name) {
|
|
Some(id) => Resolution::Resolved(id),
|
|
None => Resolution::NotFound,
|
|
},
|
|
// The port list lives in whatever file declares the module.
|
|
None => Resolution::ExternalCandidate { paths: vec![module.clone()] },
|
|
},
|
|
}
|
|
}
|
|
|
|
/// Walk the scope chain outward: names declared in a scope first, then the
|
|
/// packages that scope imports, then the enclosing scope.
|
|
fn lexical(scopes: &[Scope], from: ScopeId, name: &str) -> Resolution {
|
|
let mut external = Vec::new();
|
|
let mut current = Some(from);
|
|
while let Some(id) = current {
|
|
let scope = &scopes[id.0 as usize];
|
|
if let Some(sym) = scope.lookup_local(name) {
|
|
return Resolution::Resolved(sym);
|
|
}
|
|
for import in &scope.imports {
|
|
if !import.supplies(name) {
|
|
continue;
|
|
}
|
|
match import.scope {
|
|
Some(pkg) => {
|
|
if let Some(sym) = scopes[pkg.0 as usize].lookup_local(name) {
|
|
return Resolution::Resolved(sym);
|
|
}
|
|
}
|
|
// A package this file does not declare: only the workspace
|
|
// index can say whether it holds the name.
|
|
None => {
|
|
let path = import.path();
|
|
if !external.contains(&path) {
|
|
external.push(path);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
current = scope.parent;
|
|
}
|
|
if external.is_empty() {
|
|
Resolution::NotFound
|
|
} else {
|
|
Resolution::ExternalCandidate { paths: external }
|
|
}
|
|
}
|
|
|
|
fn named_at_file_scope(
|
|
scopes: &[Scope],
|
|
symbols: &[crate::model::Symbol],
|
|
name: &str,
|
|
kind: SymbolKind,
|
|
) -> Resolution {
|
|
match file_symbol(scopes, symbols, name, kind) {
|
|
Some(id) => Resolution::Resolved(id),
|
|
None => Resolution::ExternalCandidate { paths: vec![name.to_string()] },
|
|
}
|
|
}
|
|
|
|
fn owning_scope(
|
|
scopes: &[Scope],
|
|
symbols: &[crate::model::Symbol],
|
|
name: &str,
|
|
kind: SymbolKind,
|
|
) -> Option<ScopeId> {
|
|
let id = file_symbol(scopes, symbols, name, kind)?;
|
|
symbols[id.0 as usize].child_scope
|
|
}
|
|
|
|
/// Modules and packages are file-scope names, so one lookup finds them.
|
|
fn file_symbol(
|
|
scopes: &[Scope],
|
|
symbols: &[crate::model::Symbol],
|
|
name: &str,
|
|
kind: SymbolKind,
|
|
) -> Option<SymbolId> {
|
|
let id = scopes.first()?.lookup_local(name)?;
|
|
if symbols.get(id.0 as usize)?.kind == kind {
|
|
Some(id)
|
|
} else {
|
|
None
|
|
}
|
|
}
|