Files
lucisv/crates/vlog_semantic/src/resolve.rs
T
lucioandClaude Fable 5 33f58196e3 Refactoring M2: vlog_refactor crate + LSP rename
New crate vlog_refactor: rename any symbol vlog_semantic resolves, in-file
and cross-file, with whole-edit-or-nothing verification (token identity +
before/after resolution print over every file spelling either name).
End labels rename too via a new vlog_semantic Target::Declaration bound at
declaration time. Server ships prepareRename/rename; refusals are LSP
request errors, define-body/macro-argument/hierarchical-tail occurrences
warn instead of guessing. QA round 1 found two silent HIGHs (third-file
collision, macro-argument silence) — both closed with regression tests.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-08-24 05:12:53 -07:00

286 lines
11 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, Symbol, SymbolId, SymbolKind};
/// Resolve one pending use against the finished scope tree.
pub(crate) fn resolve(scopes: &[Scope], symbols: &[Symbol], p: &Pending) -> Resolution {
match &p.target {
// An end label names the construct it closes; the binder already knew
// which symbol that is, so there is nothing to look up.
Target::Declaration(id) => Resolution::Resolved(*id),
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::Unit => unit(scopes, symbols, p.scope, &p.name),
Target::UnitMember(unit) => unit_member(scopes, symbols, p.scope, unit, &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()] },
},
Target::ThisMember => this_member(scopes, p.scope, &p.name),
Target::SuperMember => super_member(scopes, p.scope),
}
}
/// Walk the scope chain outward: names declared in a scope first, then the
/// packages that scope imports, then the enclosing scope. A miss inside a
/// class whose header says `extends` becomes an inheritance candidate — the
/// base (which this crate never chases, in this file or out of it) could
/// supply the name, and so could any import path collected on the way out.
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;
}
miss(scopes, from, external, false)
}
/// A unit name — package or class (or a `typedef class` forward declaration):
/// the head of `p::x`, an `extends`/`implements` base, the `cls` of an
/// out-of-body definition. Resolved on the lexical chain, non-unit shadowers
/// skipped; imports are consulted because a class can arrive through
/// `import p::*`. A miss always leaves at least the bare name as an external
/// path, so `extends Base` reaches the workspace's unit table — and it stays
/// an `ExternalCandidate` even inside an extending class, because a base
/// class supplies members, not unit names (see [`miss`]).
fn unit(scopes: &[Scope], symbols: &[Symbol], 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) {
if is_unit_kind(symbols, sym) {
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) {
if is_unit_kind(symbols, sym) {
return Resolution::Resolved(sym);
}
}
}
None => {
let path = import.path();
if !external.contains(&path) {
external.push(path);
}
}
}
}
current = scope.parent;
}
external.push(name.to_string());
miss(scopes, from, external, true)
}
/// The `x` of `p::x`: looked up inside the named unit's scope only, never in
/// the lexical chain. The unit itself is found lexically (a class nested in
/// an in-file package is reachable from inside that package); in-file
/// imports are not consulted here — a unit reached through one is answered
/// by the head reference, and the member honestly stays external.
fn unit_member(
scopes: &[Scope],
symbols: &[Symbol],
from: ScopeId,
unit: &str,
name: &str,
) -> Resolution {
let mut current = Some(from);
while let Some(id) = current {
let scope = &scopes[id.0 as usize];
if let Some(sym) = scope.lookup_local(unit) {
match symbols[sym.0 as usize].kind {
SymbolKind::Package | SymbolKind::Class => {
if let Some(child) = symbols[sym.0 as usize].child_scope {
if let Some(found) = scopes[child.0 as usize].lookup_local(name) {
return Resolution::Resolved(found);
}
return match (
symbols[sym.0 as usize].kind,
&scopes[child.0 as usize].extends,
) {
// A static member may be inherited; the base is
// recorded, never chased.
(SymbolKind::Class, Some(base)) => Resolution::InheritedCandidate {
base: base.clone(),
paths: Vec::new(),
},
// A package fully known locally: no workspace
// index could change the answer.
_ => Resolution::NotFound,
};
}
}
// `typedef class c;` promises the class lives elsewhere: the
// member is the workspace's to find.
SymbolKind::Typedef => {
return Resolution::ExternalCandidate { paths: vec![format!("{unit}::{name}")] }
}
// A non-unit shadower; keep walking outward.
_ => {}
}
}
current = scope.parent;
}
Resolution::ExternalCandidate { paths: vec![format!("{unit}::{name}")] }
}
/// `this.x`: the enclosing class's own scope only — a member access cannot
/// name a method local or an enclosing module net.
fn this_member(scopes: &[Scope], from: ScopeId, name: &str) -> Resolution {
let Some(class) = nearest_class(scopes, from) else {
return Resolution::NotFound;
};
if let Some(sym) = scopes[class.0 as usize].lookup_local(name) {
return Resolution::Resolved(sym);
}
match &scopes[class.0 as usize].extends {
Some(base) => Resolution::InheritedCandidate { base: base.clone(), paths: Vec::new() },
None => Resolution::NotFound,
}
}
/// `super.x`: always the base class's business — recorded against it, never
/// chased. A `super` in a class without `extends` is illegal anyway.
fn super_member(scopes: &[Scope], from: ScopeId) -> Resolution {
match nearest_class(scopes, from).and_then(|c| scopes[c.0 as usize].extends.clone()) {
Some(base) => Resolution::InheritedCandidate { base, paths: Vec::new() },
None => Resolution::NotFound,
}
}
/// How a full miss ends. `unit_lookup` is set for [`Target::Unit`], whose
/// paths always hold at least the bare name — and whose misses stay
/// `ExternalCandidate` even inside an extending class, because a base class
/// supplies *members*, not package or class names (rules 5/8 of the
/// class-semantics README; QA-found, lead-ruled). For a member miss, the
/// nearest extends-carrying enclosing class's base could supply the name
/// (the LRM even puts it ahead of the enclosing scopes this walk already
/// searched — the deliberate inexactness the same README argues), so it
/// becomes `InheritedCandidate`.
fn miss(scopes: &[Scope], from: ScopeId, external: Vec<String>, unit_lookup: bool) -> Resolution {
if unit_lookup {
return Resolution::ExternalCandidate { paths: external };
}
if let Some(base) = nearest_extends(scopes, from) {
return Resolution::InheritedCandidate { base, paths: external };
}
if external.is_empty() {
Resolution::NotFound
} else {
Resolution::ExternalCandidate { paths: external }
}
}
/// The innermost enclosing class scope, extends or not.
fn nearest_class(scopes: &[Scope], from: ScopeId) -> Option<ScopeId> {
let mut current = Some(from);
while let Some(id) = current {
let scope = &scopes[id.0 as usize];
if scope.kind == crate::model::ScopeKind::Class {
return Some(id);
}
current = scope.parent;
}
None
}
/// The `extends` path of the innermost enclosing class that has one.
fn nearest_extends(scopes: &[Scope], from: ScopeId) -> Option<String> {
let mut current = Some(from);
while let Some(id) = current {
let scope = &scopes[id.0 as usize];
if scope.kind == crate::model::ScopeKind::Class {
if let Some(base) = &scope.extends {
return Some(base.clone());
}
}
current = scope.parent;
}
None
}
fn is_unit_kind(symbols: &[Symbol], sym: SymbolId) -> bool {
matches!(
symbols.get(sym.0 as usize).map(|s| s.kind),
Some(SymbolKind::Package | SymbolKind::Class | SymbolKind::Typedef)
)
}
fn named_at_file_scope(
scopes: &[Scope],
symbols: &[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: &[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: &[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
}
}