diff options
author | 2019-02-26 23:25:16 +0100 | |
---|---|---|
committer | 2019-02-26 23:25:16 +0100 | |
commit | 8eccef7d9cda8a60594b86d31b656a3680d1ca16 (patch) | |
tree | ee9aa60d3c109b4583299cf6a71277507a4be241 /macros/src | |
parent | 2fd6ae69d1c79635896b643b1094b3805d1ec6c2 (diff) | |
download | rtic-8eccef7d9cda8a60594b86d31b656a3680d1ca16.tar.gz rtic-8eccef7d9cda8a60594b86d31b656a3680d1ca16.tar.zst rtic-8eccef7d9cda8a60594b86d31b656a3680d1ca16.zip |
refactor: make `binds` harder to misuse
Diffstat (limited to 'macros/src')
-rw-r--r-- | macros/src/check.rs | 8 | ||||
-rw-r--r-- | macros/src/codegen.rs | 20 | ||||
-rw-r--r-- | macros/src/syntax.rs | 9 |
3 files changed, 16 insertions, 21 deletions
diff --git a/macros/src/check.rs b/macros/src/check.rs index ab864616..4adc2c17 100644 --- a/macros/src/check.rs +++ b/macros/src/check.rs @@ -106,12 +106,8 @@ pub fn app(app: &App) -> parse::Result<()> { } // Check that free interrupts are not being used - for (name, interrupt) in &app.interrupts { - let name = if let Some(ref binds) = interrupt.args.binds { - binds - } else { - name - }; + for (handler, interrupt) in &app.interrupts { + let name = interrupt.args.binds(handler); if app.free_interrupts.contains_key(name) { return Err(parse::Error::new( diff --git a/macros/src/codegen.rs b/macros/src/codegen.rs index 0e25e8a7..1d201c08 100644 --- a/macros/src/codegen.rs +++ b/macros/src/codegen.rs @@ -468,12 +468,8 @@ fn post_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::Tok // the device into compile errors let device = &app.args.device; let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS); - for (name, exception) in &app.exceptions { - let name = if let Some(ref binds) = exception.args.binds { - binds - } else { - name - }; + for (handler, exception) in &app.exceptions { + let name = exception.args.binds(handler); let priority = exception.args.priority; exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits)))); exprs.push(quote!(p.SCB.set_priority( @@ -1128,7 +1124,7 @@ fn exceptions(ctxt: &mut Context, app: &App, analysis: &Analysis) -> Vec<proc_ma }; let locals = mk_locals(&exception.statics, false); - let symbol = exception.args.binds.as_ref().unwrap_or(ident).to_string(); + let symbol = exception.args.binds(ident).to_string(); let alias = ctxt.ident_gen.mk_ident(None, false); let unsafety = &exception.unsafety; quote!( @@ -1214,7 +1210,7 @@ fn interrupts( let locals = mk_locals(&interrupt.statics, false); let alias = ctxt.ident_gen.mk_ident(None, false); - let symbol = interrupt.args.binds.as_ref().unwrap_or(ident).to_string(); + let symbol = interrupt.args.binds(ident).to_string(); let unsafety = &interrupt.unsafety; scoped.push(quote!( // unsafe trampoline to deter end-users from calling this non-reentrant function @@ -1997,12 +1993,8 @@ fn pre_init(ctxt: &Context, app: &App, analysis: &Analysis) -> proc_macro2::Toke // the device into compile errors let device = &app.args.device; let nvic_prio_bits = quote!(#device::NVIC_PRIO_BITS); - for (name, interrupt) in &app.interrupts { - let name = if let Some(ref binds) = interrupt.args.binds { - binds - } else { - name - }; + for (handler, interrupt) in &app.interrupts { + let name = interrupt.args.binds(handler); let priority = interrupt.args.priority; exprs.push(quote!(p.NVIC.enable(#device::Interrupt::#name);)); exprs.push(quote!(assert!(#priority <= (1 << #nvic_prio_bits));)); diff --git a/macros/src/syntax.rs b/macros/src/syntax.rs index 23981d98..7f87f633 100644 --- a/macros/src/syntax.rs +++ b/macros/src/syntax.rs @@ -731,13 +731,20 @@ pub struct Exception { } pub struct ExceptionArgs { - pub binds: Option<Ident>, + binds: Option<Ident>, pub priority: u8, pub resources: Idents, pub schedule: Idents, pub spawn: Idents, } +impl ExceptionArgs { + /// Returns the name of the exception / interrupt this handler binds to + pub fn binds<'a>(&'a self, handler: &'a Ident) -> &'a Ident { + self.binds.as_ref().unwrap_or(handler) + } +} + impl Parse for ExceptionArgs { fn parse(input: ParseStream<'_>) -> parse::Result<Self> { parse_args(input, /* binds */ true, /* capacity */ false).map( |