Files
lucisv/crates/vlog_parser/src/ast/sva.rs
T
lucioandClaude Fable 5 9b93f33ae3 Decomposition, lane A: vlog_parser's parser.rs and ast.rs become twenty-seven modules, a pure move
TODO section 28, lane A (the file-size rule in CLAUDE.md).
parser.rs (8 786 production + 5 044 test lines) becomes parser/{mod,
cursor, recover, directive, split, verbatim, item, module, decl, tf,
instance, stmt, keyword_stmt, expr, primary, class, covergroup,
assertion, sva}.rs with the tests regrouped into parser/tests/ (14
files, 309 tests, same names and bodies); ast.rs (3 313) becomes
ast/{mod, module, decl, stmt, expr, class, covergroup, sva}.rs, every
type re-exported so vlog_parser::ast::X is unchanged. Nothing over 800
production lines; the two keyword dispatch tables kept whole and
documented as such, parse_keyword_stmt in a file of its own. Carved by
a line-range manifest that had to concatenate back to the original
byte for byte before writing; 149 methods and 18 items pub(super) as
the compiler demanded. Proof: per-binary test counts identical
(workspace 3 215 / 48), 4 807 corpus + opentitan files byte-identical
to the v0.33.0 binary, cargo doc item list unchanged, and a multiset +
signature census done by the dev, redone by QA, re-run by the lead.

docs/features/decomposition-parser/README.md.

Co-Authored-By: Claude Fable 5 <[email protected]>
2026-09-02 21:13:32 -07:00

364 lines
13 KiB
Rust

