|
| 1 | +// Copyright (c) 2022-present INESC-ID. |
| 2 | +// Distributed under the MIT license that can be found in the LICENSE file. |
| 3 | + |
| 4 | +use proc_macro2::TokenStream as TokenStream2; |
| 5 | +use quote::{format_ident, quote}; |
| 6 | +use syn::visit_mut::{self, VisitMut}; |
| 7 | +use syn::{Expr, ExprBreak, Lifetime, Pat}; |
| 8 | + |
| 9 | +pub struct Arm { |
| 10 | + pub label: String, |
| 11 | + pub entry: ArmEntry, |
| 12 | + pub body: Expr, |
| 13 | +} |
| 14 | + |
| 15 | +pub enum ArmEntry { |
| 16 | + Dispatch { pat: Pat, guard: Option<Expr> }, |
| 17 | + LabelOnly, |
| 18 | +} |
| 19 | + |
| 20 | +pub fn emit_state_machine(condition: Option<Expr>, arms: Vec<Arm>) -> TokenStream2 { |
| 21 | + let lbl = Lifetime::new("'__sm", proc_macro2::Span::call_site()); |
| 22 | + let s = format_ident!("__s"); |
| 23 | + |
| 24 | + let base = if condition.is_some() { 1u32 } else { 0u32 }; |
| 25 | + |
| 26 | + let dispatch_arm = condition.map(|scrut| { |
| 27 | + let case_arms = arms |
| 28 | + .iter() |
| 29 | + .enumerate() |
| 30 | + .filter_map(|(i, arm)| match &arm.entry { |
| 31 | + ArmEntry::Dispatch { pat, guard } => { |
| 32 | + let idx = base + i as u32; |
| 33 | + let guard = guard.as_ref().map(|g| quote! { if #g }); |
| 34 | + Some(quote! { #pat #guard => { #s = #idx; continue #lbl; } }) |
| 35 | + } |
| 36 | + ArmEntry::LabelOnly => None, |
| 37 | + }); |
| 38 | + quote! { |
| 39 | + 0u32 => { |
| 40 | + #[allow(unreachable_patterns)] |
| 41 | + match #scrut { |
| 42 | + #(#case_arms,)* |
| 43 | + _ => break #lbl, |
| 44 | + } |
| 45 | + } |
| 46 | + } |
| 47 | + }); |
| 48 | + |
| 49 | + let n = arms.len(); |
| 50 | + let body_arms = arms.iter().enumerate().map(|(i, arm)| { |
| 51 | + let idx = base + i as u32; |
| 52 | + let body = rewrite_body(&arm.body, &lbl); |
| 53 | + let tail = if i + 1 < n { |
| 54 | + let next = idx + 1; |
| 55 | + quote! { #s = #next; continue #lbl; } |
| 56 | + } else { |
| 57 | + quote! { break #lbl; } |
| 58 | + }; |
| 59 | + quote! { |
| 60 | + #idx => { |
| 61 | + #[allow(unreachable_code)] |
| 62 | + { #body; #tail } |
| 63 | + } |
| 64 | + } |
| 65 | + }); |
| 66 | + |
| 67 | + quote! {{ |
| 68 | + let mut #s: u32 = 0; |
| 69 | + #[allow(unreachable_code, unused_labels)] |
| 70 | + #lbl: loop { |
| 71 | + match #s { |
| 72 | + #dispatch_arm |
| 73 | + #(#body_arms)* |
| 74 | + _ => break #lbl, |
| 75 | + } |
| 76 | + } |
| 77 | + }} |
| 78 | +} |
| 79 | + |
| 80 | +fn rewrite_body(body: &Expr, label: &Lifetime) -> TokenStream2 { |
| 81 | + let mut body = body.clone(); |
| 82 | + BreakRewriter { |
| 83 | + label: label.clone(), |
| 84 | + } |
| 85 | + .visit_expr_mut(&mut body); |
| 86 | + quote! { #body } |
| 87 | +} |
| 88 | + |
| 89 | +struct BreakRewriter { |
| 90 | + label: Lifetime, |
| 91 | +} |
| 92 | + |
| 93 | +impl VisitMut for BreakRewriter { |
| 94 | + fn visit_expr_break_mut(&mut self, node: &mut ExprBreak) { |
| 95 | + if node.label.is_none() { |
| 96 | + node.label = Some(self.label.clone()); |
| 97 | + } |
| 98 | + } |
| 99 | + fn visit_expr_loop_mut(&mut self, _: &mut syn::ExprLoop) {} |
| 100 | + fn visit_expr_while_mut(&mut self, _: &mut syn::ExprWhile) {} |
| 101 | + fn visit_expr_for_loop_mut(&mut self, _: &mut syn::ExprForLoop) {} |
| 102 | +} |
0 commit comments