use hir_lang::{Prim, Symbol};
use crate::mono::MonoPolicy;
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Discipline {
Dynamic,
Gradual,
StaticExplicit,
StaticInferred,
Hm,
}
impl Discipline {
#[must_use]
pub const fn is_implemented(self) -> bool {
matches!(
self,
Self::Dynamic | Self::StaticExplicit | Self::StaticInferred
)
}
#[must_use]
pub const fn name(self) -> &'static str {
match self {
Self::Dynamic => "dynamic",
Self::Gradual => "gradual",
Self::StaticExplicit => "static-explicit",
Self::StaticInferred => "static-inferred",
Self::Hm => "hm",
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum RecordTyping {
Nominal,
Structural,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Polymorphism {
Traits,
Interfaces,
Typeclasses,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum NullLet {
NullType,
Nullable,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TryHook {
#[default]
Template,
Intrinsic {
name: Symbol,
convert: Option<Symbol>,
},
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct Profile {
pub discipline: Discipline,
pub subtyping: bool,
pub records: RecordTyping,
pub numeric_widening: bool,
pub null_safety: bool,
pub exhaustive_match: bool,
pub polymorphism: Polymorphism,
pub default_int: Prim,
pub default_float: Prim,
pub string_add: bool,
pub certain_errors: bool,
pub method_autoref: bool,
pub mono: MonoPolicy,
pub null_let: NullLet,
pub try_hook: TryHook,
}
impl Default for Profile {
fn default() -> Self {
Self::static_explicit()
}
}
impl Profile {
#[must_use]
pub const fn dynamic() -> Self {
Self {
discipline: Discipline::Dynamic,
subtyping: true,
records: RecordTyping::Structural,
numeric_widening: true,
null_safety: false,
exhaustive_match: false,
polymorphism: Polymorphism::Interfaces,
default_int: Prim::I64,
default_float: Prim::F64,
string_add: true,
certain_errors: true,
method_autoref: true,
mono: MonoPolicy::Shared,
null_let: NullLet::NullType,
try_hook: TryHook::Template,
}
}
#[must_use]
pub const fn gradual() -> Self {
Self {
discipline: Discipline::Gradual,
null_safety: true,
..Self::dynamic()
}
}
#[must_use]
pub const fn static_explicit() -> Self {
Self {
discipline: Discipline::StaticExplicit,
subtyping: false,
records: RecordTyping::Nominal,
numeric_widening: false,
null_safety: true,
exhaustive_match: true,
polymorphism: Polymorphism::Interfaces,
default_int: Prim::I64,
default_float: Prim::F64,
string_add: false,
certain_errors: true,
method_autoref: true,
mono: MonoPolicy::Copy,
null_let: NullLet::NullType,
try_hook: TryHook::Template,
}
}
#[must_use]
pub const fn static_inferred() -> Self {
Self {
discipline: Discipline::StaticInferred,
polymorphism: Polymorphism::Traits,
null_let: NullLet::Nullable,
..Self::static_explicit()
}
}
#[must_use]
pub const fn hm() -> Self {
Self {
discipline: Discipline::Hm,
polymorphism: Polymorphism::Typeclasses,
..Self::static_explicit()
}
}
#[must_use]
pub const fn with_subtyping(mut self, on: bool) -> Self {
self.subtyping = on;
self
}
#[must_use]
pub const fn with_records(mut self, records: RecordTyping) -> Self {
self.records = records;
self
}
#[must_use]
pub const fn with_numeric_widening(mut self, on: bool) -> Self {
self.numeric_widening = on;
self
}
#[must_use]
pub const fn with_null_safety(mut self, on: bool) -> Self {
self.null_safety = on;
self
}
#[must_use]
pub const fn with_exhaustive_match(mut self, on: bool) -> Self {
self.exhaustive_match = on;
self
}
#[must_use]
pub const fn with_polymorphism(mut self, polymorphism: Polymorphism) -> Self {
self.polymorphism = polymorphism;
self
}
#[must_use]
pub const fn with_literal_defaults(mut self, int: Prim, float: Prim) -> Self {
self.default_int = int;
self.default_float = float;
self
}
#[must_use]
pub const fn with_string_add(mut self, on: bool) -> Self {
self.string_add = on;
self
}
#[must_use]
pub const fn with_certain_errors(mut self, on: bool) -> Self {
self.certain_errors = on;
self
}
#[must_use]
pub const fn with_method_autoref(mut self, on: bool) -> Self {
self.method_autoref = on;
self
}
#[must_use]
pub const fn with_mono(mut self, mono: MonoPolicy) -> Self {
self.mono = mono;
self
}
#[must_use]
pub const fn with_null_let(mut self, null_let: NullLet) -> Self {
self.null_let = null_let;
self
}
#[must_use]
pub const fn with_try_hook(mut self, hook: TryHook) -> Self {
self.try_hook = hook;
self
}
pub(crate) const fn is_static(&self) -> bool {
!matches!(self.discipline, Discipline::Dynamic)
}
pub(crate) const fn infers_closures(&self) -> bool {
matches!(self.discipline, Discipline::StaticInferred | Discipline::Hm)
}
}