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};
17
18macro_rules! with_api {
39 ($S:ident, $m:ident) => {
40 $m! {
41 fn injected_env_var(var: &str) -> Option<String>;
42 fn track_env_var(var: &str, value: Option<&str>);
43 fn track_path(path: &str);
44 fn literal_from_str(s: &str) -> Result<Literal<$S::Span, $S::Symbol>, ()>;
45 fn emit_diagnostic(diagnostic: Diagnostic<$S::Span>);
46
47 fn ts_drop(stream: $S::TokenStream);
48 fn ts_clone(stream: &$S::TokenStream) -> $S::TokenStream;
49 fn ts_is_empty(stream: &$S::TokenStream) -> bool;
50 fn ts_expand_expr(stream: &$S::TokenStream) -> Result<$S::TokenStream, ()>;
51 fn ts_from_str(src: &str) -> $S::TokenStream;
52 fn ts_to_string(stream: &$S::TokenStream) -> String;
53 fn ts_from_token_tree(
54 tree: TokenTree<$S::TokenStream, $S::Span, $S::Symbol>,
55 ) -> $S::TokenStream;
56 fn ts_concat_trees(
57 base: Option<$S::TokenStream>,
58 trees: Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>,
59 ) -> $S::TokenStream;
60 fn ts_concat_streams(
61 base: Option<$S::TokenStream>,
62 streams: Vec<$S::TokenStream>,
63 ) -> $S::TokenStream;
64 fn ts_into_trees(
65 stream: $S::TokenStream
66 ) -> Vec<TokenTree<$S::TokenStream, $S::Span, $S::Symbol>>;
67
68 fn span_debug(span: $S::Span) -> String;
69 fn span_parent(span: $S::Span) -> Option<$S::Span>;
70 fn span_source(span: $S::Span) -> $S::Span;
71 fn span_byte_range(span: $S::Span) -> Range<usize>;
72 fn span_start(span: $S::Span) -> $S::Span;
73 fn span_end(span: $S::Span) -> $S::Span;
74 fn span_line(span: $S::Span) -> usize;
75 fn span_column(span: $S::Span) -> usize;
76 fn span_file(span: $S::Span) -> String;
77 fn span_local_file(span: $S::Span) -> Option<String>;
78 fn span_join(span: $S::Span, other: $S::Span) -> Option<$S::Span>;
79 fn span_subspan(span: $S::Span, start: Bound<usize>, end: Bound<usize>) -> Option<$S::Span>;
80 fn span_resolved_at(span: $S::Span, at: $S::Span) -> $S::Span;
81 fn span_source_text(span: $S::Span) -> Option<String>;
82 fn span_save_span(span: $S::Span) -> usize;
83 fn span_recover_proc_macro_span(id: usize) -> $S::Span;
84
85 fn symbol_normalize_and_validate_ident(string: &str) -> Result<$S::Symbol, ()>;
86 }
87 };
88}
89
90pub(crate) struct Methods;
91
92#[allow(unsafe_code)]
93mod arena;
94#[allow(unsafe_code)]
95mod buffer;
96#[deny(unsafe_code)]
97pub mod client;
98#[allow(unsafe_code)]
99mod closure;
100#[forbid(unsafe_code)]
101mod fxhash;
102#[forbid(unsafe_code)]
103mod handle;
104#[macro_use]
105#[forbid(unsafe_code)]
106mod rpc;
107#[allow(unsafe_code)]
108mod selfless_reify;
109#[forbid(unsafe_code)]
110pub mod server;
111#[allow(unsafe_code)]
112mod symbol;
113
114use buffer::Buffer;
115pub use rpc::PanicMessage;
116use rpc::{Decode, Encode};
117
118#[repr(C)]
124pub struct BridgeConfig<'a> {
125 input: Buffer,
127
128 dispatch: closure::Closure<'a, Buffer, Buffer>,
130
131 force_show_panics: bool,
133}
134
135impl !Send for BridgeConfig<'_> {}
136impl !Sync for BridgeConfig<'_> {}
137
138macro_rules! declare_tags {
139 (
140 $(fn $method:ident($($arg:ident: $arg_ty:ty),* $(,)?) $(-> $ret_ty:ty)*;)*
141 ) => {
142 #[allow(non_camel_case_types)]
143 pub(super) enum ApiTags {
144 $($method),*
145 }
146 rpc_encode_decode!(enum ApiTags { $($method),* });
147 }
148}
149with_api!(self, declare_tags);
150
151trait Mark {
156 type Unmarked;
157 fn mark(unmarked: Self::Unmarked) -> Self;
158 fn unmark(self) -> Self::Unmarked;
159}
160
161#[derive(Copy, Clone, PartialEq, Eq, Hash)]
162struct Marked<T, M> {
163 value: T,
164 _marker: marker::PhantomData<M>,
165}
166
167impl<T, M> Mark for Marked<T, M> {
168 type Unmarked = T;
169 fn mark(unmarked: Self::Unmarked) -> Self {
170 Marked { value: unmarked, _marker: marker::PhantomData }
171 }
172 fn unmark(self) -> Self::Unmarked {
173 self.value
174 }
175}
176impl<'a, T, M> Mark for &'a Marked<T, M> {
177 type Unmarked = &'a T;
178 fn mark(_: Self::Unmarked) -> Self {
179 unreachable!()
180 }
181 fn unmark(self) -> Self::Unmarked {
182 &self.value
183 }
184}
185
186impl<T: Mark> Mark for Vec<T> {
187 type Unmarked = Vec<T::Unmarked>;
188 fn mark(unmarked: Self::Unmarked) -> Self {
189 unmarked.into_iter().map(T::mark).collect()
191 }
192 fn unmark(self) -> Self::Unmarked {
193 self.into_iter().map(T::unmark).collect()
195 }
196}
197
198macro_rules! mark_noop {
199 ($($ty:ty),* $(,)?) => {
200 $(
201 impl Mark for $ty {
202 type Unmarked = Self;
203 fn mark(unmarked: Self::Unmarked) -> Self {
204 unmarked
205 }
206 fn unmark(self) -> Self::Unmarked {
207 self
208 }
209 }
210 )*
211 }
212}
213mark_noop! {
214 (),
215 bool,
216 &'_ str,
217 String,
218 u8,
219 usize,
220 Delimiter,
221 LitKind,
222 Level,
223}
224
225rpc_encode_decode!(
226 enum Delimiter {
227 Parenthesis,
228 Brace,
229 Bracket,
230 None,
231 }
232);
233rpc_encode_decode!(
234 enum Level {
235 Error,
236 Warning,
237 Note,
238 Help,
239 }
240);
241
242#[derive(Copy, Clone, Eq, PartialEq, Debug)]
243pub enum LitKind {
244 Byte,
245 Char,
246 Integer,
247 Float,
248 Str,
249 StrRaw(u8),
250 ByteStr,
251 ByteStrRaw(u8),
252 CStr,
253 CStrRaw(u8),
254 ErrWithGuar,
259}
260
261rpc_encode_decode!(
262 enum LitKind {
263 Byte,
264 Char,
265 Integer,
266 Float,
267 Str,
268 StrRaw(n),
269 ByteStr,
270 ByteStrRaw(n),
271 CStr,
272 CStrRaw(n),
273 ErrWithGuar,
274 }
275);
276
277macro_rules! mark_compound {
278 (struct $name:ident <$($T:ident),+> { $($field:ident),* $(,)? }) => {
279 impl<$($T: Mark),+> Mark for $name <$($T),+> {
280 type Unmarked = $name <$($T::Unmarked),+>;
281 fn mark(unmarked: Self::Unmarked) -> Self {
282 $name {
283 $($field: Mark::mark(unmarked.$field)),*
284 }
285 }
286 fn unmark(self) -> Self::Unmarked {
287 $name {
288 $($field: Mark::unmark(self.$field)),*
289 }
290 }
291 }
292 };
293 (enum $name:ident <$($T:ident),+> { $($variant:ident $(($field:ident))?),* $(,)? }) => {
294 impl<$($T: Mark),+> Mark for $name <$($T),+> {
295 type Unmarked = $name <$($T::Unmarked),+>;
296 fn mark(unmarked: Self::Unmarked) -> Self {
297 match unmarked {
298 $($name::$variant $(($field))? => {
299 $name::$variant $((Mark::mark($field)))?
300 })*
301 }
302 }
303 fn unmark(self) -> Self::Unmarked {
304 match self {
305 $($name::$variant $(($field))? => {
306 $name::$variant $((Mark::unmark($field)))?
307 })*
308 }
309 }
310 }
311 }
312}
313
314macro_rules! compound_traits {
315 ($($t:tt)*) => {
316 rpc_encode_decode!($($t)*);
317 mark_compound!($($t)*);
318 };
319}
320
321compound_traits!(
322 enum Bound<T> {
323 Included(x),
324 Excluded(x),
325 Unbounded,
326 }
327);
328
329compound_traits!(
330 enum Option<T> {
331 Some(t),
332 None,
333 }
334);
335
336compound_traits!(
337 enum Result<T, E> {
338 Ok(t),
339 Err(e),
340 }
341);
342
343#[derive(Copy, Clone)]
344pub struct DelimSpan<Span> {
345 pub open: Span,
346 pub close: Span,
347 pub entire: Span,
348}
349
350impl<Span: Copy> DelimSpan<Span> {
351 pub fn from_single(span: Span) -> Self {
352 DelimSpan { open: span, close: span, entire: span }
353 }
354}
355
356compound_traits!(struct DelimSpan<Span> { open, close, entire });
357
358#[derive(Clone)]
359pub struct Group<TokenStream, Span> {
360 pub delimiter: Delimiter,
361 pub stream: Option<TokenStream>,
362 pub span: DelimSpan<Span>,
363}
364
365compound_traits!(struct Group<TokenStream, Span> { delimiter, stream, span });
366
367#[derive(Clone)]
368pub struct Punct<Span> {
369 pub ch: u8,
370 pub joint: bool,
371 pub span: Span,
372}
373
374compound_traits!(struct Punct<Span> { ch, joint, span });
375
376#[derive(Copy, Clone, Eq, PartialEq)]
377pub struct Ident<Span, Symbol> {
378 pub sym: Symbol,
379 pub is_raw: bool,
380 pub span: Span,
381}
382
383compound_traits!(struct Ident<Span, Symbol> { sym, is_raw, span });
384
385#[derive(Clone, Eq, PartialEq)]
386pub struct Literal<Span, Symbol> {
387 pub kind: LitKind,
388 pub symbol: Symbol,
389 pub suffix: Option<Symbol>,
390 pub span: Span,
391}
392
393compound_traits!(struct Literal<Sp, Sy> { kind, symbol, suffix, span });
394
395#[derive(Clone)]
396pub enum TokenTree<TokenStream, Span, Symbol> {
397 Group(Group<TokenStream, Span>),
398 Punct(Punct<Span>),
399 Ident(Ident<Span, Symbol>),
400 Literal(Literal<Span, Symbol>),
401}
402
403compound_traits!(
404 enum TokenTree<TokenStream, Span, Symbol> {
405 Group(tt),
406 Punct(tt),
407 Ident(tt),
408 Literal(tt),
409 }
410);
411
412#[derive(Clone, Debug)]
413pub struct Diagnostic<Span> {
414 pub level: Level,
415 pub message: String,
416 pub spans: Vec<Span>,
417 pub children: Vec<Diagnostic<Span>>,
418}
419
420compound_traits!(
421 struct Diagnostic<Span> { level, message, spans, children }
422);
423
424#[derive(Clone)]
427pub struct ExpnGlobals<Span> {
428 pub def_site: Span,
429 pub call_site: Span,
430 pub mixed_site: Span,
431}
432
433compound_traits!(
434 struct ExpnGlobals<Span> { def_site, call_site, mixed_site }
435);
436
437compound_traits!(
438 struct Range<T> { start, end }
439);