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    /// The kind of the registered attribute at `path` (last segment is the
44    /// name), if any. Used to know which attributes to parse and where they
45    /// may appear.
46    fn attribute_kind(&self, _path: &[&str]) -> Option<crate::attributes::AttributeKind> {
47        None
48    }
49
50    /// Parses the arguments of the registered attribute at `path` into its
51    /// registered type, returned type-erased. `None` means no such attribute.
52    fn parse_attribute(
53        &self,
54        _path: &[&str],
55        _input: &crate::attributes::AttrInput,
56    ) -> Option<Result<std::sync::Arc<dyn std::any::Any + Send + Sync>, crate::attributes::AttrError>>
57    {
58        None
59    }
60}
61
62impl<T: NativeProvider + ?Sized> NativeProvider for std::sync::Arc<T> {
63    fn modules(&self) -> Vec<NativeModule> {
64        (**self).modules()
65    }
66
67    fn expand(&self, path: &[String]) -> NativeModule {
68        (**self).expand(path)
69    }
70
71    fn register_enum_builtins(&self, enum_def: super::DefId) -> Vec<NativeFnDecl> {
72        (**self).register_enum_builtins(enum_def)
73    }
74
75    fn macro_expander(
76        &self,
77        path: &[&str],
78    ) -> Option<std::sync::Arc<dyn super::macros::MacroExpander>> {
79        (**self).macro_expander(path)
80    }
81
82    fn attribute_kind(&self, path: &[&str]) -> Option<crate::attributes::AttributeKind> {
83        (**self).attribute_kind(path)
84    }
85
86    fn parse_attribute(
87        &self,
88        path: &[&str],
89        input: &crate::attributes::AttrInput,
90    ) -> Option<Result<std::sync::Arc<dyn std::any::Any + Send + Sync>, crate::attributes::AttrError>>
91    {
92        (**self).parse_attribute(path, input)
93    }
94}
95
96/// Native modules that are all declared up front.
97impl NativeProvider for Vec<NativeModule> {
98    fn modules(&self) -> Vec<NativeModule> {
99        self.clone()
100    }
101
102    fn expand(&self, path: &[String]) -> NativeModule {
103        panic!("`{}` was not declared lazy", path.join("::"))
104    }
105}
106
107/// A module of native items, such as `std` or one of its submodules.
108#[derive(Debug, Clone, Default)]
109pub struct NativeModule {
110    pub name: String,
111    /// Whether the module's contents are only provided when first needed,
112    /// by [`NativeProvider::expand`]. A lazy module is declared empty.
113    pub lazy: bool,
114    pub functions: Vec<NativeFnDecl>,
115    pub types: Vec<NativeTypeDecl>,
116    pub modules: Vec<NativeModule>,
117}
118
119#[derive(Debug, Clone)]
120pub struct NativeFnDecl {
121    pub name: String,
122    pub id: NativeFnId,
123    pub param_names: Vec<String>,
124}
125
126/// A native type and its methods and associated functions, which are in
127/// its scope as `Type::name`.
128#[derive(Debug, Clone)]
129pub struct NativeTypeDecl {
130    pub name: String,
131    pub id: NativeTypeId,
132    pub functions: Vec<NativeFnDecl>,
133    /// For an enum registered from Rust, its variants, which are in its
134    /// scope as `Type::Variant` and build and match its values.
135    pub variants: Vec<NativeVariantDecl>,
136}
137
138/// A variant of a native enum.
139#[derive(Debug, Clone)]
140pub struct NativeVariantDecl {
141    pub name: String,
142    pub kind: NativeVariantKind,
143    /// For `#[repr(inttype)]` enums registered from Rust, the integer
144    /// discriminant. When set, the variant lowers to a constant integer.
145    pub repr_value: Option<i64>,
146}
147
148/// The shape of a native variant's fields, which decides how it is
149/// written: `E::A`, `E::B(x)` or `E::C { x }`.
150#[derive(Debug, Clone, Copy, PartialEq, Eq)]
151pub enum NativeVariantKind {
152    Unit,
153    Tuple,
154    Named,
155}