1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Internal interface for communicating between a `proc_macro` client
12 //! (a proc macro crate) and a `proc_macro` server (a compiler front-end).
14 //! Serialization (with C ABI buffers) and unique integer handles are employed
15 //! to allow safely interfacing between two copies of `proc_macro` built
16 //! (from the same source) by different compilers with potentially mismatching
17 //! Rust ABIs (e.g., stage0/bin/rustc vs stage1/bin/rustc during bootstrap).
27 use std::sync::atomic::AtomicUsize;
30 use {Delimiter, Level, LineColumn, Spacing};
32 /// Higher-order macro describing the server RPC API, allowing automatic
33 /// generation of type-safe Rust APIs, both client-side and server-side.
35 /// `with_api!(MySelf, my_self, my_macro)` expands to:
36 /// ```rust,ignore (pseudo-code)
41 /// fn character(ch: char) -> MySelf::Literal;
43 /// fn span(my_self: &MySelf::Literal) -> MySelf::Span;
44 /// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span);
50 /// The first two arguments serve to customize the arguments names
51 /// and argument/return types, to enable several different usecases:
53 /// If `my_self` is just `self`, then each `fn` signature can be used
54 /// as-is for a method. If it's anything else (`self_` in practice),
55 /// then the signatures don't have a special `self` argument, and
56 /// can, therefore, have a different one introduced.
58 /// If `MySelf` is just `Self`, then the types are only valid inside
59 /// a trait or a trait impl, where the trait has associated types
60 /// for each of the API types. If non-associated types are desired,
61 /// a module name (`self` in practice) can be used instead of `Self`.
62 macro_rules! with_api {
63 ($S:ident, $self:ident, $m:ident) => {
66 fn drop($self: $S::TokenStream);
67 fn clone($self: &$S::TokenStream) -> $S::TokenStream;
68 fn new() -> $S::TokenStream;
69 fn is_empty($self: &$S::TokenStream) -> bool;
70 fn from_str(src: &str) -> $S::TokenStream;
71 fn to_string($self: &$S::TokenStream) -> String;
73 tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>,
75 fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter;
78 fn drop($self: $S::TokenStreamBuilder);
79 fn new() -> $S::TokenStreamBuilder;
80 fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream);
81 fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream;
84 fn drop($self: $S::TokenStreamIter);
85 fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter;
87 $self: &mut $S::TokenStreamIter,
88 ) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
91 fn drop($self: $S::Group);
92 fn clone($self: &$S::Group) -> $S::Group;
93 fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group;
94 fn delimiter($self: &$S::Group) -> Delimiter;
95 fn stream($self: &$S::Group) -> $S::TokenStream;
96 fn span($self: &$S::Group) -> $S::Span;
97 fn span_open($self: &$S::Group) -> $S::Span;
98 fn span_close($self: &$S::Group) -> $S::Span;
99 fn set_span($self: &mut $S::Group, span: $S::Span);
102 fn new(ch: char, spacing: Spacing) -> $S::Punct;
103 fn as_char($self: $S::Punct) -> char;
104 fn spacing($self: $S::Punct) -> Spacing;
105 fn span($self: $S::Punct) -> $S::Span;
106 fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct;
109 fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
110 fn span($self: $S::Ident) -> $S::Span;
111 fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident;
114 fn drop($self: $S::Literal);
115 fn clone($self: &$S::Literal) -> $S::Literal;
116 // FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
117 fn debug($self: &$S::Literal) -> String;
118 fn integer(n: &str) -> $S::Literal;
119 fn typed_integer(n: &str, kind: &str) -> $S::Literal;
120 fn float(n: &str) -> $S::Literal;
121 fn f32(n: &str) -> $S::Literal;
122 fn f64(n: &str) -> $S::Literal;
123 fn string(string: &str) -> $S::Literal;
124 fn character(ch: char) -> $S::Literal;
125 fn byte_string(bytes: &[u8]) -> $S::Literal;
126 fn span($self: &$S::Literal) -> $S::Span;
127 fn set_span($self: &mut $S::Literal, span: $S::Span);
132 ) -> Option<$S::Span>;
135 fn drop($self: $S::SourceFile);
136 fn clone($self: &$S::SourceFile) -> $S::SourceFile;
137 fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool;
138 fn path($self: &$S::SourceFile) -> String;
139 fn is_real($self: &$S::SourceFile) -> bool;
142 fn drop($self: $S::MultiSpan);
143 fn new() -> $S::MultiSpan;
144 fn push($self: &mut $S::MultiSpan, span: $S::Span);
147 fn drop($self: $S::Diagnostic);
148 fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic;
150 $self: &mut $S::Diagnostic,
155 fn emit($self: $S::Diagnostic);
158 fn debug($self: $S::Span) -> String;
159 fn def_site() -> $S::Span;
160 fn call_site() -> $S::Span;
161 fn source_file($self: $S::Span) -> $S::SourceFile;
162 fn parent($self: $S::Span) -> Option<$S::Span>;
163 fn source($self: $S::Span) -> $S::Span;
164 fn start($self: $S::Span) -> LineColumn;
165 fn end($self: $S::Span) -> LineColumn;
166 fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
167 fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
173 // FIXME(eddyb) this calls `encode` for each argument, but in reverse,
174 // to avoid borrow conflicts from borrows started by `&mut` arguments.
175 macro_rules! reverse_encode {
176 ($writer:ident;) => {};
177 ($writer:ident; $first:ident $(, $rest:ident)*) => {
178 reverse_encode!($writer; $($rest),*);
179 $first.encode(&mut $writer, &mut ());
183 // FIXME(eddyb) this calls `decode` for each argument, but in reverse,
184 // to avoid borrow conflicts from borrows started by `&mut` arguments.
185 macro_rules! reverse_decode {
186 ($reader:ident, $s:ident;) => {};
187 ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => {
188 reverse_decode!($reader, $s; $($rest: $rest_ty),*);
189 let $first = <$first_ty>::decode(&mut $reader, $s);
193 #[allow(unsafe_code)]
195 #[forbid(unsafe_code)]
197 #[allow(unsafe_code)]
199 #[forbid(unsafe_code)]
202 #[forbid(unsafe_code)]
204 #[allow(unsafe_code)]
206 #[forbid(unsafe_code)]
209 use self::buffer::Buffer;
210 pub use self::rpc::PanicMessage;
211 use self::rpc::{Decode, DecodeMut, Encode, Reader, Writer};
213 /// An active connection between a server and a client.
214 /// The server creates the bridge (`Bridge::run_server` in `server.rs`),
215 /// then passes it to the client through the function pointer in the `run`
216 /// field of `client::Client`. The client holds its copy of the `Bridge`
217 /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`).
219 pub struct Bridge<'a> {
220 /// Reusable buffer (only `clear`-ed, never shrunk), primarily
221 /// used for making requests, but also for passing input to client.
222 cached_buffer: Buffer<u8>,
224 /// Server-side function that the client uses to make requests.
225 dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
228 impl<'a> !Sync for Bridge<'a> {}
229 impl<'a> !Send for Bridge<'a> {}
231 #[forbid(unsafe_code)]
232 #[allow(non_camel_case_types)]
234 use super::rpc::{DecodeMut, Encode, Reader, Writer};
236 macro_rules! declare_tags {
238 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)*) $(-> $ret_ty:ty)*;)*
241 pub(super) enum $name {
244 rpc_encode_decode!(enum $name { $($method),* });
248 pub(super) enum Method {
251 rpc_encode_decode!(enum Method { $($name(m)),* });
254 with_api!(self, self, declare_tags);
257 /// Helper to wrap associated types to allow trait impl dispatch.
258 /// That is, normally a pair of impls for `T::Foo` and `T::Bar`
259 /// can overlap, but if the impls are, instead, on types like
260 /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't.
263 fn mark(unmarked: Self::Unmarked) -> Self;
266 /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details).
269 fn unmark(self) -> Self::Unmarked;
272 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
273 struct Marked<T, M> {
275 _marker: marker::PhantomData<M>,
278 impl<T, M> Mark for Marked<T, M> {
280 fn mark(unmarked: Self::Unmarked) -> Self {
283 _marker: marker::PhantomData,
287 impl<T, M> Unmark for Marked<T, M> {
289 fn unmark(self) -> Self::Unmarked {
293 impl<T, M> Unmark for &'a Marked<T, M> {
294 type Unmarked = &'a T;
295 fn unmark(self) -> Self::Unmarked {
299 impl<T, M> Unmark for &'a mut Marked<T, M> {
300 type Unmarked = &'a mut T;
301 fn unmark(self) -> Self::Unmarked {
306 impl<T: Mark> Mark for Option<T> {
307 type Unmarked = Option<T::Unmarked>;
308 fn mark(unmarked: Self::Unmarked) -> Self {
309 unmarked.map(T::mark)
312 impl<T: Unmark> Unmark for Option<T> {
313 type Unmarked = Option<T::Unmarked>;
314 fn unmark(self) -> Self::Unmarked {
319 macro_rules! mark_noop {
320 ($($ty:ty),* $(,)*) => {
323 type Unmarked = Self;
324 fn mark(unmarked: Self::Unmarked) -> Self {
328 impl Unmark for $ty {
329 type Unmarked = Self;
330 fn unmark(self) -> Self::Unmarked {
367 rpc_encode_decode!(struct LineColumn { line, column });
376 pub enum TokenTree<G, P, I, L> {
383 impl<G: Mark, P: Mark, I: Mark, L: Mark> Mark for TokenTree<G, P, I, L> {
384 type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
385 fn mark(unmarked: Self::Unmarked) -> Self {
387 TokenTree::Group(tt) => TokenTree::Group(G::mark(tt)),
388 TokenTree::Punct(tt) => TokenTree::Punct(P::mark(tt)),
389 TokenTree::Ident(tt) => TokenTree::Ident(I::mark(tt)),
390 TokenTree::Literal(tt) => TokenTree::Literal(L::mark(tt)),
394 impl<G: Unmark, P: Unmark, I: Unmark, L: Unmark> Unmark for TokenTree<G, P, I, L> {
395 type Unmarked = TokenTree<G::Unmarked, P::Unmarked, I::Unmarked, L::Unmarked>;
396 fn unmark(self) -> Self::Unmarked {
398 TokenTree::Group(tt) => TokenTree::Group(tt.unmark()),
399 TokenTree::Punct(tt) => TokenTree::Punct(tt.unmark()),
400 TokenTree::Ident(tt) => TokenTree::Ident(tt.unmark()),
401 TokenTree::Literal(tt) => TokenTree::Literal(tt.unmark()),
407 enum TokenTree<G, P, I, L> {