]>
Commit | Line | Data |
---|---|---|
064997fb FG |
1 | //! Internal interface for communicating between a `proc_macro` client |
2 | //! (a proc macro crate) and a `proc_macro` server (a compiler front-end). | |
3 | //! | |
4 | //! Serialization (with C ABI buffers) and unique integer handles are employed | |
5 | //! to allow safely interfacing between two copies of `proc_macro` built | |
6 | //! (from the same source) by different compilers with potentially mismatching | |
7 | //! Rust ABIs (e.g., stage0/bin/rustc vs stage1/bin/rustc during bootstrap). | |
8 | ||
9 | #![deny(unsafe_code)] | |
10 | ||
11 | pub use super::{Delimiter, Level, LineColumn, Spacing}; | |
12 | use std::fmt; | |
13 | use std::hash::Hash; | |
14 | use std::marker; | |
15 | use std::mem; | |
16 | use std::ops::Bound; | |
17 | use std::panic; | |
18 | use std::sync::atomic::AtomicUsize; | |
19 | use std::sync::Once; | |
20 | use std::thread; | |
21 | ||
22 | /// Higher-order macro describing the server RPC API, allowing automatic | |
23 | /// generation of type-safe Rust APIs, both client-side and server-side. | |
24 | /// | |
25 | /// `with_api!(MySelf, my_self, my_macro)` expands to: | |
26 | /// ```rust,ignore (pseudo-code) | |
27 | /// my_macro! { | |
28 | /// // ... | |
29 | /// Literal { | |
30 | /// // ... | |
31 | /// fn character(ch: char) -> MySelf::Literal; | |
32 | /// // ... | |
33 | /// fn span(my_self: &MySelf::Literal) -> MySelf::Span; | |
34 | /// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span); | |
35 | /// }, | |
36 | /// // ... | |
37 | /// } | |
38 | /// ``` | |
39 | /// | |
40 | /// The first two arguments serve to customize the arguments names | |
41 | /// and argument/return types, to enable several different usecases: | |
42 | /// | |
43 | /// If `my_self` is just `self`, then each `fn` signature can be used | |
44 | /// as-is for a method. If it's anything else (`self_` in practice), | |
45 | /// then the signatures don't have a special `self` argument, and | |
46 | /// can, therefore, have a different one introduced. | |
47 | /// | |
48 | /// If `MySelf` is just `Self`, then the types are only valid inside | |
49 | /// a trait or a trait impl, where the trait has associated types | |
50 | /// for each of the API types. If non-associated types are desired, | |
51 | /// a module name (`self` in practice) can be used instead of `Self`. | |
52 | macro_rules! with_api { | |
53 | ($S:ident, $self:ident, $m:ident) => { | |
54 | $m! { | |
55 | FreeFunctions { | |
56 | fn drop($self: $S::FreeFunctions); | |
57 | fn track_env_var(var: &str, value: Option<&str>); | |
58 | fn track_path(path: &str); | |
59 | }, | |
60 | TokenStream { | |
61 | fn drop($self: $S::TokenStream); | |
62 | fn clone($self: &$S::TokenStream) -> $S::TokenStream; | |
63 | fn is_empty($self: &$S::TokenStream) -> bool; | |
64 | fn expand_expr($self: &$S::TokenStream) -> Result<$S::TokenStream, ()>; | |
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 concat_trees( | |
71 | base: Option<$S::TokenStream>, | |
72 | trees: Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>, | |
73 | ) -> $S::TokenStream; | |
74 | fn concat_streams( | |
75 | base: Option<$S::TokenStream>, | |
76 | streams: Vec<$S::TokenStream>, | |
77 | ) -> $S::TokenStream; | |
78 | fn into_trees( | |
79 | $self: $S::TokenStream | |
80 | ) -> Vec<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>; | |
81 | }, | |
82 | Group { | |
83 | fn drop($self: $S::Group); | |
84 | fn clone($self: &$S::Group) -> $S::Group; | |
85 | fn new(delimiter: Delimiter, stream: Option<$S::TokenStream>) -> $S::Group; | |
86 | fn delimiter($self: &$S::Group) -> Delimiter; | |
87 | fn stream($self: &$S::Group) -> $S::TokenStream; | |
88 | fn span($self: &$S::Group) -> $S::Span; | |
89 | fn span_open($self: &$S::Group) -> $S::Span; | |
90 | fn span_close($self: &$S::Group) -> $S::Span; | |
91 | fn set_span($self: &mut $S::Group, span: $S::Span); | |
92 | }, | |
93 | Punct { | |
94 | fn new(ch: char, spacing: Spacing) -> $S::Punct; | |
95 | fn as_char($self: $S::Punct) -> char; | |
96 | fn spacing($self: $S::Punct) -> Spacing; | |
97 | fn span($self: $S::Punct) -> $S::Span; | |
98 | fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct; | |
99 | }, | |
100 | Ident { | |
101 | fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident; | |
102 | fn span($self: $S::Ident) -> $S::Span; | |
103 | fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident; | |
104 | }, | |
105 | Literal { | |
106 | fn drop($self: $S::Literal); | |
107 | fn clone($self: &$S::Literal) -> $S::Literal; | |
108 | fn from_str(s: &str) -> Result<$S::Literal, ()>; | |
109 | fn to_string($self: &$S::Literal) -> String; | |
110 | fn debug_kind($self: &$S::Literal) -> String; | |
111 | fn symbol($self: &$S::Literal) -> String; | |
112 | fn suffix($self: &$S::Literal) -> Option<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 before($self: $S::Span) -> $S::Span; | |
163 | fn after($self: $S::Span) -> $S::Span; | |
164 | fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; | |
165 | fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; | |
166 | fn source_text($self: $S::Span) -> Option<String>; | |
167 | fn save_span($self: $S::Span) -> usize; | |
168 | fn recover_proc_macro_span(id: usize) -> $S::Span; | |
169 | }, | |
170 | } | |
171 | }; | |
172 | } | |
173 | ||
174 | // FIXME(eddyb) this calls `encode` for each argument, but in reverse, | |
175 | // to match the ordering in `reverse_decode`. | |
176 | macro_rules! reverse_encode { | |
177 | ($writer:ident;) => {}; | |
178 | ($writer:ident; $first:ident $(, $rest:ident)*) => { | |
179 | reverse_encode!($writer; $($rest),*); | |
180 | $first.encode(&mut $writer, &mut ()); | |
181 | } | |
182 | } | |
183 | ||
184 | // FIXME(eddyb) this calls `decode` for each argument, but in reverse, | |
185 | // to avoid borrow conflicts from borrows started by `&mut` arguments. | |
186 | macro_rules! reverse_decode { | |
187 | ($reader:ident, $s:ident;) => {}; | |
188 | ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => { | |
189 | reverse_decode!($reader, $s; $($rest: $rest_ty),*); | |
190 | let $first = <$first_ty>::decode(&mut $reader, $s); | |
191 | } | |
192 | } | |
193 | ||
194 | #[allow(unsafe_code)] | |
195 | mod buffer; | |
196 | #[forbid(unsafe_code)] | |
197 | pub mod client; | |
198 | #[allow(unsafe_code)] | |
199 | mod closure; | |
200 | #[forbid(unsafe_code)] | |
201 | mod handle; | |
202 | #[macro_use] | |
203 | #[forbid(unsafe_code)] | |
204 | mod rpc; | |
205 | #[allow(unsafe_code)] | |
206 | mod scoped_cell; | |
207 | #[allow(unsafe_code)] | |
208 | mod selfless_reify; | |
209 | #[forbid(unsafe_code)] | |
210 | pub mod server; | |
211 | ||
212 | use buffer::Buffer; | |
213 | pub use rpc::PanicMessage; | |
214 | use rpc::{Decode, DecodeMut, Encode, Reader, Writer}; | |
215 | ||
216 | /// An active connection between a server and a client. | |
217 | /// The server creates the bridge (`Bridge::run_server` in `server.rs`), | |
218 | /// then passes it to the client through the function pointer in the `run` | |
219 | /// field of `client::Client`. The client holds its copy of the `Bridge` | |
220 | /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`). | |
221 | #[repr(C)] | |
222 | pub struct Bridge<'a> { | |
223 | /// Reusable buffer (only `clear`-ed, never shrunk), primarily | |
224 | /// used for making requests, but also for passing input to client. | |
225 | cached_buffer: Buffer, | |
226 | ||
227 | /// Server-side function that the client uses to make requests. | |
228 | dispatch: closure::Closure<'a, Buffer, Buffer>, | |
229 | ||
230 | /// If 'true', always invoke the default panic hook | |
231 | force_show_panics: bool, | |
232 | ||
233 | // Prevent Send and Sync impls. `!Send`/`!Sync` is the usual way of doing | |
234 | // this, but that requires unstable features. rust-analyzer uses this code | |
235 | // and avoids unstable features. | |
236 | _marker: marker::PhantomData<*mut ()>, | |
237 | } | |
238 | ||
239 | #[forbid(unsafe_code)] | |
240 | #[allow(non_camel_case_types)] | |
241 | mod api_tags { | |
242 | use super::rpc::{DecodeMut, Encode, Reader, Writer}; | |
243 | ||
244 | macro_rules! declare_tags { | |
245 | ($($name:ident { | |
246 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* | |
247 | }),* $(,)?) => { | |
248 | $( | |
249 | pub(super) enum $name { | |
250 | $($method),* | |
251 | } | |
252 | rpc_encode_decode!(enum $name { $($method),* }); | |
253 | )* | |
254 | ||
255 | pub(super) enum Method { | |
256 | $($name($name)),* | |
257 | } | |
258 | rpc_encode_decode!(enum Method { $($name(m)),* }); | |
259 | } | |
260 | } | |
261 | with_api!(self, self, declare_tags); | |
262 | } | |
263 | ||
264 | /// Helper to wrap associated types to allow trait impl dispatch. | |
265 | /// That is, normally a pair of impls for `T::Foo` and `T::Bar` | |
266 | /// can overlap, but if the impls are, instead, on types like | |
267 | /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't. | |
268 | trait Mark { | |
269 | type Unmarked; | |
270 | fn mark(unmarked: Self::Unmarked) -> Self; | |
271 | } | |
272 | ||
273 | /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details). | |
274 | trait Unmark { | |
275 | type Unmarked; | |
276 | fn unmark(self) -> Self::Unmarked; | |
277 | } | |
278 | ||
279 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | |
280 | struct Marked<T, M> { | |
281 | value: T, | |
282 | _marker: marker::PhantomData<M>, | |
283 | } | |
284 | ||
285 | impl<T, M> Mark for Marked<T, M> { | |
286 | type Unmarked = T; | |
287 | fn mark(unmarked: Self::Unmarked) -> Self { | |
288 | Marked { value: unmarked, _marker: marker::PhantomData } | |
289 | } | |
290 | } | |
291 | impl<T, M> Unmark for Marked<T, M> { | |
292 | type Unmarked = T; | |
293 | fn unmark(self) -> Self::Unmarked { | |
294 | self.value | |
295 | } | |
296 | } | |
297 | impl<'a, T, M> Unmark for &'a Marked<T, M> { | |
298 | type Unmarked = &'a T; | |
299 | fn unmark(self) -> Self::Unmarked { | |
300 | &self.value | |
301 | } | |
302 | } | |
303 | impl<'a, T, M> Unmark for &'a mut Marked<T, M> { | |
304 | type Unmarked = &'a mut T; | |
305 | fn unmark(self) -> Self::Unmarked { | |
306 | &mut self.value | |
307 | } | |
308 | } | |
309 | ||
310 | impl<T: Mark> Mark for Vec<T> { | |
311 | type Unmarked = Vec<T::Unmarked>; | |
312 | fn mark(unmarked: Self::Unmarked) -> Self { | |
313 | // Should be a no-op due to std's in-place collect optimizations. | |
314 | unmarked.into_iter().map(T::mark).collect() | |
315 | } | |
316 | } | |
317 | impl<T: Unmark> Unmark for Vec<T> { | |
318 | type Unmarked = Vec<T::Unmarked>; | |
319 | fn unmark(self) -> Self::Unmarked { | |
320 | // Should be a no-op due to std's in-place collect optimizations. | |
321 | self.into_iter().map(T::unmark).collect() | |
322 | } | |
323 | } | |
324 | ||
325 | macro_rules! mark_noop { | |
326 | ($($ty:ty),* $(,)?) => { | |
327 | $( | |
328 | impl Mark for $ty { | |
329 | type Unmarked = Self; | |
330 | fn mark(unmarked: Self::Unmarked) -> Self { | |
331 | unmarked | |
332 | } | |
333 | } | |
334 | impl Unmark for $ty { | |
335 | type Unmarked = Self; | |
336 | fn unmark(self) -> Self::Unmarked { | |
337 | self | |
338 | } | |
339 | } | |
340 | )* | |
341 | } | |
342 | } | |
343 | mark_noop! { | |
344 | (), | |
345 | bool, | |
346 | char, | |
347 | &'_ [u8], | |
348 | &'_ str, | |
349 | String, | |
350 | usize, | |
351 | Delimiter, | |
352 | Level, | |
353 | LineColumn, | |
354 | Spacing, | |
355 | } | |
356 | ||
357 | rpc_encode_decode!( | |
358 | enum Delimiter { | |
359 | Parenthesis, | |
360 | Brace, | |
361 | Bracket, | |
362 | None, | |
363 | } | |
364 | ); | |
365 | rpc_encode_decode!( | |
366 | enum Level { | |
367 | Error, | |
368 | Warning, | |
369 | Note, | |
370 | Help, | |
371 | } | |
372 | ); | |
373 | rpc_encode_decode!(struct LineColumn { line, column }); | |
374 | rpc_encode_decode!( | |
375 | enum Spacing { | |
376 | Alone, | |
377 | Joint, | |
378 | } | |
379 | ); | |
380 | ||
381 | macro_rules! mark_compound { | |
382 | (enum $name:ident <$($T:ident),+> { $($variant:ident $(($field:ident))?),* $(,)? }) => { | |
383 | impl<$($T: Mark),+> Mark for $name <$($T),+> { | |
384 | type Unmarked = $name <$($T::Unmarked),+>; | |
385 | fn mark(unmarked: Self::Unmarked) -> Self { | |
386 | match unmarked { | |
387 | $($name::$variant $(($field))? => { | |
388 | $name::$variant $((Mark::mark($field)))? | |
389 | })* | |
390 | } | |
391 | } | |
392 | } | |
393 | ||
394 | impl<$($T: Unmark),+> Unmark for $name <$($T),+> { | |
395 | type Unmarked = $name <$($T::Unmarked),+>; | |
396 | fn unmark(self) -> Self::Unmarked { | |
397 | match self { | |
398 | $($name::$variant $(($field))? => { | |
399 | $name::$variant $((Unmark::unmark($field)))? | |
400 | })* | |
401 | } | |
402 | } | |
403 | } | |
404 | } | |
405 | } | |
406 | ||
407 | macro_rules! compound_traits { | |
408 | ($($t:tt)*) => { | |
409 | rpc_encode_decode!($($t)*); | |
410 | mark_compound!($($t)*); | |
411 | }; | |
412 | } | |
413 | ||
414 | compound_traits!( | |
415 | enum Bound<T> { | |
416 | Included(x), | |
417 | Excluded(x), | |
418 | Unbounded, | |
419 | } | |
420 | ); | |
421 | ||
422 | compound_traits!( | |
423 | enum Option<T> { | |
424 | Some(t), | |
425 | None, | |
426 | } | |
427 | ); | |
428 | ||
429 | compound_traits!( | |
430 | enum Result<T, E> { | |
431 | Ok(t), | |
432 | Err(e), | |
433 | } | |
434 | ); | |
435 | ||
436 | #[derive(Clone)] | |
437 | pub enum TokenTree<G, P, I, L> { | |
438 | Group(G), | |
439 | Punct(P), | |
440 | Ident(I), | |
441 | Literal(L), | |
442 | } | |
443 | ||
444 | compound_traits!( | |
445 | enum TokenTree<G, P, I, L> { | |
446 | Group(tt), | |
447 | Punct(tt), | |
448 | Ident(tt), | |
449 | Literal(tt), | |
450 | } | |
451 | ); |