A path segment naming a block (bare, indexed, nested, cross-file) is now a reference to its label: vlog_semantic declares a `for` block's label in the enclosing scope, makes `generate … endgenerate` no scope, gives a block a member scope, walks an index in a path, and files blocks on a module's hierarchical surface under `module.label` (FACTS_FORMAT 21). The LSP, CLI and MCP rename/references get it through the engine; lucisv-mcp's `block` refusal is lifted. No verifier normalization (attacks in check/qa_block_paths.rs, qa_block_attacks.rs). Reworks after QA: unresolved path segments warned in every file of the set; sibling-arm members warned; a label beside a net is a duplicate-declaration; function-local block keys never collide; file names path-qualified when base names collide; rename warnings and lucisv-mcp mentions scoped to the declaration's home set (rename/home.rs, scoped.rs), falling back to unscoped on any doubt. TODO §65.4 and §65.6 filed. docs/features/generate-label-rename. Workspace lib/bins/tests: 79 suites, 5 064 passed, 0 failed, 51 ignored (guard exit 0). Co-Authored-By: Claude Opus 5.5 <[email protected]>
232 lines
8.6 KiB
Rust
232 lines
8.6 KiB
Rust
//! Named blocks as places a path reaches through (TODO §65.1): a generate
|
|
//! block's label is declared where the construct is written, an index into a
|
|
//! generate array is walked as the block itself, and a module's blocks and
|
|
//! their members are on its hierarchical surface in the workspace index.
|
|
//! `docs/features/generate-label-rename/README.md`.
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use vlog_parser::parse_source_tolerant;
|
|
use vlog_semantic::{
|
|
analyze, resolve_external, ExternalTarget, Resolution, Semantics, Surface, SymbolKind,
|
|
WorkspaceIndex,
|
|
};
|
|
|
|
fn sem(src: &str) -> Semantics {
|
|
let (file, _) = parse_source_tolerant(src);
|
|
analyze(&file, src)
|
|
}
|
|
|
|
/// Offset of the `n`th (0-based) occurrence of `needle`, plus `skip` bytes.
|
|
fn at(src: &str, needle: &str, n: usize, skip: usize) -> u32 {
|
|
(src.match_indices(needle).nth(n).expect("occurrence must exist").0 + skip) as u32
|
|
}
|
|
|
|
fn resolution(s: &Semantics, offset: u32) -> Resolution {
|
|
s.reference_at(offset).unwrap_or_else(|| panic!("no reference at {offset}")).resolution.clone()
|
|
}
|
|
|
|
fn declared(s: &Semantics, src: &str, needle: &str, skip: usize) -> Resolution {
|
|
let id = s.symbol_at(at(src, needle, 0, skip)).expect("a declaration there");
|
|
Resolution::Resolved(id)
|
|
}
|
|
|
|
const LOOP: &str = "module fsm (output logic busy);
|
|
for (genvar gi = 0; gi < 2; gi++) begin : g_lane
|
|
logic lane_q;
|
|
end : g_lane
|
|
assign busy = g_lane[0].lane_q | g_lane[gi].lane_q;
|
|
endmodule
|
|
";
|
|
|
|
#[test]
|
|
fn a_for_generate_label_is_a_name_of_the_enclosing_module_and_its_genvar_is_not() {
|
|
let s = sem(LOOP);
|
|
let label = declared(&s, LOOP, "begin : g_lane", 8);
|
|
assert_eq!(resolution(&s, at(LOOP, "g_lane[0]", 0, 0)), label);
|
|
assert_eq!(resolution(&s, at(LOOP, "end : g_lane", 0, 6)), label, "the closing label");
|
|
assert_eq!(resolution(&s, at(LOOP, "g_lane[gi]", 0, 0)), label);
|
|
assert_eq!(resolution(&s, at(LOOP, "[gi]", 0, 1)), Resolution::NotFound, "no genvar leak");
|
|
let lane_q = declared(&s, LOOP, "logic lane_q", 6);
|
|
assert_eq!(resolution(&s, at(LOOP, ".lane_q", 0, 1)), lane_q, "through the index");
|
|
assert_eq!(resolution(&s, at(LOOP, ".lane_q", 1, 1)), lane_q);
|
|
}
|
|
|
|
#[test]
|
|
fn a_nested_block_is_reached_through_its_parent_and_an_index() {
|
|
let src = "module n (output logic o);
|
|
if (1) begin : g_a
|
|
for (genvar j = 0; j < 4; j++) begin : g_b
|
|
logic x;
|
|
end
|
|
end
|
|
assign o = g_a.g_b[2].x;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "g_a.g_b", 0, 4)), declared(&s, src, "begin : g_b", 8));
|
|
assert_eq!(resolution(&s, at(src, "[2].x", 0, 4)), declared(&s, src, "logic x", 6));
|
|
}
|
|
|
|
#[test]
|
|
fn a_generate_region_is_not_a_scope() {
|
|
let src = "module r (output logic o);
|
|
generate
|
|
if (1) begin : g_in
|
|
logic q;
|
|
end
|
|
wire w;
|
|
endgenerate
|
|
assign o = g_in.q | w;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "g_in.q", 0, 0)), declared(&s, src, "begin : g_in", 8));
|
|
assert_eq!(resolution(&s, at(src, "| w", 0, 2)), declared(&s, src, "wire w", 5));
|
|
}
|
|
|
|
#[test]
|
|
fn both_arms_of_a_generate_if_are_one_label_closing_labels_included() {
|
|
let src = "module d (output logic o);
|
|
if (1) begin : g
|
|
logic q;
|
|
end : g
|
|
else begin : g
|
|
logic q;
|
|
end : g
|
|
assign o = g.q;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let first = declared(&s, src, "begin : g", 8);
|
|
for (needle, n) in [("begin : g", 1), ("end : g", 0), ("end : g", 1)] {
|
|
let skip = needle.len() - 1;
|
|
assert_eq!(resolution(&s, at(src, needle, n, skip)), first, "{needle} #{n}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_prefix_statement_label_owns_its_closing_label() {
|
|
let src = "module p;\n initial lbl: begin\n disable lbl;\n end : lbl\nendmodule\n";
|
|
let s = sem(src);
|
|
let label = declared(&s, src, "lbl:", 0);
|
|
assert_eq!(resolution(&s, at(src, "disable lbl", 0, 8)), label);
|
|
assert_eq!(resolution(&s, at(src, "end : lbl", 0, 6)), label);
|
|
}
|
|
|
|
#[test]
|
|
fn an_index_is_walked_only_where_the_link_has_members_of_its_own() {
|
|
let src = "module t (output logic o);
|
|
typedef struct packed { logic f; } pkt_t;
|
|
pkt_t [1:0] packed_arr;
|
|
pkt_t unpacked_arr [2];
|
|
assign o = packed_arr[0].f | unpacked_arr[1].f;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "[0].f", 0, 4)), declared(&s, src, "logic f", 6));
|
|
assert!(s.reference_at(at(src, "[1].f", 0, 4)).is_none(), "an unpacked element is untyped");
|
|
}
|
|
|
|
#[test]
|
|
fn a_blocks_member_key_is_its_modules_and_its_labels() {
|
|
let src = "module sub;\n if (1) begin : g_a\n if (1) begin : g_b\n logic x;\n end\n end\nendmodule\n";
|
|
let s = sem(src);
|
|
let key_of = |name: &str| {
|
|
let id = s.symbols_named(name).next().expect("declared");
|
|
s.symbol(id).and_then(|sym| s.member_key(sym.scope))
|
|
};
|
|
assert_eq!(key_of("g_a").as_deref(), Some("sub"));
|
|
assert_eq!(key_of("g_b").as_deref(), Some("sub.g_a"));
|
|
assert_eq!(key_of("x").as_deref(), Some("sub.g_a.g_b"));
|
|
}
|
|
|
|
const SUB: &str = "module sub (input logic clk);
|
|
for (genvar i = 0; i < 2; i++) begin : g_lane
|
|
logic sig;
|
|
if (1) begin : g_in
|
|
logic deep;
|
|
end
|
|
end
|
|
endmodule
|
|
";
|
|
|
|
const TOP: &str = "module top (output logic o);
|
|
sub u_x (.clk(1'b0));
|
|
assign o = u_x.g_lane[1].sig | u_x.g_lane[0].g_in.deep;
|
|
endmodule
|
|
";
|
|
|
|
fn index() -> WorkspaceIndex {
|
|
WorkspaceIndex::build([("sub.sv", SUB), ("top.sv", TOP)].map(|(p, s)| (PathBuf::from(p), s)))
|
|
}
|
|
|
|
/// The one declaration the overlay answers the reference at `offset` with,
|
|
/// as `(kind, the name it spells in SUB)`.
|
|
fn external(s: &Semantics, ws: &WorkspaceIndex, offset: u32) -> (SymbolKind, String) {
|
|
let overlay = resolve_external(s, ws);
|
|
let id = s.reference_id_at(offset).expect("a reference there");
|
|
match overlay.get(id) {
|
|
Some(ExternalTarget::Resolved(def)) => {
|
|
(def.kind, SUB[def.name_span.as_range()].to_string())
|
|
}
|
|
other => panic!("not resolved: {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_path_from_another_file_reaches_the_block_and_its_members() {
|
|
let ws = index();
|
|
let s = sem(TOP);
|
|
let block = (SymbolKind::Block, "g_lane".to_string());
|
|
assert_eq!(external(&s, &ws, at(TOP, "u_x.g_lane", 0, 4)), block);
|
|
assert_eq!(external(&s, &ws, at(TOP, "u_x.g_lane", 1, 4)), block);
|
|
assert_eq!(external(&s, &ws, at(TOP, ".sig", 0, 1)), (SymbolKind::Var, "sig".to_string()));
|
|
assert_eq!(external(&s, &ws, at(TOP, ".g_in", 0, 1)), (SymbolKind::Block, "g_in".into()));
|
|
assert_eq!(external(&s, &ws, at(TOP, ".deep", 0, 1)), (SymbolKind::Var, "deep".into()));
|
|
}
|
|
|
|
#[test]
|
|
fn a_block_and_its_members_are_hierarchical_only_under_the_dotted_key() {
|
|
let ws = index();
|
|
assert!(ws.member("sub", "g_lane").is_empty(), "no connection list can name a block");
|
|
assert_eq!(ws.member_in("sub", "g_lane", Surface::Hierarchical).len(), 1);
|
|
assert_eq!(ws.member_in("sub.g_lane", "sig", Surface::Hierarchical).len(), 1);
|
|
assert_eq!(ws.member_in("sub.g_lane.g_in", "deep", Surface::Hierarchical).len(), 1);
|
|
assert!(
|
|
ws.member_in("g_lane", "sig", Surface::Hierarchical).is_empty(),
|
|
"never the bare label"
|
|
);
|
|
let held: Vec<&str> = ws.hierarchical_declarations("g_lane").map(|(unit, _)| unit).collect();
|
|
assert_eq!(held, vec!["sub"]);
|
|
}
|
|
|
|
#[test]
|
|
fn an_unnamed_block_declares_nothing_a_path_can_name() {
|
|
let src = "module u (output logic o);\n if (1) begin\n logic w;\n end\n assign o = genblk1.w;\nendmodule\n";
|
|
let s = sem(src);
|
|
assert_eq!(resolution(&s, at(src, "genblk1", 0, 0)), Resolution::NotFound);
|
|
assert!(s.reference_at(at(src, "genblk1.w", 0, 8)).is_none());
|
|
assert!(s.symbols_named("genblk1").next().is_none(), "no symbol is invented for it");
|
|
}
|
|
|
|
/// QA F3: a block label that collides with a net of its scope is a duplicate
|
|
/// and **not** a use of the first declaration, either way round; two arms
|
|
/// sharing a label still are one label.
|
|
#[test]
|
|
fn a_block_label_beside_a_same_named_net_is_a_duplicate_and_not_a_use() {
|
|
let net_first = "module c;\n wire g;\n if (1) begin : g\n end : g\nendmodule\n";
|
|
let s = sem(net_first);
|
|
assert_eq!(s.duplicates().len(), 1);
|
|
let net = s.symbols_named("g").next().expect("the net");
|
|
assert_eq!(s.symbol(net).map(|x| x.kind), Some(SymbolKind::Net));
|
|
assert!(s.references_of(net).is_empty(), "the label is not a read of the net");
|
|
|
|
let block_first = "module c;\n if (1) begin : g\n end : g\n wire g;\nendmodule\n";
|
|
let s = sem(block_first);
|
|
assert_eq!(s.duplicates().len(), 1);
|
|
let block = s.symbols_named("g").next().expect("the block");
|
|
let uses: Vec<u32> = s.references_of(block).iter().map(|r| r.name_span.start).collect();
|
|
assert_eq!(uses, vec![at(block_first, "end : g", 0, 6)], "only its closing label");
|
|
}
|