proc_macro: stop using a remote object handle for Punct
This greatly reduces round-trips to fetch relevant extra information about the token in proc macro code, and avoids RPC messages to create Punct tokens.
This commit is contained in:
parent
3b0d4813ab
commit
72bfe618fa
6 changed files with 49 additions and 95 deletions
|
@ -14,8 +14,8 @@ use rustc_span::def_id::CrateNum;
|
|||
use rustc_span::symbol::{self, kw, sym, Symbol};
|
||||
use rustc_span::{BytePos, FileName, Pos, SourceFile, Span};
|
||||
|
||||
use pm::bridge::{server, ExpnGlobals, TokenTree};
|
||||
use pm::{Delimiter, Level, LineColumn, Spacing};
|
||||
use pm::bridge::{server, ExpnGlobals, Punct, TokenTree};
|
||||
use pm::{Delimiter, Level, LineColumn};
|
||||
use std::ops::Bound;
|
||||
use std::{ascii, panic};
|
||||
|
||||
|
@ -50,7 +50,7 @@ impl ToInternal<token::Delimiter> for Delimiter {
|
|||
}
|
||||
|
||||
impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
|
||||
for TokenTree<Group, Punct, Ident, Literal>
|
||||
for TokenTree<Span, Group, Ident, Literal>
|
||||
{
|
||||
fn from_internal(
|
||||
((tree, spacing), stack, rustc): (TreeAndSpacing, &mut Vec<Self>, &mut Rustc<'_, '_>),
|
||||
|
@ -79,16 +79,16 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
|
|||
}
|
||||
macro_rules! op {
|
||||
($a:expr) => {
|
||||
tt!(Punct::new($a, joint))
|
||||
tt!(Punct { ch: $a, joint })
|
||||
};
|
||||
($a:expr, $b:expr) => {{
|
||||
stack.push(tt!(Punct::new($b, joint)));
|
||||
tt!(Punct::new($a, true))
|
||||
stack.push(tt!(Punct { ch: $b, joint }));
|
||||
tt!(Punct { ch: $a, joint: true })
|
||||
}};
|
||||
($a:expr, $b:expr, $c:expr) => {{
|
||||
stack.push(tt!(Punct::new($c, joint)));
|
||||
stack.push(tt!(Punct::new($b, true)));
|
||||
tt!(Punct::new($a, true))
|
||||
stack.push(tt!(Punct { ch: $c, joint }));
|
||||
stack.push(tt!(Punct { ch: $b, joint: true }));
|
||||
tt!(Punct { ch: $a, joint: true })
|
||||
}};
|
||||
}
|
||||
|
||||
|
@ -146,7 +146,7 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
|
|||
Lifetime(name) => {
|
||||
let ident = symbol::Ident::new(name, span).without_first_quote();
|
||||
stack.push(tt!(Ident::new(rustc.sess(), ident.name, false)));
|
||||
tt!(Punct::new('\'', true))
|
||||
tt!(Punct { ch: '\'', joint: true })
|
||||
}
|
||||
Literal(lit) => tt!(Literal { lit }),
|
||||
DocComment(_, attr_style, data) => {
|
||||
|
@ -169,9 +169,9 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
|
|||
flatten: false,
|
||||
}));
|
||||
if attr_style == ast::AttrStyle::Inner {
|
||||
stack.push(tt!(Punct::new('!', false)));
|
||||
stack.push(tt!(Punct { ch: '!', joint: false }));
|
||||
}
|
||||
tt!(Punct::new('#', false))
|
||||
tt!(Punct { ch: '#', joint: false })
|
||||
}
|
||||
|
||||
Interpolated(nt) if let NtIdent(ident, is_raw) = *nt => {
|
||||
|
@ -192,7 +192,7 @@ impl FromInternal<(TreeAndSpacing, &'_ mut Vec<Self>, &mut Rustc<'_, '_>)>
|
|||
}
|
||||
}
|
||||
|
||||
impl ToInternal<TokenStream> for TokenTree<Group, Punct, Ident, Literal> {
|
||||
impl ToInternal<TokenStream> for TokenTree<Span, Group, Ident, Literal> {
|
||||
fn to_internal(self) -> TokenStream {
|
||||
use rustc_ast::token::*;
|
||||
|
||||
|
@ -288,27 +288,6 @@ pub struct Group {
|
|||
flatten: bool,
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Punct {
|
||||
ch: char,
|
||||
// NB. not using `Spacing` here because it doesn't implement `Hash`.
|
||||
joint: bool,
|
||||
span: Span,
|
||||
}
|
||||
|
||||
impl Punct {
|
||||
fn new(ch: char, joint: bool, span: Span) -> Punct {
|
||||
const LEGAL_CHARS: &[char] = &[
|
||||
'=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^', '&', '|', '@', '.', ',', ';',
|
||||
':', '#', '$', '?', '\'',
|
||||
];
|
||||
if !LEGAL_CHARS.contains(&ch) {
|
||||
panic!("unsupported character `{:?}`", ch)
|
||||
}
|
||||
Punct { ch, joint, span }
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
|
||||
pub struct Ident {
|
||||
sym: Symbol,
|
||||
|
@ -378,7 +357,6 @@ impl server::Types for Rustc<'_, '_> {
|
|||
type FreeFunctions = FreeFunctions;
|
||||
type TokenStream = TokenStream;
|
||||
type Group = Group;
|
||||
type Punct = Punct;
|
||||
type Ident = Ident;
|
||||
type Literal = Literal;
|
||||
type SourceFile = Lrc<SourceFile>;
|
||||
|
@ -471,7 +449,7 @@ impl server::TokenStream for Rustc<'_, '_> {
|
|||
|
||||
fn from_token_tree(
|
||||
&mut self,
|
||||
tree: TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>,
|
||||
tree: TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>,
|
||||
) -> Self::TokenStream {
|
||||
tree.to_internal()
|
||||
}
|
||||
|
@ -479,7 +457,7 @@ impl server::TokenStream for Rustc<'_, '_> {
|
|||
fn concat_trees(
|
||||
&mut self,
|
||||
base: Option<Self::TokenStream>,
|
||||
trees: Vec<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>>,
|
||||
trees: Vec<TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>>,
|
||||
) -> Self::TokenStream {
|
||||
let mut builder = tokenstream::TokenStreamBuilder::new();
|
||||
if let Some(base) = base {
|
||||
|
@ -509,7 +487,7 @@ impl server::TokenStream for Rustc<'_, '_> {
|
|||
fn into_trees(
|
||||
&mut self,
|
||||
stream: Self::TokenStream,
|
||||
) -> Vec<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> {
|
||||
) -> Vec<TokenTree<Self::Span, Self::Group, Self::Ident, Self::Literal>> {
|
||||
// FIXME: This is a raw port of the previous approach (which had a
|
||||
// `TokenStreamIter` server-side object with a single `next` method),
|
||||
// and can probably be optimized (for bulk conversion).
|
||||
|
@ -577,28 +555,6 @@ impl server::Group for Rustc<'_, '_> {
|
|||
}
|
||||
}
|
||||
|
||||
impl server::Punct for Rustc<'_, '_> {
|
||||
fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
|
||||
Punct::new(ch, spacing == Spacing::Joint, self.call_site)
|
||||
}
|
||||
|
||||
fn as_char(&mut self, punct: Self::Punct) -> char {
|
||||
punct.ch
|
||||
}
|
||||
|
||||
fn spacing(&mut self, punct: Self::Punct) -> Spacing {
|
||||
if punct.joint { Spacing::Joint } else { Spacing::Alone }
|
||||
}
|
||||
|
||||
fn span(&mut self, punct: Self::Punct) -> Self::Span {
|
||||
punct.span
|
||||
}
|
||||
|
||||
fn with_span(&mut self, punct: Self::Punct, span: Self::Span) -> Self::Punct {
|
||||
Punct { span, ..punct }
|
||||
}
|
||||
}
|
||||
|
||||
impl server::Ident for Rustc<'_, '_> {
|
||||
fn new(&mut self, string: &str, span: Self::Span, is_raw: bool) -> Self::Ident {
|
||||
Ident::new(self.sess(), Symbol::intern(string), is_raw, span)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue