5 macro_rules! define_handles {
7 'owned: $($oty:ident,)*
8 'interned: $($ity:ident,)*
11 #[allow(non_snake_case)]
12 pub struct HandleCounters {
13 $($oty: AtomicUsize,)*
14 $($ity: AtomicUsize,)*
18 // FIXME(#53451) public to work around `Cannot create local mono-item` ICE.
19 pub extern "C" fn get() -> &'static Self {
20 static COUNTERS: HandleCounters = HandleCounters {
21 $($oty: AtomicUsize::new(1),)*
22 $($ity: AtomicUsize::new(1),)*
28 // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
30 #[allow(non_snake_case)]
31 pub(super) struct HandleStore<S: server::Types> {
32 $($oty: handle::OwnedStore<S::$oty>,)*
33 $($ity: handle::InternedStore<S::$ity>,)*
36 impl<S: server::Types> HandleStore<S> {
37 pub(super) fn new(handle_counters: &'static HandleCounters) -> Self {
39 $($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
40 $($ity: handle::InternedStore::new(&handle_counters.$ity),)*
47 pub(crate) struct $oty(handle::Handle);
48 impl !Send for $oty {}
49 impl !Sync for $oty {}
51 // Forward `Drop::drop` to the inherent `drop` method.
58 impl<S> Encode<S> for $oty {
59 fn encode(self, w: &mut Writer, s: &mut S) {
66 impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
67 for Marked<S::$oty, $oty>
69 fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
70 s.$oty.take(handle::Handle::decode(r, &mut ()))
74 impl<S> Encode<S> for &$oty {
75 fn encode(self, w: &mut Writer, s: &mut S) {
80 impl<S: server::Types> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>>
81 for &'s Marked<S::$oty, $oty>
83 fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self {
84 &s.$oty[handle::Handle::decode(r, &mut ())]
88 impl<S> Encode<S> for &mut $oty {
89 fn encode(self, w: &mut Writer, s: &mut S) {
94 impl<S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>>
95 for &'s mut Marked<S::$oty, $oty>
99 s: &'s mut HandleStore<server::MarkedTypes<S>>
101 &mut s.$oty[handle::Handle::decode(r, &mut ())]
105 impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
106 for Marked<S::$oty, $oty>
108 fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
109 s.$oty.alloc(self).encode(w, s);
113 impl<S> DecodeMut<'_, '_, S> for $oty {
114 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
115 $oty(handle::Handle::decode(r, s))
122 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
123 pub(crate) struct $ity(handle::Handle);
124 impl !Send for $ity {}
125 impl !Sync for $ity {}
127 impl<S> Encode<S> for $ity {
128 fn encode(self, w: &mut Writer, s: &mut S) {
133 impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
134 for Marked<S::$ity, $ity>
136 fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
137 s.$ity.copy(handle::Handle::decode(r, &mut ()))
141 impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
142 for Marked<S::$ity, $ity>
144 fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
145 s.$ity.alloc(self).encode(w, s);
149 impl<S> DecodeMut<'_, '_, S> for $ity {
150 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
151 $ity(handle::Handle::decode(r, s))
174 // FIXME(eddyb) generate these impls by pattern-matching on the
175 // names of methods - also could use the presence of `fn drop`
176 // to distinguish between 'owned and 'interned, above.
177 // Alternatively, special 'modes" could be listed of types in with_api
178 // instead of pattern matching on methods, here and in server decl.
180 impl Clone for TokenStream {
181 fn clone(&self) -> Self {
186 impl Clone for TokenStreamIter {
187 fn clone(&self) -> Self {
192 impl Clone for Group {
193 fn clone(&self) -> Self {
198 impl Clone for Literal {
199 fn clone(&self) -> Self {
204 // FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
205 impl fmt::Debug for Literal {
206 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
207 f.write_str(&self.debug())
211 impl Clone for SourceFile {
212 fn clone(&self) -> Self {
217 impl fmt::Debug for Span {
218 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
219 f.write_str(&self.debug())
223 macro_rules! define_client_side {
225 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
228 $(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)* {
229 Bridge::with(|bridge| {
230 let mut b = bridge.cached_buffer.take();
233 api_tags::Method::$name(api_tags::$name::$method).encode(&mut b, &mut ());
234 reverse_encode!(b; $($arg),*);
236 b = bridge.dispatch.call(b);
238 let r = Result::<_, PanicMessage>::decode(&mut &b[..], &mut ());
240 bridge.cached_buffer = b;
242 r.unwrap_or_else(|e| panic::resume_unwind(e.into()))
248 with_api!(self, self, define_client_side);
250 enum BridgeState<'a> {
251 /// No server is currently connected to this client.
254 /// A server is connected and available for requests.
255 Connected(Bridge<'a>),
257 /// Access to the bridge is being exclusively acquired
258 /// (e.g., during `BridgeState::with`).
264 impl<'a> scoped_cell::ApplyL<'a> for BridgeStateL {
265 type Out = BridgeState<'a>;
269 static BRIDGE_STATE: scoped_cell::ScopedCell<BridgeStateL> =
270 scoped_cell::ScopedCell::new(BridgeState::NotConnected);
273 impl BridgeState<'_> {
274 /// Take exclusive control of the thread-local
275 /// `BridgeState`, and pass it to `f`, mutably.
276 /// The state will be restored after `f` exits, even
277 /// by panic, including modifications made to it by `f`.
279 /// N.B., while `f` is running, the thread-local state
280 /// is `BridgeState::InUse`.
281 fn with<R>(f: impl FnOnce(&mut BridgeState<'_>) -> R) -> R {
282 BRIDGE_STATE.with(|state| {
283 state.replace(BridgeState::InUse, |mut state| {
284 // FIXME(#52812) pass `f` directly to `replace` when `RefMutL` is gone
292 fn enter<R>(self, f: impl FnOnce() -> R) -> R {
293 // Hide the default panic output within `proc_macro` expansions.
294 // NB. the server can't do this because it may use a different libstd.
295 static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
296 HIDE_PANICS_DURING_EXPANSION.call_once(|| {
297 let prev = panic::take_hook();
298 panic::set_hook(Box::new(move |info| {
299 let hide = BridgeState::with(|state| match state {
300 BridgeState::NotConnected => false,
301 BridgeState::Connected(_) | BridgeState::InUse => true,
309 BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f))
312 fn with<R>(f: impl FnOnce(&mut Bridge<'_>) -> R) -> R {
313 BridgeState::with(|state| match state {
314 BridgeState::NotConnected => {
315 panic!("procedural macro API is used outside of a procedural macro");
317 BridgeState::InUse => {
318 panic!("procedural macro API is used while it's already in use");
320 BridgeState::Connected(bridge) => f(bridge),
325 /// A client-side "global object" (usually a function pointer),
326 /// which may be using a different `proc_macro` from the one
327 /// used by the server, but can be interacted with compatibly.
329 /// N.B., `F` must have FFI-friendly memory layout (e.g., a pointer).
330 /// The call ABI of function pointers used for `F` doesn't
331 /// need to match between server and client, since it's only
332 /// passed between them and (eventually) called by the client.
334 #[derive(Copy, Clone)]
335 pub struct Client<F> {
336 pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters,
337 pub(super) run: extern "C" fn(Bridge<'_>, F) -> Buffer<u8>,
341 // FIXME(#53451) public to work around `Cannot create local mono-item` ICE,
342 // affecting not only the function itself, but also the `BridgeState` `thread_local!`.
343 pub extern "C" fn __run_expand1(
344 mut bridge: Bridge<'_>,
345 f: fn(crate::TokenStream) -> crate::TokenStream,
347 // The initial `cached_buffer` contains the input.
348 let mut b = bridge.cached_buffer.take();
350 panic::catch_unwind(panic::AssertUnwindSafe(|| {
352 let reader = &mut &b[..];
353 let input = TokenStream::decode(reader, &mut ());
355 // Put the `cached_buffer` back in the `Bridge`, for requests.
356 Bridge::with(|bridge| bridge.cached_buffer = b.take());
358 let output = f(crate::TokenStream(input)).0;
360 // Take the `cached_buffer` back out, for the output value.
361 b = Bridge::with(|bridge| bridge.cached_buffer.take());
363 // HACK(eddyb) Separate encoding a success value (`Ok(output)`)
364 // from encoding a panic (`Err(e: PanicMessage)`) to avoid
365 // having handles outside the `bridge.enter(|| ...)` scope, and
366 // to catch panics that could happen while encoding the success.
368 // Note that panics should be impossible beyond this point, but
369 // this is defensively trying to avoid any accidental panicking
370 // reaching the `extern "C"` (which should `abort` but may not
371 // at the moment, so this is also potentially preventing UB).
373 Ok::<_, ()>(output).encode(&mut b, &mut ());
376 .map_err(PanicMessage::from)
377 .unwrap_or_else(|e| {
379 Err::<(), _>(e).encode(&mut b, &mut ());
384 impl Client<fn(crate::TokenStream) -> crate::TokenStream> {
385 pub const fn expand1(f: fn(crate::TokenStream) -> crate::TokenStream) -> Self {
387 get_handle_counters: HandleCounters::get,
394 // FIXME(#53451) public to work around `Cannot create local mono-item` ICE,
395 // affecting not only the function itself, but also the `BridgeState` `thread_local!`.
396 pub extern "C" fn __run_expand2(
397 mut bridge: Bridge<'_>,
398 f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
400 // The initial `cached_buffer` contains the input.
401 let mut b = bridge.cached_buffer.take();
403 panic::catch_unwind(panic::AssertUnwindSafe(|| {
405 let reader = &mut &b[..];
406 let input = TokenStream::decode(reader, &mut ());
407 let input2 = TokenStream::decode(reader, &mut ());
409 // Put the `cached_buffer` back in the `Bridge`, for requests.
410 Bridge::with(|bridge| bridge.cached_buffer = b.take());
412 let output = f(crate::TokenStream(input), crate::TokenStream(input2)).0;
414 // Take the `cached_buffer` back out, for the output value.
415 b = Bridge::with(|bridge| bridge.cached_buffer.take());
417 // HACK(eddyb) Separate encoding a success value (`Ok(output)`)
418 // from encoding a panic (`Err(e: PanicMessage)`) to avoid
419 // having handles outside the `bridge.enter(|| ...)` scope, and
420 // to catch panics that could happen while encoding the success.
422 // Note that panics should be impossible beyond this point, but
423 // this is defensively trying to avoid any accidental panicking
424 // reaching the `extern "C"` (which should `abort` but may not
425 // at the moment, so this is also potentially preventing UB).
427 Ok::<_, ()>(output).encode(&mut b, &mut ());
430 .map_err(PanicMessage::from)
431 .unwrap_or_else(|e| {
433 Err::<(), _>(e).encode(&mut b, &mut ());
438 impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> {
439 pub const fn expand2(
440 f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream
443 get_handle_counters: HandleCounters::get,
451 #[derive(Copy, Clone)]
454 trait_name: &'static str,
455 attributes: &'static [&'static str],
456 client: Client<fn(crate::TokenStream) -> crate::TokenStream>,
461 client: Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream>,
466 client: Client<fn(crate::TokenStream) -> crate::TokenStream>,
471 pub fn name(&self) -> &'static str {
473 ProcMacro::CustomDerive { trait_name, .. } => trait_name,
474 ProcMacro::Attr { name, .. } => name,
475 ProcMacro::Bang { name, ..} => name
479 pub const fn custom_derive(
480 trait_name: &'static str,
481 attributes: &'static [&'static str],
482 expand: fn(crate::TokenStream) -> crate::TokenStream,
484 ProcMacro::CustomDerive {
487 client: Client::expand1(expand),
493 expand: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream,
497 client: Client::expand2(expand),
503 expand: fn(crate::TokenStream) -> crate::TokenStream
507 client: Client::expand1(expand),