Skip to content
Closed
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
11 changes: 11 additions & 0 deletions conformance/sv-tests/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,17 @@ passing negative case: its return types conflict, which is rejected. Calls
through interface class handles, `$cast`, parameterized classes and shallow
copies stay unsupported.

Interface class handles and `$cast` of class handles update two more rows: both
state modes for `8.26.5--cast_between_interface_classes` change from fail to
pass. The case was simulated with calls added after the cast, which reached the
object through both interface class handles. Full official runs at the pinned
revision report 544 passing cases in 2state and 546 in 4state, with no passing
case regressing: an interface class whose methods use its own typedefs or type
parameters, as in `8.26.3--type_access_extends` and
`8.26.6.2--parameter_type_conflict`, is still elaborated without being lowered.
`6.24.2--cast_fn`, `6.24.2--cast_task` and `8.16--cast_func` keep failing
because they cast to integral and enum variables.

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
4 changes: 2 additions & 2 deletions conformance/sv-tests/expected.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -1523,8 +1523,8 @@ chapter-8/8.26.4--illegal_forward_def_implements.sv 2state pass
chapter-8/8.26.4--illegal_forward_def_implements.sv 4state pass
chapter-8/8.26.4--illegal_implements_parameter.sv 2state pass
chapter-8/8.26.4--illegal_implements_parameter.sv 4state pass
chapter-8/8.26.5--cast_between_interface_classes.sv 2state fail
chapter-8/8.26.5--cast_between_interface_classes.sv 4state fail
chapter-8/8.26.5--cast_between_interface_classes.sv 2state pass
chapter-8/8.26.5--cast_between_interface_classes.sv 4state pass
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
Expand Down
197 changes: 156 additions & 41 deletions crates/celox-sv-analyzer/src/ast/classes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ 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).
/// The interface classes the class implements, or the interface class
/// extends (IEEE 1800-2023 8.26).
pub interfaces: Vec<String>,
/// A `virtual class` or an interface class, which cannot be constructed
/// (IEEE 1800-2023 8.21, 8.26).
pub is_abstract: bool,
/// An interface class: it has no properties and its methods are pure
/// virtual (IEEE 1800-2023 8.26).
pub is_interface: 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 @@ -138,26 +145,38 @@ impl Class {
.filter(|method| method.owner == self.name && method.is_virtual)
}

/// Whether the class is `ancestor` or derives from it.
/// Whether the class is `ancestor`, derives from it or implements it,
/// so that a handle of the class may be assigned to a variable of
/// `ancestor` (IEEE 1800-2023 8.16, 8.26.5).
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 {
let mut pending = vec![self.name.clone()];
let mut seen = HashSet::default();
while let Some(name) = pending.pop() {
if name == ancestor {
return true;
}
class = self::class(tree, &name, env, aliases)
.ok()
.and_then(|class| class.base.clone());
if !seen.insert(name.clone()) {
continue;
}
if let Ok(class) = self::class(tree, &name, env, aliases) {
pending.extend(class.base.iter().chain(&class.interfaces).cloned());
}
}
false
}

/// The subroutine that `$cast` to a variable of `class` calls (IEEE
/// 1800-2023 8.16).
pub fn cast_name(class: &str) -> String {
format!("{class}::$cast")
}

/// An allocation of an uninitialized object of the class.
pub fn allocation(&self) -> Expr {
let handles = self
Expand Down Expand Up @@ -192,8 +211,12 @@ fn declaration<'a>(
.find(|class| instances::normalize_identifier(&class.name.spelling) == name)
}

/// The classes that body item `item` declares, except interface classes,
/// which have no implementation (IEEE 1800-2023 8.26).
/// Whether `name` is an interface class (IEEE 1800-2023 8.26).
pub(super) fn is_interface(tree: &AnalysisContext<'_>, name: &str) -> bool {
declaration(tree, name).is_some_and(|declaration| declaration.interface)
}