//! Assertions (IEEE 1800-2017 §16): `property`/`sequence` declarations,
//! concurrent assertion items, and the property-expression tree.
use super::*;
/// A `property name (...); ... endproperty [: name]` declaration and its
/// `sequence` / `endsequence` twin (IEEE 1800 §16.8, §16.12).
///
/// A body the SVA grammar does not recognise never reaches this node:
/// [`Parser::parse_prop_seq_decl`](crate::parser::Parser) rewinds and produces
/// the [`ItemKind::Verbatim`] leaf a declaration has been since the
/// `assertions` round, byte for byte. See
/// `docs/features/sva-bodies/README.md`.
#[derive(Debug)]
pub struct PropSeqDecl {
/// `property` or `sequence`, as written.
pub kw: String,
/// Comments written between the keyword and the name.
pub before_name: Vec<Comment>,
pub name: Name,
/// Comments written between the name and the `(` of the formal list.
pub before_args: Vec<Comment>,
/// The formal arguments; `None` when no parentheses were written.
pub args: Option<TfPorts>,
/// Comments written between the header and its `;`.
pub before_semi: Vec<Comment>,
/// `assertion_variable_declaration`s written before the body
/// (IEEE 1800 §16.12): ordinary data declarations.
pub locals: Vec<Item>,
/// The property spec / sequence expression the declaration stands for.
pub spec: PropSpec,
/// Comments written between the spec and its optional `;`.
pub before_spec_semi: Vec<Comment>,
/// True when a `;` was written after the spec. The LRM makes it optional
/// for a `property` and required for a `sequence`; both spellings occur,
/// so whichever was written is what prints.
pub spec_semi: bool,
/// Comments written between the spec and the closing keyword.
pub dangling: Vec<Comment>,
/// Comments written in the two gaps of `endproperty : p`.
pub end_label_comments: Box<LabelComments>,
pub end_label: Option<Name>,
}
/// A concurrent assertion statement: `[label :] assert|assume|cover|restrict
/// property ( spec ) action_block`, `cover sequence ( spec ) …` and the
/// `expect ( spec ) …` statement (IEEE 1800 §16.14, §16.17).
///
/// A spec the SVA grammar does not recognise never reaches this node, for the
/// reason [`PropSeqDecl`] gives.
#[derive(Debug)]
pub struct AssertionDecl {
/// The `lbl :` written in front, and the comments in its two gaps.
pub label: Option<Name>,
pub label_comments: Box<LabelComments>,
/// `assert`, `assume`, `cover`, `restrict` or `expect`, as written.
pub kw: String,
/// Comments written between that keyword and `property`/`sequence`.
pub after_kw: Vec<Comment>,
/// `property` or `sequence`; empty for the `expect` spelling, which
/// writes neither.
pub kw2: String,
/// Comments written between `property`/`sequence` and the `(`.
pub before_open: Vec<Comment>,
/// Comments written just inside the `(`.
pub after_open: Vec<Comment>,
pub spec: PropSpec,
/// Comments written just before the `)`.
pub before_close: Vec<Comment>,
pub action: ActionBlock,
}
/// The `action_block` after a concurrent assertion's spec (IEEE 1800 §16.14):
/// `;`, a pass statement, `else` a fail statement, or both.
#[derive(Debug, Default)]
pub struct ActionBlock {
/// Comments written between the `)` and the `;` of the bare spelling.
pub before: Vec<Comment>,
/// The pass statement. `None` for the bare `;` spelling and for the
/// `else`-only spelling.
pub pass: Option<Box<Stmt>>,
/// Comments written between the pass statement and `else`.
pub before_else: Vec<Comment>,
/// Comments written between `else` and the fail statement.
pub after_else: Vec<Comment>,
pub fail: Option<Box<Stmt>>,
}
/// A `property_spec` (IEEE 1800 §16.12): an optional clocking event, an
/// optional `disable iff ( … )`, and the property expression itself.
#[derive(Debug)]
pub struct PropSpec {
/// Comments written in front of the whole spec — between a property
/// declaration's last local variable and its body, say. Empty wherever
/// an enclosing construct already owns that gap (a concurrent
/// assertion's `after_open` does).
pub leading: Vec<Comment>,
/// A leading `@( … )` clocking event.
pub clocking: Option<SensList>,
pub disable: Option<Box<DisableIff>>,
/// Comments written between the header of the spec — its clocking event,
/// its `disable iff`, or the `(`/`;` before it — and the property
/// expression itself.
pub before_expr: Vec<Comment>,
pub expr: PropExpr,
}
/// `disable iff ( expression_or_dist )` (IEEE 1800 §16.12), and the
/// `default disable iff ( … );` item that states one for a whole scope
/// (§16.15).
#[derive(Debug)]
pub struct DisableIff {
/// True when the source wrote the `( … )` around the expression. The
/// clause inside a property spec always does; the `default` item's
/// `expression_or_dist` usually does not.
pub parens: bool,
/// Comments written in front of `disable`: after the spec's clocking
/// event, or after the `default` keyword of the item spelling.
pub before_kw: Vec<Comment>,
/// Comments written between `disable` and `iff`.
pub after_disable: Vec<Comment>,
/// Comments written between `iff` and the `(`.
pub after_iff: Vec<Comment>,
/// Comments written just inside the `(`.
pub after_open: Vec<Comment>,
pub expr: Expr,
/// Comments written just before the `)`.
pub before_close: Vec<Comment>,
/// Comments written between the `)` and the `;` of the `default`
/// spelling. Always empty for the clause inside a property spec, which
/// writes no `;`.
pub before_semi: Vec<Comment>,
}
/// One node of the `sequence_expr` / `property_expr` grammar.
#[derive(Debug)]
pub struct PropExpr {
pub kind: PropKind,
/// Code extent of the node.
pub span: TextRange,
}
/// The shapes a [`PropExpr`] takes. Every payload is boxed: the tree is
/// walked recursively and the variants differ in size by an order of
/// magnitude.
#[derive(Debug)]
pub enum PropKind {
/// An ordinary boolean expression, a sequence instance with its
/// arguments, or `s.triggered` / `s.matched` — all of which are the
/// expression grammar lucisv already has.
Expr(Box<Expr>),
/// `( prop [, match_items] )`. Written parentheses are kept as nodes and
/// printed back; the formatter invents none of its own.
Paren(Box<ParenProp>),
/// A binary operator: a word (`and`, `throughout`, `until`), a symbol
/// (`|->`, `#=#`) or a cycle delay (`a ##1 b`).
Binary(Box<PropBinary>),
/// A prefix operator word: `not`, `nexttime`, `s_always`,
/// `s_eventually`, …
Unary(Box<PropUnary>),
/// `strong ( seq )`, `weak ( seq )`, `first_match ( seq [, items] )`.
Call(Box<PropCall>),
/// `accept_on ( expr ) prop` and its three siblings.
Abort(Box<PropAbort>),
/// A leading cycle delay: `##1 b`, `##[1:3] b`.
Delay(Box<PropDelay>),
/// A repetition suffix: `b [*3]`, `b [->1:2]`.
Repeat(Box<SvaRepeat>),
/// `if ( expr ) prop [else prop]`.
If(Box<PropIf>),
/// `case ( expr ) … endcase`.
Case(Box<PropCase>),
/// A nested `property_spec`: a clocking event or a `disable iff` written
/// inside parentheses.
Spec(Box<PropSpec>),
}
/// `( prop )`, with the optional `, x = e` sequence match items
/// (IEEE 1800 §16.10).
#[derive(Debug)]
pub struct ParenProp {
/// Comments written just inside the `(`.
pub after_open: Vec<Comment>,
pub expr: PropExpr,
pub items: Vec<MatchItem>,
/// Comments written just before the `)`.
pub before_close: Vec<Comment>,
}
/// One `sequence_match_item`: `x = e`, `x += 1`, `x++` or a subroutine call.
#[derive(Debug)]
pub struct MatchItem {
pub meta: Meta,
/// Comments written between whatever precedes this item and the `,` that
/// introduces it.
pub before_comma: Vec<Comment>,
pub lhs: Expr,
/// The assignment operator, when one was written.
pub op: Option<String>,
/// Comments written on either side of that operator.
pub comments: Box<BinaryComments>,
pub rhs: Option<Expr>,
/// Code extent of the item.
pub span: TextRange,
}
/// A binary operator of the SVA grammar and the comments written beside it.
#[derive(Debug)]
pub struct PropBinary {
pub lhs: PropExpr,
pub op: PropOp,
pub comments: Box<BinaryComments>,
pub rhs: PropExpr,
}
/// Which binary operator a [`PropBinary`] spells.
#[derive(Debug)]
pub enum PropOp {
/// A word or symbol operator printed back exactly as written: `and`,
/// `intersect`, `|->`, `#-#`, …
Word(String),
/// `a ##1 b` — the cycle delay is the operator.
Delay(Box<CycleDelay>),
}
/// A `cycle_delay_range` (IEEE 1800 §16.7): `##3`, `##N`, `##(N+1)`,
/// `##[1:3]`, `##[1:$]`, `##[+]`, `##[*]`.
#[derive(Debug)]
pub struct CycleDelay {
/// Comments written between `##` and what follows it.
pub after_hash: Vec<Comment>,
/// The constant form's expression; `None` for the bracketed forms.
pub expr: Option<Expr>,
/// The bracketed form; `None` for the constant form.
pub range: Option<Box<SvaRange>>,
}
/// A leading cycle delay: `##1 b`.
#[derive(Debug)]
pub struct PropDelay {
pub delay: Box<CycleDelay>,
/// Comments written between the delay and its operand.
pub after_delay: Vec<Comment>,
pub expr: PropExpr,
}
/// The bracketed operand shared by `##[…]`, `[*…]`, `[=…]`, `[->…]` and the
/// optional `[…]` count of `nexttime`/`always`/`eventually`.
#[derive(Debug, Default)]
pub struct SvaRange {
/// Comments written just inside the `[`.
pub after_open: Vec<Comment>,
/// `+` or `*` for the two operand-less shorthands (`[+]`, `[*]`,
/// `##[+]`, `##[*]`); empty otherwise.
pub shorthand: String,
pub lo: Option<Expr>,
/// Comments written between the low bound and the `:`.
pub before_colon: Vec<Comment>,
/// Comments written between the `:` and the high bound.
pub after_colon: Vec<Comment>,
/// The high bound, `$` included; `None` when no `:` was written.
pub hi: Option<Expr>,
/// Comments written just before the `]`.
pub before_close: Vec<Comment>,
}
/// A repetition suffix (IEEE 1800 §16.9.2, §16.9.10): `[*3]`, `[*1:$]`,
/// `[*]`, `[+]`, `[=2]`, `[->1:2]`.
#[derive(Debug)]
pub struct SvaRepeat {
pub expr: PropExpr,
/// Comments written between the operand and the `[`.
pub before_open: Vec<Comment>,
/// `*`, `=` or `->`; empty for the bare `[+]`, whose `+` is the
/// [`SvaRange::shorthand`].
pub op: String,
/// Comments written between that operator and the bound.
pub after_op: Vec<Comment>,
pub range: Box<SvaRange>,
}
/// A prefix operator word: `not`, `nexttime`, `s_nexttime`, `always`,
/// `s_always`, `eventually`, `s_eventually`.
#[derive(Debug)]
pub struct PropUnary {
pub op: String,
/// Comments written between the operator and its operand — or its `[`.
pub comments: Vec<Comment>,
/// The optional bracketed count of `nexttime [2] p`, `always [1:3] p`.
pub count: Option<Box<SvaRange>>,
/// Comments written between that count's `]` and the operand.
pub after_count: Vec<Comment>,
pub expr: PropExpr,
}
/// `strong ( seq )`, `weak ( seq )` or `first_match ( seq [, items] )`.
#[derive(Debug)]
pub struct PropCall {
pub name: String,
/// Comments written between the name and the `(`.
pub before_open: Vec<Comment>,
/// Comments written just inside the `(`.
pub after_open: Vec<Comment>,
pub expr: PropExpr,
/// `first_match`'s optional trailing match items.
pub items: Vec<MatchItem>,
/// Comments written just before the `)`.
pub before_close: Vec<Comment>,
}
/// `accept_on ( expr ) prop`, and `reject_on` / `sync_accept_on` /
/// `sync_reject_on` (IEEE 1800 §16.12.10).
#[derive(Debug)]
pub struct PropAbort {
pub kw: String,
/// Comments written in the three gaps of `accept_on /*a*/ ( /*b*/ e
/// /*c*/ )`.
pub comments: Box<HeaderComments>,
pub cond: Expr,
pub expr: PropExpr,
}
/// `if ( expr ) prop [else prop]` at property position (IEEE 1800 §16.12.5).
#[derive(Debug)]
pub struct PropIf {
/// The `if`'s three header gaps; `before_body` holds `) /*c*/ prop`.
pub comments: Box<HeaderComments>,
pub cond: Expr,
pub then: PropExpr,
/// Comments written between the then-branch and `else`.
pub before_else: Vec<Comment>,
/// Comments written between `else` and the else-branch.
pub after_else: Vec<Comment>,
pub els: Option<PropExpr>,
}
/// `case ( expr ) … endcase` at property position (IEEE 1800 §16.12.5).
#[derive(Debug)]
pub struct PropCase {
pub comments: Box<HeaderComments>,
pub cond: Expr,
pub items: Vec<PropCaseItem>,
/// Comments written between the last item and `endcase`.
pub dangling: Vec<Comment>,
}
/// One arm of a property `case`.
#[derive(Debug)]
pub struct PropCaseItem {
pub meta: Meta,
/// The labels written before the `:`; empty for `default`.
pub labels: Vec<ExprItem>,
pub is_default: bool,
/// Comments written between the labels (or `default`) and the `:`.
pub before_colon: Vec<Comment>,
/// Comments written between the `:` and the expression.
pub after_colon: Vec<Comment>,
pub expr: PropExpr,
/// Comments written between the expression and the arm's `;`.
pub before_semi: Vec<Comment>,
/// Code extent of the arm.
pub span: TextRange,
}