Collect and generate required use-statements

This commit is contained in:
Henrik Tjäder 2020-05-26 10:52:10 +00:00
parent dcc31fb884
commit 9fd052b876
6 changed files with 57 additions and 12 deletions

View file

@ -1,5 +1,5 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::{quote, format_ident};
use rtic_syntax::{ast::App, Context}; use rtic_syntax::{ast::App, Context};
use crate::{ use crate::{
@ -23,6 +23,8 @@ pub fn codegen(
Vec<TokenStream2>, Vec<TokenStream2>,
// user_idle // user_idle
Option<TokenStream2>, Option<TokenStream2>,
// user_idle_imports
Vec<TokenStream2>,
// call_idle // call_idle
TokenStream2, TokenStream2,
) { ) {
@ -34,15 +36,25 @@ pub fn codegen(
let mut locals_pat = None; let mut locals_pat = None;
let mut locals_new = None; let mut locals_new = None;
let mut user_idle_imports = vec![];
let name = &idle.name;
if !idle.args.resources.is_empty() { if !idle.args.resources.is_empty() {
let (item, constructor) = let (item, constructor) =
resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis); resources_struct::codegen(Context::Idle, 0, &mut needs_lt, app, analysis);
root_idle.push(item); root_idle.push(item);
const_app = Some(constructor); 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() { if !idle.locals.is_empty() {
let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app); let (locals, pat) = locals::codegen(Context::Idle, &idle.locals, app);
@ -66,6 +78,12 @@ pub fn codegen(
#(#stmts)* #(#stmts)*
} }
)); ));
user_idle_imports.push(quote!(
#(#attrs)*
#[allow(non_snake_case)]
#cfg_core
use super::#name;
));
let locals_new = locals_new.iter(); let locals_new = locals_new.iter();
let call_idle = quote!(crate::#name( let call_idle = quote!(crate::#name(
@ -73,12 +91,13 @@ pub fn codegen(
#name::Context::new(&rtic::export::Priority::new(0)) #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 { } else {
( (
None, None,
vec![], vec![],
None, None,
vec![],
quote!(loop { quote!(loop {
rtic::export::wfi() rtic::export::wfi()
}), }),

View file

@ -1,5 +1,5 @@
use proc_macro2::TokenStream as TokenStream2; use proc_macro2::TokenStream as TokenStream2;
use quote::quote; use quote::{quote, format_ident};
use rtic_syntax::{ast::App, Context}; use rtic_syntax::{ast::App, Context};
use crate::{ use crate::{
@ -24,6 +24,8 @@ pub fn codegen(
Vec<TokenStream2>, Vec<TokenStream2>,
// user_init -- the `#[init]` function written by the user // user_init -- the `#[init]` function written by the user
Option<TokenStream2>, 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 // call_init -- the call to the user `#[init]` if there's one
Option<TokenStream2>, Option<TokenStream2>,
) { ) {
@ -34,6 +36,8 @@ pub fn codegen(
let mut root_init = vec![]; let mut root_init = vec![];
let mut user_init_imports = vec![];
let ret = { let ret = {
let late_fields = analysis let late_fields = analysis
.late_resources .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)) Some(quote!(-> #name::LateResources))
} else { } else {
None None
@ -89,6 +99,12 @@ pub fn codegen(
#(#stmts)* #(#stmts)*
} }
)); ));
user_init_imports.push(quote!(
#(#attrs)*
#cfg_core
#[allow(non_snake_case)]
use super::#name;
));
let mut const_app = None; let mut const_app = None;
if !init.args.resources.is_empty() { if !init.args.resources.is_empty() {
@ -97,6 +113,12 @@ pub fn codegen(
root_init.push(item); root_init.push(item);
const_app = Some(constructor); 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(); let locals_new = locals_new.iter();
@ -106,8 +128,8 @@ pub fn codegen(
root_init.push(module::codegen(Context::Init, needs_lt, app, extra)); 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 { } else {
(None, vec![], None, None) (None, vec![], None, vec![], None)
} }
} }

View file

@ -89,7 +89,7 @@ pub fn codegen(
#[allow(non_camel_case_types)] #[allow(non_camel_case_types)]
#(#cfgs)* #(#cfgs)*
#cfg_core #cfg_core
use super::#name; use super::resources::#name;
)); ));
const_app.push(util::impl_mutex( const_app.push(util::impl_mutex(
@ -107,6 +107,11 @@ pub fn codegen(
let mod_resources = if mod_resources.is_empty() { let mod_resources = if mod_resources.is_empty() {
quote!() quote!()
} else { } else {
// Also import the resource module
mod_resources_imports.push(quote!(
use super::resources;
));
quote!(mod resources { quote!(mod resources {
use rtic::export::Priority; use rtic::export::Priority;

View file

@ -165,7 +165,7 @@ pub fn codegen(
let constructor = quote!( let constructor = quote!(
impl<#lt> #ident<#lt> { impl<#lt> #ident<#lt> {
#[inline(always)] #[inline(always)]
unsafe fn new(#arg) -> Self { pub unsafe fn new(#arg) -> Self {
#ident { #ident {
#(#values,)* #(#values,)*
} }

View file

@ -145,14 +145,13 @@ pub fn codegen(
#(#attrs)* #(#attrs)*
#(#cfgs)* #(#cfgs)*
#[allow(non_snake_case)] #[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 _; use rtic::Mutex as _;
#(#stmts)* #(#stmts)*
} }
)); ));
software_tasks_imports.push(quote!( software_tasks_imports.push(quote!(
#(#attrs)*
#(#cfgs)* #(#cfgs)*
#[allow(non_snake_case)] #[allow(non_snake_case)]
use super::#name; use super::#name;

View file

@ -40,7 +40,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
methods.push(quote!( methods.push(quote!(
#(#cfgs)* #(#cfgs)*
fn #name(&self #(,#args)*) -> Result<(), #ty> { pub fn #name(&self #(,#args)*) -> Result<(), #ty> {
#let_instant #let_instant
#body #body
} }
@ -92,7 +92,7 @@ pub fn codegen(app: &App, analysis: &Analysis, extra: &Extra) -> Vec<TokenStream
methods.push(quote!( methods.push(quote!(
#(#cfgs)* #(#cfgs)*
#[inline(always)] #[inline(always)]
fn #name(&self #(,#args)*) -> Result<(), #ty> { pub fn #name(&self #(,#args)*) -> Result<(), #ty> {
unsafe { unsafe {
#let_instant #let_instant
#spawn(self.priority() #instant #(,#untupled)*) #spawn(self.priority() #instant #(,#untupled)*)