rtic/rtic-macros/src/codegen/idle.rs

59 lines
1.5 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
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![];
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:26:55 +01:00
quote!(
#(#mod_app)*
#(#root_idle)*
2023-01-07 14:26:55 +01:00
#user_idle
)
} else {
quote!()
}
}