diff options
| author | Camille GILLOT <gillot.camille@gmail.com> | 2019-11-10 18:24:37 +0100 |
|---|---|---|
| committer | Camille GILLOT <gillot.camille@gmail.com> | 2019-11-22 20:01:45 +0100 |
| commit | 31298b41d65093b2ea260d8fe8820da6db6dac94 (patch) | |
| tree | 2a792a21b0dcb3834272399e6827ddb2443c8d9f /src/libsyntax | |
| parent | 0073d3be97707b34f747c2633ac02e8c9ea89452 (diff) | |
Invert implementations for TokenKind.
Also export a bunch of Token-related impls.
Diffstat (limited to 'src/libsyntax')
| -rw-r--r-- | src/libsyntax/ast.rs | 2 | ||||
| -rw-r--r-- | src/libsyntax/lib.rs | 5 | ||||
| -rw-r--r-- | src/libsyntax/token.rs | 11 | ||||
| -rw-r--r-- | src/libsyntax/tokenstream.rs | 31 |
4 files changed, 46 insertions, 3 deletions
diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 01abd09aa91..3cc6a043e3b 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -2276,7 +2276,7 @@ impl rustc_serialize::Decodable for AttrId { } } -#[derive(Clone, RustcEncodable, RustcDecodable, Debug)] +#[derive(Clone, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct AttrItem { pub path: Path, pub tokens: TokenStream, diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 7865323f842..939e1877b4a 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -20,6 +20,7 @@ #![recursion_limit="256"] pub use errors; +use rustc_data_structures::stable_hasher::StableHasher; use rustc_data_structures::sync::Lock; use rustc_index::bit_set::GrowableBitSet; pub use rustc_data_structures::thin_vec::ThinVec; @@ -114,4 +115,6 @@ pub mod early_buffered_lints; /// Requirements for a `StableHashingContext` to be used in this crate. /// This is a hack to allow using the `HashStable_Generic` derive macro /// instead of implementing everything in librustc. -pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike {} +pub trait StableHashingContextLike: syntax_pos::StableHashingContextLike { + fn hash_stable_tokenkind(&mut self, tokenkind: &token::TokenKind, hasher: &mut StableHasher); +} diff --git a/src/libsyntax/token.rs b/src/libsyntax/token.rs index fd1623384a4..305db739399 100644 --- a/src/libsyntax/token.rs +++ b/src/libsyntax/token.rs @@ -14,6 +14,7 @@ use syntax_pos::{self, Span, DUMMY_SP}; use std::fmt; use std::mem; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use rustc_macros::HashStable_Generic; @@ -262,7 +263,15 @@ pub enum TokenKind { #[cfg(target_arch = "x86_64")] rustc_data_structures::static_assert_size!(TokenKind, 16); -#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug)] +impl<CTX> HashStable<CTX> for TokenKind + where CTX: crate::StableHashingContextLike +{ + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + hcx.hash_stable_tokenkind(self, hasher) + } +} + +#[derive(Clone, PartialEq, RustcEncodable, RustcDecodable, Debug, HashStable_Generic)] pub struct Token { pub kind: TokenKind, pub span: Span, diff --git a/src/libsyntax/tokenstream.rs b/src/libsyntax/tokenstream.rs index 40f00aaa5d8..6de9a0fb070 100644 --- a/src/libsyntax/tokenstream.rs +++ b/src/libsyntax/tokenstream.rs @@ -16,6 +16,7 @@ use crate::token::{self, DelimToken, Token, TokenKind}; use syntax_pos::{Span, DUMMY_SP}; +use rustc_data_structures::stable_hasher::{HashStable, StableHasher}; use rustc_data_structures::sync::Lrc; use smallvec::{SmallVec, smallvec}; @@ -51,6 +52,26 @@ where TokenStream: Send + Sync, {} +impl<CTX> HashStable<CTX> for TokenTree + where CTX: crate::StableHashingContextLike +{ + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + mem::discriminant(self).hash_stable(hcx, hasher); + match *self { + TokenTree::Token(ref token) => { + token.hash_stable(hcx, hasher); + } + TokenTree::Delimited(span, delim, ref tts) => { + span.hash_stable(hcx, hasher); + std::hash::Hash::hash(&delim, hasher); + for sub_tt in tts.trees() { + sub_tt.hash_stable(hcx, hasher); + } + } + } + } +} + impl TokenTree { /// Checks if this TokenTree is equal to the other, regardless of span information. pub fn eq_unspanned(&self, other: &TokenTree) -> bool { @@ -115,6 +136,16 @@ impl TokenTree { } } +impl<CTX> HashStable<CTX> for TokenStream + where CTX: crate::StableHashingContextLike +{ + fn hash_stable(&self, hcx: &mut CTX, hasher: &mut StableHasher) { + for sub_tt in self.trees() { + sub_tt.hash_stable(hcx, hasher); + } + } +} + /// A `TokenStream` is an abstract sequence of tokens, organized into `TokenTree`s. /// /// The goal is for procedural macros to work with `TokenStream`s and `TokenTree`s |
