diff options
author | Edwin Cheng <[email protected]> | 2020-04-04 09:04:01 +0100 |
---|---|---|
committer | Edwin Cheng <[email protected]> | 2020-04-06 16:07:48 +0100 |
commit | b2844917ad29e967043bea4e187421a6a3f61682 (patch) | |
tree | 92e8a79dee5710bd8dc88879f4e8bffc7c650eff /crates/ra_proc_macro_srv/src/proc_macro | |
parent | 40616fdb49252c17d8e11a4413ff861692d4cc03 (diff) |
Add proc_macro mod (copy from lib_proc_macro)
Diffstat (limited to 'crates/ra_proc_macro_srv/src/proc_macro')
10 files changed, 2939 insertions, 0 deletions
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/buffer.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/buffer.rs new file mode 100644 index 000000000..dae6ff1d1 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/buffer.rs | |||
@@ -0,0 +1,149 @@ | |||
1 | //! lib-proc-macro Buffer management for same-process client<->server communication. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/buffer.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use std::io::{self, Write}; | ||
7 | use std::mem; | ||
8 | use std::ops::{Deref, DerefMut}; | ||
9 | use std::slice; | ||
10 | |||
11 | #[repr(C)] | ||
12 | struct Slice<'a, T> { | ||
13 | data: &'a [T; 0], | ||
14 | len: usize, | ||
15 | } | ||
16 | |||
17 | unsafe impl<'a, T: Sync> Sync for Slice<'a, T> {} | ||
18 | unsafe impl<'a, T: Sync> Send for Slice<'a, T> {} | ||
19 | |||
20 | impl<'a, T> Copy for Slice<'a, T> {} | ||
21 | impl<'a, T> Clone for Slice<'a, T> { | ||
22 | fn clone(&self) -> Self { | ||
23 | *self | ||
24 | } | ||
25 | } | ||
26 | |||
27 | impl<'a, T> From<&'a [T]> for Slice<'a, T> { | ||
28 | fn from(xs: &'a [T]) -> Self { | ||
29 | Slice { data: unsafe { &*(xs.as_ptr() as *const [T; 0]) }, len: xs.len() } | ||
30 | } | ||
31 | } | ||
32 | |||
33 | impl<'a, T> Deref for Slice<'a, T> { | ||
34 | type Target = [T]; | ||
35 | fn deref(&self) -> &[T] { | ||
36 | unsafe { slice::from_raw_parts(self.data.as_ptr(), self.len) } | ||
37 | } | ||
38 | } | ||
39 | |||
40 | #[repr(C)] | ||
41 | pub struct Buffer<T: Copy> { | ||
42 | data: *mut T, | ||
43 | len: usize, | ||
44 | capacity: usize, | ||
45 | extend_from_slice: extern "C" fn(Buffer<T>, Slice<'_, T>) -> Buffer<T>, | ||
46 | drop: extern "C" fn(Buffer<T>), | ||
47 | } | ||
48 | |||
49 | unsafe impl<T: Copy + Sync> Sync for Buffer<T> {} | ||
50 | unsafe impl<T: Copy + Send> Send for Buffer<T> {} | ||
51 | |||
52 | impl<T: Copy> Default for Buffer<T> { | ||
53 | fn default() -> Self { | ||
54 | Self::from(vec![]) | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl<T: Copy> Deref for Buffer<T> { | ||
59 | type Target = [T]; | ||
60 | fn deref(&self) -> &[T] { | ||
61 | unsafe { slice::from_raw_parts(self.data as *const T, self.len) } | ||
62 | } | ||
63 | } | ||
64 | |||
65 | impl<T: Copy> DerefMut for Buffer<T> { | ||
66 | fn deref_mut(&mut self) -> &mut [T] { | ||
67 | unsafe { slice::from_raw_parts_mut(self.data, self.len) } | ||
68 | } | ||
69 | } | ||
70 | |||
71 | impl<T: Copy> Buffer<T> { | ||
72 | pub(super) fn new() -> Self { | ||
73 | Self::default() | ||
74 | } | ||
75 | |||
76 | pub(super) fn clear(&mut self) { | ||
77 | self.len = 0; | ||
78 | } | ||
79 | |||
80 | pub(super) fn take(&mut self) -> Self { | ||
81 | mem::take(self) | ||
82 | } | ||
83 | |||
84 | pub(super) fn extend_from_slice(&mut self, xs: &[T]) { | ||
85 | // Fast path to avoid going through an FFI call. | ||
86 | if let Some(final_len) = self.len.checked_add(xs.len()) { | ||
87 | if final_len <= self.capacity { | ||
88 | let dst = unsafe { slice::from_raw_parts_mut(self.data, self.capacity) }; | ||
89 | dst[self.len..][..xs.len()].copy_from_slice(xs); | ||
90 | self.len = final_len; | ||
91 | return; | ||
92 | } | ||
93 | } | ||
94 | let b = self.take(); | ||
95 | *self = (b.extend_from_slice)(b, Slice::from(xs)); | ||
96 | } | ||
97 | } | ||
98 | |||
99 | impl Write for Buffer<u8> { | ||
100 | fn write(&mut self, xs: &[u8]) -> io::Result<usize> { | ||
101 | self.extend_from_slice(xs); | ||
102 | Ok(xs.len()) | ||
103 | } | ||
104 | |||
105 | fn write_all(&mut self, xs: &[u8]) -> io::Result<()> { | ||
106 | self.extend_from_slice(xs); | ||
107 | Ok(()) | ||
108 | } | ||
109 | |||
110 | fn flush(&mut self) -> io::Result<()> { | ||
111 | Ok(()) | ||
112 | } | ||
113 | } | ||
114 | |||
115 | impl<T: Copy> Drop for Buffer<T> { | ||
116 | fn drop(&mut self) { | ||
117 | let b = self.take(); | ||
118 | (b.drop)(b); | ||
119 | } | ||
120 | } | ||
121 | |||
122 | impl<T: Copy> From<Vec<T>> for Buffer<T> { | ||
123 | fn from(mut v: Vec<T>) -> Self { | ||
124 | let (data, len, capacity) = (v.as_mut_ptr(), v.len(), v.capacity()); | ||
125 | mem::forget(v); | ||
126 | |||
127 | // This utility function is nested in here because it can *only* | ||
128 | // be safely called on `Buffer`s created by *this* `proc_macro`. | ||
129 | fn to_vec<T: Copy>(b: Buffer<T>) -> Vec<T> { | ||
130 | unsafe { | ||
131 | let Buffer { data, len, capacity, .. } = b; | ||
132 | mem::forget(b); | ||
133 | Vec::from_raw_parts(data, len, capacity) | ||
134 | } | ||
135 | } | ||
136 | |||
137 | extern "C" fn extend_from_slice<T: Copy>(b: Buffer<T>, xs: Slice<'_, T>) -> Buffer<T> { | ||
138 | let mut v = to_vec(b); | ||
139 | v.extend_from_slice(&xs); | ||
140 | Buffer::from(v) | ||
141 | } | ||
142 | |||
143 | extern "C" fn drop<T: Copy>(b: Buffer<T>) { | ||
144 | mem::drop(to_vec(b)); | ||
145 | } | ||
146 | |||
147 | Buffer { data, len, capacity, extend_from_slice, drop } | ||
148 | } | ||
149 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/client.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/client.rs new file mode 100644 index 000000000..4b5dc7fd0 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/client.rs | |||
@@ -0,0 +1,472 @@ | |||
1 | //! lib-proc-macro Client-side types. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/client.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use super::*; | ||
7 | |||
8 | macro_rules! define_handles { | ||
9 | ( | ||
10 | 'owned: $($oty:ident,)* | ||
11 | 'interned: $($ity:ident,)* | ||
12 | ) => { | ||
13 | #[repr(C)] | ||
14 | #[allow(non_snake_case)] | ||
15 | pub struct HandleCounters { | ||
16 | $($oty: AtomicUsize,)* | ||
17 | $($ity: AtomicUsize,)* | ||
18 | } | ||
19 | |||
20 | impl HandleCounters { | ||
21 | // FIXME(eddyb) use a reference to the `static COUNTERS`, intead of | ||
22 | // a wrapper `fn` pointer, once `const fn` can reference `static`s. | ||
23 | extern "C" fn get() -> &'static Self { | ||
24 | static COUNTERS: HandleCounters = HandleCounters { | ||
25 | $($oty: AtomicUsize::new(1),)* | ||
26 | $($ity: AtomicUsize::new(1),)* | ||
27 | }; | ||
28 | &COUNTERS | ||
29 | } | ||
30 | } | ||
31 | |||
32 | // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`. | ||
33 | #[repr(C)] | ||
34 | #[allow(non_snake_case)] | ||
35 | pub(super) struct HandleStore<S: server::Types> { | ||
36 | $($oty: handle::OwnedStore<S::$oty>,)* | ||
37 | $($ity: handle::InternedStore<S::$ity>,)* | ||
38 | } | ||
39 | |||
40 | impl<S: server::Types> HandleStore<S> { | ||
41 | pub(super) fn new(handle_counters: &'static HandleCounters) -> Self { | ||
42 | HandleStore { | ||
43 | $($oty: handle::OwnedStore::new(&handle_counters.$oty),)* | ||
44 | $($ity: handle::InternedStore::new(&handle_counters.$ity),)* | ||
45 | } | ||
46 | } | ||
47 | } | ||
48 | |||
49 | $( | ||
50 | #[repr(C)] | ||
51 | pub struct $oty(pub(crate) handle::Handle); | ||
52 | // impl !Send for $oty {} | ||
53 | // impl !Sync for $oty {} | ||
54 | |||
55 | // Forward `Drop::drop` to the inherent `drop` method. | ||
56 | impl Drop for $oty { | ||
57 | fn drop(&mut self) { | ||
58 | $oty(self.0).drop(); | ||
59 | } | ||
60 | } | ||
61 | |||
62 | impl<S> Encode<S> for $oty { | ||
63 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
64 | let handle = self.0; | ||
65 | mem::forget(self); | ||
66 | handle.encode(w, s); | ||
67 | } | ||
68 | } | ||
69 | |||
70 | impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>> | ||
71 | for Marked<S::$oty, $oty> | ||
72 | { | ||
73 | fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self { | ||
74 | s.$oty.take(handle::Handle::decode(r, &mut ())) | ||
75 | } | ||
76 | } | ||
77 | |||
78 | impl<S> Encode<S> for &$oty { | ||
79 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
80 | self.0.encode(w, s); | ||
81 | } | ||
82 | } | ||
83 | |||
84 | impl<'s, S: server::Types,> Decode<'_, 's, HandleStore<server::MarkedTypes<S>>> | ||
85 | for &'s Marked<S::$oty, $oty> | ||
86 | { | ||
87 | fn decode(r: &mut Reader<'_>, s: &'s HandleStore<server::MarkedTypes<S>>) -> Self { | ||
88 | &s.$oty[handle::Handle::decode(r, &mut ())] | ||
89 | } | ||
90 | } | ||
91 | |||
92 | impl<S> Encode<S> for &mut $oty { | ||
93 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
94 | self.0.encode(w, s); | ||
95 | } | ||
96 | } | ||
97 | |||
98 | impl<'s, S: server::Types> DecodeMut<'_, 's, HandleStore<server::MarkedTypes<S>>> | ||
99 | for &'s mut Marked<S::$oty, $oty> | ||
100 | { | ||
101 | fn decode( | ||
102 | r: &mut Reader<'_>, | ||
103 | s: &'s mut HandleStore<server::MarkedTypes<S>> | ||
104 | ) -> Self { | ||
105 | &mut s.$oty[handle::Handle::decode(r, &mut ())] | ||
106 | } | ||
107 | } | ||
108 | |||
109 | impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>> | ||
110 | for Marked<S::$oty, $oty> | ||
111 | { | ||
112 | fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) { | ||
113 | s.$oty.alloc(self).encode(w, s); | ||
114 | } | ||
115 | } | ||
116 | |||
117 | impl<S> DecodeMut<'_, '_, S> for $oty { | ||
118 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
119 | $oty(handle::Handle::decode(r, s)) | ||
120 | } | ||
121 | } | ||
122 | )* | ||
123 | |||
124 | $( | ||
125 | #[repr(C)] | ||
126 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
127 | pub(crate) struct $ity(handle::Handle); | ||
128 | // impl !Send for $ity {} | ||
129 | // impl !Sync for $ity {} | ||
130 | |||
131 | impl<S> Encode<S> for $ity { | ||
132 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
133 | self.0.encode(w, s); | ||
134 | } | ||
135 | } | ||
136 | |||
137 | impl<S: server::Types> DecodeMut<'_, '_, HandleStore<server::MarkedTypes<S>>> | ||
138 | for Marked<S::$ity, $ity> | ||
139 | { | ||
140 | fn decode(r: &mut Reader<'_>, s: &mut HandleStore<server::MarkedTypes<S>>) -> Self { | ||
141 | s.$ity.copy(handle::Handle::decode(r, &mut ())) | ||
142 | } | ||
143 | } | ||
144 | |||
145 | impl<S: server::Types> Encode<HandleStore<server::MarkedTypes<S>>> | ||
146 | for Marked<S::$ity, $ity> | ||
147 | { | ||
148 | fn encode(self, w: &mut Writer, s: &mut HandleStore<server::MarkedTypes<S>>) { | ||
149 | s.$ity.alloc(self).encode(w, s); | ||
150 | } | ||
151 | } | ||
152 | |||
153 | impl<S> DecodeMut<'_, '_, S> for $ity { | ||
154 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
155 | $ity(handle::Handle::decode(r, s)) | ||
156 | } | ||
157 | } | ||
158 | )* | ||
159 | } | ||
160 | } | ||
161 | define_handles! { | ||
162 | 'owned: | ||
163 | TokenStream, | ||
164 | TokenStreamBuilder, | ||
165 | TokenStreamIter, | ||
166 | Group, | ||
167 | Literal, | ||
168 | SourceFile, | ||
169 | MultiSpan, | ||
170 | Diagnostic, | ||
171 | |||
172 | 'interned: | ||
173 | Punct, | ||
174 | Ident, | ||
175 | Span, | ||
176 | } | ||
177 | |||
178 | // FIXME(eddyb) generate these impls by pattern-matching on the | ||
179 | // names of methods - also could use the presence of `fn drop` | ||
180 | // to distinguish between 'owned and 'interned, above. | ||
181 | // Alternatively, special 'modes" could be listed of types in with_api | ||
182 | // instead of pattern matching on methods, here and in server decl. | ||
183 | |||
184 | impl Clone for TokenStream { | ||
185 | fn clone(&self) -> Self { | ||
186 | self.clone() | ||
187 | } | ||
188 | } | ||
189 | |||
190 | impl Clone for TokenStreamIter { | ||
191 | fn clone(&self) -> Self { | ||
192 | self.clone() | ||
193 | } | ||
194 | } | ||
195 | |||
196 | impl Clone for Group { | ||
197 | fn clone(&self) -> Self { | ||
198 | self.clone() | ||
199 | } | ||
200 | } | ||
201 | |||
202 | impl Clone for Literal { | ||
203 | fn clone(&self) -> Self { | ||
204 | self.clone() | ||
205 | } | ||
206 | } | ||
207 | |||
208 | // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. | ||
209 | impl fmt::Debug for Literal { | ||
210 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
211 | f.write_str(&self.debug()) | ||
212 | } | ||
213 | } | ||
214 | |||
215 | impl Clone for SourceFile { | ||
216 | fn clone(&self) -> Self { | ||
217 | self.clone() | ||
218 | } | ||
219 | } | ||
220 | |||
221 | impl fmt::Debug for Span { | ||
222 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
223 | f.write_str(&self.debug()) | ||
224 | } | ||
225 | } | ||
226 | |||
227 | macro_rules! define_client_side { | ||
228 | ($($name:ident { | ||
229 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* | ||
230 | }),* $(,)?) => { | ||
231 | $(impl $name { | ||
232 | #[allow(unused)] | ||
233 | $(pub(crate) fn $method($($arg: $arg_ty),*) $(-> $ret_ty)* { | ||
234 | panic!("hello"); | ||
235 | // Bridge::with(|bridge| { | ||
236 | // let mut b = bridge.cached_buffer.take(); | ||
237 | |||
238 | // b.clear(); | ||
239 | // api_tags::Method::$name(api_tags::$name::$method).encode(&mut b, &mut ()); | ||
240 | // reverse_encode!(b; $($arg),*); | ||
241 | |||
242 | // b = bridge.dispatch.call(b); | ||
243 | |||
244 | // let r = Result::<_, PanicMessage>::decode(&mut &b[..], &mut ()); | ||
245 | |||
246 | // bridge.cached_buffer = b; | ||
247 | |||
248 | // r.unwrap_or_else(|e| panic::resume_unwind(e.into())) | ||
249 | // }) | ||
250 | })* | ||
251 | })* | ||
252 | } | ||
253 | } | ||
254 | with_api!(self, self, define_client_side); | ||
255 | |||
256 | enum BridgeState<'a> { | ||
257 | /// No server is currently connected to this client. | ||
258 | NotConnected, | ||
259 | |||
260 | /// A server is connected and available for requests. | ||
261 | Connected(Bridge<'a>), | ||
262 | |||
263 | /// Access to the bridge is being exclusively acquired | ||
264 | /// (e.g., during `BridgeState::with`). | ||
265 | InUse, | ||
266 | } | ||
267 | |||
268 | enum BridgeStateL {} | ||
269 | |||
270 | impl<'a> scoped_cell::ApplyL<'a> for BridgeStateL { | ||
271 | type Out = BridgeState<'a>; | ||
272 | } | ||
273 | |||
274 | thread_local! { | ||
275 | static BRIDGE_STATE: scoped_cell::ScopedCell<BridgeStateL> = | ||
276 | scoped_cell::ScopedCell::new(BridgeState::NotConnected); | ||
277 | } | ||
278 | |||
279 | impl BridgeState<'_> { | ||
280 | /// Take exclusive control of the thread-local | ||
281 | /// `BridgeState`, and pass it to `f`, mutably. | ||
282 | /// The state will be restored after `f` exits, even | ||
283 | /// by panic, including modifications made to it by `f`. | ||
284 | /// | ||
285 | /// N.B., while `f` is running, the thread-local state | ||
286 | /// is `BridgeState::InUse`. | ||
287 | fn with<R>(f: impl FnOnce(&mut BridgeState<'_>) -> R) -> R { | ||
288 | BRIDGE_STATE.with(|state| { | ||
289 | state.replace(BridgeState::InUse, |mut state| { | ||
290 | // FIXME(#52812) pass `f` directly to `replace` when `RefMutL` is gone | ||
291 | f(&mut *state) | ||
292 | }) | ||
293 | }) | ||
294 | } | ||
295 | } | ||
296 | |||
297 | impl Bridge<'_> { | ||
298 | fn enter<R>(self, f: impl FnOnce() -> R) -> R { | ||
299 | // Hide the default panic output within `proc_macro` expansions. | ||
300 | // NB. the server can't do this because it may use a different libstd. | ||
301 | static HIDE_PANICS_DURING_EXPANSION: Once = Once::new(); | ||
302 | HIDE_PANICS_DURING_EXPANSION.call_once(|| { | ||
303 | let prev = panic::take_hook(); | ||
304 | panic::set_hook(Box::new(move |info| { | ||
305 | let hide = BridgeState::with(|state| match state { | ||
306 | BridgeState::NotConnected => false, | ||
307 | BridgeState::Connected(_) | BridgeState::InUse => true, | ||
308 | }); | ||
309 | if !hide { | ||
310 | prev(info) | ||
311 | } | ||
312 | })); | ||
313 | }); | ||
314 | |||
315 | BRIDGE_STATE.with(|state| state.set(BridgeState::Connected(self), f)) | ||
316 | } | ||
317 | |||
318 | fn with<R>(f: impl FnOnce(&mut Bridge<'_>) -> R) -> R { | ||
319 | BridgeState::with(|state| match state { | ||
320 | BridgeState::NotConnected => { | ||
321 | panic!("procedural macro API is used outside of a procedural macro"); | ||
322 | } | ||
323 | BridgeState::InUse => { | ||
324 | panic!("procedural macro API is used while it's already in use"); | ||
325 | } | ||
326 | BridgeState::Connected(bridge) => f(bridge), | ||
327 | }) | ||
328 | } | ||
329 | } | ||
330 | |||
331 | /// A client-side "global object" (usually a function pointer), | ||
332 | /// which may be using a different `proc_macro` from the one | ||
333 | /// used by the server, but can be interacted with compatibly. | ||
334 | /// | ||
335 | /// N.B., `F` must have FFI-friendly memory layout (e.g., a pointer). | ||
336 | /// The call ABI of function pointers used for `F` doesn't | ||
337 | /// need to match between server and client, since it's only | ||
338 | /// passed between them and (eventually) called by the client. | ||
339 | #[repr(C)] | ||
340 | #[derive(Copy, Clone)] | ||
341 | pub struct Client<F> { | ||
342 | // FIXME(eddyb) use a reference to the `static COUNTERS`, intead of | ||
343 | // a wrapper `fn` pointer, once `const fn` can reference `static`s. | ||
344 | pub(super) get_handle_counters: extern "C" fn() -> &'static HandleCounters, | ||
345 | pub(super) run: extern "C" fn(Bridge<'_>, F) -> Buffer<u8>, | ||
346 | pub(super) f: F, | ||
347 | } | ||
348 | |||
349 | /// Client-side helper for handling client panics, entering the bridge, | ||
350 | /// deserializing input and serializing output. | ||
351 | // FIXME(eddyb) maybe replace `Bridge::enter` with this? | ||
352 | fn run_client<A: for<'a, 's> DecodeMut<'a, 's, ()>, R: Encode<()>>( | ||
353 | mut bridge: Bridge<'_>, | ||
354 | f: impl FnOnce(A) -> R, | ||
355 | ) -> Buffer<u8> { | ||
356 | // The initial `cached_buffer` contains the input. | ||
357 | let mut b = bridge.cached_buffer.take(); | ||
358 | |||
359 | panic::catch_unwind(panic::AssertUnwindSafe(|| { | ||
360 | bridge.enter(|| { | ||
361 | let reader = &mut &b[..]; | ||
362 | let input = A::decode(reader, &mut ()); | ||
363 | |||
364 | // Put the `cached_buffer` back in the `Bridge`, for requests. | ||
365 | Bridge::with(|bridge| bridge.cached_buffer = b.take()); | ||
366 | |||
367 | let output = f(input); | ||
368 | |||
369 | // Take the `cached_buffer` back out, for the output value. | ||
370 | b = Bridge::with(|bridge| bridge.cached_buffer.take()); | ||
371 | |||
372 | // HACK(eddyb) Separate encoding a success value (`Ok(output)`) | ||
373 | // from encoding a panic (`Err(e: PanicMessage)`) to avoid | ||
374 | // having handles outside the `bridge.enter(|| ...)` scope, and | ||
375 | // to catch panics that could happen while encoding the success. | ||
376 | // | ||
377 | // Note that panics should be impossible beyond this point, but | ||
378 | // this is defensively trying to avoid any accidental panicking | ||
379 | // reaching the `extern "C"` (which should `abort` but may not | ||
380 | // at the moment, so this is also potentially preventing UB). | ||
381 | b.clear(); | ||
382 | Ok::<_, ()>(output).encode(&mut b, &mut ()); | ||
383 | }) | ||
384 | })) | ||
385 | .map_err(PanicMessage::from) | ||
386 | .unwrap_or_else(|e| { | ||
387 | b.clear(); | ||
388 | Err::<(), _>(e).encode(&mut b, &mut ()); | ||
389 | }); | ||
390 | b | ||
391 | } | ||
392 | |||
393 | impl Client<fn(crate::TokenStream) -> crate::TokenStream> { | ||
394 | pub fn expand1(f: fn(crate::TokenStream) -> crate::TokenStream) -> Self { | ||
395 | extern "C" fn run( | ||
396 | bridge: Bridge<'_>, | ||
397 | f: impl FnOnce(crate::TokenStream) -> crate::TokenStream, | ||
398 | ) -> Buffer<u8> { | ||
399 | run_client(bridge, |input| f(crate::TokenStream(input)).0) | ||
400 | } | ||
401 | Client { get_handle_counters: HandleCounters::get, run, f } | ||
402 | } | ||
403 | } | ||
404 | |||
405 | impl Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> { | ||
406 | pub fn expand2(f: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream) -> Self { | ||
407 | extern "C" fn run( | ||
408 | bridge: Bridge<'_>, | ||
409 | f: impl FnOnce(crate::TokenStream, crate::TokenStream) -> crate::TokenStream, | ||
410 | ) -> Buffer<u8> { | ||
411 | run_client(bridge, |(input, input2)| { | ||
412 | f(crate::TokenStream(input), crate::TokenStream(input2)).0 | ||
413 | }) | ||
414 | } | ||
415 | Client { get_handle_counters: HandleCounters::get, run, f } | ||
416 | } | ||
417 | } | ||
418 | |||
419 | #[repr(C)] | ||
420 | #[derive(Copy, Clone)] | ||
421 | pub enum ProcMacro { | ||
422 | CustomDerive { | ||
423 | trait_name: &'static str, | ||
424 | attributes: &'static [&'static str], | ||
425 | client: Client<fn(crate::TokenStream) -> crate::TokenStream>, | ||
426 | }, | ||
427 | |||
428 | Attr { | ||
429 | name: &'static str, | ||
430 | client: Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream>, | ||
431 | }, | ||
432 | |||
433 | Bang { | ||
434 | name: &'static str, | ||
435 | client: Client<fn(crate::TokenStream) -> crate::TokenStream>, | ||
436 | }, | ||
437 | } | ||
438 | |||
439 | impl std::fmt::Debug for ProcMacro { | ||
440 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
441 | write!(f, "ProcMacro {{ name: {} }}", self.name()) | ||
442 | } | ||
443 | } | ||
444 | |||
445 | impl ProcMacro { | ||
446 | pub fn name(&self) -> &'static str { | ||
447 | match self { | ||
448 | ProcMacro::CustomDerive { trait_name, .. } => trait_name, | ||
449 | ProcMacro::Attr { name, .. } => name, | ||
450 | ProcMacro::Bang { name, .. } => name, | ||
451 | } | ||
452 | } | ||
453 | |||
454 | pub fn custom_derive( | ||
455 | trait_name: &'static str, | ||
456 | attributes: &'static [&'static str], | ||
457 | expand: fn(crate::TokenStream) -> crate::TokenStream, | ||
458 | ) -> Self { | ||
459 | ProcMacro::CustomDerive { trait_name, attributes, client: Client::expand1(expand) } | ||
460 | } | ||
461 | |||
462 | pub fn attr( | ||
463 | name: &'static str, | ||
464 | expand: fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream, | ||
465 | ) -> Self { | ||
466 | ProcMacro::Attr { name, client: Client::expand2(expand) } | ||
467 | } | ||
468 | |||
469 | pub fn bang(name: &'static str, expand: fn(crate::TokenStream) -> crate::TokenStream) -> Self { | ||
470 | ProcMacro::Bang { name, client: Client::expand1(expand) } | ||
471 | } | ||
472 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs new file mode 100644 index 000000000..b8addff4a --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/closure.rs | |||
@@ -0,0 +1,27 @@ | |||
1 | //! lib-proc-macro Closure type (equivalent to `&mut dyn FnMut(A) -> R`) that's `repr(C)`. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/closure.rs# | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | #[repr(C)] | ||
7 | pub struct Closure<'a, A, R> { | ||
8 | call: unsafe extern "C" fn(&mut Env, A) -> R, | ||
9 | env: &'a mut Env, | ||
10 | } | ||
11 | |||
12 | struct Env; | ||
13 | |||
14 | impl<'a, A, R, F: FnMut(A) -> R> From<&'a mut F> for Closure<'a, A, R> { | ||
15 | fn from(f: &'a mut F) -> Self { | ||
16 | unsafe extern "C" fn call<A, R, F: FnMut(A) -> R>(env: &mut Env, arg: A) -> R { | ||
17 | (*(env as *mut _ as *mut F))(arg) | ||
18 | } | ||
19 | Closure { call: call::<A, R, F>, env: unsafe { &mut *(f as *mut _ as *mut Env) } } | ||
20 | } | ||
21 | } | ||
22 | |||
23 | impl<'a, A, R> Closure<'a, A, R> { | ||
24 | pub fn call(&mut self, arg: A) -> R { | ||
25 | unsafe { (self.call)(self.env, arg) } | ||
26 | } | ||
27 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/handle.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/handle.rs new file mode 100644 index 000000000..a2f77b5ac --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/handle.rs | |||
@@ -0,0 +1,73 @@ | |||
1 | //! lib-proc-macro Server-side handles and storage for per-handle data. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/handle.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use std::collections::{BTreeMap, HashMap}; | ||
7 | use std::hash::Hash; | ||
8 | use std::num::NonZeroU32; | ||
9 | use std::ops::{Index, IndexMut}; | ||
10 | use std::sync::atomic::{AtomicUsize, Ordering}; | ||
11 | |||
12 | pub(super) type Handle = NonZeroU32; | ||
13 | |||
14 | pub(super) struct OwnedStore<T: 'static> { | ||
15 | counter: &'static AtomicUsize, | ||
16 | data: BTreeMap<Handle, T>, | ||
17 | } | ||
18 | |||
19 | impl<T> OwnedStore<T> { | ||
20 | pub(super) fn new(counter: &'static AtomicUsize) -> Self { | ||
21 | // Ensure the handle counter isn't 0, which would panic later, | ||
22 | // when `NonZeroU32::new` (aka `Handle::new`) is called in `alloc`. | ||
23 | assert_ne!(counter.load(Ordering::SeqCst), 0); | ||
24 | |||
25 | OwnedStore { counter, data: BTreeMap::new() } | ||
26 | } | ||
27 | } | ||
28 | |||
29 | impl<T> OwnedStore<T> { | ||
30 | pub(super) fn alloc(&mut self, x: T) -> Handle { | ||
31 | let counter = self.counter.fetch_add(1, Ordering::SeqCst); | ||
32 | let handle = Handle::new(counter as u32).expect("`proc_macro` handle counter overflowed"); | ||
33 | assert!(self.data.insert(handle, x).is_none()); | ||
34 | handle | ||
35 | } | ||
36 | |||
37 | pub(super) fn take(&mut self, h: Handle) -> T { | ||
38 | self.data.remove(&h).expect("use-after-free in `proc_macro` handle") | ||
39 | } | ||
40 | } | ||
41 | |||
42 | impl<T> Index<Handle> for OwnedStore<T> { | ||
43 | type Output = T; | ||
44 | fn index(&self, h: Handle) -> &T { | ||
45 | self.data.get(&h).expect("use-after-free in `proc_macro` handle") | ||
46 | } | ||
47 | } | ||
48 | |||
49 | impl<T> IndexMut<Handle> for OwnedStore<T> { | ||
50 | fn index_mut(&mut self, h: Handle) -> &mut T { | ||
51 | self.data.get_mut(&h).expect("use-after-free in `proc_macro` handle") | ||
52 | } | ||
53 | } | ||
54 | |||
55 | pub(super) struct InternedStore<T: 'static> { | ||
56 | owned: OwnedStore<T>, | ||
57 | interner: HashMap<T, Handle>, | ||
58 | } | ||
59 | |||
60 | impl<T: Copy + Eq + Hash> InternedStore<T> { | ||
61 | pub(super) fn new(counter: &'static AtomicUsize) -> Self { | ||
62 | InternedStore { owned: OwnedStore::new(counter), interner: HashMap::new() } | ||
63 | } | ||
64 | |||
65 | pub(super) fn alloc(&mut self, x: T) -> Handle { | ||
66 | let owned = &mut self.owned; | ||
67 | *self.interner.entry(x).or_insert_with(|| owned.alloc(x)) | ||
68 | } | ||
69 | |||
70 | pub(super) fn copy(&mut self, h: Handle) -> T { | ||
71 | self.owned[h] | ||
72 | } | ||
73 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/mod.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/mod.rs new file mode 100644 index 000000000..6ae3926b2 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/mod.rs | |||
@@ -0,0 +1,404 @@ | |||
1 | //! lib-proc-macro Internal interface for communicating between a `proc_macro` client | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/mod.rs | ||
4 | //! augmented with removing unstable features | ||
5 | //! | ||
6 | //! Internal interface for communicating between a `proc_macro` client | ||
7 | //! (a proc macro crate) and a `proc_macro` server (a compiler front-end). | ||
8 | //! | ||
9 | //! Serialization (with C ABI buffers) and unique integer handles are employed | ||
10 | //! to allow safely interfacing between two copies of `proc_macro` built | ||
11 | //! (from the same source) by different compilers with potentially mismatching | ||
12 | //! Rust ABIs (e.g., stage0/bin/rustc vs stage1/bin/rustc during bootstrap). | ||
13 | |||
14 | #![deny(unsafe_code)] | ||
15 | |||
16 | pub use crate::proc_macro::{Delimiter, Level, LineColumn, Spacing}; | ||
17 | use std::fmt; | ||
18 | use std::hash::Hash; | ||
19 | use std::marker; | ||
20 | use std::mem; | ||
21 | use std::ops::Bound; | ||
22 | use std::panic; | ||
23 | use std::sync::atomic::AtomicUsize; | ||
24 | use std::sync::Once; | ||
25 | use std::thread; | ||
26 | |||
27 | /// Higher-order macro describing the server RPC API, allowing automatic | ||
28 | /// generation of type-safe Rust APIs, both client-side and server-side. | ||
29 | /// | ||
30 | /// `with_api!(MySelf, my_self, my_macro)` expands to: | ||
31 | /// ```rust,ignore (pseudo-code) | ||
32 | /// my_macro! { | ||
33 | /// // ... | ||
34 | /// Literal { | ||
35 | /// // ... | ||
36 | /// fn character(ch: char) -> MySelf::Literal; | ||
37 | /// // ... | ||
38 | /// fn span(my_self: &MySelf::Literal) -> MySelf::Span; | ||
39 | /// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span); | ||
40 | /// }, | ||
41 | /// // ... | ||
42 | /// } | ||
43 | /// ``` | ||
44 | /// | ||
45 | /// The first two arguments serve to customize the arguments names | ||
46 | /// and argument/return types, to enable several different usecases: | ||
47 | /// | ||
48 | /// If `my_self` is just `self`, then each `fn` signature can be used | ||
49 | /// as-is for a method. If it's anything else (`self_` in practice), | ||
50 | /// then the signatures don't have a special `self` argument, and | ||
51 | /// can, therefore, have a different one introduced. | ||
52 | /// | ||
53 | /// If `MySelf` is just `Self`, then the types are only valid inside | ||
54 | /// a trait or a trait impl, where the trait has associated types | ||
55 | /// for each of the API types. If non-associated types are desired, | ||
56 | /// a module name (`self` in practice) can be used instead of `Self`. | ||
57 | macro_rules! with_api { | ||
58 | ($S:ident, $self:ident, $m:ident) => { | ||
59 | $m! { | ||
60 | TokenStream { | ||
61 | fn drop($self: $S::TokenStream); | ||
62 | fn clone($self: &$S::TokenStream) -> $S::TokenStream; | ||
63 | fn new() -> $S::TokenStream; | ||
64 | fn is_empty($self: &$S::TokenStream) -> bool; | ||
65 | fn from_str(src: &str) -> $S::TokenStream; | ||
66 | fn to_string($self: &$S::TokenStream) -> String; | ||
67 | fn from_token_tree( | ||
68 | tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>, | ||
69 | ) -> $S::TokenStream; | ||
70 | fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter; | ||
71 | }, | ||
72 | TokenStreamBuilder { | ||
73 | fn drop($self: $S::TokenStreamBuilder); | ||
74 | fn new() -> $S::TokenStreamBuilder; | ||
75 | fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream); | ||
76 | fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream; | ||
77 | }, | ||
78 | TokenStreamIter { | ||
79 | fn drop($self: $S::TokenStreamIter); | ||
80 | fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter; | ||
81 | fn next( | ||
82 | $self: &mut $S::TokenStreamIter, | ||
83 | ) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>; | ||
84 | }, | ||
85 | Group { | ||
86 | fn drop($self: $S::Group); | ||
87 | fn clone($self: &$S::Group) -> $S::Group; | ||
88 | fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group; | ||
89 | fn delimiter($self: &$S::Group) -> Delimiter; | ||
90 | fn stream($self: &$S::Group) -> $S::TokenStream; | ||
91 | fn span($self: &$S::Group) -> $S::Span; | ||
92 | fn span_open($self: &$S::Group) -> $S::Span; | ||
93 | fn span_close($self: &$S::Group) -> $S::Span; | ||
94 | fn set_span($self: &mut $S::Group, span: $S::Span); | ||
95 | }, | ||
96 | Punct { | ||
97 | fn new(ch: char, spacing: Spacing) -> $S::Punct; | ||
98 | fn as_char($self: $S::Punct) -> char; | ||
99 | fn spacing($self: $S::Punct) -> Spacing; | ||
100 | fn span($self: $S::Punct) -> $S::Span; | ||
101 | fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct; | ||
102 | }, | ||
103 | Ident { | ||
104 | fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident; | ||
105 | fn span($self: $S::Ident) -> $S::Span; | ||
106 | fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident; | ||
107 | }, | ||
108 | Literal { | ||
109 | fn drop($self: $S::Literal); | ||
110 | fn clone($self: &$S::Literal) -> $S::Literal; | ||
111 | // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. | ||
112 | fn debug($self: &$S::Literal) -> String; | ||
113 | fn integer(n: &str) -> $S::Literal; | ||
114 | fn typed_integer(n: &str, kind: &str) -> $S::Literal; | ||
115 | fn float(n: &str) -> $S::Literal; | ||
116 | fn f32(n: &str) -> $S::Literal; | ||
117 | fn f64(n: &str) -> $S::Literal; | ||
118 | fn string(string: &str) -> $S::Literal; | ||
119 | fn character(ch: char) -> $S::Literal; | ||
120 | fn byte_string(bytes: &[u8]) -> $S::Literal; | ||
121 | fn span($self: &$S::Literal) -> $S::Span; | ||
122 | fn set_span($self: &mut $S::Literal, span: $S::Span); | ||
123 | fn subspan( | ||
124 | $self: &$S::Literal, | ||
125 | start: Bound<usize>, | ||
126 | end: Bound<usize>, | ||
127 | ) -> Option<$S::Span>; | ||
128 | }, | ||
129 | SourceFile { | ||
130 | fn drop($self: $S::SourceFile); | ||
131 | fn clone($self: &$S::SourceFile) -> $S::SourceFile; | ||
132 | fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool; | ||
133 | fn path($self: &$S::SourceFile) -> String; | ||
134 | fn is_real($self: &$S::SourceFile) -> bool; | ||
135 | }, | ||
136 | MultiSpan { | ||
137 | fn drop($self: $S::MultiSpan); | ||
138 | fn new() -> $S::MultiSpan; | ||
139 | fn push($self: &mut $S::MultiSpan, span: $S::Span); | ||
140 | }, | ||
141 | Diagnostic { | ||
142 | fn drop($self: $S::Diagnostic); | ||
143 | fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic; | ||
144 | fn sub( | ||
145 | $self: &mut $S::Diagnostic, | ||
146 | level: Level, | ||
147 | msg: &str, | ||
148 | span: $S::MultiSpan, | ||
149 | ); | ||
150 | fn emit($self: $S::Diagnostic); | ||
151 | }, | ||
152 | Span { | ||
153 | fn debug($self: $S::Span) -> String; | ||
154 | fn def_site() -> $S::Span; | ||
155 | fn call_site() -> $S::Span; | ||
156 | fn mixed_site() -> $S::Span; | ||
157 | fn source_file($self: $S::Span) -> $S::SourceFile; | ||
158 | fn parent($self: $S::Span) -> Option<$S::Span>; | ||
159 | fn source($self: $S::Span) -> $S::Span; | ||
160 | fn start($self: $S::Span) -> LineColumn; | ||
161 | fn end($self: $S::Span) -> LineColumn; | ||
162 | fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; | ||
163 | fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; | ||
164 | fn source_text($self: $S::Span) -> Option<String>; | ||
165 | }, | ||
166 | } | ||
167 | }; | ||
168 | } | ||
169 | |||
170 | // FIXME(eddyb) this calls `encode` for each argument, but in reverse, | ||
171 | // to avoid borrow conflicts from borrows started by `&mut` arguments. | ||
172 | macro_rules! reverse_encode { | ||
173 | ($writer:ident;) => {}; | ||
174 | ($writer:ident; $first:ident $(, $rest:ident)*) => { | ||
175 | reverse_encode!($writer; $($rest),*); | ||
176 | $first.encode(&mut $writer, &mut ()); | ||
177 | } | ||
178 | } | ||
179 | |||
180 | // FIXME(eddyb) this calls `decode` for each argument, but in reverse, | ||
181 | // to avoid borrow conflicts from borrows started by `&mut` arguments. | ||
182 | macro_rules! reverse_decode { | ||
183 | ($reader:ident, $s:ident;) => {}; | ||
184 | ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => { | ||
185 | reverse_decode!($reader, $s; $($rest: $rest_ty),*); | ||
186 | let $first = <$first_ty>::decode(&mut $reader, $s); | ||
187 | } | ||
188 | } | ||
189 | |||
190 | #[allow(unsafe_code)] | ||
191 | mod buffer; | ||
192 | #[forbid(unsafe_code)] | ||
193 | pub mod client; | ||
194 | #[allow(unsafe_code)] | ||
195 | mod closure; | ||
196 | #[forbid(unsafe_code)] | ||
197 | mod handle; | ||
198 | #[macro_use] | ||
199 | #[forbid(unsafe_code)] | ||
200 | mod rpc; | ||
201 | #[allow(unsafe_code)] | ||
202 | mod scoped_cell; | ||
203 | #[forbid(unsafe_code)] | ||
204 | pub mod server; | ||
205 | |||
206 | use buffer::Buffer; | ||
207 | pub use rpc::PanicMessage; | ||
208 | use rpc::{Decode, DecodeMut, Encode, Reader, Writer}; | ||
209 | |||
210 | /// An active connection between a server and a client. | ||
211 | /// The server creates the bridge (`Bridge::run_server` in `server.rs`), | ||
212 | /// then passes it to the client through the function pointer in the `run` | ||
213 | /// field of `client::Client`. The client holds its copy of the `Bridge` | ||
214 | /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`). | ||
215 | #[repr(C)] | ||
216 | pub struct Bridge<'a> { | ||
217 | /// Reusable buffer (only `clear`-ed, never shrunk), primarily | ||
218 | /// used for making requests, but also for passing input to client. | ||
219 | cached_buffer: Buffer<u8>, | ||
220 | |||
221 | /// Server-side function that the client uses to make requests. | ||
222 | dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>, | ||
223 | } | ||
224 | |||
225 | #[forbid(unsafe_code)] | ||
226 | #[allow(non_camel_case_types)] | ||
227 | mod api_tags { | ||
228 | use super::rpc::{DecodeMut, Encode, Reader, Writer}; | ||
229 | |||
230 | macro_rules! declare_tags { | ||
231 | ($($name:ident { | ||
232 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* | ||
233 | }),* $(,)?) => { | ||
234 | $( | ||
235 | pub(super) enum $name { | ||
236 | $($method),* | ||
237 | } | ||
238 | rpc_encode_decode!(enum $name { $($method),* }); | ||
239 | )* | ||
240 | |||
241 | |||
242 | pub(super) enum Method { | ||
243 | $($name($name)),* | ||
244 | } | ||
245 | rpc_encode_decode!(enum Method { $($name(m)),* }); | ||
246 | } | ||
247 | } | ||
248 | with_api!(self, self, declare_tags); | ||
249 | } | ||
250 | |||
251 | /// Helper to wrap associated types to allow trait impl dispatch. | ||
252 | /// That is, normally a pair of impls for `T::Foo` and `T::Bar` | ||
253 | /// can overlap, but if the impls are, instead, on types like | ||
254 | /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't. | ||
255 | trait Mark { | ||
256 | type Unmarked; | ||
257 | fn mark(unmarked: Self::Unmarked) -> Self; | ||
258 | } | ||
259 | |||
260 | /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details). | ||
261 | trait Unmark { | ||
262 | type Unmarked; | ||
263 | fn unmark(self) -> Self::Unmarked; | ||
264 | } | ||
265 | |||
266 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | ||
267 | struct Marked<T, M> { | ||
268 | value: T, | ||
269 | _marker: marker::PhantomData<M>, | ||
270 | } | ||
271 | |||
272 | impl<T, M> Mark for Marked<T, M> { | ||
273 | type Unmarked = T; | ||
274 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
275 | Marked { value: unmarked, _marker: marker::PhantomData } | ||
276 | } | ||
277 | } | ||
278 | impl<T, M> Unmark for Marked<T, M> { | ||
279 | type Unmarked = T; | ||
280 | fn unmark(self) -> Self::Unmarked { | ||
281 | self.value | ||
282 | } | ||
283 | } | ||
284 | impl<'a, T, M> Unmark for &'a Marked<T, M> { | ||
285 | type Unmarked = &'a T; | ||
286 | fn unmark(self) -> Self::Unmarked { | ||
287 | &self.value | ||
288 | } | ||
289 | } | ||
290 | impl<'a, T, M> Unmark for &'a mut Marked<T, M> { | ||
291 | type Unmarked = &'a mut T; | ||
292 | fn unmark(self) -> Self::Unmarked { | ||
293 | &mut self.value | ||
294 | } | ||
295 | } | ||
296 | |||
297 | impl<T: Mark> Mark for Option<T> { | ||
298 | type Unmarked = Option<T::Unmarked>; | ||
299 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
300 | unmarked.map(T::mark) | ||
301 | } | ||
302 | } | ||
303 | impl<T: Unmark> Unmark for Option<T> { | ||
304 | type Unmarked = Option<T::Unmarked>; | ||
305 | fn unmark(self) -> Self::Unmarked { | ||
306 | self.map(T::unmark) | ||
307 | } | ||
308 | } | ||
309 | |||
310 | macro_rules! mark_noop { | ||
311 | ($($ty:ty),* $(,)?) => { | ||
312 | $( | ||
313 | impl Mark for $ty { | ||
314 | type Unmarked = Self; | ||
315 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
316 | unmarked | ||
317 | } | ||
318 | } | ||
319 | impl Unmark for $ty { | ||
320 | type Unmarked = Self; | ||
321 | fn unmark(self) -> Self::Unmarked { | ||
322 | self | ||
323 | } | ||
324 | } | ||
325 | )* | ||
326 | } | ||
327 | } | ||
328 | mark_noop! { | ||
329 | (), | ||
330 | bool, | ||
331 | char, | ||
332 | &'_ [u8], | ||
333 | &'_ str, | ||
334 | String, | ||
335 | Delimiter, | ||
336 | Level, | ||
337 | LineColumn, | ||
338 | Spacing, | ||
339 | Bound<usize>, | ||
340 | } | ||
341 | |||
342 | rpc_encode_decode!( | ||
343 | enum Delimiter { | ||
344 | Parenthesis, | ||
345 | Brace, | ||
346 | Bracket, | ||
347 | None, | ||
348 | } | ||
349 | ); | ||
350 | rpc_encode_decode!( | ||
351 | enum Level { | ||
352 | Error, | ||
353 | Warning, | ||
354 | Note, | ||
355 | Help, | ||
356 | } | ||
357 | ); | ||
358 | rpc_encode_decode!(struct LineColumn { line, column }); | ||
359 | rpc_encode_decode!( | ||
360 | enum Spacing { | ||
361 | Alone, | ||
362 | Joint, | ||
363 | } | ||
364 | ); | ||
365 | |||
366 | #[derive(Clone)] | ||
367 | pub enum TokenTree<G, P, I, L> { | ||
368 | Group(G), | ||
369 | Punct(P), | ||
370 | Ident(I), | ||
371 | Literal(L), | ||
372 | } | ||
373 | |||
374 | impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> { | ||
375 | type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>; | ||
376 | fn mark(unmarked: Self::Unmarked) -> Self { | ||
377 | match unmarked { | ||
378 | TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)), | ||
379 | TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)), | ||
380 | TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)), | ||
381 | TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)), | ||
382 | } | ||
383 | } | ||
384 | } | ||
385 | impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> { | ||
386 | type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>; | ||
387 | fn unmark(self) -> Self::Unmarked { | ||
388 | match self { | ||
389 | TokenTree::Group(tt) => TokenTree::Group(tt.unmark()), | ||
390 | TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()), | ||
391 | TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()), | ||
392 | TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()), | ||
393 | } | ||
394 | } | ||
395 | } | ||
396 | |||
397 | rpc_encode_decode!( | ||
398 | enum TokenTree<G, P, I, L> { | ||
399 | Group(tt), | ||
400 | Punct(tt), | ||
401 | Ident(tt), | ||
402 | Literal(tt), | ||
403 | } | ||
404 | ); | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/rpc.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/rpc.rs new file mode 100644 index 000000000..3528d5c99 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/rpc.rs | |||
@@ -0,0 +1,311 @@ | |||
1 | //! lib-proc-macro Serialization for client-server communication. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/rpc.rs | ||
4 | //! augmented with removing unstable features | ||
5 | //! | ||
6 | //! Serialization for client-server communication. | ||
7 | |||
8 | use std::any::Any; | ||
9 | use std::char; | ||
10 | use std::io::Write; | ||
11 | use std::num::NonZeroU32; | ||
12 | use std::ops::Bound; | ||
13 | use std::str; | ||
14 | |||
15 | pub(super) type Writer = super::buffer::Buffer<u8>; | ||
16 | |||
17 | pub(super) trait Encode<S>: Sized { | ||
18 | fn encode(self, w: &mut Writer, s: &mut S); | ||
19 | } | ||
20 | |||
21 | pub(super) type Reader<'a> = &'a [u8]; | ||
22 | |||
23 | pub(super) trait Decode<'a, 's, S>: Sized { | ||
24 | fn decode(r: &mut Reader<'a>, s: &'s S) -> Self; | ||
25 | } | ||
26 | |||
27 | pub(super) trait DecodeMut<'a, 's, S>: Sized { | ||
28 | fn decode(r: &mut Reader<'a>, s: &'s mut S) -> Self; | ||
29 | } | ||
30 | |||
31 | macro_rules! rpc_encode_decode { | ||
32 | (le $ty:ty) => { | ||
33 | impl<S> Encode<S> for $ty { | ||
34 | fn encode(self, w: &mut Writer, _: &mut S) { | ||
35 | w.write_all(&self.to_le_bytes()).unwrap(); | ||
36 | } | ||
37 | } | ||
38 | |||
39 | impl<S> DecodeMut<'_, '_, S> for $ty { | ||
40 | fn decode(r: &mut Reader<'_>, _: &mut S) -> Self { | ||
41 | const N: usize = ::std::mem::size_of::<$ty>(); | ||
42 | |||
43 | let mut bytes = [0; N]; | ||
44 | bytes.copy_from_slice(&r[..N]); | ||
45 | *r = &r[N..]; | ||
46 | |||
47 | Self::from_le_bytes(bytes) | ||
48 | } | ||
49 | } | ||
50 | }; | ||
51 | (struct $name:ident { $($field:ident),* $(,)? }) => { | ||
52 | impl<S> Encode<S> for $name { | ||
53 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
54 | $(self.$field.encode(w, s);)* | ||
55 | } | ||
56 | } | ||
57 | |||
58 | impl<S> DecodeMut<'_, '_, S> for $name { | ||
59 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
60 | $name { | ||
61 | $($field: DecodeMut::decode(r, s)),* | ||
62 | } | ||
63 | } | ||
64 | } | ||
65 | }; | ||
66 | (enum $name:ident $(<$($T:ident),+>)? { $($variant:ident $(($field:ident))*),* $(,)? }) => { | ||
67 | impl<S, $($($T: Encode<S>),+)?> Encode<S> for $name $(<$($T),+>)? { | ||
68 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
69 | // HACK(eddyb): `Tag` enum duplicated between the | ||
70 | // two impls as there's no other place to stash it. | ||
71 | #[allow(non_upper_case_globals)] | ||
72 | mod tag { | ||
73 | #[repr(u8)] enum Tag { $($variant),* } | ||
74 | |||
75 | $(pub const $variant: u8 = Tag::$variant as u8;)* | ||
76 | } | ||
77 | |||
78 | match self { | ||
79 | $($name::$variant $(($field))* => { | ||
80 | tag::$variant.encode(w, s); | ||
81 | $($field.encode(w, s);)* | ||
82 | })* | ||
83 | } | ||
84 | } | ||
85 | } | ||
86 | |||
87 | impl<'a, S, $($($T: for<'s> DecodeMut<'a, 's, S>),+)?> DecodeMut<'a, '_, S> | ||
88 | for $name $(<$($T),+>)? | ||
89 | { | ||
90 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
91 | // HACK(eddyb): `Tag` enum duplicated between the | ||
92 | // two impls as there's no other place to stash it. | ||
93 | #[allow(non_upper_case_globals)] | ||
94 | mod tag { | ||
95 | #[repr(u8)] enum Tag { $($variant),* } | ||
96 | |||
97 | $(pub const $variant: u8 = Tag::$variant as u8;)* | ||
98 | } | ||
99 | |||
100 | match u8::decode(r, s) { | ||
101 | $(tag::$variant => { | ||
102 | $(let $field = DecodeMut::decode(r, s);)* | ||
103 | $name::$variant $(($field))* | ||
104 | })* | ||
105 | _ => unreachable!(), | ||
106 | } | ||
107 | } | ||
108 | } | ||
109 | } | ||
110 | } | ||
111 | |||
112 | impl<S> Encode<S> for () { | ||
113 | fn encode(self, _: &mut Writer, _: &mut S) {} | ||
114 | } | ||
115 | |||
116 | impl<S> DecodeMut<'_, '_, S> for () { | ||
117 | fn decode(_: &mut Reader<'_>, _: &mut S) -> Self {} | ||
118 | } | ||
119 | |||
120 | impl<S> Encode<S> for u8 { | ||
121 | fn encode(self, w: &mut Writer, _: &mut S) { | ||
122 | w.write_all(&[self]).unwrap(); | ||
123 | } | ||
124 | } | ||
125 | |||
126 | impl<S> DecodeMut<'_, '_, S> for u8 { | ||
127 | fn decode(r: &mut Reader<'_>, _: &mut S) -> Self { | ||
128 | let x = r[0]; | ||
129 | *r = &r[1..]; | ||
130 | x | ||
131 | } | ||
132 | } | ||
133 | |||
134 | rpc_encode_decode!(le u32); | ||
135 | rpc_encode_decode!(le usize); | ||
136 | |||
137 | impl<S> Encode<S> for bool { | ||
138 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
139 | (self as u8).encode(w, s); | ||
140 | } | ||
141 | } | ||
142 | |||
143 | impl<S> DecodeMut<'_, '_, S> for bool { | ||
144 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
145 | match u8::decode(r, s) { | ||
146 | 0 => false, | ||
147 | 1 => true, | ||
148 | _ => unreachable!(), | ||
149 | } | ||
150 | } | ||
151 | } | ||
152 | |||
153 | impl<S> Encode<S> for char { | ||
154 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
155 | (self as u32).encode(w, s); | ||
156 | } | ||
157 | } | ||
158 | |||
159 | impl<S> DecodeMut<'_, '_, S> for char { | ||
160 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
161 | char::from_u32(u32::decode(r, s)).unwrap() | ||
162 | } | ||
163 | } | ||
164 | |||
165 | impl<S> Encode<S> for NonZeroU32 { | ||
166 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
167 | self.get().encode(w, s); | ||
168 | } | ||
169 | } | ||
170 | |||
171 | impl<S> DecodeMut<'_, '_, S> for NonZeroU32 { | ||
172 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
173 | Self::new(u32::decode(r, s)).unwrap() | ||
174 | } | ||
175 | } | ||
176 | |||
177 | impl<S, A: Encode<S>, B: Encode<S>> Encode<S> for (A, B) { | ||
178 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
179 | self.0.encode(w, s); | ||
180 | self.1.encode(w, s); | ||
181 | } | ||
182 | } | ||
183 | |||
184 | impl<'a, S, A: for<'s> DecodeMut<'a, 's, S>, B: for<'s> DecodeMut<'a, 's, S>> DecodeMut<'a, '_, S> | ||
185 | for (A, B) | ||
186 | { | ||
187 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
188 | (DecodeMut::decode(r, s), DecodeMut::decode(r, s)) | ||
189 | } | ||
190 | } | ||
191 | |||
192 | rpc_encode_decode!( | ||
193 | enum Bound<T> { | ||
194 | Included(x), | ||
195 | Excluded(x), | ||
196 | Unbounded, | ||
197 | } | ||
198 | ); | ||
199 | |||
200 | rpc_encode_decode!( | ||
201 | enum Option<T> { | ||
202 | None, | ||
203 | Some(x), | ||
204 | } | ||
205 | ); | ||
206 | |||
207 | rpc_encode_decode!( | ||
208 | enum Result<T, E> { | ||
209 | Ok(x), | ||
210 | Err(e), | ||
211 | } | ||
212 | ); | ||
213 | |||
214 | impl<S> Encode<S> for &[u8] { | ||
215 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
216 | self.len().encode(w, s); | ||
217 | w.write_all(self).unwrap(); | ||
218 | } | ||
219 | } | ||
220 | |||
221 | impl<'a, S> DecodeMut<'a, '_, S> for &'a [u8] { | ||
222 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
223 | let len = usize::decode(r, s); | ||
224 | let xs = &r[..len]; | ||
225 | *r = &r[len..]; | ||
226 | xs | ||
227 | } | ||
228 | } | ||
229 | |||
230 | impl<S> Encode<S> for &str { | ||
231 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
232 | self.as_bytes().encode(w, s); | ||
233 | } | ||
234 | } | ||
235 | |||
236 | impl<'a, S> DecodeMut<'a, '_, S> for &'a str { | ||
237 | fn decode(r: &mut Reader<'a>, s: &mut S) -> Self { | ||
238 | str::from_utf8(<&[u8]>::decode(r, s)).unwrap() | ||
239 | } | ||
240 | } | ||
241 | |||
242 | impl<S> Encode<S> for String { | ||
243 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
244 | self[..].encode(w, s); | ||
245 | } | ||
246 | } | ||
247 | |||
248 | impl<S> DecodeMut<'_, '_, S> for String { | ||
249 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
250 | <&str>::decode(r, s).to_string() | ||
251 | } | ||
252 | } | ||
253 | |||
254 | /// Simplied version of panic payloads, ignoring | ||
255 | /// types other than `&'static str` and `String`. | ||
256 | #[derive(Debug)] | ||
257 | pub enum PanicMessage { | ||
258 | StaticStr(&'static str), | ||
259 | String(String), | ||
260 | Unknown, | ||
261 | } | ||
262 | |||
263 | impl From<Box<dyn Any + Send>> for PanicMessage { | ||
264 | fn from(payload: Box<dyn Any + Send + 'static>) -> Self { | ||
265 | if let Some(s) = payload.downcast_ref::<&'static str>() { | ||
266 | return PanicMessage::StaticStr(s); | ||
267 | } | ||
268 | if let Ok(s) = payload.downcast::<String>() { | ||
269 | return PanicMessage::String(*s); | ||
270 | } | ||
271 | PanicMessage::Unknown | ||
272 | } | ||
273 | } | ||
274 | |||
275 | impl Into<Box<dyn Any + Send>> for PanicMessage { | ||
276 | fn into(self) -> Box<dyn Any + Send> { | ||
277 | match self { | ||
278 | PanicMessage::StaticStr(s) => Box::new(s), | ||
279 | PanicMessage::String(s) => Box::new(s), | ||
280 | PanicMessage::Unknown => { | ||
281 | struct UnknownPanicMessage; | ||
282 | Box::new(UnknownPanicMessage) | ||
283 | } | ||
284 | } | ||
285 | } | ||
286 | } | ||
287 | |||
288 | impl PanicMessage { | ||
289 | pub fn as_str(&self) -> Option<&str> { | ||
290 | match self { | ||
291 | PanicMessage::StaticStr(s) => Some(s), | ||
292 | PanicMessage::String(s) => Some(s), | ||
293 | PanicMessage::Unknown => None, | ||
294 | } | ||
295 | } | ||
296 | } | ||
297 | |||
298 | impl<S> Encode<S> for PanicMessage { | ||
299 | fn encode(self, w: &mut Writer, s: &mut S) { | ||
300 | self.as_str().encode(w, s); | ||
301 | } | ||
302 | } | ||
303 | |||
304 | impl<S> DecodeMut<'_, '_, S> for PanicMessage { | ||
305 | fn decode(r: &mut Reader<'_>, s: &mut S) -> Self { | ||
306 | match Option::<String>::decode(r, s) { | ||
307 | Some(s) => PanicMessage::String(s), | ||
308 | None => PanicMessage::Unknown, | ||
309 | } | ||
310 | } | ||
311 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/scoped_cell.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/scoped_cell.rs new file mode 100644 index 000000000..6ef7ea43c --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/scoped_cell.rs | |||
@@ -0,0 +1,84 @@ | |||
1 | //! lib-proc-macro `Cell` variant for (scoped) existential lifetimes. | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/scoped_cell.rs#L1 | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use std::cell::Cell; | ||
7 | use std::mem; | ||
8 | use std::ops::{Deref, DerefMut}; | ||
9 | |||
10 | /// Type lambda application, with a lifetime. | ||
11 | #[allow(unused_lifetimes)] | ||
12 | pub trait ApplyL<'a> { | ||
13 | type Out; | ||
14 | } | ||
15 | |||
16 | /// Type lambda taking a lifetime, i.e., `Lifetime -> Type`. | ||
17 | pub trait LambdaL: for<'a> ApplyL<'a> {} | ||
18 | |||
19 | impl<T: for<'a> ApplyL<'a>> LambdaL for T {} | ||
20 | |||
21 | // HACK(eddyb) work around projection limitations with a newtype | ||
22 | // FIXME(#52812) replace with `&'a mut <T as ApplyL<'b>>::Out` | ||
23 | pub struct RefMutL<'a, 'b, T: LambdaL>(&'a mut <T as ApplyL<'b>>::Out); | ||
24 | |||
25 | impl<'a, 'b, T: LambdaL> Deref for RefMutL<'a, 'b, T> { | ||
26 | type Target = <T as ApplyL<'b>>::Out; | ||
27 | fn deref(&self) -> &Self::Target { | ||
28 | self.0 | ||
29 | } | ||
30 | } | ||
31 | |||
32 | impl<'a, 'b, T: LambdaL> DerefMut for RefMutL<'a, 'b, T> { | ||
33 | fn deref_mut(&mut self) -> &mut Self::Target { | ||
34 | self.0 | ||
35 | } | ||
36 | } | ||
37 | |||
38 | pub struct ScopedCell<T: LambdaL>(Cell<<T as ApplyL<'static>>::Out>); | ||
39 | |||
40 | impl<T: LambdaL> ScopedCell<T> { | ||
41 | pub fn new(value: <T as ApplyL<'static>>::Out) -> Self { | ||
42 | ScopedCell(Cell::new(value)) | ||
43 | } | ||
44 | |||
45 | /// Sets the value in `self` to `replacement` while | ||
46 | /// running `f`, which gets the old value, mutably. | ||
47 | /// The old value will be restored after `f` exits, even | ||
48 | /// by panic, including modifications made to it by `f`. | ||
49 | pub fn replace<'a, R>( | ||
50 | &self, | ||
51 | replacement: <T as ApplyL<'a>>::Out, | ||
52 | f: impl for<'b, 'c> FnOnce(RefMutL<'b, 'c, T>) -> R, | ||
53 | ) -> R { | ||
54 | /// Wrapper that ensures that the cell always gets filled | ||
55 | /// (with the original state, optionally changed by `f`), | ||
56 | /// even if `f` had panicked. | ||
57 | struct PutBackOnDrop<'a, T: LambdaL> { | ||
58 | cell: &'a ScopedCell<T>, | ||
59 | value: Option<<T as ApplyL<'static>>::Out>, | ||
60 | } | ||
61 | |||
62 | impl<'a, T: LambdaL> Drop for PutBackOnDrop<'a, T> { | ||
63 | fn drop(&mut self) { | ||
64 | self.cell.0.set(self.value.take().unwrap()); | ||
65 | } | ||
66 | } | ||
67 | |||
68 | let mut put_back_on_drop = PutBackOnDrop { | ||
69 | cell: self, | ||
70 | value: Some(self.0.replace(unsafe { | ||
71 | let erased = mem::transmute_copy(&replacement); | ||
72 | mem::forget(replacement); | ||
73 | erased | ||
74 | })), | ||
75 | }; | ||
76 | |||
77 | f(RefMutL(put_back_on_drop.value.as_mut().unwrap())) | ||
78 | } | ||
79 | |||
80 | /// Sets the value in `self` to `value` while running `f`. | ||
81 | pub fn set<R>(&self, value: <T as ApplyL<'_>>::Out, f: impl FnOnce() -> R) -> R { | ||
82 | self.replace(value, |_| f()) | ||
83 | } | ||
84 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/bridge/server.rs b/crates/ra_proc_macro_srv/src/proc_macro/bridge/server.rs new file mode 100644 index 000000000..45d41ac02 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/bridge/server.rs | |||
@@ -0,0 +1,323 @@ | |||
1 | //! lib-proc-macro server-side traits | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/server.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use super::*; | ||
7 | |||
8 | // FIXME(eddyb) generate the definition of `HandleStore` in `server.rs`. | ||
9 | use super::client::HandleStore; | ||
10 | |||
11 | /// Declare an associated item of one of the traits below, optionally | ||
12 | /// adjusting it (i.e., adding bounds to types and default bodies to methods). | ||
13 | macro_rules! associated_item { | ||
14 | (type TokenStream) => | ||
15 | (type TokenStream: 'static + Clone;); | ||
16 | (type TokenStreamBuilder) => | ||
17 | (type TokenStreamBuilder: 'static;); | ||
18 | (type TokenStreamIter) => | ||
19 | (type TokenStreamIter: 'static + Clone;); | ||
20 | (type Group) => | ||
21 | (type Group: 'static + Clone;); | ||
22 | (type Punct) => | ||
23 | (type Punct: 'static + Copy + Eq + Hash;); | ||
24 | (type Ident) => | ||
25 | (type Ident: 'static + Copy + Eq + Hash;); | ||
26 | (type Literal) => | ||
27 | (type Literal: 'static + Clone;); | ||
28 | (type SourceFile) => | ||
29 | (type SourceFile: 'static + Clone;); | ||
30 | (type MultiSpan) => | ||
31 | (type MultiSpan: 'static;); | ||
32 | (type Diagnostic) => | ||
33 | (type Diagnostic: 'static;); | ||
34 | (type Span) => | ||
35 | (type Span: 'static + Copy + Eq + Hash;); | ||
36 | (fn drop(&mut self, $arg:ident: $arg_ty:ty)) => | ||
37 | (fn drop(&mut self, $arg: $arg_ty) { mem::drop($arg) }); | ||
38 | (fn clone(&mut self, $arg:ident: $arg_ty:ty) -> $ret_ty:ty) => | ||
39 | (fn clone(&mut self, $arg: $arg_ty) -> $ret_ty { $arg.clone() }); | ||
40 | ($($item:tt)*) => ($($item)*;) | ||
41 | } | ||
42 | |||
43 | macro_rules! declare_server_traits { | ||
44 | ($($name:ident { | ||
45 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* | ||
46 | }),* $(,)?) => { | ||
47 | pub trait Types { | ||
48 | $(associated_item!(type $name);)* | ||
49 | } | ||
50 | |||
51 | $(pub trait $name: Types { | ||
52 | $(associated_item!(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)?);)* | ||
53 | })* | ||
54 | |||
55 | pub trait Server: Types $(+ $name)* {} | ||
56 | impl<S: Types $(+ $name)*> Server for S {} | ||
57 | } | ||
58 | } | ||
59 | with_api!(Self, self_, declare_server_traits); | ||
60 | |||
61 | pub(super) struct MarkedTypes<S: Types>(S); | ||
62 | |||
63 | macro_rules! define_mark_types_impls { | ||
64 | ($($name:ident { | ||
65 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* | ||
66 | }),* $(,)?) => { | ||
67 | impl<S: Types> Types for MarkedTypes<S> { | ||
68 | $(type $name = Marked<S::$name, client::$name>;)* | ||
69 | } | ||
70 | |||
71 | $(impl<S: $name> $name for MarkedTypes<S> { | ||
72 | $(fn $method(&mut self, $($arg: $arg_ty),*) $(-> $ret_ty)? { | ||
73 | <_>::mark($name::$method(&mut self.0, $($arg.unmark()),*)) | ||
74 | })* | ||
75 | })* | ||
76 | } | ||
77 | } | ||
78 | with_api!(Self, self_, define_mark_types_impls); | ||
79 | |||
80 | struct Dispatcher<S: Types> { | ||
81 | handle_store: HandleStore<S>, | ||
82 | server: S, | ||
83 | } | ||
84 | |||
85 | macro_rules! define_dispatcher_impl { | ||
86 | ($($name:ident { | ||
87 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)?;)* | ||
88 | }),* $(,)?) => { | ||
89 | // FIXME(eddyb) `pub` only for `ExecutionStrategy` below. | ||
90 | pub trait DispatcherTrait { | ||
91 | // HACK(eddyb) these are here to allow `Self::$name` to work below. | ||
92 | $(type $name;)* | ||
93 | fn dispatch(&mut self, b: Buffer<u8>) -> Buffer<u8>; | ||
94 | } | ||
95 | |||
96 | impl<S: Server> DispatcherTrait for Dispatcher<MarkedTypes<S>> { | ||
97 | $(type $name = <MarkedTypes<S> as Types>::$name;)* | ||
98 | fn dispatch(&mut self, mut b: Buffer<u8>) -> Buffer<u8> { | ||
99 | let Dispatcher { handle_store, server } = self; | ||
100 | |||
101 | let mut reader = &b[..]; | ||
102 | match api_tags::Method::decode(&mut reader, &mut ()) { | ||
103 | $(api_tags::Method::$name(m) => match m { | ||
104 | $(api_tags::$name::$method => { | ||
105 | let mut call_method = || { | ||
106 | reverse_decode!(reader, handle_store; $($arg: $arg_ty),*); | ||
107 | $name::$method(server, $($arg),*) | ||
108 | }; | ||
109 | // HACK(eddyb) don't use `panic::catch_unwind` in a panic. | ||
110 | // If client and server happen to use the same `libstd`, | ||
111 | // `catch_unwind` asserts that the panic counter was 0, | ||
112 | // even when the closure passed to it didn't panic. | ||
113 | let r = if thread::panicking() { | ||
114 | Ok(call_method()) | ||
115 | } else { | ||
116 | panic::catch_unwind(panic::AssertUnwindSafe(call_method)) | ||
117 | .map_err(PanicMessage::from) | ||
118 | }; | ||
119 | |||
120 | b.clear(); | ||
121 | r.encode(&mut b, handle_store); | ||
122 | })* | ||
123 | }),* | ||
124 | } | ||
125 | b | ||
126 | } | ||
127 | } | ||
128 | } | ||
129 | } | ||
130 | with_api!(Self, self_, define_dispatcher_impl); | ||
131 | |||
132 | pub trait ExecutionStrategy { | ||
133 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
134 | &self, | ||
135 | dispatcher: &mut impl DispatcherTrait, | ||
136 | input: Buffer<u8>, | ||
137 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
138 | client_data: D, | ||
139 | ) -> Buffer<u8>; | ||
140 | } | ||
141 | |||
142 | pub struct SameThread; | ||
143 | |||
144 | impl ExecutionStrategy for SameThread { | ||
145 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
146 | &self, | ||
147 | dispatcher: &mut impl DispatcherTrait, | ||
148 | input: Buffer<u8>, | ||
149 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
150 | client_data: D, | ||
151 | ) -> Buffer<u8> { | ||
152 | let mut dispatch = |b| dispatcher.dispatch(b); | ||
153 | |||
154 | run_client(Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, client_data) | ||
155 | } | ||
156 | } | ||
157 | |||
158 | // NOTE(eddyb) Two implementations are provided, the second one is a bit | ||
159 | // faster but neither is anywhere near as fast as same-thread execution. | ||
160 | |||
161 | pub struct CrossThread1; | ||
162 | |||
163 | impl ExecutionStrategy for CrossThread1 { | ||
164 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
165 | &self, | ||
166 | dispatcher: &mut impl DispatcherTrait, | ||
167 | input: Buffer<u8>, | ||
168 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
169 | client_data: D, | ||
170 | ) -> Buffer<u8> { | ||
171 | use std::sync::mpsc::channel; | ||
172 | |||
173 | let (req_tx, req_rx) = channel(); | ||
174 | let (res_tx, res_rx) = channel(); | ||
175 | |||
176 | let join_handle = thread::spawn(move || { | ||
177 | let mut dispatch = |b| { | ||
178 | req_tx.send(b).unwrap(); | ||
179 | res_rx.recv().unwrap() | ||
180 | }; | ||
181 | |||
182 | run_client( | ||
183 | Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, | ||
184 | client_data, | ||
185 | ) | ||
186 | }); | ||
187 | |||
188 | for b in req_rx { | ||
189 | res_tx.send(dispatcher.dispatch(b)).unwrap(); | ||
190 | } | ||
191 | |||
192 | join_handle.join().unwrap() | ||
193 | } | ||
194 | } | ||
195 | |||
196 | pub struct CrossThread2; | ||
197 | |||
198 | impl ExecutionStrategy for CrossThread2 { | ||
199 | fn run_bridge_and_client<D: Copy + Send + 'static>( | ||
200 | &self, | ||
201 | dispatcher: &mut impl DispatcherTrait, | ||
202 | input: Buffer<u8>, | ||
203 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
204 | client_data: D, | ||
205 | ) -> Buffer<u8> { | ||
206 | use std::sync::{Arc, Mutex}; | ||
207 | |||
208 | enum State<T> { | ||
209 | Req(T), | ||
210 | Res(T), | ||
211 | } | ||
212 | |||
213 | let mut state = Arc::new(Mutex::new(State::Res(Buffer::new()))); | ||
214 | |||
215 | let server_thread = thread::current(); | ||
216 | let state2 = state.clone(); | ||
217 | let join_handle = thread::spawn(move || { | ||
218 | let mut dispatch = |b| { | ||
219 | *state2.lock().unwrap() = State::Req(b); | ||
220 | server_thread.unpark(); | ||
221 | loop { | ||
222 | thread::park(); | ||
223 | if let State::Res(b) = &mut *state2.lock().unwrap() { | ||
224 | break b.take(); | ||
225 | } | ||
226 | } | ||
227 | }; | ||
228 | |||
229 | let r = run_client( | ||
230 | Bridge { cached_buffer: input, dispatch: (&mut dispatch).into() }, | ||
231 | client_data, | ||
232 | ); | ||
233 | |||
234 | // Wake up the server so it can exit the dispatch loop. | ||
235 | drop(state2); | ||
236 | server_thread.unpark(); | ||
237 | |||
238 | r | ||
239 | }); | ||
240 | |||
241 | // Check whether `state2` was dropped, to know when to stop. | ||
242 | while Arc::get_mut(&mut state).is_none() { | ||
243 | thread::park(); | ||
244 | let mut b = match &mut *state.lock().unwrap() { | ||
245 | State::Req(b) => b.take(), | ||
246 | _ => continue, | ||
247 | }; | ||
248 | b = dispatcher.dispatch(b.take()); | ||
249 | *state.lock().unwrap() = State::Res(b); | ||
250 | join_handle.thread().unpark(); | ||
251 | } | ||
252 | |||
253 | join_handle.join().unwrap() | ||
254 | } | ||
255 | } | ||
256 | |||
257 | fn run_server< | ||
258 | S: Server, | ||
259 | I: Encode<HandleStore<MarkedTypes<S>>>, | ||
260 | O: for<'a, 's> DecodeMut<'a, 's, HandleStore<MarkedTypes<S>>>, | ||
261 | D: Copy + Send + 'static, | ||
262 | >( | ||
263 | strategy: &impl ExecutionStrategy, | ||
264 | handle_counters: &'static client::HandleCounters, | ||
265 | server: S, | ||
266 | input: I, | ||
267 | run_client: extern "C" fn(Bridge<'_>, D) -> Buffer<u8>, | ||
268 | client_data: D, | ||
269 | ) -> Result<O, PanicMessage> { | ||
270 | let mut dispatcher = | ||
271 | Dispatcher { handle_store: HandleStore::new(handle_counters), server: MarkedTypes(server) }; | ||
272 | |||
273 | let mut b = Buffer::new(); | ||
274 | input.encode(&mut b, &mut dispatcher.handle_store); | ||
275 | |||
276 | b = strategy.run_bridge_and_client(&mut dispatcher, b, run_client, client_data); | ||
277 | |||
278 | Result::decode(&mut &b[..], &mut dispatcher.handle_store) | ||
279 | } | ||
280 | |||
281 | impl client::Client<fn(crate::TokenStream) -> crate::TokenStream> { | ||
282 | pub fn run<S: Server>( | ||
283 | &self, | ||
284 | strategy: &impl ExecutionStrategy, | ||
285 | server: S, | ||
286 | input: S::TokenStream, | ||
287 | ) -> Result<S::TokenStream, PanicMessage> { | ||
288 | let client::Client { get_handle_counters, run, f } = *self; | ||
289 | run_server( | ||
290 | strategy, | ||
291 | get_handle_counters(), | ||
292 | server, | ||
293 | <MarkedTypes<S> as Types>::TokenStream::mark(input), | ||
294 | run, | ||
295 | f, | ||
296 | ) | ||
297 | .map(<MarkedTypes<S> as Types>::TokenStream::unmark) | ||
298 | } | ||
299 | } | ||
300 | |||
301 | impl client::Client<fn(crate::TokenStream, crate::TokenStream) -> crate::TokenStream> { | ||
302 | pub fn run<S: Server>( | ||
303 | &self, | ||
304 | strategy: &impl ExecutionStrategy, | ||
305 | server: S, | ||
306 | input: S::TokenStream, | ||
307 | input2: S::TokenStream, | ||
308 | ) -> Result<S::TokenStream, PanicMessage> { | ||
309 | let client::Client { get_handle_counters, run, f } = *self; | ||
310 | run_server( | ||
311 | strategy, | ||
312 | get_handle_counters(), | ||
313 | server, | ||
314 | ( | ||
315 | <MarkedTypes<S> as Types>::TokenStream::mark(input), | ||
316 | <MarkedTypes<S> as Types>::TokenStream::mark(input2), | ||
317 | ), | ||
318 | run, | ||
319 | f, | ||
320 | ) | ||
321 | .map(<MarkedTypes<S> as Types>::TokenStream::unmark) | ||
322 | } | ||
323 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/diagnostic.rs b/crates/ra_proc_macro_srv/src/proc_macro/diagnostic.rs new file mode 100644 index 000000000..9029f8815 --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/diagnostic.rs | |||
@@ -0,0 +1,170 @@ | |||
1 | //! lib-proc-macro diagnostic | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/diagnostic.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | use crate::proc_macro::Span; | ||
7 | |||
8 | /// An enum representing a diagnostic level. | ||
9 | #[derive(Copy, Clone, Debug)] | ||
10 | #[non_exhaustive] | ||
11 | pub enum Level { | ||
12 | /// An error. | ||
13 | Error, | ||
14 | /// A warning. | ||
15 | Warning, | ||
16 | /// A note. | ||
17 | Note, | ||
18 | /// A help message. | ||
19 | Help, | ||
20 | } | ||
21 | |||
22 | /// Trait implemented by types that can be converted into a set of `Span`s. | ||
23 | pub trait MultiSpan { | ||
24 | /// Converts `self` into a `Vec<Span>`. | ||
25 | fn into_spans(self) -> Vec<Span>; | ||
26 | } | ||
27 | |||
28 | impl MultiSpan for Span { | ||
29 | fn into_spans(self) -> Vec<Span> { | ||
30 | vec![self] | ||
31 | } | ||
32 | } | ||
33 | |||
34 | impl MultiSpan for Vec<Span> { | ||
35 | fn into_spans(self) -> Vec<Span> { | ||
36 | self | ||
37 | } | ||
38 | } | ||
39 | |||
40 | impl<'a> MultiSpan for &'a [Span] { | ||
41 | fn into_spans(self) -> Vec<Span> { | ||
42 | self.to_vec() | ||
43 | } | ||
44 | } | ||
45 | |||
46 | /// A structure representing a diagnostic message and associated children | ||
47 | /// messages. | ||
48 | #[derive(Clone, Debug)] | ||
49 | pub struct Diagnostic { | ||
50 | level: Level, | ||
51 | message: String, | ||
52 | spans: Vec<Span>, | ||
53 | children: Vec<Diagnostic>, | ||
54 | } | ||
55 | |||
56 | macro_rules! diagnostic_child_methods { | ||
57 | ($spanned:ident, $regular:ident, $level:expr) => ( | ||
58 | /// Adds a new child diagnostic message to `self` with the level | ||
59 | /// identified by this method's name with the given `spans` and | ||
60 | /// `message`. | ||
61 | pub fn $spanned<S, T>(mut self, spans: S, message: T) -> Diagnostic | ||
62 | where S: MultiSpan, T: Into<String> | ||
63 | { | ||
64 | self.children.push(Diagnostic::spanned(spans, $level, message)); | ||
65 | self | ||
66 | } | ||
67 | |||
68 | /// Adds a new child diagnostic message to `self` with the level | ||
69 | /// identified by this method's name with the given `message`. | ||
70 | pub fn $regular<T: Into<String>>(mut self, message: T) -> Diagnostic { | ||
71 | self.children.push(Diagnostic::new($level, message)); | ||
72 | self | ||
73 | } | ||
74 | ) | ||
75 | } | ||
76 | |||
77 | /// Iterator over the children diagnostics of a `Diagnostic`. | ||
78 | #[derive(Debug, Clone)] | ||
79 | pub struct Children<'a>(std::slice::Iter<'a, Diagnostic>); | ||
80 | |||
81 | impl<'a> Iterator for Children<'a> { | ||
82 | type Item = &'a Diagnostic; | ||
83 | |||
84 | fn next(&mut self) -> Option<Self::Item> { | ||
85 | self.0.next() | ||
86 | } | ||
87 | } | ||
88 | |||
89 | impl Diagnostic { | ||
90 | /// Creates a new diagnostic with the given `level` and `message`. | ||
91 | pub fn new<T: Into<String>>(level: Level, message: T) -> Diagnostic { | ||
92 | Diagnostic { level: level, message: message.into(), spans: vec![], children: vec![] } | ||
93 | } | ||
94 | |||
95 | /// Creates a new diagnostic with the given `level` and `message` pointing to | ||
96 | /// the given set of `spans`. | ||
97 | pub fn spanned<S, T>(spans: S, level: Level, message: T) -> Diagnostic | ||
98 | where | ||
99 | S: MultiSpan, | ||
100 | T: Into<String>, | ||
101 | { | ||
102 | Diagnostic { | ||
103 | level: level, | ||
104 | message: message.into(), | ||
105 | spans: spans.into_spans(), | ||
106 | children: vec![], | ||
107 | } | ||
108 | } | ||
109 | |||
110 | diagnostic_child_methods!(span_error, error, Level::Error); | ||
111 | diagnostic_child_methods!(span_warning, warning, Level::Warning); | ||
112 | diagnostic_child_methods!(span_note, note, Level::Note); | ||
113 | diagnostic_child_methods!(span_help, help, Level::Help); | ||
114 | |||
115 | /// Returns the diagnostic `level` for `self`. | ||
116 | pub fn level(&self) -> Level { | ||
117 | self.level | ||
118 | } | ||
119 | |||
120 | /// Sets the level in `self` to `level`. | ||
121 | pub fn set_level(&mut self, level: Level) { | ||
122 | self.level = level; | ||
123 | } | ||
124 | |||
125 | /// Returns the message in `self`. | ||
126 | pub fn message(&self) -> &str { | ||
127 | &self.message | ||
128 | } | ||
129 | |||
130 | /// Sets the message in `self` to `message`. | ||
131 | pub fn set_message<T: Into<String>>(&mut self, message: T) { | ||
132 | self.message = message.into(); | ||
133 | } | ||
134 | |||
135 | /// Returns the `Span`s in `self`. | ||
136 | pub fn spans(&self) -> &[Span] { | ||
137 | &self.spans | ||
138 | } | ||
139 | |||
140 | /// Sets the `Span`s in `self` to `spans`. | ||
141 | pub fn set_spans<S: MultiSpan>(&mut self, spans: S) { | ||
142 | self.spans = spans.into_spans(); | ||
143 | } | ||
144 | |||
145 | /// Returns an iterator over the children diagnostics of `self`. | ||
146 | pub fn children(&self) -> Children<'_> { | ||
147 | Children(self.children.iter()) | ||
148 | } | ||
149 | |||
150 | /// Emit the diagnostic. | ||
151 | pub fn emit(self) { | ||
152 | fn to_internal(spans: Vec<Span>) -> crate::proc_macro::bridge::client::MultiSpan { | ||
153 | let mut multi_span = crate::proc_macro::bridge::client::MultiSpan::new(); | ||
154 | for span in spans { | ||
155 | multi_span.push(span.0); | ||
156 | } | ||
157 | multi_span | ||
158 | } | ||
159 | |||
160 | let mut diag = crate::proc_macro::bridge::client::Diagnostic::new( | ||
161 | self.level, | ||
162 | &self.message[..], | ||
163 | to_internal(self.spans), | ||
164 | ); | ||
165 | for c in self.children { | ||
166 | diag.sub(c.level, &c.message[..], to_internal(c.spans)); | ||
167 | } | ||
168 | diag.emit(); | ||
169 | } | ||
170 | } | ||
diff --git a/crates/ra_proc_macro_srv/src/proc_macro/mod.rs b/crates/ra_proc_macro_srv/src/proc_macro/mod.rs new file mode 100644 index 000000000..e35a6ff8b --- /dev/null +++ b/crates/ra_proc_macro_srv/src/proc_macro/mod.rs | |||
@@ -0,0 +1,926 @@ | |||
1 | //! lib-proc-macro main module | ||
2 | //! | ||
3 | //! Copy from https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/lib.rs | ||
4 | //! augmented with removing unstable features | ||
5 | |||
6 | // NOTE(@edwin0cheng): | ||
7 | // Because we just copy the bridge module from rustc for ABI compatible | ||
8 | // There are some unused stuffs inside it. | ||
9 | // We suppress these warning here. | ||
10 | #[doc(hidden)] | ||
11 | #[allow(unused_macros)] | ||
12 | #[allow(unused_variables)] | ||
13 | pub mod bridge; | ||
14 | |||
15 | mod diagnostic; | ||
16 | |||
17 | pub use diagnostic::{Diagnostic, Level, MultiSpan}; | ||
18 | |||
19 | use std::ops::{Bound, RangeBounds}; | ||
20 | use std::path::PathBuf; | ||
21 | use std::str::FromStr; | ||
22 | use std::{fmt, iter, mem}; | ||
23 | |||
24 | /// The main type provided by this crate, representing an abstract stream of | ||
25 | /// tokens, or, more specifically, a sequence of token trees. | ||
26 | /// The type provide interfaces for iterating over those token trees and, conversely, | ||
27 | /// collecting a number of token trees into one stream. | ||
28 | /// | ||
29 | /// This is both the input and output of `#[proc_macro]`, `#[proc_macro_attribute]` | ||
30 | /// and `#[proc_macro_derive]` definitions. | ||
31 | #[derive(Clone)] | ||
32 | pub struct TokenStream(bridge::client::TokenStream); | ||
33 | |||
34 | /// Error returned from `TokenStream::from_str` | ||
35 | #[derive(Debug)] | ||
36 | pub struct LexError { | ||
37 | _inner: (), | ||
38 | } | ||
39 | |||
40 | impl TokenStream { | ||
41 | /// Returns an empty `TokenStream` containing no token trees. | ||
42 | pub fn new() -> TokenStream { | ||
43 | TokenStream(bridge::client::TokenStream::new()) | ||
44 | } | ||
45 | |||
46 | /// Checks if this `TokenStream` is empty. | ||
47 | pub fn is_empty(&self) -> bool { | ||
48 | self.0.is_empty() | ||
49 | } | ||
50 | } | ||
51 | |||
52 | /// Attempts to break the string into tokens and parse those tokens into a token stream. | ||
53 | /// May fail for a number of reasons, for example, if the string contains unbalanced delimiters | ||
54 | /// or characters not existing in the language. | ||
55 | /// All tokens in the parsed stream get `Span::call_site()` spans. | ||
56 | /// | ||
57 | /// NOTE: some errors may cause panics instead of returning `LexError`. We reserve the right to | ||
58 | /// change these errors into `LexError`s later. | ||
59 | impl FromStr for TokenStream { | ||
60 | type Err = LexError; | ||
61 | |||
62 | fn from_str(src: &str) -> Result<TokenStream, LexError> { | ||
63 | Ok(TokenStream(bridge::client::TokenStream::from_str(src))) | ||
64 | } | ||
65 | } | ||
66 | |||
67 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
68 | // based on it (the reverse of the usual relationship between the two). | ||
69 | // impl ToString for TokenStream { | ||
70 | // fn to_string(&self) -> String { | ||
71 | // self.0.to_string() | ||
72 | // } | ||
73 | // } | ||
74 | |||
75 | /// Prints the token stream as a string that is supposed to be losslessly convertible back | ||
76 | /// into the same token stream (modulo spans), except for possibly `TokenTree::Group`s | ||
77 | /// with `Delimiter::None` delimiters and negative numeric literals. | ||
78 | impl fmt::Display for TokenStream { | ||
79 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
80 | f.write_str(&self.to_string()) | ||
81 | } | ||
82 | } | ||
83 | |||
84 | /// Prints token in a form convenient for debugging. | ||
85 | impl fmt::Debug for TokenStream { | ||
86 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
87 | f.write_str("TokenStream ")?; | ||
88 | f.debug_list().entries(self.clone()).finish() | ||
89 | } | ||
90 | } | ||
91 | |||
92 | /// Creates a token stream containing a single token tree. | ||
93 | impl From<TokenTree> for TokenStream { | ||
94 | fn from(tree: TokenTree) -> TokenStream { | ||
95 | TokenStream(bridge::client::TokenStream::from_token_tree(match tree { | ||
96 | TokenTree::Group(tt) => bridge::TokenTree::Group(tt.0), | ||
97 | TokenTree::Punct(tt) => bridge::TokenTree::Punct(tt.0), | ||
98 | TokenTree::Ident(tt) => bridge::TokenTree::Ident(tt.0), | ||
99 | TokenTree::Literal(tt) => bridge::TokenTree::Literal(tt.0), | ||
100 | })) | ||
101 | } | ||
102 | } | ||
103 | |||
104 | /// Collects a number of token trees into a single stream. | ||
105 | impl iter::FromIterator<TokenTree> for TokenStream { | ||
106 | fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self { | ||
107 | trees.into_iter().map(TokenStream::from).collect() | ||
108 | } | ||
109 | } | ||
110 | |||
111 | /// A "flattening" operation on token streams, collects token trees | ||
112 | /// from multiple token streams into a single stream. | ||
113 | impl iter::FromIterator<TokenStream> for TokenStream { | ||
114 | fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self { | ||
115 | let mut builder = bridge::client::TokenStreamBuilder::new(); | ||
116 | streams.into_iter().for_each(|stream| builder.push(stream.0)); | ||
117 | TokenStream(builder.build()) | ||
118 | } | ||
119 | } | ||
120 | |||
121 | impl Extend<TokenTree> for TokenStream { | ||
122 | fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, trees: I) { | ||
123 | self.extend(trees.into_iter().map(TokenStream::from)); | ||
124 | } | ||
125 | } | ||
126 | |||
127 | impl Extend<TokenStream> for TokenStream { | ||
128 | fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) { | ||
129 | // FIXME(eddyb) Use an optimized implementation if/when possible. | ||
130 | *self = iter::once(mem::replace(self, Self::new())).chain(streams).collect(); | ||
131 | } | ||
132 | } | ||
133 | |||
134 | /// Public implementation details for the `TokenStream` type, such as iterators. | ||
135 | pub mod token_stream { | ||
136 | use crate::proc_macro::{bridge, Group, Ident, Literal, Punct, TokenStream, TokenTree}; | ||
137 | |||
138 | /// An iterator over `TokenStream`'s `TokenTree`s. | ||
139 | /// The iteration is "shallow", e.g., the iterator doesn't recurse into delimited groups, | ||
140 | /// and returns whole groups as token trees. | ||
141 | #[derive(Clone)] | ||
142 | pub struct IntoIter(bridge::client::TokenStreamIter); | ||
143 | |||
144 | impl Iterator for IntoIter { | ||
145 | type Item = TokenTree; | ||
146 | |||
147 | fn next(&mut self) -> Option<TokenTree> { | ||
148 | self.0.next().map(|tree| match tree { | ||
149 | bridge::TokenTree::Group(tt) => TokenTree::Group(Group(tt)), | ||
150 | bridge::TokenTree::Punct(tt) => TokenTree::Punct(Punct(tt)), | ||
151 | bridge::TokenTree::Ident(tt) => TokenTree::Ident(Ident(tt)), | ||
152 | bridge::TokenTree::Literal(tt) => TokenTree::Literal(Literal(tt)), | ||
153 | }) | ||
154 | } | ||
155 | } | ||
156 | |||
157 | impl IntoIterator for TokenStream { | ||
158 | type Item = TokenTree; | ||
159 | type IntoIter = IntoIter; | ||
160 | |||
161 | fn into_iter(self) -> IntoIter { | ||
162 | IntoIter(self.0.into_iter()) | ||
163 | } | ||
164 | } | ||
165 | } | ||
166 | |||
167 | /// A region of source code, along with macro expansion information. | ||
168 | #[derive(Copy, Clone)] | ||
169 | pub struct Span(bridge::client::Span); | ||
170 | |||
171 | macro_rules! diagnostic_method { | ||
172 | ($name:ident, $level:expr) => ( | ||
173 | /// Creates a new `Diagnostic` with the given `message` at the span | ||
174 | /// `self`. | ||
175 | pub fn $name<T: Into<String>>(self, message: T) -> Diagnostic { | ||
176 | Diagnostic::spanned(self, $level, message) | ||
177 | } | ||
178 | ) | ||
179 | } | ||
180 | |||
181 | impl Span { | ||
182 | /// A span that resolves at the macro definition site. | ||
183 | pub fn def_site() -> Span { | ||
184 | Span(bridge::client::Span::def_site()) | ||
185 | } | ||
186 | |||
187 | /// The span of the invocation of the current procedural macro. | ||
188 | /// Identifiers created with this span will be resolved as if they were written | ||
189 | /// directly at the macro call location (call-site hygiene) and other code | ||
190 | /// at the macro call site will be able to refer to them as well. | ||
191 | pub fn call_site() -> Span { | ||
192 | Span(bridge::client::Span::call_site()) | ||
193 | } | ||
194 | |||
195 | /// A span that represents `macro_rules` hygiene, and sometimes resolves at the macro | ||
196 | /// definition site (local variables, labels, `$crate`) and sometimes at the macro | ||
197 | /// call site (everything else). | ||
198 | /// The span location is taken from the call-site. | ||
199 | pub fn mixed_site() -> Span { | ||
200 | Span(bridge::client::Span::mixed_site()) | ||
201 | } | ||
202 | |||
203 | /// The original source file into which this span points. | ||
204 | pub fn source_file(&self) -> SourceFile { | ||
205 | SourceFile(self.0.source_file()) | ||
206 | } | ||
207 | |||
208 | /// The `Span` for the tokens in the previous macro expansion from which | ||
209 | /// `self` was generated from, if any. | ||
210 | pub fn parent(&self) -> Option<Span> { | ||
211 | self.0.parent().map(Span) | ||
212 | } | ||
213 | |||
214 | /// The span for the origin source code that `self` was generated from. If | ||
215 | /// this `Span` wasn't generated from other macro expansions then the return | ||
216 | /// value is the same as `*self`. | ||
217 | pub fn source(&self) -> Span { | ||
218 | Span(self.0.source()) | ||
219 | } | ||
220 | |||
221 | /// Gets the starting line/column in the source file for this span. | ||
222 | pub fn start(&self) -> LineColumn { | ||
223 | self.0.start() | ||
224 | } | ||
225 | |||
226 | /// Gets the ending line/column in the source file for this span. | ||
227 | pub fn end(&self) -> LineColumn { | ||
228 | self.0.end() | ||
229 | } | ||
230 | |||
231 | /// Creates a new span encompassing `self` and `other`. | ||
232 | /// | ||
233 | /// Returns `None` if `self` and `other` are from different files. | ||
234 | pub fn join(&self, other: Span) -> Option<Span> { | ||
235 | self.0.join(other.0).map(Span) | ||
236 | } | ||
237 | |||
238 | /// Creates a new span with the same line/column information as `self` but | ||
239 | /// that resolves symbols as though it were at `other`. | ||
240 | pub fn resolved_at(&self, other: Span) -> Span { | ||
241 | Span(self.0.resolved_at(other.0)) | ||
242 | } | ||
243 | |||
244 | /// Creates a new span with the same name resolution behavior as `self` but | ||
245 | /// with the line/column information of `other`. | ||
246 | pub fn located_at(&self, other: Span) -> Span { | ||
247 | other.resolved_at(*self) | ||
248 | } | ||
249 | |||
250 | /// Compares to spans to see if they're equal. | ||
251 | pub fn eq(&self, other: &Span) -> bool { | ||
252 | self.0 == other.0 | ||
253 | } | ||
254 | |||
255 | /// Returns the source text behind a span. This preserves the original source | ||
256 | /// code, including spaces and comments. It only returns a result if the span | ||
257 | /// corresponds to real source code. | ||
258 | /// | ||
259 | /// Note: The observable result of a macro should only rely on the tokens and | ||
260 | /// not on this source text. The result of this function is a best effort to | ||
261 | /// be used for diagnostics only. | ||
262 | pub fn source_text(&self) -> Option<String> { | ||
263 | self.0.source_text() | ||
264 | } | ||
265 | |||
266 | diagnostic_method!(error, Level::Error); | ||
267 | diagnostic_method!(warning, Level::Warning); | ||
268 | diagnostic_method!(note, Level::Note); | ||
269 | diagnostic_method!(help, Level::Help); | ||
270 | } | ||
271 | |||
272 | /// Prints a span in a form convenient for debugging. | ||
273 | impl fmt::Debug for Span { | ||
274 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
275 | self.0.fmt(f) | ||
276 | } | ||
277 | } | ||
278 | |||
279 | /// A line-column pair representing the start or end of a `Span`. | ||
280 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
281 | pub struct LineColumn { | ||
282 | /// The 1-indexed line in the source file on which the span starts or ends (inclusive). | ||
283 | pub line: usize, | ||
284 | /// The 0-indexed column (in UTF-8 characters) in the source file on which | ||
285 | /// the span starts or ends (inclusive). | ||
286 | pub column: usize, | ||
287 | } | ||
288 | |||
289 | /// The source file of a given `Span`. | ||
290 | #[derive(Clone)] | ||
291 | pub struct SourceFile(bridge::client::SourceFile); | ||
292 | |||
293 | impl SourceFile { | ||
294 | /// Gets the path to this source file. | ||
295 | /// | ||
296 | /// ### Note | ||
297 | /// If the code span associated with this `SourceFile` was generated by an external macro, this | ||
298 | /// macro, this may not be an actual path on the filesystem. Use [`is_real`] to check. | ||
299 | /// | ||
300 | /// Also note that even if `is_real` returns `true`, if `--remap-path-prefix` was passed on | ||
301 | /// the command line, the path as given may not actually be valid. | ||
302 | /// | ||
303 | /// [`is_real`]: #method.is_real | ||
304 | pub fn path(&self) -> PathBuf { | ||
305 | PathBuf::from(self.0.path()) | ||
306 | } | ||
307 | |||
308 | /// Returns `true` if this source file is a real source file, and not generated by an external | ||
309 | /// macro's expansion. | ||
310 | pub fn is_real(&self) -> bool { | ||
311 | // This is a hack until intercrate spans are implemented and we can have real source files | ||
312 | // for spans generated in external macros. | ||
313 | // https://github.com/rust-lang/rust/pull/43604#issuecomment-333334368 | ||
314 | self.0.is_real() | ||
315 | } | ||
316 | } | ||
317 | |||
318 | impl fmt::Debug for SourceFile { | ||
319 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
320 | f.debug_struct("SourceFile") | ||
321 | .field("path", &self.path()) | ||
322 | .field("is_real", &self.is_real()) | ||
323 | .finish() | ||
324 | } | ||
325 | } | ||
326 | |||
327 | impl PartialEq for SourceFile { | ||
328 | fn eq(&self, other: &Self) -> bool { | ||
329 | self.0.eq(&other.0) | ||
330 | } | ||
331 | } | ||
332 | |||
333 | impl Eq for SourceFile {} | ||
334 | |||
335 | /// A single token or a delimited sequence of token trees (e.g., `[1, (), ..]`). | ||
336 | #[derive(Clone)] | ||
337 | pub enum TokenTree { | ||
338 | /// A token stream surrounded by bracket delimiters. | ||
339 | Group(Group), | ||
340 | /// An identifier. | ||
341 | Ident(Ident), | ||
342 | /// A single punctuation character (`+`, `,`, `$`, etc.). | ||
343 | Punct(Punct), | ||
344 | /// A literal character (`'a'`), string (`"hello"`), number (`2.3`), etc. | ||
345 | Literal(Literal), | ||
346 | } | ||
347 | |||
348 | impl TokenTree { | ||
349 | /// Returns the span of this tree, delegating to the `span` method of | ||
350 | /// the contained token or a delimited stream. | ||
351 | pub fn span(&self) -> Span { | ||
352 | match *self { | ||
353 | TokenTree::Group(ref t) => t.span(), | ||
354 | TokenTree::Ident(ref t) => t.span(), | ||
355 | TokenTree::Punct(ref t) => t.span(), | ||
356 | TokenTree::Literal(ref t) => t.span(), | ||
357 | } | ||
358 | } | ||
359 | |||
360 | /// Configures the span for *only this token*. | ||
361 | /// | ||
362 | /// Note that if this token is a `Group` then this method will not configure | ||
363 | /// the span of each of the internal tokens, this will simply delegate to | ||
364 | /// the `set_span` method of each variant. | ||
365 | pub fn set_span(&mut self, span: Span) { | ||
366 | match *self { | ||
367 | TokenTree::Group(ref mut t) => t.set_span(span), | ||
368 | TokenTree::Ident(ref mut t) => t.set_span(span), | ||
369 | TokenTree::Punct(ref mut t) => t.set_span(span), | ||
370 | TokenTree::Literal(ref mut t) => t.set_span(span), | ||
371 | } | ||
372 | } | ||
373 | } | ||
374 | |||
375 | /// Prints token tree in a form convenient for debugging. | ||
376 | impl fmt::Debug for TokenTree { | ||
377 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
378 | // Each of these has the name in the struct type in the derived debug, | ||
379 | // so don't bother with an extra layer of indirection | ||
380 | match *self { | ||
381 | TokenTree::Group(ref tt) => tt.fmt(f), | ||
382 | TokenTree::Ident(ref tt) => tt.fmt(f), | ||
383 | TokenTree::Punct(ref tt) => tt.fmt(f), | ||
384 | TokenTree::Literal(ref tt) => tt.fmt(f), | ||
385 | } | ||
386 | } | ||
387 | } | ||
388 | |||
389 | impl From<Group> for TokenTree { | ||
390 | fn from(g: Group) -> TokenTree { | ||
391 | TokenTree::Group(g) | ||
392 | } | ||
393 | } | ||
394 | |||
395 | impl From<Ident> for TokenTree { | ||
396 | fn from(g: Ident) -> TokenTree { | ||
397 | TokenTree::Ident(g) | ||
398 | } | ||
399 | } | ||
400 | |||
401 | impl From<Punct> for TokenTree { | ||
402 | fn from(g: Punct) -> TokenTree { | ||
403 | TokenTree::Punct(g) | ||
404 | } | ||
405 | } | ||
406 | |||
407 | impl From<Literal> for TokenTree { | ||
408 | fn from(g: Literal) -> TokenTree { | ||
409 | TokenTree::Literal(g) | ||
410 | } | ||
411 | } | ||
412 | |||
413 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
414 | // based on it (the reverse of the usual relationship between the two). | ||
415 | // impl ToString for TokenTree { | ||
416 | // fn to_string(&self) -> String { | ||
417 | // match *self { | ||
418 | // TokenTree::Group(ref t) => t.to_string(), | ||
419 | // TokenTree::Ident(ref t) => t.to_string(), | ||
420 | // TokenTree::Punct(ref t) => t.to_string(), | ||
421 | // TokenTree::Literal(ref t) => t.to_string(), | ||
422 | // } | ||
423 | // } | ||
424 | // } | ||
425 | |||
426 | /// Prints the token tree as a string that is supposed to be losslessly convertible back | ||
427 | /// into the same token tree (modulo spans), except for possibly `TokenTree::Group`s | ||
428 | /// with `Delimiter::None` delimiters and negative numeric literals. | ||
429 | impl fmt::Display for TokenTree { | ||
430 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
431 | f.write_str(&self.to_string()) | ||
432 | } | ||
433 | } | ||
434 | |||
435 | /// A delimited token stream. | ||
436 | /// | ||
437 | /// A `Group` internally contains a `TokenStream` which is surrounded by `Delimiter`s. | ||
438 | #[derive(Clone)] | ||
439 | pub struct Group(bridge::client::Group); | ||
440 | |||
441 | /// Describes how a sequence of token trees is delimited. | ||
442 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
443 | pub enum Delimiter { | ||
444 | /// `( ... )` | ||
445 | Parenthesis, | ||
446 | /// `{ ... }` | ||
447 | Brace, | ||
448 | /// `[ ... ]` | ||
449 | Bracket, | ||
450 | /// `Ø ... Ø` | ||
451 | /// An implicit delimiter, that may, for example, appear around tokens coming from a | ||
452 | /// "macro variable" `$var`. It is important to preserve operator priorities in cases like | ||
453 | /// `$var * 3` where `$var` is `1 + 2`. | ||
454 | /// Implicit delimiters may not survive roundtrip of a token stream through a string. | ||
455 | None, | ||
456 | } | ||
457 | |||
458 | impl Group { | ||
459 | /// Creates a new `Group` with the given delimiter and token stream. | ||
460 | /// | ||
461 | /// This constructor will set the span for this group to | ||
462 | /// `Span::call_site()`. To change the span you can use the `set_span` | ||
463 | /// method below. | ||
464 | pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group { | ||
465 | Group(bridge::client::Group::new(delimiter, stream.0)) | ||
466 | } | ||
467 | |||
468 | /// Returns the delimiter of this `Group` | ||
469 | pub fn delimiter(&self) -> Delimiter { | ||
470 | self.0.delimiter() | ||
471 | } | ||
472 | |||
473 | /// Returns the `TokenStream` of tokens that are delimited in this `Group`. | ||
474 | /// | ||
475 | /// Note that the returned token stream does not include the delimiter | ||
476 | /// returned above. | ||
477 | pub fn stream(&self) -> TokenStream { | ||
478 | TokenStream(self.0.stream()) | ||
479 | } | ||
480 | |||
481 | /// Returns the span for the delimiters of this token stream, spanning the | ||
482 | /// entire `Group`. | ||
483 | /// | ||
484 | /// ```text | ||
485 | /// pub fn span(&self) -> Span { | ||
486 | /// ^^^^^^^ | ||
487 | /// ``` | ||
488 | pub fn span(&self) -> Span { | ||
489 | Span(self.0.span()) | ||
490 | } | ||
491 | |||
492 | /// Returns the span pointing to the opening delimiter of this group. | ||
493 | /// | ||
494 | /// ```text | ||
495 | /// pub fn span_open(&self) -> Span { | ||
496 | /// ^ | ||
497 | /// ``` | ||
498 | pub fn span_open(&self) -> Span { | ||
499 | Span(self.0.span_open()) | ||
500 | } | ||
501 | |||
502 | /// Returns the span pointing to the closing delimiter of this group. | ||
503 | /// | ||
504 | /// ```text | ||
505 | /// pub fn span_close(&self) -> Span { | ||
506 | /// ^ | ||
507 | /// ``` | ||
508 | pub fn span_close(&self) -> Span { | ||
509 | Span(self.0.span_close()) | ||
510 | } | ||
511 | |||
512 | /// Configures the span for this `Group`'s delimiters, but not its internal | ||
513 | /// tokens. | ||
514 | /// | ||
515 | /// This method will **not** set the span of all the internal tokens spanned | ||
516 | /// by this group, but rather it will only set the span of the delimiter | ||
517 | /// tokens at the level of the `Group`. | ||
518 | pub fn set_span(&mut self, span: Span) { | ||
519 | self.0.set_span(span.0); | ||
520 | } | ||
521 | } | ||
522 | |||
523 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
524 | // based on it (the reverse of the usual relationship between the two). | ||
525 | // impl ToString for Group { | ||
526 | // fn to_string(&self) -> String { | ||
527 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
528 | // } | ||
529 | // } | ||
530 | |||
531 | /// Prints the group as a string that should be losslessly convertible back | ||
532 | /// into the same group (modulo spans), except for possibly `TokenTree::Group`s | ||
533 | /// with `Delimiter::None` delimiters. | ||
534 | impl fmt::Display for Group { | ||
535 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
536 | f.write_str(&self.to_string()) | ||
537 | } | ||
538 | } | ||
539 | |||
540 | impl fmt::Debug for Group { | ||
541 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
542 | f.debug_struct("Group") | ||
543 | .field("delimiter", &self.delimiter()) | ||
544 | .field("stream", &self.stream()) | ||
545 | .field("span", &self.span()) | ||
546 | .finish() | ||
547 | } | ||
548 | } | ||
549 | |||
550 | /// An `Punct` is an single punctuation character like `+`, `-` or `#`. | ||
551 | /// | ||
552 | /// Multi-character operators like `+=` are represented as two instances of `Punct` with different | ||
553 | /// forms of `Spacing` returned. | ||
554 | #[derive(Clone)] | ||
555 | pub struct Punct(bridge::client::Punct); | ||
556 | |||
557 | /// Whether an `Punct` is followed immediately by another `Punct` or | ||
558 | /// followed by another token or whitespace. | ||
559 | #[derive(Copy, Clone, Debug, PartialEq, Eq)] | ||
560 | pub enum Spacing { | ||
561 | /// e.g., `+` is `Alone` in `+ =`, `+ident` or `+()`. | ||
562 | Alone, | ||
563 | /// e.g., `+` is `Joint` in `+=` or `'#`. | ||
564 | /// Additionally, single quote `'` can join with identifiers to form lifetimes `'ident`. | ||
565 | Joint, | ||
566 | } | ||
567 | |||
568 | impl Punct { | ||
569 | /// Creates a new `Punct` from the given character and spacing. | ||
570 | /// The `ch` argument must be a valid punctuation character permitted by the language, | ||
571 | /// otherwise the function will panic. | ||
572 | /// | ||
573 | /// The returned `Punct` will have the default span of `Span::call_site()` | ||
574 | /// which can be further configured with the `set_span` method below. | ||
575 | pub fn new(ch: char, spacing: Spacing) -> Punct { | ||
576 | Punct(bridge::client::Punct::new(ch, spacing)) | ||
577 | } | ||
578 | |||
579 | /// Returns the value of this punctuation character as `char`. | ||
580 | pub fn as_char(&self) -> char { | ||
581 | self.0.as_char() | ||
582 | } | ||
583 | |||
584 | /// Returns the spacing of this punctuation character, indicating whether it's immediately | ||
585 | /// followed by another `Punct` in the token stream, so they can potentially be combined into | ||
586 | /// a multi-character operator (`Joint`), or it's followed by some other token or whitespace | ||
587 | /// (`Alone`) so the operator has certainly ended. | ||
588 | pub fn spacing(&self) -> Spacing { | ||
589 | self.0.spacing() | ||
590 | } | ||
591 | |||
592 | /// Returns the span for this punctuation character. | ||
593 | pub fn span(&self) -> Span { | ||
594 | Span(self.0.span()) | ||
595 | } | ||
596 | |||
597 | /// Configure the span for this punctuation character. | ||
598 | pub fn set_span(&mut self, span: Span) { | ||
599 | self.0 = self.0.with_span(span.0); | ||
600 | } | ||
601 | } | ||
602 | |||
603 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
604 | // based on it (the reverse of the usual relationship between the two). | ||
605 | // impl ToString for Punct { | ||
606 | // fn to_string(&self) -> String { | ||
607 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
608 | // } | ||
609 | // } | ||
610 | |||
611 | /// Prints the punctuation character as a string that should be losslessly convertible | ||
612 | /// back into the same character. | ||
613 | impl fmt::Display for Punct { | ||
614 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
615 | f.write_str(&self.to_string()) | ||
616 | } | ||
617 | } | ||
618 | |||
619 | impl fmt::Debug for Punct { | ||
620 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
621 | f.debug_struct("Punct") | ||
622 | .field("ch", &self.as_char()) | ||
623 | .field("spacing", &self.spacing()) | ||
624 | .field("span", &self.span()) | ||
625 | .finish() | ||
626 | } | ||
627 | } | ||
628 | |||
629 | /// An identifier (`ident`). | ||
630 | #[derive(Clone, PartialEq, Eq, Hash)] | ||
631 | pub struct Ident(bridge::client::Ident); | ||
632 | |||
633 | impl Ident { | ||
634 | /// Creates a new `Ident` with the given `string` as well as the specified | ||
635 | /// `span`. | ||
636 | /// The `string` argument must be a valid identifier permitted by the | ||
637 | /// language, otherwise the function will panic. | ||
638 | /// | ||
639 | /// Note that `span`, currently in rustc, configures the hygiene information | ||
640 | /// for this identifier. | ||
641 | /// | ||
642 | /// As of this time `Span::call_site()` explicitly opts-in to "call-site" hygiene | ||
643 | /// meaning that identifiers created with this span will be resolved as if they were written | ||
644 | /// directly at the location of the macro call, and other code at the macro call site will be | ||
645 | /// able to refer to them as well. | ||
646 | /// | ||
647 | /// Later spans like `Span::def_site()` will allow to opt-in to "definition-site" hygiene | ||
648 | /// meaning that identifiers created with this span will be resolved at the location of the | ||
649 | /// macro definition and other code at the macro call site will not be able to refer to them. | ||
650 | /// | ||
651 | /// Due to the current importance of hygiene this constructor, unlike other | ||
652 | /// tokens, requires a `Span` to be specified at construction. | ||
653 | pub fn new(string: &str, span: Span) -> Ident { | ||
654 | Ident(bridge::client::Ident::new(string, span.0, false)) | ||
655 | } | ||
656 | |||
657 | /// Same as `Ident::new`, but creates a raw identifier (`r#ident`). | ||
658 | pub fn new_raw(string: &str, span: Span) -> Ident { | ||
659 | Ident(bridge::client::Ident::new(string, span.0, true)) | ||
660 | } | ||
661 | |||
662 | /// Returns the span of this `Ident`, encompassing the entire string returned | ||
663 | /// by `as_str`. | ||
664 | pub fn span(&self) -> Span { | ||
665 | Span(self.0.span()) | ||
666 | } | ||
667 | |||
668 | /// Configures the span of this `Ident`, possibly changing its hygiene context. | ||
669 | pub fn set_span(&mut self, span: Span) { | ||
670 | self.0 = self.0.with_span(span.0); | ||
671 | } | ||
672 | } | ||
673 | |||
674 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
675 | // based on it (the reverse of the usual relationship between the two). | ||
676 | // impl ToString for Ident { | ||
677 | // fn to_string(&self) -> String { | ||
678 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
679 | // } | ||
680 | // } | ||
681 | |||
682 | /// Prints the identifier as a string that should be losslessly convertible | ||
683 | /// back into the same identifier. | ||
684 | impl fmt::Display for Ident { | ||
685 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
686 | f.write_str(&self.to_string()) | ||
687 | } | ||
688 | } | ||
689 | |||
690 | impl fmt::Debug for Ident { | ||
691 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
692 | f.debug_struct("Ident") | ||
693 | .field("ident", &self.to_string()) | ||
694 | .field("span", &self.span()) | ||
695 | .finish() | ||
696 | } | ||
697 | } | ||
698 | |||
699 | /// A literal string (`"hello"`), byte string (`b"hello"`), | ||
700 | /// character (`'a'`), byte character (`b'a'`), an integer or floating point number | ||
701 | /// with or without a suffix (`1`, `1u8`, `2.3`, `2.3f32`). | ||
702 | /// Boolean literals like `true` and `false` do not belong here, they are `Ident`s. | ||
703 | #[derive(Clone)] | ||
704 | pub struct Literal(bridge::client::Literal); | ||
705 | |||
706 | macro_rules! suffixed_int_literals { | ||
707 | ($($name:ident => $kind:ident,)*) => ($( | ||
708 | /// Creates a new suffixed integer literal with the specified value. | ||
709 | /// | ||
710 | /// This function will create an integer like `1u32` where the integer | ||
711 | /// value specified is the first part of the token and the integral is | ||
712 | /// also suffixed at the end. | ||
713 | /// Literals created from negative numbers may not survive round-trips through | ||
714 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
715 | /// | ||
716 | /// Literals created through this method have the `Span::call_site()` | ||
717 | /// span by default, which can be configured with the `set_span` method | ||
718 | /// below. | ||
719 | pub fn $name(n: $kind) -> Literal { | ||
720 | Literal(bridge::client::Literal::typed_integer(&n.to_string(), stringify!($kind))) | ||
721 | } | ||
722 | )*) | ||
723 | } | ||
724 | |||
725 | macro_rules! unsuffixed_int_literals { | ||
726 | ($($name:ident => $kind:ident,)*) => ($( | ||
727 | /// Creates a new unsuffixed integer literal with the specified value. | ||
728 | /// | ||
729 | /// This function will create an integer like `1` where the integer | ||
730 | /// value specified is the first part of the token. No suffix is | ||
731 | /// specified on this token, meaning that invocations like | ||
732 | /// `Literal::i8_unsuffixed(1)` are equivalent to | ||
733 | /// `Literal::u32_unsuffixed(1)`. | ||
734 | /// Literals created from negative numbers may not survive rountrips through | ||
735 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
736 | /// | ||
737 | /// Literals created through this method have the `Span::call_site()` | ||
738 | /// span by default, which can be configured with the `set_span` method | ||
739 | /// below. | ||
740 | pub fn $name(n: $kind) -> Literal { | ||
741 | Literal(bridge::client::Literal::integer(&n.to_string())) | ||
742 | } | ||
743 | )*) | ||
744 | } | ||
745 | |||
746 | impl Literal { | ||
747 | suffixed_int_literals! { | ||
748 | u8_suffixed => u8, | ||
749 | u16_suffixed => u16, | ||
750 | u32_suffixed => u32, | ||
751 | u64_suffixed => u64, | ||
752 | u128_suffixed => u128, | ||
753 | usize_suffixed => usize, | ||
754 | i8_suffixed => i8, | ||
755 | i16_suffixed => i16, | ||
756 | i32_suffixed => i32, | ||
757 | i64_suffixed => i64, | ||
758 | i128_suffixed => i128, | ||
759 | isize_suffixed => isize, | ||
760 | } | ||
761 | |||
762 | unsuffixed_int_literals! { | ||
763 | u8_unsuffixed => u8, | ||
764 | u16_unsuffixed => u16, | ||
765 | u32_unsuffixed => u32, | ||
766 | u64_unsuffixed => u64, | ||
767 | u128_unsuffixed => u128, | ||
768 | usize_unsuffixed => usize, | ||
769 | i8_unsuffixed => i8, | ||
770 | i16_unsuffixed => i16, | ||
771 | i32_unsuffixed => i32, | ||
772 | i64_unsuffixed => i64, | ||
773 | i128_unsuffixed => i128, | ||
774 | isize_unsuffixed => isize, | ||
775 | } | ||
776 | |||
777 | /// Creates a new unsuffixed floating-point literal. | ||
778 | /// | ||
779 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where | ||
780 | /// the float's value is emitted directly into the token but no suffix is | ||
781 | /// used, so it may be inferred to be a `f64` later in the compiler. | ||
782 | /// Literals created from negative numbers may not survive rountrips through | ||
783 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
784 | /// | ||
785 | /// # Panics | ||
786 | /// | ||
787 | /// This function requires that the specified float is finite, for | ||
788 | /// example if it is infinity or NaN this function will panic. | ||
789 | pub fn f32_unsuffixed(n: f32) -> Literal { | ||
790 | if !n.is_finite() { | ||
791 | panic!("Invalid float literal {}", n); | ||
792 | } | ||
793 | Literal(bridge::client::Literal::float(&n.to_string())) | ||
794 | } | ||
795 | |||
796 | /// Creates a new suffixed floating-point literal. | ||
797 | /// | ||
798 | /// This constructor will create a literal like `1.0f32` where the value | ||
799 | /// specified is the preceding part of the token and `f32` is the suffix of | ||
800 | /// the token. This token will always be inferred to be an `f32` in the | ||
801 | /// compiler. | ||
802 | /// Literals created from negative numbers may not survive rountrips through | ||
803 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
804 | /// | ||
805 | /// # Panics | ||
806 | /// | ||
807 | /// This function requires that the specified float is finite, for | ||
808 | /// example if it is infinity or NaN this function will panic. | ||
809 | pub fn f32_suffixed(n: f32) -> Literal { | ||
810 | if !n.is_finite() { | ||
811 | panic!("Invalid float literal {}", n); | ||
812 | } | ||
813 | Literal(bridge::client::Literal::f32(&n.to_string())) | ||
814 | } | ||
815 | |||
816 | /// Creates a new unsuffixed floating-point literal. | ||
817 | /// | ||
818 | /// This constructor is similar to those like `Literal::i8_unsuffixed` where | ||
819 | /// the float's value is emitted directly into the token but no suffix is | ||
820 | /// used, so it may be inferred to be a `f64` later in the compiler. | ||
821 | /// Literals created from negative numbers may not survive rountrips through | ||
822 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
823 | /// | ||
824 | /// # Panics | ||
825 | /// | ||
826 | /// This function requires that the specified float is finite, for | ||
827 | /// example if it is infinity or NaN this function will panic. | ||
828 | pub fn f64_unsuffixed(n: f64) -> Literal { | ||
829 | if !n.is_finite() { | ||
830 | panic!("Invalid float literal {}", n); | ||
831 | } | ||
832 | Literal(bridge::client::Literal::float(&n.to_string())) | ||
833 | } | ||
834 | |||
835 | /// Creates a new suffixed floating-point literal. | ||
836 | /// | ||
837 | /// This constructor will create a literal like `1.0f64` where the value | ||
838 | /// specified is the preceding part of the token and `f64` is the suffix of | ||
839 | /// the token. This token will always be inferred to be an `f64` in the | ||
840 | /// compiler. | ||
841 | /// Literals created from negative numbers may not survive rountrips through | ||
842 | /// `TokenStream` or strings and may be broken into two tokens (`-` and positive literal). | ||
843 | /// | ||
844 | /// # Panics | ||
845 | /// | ||
846 | /// This function requires that the specified float is finite, for | ||
847 | /// example if it is infinity or NaN this function will panic. | ||
848 | pub fn f64_suffixed(n: f64) -> Literal { | ||
849 | if !n.is_finite() { | ||
850 | panic!("Invalid float literal {}", n); | ||
851 | } | ||
852 | Literal(bridge::client::Literal::f64(&n.to_string())) | ||
853 | } | ||
854 | |||
855 | /// String literal. | ||
856 | pub fn string(string: &str) -> Literal { | ||
857 | Literal(bridge::client::Literal::string(string)) | ||
858 | } | ||
859 | |||
860 | /// Character literal. | ||
861 | pub fn character(ch: char) -> Literal { | ||
862 | Literal(bridge::client::Literal::character(ch)) | ||
863 | } | ||
864 | |||
865 | /// Byte string literal. | ||
866 | pub fn byte_string(bytes: &[u8]) -> Literal { | ||
867 | Literal(bridge::client::Literal::byte_string(bytes)) | ||
868 | } | ||
869 | |||
870 | /// Returns the span encompassing this literal. | ||
871 | pub fn span(&self) -> Span { | ||
872 | Span(self.0.span()) | ||
873 | } | ||
874 | |||
875 | /// Configures the span associated for this literal. | ||
876 | pub fn set_span(&mut self, span: Span) { | ||
877 | self.0.set_span(span.0); | ||
878 | } | ||
879 | |||
880 | /// Returns a `Span` that is a subset of `self.span()` containing only the | ||
881 | /// source bytes in range `range`. Returns `None` if the would-be trimmed | ||
882 | /// span is outside the bounds of `self`. | ||
883 | // FIXME(SergioBenitez): check that the byte range starts and ends at a | ||
884 | // UTF-8 boundary of the source. otherwise, it's likely that a panic will | ||
885 | // occur elsewhere when the source text is printed. | ||
886 | // FIXME(SergioBenitez): there is no way for the user to know what | ||
887 | // `self.span()` actually maps to, so this method can currently only be | ||
888 | // called blindly. For example, `to_string()` for the character 'c' returns | ||
889 | // "'\u{63}'"; there is no way for the user to know whether the source text | ||
890 | // was 'c' or whether it was '\u{63}'. | ||
891 | pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> { | ||
892 | // HACK(eddyb) something akin to `Option::cloned`, but for `Bound<&T>`. | ||
893 | fn cloned_bound<T: Clone>(bound: Bound<&T>) -> Bound<T> { | ||
894 | match bound { | ||
895 | Bound::Included(x) => Bound::Included(x.clone()), | ||
896 | Bound::Excluded(x) => Bound::Excluded(x.clone()), | ||
897 | Bound::Unbounded => Bound::Unbounded, | ||
898 | } | ||
899 | } | ||
900 | |||
901 | self.0.subspan(cloned_bound(range.start_bound()), cloned_bound(range.end_bound())).map(Span) | ||
902 | } | ||
903 | } | ||
904 | |||
905 | // N.B., the bridge only provides `to_string`, implement `fmt::Display` | ||
906 | // based on it (the reverse of the usual relationship between the two). | ||
907 | // impl ToString for Literal { | ||
908 | // fn to_string(&self) -> String { | ||
909 | // TokenStream::from(TokenTree::from(self.clone())).to_string() | ||
910 | // } | ||
911 | // } | ||
912 | |||
913 | /// Prints the literal as a string that should be losslessly convertible | ||
914 | /// back into the same literal (except for possible rounding for floating point literals). | ||
915 | impl fmt::Display for Literal { | ||
916 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
917 | f.write_str(&self.to_string()) | ||
918 | } | ||
919 | } | ||
920 | |||
921 | impl fmt::Debug for Literal { | ||
922 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
923 | // FIXME(eddyb) `Literal` should not expose internal `Debug` impls. | ||
924 | self.0.fmt(f) | ||
925 | } | ||
926 | } | ||