mirror of
https://github.com/rtic-rs/rtic.git
synced 2024-11-24 04:32:52 +01:00
Collect and generate required use-statements
This commit is contained in:
parent
dcc31fb884
commit
9fd052b876
6 changed files with 57 additions and 12 deletions
|
@ -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::{
|
||||
|
@ -23,6 +23,8 @@ pub fn codegen(
|
|||
Vec<TokenStream2>,
|
||||
// user_idle
|
||||
Option<TokenStream2>,
|
||||
// user_idle_imports
|
||||
Vec<TokenStream2>,
|
||||
// call_idle
|
||||
TokenStream2,
|
||||
) {
|
||||
|
@ -34,15 +36,25 @@ pub fn codegen(
|
|||
let mut locals_pat = None;
|
||||
let mut locals_new = None;
|
||||
|
||||
let mut user_idle_imports = vec![];
|
||||
|
||||
let name = &idle.name;
|
||||
|
||||
if !idle.args.resources.is_empty() {
|
||||
let (item, constructor) =
|
||||
resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis);
|
||||
|
||||
root_idle.push(item);
|
||||
const_app = Some(constructor);
|
||||
|
||||
let name_resource = format_ident!("{}Resources", name);
|
||||
user_idle_imports.push(quote!(
|
||||
#[allow(non_snake_case)]
|
||||
use super::#name_resource;
|
||||
));
|
||||
|
||||
}
|
||||
|
||||
let name = &idle.name;
|
||||
if !idle.locals.is_empty() {
|
||||
let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app);
|
||||
|
||||
|
@ -66,6 +78,12 @@ pub fn codegen(
|
|||
#(#stmts)*
|
||||
}
|
||||
));
|
||||
user_idle_imports.push(quote!(
|
||||
#(#attrs)*
|
||||
#[allow(non_snake_case)]
|
||||
#cfg_core
|
||||
use super::#name;
|
||||
));
|
||||
|
||||
let locals_new = locals_new.iter();
|
||||
let call_idle = quote!(crate::#name(
|
||||
|
@ -73,12 +91,13 @@ pub fn codegen(
|
|||
#name::Context::new(&rtic::export::Priority::new(0))
|
||||
));
|
||||
|
||||
(const_app, root_idle, user_idle, call_idle)
|
||||
(const_app, root_idle, user_idle, user_idle_imports, call_idle)
|
||||
} else {
|
||||
(
|
||||
None,
|
||||
vec![],
|
||||
None,
|
||||
vec![],
|
||||
quote!(loop {
|
||||
rtic::export::wfi()
|
||||
}),
|
||||
|
|
|
@ -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<TokenStream2>,
|
||||
// user_init -- the `#[init]` function written by the user
|
||||
Option<TokenStream2>,
|
||||
// user_init_imports -- the imports for `#[init]` functio written by the user
|
||||
Vec<TokenStream2>,
|
||||
// call_init -- the call to the user `#[init]` if there's one
|
||||
Option<TokenStream2>,
|
||||
) {
|
||||
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -89,7 +89,7 @@ pub fn codegen(
|
|||
#[allow(non_camel_case_types)]
|
||||
#(#cfgs)*
|
||||
#cfg_core
|
||||
use super::#name;
|
||||
use super::resources::#name;
|
||||
));
|
||||
|
||||
const_app.push(util::impl_mutex(
|
||||
|
@ -107,6 +107,11 @@ pub fn codegen(
|
|||
let mod_resources = if mod_resources.is_empty() {
|
||||
quote!()
|
||||
} else {
|
||||
// Also import the resource module
|
||||
mod_resources_imports.push(quote!(
|
||||
use super::resources;
|
||||
));
|
||||
|
||||
quote!(mod resources {
|
||||
use rtic::export::Priority;
|
||||
|
||||
|
|
|
@ -165,7 +165,7 @@ pub fn codegen(
|
|||
let constructor = quote!(
|
||||
impl<#lt> #ident<#lt> {
|
||||
#[inline(always)]
|
||||
unsafe fn new(#arg) -> Self {
|
||||
pub unsafe fn new(#arg) -> Self {
|
||||
#ident {
|
||||
#(#values,)*
|
||||
}
|
||||
|
|
|
@ -145,14 +145,13 @@ pub fn codegen(
|
|||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#[allow(non_snake_case)]
|
||||
fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) {
|
||||
pub fn #name(#(#locals_pat,)* #context: #name::Context #(,#inputs)*) {
|
||||
use rtic::Mutex as _;
|
||||
|
||||
#(#stmts)*
|
||||
}
|
||||
));
|
||||
software_tasks_imports.push(quote!(
|
||||
#(#attrs)*
|
||||
#(#cfgs)*
|
||||
#[allow(non_snake_case)]
|
||||
use super::#name;
|
||||
|
|
|
@ -40,7 +40,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
|
|||
|
||||
methods.push(quote!(
|
||||
#(#cfgs)*
|
||||
fn #name(&self #(,#args)*) -> Result<(), #ty> {
|
||||
pub fn #name(&self #(,#args)*) -> Result<(), #ty> {
|
||||
#let_instant
|
||||
#body
|
||||
}
|
||||
|
@ -92,7 +92,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
|
|||
methods.push(quote!(
|
||||
#(#cfgs)*
|
||||
#[inline(always)]
|
||||
fn #name(&self #(,#args)*) -> Result<(), #ty> {
|
||||
pub fn #name(&self #(,#args)*) -> Result<(), #ty> {
|
||||
unsafe {
|
||||
#let_instant
|
||||
#spawn(self.priority() #instant #(,#untupled)*)
|
||||
|
|
Loading…
Reference in a new issue