polkavm_derive_impl/
define_abi.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
use quote::quote;
use syn::spanned::Spanned;

use crate::abi_support::AbiSupportAttributes;
use crate::common::{is_cfg, is_doc, is_rustfmt};

pub fn polkavm_define_abi(attributes: AbiSupportAttributes, input: syn::ItemMod) -> Result<proc_macro2::TokenStream, syn::Error> {
    let mut outer_cfg_attributes = Vec::new();
    let mut outer_doc_attributes = Vec::new();

    let Some(content) = input.content else {
        return Err(syn::Error::new(input.ident.span(), "the module must have curly braces"));
    };

    if !content.1.is_empty() {
        return Err(syn::Error::new(input.ident.span(), "the module must be empty"));
    }

    for attr in input.attrs {
        if is_rustfmt(&attr) {
            continue;
        }

        if is_cfg(&attr) {
            outer_cfg_attributes.push(attr);
            continue;
        }

        if is_doc(&attr) {
            outer_doc_attributes.push(attr);
            continue;
        }

        unsupported!(attr);
    }

    unsupported_if_some!(input.unsafety);
    unsupported_if_some!(content.1.first());

    let mod_token = input.mod_token;
    let visibility = input.vis;
    let ident = input.ident;
    let support_code = crate::abi_support::polkavm_impl_abi_support(attributes);

    Ok(quote! {
        #(#outer_cfg_attributes)*
        #(#outer_doc_attributes)*
        #visibility #mod_token #ident {
            #support_code
        }
    })
}