aboutsummaryrefslogtreecommitdiff
path: root/macros/src/codegen/idle.rs
blob: 2aa9975142fbe7373fd8b4b9c9db7c95ca89f106 (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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use proc_macro2::TokenStream as TokenStream2;
use quote::{format_ident, quote};
use rtic_syntax::{ast::App, Context};

use crate::{
    analyze::Analysis,
    check::Extra,
    codegen::{locals, module, resources_struct},
};

/// Generates support code for `#[idle]` functions
pub fn codegen(
    app: &App,
    analysis: &Analysis,
    extra: &Extra,
) -> (
    // const_app_idle -- the `${idle}Resources` constructor
    Option<TokenStream2>,
    // root_idle -- items that must be placed in the root of the crate:
    // - the `${idle}Locals` struct
    // - the `${idle}Resources` struct
    // - the `${idle}` module, which contains types like `${idle}::Context`
    Vec<TokenStream2>,
    // user_idle
    Option<TokenStream2>,
    // user_idle_imports
    Vec<TokenStream2>,
    // call_idle
    TokenStream2,
) {
    if app.idles.len() > 0 {
        let idle = &app.idles.first().unwrap();
        let mut needs_lt = false;
        let mut const_app = None;
        let mut root_idle = vec![];
        let mut locals_pat = None;
        let mut locals_new = None;

        let mut user_idle_imports = vec![];

        let name = &idle.name;

        if !idle.args.resources.is_empty() {
            let (item, constructor) =
                resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis);

            root_idle.push(item);
            const_app = Some(constructor);

            let name_resource = format_ident!("{}Resources", name);
            user_idle_imports.push(quote!(
                    #[allow(non_snake_case)]
                    use super::#name_resource;
            ));
        }

        if !idle.locals.is_empty() {
            let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app);

            locals_new = Some(quote!(#name::Locals::new()));
            locals_pat = Some(pat);
            root_idle.push(locals);
        }

        root_idle.push(module::codegen(Context::Idle, needs_lt, app, extra));

        let attrs = &idle.attrs;
        let context = &idle.context;
        let stmts = &idle.stmts;
        let locals_pat = locals_pat.iter();
        let user_idle = Some(quote!(
            #(#attrs)*
            #[allow(non_snake_case)]
            fn #name(#(#locals_pat,)* #context: #name::Context) -> ! {
                use rtic::Mutex as _;

                #(#stmts)*
            }
        ));
        user_idle_imports.push(quote!(
            #(#attrs)*
            #[allow(non_snake_case)]
            use super::#name;
        ));

        let locals_new = locals_new.iter();
        let call_idle = quote!(crate::#name(
            #(#locals_new,)*
            #name::Context::new(&rtic::export::Priority::new(0))
        ));

        (
            const_app,
            root_idle,
            user_idle,
            user_idle_imports,
            call_idle,
        )
    } else {
        (
            None,
            vec![],
            None,
            vec![],
            quote!(loop {
                rtic::export::wfi()
            }),
        )
    }
}