referrerpolicy=no-referrer-when-downgrade

sp_runtime_interface_proc_macro/
utils.rs

1// This file is part of Substrate.
2
3// Copyright (C) Parity Technologies (UK) Ltd.
4// SPDX-License-Identifier: Apache-2.0
5
6// Licensed under the Apache License, Version 2.0 (the "License");
7// you may not use this file except in compliance with the License.
8// You may obtain a copy of the License at
9//
10// 	http://www.apache.org/licenses/LICENSE-2.0
11//
12// Unless required by applicable law or agreed to in writing, software
13// distributed under the License is distributed on an "AS IS" BASIS,
14// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15// See the License for the specific language governing permissions and
16// limitations under the License.
17
18//! Util function used by this crate.
19
20use 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
42/// A concrete, specific version of a runtime interface function.
43pub 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
83/// Runtime interface function with all associated versions of this function.
84struct 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	/// Returns the latest version of this runtime interface function plus the actual function
101	/// implementation.
102	///
103	/// This isn't required to be the latest version, because a runtime interface function can be
104	/// annotated with `register_only` to ensure that the host exposes the host function but it
105	/// isn't used when compiling the runtime.
106	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	/// Add a different version of the function.
118	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
141/// All functions of a runtime interface grouped by the function names.
142pub struct RuntimeInterface {
143	items: BTreeMap<syn::Ident, RuntimeInterfaceFunctionSet>,
144}
145
146impl RuntimeInterface {
147	/// Returns an iterator over all runtime interface function
148	/// [`latest_version_to_call`](RuntimeInterfaceFunctionSet::latest_version).
149	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
163/// Generates the include for the runtime-interface crate.
164pub 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
181/// Generates the access to the `sp-runtime-interface` crate.
182pub 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
190/// Create the exchangeable host function identifier for the given function name.
191pub fn create_exchangeable_host_function_ident(name: &Ident) -> Ident {
192	Ident::new(&format!("host_{}", name), Span::call_site())
193}
194
195/// Create the host function identifier for the given function name.
196pub 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
203/// Create the host function identifier for the given function name.
204pub fn create_function_ident_with_version(name: &Ident, version: u32) -> Ident {
205	Ident::new(&format!("{}_version_{}", name, version), Span::call_site())
206}
207
208/// Returns the function arguments of the given `Signature`, minus any `self` arguments.
209pub 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
230/// Returns the function argument names of the given `Signature`, minus any `self`.
231pub fn get_function_argument_names(sig: &Signature) -> impl Iterator<Item = Box<Pat>> + '_ {
232	get_function_arguments(sig).map(|pt| pt.pat)
233}
234
235/// Returns the function argument types of the given `Signature`, minus any `Self` type.
236pub fn get_function_argument_types(sig: &Signature) -> impl Iterator<Item = Box<Type>> + '_ {
237	get_function_arguments(sig).map(|pt| pt.ty)
238}
239
240/// Returns the function argument names and types, minus any `self`.
241pub fn get_function_argument_names_and_types(
242	sig: &Signature,
243) -> impl Iterator<Item = (Box<Pat>, Box<Type>)> + '_ {
244	get_function_arguments(sig).map(|pt| (pt.pat, pt.ty))
245}
246
247/// Returns an iterator over all trait methods for the given trait definition.
248fn get_trait_methods(trait_def: &ItemTrait) -> impl Iterator<Item = &TraitItemFn> {
249	trait_def.items.iter().filter_map(|i| match i {
250		TraitItem::Fn(ref method) => Some(method),
251		_ => None,
252	})
253}
254
255/// The version attribute that can be found above a runtime interface function.
256///
257/// Supports the following formats:
258/// - `#[version(1)]`
259/// - `#[version(1, register_only)]`
260///
261/// While this struct is only for parsing the inner parts inside the `()`.
262struct VersionAttribute {
263	version: u32,
264	register_only: Option<attributes::register_only>,
265}
266
267impl VersionAttribute {
268	/// Is this function version callable?
269	fn is_callable(&self) -> bool {
270		self.register_only.is_none()
271	}
272}
273
274impl Default for VersionAttribute {
275	fn default() -> Self {
276		Self { version: 1, register_only: None }
277	}
278}
279
280impl Parse for VersionAttribute {
281	fn parse(input: syn::parse::ParseStream) -> Result<Self> {
282		let version: LitInt = input.parse()?;
283		let register_only = if input.peek(token::Comma) {
284			let _ = input.parse::<token::Comma>();
285			Some(input.parse()?)
286		} else {
287			if !input.is_empty() {
288				return Err(Error::new(input.span(), "Unexpected token, expected `,`."))
289			}
290
291			None
292		};
293
294		Ok(Self { version: version.base10_parse()?, register_only })
295	}
296}
297
298/// Return [`VersionAttribute`], if present.
299fn get_item_version(item: &TraitItemFn) -> Result<Option<VersionAttribute>> {
300	item.attrs
301		.iter()
302		.find(|attr| attr.path().is_ident("version"))
303		.map(|attr| attr.parse_args())
304		.transpose()
305}
306
307/// Returns all runtime interface members, with versions.
308pub fn get_runtime_interface(trait_def: &ItemTrait) -> Result<RuntimeInterface> {
309	let mut functions: BTreeMap<syn::Ident, RuntimeInterfaceFunctionSet> = BTreeMap::new();
310
311	for item in get_trait_methods(trait_def) {
312		let name = item.sig.ident.clone();
313		let version = get_item_version(item)?.unwrap_or_default();
314
315		if version.version < 1 {
316			return Err(Error::new(item.span(), "Version needs to be at least `1`."))
317		}
318
319		match functions.entry(name.clone()) {
320			Entry::Vacant(entry) => {
321				entry.insert(RuntimeInterfaceFunctionSet::new(version, item)?);
322			},
323			Entry::Occupied(mut entry) => {
324				entry.get_mut().add_version(version, item)?;
325			},
326		}
327	}
328
329	for function in functions.values() {
330		let mut next_expected = 1;
331		for (version, item) in function.versions.iter() {
332			if next_expected != *version {
333				return Err(Error::new(
334					item.span(),
335					format!(
336						"Unexpected version attribute: missing version '{}' for this function",
337						next_expected
338					),
339				))
340			}
341			next_expected += 1;
342		}
343	}
344
345	Ok(RuntimeInterface { items: functions })
346}
347
348pub fn host_inner_arg_ty(ty: &syn::Type) -> syn::Type {
349	let crate_ = generate_crate_access();
350	syn::parse2::<syn::Type>(quote! { <#ty as #crate_::RIType>::Inner })
351		.expect("parsing doesn't fail")
352}
353
354pub fn pat_ty_to_host_inner(mut pat: syn::PatType) -> syn::PatType {
355	pat.ty = Box::new(host_inner_arg_ty(&pat.ty));
356	pat
357}
358
359pub fn host_inner_return_ty(ty: &syn::ReturnType) -> syn::ReturnType {
360	let crate_ = generate_crate_access();
361	match ty {
362		syn::ReturnType::Default => syn::ReturnType::Default,
363		syn::ReturnType::Type(ref arrow, ref ty) =>
364			syn::parse2::<syn::ReturnType>(quote! { #arrow <#ty as #crate_::RIType>::Inner })
365				.expect("parsing doesn't fail"),
366	}
367}
368
369pub fn unpack_inner_types_in_signature(sig: &mut syn::Signature) {
370	sig.output = crate::utils::host_inner_return_ty(&sig.output);
371	for arg in sig.inputs.iter_mut() {
372		match arg {
373			syn::FnArg::Typed(ref mut pat_ty) => {
374				*pat_ty = crate::utils::pat_ty_to_host_inner(pat_ty.clone());
375			},
376			syn::FnArg::Receiver(..) => {},
377		}
378	}
379}