Skip to content

Commit 91fa948

Browse files
committed
reorganize proc_macro crate
1 parent 42b384e commit 91fa948

File tree

14 files changed

+158
-143
lines changed

14 files changed

+158
-143
lines changed

library/proc_macro/src/bridge/client.rs renamed to library/proc_macro/src/backend/client.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ macro_rules! define_client_handles {
2525

2626
$(
2727
pub(crate) struct $oty {
28-
handle: handle::Handle,
28+
handle: Handle,
2929
}
3030

3131
impl !Send for $oty {}
@@ -61,7 +61,7 @@ macro_rules! define_client_handles {
6161
impl<S> DecodeMut<'_, '_, S> for $oty {
6262
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
6363
$oty {
64-
handle: handle::Handle::decode(r, s),
64+
handle: Handle::decode(r, s),
6565
}
6666
}
6767
}
@@ -70,7 +70,7 @@ macro_rules! define_client_handles {
7070
$(
7171
#[derive(Copy, Clone, PartialEq, Eq, Hash)]
7272
pub(crate) struct $ity {
73-
handle: handle::Handle,
73+
handle: Handle,
7474
}
7575

7676
impl !Send for $ity {}
@@ -85,7 +85,7 @@ macro_rules! define_client_handles {
8585
impl<S> DecodeMut<'_, '_, S> for $ity {
8686
fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
8787
$ity {
88-
handle: handle::Handle::decode(r, s),
88+
handle: Handle::decode(r, s),
8989
}
9090
}
9191
}
@@ -161,7 +161,7 @@ struct Bridge<'a> {
161161
cached_buffer: Buffer,
162162

163163
/// Server-side function that the client uses to make requests.
164-
dispatch: closure::Closure<'a, Buffer, Buffer>,
164+
dispatch: Closure<'a, Buffer, Buffer>,
165165

166166
/// Provided globals for this macro expansion.
167167
globals: ExpnGlobals<Span>,
@@ -327,7 +327,7 @@ impl Client<crate::TokenStream, crate::TokenStream> {
327327
pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
328328
Client {
329329
handle_counters: &COUNTERS,
330-
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
330+
run: reify_to_extern_c_fn_hrt_bridge(move |bridge| {
331331
run_client(bridge, |input| f(crate::TokenStream(Some(input))).0)
332332
}),
333333
_marker: PhantomData,
@@ -341,7 +341,7 @@ impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
341341
) -> Self {
342342
Client {
343343
handle_counters: &COUNTERS,
344-
run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
344+
run: reify_to_extern_c_fn_hrt_bridge(move |bridge| {
345345
run_client(bridge, |(input, input2)| {
346346
f(crate::TokenStream(Some(input)), crate::TokenStream(Some(input2))).0
347347
})

library/proc_macro/src/bridge/mod.rs renamed to library/proc_macro/src/backend/mod.rs

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -119,31 +119,24 @@ macro_rules! with_api_handle_types {
119119
};
120120
}
121121

122-
#[allow(unsafe_code)]
123-
mod arena;
124-
#[allow(unsafe_code)]
125-
mod buffer;
126122
#[deny(unsafe_code)]
127123
pub mod client;
128-
#[allow(unsafe_code)]
129-
mod closure;
130-
#[forbid(unsafe_code)]
131-
mod fxhash;
132-
#[forbid(unsafe_code)]
133-
mod handle;
134124
#[macro_use]
135125
#[forbid(unsafe_code)]
136126
mod rpc;
137-
#[allow(unsafe_code)]
138-
mod selfless_reify;
139127
#[forbid(unsafe_code)]
140128
pub mod server;
129+
mod support;
141130
#[allow(unsafe_code)]
142131
mod symbol;
143-
144-
use buffer::Buffer;
145132
pub use rpc::PanicMessage;
146133
use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
134+
pub(crate) use support::arena::Arena;
135+
pub(crate) use support::buffer::Buffer;
136+
pub(crate) use support::closure::Closure;
137+
pub(crate) use support::fxhash::FxHashMap;
138+
pub(crate) use support::handle::{Handle, InternedStore, OwnedStore};
139+
pub(crate) use support::selfless_reify::reify_to_extern_c_fn_hrt_bridge;
147140

148141
/// Configuration for establishing an active connection between a server and a
149142
/// client. The server creates the bridge config (`run_server` in `server.rs`),
@@ -156,7 +149,7 @@ pub struct BridgeConfig<'a> {
156149
input: Buffer,
157150

158151
/// Server-side function that the client uses to make requests.
159-
dispatch: closure::Closure<'a, Buffer, Buffer>,
152+
dispatch: Closure<'a, Buffer, Buffer>,
160153

161154
/// If 'true', always invoke the default panic hook
162155
force_show_panics: bool,

library/proc_macro/src/bridge/rpc.rs renamed to library/proc_macro/src/backend/rpc.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::io::Write;
55
use std::num::NonZero;
66
use std::str;
77

8-
pub(super) type Writer = super::buffer::Buffer;
8+
pub(super) type Writer = super::Buffer;
99

1010
pub(super) trait Encode<S>: Sized {
1111
fn encode(self, w: &mut Writer, s: &mut S);

library/proc_macro/src/bridge/server.rs renamed to library/proc_macro/src/backend/server.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ macro_rules! define_server_handles {
1212
) => {
1313
#[allow(non_snake_case)]
1414
pub(super) struct HandleStore<S: Types> {
15-
$($oty: handle::OwnedStore<S::$oty>,)*
16-
$($ity: handle::InternedStore<S::$ity>,)*
15+
$($oty: OwnedStore<S::$oty>,)*
16+
$($ity: InternedStore<S::$ity>,)*
1717
}
1818

1919
impl<S: Types> HandleStore<S> {
2020
fn new(handle_counters: &'static client::HandleCounters) -> Self {
2121
HandleStore {
22-
$($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
23-
$($ity: handle::InternedStore::new(&handle_counters.$ity),)*
22+
$($oty: OwnedStore::new(&handle_counters.$oty),)*
23+
$($ity: InternedStore::new(&handle_counters.$ity),)*
2424
}
2525
}
2626
}
@@ -36,15 +36,15 @@ macro_rules! define_server_handles {
3636
for Marked<S::$oty, client::$oty>
3737
{
3838
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
39-
s.$oty.take(handle::Handle::decode(r, &mut ()))
39+
s.$oty.take(Handle::decode(r, &mut ()))
4040
}
4141
}
4242

4343
impl<'s, S: Types> Decode<'_, 's, HandleStore<MarkedTypes<S>>>
4444
for &'s Marked<S::$oty, client::$oty>
4545
{
4646
fn decode(r: &mut Reader<'_>, s: &'s HandleStore<MarkedTypes<S>>) -> Self {
47-
&s.$oty[handle::Handle::decode(r, &mut ())]
47+
&s.$oty[Handle::decode(r, &mut ())]
4848
}
4949
}
5050

@@ -55,7 +55,7 @@ macro_rules! define_server_handles {
5555
r: &mut Reader<'_>,
5656
s: &'s mut HandleStore<MarkedTypes<S>>
5757
) -> Self {
58-
&mut s.$oty[handle::Handle::decode(r, &mut ())]
58+
&mut s.$oty[Handle::decode(r, &mut ())]
5959
}
6060
}
6161
)*
@@ -71,7 +71,7 @@ macro_rules! define_server_handles {
7171
for Marked<S::$ity, client::$ity>
7272
{
7373
fn decode(r: &mut Reader<'_>, s: &mut HandleStore<MarkedTypes<S>>) -> Self {
74-
s.$ity.copy(handle::Handle::decode(r, &mut ()))
74+
s.$ity.copy(Handle::decode(r, &mut ()))
7575
}
7676
}
7777
)*

library/proc_macro/src/bridge/buffer.rs renamed to library/proc_macro/src/backend/support/buffer.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,17 +41,17 @@ impl DerefMut for Buffer {
4141

4242
impl Buffer {
4343
#[inline]
44-
pub(super) fn new() -> Self {
44+
pub(crate) fn new() -> Self {
4545
Self::default()
4646
}
4747

4848
#[inline]
49-
pub(super) fn clear(&mut self) {
49+
pub(crate) fn clear(&mut self) {
5050
self.len = 0;
5151
}
5252

5353
#[inline]
54-
pub(super) fn take(&mut self) -> Self {
54+
pub(crate) fn take(&mut self) -> Self {
5555
mem::take(self)
5656
}
5757

@@ -60,7 +60,7 @@ impl Buffer {
6060
// (avoiding a memmove call). With extend_from_slice, LLVM at least
6161
// currently is not able to make that optimization.
6262
#[inline]
63-
pub(super) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
63+
pub(crate) fn extend_from_array<const N: usize>(&mut self, xs: &[u8; N]) {
6464
if xs.len() > (self.capacity - self.len) {
6565
let b = self.take();
6666
*self = (b.reserve)(b, xs.len());
@@ -72,7 +72,7 @@ impl Buffer {
7272
}
7373

7474
#[inline]
75-
pub(super) fn extend_from_slice(&mut self, xs: &[u8]) {
75+
pub(crate) fn extend_from_slice(&mut self, xs: &[u8]) {
7676
if xs.len() > (self.capacity - self.len) {
7777
let b = self.take();
7878
*self = (b.reserve)(b, xs.len());
@@ -84,7 +84,7 @@ impl Buffer {
8484
}
8585

8686
#[inline]
87-
pub(super) fn push(&mut self, v: u8) {
87+
pub(crate) fn push(&mut self, v: u8) {
8888
// The code here is taken from Vec::push, and we know that reserve()
8989
// will panic if we're exceeding isize::MAX bytes and so there's no need
9090
// to check for overflow.

library/proc_macro/src/bridge/closure.rs renamed to library/proc_macro/src/backend/support/closure.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::marker::PhantomData;
44

55
#[repr(C)]
6-
pub(super) struct Closure<'a, A, R> {
6+
pub(crate) struct Closure<'a, A, R> {
77
call: unsafe extern "C" fn(*mut Env, A) -> R,
88
env: *mut Env,
99
// Prevent Send and Sync impls.
@@ -24,7 +24,7 @@ impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> {
2424
}
2525

2626
impl<'a, A, R> Closure<'a, A, R> {
27-
pub(super) fn call(&mut self, arg: A) -> R {
27+
pub(crate) fn call(&mut self, arg: A) -> R {
2828
unsafe { (self.call)(self.env, arg) }
2929
}
3030
}

library/proc_macro/src/bridge/fxhash.rs renamed to library/proc_macro/src/backend/support/fxhash.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::hash::{BuildHasherDefault, Hasher};
99
use std::ops::BitXor;
1010

1111
/// Type alias for a hashmap using the `fx` hash algorithm.
12-
pub(super) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
12+
pub(crate) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
1313

1414
/// A speedy hash algorithm for use within rustc. The hashmap in alloc by
1515
/// default uses SipHash which isn't quite as speedy as we want. In the compiler
@@ -23,7 +23,7 @@ pub(super) type FxHashMap<K, V> = HashMap<K, V, BuildHasherDefault<FxHasher>>;
2323
/// similar or slightly worse than FNV, but the speed of the hash function
2424
/// itself is much higher because it works on up to 8 bytes at a time.
2525
#[derive(Default)]
26-
pub(super) struct FxHasher {
26+
pub(crate) struct FxHasher {
2727
hash: usize,
2828
}
2929

library/proc_macro/src/bridge/handle.rs renamed to library/proc_macro/src/backend/support/handle.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@ use std::sync::atomic::{AtomicU32, Ordering};
88

99
use super::fxhash::FxHashMap;
1010

11-
pub(super) type Handle = NonZero<u32>;
11+
pub(crate) type Handle = NonZero<u32>;
1212

1313
/// A store that associates values of type `T` with numeric handles. A value can
1414
/// be looked up using its handle.
15-
pub(super) struct OwnedStore<T: 'static> {
15+
pub(crate) struct OwnedStore<T: 'static> {
1616
counter: &'static AtomicU32,
1717
data: BTreeMap<Handle, T>,
1818
}
1919

2020
impl<T> OwnedStore<T> {
21-
pub(super) fn new(counter: &'static AtomicU32) -> Self {
21+
pub(crate) fn new(counter: &'static AtomicU32) -> Self {
2222
// Ensure the handle counter isn't 0, which would panic later,
2323
// when `NonZero::new` (aka `Handle::new`) is called in `alloc`.
2424
assert_ne!(counter.load(Ordering::Relaxed), 0);
@@ -28,14 +28,14 @@ impl<T> OwnedStore<T> {
2828
}
2929

3030
impl<T> OwnedStore<T> {
31-
pub(super) fn alloc(&mut self, x: T) -> Handle {
31+
pub(crate) fn alloc(&mut self, x: T) -> Handle {
3232
let counter = self.counter.fetch_add(1, Ordering::Relaxed);
3333
let handle = Handle::new(counter).expect("`proc_macro` handle counter overflowed");
3434
assert!(self.data.insert(handle, x).is_none());
3535
handle
3636
}
3737

38-
pub(super) fn take(&mut self, h: Handle) -> T {
38+
pub(crate) fn take(&mut self, h: Handle) -> T {
3939
self.data.remove(&h).expect("use-after-free in `proc_macro` handle")
4040
}
4141
}
@@ -54,22 +54,22 @@ impl<T> IndexMut<Handle> for OwnedStore<T> {
5454
}
5555

5656
/// Like `OwnedStore`, but avoids storing any value more than once.
57-
pub(super) struct InternedStore<T: 'static> {
57+
pub(crate) struct InternedStore<T: 'static> {
5858
owned: OwnedStore<T>,
5959
interner: FxHashMap<T, Handle>,
6060
}
6161

6262
impl<T: Copy + Eq + Hash> InternedStore<T> {
63-
pub(super) fn new(counter: &'static AtomicU32) -> Self {
63+
pub(crate) fn new(counter: &'static AtomicU32) -> Self {
6464
InternedStore { owned: OwnedStore::new(counter), interner: FxHashMap::default() }
6565
}
6666

67-
pub(super) fn alloc(&mut self, x: T) -> Handle {
67+
pub(crate) fn alloc(&mut self, x: T) -> Handle {
6868
let owned = &mut self.owned;
6969
*self.interner.entry(x).or_insert_with(|| owned.alloc(x))
7070
}
7171

72-
pub(super) fn copy(&mut self, h: Handle) -> T {
72+
pub(crate) fn copy(&mut self, h: Handle) -> T {
7373
self.owned[h]
7474
}
7575
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#[allow(unsafe_code)]
2+
pub(crate) mod arena;
3+
#[allow(unsafe_code)]
4+
pub(crate) mod buffer;
5+
#[allow(unsafe_code)]
6+
pub(crate) mod closure;
7+
#[forbid(unsafe_code)]
8+
pub(crate) mod fxhash;
9+
#[forbid(unsafe_code)]
10+
pub(crate) mod handle;
11+
#[allow(unsafe_code)]
12+
pub(crate) mod selfless_reify;

0 commit comments

Comments
 (0)