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
15 changes: 15 additions & 0 deletions conformance/sv-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ runs at the pinned revision report 534 passing cases in 2state and 536 in
interface classes, classes in packages, parameterized classes and shallow copies
stay unsupported.

Virtual methods, abstract classes and classes implementing interface classes
update eighteen more rows: both state modes for `8.15--super-default-new`,
`8.20--virtual_method`, `8.21--abstract_class`, `8.22--dynamic_method_lookup`,
`8.26.2--implements`, `8.26.2--implements_extends`,
`8.26.2--implements_multiple`, `8.26.6.1--name_conflict_resolved` and
`8.26.7--partial_implementation` change from fail to pass. Each was simulated
and its displayed values checked: the override of the object's class runs
through base-class handles and an array of them, and the shadowed property of
`8.20--virtual_method` reads the derived class's value. Full official runs at
the pinned revision report 543 passing cases in 2state and 545 in 4state, with
no passing case regressing. `8.26.6.1--name_conflict_unresolved` stays a
passing negative case: its return types conflict, which is rejected. Calls
through interface class handles, `$cast`, parameterized classes and shallow
copies stay unsupported.

The report contains each case’s state mode, verdict, selected execution mode,
tags, tool return code and path to the official log. The output directory retains
both the official log and runner diagnostics for each case and state mode.
Expand Down
36 changes: 18 additions & 18 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1473,8 +1473,8 @@ chapter-8/8.13--inheritance.sv 2state pass
chapter-8/8.13--inheritance.sv 4state pass
chapter-8/8.14--override_member.sv 2state pass
chapter-8/8.14--override_member.sv 4state pass
chapter-8/8.15--super-default-new.sv 2state fail
chapter-8/8.15--super-default-new.sv 4state fail
chapter-8/8.15--super-default-new.sv 2state pass
chapter-8/8.15--super-default-new.sv 4state pass
chapter-8/8.15--super.sv 2state pass
chapter-8/8.15--super.sv 4state pass
chapter-8/8.16--cast_func.sv 2state fail
Expand All @@ -1489,14 +1489,14 @@ chapter-8/8.19--global_constant.sv 2state pass
chapter-8/8.19--global_constant.sv 4state pass
chapter-8/8.19--instance_constant.sv 2state pass
chapter-8/8.19--instance_constant.sv 4state pass
chapter-8/8.20--virtual_method.sv 2state fail
chapter-8/8.20--virtual_method.sv 4state fail
chapter-8/8.21--abstract_class.sv 2state fail
chapter-8/8.21--abstract_class.sv 4state fail
chapter-8/8.20--virtual_method.sv 2state pass
chapter-8/8.20--virtual_method.sv 4state pass
chapter-8/8.21--abstract_class.sv 2state pass
chapter-8/8.21--abstract_class.sv 4state pass
chapter-8/8.21--abstract_class_inst.sv 2state pass
chapter-8/8.21--abstract_class_inst.sv 4state pass
chapter-8/8.22--dynamic_method_lookup.sv 2state fail
chapter-8/8.22--dynamic_method_lookup.sv 4state fail
chapter-8/8.22--dynamic_method_lookup.sv 2state pass
chapter-8/8.22--dynamic_method_lookup.sv 4state pass
chapter-8/8.23--scope_resolution.sv 2state pass
chapter-8/8.23--scope_resolution.sv 4state pass
chapter-8/8.24--out_of_block_methods.sv 2state pass
Expand All @@ -1507,12 +1507,12 @@ chapter-8/8.25.1--parametrized_class_invalid_scope_resolution.sv 2state pass
chapter-8/8.25.1--parametrized_class_invalid_scope_resolution.sv 4state pass
chapter-8/8.25.1--parametrized_class_scope_resolution.sv 2state fail
chapter-8/8.25.1--parametrized_class_scope_resolution.sv 4state fail
chapter-8/8.26.2--implements.sv 2state fail
chapter-8/8.26.2--implements.sv 4state fail
chapter-8/8.26.2--implements_extends.sv 2state fail
chapter-8/8.26.2--implements_extends.sv 4state fail
chapter-8/8.26.2--implements_multiple.sv 2state fail
chapter-8/8.26.2--implements_multiple.sv 4state fail
chapter-8/8.26.2--implements.sv 2state pass
chapter-8/8.26.2--implements.sv 4state pass
chapter-8/8.26.2--implements_extends.sv 2state pass
chapter-8/8.26.2--implements_extends.sv 4state pass
chapter-8/8.26.2--implements_multiple.sv 2state pass
chapter-8/8.26.2--implements_multiple.sv 4state pass
chapter-8/8.26.3--type_access_extends.sv 2state pass
chapter-8/8.26.3--type_access_extends.sv 4state pass
chapter-8/8.26.3--type_access_implements.sv 2state fail
Expand All @@ -1529,8 +1529,8 @@ chapter-8/8.26.5--implemented_class_handle.sv 2state pass
chapter-8/8.26.5--implemented_class_handle.sv 4state pass
chapter-8/8.26.5--invalid_interface_instantiation.sv 2state pass
chapter-8/8.26.5--invalid_interface_instantiation.sv 4state pass
chapter-8/8.26.6.1--name_conflict_resolved.sv 2state fail
chapter-8/8.26.6.1--name_conflict_resolved.sv 4state fail
chapter-8/8.26.6.1--name_conflict_resolved.sv 2state pass
chapter-8/8.26.6.1--name_conflict_resolved.sv 4state pass
chapter-8/8.26.6.1--name_conflict_unresolved.sv 2state pass
chapter-8/8.26.6.1--name_conflict_unresolved.sv 4state pass
chapter-8/8.26.6.2--parameter_type_conflict.sv 2state pass
Expand All @@ -1541,8 +1541,8 @@ chapter-8/8.26.6.3--diamond_relationship.sv 2state pass
chapter-8/8.26.6.3--diamond_relationship.sv 4state pass
chapter-8/8.26.6.3--diamond_relationship_parametrized.sv 2state fail
chapter-8/8.26.6.3--diamond_relationship_parametrized.sv 4state fail
chapter-8/8.26.7--partial_implementation.sv 2state fail
chapter-8/8.26.7--partial_implementation.sv 4state fail
chapter-8/8.26.7--partial_implementation.sv 2state pass
chapter-8/8.26.7--partial_implementation.sv 4state pass
chapter-8/8.27--forward_declaration.sv 2state pass
chapter-8/8.27--forward_declaration.sv 4state pass
chapter-8/8.4--instantiation.sv 2state pass
Expand Down
187 changes: 181 additions & 6 deletions crates/celox-sv-analyzer/src/ast/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,19 @@ pub(super) struct Method {
pub name: String,
/// The class that declares the method.
pub owner: String,
/// The definition, or the prototype of a pure virtual method.
pub native: celox_sv_syntax::SubroutineId,
pub qualifiers: celox_sv_syntax::ClassQualifiers,
/// Declared `virtual` here or in a base: a call through a handle runs
/// the override of the object's class (IEEE 1800-2023 8.20).
pub is_virtual: bool,
}

