sp_runtime_interface_proc_macro/
utils.rs1use proc_macro2::{Span, TokenStream};
21
22use syn::{
23 parse::Parse, parse_quote, spanned::Spanned, token, Error, FnArg, Ident, ItemTrait, LitInt,
24 Pat, PatType, Result, Signature, TraitItem, TraitItemFn, Type,
25};
26
27use proc_macro_crate::{crate_name, FoundCrate};
28
29use std::{
30 collections::{btree_map::Entry, BTreeMap},
31 env,
32};
33
34use quote::quote;
35
36use inflector::Inflector;
37
38mod attributes {
39 syn::custom_keyword!(register_only);
40}
41
42pub struct RuntimeInterfaceFunction {
44 item: TraitItemFn,
45 should_trap_on_return: bool,
46}
47
48impl std::ops::Deref for RuntimeInterfaceFunction {
49 type Target = TraitItemFn;
50 fn deref(&self) -> &Self::Target {
51 &self.item
52 }
53}
54
55impl RuntimeInterfaceFunction {
56 fn new(item: &TraitItemFn) -> Result<Self> {
57 let mut item = item.clone();
58 let mut should_trap_on_return = false;
59 item.attrs.retain(|attr| {
60 if attr.path().is_ident("trap_on_return") {
61 should_trap_on_return = true;
62 false
63 } else {
64 true
65 }
66 });
67
68 if should_trap_on_return && !matches!(item.sig.output, syn::ReturnType::Default) {
69 return Err(Error::new(
70 item.sig.ident.span(),
71 "Methods marked as #[trap_on_return] cannot return anything",
72 ))
73 }
74
75 Ok(Self { item, should_trap_on_return })
76 }
77
78 pub fn should_trap_on_return(&self) -> bool {
79 self.should_trap_on_return
80 }
81}
82
83struct RuntimeInterfaceFunctionSet {
85 latest_version_to_call: Option<u32>,
86 versions: BTreeMap<u32, RuntimeInterfaceFunction>,
87}
88
89impl RuntimeInterfaceFunctionSet {
90 fn new(version: VersionAttribute, trait_item: &TraitItemFn) -> Result<Self> {
91 Ok(Self {
92 latest_version_to_call: version.is_callable().then_some(version.version),
93 versions: BTreeMap::from([(
94 version.version,
95 RuntimeInterfaceFunction::new(trait_item)?,
96 )]),
97 })
98 }
99
100 pub fn latest_version_to_call(&self) -> Option<(u32, &RuntimeInterfaceFunction)> {
107 self.latest_version_to_call.map(|v| {
108 (
109 v,
110 self.versions.get(&v).expect(
111 "If latest_version_to_call has a value, the key with this value is in the versions; qed",
112 ),
113 )
114 })
115 }
116
117 fn add_version(&mut self, version: VersionAttribute, trait_item: &TraitItemFn) -> Result<()> {
119 if let Some(existing_item) = self.versions.get(&version.version) {
120 let mut err = Error::new(trait_item.span(), "Duplicated version attribute");
121 err.combine(Error::new(
122 existing_item.span(),
123 "Previous version with the same number defined here",
124 ));
125
126 return Err(err)
127 }
128
129 self.versions
130 .insert(version.version, RuntimeInterfaceFunction::new(trait_item)?);
131 if self.latest_version_to_call.map_or(true, |v| v < version.version) &&
132 version.is_callable()
133 {
134 self.latest_version_to_call = Some(version.version);
135 }
136
137 Ok(())
138 }
139}
140
141pub struct RuntimeInterface {
143 items: BTreeMap<syn::Ident, RuntimeInterfaceFunctionSet>,
144}
145
146impl RuntimeInterface {
147 pub fn latest_versions_to_call(
150 &self,
151 ) -> impl Iterator<Item = (u32, &RuntimeInterfaceFunction)> {
152 self.items.iter().filter_map(|(_, item)| item.latest_version_to_call())
153 }
154
155 pub fn all_versions(&self) -> impl Iterator<Item = (u32, &RuntimeInterfaceFunction)> {
156 self.items
157 .iter()
158 .flat_map(|(_, item)| item.versions.iter())
159 .map(|(v, i)| (*v, i))
160 }
161}
162
163pub fn generate_runtime_interface_include() -> TokenStream {
165 match crate_name("sp-runtime-interface") {
166 Ok(FoundCrate::Itself) => quote!(),
167 Ok(FoundCrate::Name(crate_name)) => {
168 let crate_name = Ident::new(&crate_name, Span::call_site());
169 quote!(
170 #[doc(hidden)]
171 extern crate #crate_name as proc_macro_runtime_interface;
172 )
173 },
174 Err(e) => {
175 let err = Error::new(Span::call_site(), e).to_compile_error();
176 quote!( #err )
177 },
178 }
179}
180
181pub fn generate_crate_access() -> TokenStream {
183 if env::var("CARGO_PKG_NAME").unwrap() == "sp-runtime-interface" {
184 quote!(sp_runtime_interface)
185 } else {
186 quote!(proc_macro_runtime_interface)
187 }
188}
189
190pub fn create_exchangeable_host_function_ident(name: &Ident) -> Ident {
192 Ident::new(&format!("host_{}", name), Span::call_site())
193}
194
195pub fn create_host_function_ident(name: &Ident, version: u32, trait_name: &Ident) -> Ident {
197 Ident::new(
198 &format!("ext_{}_{}_version_{}", trait_name.to_string().to_snake_case(), name, version),
199 Span::call_site(),
200 )
201}
202
203pub fn create_function_ident_with_version(name: &Ident, version: u32) -> Ident {
205 Ident::new(&format!("{}_version_{}", name, version), Span::call_site())
206}
207
208pub fn get_function_arguments(sig: &Signature) -> impl Iterator<Item = PatType> + '_ {
210 sig.inputs
211 .iter()
212 .filter_map(|a| match a {
213 FnArg::Receiver(_) => None,
214 FnArg::Typed(pat_type) => Some(pat_type),
215 })
216 .enumerate()
217 .map(|(i, arg)| {
218 let mut res = arg.clone();
219 if let Pat::Wild(wild) = &*arg.pat {
220 let ident =
221 Ident::new(&format!("__runtime_interface_generated_{}_", i), wild.span());
222
223 res.pat = Box::new(parse_quote!( #ident ))
224 }
225
226 res
227 })
228}
229
230pub fn get_function_argument_names(sig: &Signature) -> impl Iterator<Item = Box<Pat>> + '_ {
232 get_function_arguments(sig).map(|pt| pt.pat)
233}
234
235pub fn get_function_argument_types(sig: &Signature) -> impl Iterator<Item = Box<Type>> + '_ {
237 get_function_arguments(sig).map(|pt| pt.ty)
238}
239
240pub fn get_function_argument_types_without_ref(
243 sig: &Signature,
244) -> impl Iterator<Item = Box<Type>> + '_ {
245 get_function_arguments(sig).map(|pt| pt.ty).map(|ty| match *ty {
246 Type::Reference(type_ref) => type_ref.elem,
247 _ => ty,
248 })
249}
250
251pub fn get_function_argument_names_and_types_without_ref(
254 sig: &Signature,
255) -> impl Iterator<Item = (Box<Pat>, Box<Type>)> + '_ {
256 get_function_arguments(sig).map(|pt| match *pt.ty {
257 Type::Reference(type_ref) => (pt.pat, type_ref.elem),
258 _ => (pt.pat, pt.ty),
259 })
260}
261
262pub fn get_function_argument_types_ref_and_mut(
265 sig: &Signature,
266) -> impl Iterator<Item = Option<(token::And, Option<token::Mut>)>> + '_ {
267 get_function_arguments(sig).map(|pt| pt.ty).map(|ty| match *ty {
268 Type::Reference(type_ref) => Some((type_ref.and_token, type_ref.mutability)),
269 _ => None,
270 })
271}
272
273fn get_trait_methods(trait_def: &ItemTrait) -> impl Iterator<Item = &TraitItemFn> {
275 trait_def.items.iter().filter_map(|i| match i {
276 TraitItem::Fn(ref method) => Some(method),
277 _ => None,
278 })
279}
280
281struct VersionAttribute {
289 version: u32,
290 register_only: Option<attributes::register_only>,
291}
292
293impl VersionAttribute {
294 fn is_callable(&self) -> bool {
296 self.register_only.is_none()
297 }
298}
299
300impl Default for VersionAttribute {
301 fn default() -> Self {
302 Self { version: 1, register_only: None }
303 }
304}
305
306impl Parse for VersionAttribute {
307 fn parse(input: syn::parse::ParseStream) -> Result<Self> {
308 let version: LitInt = input.parse()?;
309 let register_only = if input.peek(token::Comma) {
310 let _ = input.parse::<token::Comma>();
311 Some(input.parse()?)
312 } else {
313 if !input.is_empty() {
314 return Err(Error::new(input.span(), "Unexpected token, expected `,`."))
315 }
316
317 None
318 };
319
320 Ok(Self { version: version.base10_parse()?, register_only })
321 }
322}
323
324fn get_item_version(item: &TraitItemFn) -> Result<Option<VersionAttribute>> {
326 item.attrs
327 .iter()
328 .find(|attr| attr.path().is_ident("version"))
329 .map(|attr| attr.parse_args())
330 .transpose()
331}
332
333pub fn get_runtime_interface(trait_def: &ItemTrait) -> Result<RuntimeInterface> {
335 let mut functions: BTreeMap<syn::Ident, RuntimeInterfaceFunctionSet> = BTreeMap::new();
336
337 for item in get_trait_methods(trait_def) {
338 let name = item.sig.ident.clone();
339 let version = get_item_version(item)?.unwrap_or_default();
340
341 if version.version < 1 {
342 return Err(Error::new(item.span(), "Version needs to be at least `1`."))
343 }
344
345 match functions.entry(name.clone()) {
346 Entry::Vacant(entry) => {
347 entry.insert(RuntimeInterfaceFunctionSet::new(version, item)?);
348 },
349 Entry::Occupied(mut entry) => {
350 entry.get_mut().add_version(version, item)?;
351 },
352 }
353 }
354
355 for function in functions.values() {
356 let mut next_expected = 1;
357 for (version, item) in function.versions.iter() {
358 if next_expected != *version {
359 return Err(Error::new(
360 item.span(),
361 format!(
362 "Unexpected version attribute: missing version '{}' for this function",
363 next_expected
364 ),
365 ))
366 }
367 next_expected += 1;
368 }
369 }
370
371 Ok(RuntimeInterface { items: functions })
372}