]>
Commit | Line | Data |
---|---|---|
a1dfa0c6 XL |
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 | |
0731742a | 7 | //! Rust ABIs (e.g., stage0/bin/rustc vs stage1/bin/rustc during bootstrap). |
a1dfa0c6 XL |
8 | |
9 | #![deny(unsafe_code)] | |
10 | ||
dfeec247 | 11 | use crate::{Delimiter, Level, LineColumn, Spacing}; |
a1dfa0c6 XL |
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; | |
a1dfa0c6 XL |
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! { | |
3dfed10e XL |
55 | FreeFunctions { |
56 | fn drop($self: $S::FreeFunctions); | |
57 | fn track_env_var(var: &str, value: Option<&str>); | |
136023e0 | 58 | fn track_path(path: &str); |
3dfed10e | 59 | }, |
a1dfa0c6 XL |
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; | |
17df50a5 | 111 | fn from_str(s: &str) -> Result<$S::Literal, ()>; |
f9f354fc XL |
112 | fn debug_kind($self: &$S::Literal) -> String; |
113 | fn symbol($self: &$S::Literal) -> String; | |
114 | fn suffix($self: &$S::Literal) -> Option<String>; | |
a1dfa0c6 XL |
115 | fn integer(n: &str) -> $S::Literal; |
116 | fn typed_integer(n: &str, kind: &str) -> $S::Literal; | |
117 | fn float(n: &str) -> $S::Literal; | |
118 | fn f32(n: &str) -> $S::Literal; | |
119 | fn f64(n: &str) -> $S::Literal; | |
120 | fn string(string: &str) -> $S::Literal; | |
121 | fn character(ch: char) -> $S::Literal; | |
122 | fn byte_string(bytes: &[u8]) -> $S::Literal; | |
123 | fn span($self: &$S::Literal) -> $S::Span; | |
124 | fn set_span($self: &mut $S::Literal, span: $S::Span); | |
125 | fn subspan( | |
126 | $self: &$S::Literal, | |
127 | start: Bound<usize>, | |
128 | end: Bound<usize>, | |
129 | ) -> Option<$S::Span>; | |
130 | }, | |
131 | SourceFile { | |
132 | fn drop($self: $S::SourceFile); | |
133 | fn clone($self: &$S::SourceFile) -> $S::SourceFile; | |
134 | fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool; | |
135 | fn path($self: &$S::SourceFile) -> String; | |
136 | fn is_real($self: &$S::SourceFile) -> bool; | |
137 | }, | |
138 | MultiSpan { | |
139 | fn drop($self: $S::MultiSpan); | |
140 | fn new() -> $S::MultiSpan; | |
141 | fn push($self: &mut $S::MultiSpan, span: $S::Span); | |
142 | }, | |
143 | Diagnostic { | |
144 | fn drop($self: $S::Diagnostic); | |
145 | fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic; | |
146 | fn sub( | |
147 | $self: &mut $S::Diagnostic, | |
148 | level: Level, | |
149 | msg: &str, | |
150 | span: $S::MultiSpan, | |
151 | ); | |
152 | fn emit($self: $S::Diagnostic); | |
153 | }, | |
154 | Span { | |
155 | fn debug($self: $S::Span) -> String; | |
156 | fn def_site() -> $S::Span; | |
157 | fn call_site() -> $S::Span; | |
e74abb32 | 158 | fn mixed_site() -> $S::Span; |
a1dfa0c6 XL |
159 | fn source_file($self: $S::Span) -> $S::SourceFile; |
160 | fn parent($self: $S::Span) -> Option<$S::Span>; | |
161 | fn source($self: $S::Span) -> $S::Span; | |
162 | fn start($self: $S::Span) -> LineColumn; | |
163 | fn end($self: $S::Span) -> LineColumn; | |
164 | fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>; | |
165 | fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span; | |
532ac7d7 | 166 | fn source_text($self: $S::Span) -> Option<String>; |
17df50a5 XL |
167 | fn save_span($self: $S::Span) -> usize; |
168 | fn recover_proc_macro_span(id: usize) -> $S::Span; | |
a1dfa0c6 XL |
169 | }, |
170 | } | |
171 | }; | |
172 | } | |
173 | ||
174 | // FIXME(eddyb) this calls `encode` for each argument, but in reverse, | |
175 | // to avoid borrow conflicts from borrows started by `&mut` arguments. | |
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 | #[forbid(unsafe_code)] | |
208 | pub mod server; | |
209 | ||
9fa01778 XL |
210 | use buffer::Buffer; |
211 | pub use rpc::PanicMessage; | |
212 | use rpc::{Decode, DecodeMut, Encode, Reader, Writer}; | |
a1dfa0c6 XL |
213 | |
214 | /// An active connection between a server and a client. | |
215 | /// The server creates the bridge (`Bridge::run_server` in `server.rs`), | |
216 | /// then passes it to the client through the function pointer in the `run` | |
217 | /// field of `client::Client`. The client holds its copy of the `Bridge` | |
218 | /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`). | |
219 | #[repr(C)] | |
220 | pub struct Bridge<'a> { | |
221 | /// Reusable buffer (only `clear`-ed, never shrunk), primarily | |
222 | /// used for making requests, but also for passing input to client. | |
223 | cached_buffer: Buffer<u8>, | |
224 | ||
225 | /// Server-side function that the client uses to make requests. | |
226 | dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>, | |
1b1a35ee XL |
227 | |
228 | /// If 'true', always invoke the default panic hook | |
229 | force_show_panics: bool, | |
a1dfa0c6 XL |
230 | } |
231 | ||
232 | impl<'a> !Sync for Bridge<'a> {} | |
233 | impl<'a> !Send for Bridge<'a> {} | |
234 | ||
235 | #[forbid(unsafe_code)] | |
236 | #[allow(non_camel_case_types)] | |
237 | mod api_tags { | |
238 | use super::rpc::{DecodeMut, Encode, Reader, Writer}; | |
239 | ||
240 | macro_rules! declare_tags { | |
241 | ($($name:ident { | |
9fa01778 XL |
242 | $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)* |
243 | }),* $(,)?) => { | |
a1dfa0c6 XL |
244 | $( |
245 | pub(super) enum $name { | |
246 | $($method),* | |
247 | } | |
248 | rpc_encode_decode!(enum $name { $($method),* }); | |
249 | )* | |
250 | ||
251 | ||
252 | pub(super) enum Method { | |
253 | $($name($name)),* | |
254 | } | |
255 | rpc_encode_decode!(enum Method { $($name(m)),* }); | |
256 | } | |
257 | } | |
258 | with_api!(self, self, declare_tags); | |
259 | } | |
260 | ||
261 | /// Helper to wrap associated types to allow trait impl dispatch. | |
262 | /// That is, normally a pair of impls for `T::Foo` and `T::Bar` | |
263 | /// can overlap, but if the impls are, instead, on types like | |
264 | /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't. | |
265 | trait Mark { | |
266 | type Unmarked; | |
267 | fn mark(unmarked: Self::Unmarked) -> Self; | |
268 | } | |
269 | ||
270 | /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details). | |
271 | trait Unmark { | |
272 | type Unmarked; | |
273 | fn unmark(self) -> Self::Unmarked; | |
274 | } | |
275 | ||
276 | #[derive(Copy, Clone, PartialEq, Eq, Hash)] | |
277 | struct Marked<T, M> { | |
278 | value: T, | |
279 | _marker: marker::PhantomData<M>, | |
280 | } | |
281 | ||
282 | impl<T, M> Mark for Marked<T, M> { | |
283 | type Unmarked = T; | |
284 | fn mark(unmarked: Self::Unmarked) -> Self { | |
dfeec247 | 285 | Marked { value: unmarked, _marker: marker::PhantomData } |
a1dfa0c6 XL |
286 | } |
287 | } | |
288 | impl<T, M> Unmark for Marked<T, M> { | |
289 | type Unmarked = T; | |
290 | fn unmark(self) -> Self::Unmarked { | |
291 | self.value | |
292 | } | |
293 | } | |
294 | impl<T, M> Unmark for &'a Marked<T, M> { | |
295 | type Unmarked = &'a T; | |
296 | fn unmark(self) -> Self::Unmarked { | |
297 | &self.value | |
298 | } | |
299 | } | |
300 | impl<T, M> Unmark for &'a mut Marked<T, M> { | |
301 | type Unmarked = &'a mut T; | |
302 | fn unmark(self) -> Self::Unmarked { | |
303 | &mut self.value | |
304 | } | |
305 | } | |
306 | ||
307 | impl<T: Mark> Mark for Option<T> { | |
308 | type Unmarked = Option<T::Unmarked>; | |
309 | fn mark(unmarked: Self::Unmarked) -> Self { | |
310 | unmarked.map(T::mark) | |
311 | } | |
312 | } | |
313 | impl<T: Unmark> Unmark for Option<T> { | |
314 | type Unmarked = Option<T::Unmarked>; | |
315 | fn unmark(self) -> Self::Unmarked { | |
316 | self.map(T::unmark) | |
317 | } | |
318 | } | |
319 | ||
17df50a5 XL |
320 | impl<T: Mark, E: Mark> Mark for Result<T, E> { |
321 | type Unmarked = Result<T::Unmarked, E::Unmarked>; | |
322 | fn mark(unmarked: Self::Unmarked) -> Self { | |
323 | unmarked.map(T::mark).map_err(E::mark) | |
324 | } | |
325 | } | |
326 | impl<T: Unmark, E: Unmark> Unmark for Result<T, E> { | |
327 | type Unmarked = Result<T::Unmarked, E::Unmarked>; | |
328 | fn unmark(self) -> Self::Unmarked { | |
329 | self.map(T::unmark).map_err(E::unmark) | |
330 | } | |
331 | } | |
332 | ||
a1dfa0c6 | 333 | macro_rules! mark_noop { |
9fa01778 | 334 | ($($ty:ty),* $(,)?) => { |
a1dfa0c6 XL |
335 | $( |
336 | impl Mark for $ty { | |
337 | type Unmarked = Self; | |
338 | fn mark(unmarked: Self::Unmarked) -> Self { | |
339 | unmarked | |
340 | } | |
341 | } | |
342 | impl Unmark for $ty { | |
343 | type Unmarked = Self; | |
344 | fn unmark(self) -> Self::Unmarked { | |
345 | self | |
346 | } | |
347 | } | |
348 | )* | |
349 | } | |
350 | } | |
351 | mark_noop! { | |
352 | (), | |
353 | bool, | |
354 | char, | |
355 | &'a [u8], | |
356 | &'a str, | |
357 | String, | |
17df50a5 | 358 | usize, |
a1dfa0c6 XL |
359 | Delimiter, |
360 | Level, | |
361 | LineColumn, | |
362 | Spacing, | |
363 | Bound<usize>, | |
364 | } | |
365 | ||
366 | rpc_encode_decode!( | |
367 | enum Delimiter { | |
368 | Parenthesis, | |
369 | Brace, | |
370 | Bracket, | |
371 | None, | |
372 | } | |
373 | ); | |
374 | rpc_encode_decode!( | |
375 | enum Level { | |
376 | Error, | |
377 | Warning, | |
378 | Note, | |
379 | Help, | |
380 | } | |
381 | ); | |
382 | rpc_encode_decode!(struct LineColumn { line, column }); | |
383 | rpc_encode_decode!( | |
384 | enum Spacing { | |
385 | Alone, | |
386 | Joint, | |
387 | } | |
388 | ); | |
389 | ||
390 | #[derive(Clone)] | |
391 | pub enum TokenTree<G, P, I, L> { | |
392 | Group(G), | |
393 | Punct(P), | |
394 | Ident(I), | |
395 | Literal(L), | |
396 | } | |
397 | ||
398 | impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> { | |
399 | type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>; | |
400 | fn mark(unmarked: Self::Unmarked) -> Self { | |
401 | match unmarked { | |
402 | TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)), | |
403 | TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)), | |
404 | TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)), | |
405 | TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)), | |
406 | } | |
407 | } | |
408 | } | |
409 | impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> { | |
410 | type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>; | |
411 | fn unmark(self) -> Self::Unmarked { | |
412 | match self { | |
413 | TokenTree::Group(tt) => TokenTree::Group(tt.unmark()), | |
414 | TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()), | |
415 | TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()), | |
416 | TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()), | |
417 | } | |
418 | } | |
419 | } | |
420 | ||
421 | rpc_encode_decode!( | |
422 | enum TokenTree<G, P, I, L> { | |
423 | Group(tt), | |
424 | Punct(tt), | |
425 | Ident(tt), | |
426 | Literal(tt), | |
427 | } | |
428 | ); |