diff options
| author | Nika Layzell <nika@thelayzells.com> | 2022-06-27 20:03:56 -0400 |
|---|---|---|
| committer | Nika Layzell <nika@thelayzells.com> | 2022-06-28 09:54:29 -0400 |
| commit | 64a7d57046bc4653dddb346b51cd66a8980f3533 (patch) | |
| tree | 99e98144d5fc04f147314b36735174dd910dd3a2 /compiler/rustc_expand/src | |
| parent | f28dfdf1c777ff83660cec0b432acabde1a2f7a4 (diff) | |
| download | rust-64a7d57046bc4653dddb346b51cd66a8980f3533.tar.gz rust-64a7d57046bc4653dddb346b51cd66a8980f3533.zip | |
review changes
longer names for RPC generics and reduced dependency on macros in the server.
Diffstat (limited to 'compiler/rustc_expand/src')
| -rw-r--r-- | compiler/rustc_expand/src/proc_macro_server.rs | 199 |
1 files changed, 95 insertions, 104 deletions
diff --git a/compiler/rustc_expand/src/proc_macro_server.rs b/compiler/rustc_expand/src/proc_macro_server.rs index 75731b9071d..0e909e2f982 100644 --- a/compiler/rustc_expand/src/proc_macro_server.rs +++ b/compiler/rustc_expand/src/proc_macro_server.rs @@ -55,8 +55,10 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> fn from_internal((stream, rustc): (TokenStream, &mut Rustc<'_, '_>)) -> Self { use rustc_ast::token::*; + // Estimate the capacity as `stream.len()` rounded up to the next power + // of two to limit the number of required reallocations. + let mut trees = Vec::with_capacity(stream.len().next_power_of_two()); let mut cursor = stream.into_trees(); - let mut trees = Vec::new(); while let Some((tree, spacing)) = cursor.next_with_spacing() { let joint = spacing == Joint; @@ -77,95 +79,78 @@ impl FromInternal<(TokenStream, &mut Rustc<'_, '_>)> tokenstream::TokenTree::Token(token) => token, }; - macro_rules! tt { - ($ty:ident { $($field:ident $(: $value:expr)*),+ $(,)? }) => ( - trees.push(TokenTree::$ty(self::$ty { - $($field $(: $value)*,)+ - span, - })) - ); - ($ty:ident::$method:ident($($value:expr),*)) => ( - trees.push(TokenTree::$ty(self::$ty::$method($($value,)* span))) - ); - } - macro_rules! op { - ($a:expr) => {{ - tt!(Punct { ch: $a, joint }); - }}; - ($a:expr, $b:expr) => {{ - tt!(Punct { ch: $a, joint: true }); - tt!(Punct { ch: $b, joint }); - }}; - ($a:expr, $b:expr, $c:expr) => {{ - tt!(Punct { ch: $a, joint: true }); - tt!(Punct { ch: $b, joint: true }); - tt!(Punct { ch: $c, joint }); - }}; - } + let mut op = |s: &str| { + assert!(s.is_ascii()); + trees.extend(s.as_bytes().iter().enumerate().map(|(idx, &ch)| { + TokenTree::Punct(Punct { ch, joint: joint || idx != s.len() - 1, span }) + })); + }; match kind { - Eq => op!('='), - Lt => op!('<'), - Le => op!('<', '='), - EqEq => op!('=', '='), - Ne => op!('!', '='), - Ge => op!('>', '='), - Gt => op!('>'), - AndAnd => op!('&', '&'), - OrOr => op!('|', '|'), - Not => op!('!'), - Tilde => op!('~'), - BinOp(Plus) => op!('+'), - BinOp(Minus) => op!('-'), - BinOp(Star) => op!('*'), - BinOp(Slash) => op!('/'), - BinOp(Percent) => op!('%'), - BinOp(Caret) => op!('^'), - BinOp(And) => op!('&'), - BinOp(Or) => op!('|'), - BinOp(Shl) => op!('<', '<'), - BinOp(Shr) => op!('>', '>'), - BinOpEq(Plus) => op!('+', '='), - BinOpEq(Minus) => op!('-', '='), - BinOpEq(Star) => op!('*', '='), - BinOpEq(Slash) => op!('/', '='), - BinOpEq(Percent) => op!('%', '='), - BinOpEq(Caret) => op!('^', '='), - BinOpEq(And) => op!('&', '='), - BinOpEq(Or) => op!('|', '='), - BinOpEq(Shl) => op!('<', '<', '='), - BinOpEq(Shr) => op!('>', '>', '='), - At => op!('@'), - Dot => op!('.'), - DotDot => op!('.', '.'), - DotDotDot => op!('.', '.', '.'), - DotDotEq => op!('.', '.', '='), - Comma => op!(','), - Semi => op!(';'), - Colon => op!(':'), - ModSep => op!(':', ':'), - RArrow => op!('-', '>'), - LArrow => op!('<', '-'), - FatArrow => op!('=', '>'), - Pound => op!('#'), - Dollar => op!('$'), - Question => op!('?'), - SingleQuote => op!('\''), - - Ident(name, false) if name == kw::DollarCrate => tt!(Ident::dollar_crate()), - Ident(name, is_raw) => tt!(Ident::new(rustc.sess(), name, is_raw)), + Eq => op("="), + Lt => op("<"), + Le => op("<="), + EqEq => op("=="), + Ne => op("!="), + Ge => op(">="), + Gt => op(">"), + AndAnd => op("&&"), + OrOr => op("||"), + Not => op("!"), + Tilde => op("~"), + BinOp(Plus) => op("+"), + BinOp(Minus) => op("-"), + BinOp(Star) => op("*"), + BinOp(Slash) => op("/"), + BinOp(Percent) => op("%"), + BinOp(Caret) => op("^"), + BinOp(And) => op("&"), + BinOp(Or) => op("|"), + BinOp(Shl) => op("<<"), + BinOp(Shr) => op(">>"), + BinOpEq(Plus) => op("+="), + BinOpEq(Minus) => op("-="), + BinOpEq(Star) => op("*="), + BinOpEq(Slash) => op("/="), + BinOpEq(Percent) => op("%="), + BinOpEq(Caret) => op("^="), + BinOpEq(And) => op("&="), + BinOpEq(Or) => op("|="), + BinOpEq(Shl) => op("<<="), + BinOpEq(Shr) => op(">>="), + At => op("@"), + Dot => op("."), + DotDot => op(".."), + DotDotDot => op("..."), + DotDotEq => op("..="), + Comma => op(","), + Semi => op(";"), + Colon => op(":"), + ModSep => op("::"), + RArrow => op("->"), + LArrow => op("<-"), + FatArrow => op("=>"), + Pound => op("#"), + Dollar => op("$"), + Question => op("?"), + SingleQuote => op("'"), + + Ident(name, false) if name == kw::DollarCrate => trees.push(TokenTree::Ident(Ident::dollar_crate(span))), + Ident(name, is_raw) => trees.push(TokenTree::Ident(Ident::new(rustc.sess(), name, is_raw, span))), Lifetime(name) => { let ident = symbol::Ident::new(name, span).without_first_quote(); - tt!(Punct { ch: '\'', joint: true }); - tt!(Ident::new(rustc.sess(), ident.name, false)); + trees.extend([ + TokenTree::Punct(Punct { ch: b'\'', joint: true, span }), + TokenTree::Ident(Ident::new(rustc.sess(), ident.name, false, span)), + ]); } - Literal(lit) => tt!(Literal { lit }), + Literal(lit) => trees.push(TokenTree::Literal(self::Literal { lit, span })), DocComment(_, attr_style, data) => { let mut escaped = String::new(); for ch in data.as_str().chars() { escaped.extend(ch.escape_debug()); } - let stream = vec