aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/assertions.rs
blob: f6a098b5e6f39556fc3b256560d906c67f636ef4 (plain) (blame)
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
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;

use crate::{analyze::Analysis, check::Extra, codegen::util};
use rtic_syntax::ast::App;

/// Generates compile-time assertions that check that types implement the `Send` / `Sync` traits
pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream2> {
    let mut stmts = vec![];

    for ty in &analysis.send_types {
        stmts.push(quote!(rtic::export::assert_send::<#ty>();));
    }

    for ty in &analysis.sync_types {
        stmts.push(quote!(rtic::export::assert_sync::<#ty>();));
    }

    for (_, monotonic) in &app.monotonics {
        let ty = &monotonic.ty;
        stmts.push(quote!(rtic::export::assert_monotonic::<#ty>();));
    }

    let device = &extra.device;
    let arm_v6_checks: Vec<_> = app
        .hardware_tasks
        .iter()
        .filter_map(|(_, task)| {
            if !util::is_exception(&task.args.binds) {
                let interrupt_name = &task.args.binds;
                Some(quote!(
                    if (#device::Interrupt::#interrupt_name as u32) > 31 {
                        ::core::panic!("An interrupt above value 31 is used while in armv6");
                    }
                ))
            } else {
                None
            }
        })
        .collect();

    let const_check = quote! {
        const _CONST_CHECK: () = {
            if rtic::export::is_armv6() {
                #(#arm_v6_checks)*
            } else {
                // TODO: Add armv7 checks here
            }
        };

        let _ = _CONST_CHECK;
    };

    stmts.push(const_check);

    stmts
}