use std::collections::{HashMap, HashSet};
use crate::Type;
mod declarations;
pub(crate) use declarations::{collect, validate};
#[derive(Clone, Debug)]
struct Definition {
parameters: Vec<String>,
fields: Vec<Type>,
declared: bool,
instances: HashSet<Type>,
}
#[derive(Clone, Debug, Default)]
pub struct CopyTypes {
definitions: HashMap<String, Definition>,
}
impl CopyTypes {
pub(crate) fn extend(&mut self, next: Self) {
self.definitions.extend(next.definitions);
}
pub fn define(&mut self, name: String, parameters: Vec<String>, fields: Vec<Type>) {
self.definitions.insert(
name,
Definition {
parameters,
fields,
declared: false,
instances: HashSet::new(),
},
);
}
pub fn implement(&mut self, name: &str) -> bool {
let Some(definition) = self.definitions.get_mut(name) else {
return false;
};
definition.declared = true;
true
}
pub fn implement_instance(&mut self, ty: &Type) -> bool {
let Type::Named { name, arguments } = ty else {
return false;
};
let Some(definition) = self.definitions.get_mut(name) else {
return false;
};
if definition.parameters.len() != arguments.len() || !ty.is_concrete_type() {
return false;
}
definition.instances.insert(ty.clone());
true
}
pub(crate) fn is_unconditional_target(&self, ty: &Type) -> bool {
let Type::Named { name, arguments } = ty else {
return false;
};
let Some(definition) = self.definitions.get(name) else {
return false;
};
let mut variables = HashSet::new();
definition.parameters.len() == arguments.len()
&& arguments.iter().all(|argument| match argument {
Type::Variable(name) | Type::BoundVariable { name, .. } => variables.insert(name),
_ => false,
})
}
pub fn is_copy(&self, ty: &Type, host_types: &HashSet<String>) -> bool {
self.check(ty, host_types, &mut HashSet::new(), false)
}
pub fn fields_are_copy(&self, ty: &Type, host_types: &HashSet<String>) -> bool {
self.check(ty, host_types, &mut HashSet::new(), true)
}
pub fn fields_satisfy(&self, ty: &Type, mut is_copy: impl FnMut(&Type) -> bool) -> bool {
let Type::Named { name, arguments } = ty else {
return false;
};
let Some(definition) = self.definitions.get(name) else {
return false;
};
if definition.parameters.len() != arguments.len() {
return false;
}
let bindings = definition
.parameters
.iter()
.cloned()
.zip(arguments.iter().cloned())
.collect();
definition
.fields
.iter()
.all(|field| is_copy(&field.substitute(&bindings)))
}
fn check(
&self,
ty: &Type,
hosts: &HashSet<String>,
visiting: &mut HashSet<Type>,
root: bool,
) -> bool {
match ty {
Type::Unit
| Type::Bool
| Type::Char
| Type::Reference { .. }
| Type::Function { .. }
| Type::IntegerVariable(_)
| Type::IntegerInference(_)
| Type::FloatVariable(_)
| Type::FloatInference(_) => true,
Type::Integer(_)
| Type::Float(_)
| Type::String
| Type::Option(_)
| Type::Result(..) => self.native_is_copy(ty, hosts, visiting),
Type::Tuple(items) => items
.iter()
.all(|item| self.check(item, hosts, visiting, false)),
Type::Array { element, .. } | Type::ArrayParameter { element, .. } => {
self.check(element, hosts, visiting, false)
}
Type::Named { name, arguments } => {
if !self.definitions.contains_key(name)
&& arguments.is_empty()
&& (name == "HostHandle" || hosts.contains(name))
{
return true;
}
let Some(definition) = self.definitions.get(name) else {
return self.native_is_copy(ty, hosts, visiting);
};
if (!root && !definition.declared && !definition.instances.contains(ty))
|| definition.parameters.len() != arguments.len()
|| visiting.len() >= 64
|| !visiting.insert(ty.clone())
{
return false;
}
let substitutions = definition
.parameters
.iter()
.cloned()
.zip(arguments.iter().cloned())
.collect();
let result = definition.fields.iter().all(|field| {
self.check(&field.substitute(&substitutions), hosts, visiting, false)
});
visiting.remove(ty);
result
}
_ => false,
}
}
fn native_is_copy(
&self,
ty: &Type,
hosts: &HashSet<String>,
visiting: &mut HashSet<Type>,
) -> bool {
crate::standard_library::builtin_implements_with(ty, "Copy", |argument, bound| {
bound == "Copy" && self.check(argument, hosts, visiting, false)
})
}
}