aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / vendor / syn-2.0.119 / tests / common / visit.rs
| 1 | use proc_macro2::{Delimiter, Group, TokenStream, TokenTree}; |
| 2 | use std::mem; |
| 3 | use syn::visit_mut::{self, VisitMut}; |
| 4 | use syn::{Expr, File, Generics, LifetimeParam, MacroDelimiter, Stmt, StmtMacro, TypeParam}; |
| 5 | |
| 6 | pub struct FlattenParens { |
| 7 | discard_paren_attrs: bool, |
| 8 | } |
| 9 | |
| 10 | impl FlattenParens { |
| 11 | pub fn discard_attrs() -> Self { |
| 12 | FlattenParens { |
| 13 | discard_paren_attrs: true, |
| 14 | } |
| 15 | } |
| 16 | |
| 17 | pub fn combine_attrs() -> Self { |
| 18 | FlattenParens { |
| 19 | discard_paren_attrs: false, |
| 20 | } |
| 21 | } |
| 22 | |
| 23 | pub fn visit_token_stream_mut(tokens: &mut TokenStream) { |
| 24 | *tokens = mem::take(tokens) |
| 25 | .into_iter() |
| 26 | .flat_map(|tt| { |
| 27 | if let TokenTree::Group(group) = tt { |
| 28 | let delimiter = group.delimiter(); |
| 29 | let mut content = group.stream(); |
| 30 | Self::visit_token_stream_mut(&mut content); |
| 31 | if let Delimiter::Parenthesis = delimiter { |
| 32 | content |
| 33 | } else { |
| 34 | TokenStream::from(TokenTree::Group(Group::new(delimiter, content))) |
| 35 | } |
| 36 | } else { |
| 37 | TokenStream::from(tt) |
| 38 | } |
| 39 | }) |
| 40 | .collect(); |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | impl VisitMut for FlattenParens { |
| 45 | fn visit_expr_mut(&mut self, e: &mut Expr) { |
| 46 | while let Expr::Paren(paren) = e { |
| 47 | let paren_attrs = mem::take(&mut paren.attrs); |
| 48 | *e = mem::replace(&mut *paren.expr, Expr::PLACEHOLDER); |
| 49 | if !paren_attrs.is_empty() && !self.discard_paren_attrs { |
| 50 | let nested_attrs = match e { |
| 51 | Expr::Assign(e) => &mut e.attrs, |
| 52 | Expr::Binary(e) => &mut e.attrs, |
| 53 | Expr::Cast(e) => &mut e.attrs, |
| 54 | _ => unimplemented!(), |
| 55 | }; |
| 56 | assert!(nested_attrs.is_empty()); |
| 57 | *nested_attrs = paren_attrs; |
| 58 | } |
| 59 | } |
| 60 | visit_mut::visit_expr_mut(self, e); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | pub struct AsIfPrinted; |
| 65 | |
| 66 | impl VisitMut for AsIfPrinted { |
| 67 | fn visit_file_mut(&mut self, file: &mut File) { |
| 68 | file.shebang = None; |
| 69 | visit_mut::visit_file_mut(self, file); |
| 70 | } |
| 71 | |
| 72 | fn visit_generics_mut(&mut self, generics: &mut Generics) { |
| 73 | if generics.params.is_empty() { |
| 74 | generics.lt_token = None; |
| 75 | generics.gt_token = None; |
| 76 | } |
| 77 | if let Some(where_clause) = &generics.where_clause { |
| 78 | if where_clause.predicates.is_empty() { |
| 79 | generics.where_clause = None; |
| 80 | } |
| 81 | } |
| 82 | visit_mut::visit_generics_mut(self, generics); |
| 83 | } |
| 84 | |
| 85 | fn visit_lifetime_param_mut(&mut self, param: &mut LifetimeParam) { |
| 86 | if param.bounds.is_empty() { |
| 87 | param.colon_token = None; |
| 88 | } |
| 89 | visit_mut::visit_lifetime_param_mut(self, param); |
| 90 | } |
| 91 | |
| 92 | fn visit_stmt_mut(&mut self, stmt: &mut Stmt) { |
| 93 | if let Stmt::Expr(expr, semi) = stmt { |
| 94 | if let Expr::Macro(e) = expr { |
| 95 | if match e.mac.delimiter { |
| 96 | MacroDelimiter::Brace(_) => true, |
| 97 | MacroDelimiter::Paren(_) | MacroDelimiter::Bracket(_) => semi.is_some(), |
| 98 | } { |
| 99 | let Expr::Macro(expr) = mem::replace(expr, Expr::PLACEHOLDER) else { |
| 100 | unreachable!(); |
| 101 | }; |
| 102 | *stmt = Stmt::Macro(StmtMacro { |
| 103 | attrs: expr.attrs, |
| 104 | mac: expr.mac, |
| 105 | semi_token: *semi, |
| 106 | }); |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | visit_mut::visit_stmt_mut(self, stmt); |
| 111 | } |
| 112 | |
| 113 | fn visit_type_param_mut(&mut self, param: &mut TypeParam) { |
| 114 | if param.bounds.is_empty() { |
| 115 | param.colon_token = None; |
| 116 | } |
| 117 | visit_mut::visit_type_param_mut(self, param); |
| 118 | } |
| 119 | } |