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]>
833 lines
28 KiB
Rust
833 lines
28 KiB
Rust
// Author: Guoliang Chen, a Senior Digital Design Engineer
|
|
|
|
//! Class semantics: what a class body binds, how its members resolve, and
|
|
//! the honest shapes for everything inheritance-flavored. The rules live in
|
|
//! `docs/features/class-semantics/README.md`.
|
|
|
|
use std::path::PathBuf;
|
|
use vlog_parser::parse_source;
|
|
use vlog_semantic::{
|
|
analyze, resolve_external, Definition, ExternalTarget, Resolution, ScopeKind, Semantics,
|
|
Surface, SymbolId, SymbolKind, WorkspaceIndex,
|
|
};
|
|
|
|
fn sem(src: &str) -> Semantics {
|
|
let file = parse_source(src).expect("fixture must parse");
|
|
analyze(&file, 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 symbol_named<'a>(s: &'a Semantics, name: &str) -> &'a vlog_semantic::Symbol {
|
|
s.symbols().iter().find(|sym| sym.name == name).unwrap_or_else(|| panic!("no symbol {name}"))
|
|
}
|
|
|
|
fn symbol_id(s: &Semantics, name: &str) -> SymbolId {
|
|
let i = s.symbols().iter().position(|sym| sym.name == name).expect("symbol must exist");
|
|
SymbolId(i as u32)
|
|
}
|
|
|
|
fn resolution_at(s: &Semantics, offset: u32) -> Resolution {
|
|
s.reference_at(offset).expect("a reference at the offset").resolution.clone()
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Binding
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_class_binds_a_symbol_a_scope_and_its_members() {
|
|
let src = "\
|
|
class c #(parameter int W = 8, type T = int);
|
|
int prop;
|
|
rand logic [W-1:0] bits;
|
|
static int count;
|
|
localparam int LP = 2;
|
|
typedef enum { IDLE, BUSY } state_e;
|
|
function new(int seed);
|
|
endfunction
|
|
virtual function int get(int idx);
|
|
return prop + idx;
|
|
endfunction
|
|
extern task run(int cycles);
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let class = symbol_named(&s, "c");
|
|
assert_eq!(class.kind, SymbolKind::Class);
|
|
let scope = class.child_scope.and_then(|id| s.scope(id)).expect("a class scope");
|
|
assert_eq!(scope.kind, ScopeKind::Class);
|
|
|
|
let expect = [
|
|
("W", SymbolKind::Param),
|
|
("T", SymbolKind::Param),
|
|
("prop", SymbolKind::Var),
|
|
("bits", SymbolKind::Var),
|
|
("count", SymbolKind::Var),
|
|
// `localparam int LP = 2;` — its own kind since 2026-09-02, so a
|
|
// header `localparam` can be kept out of a `#(.` override list.
|
|
("LP", SymbolKind::LocalParam),
|
|
("state_e", SymbolKind::Typedef),
|
|
("IDLE", SymbolKind::EnumVariant),
|
|
("BUSY", SymbolKind::EnumVariant),
|
|
("new", SymbolKind::Function),
|
|
("get", SymbolKind::Function),
|
|
("run", SymbolKind::Task),
|
|
];
|
|
for (name, kind) in expect {
|
|
let member = symbol_named(&s, name);
|
|
assert_eq!(member.kind, kind, "{name}");
|
|
assert_eq!(member.scope, class.child_scope.expect("checked above"), "{name}");
|
|
assert_eq!(&src[member.name_span.as_range()], name, "{name} span");
|
|
}
|
|
// The method body resolves the property and the header parameter.
|
|
let prop = resolution_at(&s, nth(src, "prop + idx", 0));
|
|
assert_eq!(prop, Resolution::Resolved(symbol_id(&s, "prop")));
|
|
let w_use = resolution_at(&s, nth(src, "W-1", 0));
|
|
assert_eq!(w_use, Resolution::Resolved(symbol_id(&s, "W")));
|
|
// And the method argument.
|
|
let idx = resolution_at(&s, nth(src, "prop + idx", 0) + 7);
|
|
assert_eq!(idx, Resolution::Resolved(symbol_id(&s, "idx")));
|
|
}
|
|
|
|
#[test]
|
|
fn member_definition_and_references_round_trip() {
|
|
let src = "\
|
|
class c;
|
|
int counter;
|
|
function void bump();
|
|
counter = counter + 1;
|
|
endfunction
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let id = symbol_id(&s, "counter");
|
|
let uses = s.references_of(id);
|
|
assert_eq!(uses.len(), 2, "both body uses, not the declaration");
|
|
for r in &uses {
|
|
assert_eq!(&src[r.name_span.as_range()], "counter");
|
|
}
|
|
// definition_of from a use lands on the declaration.
|
|
let def = s.definition_of(nth(src, "counter + 1", 0)).expect("go to definition");
|
|
assert_eq!(def.name, "counter");
|
|
assert_eq!(def.kind, SymbolKind::Var);
|
|
assert_eq!(def.name_span.start, nth(src, "counter", 0));
|
|
}
|
|
|
|
#[test]
|
|
fn a_method_arg_shadows_a_property_shadows_a_module_net() {
|
|
let src = "\
|
|
module m;
|
|
wire x;
|
|
class c;
|
|
int x;
|
|
function int f(int x);
|
|
return x;
|
|
endfunction
|
|
function int g();
|
|
return x;
|
|
endfunction
|
|
endclass
|
|
assign x = 1;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
// Three distinct symbols named x, innermost wins at each use.
|
|
let xs: Vec<_> = s.symbols().iter().filter(|sym| sym.name == "x").collect();
|
|
assert_eq!(xs.len(), 3);
|
|
let arg = resolution_at(&s, nth(src, "return x", 0) + 7);
|
|
let prop = resolution_at(&s, nth(src, "return x", 1) + 7);
|
|
let net = resolution_at(&s, nth(src, "assign x", 0) + 7);
|
|
let kind_of = |r: &Resolution| match r {
|
|
Resolution::Resolved(id) => s.symbol(*id).map(|sym| sym.kind),
|
|
_ => None,
|
|
};
|
|
assert_eq!(kind_of(&arg), Some(SymbolKind::TfArg));
|
|
assert_eq!(kind_of(&prop), Some(SymbolKind::Var));
|
|
assert_eq!(kind_of(&net), Some(SymbolKind::Net));
|
|
assert_ne!(arg, prop);
|
|
assert_ne!(prop, net);
|
|
// references_of splits cleanly per binding.
|
|
for sym in &xs {
|
|
let id = SymbolId(
|
|
s.symbols().iter().position(|c| std::ptr::eq(*sym, c)).expect("from iteration") as u32,
|
|
);
|
|
assert!(s.references_of(id).len() <= 1);
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// this / super
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn this_resolves_members_in_the_class_scope_only() {
|
|
let src = "\
|
|
module m;
|
|
wire outside;
|
|
class c;
|
|
int prop;
|
|
function void f(int prop);
|
|
this.prop = prop;
|
|
this.outside = 1;
|
|
endfunction
|
|
endclass
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
// this.prop names the property, not the shadowing argument.
|
|
let via_this = resolution_at(&s, nth(src, "this.prop", 0) + 5);
|
|
assert_eq!(via_this, Resolution::Resolved(symbol_id(&s, "prop")));
|
|
let assigned = s.reference_at(nth(src, "this.prop = prop", 0) + 12).expect("the rhs");
|
|
match &assigned.resolution {
|
|
Resolution::Resolved(id) => {
|
|
assert_eq!(s.symbol(*id).map(|sym| sym.kind), Some(SymbolKind::TfArg))
|
|
}
|
|
other => panic!("the bare rhs is the argument, got {other:?}"),
|
|
}
|
|
// this.outside cannot reach the module net, and c has no base to blame.
|
|
let outside = resolution_at(&s, nth(src, "this.outside", 0) + 5);
|
|
assert_eq!(outside, Resolution::NotFound);
|
|
// `this` itself is never a recorded use.
|
|
assert!(s.reference_at(nth(src, "this.prop", 0)).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn super_and_inheritance_misses_record_the_base() {
|
|
let src = "\
|
|
class c extends uvm_object;
|
|
function new(string name);
|
|
super.new(name);
|
|
super.whatever();
|
|
set_name(name);
|
|
endfunction
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let expect = Resolution::InheritedCandidate { base: "uvm_object".into(), paths: Vec::new() };
|
|
assert_eq!(resolution_at(&s, nth(src, "super.new", 0) + 6), expect);
|
|
assert_eq!(resolution_at(&s, nth(src, "super.whatever", 0) + 6), expect);
|
|
// A plain call that misses the whole lexical chain blames the base too,
|
|
// and carries no path: a callee is a value position, and the promotion
|
|
// (`docs/features/overlay-promotion/README.md`) fires on type positions
|
|
// only, so nothing sweeps `set_name` through the workspace's unit table.
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "set_name", 0)),
|
|
Resolution::InheritedCandidate { base: "uvm_object".into(), paths: Vec::new() }
|
|
);
|
|
// `super` itself is never a recorded use.
|
|
assert!(s.reference_at(nth(src, "super.new", 0)).is_none());
|
|
}
|
|
|
|
#[test]
|
|
fn an_inheritance_miss_keeps_the_import_paths_it_collected() {
|
|
let src = "\
|
|
class c extends uvm_component;
|
|
import uvm_pkg::*;
|
|
function void f();
|
|
report(UVM_LOW);
|
|
endfunction
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
match resolution_at(&s, nth(src, "UVM_LOW", 0)) {
|
|
Resolution::InheritedCandidate { base, paths } => {
|
|
assert_eq!(base, "uvm_component");
|
|
// The imports in walk order, and nothing else: a call argument
|
|
// is a value position, so the bare name is not appended.
|
|
assert_eq!(paths, vec!["uvm_pkg::*".to_string()]);
|
|
}
|
|
other => panic!("expected an inherited candidate, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn null_and_new_are_never_plain_uses() {
|
|
let src = "\
|
|
class c;
|
|
c next;
|
|
function void f();
|
|
next = new();
|
|
next = null;
|
|
endfunction
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
assert!(s.reference_at(nth(src, "new()", 0)).is_none());
|
|
assert!(s.reference_at(nth(src, "null", 0)).is_none());
|
|
// The property use on the left still resolves.
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "next = new", 0)),
|
|
Resolution::Resolved(symbol_id(&s, "next"))
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// extends
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn extends_resolves_to_an_in_file_class() {
|
|
let src = "\
|
|
class base #(parameter int W = 4);
|
|
int held;
|
|
endclass
|
|
|
|
class child extends base #(.W(8));
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let base_ref = resolution_at(&s, nth(src, "extends base", 0) + 8);
|
|
assert_eq!(base_ref, Resolution::Resolved(symbol_id(&s, "base")));
|
|
// The override name resolves into the base's scope.
|
|
let w = resolution_at(&s, nth(src, ".W(8)", 0) + 1);
|
|
assert_eq!(w, Resolution::Resolved(symbol_id(&s, "W")));
|
|
}
|
|
|
|
#[test]
|
|
fn extends_override_values_see_the_class_own_parameters() {
|
|
let src = "\
|
|
class base #(parameter int W = 4);
|
|
endclass
|
|
|
|
class child #(parameter int CW = 16) extends base #(.W(CW));
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let cw = resolution_at(&s, nth(src, ".W(CW)", 0) + 3);
|
|
assert_eq!(cw, Resolution::Resolved(symbol_id(&s, "CW")));
|
|
}
|
|
|
|
#[test]
|
|
fn extends_of_a_class_from_another_file_is_an_external_candidate() {
|
|
let src = "\
|
|
import uvm_pkg::*;
|
|
|
|
class c extends uvm_object;
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
match resolution_at(&s, nth(src, "uvm_object", 0)) {
|
|
Resolution::ExternalCandidate { paths, .. } => {
|
|
// The wildcard import and the bare unit name, in that order.
|
|
assert_eq!(paths, vec!["uvm_pkg::*".to_string(), "uvm_object".to_string()]);
|
|
}
|
|
other => panic!("expected an external candidate, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_package_scoped_extends_records_head_and_tail() {
|
|
let src = "\
|
|
package p;
|
|
class base;
|
|
int held;
|
|
endclass
|
|
endpackage
|
|
|
|
class child extends p::base;
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let head = resolution_at(&s, nth(src, "p::base", 0));
|
|
assert_eq!(head, Resolution::Resolved(symbol_id(&s, "p")));
|
|
let tail = resolution_at(&s, nth(src, "p::base", 0) + 3);
|
|
assert_eq!(tail, Resolution::Resolved(symbol_id(&s, "base")));
|
|
}
|
|
|
|
#[test]
|
|
fn static_access_resolves_into_an_in_file_class() {
|
|
let src = "\
|
|
class c;
|
|
localparam int K = 3;
|
|
typedef logic [1:0] pair_t;
|
|
endclass
|
|
|
|
module m;
|
|
int x = c::K;
|
|
c::pair_t y;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "c::K", 0) + 3),
|
|
Resolution::Resolved(symbol_id(&s, "K"))
|
|
);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "c::pair_t", 0) + 3),
|
|
Resolution::Resolved(symbol_id(&s, "pair_t"))
|
|
);
|
|
// A static miss on a class that extends blames the base.
|
|
let src2 = "\
|
|
class c extends base_c;
|
|
endclass
|
|
|
|
module m;
|
|
int x = c::MISSING;
|
|
endmodule
|
|
";
|
|
let s2 = sem(src2);
|
|
assert_eq!(
|
|
resolution_at(&s2, nth(src2, "c::MISSING", 0) + 3),
|
|
Resolution::InheritedCandidate { base: "base_c".into(), paths: Vec::new() }
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_unit_miss_inside_an_extending_class_stays_external_a_member_miss_does_not() {
|
|
// A base class supplies *members*, not package or class names (rules
|
|
// 5/8): the head of `other_pkg::THING` and a nested class's own
|
|
// `extends` base stay external candidates — the workspace's to answer —
|
|
// while a plain member miss in the same position is the base's business.
|
|
let src = "\
|
|
class c extends base_c;
|
|
function void f();
|
|
int x = other_pkg::THING;
|
|
member_miss();
|
|
endfunction
|
|
class inner extends other_base;
|
|
endclass
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "other_pkg", 0)),
|
|
Resolution::ExternalCandidate {
|
|
paths: vec!["other_pkg".to_string()],
|
|
surface: Surface::Connection,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "other_base", 0)),
|
|
Resolution::ExternalCandidate {
|
|
paths: vec!["other_base".to_string()],
|
|
surface: Surface::Connection,
|
|
}
|
|
);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "member_miss", 0)),
|
|
Resolution::InheritedCandidate { base: "base_c".into(), paths: Vec::new() }
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_missing_member_of_an_in_file_package_is_not_found() {
|
|
// The pinned rule 6 of the vlog-semantic README: the package is fully
|
|
// known locally, so no workspace index could change the answer — and the
|
|
// extends fallback must not leak into the package arm either.
|
|
let src = "\
|
|
package p;
|
|
localparam int K = 1;
|
|
endpackage
|
|
|
|
module m;
|
|
int x = p::MISSING;
|
|
endmodule
|
|
|
|
class c extends base_c;
|
|
function void f();
|
|
int y = p::ALSO_MISSING;
|
|
endfunction
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(resolution_at(&s, nth(src, "p::MISSING", 0) + 3), Resolution::NotFound);
|
|
assert_eq!(resolution_at(&s, nth(src, "p::ALSO_MISSING", 0) + 3), Resolution::NotFound);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Out-of-body definitions
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn an_out_of_body_definition_binds_into_the_class_and_round_trips() {
|
|
let src = "\
|
|
class c;
|
|
int size;
|
|
extern function void configure(int s);
|
|
endclass
|
|
|
|
function void c::configure(int s);
|
|
size = s;
|
|
endfunction
|
|
";
|
|
let s = sem(src);
|
|
// One `configure` symbol — the prototype — living in c's scope.
|
|
let protos: Vec<_> = s.symbols().iter().filter(|sym| sym.name == "configure").collect();
|
|
assert_eq!(protos.len(), 1, "the prototype is the symbol; the definition site is a use");
|
|
let class = symbol_named(&s, "c");
|
|
assert_eq!(protos[0].scope, class.child_scope.expect("class scope"));
|
|
// The definition-site name is a reference to the prototype.
|
|
let def_site = resolution_at(&s, nth(src, "c::configure", 0) + 3);
|
|
assert_eq!(def_site, Resolution::Resolved(symbol_id(&s, "configure")));
|
|
// The `c::` head navigates to the class.
|
|
let head = resolution_at(&s, nth(src, "c::configure", 0));
|
|
assert_eq!(head, Resolution::Resolved(symbol_id(&s, "c")));
|
|
// The body resolves the property through the class scope.
|
|
let size_use = resolution_at(&s, nth(src, "size = s", 0));
|
|
assert_eq!(size_use, Resolution::Resolved(symbol_id(&s, "size")));
|
|
// And the definition's own argument.
|
|
let arg = resolution_at(&s, nth(src, "size = s", 0) + 7);
|
|
match arg {
|
|
Resolution::Resolved(id) => {
|
|
assert_eq!(s.symbol(id).map(|sym| sym.kind), Some(SymbolKind::TfArg))
|
|
}
|
|
other => panic!("expected the argument, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn an_out_of_body_definition_for_an_unknown_class_stays_navigable_outward() {
|
|
let src = "\
|
|
function void elsewhere::helper(int x);
|
|
int y;
|
|
y = x;
|
|
endfunction
|
|
";
|
|
let s = sem(src);
|
|
// The class is not in this file: the head is an external candidate...
|
|
match resolution_at(&s, nth(src, "elsewhere", 0)) {
|
|
Resolution::ExternalCandidate { paths, .. } => {
|
|
assert_eq!(paths, vec!["elsewhere".to_string()])
|
|
}
|
|
other => panic!("expected an external candidate, got {other:?}"),
|
|
}
|
|
// ...the method still binds (in the writing scope) and its body resolves.
|
|
let helper = symbol_named(&s, "helper");
|
|
assert_eq!(helper.kind, SymbolKind::Function);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "y = x", 0) + 4),
|
|
Resolution::Resolved(symbol_id(&s, "x"))
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Nesting and packaging
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn a_nested_class_binds_inside_its_outer_class() {
|
|
let src = "\
|
|
class outer;
|
|
int shared;
|
|
class inner;
|
|
function int f();
|
|
return shared;
|
|
endfunction
|
|
endclass
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let outer = symbol_named(&s, "outer");
|
|
let inner = symbol_named(&s, "inner");
|
|
assert_eq!(inner.kind, SymbolKind::Class);
|
|
assert_eq!(inner.scope, outer.child_scope.expect("outer scope"));
|
|
// The inner method reaches the outer property lexically (documented
|
|
// simplification: static-vs-instance access is not modelled).
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "return shared", 0) + 7),
|
|
Resolution::Resolved(symbol_id(&s, "shared"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_class_in_a_package_is_a_package_member() {
|
|
let src = "\
|
|
package p;
|
|
class item;
|
|
int len;
|
|
endclass
|
|
endpackage
|
|
|
|
module m;
|
|
import p::*;
|
|
item it;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
let item = symbol_named(&s, "item");
|
|
let pkg = symbol_named(&s, "p");
|
|
assert_eq!(item.scope, pkg.child_scope.expect("package scope"));
|
|
// The type use in the importing module resolves through the wildcard.
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "item it", 0)),
|
|
Resolution::Resolved(symbol_id(&s, "item"))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn a_forward_declared_class_defers_its_members_to_the_workspace() {
|
|
let src = "\
|
|
typedef class later;
|
|
|
|
module m;
|
|
int x = later::K;
|
|
endmodule
|
|
";
|
|
let s = sem(src);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "later::K", 0) + 7),
|
|
Resolution::ExternalCandidate {
|
|
paths: vec!["later::K".to_string()],
|
|
surface: Surface::Connection,
|
|
}
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Outline and opaque members
|
|
// ----------------------------------------------------------------------
|
|
|
|
#[test]
|
|
fn document_symbols_nest_class_members_under_the_class() {
|
|
let src = "\
|
|
class c;
|
|
int prop;
|
|
function void f();
|
|
int local_v;
|
|
endfunction
|
|
endclass
|
|
|
|
function void c::g();
|
|
int hidden;
|
|
endfunction
|
|
";
|
|
let s = sem(src);
|
|
let outline = s.document_symbols();
|
|
assert_eq!(outline.len(), 1, "one top-level node: the class");
|
|
let class = &outline[0];
|
|
assert_eq!(s.symbol(class.symbol).map(|sym| sym.name.as_str()), Some("c"));
|
|
let names: Vec<_> = class
|
|
.children
|
|
.iter()
|
|
.filter_map(|d| s.symbol(d.symbol).map(|sym| sym.name.as_str()))
|
|
.collect();
|
|
assert_eq!(names, vec!["prop", "f", "g"], "members nest; method locals do not spill");
|
|
let f = &class.children[1];
|
|
let f_children: Vec<_> =
|
|
f.children.iter().filter_map(|d| s.symbol(d.symbol).map(|sym| sym.name.as_str())).collect();
|
|
assert_eq!(f_children, vec!["local_v"]);
|
|
}
|
|
|
|
#[test]
|
|
fn a_covergroup_declares_its_name_and_binds_inside() {
|
|
let src = "\
|
|
class c;
|
|
rand int x;
|
|
covergroup cg;
|
|
coverpoint x;
|
|
endgroup
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
// Round 2 of the wave: the covergroup declares a symbol of its own and
|
|
// the sampled expression is an ordinary reference to the class property.
|
|
// See `docs/features/covergroup-bodies/README.md`.
|
|
assert!(s.symbols().iter().any(|sym| sym.name == "cg"));
|
|
let r = s.reference_at(nth(src, "coverpoint x", 0) + 11).expect("`x` is a reference");
|
|
assert_eq!(r.name, "x");
|
|
assert_eq!(
|
|
s.definition_of(nth(src, "coverpoint x", 0) + 11).map(|d| d.name_span),
|
|
Some(vlog_parser::TextRange::new(
|
|
nth(src, "rand int x", 0) + 9,
|
|
nth(src, "rand int x", 0) + 10,
|
|
))
|
|
);
|
|
// Queries at every offset stay panic-free.
|
|
for offset in 0..=src.len() as u32 {
|
|
let _ = s.definition_of(offset);
|
|
let _ = s.symbol_at(offset);
|
|
let _ = s.reference_at(offset);
|
|
}
|
|
}
|
|
|
|
/// The `class-semantics` rule 2 ("verbatim members stay opaque") no longer
|
|
/// covers constraints: since `constraint-bodies` a constraint declares a
|
|
/// symbol and everything inside it binds in the class scope. See
|
|
/// `docs/features/constraint-bodies/README.md`.
|
|
#[test]
|
|
fn a_constraint_binds_a_symbol_and_its_body_resolves_in_the_class() {
|
|
let src = "\
|
|
class c;
|
|
rand int x;
|
|
constraint c_x { x inside {[0:7]}; }
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
let c_x = symbol_named(&s, "c_x");
|
|
assert_eq!(c_x.kind, SymbolKind::Constraint);
|
|
assert_eq!(&src[c_x.name_span.as_range()], "c_x");
|
|
// The body's `x` is the class's own property.
|
|
let r = resolution_at(&s, nth(src, "x inside", 0));
|
|
assert!(matches!(r, Resolution::Resolved(id) if id == symbol_id(&s, "x")), "{r:?}");
|
|
for offset in 0..=src.len() as u32 {
|
|
let _ = s.definition_of(offset);
|
|
let _ = s.symbol_at(offset);
|
|
let _ = s.reference_at(offset);
|
|
}
|
|
}
|
|
|
|
/// An out-of-block `constraint C::name { ... }` binds into the class the same
|
|
/// way an out-of-body method does — the `extern` prototype owns the name and
|
|
/// the definition attaches to it.
|
|
#[test]
|
|
fn an_out_of_block_constraint_binds_into_its_class() {
|
|
let src = "\
|
|
class my_seq;
|
|
rand int num_trans;
|
|
extern constraint num_trans_c;
|
|
endclass
|
|
|
|
constraint my_seq::num_trans_c {
|
|
num_trans inside {[1:20]};
|
|
}
|
|
";
|
|
let s = sem(src);
|
|
let sym = symbol_named(&s, "num_trans_c");
|
|
assert_eq!(sym.kind, SymbolKind::Constraint);
|
|
// The prototype declares it; the definition site is a reference to it.
|
|
assert_eq!(&src[sym.name_span.as_range()], "num_trans_c");
|
|
assert!(sym.name_span.start < nth(src, "constraint my_seq::num_trans_c", 0));
|
|
let r = resolution_at(&s, nth(src, "num_trans inside", 0));
|
|
assert!(matches!(r, Resolution::Resolved(id) if id == symbol_id(&s, "num_trans")), "{r:?}");
|
|
for offset in 0..=src.len() as u32 {
|
|
let _ = s.definition_of(offset);
|
|
let _ = s.symbol_at(offset);
|
|
let _ = s.reference_at(offset);
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// Workspace index
|
|
// ----------------------------------------------------------------------
|
|
|
|
fn index(files: &[(&str, &str)]) -> WorkspaceIndex {
|
|
WorkspaceIndex::build(files.iter().map(|(p, s)| (PathBuf::from(p), *s)))
|
|
}
|
|
|
|
#[test]
|
|
fn a_file_scope_class_is_a_unit_and_cross_file_extends_resolves() {
|
|
let sources = [
|
|
("base.svh", "class base;\n int held;\n function void poke(); endfunction\nendclass\n"),
|
|
("child.svh", "class child extends base;\nendclass\n"),
|
|
];
|
|
let ws = index(&sources);
|
|
let units = ws.units("base");
|
|
assert_eq!(units.len(), 1);
|
|
assert_eq!(units[0].kind, SymbolKind::Class);
|
|
// Members are exported.
|
|
assert_eq!(ws.member("base", "held").len(), 1);
|
|
assert_eq!(ws.member("base", "poke").len(), 1);
|
|
// The extends candidate resolves through the overlay.
|
|
let file = parse_source(sources[1].1).expect("child parses");
|
|
let s = analyze(&file, sources[1].1);
|
|
let overlay = resolve_external(&s, &ws);
|
|
let offset = nth(sources[1].1, "base", 0);
|
|
match overlay.definition_of(&s, offset) {
|
|
Some(Definition::External(defs)) => {
|
|
assert_eq!(defs.len(), 1);
|
|
assert_eq!(defs[0].kind, SymbolKind::Class);
|
|
assert_eq!(&sources[0].1[defs[0].name_span.as_range()], "base");
|
|
}
|
|
other => panic!("expected the workspace answer, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn a_package_nested_class_reaches_through_imports_and_static_access() {
|
|
let sources = [
|
|
(
|
|
"pkg.sv",
|
|
"package p;\n class item;\n localparam int K = 9;\n endclass\nendpackage\n",
|
|
),
|
|
("use.sv", "import p::*;\n\nclass mine extends item;\n int x = item::K;\nendclass\n"),
|
|
];
|
|
let ws = index(&sources);
|
|
// The class is a package member, and its own members are registered
|
|
// under the class's name (the nested-class recursion).
|
|
assert_eq!(ws.member("p", "item").len(), 1);
|
|
assert_eq!(ws.member("item", "K").len(), 1);
|
|
assert_eq!(ws.counts().classes, 2, "one package member, one file-scope unit");
|
|
|
|
let file = parse_source(sources[1].1).expect("use.sv parses");
|
|
let s = analyze(&file, sources[1].1);
|
|
let overlay = resolve_external(&s, &ws);
|
|
// extends item → through the wildcard import.
|
|
match overlay.definition_of(&s, nth(sources[1].1, "extends item", 0) + 8) {
|
|
Some(Definition::External(defs)) => assert_eq!(defs[0].kind, SymbolKind::Class),
|
|
other => panic!("expected the class, got {other:?}"),
|
|
}
|
|
// item::K → through the member table keyed by the class name.
|
|
match overlay.definition_of(&s, nth(sources[1].1, "item::K", 0) + 6) {
|
|
Some(Definition::External(defs)) => {
|
|
// A `localparam` is still exported by its class and still
|
|
// resolves through the member table — the 2026-09-02 split
|
|
// changes what it may be *offered* for, never what it resolves to.
|
|
assert_eq!(defs[0].kind, SymbolKind::LocalParam);
|
|
assert_eq!(&sources[0].1[defs[0].name_span.as_range()], "K");
|
|
}
|
|
other => panic!("expected the localparam, got {other:?}"),
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn workspace_symbols_list_classes_and_their_members() {
|
|
let ws = index(&[(
|
|
"p.sv",
|
|
"package p;\n class driver;\n task run(); endtask\n endclass\nendpackage\n",
|
|
)]);
|
|
let hits = ws.workspace_symbols("driver");
|
|
assert!(hits.iter().any(|h| h.name == "driver" && h.container == Some("p")));
|
|
let runs = ws.workspace_symbols("run");
|
|
assert!(runs.iter().any(|h| h.name == "run" && h.container == Some("driver")));
|
|
}
|
|
|
|
/// **Changed 2026-09-01 by class-member-access** (topo §2.9-9, filed by this
|
|
/// feature and delivered there). The single-file answer is exactly what it
|
|
/// was — this crate still never chases a base, in this file or out of it — but
|
|
/// the *overlay* now does: `resolve_external` follows the recorded base
|
|
/// through the workspace's member tables and up its own `extends` chain. The
|
|
/// old name of this test was
|
|
/// `inherited_candidates_are_not_the_workspace_overlays_business`.
|
|
#[test]
|
|
fn inherited_candidates_are_chased_by_the_overlay_and_only_by_it() {
|
|
let sources = [
|
|
("base.svh", "class base;\n int held;\nendclass\n"),
|
|
(
|
|
"child.svh",
|
|
"class child extends base;\n function void f();\n held = 1;\n endfunction\nendclass\n",
|
|
),
|
|
];
|
|
let ws = index(&sources);
|
|
let file = parse_source(sources[1].1).expect("child parses");
|
|
let s = analyze(&file, sources[1].1);
|
|
// The single-file layer's answer is unchanged: the base, and the (empty)
|
|
// import paths the lexical walk collected. `held = 1` is a value
|
|
// position, so the type-position promotion gave it no path to sweep.
|
|
let held = s.reference_at(nth(sources[1].1, "held = 1", 0)).expect("the use");
|
|
assert_eq!(
|
|
held.resolution,
|
|
Resolution::InheritedCandidate { base: "base".into(), paths: Vec::new() }
|
|
);
|
|
// The overlay chases `base` itself: `member("base", "held")` answers.
|
|
let overlay = resolve_external(&s, &ws);
|
|
let id = s.reference_id_at(nth(sources[1].1, "held = 1", 0)).expect("an id");
|
|
let def = overlay.get(id).and_then(ExternalTarget::best).expect("the inherited property");
|
|
assert_eq!(ws.path(def.file).map(|p| p.file_name().unwrap()), Some("base.svh".as_ref()));
|
|
assert_eq!(def.kind, SymbolKind::Var);
|
|
|
|
// A base the workspace does not hold is still absent from the overlay:
|
|
// the chase ends at the first name nothing declares, and records nothing.
|
|
let orphan = "class child extends nowhere_c;\n function void f();\n held = 1;\n endfunction\nendclass\n";
|
|
let file = parse_source(orphan).expect("child parses");
|
|
let s = analyze(&file, orphan);
|
|
let overlay = resolve_external(&s, &ws);
|
|
let id = s.reference_id_at(nth(orphan, "held = 1", 0)).expect("an id");
|
|
assert!(overlay.get(id).is_none(), "a name the workspace cannot answer is not in the overlay");
|
|
}
|
|
|
|
// Author: Guoliang Chen. Lucisv is made by Guoliang Chen with love, luciochen.com
|