Repository navigation
feat(sv): support classes declared in packages and the compilation unit - #1850
Conversation
|
This pull request is part of a Mergify stack:
|
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 85455f4c50
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for class in &imported.classes { | ||
| for method in class.dispatched_methods() { | ||
| let name = classes::Class::dispatcher_name(&class.name, &method.name); |
There was a problem hiding this comment.
Rebuild imported cast helpers for locally declared subclasses
When a module declares D extends p::B, $cast(b, d) with destination type p::B calls the imported p::B cast helper. That helper's accepted class IDs were fixed by lower_cast while analyzing the package, before D was visible. This new loop rebuilds virtual dispatchers but leaves cast helpers unchanged, so the valid cast returns 0 and leaves the destination unchanged. Regenerate imported cast helpers against the consuming scope's visible classes as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: the importing scope rebuilds an imported $cast helper over the classes it sees when one of its own classes is a target, as it does for virtual dispatchers; $cast(b, d) with D extends p::B returns 1. Covered by classes_of_packages_in_other_sources_keep_their_behaviour.
| pub(super) fn handle_type(tree: &AnalysisContext<'_>, name: &str) -> Option<Type> { | ||
| declaration(tree, name)?; | ||
| Some(handle(name)) | ||
| (declaration(tree, name).is_some() || imported(tree, name).is_some()).then(|| handle(name)) |
There was a problem hiding this comment.
Recognize imported layouts when validating interface classes
When an interface class is declared in a separate package source, a module class using implements p::I still fails in interface_name: that function requires declaration(tree, &name) to find a native interface declaration in the consumer's source. Although this change makes the imported handle type and layout available, that check never consults the imported layout's is_interface flag and rejects the valid implementation as “not an interface class.” Validate imported interfaces through their exported layouts too; the added same-source package test does not exercise this path.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: an imported interface class is validated by its exported layout. Covered by classes_of_packages_in_other_sources_keep_their_behaviour (implements p::I with the package in another source).
| let Some(declaration) = declaration else { | ||
| let class = imported(tree, name).ok_or_else(|| unsupported(format!("class `{name}`")))?; | ||
| tree.state | ||
| .classes | ||
| .borrow_mut() | ||
| .insert(name.to_owned(), class.clone()); | ||
| return Ok(class); |
There was a problem hiding this comment.
Remove consumer-source lookups when checking imported hooks
For a package class declared in another source that defines pre_randomize or post_randomize, calling object.randomize() invokes Class::check_randomize(tree), which still indexes the consumer's declarations with method.native. The imported layout retains the declaring source's SubroutineId, so a consumer with no subroutines panics in Declarations::subroutine; with enough unrelated subroutines it validates the wrong declaration instead. Export the hook metadata needed by this check, or perform the validation in the declaring scope, rather than dereferencing the foreign ID.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: methods export whether they are functions and their formals, and check_randomize uses those instead of a syntax id. Covered by classes_of_packages_in_other_sources_keep_their_behaviour (pre_randomize of a class from another source).
| for expression in &native.expressions { | ||
| if let ExprKind::Name(name) = &expression.kind | ||
| && !pattern_keys.contains(&expression.tokens.start) | ||
| && !qualified.iter().any(|base| { | ||
| native.expression(*base).tokens == expression.tokens | ||
| && matches!(&native.expression(*base).kind, ExprKind::Name(_)) | ||
| }) | ||
| { | ||
| add(&name.spelling, expression.tokens.start); |
There was a problem hiding this comment.
Exclude actual package qualifiers from wildcard binding
When two wildcard-imported packages both declare an item named p, referencing an unrelated package through p::x now incorrectly triggers an ambiguous wildcard-import error for p. This loop adds every expression name, including actual package qualifiers, to unqualified_names; resolve_imports subsequently passes those names to candidates, which reports the conflict even though no unqualified package item was referenced. Previously package qualifiers were excluded. Distinguish actual package qualifiers from imported class qualifiers before treating them as wildcard references.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: a :: scope that names a package no longer counts as a wildcard-import reference. Covered by classes_of_packages_in_other_sources_keep_their_behaviour (two wildcard packages declaring p next to p::B).
| /// The classes the scope declares, laid out, by their qualified names | ||
| /// (IEEE 1800-2023 26.3): a scope that uses them may be analyzed from | ||
| /// another source, without their declarations. | ||
| pub classes: Vec<classes::Class>, |
There was a problem hiding this comment.
Export class-scoped typedefs alongside imported layouts
For a package containing class C; typedef bit [7:0] word_t; endclass, a consuming module with import p::C; C::word_t value; cannot resolve the typedef. class_scope resolves the imported class correctly, but specializations::member_type searches only tree.state.class_specializations; module analysis creates a fresh AnalysisState, and the exported Class layout contains none of the specialization aliases. This also affects non-parameterized classes, whose typedefs are stored in their sole specialization. Export the resolved class-scoped types and make member lookup use them rather than relying on the declaring analysis's private state.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Tracked in #1892: class-scoped types of an imported class are not exported yet; such a use reports the type unknown.
|
Queued — the merge queue status continues in this comment ↓. |
75eeaba to
2a56321
Compare
85455f4 to
6de9ff1
Compare
Revision history
|
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 6de9ff1cce
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| for class in &imported.classes { | ||
| for method in class.dispatched_methods() { | ||
| let name = classes::Class::dispatcher_name(&class.name, &method.name); |
There was a problem hiding this comment.
Rebuild imported randomization dispatch for local subclasses
When a module declares D extends p::B and calls randomize() through a p::B handle holding a D, the call still uses the package's previously lowered p::B::randomize. Its dynamic-class cases were generated before D was visible, and this loop rebuilds only user-declared virtual dispatchers. The call consequently falls through to ObjectOp::Check(B.id), which rejects the derived object with RandomizationClassMismatch instead of randomizing it. Object-wide rand_mode has the same problem. Rebuild these imported built-in dispatchers against the consumer's visible subclasses as well.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Tracked in #1892: the imported randomization dispatch is not rebuilt for module subclasses yet; the call fails with a runtime error rather than randomizing the wrong object.
| let Some(name) = classes::resolve(tree, &name) else { | ||
| return Ok(None); | ||
| } | ||
| }; |
There was a problem hiding this comment.
Resolve the qualified specialization key inside package classes
Inside a parameterized package class, an unqualified self-scope such as C::N no longer finds the active specialization: specializations::specialize now stores its scope alias as p::C::, but the preceding lookup still searches for C::. Resolving C to p::C here happens after that lookup, so named_class reports UnspecializedClassScope despite being inside the specialization. For example, a package containing class C #(int N = 1); static function int f(); return C::N; endfunction endclass and typedef C#(2) C2; cannot lower f. Look up the active specialization using the resolved class name; the analogous lookup in type_binding::class_scope needs the same treatment for self-qualified types.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: the scope-of-specialization lookup also tries the resolved (qualified) class name, so C::N inside a parameterized package class works. Covered by classes_of_packages_in_other_sources_keep_their_behaviour (p::P2::f()).
| &instances::normalize_identifier(&package.spelling), | ||
| &instances::normalize_identifier(&name.spelling), | ||
| ); | ||
| return Ok(classes::handle_type(tree, &class).map(|_| class)); |
There was a problem hiding this comment.
Resolve package-qualified class typedefs before member lookup
When a package exports a typedef of a class, such as class C; endclass typedef C Alias;, a consumer can bind the handle type p::Alias, but p::Alias::new cannot resolve its constructor. This new branch checks only handle_type(tree, "p::Alias"), which requires a class declaration or exported layout with that exact name; the layout is actually named p::C. Unlike the unqualified-name path below, it never checks dimensions.type_aliases for the underlying class. This also prevents qualified static calls through exported typedefs of parameterized specializations, even though those typedefs are the supported way to use such specializations outside their declaring scope. Resolve the qualified type alias to its class before checking the layout.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: p::Alias:: resolves the package typedef to its class first. Covered by classes_of_packages_in_other_sources_keep_their_behaviour (p::Alias::new).
| Some(unit) if unit.kind == celox_sv_syntax::UnitKind::Package => format!( | ||
| "{}::{name}", | ||
| instances::normalize_identifier(&unit.name.spelling) | ||
| ), |
There was a problem hiding this comment.
Restrict extern method lookup to the declaring package
When two packages in the same source each declare a class C with an extern method f, their layouts now receive distinct qualified names, but both declared_methods and class still call out_of_block_definition with the unqualified declaration spelling C. That helper scans every subroutine in the source and returns the first matching C::f, without checking its enclosing package. Consequently, if p defines C::f to return 1 and q defines it to return 2, q::C::f() is lowered using the body from p and returns 1. Match the definition's enclosing package as well as its class and method names.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 8ce6209: an out-of-block definition must be in the same package (or module, or compilation unit) as its class, so q::C::f and r::C::f keep their own bodies. Covered by classes_of_packages_in_other_sources_keep_their_behaviour.
2a56321 to
e11b661
Compare
6de9ff1 to
8ce6209
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 8ce62098c8
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| instances::normalize_identifier(&unit.name.spelling) | ||
| ), | ||
| Some(_) => name, | ||
| None => format!("{}::{name}", packages::UNIT), |
There was a problem hiding this comment.
Include compilation-unit identity in class names
When two source files each declare a compilation-unit class named C, both receive the internal name $unit::C, although these are separate declarations. Packages::analyze_with_roots_impl analyzes all compilation units using one shared AnalysisState, so classes::class and specialization lookup reuse the first file's cached layout for the second file. For example, classes with an 8-bit and a 16-bit property named x can both acquire the first layout, truncating values in the second file. Include the declaring compilation unit in the internal class identity while preserving $unit::C as its source-level spelling.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5e90e35: each compilation unit is analyzed with its class caches cleared, and the run-time identifier of a compilation-unit class includes its source path, so two sources' $unit::C keep their own layouts and identifiers. Covered by classes_keep_the_identity_and_signatures_of_their_scopes.
| for class_name in classes::specializations_of(tree, class) { | ||
| let _ = class_signatures(class, &class_name, tree, context); |
There was a problem hiding this comment.
Preserve imported signatures instead of rebinding foreign classes
When a package and its consuming module are in the same source, this loop still visits the package's class declarations and republishes their signatures using the module's PackedDimensions. Module analysis has a fresh specialization state, so class_signatures cannot recover the package's declaration environment and overwrites the correctly imported metadata. For example, a package with N = 8 and a static method returning bit [N-1:0] is rebound as a 16-bit return in a consuming module declaring N = 16, making return-width queries and expression sizing incorrect. Restrict signature discovery to classes declared in the current scope and retain exported signatures for imported classes.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5e90e35: signature discovery publishes only the classes the current scope declares; imported classes keep the signatures their scope exported, so a package method returning bit [N-1:0] keeps the package's N. Covered by classes_keep_the_identity_and_signatures_of_their_scopes.
| if packages.get(package_name).is_none() | ||
| && (imports | ||
| .explicit | ||
| .iter() | ||
| .any(|(_, item)| item == package_name) |
There was a problem hiding this comment.
Recognize class imports declared inside generate blocks
When a generate block imports a package class and uses a class-qualified member, such as if (1) begin : g import p::C; initial value = C::f(); end, this check does not recognize C as an imported class. ScopeImports::from_scope deliberately excludes generate-block imports from imports.explicit and imports.wildcard, keeping them in the generate scopes instead. Consequently, resolution falls through to package(package_name)? and reports an unknown package C before the later generate-scope lookup can bind it. Consult the enclosing generate imports when distinguishing class qualifiers from package qualifiers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Tracked in #1892 (item 3): a class imported inside a generate block and used as C::f() is rejected as an unknown package, not lowered wrongly.
| if targets | ||
| .iter() | ||
| .all(|(_, ids)| ids.iter().all(|id| imported_ids.contains(id))) | ||
| { | ||
| continue; |
There was a problem hiding this comment.
Rebuild dispatchers for subclasses imported from other packages
When package q declares D extends p::B and overrides a virtual method, a module using q::D through a p::B handle still receives the original dispatcher from p. Although analyzing q rebuilds that dispatcher to include D, Packages::closure imports dependencies first and ScopeSymbols::extend keeps the first subroutine with each name, discarding q's rebuilt version. This condition then skips rebuilding in the module because both B and D have imported IDs, leaving no dispatch case for D and failing to call its override. Determine whether the retained dispatcher covers the targets rather than assuming every imported class is already covered.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 5e90e35: an imported dispatcher (or $cast helper) is rebuilt whenever its case labels miss a target the importing scope sees, so q::D extends p::B reaches its override through a p::B handle. Covered by classes_keep_the_identity_and_signatures_of_their_scopes.
e11b661 to
d441141
Compare
5e90e35 to
af3f0a8
Compare
d441141 to
458faa0
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: af3f0a8998
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| /// The definition, or the prototype of a pure virtual method, in the | ||
| /// source of the scope that declares the class. | ||
| pub native: celox_sv_syntax::SubroutineId, |
There was a problem hiding this comment.
Include method ownership when comparing imported syntax IDs
When a base class comes from another source, its inherited methods retain source-local SubroutineId values that can equal those of unrelated methods in the derived class. procedural::lower_subroutine still determines staticness with class.methods.iter().any(|method| method.native == native_id && method.qualifiers.is_static), without checking ownership. For example, if the package source's first method is a static B::g and the module source's first method is an instance D::f, both IDs coincide and D::f is incorrectly lowered without this; accessing an instance property then rejects an otherwise valid design. Restrict that comparison to the declaring class, or use the current method's qualified identity.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 4eb7c7e: lower_subroutine treats a method as static only when the class itself declares it (method.owner == class.name), so an inherited method of a class from another source with the same source-local identifier no longer makes the derived method static. Covered by inherited_members_of_package_classes_keep_their_declarations (fails without the fix: "non-static property y in a static method").
| let Some(declaration) = declaration else { | ||
| let class = imported(tree, name).ok_or_else(|| unsupported(format!("class `{name}`")))?; | ||
| tree.state | ||
| .classes | ||
| .borrow_mut() | ||
| .insert(name.to_owned(), class.clone()); |
There was a problem hiding this comment.
Resolve exported static-property types in their declaring scope
When an imported base has a static property whose width uses a package parameter, the exported layout still contains the unqualified parameter expression. For example, with p::N = 8, B::x declared as static bit [N-1:0] x, and a consuming module declaring N = 16 and D extends p::B, BodyBuilder::enter_class_scope installs the inherited property's unresolved type into the derived method's dimensions. Consequently, $bits(x) inside a method of D evaluates to 16 although the package allocated an 8-bit variable. Resolve the property types against their declaration environment before exporting or importing the layout, rather than cloning those unresolved types into the consumer.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in 4eb7c7e: the exported layouts fix the types of properties and static properties in the declaring scope (type_parameters::resolve with the package's constants and the specialization's parameters) before another scope imports them. Covered by inherited_members_of_package_classes_keep_their_declarations: $bits(x) in a module subclass of p::B is 8 with module N = 16 (16 without the fix).
| if packages.get(package_name).is_none() | ||
| && (imports | ||
| .explicit | ||
| .iter() | ||
| .any(|(_, item)| item == package_name) |
There was a problem hiding this comment.
Recognize class imports inherited from the compilation unit
When import p::C; or import p::*; appears at compilation-unit scope before a module, a class-qualified call such as C::f() inside that module is rejected as an unknown package C. The module's ScopeImports does not contain its enclosing compilation unit's imports, so this condition falls through to package(package_name)? before the later unit.visibility.imports lookup can resolve C. Include the compilation-unit imports visible before the module when distinguishing imported class qualifiers from package qualifiers.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
This is a rejection (unknown package C) rather than a wrong result; added to #1892 (item 4) together with the generate-block import case, since both need the class-qualifier classification to consult imports outside the module's own ScopeImports.
458faa0 to
322bd44
Compare
af3f0a8 to
3493193
Compare
322bd44 to
9120d4d
Compare
3493193 to
4eb7c7e
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
1 similar comment
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
4eb7c7e to
8b00336
Compare
9120d4d to
06d0395
Compare
|
Local review (Codex CLI) of this commit:
Rebased onto master: the wildcard-import name collection now uses the per-scope |
8b00336 to
f03b45c
Compare
06d0395 to
ce9510e
Compare
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: f03b45cdf1
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| let class = scope::qualified_name( | ||
| &tree.state, | ||
| &instances::normalize_identifier(&package.spelling), | ||
| &instances::normalize_identifier(&name.spelling), | ||
| ); |
There was a problem hiding this comment.
Preserve class qualification inside its declaring package
When a package refers to its own class as p::C, scope::qualified_name returns the unqualified name C while enter_package("p") is active. However, class declarations and layouts are now keyed by p::C, so the subsequent handle_type lookup fails. For example, package p; class C; endclass function C make(); return p::C::new; endfunction endpackage cannot resolve its constructor even though C::new works. Preserve the fully qualified class identity here, or resolve the resulting local name before looking up the layout; package-local type binding needs the same treatment.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Fixed in af8df00: inside package p, a qualified p::C whose local name does not name a layout is resolved to the package's class (p::C) both in named_class (so p::C::new constructs) and in type binding (so p::C h; is a handle of p::C). Covered by class::package_classes_by_their_qualified_names.
A class declared in a package or in the compilation unit is named by its scope, `p::C` or `$unit::C`, and is laid out and lowered once, in that scope (IEEE 1800-2023 3.12.1, 26.3). The scope exports the class layouts with its symbols, and the class as a type, so a module uses the class through an import, `p::C`, `p::C::member` or, for the compilation unit, its name, also from another source: it never lays out a class of another scope again, and calls its methods through their exported formals. Static properties of the classes are package variables. A class of another scope is a dispatch and `$cast` target of the scopes that see it; when a class of a module overrides a virtual method of such a class, the module lowers the method's dispatcher again over every class it sees, so calls through package code reach the override too. A specialization of a parameterized class is made from its declaration, so outside its scope only the specializations the scope names are usable; a new one reports that. Static properties follow the existing rule for package variables, which processes of one instance may write (#1146). The sv_tests driver elaborates sources of packages and compilation-unit items alone, reporting the errors of their compilation units. sv-tests: 31 chapter 18 cases that declare only classes pass (1112/1123 passing); seven were simulated with a module using the class, the others pass by elaboration and keep their constraints unchecked, while `randomize()` of a class with constraints stays rejected. Icarus 13.0 rejects the new shared cases on constructs it does not support, recorded with the existing groups; Verilator passes them. Change-Id: I12b64cde387059361235e2b6ccbf6591c4087907
f03b45c to
af8df00
Compare
ce9510e to
0cc4b2e
Compare
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
1 similar comment
|
You have reached your Codex usage limits for code reviews. You can see your limits in the Codex usage dashboard. |
Merge Queue Status
This pull request spent 33 minutes 49 seconds in the queue, including 32 minutes 10 seconds running CI. Required conditions to merge
|
A class declared in a package or in the compilation unit is named by its
scope,
p::Cor$unit::C, and is laid out and lowered once, in that scope(IEEE 1800-2023 3.12.1, 26.3). The scope exports the class layouts with its
symbols, and the class as a type, so a module uses the class through an
import,
p::C,p::C::memberor, for the compilation unit, its name, alsofrom another source: it never lays out a class of another scope again, and
calls its methods through their exported formals. Static properties of the
classes are package variables. A class of another scope is a dispatch and
$casttarget of the scopes that see it; when a class of a module overrides avirtual method of such a class, the module lowers the method's dispatcher
again over every class it sees, so calls through package code reach the
override too.
A specialization of a parameterized class is made from its declaration, so
outside its scope only the specializations the scope names are usable; a
new one reports that. Static properties follow the existing rule for package
variables, which processes of one instance may write (#1146).
The sv_tests driver elaborates sources of packages and compilation-unit items
alone, reporting the errors of their compilation units.
sv-tests: 31 chapter 18 cases that declare only classes pass (1112/1123
passing); seven were simulated with a module using the class, the others
pass by elaboration and keep their constraints unchecked, while
randomize()of a class with constraints stays rejected. Icarus 13.0 rejects the new
shared cases on constructs it does not support, recorded with the existing
groups; Verilator passes them.
Depends-On: #1810