macros/
export.rs

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