summaryrefslogtreecommitdiffstats
path: root/third_party/rust/uniffi_macros/src/export.rs
blob: 22d469eec60ea74093eb37674183f38b8c3fc7f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::collections::BTreeMap;

use proc_macro2::{Ident, TokenStream};
use quote::{format_ident, quote, quote_spanned};
use uniffi_meta::{checksum, FnMetadata, MethodMetadata, Type};

pub(crate) mod metadata;
mod scaffolding;

pub use self::metadata::gen_metadata;
use self::{
    metadata::convert::{convert_type, try_split_result},
    scaffolding::{gen_fn_scaffolding, gen_method_scaffolding},
};
use crate::util::{assert_type_eq, create_metadata_static_var};

// TODO(jplatte): Ensure no generics, no async, …
// TODO(jplatte): Aggregate errors instead of short-circuiting, wherever possible

pub enum ExportItem {
    Function {
        sig: Signature,
        metadata: FnMetadata,
    },
    Impl {
        self_ident: Ident,
        methods: Vec<syn::Result<Method>>,
    },
}

pub struct Method {
    sig: Signature,
    metadata: MethodMetadata,
}

pub struct Signature {
    ident: Ident,
    inputs: Vec<syn::FnArg>,
    output: Option<FunctionReturn>,
}

impl Signature {
    fn new(item: syn::Signature) -> syn::Result<Self> {
        let output = match item.output {
            syn::ReturnType::Default => None,
            syn::ReturnType::Type(_, ty) => Some(FunctionReturn::new(ty)?),
        };

        Ok(Self {
            ident: item.ident,
            inputs: item.inputs.into_iter().collect(),
            output,
        })
    }
}

pub struct FunctionReturn {
    ty: Box<syn::Type>,
    throws: Option<Ident>,
}

impl FunctionReturn {
    fn new(ty: Box<syn::Type>) -> syn::Result<Self> {
        Ok(match try_split_result(&ty)? {
            Some((ok_type, throws)) => FunctionReturn {
                ty: Box::new(ok_type.to_owned()),
                throws: Some(throws),
            },
            None => FunctionReturn { ty, throws: None },
        })
    }
}

pub fn expand_export(metadata: ExportItem, mod_path: &[String]) -> TokenStream {
    match metadata {
        ExportItem::Function { sig, metadata } => {
            let checksum = checksum(&metadata);
            let scaffolding = gen_fn_scaffolding(&sig, mod_path, checksum);
            let type_assertions = fn_type_assertions(&sig);
            let meta_static_var = create_metadata_static_var(&sig.ident, metadata.into());

            quote! {
                #scaffolding
                #type_assertions
                #meta_static_var
            }
        }
        ExportItem::Impl {
            methods,
            self_ident,
        } => {
            let method_tokens: TokenStream = methods
                .into_iter()
                .map(|res| {
                    res.map_or_else(
                        syn::Error::into_compile_error,
                        |Method { sig, metadata }| {
                            let checksum = checksum(&metadata);
                            let scaffolding =
                                gen_method_scaffolding(&sig, mod_path, checksum, &self_ident);
                            let type_assertions = fn_type_assertions(&sig);
                            let meta_static_var = create_metadata_static_var(
                                &format_ident!("{}_{}", metadata.self_name, sig.ident),
                                metadata.into(),
                            );

                            quote! {
                                #scaffolding
                                #type_assertions
                                #meta_static_var
                            }
                        },
                    )
                })
                .collect();

            quote_spanned! {self_ident.span()=>
                ::uniffi::deps::static_assertions::assert_type_eq_all!(
                    #self_ident,
                    crate::uniffi_types::#self_ident
                );

                #method_tokens
            }
        }
    }
}

fn fn_type_assertions(sig: &Signature) -> TokenStream {
    // Convert uniffi_meta::Type back to a Rust type
    fn convert_type_back(ty: &Type) -> TokenStream {
        match &ty {
            Type::U8 => quote! { ::std::primitive::u8 },
            Type::U16 => quote! { ::std::primitive::u16 },
            Type::U32 => quote! { ::std::primitive::u32 },
            Type::U64 => quote! { ::std::primitive::u64 },
            Type::I8 => quote! { ::std::primitive::i8 },
            Type::I16 => quote! { ::std::primitive::i16 },
            Type::I32 => quote! { ::std::primitive::i32 },
            Type::I64 => quote! { ::std::primitive::i64 },
            Type::F32 => quote! { ::std::primitive::f32 },
            Type::F64 => quote! { ::std::primitive::f64 },
            Type::Bool => quote! { ::std::primitive::bool },
            Type::String => quote! { ::std::string::String },
            Type::Option { inner_type } => {
                let inner = convert_type_back(inner_type);
                quote! { ::std::option::Option<#inner> }
            }
            Type::Vec { inner_type } => {
                let inner = convert_type_back(inner_type);
                quote! { ::std::vec::Vec<#inner> }
            }
            Type::HashMap {
                key_type,
                value_type,
            } => {
                let key = convert_type_back(key_type);
                let value = convert_type_back(value_type);
                quote! { ::std::collections::HashMap<#key, #value> }
            }
            Type::ArcObject { object_name } => {
                let object_ident = format_ident!("{object_name}");
                quote! { ::std::sync::Arc<crate::uniffi_types::#object_ident> }
            }
            Type::Unresolved { name } => {
                let ident = format_ident!("{name}");
                quote! { crate::uniffi_types::#ident }
            }
        }
    }

    let input_types = sig.inputs.iter().filter_map(|input| match input {
        syn::FnArg::Receiver(_) => None,
        syn::FnArg::Typed(pat_ty) => match &*pat_ty.pat {
            // Self type is asserted separately for impl blocks
            syn::Pat::Ident(i) if i.ident == "self" => None,
            _ => Some(&pat_ty.ty),
        },
    });
    let output_type = sig.output.as_ref().map(|s| &s.ty);

    let type_assertions: BTreeMap<_, _> = input_types
        .chain(output_type)
        .filter_map(|ty| {
            convert_type(ty).ok().map(|meta_ty| {
                let expected_ty = convert_type_back(&meta_ty);
                let assert = assert_type_eq(ty, expected_ty);
                (meta_ty, assert)
            })
        })
        .collect();
    let input_output_type_assertions: TokenStream = type_assertions.into_values().collect();

    let throws_type_assertion = sig.output.as_ref().and_then(|s| {
        let ident = s.throws.as_ref()?;
        Some(assert_type_eq(
            ident,
            quote! { crate::uniffi_types::#ident },
        ))
    });

    quote! {
        #input_output_type_assertions
        #throws_type_assertion
    }
}