Files
lucisv/crates/vlog_semantic/tests/binds.rs
T
Guoliang ChenandClaude Mythos 5.2 71d13b5fbe
ci / checks (push) Successful in 1m0s
ci / corpus (push) Successful in 18s
ci / linux (push) Successful in 6m4s
release / build-and-publish (push) Successful in 5m31s
Release 0.66.2: every source file names its author
Every tracked file that can carry a comment (880 of them) now opens with
"Author: Guoliang Chen, a Senior Digital Design Engineer" and closes with
"Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love,
luciochen.com", in the file's own comment syntax. No behaviour change: the
diff outside the version bump and changelogs is comment lines only.

- tools/stamp_author.py: writes the lines; idempotent, `--check` lists
  what is missing. Headers go under a shebang and under Markdown front
  matter.
- Left alone: test fixtures, cases and snapshots (byte-exact inputs), JSON
  and JSONL, logs, images, lockfiles, docs/site/dist (regenerated) and the
  LICENSE files (already carry the copyright line).
- tools/linux_check.sh: --help reads its header from line 3, now that the
  author line sits under the shebang.
- tools/gates.sh green on this tree: 5 545 tests, corpus 814/6 as before.

Co-Authored-By: Claude Mythos 5.2 <[email protected]>
2026-11-24 12:11:09 -08:00

210 lines
9.5 KiB
Rust

