A port declared the old way is one symbol named in the module header and finished in the body by `input signed [31:0] data_in;`. The binder already merged the two; what was missing was a record of *where* the declaration is completed, so everything reading the symbol saw only the header's bare `data_in,`. `Symbol` and the index's `Def` gain `detail_span`, set by the binder when a body port declaration resolves to a port already declared in that scope — first writer wins, so a header naming a port twice stays deterministic and is the linter's to complain about. Old-style task and function arguments take the same path: the binder performs the same merge for them, so they are not special-cased. The persisted facts record grows two numbers per declaration, written unconditionally so the stream stays flat. `FACTS_FORMAT` moves 2 → 3 because a store written before this cannot be read after it: only the last declaration in a record is short enough to fail `done()`, and every earlier one would be read field-shifted by two — a cache handing back wrong spans, which is the shape the safety invariants exist to catch. A store from the old format is now discarded whole. Also: `Semantics::symbols_named`, a name-sorted side table of symbol ids searched with two `partition_point`s, so looking a name up is a binary search instead of a walk of every symbol. Stable-sorted, so symbols sharing a name keep declaration order and the lookup is an identity against the scan it replaces. A side table rather than a map keyed by name, because `Semantics` is cloned per document and need not hold a second copy of every name it already has. Co-Authored-By: Claude Opus 5 <[email protected]>
227 lines
8.4 KiB
Rust
227 lines
8.4 KiB
Rust
//! The tables `analyze` fills in: scopes, symbols and references.
|
|
|
|
use std::collections::HashMap;
|
|
use vlog_parser::span::TextRange;
|
|
|
|
/// Index of a [`Scope`] in [`Semantics::scopes`](crate::Semantics::scopes).
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
pub struct ScopeId(pub u32);
|
|
|
|
/// Index of a [`Symbol`] in [`Semantics::symbols`](crate::Semantics::symbols).
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
pub struct SymbolId(pub u32);
|
|
|
|
/// Index of a [`Reference`] in
|
|
/// [`Semantics::references`](crate::Semantics::references). The workspace
|
|
/// index keys its overlay by this.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
|
|
pub struct ReferenceId(pub u32);
|
|
|
|
/// What kind of declaration a [`Symbol`] came from.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum SymbolKind {
|
|
Module,
|
|
Package,
|
|
/// A module port, from either header style.
|
|
Port,
|
|
/// A net declaration (`wire`, `tri`, ...).
|
|
Net,
|
|
/// A variable declaration (`logic`, `reg`, `int`, ...).
|
|
Var,
|
|
Param,
|
|
Genvar,
|
|
Typedef,
|
|
/// A name introduced by an `enum` body, visible in the enclosing scope.
|
|
EnumVariant,
|
|
Function,
|
|
Task,
|
|
/// A function or task argument.
|
|
TfArg,
|
|
/// A module or gate instance.
|
|
Instance,
|
|
/// A named `begin`/`fork`/generate block.
|
|
Block,
|
|
/// A member of a `struct`/`union` body.
|
|
StructMember,
|
|
/// A `class` declaration.
|
|
Class,
|
|
}
|
|
|
|
/// A declaration: a name bound in one scope.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Symbol {
|
|
pub name: String,
|
|
pub kind: SymbolKind,
|
|
/// Byte range of the declared name itself — what an editor highlights.
|
|
pub name_span: TextRange,
|
|
/// Byte range of the construct that owns the name (the whole `module`,
|
|
/// the whole declarator, the whole port declaration).
|
|
pub decl_span: TextRange,
|
|
/// Where the *complete* declaration is written, when that is not
|
|
/// `decl_span`: a non-ANSI port's header names it and a body `input
|
|
/// signed [31:0] data_in;` finishes it, so hover and go-to-definition
|
|
/// point here — the line that carries the direction, the type and the
|
|
/// width — while `name_span`/`decl_span` stay in the header. `None` for
|
|
/// every declaration that is complete where it is named.
|
|
pub detail_span: Option<TextRange>,
|
|
/// The scope the name is bound in.
|
|
pub scope: ScopeId,
|
|
/// The scope this declaration opens, for the kinds that open one
|
|
/// (module, package, function, task, named block, struct body).
|
|
pub child_scope: Option<ScopeId>,
|
|
}
|
|
|
|
/// What a [`Reference`] turned out to name.
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum Resolution {
|
|
/// Bound to a declaration in this file.
|
|
Resolved(SymbolId),
|
|
/// Nothing in this file binds it, but a named external unit could: a
|
|
/// package imported with `import p::*` / `import p::x`, a package written
|
|
/// as `p::x`, a module instantiated but declared in another file, or an
|
|
/// unqualified **type** name — an `interface`, a package-less `class` —
|
|
/// that only a file-scope declaration somewhere else can be. `paths` names
|
|
/// those units — a path containing `::` is a package path, a bare name is a
|
|
/// module, package or class name — in the order they are tried, the use's
|
|
/// own name last. The workspace index (TODO.md §6) is what finishes these.
|
|
ExternalCandidate { paths: Vec<String> },
|
|
/// A use inside a class that nothing lexical binds, where the class's
|
|
/// `extends` could: `base` is the base-class path as written
|
|
/// (`uvm_component`, `pkg::base`), and `paths` are the import paths the
|
|
/// lexical walk collected — plus the use's own name when it was written in
|
|
/// a type position — any of which could supply it. Nothing chases `base`,
|
|
/// in this file or out of it; the
|
|
/// workspace overlay may answer from `paths` without displacing what this
|
|
/// says, because a resolved base would have been `Resolved` already. See
|
|
/// `docs/features/class-semantics/README.md` and
|
|
/// `docs/features/overlay-promotion/README.md`.
|
|
InheritedCandidate { base: String, paths: Vec<String> },
|
|
/// Nothing in this file binds it and no external unit could: a member of a
|
|
/// package this file fully declares, a `.port` of a module it declares, a
|
|
/// `this.x`/`super.x` with no class or base to blame, or a plain
|
|
/// value-position identifier with no import in scope. An unqualified miss
|
|
/// in a **type** position is never this — the workspace's unit table could
|
|
/// hold it — so that one is an `ExternalCandidate` carrying its own name.
|
|
NotFound,
|
|
}
|
|
|
|
/// A *use* of a name: an expression identifier, a type name, an instantiated
|
|
/// module's name, a `.port` in a connection list, a package in an `import`.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Reference {
|
|
pub name: String,
|
|
/// Byte range of the identifier as written.
|
|
pub name_span: TextRange,
|
|
/// The scope the use is written in.
|
|
pub scope: ScopeId,
|
|
pub resolution: Resolution,
|
|
}
|
|
|
|
impl Reference {
|
|
/// The symbol this use names, if it is declared in this file.
|
|
pub fn resolved(&self) -> Option<SymbolId> {
|
|
match self.resolution {
|
|
Resolution::Resolved(id) => Some(id),
|
|
_ => None,
|
|
}
|
|
}
|
|
|
|
/// True when only a workspace-wide index can finish this reference.
|
|
pub fn is_external_candidate(&self) -> bool {
|
|
matches!(self.resolution, Resolution::ExternalCandidate { .. })
|
|
}
|
|
|
|
/// True when the name could only come through the class's `extends`.
|
|
pub fn is_inherited_candidate(&self) -> bool {
|
|
matches!(self.resolution, Resolution::InheritedCandidate { .. })
|
|
}
|
|
}
|
|
|
|
/// What construct a [`Scope`] came from.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub enum ScopeKind {
|
|
File,
|
|
Module,
|
|
Package,
|
|
Function,
|
|
Task,
|
|
/// A `begin`/`end`, `fork`/`join`, `for` or `foreach` body.
|
|
Block,
|
|
/// A generate region, a generate block, or a `for` generate's header.
|
|
Generate,
|
|
/// A `struct`/`union` body. Never searched by name lookup — members are
|
|
/// reached through `x.field`, which this crate does not resolve.
|
|
Struct,
|
|
/// A `class` body.
|
|
Class,
|
|
}
|
|
|
|
/// One `import p::x;` / `import p::*;` recorded in the scope that wrote it.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Import {
|
|
/// The package name written before `::`.
|
|
pub package: String,
|
|
/// The imported name; `None` for a `::*` wildcard.
|
|
pub item: Option<String>,
|
|
/// The package's scope when the package is declared in this file.
|
|
pub scope: Option<ScopeId>,
|
|
/// Byte range of the package name as written.
|
|
pub name_span: TextRange,
|
|
}
|
|
|
|
impl Import {
|
|
/// The import path as written, e.g. `pkg::*`.
|
|
pub fn path(&self) -> String {
|
|
match &self.item {
|
|
Some(item) => format!("{}::{}", self.package, item),
|
|
None => format!("{}::*", self.package),
|
|
}
|
|
}
|
|
|
|
/// True when this import could supply `name`.
|
|
pub fn supplies(&self, name: &str) -> bool {
|
|
match &self.item {
|
|
Some(item) => item == name,
|
|
None => true,
|
|
}
|
|
}
|
|
}
|
|
|
|
/// A lexical scope: a set of names plus a link to the scope enclosing it.
|
|
#[derive(Debug, Clone)]
|
|
pub struct Scope {
|
|
pub kind: ScopeKind,
|
|
pub parent: Option<ScopeId>,
|
|
/// The symbol whose declaration opened this scope; `None` for the file
|
|
/// scope and for unnamed blocks.
|
|
pub owner: Option<SymbolId>,
|
|
/// Byte range of the construct the scope covers.
|
|
pub span: TextRange,
|
|
/// Symbols bound here, in declaration order.
|
|
pub symbols: Vec<SymbolId>,
|
|
pub children: Vec<ScopeId>,
|
|
pub imports: Vec<Import>,
|
|
/// For a [`ScopeKind::Class`] scope: the `extends` path as written, when
|
|
/// the class has one. What an inheritance miss reports as its `base`.
|
|
pub(crate) extends: Option<String>,
|
|
/// Name → symbol, first declaration wins (see the README's shadowing
|
|
/// rules). Kept separate from `symbols` so a redeclaration is still
|
|
/// visible in the outline.
|
|
pub(crate) names: HashMap<String, SymbolId>,
|
|
}
|
|
|
|
impl Scope {
|
|
/// The symbol this scope binds `name` to, ignoring enclosing scopes.
|
|
pub fn lookup_local(&self, name: &str) -> Option<SymbolId> {
|
|
self.names.get(name).copied()
|
|
}
|
|
}
|
|
|
|
/// One node of the outline tree returned by
|
|
/// [`Semantics::document_symbols`](crate::Semantics::document_symbols).
|
|
#[derive(Debug, Clone)]
|
|
pub struct DocumentSymbol {
|
|
pub symbol: SymbolId,
|
|
pub children: Vec<DocumentSymbol>,
|
|
}
|