Files
lucisv/crates/vlog_semantic/src/bind/stmt.rs
T
lucioandClaude Fable 5.1 4d59e80568
ci / checks (push) Successful in 1m3s
ci / corpus (push) Successful in 33s
Type-name spans on the AST; vlog_semantic/src/scan.rs retired
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]>
2026-09-05 09:24:19 -05:00

217 lines
9.1 KiB
Rust

//! Statements: blocks and their labels, `if`/`case`, the loops and the scopes
//! their variables get, assignments and timing controls, block-scoped
//! declarations, and the `@(...)` sensitivity list that clocking blocks,
//! covergroups and property specs share with them.
use vlog_parser::ast::*;
use crate::model::{ScopeId, ScopeKind, SymbolKind};
use super::decl::param_kind;
use super::{Binder, Target};
impl<'a> Binder<'a> {
pub(super) fn bind_stmts(&mut self, stmts: &'a [Stmt], scope: ScopeId) {
for s in stmts {
self.bind_stmt(s, scope);
}
}
pub(super) fn bind_stmt(&mut self, stmt: &'a Stmt, scope: ScopeId) {
match &stmt.kind {
StmtKind::Block { label, label_span, stmts, end_label, end_label_span, .. }
| StmtKind::ForkJoin { label, label_span, stmts, end_label, end_label_span, .. } => {
let inner = match (label, *label_span) {
(Some(label), Some(ls)) => self.declare_with_scope(
scope,
label,
SymbolKind::Block,
ls,
stmt.span,
ScopeKind::Block,
),
_ => self.new_scope(ScopeKind::Block, scope, stmt.span),
};
self.bind_stmts(stmts, inner);
self.use_end_label(scope, inner, end_label.as_ref(), *end_label_span);
}
StmtKind::If { cond, then, els, .. } => {
self.bind_expr(cond, scope);
self.bind_stmt(then, scope);
if let Some(e) = els {
self.bind_stmt(e, scope);
}
}
StmtKind::Case { expr, items, .. } => {
self.bind_expr(expr, scope);
self.bind_case_items(items, scope);
}
StmtKind::RandCase { items, .. } => self.bind_case_items(items, scope),
// A statement label names the statement the way a block name
// names its block (IEEE 1800 §9.3.5).
StmtKind::Labeled { name, name_span, stmt: inner } => {
self.declare(scope, name, SymbolKind::Block, *name_span, stmt.span);
self.bind_stmt(inner, scope);
}
StmtKind::Import(paths) => {
for path in paths {
self.bind_import_path(path, scope);
}
}
StmtKind::For { init, cond, step, body, .. } => {
let inner = self.new_scope(ScopeKind::Block, scope, stmt.span);
for i in init {
match &i.ty {
Some(ty) => {
if let ExprKind::Ident(name) = &i.assign.lhs.kind {
self.bind_data_type(ty, scope);
self.declare(
inner,
name,
SymbolKind::Var,
i.assign.lhs.span,
i.assign.lhs.span,
);
}
}
None => self.bind_expr(&i.assign.lhs, inner),
}
self.bind_expr(&i.assign.rhs, inner);
}
self.bind_opt_expr(cond.as_ref(), inner);
for s in step {
match s {
ForStep::Assign(a) => {
self.bind_expr(&a.lhs, inner);
self.bind_expr(&a.rhs, inner);
}
ForStep::Expr(e) => self.bind_expr(e, inner),
}
}
self.bind_stmt(body, inner);
}
StmtKind::While { cond, body } | StmtKind::DoWhile { body, cond, .. } => {
self.bind_expr(cond, scope);
self.bind_stmt(body, scope);
}
StmtKind::Repeat { count, body } => {
self.bind_expr(count, scope);
self.bind_stmt(body, scope);
}
StmtKind::Forever { body } => self.bind_stmt(body, scope),
StmtKind::Foreach { target, vars, body, .. } => {
let inner = self.new_scope(ScopeKind::Block, scope, stmt.span);
self.bind_expr(target, scope);
// A skipped dimension (`arr[, j]`) arrives as an empty name;
// `declare` already ignores those, so it declares nothing.
for v in vars {
let span = v.name.span;
self.declare(inner, &v.name.text, SymbolKind::Var, span, span);
}
self.bind_stmt(body, inner);
}
StmtKind::Assign { lhs, timing, rhs, .. } => {
self.bind_expr(lhs, scope);
if let Some(t) = timing {
self.bind_timing(t, scope);
}
self.bind_expr(rhs, scope);
}
StmtKind::ForceRelease { lhs, rhs, .. } => {
self.bind_expr(lhs, scope);
self.bind_opt_expr(rhs.as_ref(), scope);
}
StmtKind::Timing { control, stmt } => {
self.bind_timing(control, scope);
if let Some(s) = stmt {
self.bind_stmt(s, scope);
}
}
StmtKind::Wait { cond, stmt } => {
self.bind_expr(cond, scope);
if let Some(s) = stmt {
self.bind_stmt(s, scope);
}
}
StmtKind::ExprStmt(e) | StmtKind::EventTrigger(e) => self.bind_expr(e, scope),
StmtKind::VarDecl(d) => self.bind_data_decl(d, scope),
// A block-scoped type binds exactly as an item-level one does; the
// scope it lands in is the enclosing block's, which is what
// IEEE 1800 §6.18 asks for.
StmtKind::Typedef(t) => self.bind_typedef(t, stmt.span, scope),
StmtKind::ParamDecl(p) => {
self.bind_declarators(&p.declarators, param_kind(&p.kw), &p.ty, scope)
}
StmtKind::PortDecl(p) => self.bind_port_decl(p, stmt.span, scope),
StmtKind::Return { expr: e, .. } => self.bind_opt_expr(e.as_ref(), scope),
StmtKind::Disable { name, name_span } => {
// A use, not a declaration, but the parser spans it like any
// other name. Checked before it is recorded, the rule every
// derived span here follows: `disable a . b` written apart
// costs the reference rather than mislocating it.
let ns = self.exact_or_empty(*name_span, name);
if !ns.is_empty() {
self.use_name(scope, name, ns, Target::Lexical);
}
}
StmtKind::Assert { cond, then, els, .. } => {
self.bind_expr(cond, scope);
if let Some(s) = then {
self.bind_stmt(s, scope);
}
if let Some(s) = els {
self.bind_stmt(s, scope);
}
}
StmtKind::Break
| StmtKind::Continue
| StmtKind::WaitFork
| StmtKind::Directive(_)
| StmtKind::MacroStmt(_)
| StmtKind::Null
// Verbatim source: opaque to name resolution, exactly as the
// item-level `ItemKind::Verbatim` is.
| StmtKind::Verbatim(_)
| StmtKind::Error(_) => {}
StmtKind::Assertion(a) => self.bind_assertion(a, scope),
}
}
/// The labels and bodies of a `case`/`randcase` body.
fn bind_case_items(&mut self, items: &'a [CaseItem], scope: ScopeId) {
for it in items {
for l in &it.labels {
self.bind_expr(&l.expr, scope);
}
self.bind_stmt(&it.stmt, scope);
}
}
fn bind_timing(&mut self, t: &'a TimingControl, scope: ScopeId) {
match t {
TimingControl::Delay { expr, .. } => self.bind_expr(expr, scope),
TimingControl::Event(list) => self.bind_sens_list(list, scope),
}
}
/// An `@( ... )` sensitivity list — a statement's timing control, and a
/// clocking block's event, which is the same grammar.
pub(super) fn bind_sens_list(&mut self, list: &SensList, scope: ScopeId) {
match list {
SensList::List { items, .. } => {
for i in items {
self.bind_expr(&i.expr, scope);
// `@(posedge clk iff enable)` reads `enable` as ordinarily
// as it reads `clk`.
if let Some(iff) = &i.iff {
self.bind_expr(iff, scope);
}
}
}
SensList::Star { .. } => {}
// `@name;` without parens — the expression is an ordinary use.
SensList::Bare { expr, .. } => self.bind_expr(expr, scope),
}
}
}