Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1833,22 +1833,22 @@ generated/binary_op/11.4.8--binary_op_bit_xor.sv 2state pass
generated/binary_op/11.4.8--binary_op_bit_xor.sv 4state pass
generated/easyUVM/top.sv 2state fail
generated/easyUVM/top.sv 4state fail
generated/encapsulation/8.18--inherited_local_from_inside.sv 2state fail
generated/encapsulation/8.18--inherited_local_from_inside.sv 4state fail
generated/encapsulation/8.18--inherited_local_from_outside.sv 2state fail
generated/encapsulation/8.18--inherited_local_from_outside.sv 4state fail
generated/encapsulation/8.18--inherited_local_from_inside.sv 2state pass
generated/encapsulation/8.18--inherited_local_from_inside.sv 4state pass
generated/encapsulation/8.18--inherited_local_from_outside.sv 2state pass
generated/encapsulation/8.18--inherited_local_from_outside.sv 4state pass
generated/encapsulation/8.18--inherited_prot_from_inside.sv 2state pass
generated/encapsulation/8.18--inherited_prot_from_inside.sv 4state pass
generated/encapsulation/8.18--inherited_prot_from_outside.sv 2state fail
generated/encapsulation/8.18--inherited_prot_from_outside.sv 4state fail
generated/encapsulation/8.18--inherited_prot_from_outside.sv 2state pass
generated/encapsulation/8.18--inherited_prot_from_outside.sv 4state pass
generated/encapsulation/8.18--local_from_inside.sv 2state pass
generated/encapsulation/8.18--local_from_inside.sv 4state pass
generated/encapsulation/8.18--local_from_outside.sv 2state fail
generated/encapsulation/8.18--local_from_outside.sv 4state fail
generated/encapsulation/8.18--local_from_outside.sv 2state pass
generated/encapsulation/8.18--local_from_outside.sv 4state pass
generated/encapsulation/8.18--prot_from_inside.sv 2state pass
generated/encapsulation/8.18--prot_from_inside.sv 4state pass
generated/encapsulation/8.18--prot_from_outside.sv 2state fail
generated/encapsulation/8.18--prot_from_outside.sv 4state fail
generated/encapsulation/8.18--prot_from_outside.sv 2state pass
generated/encapsulation/8.18--prot_from_outside.sv 4state pass
generated/equality_operators_sim/11.4.5--equality_operator_0.sv 2state fail
generated/equality_operators_sim/11.4.5--equality_operator_0.sv 4state fail
generated/equality_operators_sim/11.4.5--equality_operator_1.sv 2state fail
Expand Down
4 changes: 4 additions & 0 deletions crates/celox-sv-analyzer/src/ast/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ impl Property {
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct StaticProperty {
pub name: String,
pub local: bool,
pub protected: bool,
/// The class that declares the property.
pub owner: String,
pub r#type: Type,
Expand Down Expand Up @@ -460,6 +462,8 @@ pub(super) fn class(
if property.qualifiers.is_static {
statics.retain(|known| known.name != property_name);
statics.push(StaticProperty {
local: property.qualifiers.local,
protected: property.qualifiers.protected,
variable: Class::subroutine_name(name, &property_name),
name: property_name,
owner: name.to_owned(),
Expand Down
6 changes: 3 additions & 3 deletions crates/celox-sv-analyzer/src/ast/expression_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ pub(super) use direct::{
pub(super) use lvalues::{net_lvalue, variable_lvalue};
pub(super) use objects::{
Cast as ObjectCast, Receiver as ObjectReceiver, cast as object_cast,
check_assignment as check_handle_assignment, constructor as object_constructor,
method as object_method, property as object_property, rand_mode_call as object_rand_mode_call,
receiver as object_receiver, this as object_this,
check_access as check_member_access, check_assignment as check_handle_assignment,
constructor as object_constructor, method as object_method, property as object_property,
rand_mode_call as object_rand_mode_call, receiver as object_receiver, this as object_this,
};
pub(super) use strings::{
call as string_call, method as string_method, native_string, to_string as string_operand,
Expand Down
138 changes: 110 additions & 28 deletions crates/celox-sv-analyzer/src/ast/expression_binding/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,38 @@ pub(in crate::ast) fn native_class(
property.r#type.class().map(str::to_owned)
}

/// Access depends on the lexical class, not the class of the receiver (8.18).
pub(in crate::ast) fn check_access(
tree: &AnalysisContext<'_>,
dimensions: &PackedDimensions,
owner: &str,
name: &str,
local: bool,
protected: bool,
) -> Converted<()> {
if !local && !protected {
return Ok(());
}
if let Some(scope) = dimensions.class_scope.as_deref() {
if scope == owner
|| (!local
&& class(tree, dimensions, scope)?.derives_from(
tree,
owner,
&dimensions.const_env,
&dimensions.type_aliases,
))
{
return Ok(());
}
}
Err(AnalyzerError::InaccessibleClassMember {
owner: owner.to_owned(),
name: name.to_owned(),
visibility: if local { "local" } else { "protected" },
})
}

/// The class whose method is being bound, whose properties and methods
/// unqualified names may refer to.
pub(in crate::ast) fn scope_class(dimensions: &PackedDimensions) -> Option<String> {
Expand Down Expand Up @@ -115,9 +147,20 @@ pub(in crate::ast) fn static_property(
let Some(class_name) = class_name else {
return Ok(None);
};
Ok(class(tree, dimensions, &class_name)?
.static_property(&instances::normalize_identifier(&name.spelling))
.map(|property| property.variable.clone()))
let class = class(tree, dimensions, &class_name)?;
let Some(property) = class.static_property(&instances::normalize_identifier(&name.spelling))
else {
return Ok(None);
};
check_access(
tree,
dimensions,
&property.owner,
Comment on lines +155 to +158

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Preserve class scope when checking static property initializers

When a static property initializer accesses a local or protected member of its own class, this check rejects valid code. For example, class C; local static int secret = 1; static int value = C::secret; endclass now produces InaccessibleClassMember. In procedural.rs, initial_processes_from_scope collects static property initializers and binds them using module-level item_dimensions, without setting class_scope, so the new guard treats this same-class access as external. Preserve each initializer's owning class and bind it in that lexical scope before enforcing visibility.

Useful? React with 👍 / 👎.

&property.name,
property.local,
property.protected,
)?;
Ok(Some(property.variable.clone()))
}

/// The property that native expression `id` names, `h.p` or an unqualified
Expand Down Expand Up @@ -157,9 +200,28 @@ pub(in crate::ast) fn property(
_ => return Ok(None),
};
let class = class(tree, dimensions, &class_name)?;
let Some(property) = class.property(&instances::normalize_identifier(&name.spelling)) else {
let name = instances::normalize_identifier(&name.spelling);
let Some(property) = class.property(&name) else {
if let Some(property) = class.static_property(&name) {
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
}
return Ok(None);
};
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
if receiver.is_none() && dimensions.get(THIS).is_none() {
return Err(unsupported(format!(
"non-static property `{}` in a static method",
Expand Down Expand Up @@ -202,8 +264,26 @@ pub(in crate::ast) fn unqualified_property(
};
let class = class(tree, dimensions, &class_name)?;
let Some(property) = class.property(name) else {
if let Some(property) = class.static_property(name) {
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
}
return Ok(None);
};
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)?;
if dimensions.get(THIS).is_none() {
return Err(unsupported(format!(
"non-static property `{name}` in a static method"
Expand Down Expand Up @@ -300,6 +380,14 @@ pub(in crate::ast) fn method(
let Some(method) = class.method(&method_name) else {
return Ok(None);
};
check_access(
tree,
dimensions,
&method.owner,
&method.name,
method.qualifiers.local,
method.qualifiers.protected,
)?;
if through_super && method.is_pure() {
return Err(unsupported(format!(
"pure virtual method `{}` called through `super`",
Expand Down Expand Up @@ -485,6 +573,16 @@ pub(in crate::ast) fn constructor(
_ => return Ok(None),
};
let class = class(tree, dimensions, &class_name)?;
if let Some(method) = class.method("new") {
check_access(
tree,
dimensions,
&method.owner,
&method.name,
method.qualifiers.local,
method.qualifiers.protected,
)?;
}
if class.is_abstract {
return Err(unsupported(format!(
"object of abstract class `{class_name}`"
Expand Down Expand Up @@ -674,28 +772,12 @@ fn check_random_access(
dimensions: &PackedDimensions,
property: &classes::Property,
) -> Converted<()> {
let scope = scope_class(dimensions);
let accessible = if property.local {
scope.as_deref() == Some(&property.owner)
} else if property.protected {
scope
.and_then(|name| class(tree, dimensions, &name).ok())
.is_some_and(|class| {
class.derives_from(
tree,
&property.owner,
&dimensions.const_env,
&dimensions.type_aliases,
)
})
} else {
true
};
if !accessible {
return Err(unsupported(format!(
"randomization access to non-public property `{}`",
property.name
)));
}
Ok(())
check_access(
tree,
dimensions,
&property.owner,
&property.name,
property.local,
property.protected,
)
}
15 changes: 15 additions & 0 deletions crates/celox-sv-analyzer/src/ast/procedural.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,11 @@ impl<'s, 't, 'a> BodyBuilder<'s, 't, 'a> {
.push(entry);
}
for property in &class.statics {
// A base's local static is not visible in the derived class (8.18).
// Keep it out of the alias table so binding can diagnose its use.
if property.local && property.owner != class.name {
continue;
}
let entry = self.take_binding(
&property.name,
Some(property.variable.clone()),
Expand Down Expand Up @@ -2468,6 +2473,16 @@ impl BodyBuilder<'_, '_, '_> {
&self.dims.const_env,
&self.dims.type_aliases,
)?;
if let Some(method) = base.method("new") {
expression_binding::check_member_access(
self.tree,
&self.dims,
&method.owner,
&method.name,
method.qualifiers.local,
method.qualifiers.protected,
)?;
}
let formals = base.constructor_formals(native);
if arguments.len() > formals {
return Err(unsupported(format!(
Expand Down
7 changes: 7 additions & 0 deletions crates/celox-sv-analyzer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ pub enum AnalyzerError {
Parse(String),
#[error("Unsupported SystemVerilog construct: {0}")]
Unsupported(String),
/// A class member is inaccessible from the lexical scope (IEEE 1800-2023 8.18).
#[error("{visibility} class member `{owner}::{name}` is not accessible from this scope")]
InaccessibleClassMember {
owner: String,
name: String,
visibility: &'static str,
},
/// A dotted name whose first component names no variable or interface
/// of the scope: a hierarchical name (IEEE 1800-2023 23.6) that the
/// caller did not resolve to a signal of another instance.
Expand Down
Loading
Loading