diff --git a/CHANGELOG.md b/CHANGELOG.md index aa2260e..8269a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ This project adheres to [Semantic Versioning](http://semver.org/). ### Added - CI changelog entry enforcer +- Support for externally defined #[init] and #[idle] ## [v1.0.0] - 2021-12-25 diff --git a/examples/extern_idle.rs b/examples/extern_idle.rs new file mode 100644 index 0000000..513a004 --- /dev/null +++ b/examples/extern_idle.rs @@ -0,0 +1,40 @@ +//! examples/extern_idle + +#[mock::app(parse_binds, dispatchers = [UART1])] +mod app { + // idle externally implemented + use crate::{bar, foo}; + + #[shared] + struct Shared { + a: u32, + } + + #[local] + struct Local {} + + #[init] + fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {} + + extern "Rust" { + // Software task + #[task(shared = [a], priority = 2)] + fn foo(_: foo::Context, _: u32); + + // Hardware task + #[task(binds = UART0, shared = [a], priority = 2)] + // #[inline(always)] // would be rejected + fn bar(_: bar::Context); + + // Externally defined idle task + #[idle()] + fn idle(_: idle::Context) -> !; + } +} + +// The actual functions to dispatch are +// defined outside of the mod `app`. +// +// fn foo(_: foo::Context, _: u32) {} +// fn bar(_: bar::Context, _: u32) {} +// fn idle(_: idle::Context) -> ! {} diff --git a/examples/extern_init.rs b/examples/extern_init.rs new file mode 100644 index 0000000..0f4ac4e --- /dev/null +++ b/examples/extern_init.rs @@ -0,0 +1,41 @@ +//! examples/extern_init + +#[mock::app(parse_binds, dispatchers = [UART1])] +mod app { + // init externally implemented + use crate::{bar, foo, idle, init}; + + #[shared] + struct Shared { + a: u32, + } + + #[local] + struct Local {} + + #[idle] + fn idle(_: idle::Context) -> ! {} + + extern "Rust" { + + // Externally defined init + #[init] + fn init(_: init::Context) -> (Shared, Local, init::Monotonics); + + // Software task + #[task(shared = [a], priority = 2)] + fn foo(_: foo::Context, _: u32); + + // Hardware task + #[task(binds = UART0, shared = [a], priority = 2)] + //#[inline(always)] // would be rejected + fn bar(_: bar::Context); + } +} + +// The actual functions to dispatch are +// defined outside of the mod `app`. +// +// fn foo(_: foo::Context, _: u32) {} +// fn bar(_: bar::Context, _: u32) {} +// fn init(_: init::Context) -> (Shared, Local, init::Monotonics) {} diff --git a/src/ast.rs b/src/ast.rs index d218eba..dbe5807 100644 --- a/src/ast.rs +++ b/src/ast.rs @@ -90,6 +90,9 @@ pub struct Init { /// The name of the user provided local resources struct pub user_local_struct: Ident, + + /// The task is declared externally + pub is_extern: bool, } /// `init` context metadata @@ -126,6 +129,9 @@ pub struct Idle { /// The statements that make up this `idle` function pub stmts: Vec, + + /// The task is declared externally + pub is_extern: bool, } /// `idle` context metadata diff --git a/src/parse/app.rs b/src/parse/app.rs index cd9f0dd..86f0cad 100644 --- a/src/parse/app.rs +++ b/src/parse/app.rs @@ -383,7 +383,46 @@ impl App { for item in mod_.items { if let ForeignItem::Fn(mut item) = item { let span = item.sig.ident.span(); + // Find externally defined #[init] tasks if let Some(pos) = item + .attrs + .iter() + .position(|attr| util::attr_eq(attr, "init")) + { + let args = InitArgs::parse(item.attrs.remove(pos).tokens)?; + + // If an init function already exists, error + if init.is_some() { + return Err(parse::Error::new( + span, + "`#[init]` function must appear at most once", + )); + } + + check_ident(&item.sig.ident)?; + + init = Some(Init::parse_foreign(args, item.clone())?); + + // Find externally defined #[idle] tasks + } else if let Some(pos) = item + .attrs + .iter() + .position(|attr| util::attr_eq(attr, "idle")) + { + let args = IdleArgs::parse(item.attrs.remove(pos).tokens)?; + + // If an idle function already exists, error + if idle.is_some() { + return Err(parse::Error::new( + span, + "`#[idle]` function must appear at most once", + )); + } + + check_ident(&item.sig.ident)?; + + idle = Some(Idle::parse_foreign(args, item.clone())?); + } else if let Some(pos) = item .attrs .iter() .position(|attr| util::attr_eq(attr, "task")) @@ -400,7 +439,7 @@ impl App { if item.attrs.len() != 1 { return Err(parse::Error::new( span, - "`extern` task required `#[task(..)]` attribute", + "`extern` tasks only supports one attribute: `#[task(..)]`", )); } @@ -430,14 +469,10 @@ impl App { } else { return Err(parse::Error::new( span, - "`extern` task required `#[task(..)]` attribute", + "`extern` task, init or idle must have either `#[task(..)]`, + `#[init(..)]` or `#[idle(..)]` attribute", )); } - } else { - return Err(parse::Error::new( - item.span(), - "this item must live outside the `#[app]` module", - )); } } } diff --git a/src/parse/hardware_task.rs b/src/parse/hardware_task.rs index e655505..5fb7a59 100644 --- a/src/parse/hardware_task.rs +++ b/src/parse/hardware_task.rs @@ -47,9 +47,6 @@ impl HardwareTask { ), )) } -} - -impl HardwareTask { pub(crate) fn parse_foreign( args: HardwareTaskArgs, item: ForeignItemFn, diff --git a/src/parse/idle.rs b/src/parse/idle.rs index a9c5577..26e3df1 100644 --- a/src/parse/idle.rs +++ b/src/parse/idle.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use syn::{parse, ItemFn}; +use syn::{parse, ForeignItemFn, ItemFn, Stmt}; use crate::{ ast::{Idle, IdleArgs}, @@ -29,6 +29,37 @@ impl Idle { context, name: item.sig.ident, stmts: item.block.stmts, + is_extern: false, + }); + } + } + } + + Err(parse::Error::new( + item.sig.ident.span(), + &format!( + "this `#[idle]` function must have signature `fn({}::Context) -> !`", + name + ), + )) + } + pub(crate) fn parse_foreign(args: IdleArgs, item: ForeignItemFn) -> parse::Result { + let valid_signature = util::check_foreign_fn_signature(&item) + && item.sig.inputs.len() == 1 + && util::type_is_bottom(&item.sig.output); + + let name = item.sig.ident.to_string(); + + if valid_signature { + if let Some((context, Ok(rest))) = util::parse_inputs(item.sig.inputs, &name) { + if rest.is_empty() { + return Ok(Idle { + args, + attrs: item.attrs, + context, + name: item.sig.ident, + stmts: Vec::::new(), + is_extern: true, }); } } diff --git a/src/parse/init.rs b/src/parse/init.rs index cf89c4e..bf694c8 100644 --- a/src/parse/init.rs +++ b/src/parse/init.rs @@ -1,6 +1,6 @@ use proc_macro2::TokenStream as TokenStream2; -use syn::{parse, ItemFn}; +use syn::{parse, ForeignItemFn, ItemFn, Stmt}; use crate::{ ast::{Init, InitArgs}, @@ -35,6 +35,43 @@ impl Init { stmts: item.block.stmts, user_shared_struct, user_local_struct, + is_extern: false, + }); + } + } + } + } + + Err(parse::Error::new( + span, + &format!( + "the `#[init]` function must have signature `fn({}::Context) -> (Shared resources struct, Local resources struct, {0}::Monotonics)`", + name + ), + )) + } + pub(crate) fn parse_foreign(args: InitArgs, item: ForeignItemFn) -> parse::Result { + let valid_signature = util::check_foreign_fn_signature(&item) && item.sig.inputs.len() == 1; + + let span = item.sig.ident.span(); + + let name = item.sig.ident.to_string(); + + if valid_signature { + if let Ok((user_shared_struct, user_local_struct)) = + util::type_is_init_return(&item.sig.output, &name) + { + if let Some((context, Ok(rest))) = util::parse_inputs(item.sig.inputs, &name) { + if rest.is_empty() { + return Ok(Init { + args, + attrs: item.attrs, + context, + name: item.sig.ident, + stmts: Vec::::new(), + user_shared_struct, + user_local_struct, + is_extern: true, }); } }