TODO §47 (docs/features/type-name-spans). Closes topo §2.9-2.
`DataType` carries `name_start` (a `u32` in its existing padding, so
`Expr` stays at its 96-byte pin) and derives `name_span()` and
`segments()` — the `::`/`.` split from the span's own text, with a
leading escaped identifier read to its terminator first. Five parser
construction sites set it; an implicit type gets the empty range at the
position its name would have had. `StmtKind::Disable { name, name_span }`.
The binder walks `ty.segments()` and verifies every segment's range
against the source before recording it (`exact_or_empty`); a segment that
does not read back is recorded with the empty range, never dropped, so
the census's `unplaced` counter is the alarm a join regression would
move (review rework of QA's MEDIUM). `scan.rs` deleted; `bind_data_type`
lost its `window` and ten call sites their window arithmetic.
Census unchanged in every field (860 files, 27 486 symbols, 103 870
references, 1 197 unresolved, 0 unplaced); 0 of 860 corpus files differ;
200/200 opentitan definition/hover/typeDefinition answers identical.
Co-Authored-By: Claude Fable 5.1 <[email protected]>
429 lines
18 KiB
Rust
429 lines
18 KiB
Rust
//! Classes: the header (`#(...)`, `extends`, `implements`), the body, the
|
|
//! constraint blocks a class holds — `constraint c { ... }`, its `cls::c`
|
|
//! out-of-block form and `randomize() with { ... }` — and the out-of-body
|
|
//! method definitions deferred until the whole file is bound, so the class
|
|
//! scope exists wherever the class is written. Constraints live here rather
|
|
//! than in a file of their own because a constraint body *is* class scope:
|
|
//! every bare name in it is a property with `this.` implicit (IEEE 1800
|
|
//! §18.5), and the out-of-block form goes through the same deferral as a
|
|
//! method does.
|
|
|
|
use vlog_parser::ast::*;
|
|
use vlog_parser::span::TextRange;
|
|
|
|
use crate::model::{ScopeId, ScopeKind, SymbolKind};
|
|
|
|
use super::{Binder, Deferred, Target};
|
|
|
|
impl<'a> Binder<'a> {
|
|
pub(super) fn bind_class(&mut self, c: &'a ClassDecl, span: TextRange, scope: ScopeId) {
|
|
let inner = self.declare_with_scope(
|
|
scope,
|
|
&c.name,
|
|
SymbolKind::Class,
|
|
c.name_span,
|
|
span,
|
|
ScopeKind::Class,
|
|
);
|
|
// Header `#( ... )` parameters bind like a module's.
|
|
self.bind_param_ports(c.param_ports.iter().flatten(), inner);
|
|
if let Some(ext) = &c.extends {
|
|
self.scopes[inner.0 as usize].extends = Some(ext.name.clone());
|
|
// The base name resolves from the *enclosing* scope — a class is
|
|
// not visible to its own header.
|
|
self.use_scoped_unit(&ext.name, ext.name_span, scope);
|
|
// Overrides: `.W` is the base's parameter; the value may name the
|
|
// class's own parameters (`extends base #(.W(W))`), so it binds
|
|
// in the class scope.
|
|
let base = last_segment(&ext.name);
|
|
for conn in ext.params.iter().flatten().filter_map(ListElem::ok) {
|
|
if conn.name != "*" {
|
|
self.use_name(
|
|
inner,
|
|
&conn.name,
|
|
conn.name_span,
|
|
Target::UnitMember(base.to_string()),
|
|
);
|
|
}
|
|
self.bind_opt_expr(conn.expr.as_ref(), inner);
|
|
}
|
|
}
|
|
for n in &c.implements {
|
|
self.use_scoped_unit(&n.text, n.span, scope);
|
|
}
|
|
// The body is an ordinary item list; everything the item walk already
|
|
// does — declarations, typedefs, localparams, imports, methods,
|
|
// nested classes — works here unchanged. Verbatim members
|
|
// (constraints, covergroups) stay opaque through the item arm.
|
|
self.bind_items(&c.items, inner);
|
|
self.use_end_label(scope, inner, c.end_label.as_ref(), c.end_label_span);
|
|
}
|
|
|
|
/// Record a unit name that may be scope-qualified: `base`, `pkg::base`.
|
|
/// A qualified spelling records head and tail (middle segments of a
|
|
/// longer chain are not recorded; the corpus has none). Both spans are
|
|
/// derived from the glued text, so each is verified before use and the
|
|
/// reference is skipped when the source spelled spaces around the `::`.
|
|
fn use_scoped_unit(&mut self, name: &str, span: TextRange, scope: ScopeId) {
|
|
match name.rsplit_once("::") {
|
|
Some((head_path, tail)) => {
|
|
let head = head_path.split("::").next().unwrap_or(head_path);
|
|
let head_span = self.exact_or_empty(
|
|
TextRange::new(span.start, span.start.saturating_add(head.len() as u32)),
|
|
head,
|
|
);
|
|
if !head_span.is_empty() {
|
|
self.use_name(scope, head, head_span, Target::Unit);
|
|
}
|
|
let tail_span = self.exact_or_empty(
|
|
TextRange::new(span.end.saturating_sub(tail.len() as u32), span.end),
|
|
tail,
|
|
);
|
|
let owner = head_path.rsplit("::").next().unwrap_or(head_path);
|
|
if !tail_span.is_empty() {
|
|
self.use_name(scope, tail, tail_span, Target::UnitMember(owner.to_string()));
|
|
}
|
|
}
|
|
None => self.use_name(scope, name, span, Target::Unit),
|
|
}
|
|
}
|
|
|
|
/// A `constraint c { ... }` declaration, its `constraint cls::c { ... }`
|
|
/// out-of-block form, and the `extern constraint c;` prototype.
|
|
///
|
|
/// The out-of-block form is deferred exactly as an out-of-body method is,
|
|
/// so `cls` can be looked up wherever it is written — or stood in for
|
|
/// when it lives in another file.
|
|
pub(super) fn bind_constraint(
|
|
&mut self,
|
|
c: &'a ConstraintDecl,
|
|
span: TextRange,
|
|
scope: ScopeId,
|
|
) {
|
|
if !c.scope.is_empty() {
|
|
self.deferred.push(Deferred::Constraint(c, span, scope));
|
|
return;
|
|
}
|
|
self.bind_constraint_in(c, span, scope);
|
|
}
|
|
|
|
/// Declare the constraint in `home` and bind its body in a scope hanging
|
|
/// off it.
|
|
///
|
|
/// The body scope's parent is the class scope, which is the whole point:
|
|
/// every bare name inside a constraint is a property of the class with
|
|
/// `this.` implicit (IEEE 1800 §18.5), so ordinary lexical lookup gives
|
|
/// the right answer with no special target. A prototype has no body and
|
|
/// opens no scope.
|
|
fn bind_constraint_in(&mut self, c: &'a ConstraintDecl, span: TextRange, home: ScopeId) {
|
|
let Some(set) = &c.body else {
|
|
self.declare(home, &c.name.text, SymbolKind::Constraint, c.name.span, span);
|
|
return;
|
|
};
|
|
let inner = self.declare_with_scope(
|
|
home,
|
|
&c.name.text,
|
|
SymbolKind::Constraint,
|
|
c.name.span,
|
|
span,
|
|
ScopeKind::Constraint,
|
|
);
|
|
// A definition whose `extern` prototype already bound the name gets no
|
|
// symbol of its own; point the body scope at the prototype so the
|
|
// constraint's `foreach` variables do not spill into the class
|
|
// outline — the rule out-of-body methods follow.
|
|
if self.scopes[inner.0 as usize].owner.is_none() {
|
|
self.scopes[inner.0 as usize].owner =
|
|
self.scopes[home.0 as usize].lookup_local(&c.name.text);
|
|
}
|
|
self.bind_constraint_set(set, inner);
|
|
}
|
|
|
|
fn bind_constraint_set(&mut self, set: &ConstraintSet, scope: ScopeId) {
|
|
for item in &set.items {
|
|
self.bind_constraint_item(item, scope);
|
|
}
|
|
}
|
|
|
|
fn bind_constraint_item(&mut self, item: &ConstraintItem, scope: ScopeId) {
|
|
match &item.kind {
|
|
ConstraintItemKind::Expr { expr, .. }
|
|
| ConstraintItemKind::DisableSoft { expr, .. } => self.bind_expr(expr, scope),
|
|
ConstraintItemKind::Implication { cond, set, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_constraint_set(set, scope);
|
|
}
|
|
ConstraintItemKind::If { cond, then, els, .. } => {
|
|
self.bind_expr(cond, scope);
|
|
self.bind_constraint_set(then, scope);
|
|
if let Some(els) = els {
|
|
self.bind_constraint_set(els, scope);
|
|
}
|
|
}
|
|
// The loop variables are the constraint's own, so they get a
|
|
// scope of their own — the same shape a `foreach` statement uses,
|
|
// and for the same reason: `i` must not leak to the item beside
|
|
// this one. A skipped dimension arrives as an empty name, which
|
|
// `declare` already ignores.
|
|
ConstraintItemKind::Foreach { array, vars, set, .. } => {
|
|
let inner = self.new_scope(ScopeKind::Block, scope, item.span);
|
|
self.bind_expr(array, scope);
|
|
for v in vars {
|
|
let span = v.name.span;
|
|
self.declare(inner, &v.name.text, SymbolKind::Var, span, span);
|
|
}
|
|
self.bind_constraint_set(set, inner);
|
|
}
|
|
ConstraintItemKind::SolveBefore { solve, before, .. } => {
|
|
for e in solve.iter().chain(before.iter()) {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
}
|
|
ConstraintItemKind::Unique { items, .. } => {
|
|
for e in items {
|
|
self.bind_expr(&e.expr, scope);
|
|
}
|
|
}
|
|
// A directive is not evaluated, and a null item names nothing.
|
|
ConstraintItemKind::Directive(_) | ConstraintItemKind::Empty => {}
|
|
}
|
|
}
|
|
|
|
/// `base with { ... }` — a `randomize` call's inline constraint block
|
|
/// (IEEE 1800 §18.7).
|
|
///
|
|
/// The block is bound in the **caller's** scope, because that is where it
|
|
/// is written and what its `local::`-free names may also reach. What
|
|
/// makes it useful is the head: in `req.randomize() with { addr == 0; }`
|
|
/// the `addr` is `req`'s property, not the writing scope's, so while the
|
|
/// block is bound every bare name is recorded as a member of `req` —
|
|
/// through the very `Target::MemberOf` chase `head.member` already uses,
|
|
/// which then follows `req`'s declared type into its class, across files
|
|
/// included.
|
|
///
|
|
/// A call with no such head — a bare `randomize() with` (implicitly
|
|
/// `this`, already in scope) or `std::randomize(x) with` (whose names are
|
|
/// the caller's own) — keeps ordinary lexical binding.
|
|
pub(super) fn bind_with_constraint(
|
|
&mut self,
|
|
base: &Expr,
|
|
block: &ConstraintBody,
|
|
scope: ScopeId,
|
|
) {
|
|
self.bind_expr(base, scope);
|
|
let ConstraintBody::Items(set) = block else {
|
|
// A body the grammar did not recognise stays opaque, exactly as
|
|
// every `with` block was before this round.
|
|
return;
|
|
};
|
|
let outer = std::mem::replace(&mut self.with_head, randomize_head(base));
|
|
self.bind_constraint_set(set, scope);
|
|
self.with_head = outer;
|
|
}
|
|
|
|
/// Bind the out-of-body method definitions collected during the walk.
|
|
/// Runs after everything else is bound, so the class scope exists no
|
|
/// matter where the class is written.
|
|
pub(crate) fn bind_deferred(&mut self) {
|
|
let deferred = std::mem::take(&mut self.deferred);
|
|
for d in deferred {
|
|
match d {
|
|
Deferred::Function(f, span, scope) => {
|
|
let path = scope_path(&f.scope);
|
|
let path = path.as_str();
|
|
self.bind_out_of_body(
|
|
path,
|
|
&f.name,
|
|
f.name_span,
|
|
span,
|
|
scope,
|
|
SymbolKind::Function,
|
|
ScopeKind::Function,
|
|
f.ret.as_ref(),
|
|
&f.args,
|
|
&f.stmts,
|
|
);
|
|
}
|
|
Deferred::Constraint(c, span, scope) => {
|
|
let path = scope_path(&c.scope);
|
|
let path = path.as_str();
|
|
let home = self.out_of_body_home(path, c.name.span, span, scope);
|
|
self.bind_constraint_in(c, span, home);
|
|
}
|
|
Deferred::Task(t, span, scope) => {
|
|
let path = scope_path(&t.scope);
|
|
let path = path.as_str();
|
|
self.bind_out_of_body(
|
|
path,
|
|
&t.name,
|
|
t.name_span,
|
|
span,
|
|
scope,
|
|
SymbolKind::Task,
|
|
ScopeKind::Task,
|
|
None,
|
|
&t.args,
|
|
&t.stmts,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// The scope an out-of-body definition's name belongs in, and the
|
|
/// reference to the class path that names it.
|
|
///
|
|
/// The written class path is a reference of its own — navigation from
|
|
/// `cls::method` to the class. Its span is derived (the parser glues the
|
|
/// path), so it is verified and skipped when inexact.
|
|
///
|
|
/// The home is the class scope when the class is in this file, else a
|
|
/// scope standing in for that class — empty, so every lookup falls
|
|
/// straight through it to the writing scope, but marked with the class
|
|
/// path as its `extends`, so a name nothing else binds is an
|
|
/// `InheritedCandidate` against the class rather than a `NotFound`. That
|
|
/// is what lets `task c2::update(...)` — and `constraint c2::c { ... }` —
|
|
/// see `c2`'s properties when `c2` is declared in another file: the
|
|
/// workspace index finishes the candidate through its member table.
|
|
fn out_of_body_home(
|
|
&mut self,
|
|
path: &str,
|
|
name_span: TextRange,
|
|
span: TextRange,
|
|
decl_scope: ScopeId,
|
|
) -> ScopeId {
|
|
let cls = last_segment(path);
|
|
let cls_span = TextRange::new(
|
|
name_span.start.saturating_sub(2).saturating_sub(cls.len() as u32),
|
|
name_span.start.saturating_sub(2),
|
|
);
|
|
let cls_span = self.exact_or_empty(cls_span, cls);
|
|
if !cls_span.is_empty() {
|
|
self.use_name(decl_scope, cls, cls_span, Target::Unit);
|
|
}
|
|
match self.find_unit_scope(decl_scope, path) {
|
|
Some(scope) => scope,
|
|
None if path.is_empty() => decl_scope,
|
|
None => {
|
|
let stand_in = self.new_scope(ScopeKind::Class, decl_scope, span);
|
|
self.scopes[stand_in.0 as usize].extends = Some(path.to_string());
|
|
stand_in
|
|
}
|
|
}
|
|
}
|
|
|
|
/// One `function void cls::method(...)` definition: the method symbol
|
|
/// binds into `cls`'s scope (an `extern` prototype already there makes
|
|
/// the definition-site name a reference to it — the same
|
|
/// redeclaration-becomes-reference rule non-ANSI ports use), and the body
|
|
/// scope hangs under `cls`'s scope so properties resolve.
|
|
// The allow: one call shape, two callers; a parameter struct would
|
|
// outweigh the function.
|
|
#[allow(clippy::too_many_arguments)]
|
|
fn bind_out_of_body(
|
|
&mut self,
|
|
path: &str,
|
|
name: &str,
|
|
name_span: TextRange,
|
|
span: TextRange,
|
|
decl_scope: ScopeId,
|
|
kind: SymbolKind,
|
|
scope_kind: ScopeKind,
|
|
ret: Option<&DataType>,
|
|
args: &'a [ListElem<TfArg>],
|
|
stmts: &'a [Stmt],
|
|
) {
|
|
let home = self.out_of_body_home(path, name_span, span, decl_scope);
|
|
let sym = self.declare(home, name, kind, name_span, span);
|
|
let body = self.new_scope(scope_kind, home, span);
|
|
match sym {
|
|
Some(sym) => {
|
|
self.symbols[sym.0 as usize].child_scope = Some(body);
|
|
self.scopes[body.0 as usize].owner = Some(sym);
|
|
}
|
|
// The prototype owns the name; mark the body scope as owned by it
|
|
// (without touching the prototype's own scope) so method locals
|
|
// do not spill into the class outline.
|
|
None => {
|
|
self.scopes[body.0 as usize].owner =
|
|
self.scopes[home.0 as usize].lookup_local(name);
|
|
}
|
|
}
|
|
// Return type, arguments and body all resolve from inside the class,
|
|
// as the LRM says an out-of-body declaration does.
|
|
if let Some(ret) = ret {
|
|
self.bind_data_type(ret, body);
|
|
}
|
|
self.bind_tf_args(args, body);
|
|
self.bind_stmts(stmts, body);
|
|
}
|
|
|
|
/// The scope of the unit `path` names, resolved lexically from `from` and
|
|
/// descended segment by segment (`pkg::cls`).
|
|
fn find_unit_scope(&self, from: ScopeId, path: &str) -> Option<ScopeId> {
|
|
let mut segments = path.split("::");
|
|
let mut scope = self.lookup_unit_scope(from, segments.next()?)?;
|
|
for seg in segments {
|
|
let sym = self.scopes[scope.0 as usize].lookup_local(seg)?;
|
|
let s = self.symbols.get(sym.0 as usize)?;
|
|
if !matches!(s.kind, SymbolKind::Class | SymbolKind::Package) {
|
|
return None;
|
|
}
|
|
scope = s.child_scope?;
|
|
}
|
|
Some(scope)
|
|
}
|
|
|
|
/// The scope of the class or package `name`, found on the lexical chain
|
|
/// from `from`; non-unit shadowers are skipped.
|
|
fn lookup_unit_scope(&self, from: ScopeId, name: &str) -> Option<ScopeId> {
|
|
let mut current = Some(from);
|
|
while let Some(id) = current {
|
|
let scope = self.scopes.get(id.0 as usize)?;
|
|
if let Some(sym) = scope.lookup_local(name) {
|
|
if let Some(s) = self.symbols.get(sym.0 as usize) {
|
|
if matches!(s.kind, SymbolKind::Class | SymbolKind::Package) {
|
|
if let Some(child) = s.child_scope {
|
|
return Some(child);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
current = scope.parent;
|
|
}
|
|
None
|
|
}
|
|
}
|
|
|
|
/// The last `::` segment of a possibly-qualified path (`pkg::cls` → `cls`).
|
|
fn last_segment(path: &str) -> &str {
|
|
path.rsplit("::").next().unwrap_or(path)
|
|
}
|
|
|
|
/// The `obj` / `obj.sub` a `randomize() with` call is made on, when the call
|
|
/// has such a head at all: `obj.randomize()` and `a.b.randomize()` do,
|
|
/// `randomize()` and `std::randomize(x)` do not. Returns the head name and
|
|
/// the middle links, which is exactly what [`Target::MemberOf`] carries.
|
|
fn randomize_head(base: &Expr) -> Option<(String, Vec<String>)> {
|
|
let ExprKind::Call { callee, .. } = &base.kind else { return None };
|
|
let ExprKind::Member { base, name, .. } = &callee.kind else { return None };
|
|
if name != "randomize" {
|
|
return None;
|
|
}
|
|
let mut mids = Vec::new();
|
|
let mut cur = base.as_ref();
|
|
loop {
|
|
match &cur.kind {
|
|
ExprKind::Ident(head) => {
|
|
mids.reverse();
|
|
return Some((head.clone(), mids));
|
|
}
|
|
ExprKind::Member { base, name, .. } => {
|
|
mids.push(name.clone());
|
|
cur = base;
|
|
}
|
|
_ => return None,
|
|
}
|
|
}
|
|
}
|