aboutsummaryrefslogtreecommitdiff
path: root/embassy-macros/src/macros/cortex_m_interrupt.rs
blob: 13af8ca075267faa1b784c9c1f4819e08e0482c0 (plain)
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::iter;

use darling::FromMeta;
use proc_macro2::TokenStream;
use quote::quote;
use syn::{ReturnType, Type, Visibility};

use crate::util::ctxt::Ctxt;

#[derive(Debug, FromMeta)]
struct Args {}

pub fn run(args: syn::AttributeArgs, mut f: syn::ItemFn) -> Result<TokenStream, TokenStream> {
    let _args = Args::from_list(&args).map_err(|e| e.write_errors())?;

    let ident = f.sig.ident.clone();
    let ident_s = ident.to_string();

    // XXX should we blacklist other attributes?

    let valid_signature = f.sig.constness.is_none()
        && f.vis == Visibility::Inherited
        && f.sig.abi.is_none()
        && f.sig.inputs.is_empty()
        && f.sig.generics.params.is_empty()
        && f.sig.generics.where_clause.is_none()
        && f.sig.variadic.is_none()
        && match f.sig.output {
            ReturnType::Default => true,
            ReturnType::Type(_, ref ty) => match **ty {
                Type::Tuple(ref tuple) => tuple.elems.is_empty(),
                Type::Never(..) => true,
                _ => false,
            },
        };

    let ctxt = Ctxt::new();

    if !valid_signature {
        ctxt.error_spanned_by(
            &f.sig,
            "`#[interrupt]` handlers must have signature `[unsafe] fn() [-> !]`",
        );
    }

    ctxt.check()?;

    f.block.stmts = iter::once(
        syn::parse2(quote! {{
            // Check that this interrupt actually exists
            let __irq_exists_check: interrupt::#ident;
        }})
        .unwrap(),
    )
    .chain(f.block.stmts)
    .collect();

    let result = quote!(
        #[doc(hidden)]
        #[export_name = #ident_s]
        #[allow(non_snake_case)]
        #f
    );

    Ok(result)
}