rils_builtins 0.4.0

Shared built-in API metadata for the Rils toolchain
Documentation
//! Static descriptions of APIs shipped as part of Rils.
//!
//! Nothing in this crate performs I/O or executes script code. Tooling and
//! runtimes share these declarations while choosing their own implementation
//! strategy for runtime, intrinsic and host-backed items.

pub use rils_syntax::IntegerType;

/// A recursive type expression independent of the frontend's concrete `Type`.
#[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 {
    /// Names from the language declaration, excluding the receiver.
    pub parameter_names: &'static [&'static str],
    pub parameters: &'static [TypePattern],
    /// Required callable capabilities, indexed in `parameters` (without receiver).
    /// An empty list imposes no invocation contract beyond the type signature.
    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)
}

/// Resolves a numeric operation path, including the target type of an
/// associated integer function.
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))
}