remove vis restriction for local and shared resources

This commit is contained in:
Andrew Gazelka 2023-04-11 13:10:26 -07:00
parent 44c614d792
commit e47914ee50
No known key found for this signature in database
GPG key ID: 67C80748D14ED9F6
8 changed files with 36 additions and 77 deletions

View file

@ -18,7 +18,9 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
let attrs = &init.attrs; let attrs = &init.attrs;
let stmts = &init.stmts; let stmts = &init.stmts;
let shared = &init.user_shared_struct; let shared = &init.user_shared_struct;
let shared_vis = &app.shared_resources_vis;
let local = &init.user_local_struct; let local = &init.user_local_struct;
let local_vis = &app.local_resources_vis;
let shared_resources: Vec<_> = app let shared_resources: Vec<_> = app
.shared_resources .shared_resources
@ -27,10 +29,11 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
let ty = &v.ty; let ty = &v.ty;
let cfgs = &v.cfgs; let cfgs = &v.cfgs;
let docs = &v.docs; let docs = &v.docs;
let vis = &v.vis;
quote!( quote!(
#(#cfgs)* #(#cfgs)*
#(#docs)* #(#docs)*
#k: #ty, #vis #k: #ty,
) )
}) })
.collect(); .collect();
@ -41,20 +44,21 @@ pub fn codegen(app: &App, analysis: &Analysis) -> TokenStream2 {
let ty = &v.ty; let ty = &v.ty;
let cfgs = &v.cfgs; let cfgs = &v.cfgs;
let docs = &v.docs; let docs = &v.docs;
let vis = &v.vis;
quote!( quote!(
#(#cfgs)* #(#cfgs)*
#(#docs)* #(#docs)*
#k: #ty, #vis #k: #ty,
) )
}) })
.collect(); .collect();
root_init.push(quote! { root_init.push(quote! {
struct #shared { #shared_vis struct #shared {
#(#shared_resources)* #(#shared_resources)*
} }
struct #local { #local_vis struct #local {
#(#local_resources)* #(#local_resources)*
} }
}); });

View file

@ -23,9 +23,13 @@ pub struct App {
/// Resources shared between tasks defined in `#[shared]` /// Resources shared between tasks defined in `#[shared]`
pub shared_resources: Map<SharedResource>, pub shared_resources: Map<SharedResource>,
pub shared_resources_vis: syn::Visibility,
/// Task local resources defined in `#[local]` /// Task local resources defined in `#[local]`
pub local_resources: Map<LocalResource>, pub local_resources: Map<LocalResource>,
pub local_resources_vis: syn::Visibility,
/// User imports /// User imports
pub user_imports: Vec<ItemUse>, pub user_imports: Vec<ItemUse>,
@ -170,6 +174,9 @@ pub struct SharedResource {
/// Shared resource properties /// Shared resource properties
pub properties: SharedResourceProperties, pub properties: SharedResourceProperties,
/// The visibility of this resource
pub vis: syn::Visibility,
} }
/// A local resource, defined in `#[local]` /// A local resource, defined in `#[local]`
@ -187,6 +194,9 @@ pub struct LocalResource {
/// The type of this resource /// The type of this resource
pub ty: Box<Type>, pub ty: Box<Type>,
/// The visibility of this resource
pub vis: syn::Visibility,
} }
/// An async software task /// An async software task

View file

