1 //! lib-proc-macro Internal interface for communicating between a `proc_macro` client
3 //! Copy from <https://github.com/rust-lang/rust/blob/6050e523bae6de61de4e060facc43dc512adaccd/src/libproc_macro/bridge/mod.rs>
4 //! augmented with removing unstable features
6 //! Internal interface for communicating between a `proc_macro` client
7 //! (a proc macro crate) and a `proc_macro` server (a compiler front-end).
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).
16 pub use super::{Delimiter, Level, LineColumn, Spacing};
23 use std::sync::atomic::AtomicUsize;
27 /// Higher-order macro describing the server RPC API, allowing automatic
28 /// generation of type-safe Rust APIs, both client-side and server-side.
30 /// `with_api!(MySelf, my_self, my_macro)` expands to:
31 /// ```rust,ignore (pseudo-code)
36 /// fn character(ch: char) -> MySelf::Literal;
38 /// fn span(my_self: &MySelf::Literal) -> MySelf::Span;
39 /// fn set_span(my_self: &mut MySelf::Literal, span: MySelf::Span);
45 /// The first two arguments serve to customize the arguments names
46 /// and argument/return types, to enable several different usecases:
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.
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) => {
61 fn drop($self: $S::FreeFunctions);
62 fn track_env_var(var: &str, value: Option<&str>);
65 fn drop($self: $S::TokenStream);
66 fn clone($self: &$S::TokenStream) -> $S::TokenStream;
67 fn new() -> $S::TokenStream;
68 fn is_empty($self: &$S::TokenStream) -> bool;
69 fn from_str(src: &str) -> $S::TokenStream;
70 fn to_string($self: &$S::TokenStream) -> String;
72 tree: TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>,
74 fn into_iter($self: $S::TokenStream) -> $S::TokenStreamIter;
77 fn drop($self: $S::TokenStreamBuilder);
78 fn new() -> $S::TokenStreamBuilder;
79 fn push($self: &mut $S::TokenStreamBuilder, stream: $S::TokenStream);
80 fn build($self: $S::TokenStreamBuilder) -> $S::TokenStream;
83 fn drop($self: $S::TokenStreamIter);
84 fn clone($self: &$S::TokenStreamIter) -> $S::TokenStreamIter;
86 $self: &mut $S::TokenStreamIter,
87 ) -> Option<TokenTree<$S::Group, $S::Punct, $S::Ident, $S::Literal>>;
90 fn drop($self: $S::Group);
91 fn clone($self: &$S::Group) -> $S::Group;
92 fn new(delimiter: Delimiter, stream: $S::TokenStream) -> $S::Group;
93 fn delimiter($self: &$S::Group) -> Delimiter;
94 fn stream($self: &$S::Group) -> $S::TokenStream;
95 fn span($self: &$S::Group) -> $S::Span;
96 fn span_open($self: &$S::Group) -> $S::Span;
97 fn span_close($self: &$S::Group) -> $S::Span;
98 fn set_span($self: &mut $S::Group, span: $S::Span);
101 fn new(ch: char, spacing: Spacing) -> $S::Punct;
102 fn as_char($self: $S::Punct) -> char;
103 fn spacing($self: $S::Punct) -> Spacing;
104 fn span($self: $S::Punct) -> $S::Span;
105 fn with_span($self: $S::Punct, span: $S::Span) -> $S::Punct;
108 fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
109 fn span($self: $S::Ident) -> $S::Span;
110 fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident;
113 fn drop($self: $S::Literal);
114 fn clone($self: &$S::Literal) -> $S::Literal;
115 fn from_str(s: &str) -> Result<$S::Literal, ()>;
116 fn debug_kind($self: &$S::Literal) -> String;
117 fn symbol($self: &$S::Literal) -> String;
118 fn suffix($self: &$S::Literal) -> Option<String>;
119 fn integer(n: &str) -> $S::Literal;
120 fn typed_integer(n: &str, kind: &str) -> $S::Literal;
121 fn float(n: &str) -> $S::Literal;
122 fn f32(n: &str) -> $S::Literal;
123 fn f64(n: &str) -> $S::Literal;
124 fn string(string: &str) -> $S::Literal;
125 fn character(ch: char) -> $S::Literal;
126 fn byte_string(bytes: &[u8]) -> $S::Literal;
127 fn span($self: &$S::Literal) -> $S::Span;
128 fn set_span($self: &mut $S::Literal, span: $S::Span);
133 ) -> Option<$S::Span>;
136 fn drop($self: $S::SourceFile);
137 fn clone($self: &$S::SourceFile) -> $S::SourceFile;
138 fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool;
139 fn path($self: &$S::SourceFile) -> String;
140 fn is_real($self: &$S::SourceFile) -> bool;
143 fn drop($self: $S::MultiSpan);
144 fn new() -> $S::MultiSpan;
145 fn push($self: &mut $S::MultiSpan, span: $S::Span);
148 fn drop($self: $S::Diagnostic);
149 fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic;
151 $self: &mut $S::Diagnostic,
156 fn emit($self: $S::Diagnostic);
159 fn debug($self: $S::Span) -> String;
160 fn def_site() -> $S::Span;
161 fn call_site() -> $S::Span;
162 fn mixed_site() -> $S::Span;
163 fn source_file($self: $S::Span) -> $S::SourceFile;
164 fn parent($self: $S::Span) -> Option<$S::Span>;
165 fn source($self: $S::Span) -> $S::Span;
166 fn start($self: $S::Span) -> LineColumn;
167 fn end($self: $S::Span) -> LineColumn;
168 fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
169 fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
170 fn source_text($self: $S::Span) -> Option<String>;
176 // FIXME(eddyb) this calls `encode` for each argument, but in reverse,
177 // to avoid borrow conflicts from borrows started by `&mut` arguments.
178 macro_rules! reverse_encode {
179 ($writer:ident;) => {};
180 ($writer:ident; $first:ident $(, $rest:ident)*) => {
181 reverse_encode!($writer; $($rest),*);
182 $first.encode(&mut $writer, &mut ());
186 // FIXME(eddyb) this calls `decode` for each argument, but in reverse,
187 // to avoid borrow conflicts from borrows started by `&mut` arguments.
188 macro_rules! reverse_decode {
189 ($reader:ident, $s:ident;) => {};
190 ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => {
191 reverse_decode!($reader, $s; $($rest: $rest_ty),*);
192 let $first = <$first_ty>::decode(&mut $reader, $s);
196 #[allow(unsafe_code)]
198 #[forbid(unsafe_code)]
200 #[allow(unsafe_code)]
202 #[forbid(unsafe_code)]
205 #[forbid(unsafe_code)]
207 #[allow(unsafe_code)]
209 #[forbid(unsafe_code)]
213 pub use rpc::PanicMessage;
214 use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
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`).
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<u8>,
227 /// Server-side function that the client uses to make requests.
228 dispatch: closure::Closure<'a, Buffer<u8>, Buffer<u8>>,
230 /// If 'true', always invoke the default panic hook
231 force_show_panics: bool,
234 // impl<'a> !Sync for Bridge<'a> {}
235 // impl<'a> !Send for Bridge<'a> {}
237 #[forbid(unsafe_code)]
238 #[allow(non_camel_case_types)]
240 use super::rpc::{DecodeMut, Encode, Reader, Writer};
242 macro_rules! declare_tags {
244 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
247 pub(super) enum $name {
250 rpc_encode_decode!(enum $name { $($method),* });
254 pub(super) enum Method {
257 rpc_encode_decode!(enum Method { $($name(m)),* });
260 with_api!(self, self, declare_tags);
263 /// Helper to wrap associated types to allow trait impl dispatch.
264 /// That is, normally a pair of impls for `T::Foo` and `T::Bar`
265 /// can overlap, but if the impls are, instead, on types like
266 /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't.
269 fn mark(unmarked: Self::Unmarked) -> Self;
272 /// Unwrap types wrapped by `cov_mark::mark` (see `Mark` for details).
275 fn unmark(self) -> Self::Unmarked;
278 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
279 struct Marked<T, M> {
281 _marker: marker::PhantomData<M>,
284 impl<T, M> Mark for Marked<T, M> {
286 fn mark(unmarked: Self::Unmarked) -> Self {
287 Marked { value: unmarked, _marker: marker::PhantomData }
290 impl<T, M> Unmark for Marked<T, M> {
292 fn unmark(self) -> Self::Unmarked {
296 impl<'a, T, M> Unmark for &'a Marked<T, M> {
297 type Unmarked = &'a T;
298 fn unmark(self) -> Self::Unmarked {
302 impl<'a, T, M> Unmark for &'a mut Marked<T, M> {
303 type Unmarked = &'a mut T;
304 fn unmark(self) -> Self::Unmarked {
309 impl<T: Mark> Mark for Option<T> {
310 type Unmarked = Option<T::Unmarked>;
311 fn mark(unmarked: Self::Unmarked) -> Self {
312 unmarked.map(T::mark)
315 impl<T: Unmark> Unmark for Option<T> {
316 type Unmarked = Option<T::Unmarked>;
317 fn unmark(self) -> Self::Unmarked {
321 impl<T: Mark, E: Mark> Mark for Result<T, E> {
322 type Unmarked = Result<T::Unmarked, E::Unmarked>;
323 fn mark(unmarked: Self::Unmarked) -> Self {
324 unmarked.map(T::mark).map_err(E::mark)
327 impl<T: Unmark, E: Unmark> Unmark for Result<T, E> {
328 type Unmarked = Result<T::Unmarked, E::Unmarked>;
329 fn unmark(self) -> Self::Unmarked {
330 self.map(T::unmark).map_err(E::unmark)
334 macro_rules! mark_noop {
335 ($($ty:ty),* $(,)?) => {
338 type Unmarked = Self;
339 fn mark(unmarked: Self::Unmarked) -> Self {
343 impl Unmark for $ty {
344 type Unmarked = Self;
345 fn unmark(self) -> Self::Unmarked {
382 rpc_encode_decode!(struct LineColumn { line, column });
391 pub enum TokenTree<G, P, I, L> {
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 {
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)),
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 {
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()),
422 enum TokenTree<G, P, I, L> {