Skip to content

More proc macro tweaks #97445

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move HandleStore into server.rs.
This fixes two `FIXME` comments, and makes things clearer, by not having
server code in `client.rs`.
  • Loading branch information
nnethercote committed Jun 9, 2022
commit 0d7dfaecc7acd0795740a3db16d91880866ac794
92 changes: 4 additions & 88 deletions library/proc_macro/src/bridge/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ macro_rules! define_handles {
) => {
#[repr(C)]
#[allow(non_snake_case)]
pub struct HandleCounters {
$($oty: AtomicUsize,)*
$($ity: AtomicUsize,)*
pub(super) struct HandleCounters {
$(pub(super) $oty: AtomicUsize,)*
$(pub(super) $ity: AtomicUsize,)*
}

impl HandleCounters {
Expand All @@ -28,23 +28,6 @@ macro_rules! define_handles {
}
}

// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
#[repr(C)]
#[allow(non_snake_case)]
pub(super) struct HandleStore<S: server::Types> {
$($oty: handle::OwnedStore<S::$oty>,)*
$($ity: handle::InternedStore<S::$ity>,)*
}

impl<S: server::Types> HandleStore<S> {
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 {
Expand Down Expand Up @@ -73,53 +56,18 @@ macro_rules! define_handles {
}
}

impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
for Marked<S::$oty, $oty>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
s.$oty.take(handle::Handle::decode(r, &mut ()))
}
}

impl<S> Encode<S> for &$oty {
fn encode(self, w: &mut Writer, s: &mut S) {
self.handle.encode(w, s);
}
}

impl<'s, S: server::Types> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>>
for &'s Marked<S::$oty, $oty>
{
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self {
&s.$oty[handle::Handle::decode(r, &mut ())]
}
}

impl<S> Encode<S> for &mut $oty {
fn encode(self, w: &mut Writer, s: &mut S) {
self.handle.encode(w, s);
}
}

impl<'s, S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>>
for &'s mut Marked<S::$oty, $oty>
{
fn decode(
r: &mut Reader<'_>,
s: &'s mut HandleStore<server::MarkedTypes<S>>
) -> Self {
&mut s.$oty[handle::Handle::decode(r, &mut ())]
}
}

impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
for Marked<S::$oty, $oty>
{
fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
s.$oty.alloc(self).encode(w, s);
}
}

impl<S> DecodeMut<'_, '_, S> for $oty {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$oty {
Expand Down Expand Up @@ -147,22 +95,6 @@ macro_rules! define_handles {
}
}

impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
for Marked<S::$ity, $ity>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
s.$ity.copy(handle::Handle::decode(r, &mut ()))
}
}

impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
for Marked<S::$ity, $ity>
{
fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
s.$ity.alloc(self).encode(w, s);
}
}

impl<S> DecodeMut<'_, '_, S> for $ity {
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
$ity {
Expand All @@ -174,23 +106,7 @@ macro_rules! define_handles {
)*
}
}
define_handles! {
'owned:
FreeFunctions,
TokenStream,
TokenStreamBuilder,
TokenStreamIter,
Group,
Literal,
SourceFile,
MultiSpan,
Diagnostic,

'interned:
Punct,
Ident,
Span,
}
with_api_types!(define_handles);

// FIXME(eddyb) generate these impls by pattern-matching on the
// names of methods - also could use the presence of `fn drop`
Expand Down
24 changes: 24 additions & 0 deletions library/proc_macro/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,30 @@ macro_rules! with_api {
};
}

// Similar to `with_api`, but only lists the types, and they are divided into
// the two storage categories.
macro_rules! with_api_types {
($m:ident) => {
$m! {
'owned:
FreeFunctions,
TokenStream,
TokenStreamBuilder,
TokenStreamIter,
Group,
Literal,
SourceFile,
MultiSpan,
Diagnostic,

'interned:
Punct,
Ident,
Span,
}
};
}

// FIXME(eddyb) this calls `encode` for each argument, but in reverse,
// to match the ordering in `reverse_decode`.
macro_rules! reverse_encode {
Expand Down
79 changes: 76 additions & 3 deletions library/proc_macro/src/bridge/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,81 @@

use super::*;

// FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
use super::client::HandleStore;
macro_rules! define_handles {
(
'owned: $($oty:ident,)*
'interned: $($ity:ident,)*
) => {
#[repr(C)]
#[allow(non_snake_case)]
struct HandleStore<S: Types> {
$($oty: handle::OwnedStore<S::$oty>,)*
$($ity: handle::InternedStore<S::$ity>,)*
}

impl<S: Types> HandleStore<S> {
fn new(handle_counters: &'static client::HandleCounters) -> Self {
HandleStore {
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
}
}
}

$(
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>>
for Marked<S::$oty, client::$oty>
{
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
s.$oty.alloc(self).encode(w, s);
}
}

impl<S: Types> DecodeMut<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$oty.take(handle::Handle::decode(r, &mut ()))
}
}

impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<MarkedTypes<S>>) -> Self {
&s.$oty[handle::Handle::decode(r, &mut ())]
}
}

impl<'s, S: Types> DecodeMut<'_, 's, HandleStore<MarkedTypes<S>>>
for &'s mut Marked<S::$oty, client::$oty>
{
fn decode(r: &mut Reader<'_>, s: &'s mut HandleStore<MarkedTypes<S>>) -> Self {
&mut s.$oty[handle::Handle::decode(r, &mut ())]
}
}
)*

$(
impl<S: Types> Encode<HandleStore<MarkedTypes<S>>>
for Marked<S::$ity, client::$ity>
{
fn encode(self, w: &mut Writer, s: &mut HandleStore<MarkedTypes<S>>) {
s.$ity.alloc(self).encode(w, s);
}
}

impl<S: Types> DecodeMut<'_, '_, HandleStore<MarkedTypes<S>>>
for Marked<S::$ity, client::$ity>
{
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
s.$ity.copy(handle::Handle::decode(r, &mut ()))
}
}
)*
}
}
with_api_types!(define_handles);

pub trait Types {
type FreeFunctions: 'static;
Expand Down Expand Up @@ -46,7 +119,7 @@ macro_rules! declare_server_traits {
}
with_api!(Self, self_, declare_server_traits);

pub(super) struct MarkedTypes<S: Types>(S);
struct MarkedTypes<S: Types>(S);

macro_rules! define_mark_types_impls {
($($name:ident {
Expand Down