Files
lucisv/crates/vlog_semantic/src/model.rs
T
lucioandClaude Fable 5 ce2f858e5e Class member access: in_item.user_id resolves, through the handle's type and up its extends chain
DV round 1 (TODO section 15). The tail of head.member gets a reference
when the head is a plain identifier whose declared type (Symbol::type_name,
new) is a class or module: the member is found in that scope, else up the
in-file extends chain, else the workspace's member table chases
WorkspaceIndex::base_of (a subclass-to-base map rebuilt from the existing
inheritance census, no new facts field) - cycle-guarded on both sides and
placed in follow_one so hover, cross-file references and rename cannot
disagree. this.x/super.x misses ride the same loop, delivering topo
2.9-9. An out-of-class method body (task c2::update) sees its class's
members through a stand-in Class scope. FACTS_FORMAT 5 -> 6: the layout is
unchanged but the reference census grew.

Honesty by construction, after QA's one REWORK: a head whose type is
declared in-file as anything without a member scope (typedef, alias,
enum, param) records nothing - a two-state Option had stood in for the
three-state TypeHere question, and a typedef named bus_t could have been
answered by an unrelated class bus_t elsewhere. The fix removed exactly
438 wrong postings from the corpus with unresolved unchanged at 1256.

Hover 7-143 us; opentitan tl_seq_item member 0.12 ms cold, 0.01 ms warm;
store +2.85 percent.

docs/features/class-member-access/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-01 20:17:18 -07:00

245 lines
9.5 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 declared type **as written**, for the declarations that have one:
/// `c1` for `c1 in_item`, `pkg::c1` for a qualified spelling,
/// `clk_rst_if` for `virtual clk_rst_if vif`. `None` for a built-in type
/// (`logic`, `bit`, `int` — a keyword names no declaration) and for every
/// declaration that has no type at all (a module, a block label, an
/// instance). It is a *name*, not a resolved type: what member access
/// (`handle.member`) needs is the spelling to look the type up with, and
/// resolving it is `resolve.rs`'s business, on the same lexical chain the
/// type reference itself takes. See
/// `docs/features/class-member-access/README.md`.
pub type_name: Option<String>,
/// 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. The single-file layer
/// never chases `base`; the workspace overlay now does, upward through
/// [`crate::WorkspaceIndex::base_of`] after its `paths` miss, without
/// displacing what this says — a base resolved *in this file* would have
/// been `Resolved` already. See `docs/features/class-semantics/README.md`,
/// `docs/features/overlay-promotion/README.md` and
/// `docs/features/class-member-access/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()
}
/// The `extends` path this class scope was written with, as written.
/// `None` for every other scope, and for a class that extends nothing.
pub fn extends(&self) -> Option<&str> {
self.extends.as_deref()
}
}
/// 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>,
}