//! Every expression written in a file. //! //! `visit` walks the item and statement spines and `decls` the types and //! dimensions; neither reaches *into* an expression, and a rule that judges //! the shape of one (`suggest-parentheses`) needs every node of every tree, //! wherever it was written. Collecting them lives here so each of the three //! files stays one sentence long. use vlog_parser::ast::{ Connections, Expr, ExprKind, ForStep, Instantiation, ItemKind, ListElem, SourceFile, StmtKind, TimingControl, }; use crate::visit::{walk_all_stmts, walk_items, walk_param_ports}; /// Calls `f` on every expression of the file, sub-expressions included. /// /// The **roots** are the places an expression is written: a continuous or /// procedural assignment, a declaration's initialiser, a condition, a call /// argument, an instantiation's connections, a generate header, an expression /// statement. Sub-expressions are then reached through every composite /// [`ExprKind`], so a node buried in a concatenation inside a call argument is /// visited like any other. pub(crate) fn walk_exprs(file: &SourceFile, f: &mut dyn FnMut(&Expr)) { let mut root = |expr: &Expr| descend(expr, f); walk_param_ports(file, &mut |port| { if let Some(default) = &port.default { root(default); } }); walk_items(file, &mut |item| match &item.kind { ItemKind::ContAssign(assign) => { if let Some(delay) = &assign.delay { root(delay); } for pair in &assign.assigns { root(&pair.lhs); root(&pair.rhs); } } ItemKind::ParamDecl(decl) => initialisers(&decl.declarators, &mut root), ItemKind::DataDecl(decl) => initialisers(&decl.declarators, &mut root), ItemKind::PortDecl(decl) => initialisers(&decl.declarators, &mut root), ItemKind::ElabTask(expr) => root(expr), ItemKind::Instantiation(inst) => connections(inst, &mut root), ItemKind::Bind(bind) => connections(&bind.inst, &mut root), ItemKind::GenIf(gen_if) => root(&gen_if.cond), ItemKind::GenFor(gen_for) => { root(&gen_for.init.lhs); root(&gen_for.init.rhs); root(&gen_for.cond); match &gen_for.step { ForStep::Assign(assign) => { root(&assign.lhs); root(&assign.rhs); } ForStep::Expr(expr) => root(expr), } } ItemKind::GenCase(gen_case) => { root(&gen_case.expr); for case_item in &gen_case.items { for label in &case_item.labels { root(&label.expr); } } } ItemKind::Defparam(pairs) => { for (target, value) in pairs { root(target); root(value); } } ItemKind::Alias(terms) => { for term in terms { root(term); } } _ => {} }); walk_all_stmts(file, &mut |stmt| match &stmt.kind { StmtKind::Assign { lhs, rhs, timing, .. } => { root(lhs); root(rhs); if let Some(TimingControl::Delay { expr, .. }) = timing { root(expr); } } StmtKind::ExprStmt(expr) => root(expr), StmtKind::EventTrigger(t) => root(&t.expr), StmtKind::If { cond, .. } => root(cond), StmtKind::Case { expr, items, .. } => { root(expr); for case_item in items { for label in &case_item.labels { root(&label.expr); } } } StmtKind::RandCase { items, .. } => { for case_item in items { for label in &case_item.labels { root(&label.expr); } } } StmtKind::For { init, cond, step, .. } => { for one in init { root(&one.assign.lhs); root(&one.assign.rhs); } if let Some(cond) = cond { root(cond); } for one in step { match one { ForStep::Assign(assign) => { root(&assign.lhs); root(&assign.rhs); } ForStep::Expr(expr) => root(expr), } } } StmtKind::While { cond, .. } | StmtKind::DoWhile { cond, .. } => root(cond), StmtKind::Repeat { count, .. } => root(count), StmtKind::Foreach { target, .. } => root(target), StmtKind::Wait { cond, .. } => root(cond), // A sensitivity list holds edge terms, not values; only the `#delay` // spelling carries an expression a rule would judge. StmtKind::Timing { control: TimingControl::Delay { expr, .. }, .. } => root(expr), StmtKind::ForceRelease { lhs, rhs, .. } => { root(lhs); if let Some(rhs) = rhs { root(rhs); } } StmtKind::Return { expr: Some(expr), .. } => root(expr), StmtKind::VarDecl(decl) => initialisers(&decl.declarators, &mut root), StmtKind::ParamDecl(decl) => initialisers(&decl.declarators, &mut root), StmtKind::PortDecl(decl) => initialisers(&decl.declarators, &mut root), _ => {} }); } fn initialisers(declarators: &[vlog_parser::ast::Declarator], root: &mut dyn FnMut(&Expr)) { for declarator in declarators { if let Some(init) = &declarator.init { root(init); } } } fn connections(inst: &Instantiation, root: &mut dyn FnMut(&Expr)) { if let Some(params) = &inst.params { for conn in params.iter().filter_map(ListElem::ok) { if let Some(expr) = &conn.expr { root(expr); } } } for instance in &inst.instances { match &instance.conns { Connections::Named(conns) => { for conn in conns.iter().filter_map(ListElem::ok) { if let Some(expr) = &conn.expr { root(expr); } } } Connections::Ordered(conns) => { for conn in conns.iter().filter_map(ListElem::ok) { if let Some(expr) = &conn.expr { root(expr); } } } } } } /// One expression and everything inside it. fn descend(expr: &Expr, f: &mut dyn FnMut(&Expr)) { f(expr); match &expr.kind { ExprKind::Unary { expr: inner, .. } | ExprKind::Postfix { expr: inner, .. } | ExprKind::New(inner) | ExprKind::Paren { inner, .. } => descend(inner, f), ExprKind::Binary { lhs, rhs, .. } => { descend(lhs, f); descend(rhs, f); } ExprKind::Ternary { cond, then, els, .. } => { descend(cond, f); descend(then, f); descend(els, f); } ExprKind::Call { callee, args, .. } => { descend(callee, f); for arg in args { descend(&arg.expr, f); } } ExprKind::Index { base, index, .. } => { descend(base, f); descend(index, f); } ExprKind::Range { base, msb, lsb, .. } => { descend(base, f); descend(msb, f); descend(lsb, f); } ExprKind::Member { base, .. } | ExprKind::Scope { base, .. } | ExprKind::Parameterized { base, .. } => descend(base, f), ExprKind::Concat(items) => { for item in items { descend(&item.expr, f); } } ExprKind::Repl { count, inner } | ExprKind::AssignPatternRepl { count, inner } => { descend(count, f); for item in inner { descend(&item.expr, f); } } ExprKind::Stream { slice, inner, .. } => { if let Some(slice) = slice { descend(slice, f); } for item in inner { descend(&item.expr, f); } } ExprKind::Cast { ty, expr: inner, .. } => { descend(ty, f); descend(inner, f); } ExprKind::Inside { lhs, ranges, .. } => { descend(lhs, f); for item in ranges { descend(&item.expr, f); } } ExprKind::InsideMacro { lhs, .. } => descend(lhs, f), ExprKind::With { base, .. } | ExprKind::WithConstraint { base, .. } => descend(base, f), // The stream range's operands are reads like any index's. ExprKind::StreamWith { base, range } => { descend(base, f); descend(&range.msb, f); if let Some(lsb) = &range.lsb { descend(lsb, f); } } ExprKind::Dist { expr: inner, .. } => descend(inner, f), ExprKind::NamedArg { value, .. } => descend(value, f), ExprKind::AssignPattern(items) => { for item in items { if let Some(key) = &item.key { descend(key, f); } descend(&item.value, f); } } _ => {} } } #[cfg(test)] mod tests { use super::*; use vlog_parser::parse_source_tolerant; fn ternaries(src: &str) -> usize { let (file, _) = parse_source_tolerant(src); let mut n = 0; walk_exprs(&file, &mut |expr| { if matches!(expr.kind, ExprKind::Ternary { .. }) { n += 1; } }); n } #[test] fn reaches_an_expression_written_anywhere() { let src = "module m #(parameter int P = a ? 1 : 2) (\n\ input logic i\n\ );\n\ logic v = b ? 1 : 2;\n\ assign w = c ? 1 : 2;\n\ sub u (.p(d ? 1 : 2));\n\ if (e ? 1 : 2) begin : g\n wire x;\n end\n\ initial begin\n\ if (f ? 1 : 2) y = g_ ? 1 : 2;\n\ z = {h ? 1 : 2, call(i ? 1 : 2)};\n\ end\n\ endmodule\n"; assert_eq!(ternaries(src), 9); } #[test] fn a_nested_expression_is_reached_through_every_composite() { assert_eq!(ternaries("module m;\n assign x = ~(a ? 1 : 2);\nendmodule\n"), 1); assert_eq!(ternaries("module m;\n assign x = y[a ? 1 : 2];\nendmodule\n"), 1); assert_eq!(ternaries("module m;\n assign x = int'(a ? 1 : 2);\nendmodule\n"), 1); assert_eq!(ternaries("module m;\n assign x = {2{a ? 1 : 2}};\nendmodule\n"), 1); } #[test] fn a_file_with_no_expressions_visits_nothing() { assert_eq!(ternaries("module m;\nendmodule\n"), 0); } }