rtic/macros/src/codegen/idle.rs

68 lines
1.9 KiB
Rust
Raw Normal View History

use crate::syntax::{ast::App, Context};
use crate::{
analyze::Analysis,
2021-07-06 22:47:48 +02:00
codegen::{local_resources_struct, module, shared_resources_struct},
};
use proc_macro2::TokenStream as TokenStream2;
use quote::quote;
/// Generates support code for `#[idle]` functions
pub fn codegen(
app: &App,
analysis: &Analysis,
) -> (
2020-10-01 18:17:15 +02:00
// mod_app_idle -- the `${idle}Resources` constructor
2021-07-07 21:03:56 +02:00
Vec<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>,
) {
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![];
let mut root_idle = vec![];
let name = &idle.name;
2021-07-06 22:47:48 +02:00
if !idle.args.shared_resources.is_empty() {
let (item, constructor) = shared_resources_struct::codegen(Context::Idle, app);
root_idle.push(item);
2021-07-07 21:03:56 +02:00
mod_app.push(constructor);
}
2021-07-07 21:03:56 +02:00
if !idle.args.local_resources.is_empty() {
let (item, constructor) = local_resources_struct::codegen(Context::Idle, app);
2021-07-07 21:03:56 +02:00
root_idle.push(item);
mod_app.push(constructor);
}
root_idle.push(module::codegen(Context::Idle, app, analysis));
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 _;
use rtic::mutex::prelude::*;
#(#stmts)*
}
));
2023-01-07 14:06:11 +01:00
(mod_app, root_idle, user_idle)
} else {
// TODO: No idle defined, check for 0-priority tasks and generate an executor if needed
2023-01-07 14:06:11 +01:00
(vec![], vec![], None)
}
}