xur-resolve 0.1.6

Module loading and name resolution for the Xur language
Documentation
//! Modules implemented in Rust, as name resolution sees them.
//!
//! The resolver only needs names; the runtime holds the implementations and
//! numbers them, and resolved paths refer to native items by those numbers.

/// Identifies a function implemented in Rust.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NativeFnId(pub u32);

/// Identifies a type implemented in Rust.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct NativeTypeId(pub u32);

/// Supplies the native modules: their declarations up front, and the
/// contents of lazy modules once a program looks inside them.
///
/// Providers are shared with a running program, which may import modules,
/// and so expand lazy native modules, from any thread.
pub trait NativeProvider: Send + Sync {
    fn modules(&self) -> Vec<NativeModule>;

    /// The contents of the lazy module at `path` (such as `["std", "net"]`),
    /// which is now needed. Its own submodules may be lazy in turn.
    fn expand(&self, path: &[String]) -> NativeModule;

    /// Registers built-in functions (`from_str`, `len`, `values`) for the
    /// enum `enum_def`. Returns their declarations so the resolver can add
    /// them to the enum's scope.
    fn register_enum_builtins(&self, _enum_def: super::DefId) -> Vec<NativeFnDecl> {
        Vec::new()
    }

    /// Returns the macro expander at `path`, if any. The path is a split
    /// module path like `["std", "stub", "il2cpp", "il2cpp_proxy"]` where
    /// the last segment is the macro name and the rest is its module.
    fn macro_expander(
        &self,
        _path: &[&str],
    ) -> Option<std::sync::Arc<dyn super::macros::MacroExpander>> {
        None
    }
}

impl<T: NativeProvider + ?Sized> NativeProvider for std::sync::Arc<T> {
    fn modules(&self) -> Vec<NativeModule> {
        (**self).modules()
    }

    fn expand(&self, path: &[String]) -> NativeModule {
        (**self).expand(path)
    }

    fn register_enum_builtins(&self, enum_def: super::DefId) -> Vec<NativeFnDecl> {
        (**self).register_enum_builtins(enum_def)
    }

    fn macro_expander(
        &self,
        path: &[&str],
    ) -> Option<std::sync::Arc<dyn super::macros::MacroExpander>> {
        (**self).macro_expander(path)
    }
}

/// Native modules that are all declared up front.
impl NativeProvider for Vec<NativeModule> {
    fn modules(&self) -> Vec<NativeModule> {
        self.clone()
    }

    fn expand(&self, path: &[String]) -> NativeModule {
        panic!("`{}` was not declared lazy", path.join("::"))
    }
}

/// A module of native items, such as `std` or one of its submodules.
#[derive(Debug, Clone, Default)]
pub struct NativeModule {
    pub name: String,
    /// Whether the module's contents are only provided when first needed,
    /// by [`NativeProvider::expand`]. A lazy module is declared empty.
    pub lazy: bool,
    pub functions: Vec<NativeFnDecl>,
    pub types: Vec<NativeTypeDecl>,
    pub modules: Vec<NativeModule>,
}

#[derive(Debug, Clone)]
pub struct NativeFnDecl {
    pub name: String,
    pub id: NativeFnId,
    pub param_names: Vec<String>,
}

/// A native type and its methods and associated functions, which are in
/// its scope as `Type::name`.
#[derive(Debug, Clone)]
pub struct NativeTypeDecl {
    pub name: String,
    pub id: NativeTypeId,
    pub functions: Vec<NativeFnDecl>,
    /// For an enum registered from Rust, its variants, which are in its
    /// scope as `Type::Variant` and build and match its values.
    pub variants: Vec<NativeVariantDecl>,
}

/// A variant of a native enum.
#[derive(Debug, Clone)]
pub struct NativeVariantDecl {
    pub name: String,
    pub kind: NativeVariantKind,
    /// For `#[repr(inttype)]` enums registered from Rust, the integer
    /// discriminant. When set, the variant lowers to a constant integer.
    pub repr_value: Option<i64>,
}

/// The shape of a native variant's fields, which decides how it is
/// written: `E::A`, `E::B(x)` or `E::C { x }`.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NativeVariantKind {
    Unit,
    Tuple,
    Named,
}