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).
11 pub use super::{Delimiter, Level, LineColumn, Spacing};
18 use std::sync::atomic::AtomicUsize;
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::FreeFunctions);
57 fn track_env_var(var: &str, value: Option<&str>);
58 fn track_path(path: &str);
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;
68 tree: TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>,
71 base: Option<$S::TokenStream>,
72 trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>>,
75 base: Option<$S::TokenStream>,
76 streams: Vec<$S::TokenStream>,
79 $self: $S::TokenStream
80 ) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Ident, $S::Literal>>;
83 fn new(string: &str, span: $S::Span, is_raw: bool) -> $S::Ident;
84 fn span($self: $S::Ident) -> $S::Span;
85 fn with_span($self: $S::Ident, span: $S::Span) -> $S::Ident;
88 fn drop($self: $S::Literal);
89 fn clone($self: &$S::Literal) -> $S::Literal;
90 fn from_str(s: &str) -> Result<$S::Literal, ()>;
91 fn to_string($self: &$S::Literal) -> String;
92 fn debug_kind($self: &$S::Literal) -> String;
93 fn symbol($self: &$S::Literal) -> String;
94 fn suffix($self: &$S::Literal) -> Option<String>;
95 fn integer(n: &str) -> $S::Literal;
96 fn typed_integer(n: &str, kind: &str) -> $S::Literal;
97 fn float(n: &str) -> $S::Literal;
98 fn f32(n: &str) -> $S::Literal;
99 fn f64(n: &str) -> $S::Literal;
100 fn string(string: &str) -> $S::Literal;
101 fn character(ch: char) -> $S::Literal;
102 fn byte_string(bytes: &[u8]) -> $S::Literal;
103 fn span($self: &$S::Literal) -> $S::Span;
104 fn set_span($self: &mut $S::Literal, span: $S::Span);
109 ) -> Option<$S::Span>;
112 fn drop($self: $S::SourceFile);
113 fn clone($self: &$S::SourceFile) -> $S::SourceFile;
114 fn eq($self: &$S::SourceFile, other: &$S::SourceFile) -> bool;
115 fn path($self: &$S::SourceFile) -> String;
116 fn is_real($self: &$S::SourceFile) -> bool;
119 fn drop($self: $S::MultiSpan);
120 fn new() -> $S::MultiSpan;
121 fn push($self: &mut $S::MultiSpan, span: $S::Span);
124 fn drop($self: $S::Diagnostic);
125 fn new(level: Level, msg: &str, span: $S::MultiSpan) -> $S::Diagnostic;
127 $self: &mut $S::Diagnostic,
132 fn emit($self: $S::Diagnostic);
135 fn debug($self: $S::Span) -> String;
136 fn source_file($self: $S::Span) -> $S::SourceFile;
137 fn parent($self: $S::Span) -> Option<$S::Span>;
138 fn source($self: $S::Span) -> $S::Span;
139 fn start($self: $S::Span) -> LineColumn;
140 fn end($self: $S::Span) -> LineColumn;
141 fn before($self: $S::Span) -> $S::Span;
142 fn after($self: $S::Span) -> $S::Span;
143 fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
144 fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
145 fn source_text($self: $S::Span) -> Option<String>;
146 fn save_span($self: $S::Span) -> usize;
147 fn recover_proc_macro_span(id: usize) -> $S::Span;
153 // FIXME(eddyb) this calls `encode` for each argument, but in reverse,
154 // to match the ordering in `reverse_decode`.
155 macro_rules! reverse_encode {
156 ($writer:ident;) => {};
157 ($writer:ident; $first:ident $(, $rest:ident)*) => {
158 reverse_encode!($writer; $($rest),*);
159 $first.encode(&mut $writer, &mut ());
163 // FIXME(eddyb) this calls `decode` for each argument, but in reverse,
164 // to avoid borrow conflicts from borrows started by `&mut` arguments.
165 macro_rules! reverse_decode {
166 ($reader:ident, $s:ident;) => {};
167 ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => {
168 reverse_decode!($reader, $s; $($rest: $rest_ty),*);
169 let $first = <$first_ty>::decode(&mut $reader, $s);
173 #[allow(unsafe_code)]
175 #[forbid(unsafe_code)]
177 #[allow(unsafe_code)]
179 #[forbid(unsafe_code)]
182 #[forbid(unsafe_code)]
184 #[allow(unsafe_code)]
186 #[allow(unsafe_code)]
188 #[forbid(unsafe_code)]
192 pub use rpc::PanicMessage;
193 use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
195 /// Configuration for establishing an active connection between a server and a
196 /// client. The server creates the bridge config (`run_server` in `server.rs`),
197 /// then passes it to the client through the function pointer in the `run` field
198 /// of `client::Client`. The client constructs a local `Bridge` from the config
199 /// in TLS during its execution (`Bridge::{enter, with}` in `client.rs`).
201 pub struct BridgeConfig<'a> {
202 /// Buffer used to pass initial input to the client.
205 /// Server-side function that the client uses to make requests.
206 dispatch: closure::Closure<'a, Buffer, Buffer>,
208 /// If 'true', always invoke the default panic hook
209 force_show_panics: bool,
211 // Prevent Send and Sync impls. `!Send`/`!Sync` is the usual way of doing
212 // this, but that requires unstable features. rust-analyzer uses this code
213 // and avoids unstable features.
214 _marker: marker::PhantomData<*mut ()>,
217 #[forbid(unsafe_code)]
218 #[allow(non_camel_case_types)]
220 use super::rpc::{DecodeMut, Encode, Reader, Writer};
222 macro_rules! declare_tags {
224 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
227 pub(super) enum $name {
230 rpc_encode_decode!(enum $name { $($method),* });
233 pub(super) enum Method {
236 rpc_encode_decode!(enum Method { $($name(m)),* });
239 with_api!(self, self, declare_tags);
242 /// Helper to wrap associated types to allow trait impl dispatch.
243 /// That is, normally a pair of impls for `T::Foo` and `T::Bar`
244 /// can overlap, but if the impls are, instead, on types like
245 /// `Marked<T::Foo, Foo>` and `Marked<T::Bar, Bar>`, they can't.
248 fn mark(unmarked: Self::Unmarked) -> Self;
251 /// Unwrap types wrapped by `Mark::mark` (see `Mark` for details).
254 fn unmark(self) -> Self::Unmarked;
257 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
258 struct Marked<T, M> {
260 _marker: marker::PhantomData<M>,
263 impl<T, M> Mark for Marked<T, M> {
265 fn mark(unmarked: Self::Unmarked) -> Self {
266 Marked { value: unmarked, _marker: marker::PhantomData }
269 impl<T, M> Unmark for Marked<T, M> {
271 fn unmark(self) -> Self::Unmarked {
275 impl<'a, T, M> Unmark for &'a Marked<T, M> {
276 type Unmarked = &'a T;
277 fn unmark(self) -> Self::Unmarked {
281 impl<'a, T, M> Unmark for &'a mut Marked<T, M> {
282 type Unmarked = &'a mut T;
283 fn unmark(self) -> Self::Unmarked {
288 impl<T: Mark> Mark for Vec<T> {
289 type Unmarked = Vec<T::Unmarked>;
290 fn mark(unmarked: Self::Unmarked) -> Self {
291 // Should be a no-op due to std's in-place collect optimizations.
292 unmarked.into_iter().map(T::mark).collect()
295 impl<T: Unmark> Unmark for Vec<T> {
296 type Unmarked = Vec<T::Unmarked>;
297 fn unmark(self) -> Self::Unmarked {
298 // Should be a no-op due to std's in-place collect optimizations.
299 self.into_iter().map(T::unmark).collect()
303 macro_rules! mark_noop {
304 ($($ty:ty),* $(,)?) => {
307 type Unmarked = Self;
308 fn mark(unmarked: Self::Unmarked) -> Self {
312 impl Unmark for $ty {
313 type Unmarked = Self;
314 fn unmark(self) -> Self::Unmarked {
352 rpc_encode_decode!(struct LineColumn { line, column });
360 macro_rules! mark_compound {
361 (struct $name:ident <$($T:ident),+> { $($field:ident),* $(,)? }) => {
362 impl<$($T: Mark),+> Mark for $name <$($T),+> {
363 type Unmarked = $name <$($T::Unmarked),+>;
364 fn mark(unmarked: Self::Unmarked) -> Self {
366 $($field: Mark::mark(unmarked.$field)),*
370 impl<$($T: Unmark),+> Unmark for $name <$($T),+> {
371 type Unmarked = $name <$($T::Unmarked),+>;
372 fn unmark(self) -> Self::Unmarked {
374 $($field: Unmark::unmark(self.$field)),*
379 (enum $name:ident <$($T:ident),+> { $($variant:ident $(($field:ident))?),* $(,)? }) => {
380 impl<$($T: Mark),+> Mark for $name <$($T),+> {
381 type Unmarked = $name <$($T::Unmarked),+>;
382 fn mark(unmarked: Self::Unmarked) -> Self {
384 $($name::$variant $(($field))? => {
385 $name::$variant $((Mark::mark($field)))?
390 impl<$($T: Unmark),+> Unmark for $name <$($T),+> {
391 type Unmarked = $name <$($T::Unmarked),+>;
392 fn unmark(self) -> Self::Unmarked {
394 $($name::$variant $(($field))? => {
395 $name::$variant $((Unmark::unmark($field)))?
403 macro_rules! compound_traits {
405 rpc_encode_decode!($($t)*);
406 mark_compound!($($t)*);
432 #[derive(Copy, Clone)]
433 pub struct DelimSpan<Span> {
439 impl<Span: Copy> DelimSpan<Span> {
440 pub fn from_single(span: Span) -> Self {
441 DelimSpan { open: span, close: span, entire: span }
445 compound_traits!(struct DelimSpan<Span> { open, close, entire });
448 pub struct Group<TokenStream, Span> {
449 pub delimiter: Delimiter,
450 pub stream: Option<TokenStream>,
451 pub span: DelimSpan<Span>,
454 compound_traits!(struct Group<TokenStream, Span> { delimiter, stream, span });
457 pub struct Punct<Span> {
463 compound_traits!(struct Punct<Span> { ch, joint, span });
466 pub enum TokenTree<TokenStream, Span, Ident, Literal> {
467 Group(Group<TokenStream, Span>),
474 enum TokenTree<TokenStream, Span, Ident, Literal> {
482 /// Globals provided alongside the initial inputs for a macro expansion.
483 /// Provides values such as spans which are used frequently to avoid RPC.
485 pub struct ExpnGlobals<Span> {
488 pub mixed_site: Span,
492 struct ExpnGlobals<Span> { def_site, call_site, mixed_site }