Debian Codegraph @

third_party/rust/uniffi_macros/src/export/item.rs

raw
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5 use crate::fnsig::FnSignature;
6 use proc_macro::TokenStream;
7 use proc_macro2::{Ident, Span};
8 use quote::ToTokens;
9
10 use super::attributes::{
11 ExportFnArgs, ExportImplArgs, ExportStructArgs, ExportTraitArgs, ExportedImplFnAttributes,
12 };
13 use crate::util::extract_docstring;
14 use uniffi_meta::UniffiTraitDiscriminants;
15
16 pub(super) enum ExportItem {
17 Function {
18 sig: FnSignature,
19 args: ExportFnArgs,
20 },
21 Impl {
22 self_ident: Ident,
23 items: Vec<ImplItem>,
24 args: ExportImplArgs,
25 trait_: Option<syn::Path>,
26 },
27 Trait {
28 self_ident: Ident,
29 items: Vec<ImplItem>,
30 with_foreign: bool,
31 callback_interface_only: bool,
32 docstring: String,
33 args: ExportTraitArgs,
34 },
35 Struct {
36 self_ident: Ident,
37 uniffi_traits: Vec<UniffiTraitDiscriminants>,
38 },
39 }
40
41 impl ExportItem {
42 pub fn new(item: syn::Item, attr_args: TokenStream) -> syn::Result<Self> {
43 match item {
44 syn::Item::Fn(item) => {
45 let args: ExportFnArgs = syn::parse(attr_args)?;
46 let docstring = extract_docstring(&item.attrs)?;
47 let sig = FnSignature::new_function(item.sig, args.clone(), docstring)?;
48 Ok(Self::Function { sig, args })
49 }
50 syn::Item::Impl(item) => Self::from_impl(item, attr_args),
51 syn::Item::Trait(item) => Self::from_trait(item, attr_args),
52 syn::Item::Struct(item) => Self::from_struct(item, attr_args),
53 // FIXME: Support const / static?
54 _ => Err(syn::Error::new(
55 Span::call_site(),
56 "unsupported item: only functions and impl \
57 blocks may be annotated with this attribute",
58 )),
59 }
60 }
61
62 pub fn from_impl(item: syn::ItemImpl, attr_args: TokenStream) -> syn::Result<Self> {
63 let args: ExportImplArgs = syn::parse(attr_args)?;
64 if !item.generics.params.is_empty() || item.generics.where_clause.is_some() {
65 return Err(syn::Error::new_spanned(
66 &item.generics,
67 "generic impls are not currently supported by uniffi::export",
68 ));
69 }
70
71 let type_path = type_as_type_path(&item.self_ty)?;
72
73 if type_path.qself.is_some() {
74 return Err(syn::Error::new_spanned(
75 type_path,
76 "qualified self types are not currently supported by uniffi::export",
77 ));
78 }
79
80 let self_ident = match type_path.path.get_ident() {
81 Some(id) => id,
82 None => {
83 return Err(syn::Error::new_spanned(
84 type_path,
85 "qualified paths in self-types are not currently supported by uniffi::export",
86 ));
87 }
88 };
89
90 let items = item
91 .items
92 .into_iter()
93 .map(|item| {
94 let impl_fn = match item {
95 syn::ImplItem::Fn(m) => m,
96 _ => {
97 return Err(syn::Error::new_spanned(
98 item,
99 "only fn's are supported in impl blocks annotated with uniffi::export",
100 ));
101 }
102 };
103
104 let docstring = extract_docstring(&impl_fn.attrs)?;
105 let attrs = ExportedImplFnAttributes::new(&impl_fn.attrs)?;
106 let item = if attrs.constructor {
107 ImplItem::Constructor(FnSignature::new_constructor(
108 self_ident.clone(),
109 impl_fn.sig,
110 attrs.args,
111 docstring,
112 )?)
113 } else {
114 ImplItem::Method(FnSignature::new_method(
115 self_ident.clone(),
116 impl_fn.sig,
117 attrs.args,
118 docstring,
119 )?)
120 };
121
122 Ok(item)
123 })
124 .collect::<syn::Result<_>>()?;
125
126 Ok(Self::Impl {
127 items,
128 self_ident: self_ident.to_owned(),
129 args,
130 trait_: item.trait_.map(|t| t.1),
131 })
132 }
133
134 fn from_trait(item: syn::ItemTrait, attr_args: TokenStream) -> syn::Result<Self> {
135 let args: ExportTraitArgs = syn::parse(attr_args)?;
136 let with_foreign = args.callback_interface.is_some() || args.with_foreign.is_some();
137 let callback_interface_only = args.callback_interface.is_some();
138
139 if !item.generics.params.is_empty() || item.generics.where_clause.is_some() {
140 return Err(syn::Error::new_spanned(
141 &item.generics,
142 "generic impls are not currently supported by uniffi::export",
143 ));
144 }
145
146 let self_ident = item.ident.to_owned();
147 let docstring = extract_docstring(&item.attrs)?;
148 let items = item
149 .items
150 .into_iter()
151 .enumerate()
152 .map(|(i, item)| {
153 let tim = match item {
154 syn::TraitItem::Fn(tim) => tim,
155 _ => {
156 return Err(syn::Error::new_spanned(
157 item,
158 "only fn's are supported in traits annotated with uniffi::export",
159 ));
160 }
161 };
162
163 let docstring = extract_docstring(&tim.attrs)?;
164 let attrs = ExportedImplFnAttributes::new(&tim.attrs)?;
165 let item = if attrs.constructor {
166 return Err(syn::Error::new_spanned(
167 tim,
168 "exported traits can not have constructors",
169 ));
170 } else {
171 ImplItem::Method(FnSignature::new_trait_method(
172 self_ident.clone(),
173 tim.sig,
174 ExportFnArgs::default(),
175 i as u32,
176 docstring,
177 )?)
178 };
179
180 Ok(item)
181 })
182 .collect::<syn::Result<_>>()?;
183
184 Ok(Self::Trait {
185 items,
186 self_ident,
187 with_foreign,
188 callback_interface_only,
189 docstring,
190 args,
191 })
192 }
193
194 fn from_struct(item: syn::ItemStruct, attr_args: TokenStream) -> syn::Result<Self> {
195 let args: ExportStructArgs = syn::parse(attr_args)?;
196 let uniffi_traits: Vec<UniffiTraitDiscriminants> = args.traits.into_iter().collect();
197 if uniffi_traits.is_empty() {
198 Err(syn::Error::new(Span::call_site(),
199 "uniffi::export on a struct must supply a builtin trait name. Did you mean `#[derive(uniffi::Object)]`?"
200 ))
201 } else {
202 Ok(Self::Struct {
203 self_ident: item.ident,
204 uniffi_traits,
205 })
206 }
207 }
208 }
209
210 pub(super) enum ImplItem {
211 Constructor(FnSignature),
212 Method(FnSignature),
213 }
214
215 fn type_as_type_path(ty: &syn::Type) -> syn::Result<&syn::TypePath> {
216 match ty {
217 syn::Type::Group(g) => type_as_type_path(&g.elem),
218 syn::Type::Paren(p) => type_as_type_path(&p.elem),
219 syn::Type::Path(p) => Ok(p),
220 _ => Err(type_not_supported(ty)),
221 }
222 }
223
224 fn type_not_supported(ty: &impl ToTokens) -> syn::Error {
225 syn::Error::new_spanned(
226 ty,
227 "this type is not currently supported by uniffi::export in this position",
228 )
229 }