/// The classes and interface classes that body item `item` declares.
pub(super) fn item_classes(
item: celox_sv_syntax::BodyItemId,
tree: &AnalysisContext<'_>,
Expand All @@ -208,9 +231,7 @@ pub(super) fn item_classes(
.classes
.iter()
.filter(|class| {
!class.interface
&& item.tokens.start <= class.tokens.start
&& class.tokens.end <= item.tokens.end
item.tokens.start <= class.tokens.start && class.tokens.end <= item.tokens.end
})
.map(|class| instances::normalize_identifier(&class.name.spelling))
.collect()
Expand Down Expand Up @@ -308,10 +329,15 @@ pub(super) fn class(
}
let declaration =
declaration(tree, name).ok_or_else(|| unsupported(format!("class `{name}`")))?;
let native = tree.declarations();
let interfaces = declaration
.interfaces
.iter()
.map(|&interface| interface_name(tree, interface, env, aliases))
.collect::<Converted<Vec<_>>>()?;
if declaration.interface {
return Err(unsupported("interface class"));
return interface_class(tree, name, declaration, interfaces, env, aliases);
}
let native = tree.declarations();
let base = match &declaration.base {
None => None,
Some(base) => {
Expand Down Expand Up @@ -406,8 +432,13 @@ pub(super) fn 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 interface in &interfaces {
required.extend(
class(tree, interface, env, aliases)?
.methods
.iter()
.map(|method| (method.name.clone(), method.native)),
);
}
for (method_name, prototype) in required {
match methods.iter_mut().find(|method| method.name == method_name) {
Expand Down Expand Up @@ -447,7 +478,9 @@ pub(super) fn class(
name: name.to_owned(),
id: class_id(name),
base: base.map(|base| base.name.clone()),
interfaces,
is_abstract: declaration.virtual_class,
is_interface: false,
properties,
base_properties,
statics,
Expand All @@ -461,32 +494,84 @@ 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(
/// The interface class that type `interface` of an `implements` or an
/// interface class's `extends` list names (IEEE 1800-2023 8.26).
fn interface_name(
tree: &AnalysisContext<'_>,
interface: celox_sv_syntax::TypeId,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
methods: &mut Vec<(String, celox_sv_syntax::SubroutineId)>,
) -> Converted<()> {
) -> Converted<String> {
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));
if !declaration(tree, &name).is_some_and(|declaration| declaration.interface) {
return Err(unsupported(format!(
"`implements` of `{name}`, not an interface class"
)));
}
Ok(name)
}

/// Interface class `name`, whose methods are the pure virtual methods it
/// declares and those of the interface classes it extends (IEEE 1800-2023
/// 8.26). A handle of it refers to an object of a class implementing it.
fn interface_class(
tree: &AnalysisContext<'_>,
name: &str,
declaration: &celox_sv_syntax::ClassDeclaration,
interfaces: Vec<String>,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
) -> Converted<Rc<Class>> {
let native = tree.declarations();
let mut methods: Vec<Method> = Vec::new();
for interface in &interfaces {
for method in &class(tree, interface, env, aliases)?.methods {
methods.retain(|known| known.name != method.name);
methods.push(method.clone());
}
}
for &base in &declaration.interfaces {
interface_methods(tree, base, env, aliases, methods)?;
for method in &declaration.methods {
let subroutine = native.subroutine(method.subroutine);
// A method whose types cannot be resolved, such as one using a
// type parameter or a typedef of the interface class, leaves the
// interface class without a layout; it is lowered only when used.
type_binding::subroutine_formals(
subroutine,
tree,
&PackedDimensions::new(HashMap::default(), env, aliases),
)?;
return_type(tree, method.subroutine, env, aliases)?;
let method_name = instances::normalize_identifier(&subroutine.name.spelling);
methods.retain(|known| known.name != method_name);
methods.push(Method {
name: method_name,
owner: name.to_owned(),
native: method.subroutine,
qualifiers: method.qualifiers,
is_virtual: true,
});
}
Ok(())
let class = Rc::new(Class {
name: name.to_owned(),
id: class_id(name),
base: None,
interfaces,
is_abstract: true,
is_interface: true,
properties: Vec::new(),
base_properties: 0,
statics: Vec::new(),
methods,
size: 1,
});
tree.state
.classes
.borrow_mut()
.insert(name.to_owned(), class.clone());
Ok(class)
}

/// The return type of subroutine `id`, `None` for a task or `void`.
Expand Down Expand Up @@ -562,17 +647,16 @@ 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(
/// The classes whose objects a handle of class `ancestor` may refer to:
/// those deriving from it or implementing it that are not abstract (IEEE
/// 1800-2023 8.16), laid out.
fn object_classes(
tree: &AnalysisContext<'_>,
owner: &str,
method: &str,
ancestor: &str,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
) -> Converted<Vec<(String, Vec<u32>)>> {
let mut targets: Vec<(String, Vec<u32>)> = Vec::new();
) -> Converted<Vec<Rc<Class>>> {
let mut classes = Vec::new();
let mut ids = HashMap::default();
for declaration in &tree.declarations().classes {
if declaration.interface || declaration.virtual_class {
Expand All @@ -583,14 +667,31 @@ pub(super) fn dispatch_targets(
let Ok(class) = class(tree, &name, env, aliases) else {
continue;
};
if !class.derives_from(tree, owner, env, aliases) {
if !class.derives_from(tree, ancestor, 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"
)));
}
classes.push(class);
}
Ok(classes)
}

/// The overrides that dispatcher `Class::dispatcher_name(owner, method)`
/// calls: each implementation of the method with the classes, deriving from
/// or implementing `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();
for class in object_classes(tree, owner, env, aliases)? {
let Some(implementation) = class.method(method) else {
continue;
};
Expand All @@ -603,6 +704,20 @@ pub(super) fn dispatch_targets(
Ok(targets)
}

/// The run-time identifiers of the classes whose objects `$cast` may assign
/// to a variable of class `name` (IEEE 1800-2023 8.16).
pub(super) fn cast_targets(
tree: &AnalysisContext<'_>,
name: &str,
env: &HashMap<String, i128>,
aliases: &HashMap<String, Type>,
) -> Converted<Vec<u32>> {
Ok(object_classes(tree, name, env, aliases)?
.iter()
.map(|class| class.id)
.collect())
}

/// A call of object operation `op`.
pub(super) fn object_call(op: ObjectOp, args: Vec<Expr>) -> Expr {
Expr::Call {
Expand Down
29 changes: 24 additions & 5 deletions crates/celox-sv-analyzer/src/ast/dimensions/discovery.rs
Original file line number Diff line number Diff line change
Expand Up @@ -457,15 +457,36 @@ fn class_signatures(
context,
);
}
if !constructor {
if !constructor && !class.interface {
publish_signature(
classes::Class::subroutine_name(&class_name, "new"),
vec![this],
Some(Some(handle)),
Some(Some(handle.clone())),
tree,
context,
);
}
// `$cast` to a variable of the class (8.16).
let formal = |name: &str, direction| {
(
FunctionArgumentMetadata {
name: name.into(),
direction,
has_default: false,
},
handle.clone(),
)
};
publish_signature(
classes::Class::cast_name(&class_name),
vec![
formal(procedural::CAST_DESTINATION, ParamDirection::Inout),
formal(procedural::CAST_SOURCE, ParamDirection::Input),
],
Some(Some(procedural::int_type())),
tree,
context,
);
Ok(())
}
/// Publish the signatures of the methods of every class in the source.
Expand All @@ -474,9 +495,7 @@ pub(in crate::ast) fn all_class_signatures(
context: &mut PackedDimensions,
) {
for class in &tree.declarations().classes {
if !class.interface {
let _ = class_signatures(class, tree, context);
}
let _ = class_signatures(class, tree, context);
}
}
fn publish_signature(
Expand Down
Loading
Loading