//! The nets, variables and ports a module declares — what the two signal //! naming rules judge. //! //! A module's own declarations are a different population from "every //! declared name in the file": a class property, a loop variable and a //! function local are not signals, and a rule that judged them would be //! reporting on things the style guide says nothing about. Collecting them //! lives here rather than in `visit` (the item and statement spines) or //! `decls` (types and dimensions), so each of the three files stays one //! sentence long. use vlog_parser::ast::{ AnsiPort, GenBlock, GenElse, Item, ItemKind, ListElem, PortList, SourceFile, }; use vlog_parser::span::TextRange; /// One net, variable or port a module declares. pub(crate) struct SignalDecl { /// The declared name, as written. pub(crate) name: String, /// Byte range of that name. pub(crate) span: TextRange, /// The word a diagnostic calls it: `port` or `signal`. pub(crate) what: &'static str, /// The direction of a port, as written (`input`, `output`, `inout`), or /// `None` for a signal. pub(crate) dir: Option, } /// Every net, variable and port declared inside a module, interface or /// program of `file`, header and body alike, generate blocks included. /// /// A `class` body is deliberately not descended into: its properties are not /// signals. A function or task body is not either — a local is scoped to the /// subroutine and named for it. pub(crate) fn module_signals(file: &SourceFile) -> Vec { let mut out = Vec::new(); for item in &file.items { collect_item(item, &mut out); } out } fn collect_item(item: &Item, out: &mut Vec) { match &item.kind { ItemKind::Module(module) => { header_ports(&module.ports, out); for inner in &module.items { collect_body(inner, out); } } // A module may be written inside a package; nothing else at file // level holds one. ItemKind::Package { items, .. } => { for inner in items { collect_item(inner, out); } } _ => {} } } /// One item of a module body. fn collect_body(item: &Item, out: &mut Vec) { match &item.kind { ItemKind::DataDecl(decl) => { for declarator in &decl.declarators { out.push(SignalDecl { name: declarator.name.clone(), span: declarator.name_span, what: "signal", dir: None, }); } } ItemKind::PortDecl(decl) => { for declarator in &decl.declarators { out.push(SignalDecl { name: declarator.name.clone(), span: declarator.name_span, what: "port", dir: Some(decl.dir.clone()), }); } } // A nested module is a module of its own. ItemKind::Module(_) => collect_item(item, out), ItemKind::GenerateRegion { items, .. } => { for inner in items { collect_body(inner, out); } } ItemKind::GenBlock(b) => block(b, out), ItemKind::GenIf(gen_if) => { block(&gen_if.then, out); let mut els = gen_if.els.as_deref(); while let Some(branch) = els { match branch { GenElse::If(_, nested) => { block(&nested.then, out); els = nested.els.as_deref(); } GenElse::Block(inner) => { block(inner, out); els = None; } } } } ItemKind::GenFor(gen_for) => block(&gen_for.body, out), ItemKind::GenCase(gen_case) => { for case_item in &gen_case.items { block(&case_item.body, out); } } _ => {} } } fn block(block: &GenBlock, out: &mut Vec) { for item in &block.items { collect_body(item, out); } } /// The ports of a module header, in either style. fn header_ports(ports: &PortList, out: &mut Vec) { match ports { PortList::Ansi(ports) => { // A port with no direction keyword continues the previous one, as // the language says; a first port with none is `inout` by default. let mut carried: Option = None; for port in ports.iter().filter_map(ListElem::ok) { carried = direction(port).or(carried); out.push(SignalDecl { name: port.name.clone(), span: port.name_span, what: "port", dir: carried.clone(), }); } } // A non-ANSI header *names* its ports; the body declares them // (`input clk;`), and that declaration is the one collected — so a // rule reports each port once, where its direction is written. PortList::NonAnsi(_) | PortList::None | PortList::Empty => {} } } fn direction(port: &AnsiPort) -> Option { port.dir.clone() } #[cfg(test)] mod tests { use super::*; use vlog_parser::parse_source_tolerant; fn signals(src: &str) -> Vec<(String, &'static str, Option)> { let (file, _) = parse_source_tolerant(src); module_signals(&file).into_iter().map(|decl| (decl.name, decl.what, decl.dir)).collect() } #[test] fn collects_header_ports_body_ports_and_signals() { let src = "module m (input logic clk, output data);\n\ \x20 wire a;\n\ \x20 logic [7:0] b, c;\n\ endmodule\n"; assert_eq!( signals(src), vec![ ("clk".to_string(), "port", Some("input".to_string())), ("data".to_string(), "port", Some("output".to_string())), ("a".to_string(), "signal", None), ("b".to_string(), "signal", None), ("c".to_string(), "signal", None), ] ); } #[test] fn a_port_with_no_direction_continues_the_previous_one() { let src = "module m (input logic a, b, output c);\nendmodule\n"; let dirs: Vec> = signals(src).into_iter().map(|s| s.2).collect(); assert_eq!( dirs, vec![Some("input".to_string()), Some("input".to_string()), Some("output".to_string())] ); } /// A non-ANSI port is declared once, in the body: the header only lists /// the names, and collecting those too would report every such port /// twice. #[test] fn a_non_ansi_port_is_collected_where_the_body_declares_it() { let src = "module m (a, b);\n input a;\n output b;\nendmodule\n"; assert_eq!( signals(src), vec![ ("a".to_string(), "port", Some("input".to_string())), ("b".to_string(), "port", Some("output".to_string())), ] ); } #[test] fn generate_blocks_are_descended_into_and_class_bodies_are_not() { let src = "module m;\n\ \x20 generate\n\ \x20 if (A) begin : g\n wire in_gen;\n end\n\ \x20 endgenerate\n\ \x20 for (genvar i = 0; i < 2; i++) begin : l\n wire looped;\n end\n\ endmodule\n\ class c;\n logic property_of_a_class;\nendclass\n"; let names: Vec = signals(src).into_iter().map(|s| s.0).collect(); assert_eq!(names, vec!["in_gen".to_string(), "looped".to_string()]); } #[test] fn an_interface_and_a_module_in_a_package_are_covered() { let src = "interface i (input clk);\n wire w;\nendinterface\n\ package p;\n module inner;\n wire deep;\n endmodule\nendpackage\n"; let names: Vec = signals(src).into_iter().map(|s| s.0).collect(); assert_eq!(names, vec!["clk".to_string(), "w".to_string(), "deep".to_string()]); } #[test] fn a_function_local_is_not_a_signal() { let src = "module m;\n function void f();\n logic local_var;\n endfunction\n\ endmodule\n"; assert!(signals(src).is_empty()); } }