From e305994beb1347e2fcadf5c84acec60fb6902551 Mon Sep 17 00:00:00 2001 From: Eduard-Mihai Burtescu Date: Fri, 16 Mar 2018 01:09:22 +0200 Subject: [PATCH] proc_macro: introduce a "bridge" between clients (proc macros) and servers (compiler front-ends). --- src/libproc_macro/bridge/buffer.rs | 170 +++++ src/libproc_macro/bridge/client.rs | 504 ++++++++++++++ src/libproc_macro/bridge/closure.rs | 42 ++ src/libproc_macro/bridge/handle.rs | 92 +++ src/libproc_macro/bridge/mod.rs | 413 ++++++++++++ src/libproc_macro/bridge/rpc.rs | 319 +++++++++ src/libproc_macro/bridge/scoped_cell.rs | 90 +++ src/libproc_macro/bridge/server.rs | 352 ++++++++++ src/libproc_macro/diagnostic.rs | 30 +- src/libproc_macro/lib.rs | 463 +++++-------- src/libproc_macro/rustc.rs | 631 +++++++++++++++--- src/librustc/dep_graph/dep_node.rs | 2 +- src/librustc/session/mod.rs | 8 +- src/librustc/ty/query/config.rs | 2 +- src/librustc/ty/query/mod.rs | 2 +- src/librustc/ty/query/plumbing.rs | 2 +- src/librustc/util/common.rs | 13 +- .../back/symbol_export.rs | 2 +- src/librustc_codegen_utils/symbol_names.rs | 4 +- src/librustc_driver/driver.rs | 8 +- src/librustc_driver/lib.rs | 2 +- ...erive_registrar.rs => proc_macro_decls.rs} | 10 +- src/librustc_metadata/creader.rs | 86 +-- src/librustc_metadata/cstore_impl.rs | 7 +- src/librustc_metadata/encoder.rs | 4 +- src/librustc_metadata/locator.rs | 2 +- src/librustc_metadata/schema.rs | 2 +- src/libsyntax/feature_gate.rs | 4 +- src/libsyntax_ext/deriving/custom.rs | 65 +- src/libsyntax_ext/lib.rs | 4 +- ...macro_registrar.rs => proc_macro_decls.rs} | 142 ++-- src/libsyntax_ext/proc_macro_impl.rs | 42 +- 32 files changed, 2893 insertions(+), 626 deletions(-) create mode 100644 src/libproc_macro/bridge/buffer.rs create mode 100644 src/libproc_macro/bridge/client.rs create mode 100644 src/libproc_macro/bridge/closure.rs create mode 100644 src/libproc_macro/bridge/handle.rs create mode 100644 src/libproc_macro/bridge/mod.rs create mode 100644 src/libproc_macro/bridge/rpc.rs create mode 100644 src/libproc_macro/bridge/scoped_cell.rs create mode 100644 src/libproc_macro/bridge/server.rs rename src/librustc_driver/{derive_registrar.rs => proc_macro_decls.rs} (81%) rename src/libsyntax_ext/{proc_macro_registrar.rs => proc_macro_decls.rs} (76%) diff --git a/src/libproc_macro/bridge/buffer.rs b/src/libproc_macro/bridge/buffer.rs new file mode 100644 index 0000000000000..f228841c1e80f --- /dev/null +++ b/src/libproc_macro/bridge/buffer.rs @@ -0,0 +1,170 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Buffer management for same-process client<->server communication. + +use std::io::{self, Write}; +use std::mem; +use std::ops::{Deref, DerefMut}; +use std::slice; + +#[repr(C)] +struct Slice<'a, T: 'a> { + data: &'a [T; 0], + len: usize, +} + +unsafe impl<'a, T: Sync> Sync for Slice<'a, T> {} +unsafe impl<'a, T: Sync> Send for Slice<'a, T> {} + +impl Copy for Slice<'a, T> {} +impl Clone for Slice<'a, T> { + fn clone(&self) -> Self { + *self + } +} + +impl From<&'a [T]> for Slice<'a, T> { + fn from(xs: &'a [T]) -> Self { + Slice { + data: unsafe { &*(xs.as_ptr() as *const [T; 0]) }, + len: xs.len(), + } + } +} + +impl Deref for Slice<'a, T> { + type Target = [T]; + fn deref(&self) -> &[T] { + unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } + } +} + +#[repr(C)] +pub struct Buffer { + data: *mut T, + len: usize, + capacity: usize, + extend_from_slice: extern "C" fn(Buffer, Slice) -> Buffer, + drop: extern "C" fn(Buffer), +} + +unsafe impl Sync for Buffer {} +unsafe impl Send for Buffer {} + +impl Default for Buffer { + fn default() -> Self { + Self::from(vec![]) + } +} + +impl Deref for Buffer { + type Target = [T]; + fn deref(&self) -> &[T] { + unsafe { slice::from_raw_parts(self.data as *const T, self.len) } + } +} + +impl DerefMut for Buffer { + fn deref_mut(&mut self) -> &mut [T] { + unsafe { slice::from_raw_parts_mut(self.data, self.len) } + } +} + +impl Buffer { + pub(super) fn new() -> Self { + Self::default() + } + + pub(super) fn clear(&mut self) { + self.len = 0; + } + + pub(super) fn take(&mut self) -> Self { + mem::replace(self, Self::default()) + } + + pub(super) fn extend_from_slice(&mut self, xs: &[T]) { + // Fast path to avoid going through an FFI call. + if let Some(final_len) = self.len.checked_add(xs.len()) { + if final_len <= self.capacity { + let dst = unsafe { slice::from_raw_parts_mut(self.data, self.capacity) }; + dst[self.len..][..xs.len()].copy_from_slice(xs); + self.len = final_len; + return; + } + } + let b = self.take(); + *self = (b.extend_from_slice)(b, Slice::from(xs)); + } +} + +impl Write for Buffer { + fn write(&mut self, xs: &[u8]) -> io::Result { + self.extend_from_slice(xs); + Ok(xs.len()) + } + + fn write_all(&mut self, xs: &[u8]) -> io::Result<()> { + self.extend_from_slice(xs); + Ok(()) + } + + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +impl Drop for Buffer { + fn drop(&mut self) { + let b = self.take(); + (b.drop)(b); + } +} + +impl From> for Buffer { + fn from(mut v: Vec) -> Self { + let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); + mem::forget(v); + + // This utility function is nested in here because it can *only* + // be safely called on `Buffer`s created by *this* `proc_macro`. + fn to_vec(b: Buffer) -> Vec { + unsafe { + let Buffer { + data, + len, + capacity, + .. + } = b; + mem::forget(b); + Vec::from_raw_parts(data, len, capacity) + } + } + + extern "C" fn extend_from_slice(b: Buffer, xs: Slice) -> Buffer { + let mut v = to_vec(b); + v.extend_from_slice(&xs); + Buffer::from(v) + } + + extern "C" fn drop(b: Buffer) { + mem::drop(to_vec(b)); + } + + Buffer { + data, + len, + capacity, + extend_from_slice, + drop, + } + } +} diff --git a/src/libproc_macro/bridge/client.rs b/src/libproc_macro/bridge/client.rs new file mode 100644 index 0000000000000..ed27df4496256 --- /dev/null +++ b/src/libproc_macro/bridge/client.rs @@ -0,0 +1,504 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Client-side types. + +use super::*; + +macro_rules! define_handles { + ( + 'owned: $($oty:ident,)* + 'interned: $($ity:ident,)* + ) => { + #[repr(C)] + #[allow(non_snake_case)] + pub struct HandleCounters { + $($oty: AtomicUsize,)* + $($ity: AtomicUsize,)* + } + + impl HandleCounters { + // FIXME(#53451) public to work around `Cannot create local mono-item` ICE. + pub extern "C" fn get() -> &'static Self { + static COUNTERS: HandleCounters = HandleCounters { + $($oty: AtomicUsize::new(1),)* + $($ity: AtomicUsize::new(1),)* + }; + &COUNTERS + } + } + + // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`. + #[repr(C)] + #[allow(non_snake_case)] + pub(super) struct HandleStore { + $($oty: handle::OwnedStore,)* + $($ity: handle::InternedStore,)* + } + + impl HandleStore { + pub(super) fn new(handle_counters: &'static HandleCounters) -> Self { + HandleStore { + $($oty: handle::OwnedStore::new(&handle_counters.$oty),)* + $($ity: handle::InternedStore::new(&handle_counters.$ity),)* + } + } + } + + $( + #[repr(C)] + pub(crate) struct $oty(handle::Handle); + impl !Send for $oty {} + impl !Sync for $oty {} + + // Forward `Drop::drop` to the inherent `drop` method. + impl Drop for $oty { + fn drop(&mut self) { + $oty(self.0).drop(); + } + } + + impl Encode for $oty { + fn encode(self, w: &mut Writer, s: &mut S) { + let handle = self.0; + mem::forget(self); + handle.encode(w, s); + } + } + + impl DecodeMut<'_, '_, HandleStore>> + for Marked + { + fn decode(r: &mut Reader, s: &mut HandleStore>) -> Self { + s.$oty.take(handle::Handle::decode(r, &mut ())) + } + } + + impl Encode for &$oty { + fn encode(self, w: &mut Writer, s: &mut S) { + self.0.encode(w, s); + } + } + + impl Decode<'_, 's, HandleStore>> + for &'s Marked + { + fn decode(r: &mut Reader, s: &'s HandleStore>) -> Self { + &s.$oty[handle::Handle::decode(r, &mut ())] + } + } + + impl Encode for &mut $oty { + fn encode(self, w: &mut Writer, s: &mut S) { + self.0.encode(w, s); + } + } + + impl DecodeMut<'_, 's, HandleStore>> + for &'s mut Marked + { + fn decode(r: &mut Reader, s: &'s mut HandleStore>) -> Self { + &mut s.$oty[handle::Handle::decode(r, &mut ())] + } + } + + impl Encode>> + for Marked + { + fn encode(self, w: &mut Writer, s: &mut HandleStore>) { + s.$oty.alloc(self).encode(w, s); + } + } + + impl DecodeMut<'_, '_, S> for $oty { + fn decode(r: &mut Reader, s: &mut S) -> Self { + $oty(handle::Handle::decode(r, s)) + } + } + )* + + $( + #[repr(C)] + #[derive(Copy, Clone, PartialEq, Eq, Hash)] + pub(crate) struct $ity(handle::Handle); + impl !Send for $ity {} + impl !Sync for $ity {} + + impl Encode for $ity { + fn encode(self, w: &mut Writer, s: &mut S) { + self.0.encode(w, s); + } + } + + impl DecodeMut<'_, '_, HandleStore>> + for Marked + { + fn decode(r: &mut Reader, s: &mut HandleStore>) -> Self { + s.$ity.copy(handle::Handle::decode(r, &mut ())) + } + } + + impl Encode>> + for Marked + { + fn encode(self, w: &mut Writer, s: &mut HandleStore>) { + s.$ity.alloc(self).encode(w, s); + } + } + + impl DecodeMut<'_, '_, S> for $ity { + fn decode(r: &mut Reader, s: &mut S) -> Self { + $ity(handle::Handle::decode(r, s)) + } + } + )* + } +} +define_handles! { + 'owned: + TokenStream, + TokenStreamBuilder, + TokenStreamIter, + Group, + Literal, + SourceFile, + MultiSpan, + Diagnostic, + + 'interned: + Punct, + Ident, + Span, +} + +// FIXME(eddyb) generate these impls by pattern-matching on the +// names of methods - also could use the presence of `fn drop` +// to distinguish between 'owned and 'interned, above. +// Alternatively, special 'modes" could be listed of types in with_api +// instead of pattern matching on methods, here and in server decl. + +impl Clone for TokenStream { + fn clone(&self) -> Self { + self.clone() + } +} + +impl Clone for TokenStreamIter { + fn clone(&self) -> Self { + self.clone() + } +} + +impl Clone for Group { + fn clone(&self) -> Self { + self.clone() + } +} + +impl Clone for Literal { + fn clone(&self) -> Self { + self.clone() + } +} + +// FIXME(eddyb) `Literal` should not expose internal `Debug` impls. +impl fmt::Debug for Literal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&self.debug()) + } +} + +impl Clone for SourceFile { + fn clone(&self) -> Self { + self.clone() + } +} + +impl fmt::Debug for Span { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + f.write_str(&self.debug()) + } +} + +macro_rules! define_client_side { + ($($name:ident { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)*) $(-> $ret_ty:ty)*;)* + }),* $(,)*) => { + $(impl $name { + $(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)* { + Bridge::with(|bridge| { + let mut b = bridge.cached_buffer.take(); + + b.clear(); + api_tags::Method::$name(api_tags::$name::$method).encode(&mut b, &mut ()); + reverse_encode!(b; $($arg),*); + + b = bridge.dispatch.call(b); + + let r = Result::<_, PanicMessage>::decode(&mut &b[..], &mut ()); + + bridge.cached_buffer = b; + + r.unwrap_or_else(|e| panic::resume_unwind(e.into())) + }) + })* + })* + } +} +with_api!(self, self, define_client_side); + +enum BridgeState<'a> { + /// No server is currently connected to this client. + NotConnected, + + /// A server is connected and available for requests. + Connected(Bridge<'a>), + + /// Access to the bridge is being exclusively acquired + /// (e.g. during `BridgeState::with`). + InUse, +} + +enum BridgeStateL {} + +impl<'a> scoped_cell::ApplyL<'a> for BridgeStateL { + type Out = BridgeState<'a>; +} + +thread_local! { + static BRIDGE_STATE: scoped_cell::ScopedCell = + scoped_cell::ScopedCell::new(BridgeState::NotConnected); +} + +impl BridgeState<'_> { + /// Take exclusive control of the thread-local + /// `BridgeState`, and pass it to `f`, mutably. + /// The state will be restored after `f` exits, even + /// by panic, including modifications made to it by `f`. + /// + /// NB: while `f` is running, the thread-local state + /// is `BridgeState::InUse`. + fn with(f: impl FnOnce(&mut BridgeState) -> R) -> R { + BRIDGE_STATE.with(|state| { + state.replace(BridgeState::InUse, |mut state| { + // FIXME(#52812) pass `f` directly to `replace` when `RefMutL` is gone + f(&mut *state) + }) + }) + } +} + +impl Bridge<'_> { + fn enter(self, f: impl FnOnce() -> R) -> R { + // Hide the default panic output within `proc_macro` expansions. + // NB. the server can't do this because it may use a different libstd. + static HIDE_PANICS_DURING_EXPANSION: Once = Once::new(); + HIDE_PANICS_DURING_EXPANSION.call_once(|| { + let prev = panic::take_hook(); + panic::set_hook(Box::new(move |info| { + let hide = BridgeState::with(|state| match state { + BridgeState::NotConnected => false, + BridgeState::Connected(_) | BridgeState::InUse => true, + }); + if !hide { + prev(info) + } + })); + }); + + BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f)) + } + + fn with(f: impl FnOnce(&mut Bridge) -> R) -> R { + BridgeState::with(|state| match state { + BridgeState::NotConnected => { + panic!("procedural macro API is used outside of a procedural macro"); + } + BridgeState::InUse => { + panic!("procedural macro API is used while it's already in use"); + } + BridgeState::Connected(bridge) => f(bridge), + }) + } +} + +/// A client-side "global object" (usually a function pointer), +/// which may be using a different `proc_macro` from the one +/// used by the server, but can be interacted with compatibly. +/// +/// NB: `F` must have FFI-friendly memory layout (e.g. a pointer). +/// The call ABI of function pointers used for `F` doesn't +/// need to match between server and client, since it's only +/// passed between them and (eventually) called by the client. +#[repr(C)] +#[derive(Copy, Clone)] +pub struct Client { + pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters, + pub(super) run: extern "C" fn(Bridge, F) -> Buffer, + pub(super) f: F, +} + +// FIXME(#53451) public to work around `Cannot create local mono-item` ICE, +// affecting not only the function itself, but also the `BridgeState` `thread_local!`. +pub extern "C" fn __run_expand1( + mut bridge: Bridge, + f: fn(::TokenStream) -> ::TokenStream, +) -> Buffer { + // The initial `cached_buffer` contains the input. + let mut b = bridge.cached_buffer.take(); + + panic::catch_unwind(panic::AssertUnwindSafe(|| { + bridge.enter(|| { + let reader = &mut &b[..]; + let input = TokenStream::decode(reader, &mut ()); + + // Put the `cached_buffer` back in the `Bridge`, for requests. + Bridge::with(|bridge| bridge.cached_buffer = b.take()); + + let output = f(::TokenStream(input)).0; + + // Take the `cached_buffer` back out, for the output value. + b = Bridge::with(|bridge| bridge.cached_buffer.take()); + + // HACK(eddyb) Separate encoding a success value (`Ok(output)`) + // from encoding a panic (`Err(e: PanicMessage)`) to avoid + // having handles outside the `bridge.enter(|| ...)` scope, and + // to catch panics that could happen while encoding the success. + // + // Note that panics should be impossible beyond this point, but + // this is defensively trying to avoid any accidental panicking + // reaching the `extern "C"` (which should `abort` but may not + // at the moment, so this is also potentially preventing UB). + b.clear(); + Ok::<_, ()>(output).encode(&mut b, &mut ()); + }) + })) + .map_err(PanicMessage::from) + .unwrap_or_else(|e| { + b.clear(); + Err::<(), _>(e).encode(&mut b, &mut ()); + }); + b +} + +impl Client ::TokenStream> { + pub const fn expand1(f: fn(::TokenStream) -> ::TokenStream) -> Self { + Client { + get_handle_counters: HandleCounters::get, + run: __run_expand1, + f, + } + } +} + +// FIXME(#53451) public to work around `Cannot create local mono-item` ICE, +// affecting not only the function itself, but also the `BridgeState` `thread_local!`. +pub extern "C" fn __run_expand2( + mut bridge: Bridge, + f: fn(::TokenStream, ::TokenStream) -> ::TokenStream, +) -> Buffer { + // The initial `cached_buffer` contains the input. + let mut b = bridge.cached_buffer.take(); + + panic::catch_unwind(panic::AssertUnwindSafe(|| { + bridge.enter(|| { + let reader = &mut &b[..]; + let input = TokenStream::decode(reader, &mut ()); + let input2 = TokenStream::decode(reader, &mut ()); + + // Put the `cached_buffer` back in the `Bridge`, for requests. + Bridge::with(|bridge| bridge.cached_buffer = b.take()); + + let output = f(::TokenStream(input), ::TokenStream(input2)).0; + + // Take the `cached_buffer` back out, for the output value. + b = Bridge::with(|bridge| bridge.cached_buffer.take()); + + // HACK(eddyb) Separate encoding a success value (`Ok(output)`) + // from encoding a panic (`Err(e: PanicMessage)`) to avoid + // having handles outside the `bridge.enter(|| ...)` scope, and + // to catch panics that could happen while encoding the success. + // + // Note that panics should be impossible beyond this point, but + // this is defensively trying to avoid any accidental panicking + // reaching the `extern "C"` (which should `abort` but may not + // at the moment, so this is also potentially preventing UB). + b.clear(); + Ok::<_, ()>(output).encode(&mut b, &mut ()); + }) + })) + .map_err(PanicMessage::from) + .unwrap_or_else(|e| { + b.clear(); + Err::<(), _>(e).encode(&mut b, &mut ()); + }); + b +} + +impl Client ::TokenStream> { + pub const fn expand2(f: fn(::TokenStream, ::TokenStream) -> ::TokenStream) -> Self { + Client { + get_handle_counters: HandleCounters::get, + run: __run_expand2, + f, + } + } +} + +#[repr(C)] +#[derive(Copy, Clone)] +pub enum ProcMacro { + CustomDerive { + trait_name: &'static str, + attributes: &'static [&'static str], + client: Client ::TokenStream>, + }, + + Attr { + name: &'static str, + client: Client ::TokenStream>, + }, + + Bang { + name: &'static str, + client: Client ::TokenStream>, + }, +} + +impl ProcMacro { + pub const fn custom_derive( + trait_name: &'static str, + attributes: &'static [&'static str], + expand: fn(::TokenStream) -> ::TokenStream, + ) -> Self { + ProcMacro::CustomDerive { + trait_name, + attributes, + client: Client::expand1(expand), + } + } + + pub const fn attr( + name: &'static str, + expand: fn(::TokenStream, ::TokenStream) -> ::TokenStream, + ) -> Self { + ProcMacro::Attr { + name, + client: Client::expand2(expand), + } + } + + pub const fn bang(name: &'static str, expand: fn(::TokenStream) -> ::TokenStream) -> Self { + ProcMacro::Bang { + name, + client: Client::expand1(expand), + } + } +} diff --git a/src/libproc_macro/bridge/closure.rs b/src/libproc_macro/bridge/closure.rs new file mode 100644 index 0000000000000..92fe7baae097c --- /dev/null +++ b/src/libproc_macro/bridge/closure.rs @@ -0,0 +1,42 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`. + +#[repr(C)] +pub struct Closure<'a, A, R> { + call: unsafe extern "C" fn(&mut Env, A) -> R, + env: &'a mut Env, +} + +extern "C" { + type Env; +} + +impl<'a, A, R> !Sync for Closure<'a, A, R> {} +impl<'a, A, R> !Send for Closure<'a, A, R> {} + +impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> { + fn from(f: &'a mut F) -> Self { + unsafe extern "C" fn call R>(env: &mut Env, arg: A) -> R { + (*(env as *mut _ as *mut F))(arg) + } + Closure { + call: call::, + env: unsafe { &mut *(f as *mut _ as *mut Env) }, + } + } +} + +impl<'a, A, R> Closure<'a, A, R> { + pub fn call(&mut self, arg: A) -> R { + unsafe { (self.call)(self.env, arg) } + } +} diff --git a/src/libproc_macro/bridge/handle.rs b/src/libproc_macro/bridge/handle.rs new file mode 100644 index 0000000000000..5c91a1408a42f --- /dev/null +++ b/src/libproc_macro/bridge/handle.rs @@ -0,0 +1,92 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Server-side handles and storage for per-handle data. + +use std::collections::{BTreeMap, HashMap}; +use std::hash::Hash; +use std::num::NonZeroU32; +use std::ops::{Index, IndexMut}; +use std::sync::atomic::{AtomicUsize, Ordering}; + +pub(super) type Handle = NonZeroU32; + +pub(super) struct OwnedStore { + counter: &'static AtomicUsize, + data: BTreeMap, +} + +impl OwnedStore { + pub(super) fn new(counter: &'static AtomicUsize) -> Self { + // Ensure the handle counter isn't 0, which would panic later, + // when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`. + assert_ne!(counter.load(Ordering::SeqCst), 0); + + OwnedStore { + counter, + data: BTreeMap::new(), + } + } +} + +impl OwnedStore { + pub(super) fn alloc(&mut self, x: T) -> Handle { + let counter = self.counter.fetch_add(1, Ordering::SeqCst); + let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed"); + assert!(self.data.insert(handle, x).is_none()); + handle + } + + pub(super) fn take(&mut self, h: Handle) -> T { + self.data + .remove(&h) + .expect("use-after-free in `proc_macro` handle") + } +} + +impl Index for OwnedStore { + type Output = T; + fn index(&self, h: Handle) -> &T { + self.data + .get(&h) + .expect("use-after-free in `proc_macro` handle") + } +} + +impl IndexMut for OwnedStore { + fn index_mut(&mut self, h: Handle) -> &mut T { + self.data + .get_mut(&h) + .expect("use-after-free in `proc_macro` handle") + } +} + +pub(super) struct InternedStore { + owned: OwnedStore, + interner: HashMap, +} + +impl InternedStore { + pub(super) fn new(counter: &'static AtomicUsize) -> Self { + InternedStore { + owned: OwnedStore::new(counter), + interner: HashMap::new(), + } + } + + pub(super) fn alloc(&mut self, x: T) -> Handle { + let owned = &mut self.owned; + *self.interner.entry(x).or_insert_with(|| owned.alloc(x)) + } + + pub(super) fn copy(&mut self, h: Handle) -> T { + self.owned[h] + } +} diff --git a/src/libproc_macro/bridge/mod.rs b/src/libproc_macro/bridge/mod.rs new file mode 100644 index 0000000000000..f03c63fc04c81 --- /dev/null +++ b/src/libproc_macro/bridge/mod.rs @@ -0,0 +1,413 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Internal interface for communicating between a `proc_macro` client +//! (a proc macro crate) and a `proc_macro` server (a compiler front-end). +//! +//! Serialization (with C ABI buffers) and unique integer handles are employed +//! to allow safely interfacing between two copies of `proc_macro` built +//! (from the same source) by different compilers with potentially mismatching +//! Rust ABIs (e.g. stage0/bin/rustc vs stage1/bin/rustc during bootstrap). + +#![deny(unsafe_code)] + +use std::fmt; +use std::hash::Hash; +use std::marker; +use std::mem; +use std::ops::Bound; +use std::panic; +use std::sync::atomic::AtomicUsize; +use std::sync::Once; +use std::thread; +use {Delimiter, Level, LineColumn, Spacing}; + +/// Higher-order macro describing the server RPC API, allowing automatic +/// generation of type-safe Rust APIs, both client-side and server-side. +/// +/// `with_api!(MySelf, my_self, my_macro)` expands to: +/// ```rust,ignore (pseudo-code) +/// my_macro! { +/// // ... +/// Literal { +/// // ... +/// fn character(ch: char) -> MySelf::Literal; +/// // ... +/// fn span(my_self: &MySelf::Literal) -> MySelf::Span; +/// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span); +/// }, +/// // ... +/// } +/// ``` +/// +/// The first two arguments serve to customize the arguments names +/// and argument/return types, to enable several different usecases: +/// +/// If `my_self` is just `self`, then each `fn` signature can be used +/// as-is for a method. If it's anything else (`self_` in practice), +/// then the signatures don't have a special `self` argument, and +/// can, therefore, have a different one introduced. +/// +/// If `MySelf` is just `Self`, then the types are only valid inside +/// a trait or a trait impl, where the trait has associated types +/// for each of the API types. If non-associated types are desired, +/// a module name (`self` in practice) can be used instead of `Self`. +macro_rules! with_api { + ($S:ident, $self:ident, $m:ident) => { + $m! { + TokenStream { + fn drop($self: $S::TokenStream); + fn clone($self: &$S::TokenStream) -> $S::TokenStream; + fn new() -> $S::TokenStream; + fn is_empty($self: &$S::TokenStream) -> bool; + fn from_str(src: &str) -> $S::TokenStream; + fn to_string($self: &$S::TokenStream) -> String; + fn from_token_tree( + tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>, + ) -> $S::TokenStream; + fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter; + }, + TokenStreamBuilder { + fn drop($self: $S::TokenStreamBuilder); + fn new() -> $S::TokenStreamBuilder; + fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream); + fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream; + }, + TokenStreamIter { + fn drop($self: $S::TokenStreamIter); + fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter; + fn next( + $self: &mut $S::TokenStreamIter, + ) -> Option>; + }, + Group { + fn drop($self: $S::Group); + fn clone($self: &$S::Group) -> $S::Group; + fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group; + fn delimiter($self: &$S::Group) -> Delimiter; + fn stream($self: &$S::Group) -> $S::TokenStream; + fn span($self: &$S::Group) -> $S::Span; + fn span_open($self: &$S::Group) -> $S::Span; + fn span_close($self: &$S::Group) -> $S::Span; + fn set_span($self: &mut $S::Group, span: $S::Span); + }, + Punct { + fn new(ch: char, spacing: Spacing) -> $S::Punct; + fn as_char($self: $S::Punct) -> char; + fn spacing($self: $S::Punct) -> Spacing; + fn span($self: $S::Punct) -> $S::Span; + fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct; + }, + Ident { + fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident; + fn span($self: $S::Ident) -> $S::Span; + fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident; + }, + Literal { + fn drop($self: $S::Literal); + fn clone($self: &$S::Literal) -> $S::Literal; + // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. + fn debug($self: &$S::Literal) -> String; + fn integer(n: &str) -> $S::Literal; + fn typed_integer(n: &str, kind: &str) -> $S::Literal; + fn float(n: &str) -> $S::Literal; + fn f32(n: &str) -> $S::Literal; + fn f64(n: &str) -> $S::Literal; + fn string(string: &str) -> $S::Literal; + fn character(ch: char) -> $S::Literal; + fn byte_string(bytes: &[u8]) -> $S::Literal; + fn span($self: &$S::Literal) -> $S::Span; + fn set_span($self: &mut $S::Literal, span: $S::Span); + fn subspan( + $self: &$S::Literal, + start: Bound, + end: Bound, + ) -> Option<$S::Span>; + }, + SourceFile { + fn drop($self: $S::SourceFile); + fn clone($self: &$S::SourceFile) -> $S::SourceFile; + fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool; + fn path($self: &$S::SourceFile) -> String; + fn is_real($self: &$S::SourceFile) -> bool; + }, + MultiSpan { + fn drop($self: $S::MultiSpan); + fn new() -> $S::MultiSpan; + fn push($self: &mut $S::MultiSpan, span: $S::Span); + }, + Diagnostic { + fn drop($self: $S::Diagnostic); + fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic; + fn sub( + $self: &mut $S::Diagnostic, + level: Level, + msg: &str, + span: $S::MultiSpan, + ); + fn emit($self: $S::Diagnostic); + }, + Span { + fn debug($self: $S::Span) -> String; + fn def_site() -> $S::Span; + fn call_site() -> $S::Span; + fn source_file($self: $S::Span) -> $S::SourceFile; + fn parent($self: $S::Span) -> Option<$S::Span>; + fn source($self: $S::Span) -> $S::Span; + fn start($self: $S::Span) -> LineColumn; + fn end($self: $S::Span) -> LineColumn; + fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; + fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; + }, + } + }; +} + +// FIXME(eddyb) this calls `encode` for each argument, but in reverse, +// to avoid borrow conflicts from borrows started by `&mut` arguments. +macro_rules! reverse_encode { + ($writer:ident;) => {}; + ($writer:ident; $first:ident $(, $rest:ident)*) => { + reverse_encode!($writer; $($rest),*); + $first.encode(&mut $writer, &mut ()); + } +} + +// FIXME(eddyb) this calls `decode` for each argument, but in reverse, +// to avoid borrow conflicts from borrows started by `&mut` arguments. +macro_rules! reverse_decode { + ($reader:ident, $s:ident;) => {}; + ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => { + reverse_decode!($reader, $s; $($rest: $rest_ty),*); + let $first = <$first_ty>::decode(&mut $reader, $s); + } +} + +#[allow(unsafe_code)] +mod buffer; +#[forbid(unsafe_code)] +pub mod client; +#[allow(unsafe_code)] +mod closure; +#[forbid(unsafe_code)] +mod handle; +#[macro_use] +#[forbid(unsafe_code)] +mod rpc; +#[allow(unsafe_code)] +mod scoped_cell; +#[forbid(unsafe_code)] +pub mod server; + +use self::buffer::Buffer; +pub use self::rpc::PanicMessage; +use self::rpc::{Decode, DecodeMut, Encode, Reader, Writer}; + +/// An active connection between a server and a client. +/// The server creates the bridge (`Bridge::run_server` in `server.rs`), +/// then passes it to the client through the function pointer in the `run` +/// field of `client::Client`. The client holds its copy of the `Bridge` +/// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`). +#[repr(C)] +pub struct Bridge<'a> { + /// Reusable buffer (only `clear`-ed, never shrunk), primarily + /// used for making requests, but also for passing input to client. + cached_buffer: Buffer, + + /// Server-side function that the client uses to make requests. + dispatch: closure::Closure<'a, Buffer, Buffer>, +} + +impl<'a> !Sync for Bridge<'a> {} +impl<'a> !Send for Bridge<'a> {} + +#[forbid(unsafe_code)] +#[allow(non_camel_case_types)] +mod api_tags { + use super::rpc::{DecodeMut, Encode, Reader, Writer}; + + macro_rules! declare_tags { + ($($name:ident { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)*) $(-> $ret_ty:ty)*;)* + }),* $(,)*) => { + $( + pub(super) enum $name { + $($method),* + } + rpc_encode_decode!(enum $name { $($method),* }); + )* + + + pub(super) enum Method { + $($name($name)),* + } + rpc_encode_decode!(enum Method { $($name(m)),* }); + } + } + with_api!(self, self, declare_tags); +} + +/// Helper to wrap associated types to allow trait impl dispatch. +/// That is, normally a pair of impls for `T::Foo` and `T::Bar` +/// can overlap, but if the impls are, instead, on types like +/// `Marked` and `Marked`, they can't. +trait Mark { + type Unmarked; + fn mark(unmarked: Self::Unmarked) -> Self; +} + +/// Unwrap types wrapped by `Mark::mark` (see `Mark` for details). +trait Unmark { + type Unmarked; + fn unmark(self) -> Self::Unmarked; +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +struct Marked { + value: T, + _marker: marker::PhantomData, +} + +impl Mark for Marked { + type Unmarked = T; + fn mark(unmarked: Self::Unmarked) -> Self { + Marked { + value: unmarked, + _marker: marker::PhantomData, + } + } +} +impl Unmark for Marked { + type Unmarked = T; + fn unmark(self) -> Self::Unmarked { + self.value + } +} +impl Unmark for &'a Marked { + type Unmarked = &'a T; + fn unmark(self) -> Self::Unmarked { + &self.value + } +} +impl Unmark for &'a mut Marked { + type Unmarked = &'a mut T; + fn unmark(self) -> Self::Unmarked { + &mut self.value + } +} + +impl Mark for Option { + type Unmarked = Option; + fn mark(unmarked: Self::Unmarked) -> Self { + unmarked.map(T::mark) + } +} +impl Unmark for Option { + type Unmarked = Option; + fn unmark(self) -> Self::Unmarked { + self.map(T::unmark) + } +} + +macro_rules! mark_noop { + ($($ty:ty),* $(,)*) => { + $( + impl Mark for $ty { + type Unmarked = Self; + fn mark(unmarked: Self::Unmarked) -> Self { + unmarked + } + } + impl Unmark for $ty { + type Unmarked = Self; + fn unmark(self) -> Self::Unmarked { + self + } + } + )* + } +} +mark_noop! { + (), + bool, + char, + &'a [u8], + &'a str, + String, + Delimiter, + Level, + LineColumn, + Spacing, + Bound, +} + +rpc_encode_decode!( + enum Delimiter { + Parenthesis, + Brace, + Bracket, + None, + } +); +rpc_encode_decode!( + enum Level { + Error, + Warning, + Note, + Help, + } +); +rpc_encode_decode!(struct LineColumn { line, column }); +rpc_encode_decode!( + enum Spacing { + Alone, + Joint, + } +); + +#[derive(Clone)] +pub enum TokenTree { + Group(G), + Punct(P), + Ident(I), + Literal(L), +} + +impl Mark for TokenTree { + type Unmarked = TokenTree; + fn mark(unmarked: Self::Unmarked) -> Self { + match unmarked { + TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)), + TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)), + TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)), + TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)), + } + } +} +impl Unmark for TokenTree { + type Unmarked = TokenTree; + fn unmark(self) -> Self::Unmarked { + match self { + TokenTree::Group(tt) => TokenTree::Group(tt.unmark()), + TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()), + TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()), + TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()), + } + } +} + +rpc_encode_decode!( + enum TokenTree { + Group(tt), + Punct(tt), + Ident(tt), + Literal(tt), + } +); diff --git a/src/libproc_macro/bridge/rpc.rs b/src/libproc_macro/bridge/rpc.rs new file mode 100644 index 0000000000000..fafc3d0074065 --- /dev/null +++ b/src/libproc_macro/bridge/rpc.rs @@ -0,0 +1,319 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Serialization for client<->server communication. + +use std::any::Any; +use std::char; +use std::io::Write; +use std::num::NonZeroU32; +use std::ops::Bound; +use std::str; + +pub(super) type Writer = super::buffer::Buffer; + +pub(super) trait Encode: Sized { + fn encode(self, w: &mut Writer, s: &mut S); +} + +pub(super) type Reader<'a> = &'a [u8]; + +pub(super) trait Decode<'a, 's, S>: Sized { + fn decode(r: &mut Reader<'a>, s: &'s S) -> Self; +} + +pub(super) trait DecodeMut<'a, 's, S>: Sized { + fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self; +} + +macro_rules! rpc_encode_decode { + (uleb128 $ty:ty) => { + impl Encode for $ty { + fn encode(mut self, w: &mut Writer, s: &mut S) { + let mut byte = 0x80; + while byte & 0x80 != 0 { + byte = (self & 0x7f) as u8; + self >>= 7; + if self != 0 { + byte |= 0x80; + } + byte.encode(w, s); + } + } + } + + impl DecodeMut<'_, '_, S> for $ty { + fn decode(r: &mut Reader, s: &mut S) -> Self { + let mut byte = 0x80; + let mut v = 0; + let mut shift = 0; + while byte & 0x80 != 0 { + byte = u8::decode(r, s); + v |= ((byte & 0x7f) as Self) << shift; + shift += 7; + } + v + } + } + }; + (struct $name:ident { $($field:ident),* $(,)* }) => { + impl Encode for $name { + fn encode(self, w: &mut Writer, s: &mut S) { + $(self.$field.encode(w, s);)* + } + } + + impl DecodeMut<'_, '_, S> for $name { + fn decode(r: &mut Reader, s: &mut S) -> Self { + $name { + $($field: DecodeMut::decode(r, s)),* + } + } + } + }; + (enum $name:ident $(<$($T:ident),+>)* { $($variant:ident $(($field:ident))*),* $(,)* }) => { + impl),+)*> Encode for $name $(<$($T),+>)* { + fn encode(self, w: &mut Writer, s: &mut S) { + // HACK(eddyb) `Tag` enum duplicated between the + // two impls as there's no other place to stash it. + #[repr(u8)] enum Tag { $($variant),* } + #[allow(non_upper_case_globals)] + impl Tag { $(const $variant: u8 = Tag::$variant as u8;)* } + + match self { + $($name::$variant $(($field))* => { + ::$variant.encode(w, s); + $($field.encode(w, s);)* + })* + } + } + } + + impl DecodeMut<'a, 's, S>),+)*> DecodeMut<'a, '_, S> + for $name $(<$($T),+>)* + { + fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { + // HACK(eddyb) `Tag` enum duplicated between the + // two impls as there's no other place to stash it. + #[repr(u8)] enum Tag { $($variant),* } + #[allow(non_upper_case_globals)] + impl Tag { $(const $variant: u8 = Tag::$variant as u8;)* } + + match u8::decode(r, s) { + $(::$variant => { + $(let $field = DecodeMut::decode(r, s);)* + $name::$variant $(($field))* + })* + _ => unreachable!(), + } + } + } + } +} + +impl Encode for () { + fn encode(self, _: &mut Writer, _: &mut S) {} +} + +impl DecodeMut<'_, '_, S> for () { + fn decode(_: &mut Reader, _: &mut S) -> Self {} +} + +impl Encode for u8 { + fn encode(self, w: &mut Writer, _: &mut S) { + w.write_all(&[self]).unwrap(); + } +} + +impl DecodeMut<'_, '_, S> for u8 { + fn decode(r: &mut Reader, _: &mut S) -> Self { + let x = r[0]; + *r = &r[1..]; + x + } +} + +rpc_encode_decode!(uleb128 u32); +rpc_encode_decode!(uleb128 usize); + +impl Encode for bool { + fn encode(self, w: &mut Writer, s: &mut S) { + (self as u8).encode(w, s); + } +} + +impl DecodeMut<'_, '_, S> for bool { + fn decode(r: &mut Reader, s: &mut S) -> Self { + match u8::decode(r, s) { + 0 => false, + 1 => true, + _ => unreachable!(), + } + } +} + +impl Encode for char { + fn encode(self, w: &mut Writer, s: &mut S) { + (self as u32).encode(w, s); + } +} + +impl DecodeMut<'_, '_, S> for char { + fn decode(r: &mut Reader, s: &mut S) -> Self { + char::from_u32(u32::decode(r, s)).unwrap() + } +} + +impl Encode for NonZeroU32 { + fn encode(self, w: &mut Writer, s: &mut S) { + self.get().encode(w, s); + } +} + +impl DecodeMut<'_, '_, S> for NonZeroU32 { + fn decode(r: &mut Reader, s: &mut S) -> Self { + Self::new(u32::decode(r, s)).unwrap() + } +} + +impl, B: Encode> Encode for (A, B) { + fn encode(self, w: &mut Writer, s: &mut S) { + self.0.encode(w, s); + self.1.encode(w, s); + } +} + +impl DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> + for (A, B) +{ + fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { + (DecodeMut::decode(r, s), DecodeMut::decode(r, s)) + } +} + +rpc_encode_decode!( + enum Bound { + Included(x), + Excluded(x), + Unbounded, + } +); + +rpc_encode_decode!( + enum Option { + None, + Some(x), + } +); + +rpc_encode_decode!( + enum Result { + Ok(x), + Err(e), + } +); + +impl Encode for &[u8] { + fn encode(self, w: &mut Writer, s: &mut S) { + self.len().encode(w, s); + w.write_all(self).unwrap(); + } +} + +impl DecodeMut<'a, '_, S> for &'a [u8] { + fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { + let len = usize::decode(r, s); + let xs = &r[..len]; + *r = &r[len..]; + xs + } +} + +impl Encode for &str { + fn encode(self, w: &mut Writer, s: &mut S) { + self.as_bytes().encode(w, s); + } +} + +impl DecodeMut<'a, '_, S> for &'a str { + fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { + str::from_utf8(<&[u8]>::decode(r, s)).unwrap() + } +} + +impl Encode for String { + fn encode(self, w: &mut Writer, s: &mut S) { + self[..].encode(w, s); + } +} + +impl DecodeMut<'_, '_, S> for String { + fn decode(r: &mut Reader, s: &mut S) -> Self { + <&str>::decode(r, s).to_string() + } +} + +/// Simplied version of panic payloads, ignoring +/// types other than `&'static str` and `String`. +pub enum PanicMessage { + StaticStr(&'static str), + String(String), + Unknown, +} + +impl From> for PanicMessage { + fn from(payload: Box) -> Self { + if let Some(s) = payload.downcast_ref::<&'static str>() { + return PanicMessage::StaticStr(s); + } + if let Ok(s) = payload.downcast::() { + return PanicMessage::String(*s); + } + PanicMessage::Unknown + } +} + +impl Into> for PanicMessage { + fn into(self) -> Box { + match self { + PanicMessage::StaticStr(s) => Box::new(s), + PanicMessage::String(s) => Box::new(s), + PanicMessage::Unknown => { + struct UnknownPanicMessage; + Box::new(UnknownPanicMessage) + } + } + } +} + +impl PanicMessage { + pub fn as_str(&self) -> Option<&str> { + match self { + PanicMessage::StaticStr(s) => Some(s), + PanicMessage::String(s) => Some(s), + PanicMessage::Unknown => None, + } + } +} + +impl Encode for PanicMessage { + fn encode(self, w: &mut Writer, s: &mut S) { + self.as_str().encode(w, s); + } +} + +impl DecodeMut<'_, '_, S> for PanicMessage { + fn decode(r: &mut Reader, s: &mut S) -> Self { + match Option::::decode(r, s) { + Some(s) => PanicMessage::String(s), + None => PanicMessage::Unknown, + } + } +} diff --git a/src/libproc_macro/bridge/scoped_cell.rs b/src/libproc_macro/bridge/scoped_cell.rs new file mode 100644 index 0000000000000..51d1fece79b92 --- /dev/null +++ b/src/libproc_macro/bridge/scoped_cell.rs @@ -0,0 +1,90 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! `Cell` variant for (scoped) existential lifetimes. + +use std::cell::Cell; +use std::mem; +use std::ops::{Deref, DerefMut}; + +/// Type lambda application, with a lifetime. +pub trait ApplyL<'a> { + type Out; +} + +/// Type lambda taking a lifetime, i.e. `Lifetime -> Type`. +pub trait LambdaL: for<'a> ApplyL<'a> {} + +impl ApplyL<'a>> LambdaL for T {} + +// HACK(eddyb) work around projection limitations with a newtype +// FIXME(#52812) replace with `&'a mut >::Out` +pub struct RefMutL<'a, 'b, T: LambdaL>(&'a mut >::Out); + +impl<'a, 'b, T: LambdaL> Deref for RefMutL<'a, 'b, T> { + type Target = >::Out; + fn deref(&self) -> &Self::Target { + self.0 + } +} + +impl<'a, 'b, T: LambdaL> DerefMut for RefMutL<'a, 'b, T> { + fn deref_mut(&mut self) -> &mut Self::Target { + self.0 + } +} + +pub struct ScopedCell(Cell<>::Out>); + +impl ScopedCell { + pub const fn new(value: >::Out) -> Self { + ScopedCell(Cell::new(value)) + } + + /// Set the value in `self` to `replacement` while + /// running `f`, which gets the old value, mutably. + /// The old value will be restored after `f` exits, even + /// by panic, including modifications made to it by `f`. + pub fn replace<'a, R>( + &self, + replacement: >::Out, + f: impl for<'b, 'c> FnOnce(RefMutL<'b, 'c, T>) -> R, + ) -> R { + /// Wrapper that ensures that the cell always gets filled + /// (with the original state, optionally changed by `f`), + /// even if `f` had panicked. + struct PutBackOnDrop<'a, T: LambdaL> { + cell: &'a ScopedCell, + value: Option<>::Out>, + } + + impl<'a, T: LambdaL> Drop for PutBackOnDrop<'a, T> { + fn drop(&mut self) { + self.cell.0.set(self.value.take().unwrap()); + } + } + + let mut put_back_on_drop = PutBackOnDrop { + cell: self, + value: Some(self.0.replace(unsafe { + let erased = mem::transmute_copy(&replacement); + mem::forget(replacement); + erased + })), + }; + + f(RefMutL(put_back_on_drop.value.as_mut().unwrap())) + } + + /// Set the value in `self` to `value` while running `f`. + pub fn set<'a, R>(&self, value: >::Out, f: impl FnOnce() -> R) -> R { + self.replace(value, |_| f()) + } +} diff --git a/src/libproc_macro/bridge/server.rs b/src/libproc_macro/bridge/server.rs new file mode 100644 index 0000000000000..f500b17d1caf3 --- /dev/null +++ b/src/libproc_macro/bridge/server.rs @@ -0,0 +1,352 @@ +// Copyright 2018 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +//! Server-side traits. + +use super::*; + +// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`. +use super::client::HandleStore; + +/// Declare an associated item of one of the traits below, optionally +/// adjusting it (i.e. adding bounds to types and default bodies to methods). +macro_rules! associated_item { + (type TokenStream) => + (type TokenStream: 'static + Clone;); + (type TokenStreamBuilder) => + (type TokenStreamBuilder: 'static;); + (type TokenStreamIter) => + (type TokenStreamIter: 'static + Clone;); + (type Group) => + (type Group: 'static + Clone;); + (type Punct) => + (type Punct: 'static + Copy + Eq + Hash;); + (type Ident) => + (type Ident: 'static + Copy + Eq + Hash;); + (type Literal) => + (type Literal: 'static + Clone;); + (type SourceFile) => + (type SourceFile: 'static + Clone;); + (type MultiSpan) => + (type MultiSpan: 'static;); + (type Diagnostic) => + (type Diagnostic: 'static;); + (type Span) => + (type Span: 'static + Copy + Eq + Hash;); + (fn drop(&mut self, $arg:ident: $arg_ty:ty)) => + (fn drop(&mut self, $arg: $arg_ty) { mem::drop($arg) }); + (fn clone(&mut self, $arg:ident: $arg_ty:ty) -> $ret_ty:ty) => + (fn clone(&mut self, $arg: $arg_ty) -> $ret_ty { $arg.clone() }); + ($($item:tt)*) => ($($item)*;) +} + +macro_rules! declare_server_traits { + ($($name:ident { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)*) $(-> $ret_ty:ty)*;)* + }),* $(,)*) => { + pub trait Types { + $(associated_item!(type $name);)* + } + + $(pub trait $name: Types { + $(associated_item!(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)*);)* + })* + + pub trait Server: Types $(+ $name)* {} + impl Server for S {} + } +} +with_api!(Self, self_, declare_server_traits); + +pub(super) struct MarkedTypes(S); + +macro_rules! define_mark_types_impls { + ($($name:ident { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)*) $(-> $ret_ty:ty)*;)* + }),* $(,)*) => { + impl Types for MarkedTypes { + $(type $name = Marked;)* + } + + $(impl $name for MarkedTypes { + $(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)* { + <_>::mark($name::$method(&mut self.0, $($arg.unmark()),*)) + })* + })* + } +} +with_api!(Self, self_, define_mark_types_impls); + +struct Dispatcher { + handle_store: HandleStore, + server: S, +} + +macro_rules! define_dispatcher_impl { + ($($name:ident { + $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)*) $(-> $ret_ty:ty)*;)* + }),* $(,)*) => { + // FIXME(eddyb) `pub` only for `ExecutionStrategy` below. + pub trait DispatcherTrait { + // HACK(eddyb) these are here to allow `Self::$name` to work below. + $(type $name;)* + fn dispatch(&mut self, b: Buffer) -> Buffer; + } + + impl DispatcherTrait for Dispatcher> { + $(type $name = as Types>::$name;)* + fn dispatch(&mut self, mut b: Buffer) -> Buffer { + let Dispatcher { handle_store, server } = self; + + let mut reader = &b[..]; + match api_tags::Method::decode(&mut reader, &mut ()) { + $(api_tags::Method::$name(m) => match m { + $(api_tags::$name::$method => { + let mut call_method = || { + reverse_decode!(reader, handle_store; $($arg: $arg_ty),*); + $name::$method(server, $($arg),*) + }; + // HACK(eddyb) don't use `panic::catch_unwind` in a panic. + // If client and server happen to use the same `libstd`, + // `catch_unwind` asserts that the panic counter was 0, + // even when the closure passed to it didn't panic. + let r = if thread::panicking() { + Ok(call_method()) + } else { + panic::catch_unwind(panic::AssertUnwindSafe(call_method)) + .map_err(PanicMessage::from) + }; + + b.clear(); + r.encode(&mut b, handle_store); + })* + }),* + } + b + } + } + } +} +with_api!(Self, self_, define_dispatcher_impl); + +pub trait ExecutionStrategy { + fn run_bridge_and_client( + &self, + dispatcher: &mut impl DispatcherTrait, + input: Buffer, + run_client: extern "C" fn(Bridge, D) -> Buffer, + client_data: D, + ) -> Buffer; +} + +pub struct SameThread; + +impl ExecutionStrategy for SameThread { + fn run_bridge_and_client( + &self, + dispatcher: &mut impl DispatcherTrait, + input: Buffer, + run_client: extern "C" fn(Bridge, D) -> Buffer, + client_data: D, + ) -> Buffer { + let mut dispatch = |b| dispatcher.dispatch(b); + + run_client( + Bridge { + cached_buffer: input, + dispatch: (&mut dispatch).into(), + }, + client_data, + ) + } +} + +// NOTE(eddyb) Two implementations are provided, the second one is a bit +// faster but neither is anywhere near as fast as same-thread execution. + +pub struct CrossThread1; + +impl ExecutionStrategy for CrossThread1 { + fn run_bridge_and_client( + &self, + dispatcher: &mut impl DispatcherTrait, + input: Buffer, + run_client: extern "C" fn(Bridge, D) -> Buffer, + client_data: D, + ) -> Buffer { + use std::sync::mpsc::channel; + + let (req_tx, req_rx) = channel(); + let (res_tx, res_rx) = channel(); + + let join_handle = thread::spawn(move || { + let mut dispatch = |b| { + req_tx.send(b).unwrap(); + res_rx.recv().unwrap() + }; + + run_client( + Bridge { + cached_buffer: input, + dispatch: (&mut dispatch).into(), + }, + client_data, + ) + }); + + for b in req_rx { + res_tx.send(dispatcher.dispatch(b)).unwrap(); + } + + join_handle.join().unwrap() + } +} + +pub struct CrossThread2; + +impl ExecutionStrategy for CrossThread2 { + fn run_bridge_and_client( + &self, + dispatcher: &mut impl DispatcherTrait, + input: Buffer, + run_client: extern "C" fn(Bridge, D) -> Buffer, + client_data: D, + ) -> Buffer { + use std::sync::{Arc, Mutex}; + + enum State { + Req(T), + Res(T), + } + + let mut state = Arc::new(Mutex::new(State::Res(Buffer::new()))); + + let server_thread = thread::current(); + let state2 = state.clone(); + let join_handle = thread::spawn(move || { + let mut dispatch = |b| { + *state2.lock().unwrap() = State::Req(b); + server_thread.unpark(); + loop { + thread::park(); + if let State::Res(b) = &mut *state2.lock().unwrap() { + break b.take(); + } + } + }; + + let r = run_client( + Bridge { + cached_buffer: input, + dispatch: (&mut dispatch).into(), + }, + client_data, + ); + + // Wake up the server so it can exit the dispatch loop. + drop(state2); + server_thread.unpark(); + + r + }); + + // Check whether `state2` was dropped, to know when to stop. + while Arc::get_mut(&mut state).is_none() { + thread::park(); + let mut b = match &mut *state.lock().unwrap() { + State::Req(b) => b.take(), + _ => continue, + }; + b = dispatcher.dispatch(b.take()); + *state.lock().unwrap() = State::Res(b); + join_handle.thread().unpark(); + } + + join_handle.join().unwrap() + } +} + +fn run_server< + S: Server, + I: Encode>>, + O: for<'a, 's> DecodeMut<'a, 's, HandleStore>>, + D: Copy + Send + 'static, +>( + strategy: &impl ExecutionStrategy, + handle_counters: &'static client::HandleCounters, + server: S, + input: I, + run_client: extern "C" fn(Bridge, D) -> Buffer, + client_data: D, +) -> Result { + let mut dispatcher = Dispatcher { + handle_store: HandleStore::new(handle_counters), + server: MarkedTypes(server), + }; + + let mut b = Buffer::new(); + input.encode(&mut b, &mut dispatcher.handle_store); + + b = strategy.run_bridge_and_client(&mut dispatcher, b, run_client, client_data); + + Result::decode(&mut &b[..], &mut dispatcher.handle_store) +} + +impl client::Client ::TokenStream> { + pub fn run( + &self, + strategy: &impl ExecutionStrategy, + server: S, + input: S::TokenStream, + ) -> Result { + let client::Client { + get_handle_counters, + run, + f, + } = *self; + run_server( + strategy, + get_handle_counters(), + server, + as Types>::TokenStream::mark(input), + run, + f, + ) + .map( as Types>::TokenStream::unmark) + } +} + +impl client::Client ::TokenStream> { + pub fn run( + &self, + strategy: &impl ExecutionStrategy, + server: S, + input: S::TokenStream, + input2: S::TokenStream, + ) -> Result { + let client::Client { + get_handle_counters, + run, + f, + } = *self; + run_server( + strategy, + get_handle_counters(), + server, + ( + as Types>::TokenStream::mark(input), + as Types>::TokenStream::mark(input2), + ), + run, + f, + ) + .map( as Types>::TokenStream::unmark) + } +} diff --git a/src/libproc_macro/diagnostic.rs b/src/libproc_macro/diagnostic.rs index bf23de3943702..4234f0bcd21c1 100644 --- a/src/libproc_macro/diagnostic.rs +++ b/src/libproc_macro/diagnostic.rs @@ -10,8 +10,6 @@ use Span; -use rustc_errors as errors; - /// An enum representing a diagnostic level. #[unstable(feature = "proc_macro_diagnostic", issue = "54140")] #[derive(Copy, Clone, Debug)] @@ -180,22 +178,22 @@ impl Diagnostic { /// Emit the diagnostic. #[unstable(feature = "proc_macro_diagnostic", issue = "54140")] pub fn emit(self) { - fn to_internal(spans: Vec) -> ::syntax_pos::MultiSpan { - let spans: Vec<_> = spans.into_iter().map(|s| s.0).collect(); - ::syntax_pos::MultiSpan::from_spans(spans) + fn to_internal(spans: Vec) -> ::bridge::client::MultiSpan { + let mut multi_span = ::bridge::client::MultiSpan::new(); + for span in spans { + multi_span.push(span.0); + } + multi_span } - let level = self.level.to_internal(); - let mut diag = errors::Diagnostic::new(level, &*self.message); - diag.set_span(to_internal(self.spans)); - - for child in self.children { - let level = child.level.to_internal(); - diag.sub(level, &*child.message, to_internal(child.spans), None); + let mut diag = ::bridge::client::Diagnostic::new( + self.level, + &self.message[..], + to_internal(self.spans), + ); + for c in self.children { + diag.sub(c.level, &c.message[..], to_internal(c.spans)); } - - ::__internal::with_sess(move |sess, _| { - errors::DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit(); - }); + diag.emit(); } } diff --git a/src/libproc_macro/lib.rs b/src/libproc_macro/lib.rs index e9d2d97e3646b..49ec6b41ce756 100644 --- a/src/libproc_macro/lib.rs +++ b/src/libproc_macro/lib.rs @@ -30,9 +30,12 @@ #![feature(nll)] #![feature(rustc_private)] #![feature(staged_api)] -#![feature(lang_items)] +#![feature(const_fn)] +#![feature(extern_types)] +#![feature(in_band_lifetimes)] #![feature(optin_builtin_traits)] #![feature(non_exhaustive)] +#![feature(specialization)] #![recursion_limit="256"] @@ -41,6 +44,10 @@ extern crate syntax_pos; extern crate rustc_errors; extern crate rustc_data_structures; +#[unstable(feature = "proc_macro_internals", issue = "27812")] +#[doc(hidden)] +pub mod bridge; + #[unstable(feature = "proc_macro_internals", issue = "27812")] #[doc(hidden)] pub mod rustc; @@ -50,18 +57,11 @@ mod diagnostic; #[unstable(feature = "proc_macro_diagnostic", issue = "54140")] pub use diagnostic::{Diagnostic, Level, MultiSpan}; +use std::{fmt, iter, mem}; use std::ops::{Bound, RangeBounds}; -use std::{ascii, fmt, iter}; use std::path::PathBuf; -use rustc_data_structures::sync::Lrc; use std::str::FromStr; -use syntax::errors::DiagnosticBuilder; -use syntax::parse::{self, token}; -use syntax::symbol::Symbol; -use syntax::tokenstream::{self, DelimSpan}; -use syntax_pos::{Pos, FileName, BytePos}; - /// The main type provided by this crate, representing an abstract stream of /// tokens, or, more specifically, a sequence of token trees. /// The type provide interfaces for iterating over those token trees and, conversely, @@ -71,7 +71,7 @@ use syntax_pos::{Pos, FileName, BytePos}; /// and `#[proc_macro_derive]` definitions. #[stable(feature = "proc_macro_lib", since = "1.15.0")] #[derive(Clone)] -pub struct TokenStream(tokenstream::TokenStream); +pub struct TokenStream(bridge::client::TokenStream); #[stable(feature = "proc_macro_lib", since = "1.15.0")] impl !Send for TokenStream {} @@ -94,7 +94,7 @@ impl TokenStream { /// Returns an empty `TokenStream` containing no token trees. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn new() -> TokenStream { - TokenStream(tokenstream::TokenStream::empty()) + TokenStream(bridge::client::TokenStream::new()) } /// Checks if this `TokenStream` is empty. @@ -116,11 +116,16 @@ impl FromStr for TokenStream { type Err = LexError; fn from_str(src: &str) -> Result { - __internal::with_sess(|sess, data| { - Ok(__internal::token_stream_wrap(parse::parse_stream_from_source_str( - FileName::ProcMacroSourceCode, src.to_string(), sess, Some(data.call_site.0) - ))) - }) + Ok(TokenStream(bridge::client::TokenStream::from_str(src))) + } +} + +// NB: the bridge only provides `to_string`, implement `fmt::Display` +// based on it (the reverse of the usual relationship between the two). +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl ToString for TokenStream { + fn to_string(&self) -> String { + self.0.to_string() } } @@ -130,7 +135,7 @@ impl FromStr for TokenStream { #[stable(feature = "proc_macro_lib", since = "1.15.0")] impl fmt::Display for TokenStream { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.0.fmt(f) + f.write_str(&self.to_string()) } } @@ -150,7 +155,12 @@ pub use quote::{quote, quote_span}; #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl From for TokenStream { fn from(tree: TokenTree) -> TokenStream { - TokenStream(tree.to_internal()) + TokenStream(bridge::client::TokenStream::from_token_tree(match tree { + TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0), + TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0), + TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0), + TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0) + })) } } @@ -167,7 +177,7 @@ impl iter::FromIterator for TokenStream { #[stable(feature = "proc_macro_lib", since = "1.15.0")] impl iter::FromIterator for TokenStream { fn from_iter>(streams: I) -> Self { - let mut builder = tokenstream::TokenStreamBuilder::new(); + let mut builder = bridge::client::TokenStreamBuilder::new(); for stream in streams { builder.push(stream.0); } @@ -185,52 +195,34 @@ impl Extend for TokenStream { #[stable(feature = "token_stream_extend", since = "1.30.0")] impl Extend for TokenStream { fn extend>(&mut self, streams: I) { - self.0.extend(streams.into_iter().map(|stream| stream.0)); + // FIXME(eddyb) Use an optimized implementation if/when possible. + *self = iter::once(mem::replace(self, Self::new())).chain(streams).collect(); } } /// Public implementation details for the `TokenStream` type, such as iterators. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub mod token_stream { - use syntax::tokenstream; - use {TokenTree, TokenStream, Delimiter}; + use {bridge, Group, Ident, Literal, Punct, TokenTree, TokenStream}; /// An iterator over `TokenStream`'s `TokenTree`s. /// The iteration is "shallow", e.g. the iterator doesn't recurse into delimited groups, /// and returns whole groups as token trees. #[derive(Clone)] #[stable(feature = "proc_macro_lib2", since = "1.29.0")] - pub struct IntoIter { - cursor: tokenstream::Cursor, - stack: Vec, - } + pub struct IntoIter(bridge::client::TokenStreamIter); #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl Iterator for IntoIter { type Item = TokenTree; fn next(&mut self) -> Option { - loop { - let tree = self.stack.pop().or_else(|| { - let next = self.cursor.next_as_stream()?; - Some(TokenTree::from_internal(next, &mut self.stack)) - })?; - // HACK: The condition "dummy span + group with empty delimiter" represents an AST - // fragment approximately converted into a token stream. This may happen, for - // example, with inputs to proc macro attributes, including derives. Such "groups" - // need to flattened during iteration over stream's token trees. - // Eventually this needs to be removed in favor of keeping original token trees - // and not doing the roundtrip through AST. - if tree.span().0.is_dummy() { - if let TokenTree::Group(ref group) = tree { - if group.delimiter() == Delimiter::None { - self.cursor.insert(group.stream.clone().0); - continue - } - } - } - return Some(tree); - } + self.0.next().map(|tree| match tree { + bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)), + bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)), + bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)), + bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)), + }) } } @@ -240,7 +232,7 @@ pub mod token_stream { type IntoIter = IntoIter; fn into_iter(self) -> IntoIter { - IntoIter { cursor: self.0.trees(), stack: Vec::new() } + IntoIter(self.0.into_iter()) } } } @@ -264,7 +256,7 @@ mod quote; /// A region of source code, along with macro expansion information. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] #[derive(Copy, Clone)] -pub struct Span(syntax_pos::Span); +pub struct Span(bridge::client::Span); #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl !Send for Span {} @@ -286,7 +278,7 @@ impl Span { /// A span that resolves at the macro definition site. #[unstable(feature = "proc_macro_def_site", issue = "54724")] pub fn def_site() -> Span { - ::__internal::with_sess(|_, data| data.def_site) + Span(bridge::client::Span::def_site()) } /// The span of the invocation of the current procedural macro. @@ -295,15 +287,13 @@ impl Span { /// at the macro call site will be able to refer to them as well. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn call_site() -> Span { - ::__internal::with_sess(|_, data| data.call_site) + Span(bridge::client::Span::call_site()) } /// The original source file into which this span points. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn source_file(&self) -> SourceFile { - SourceFile { - source_file: __internal::lookup_char_pos(self.0.lo()).file, - } + SourceFile(self.0.source_file()) } /// The `Span` for the tokens in the previous macro expansion from which @@ -318,27 +308,19 @@ impl Span { /// value is the same as `*self`. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn source(&self) -> Span { - Span(self.0.source_callsite()) + Span(self.0.source()) } /// Get the starting line/column in the source file for this span. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn start(&self) -> LineColumn { - let loc = __internal::lookup_char_pos(self.0.lo()); - LineColumn { - line: loc.line, - column: loc.col.to_usize() - } + self.0.start() } /// Get the ending line/column in the source file for this span. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn end(&self) -> LineColumn { - let loc = __internal::lookup_char_pos(self.0.hi()); - LineColumn { - line: loc.line, - column: loc.col.to_usize() - } + self.0.end() } /// Create a new span encompassing `self` and `other`. @@ -346,19 +328,14 @@ impl Span { /// Returns `None` if `self` and `other` are from different files. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn join(&self, other: Span) -> Option { - let self_loc = __internal::lookup_char_pos(self.0.lo()); - let other_loc = __internal::lookup_char_pos(other.0.lo()); - - if self_loc.file.name != other_loc.file.name { return None } - - Some(Span(self.0.to(other.0))) + self.0.join(other.0).map(Span) } /// Creates a new span with the same line/column information as `self` but /// that resolves symbols as though it were at `other`. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn resolved_at(&self, other: Span) -> Span { - Span(self.0.with_ctxt(other.0.ctxt())) + Span(self.0.resolved_at(other.0)) } /// Creates a new span with the same name resolution behavior as `self` but @@ -384,10 +361,7 @@ impl Span { #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl fmt::Debug for Span { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{:?} bytes({}..{})", - self.0.ctxt(), - self.0.lo().0, - self.0.hi().0) + self.0.fmt(f) } } @@ -412,14 +386,7 @@ impl !Sync for LineColumn {} /// The source file of a given `Span`. #[unstable(feature = "proc_macro_span", issue = "54725")] #[derive(Clone)] -pub struct SourceFile { - source_file: Lrc, -} - -#[unstable(feature = "proc_macro_span", issue = "54725")] -impl !Send for SourceFile {} -#[unstable(feature = "proc_macro_span", issue = "54725")] -impl !Sync for SourceFile {} +pub struct SourceFile(bridge::client::SourceFile); impl SourceFile { /// Get the path to this source file. @@ -434,10 +401,7 @@ impl SourceFile { /// [`is_real`]: #method.is_real #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn path(&self) -> PathBuf { - match self.source_file.name { - FileName::Real(ref path) => path.clone(), - _ => PathBuf::from(self.source_file.name.to_string()) - } + PathBuf::from(self.0.path()) } /// Returns `true` if this source file is a real source file, and not generated by an external @@ -447,7 +411,7 @@ impl SourceFile { // This is a hack until intercrate spans are implemented and we can have real source files // for spans generated in external macros. // https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368 - self.source_file.is_real_file() + self.0.is_real() } } @@ -465,7 +429,7 @@ impl fmt::Debug for SourceFile { #[unstable(feature = "proc_macro_span", issue = "54725")] impl PartialEq for SourceFile { fn eq(&self, other: &Self) -> bool { - Lrc::ptr_eq(&self.source_file, &other.source_file) + self.0.eq(&other.0) } } @@ -579,18 +543,27 @@ impl From for TokenTree { } } +// NB: the bridge only provides `to_string`, implement `fmt::Display` +// based on it (the reverse of the usual relationship between the two). +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl ToString for TokenTree { + fn to_string(&self) -> String { + match *self { + TokenTree::Group(ref t) => t.to_string(), + TokenTree::Ident(ref t) => t.to_string(), + TokenTree::Punct(ref t) => t.to_string(), + TokenTree::Literal(ref t) => t.to_string(), + } + } +} + /// Prints the token tree as a string that is supposed to be losslessly convertible back /// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s /// with `Delimiter::None` delimiters and negative numeric literals. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl fmt::Display for TokenTree { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - match *self { - TokenTree::Group(ref t) => t.fmt(f), - TokenTree::Ident(ref t) => t.fmt(f), - TokenTree::Punct(ref t) => t.fmt(f), - TokenTree::Literal(ref t) => t.fmt(f), - } + f.write_str(&self.to_string()) } } @@ -599,11 +572,7 @@ impl fmt::Display for TokenTree { /// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s. #[derive(Clone)] #[stable(feature = "proc_macro_lib2", since = "1.29.0")] -pub struct Group { - delimiter: Delimiter, - stream: TokenStream, - span: DelimSpan, -} +pub struct Group(bridge::client::Group); #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl !Send for Group {} @@ -640,17 +609,13 @@ impl Group { /// method below. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { - Group { - delimiter: delimiter, - stream: stream, - span: DelimSpan::from_single(Span::call_site().0), - } + Group(bridge::client::Group::new(delimiter, stream.0)) } /// Returns the delimiter of this `Group` #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn delimiter(&self) -> Delimiter { - self.delimiter + self.0.delimiter() } /// Returns the `TokenStream` of tokens that are delimited in this `Group`. @@ -659,7 +624,7 @@ impl Group { /// returned above. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn stream(&self) -> TokenStream { - self.stream.clone() + TokenStream(self.0.stream()) } /// Returns the span for the delimiters of this token stream, spanning the @@ -671,7 +636,7 @@ impl Group { /// ``` #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn span(&self) -> Span { - Span(self.span.entire()) + Span(self.0.span()) } /// Returns the span pointing to the opening delimiter of this group. @@ -682,7 +647,7 @@ impl Group { /// ``` #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn span_open(&self) -> Span { - Span(self.span.open) + Span(self.0.span_open()) } /// Returns the span pointing to the closing delimiter of this group. @@ -693,7 +658,7 @@ impl Group { /// ``` #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn span_close(&self) -> Span { - Span(self.span.close) + Span(self.0.span_close()) } /// Configures the span for this `Group`'s delimiters, but not its internal @@ -704,7 +669,16 @@ impl Group { /// tokens at the level of the `Group`. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn set_span(&mut self, span: Span) { - self.span = DelimSpan::from_single(span.0); + self.0.set_span(span.0); + } +} + +// NB: the bridge only provides `to_string`, implement `fmt::Display` +// based on it (the reverse of the usual relationship between the two). +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl ToString for Group { + fn to_string(&self) -> String { + TokenStream::from(TokenTree::from(self.clone())).to_string() } } @@ -714,7 +688,7 @@ impl Group { #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl fmt::Display for Group { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - TokenStream::from(TokenTree::from(self.clone())).fmt(f) + f.write_str(&self.to_string()) } } @@ -735,11 +709,7 @@ impl fmt::Debug for Group { /// forms of `Spacing` returned. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] #[derive(Clone)] -pub struct Punct { - ch: char, - spacing: Spacing, - span: Span, -} +pub struct Punct(bridge::client::Punct); #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl !Send for Punct {} @@ -774,17 +744,13 @@ impl Punct { if !LEGAL_CHARS.contains(&ch) { panic!("unsupported character `{:?}`", ch) } - Punct { - ch: ch, - spacing: spacing, - span: Span::call_site(), - } + Punct(bridge::client::Punct::new(ch, spacing)) } /// Returns the value of this punctuation character as `char`. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn as_char(&self) -> char { - self.ch + self.0.as_char() } /// Returns the spacing of this punctuation character, indicating whether it's immediately @@ -793,19 +759,28 @@ impl Punct { /// (`Alone`) so the operator has certainly ended. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn spacing(&self) -> Spacing { - self.spacing + self.0.spacing() } /// Returns the span for this punctuation character. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn span(&self) -> Span { - self.span + Span(self.0.span()) } /// Configure the span for this punctuation character. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn set_span(&mut self, span: Span) { - self.span = span; + self.0 = self.0.with_span(span.0); + } +} + +// NB: the bridge only provides `to_string`, implement `fmt::Display` +// based on it (the reverse of the usual relationship between the two). +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl ToString for Punct { + fn to_string(&self) -> String { + TokenStream::from(TokenTree::from(self.clone())).to_string() } } @@ -814,7 +789,7 @@ impl Punct { #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl fmt::Display for Punct { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - TokenStream::from(TokenTree::from(self.clone())).fmt(f) + f.write_str(&self.to_string()) } } @@ -832,16 +807,7 @@ impl fmt::Debug for Punct { /// An identifier (`ident`). #[derive(Clone)] #[stable(feature = "proc_macro_lib2", since = "1.29.0")] -pub struct Ident { - sym: Symbol, - span: Span, - is_raw: bool, -} - -#[stable(feature = "proc_macro_lib2", since = "1.29.0")] -impl !Send for Ident {} -#[stable(feature = "proc_macro_lib2", since = "1.29.0")] -impl !Sync for Ident {} +pub struct Ident(bridge::client::Ident); impl Ident { fn is_valid(string: &str) -> bool { @@ -878,7 +844,7 @@ impl Ident { if !Ident::is_valid(string) { panic!("`{:?}` is not a valid identifier", string) } - Ident::new_maybe_raw(string, span, false) + Ident(bridge::client::Ident::new(string, span.0, false)) } /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). @@ -887,20 +853,29 @@ impl Ident { if !Ident::is_valid(string) { panic!("`{:?}` is not a valid identifier", string) } - Ident::new_maybe_raw(string, span, true) + Ident(bridge::client::Ident::new(string, span.0, true)) } /// Returns the span of this `Ident`, encompassing the entire string returned /// by `as_str`. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn span(&self) -> Span { - self.span + Span(self.0.span()) } /// Configures the span of this `Ident`, possibly changing its hygiene context. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn set_span(&mut self, span: Span) { - self.span = span; + self.0 = self.0.with_span(span.0); + } +} + +// NB: the bridge only provides `to_string`, implement `fmt::Display` +// based on it (the reverse of the usual relationship between the two). +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl ToString for Ident { + fn to_string(&self) -> String { + TokenStream::from(TokenTree::from(self.clone())).to_string() } } @@ -909,7 +884,7 @@ impl Ident { #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl fmt::Display for Ident { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - TokenStream::from(TokenTree::from(self.clone())).fmt(f) + f.write_str(&self.to_string()) } } @@ -927,19 +902,9 @@ impl fmt::Debug for Ident { /// character (`'a'`), byte character (`b'a'`), an integer or floating point number /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`). /// Boolean literals like `true` and `false` do not belong here, they are `Ident`s. -// FIXME(eddyb) `Literal` should not expose internal `Debug` impls. -#[derive(Clone, Debug)] -#[stable(feature = "proc_macro_lib2", since = "1.29.0")] -pub struct Literal { - lit: token::Lit, - suffix: Option, - span: Span, -} - -#[stable(feature = "proc_macro_lib2", since = "1.29.0")] -impl !Send for Literal {} +#[derive(Clone)] #[stable(feature = "proc_macro_lib2", since = "1.29.0")] -impl !Sync for Literal {} +pub struct Literal(bridge::client::Literal); macro_rules! suffixed_int_literals { ($($name:ident => $kind:ident,)*) => ($( @@ -956,11 +921,7 @@ macro_rules! suffixed_int_literals { /// below. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn $name(n: $kind) -> Literal { - Literal { - lit: token::Lit::Integer(Symbol::intern(&n.to_string())), - suffix: Some(Symbol::intern(stringify!($kind))), - span: Span::call_site(), - } + Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind))) } )*) } @@ -982,11 +943,7 @@ macro_rules! unsuffixed_int_literals { /// below. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn $name(n: $kind) -> Literal { - Literal { - lit: token::Lit::Integer(Symbol::intern(&n.to_string())), - suffix: None, - span: Span::call_site(), - } + Literal(bridge::client::Literal::integer(&n.to_string())) } )*) } @@ -1039,11 +996,7 @@ impl Literal { if !n.is_finite() { panic!("Invalid float literal {}", n); } - Literal { - lit: token::Lit::Float(Symbol::intern(&n.to_string())), - suffix: None, - span: Span::call_site(), - } + Literal(bridge::client::Literal::float(&n.to_string())) } /// Creates a new suffixed floating-point literal. @@ -1064,11 +1017,7 @@ impl Literal { if !n.is_finite() { panic!("Invalid float literal {}", n); } - Literal { - lit: token::Lit::Float(Symbol::intern(&n.to_string())), - suffix: Some(Symbol::intern("f32")), - span: Span::call_site(), - } + Literal(bridge::client::Literal::f32(&n.to_string())) } /// Creates a new unsuffixed floating-point literal. @@ -1088,11 +1037,7 @@ impl Literal { if !n.is_finite() { panic!("Invalid float literal {}", n); } - Literal { - lit: token::Lit::Float(Symbol::intern(&n.to_string())), - suffix: None, - span: Span::call_site(), - } + Literal(bridge::client::Literal::float(&n.to_string())) } /// Creates a new suffixed floating-point literal. @@ -1113,61 +1058,37 @@ impl Literal { if !n.is_finite() { panic!("Invalid float literal {}", n); } - Literal { - lit: token::Lit::Float(Symbol::intern(&n.to_string())), - suffix: Some(Symbol::intern("f64")), - span: Span::call_site(), - } + Literal(bridge::client::Literal::f64(&n.to_string())) } /// String literal. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn string(string: &str) -> Literal { - let mut escaped = String::new(); - for ch in string.chars() { - escaped.extend(ch.escape_debug()); - } - Literal { - lit: token::Lit::Str_(Symbol::intern(&escaped)), - suffix: None, - span: Span::call_site(), - } + Literal(bridge::client::Literal::string(string)) } /// Character literal. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn character(ch: char) -> Literal { - let mut escaped = String::new(); - escaped.extend(ch.escape_unicode()); - Literal { - lit: token::Lit::Char(Symbol::intern(&escaped)), - suffix: None, - span: Span::call_site(), - } + Literal(bridge::client::Literal::character(ch)) } /// Byte string literal. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn byte_string(bytes: &[u8]) -> Literal { - let string = bytes.iter().cloned().flat_map(ascii::escape_default) - .map(Into::::into).collect::(); - Literal { - lit: token::Lit::ByteStr(Symbol::intern(&string)), - suffix: None, - span: Span::call_site(), - } + Literal(bridge::client::Literal::byte_string(bytes)) } /// Returns the span encompassing this literal. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn span(&self) -> Span { - self.span + Span(self.0.span()) } /// Configures the span associated for this literal. #[stable(feature = "proc_macro_lib2", since = "1.29.0")] pub fn set_span(&mut self, span: Span) { - self.span = span; + self.0.set_span(span.0); } /// Returns a `Span` that is a subset of `self.span()` containing only the @@ -1183,35 +1104,28 @@ impl Literal { // was 'c' or whether it was '\u{63}'. #[unstable(feature = "proc_macro_span", issue = "54725")] pub fn subspan>(&self, range: R) -> Option { - let inner = self.span().0; - let length = inner.hi().to_usize() - inner.lo().to_usize(); - - let start = match range.start_bound() { - Bound::Included(&lo) => lo, - Bound::Excluded(&lo) => lo + 1, - Bound::Unbounded => 0, - }; - - let end = match range.end_bound() { - Bound::Included(&hi) => hi + 1, - Bound::Excluded(&hi) => hi, - Bound::Unbounded => length, - }; - - // Bounds check the values, preventing addition overflow and OOB spans. - if start > u32::max_value() as usize - || end > u32::max_value() as usize - || (u32::max_value() - start as u32) < inner.lo().to_u32() - || (u32::max_value() - end as u32) < inner.lo().to_u32() - || start >= end - || end > length - { - return None; + // HACK(eddyb) something akin to `Option::cloned`, but for `Bound<&T>`. + fn cloned_bound(bound: Bound<&T>) -> Bound { + match bound { + Bound::Included(x) => Bound::Included(x.clone()), + Bound::Excluded(x) => Bound::Excluded(x.clone()), + Bound::Unbounded => Bound::Unbounded, + } } - let new_lo = inner.lo() + BytePos::from_usize(start); - let new_hi = inner.lo() + BytePos::from_usize(end); - Some(Span(inner.with_lo(new_lo).with_hi(new_hi))) + self.0.subspan( + cloned_bound(range.start_bound()), + cloned_bound(range.end_bound()), + ).map(Span) + } +} + +// NB: the bridge only provides `to_string`, implement `fmt::Display` +// based on it (the reverse of the usual relationship between the two). +#[stable(feature = "proc_macro_lib", since = "1.15.0")] +impl ToString for Literal { + fn to_string(&self) -> String { + TokenStream::from(TokenTree::from(self.clone())).to_string() } } @@ -1220,7 +1134,15 @@ impl Literal { #[stable(feature = "proc_macro_lib2", since = "1.29.0")] impl fmt::Display for Literal { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - TokenStream::from(TokenTree::from(self.clone())).fmt(f) + f.write_str(&self.to_string()) + } +} + +#[stable(feature = "proc_macro_lib2", since = "1.29.0")] +impl fmt::Debug for Literal { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. + self.0.fmt(f) } } @@ -1239,60 +1161,21 @@ pub mod __internal { use std::cell::Cell; use std::ptr; - use syntax::ast; + use syntax::errors::DiagnosticBuilder; use syntax::ext::base::ExtCtxt; - use syntax::ptr::P; - use syntax::parse::{self, ParseSess}; - use syntax::parse::token::{self, Token}; - use syntax::tokenstream; - use syntax_pos::{BytePos, Loc, DUMMY_SP}; + use syntax::parse::ParseSess; + use syntax_pos::{BytePos, Loc, DUMMY_SP, Span}; use syntax_pos::hygiene::{SyntaxContext, Transparency}; - use super::{TokenStream, LexError, Span}; + use super::LexError; pub fn lookup_char_pos(pos: BytePos) -> Loc { with_sess(|sess, _| sess.source_map().lookup_char_pos(pos)) } - pub fn new_token_stream(item: P) -> TokenStream { - let token = Token::interpolated(token::NtItem(item)); - TokenStream(tokenstream::TokenTree::Token(DUMMY_SP, token).into()) - } - - pub fn token_stream_wrap(inner: tokenstream::TokenStream) -> TokenStream { - TokenStream(inner) - } - - pub fn token_stream_parse_items(stream: TokenStream) -> Result>, LexError> { - with_sess(move |sess, _| { - let mut parser = parse::stream_to_parser(sess, stream.0); - let mut items = Vec::new(); - - while let Some(item) = try!(parser.parse_item().map_err(super::parse_to_lex_err)) { - items.push(item) - } - - Ok(items) - }) - } - - pub fn token_stream_inner(stream: TokenStream) -> tokenstream::TokenStream { - stream.0 - } - - pub trait Registry { - fn register_custom_derive(&mut self, - trait_name: &str, - expand: fn(TokenStream) -> TokenStream, - attributes: &[&'static str]); - - fn register_attr_proc_macro(&mut self, - name: &str, - expand: fn(TokenStream, TokenStream) -> TokenStream); - - fn register_bang_proc_macro(&mut self, - name: &str, - expand: fn(TokenStream) -> TokenStream); + pub fn parse_to_lex_err(mut err: DiagnosticBuilder) -> LexError { + err.cancel(); + LexError { _inner: () } } #[derive(Clone, Copy)] @@ -1311,7 +1194,7 @@ pub mod __internal { thread_local! { static CURRENT_SESS: Cell = Cell::new(ProcMacroSess { parse_sess: ptr::null(), - data: ProcMacroData { def_site: Span(DUMMY_SP), call_site: Span(DUMMY_SP) }, + data: ProcMacroData { def_site: DUMMY_SP, call_site: DUMMY_SP }, }); } @@ -1331,10 +1214,9 @@ pub mod __internal { // No way to determine def location for a proc macro right now, so use call location. let location = cx.current_expansion.mark.expn_info().unwrap().call_site; - let to_span = |transparency| Span(location.with_ctxt( + let to_span = |transparency| location.with_ctxt( SyntaxContext::empty().apply_mark_with_transparency(cx.current_expansion.mark, - transparency)) - ); + transparency)); p.set(ProcMacroSess { parse_sess: cx.parse_sess, data: ProcMacroData { @@ -1361,8 +1243,3 @@ pub mod __internal { f(unsafe { &*sess.parse_sess }, &sess.data) } } - -fn parse_to_lex_err(mut err: DiagnosticBuilder) -> LexError { - err.cancel(); - LexError { _inner: () } -} diff --git a/src/libproc_macro/rustc.rs b/src/libproc_macro/rustc.rs index 3ce02d1afb10b..a58de36e0ad73 100644 --- a/src/libproc_macro/rustc.rs +++ b/src/libproc_macro/rustc.rs @@ -8,31 +8,22 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use {Delimiter, Level, Spacing, Span, __internal}; -use {Group, Ident, Literal, Punct, TokenTree}; +use bridge::{server, TokenTree}; +use {Delimiter, Level, LineColumn, Spacing, __internal}; -use rustc_errors as errors; +use rustc_data_structures::sync::Lrc; +use rustc_errors::{self as errors, Diagnostic, DiagnosticBuilder}; +use std::ascii; +use std::ops::Bound; use syntax::ast; use syntax::parse::lexer::comments; -use syntax::parse::token; -use syntax::tokenstream; +use syntax::parse::{self, token}; +use syntax::tokenstream::{self, DelimSpan, TokenStream}; use syntax_pos::symbol::{keywords, Symbol}; - -impl Ident { - pub(crate) fn new_maybe_raw(string: &str, span: Span, is_raw: bool) -> Ident { - let sym = Symbol::intern(string); - if is_raw - && (sym == keywords::Underscore.name() - || ast::Ident::with_empty_ctxt(sym).is_path_segment_keyword()) - { - panic!("`{:?}` is not a valid raw identifier", string) - } - Ident { sym, span, is_raw } - } -} +use syntax_pos::{BytePos, FileName, MultiSpan, Pos, SourceFile, Span}; impl Delimiter { - pub(crate) fn from_internal(delim: token::DelimToken) -> Delimiter { + fn from_internal(delim: token::DelimToken) -> Delimiter { match delim { token::Paren => Delimiter::Parenthesis, token::Brace => Delimiter::Brace, @@ -41,7 +32,7 @@ impl Delimiter { } } - pub(crate) fn to_internal(self) -> token::DelimToken { + fn to_internal(self) -> token::DelimToken { match self { Delimiter::Parenthesis => token::Paren, Delimiter::Brace => token::Brace, @@ -51,48 +42,52 @@ impl Delimiter { } } -impl TokenTree { - pub(crate) fn from_internal( - stream: tokenstream::TokenStream, - stack: &mut Vec, - ) -> TokenTree { +impl TokenTree { + fn from_internal(stream: TokenStream, stack: &mut Vec) -> Self { use syntax::parse::token::*; - let (tree, is_joint) = stream.as_tree(); + let (tree, joint) = stream.as_tree(); let (span, token) = match tree { - tokenstream::TokenTree::Token(span, token) => (span, token), tokenstream::TokenTree::Delimited(span, delimed) => { let delimiter = Delimiter::from_internal(delimed.delim); - let mut g = Group::new(delimiter, ::TokenStream(delimed.tts.into())); - g.span = span; - return g.into(); + return TokenTree::Group(Group { + delimiter, + stream: delimed.tts.into(), + span, + }); } + tokenstream::TokenTree::Token(span, token) => (span, token), }; - let op_kind = if is_joint { - Spacing::Joint - } else { - Spacing::Alone - }; macro_rules! tt { - ($e:expr) => {{ - let mut x = TokenTree::from($e); - x.set_span(Span(span)); - x - }}; + ($ty:ident { $($field:ident $(: $value:expr)*),+ $(,)* }) => ( + TokenTree::$ty(self::$ty { + $($field $(: $value)*,)* + span, + }) + ) } macro_rules! op { ($a:expr) => { - tt!(Punct::new($a, op_kind)) + tt!(Punct { ch: $a, joint }) }; ($a:expr, $b:expr) => {{ - stack.push(tt!(Punct::new($b, op_kind))); - tt!(Punct::new($a, Spacing::Joint)) + 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, op_kind))); - stack.push(tt!(Punct::new($b, Spacing::Joint))); - tt!(Punct::new($a, Spacing::Joint)) + stack.push(tt!(Punct { ch: $c, joint })); + stack.push(tt!(Punct { + ch: $b, + joint: true + })); + tt!(Punct { + ch: $a, + joint: true + }) }}; } @@ -145,37 +140,61 @@ impl TokenTree { Question => op!('?'), SingleQuote => op!('\''), - Ident(ident, false) => tt!(self::Ident::new(&ident.as_str(), Span(span))), - Ident(ident, true) => tt!(self::Ident::new_raw(&ident.as_str(), Span(span))), + Ident(ident, is_raw) => tt!(Ident { + sym: ident.name, + is_raw + }), Lifetime(ident) => { let ident = ident.without_first_quote(); - stack.push(tt!(self::Ident::new(&ident.as_str(), Span(span)))); - tt!(Punct::new('\'', Spacing::Joint)) + stack.push(tt!(Ident { + sym: ident.name, + is_raw: false + })); + tt!(Punct { + ch: '\'', + joint: true + }) } - Literal(lit, suffix) => tt!(self::Literal { - lit, - suffix, - span: Span(span) - }), + Literal(lit, suffix) => tt!(Literal { lit, suffix }), DocComment(c) => { let style = comments::doc_comment_style(&c.as_str()); let stripped = comments::strip_doc_comment_decoration(&c.as_str()); + let mut escaped = String::new(); + for ch in stripped.chars() { + escaped.extend(ch.escape_debug()); + } let stream = vec![ - tt!(self::Ident::new("doc", Span(span))), - tt!(Punct::new('=', Spacing::Alone)), - tt!(self::Literal::string(&stripped)), - ].into_iter() - .collect(); - stack.push(tt!(Group::new(Delimiter::Bracket, stream))); + Ident(ast::Ident::new(Symbol::intern("doc"), span), false), + Eq, + Literal(Lit::Str_(Symbol::intern(&escaped)), None), + ] + .into_iter() + .map(|token| tokenstream::TokenTree::Token(span, token)) + .collect(); + stack.push(TokenTree::Group(Group { + delimiter: Delimiter::Bracket, + stream, + span: DelimSpan::from_single(span), + })); if style == ast::AttrStyle::Inner { - stack.push(tt!(Punct::new('!', Spacing::Alone))); + stack.push(tt!(Punct { + ch: '!', + joint: false + })); } - tt!(Punct::new('#', Spacing::Alone)) + tt!(Punct { + ch: '#', + joint: false + }) } Interpolated(_) => __internal::with_sess(|sess, _| { - let tts = token.interpolated_to_tokenstream(sess, span); - tt!(Group::new(Delimiter::None, ::TokenStream(tts))) + let stream = token.interpolated_to_tokenstream(sess, span); + TokenTree::Group(Group { + delimiter: Delimiter::None, + stream, + span: DelimSpan::from_single(span), + }) }), DotEq => op!('.', '='), @@ -184,56 +203,55 @@ impl TokenTree { } } - pub(crate) fn to_internal(self) -> tokenstream::TokenStream { + fn to_internal(self) -> TokenStream { use syntax::parse::token::*; - use syntax::tokenstream::{Delimited, TokenTree}; - - let (ch, kind, span) = match self { - self::TokenTree::Punct(tt) => (tt.as_char(), tt.spacing(), tt.span()), - self::TokenTree::Group(tt) => { - return TokenTree::Delimited( - tt.span, - Delimited { - delim: tt.delimiter.to_internal(), - tts: tt.stream.0.into(), + + let (ch, joint, span) = match self { + TokenTree::Punct(Punct { ch, joint, span }) => (ch, joint, span), + TokenTree::Group(Group { + delimiter, + stream, + span, + }) => { + return tokenstream::TokenTree::Delimited( + span, + tokenstream::Delimited { + delim: delimiter.to_internal(), + tts: stream.into(), }, - ).into(); + ) + .into(); } - self::TokenTree::Ident(tt) => { - let token = Ident(ast::Ident::new(tt.sym, tt.span.0), tt.is_raw); - return TokenTree::Token(tt.span.0, token).into(); + TokenTree::Ident(self::Ident { sym, span, is_raw }) => { + let token = Ident(ast::Ident::new(sym, span), is_raw); + return tokenstream::TokenTree::Token(span, token).into(); } - self::TokenTree::Literal(self::Literal { + TokenTree::Literal(self::Literal { lit: Lit::Integer(ref a), suffix, span, - }) - if a.as_str().starts_with("-") => - { + }) if a.as_str().starts_with("-") => { let minus = BinOp(BinOpToken::Minus); let integer = Symbol::intern(&a.as_str()[1..]); let integer = Literal(Lit::Integer(integer), suffix); - let a = TokenTree::Token(span.0, minus); - let b = TokenTree::Token(span.0, integer); + let a = tokenstream::TokenTree::Token(span, minus); + let b = tokenstream::TokenTree::Token(span, integer); return vec![a, b].into_iter().collect(); } - self::TokenTree::Literal(self::Literal { + TokenTree::Literal(self::Literal { lit: Lit::Float(ref a), suffix, span, - }) - if a.as_str().starts_with("-") => - { + }) if a.as_str().starts_with("-") => { let minus = BinOp(BinOpToken::Minus); let float = Symbol::intern(&a.as_str()[1..]); let float = Literal(Lit::Float(float), suffix); - let a = TokenTree::Token(span.0, minus); - let b = TokenTree::Token(span.0, float); + let a = tokenstream::TokenTree::Token(span, minus); + let b = tokenstream::TokenTree::Token(span, float); return vec![a, b].into_iter().collect(); } - self::TokenTree::Literal(tt) => { - let token = Literal(tt.lit, tt.suffix); - return TokenTree::Token(tt.span.0, token).into(); + TokenTree::Literal(self::Literal { lit, suffix, span }) => { + return tokenstream::TokenTree::Token(span, Literal(lit, suffix)).into() } }; @@ -263,16 +281,17 @@ impl TokenTree { _ => unreachable!(), }; - let tree = TokenTree::Token(span.0, token); - match kind { - Spacing::Alone => tree.into(), - Spacing::Joint => tree.joint(), + let tree = tokenstream::TokenTree::Token(span, token); + if joint { + tree.joint() + } else { + tree.into() } } } impl Level { - pub(crate) fn to_internal(self) -> errors::Level { + fn to_internal(self) -> errors::Level { match self { Level::Error => errors::Level::Error, Level::Warning => errors::Level::Warning, @@ -281,3 +300,415 @@ impl Level { } } } + +#[derive(Clone)] +pub struct TokenStreamIter { + cursor: tokenstream::Cursor, + stack: Vec>, +} + +#[derive(Clone)] +pub struct Group { + delimiter: Delimiter, + stream: TokenStream, + span: DelimSpan, +} + +#[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, +} + +#[derive(Copy, Clone, PartialEq, Eq, Hash)] +pub struct Ident { + sym: Symbol, + span: Span, + is_raw: bool, +} + +// FIXME(eddyb) `Literal` should not expose internal `Debug` impls. +#[derive(Clone, Debug)] +pub struct Literal { + lit: token::Lit, + suffix: Option, + span: Span, +} + +pub struct Rustc; + +impl server::Types for Rustc { + type TokenStream = TokenStream; + type TokenStreamBuilder = tokenstream::TokenStreamBuilder; + type TokenStreamIter = TokenStreamIter; + type Group = Group; + type Punct = Punct; + type Ident = Ident; + type Literal = Literal; + type SourceFile = Lrc; + type MultiSpan = Vec; + type Diagnostic = Diagnostic; + type Span = Span; +} + +impl server::TokenStream for Rustc { + fn new(&mut self) -> Self::TokenStream { + TokenStream::empty() + } + fn is_empty(&mut self, stream: &Self::TokenStream) -> bool { + stream.is_empty() + } + fn from_str(&mut self, src: &str) -> Self::TokenStream { + ::__internal::with_sess(|sess, data| { + parse::parse_stream_from_source_str( + FileName::ProcMacroSourceCode, + src.to_string(), + sess, + Some(data.call_site), + ) + }) + } + fn to_string(&mut self, stream: &Self::TokenStream) -> String { + stream.to_string() + } + fn from_token_tree( + &mut self, + tree: TokenTree, + ) -> Self::TokenStream { + tree.to_internal() + } + fn into_iter(&mut self, stream: Self::TokenStream) -> Self::TokenStreamIter { + TokenStreamIter { + cursor: stream.trees(), + stack: vec![], + } + } +} + +impl server::TokenStreamBuilder for Rustc { + fn new(&mut self) -> Self::TokenStreamBuilder { + tokenstream::TokenStreamBuilder::new() + } + fn push(&mut self, builder: &mut Self::TokenStreamBuilder, stream: Self::TokenStream) { + builder.push(stream); + } + fn build(&mut self, builder: Self::TokenStreamBuilder) -> Self::TokenStream { + builder.build() + } +} + +impl server::TokenStreamIter for Rustc { + fn next( + &mut self, + iter: &mut Self::TokenStreamIter, + ) -> Option> { + loop { + let tree = iter.stack.pop().or_else(|| { + let next = iter.cursor.next_as_stream()?; + Some(TokenTree::from_internal(next, &mut iter.stack)) + })?; + // HACK: The condition "dummy span + group with empty delimiter" represents an AST + // fragment approximately converted into a token stream. This may happen, for + // example, with inputs to proc macro attributes, including derives. Such "groups" + // need to flattened during iteration over stream's token trees. + // Eventually this needs to be removed in favor of keeping original token trees + // and not doing the roundtrip through AST. + if let TokenTree::Group(ref group) = tree { + if group.delimiter == Delimiter::None && group.span.entire().is_dummy() { + iter.cursor.insert(group.stream.clone()); + continue; + } + } + return Some(tree); + } + } +} + +impl server::Group for Rustc { + fn new(&mut self, delimiter: Delimiter, stream: Self::TokenStream) -> Self::Group { + Group { + delimiter, + stream, + span: DelimSpan::from_single(server::Span::call_site(self)), + } + } + fn delimiter(&mut self, group: &Self::Group) -> Delimiter { + group.delimiter + } + fn stream(&mut self, group: &Self::Group) -> Self::TokenStream { + group.stream.clone() + } + fn span(&mut self, group: &Self::Group) -> Self::Span { + group.span.entire() + } + fn span_open(&mut self, group: &Self::Group) -> Self::Span { + group.span.open + } + fn span_close(&mut self, group: &Self::Group) -> Self::Span { + group.span.close + } + fn set_span(&mut self, group: &mut Self::Group, span: Self::Span) { + group.span = DelimSpan::from_single(span); + } +} + +impl server::Punct for Rustc { + fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct { + Punct { + ch, + joint: spacing == Spacing::Joint, + span: server::Span::call_site(self), + } + } + 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 { + let sym = Symbol::intern(string); + if is_raw + && (sym == keywords::Underscore.name() + || ast::Ident::with_empty_ctxt(sym).is_path_segment_keyword()) + { + panic!("`{:?}` is not a valid raw identifier", string) + } + Ident { sym, span, is_raw } + } + fn span(&mut self, ident: Self::Ident) -> Self::Span { + ident.span + } + fn with_span(&mut self, ident: Self::Ident, span: Self::Span) -> Self::Ident { + Ident { span, ..ident } + } +} + +impl server::Literal for Rustc { + // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. + fn debug(&mut self, literal: &Self::Literal) -> String { + format!("{:?}", literal) + } + fn integer(&mut self, n: &str) -> Self::Literal { + Literal { + lit: token::Lit::Integer(Symbol::intern(n)), + suffix: None, + span: server::Span::call_site(self), + } + } + fn typed_integer(&mut self, n: &str, kind: &str) -> Self::Literal { + Literal { + lit: token::Lit::Integer(Symbol::intern(n)), + suffix: Some(Symbol::intern(kind)), + span: server::Span::call_site(self), + } + } + fn float(&mut self, n: &str) -> Self::Literal { + Literal { + lit: token::Lit::Float(Symbol::intern(n)), + suffix: None, + span: server::Span::call_site(self), + } + } + fn f32(&mut self, n: &str) -> Self::Literal { + Literal { + lit: token::Lit::Float(Symbol::intern(n)), + suffix: Some(Symbol::intern("f32")), + span: server::Span::call_site(self), + } + } + fn f64(&mut self, n: &str) -> Self::Literal { + Literal { + lit: token::Lit::Float(Symbol::intern(n)), + suffix: Some(Symbol::intern("f64")), + span: server::Span::call_site(self), + } + } + fn string(&mut self, string: &str) -> Self::Literal { + let mut escaped = String::new(); + for ch in string.chars() { + escaped.extend(ch.escape_debug()); + } + Literal { + lit: token::Lit::Str_(Symbol::intern(&escaped)), + suffix: None, + span: server::Span::call_site(self), + } + } + fn character(&mut self, ch: char) -> Self::Literal { + let mut escaped = String::new(); + escaped.extend(ch.escape_unicode()); + Literal { + lit: token::Lit::Char(Symbol::intern(&escaped)), + suffix: None, + span: server::Span::call_site(self), + } + } + fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal { + let string = bytes + .iter() + .cloned() + .flat_map(ascii::escape_default) + .map(Into::::into) + .collect::(); + Literal { + lit: token::Lit::ByteStr(Symbol::intern(&string)), + suffix: None, + span: server::Span::call_site(self), + } + } + fn span(&mut self, literal: &Self::Literal) -> Self::Span { + literal.span + } + fn set_span(&mut self, literal: &mut Self::Literal, span: Self::Span) { + literal.span = span; + } + fn subspan( + &mut self, + literal: &Self::Literal, + start: Bound, + end: Bound, + ) -> Option { + let span = literal.span; + let length = span.hi().to_usize() - span.lo().to_usize(); + + let start = match start { + Bound::Included(lo) => lo, + Bound::Excluded(lo) => lo + 1, + Bound::Unbounded => 0, + }; + + let end = match end { + Bound::Included(hi) => hi + 1, + Bound::Excluded(hi) => hi, + Bound::Unbounded => length, + }; + + // Bounds check the values, preventing addition overflow and OOB spans. + if start > u32::max_value() as usize + || end > u32::max_value() as usize + || (u32::max_value() - start as u32) < span.lo().to_u32() + || (u32::max_value() - end as u32) < span.lo().to_u32() + || start >= end + || end > length + { + return None; + } + + let new_lo = span.lo() + BytePos::from_usize(start); + let new_hi = span.lo() + BytePos::from_usize(end); + Some(span.with_lo(new_lo).with_hi(new_hi)) + } +} + +impl server::SourceFile for Rustc { + fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool { + Lrc::ptr_eq(file1, file2) + } + fn path(&mut self, file: &Self::SourceFile) -> String { + match file.name { + FileName::Real(ref path) => path + .to_str() + .expect("non-UTF8 file path in `proc_macro::SourceFile::path`") + .to_string(), + _ => file.name.to_string(), + } + } + fn is_real(&mut self, file: &Self::SourceFile) -> bool { + file.is_real_file() + } +} + +impl server::MultiSpan for Rustc { + fn new(&mut self) -> Self::MultiSpan { + vec![] + } + fn push(&mut self, spans: &mut Self::MultiSpan, span: Self::Span) { + spans.push(span) + } +} + +impl server::Diagnostic for Rustc { + fn new(&mut self, level: Level, msg: &str, spans: Self::MultiSpan) -> Self::Diagnostic { + let mut diag = Diagnostic::new(level.to_internal(), msg); + diag.set_span(MultiSpan::from_spans(spans)); + diag + } + fn sub( + &mut self, + diag: &mut Self::Diagnostic, + level: Level, + msg: &str, + spans: Self::MultiSpan, + ) { + diag.sub(level.to_internal(), msg, MultiSpan::from_spans(spans), None); + } + fn emit(&mut self, diag: Self::Diagnostic) { + ::__internal::with_sess(move |sess, _| { + DiagnosticBuilder::new_diagnostic(&sess.span_diagnostic, diag).emit() + }); + } +} + +impl server::Span for Rustc { + fn debug(&mut self, span: Self::Span) -> String { + format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0) + } + fn def_site(&mut self) -> Self::Span { + ::__internal::with_sess(|_, data| data.def_site) + } + fn call_site(&mut self) -> Self::Span { + ::__internal::with_sess(|_, data| data.call_site) + } + fn source_file(&mut self, span: Self::Span) -> Self::SourceFile { + ::__internal::lookup_char_pos(span.lo()).file + } + fn parent(&mut self, span: Self::Span) -> Option { + span.ctxt().outer().expn_info().map(|i| i.call_site) + } + fn source(&mut self, span: Self::Span) -> Self::Span { + span.source_callsite() + } + fn start(&mut self, span: Self::Span) -> LineColumn { + let loc = ::__internal::lookup_char_pos(span.lo()); + LineColumn { + line: loc.line, + column: loc.col.to_usize(), + } + } + fn end(&mut self, span: Self::Span) -> LineColumn { + let loc = ::__internal::lookup_char_pos(span.hi()); + LineColumn { + line: loc.line, + column: loc.col.to_usize(), + } + } + fn join(&mut self, first: Self::Span, second: Self::Span) -> Option { + let self_loc = ::__internal::lookup_char_pos(first.lo()); + let other_loc = ::__internal::lookup_char_pos(second.lo()); + + if self_loc.file.name != other_loc.file.name { + return None; + } + + Some(first.to(second)) + } + fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span { + span.with_ctxt(at.ctxt()) + } +} diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index 3ff2545f78dfa..388bbc52c3b1a 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -596,7 +596,7 @@ define_dep_nodes!( <'tcx> [] ReachableNonGenerics(CrateNum), [] NativeLibraries(CrateNum), [] PluginRegistrarFn(CrateNum), - [] DeriveRegistrarFn(CrateNum), + [] ProcMacroDeclsStatic(CrateNum), [input] CrateDisambiguator(CrateNum), [input] CrateHash(CrateNum), [input] OriginalCrateName(CrateNum), diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 1187c53305d1c..d1c3acc57b723 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -68,7 +68,7 @@ pub struct Session { /// For a library crate, this is always none pub entry_fn: Once>, pub plugin_registrar_fn: Once>, - pub derive_registrar_fn: Once>, + pub proc_macro_decls_static: Once>, pub default_sysroot: Option, /// The name of the root source file of the crate, in the local file system. /// `None` means that there is no source file. @@ -687,9 +687,9 @@ impl Session { ) } - pub fn generate_derive_registrar_symbol(&self, disambiguator: CrateDisambiguator) -> String { + pub fn generate_proc_macro_decls_symbol(&self, disambiguator: CrateDisambiguator) -> String { format!( - "__rustc_derive_registrar_{}__", + "__rustc_proc_macro_decls_{}__", disambiguator.to_fingerprint().to_hex() ) } @@ -1146,7 +1146,7 @@ pub fn build_session_( // For a library crate, this is always none entry_fn: Once::new(), plugin_registrar_fn: Once::new(), - derive_registrar_fn: Once::new(), + proc_macro_decls_static: Once::new(), default_sysroot, local_crate_source_file, working_dir, diff --git a/src/librustc/ty/query/config.rs b/src/librustc/ty/query/config.rs index bb33def51cc3b..5d12aaeed5f79 100644 --- a/src/librustc/ty/query/config.rs +++ b/src/librustc/ty/query/config.rs @@ -591,7 +591,7 @@ impl<'tcx> QueryDescription<'tcx> for queries::plugin_registrar_fn<'tcx> { } } -impl<'tcx> QueryDescription<'tcx> for queries::derive_registrar_fn<'tcx> { +impl<'tcx> QueryDescription<'tcx> for queries::proc_macro_decls_static<'tcx> { fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> { "looking up the derive registrar for a crate".into() } diff --git a/src/librustc/ty/query/mod.rs b/src/librustc/ty/query/mod.rs index 22bd1e26ba3ee..699c2d111c639 100644 --- a/src/librustc/ty/query/mod.rs +++ b/src/librustc/ty/query/mod.rs @@ -470,7 +470,7 @@ define_queries! { <'tcx> [] fn foreign_modules: ForeignModules(CrateNum) -> Lrc>, [] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option, - [] fn derive_registrar_fn: DeriveRegistrarFn(CrateNum) -> Option, + [] fn proc_macro_decls_static: ProcMacroDeclsStatic(CrateNum) -> Option, [] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> CrateDisambiguator, [] fn crate_hash: CrateHash(CrateNum) -> Svh, [] fn original_crate_name: OriginalCrateName(CrateNum) -> Symbol, diff --git a/src/librustc/ty/query/plumbing.rs b/src/librustc/ty/query/plumbing.rs index efee39a1d63f9..5f33d466c4a19 100644 --- a/src/librustc/ty/query/plumbing.rs +++ b/src/librustc/ty/query/plumbing.rs @@ -1200,7 +1200,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>, DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); } DepKind::NativeLibraries => { force!(native_libraries, krate!()); } DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); } - DepKind::DeriveRegistrarFn => { force!(derive_registrar_fn, krate!()); } + DepKind::ProcMacroDeclsStatic => { force!(proc_macro_decls_static, krate!()); } DepKind::CrateDisambiguator => { force!(crate_disambiguator, krate!()); } DepKind::CrateHash => { force!(crate_hash, krate!()); } DepKind::OriginalCrateName => { force!(original_crate_name, krate!()); } diff --git a/src/librustc/util/common.rs b/src/librustc/util/common.rs index bcc0b8047ef74..7a246af82e5fe 100644 --- a/src/librustc/util/common.rs +++ b/src/librustc/util/common.rs @@ -24,7 +24,6 @@ use std::sync::mpsc::{Sender}; use syntax_pos::{SpanData}; use ty::TyCtxt; use dep_graph::{DepNode}; -use proc_macro; use lazy_static; use session::Session; @@ -47,14 +46,13 @@ lazy_static! { } fn panic_hook(info: &panic::PanicInfo<'_>) { - if !proc_macro::__internal::in_sess() { - (*DEFAULT_HOOK)(info); + (*DEFAULT_HOOK)(info); - let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false); + let backtrace = env::var_os("RUST_BACKTRACE").map(|x| &x != "0").unwrap_or(false); - if backtrace { - TyCtxt::try_print_query_stack(); - } + if backtrace { + TyCtxt::try_print_query_stack(); + } #[cfg(windows)] unsafe { @@ -66,7 +64,6 @@ fn panic_hook(info: &panic::PanicInfo<'_>) { DebugBreak(); } } - } } pub fn install_panic_hook() { diff --git a/src/librustc_codegen_ssa/back/symbol_export.rs b/src/librustc_codegen_ssa/back/symbol_export.rs index 0463da00c89d9..0fb2641a4f82e 100644 --- a/src/librustc_codegen_ssa/back/symbol_export.rs +++ b/src/librustc_codegen_ssa/back/symbol_export.rs @@ -157,7 +157,7 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, }) .collect(); - if let Some(id) = *tcx.sess.derive_registrar_fn.get() { + if let Some(id) = *tcx.sess.proc_macro_decls_static.get() { let def_id = tcx.hir.local_def_id(id); reachable_non_generics.insert(def_id, SymbolExportLevel::C); } diff --git a/src/librustc_codegen_utils/symbol_names.rs b/src/librustc_codegen_utils/symbol_names.rs index f8593363bb16a..344a252578472 100644 --- a/src/librustc_codegen_utils/symbol_names.rs +++ b/src/librustc_codegen_utils/symbol_names.rs @@ -257,9 +257,9 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance let disambiguator = tcx.sess.local_crate_disambiguator(); return tcx.sess.generate_plugin_registrar_symbol(disambiguator); } - if *tcx.sess.derive_registrar_fn.get() == Some(id) { + if *tcx.sess.proc_macro_decls_static.get() == Some(id) { let disambiguator = tcx.sess.local_crate_disambiguator(); - return tcx.sess.generate_derive_registrar_symbol(disambiguator); + return tcx.sess.generate_proc_macro_decls_symbol(disambiguator); } } diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 7ad012409b53a..c1705ad2d58ff 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -62,7 +62,7 @@ use syntax::symbol::Symbol; use syntax_pos::{FileName, hygiene}; use syntax_ext; -use derive_registrar; +use proc_macro_decls; use pretty::ReplaceBodyWithLoop; use profile; @@ -1066,7 +1066,7 @@ where let num_crate_types = crate_types.len(); let is_proc_macro_crate = crate_types.contains(&config::CrateType::ProcMacro); let is_test_crate = sess.opts.test; - syntax_ext::proc_macro_registrar::modify( + syntax_ext::proc_macro_decls::modify( &sess.parse_sess, &mut resolver, krate, @@ -1243,8 +1243,8 @@ where .set(time(sess, "looking for plugin registrar", || { plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map) })); - sess.derive_registrar_fn - .set(derive_registrar::find(&hir_map)); + sess.proc_macro_decls_static + .set(proc_macro_decls::find(&hir_map)); time(sess, "loop checking", || loops::check_crate(sess, &hir_map)); diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index ec3cb95db88f8..b063176d3cafa 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -127,7 +127,7 @@ mod test; pub mod profile; pub mod driver; pub mod pretty; -mod derive_registrar; +mod proc_macro_decls; pub mod target_features { use syntax::ast; diff --git a/src/librustc_driver/derive_registrar.rs b/src/librustc_driver/proc_macro_decls.rs similarity index 81% rename from src/librustc_driver/derive_registrar.rs rename to src/librustc_driver/proc_macro_decls.rs index 9983efce6af0b..136a27b1ced47 100644 --- a/src/librustc_driver/derive_registrar.rs +++ b/src/librustc_driver/proc_macro_decls.rs @@ -17,19 +17,19 @@ use syntax::attr; pub fn find(hir_map: &Map) -> Option { let krate = hir_map.krate(); - let mut finder = Finder { registrar: None }; + let mut finder = Finder { decls: None }; krate.visit_all_item_likes(&mut finder); - finder.registrar + finder.decls } struct Finder { - registrar: Option, + decls: Option, } impl<'v> ItemLikeVisitor<'v> for Finder { fn visit_item(&mut self, item: &hir::Item) { - if attr::contains_name(&item.attrs, "rustc_derive_registrar") { - self.registrar = Some(item.id); + if attr::contains_name(&item.attrs, "rustc_proc_macro_decls") { + self.decls = Some(item.id); } } diff --git a/src/librustc_metadata/creader.rs b/src/librustc_metadata/creader.rs index a14dd99eeb3b3..4ff29f5c04d41 100644 --- a/src/librustc_metadata/creader.rs +++ b/src/librustc_metadata/creader.rs @@ -36,7 +36,6 @@ use std::{cmp, fs}; use syntax::ast; use syntax::attr; -use syntax::edition::Edition; use syntax::ext::base::SyntaxExtension; use syntax::symbol::Symbol; use syntax::visit; @@ -231,7 +230,7 @@ impl<'a> CrateLoader<'a> { let dependencies: Vec = cnum_map.iter().cloned().collect(); - let proc_macros = crate_root.macro_derive_registrar.map(|_| { + let proc_macros = crate_root.proc_macro_decls_static.map(|_| { self.load_derive_macros(&crate_root, dylib.clone().map(|p| p.0), span) }); @@ -339,7 +338,7 @@ impl<'a> CrateLoader<'a> { match result { LoadResult::Previous(cnum) => { let data = self.cstore.get_crate_data(cnum); - if data.root.macro_derive_registrar.is_some() { + if data.root.proc_macro_decls_static.is_some() { dep_kind = DepKind::UnexportedMacrosOnly; } data.dep_kind.with_lock(|data_dep_kind| { @@ -431,7 +430,7 @@ impl<'a> CrateLoader<'a> { dep_kind: DepKind) -> cstore::CrateNumMap { debug!("resolving deps of external crate"); - if crate_root.macro_derive_registrar.is_some() { + if crate_root.proc_macro_decls_static.is_some() { return cstore::CrateNumMap::new(); } @@ -533,9 +532,8 @@ impl<'a> CrateLoader<'a> { fn load_derive_macros(&mut self, root: &CrateRoot, dylib: Option, span: Span) -> Vec<(ast::Name, Lrc)> { use std::{env, mem}; - use proc_macro::TokenStream; - use proc_macro::__internal::Registry; use dynamic_lib::DynamicLibrary; + use proc_macro::bridge::client::ProcMacro; use syntax_ext::deriving::custom::ProcMacroDerive; use syntax_ext::proc_macro_impl::{AttrProcMacro, BangProcMacro}; @@ -550,61 +548,49 @@ impl<'a> CrateLoader<'a> { Err(err) => self.sess.span_fatal(span, &err), }; - let sym = self.sess.generate_derive_registrar_symbol(root.disambiguator); - let registrar = unsafe { + let sym = self.sess.generate_proc_macro_decls_symbol(root.disambiguator); + let decls = unsafe { let sym = match lib.symbol(&sym) { Ok(f) => f, Err(err) => self.sess.span_fatal(span, &err), }; - mem::transmute::<*mut u8, fn(&mut dyn Registry)>(sym) + *(sym as *const &[ProcMacro]) }; - struct MyRegistrar { - extensions: Vec<(ast::Name, Lrc)>, - edition: Edition, - } - - impl Registry for MyRegistrar { - fn register_custom_derive(&mut self, - trait_name: &str, - expand: fn(TokenStream) -> TokenStream, - attributes: &[&'static str]) { - let attrs = attributes.iter().cloned().map(Symbol::intern).collect::>(); - let derive = ProcMacroDerive::new(expand, attrs.clone()); - let derive = SyntaxExtension::ProcMacroDerive( - Box::new(derive), attrs, self.edition - ); - self.extensions.push((Symbol::intern(trait_name), Lrc::new(derive))); - } - - fn register_attr_proc_macro(&mut self, - name: &str, - expand: fn(TokenStream, TokenStream) -> TokenStream) { - let expand = SyntaxExtension::AttrProcMacro( - Box::new(AttrProcMacro { inner: expand }), self.edition - ); - self.extensions.push((Symbol::intern(name), Lrc::new(expand))); - } - - fn register_bang_proc_macro(&mut self, - name: &str, - expand: fn(TokenStream) -> TokenStream) { - let expand = SyntaxExtension::ProcMacro { - expander: Box::new(BangProcMacro { inner: expand }), - allow_internal_unstable: false, - edition: self.edition, - }; - self.extensions.push((Symbol::intern(name), Lrc::new(expand))); + let extensions = decls.iter().map(|&decl| { + match decl { + ProcMacro::CustomDerive { trait_name, attributes, client } => { + let attrs = attributes.iter().cloned().map(Symbol::intern).collect::>(); + (trait_name, SyntaxExtension::ProcMacroDerive( + Box::new(ProcMacroDerive { + client, + attrs: attrs.clone(), + }), + attrs, + root.edition, + )) + } + ProcMacro::Attr { name, client } => { + (name, SyntaxExtension::AttrProcMacro( + Box::new(AttrProcMacro { client }), + root.edition, + )) + } + ProcMacro::Bang { name, client } => { + (name, SyntaxExtension::ProcMacro { + expander: Box::new(BangProcMacro { client }), + allow_internal_unstable: false, + edition: root.edition, + }) + } } - } - - let mut my_registrar = MyRegistrar { extensions: Vec::new(), edition: root.edition }; - registrar(&mut my_registrar); + }).map(|(name, ext)| (Symbol::intern(name), Lrc::new(ext))).collect(); // Intentionally leak the dynamic library. We can't ever unload it // since the library can make things that will live arbitrarily long. mem::forget(lib); - my_registrar.extensions + + extensions } /// Look for a plugin registrar. Returns library path, crate diff --git a/src/librustc_metadata/cstore_impl.rs b/src/librustc_metadata/cstore_impl.rs index c7bd1744a3d6d..d0fa63a6163db 100644 --- a/src/librustc_metadata/cstore_impl.rs +++ b/src/librustc_metadata/cstore_impl.rs @@ -203,8 +203,8 @@ provide! { <'tcx> tcx, def_id, other, cdata, DefId { krate: def_id.krate, index } }) } - derive_registrar_fn => { - cdata.root.macro_derive_registrar.map(|index| { + proc_macro_decls_static => { + cdata.root.proc_macro_decls_static.map(|index| { DefId { krate: def_id.krate, index } }) } @@ -431,8 +431,9 @@ impl cstore::CStore { use syntax::ext::base::SyntaxExtension; use syntax_ext::proc_macro_impl::BangProcMacro; + let client = ::proc_macro::bridge::client::Client::expand1(::proc_macro::quote); let ext = SyntaxExtension::ProcMacro { - expander: Box::new(BangProcMacro { inner: ::proc_macro::quote }), + expander: Box::new(BangProcMacro { client }), allow_internal_unstable: true, edition: data.root.edition, }; diff --git a/src/librustc_metadata/encoder.rs b/src/librustc_metadata/encoder.rs index 5421972a4adcb..2736c60ffb6fa 100644 --- a/src/librustc_metadata/encoder.rs +++ b/src/librustc_metadata/encoder.rs @@ -496,8 +496,8 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> { .plugin_registrar_fn .get() .map(|id| tcx.hir.local_def_id(id).index), - macro_derive_registrar: if is_proc_macro { - let id = tcx.sess.derive_registrar_fn.get().unwrap(); + proc_macro_decls_static: if is_proc_macro { + let id = tcx.sess.proc_macro_decls_static.get().unwrap(); Some(tcx.hir.local_def_id(id).index) } else { None diff --git a/src/librustc_metadata/locator.rs b/src/librustc_metadata/locator.rs index 528c96f240dba..d4e51693d7edf 100644 --- a/src/librustc_metadata/locator.rs +++ b/src/librustc_metadata/locator.rs @@ -713,7 +713,7 @@ impl<'a> Context<'a> { let root = metadata.get_root(); if let Some(is_proc_macro) = self.is_proc_macro { - if root.macro_derive_registrar.is_some() != is_proc_macro { + if root.proc_macro_decls_static.is_some() != is_proc_macro { return None; } } diff --git a/src/librustc_metadata/schema.rs b/src/librustc_metadata/schema.rs index da2a8ae714b5a..e91d15b78c075 100644 --- a/src/librustc_metadata/schema.rs +++ b/src/librustc_metadata/schema.rs @@ -196,7 +196,7 @@ pub struct CrateRoot { pub has_panic_handler: bool, pub has_default_lib_allocator: bool, pub plugin_registrar_fn: Option, - pub macro_derive_registrar: Option, + pub proc_macro_decls_static: Option, pub crate_deps: LazySeq, pub dylib_dependency_formats: LazySeq>, diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 3bc349170514c..c93abc39ff3b9 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -1122,8 +1122,8 @@ pub const BUILTIN_ATTRIBUTES: &'static [(&'static str, AttributeType, AttributeG ("proc_macro_attribute", Normal, Ungated), ("proc_macro", Normal, Ungated), - ("rustc_derive_registrar", Normal, Gated(Stability::Unstable, - "rustc_derive_registrar", + ("rustc_proc_macro_decls", Normal, Gated(Stability::Unstable, + "rustc_proc_macro_decls", "used internally by rustc", cfg_fn!(rustc_attrs))), diff --git a/src/libsyntax_ext/deriving/custom.rs b/src/libsyntax_ext/deriving/custom.rs index 55b3928d68eee..76826dd93e788 100644 --- a/src/libsyntax_ext/deriving/custom.rs +++ b/src/libsyntax_ext/deriving/custom.rs @@ -8,15 +8,18 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::panic; - use errors::FatalError; -use proc_macro::{TokenStream, __internal}; use syntax::ast::{self, ItemKind, Attribute, Mac}; use syntax::attr::{mark_used, mark_known}; use syntax::source_map::Span; use syntax::ext::base::*; +use syntax::parse; +use syntax::parse::token::{self, Token}; +use syntax::tokenstream; use syntax::visit::Visitor; +use syntax_pos::DUMMY_SP; + +use proc_macro_impl::EXEC_STRATEGY; struct MarkAttrs<'a>(&'a [ast::Name]); @@ -32,14 +35,10 @@ impl<'a> Visitor<'a> for MarkAttrs<'a> { } pub struct ProcMacroDerive { - inner: fn(TokenStream) -> TokenStream, - attrs: Vec, -} - -impl ProcMacroDerive { - pub fn new(inner: fn(TokenStream) -> TokenStream, attrs: Vec) -> ProcMacroDerive { - ProcMacroDerive { inner: inner, attrs: attrs } - } + pub client: ::proc_macro::bridge::client::Client< + fn(::proc_macro::TokenStream) -> ::proc_macro::TokenStream, + >, + pub attrs: Vec, } impl MultiItemModifier for ProcMacroDerive { @@ -75,10 +74,12 @@ impl MultiItemModifier for ProcMacroDerive { // Mark attributes as known, and used. MarkAttrs(&self.attrs).visit_item(&item); - let input = __internal::new_token_stream(ecx.resolver.eliminate_crate_var(item)); - let res = __internal::set_sess(ecx, || { - let inner = self.inner; - panic::catch_unwind(panic::AssertUnwindSafe(|| inner(input))) + let item = ecx.resolver.eliminate_crate_var(item); + let token = Token::interpolated(token::NtItem(item)); + let input = tokenstream::TokenTree::Token(DUMMY_SP, token).into(); + let server = ::proc_macro::rustc::Rustc; + let res = ::proc_macro::__internal::set_sess(ecx, || { + self.client.run(&EXEC_STRATEGY, server, input) }); let stream = match res { @@ -86,10 +87,7 @@ impl MultiItemModifier for ProcMacroDerive { Err(e) => { let msg = "proc-macro derive panicked"; let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.downcast_ref::() { - err.help(&format!("message: {}", s)); - } - if let Some(s) = e.downcast_ref::<&'static str>() { + if let Some(s) = e.as_str() { err.help(&format!("message: {}", s)); } @@ -99,21 +97,32 @@ impl MultiItemModifier for ProcMacroDerive { }; let error_count_before = ecx.parse_sess.span_diagnostic.err_count(); - __internal::set_sess(ecx, || { - let msg = "proc-macro derive produced unparseable tokens"; - match __internal::token_stream_parse_items(stream) { - // fail if there have been errors emitted - Ok(_) if ecx.parse_sess.span_diagnostic.err_count() > error_count_before => { - ecx.struct_span_fatal(span, msg).emit(); - FatalError.raise(); + let msg = "proc-macro derive produced unparseable tokens"; + + let mut parser = parse::stream_to_parser(ecx.parse_sess, stream); + let mut items = vec![]; + + loop { + match parser.parse_item().map_err(::proc_macro::__internal::parse_to_lex_err) { + Ok(None) => break, + Ok(Some(item)) => { + items.push(Annotatable::Item(item)) } - Ok(new_items) => new_items.into_iter().map(Annotatable::Item).collect(), Err(_) => { // FIXME: handle this better ecx.struct_span_fatal(span, msg).emit(); FatalError.raise(); } } - }) + } + + + // fail if there have been errors emitted + if ecx.parse_sess.span_diagnostic.err_count() > error_count_before { + ecx.struct_span_fatal(span, msg).emit(); + FatalError.raise(); + } + + items } } diff --git a/src/libsyntax_ext/lib.rs b/src/libsyntax_ext/lib.rs index 7c023fc5c9c22..c5bceff473396 100644 --- a/src/libsyntax_ext/lib.rs +++ b/src/libsyntax_ext/lib.rs @@ -55,9 +55,7 @@ mod trace_macros; mod test; mod test_case; -pub mod proc_macro_registrar; - - +pub mod proc_macro_decls; pub mod proc_macro_impl; use rustc_data_structures::sync::Lrc; diff --git a/src/libsyntax_ext/proc_macro_registrar.rs b/src/libsyntax_ext/proc_macro_decls.rs similarity index 76% rename from src/libsyntax_ext/proc_macro_registrar.rs rename to src/libsyntax_ext/proc_macro_decls.rs index 65e175f95df81..c859275ed020c 100644 --- a/src/libsyntax_ext/proc_macro_registrar.rs +++ b/src/libsyntax_ext/proc_macro_decls.rs @@ -91,7 +91,7 @@ pub fn modify(sess: &ParseSess, return krate; } - krate.module.items.push(mk_registrar(&mut cx, &derives, &attr_macros, &bang_macros)); + krate.module.items.push(mk_decls(&mut cx, &derives, &attr_macros, &bang_macros)); krate } @@ -339,19 +339,21 @@ impl<'a> Visitor<'a> for CollectProcMacros<'a> { // mod $gensym { // extern crate proc_macro; // -// use proc_macro::__internal::Registry; +// use proc_macro::bridge::client::ProcMacro; // -// #[plugin_registrar] -// fn registrar(registrar: &mut Registry) { -// registrar.register_custom_derive($name_trait1, ::$name1, &[]); -// registrar.register_custom_derive($name_trait2, ::$name2, &["attribute_name"]); +// #[rustc_proc_macro_decls] +// static DECLS: &[ProcMacro] = &[ +// ProcMacro::custom_derive($name_trait1, &[], ::$name1); +// ProcMacro::custom_derive($name_trait2, &["attribute_name"], ::$name2); // // ... -// } +// ]; // } -fn mk_registrar(cx: &mut ExtCtxt, - custom_derives: &[ProcMacroDerive], - custom_attrs: &[ProcMacroDef], - custom_macros: &[ProcMacroDef]) -> P { +fn mk_decls( + cx: &mut ExtCtxt, + custom_derives: &[ProcMacroDerive], + custom_attrs: &[ProcMacroDef], + custom_macros: &[ProcMacroDef], +) -> P { let mark = Mark::fresh(Mark::root()); mark.set_expn_info(ExpnInfo { call_site: DUMMY_SP, @@ -370,75 +372,67 @@ fn mk_registrar(cx: &mut ExtCtxt, Vec::new(), ast::ItemKind::ExternCrate(None)); - let __internal = Ident::from_str("__internal"); - let registry = Ident::from_str("Registry"); - let registrar = Ident::from_str("_registrar"); - let register_custom_derive = Ident::from_str("register_custom_derive"); - let register_attr_proc_macro = Ident::from_str("register_attr_proc_macro"); - let register_bang_proc_macro = Ident::from_str("register_bang_proc_macro"); + let bridge = Ident::from_str("bridge"); + let client = Ident::from_str("client"); + let proc_macro_ty = Ident::from_str("ProcMacro"); + let custom_derive = Ident::from_str("custom_derive"); + let attr = Ident::from_str("attr"); + let bang = Ident::from_str("bang"); let crate_kw = Ident::with_empty_ctxt(keywords::Crate.name()); - let local_path = |cx: &mut ExtCtxt, sp: Span, name: Ident| { - cx.path(sp.with_ctxt(span.ctxt()), vec![crate_kw, name]) + + let decls = { + let local_path = |sp: Span, name| { + cx.expr_path(cx.path(sp.with_ctxt(span.ctxt()), vec![crate_kw, name])) + }; + let proc_macro_ty_method_path = |method| cx.expr_path(cx.path(span, vec![ + proc_macro, bridge, client, proc_macro_ty, method, + ])); + custom_derives.iter().map(|cd| { + cx.expr_call(span, proc_macro_ty_method_path(custom_derive), vec![ + cx.expr_str(cd.span, cd.trait_name), + cx.expr_vec_slice( + span, + cd.attrs.iter().map(|&s| cx.expr_str(cd.span, s)).collect::>() + ), + local_path(cd.span, cd.function_name), + ]) + }).chain(custom_attrs.iter().map(|ca| { + cx.expr_call(span, proc_macro_ty_method_path(attr), vec![ + cx.expr_str(ca.span, ca.function_name.name), + local_path(ca.span, ca.function_name), + ]) + })).chain(custom_macros.iter().map(|cm| { + cx.expr_call(span, proc_macro_ty_method_path(bang), vec![ + cx.expr_str(cm.span, cm.function_name.name), + local_path(cm.span, cm.function_name), + ]) + })).collect() }; - let mut stmts = custom_derives.iter().map(|cd| { - let path = local_path(cx, cd.span, cd.function_name); - let trait_name = cx.expr_str(cd.span, cd.trait_name); - let attrs = cx.expr_vec_slice( - span, - cd.attrs.iter().map(|&s| cx.expr_str(cd.span, s)).collect::>() - ); - let registrar = cx.expr_ident(span, registrar); - let ufcs_path = cx.path(span, vec![proc_macro, __internal, registry, - register_custom_derive]); - - cx.stmt_expr(cx.expr_call(span, cx.expr_path(ufcs_path), - vec![registrar, trait_name, cx.expr_path(path), attrs])) - - }).collect::>(); - - stmts.extend(custom_attrs.iter().map(|ca| { - let name = cx.expr_str(ca.span, ca.function_name.name); - let path = local_path(cx, ca.span, ca.function_name); - let registrar = cx.expr_ident(ca.span, registrar); - - let ufcs_path = cx.path(span, - vec![proc_macro, __internal, registry, register_attr_proc_macro]); - - cx.stmt_expr(cx.expr_call(span, cx.expr_path(ufcs_path), - vec![registrar, name, cx.expr_path(path)])) - })); - - stmts.extend(custom_macros.iter().map(|cm| { - let name = cx.expr_str(cm.span, cm.function_name.name); - let path = local_path(cx, cm.span, cm.function_name); - let registrar = cx.expr_ident(cm.span, registrar); - - let ufcs_path = cx.path(span, - vec![proc_macro, __internal, registry, register_bang_proc_macro]); - - cx.stmt_expr(cx.expr_call(span, cx.expr_path(ufcs_path), - vec![registrar, name, cx.expr_path(path)])) - })); - - let path = cx.path(span, vec![proc_macro, __internal, registry]); - let registrar_path = cx.ty_path(path); - let arg_ty = cx.ty_rptr(span, registrar_path, None, ast::Mutability::Mutable); - let func = cx.item_fn(span, - registrar, - vec![cx.arg(span, registrar, arg_ty)], - cx.ty(span, ast::TyKind::Tup(Vec::new())), - cx.block(span, stmts)); - - let derive_registrar = cx.meta_word(span, Symbol::intern("rustc_derive_registrar")); - let derive_registrar = cx.attribute(span, derive_registrar); - let func = func.map(|mut i| { - i.attrs.push(derive_registrar); + let decls_static = cx.item_static( + span, + Ident::from_str("_DECLS"), + cx.ty_rptr(span, + cx.ty(span, ast::TyKind::Slice( + cx.ty_path(cx.path(span, + vec![proc_macro, bridge, client, proc_macro_ty])))), + None, ast::Mutability::Immutable), + ast::Mutability::Immutable, + cx.expr_vec_slice(span, decls), + ).map(|mut i| { + let attr = cx.meta_word(span, Symbol::intern("rustc_proc_macro_decls")); + i.attrs.push(cx.attribute(span, attr)); i.vis = respan(span, ast::VisibilityKind::Public); i }); - let ident = ast::Ident::with_empty_ctxt(Symbol::gensym("registrar")); - let module = cx.item_mod(span, span, ident, Vec::new(), vec![krate, func]).map(|mut i| { + + let module = cx.item_mod( + span, + span, + ast::Ident::with_empty_ctxt(Symbol::gensym("decls")), + vec![], + vec![krate, decls_static], + ).map(|mut i| { i.vis = respan(span, ast::VisibilityKind::Public); i }); diff --git a/src/libsyntax_ext/proc_macro_impl.rs b/src/libsyntax_ext/proc_macro_impl.rs index ff60262055bf3..1f111dfcaf9ca 100644 --- a/src/libsyntax_ext/proc_macro_impl.rs +++ b/src/libsyntax_ext/proc_macro_impl.rs @@ -8,7 +8,6 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::panic; use errors::FatalError; @@ -17,11 +16,13 @@ use syntax::ext::base::*; use syntax::tokenstream::TokenStream; use syntax::ext::base; -use proc_macro::TokenStream as TsShim; -use proc_macro::__internal; +pub const EXEC_STRATEGY: ::proc_macro::bridge::server::SameThread = + ::proc_macro::bridge::server::SameThread; pub struct AttrProcMacro { - pub inner: fn(TsShim, TsShim) -> TsShim, + pub client: ::proc_macro::bridge::client::Client< + fn(::proc_macro::TokenStream, ::proc_macro::TokenStream) -> ::proc_macro::TokenStream, + >, } impl base::AttrProcMacro for AttrProcMacro { @@ -31,22 +32,17 @@ impl base::AttrProcMacro for AttrProcMacro { annotation: TokenStream, annotated: TokenStream) -> TokenStream { - let annotation = __internal::token_stream_wrap(annotation); - let annotated = __internal::token_stream_wrap(annotated); - - let res = __internal::set_sess(ecx, || { - panic::catch_unwind(panic::AssertUnwindSafe(|| (self.inner)(annotation, annotated))) + let server = ::proc_macro::rustc::Rustc; + let res = ::proc_macro::__internal::set_sess(ecx, || { + self.client.run(&EXEC_STRATEGY, server, annotation, annotated) }); match res { - Ok(stream) => __internal::token_stream_inner(stream), + Ok(stream) => stream, Err(e) => { let msg = "custom attribute panicked"; let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.downcast_ref::() { - err.help(&format!("message: {}", s)); - } - if let Some(s) = e.downcast_ref::<&'static str>() { + if let Some(s) = e.as_str() { err.help(&format!("message: {}", s)); } @@ -58,7 +54,9 @@ impl base::AttrProcMacro for AttrProcMacro { } pub struct BangProcMacro { - pub inner: fn(TsShim) -> TsShim, + pub client: ::proc_macro::bridge::client::Client< + fn(::proc_macro::TokenStream) -> ::proc_macro::TokenStream, + >, } impl base::ProcMacro for BangProcMacro { @@ -67,21 +65,17 @@ impl base::ProcMacro for BangProcMacro { span: Span, input: TokenStream) -> TokenStream { - let input = __internal::token_stream_wrap(input); - - let res = __internal::set_sess(ecx, || { - panic::catch_unwind(panic::AssertUnwindSafe(|| (self.inner)(input))) + let server = ::proc_macro::rustc::Rustc; + let res = ::proc_macro::__internal::set_sess(ecx, || { + self.client.run(&EXEC_STRATEGY, server, input) }); match res { - Ok(stream) => __internal::token_stream_inner(stream), + Ok(stream) => stream, Err(e) => { let msg = "proc macro panicked"; let mut err = ecx.struct_span_fatal(span, msg); - if let Some(s) = e.downcast_ref::() { - err.help(&format!("message: {}", s)); - } - if let Some(s) = e.downcast_ref::<&'static str>() { + if let Some(s) = e.as_str() { err.help(&format!("message: {}", s)); }