pub use rils_syntax::IntegerType;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TypePattern {
SelfType,
Generic(&'static str),
BoundGeneric {
name: &'static str,
bounds: &'static [TypePattern],
},
AnyInteger,
Unknown,
Unit,
Bool,
Char,
String,
F32,
F64,
U32,
U8,
Usize,
Named {
path: &'static str,
arguments: &'static [TypePattern],
},
Option(&'static TypePattern),
Result {
ok: &'static TypePattern,
error: &'static TypePattern,
},
Tuple(&'static [TypePattern]),
Array {
element: &'static TypePattern,
length: usize,
},
ArrayParameter {
element: &'static TypePattern,
length: &'static str,
},
Slice(&'static TypePattern),
Function {
parameters: &'static [TypePattern],
result: &'static TypePattern,
},
Reference {
mutable: bool,
inner: &'static TypePattern,
},
Associated {
base: &'static TypePattern,
trait_name: Option<&'static str>,
trait_arguments: &'static [TypePattern],
name: &'static str,
arguments: &'static [TypePattern],
},
}
#[derive(Clone, Copy, Debug)]
pub struct BuiltinSignature {
pub parameter_names: &'static [&'static str],
pub parameters: &'static [TypePattern],
pub parameter_capabilities: &'static [(usize, crate::CallableTraitKind)],
pub result: TypePattern,
pub variadic: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum IntrinsicKind {
Method,
AssociatedFunction,
}
#[derive(Clone, Copy, Debug)]
pub struct IntrinsicDeclaration {
pub symbol: &'static str,
pub name: &'static str,
pub kind: IntrinsicKind,
pub signature: BuiltinSignature,
pub documentation: &'static str,
}
pub fn integer_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
crate::INTEGER_INTRINSICS
.iter()
.find(|item| item.kind == IntrinsicKind::Method && item.name == name)
}
pub fn integer_associated_function(name: &str) -> Option<&'static IntrinsicDeclaration> {
crate::INTEGER_INTRINSICS
.iter()
.find(|item| item.kind == IntrinsicKind::AssociatedFunction && item.name == name)
}
pub fn float_method(name: &str) -> Option<&'static IntrinsicDeclaration> {
crate::FLOAT_INTRINSICS
.iter()
.find(|item| item.name == name)
}
pub fn intrinsic(symbol: &str) -> Option<&'static IntrinsicDeclaration> {
crate::INTEGER_INTRINSICS
.iter()
.chain(crate::FLOAT_INTRINSICS)
.find(|item| item.symbol == symbol)
}
pub fn intrinsic_by_symbol(
symbol: &str,
) -> Option<(&'static IntrinsicDeclaration, Option<IntegerType>)> {
if let Some(rest) = symbol.strip_prefix("core::integer::")
&& let Some((target, method)) = rest.split_once("::")
&& let Some(target) = IntegerType::from_name(target)
{
let declaration = integer_associated_function(method)?;
return Some((declaration, Some(target)));
}
crate::INTEGER_INTRINSICS
.iter()
.chain(crate::FLOAT_INTRINSICS)
.find(|declaration| declaration.symbol == symbol)
.map(|declaration| (declaration, None))
}