Skip to main content

rust2go_macro/
lib.rs

1// Copyright 2024 ihciah. All Rights Reserved.
2
3use proc_macro::TokenStream;
4use quote::{format_ident, quote};
5use rust2go_common::{g2r::G2RTraitRepr, r2g::R2GTraitRepr, sbail};
6use syn::{parse::Parser, parse_macro_input, DeriveInput, Ident};
7
8#[proc_macro_derive(R2G)]
9pub fn r2g_derive(input: TokenStream) -> TokenStream {
10    let input = parse_macro_input!(input as DeriveInput);
11    // Skip derive when the type has generics.
12    if !input.generics.params.is_empty() {
13        return TokenStream::default();
14    }
15    // Skip derive when the type is not struct.
16    let data = match input.data {
17        syn::Data::Struct(d) => d,
18        _ => return TokenStream::default(),
19    };
20    let attrs = input.attrs;
21    let type_name = input.ident;
22    let type_name_str = type_name.to_string();
23
24    let ref_type_name = Ident::new(&format!("{type_name_str}Ref"), type_name.span());
25    let mut ref_fields = Vec::with_capacity(data.fields.len());
26    for field in data.fields.iter() {
27        let name = field.ident.as_ref().unwrap();
28        let ty = &field.ty;
29        let syn::Type::Path(path) = ty else {
30            return TokenStream::default();
31        };
32        let Some(first_seg) = path.path.segments.first() else {
33            return TokenStream::default();
34        };
35        match first_seg.ident.to_string().as_str() {
36            "Vec" | "Option" => {
37                ref_fields.push(quote! {#name: ::rust2go::ListRef});
38            }
39            "String" => {
40                ref_fields.push(quote! {#name: ::rust2go::StringRef});
41            }
42            "i8" | "i16" | "i32" | "i64" | "isize" | "u8" | "u16" | "u32" | "u64" | "usize"
43            | "f32" | "f64" | "bool" | "char" => {
44                ref_fields.push(quote! {#name: #ty});
45            }
46            ty => {
47                let ref_type = format_ident!("{ty}Ref");
48                ref_fields.push(quote! {#name: #ref_type});
49            }
50        }
51    }
52
53    let mut owned_names = Vec::with_capacity(data.fields.len());
54    let mut owned_types = Vec::with_capacity(data.fields.len());
55    for field in data.fields.iter() {
56        owned_names.push(field.ident.clone().unwrap());
57        owned_types.push(field.ty.clone());
58    }
59
60    let expanded = quote! {
61        #(#attrs)*
62        #[repr(C)]
63        pub struct #ref_type_name {
64            #(#ref_fields),*
65        }
66
67        impl ::rust2go::ToRef for #type_name {
68            const MEM_TYPE: ::rust2go::MemType = ::rust2go::max_mem_type!(#(#owned_types),*);
69            type Ref = #ref_type_name;
70
71            fn to_size(&self, acc: &mut usize) {
72                if matches!(Self::MEM_TYPE, ::rust2go::MemType::Complex) {
73                    #(self.#owned_names.to_size(acc);)*
74                }
75            }
76
77            fn to_ref(&self, buffer: &mut ::rust2go::Writer) -> Self::Ref {
78                #ref_type_name {
79                    #(#owned_names: ::rust2go::ToRef::to_ref(&self.#owned_names, buffer),)*
80                }
81            }
82        }
83
84        impl ::rust2go::FromRef for #type_name {
85            type Ref = #ref_type_name;
86
87            fn from_ref(ref_: &Self::Ref) -> Self {
88                Self {
89                    #(#owned_names: ::rust2go::FromRef::from_ref(&ref_.#owned_names),)*
90                }
91            }
92        }
93    };
94    TokenStream::from(expanded)
95}
96
97fn parse_attrs(attrs: TokenStream) -> (Option<syn::Path>, Option<usize>) {
98    let mut binding_path = None;
99    let mut queue_size = None;
100
101    type AttributeArgs = syn::punctuated::Punctuated<syn::Meta, syn::Token![,]>;
102    if let Ok(attrs) = AttributeArgs::parse_terminated.parse(attrs) {
103        for attr in attrs {
104            match attr {
105                syn::Meta::NameValue(nv) => {
106                    if nv.path.is_ident("binding") {
107                        binding_path = Some(nv.path);
108                    } else if nv.path.is_ident("queue_size") {
109                        if let syn::Expr::Lit(syn::ExprLit {
110                            lit: syn::Lit::Int(litint),
111                            ..
112                        }) = nv.value
113                        {
114                            queue_size = Some(litint.base10_parse::<usize>().unwrap());
115                        }
116                    }
117                }
118                syn::Meta::Path(p) => {
119                    binding_path = Some(p);
120                }
121                _ => {}
122            }
123        }
124    }
125    (binding_path, queue_size)
126}
127
128#[proc_macro_attribute]
129pub fn r2g(attrs: TokenStream, item: TokenStream) -> TokenStream {
130    let (binding_path, queue_size) = parse_attrs(attrs);
131    syn::parse::<syn::ItemTrait>(item)
132        .and_then(|trat| r2g_trait(binding_path, queue_size, trat))
133        .unwrap_or_else(|e| TokenStream::from(e.to_compile_error()))
134}
135
136/// Mark only
137#[proc_macro_attribute]
138pub fn r2g_struct_tag(_attrs: TokenStream, item: TokenStream) -> TokenStream {
139    item
140}
141
142#[proc_macro_attribute]
143pub fn g2r(_attrs: TokenStream, item: TokenStream) -> TokenStream {
144    syn::parse::<syn::ItemTrait>(item)
145        .and_then(g2r_trait)
146        .unwrap_or_else(|e| TokenStream::from(e.to_compile_error()))
147}
148
149fn g2r_trait(mut trat: syn::ItemTrait) -> syn::Result<TokenStream> {
150    let trat_repr = G2RTraitRepr::try_from(&trat)?;
151
152    for trat_fn in trat.items.iter_mut() {
153        match trat_fn {
154            syn::TraitItem::Fn(f) => {
155                // remove attributes of all functions
156                f.attrs.clear();
157            }
158            _ => sbail!("only fn is supported"),
159        }
160    }
161
162    let mut out = quote! {#trat};
163    out.extend(trat_repr.generate_rs()?);
164    Ok(out.into())
165}
166
167fn r2g_trait(
168    binding_path: Option<syn::Path>,
169    queue_size: Option<usize>,
170    mut trat: syn::ItemTrait,
171) -> syn::Result<TokenStream> {
172    let trat_repr = R2GTraitRepr::try_from(&trat)?;
173
174    for (fn_repr, trat_fn) in trat_repr.fns().iter().zip(trat.items.iter_mut()) {
175        match trat_fn {
176            syn::TraitItem::Fn(f) => {
177                // remove attributes of all functions
178                f.attrs.clear();
179
180                // for shm based oneway call, add unsafe
181                if fn_repr.ret().is_none() && !fn_repr.is_async() && fn_repr.mem_call_id().is_some()
182                {
183                    f.sig.unsafety = Some(syn::token::Unsafe::default());
184                }
185
186                // convert async fn return impl future
187                if fn_repr.is_async() {
188                    let orig = match fn_repr.ret() {
189                        None => quote! { () },
190                        Some(ret) => quote! { #ret },
191                    };
192                    let auto_t = match (fn_repr.ret_send(), fn_repr.ret_static()) {
193                        (true, true) => quote!( + Send + Sync + 'static),
194                        (true, false) => quote!( + Send + Sync),
195                        (false, true) => quote!( + 'static),
196                        (false, false) => quote!(),
197                    };
198                    f.sig.asyncness = None;
199                    if fn_repr.drop_safe_ret_params() {
200                        // for all functions with #[drop_safe_ret], change the return type.
201                        let tys = fn_repr.params().iter().map(|p| p.ty());
202                        f.sig.output = syn::parse_quote! { -> impl ::std::future::Future<Output = (#orig, (#(#tys,)*))> #auto_t };
203                    } else {
204                        f.sig.output = syn::parse_quote! { -> impl ::std::future::Future<Output = #orig> #auto_t };
205                    }
206
207                    // for all functions with safe=false, add unsafe
208                    if !fn_repr.is_safe() {
209                        f.sig.unsafety = Some(syn::token::Unsafe::default());
210                    }
211                }
212            }
213            _ => sbail!("only fn is supported"),
214        }
215    }
216
217    let mut out = quote! {#trat};
218    out.extend(trat_repr.generate_rs(binding_path.as_ref(), queue_size)?);
219    Ok(out.into())
220}