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]>
235 lines
8.4 KiB
Rust
235 lines
8.4 KiB
Rust
//! 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<String>,
|
|
}
|
|
|
|
/// 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<SignalDecl> {
|
|
let mut out = Vec::new();
|
|
for item in &file.items {
|
|
collect_item(item, &mut out);
|
|
}
|
|
out
|
|
}
|
|
|
|
fn collect_item(item: &Item, out: &mut Vec<SignalDecl>) {
|
|
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<SignalDecl>) {
|
|
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<SignalDecl>) {
|
|
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<SignalDecl>) {
|
|
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<String> = 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<String> {
|
|
port.dir.clone()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use vlog_parser::parse_source_tolerant;
|
|
|
|
fn signals(src: &str) -> Vec<(String, &'static str, Option<String>)> {
|
|
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<Option<String>> = 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<String> = 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<String> = 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());
|
|
}
|
|
}
|