class-semantics: classes are first-class in vlog_semantic -- class
scopes and symbols, member/method/prototype binding, this.x
(nearest-class-only), super.x, cls::x with lexical unit heads,
out-of-body cls::method bound into the class's scope with the extern
prototype as the definition (rule 10), and the honest
InheritedCandidate{base, paths} shape: a member miss in an extending
class records what the unknown base might supply (import paths
included) and is never chased, not even in-file -- the future
inheritance round's worklist, measured at 1303 candidates. Unit-name
misses stay ExternalCandidate (QA-found, lead-ruled: bases supply
members, not package names; +170 recovered navigations, pinned).
Workspace index gains classes as units (246) and their members
(11399). scan.rs migrated to parser name-spans: 26 call sites down
to the 2 documented consumers. 114044 spans, 0 unplaced, 0 panics;
UVM code now has hover/definition/references/outline.
lexer-cleanup: the three filed gaps -- @(* ) lexes via a
starts_attribute backoff (an attribute cannot begin with `)`; real
attributes pinned, no misfire on legal expressions), a leading BOM
is accepted as a named losslessness exception (mid-file BOM errors
clearly; 20k-case position fuzz clean), and non-ASCII lex errors now
decode the full character AND surface through vfmt check's error
slot (exit 2, matching format, text-rule findings still reported --
the silent-pass hole closed). The macro space-before-paren item is
parked with a QA-hardened analysis: the reading depends on a
definition vfmt cannot see, any fixed answer is provably wrong for
one population, and today's behavior fails safe.
827 workspace tests. Dev: 2 Fable agents; QA: 2 Opus agents; reviews
in docs/features/{class-semantics,lexer-cleanup}/README.md.
Co-Authored-By: Claude Fable 5 <[email protected]>
727 lines
23 KiB
Rust
727 lines
23 KiB
Rust
//! 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, Resolution, ScopeKind, Semantics, 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),
|
|
("LP", SymbolKind::Param),
|
|
("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.
|
|
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");
|
|
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()] }
|
|
);
|
|
assert_eq!(
|
|
resolution_at(&s, nth(src, "other_base", 0)),
|
|
Resolution::ExternalCandidate { paths: vec!["other_base".to_string()] }
|
|
);
|
|
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()] }
|
|
);
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// 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 verbatim_members_are_absent_but_nothing_crashes() {
|
|
let src = "\
|
|
class c;
|
|
rand int x;
|
|
constraint c_x { x inside {[0:7]}; }
|
|
covergroup cg;
|
|
coverpoint x;
|
|
endgroup
|
|
endclass
|
|
";
|
|
let s = sem(src);
|
|
// No symbol for the constraint or covergroup names, no reference inside.
|
|
assert!(s.symbols().iter().all(|sym| sym.name != "c_x" && sym.name != "cg"));
|
|
assert!(s.reference_at(nth(src, "x inside", 0)).is_none());
|
|
assert!(s.reference_at(nth(src, "coverpoint x", 0) + 11).is_none());
|
|
// 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);
|
|
}
|
|
}
|
|
|
|
// ----------------------------------------------------------------------
|
|
// 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)) => {
|
|
assert_eq!(defs[0].kind, SymbolKind::Param);
|
|
assert_eq!(&sources[0].1[defs[0].name_span.as_range()], "K");
|
|
}
|
|
other => panic!("expected the param, 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")));
|
|
}
|
|
|
|
#[test]
|
|
fn inherited_candidates_are_not_the_workspace_overlays_business() {
|
|
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 miss on `held` is an inherited candidate — never chased, even
|
|
// though the base is right there in the workspace (the documented
|
|
// future round).
|
|
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() }
|
|
);
|
|
let overlay = resolve_external(&s, &ws);
|
|
let id = s.reference_id_at(nth(sources[1].1, "held = 1", 0)).expect("an id");
|
|
assert!(overlay.get(id).is_none(), "the overlay holds external candidates only");
|
|
}
|