@ -8,7 +8,6 @@ use syn::{
Expr, ExprArray, Fields, ForeignItem, Ident, Item, LitBool, Path, Token, Visibility, Expr, ExprArray, Fields, ForeignItem, Ident, Item, LitBool, Path, Token, Visibility,
}; };
use super::Input;
use crate::syntax::{ use crate::syntax::{
ast::{ ast::{
App, AppArgs, Dispatcher, Dispatchers, HardwareTask, Idle, IdleArgs, Init, InitArgs, App, AppArgs, Dispatcher, Dispatchers, HardwareTask, Idle, IdleArgs, Init, InitArgs,
@ -18,6 +17,8 @@ use crate::syntax::{
Either, Map, Set, Either, Map, Set,
}; };
use super::Input;
impl AppArgs { impl AppArgs {
pub(crate) fn parse(tokens: TokenStream2) -> parse::Result<Self> { pub(crate) fn parse(tokens: TokenStream2) -> parse::Result<Self> {
(|input: ParseStream<'_>| -> parse::Result<Self> { (|input: ParseStream<'_>| -> parse::Result<Self> {
@ -147,9 +148,13 @@ impl App {
let mut idle = None; let mut idle = None;
let mut shared_resources_ident = None; let mut shared_resources_ident = None;
let mut shared_resources_vis = Visibility::Inherited;
let mut shared_resources = Map::new(); let mut shared_resources = Map::new();
let mut local_resources_ident = None; let mut local_resources_ident = None;
let mut local_resources_vis = Visibility::Inherited;
let mut local_resources = Map::new(); let mut local_resources = Map::new();
let mut hardware_tasks = Map::new(); let mut hardware_tasks = Map::new();
let mut software_tasks = Map::new(); let mut software_tasks = Map::new();
let mut user_imports = vec![]; let mut user_imports = vec![];
@ -283,12 +288,7 @@ impl App {
)); ));
} }
if struct_item.vis != Visibility::Inherited { shared_resources_vis = struct_item.vis.clone();
return Err(parse::Error::new(
struct_item.span(),
"this item must have inherited / private visibility",
));
}
if let Fields::Named(fields) = &mut struct_item.fields { if let Fields::Named(fields) = &mut struct_item.fields {
for field in &mut fields.named { for field in &mut fields.named {
@ -301,10 +301,8 @@ impl App {
)); ));
} }
shared_resources.insert( shared_resources
ident.clone(), .insert(ident.clone(), SharedResource::parse(field)?);
SharedResource::parse(field, ident.span())?,
);
} }
} else { } else {
return Err(parse::Error::new( return Err(parse::Error::new(
@ -328,12 +326,7 @@ impl App {
)); ));
} }
if struct_item.vis != Visibility::Inherited { local_resources_vis = struct_item.vis.clone();
return Err(parse::Error::new(
struct_item.span(),
"this item must have inherited / private visibility",
));
}
if let Fields::Named(fields) = &mut struct_item.fields { if let Fields::Named(fields) = &mut struct_item.fields {
for field in &mut fields.named { for field in &mut fields.named {
@ -346,10 +339,7 @@ impl App {
)); ));
} }
local_resources.insert( local_resources.insert(ident.clone(), LocalResource::parse(field)?);
ident.clone(),
LocalResource::parse(field, ident.span())?,
);
} }
} else { } else {
return Err(parse::Error::new( return Err(parse::Error::new(
@ -470,7 +460,9 @@ impl App {
init, init,
idle, idle,
shared_resources, shared_resources,
shared_resources_vis,
local_resources, local_resources,
local_resources_vis,
user_imports, user_imports,
user_code, user_code,
hardware_tasks, hardware_tasks,

View file

@ -1,5 +1,4 @@
use proc_macro2::Span; use syn::{parse, Field};
use syn::{parse, Field, Visibility};
use crate::syntax::parse::util::FilterAttrs; use crate::syntax::parse::util::FilterAttrs;
use crate::syntax::{ use crate::syntax::{
@ -8,14 +7,7 @@ use crate::syntax::{
}; };
impl SharedResource { impl SharedResource {
pub(crate) fn parse(item: &Field, span: Span) -> parse::Result<Self> { pub(crate) fn parse(item: &Field) -> parse::Result<Self> {
if item.vis != Visibility::Inherited {
return Err(parse::Error::new(
span,
"this field must have inherited / private visibility",
));
}
let FilterAttrs { let FilterAttrs {
cfgs, cfgs,
mut attrs, mut attrs,
@ -30,19 +22,13 @@ impl SharedResource {
docs, docs,
ty: Box::new(item.ty.clone()), ty: Box::new(item.ty.clone()),
properties: SharedResourceProperties { lock_free }, properties: SharedResourceProperties { lock_free },
vis: item.vis.clone(),
}) })
} }
} }
impl LocalResource { impl LocalResource {
pub(crate) fn parse(item: &Field, span: Span) -> parse::Result<Self> { pub(crate) fn parse(item: &Field) -> parse::Result<Self> {
if item.vis != Visibility::Inherited {
return Err(parse::Error::new(
span,
"this field must have inherited / private visibility",
));
}
let FilterAttrs { cfgs, attrs, docs } = util::filter_attributes(item.attrs.clone()); let FilterAttrs { cfgs, attrs, docs } = util::filter_attributes(item.attrs.clone());
Ok(LocalResource { Ok(LocalResource {
@ -50,6 +36,7 @@ impl LocalResource {
attrs, attrs,
docs, docs,
ty: Box::new(item.ty.clone()), ty: Box::new(item.ty.clone()),
vis: item.vis.clone(),
}) })
} }
} }

View file

@ -1,15 +0,0 @@
#![no_main]
#[rtic_macros::mock_app(device = mock)]
mod app {
#[shared]
struct Shared {}
#[local]
struct Local {
pub x: u32,
}
#[init]
fn init(_: init::Context) -> (Shared, Local) {}
}

View file

@ -1,5 +0,0 @@
error: this field must have inherited / private visibility
--> ui/local-pub.rs:10:13
|
10 | pub x: u32,
| ^

View file

@ -1,9 +0,0 @@
#![no_main]
#[rtic_macros::mock_app(device = mock)]
mod app {
#[shared]
struct Shared {
pub x: u32,
}
}

View file

@ -1,5 +0,0 @@
error: this field must have inherited / private visibility
--> ui/shared-pub.rs:7:13
|
7 | pub x: u32,
| ^