From 9fd052b876581e5aef3442b17c17b0b597f84a47 Mon Sep 17 00:00:00 2001 From: Henrik Tjäder Date: Tue, 26 May 2020 10:52:10 +0000 Subject: Collect and generate required use-statements --- macros/src/codegen/init.rs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index e0b7d699..88a7a23b 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::quote; +use quote::{quote, format_ident}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -24,6 +24,8 @@ pub fn codegen( Vec, // user_init -- the `#[init]` function written by the user Option, + // user_init_imports -- the imports for `#[init]` functio written by the user + Vec, // call_init -- the call to the user `#[init]` if there's one Option, ) { @@ -34,6 +36,8 @@ pub fn codegen( let mut root_init = vec![]; + let mut user_init_imports = vec![]; + let ret = { let late_fields = analysis .late_resources @@ -62,6 +66,12 @@ pub fn codegen( } )); + let name_late = format_ident!("{}LateResources", name); + user_init_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_late; + )); + Some(quote!(-> #name::LateResources)) } else { None @@ -89,6 +99,12 @@ pub fn codegen( #(#stmts)* } )); + user_init_imports.push(quote!( + #(#attrs)* + #cfg_core + #[allow(non_snake_case)] + use super::#name; + )); let mut const_app = None; if !init.args.resources.is_empty() { @@ -97,6 +113,12 @@ pub fn codegen( root_init.push(item); const_app = Some(constructor); + + let name_late = format_ident!("{}Resources", name); + user_init_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_late; + )); } let locals_new = locals_new.iter(); @@ -106,8 +128,8 @@ pub fn codegen( root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); - (const_app, root_init, user_init, call_init) + (const_app, root_init, user_init, user_init_imports, call_init) } else { - (None, vec![], None, None) + (None, vec![], None, vec![], None) } } -- cgit v1.2.3 From a151974245a994ec4c30bb0518677c4b99dce7e9 Mon Sep 17 00:00:00 2001 From: Henrik Tjäder Date: Tue, 8 Sep 2020 15:22:32 +0000 Subject: cfg_core is gone, cargo fmt --- macros/src/codegen.rs | 26 ++++++++++++++++++-------- macros/src/codegen/hardware_tasks.rs | 3 +-- macros/src/codegen/idle.rs | 12 ++++++++---- macros/src/codegen/init.rs | 11 ++++++++--- macros/src/codegen/resources.rs | 1 - macros/src/codegen/software_tasks.rs | 2 +- 6 files changed, 36 insertions(+), 19 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index fde9490b..e45f1a38 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -37,11 +37,13 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let pre_init_stmts = pre_init::codegen(&app, analysis, extra); - let (const_app_init, root_init, user_init, user_init_imports, call_init) = init::codegen(app, analysis, extra); + let (const_app_init, root_init, user_init, user_init_imports, call_init) = + init::codegen(app, analysis, extra); let post_init_stmts = post_init::codegen(&app, analysis); - let (const_app_idle, root_idle, user_idle, user_idle_imports, call_idle) = idle::codegen(app, analysis, extra); + let (const_app_idle, root_idle, user_idle, user_idle_imports, call_idle) = + idle::codegen(app, analysis, extra); if user_init.is_some() { const_app_imports.push(quote!( @@ -95,13 +97,22 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { } )); - let (const_app_resources, mod_resources, mod_resources_imports) = resources::codegen(app, analysis, extra); + let (const_app_resources, mod_resources, mod_resources_imports) = + resources::codegen(app, analysis, extra); - let (const_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks, user_hardware_tasks_imports) = - hardware_tasks::codegen(app, analysis, extra); + let ( + const_app_hardware_tasks, + root_hardware_tasks, + user_hardware_tasks, + user_hardware_tasks_imports, + ) = hardware_tasks::codegen(app, analysis, extra); - let (const_app_software_tasks, root_software_tasks, user_software_tasks, user_software_tasks_imports) = - software_tasks::codegen(app, analysis, extra); + let ( + const_app_software_tasks, + root_software_tasks, + user_software_tasks, + user_software_tasks_imports, + ) = software_tasks::codegen(app, analysis, extra); let const_app_dispatchers = dispatchers::codegen(app, analysis, extra); @@ -111,7 +122,6 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let const_app_schedule = schedule::codegen(app, extra); - let user_imports = app.user_imports.clone(); let name = &app.name; let device = extra.device; diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index 8f2ab2f6..a03fd779 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::{quote, format_ident}; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -128,7 +128,6 @@ pub fn codegen( #[allow(non_snake_case)] use super::#name; )); - } (const_app, root, user_tasks, hardware_tasks_imports) diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index db454a58..2aa99751 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::{quote, format_ident}; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -52,7 +52,6 @@ pub fn codegen( #[allow(non_snake_case)] use super::#name_resource; )); - } if !idle.locals.is_empty() { @@ -81,7 +80,6 @@ pub fn codegen( user_idle_imports.push(quote!( #(#attrs)* #[allow(non_snake_case)] - #cfg_core use super::#name; )); @@ -91,7 +89,13 @@ pub fn codegen( #name::Context::new(&rtic::export::Priority::new(0)) )); - (const_app, root_idle, user_idle, user_idle_imports, call_idle) + ( + const_app, + root_idle, + user_idle, + user_idle_imports, + call_idle, + ) } else { ( None, diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 88a7a23b..b350298c 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::{quote, format_ident}; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ @@ -101,7 +101,6 @@ pub fn codegen( )); user_init_imports.push(quote!( #(#attrs)* - #cfg_core #[allow(non_snake_case)] use super::#name; )); @@ -128,7 +127,13 @@ pub fn codegen( root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); - (const_app, root_init, user_init, user_init_imports, call_init) + ( + const_app, + root_init, + user_init, + user_init_imports, + call_init, + ) } else { (None, vec![], None, vec![], None) } diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index a326e68c..d18a4655 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -88,7 +88,6 @@ pub fn codegen( mod_resources_imports.push(quote!( #[allow(non_camel_case_types)] #(#cfgs)* - #cfg_core use super::resources::#name; )); diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 854c083d..9e011152 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -1,5 +1,5 @@ use proc_macro2::TokenStream as TokenStream2; -use quote::{quote, format_ident}; +use quote::{format_ident, quote}; use rtic_syntax::{ast::App, Context}; use crate::{ -- cgit v1.2.3 From 96e6350c0dfae37c3ea8032b4cc3113e37323ae5 Mon Sep 17 00:00:00 2001 From: Henrik Tjäder Date: Thu, 1 Oct 2020 16:17:15 +0000 Subject: Rename const_app to mod_app --- macros/src/codegen.rs | 50 ++++++++++++++++++------------------ macros/src/codegen/hardware_tasks.rs | 10 ++++---- macros/src/codegen/idle.rs | 14 +++------- macros/src/codegen/init.rs | 14 +++------- macros/src/codegen/resources.rs | 10 ++++---- macros/src/codegen/software_tasks.rs | 18 ++++++------- 6 files changed, 52 insertions(+), 64 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 26d7dc2d..f230d395 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -25,8 +25,8 @@ mod util; // TODO document the syntax here or in `rtic-syntax` pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { - let mut const_app = vec![]; - let mut const_app_imports = vec![]; + let mut mod_app = vec![]; + let mut mod_app_imports = vec![]; let mut mains = vec![]; let mut root = vec![]; let mut user = vec![]; @@ -37,21 +37,21 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { let pre_init_stmts = pre_init::codegen(&app, analysis, extra); - let (const_app_init, root_init, user_init, user_init_imports, call_init) = + let (mod_app_init, root_init, user_init, user_init_imports, call_init) = init::codegen(app, analysis, extra); let post_init_stmts = post_init::codegen(&app, analysis); - let (const_app_idle, root_idle, user_idle, user_idle_imports, call_idle) = + let (mod_app_idle, root_idle, user_idle, user_idle_imports, call_idle) = idle::codegen(app, analysis, extra); if user_init.is_some() { - const_app_imports.push(quote!( + mod_app_imports.push(quote!( use super::init; )) } if user_idle.is_some() { - const_app_imports.push(quote!( + mod_app_imports.push(quote!( use super::idle; )) } @@ -73,10 +73,10 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#root_idle)* )); - const_app.push(quote!( - #const_app_init + mod_app.push(quote!( + #mod_app_init - #const_app_idle + #mod_app_idle )); let main = util::suffixed("main"); @@ -97,30 +97,30 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { } )); - let (const_app_resources, mod_resources, mod_resources_imports) = + let (mod_app_resources, mod_resources, mod_resources_imports) = resources::codegen(app, analysis, extra); let ( - const_app_hardware_tasks, + mod_app_hardware_tasks, root_hardware_tasks, user_hardware_tasks, user_hardware_tasks_imports, ) = hardware_tasks::codegen(app, analysis, extra); let ( - const_app_software_tasks, + mod_app_software_tasks, root_software_tasks, user_software_tasks, user_software_tasks_imports, ) = software_tasks::codegen(app, analysis, extra); - let const_app_dispatchers = dispatchers::codegen(app, analysis, extra); + let mod_app_dispatchers = dispatchers::codegen(app, analysis, extra); - let const_app_spawn = spawn::codegen(app, analysis, extra); + let mod_app_spawn = spawn::codegen(app, analysis, extra); - let const_app_timer_queue = timer_queue::codegen(app, analysis, extra); + let mod_app_timer_queue = timer_queue::codegen(app, analysis, extra); - let const_app_schedule = schedule::codegen(app, extra); + let mod_app_schedule = schedule::codegen(app, extra); let user_imports = app.user_imports.clone(); let user_code = app.user_code.clone(); @@ -159,22 +159,22 @@ pub fn app(app: &App, analysis: &Analysis, extra: &Extra) -> TokenStream2 { #(#mod_resources_imports)* - /// Const app - #(#const_app)* + /// app module + #(#mod_app)* - #(#const_app_resources)* + #(#mod_app_resources)* - #(#const_app_hardware_tasks)* + #(#mod_app_hardware_tasks)* - #(#const_app_software_tasks)* + #(#mod_app_software_tasks)* - #(#const_app_dispatchers)* + #(#mod_app_dispatchers)* - #(#const_app_spawn)* + #(#mod_app_spawn)* - #(#const_app_timer_queue)* + #(#mod_app_timer_queue)* - #(#const_app_schedule)* + #(#mod_app_schedule)* #(#mains)* } diff --git a/macros/src/codegen/hardware_tasks.rs b/macros/src/codegen/hardware_tasks.rs index a03fd779..25f1df41 100644 --- a/macros/src/codegen/hardware_tasks.rs +++ b/macros/src/codegen/hardware_tasks.rs @@ -14,7 +14,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_hardware_tasks -- interrupt handlers and `${task}Resources` constructors + // mod_app_hardware_tasks -- interrupt handlers and `${task}Resources` constructors Vec, // root_hardware_tasks -- items that must be placed in the root of the crate: // - `${task}Locals` structs @@ -26,7 +26,7 @@ pub fn codegen( // user_hardware_tasks_imports -- the imports for `#[task]` functions written by the user Vec, ) { - let mut const_app = vec![]; + let mut mod_app = vec![]; let mut root = vec![]; let mut user_tasks = vec![]; let mut hardware_tasks_imports = vec![]; @@ -52,7 +52,7 @@ pub fn codegen( let symbol = task.args.binds.clone(); let priority = task.args.priority; - const_app.push(quote!( + mod_app.push(quote!( #[allow(non_snake_case)] #[no_mangle] unsafe fn #symbol() { @@ -90,7 +90,7 @@ pub fn codegen( root.push(item); - const_app.push(constructor); + mod_app.push(constructor); } root.push(module::codegen( @@ -130,5 +130,5 @@ pub fn codegen( )); } - (const_app, root, user_tasks, hardware_tasks_imports) + (mod_app, root, user_tasks, hardware_tasks_imports) } diff --git a/macros/src/codegen/idle.rs b/macros/src/codegen/idle.rs index 2aa99751..2e2932d7 100644 --- a/macros/src/codegen/idle.rs +++ b/macros/src/codegen/idle.rs @@ -14,7 +14,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_idle -- the `${idle}Resources` constructor + // mod_app_idle -- the `${idle}Resources` constructor Option, // root_idle -- items that must be placed in the root of the crate: // - the `${idle}Locals` struct @@ -31,7 +31,7 @@ pub fn codegen( if app.idles.len() > 0 { let idle = &app.idles.first().unwrap(); let mut needs_lt = false; - let mut const_app = None; + let mut mod_app = None; let mut root_idle = vec![]; let mut locals_pat = None; let mut locals_new = None; @@ -45,7 +45,7 @@ pub fn codegen( resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis); root_idle.push(item); - const_app = Some(constructor); + mod_app = Some(constructor); let name_resource = format_ident!("{}Resources", name); user_idle_imports.push(quote!( @@ -89,13 +89,7 @@ pub fn codegen( #name::Context::new(&rtic::export::Priority::new(0)) )); - ( - const_app, - root_idle, - user_idle, - user_idle_imports, - call_idle, - ) + (mod_app, root_idle, user_idle, user_idle_imports, call_idle) } else { ( None, diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index b350298c..77d186e5 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -14,7 +14,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_idle -- the `${init}Resources` constructor + // mod_app_idle -- the `${init}Resources` constructor Option, // root_init -- items that must be placed in the root of the crate: // - the `${init}Locals` struct @@ -105,13 +105,13 @@ pub fn codegen( use super::#name; )); - let mut const_app = None; + let mut mod_app = None; if !init.args.resources.is_empty() { let (item, constructor) = resources_struct::codegen(Context::Init, 0, &mut needs_lt, app, analysis); root_init.push(item); - const_app = Some(constructor); + mod_app = Some(constructor); let name_late = format_ident!("{}Resources", name); user_init_imports.push(quote!( @@ -127,13 +127,7 @@ pub fn codegen( root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); - ( - const_app, - root_init, - user_init, - user_init_imports, - call_init, - ) + (mod_app, root_init, user_init, user_init_imports, call_init) } else { (None, vec![], None, vec![], None) } diff --git a/macros/src/codegen/resources.rs b/macros/src/codegen/resources.rs index d18a4655..38ea5245 100644 --- a/macros/src/codegen/resources.rs +++ b/macros/src/codegen/resources.rs @@ -10,14 +10,14 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app -- the `static [mut]` variables behind the proxies + // mod_app -- the `static [mut]` variables behind the proxies Vec, // mod_resources -- the `resources` module TokenStream2, // mod_resources_imports -- the `resources` module imports Vec, ) { - let mut const_app = vec![]; + let mut mod_app = vec![]; let mut mod_resources = vec![]; let mut mod_resources_imports = vec![]; @@ -42,7 +42,7 @@ pub fn codegen( }; let attrs = &res.attrs; - const_app.push(quote!( + mod_app.push(quote!( #[allow(non_upper_case_globals)] #(#attrs)* #(#cfgs)* @@ -91,7 +91,7 @@ pub fn codegen( use super::resources::#name; )); - const_app.push(util::impl_mutex( + mod_app.push(util::impl_mutex( extra, cfgs, true, @@ -118,5 +118,5 @@ pub fn codegen( }) }; - (const_app, mod_resources, mod_resources_imports) + (mod_app, mod_resources, mod_resources_imports) } diff --git a/macros/src/codegen/software_tasks.rs b/macros/src/codegen/software_tasks.rs index 9e011152..4ae37e4e 100644 --- a/macros/src/codegen/software_tasks.rs +++ b/macros/src/codegen/software_tasks.rs @@ -13,7 +13,7 @@ pub fn codegen( analysis: &Analysis, extra: &Extra, ) -> ( - // const_app_software_tasks -- free queues, buffers and `${task}Resources` constructors + // mod_app_software_tasks -- free queues, buffers and `${task}Resources` constructors Vec, // root_software_tasks -- items that must be placed in the root of the crate: // - `${task}Locals` structs @@ -25,7 +25,7 @@ pub fn codegen( // user_software_tasks_imports -- the imports for `#[task]` functions written by the user Vec, ) { - let mut const_app = vec![]; + let mut mod_app = vec![]; let mut root = vec![]; let mut user_tasks = vec![]; let mut software_tasks_imports = vec![]; @@ -51,7 +51,7 @@ pub fn codegen( Box::new(|| util::link_section_uninit(true)), ) }; - const_app.push(quote!( + mod_app.push(quote!( /// Queue version of a free-list that keeps track of empty slots in /// the following buffers static mut #fq: #fq_ty = #fq_expr; @@ -59,13 +59,13 @@ pub fn codegen( // Generate a resource proxy if needed if let Some(ceiling) = ceiling { - const_app.push(quote!( + mod_app.push(quote!( struct #fq<'a> { priority: &'a rtic::export::Priority, } )); - const_app.push(util::impl_mutex( + mod_app.push(util::impl_mutex( extra, &[], false, @@ -85,7 +85,7 @@ pub fn codegen( let instants = util::instants_ident(name); let uninit = mk_uninit(); - const_app.push(quote!( + mod_app.push(quote!( #uninit /// Buffer that holds the instants associated to the inputs of a task static mut #instants: @@ -96,7 +96,7 @@ pub fn codegen( let uninit = mk_uninit(); let inputs = util::inputs_ident(name); - const_app.push(quote!( + mod_app.push(quote!( #uninit /// Buffer that holds the inputs of a task static mut #inputs: [core::mem::MaybeUninit<#input_ty>; #cap_lit] = @@ -124,7 +124,7 @@ pub fn codegen( root.push(item); - const_app.push(constructor); + mod_app.push(constructor); } // `${task}Locals` @@ -165,5 +165,5 @@ pub fn codegen( )); } - (const_app, root, user_tasks, software_tasks_imports) + (mod_app, root, user_tasks, software_tasks_imports) } -- cgit v1.2.3 From e7f0d9c3e3fad77dace2ce63af02559fda46cb73 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Thu, 1 Oct 2020 19:38:49 +0200 Subject: Now late resources are always used --- examples/baseline.rs | 4 +++- examples/binds.rs | 4 +++- examples/capacity.rs | 4 +++- examples/cfg.rs | 4 +++- examples/destructure.rs | 4 +++- examples/generics.rs | 4 +++- examples/hardware.rs | 4 +++- examples/idle.rs | 4 +++- examples/init.rs | 4 +++- examples/lock.rs | 4 +++- examples/message.rs | 4 +++- examples/not-send.rs | 4 +++- examples/not-sync.rs | 4 +++- examples/periodic.rs | 4 +++- examples/peripherals-taken.rs | 4 +++- examples/pool.rs | 4 +++- examples/preempt.rs | 4 +++- examples/ramfunc.rs | 4 +++- examples/resource.rs | 4 +++- examples/schedule.rs | 4 +++- examples/shared-with-init.rs | 4 +++- examples/t-binds.rs | 4 +++- examples/t-cfg.rs | 4 +++- examples/t-htask-main.rs | 6 +++-- examples/t-idle-main.rs | 4 +++- examples/t-init-main.rs | 4 +++- examples/t-resource.rs | 4 +++- examples/t-schedule.rs | 4 +++- examples/t-spawn.rs | 4 +++- examples/t-stask-main.rs | 4 +++- examples/task.rs | 4 +++- examples/types.rs | 4 +++- macros/Cargo.toml | 2 +- macros/src/codegen/init.rs | 52 ++++++++++++++++++------------------------- macros/src/codegen/module.rs | 12 +++++----- 35 files changed, 125 insertions(+), 71 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/examples/baseline.rs b/examples/baseline.rs index f46b273d..5a6dbd4c 100644 --- a/examples/baseline.rs +++ b/examples/baseline.rs @@ -13,13 +13,15 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { #[init(spawn = [foo])] - fn init(cx: init::Context) { + fn init(cx: init::Context) -> init::LateResources { // omitted: initialization of `CYCCNT` hprintln!("init(baseline = {:?})", cx.start).unwrap(); // `foo` inherits the baseline of `init`: `Instant(0)` cx.spawn.foo().unwrap(); + + init::LateResources {} } #[task(schedule = [foo])] diff --git a/examples/binds.rs b/examples/binds.rs index 82bf8964..f3ce51ec 100644 --- a/examples/binds.rs +++ b/examples/binds.rs @@ -13,10 +13,12 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::UART0); hprintln!("init").unwrap(); + + init::LateResources {} } #[idle] diff --git a/examples/capacity.rs b/examples/capacity.rs index 00cec344..cac0029c 100644 --- a/examples/capacity.rs +++ b/examples/capacity.rs @@ -12,8 +12,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::UART0); + + init::LateResources {} } #[task(binds = UART0, spawn = [foo, bar])] diff --git a/examples/cfg.rs b/examples/cfg.rs index 8eeeb2a9..4f467247 100644 --- a/examples/cfg.rs +++ b/examples/cfg.rs @@ -19,9 +19,11 @@ const APP: () = { } #[init(spawn = [foo])] - fn init(cx: init::Context) { + fn init(cx: init::Context) -> init::LateResources { cx.spawn.foo().unwrap(); cx.spawn.foo().unwrap(); + + init::LateResources {} } #[idle] diff --git a/examples/destructure.rs b/examples/destructure.rs index 1756bd9e..ad1d8594 100644 --- a/examples/destructure.rs +++ b/examples/destructure.rs @@ -22,9 +22,11 @@ const APP: () = { } #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::UART0); rtic::pend(Interrupt::UART1); + + init::LateResources {} } // Direct destructure diff --git a/examples/generics.rs b/examples/generics.rs index 40ab81ac..65c5db02 100644 --- a/examples/generics.rs +++ b/examples/generics.rs @@ -18,9 +18,11 @@ const APP: () = { } #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::UART0); rtic::pend(Interrupt::UART1); + + init::LateResources {} } #[task(binds = UART0, resources = [shared])] diff --git a/examples/hardware.rs b/examples/hardware.rs index 8105a742..30de77ab 100644 --- a/examples/hardware.rs +++ b/examples/hardware.rs @@ -12,12 +12,14 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { // Pends the UART0 interrupt but its handler won't run until *after* // `init` returns because interrupts are disabled rtic::pend(Interrupt::UART0); // equivalent to NVIC::pend hprintln!("init").unwrap(); + + init::LateResources {} } #[idle] diff --git a/examples/idle.rs b/examples/idle.rs index 3d28dac8..b029fcae 100644 --- a/examples/idle.rs +++ b/examples/idle.rs @@ -11,8 +11,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { hprintln!("init").unwrap(); + + init::LateResources {} } #[idle] diff --git a/examples/init.rs b/examples/init.rs index 315969f0..d5cebbaa 100644 --- a/examples/init.rs +++ b/examples/init.rs @@ -11,7 +11,7 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965, peripherals = true)] const APP: () = { #[init] - fn init(cx: init::Context) { + fn init(cx: init::Context) -> init::LateResources { static mut X: u32 = 0; // Cortex-M peripherals @@ -26,5 +26,7 @@ const APP: () = { hprintln!("init").unwrap(); debug::exit(debug::EXIT_SUCCESS); + + init::LateResources {} } }; diff --git a/examples/lock.rs b/examples/lock.rs index 5e3bce25..ff947c53 100644 --- a/examples/lock.rs +++ b/examples/lock.rs @@ -17,8 +17,10 @@ const APP: () = { } #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::GPIOA); + + init::LateResources {} } // when omitted priority is assumed to be `1` diff --git a/examples/message.rs b/examples/message.rs index 596f2449..a1352c0e 100644 --- a/examples/message.rs +++ b/examples/message.rs @@ -11,8 +11,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init(spawn = [foo])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { c.spawn.foo(/* no message */).unwrap(); + + init::LateResources {} } #[task(spawn = [bar])] diff --git a/examples/not-send.rs b/examples/not-send.rs index 16a874dc..999abfaf 100644 --- a/examples/not-send.rs +++ b/examples/not-send.rs @@ -23,9 +23,11 @@ const APP: () = { } #[init(spawn = [baz, quux])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { c.spawn.baz().unwrap(); c.spawn.quux().unwrap(); + + init::LateResources {} } #[task(spawn = [bar])] diff --git a/examples/not-sync.rs b/examples/not-sync.rs index a7eaac8e..5a67489f 100644 --- a/examples/not-sync.rs +++ b/examples/not-sync.rs @@ -22,8 +22,10 @@ const APP: () = { } #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { debug::exit(debug::EXIT_SUCCESS); + + init::LateResources {} } #[task(resources = [&shared])] diff --git a/examples/periodic.rs b/examples/periodic.rs index 405346e3..da56d468 100644 --- a/examples/periodic.rs +++ b/examples/periodic.rs @@ -15,10 +15,12 @@ const PERIOD: u32 = 8_000_000; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo])] - fn init(cx: init::Context) { + fn init(cx: init::Context) -> init::LateResources { // omitted: initialization of `CYCCNT` cx.schedule.foo(cx.start + PERIOD.cycles()).unwrap(); + + init::LateResources {} } #[task(schedule = [foo])] diff --git a/examples/peripherals-taken.rs b/examples/peripherals-taken.rs index cd4ba0f0..42ad8c0f 100644 --- a/examples/peripherals-taken.rs +++ b/examples/peripherals-taken.rs @@ -9,8 +9,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn main(_: main::Context) { + fn main(_: main::Context) -> main::LateResources { assert!(cortex_m::Peripherals::take().is_none()); debug::exit(debug::EXIT_SUCCESS); + + main::LateResources {} } }; diff --git a/examples/pool.rs b/examples/pool.rs index 824d5bd8..9fccdf84 100644 --- a/examples/pool.rs +++ b/examples/pool.rs @@ -20,13 +20,15 @@ pool!(P: [u8; 128]); #[app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { static mut MEMORY: [u8; 512] = [0; 512]; // Increase the capacity of the memory pool by ~4 P::grow(MEMORY); rtic::pend(Interrupt::I2C0); + + init::LateResources {} } #[task(binds = I2C0, priority = 2, spawn = [foo, bar])] diff --git a/examples/preempt.rs b/examples/preempt.rs index 3cb11029..7103b17b 100644 --- a/examples/preempt.rs +++ b/examples/preempt.rs @@ -11,8 +11,10 @@ use rtic::app; #[app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::GPIOA); + + init::LateResources {} } #[task(binds = GPIOA, priority = 1)] diff --git a/examples/ramfunc.rs b/examples/ramfunc.rs index 1f95d496..214b7e67 100644 --- a/examples/ramfunc.rs +++ b/examples/ramfunc.rs @@ -11,8 +11,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init(spawn = [bar])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { c.spawn.bar().unwrap(); + + init::LateResources {} } #[inline(never)] diff --git a/examples/resource.rs b/examples/resource.rs index 2361fd00..06aa9756 100644 --- a/examples/resource.rs +++ b/examples/resource.rs @@ -18,9 +18,11 @@ const APP: () = { } #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { rtic::pend(Interrupt::UART0); rtic::pend(Interrupt::UART1); + + init::LateResources {} } // `shared` cannot be accessed from this context diff --git a/examples/schedule.rs b/examples/schedule.rs index 70a7a5e3..b76d9e7f 100644 --- a/examples/schedule.rs +++ b/examples/schedule.rs @@ -14,7 +14,7 @@ use rtic::cyccnt::{Instant, U32Ext as _}; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo, bar])] - fn init(mut cx: init::Context) { + fn init(mut cx: init::Context) -> init::LateResources { // Initialize (enable) the monotonic timer (CYCCNT) cx.core.DCB.enable_trace(); // required on Cortex-M7 devices that software lock the DWT (e.g. STM32F7) @@ -32,6 +32,8 @@ const APP: () = { // Schedule `bar` to run 4e6 cycles in the future cx.schedule.bar(now + 4_000_000.cycles()).unwrap(); + + init::LateResources {} } #[task] diff --git a/examples/shared-with-init.rs b/examples/shared-with-init.rs index bd55f7ef..fa900a26 100644 --- a/examples/shared-with-init.rs +++ b/examples/shared-with-init.rs @@ -20,12 +20,14 @@ const APP: () = { } #[init(resources = [shared])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { // this `message` will be sent to task `UART0` let message = MustBeSend; *c.resources.shared = Some(message); rtic::pend(Interrupt::UART0); + + init::LateResources {} } #[task(binds = UART0, resources = [shared])] diff --git a/examples/t-binds.rs b/examples/t-binds.rs index 588ac46f..edf0fc69 100644 --- a/examples/t-binds.rs +++ b/examples/t-binds.rs @@ -10,7 +10,9 @@ use panic_halt as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) {} + fn init(_: init::Context) -> init::LateResources { + init::LateResources {} + } // Cortex-M exception #[task(binds = SVCall)] diff --git a/examples/t-cfg.rs b/examples/t-cfg.rs index b6c9e472..254cb8e0 100644 --- a/examples/t-cfg.rs +++ b/examples/t-cfg.rs @@ -14,9 +14,11 @@ const APP: () = { } #[init] - fn init(_: init::Context) { + fn init(_: init::Context) -> init::LateResources { #[cfg(never)] static mut BAR: u32 = 0; + + init::LateResources {} } #[idle] diff --git a/examples/t-htask-main.rs b/examples/t-htask-main.rs index c4bebf94..885019a1 100644 --- a/examples/t-htask-main.rs +++ b/examples/t-htask-main.rs @@ -9,8 +9,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) { - rtic::pend(lm3s6965::Interrupt::UART0) + fn init(_: init::Context) -> init::LateResources { + rtic::pend(lm3s6965::Interrupt::UART0); + + init::LateResources {} } #[task(binds = UART0)] diff --git a/examples/t-idle-main.rs b/examples/t-idle-main.rs index 051a9ee8..3e06cac6 100644 --- a/examples/t-idle-main.rs +++ b/examples/t-idle-main.rs @@ -9,7 +9,9 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn init(_: init::Context) {} + fn init(_: init::Context) -> init::LateResources { + init::LateResources {} + } #[idle] fn main(_: main::Context) -> ! { diff --git a/examples/t-init-main.rs b/examples/t-init-main.rs index 6a6cd991..f6c1d9ca 100644 --- a/examples/t-init-main.rs +++ b/examples/t-init-main.rs @@ -9,7 +9,9 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init] - fn main(_: main::Context) { + fn main(_: main::Context) -> main::LateResources { debug::exit(debug::EXIT_SUCCESS); + + main::LateResources {} } }; diff --git a/examples/t-resource.rs b/examples/t-resource.rs index 81ba1856..78e518c1 100644 --- a/examples/t-resource.rs +++ b/examples/t-resource.rs @@ -31,7 +31,7 @@ const APP: () = { } #[init(resources = [o1, o4, o5, o6, s3])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { // owned by `init` == `&'static mut` let _: &'static mut u32 = c.resources.o1; @@ -42,6 +42,8 @@ const APP: () = { let _: &mut u32 = c.resources.o4; let _: &mut u32 = c.resources.o5; let _: &mut u32 = c.resources.s3; + + init::LateResources {} } #[idle(resources = [o2, &o4, s1, &s3])] diff --git a/examples/t-schedule.rs b/examples/t-schedule.rs index 3854aad3..8af01aba 100644 --- a/examples/t-schedule.rs +++ b/examples/t-schedule.rs @@ -11,10 +11,12 @@ use rtic::cyccnt::{Instant, U32Ext as _}; #[rtic::app(device = lm3s6965, monotonic = rtic::cyccnt::CYCCNT)] const APP: () = { #[init(schedule = [foo, bar, baz])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { let _: Result<(), ()> = c.schedule.foo(c.start + 10.cycles()); let _: Result<(), u32> = c.schedule.bar(c.start + 20.cycles(), 0); let _: Result<(), (u32, u32)> = c.schedule.baz(c.start + 30.cycles(), 0, 1); + + init::LateResources {} } #[idle(schedule = [foo, bar, baz])] diff --git a/examples/t-spawn.rs b/examples/t-spawn.rs index 35831ccf..af2a79ea 100644 --- a/examples/t-spawn.rs +++ b/examples/t-spawn.rs @@ -10,10 +10,12 @@ use panic_halt as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init(spawn = [foo, bar, baz])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { let _: Result<(), ()> = c.spawn.foo(); let _: Result<(), u32> = c.spawn.bar(0); let _: Result<(), (u32, u32)> = c.spawn.baz(0, 1); + + init::LateResources {} } #[idle(spawn = [foo, bar, baz])] diff --git a/examples/t-stask-main.rs b/examples/t-stask-main.rs index f2709404..aefd4821 100644 --- a/examples/t-stask-main.rs +++ b/examples/t-stask-main.rs @@ -9,8 +9,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init(spawn = [main])] - fn init(cx: init::Context) { + fn init(cx: init::Context) -> init::LateResources { cx.spawn.main().ok(); + + init::LateResources {} } #[task] diff --git a/examples/task.rs b/examples/task.rs index 12c4ac83..e148b356 100644 --- a/examples/task.rs +++ b/examples/task.rs @@ -11,8 +11,10 @@ use panic_semihosting as _; #[rtic::app(device = lm3s6965)] const APP: () = { #[init(spawn = [foo])] - fn init(c: init::Context) { + fn init(c: init::Context) -> init::LateResources { c.spawn.foo().unwrap(); + + init::LateResources {} } #[task(spawn = [bar, baz])] diff --git a/examples/types.rs b/examples/types.rs index 5233f868..46d08b83 100644 --- a/examples/types.rs +++ b/examples/types.rs @@ -17,7 +17,7 @@ const APP: () = { } #[init(schedule = [foo], spawn = [foo])] - fn init(cx: init::Context) { + fn init(cx: init::Context) -> init::LateResources { let _: cyccnt::Instant = cx.start; let _: rtic::Peripherals = cx.core; let _: lm3s6965::Peripherals = cx.device; @@ -25,6 +25,8 @@ const APP: () = { let _: init::Spawn = cx.spawn; debug::exit(debug::EXIT_SUCCESS); + + init::LateResources {} } #[idle(schedule = [foo], spawn = [foo])] diff --git a/macros/Cargo.toml b/macros/Cargo.toml index 610890bb..ec5c130c 100644 --- a/macros/Cargo.toml +++ b/macros/Cargo.toml @@ -21,5 +21,5 @@ proc-macro = true proc-macro2 = "1" quote = "1" syn = "1" -rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "master", version = "0.4.0" } +rtic-syntax = { git = "https://github.com/rtic-rs/rtic-syntax", branch = "always_late_resources" } diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index e0b7d699..b41c3894 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -34,39 +34,31 @@ pub fn codegen( let mut root_init = vec![]; - let ret = { - let late_fields = analysis - .late_resources - .iter() - .flat_map(|resources| { - resources.iter().map(|name| { - let ty = &app.late_resources[name].ty; - let cfgs = &app.late_resources[name].cfgs; - - quote!( - #(#cfgs)* - pub #name: #ty - ) - }) + let late_fields = analysis + .late_resources + .iter() + .flat_map(|resources| { + resources.iter().map(|name| { + let ty = &app.late_resources[name].ty; + let cfgs = &app.late_resources[name].cfgs; + + quote!( + #(#cfgs)* + pub #name: #ty + ) }) - .collect::>(); - - if !late_fields.is_empty() { - let late_resources = util::late_resources_ident(&name); + }) + .collect::>(); - root_init.push(quote!( - /// Resources initialized at runtime - #[allow(non_snake_case)] - pub struct #late_resources { - #(#late_fields),* - } - )); + let late_resources = util::late_resources_ident(&name); - Some(quote!(-> #name::LateResources)) - } else { - None + root_init.push(quote!( + /// Resources initialized at runtime + #[allow(non_snake_case)] + pub struct #late_resources { + #(#late_fields),* } - }; + )); let mut locals_pat = None; let mut locals_new = None; @@ -85,7 +77,7 @@ pub fn codegen( let user_init = Some(quote!( #(#attrs)* #[allow(non_snake_case)] - fn #name(#(#locals_pat,)* #context: #name::Context) #ret { + fn #name(#(#locals_pat,)* #context: #name::Context) -> #name::LateResources { #(#stmts)* } )); diff --git a/macros/src/codegen/module.rs b/macros/src/codegen/module.rs index 863f6c5b..85bab3ab 100644 --- a/macros/src/codegen/module.rs +++ b/macros/src/codegen/module.rs @@ -253,14 +253,12 @@ pub fn codegen(ctxt: Context, resources_tick: bool, app: &App, extra: &Extra) -> if let Context::Init = ctxt { let init = &app.inits.first().unwrap(); - if init.returns_late_resources { - let late_resources = util::late_resources_ident(&init.name); + let late_resources = util::late_resources_ident(&init.name); - items.push(quote!( - #[doc(inline)] - pub use super::#late_resources as LateResources; - )); - } + items.push(quote!( + #[doc(inline)] + pub use super::#late_resources as LateResources; + )); } let doc = match ctxt { -- cgit v1.2.3 From 8ab7be98714d1ef6298e6feb19f16b84cd9bb4e6 Mon Sep 17 00:00:00 2001 From: Emil Fresk Date: Mon, 5 Oct 2020 20:19:52 +0200 Subject: Added back accidentally removed block --- macros/src/codegen/init.rs | 6 ++++++ macros/src/lib.rs | 3 +-- 2 files changed, 7 insertions(+), 2 deletions(-) (limited to 'macros/src/codegen/init.rs') diff --git a/macros/src/codegen/init.rs b/macros/src/codegen/init.rs index 1ced68bc..8942439b 100644 --- a/macros/src/codegen/init.rs +++ b/macros/src/codegen/init.rs @@ -63,6 +63,12 @@ pub fn codegen( } )); + let name_late = format_ident!("{}LateResources", name); + user_init_imports.push(quote!( + #[allow(non_snake_case)] + use super::#name_late; + )); + let mut locals_pat = None; let mut locals_new = None; if !init.locals.is_empty() { diff --git a/macros/src/lib.rs b/macros/src/lib.rs index 94e7eec6..e659559e 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -35,8 +35,7 @@ mod tests; /// /// The items allowed in the module block are specified below: /// -/// # 1. `#[resources] -/// struct ` +/// # 1. `#[resources] struct ` /// /// This structure contains the declaration of all the resources used by the application. Each field /// in this structure corresponds to a different resource. Each resource may optionally be given an -- cgit v1.2.3