Skip to main content

xur_resolve/
native.rs

1//! Modules implemented in Rust, as name resolution sees them.
2//!
3//! The resolver only needs names; the runtime holds the implementations and
4//! numbers them, and resolved paths refer to native items by those numbers.
5
6/// Identifies a function implemented in Rust.
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
8pub struct NativeFnId(pub u32);
9
10/// Identifies a type implemented in Rust.
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
12pub struct NativeTypeId(pub u32);
13
14/// Supplies the native modules: their declarations up front, and the
15/// contents of lazy modules once a program looks inside them.
16///
17/// Providers are shared with a running program, which may import modules,
18/// and so expand lazy native modules, from any thread.
19pub trait NativeProvider: Send + Sync {
20    fn modules(&self) -> Vec<NativeModule>;
21
22    /// The contents of the lazy module at `path` (such as `["std", "net"]`),
23    /// which is now needed. Its own submodules may be lazy in turn.
24    fn expand(&self, path: &[String]) -> NativeModule;
25
26    /// Registers built-in functions (`from_str`, `len`, `values`) for the
27    /// enum `enum_def`. Returns their declarations so the resolver can add
28    /// them to the enum's scope.
29    fn register_enum_builtins(&self, _enum_def: super::DefId) -> Vec<NativeFnDecl> {
30        Vec::new()
31    }
32
33    /// Returns the macro expander at `path`, if any. The path is a split
34    /// module path like `["std", "stub", "il2cpp", "il2cpp_proxy"]` where
35    /// the last segment is the macro name and the rest is its module.
36    fn macro_expander(
37        &self,
38        _path: &[&str],
39    ) -> Option<std::sync::Arc<dyn super::macros::MacroExpander>> {
40        None
41    }
42}
43
44impl<T: NativeProvider + ?Sized> NativeProvider for std::sync::Arc<T> {
45    fn modules(&self) -> Vec<NativeModule> {
46        (**self).modules()
47    }
48
49    fn expand(&self, path: &[String]) -> NativeModule {
50        (**self).expand(path)
51    }
52
53    fn register_enum_builtins(&self, enum_def: super::DefId) -> Vec<NativeFnDecl> {
54        (**self).register_enum_builtins(enum_def)
55    }
56
57    fn macro_expander(
58        &self,
59        path: &[&str],
60    ) -> Option<std::sync::Arc<dyn super::macros::MacroExpander>> {
61        (**self).macro_expander(path)
62    }
63}
64
65/// Native modules that are all declared up front.
66impl NativeProvider for Vec<NativeModule> {
67    fn modules(&self) -> Vec<NativeModule> {
68        self.clone()
69    }
70
71    fn expand(&self, path: &[String]) -> NativeModule {
72        panic!("`{}` was not declared lazy", path.join("::"))
73    }
74}
75
76/// A module of native items, such as `std` or one of its submodules.
77#[derive(Debug, Clone, Default)]
78pub struct NativeModule {
79    pub name: String,
80    /// Whether the module's contents are only provided when first needed,
81    /// by [`NativeProvider::expand`]. A lazy module is declared empty.
82    pub lazy: bool,
83    pub functions: Vec<NativeFnDecl>,
84    pub types: Vec<NativeTypeDecl>,
85    pub modules: Vec<NativeModule>,
86}
87
88#[derive(Debug, Clone)]
89pub struct NativeFnDecl {
90    pub name: String,
91    pub id: NativeFnId,
92    pub param_names: Vec<String>,
93}
94
95/// A native type and its methods and associated functions, which are in
96/// its scope as `Type::name`.
97#[derive(Debug, Clone)]
98pub struct NativeTypeDecl {
99    pub name: String,
100    pub id: NativeTypeId,
101    pub functions: Vec<NativeFnDecl>,
102    /// For an enum registered from Rust, its variants, which are in its
103    /// scope as `Type::Variant` and build and match its values.
104    pub variants: Vec<NativeVariantDecl>,
105}
106
107/// A variant of a native enum.
108#[derive(Debug, Clone)]
109pub struct NativeVariantDecl {
110    pub name: String,
111    pub kind: NativeVariantKind,
112    /// For `#[repr(inttype)]` enums registered from Rust, the integer
113    /// discriminant. When set, the variant lowers to a constant integer.
114    pub repr_value: Option<i64>,
115}
116
117/// The shape of a native variant's fields, which decides how it is
118/// written: `E::A`, `E::B(x)` or `E::C { x }`.
119#[derive(Debug, Clone, Copy, PartialEq, Eq)]
120pub enum NativeVariantKind {
121    Unit,
122    Tuple,
123    Named,
124}