// Author: Guoliang Chen, a Senior Digital Design Engineer
//! `bind` semantics: what a directive's target names, where each connection's
//! two halves resolve, and what is deliberately left unresolved — plus the
//! one census fact the same round turned around, that an instantiation whose
//! connection holds a `` `ifdef `` is an instantiation again.
//! The rules live in `docs/features/bind-semantics/README.md`.
use std::path::PathBuf;
use vlog_parser::parse_source;
use vlog_semantic::{
analyze, resolve_external, Def, ExternalTarget, Resolution, Semantics, SymbolKind,
WorkspaceIndex,
};
fn sem(src: &str) -> Semantics {
let file = parse_source(src).expect("fixture must parse");
analyze(&file, src)
}
fn index(files: &[(&str, &str)]) -> WorkspaceIndex {
WorkspaceIndex::build(files.iter().map(|(name, src)| (PathBuf::from(name), *src)))
}
/// Offset of the `n`th (0-based) occurrence of `needle`.
fn nth(src: &str, needle: &str, n: usize) -> u32 {
src.match_indices(needle).nth(n).expect("occurrence must exist").0 as u32
}
fn at(src: &str, needle: &str) -> u32 {
nth(src, needle, 0)
}
fn resolution(s: &Semantics, offset: u32) -> Option<Resolution> {
s.reference_at(offset).map(|r| r.resolution.clone())
}
/// The name and kind of the declaration the use at `offset` resolves to.
fn resolved(s: &Semantics, offset: u32) -> (String, SymbolKind) {
let id = s
.reference_at(offset)
.unwrap_or_else(|| panic!("no reference at {offset}"))
.resolved()
.unwrap_or_else(|| panic!("the reference at {offset} resolves to nothing"));
let sym = s.symbol(id).expect("a resolved id names a symbol");
(sym.name.clone(), sym.kind)
}
fn overlay_def(ws: &WorkspaceIndex, src: &str, offset: u32) -> Option<Def> {
let s = sem(src);
let id = s.reference_id_at(offset)?;
resolve_external(&s, ws).get(id).and_then(ExternalTarget::best).cloned()
}
// ----------------------------------------------------------------------
// the target
// ----------------------------------------------------------------------
#[test]
fn a_lone_bind_target_resolves_to_the_module_it_names() {
let src = "module top;\n logic sig;\nendmodule\nmodule tb;\n bind top chk u_chk (.x(sig));\nendmodule\nmodule chk (input x);\nendmodule\n";
let s = sem(src);
assert_eq!(resolved(&s, at(src, "bind top") + 5), ("top".to_string(), SymbolKind::Module));
// And the module being bound in is still recorded, as it always was.
assert_eq!(resolved(&s, at(src, "chk u_chk")), ("chk".to_string(), SymbolKind::Module));
}
#[test]
fn an_interface_target_resolves_too() {
let src = "interface bus_if;\nendinterface\nmodule tb;\n bind bus_if chk u (.*);\nendmodule\nmodule chk;\nendmodule\n";
let s = sem(src);
assert_eq!(
resolved(&s, at(src, "bind bus_if") + 5),
("bus_if".to_string(), SymbolKind::Interface)
);
}
#[test]
fn a_hierarchical_instance_path_target_records_nothing() {
// `top.u_dut.u_core` names a place in an elaborated hierarchy that no
// single file knows. Recording nothing is the answer; a guess would be a
// wrong one, and a wrong answer is worse than none.
let src = "module tb;\n bind top.u_dut.u_core chk u (.*);\nendmodule\nmodule chk;\nendmodule\nmodule top;\nendmodule\n";
let s = sem(src);
assert!(resolution(&s, at(src, "top.u_dut")).is_none(), "the head of a path records nothing");
// An indexed target is the same shape and records the same nothing.
let src = "module tb;\n bind top.gen[0].u_core chk u (.*);\nendmodule\nmodule chk;\nendmodule\nmodule top;\nendmodule\n";
let s = sem(src);
assert!(resolution(&s, at(src, "top.gen")).is_none());
// Including a lone name that carries a selector.
let src = "module tb;\n bind arr[0] chk u (.*);\nendmodule\nmodule chk;\nendmodule\nmodule arr;\nendmodule\n";
let s = sem(src);
assert!(resolution(&s, at(src, "arr[0]")).is_none());
}
// ----------------------------------------------------------------------
// the connections
// ----------------------------------------------------------------------
#[test]
fn a_connections_port_half_resolves_against_the_bound_in_module() {
let src = "module top;\n logic sig;\nendmodule\nmodule chk (input clk_i, input x);\nendmodule\nmodule tb;\n bind top chk u_chk (.clk_i(sig), .x(sig));\nendmodule\n";
let s = sem(src);
assert_eq!(resolved(&s, at(src, ".clk_i(sig)") + 1), ("clk_i".to_string(), SymbolKind::Port));
assert_eq!(resolved(&s, at(src, ".x(sig)") + 1), ("x".to_string(), SymbolKind::Port));
}
#[test]
fn a_connections_expression_half_resolves_in_the_target_scope() {
// `sig` is declared in `top` and nowhere else: before this round it was
// looked up in the scope the directive is *written* in and found nothing.
let src = "module top;\n logic sig;\nendmodule\nmodule chk (input x);\nendmodule\nmodule tb;\n bind top chk u_chk (.x(sig));\nendmodule\n";
let s = sem(src);
assert_eq!(resolved(&s, at(src, "(sig)") + 1), ("sig".to_string(), SymbolKind::Var));
}
#[test]
fn the_target_scope_wins_over_the_writing_scope() {
// Both scopes declare `sig`; IEEE 1800 §23.11 elaborates the connection
// inside the target, so the target's declaration is the answer.
let src = "module top;\n logic sig;\nendmodule\nmodule chk (input x);\nendmodule\nmodule tb;\n logic sig;\n bind top chk u_chk (.x(sig));\nendmodule\n";
let s = sem(src);
let id =
s.reference_at(at(src, "(sig)") + 1).expect("a reference").resolved().expect("resolved");
let sym = s.symbol(id).expect("symbol");
assert_eq!(
sym.name_span.start,
at(src, "logic sig") + 6,
"the target's `sig`, not the testbench's"
);
}
#[test]
fn a_name_the_target_does_not_hold_falls_back_to_the_writing_scope() {
// A package constant or a testbench signal named in a connection really
// does resolve where the directive is written, so a miss in the target
// falls back there — the `Target::InlineConstraint` shape.
let src = "module top;\nendmodule\nmodule chk (input x);\nendmodule\nmodule tb;\n logic local_sig;\n bind top chk u_chk (.x(local_sig));\nendmodule\n";
let s = sem(src);
assert_eq!(
resolved(&s, at(src, "(local_sig)") + 1),
("local_sig".to_string(), SymbolKind::Var)
);
}
#[test]
fn a_name_neither_scope_holds_records_nothing() {
// "Absent from the module's own scope" is not "absent": a module opens a
// scope per generate block. So a miss on both sides is silence, never a
// claim that the name resolves to nothing.
let src = "module top;\nendmodule\nmodule chk (input x);\nendmodule\nmodule tb;\n bind top chk u_chk (.x(nowhere));\nendmodule\n";
let s = sem(src);
assert!(resolution(&s, at(src, "(nowhere)") + 1).is_none());
}
#[test]
fn an_expression_half_reaches_a_target_declared_in_another_file() {
// The target is another file's module: the same question `Target::Port`
// asks of the workspace, in the same one-segment path spelling.
let files = [
("top.sv", "module top (input logic clk_i);\nendmodule\n"),
("chk.sv", "module chk (input x);\nendmodule\n"),
];
let ws = index(&files);
let tb = "module tb;\n bind top chk u_chk (.x(clk_i));\nendmodule\n";
let def = overlay_def(&ws, tb, at(tb, "(clk_i)") + 1).expect("the workspace answers");
assert_eq!(ws.path(def.file).expect("a path").to_string_lossy(), "top.sv");
}
#[test]
fn a_parameter_override_value_resolves_in_the_target_too() {
let src = "module top;\n localparam int W = 8;\nendmodule\nmodule chk #(parameter int P = 1) ();\nendmodule\nmodule tb;\n bind top chk #(.P(W)) u_chk ();\nendmodule\n";
let s = sem(src);
assert_eq!(resolved(&s, at(src, "(W)") + 1), ("W".to_string(), SymbolKind::LocalParam));
}
#[test]
fn a_binds_instance_name_is_still_not_declared_here() {
// It belongs to the target scope; declaring it in this one would put a
// name in a scope that does not have it.
let src = "module top;\nendmodule\nmodule chk;\nendmodule\nmodule tb;\n bind top chk u_chk ();\nendmodule\n";
let s = sem(src);
assert!(s.symbols().iter().all(|sym| sym.name != "u_chk"));
}
// ----------------------------------------------------------------------
// the conditional-region residue (coverage_gaps M5), where it meets the
// census — `instantiations::sites` is what a code lens counts and what the
// call hierarchy walks, so this is the fact the round was for.
// ----------------------------------------------------------------------
#[test]
fn a_directive_inside_a_connections_value_leaves_the_instantiation_countable() {
// Before TODO §26 the whole item was `ItemKind::Verbatim` and this count
// was 0: no edge, no lens, no hierarchy site, no port references.
let src = "module sub (input a, input b);\nendmodule\nmodule m;\n logic w;\n sub u_sub (\n .a(\n`ifdef X\n y\n`else\n z\n`endif\n ),\n .b(w)\n );\nendmodule\n";
let file = parse_source(src).expect("fixture must parse");
let sites = vlog_semantic::instantiations::sites(&file.items);
assert_eq!(sites.len(), 1, "one instantiation, not one opaque leaf");
assert_eq!(sites[0].inst.module, "sub");
// And the connection that is *not* the verbatim one still resolves.
let s = analyze(&file, src);
assert_eq!(resolved(&s, at(src, ".b(w)") + 1), ("b".to_string(), SymbolKind::Port));
// The verbatim one records nothing at all: its value depends on a
// `ifdef` this crate never evaluates.
assert!(resolution(&s, at(src, " y")).is_none());
}
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com