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

171 lines
4.6 KiB
Rust
Raw Normal View History

use crate::syntax::{ast::App, Context};
use core::sync::atomic::{AtomicUsize, Ordering};
use proc_macro2::{Span, TokenStream as TokenStream2};
use quote::quote;
2023-02-19 14:30:49 +01:00
use syn::{Ident, PatType};
const RTIC_INTERNAL: &str = "__rtic_internal";
2020-08-27 13:21:56 +02:00
pub fn interrupt_ident() -> Ident {
2019-06-18 10:31:31 +02:00
let span = Span::call_site();
Ident::new("interrupt", span)
2019-06-18 10:31:31 +02:00
}
/// Mark a name as internal
pub fn mark_internal_name(name: &str) -> Ident {
2023-01-08 21:30:53 +01:00
Ident::new(&format!("{RTIC_INTERNAL}_{name}"), Span::call_site())
}
2021-05-06 19:40:37 +02:00
/// Generate an internal identifier for tasks
pub fn internal_task_ident(task: &Ident, ident_name: &str) -> Ident {
2023-01-08 21:30:53 +01:00
mark_internal_name(&format!("{task}_{ident_name}"))
2021-05-06 19:40:37 +02:00
}
2019-06-29 09:11:42 +02:00
fn link_section_index() -> usize {
static INDEX: AtomicUsize = AtomicUsize::new(0);
INDEX.fetch_add(1, Ordering::Relaxed)
}
2022-02-18 19:38:48 +01:00
/// Add `link_section` attribute
pub fn link_section_uninit() -> TokenStream2 {
let section = format!(".uninit.rtic{}", link_section_index());
2019-06-29 09:11:42 +02:00
2022-02-18 19:38:48 +01:00
quote!(#[link_section = #section])
2019-06-29 09:11:42 +02:00
}
/// Regroups the inputs of a task
///
/// `inputs` could be &[`input: Foo`] OR &[`mut x: i32`, `ref y: i64`]
pub fn regroup_inputs(
inputs: &[PatType],
) -> (
// args e.g. &[`_0`], &[`_0: i32`, `_1: i64`]
Vec<TokenStream2>,
// tupled e.g. `_0`, `(_0, _1)`
TokenStream2,
// untupled e.g. &[`_0`], &[`_0`, `_1`]
Vec<TokenStream2>,
// ty e.g. `Foo`, `(i32, i64)`
TokenStream2,
) {
if inputs.len() == 1 {
let ty = &inputs[0].ty;
(
vec![quote!(_0: #ty)],
quote!(_0),
vec![quote!(_0)],
quote!(#ty),
)
} else {
let mut args = vec![];
let mut pats = vec![];
let mut tys = vec![];
for (i, input) in inputs.iter().enumerate() {
2023-01-27 11:24:43 +01:00
let i = Ident::new(&format!("_{i}"), Span::call_site());
let ty = &input.ty;
args.push(quote!(#i: #ty));
pats.push(quote!(#i));
tys.push(quote!(#ty));
}
let tupled = {
let pats = pats.clone();
quote!((#(#pats,)*))
};
let ty = quote!((#(#tys,)*));
(args, tupled, pats, ty)
}
}
2021-07-07 22:50:59 +02:00
/// Get the ident for the name of the task
pub fn get_task_name(ctxt: Context, app: &App) -> Ident {
let s = match ctxt {
Context::Init => app.init.name.to_string(),
Context::Idle => app
.idle
.as_ref()
.expect("RTIC-ICE: unable to find idle name")
.name
.to_string(),
2021-07-07 22:50:59 +02:00
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
};
Ident::new(&s, Span::call_site())
}
2021-07-05 21:40:01 +02:00
/// Generates a pre-reexport identifier for the "shared resources" struct
pub fn shared_resources_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt {
2021-07-05 21:40:01 +02:00
Context::Init => app.init.name.to_string(),
Context::Idle => app
.idle
.as_ref()
.expect("RTIC-ICE: unable to find idle name")
.name
.to_string(),
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
};
2021-07-05 21:40:01 +02:00
s.push_str("SharedResources");
mark_internal_name(&s)
2021-07-05 21:40:01 +02:00
}
/// Generates a pre-reexport identifier for the "local resources" struct
pub fn local_resources_ident(ctxt: Context, app: &App) -> Ident {
let mut s = match ctxt {
Context::Init => app.init.name.to_string(),
Context::Idle => app
.idle
.as_ref()
.expect("RTIC-ICE: unable to find idle name")
.name
.to_string(),
2021-07-05 21:40:01 +02:00
Context::HardwareTask(ident) | Context::SoftwareTask(ident) => ident.to_string(),
};
s.push_str("LocalResources");
mark_internal_name(&s)
}
2020-12-13 14:52:16 +01:00
/// Suffixed identifier
2020-08-27 13:21:56 +02:00
pub fn suffixed(name: &str) -> Ident {
2019-06-18 10:31:31 +02:00
let span = Span::call_site();
2020-08-27 13:21:56 +02:00
Ident::new(name, span)
2019-06-18 10:31:31 +02:00
}
2021-07-06 22:47:48 +02:00
pub fn static_shared_resource_ident(name: &Ident) -> Ident {
2023-01-08 21:30:53 +01:00
mark_internal_name(&format!("shared_resource_{name}"))
2021-07-06 22:47:48 +02:00
}
pub fn static_local_resource_ident(name: &Ident) -> Ident {
2023-01-08 21:30:53 +01:00
mark_internal_name(&format!("local_resource_{name}"))
2021-07-07 22:50:59 +02:00
}
pub fn declared_static_local_resource_ident(name: &Ident, task_name: &Ident) -> Ident {
2023-01-08 21:30:53 +01:00
mark_internal_name(&format!("local_{task_name}_{name}"))
2021-07-06 22:47:48 +02:00
}
pub fn need_to_lock_ident(name: &Ident) -> Ident {
2023-01-08 21:30:53 +01:00
Ident::new(&format!("{name}_that_needs_to_be_locked"), name.span())
}
2023-01-08 21:10:06 +01:00
pub fn zero_prio_dispatcher_ident() -> Ident {
Ident::new("__rtic_internal_async_0_prio_dispatcher", Span::call_site())
}
2020-12-13 14:52:16 +01:00
/// The name to get better RT flag errors
pub fn rt_err_ident() -> Ident {
Ident::new(
2021-12-25 13:17:16 +01:00
"you_must_enable_the_rt_feature_for_the_pac_in_your_cargo_toml",
2020-12-13 14:52:16 +01:00
Span::call_site(),
)
}