impl Method {
/// A pure virtual method, which has no implementation (8.21).
pub fn is_pure(&self) -> bool {
self.qualifiers.is_pure
}
}

/// A class with the layout of its objects.
Expand All @@ -58,6 +69,8 @@ pub(super) struct Class {
/// Identifies the class at run time; stable across modules and runs.
pub id: u32,
pub base: Option<String>,
/// A `virtual class`, which cannot be constructed (IEEE 1800-2023 8.21).
pub is_abstract: bool,
/// Properties of the class and its bases, bases first.
pub properties: Vec<Property>,
/// How many of `properties` the bases declare.
Expand Down Expand Up @@ -111,6 +124,40 @@ impl Class {
format!("{owner}::{method}")
}

/// The subroutine that calls the override of virtual method `method`
/// of `owner` for the class of its `this` (IEEE 1800-2023 8.20).
pub fn dispatcher_name(owner: &str, method: &str) -> String {
format!("{owner}::{method}$virtual")
}

/// The virtual methods the class itself declares, each with a
/// dispatcher.
pub fn dispatched_methods(&self) -> impl Iterator<Item = &Method> {
self.methods
.iter()
.filter(|method| method.owner == self.name && method.is_virtual)
}

/// Whether the class is `ancestor` or derives from it.
pub fn derives_from(
&self,
tree: &AnalysisContext<'_>,
ancestor: &str,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
) -> bool {
let mut class = Some(self.name.clone());
while let Some(name) = class {
if name == ancestor {
return true;
}
class = self::class(tree, &name, env, aliases)
.ok()
.and_then(|class| class.base.clone());
}
false
}

/// An allocation of an uninitialized object of the class.
pub fn allocation(&self) -> Expr {
let handles = self
Expand Down Expand Up @@ -330,12 +377,14 @@ pub(super) fn class(
for method in &declaration.methods {
let subroutine = native.subroutine(method.subroutine);
let method_name = instances::normalize_identifier(&subroutine.name.spelling);
// A pure virtual method has no implementation to call; calls of
// virtual methods are rejected where they are bound.
if method.qualifiers.is_pure {
continue;
}
let native_id = if subroutine.prototype {
// A method overriding a virtual method is virtual (8.20).
let is_virtual = method.qualifiers.is_virtual
|| methods
.iter()
.any(|known| known.name == method_name && known.is_virtual);
let native_id = if method.qualifiers.is_pure {
method.subroutine
} else if subroutine.prototype {
out_of_block_definition(tree, name, &method_name).ok_or_else(|| {
unsupported(format!(
"extern method `{name}::{method_name}` without a body"
Expand All @@ -350,12 +399,55 @@ pub(super) fn class(
owner: name.to_owned(),
native: native_id,
qualifiers: method.qualifiers,
is_virtual,
});
}
// A method of an implemented interface class is virtual, and the class
// implements it with the same return type (8.26.2, 8.26.6.1). An
// abstract class may instead re-declare it pure (8.26.7).
let mut required = Vec::new();
for &interface in &declaration.interfaces {
interface_methods(tree, interface, env, aliases, &mut required)?;
}
for (method_name, prototype) in required {
match methods.iter_mut().find(|method| method.name == method_name) {
Some(method) => {
if !method.is_virtual && method.owner != name {
return Err(unsupported(format!(
"interface class method `{method_name}` implemented by a non-virtual method of a base class"
)));
}
if return_type(tree, method.native, env, aliases)?
!= return_type(tree, prototype, env, aliases)?
{
return Err(unsupported(format!(
"method `{method_name}` of class `{name}` whose return type differs from its interface class"
)));
}
method.is_virtual = true;
}
None => {
return Err(unsupported(format!(
"class `{name}` that does not implement interface class method `{method_name}`"
)));
}
}
}
// A class that is not abstract implements every pure virtual method
// (8.21).
if !declaration.virtual_class
&& let Some(method) = methods.iter().find(|method| method.is_pure())
{
return Err(unsupported(format!(
"class `{name}` that does not implement pure virtual method `{}`",
method.name
)));
}
let class = Rc::new(Class {
name: name.to_owned(),
id: class_id(name),
base: base.map(|base| base.name.clone()),
is_abstract: declaration.virtual_class,
properties,
base_properties,
statics,
Expand All @@ -369,6 +461,48 @@ pub(super) fn class(
Ok(class)
}

/// The methods that interface class type `interface` declares or inherits,
/// with their prototypes, appended to `methods` (IEEE 1800-2023 8.26). A
/// name that several interface classes declare appears once for each.
fn interface_methods(
tree: &AnalysisContext<'_>,
interface: celox_sv_syntax::TypeId,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
methods: &mut Vec<(String, celox_sv_syntax::SubroutineId)>,
) -> Converted<()> {
let native = tree.declarations();
let name = type_binding::procedural_type(interface, native, tree, env, aliases)?
.and_then(|ty| ty.class.clone())
.ok_or_else(|| unsupported("implemented interface class type"))?;
let declaration = declaration(tree, &name)
.filter(|declaration| declaration.interface)
.ok_or_else(|| unsupported(format!("`implements` of `{name}`, not an interface class")))?;
for method in &declaration.methods {
let method_name =
instances::normalize_identifier(&native.subroutine(method.subroutine).name.spelling);
methods.push((method_name, method.subroutine));
}
for &base in &declaration.interfaces {
interface_methods(tree, base, env, aliases, methods)?;
}
Ok(())
}

/// The return type of subroutine `id`, `None` for a task or `void`.
fn return_type(
tree: &AnalysisContext<'_>,
id: celox_sv_syntax::SubroutineId,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
) -> Converted<Option<Type>> {
let native = tree.declarations();
match native.subroutine(id).return_type {
Some(ty) => type_binding::procedural_type(ty, native, tree, env, aliases),
None => Ok(None),
}
}

/// The definition `function ... class::method` outside class `class`.
fn out_of_block_definition(
tree: &AnalysisContext<'_>,
Expand Down Expand Up @@ -428,6 +562,47 @@ fn class_id(name: &str) -> u32 {
hash.max(1)
}

/// The overrides that dispatcher `Class::dispatcher_name(owner, method)`
/// calls: each implementation of the method with the classes, deriving from
/// `owner` and not abstract, whose objects run it.
pub(super) fn dispatch_targets(
tree: &AnalysisContext<'_>,
owner: &str,
method: &str,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
) -> Converted<Vec<(String, Vec<u32>)>> {
let mut targets: Vec<(String, Vec<u32>)> = Vec::new();
let mut ids = HashMap::default();
for declaration in &tree.declarations().classes {
if declaration.interface || declaration.virtual_class {
continue;
}
let name = instances::normalize_identifier(&declaration.name.spelling);
// A class that cannot be laid out has no objects.
let Ok(class) = class(tree, &name, env, aliases) else {
continue;
};
if !class.derives_from(tree, owner, env, aliases) {
continue;
}
if let Some(other) = ids.insert(class.id, name.clone()) {
return Err(unsupported(format!(
"classes `{other}` and `{name}` with the same run-time identifier"
)));
}
let Some(implementation) = class.method(method) else {
continue;
};
let target = Class::subroutine_name(&implementation.owner, method);
match targets.iter_mut().find(|(known, _)| *known == target) {
Some((_, classes)) => classes.push(class.id),
None => targets.push((target, vec![class.id])),
}
}
Ok(targets)
}

/// A call of object operation `op`.
pub(super) fn object_call(op: ObjectOp, args: Vec<Expr>) -> Expr {
Expr::Call {
Expand Down
10 changes: 10 additions & 0 deletions crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,16 @@ fn class_signatures(
} else {
return_type(subroutine, tree, context)?
};
// A method that may be virtual also has a dispatcher (8.20).
if !method.qualifiers.is_static && subroutine.kind != SubroutineKind::Constructor {
publish_signature(
classes::Class::dispatcher_name(&class_name, &name(&subroutine.name)),
formals.clone(),
result.clone(),
tree,
context,
);
}
publish_signature(
classes::Class::subroutine_name(&class_name, &name(&subroutine.name)),
formals,
Expand Down
17 changes: 15 additions & 2 deletions crates/celox-sv-analyzer/src/ast/expression_binding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,21 @@ pub(super) fn expression(
LValue::Ident(name) => dimensions.get(name),
_ => None,
};
// `new` creates an object of the variable's class (IEEE 1800-2023 8.7).
if let Some(class) = shape.and_then(|shape| shape.class.as_deref())
// `new` creates an object of the class of the variable or of the
// element of an array of handles (IEEE 1800-2023 8.7).
let class = match lhs {
LValue::Ident(_) => shape.and_then(|shape| shape.class.as_deref()),
LValue::Select {
name,
array_slice_width: None,
..
} => dimensions
.get(name)
.filter(|shape| !shape.unpacked.is_empty())
.and_then(|shape| shape.class.as_deref()),
LValue::Select { .. } => None,
};
if let Some(class) = class
&& let Some(call) = objects::constructor(tree, root, native, dimensions, class)?
{
return Ok(call);
Expand Down
Loading
Loading