xref: /linux/rust/macros/export.rs (revision f637bafe1ff15fa356c1e0576c32f077b9e6e46a)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 use proc_macro2::TokenStream;
4 use quote::quote;
5 
6 use crate::helpers::function_name;
7 
8 /// Please see [`crate::export`] for documentation.
9 pub(crate) fn export(_attr: TokenStream, ts: TokenStream) -> TokenStream {
10     let Some(name) = function_name(ts.clone()) else {
11         return "::core::compile_error!(\"The #[export] attribute must be used on a function.\");"
12             .parse::<TokenStream>()
13             .unwrap();
14     };
15 
16     // This verifies that the function has the same signature as the declaration generated by
17     // bindgen. It makes use of the fact that all branches of an if/else must have the same type.
18     let signature_check = quote!(
19         const _: () = {
20             if true {
21                 ::kernel::bindings::#name
22             } else {
23                 #name
24             };
25         };
26     );
27 
28     let no_mangle = quote!(#[no_mangle]);
29 
30     TokenStream::from_iter([signature_check, no_mangle, ts])
31 }
32