5 use std::marker::PhantomData;
7 macro_rules! define_handles {
9 'owned: $($oty:ident,)*
10 'interned: $($ity:ident,)*
13 #[allow(non_snake_case)]
14 pub struct HandleCounters {
15 $($oty: AtomicUsize,)*
16 $($ity: AtomicUsize,)*
20 // FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
21 // a wrapper `fn` pointer, once `const fn` can reference `static`s.
22 extern "C" fn get() -> &'static Self {
23 static COUNTERS: HandleCounters = HandleCounters {
24 $($oty: AtomicUsize::new(1),)*
25 $($ity: AtomicUsize::new(1),)*
31 // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`.
32 #[allow(non_snake_case)]
33 pub(super) struct HandleStore<S: server::Types> {
34 $($oty: handle::OwnedStore<S::$oty>,)*
35 $($ity: handle::InternedStore<S::$ity>,)*
38 impl<S: server::Types> HandleStore<S> {
39 pub(super) fn new(handle_counters: &'static HandleCounters) -> Self {
41 $($oty: handle::OwnedStore::new(&handle_counters.$oty),)*
42 $($ity: handle::InternedStore::new(&handle_counters.$ity),)*
48 pub(crate) struct $oty {
49 handle: handle::Handle,
50 // Prevent Send and Sync impls. `!Send`/`!Sync` is the usual
51 // way of doing this, but that requires unstable features.
52 // rust-analyzer uses this code and avoids unstable features.
53 _marker: PhantomData<*mut ()>,
56 // Forward `Drop::drop` to the inherent `drop` method.
66 impl<S> Encode<S> for $oty {
67 fn encode(self, w: &mut Writer, s: &mut S) {
68 let handle = self.handle;
74 impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
75 for Marked<S::$oty, $oty>
77 fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
78 s.$oty.take(handle::Handle::decode(r, &mut ()))
82 impl<S> Encode<S> for &$oty {
83 fn encode(self, w: &mut Writer, s: &mut S) {
84 self.handle.encode(w, s);
88 impl<'s, S: server::Types> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>>
89 for &'s Marked<S::$oty, $oty>
91 fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self {
92 &s.$oty[handle::Handle::decode(r, &mut ())]
96 impl<S> Encode<S> for &mut $oty {
97 fn encode(self, w: &mut Writer, s: &mut S) {
98 self.handle.encode(w, s);
102 impl<'s, S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>>
103 for &'s mut Marked<S::$oty, $oty>
107 s: &'s mut HandleStore<server::MarkedTypes<S>>
109 &mut s.$oty[handle::Handle::decode(r, &mut ())]
113 impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
114 for Marked<S::$oty, $oty>
116 fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
117 s.$oty.alloc(self).encode(w, s);
121 impl<S> DecodeMut<'_, '_, S> for $oty {
122 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
124 handle: handle::Handle::decode(r, s),
125 _marker: PhantomData,
132 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
133 pub(crate) struct $ity {
134 handle: handle::Handle,
135 // Prevent Send and Sync impls. `!Send`/`!Sync` is the usual
136 // way of doing this, but that requires unstable features.
137 // rust-analyzer uses this code and avoids unstable features.
138 _marker: PhantomData<*mut ()>,
141 impl<S> Encode<S> for $ity {
142 fn encode(self, w: &mut Writer, s: &mut S) {
143 self.handle.encode(w, s);
147 impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>>
148 for Marked<S::$ity, $ity>
150 fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self {
151 s.$ity.copy(handle::Handle::decode(r, &mut ()))
155 impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>>
156 for Marked<S::$ity, $ity>
158 fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) {
159 s.$ity.alloc(self).encode(w, s);
163 impl<S> DecodeMut<'_, '_, S> for $ity {
164 fn decode(r: &mut Reader<'_>, s: &mut S) -> Self {
166 handle: handle::Handle::decode(r, s),
167 _marker: PhantomData,
184 // FIXME(eddyb) generate these impls by pattern-matching on the
185 // names of methods - also could use the presence of `fn drop`
186 // to distinguish between 'owned and 'interned, above.
187 // Alternatively, special "modes" could be listed of types in with_api
188 // instead of pattern matching on methods, here and in server decl.
190 impl Clone for TokenStream {
191 fn clone(&self) -> Self {
196 impl Clone for SourceFile {
197 fn clone(&self) -> Self {
203 pub(crate) fn def_site() -> Span {
204 Bridge::with(|bridge| bridge.globals.def_site)
207 pub(crate) fn call_site() -> Span {
208 Bridge::with(|bridge| bridge.globals.call_site)
211 pub(crate) fn mixed_site() -> Span {
212 Bridge::with(|bridge| bridge.globals.mixed_site)
216 impl fmt::Debug for Span {
217 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
218 f.write_str(&self.debug())
222 pub(crate) use super::symbol::Symbol;
224 macro_rules! define_client_side {
226 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
229 $(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)* {
230 Bridge::with(|bridge| {
231 let mut buf = bridge.cached_buffer.take();
234 api_tags::Method::$name(api_tags::$name::$method).encode(&mut buf, &mut ());
235 reverse_encode!(buf; $($arg),*);
237 buf = bridge.dispatch.call(buf);
239 let r = Result::<_, PanicMessage>::decode(&mut &buf[..], &mut ());
241 bridge.cached_buffer = buf;
243 r.unwrap_or_else(|e| panic::resume_unwind(e.into()))
249 with_api!(self, self, define_client_side);
252 /// Reusable buffer (only `clear`-ed, never shrunk), primarily
253 /// used for making requests.
254 cached_buffer: Buffer,
256 /// Server-side function that the client uses to make requests.
257 dispatch: closure::Closure<'a, Buffer, Buffer>,
259 /// Provided globals for this macro expansion.
260 globals: ExpnGlobals<Span>,
263 impl<'a> !Send for Bridge<'a> {}
264 impl<'a> !Sync for Bridge<'a> {}
266 enum BridgeState<'a> {
267 /// No server is currently connected to this client.
270 /// A server is connected and available for requests.
271 Connected(Bridge<'a>),
273 /// Access to the bridge is being exclusively acquired
274 /// (e.g., during `BridgeState::with`).
280 impl<'a> scoped_cell::ApplyL<'a> for BridgeStateL {
281 type Out = BridgeState<'a>;
285 static BRIDGE_STATE: scoped_cell::ScopedCell<BridgeStateL> =
286 scoped_cell::ScopedCell::new(BridgeState::NotConnected);
289 impl BridgeState<'_> {
290 /// Take exclusive control of the thread-local
291 /// `BridgeState`, and pass it to `f`, mutably.
292 /// The state will be restored after `f` exits, even
293 /// by panic, including modifications made to it by `f`.
295 /// N.B., while `f` is running, the thread-local state
296 /// is `BridgeState::InUse`.
297 fn with<R>(f: impl FnOnce(&mut BridgeState<'_>) -> R) -> R {
298 BRIDGE_STATE.with(|state| {
299 state.replace(BridgeState::InUse, |mut state| {
300 // FIXME(#52812) pass `f` directly to `replace` when `RefMutL` is gone
308 fn with<R>(f: impl FnOnce(&mut Bridge<'_>) -> R) -> R {
309 BridgeState::with(|state| match state {
310 BridgeState::NotConnected => {
311 panic!("procedural macro API is used outside of a procedural macro");
313 BridgeState::InUse => {
314 panic!("procedural macro API is used while it's already in use");
316 BridgeState::Connected(bridge) => f(bridge),
321 pub(crate) fn is_available() -> bool {
322 BridgeState::with(|state| match state {
323 BridgeState::Connected(_) | BridgeState::InUse => true,
324 BridgeState::NotConnected => false,
328 /// A client-side RPC entry-point, which may be using a different `proc_macro`
329 /// from the one used by the server, but can be invoked compatibly.
331 /// Note that the (phantom) `I` ("input") and `O` ("output") type parameters
332 /// decorate the `Client<I, O>` with the RPC "interface" of the entry-point, but
333 /// do not themselves participate in ABI, at all, only facilitate type-checking.
335 /// E.g. `Client<TokenStream, TokenStream>` is the common proc macro interface,
336 /// used for `#[proc_macro] fn foo(input: TokenStream) -> TokenStream`,
337 /// indicating that the RPC input and output will be serialized token streams,
338 /// and forcing the use of APIs that take/return `S::TokenStream`, server-side.
340 pub struct Client<I, O> {
341 // FIXME(eddyb) use a reference to the `static COUNTERS`, instead of
342 // a wrapper `fn` pointer, once `const fn` can reference `static`s.
343 pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters,
345 pub(super) run: extern "C" fn(BridgeConfig<'_>) -> Buffer,
347 pub(super) _marker: PhantomData<fn(I) -> O>,
350 impl<I, O> Copy for Client<I, O> {}
351 impl<I, O> Clone for Client<I, O> {
352 fn clone(&self) -> Self {
357 fn maybe_install_panic_hook(force_show_panics: bool) {
358 // Hide the default panic output within `proc_macro` expansions.
359 // NB. the server can't do this because it may use a different libstd.
360 static HIDE_PANICS_DURING_EXPANSION: Once = Once::new();
361 HIDE_PANICS_DURING_EXPANSION.call_once(|| {
362 let prev = panic::take_hook();
363 panic::set_hook(Box::new(move |info| {
364 let show = BridgeState::with(|state| match state {
365 BridgeState::NotConnected => true,
366 BridgeState::Connected(_) | BridgeState::InUse => force_show_panics,
375 /// Client-side helper for handling client panics, entering the bridge,
376 /// deserializing input and serializing output.
377 // FIXME(eddyb) maybe replace `Bridge::enter` with this?
378 fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>(
379 config: BridgeConfig<'_>,
380 f: impl FnOnce(A) -> R,
382 let BridgeConfig { input: mut buf, dispatch, force_show_panics, .. } = config;
384 panic::catch_unwind(panic::AssertUnwindSafe(|| {
385 maybe_install_panic_hook(force_show_panics);
387 // Make sure the symbol store is empty before decoding inputs.
388 Symbol::invalidate_all();
390 let reader = &mut &buf[..];
391 let (globals, input) = <(ExpnGlobals<Span>, A)>::decode(reader, &mut ());
393 // Put the buffer we used for input back in the `Bridge` for requests.
395 BridgeState::Connected(Bridge { cached_buffer: buf.take(), dispatch, globals });
397 BRIDGE_STATE.with(|state| {
398 state.set(new_state, || {
399 let output = f(input);
401 // Take the `cached_buffer` back out, for the output value.
402 buf = Bridge::with(|bridge| bridge.cached_buffer.take());
404 // HACK(eddyb) Separate encoding a success value (`Ok(output)`)
405 // from encoding a panic (`Err(e: PanicMessage)`) to avoid
406 // having handles outside the `bridge.enter(|| ...)` scope, and
407 // to catch panics that could happen while encoding the success.
409 // Note that panics should be impossible beyond this point, but
410 // this is defensively trying to avoid any accidental panicking
411 // reaching the `extern "C"` (which should `abort` but might not
412 // at the moment, so this is also potentially preventing UB).
414 Ok::<_, ()>(output).encode(&mut buf, &mut ());
418 .map_err(PanicMessage::from)
419 .unwrap_or_else(|e| {
421 Err::<(), _>(e).encode(&mut buf, &mut ());
424 // Now that a response has been serialized, invalidate all symbols
425 // registered with the interner.
426 Symbol::invalidate_all();
430 impl Client<crate::TokenStream, crate::TokenStream> {
431 pub const fn expand1(f: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy) -> Self {
433 get_handle_counters: HandleCounters::get,
434 run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
435 run_client(bridge, |input| f(crate::TokenStream(Some(input))).0)
437 _marker: PhantomData,
442 impl Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream> {
443 pub const fn expand2(
444 f: impl Fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream + Copy,
447 get_handle_counters: HandleCounters::get,
448 run: super::selfless_reify::reify_to_extern_c_fn_hrt_bridge(move |bridge| {
449 run_client(bridge, |(input, input2)| {
450 f(crate::TokenStream(Some(input)), crate::TokenStream(Some(input2))).0
453 _marker: PhantomData,
459 #[derive(Copy, Clone)]
462 trait_name: &'static str,
463 attributes: &'static [&'static str],
464 client: Client<crate::TokenStream, crate::TokenStream>,
469 client: Client<(crate::TokenStream, crate::TokenStream), crate::TokenStream>,
474 client: Client<crate::TokenStream, crate::TokenStream>,
479 pub fn name(&self) -> &'static str {
481 ProcMacro::CustomDerive { trait_name, .. } => trait_name,
482 ProcMacro::Attr { name, .. } => name,
483 ProcMacro::Bang { name, .. } => name,
487 pub const fn custom_derive(
488 trait_name: &'static str,
489 attributes: &'static [&'static str],
490 expand: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy,
492 ProcMacro::CustomDerive { trait_name, attributes, client: Client::expand1(expand) }
497 expand: impl Fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream + Copy,
499 ProcMacro::Attr { name, client: Client::expand2(expand) }
504 expand: impl Fn(crate::TokenStream) -> crate::TokenStream + Copy,
506 ProcMacro::Bang { name, client: Client::expand1(expand) }