1#![deny(unsafe_code)]
10
11use std::hash::Hash;
12use std::ops::{Bound, Range};
13use std::sync::Once;
14use std::{fmt, marker, mem, panic, thread};
15
16use crate::{Delimiter, Level, Spacing};
17
18macro_rules! with_api {
49 ($S:ident, $self:ident, $m:ident) => {
50 $m! {
51 FreeFunctions {
52 fn drop($self: $S::FreeFunctions);
53 fn injected_env_var(var: &str) -> Option<String>;
54 fn track_env_var(var: &str, value: Option<&str>);
55 fn track_path(path: &str);
56 fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
57 fn emit_diagnostic(diagnostic: Diagnostic<$S::Span>);
58 },
59 TokenStream {
60 fn drop($self: $S::TokenStream);
61 fn clone($self: &$S::TokenStream) -> $S::TokenStream;
62 fn is_empty($self: &$S::TokenStream) -> bool;
63 fn expand_expr($self: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
64 fn from_str(src: &str) -> $S::TokenStream;
65 fn to_string($self: &$S::TokenStream) -> String;
66 fn from_token_tree(
67 tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol>,
68 ) -> $S::TokenStream;
69 fn concat_trees(
70 base: Option<$S::TokenStream>,
71 trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>,
72 ) -> $S::TokenStream;
73 fn concat_streams(
74 base: Option<$S::TokenStream>,
75 streams: Vec<$S::TokenStream>,
76 ) -> $S::TokenStream;
77 fn into_trees(
78 $self: $S::TokenStream
79 ) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>;
80 },
81 Span {
82 fn debug($self: $S::Span) -> String;
83 fn parent($self: $S::Span) -> Option<$S::Span>;
84 fn source($self: $S::Span) -> $S::Span;
85 fn byte_range($self: $S::Span) -> Range<usize>;
86 fn start($self: $S::Span) -> $S::Span;
87 fn end($self: $S::Span) -> $S::Span;
88 fn line($self: $S::Span) -> usize;
89 fn column($self: $S::Span) -> usize;
90 fn file($self: $S::Span) -> String;
91 fn local_file($self: $S::Span) -> Option<String>;
92 fn join($self: $S::Span, other: $S::Span) -> Option<$S::Span>;
93 fn subspan($self: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
94 fn resolved_at($self: $S::Span, at: $S::Span) -> $S::Span;
95 fn source_text($self: $S::Span) -> Option<String>;
96 fn save_span($self: $S::Span) -> usize;
97 fn recover_proc_macro_span(id: usize) -> $S::Span;
98 },
99 Symbol {
100 fn normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>;
101 },
102 }
103 };
104}
105
106macro_rules! with_api_handle_types {
109 ($m:ident) => {
110 $m! {
111 'owned:
112 FreeFunctions,
113 TokenStream,
114
115 'interned:
116 Span,
117 }
119 };
120}
121
122macro_rules! reverse_encode {
125 ($writer:ident;) => {};
126 ($writer:ident; $first:ident $(, $rest:ident)*) => {
127 reverse_encode!($writer; $($rest),*);
128 $first.encode(&mut $writer, &mut ());
129 }
130}
131
132macro_rules! reverse_decode {
135 ($reader:ident, $s:ident;) => {};
136 ($reader:ident, $s:ident; $first:ident: $first_ty:ty $(, $rest:ident: $rest_ty:ty)*) => {
137 reverse_decode!($reader, $s; $($rest: $rest_ty),*);
138 let $first = <$first_ty>::decode(&mut $reader, $s);
139 }
140}
141
142#[allow(unsafe_code)]
143mod arena;
144#[allow(unsafe_code)]
145mod buffer;
146#[deny(unsafe_code)]
147pub mod client;
148#[allow(unsafe_code)]
149mod closure;
150#[forbid(unsafe_code)]
151mod fxhash;
152#[forbid(unsafe_code)]
153mod handle;
154#[macro_use]
155#[forbid(unsafe_code)]
156mod rpc;
157#[allow(unsafe_code)]
158mod selfless_reify;
159#[forbid(unsafe_code)]
160pub mod server;
161#[allow(unsafe_code)]
162mod symbol;
163
164use buffer::Buffer;
165pub use rpc::PanicMessage;
166use rpc::{Decode, DecodeMut, Encode, Reader, Writer};
167
168#[repr(C)]
174pub struct BridgeConfig<'a> {
175 input: Buffer,
177
178 dispatch: closure::Closure<'a, Buffer, Buffer>,
180
181 force_show_panics: bool,
183
184 _marker: marker::PhantomData<*mut ()>,
188}
189
190#[forbid(unsafe_code)]
191#[allow(non_camel_case_types)]
192mod api_tags {
193 use super::rpc::{DecodeMut, Encode, Reader, Writer};
194
195 macro_rules! declare_tags {
196 ($($name:ident {
197 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
198 }),* $(,)?) => {
199 $(
200 pub(super) enum $name {
201 $($method),*
202 }
203 rpc_encode_decode!(enum $name { $($method),* });
204 )*
205
206 pub(super) enum Method {
207 $($name($name)),*
208 }
209 rpc_encode_decode!(enum Method { $($name(m)),* });
210 }
211 }
212 with_api!(self, self, declare_tags);
213}
214
215trait Mark {
220 type Unmarked;
221 fn mark(unmarked: Self::Unmarked) -> Self;
222}
223
224trait Unmark {
226 type Unmarked;
227 fn unmark(self) -> Self::Unmarked;
228}
229
230#[derive(Copy, Clone, PartialEq, Eq, Hash)]
231struct Marked<T, M> {
232 value: T,
233 _marker: marker::PhantomData<M>,
234}
235
236impl<T, M> Mark for Marked<T, M> {
237 type Unmarked = T;
238 fn mark(unmarked: Self::Unmarked) -> Self {
239 Marked { value: unmarked, _marker: marker::PhantomData }
240 }
241}
242impl<T, M> Unmark for Marked<T, M> {
243 type Unmarked = T;
244 fn unmark(self) -> Self::Unmarked {
245 self.value
246 }
247}
248impl<'a, T, M> Unmark for &'a Marked<T, M> {
249 type Unmarked = &'a T;
250 fn unmark(self) -> Self::Unmarked {
251 &self.value
252 }
253}
254impl<'a, T, M> Unmark for &'a mut Marked<T, M> {
255 type Unmarked = &'a mut T;
256 fn unmark(self) -> Self::Unmarked {
257 &mut self.value
258 }
259}
260
261impl<T: Mark> Mark for Vec<T> {
262 type Unmarked = Vec<T::Unmarked>;
263 fn mark(unmarked: Self::Unmarked) -> Self {
264 unmarked.into_iter().map(T::mark).collect()
266 }
267}
268impl<T: Unmark> Unmark for Vec<T> {
269 type Unmarked = Vec<T::Unmarked>;
270 fn unmark(self) -> Self::Unmarked {
271 self.into_iter().map(T::unmark).collect()
273 }
274}
275
276macro_rules! mark_noop {
277 ($($ty:ty),* $(,)?) => {
278 $(
279 impl Mark for $ty {
280 type Unmarked = Self;
281 fn mark(unmarked: Self::Unmarked) -> Self {
282 unmarked
283 }
284 }
285 impl Unmark for $ty {
286 type Unmarked = Self;
287 fn unmark(self) -> Self::Unmarked {
288 self
289 }
290 }
291 )*
292 }
293}
294mark_noop! {
295 (),
296 bool,
297 char,
298 &'_ [u8],
299 &'_ str,
300 String,
301 u8,
302 usize,
303 Delimiter,
304 LitKind,
305 Level,
306 Spacing,
307}
308
309rpc_encode_decode!(
310 enum Delimiter {
311 Parenthesis,
312 Brace,
313 Bracket,
314 None,
315 }
316);
317rpc_encode_decode!(
318 enum Level {
319 Error,
320 Warning,
321 Note,
322 Help,
323 }
324);
325rpc_encode_decode!(
326 enum Spacing {
327 Alone,
328 Joint,
329 }
330);
331
332#[derive(Copy, Clone, Eq, PartialEq, Debug)]
333pub enum LitKind {
334 Byte,
335 Char,
336 Integer,
337 Float,
338 Str,
339 StrRaw(u8),
340 ByteStr,
341 ByteStrRaw(u8),
342 CStr,
343 CStrRaw(u8),
344 ErrWithGuar,
349}
350
351rpc_encode_decode!(
352 enum LitKind {
353 Byte,
354 Char,
355 Integer,
356 Float,
357 Str,
358 StrRaw(n),
359 ByteStr,
360 ByteStrRaw(n),
361 CStr,
362 CStrRaw(n),
363 ErrWithGuar,
364 }
365);
366
367macro_rules! mark_compound {
368 (struct $name:ident <$($T:ident),+> { $($field:ident),* $(,)? }) => {
369 impl<$($T: Mark),+> Mark for $name <$($T),+> {
370 type Unmarked = $name <$($T::Unmarked),+>;
371 fn mark(unmarked: Self::Unmarked) -> Self {
372 $name {
373 $($field: Mark::mark(unmarked.$field)),*
374 }
375 }
376 }
377
378 impl<$($T: Unmark),+> Unmark for $name <$($T),+> {
379 type Unmarked = $name <$($T::Unmarked),+>;
380 fn unmark(self) -> Self::Unmarked {
381 $name {
382 $($field: Unmark::unmark(self.$field)),*
383 }
384 }
385 }
386 };
387 (enum $name:ident <$($T:ident),+> { $($variant:ident $(($field:ident))?),* $(,)? }) => {
388 impl<$($T: Mark),+> Mark for $name <$($T),+> {
389 type Unmarked = $name <$($T::Unmarked),+>;
390 fn mark(unmarked: Self::Unmarked) -> Self {
391 match unmarked {
392 $($name::$variant $(($field))? => {
393 $name::$variant $((Mark::mark($field)))?
394 })*
395 }
396 }
397 }
398
399 impl<$($T: Unmark),+> Unmark for $name <$($T),+> {
400 type Unmarked = $name <$($T::Unmarked),+>;
401 fn unmark(self) -> Self::Unmarked {
402 match self {
403 $($name::$variant $(($field))? => {
404 $name::$variant $((Unmark::unmark($field)))?
405 })*
406 }
407 }
408 }
409 }
410}
411
412macro_rules! compound_traits {
413 ($($t:tt)*) => {
414 rpc_encode_decode!($($t)*);
415 mark_compound!($($t)*);
416 };
417}
418
419compound_traits!(
420 enum Bound<T> {
421 Included(x),
422 Excluded(x),
423 Unbounded,
424 }
425);
426
427compound_traits!(
428 enum Option<T> {
429 Some(t),
430 None,
431 }
432);
433
434compound_traits!(
435 enum Result<T, E> {
436 Ok(t),
437 Err(e),
438 }
439);
440
441#[derive(Copy, Clone)]
442pub struct DelimSpan<Span> {
443 pub open: Span,
444 pub close: Span,
445 pub entire: Span,
446}
447
448impl<Span: Copy> DelimSpan<Span> {
449 pub fn from_single(span: Span) -> Self {
450 DelimSpan { open: span, close: span, entire: span }
451 }
452}
453
454compound_traits!(struct DelimSpan<Span> { open, close, entire });
455
456#[derive(Clone)]
457pub struct Group<TokenStream, Span> {
458 pub delimiter: Delimiter,
459 pub stream: Option<TokenStream>,
460 pub span: DelimSpan<Span>,
461}
462
463compound_traits!(struct Group<TokenStream, Span> { delimiter, stream, span });
464
465#[derive(Clone)]
466pub struct Punct<Span> {
467 pub ch: u8,
468 pub joint: bool,
469 pub span: Span,
470}
471
472compound_traits!(struct Punct<Span> { ch, joint, span });
473
474#[derive(Copy, Clone, Eq, PartialEq)]
475pub struct Ident<Span, Symbol> {
476 pub sym: Symbol,
477 pub is_raw: bool,
478 pub span: Span,
479}
480
481compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span });
482
483#[derive(Clone, Eq, PartialEq)]
484pub struct Literal<Span, Symbol> {
485 pub kind: LitKind,
486 pub symbol: Symbol,
487 pub suffix: Option<Symbol>,
488 pub span: Span,
489}
490
491compound_traits!(struct Literal<Sp, Sy> { kind, symbol, suffix, span });
492
493#[derive(Clone)]
494pub enum TokenTree<TokenStream, Span, Symbol> {
495 Group(Group<TokenStream, Span>),
496 Punct(Punct<Span>),
497 Ident(Ident<Span, Symbol>),
498 Literal(Literal<Span, Symbol>),
499}
500
501compound_traits!(
502 enum TokenTree<TokenStream, Span, Symbol> {
503 Group(tt),
504 Punct(tt),
505 Ident(tt),
506 Literal(tt),
507 }
508);
509
510#[derive(Clone, Debug)]
511pub struct Diagnostic<Span> {
512 pub level: Level,
513 pub message: String,
514 pub spans: Vec<Span>,
515 pub children: Vec<Diagnostic<Span>>,
516}
517
518compound_traits!(
519 struct Diagnostic<Span> { level, message, spans, children }
520);
521
522#[derive(Clone)]
525pub struct ExpnGlobals<Span> {
526 pub def_site: Span,
527 pub call_site: Span,
528 pub mixed_site: Span,
529}
530
531compound_traits!(
532 struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
533);
534
535compound_traits!(
536 struct Range<T> { start, end }
537);