TODO §36 (docs/features/small-items-3). Closes topo §2.9-24 and §2.9-25 and the §1 atomic-writes item. 36.1 `ItemKind::GenBlock(Box<GenBlock>)`: in tolerant mode a `begin … end` at module scope or straight inside a generate region is parsed as an unnamed generate block with a diagnostic at the keyword, so the items inside and after it survive; strict mode is untouched. Consumers walk its items; `module-begin-block`, `v2001-generate-begin` and `mismatched-labels` read the node. The formatter's `gen_block` is split by a pure extraction into `begin_block`. One server test that parsed a 3 000-deep begin nest on libtest's thread now runs on PARSE_STACK_SIZE. 36.2 `Parser::value_runs_into_next_instance`, a tolerant-mode lookahead in `parse_connection`: an unclosed `.NAME(` whose value would swallow the next instance as a call fails the element instead; `recover_element` takes the bracket the element stopped on before skipping so the skip stays at the list's level. The balanced-parens spelling stays greedy. 36.3 `lucisv_core::atomic::write_atomic`: sibling temp file + rename, permissions preserved, symlink target replaced, read-only target refused as `fs::write` refused it; used by format, `check --fix` and the cache. Also: TODO housekeeping (QA lines of §27–§35 ticked as recorded in their QA logs; the §27 P6 line and the sva-bodies README checkbox annotated as delivered by §29), topo §2.9-24/25 struck. Co-Authored-By: Claude Fable 5.1 <[email protected]>
427 lines
16 KiB
Rust
427 lines
16 KiB
Rust
//! Tree walks shared by the AST rules.
|
|
//!
|
|
//! Deliberately dumb: every walk visits every node of its kind, and the rule
|
|
//! decides what it cares about. `Error` leaves from a tolerant parse hold raw
|
|
//! text and no children, so they are visited and matched by nothing.
|
|
|
|
use vlog_parser::ast::{
|
|
Fragment, GenBlock, GenElse, Instantiation, Item, ItemKind, ListElem, ParamPort, SourceFile,
|
|
Stmt, StmtKind,
|
|
};
|
|
|
|
/// Calls `f` on every item in the file, nested items included.
|
|
pub(crate) fn walk_items(file: &SourceFile, f: &mut dyn FnMut(&Item)) {
|
|
items(&file.items, f);
|
|
}
|
|
|
|
fn items(list: &[Item], f: &mut dyn FnMut(&Item)) {
|
|
for item in list {
|
|
f(item);
|
|
match &item.kind {
|
|
ItemKind::Module(m) => items(&m.items, f),
|
|
// A class body holds ordinary items — properties, localparams,
|
|
// methods — so every AST rule sees them like any other scope.
|
|
ItemKind::Class(class) => items(&class.items, f),
|
|
ItemKind::Package { items: inner, .. } => items(inner, f),
|
|
ItemKind::GenerateRegion { items: inner, .. } => items(inner, f),
|
|
ItemKind::GenBlock(b) => items(&b.items, f),
|
|
ItemKind::GenIf(gen_if) => {
|
|
gen_block(&gen_if.then, f);
|
|
let mut els = gen_if.els.as_deref();
|
|
while let Some(branch) = els {
|
|
match branch {
|
|
GenElse::If(_, nested) => {
|
|
gen_block(&nested.then, f);
|
|
els = nested.els.as_deref();
|
|
}
|
|
GenElse::Block(block) => {
|
|
gen_block(block, f);
|
|
els = None;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
// A `constraint` body holds constraint items — expressions and
|
|
// nested sets — and neither an `Item` nor a `Stmt`, so there is
|
|
// nothing here for an item or statement walk to descend into.
|
|
// The rules that need to see inside a constraint read the
|
|
// reference table, which since `constraint-bodies` reaches every
|
|
// name in the body. Recorded as its own arm rather than left to
|
|
// the catch-all so the next reader does not re-derive it.
|
|
ItemKind::Constraint(_) => {}
|
|
// A covergroup body holds cover items — coverpoints, crosses,
|
|
// bins, options — and neither an `Item` nor a `Stmt`, so there is
|
|
// nothing here for an item or statement walk to descend into
|
|
// either. The rules that need to see inside read the reference
|
|
// table, which since `covergroup-bodies` reaches every name a
|
|
// covergroup samples.
|
|
ItemKind::Covergroup(_) => {}
|
|
ItemKind::GenFor(gen_for) => gen_block(&gen_for.body, f),
|
|
ItemKind::GenCase(gen_case) => {
|
|
for case_item in &gen_case.items {
|
|
gen_block(&case_item.body, f);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
fn gen_block(block: &GenBlock, f: &mut dyn FnMut(&Item)) {
|
|
items(&block.items, f);
|
|
}
|
|
|
|
/// The declaration an item was written inside — what a rule needs when the
|
|
/// same construct is right in one scope and wrong in another.
|
|
///
|
|
/// A generate construct is not a scope here: `localparam` inside a
|
|
/// `generate` block is still written in the module that holds it, and both
|
|
/// rules that ask this question (`proper-parameter-declaration`, the two
|
|
/// `explicit-*-lifetime`) mean the enclosing *declaration*, not the block.
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
|
pub(crate) enum ItemScope {
|
|
/// Written at file scope, outside every declaration.
|
|
File,
|
|
/// Inside a `module`, `interface` or `program` body.
|
|
Module,
|
|
/// Inside a `package` body.
|
|
Package,
|
|
/// Inside a `class` body.
|
|
Class,
|
|
}
|
|
|
|
/// Calls `f` on every item of the file, saying which declaration holds it.
|
|
pub(crate) fn walk_items_scoped(file: &SourceFile, f: &mut dyn FnMut(ItemScope, &Item)) {
|
|
scoped_items(&file.items, ItemScope::File, f);
|
|
}
|
|
|
|
fn scoped_items(list: &[Item], scope: ItemScope, f: &mut dyn FnMut(ItemScope, &Item)) {
|
|
for item in list {
|
|
f(scope, item);
|
|
match &item.kind {
|
|
ItemKind::Module(m) => scoped_items(&m.items, ItemScope::Module, f),
|
|
ItemKind::Class(class) => scoped_items(&class.items, ItemScope::Class, f),
|
|
ItemKind::Package { items: inner, .. } => scoped_items(inner, ItemScope::Package, f),
|
|
// A generate construct keeps the scope it was written in.
|
|
ItemKind::GenerateRegion { items: inner, .. } => scoped_items(inner, scope, f),
|
|
ItemKind::GenBlock(b) => scoped_items(&b.items, scope, f),
|
|
ItemKind::GenIf(gen_if) => {
|
|
scoped_items(&gen_if.then.items, scope, f);
|
|
let mut els = gen_if.els.as_deref();
|
|
while let Some(branch) = els {
|
|
match branch {
|
|
GenElse::If(_, nested) => {
|
|
scoped_items(&nested.then.items, scope, f);
|
|
els = nested.els.as_deref();
|
|
}
|
|
GenElse::Block(block) => {
|
|
scoped_items(&block.items, scope, f);
|
|
els = None;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ItemKind::GenFor(gen_for) => scoped_items(&gen_for.body.items, scope, f),
|
|
ItemKind::GenCase(gen_case) => {
|
|
for case_item in &gen_case.items {
|
|
scoped_items(&case_item.body.items, scope, f);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
}
|
|
|
|
/// Calls `f` on every statement of the file, nested statements included.
|
|
pub(crate) fn walk_all_stmts(file: &SourceFile, f: &mut dyn FnMut(&Stmt)) {
|
|
// A statement fragment is a whole file of statements with no item around
|
|
// them (`docs/features/fragment-mode/README.md`). Every statement rule
|
|
// applies to them exactly as it does inside an `always` body — the code is
|
|
// the same code, it is just `` `include``d rather than written in place.
|
|
// The other two fragment shapes hold no `Item` and no `Stmt`, so an item
|
|
// or statement walk has nothing to descend into, which is the posture a
|
|
// constraint and a covergroup body already keep.
|
|
if let Some(Fragment::Statements(stmts)) = &file.fragment {
|
|
for stmt in stmts {
|
|
walk_stmts(stmt, f);
|
|
}
|
|
}
|
|
walk_items(file, &mut |item| match &item.kind {
|
|
ItemKind::Always { stmt, .. } | ItemKind::Initial(stmt) | ItemKind::Final(stmt) => {
|
|
walk_stmts(stmt, f)
|
|
}
|
|
ItemKind::Function(decl) => stmts(&decl.stmts, f),
|
|
ItemKind::Task(decl) => stmts(&decl.stmts, f),
|
|
_ => {}
|
|
});
|
|
}
|
|
|
|
/// Calls `f` on `stmt` and every statement inside it.
|
|
pub(crate) fn walk_stmts(stmt: &Stmt, f: &mut dyn FnMut(&Stmt)) {
|
|
f(stmt);
|
|
match &stmt.kind {
|
|
StmtKind::Block { stmts: inner, .. } | StmtKind::ForkJoin { stmts: inner, .. } => {
|
|
stmts(inner, f)
|
|
}
|
|
StmtKind::If { then, els, .. } => {
|
|
walk_stmts(then, f);
|
|
if let Some(els) = els {
|
|
walk_stmts(els, f);
|
|
}
|
|
}
|
|
StmtKind::Case { items, .. } | StmtKind::RandCase { items, .. } => {
|
|
for case_item in items {
|
|
walk_stmts(&case_item.stmt, f);
|
|
}
|
|
}
|
|
// A label wraps the statement it names; the rules care about the
|
|
// statement, not the label.
|
|
StmtKind::Labeled { stmt: inner, .. } => walk_stmts(inner, f),
|
|
StmtKind::For { body, .. }
|
|
| StmtKind::While { body, .. }
|
|
| StmtKind::DoWhile { body, .. }
|
|
| StmtKind::Repeat { body, .. }
|
|
| StmtKind::Forever { body }
|
|
| StmtKind::Foreach { body, .. } => walk_stmts(body, f),
|
|
StmtKind::Timing { stmt: Some(inner), .. } | StmtKind::Wait { stmt: Some(inner), .. } => {
|
|
walk_stmts(inner, f)
|
|
}
|
|
StmtKind::Assert { then, els, .. } => {
|
|
if let Some(then) = then {
|
|
walk_stmts(then, f);
|
|
}
|
|
if let Some(els) = els {
|
|
walk_stmts(els, f);
|
|
}
|
|
}
|
|
_ => {}
|
|
}
|
|
}
|
|
|
|
fn stmts(list: &[Stmt], f: &mut dyn FnMut(&Stmt)) {
|
|
for stmt in list {
|
|
walk_stmts(stmt, f);
|
|
}
|
|
}
|
|
|
|
/// Calls `f` on every generate block of the file — the `begin … end` (or the
|
|
/// single item standing for one) of an `if`, `for` or `case` generate,
|
|
/// nested blocks included.
|
|
pub(crate) fn walk_generate_blocks(file: &SourceFile, f: &mut dyn FnMut(&GenBlock)) {
|
|
walk_items(file, &mut |item| match &item.kind {
|
|
ItemKind::GenIf(gen_if) => {
|
|
f(&gen_if.then);
|
|
let mut els = gen_if.els.as_deref();
|
|
while let Some(branch) = els {
|
|
match branch {
|
|
GenElse::If(_, nested) => {
|
|
f(&nested.then);
|
|
els = nested.els.as_deref();
|
|
}
|
|
GenElse::Block(block) => {
|
|
f(block);
|
|
els = None;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
ItemKind::GenFor(gen_for) => f(&gen_for.body),
|
|
ItemKind::GenCase(gen_case) => {
|
|
for case_item in &gen_case.items {
|
|
f(&case_item.body);
|
|
}
|
|
}
|
|
_ => {}
|
|
});
|
|
}
|
|
|
|
/// Calls `f` on every `#( ... )` parameter port of the file — a module header's
|
|
/// and a class header's alike, since both spell them with the same node.
|
|
pub(crate) fn walk_param_ports(file: &SourceFile, f: &mut dyn FnMut(&ParamPort)) {
|
|
walk_items(file, &mut |item| {
|
|
let ports = match &item.kind {
|
|
ItemKind::Module(module) => &module.param_ports,
|
|
ItemKind::Class(class) => &class.param_ports,
|
|
_ => return,
|
|
};
|
|
// An error element is a parameter a tolerant parse could not read: it
|
|
// has no name and no type, so there is nothing for a rule to judge.
|
|
for port in ports.iter().flatten().filter_map(ListElem::ok) {
|
|
f(port);
|
|
}
|
|
});
|
|
}
|
|
|
|
/// Calls `f` on every module instantiation of the file — a plain one, and the
|
|
/// one a `bind` directive carries, which is an ordinary instantiation written
|
|
/// somewhere else.
|
|
pub(crate) fn walk_instantiations(file: &SourceFile, f: &mut dyn FnMut(&Instantiation)) {
|
|
walk_items(file, &mut |item| match &item.kind {
|
|
ItemKind::Instantiation(inst) => f(inst),
|
|
ItemKind::Bind(bind) => f(&bind.inst),
|
|
_ => {}
|
|
});
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use vlog_parser::parse_source_tolerant;
|
|
|
|
fn count_assignments(src: &str) -> usize {
|
|
let (file, _) = parse_source_tolerant(src);
|
|
let mut n = 0;
|
|
walk_all_stmts(&file, &mut |stmt| {
|
|
if matches!(stmt.kind, StmtKind::Assign { .. }) {
|
|
n += 1;
|
|
}
|
|
});
|
|
n
|
|
}
|
|
|
|
#[test]
|
|
fn reaches_statements_through_every_nesting() {
|
|
let src = "module m;\n\
|
|
always_ff @(posedge clk) begin\n\
|
|
if (a) x <= 1; else x <= 2;\n\
|
|
case (y)\n\
|
|
0: for (int i = 0; i < 2; i++) z <= i;\n\
|
|
default: z <= 0;\n\
|
|
endcase\n\
|
|
end\n\
|
|
endmodule\n";
|
|
assert_eq!(count_assignments(src), 4);
|
|
}
|
|
|
|
#[test]
|
|
fn reaches_items_inside_generate() {
|
|
let src = "module m;\n\
|
|
generate\n\
|
|
if (A) begin : g\n\
|
|
always_comb x = 1;\n\
|
|
end else begin\n\
|
|
always_comb x = 2;\n\
|
|
end\n\
|
|
endgenerate\n\
|
|
endmodule\n";
|
|
assert_eq!(count_assignments(src), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn reaches_the_statements_of_a_statement_fragment() {
|
|
// A `.svh` that is nothing but the middle of a procedural block has no
|
|
// item around its statements, so the item walk cannot reach them —
|
|
// without the fragment arm every statement rule goes silent on the
|
|
// file. See `docs/features/fragment-mode/README.md`.
|
|
let src = "x = 1;\nif (a) begin\n y = 2;\nend\n";
|
|
let (file, diagnostics) = parse_source_tolerant(src);
|
|
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
|
assert!(file.items.is_empty(), "a fragment file has no items");
|
|
assert_eq!(count_assignments(src), 2);
|
|
}
|
|
|
|
#[test]
|
|
fn reaches_items_and_statements_inside_a_class_body() {
|
|
let src = "class c;\n\
|
|
localparam int A = 1;\n\
|
|
function void f(int x);\n\
|
|
case (x)\n\
|
|
1: a = 1;\n\
|
|
endcase\n\
|
|
endfunction\n\
|
|
endclass\n";
|
|
let (file, diagnostics) = parse_source_tolerant(src);
|
|
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
|
let mut params = 0;
|
|
let mut cases = 0;
|
|
walk_items(&file, &mut |item| {
|
|
if matches!(item.kind, ItemKind::ParamDecl(_)) {
|
|
params += 1;
|
|
}
|
|
});
|
|
walk_all_stmts(&file, &mut |stmt| {
|
|
if matches!(stmt.kind, StmtKind::Case { .. }) {
|
|
cases += 1;
|
|
}
|
|
});
|
|
assert_eq!((params, cases), (1, 1));
|
|
}
|
|
|
|
#[test]
|
|
fn reaches_a_class_nested_in_a_package_and_a_class() {
|
|
let src = "package p;\n\
|
|
class outer;\n\
|
|
class inner;\n\
|
|
localparam int A = 1;\n\
|
|
endclass\n\
|
|
endclass\n\
|
|
endpackage\n";
|
|
let (file, _) = parse_source_tolerant(src);
|
|
let mut params = 0;
|
|
walk_items(&file, &mut |item| {
|
|
if matches!(item.kind, ItemKind::ParamDecl(_)) {
|
|
params += 1;
|
|
}
|
|
});
|
|
assert_eq!(params, 1);
|
|
}
|
|
|
|
#[test]
|
|
fn parameter_ports_of_both_module_and_class_headers_are_reached() {
|
|
let src = "module m #(parameter int A = 1) ();\nendmodule\n\
|
|
class c #(type T = int);\nendclass\n";
|
|
let (file, _) = parse_source_tolerant(src);
|
|
let mut names = Vec::new();
|
|
walk_param_ports(&file, &mut |port| names.push(port.name.clone()));
|
|
assert_eq!(names, vec!["A".to_string(), "T".to_string()]);
|
|
}
|
|
|
|
/// The scope a rule asks about is the enclosing *declaration*, and a
|
|
/// generate construct passes the one it was written in through.
|
|
#[test]
|
|
fn every_item_is_told_which_declaration_holds_it() {
|
|
let src = "localparam int AtFile = 0;\n\
|
|
package p;\n\
|
|
localparam int InPackage = 0;\n\
|
|
class c;\n localparam int InClass = 0;\n endclass\n\
|
|
endpackage\n\
|
|
module m;\n\
|
|
localparam int InModule = 0;\n\
|
|
if (A) begin : g\n localparam int InGenerate = 0;\n end\n\
|
|
endmodule\n";
|
|
let (file, diagnostics) = parse_source_tolerant(src);
|
|
assert!(diagnostics.is_empty(), "{diagnostics:?}");
|
|
let mut seen: Vec<(String, ItemScope)> = Vec::new();
|
|
walk_items_scoped(&file, &mut |scope, item| {
|
|
if let ItemKind::ParamDecl(decl) = &item.kind {
|
|
for declarator in &decl.declarators {
|
|
seen.push((declarator.name.clone(), scope));
|
|
}
|
|
}
|
|
});
|
|
assert_eq!(
|
|
seen,
|
|
vec![
|
|
("AtFile".to_string(), ItemScope::File),
|
|
("InPackage".to_string(), ItemScope::Package),
|
|
("InClass".to_string(), ItemScope::Class),
|
|
("InModule".to_string(), ItemScope::Module),
|
|
("InGenerate".to_string(), ItemScope::Module),
|
|
]
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn an_unparsable_region_is_visited_but_holds_nothing() {
|
|
let (file, diagnostics) = parse_source_tolerant("module m;\n @@@;\nendmodule\n");
|
|
assert!(!diagnostics.is_empty());
|
|
let mut errors = 0;
|
|
walk_items(&file, &mut |item| {
|
|
if matches!(item.kind, ItemKind::Error(_)) {
|
|
errors += 1;
|
|
}
|
|
});
|
|
assert!(errors > 0, "the error leaf must be reachable");
|
|
}
|
|
}
|