1 //! Internal interface for communicating between a `proc_macro` client
2 //! (a proc macro crate) and a `proc_macro` server (a compiler front-end).
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).
17 use std::sync::atomic::AtomicUsize;
20 use crate::{Delimiter, Level, LineColumn, Spacing};
22 /// Higher-order macro describing the server RPC API, allowing automatic
23 /// generation of type-safe Rust APIs, both client-side and server-side.
25 /// `with_api!(MySelf, my_self, my_macro)` expands to:
26 /// ```rust,ignore (pseudo-code)
31 /// fn character(ch: char) -> MySelf::Literal;
33 /// fn span(my_self: &MySelf::Literal) -> MySelf::Span;
34 /// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span);
40 /// The first two arguments serve to customize the arguments names
41 /// and argument/return types, to enable several different usecases:
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.
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) => {
56 fn drop($self: $S::TokenStream);
57 fn clone($self: &$S::TokenStream) -> $S::TokenStream;
58 fn new() -> $S::TokenStream;
59 fn is_empty($self: &$S::TokenStream) -> bool;
60 fn from_str(src: &str) -> $S::TokenStream;
61 fn to_string($self: &$S::TokenStream) -> String;
63 tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>,
65 fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter;
68 fn drop($self: $S::TokenStreamBuilder);
69 fn new() -> $S::TokenStreamBuilder;
70 fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream);
71 fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream;
74 fn drop($self: $S::TokenStreamIter);
75 fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter;
77 $self: &mut $S::TokenStreamIter,
78 ) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
81 fn drop($self: $S::Group);
82 fn clone($self: &$S::Group) -> $S::Group;
83 fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group;
84 fn delimiter($self: &$S::Group) -> Delimiter;
85 fn stream($self: &$S::Group) -> $S::TokenStream;
86 fn span($self: &$S::Group) -> $S::Span;
87 fn span_open($self: &$S::Group) -> $S::Span;
88 fn span_close($self: &$S::Group) -> $S::Span;
89 fn set_span($self: &mut $S::Group, span: $S::Span);
92 fn new(ch: char, spacing: Spacing) -> $S::Punct;
93 fn as_char($self: $S::Punct) -> char;
94 fn spacing($self: $S::Punct) -> Spacing;
95 fn span($self: $S::Punct) -> $S::Span;
96 fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct;
99 fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
100 fn span($self: $S::Ident) -> $S::Span;
101 fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident;
104 fn drop($self: $S::Literal);
105 fn clone($self: &$S::Literal) -> $S::Literal;
106 // FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
107 fn debug($self: &$S::Literal) -> String;
108 fn integer(n: &str) -> $S::Literal;
109 fn typed_integer(n: &str, kind: &str) -> $S::Literal;
110 fn float(n: &str) -> $S::Literal;
111 fn f32(n: &str) -> $S::Literal;
112 fn f64(n: &str) -> $S::Literal;
113 fn string(string: &str) -> $S::Literal;
114 fn character(ch: char) -> $S::Literal;
115 fn byte_string(bytes: &[u8]) -> $S::Literal;
116 fn span($self: &$S::Literal) -> $S::Span;
117 fn set_span($self: &mut $S::Literal, span: $S::Span);
122 ) -> Option<$S::Span>;
125 fn drop($self: $S::SourceFile);
126 fn clone($self: &$S::SourceFile) -> $S::SourceFile;
127 fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool;
128 fn path($self: &$S::SourceFile) -> String;
129 fn is_real($self: &$S::SourceFile) -> bool;
132 fn drop($self: $S::MultiSpan);
133 fn new() -> $S::MultiSpan;
134 fn push($self: &mut $S::MultiSpan, span: $S::Span);
137 fn drop($self: $S::Diagnostic);
138 fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic;
140 $self: &mut $S::Diagnostic,
145 fn emit($self: $S::Diagnostic);
148 fn debug($self: $S::Span) -> String;
149 fn def_site() -> $S::Span;
150 fn call_site() -> $S::Span;
151 fn mixed_site() -> $S::Span;
152 fn source_file($self: $S::Span) -> $S::SourceFile;
153 fn parent($self: $S::Span) -> Option<$S::Span>;
154 fn source($self: $S::Span) -> $S::Span;
155 fn start($self: $S::Span) -> LineColumn;
156 fn end($self: $S::Span) -> LineColumn;
157 fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
158 fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
159 fn source_text($self: $S::Span) -> Option<String>;
165 // FIXME(eddyb) this calls `encode` for each argument, but in reverse,
166 // to avoid borrow conflicts from borrows started by `&mut` arguments.
167 macro_rules! reverse_encode {
168 ($writer:ident;) => {};
169 ($writer:ident; $first:ident $(, $rest:ident)*) => {
170 reverse_encode!($writer; $($rest),*);
171 $first.encode(&mut $writer, &mut ());
175 // FIXME(eddyb) this calls `decode` for each argument, but in reverse,
176 // to avoid borrow conflicts from borrows started by `&mut` arguments.
177 macro_rules! reverse_decode {
178 ($reader:ident, $s:ident;) => {};
179 ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => {
180 reverse_decode!($reader, $s; $($rest: $rest_ty),*);
181 let $first = <$first_ty>::decode(&mut $reader, $s);
185 #[allow(unsafe_code)]
187 #[forbid(unsafe_code)]
189 #[allow(unsafe_code)]
191 #[forbid(unsafe_code)]
194 #[forbid(unsafe_code)]
196 #[allow(unsafe_code)]
198 #[forbid(unsafe_code)]
202 pub use rpc::PanicMessage;
203 use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
205 /// An active connection between a server and a client.
206 /// The server creates the bridge (`Bridge::run_server` in `server.rs`),
207 /// then passes it to the client through the function pointer in the `run`
208 /// field of `client::Client`. The client holds its copy of the `Bridge`
209 /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`).
211 pub struct Bridge<'a> {
212 /// Reusable buffer (only `clear`-ed, never shrunk), primarily
213 /// used for making requests, but also for passing input to client.
214 cached_buffer: Buffer<u8>,
216 /// Server-side function that the client uses to make requests.
217 dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
220 impl<'a> !Sync for Bridge<'a> {}
221 impl<'a> !Send for Bridge<'a> {}
223 #[forbid(unsafe_code)]
224 #[allow(non_camel_case_types)]
226 use super::rpc::{DecodeMut, Encode, Reader, Writer};
228 macro_rules! declare_tags {
230 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
233 pub(super) enum $name {
236 rpc_encode_decode!(enum $name { $($method),* });
240 pub(super) enum Method {
243 rpc_encode_decode!(enum Method { $($name(m)),* });
246 with_api!(self, self, declare_tags);
249 /// Helper to wrap associated types to allow trait impl dispatch.
250 /// That is, normally a pair of impls for `T::Foo` and `T::Bar`
251 /// can overlap, but if the impls are, instead, on types like
252 /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't.
255 fn mark(unmarked: Self::Unmarked) -> Self;
258 /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details).
261 fn unmark(self) -> Self::Unmarked;
264 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
265 struct Marked<T, M> {
267 _marker: marker::PhantomData<M>,
270 impl<T, M> Mark for Marked<T, M> {
272 fn mark(unmarked: Self::Unmarked) -> Self {
275 _marker: marker::PhantomData,
279 impl<T, M> Unmark for Marked<T, M> {
281 fn unmark(self) -> Self::Unmarked {
285 impl<T, M> Unmark for &'a Marked<T, M> {
286 type Unmarked = &'a T;
287 fn unmark(self) -> Self::Unmarked {
291 impl<T, M> Unmark for &'a mut Marked<T, M> {
292 type Unmarked = &'a mut T;
293 fn unmark(self) -> Self::Unmarked {
298 impl<T: Mark> Mark for Option<T> {
299 type Unmarked = Option<T::Unmarked>;
300 fn mark(unmarked: Self::Unmarked) -> Self {
301 unmarked.map(T::mark)
304 impl<T: Unmark> Unmark for Option<T> {
305 type Unmarked = Option<T::Unmarked>;
306 fn unmark(self) -> Self::Unmarked {
311 macro_rules! mark_noop {
312 ($($ty:ty),* $(,)?) => {
315 type Unmarked = Self;
316 fn mark(unmarked: Self::Unmarked) -> Self {
320 impl Unmark for $ty {
321 type Unmarked = Self;
322 fn unmark(self) -> Self::Unmarked {
359 rpc_encode_decode!(struct LineColumn { line, column });
368 pub enum TokenTree<G, P, I, L> {
375 impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> {
376 type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
377 fn mark(unmarked: Self::Unmarked) -> Self {
379 TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)),
380 TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)),
381 TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)),
382 TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)),
386 impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> {
387 type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
388 fn unmark(self) -> Self::Unmarked {
390 TokenTree::Group(tt) => TokenTree::Group(tt.unmark()),
391 TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()),
392 TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()),
393 TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()),
399 enum TokenTree<G, P, I, L> {