2022-12-31 14:45:13 +01:00
|
|
|
use crate::syntax::{ast::App, Context};
|
2019-06-13 23:56:59 +02:00
|
|
|
use crate::{
|
|
|
|
analyze::Analysis,
|
2021-07-06 22:47:48 +02:00
|
|
|
codegen::{local_resources_struct, module, shared_resources_struct},
|
2019-06-13 23:56:59 +02:00
|
|
|
};
|
2022-12-31 14:45:13 +01:00
|
|
|
use proc_macro2::TokenStream as TokenStream2;
|
|
|
|
use quote::quote;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
/// Generates support code for `#[idle]` functions
|
2023-01-07 14:26:55 +01:00
|
|
|
pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
|
2021-07-06 22:47:48 +02:00
|
|
|
if let Some(idle) = &app.idle {
|
2021-07-07 21:03:56 +02:00
|
|
|
let mut mod_app = vec![];
|
2019-06-13 23:56:59 +02:00
|
|
|
let mut root_idle = vec![];
|
|
|
|
|
2020-05-26 12:52:10 +02:00
|
|
|
let name = &idle.name;
|
|
|
|
|
2021-07-06 22:47:48 +02:00
|
|
|
if !idle.args.shared_resources.is_empty() {
|
2023-01-07 11:24:13 +01:00
|
|
|
let (item, constructor) = shared_resources_struct::codegen(Context::Idle, app);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
root_idle.push(item);
|
2021-07-07 21:03:56 +02:00
|
|
|
mod_app.push(constructor);
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
|
2021-07-07 21:03:56 +02:00
|
|
|
if !idle.args.local_resources.is_empty() {
|
2023-01-07 11:24:13 +01:00
|
|
|
let (item, constructor) = local_resources_struct::codegen(Context::Idle, app);
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2021-07-07 21:03:56 +02:00
|
|
|
root_idle.push(item);
|
|
|
|
|
|
|
|
mod_app.push(constructor);
|
|
|
|
}
|
2019-06-13 23:56:59 +02:00
|
|
|
|
2023-01-07 11:24:13 +01:00
|
|
|
root_idle.push(module::codegen(Context::Idle, app, analysis));
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
let attrs = &idle.attrs;
|
|
|
|
let context = &idle.context;
|
|
|
|
let stmts = &idle.stmts;
|
|
|
|
let user_idle = Some(quote!(
|
|
|
|
#(#attrs)*
|
|
|
|
#[allow(non_snake_case)]
|
2021-07-06 22:47:48 +02:00
|
|
|
fn #name(#context: #name::Context) -> ! {
|
2020-06-11 19:18:29 +02:00
|
|
|
use rtic::Mutex as _;
|
2022-02-18 15:11:55 +01:00
|
|
|
use rtic::mutex::prelude::*;
|
2019-06-13 23:56:59 +02:00
|
|
|
|
|
|
|
#(#stmts)*
|
|
|
|
}
|
|
|
|
));
|
|
|
|
|
2023-01-07 14:26:55 +01:00
|
|
|
quote!(
|
|
|
|
#(#mod_app)*
|
|
|
|
|
|
|
|
#(#root_idle)*
|
2022-12-31 14:45:13 +01:00
|
|
|
|
2023-01-07 14:26:55 +01:00
|
|
|
#user_idle
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
quote!()
|
2019-06-13 23:56:59 +02:00
|
|
|
}
|
|
|
|
}
|