1 use crate::detection::inside_proc_macro;
2 use crate::{fallback, Delimiter, Punct, Spacing, TokenTree};
3 use std::fmt::{self, Debug, Display};
4 use std::iter::FromIterator;
5 use std::ops::RangeBounds;
6 use std::panic;
7 #[cfg(super_unstable)]
8 use std::path::PathBuf;
9 use std::str::FromStr;
10
11 #[derive(Clone)]
12 pub(crate) enum TokenStream {
13 Compiler(DeferredTokenStream),
14 Fallback(fallback::TokenStream),
15 }
16
17 // Work around https://github.com/rust-lang/rust/issues/65080.
18 // In `impl Extend<TokenTree> for TokenStream` which is used heavily by quote,
19 // we hold on to the appended tokens and do proc_macro::TokenStream::extend as
20 // late as possible to batch together consecutive uses of the Extend impl.
21 #[derive(Clone)]
22 pub(crate) struct DeferredTokenStream {
23 stream: proc_macro::TokenStream,
24 extra: Vec<proc_macro::TokenTree>,
25 }
26
27 pub(crate) enum LexError {
28 Compiler(proc_macro::LexError),
29 Fallback(fallback::LexError),
30 }
31
mismatch() -> !32 fn mismatch() -> ! {
33 panic!("stable/nightly mismatch")
34 }
35
36 impl DeferredTokenStream {
new(stream: proc_macro::TokenStream) -> Self37 fn new(stream: proc_macro::TokenStream) -> Self {
38 DeferredTokenStream {
39 stream,
40 extra: Vec::new(),
41 }
42 }
43
is_empty(&self) -> bool44 fn is_empty(&self) -> bool {
45 self.stream.is_empty() && self.extra.is_empty()
46 }
47
evaluate_now(&mut self)48 fn evaluate_now(&mut self) {
49 // If-check provides a fast short circuit for the common case of `extra`
50 // being empty, which saves a round trip over the proc macro bridge.
51 // Improves macro expansion time in winrt by 6% in debug mode.
52 if !self.extra.is_empty() {
53 self.stream.extend(self.extra.drain(..));
54 }
55 }
56
into_token_stream(mut self) -> proc_macro::TokenStream57 fn into_token_stream(mut self) -> proc_macro::TokenStream {
58 self.evaluate_now();
59 self.stream
60 }
61 }
62
63 impl TokenStream {
new() -> TokenStream64 pub fn new() -> TokenStream {
65 if inside_proc_macro() {
66 TokenStream::Compiler(DeferredTokenStream::new(proc_macro::TokenStream::new()))
67 } else {
68 TokenStream::Fallback(fallback::TokenStream::new())
69 }
70 }
71
is_empty(&self) -> bool72 pub fn is_empty(&self) -> bool {
73 match self {
74 TokenStream::Compiler(tts) => tts.is_empty(),
75 TokenStream::Fallback(tts) => tts.is_empty(),
76 }
77 }
78
unwrap_nightly(self) -> proc_macro::TokenStream79 fn unwrap_nightly(self) -> proc_macro::TokenStream {
80 match self {
81 TokenStream::Compiler(s) => s.into_token_stream(),
82 TokenStream::Fallback(_) => mismatch(),
83 }
84 }
85
unwrap_stable(self) -> fallback::TokenStream86 fn unwrap_stable(self) -> fallback::TokenStream {
87 match self {
88 TokenStream::Compiler(_) => mismatch(),
89 TokenStream::Fallback(s) => s,
90 }
91 }
92 }
93
94 impl FromStr for TokenStream {
95 type Err = LexError;
96
from_str(src: &str) -> Result<TokenStream, LexError>97 fn from_str(src: &str) -> Result<TokenStream, LexError> {
98 if inside_proc_macro() {
99 Ok(TokenStream::Compiler(DeferredTokenStream::new(
100 proc_macro_parse(src)?,
101 )))
102 } else {
103 Ok(TokenStream::Fallback(src.parse()?))
104 }
105 }
106 }
107
108 // Work around https://github.com/rust-lang/rust/issues/58736.
proc_macro_parse(src: &str) -> Result<proc_macro::TokenStream, LexError>109 fn proc_macro_parse(src: &str) -> Result<proc_macro::TokenStream, LexError> {
110 panic::catch_unwind(|| src.parse().map_err(LexError::Compiler))
111 .unwrap_or(Err(LexError::Fallback(fallback::LexError)))
112 }
113
114 impl Display for TokenStream {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result115 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
116 match self {
117 TokenStream::Compiler(tts) => Display::fmt(&tts.clone().into_token_stream(), f),
118 TokenStream::Fallback(tts) => Display::fmt(tts, f),
119 }
120 }
121 }
122
123 impl From<proc_macro::TokenStream> for TokenStream {
from(inner: proc_macro::TokenStream) -> TokenStream124 fn from(inner: proc_macro::TokenStream) -> TokenStream {
125 TokenStream::Compiler(DeferredTokenStream::new(inner))
126 }
127 }
128
129 impl From<TokenStream> for proc_macro::TokenStream {
from(inner: TokenStream) -> proc_macro::TokenStream130 fn from(inner: TokenStream) -> proc_macro::TokenStream {
131 match inner {
132 TokenStream::Compiler(inner) => inner.into_token_stream(),
133 TokenStream::Fallback(inner) => inner.to_string().parse().unwrap(),
134 }
135 }
136 }
137
138 impl From<fallback::TokenStream> for TokenStream {
from(inner: fallback::TokenStream) -> TokenStream139 fn from(inner: fallback::TokenStream) -> TokenStream {
140 TokenStream::Fallback(inner)
141 }
142 }
143
144 // Assumes inside_proc_macro().
into_compiler_token(token: TokenTree) -> proc_macro::TokenTree145 fn into_compiler_token(token: TokenTree) -> proc_macro::TokenTree {
146 match token {
147 TokenTree::Group(tt) => tt.inner.unwrap_nightly().into(),
148 TokenTree::Punct(tt) => {
149 let spacing = match tt.spacing() {
150 Spacing::Joint => proc_macro::Spacing::Joint,
151 Spacing::Alone => proc_macro::Spacing::Alone,
152 };
153 let mut op = proc_macro::Punct::new(tt.as_char(), spacing);
154 op.set_span(tt.span().inner.unwrap_nightly());
155 op.into()
156 }
157 TokenTree::Ident(tt) => tt.inner.unwrap_nightly().into(),
158 TokenTree::Literal(tt) => tt.inner.unwrap_nightly().into(),
159 }
160 }
161
162 impl From<TokenTree> for TokenStream {
from(token: TokenTree) -> TokenStream163 fn from(token: TokenTree) -> TokenStream {
164 if inside_proc_macro() {
165 TokenStream::Compiler(DeferredTokenStream::new(into_compiler_token(token).into()))
166 } else {
167 TokenStream::Fallback(token.into())
168 }
169 }
170 }
171
172 impl FromIterator<TokenTree> for TokenStream {
from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self173 fn from_iter<I: IntoIterator<Item = TokenTree>>(trees: I) -> Self {
174 if inside_proc_macro() {
175 TokenStream::Compiler(DeferredTokenStream::new(
176 trees.into_iter().map(into_compiler_token).collect(),
177 ))
178 } else {
179 TokenStream::Fallback(trees.into_iter().collect())
180 }
181 }
182 }
183
184 impl FromIterator<TokenStream> for TokenStream {
from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self185 fn from_iter<I: IntoIterator<Item = TokenStream>>(streams: I) -> Self {
186 let mut streams = streams.into_iter();
187 match streams.next() {
188 Some(TokenStream::Compiler(mut first)) => {
189 first.evaluate_now();
190 first.stream.extend(streams.map(|s| match s {
191 TokenStream::Compiler(s) => s.into_token_stream(),
192 TokenStream::Fallback(_) => mismatch(),
193 }));
194 TokenStream::Compiler(first)
195 }
196 Some(TokenStream::Fallback(mut first)) => {
197 first.extend(streams.map(|s| match s {
198 TokenStream::Fallback(s) => s,
199 TokenStream::Compiler(_) => mismatch(),
200 }));
201 TokenStream::Fallback(first)
202 }
203 None => TokenStream::new(),
204 }
205 }
206 }
207
208 impl Extend<TokenTree> for TokenStream {
extend<I: IntoIterator<Item = TokenTree>>(&mut self, stream: I)209 fn extend<I: IntoIterator<Item = TokenTree>>(&mut self, stream: I) {
210 match self {
211 TokenStream::Compiler(tts) => {
212 // Here is the reason for DeferredTokenStream.
213 for token in stream {
214 tts.extra.push(into_compiler_token(token));
215 }
216 }
217 TokenStream::Fallback(tts) => tts.extend(stream),
218 }
219 }
220 }
221
222 impl Extend<TokenStream> for TokenStream {
extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I)223 fn extend<I: IntoIterator<Item = TokenStream>>(&mut self, streams: I) {
224 match self {
225 TokenStream::Compiler(tts) => {
226 tts.evaluate_now();
227 tts.stream
228 .extend(streams.into_iter().map(TokenStream::unwrap_nightly));
229 }
230 TokenStream::Fallback(tts) => {
231 tts.extend(streams.into_iter().map(TokenStream::unwrap_stable));
232 }
233 }
234 }
235 }
236
237 impl Debug for TokenStream {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result238 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
239 match self {
240 TokenStream::Compiler(tts) => Debug::fmt(&tts.clone().into_token_stream(), f),
241 TokenStream::Fallback(tts) => Debug::fmt(tts, f),
242 }
243 }
244 }
245
246 impl From<proc_macro::LexError> for LexError {
from(e: proc_macro::LexError) -> LexError247 fn from(e: proc_macro::LexError) -> LexError {
248 LexError::Compiler(e)
249 }
250 }
251
252 impl From<fallback::LexError> for LexError {
from(e: fallback::LexError) -> LexError253 fn from(e: fallback::LexError) -> LexError {
254 LexError::Fallback(e)
255 }
256 }
257
258 impl Debug for LexError {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result259 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
260 match self {
261 LexError::Compiler(e) => Debug::fmt(e, f),
262 LexError::Fallback(e) => Debug::fmt(e, f),
263 }
264 }
265 }
266
267 #[derive(Clone)]
268 pub(crate) enum TokenTreeIter {
269 Compiler(proc_macro::token_stream::IntoIter),
270 Fallback(fallback::TokenTreeIter),
271 }
272
273 impl IntoIterator for TokenStream {
274 type Item = TokenTree;
275 type IntoIter = TokenTreeIter;
276
into_iter(self) -> TokenTreeIter277 fn into_iter(self) -> TokenTreeIter {
278 match self {
279 TokenStream::Compiler(tts) => {
280 TokenTreeIter::Compiler(tts.into_token_stream().into_iter())
281 }
282 TokenStream::Fallback(tts) => TokenTreeIter::Fallback(tts.into_iter()),
283 }
284 }
285 }
286
287 impl Iterator for TokenTreeIter {
288 type Item = TokenTree;
289
next(&mut self) -> Option<TokenTree>290 fn next(&mut self) -> Option<TokenTree> {
291 let token = match self {
292 TokenTreeIter::Compiler(iter) => iter.next()?,
293 TokenTreeIter::Fallback(iter) => return iter.next(),
294 };
295 Some(match token {
296 proc_macro::TokenTree::Group(tt) => crate::Group::_new(Group::Compiler(tt)).into(),
297 proc_macro::TokenTree::Punct(tt) => {
298 let spacing = match tt.spacing() {
299 proc_macro::Spacing::Joint => Spacing::Joint,
300 proc_macro::Spacing::Alone => Spacing::Alone,
301 };
302 let mut o = Punct::new(tt.as_char(), spacing);
303 o.set_span(crate::Span::_new(Span::Compiler(tt.span())));
304 o.into()
305 }
306 proc_macro::TokenTree::Ident(s) => crate::Ident::_new(Ident::Compiler(s)).into(),
307 proc_macro::TokenTree::Literal(l) => crate::Literal::_new(Literal::Compiler(l)).into(),
308 })
309 }
310
size_hint(&self) -> (usize, Option<usize>)311 fn size_hint(&self) -> (usize, Option<usize>) {
312 match self {
313 TokenTreeIter::Compiler(tts) => tts.size_hint(),
314 TokenTreeIter::Fallback(tts) => tts.size_hint(),
315 }
316 }
317 }
318
319 impl Debug for TokenTreeIter {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result320 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
321 f.debug_struct("TokenTreeIter").finish()
322 }
323 }
324
325 #[derive(Clone, PartialEq, Eq)]
326 #[cfg(super_unstable)]
327 pub(crate) enum SourceFile {
328 Compiler(proc_macro::SourceFile),
329 Fallback(fallback::SourceFile),
330 }
331
332 #[cfg(super_unstable)]
333 impl SourceFile {
nightly(sf: proc_macro::SourceFile) -> Self334 fn nightly(sf: proc_macro::SourceFile) -> Self {
335 SourceFile::Compiler(sf)
336 }
337
338 /// Get the path to this source file as a string.
path(&self) -> PathBuf339 pub fn path(&self) -> PathBuf {
340 match self {
341 SourceFile::Compiler(a) => a.path(),
342 SourceFile::Fallback(a) => a.path(),
343 }
344 }
345
is_real(&self) -> bool346 pub fn is_real(&self) -> bool {
347 match self {
348 SourceFile::Compiler(a) => a.is_real(),
349 SourceFile::Fallback(a) => a.is_real(),
350 }
351 }
352 }
353
354 #[cfg(super_unstable)]
355 impl Debug for SourceFile {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result356 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
357 match self {
358 SourceFile::Compiler(a) => Debug::fmt(a, f),
359 SourceFile::Fallback(a) => Debug::fmt(a, f),
360 }
361 }
362 }
363
364 #[cfg(any(super_unstable, feature = "span-locations"))]
365 pub(crate) struct LineColumn {
366 pub line: usize,
367 pub column: usize,
368 }
369
370 #[derive(Copy, Clone)]
371 pub(crate) enum Span {
372 Compiler(proc_macro::Span),
373 Fallback(fallback::Span),
374 }
375
376 impl Span {
call_site() -> Span377 pub fn call_site() -> Span {
378 if inside_proc_macro() {
379 Span::Compiler(proc_macro::Span::call_site())
380 } else {
381 Span::Fallback(fallback::Span::call_site())
382 }
383 }
384
385 #[cfg(hygiene)]
mixed_site() -> Span386 pub fn mixed_site() -> Span {
387 if inside_proc_macro() {
388 Span::Compiler(proc_macro::Span::mixed_site())
389 } else {
390 Span::Fallback(fallback::Span::mixed_site())
391 }
392 }
393
394 #[cfg(super_unstable)]
def_site() -> Span395 pub fn def_site() -> Span {
396 if inside_proc_macro() {
397 Span::Compiler(proc_macro::Span::def_site())
398 } else {
399 Span::Fallback(fallback::Span::def_site())
400 }
401 }
402
resolved_at(&self, other: Span) -> Span403 pub fn resolved_at(&self, other: Span) -> Span {
404 match (self, other) {
405 #[cfg(hygiene)]
406 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.resolved_at(b)),
407
408 // Name resolution affects semantics, but location is only cosmetic
409 #[cfg(not(hygiene))]
410 (Span::Compiler(_), Span::Compiler(_)) => other,
411
412 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.resolved_at(b)),
413 _ => mismatch(),
414 }
415 }
416
located_at(&self, other: Span) -> Span417 pub fn located_at(&self, other: Span) -> Span {
418 match (self, other) {
419 #[cfg(hygiene)]
420 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.located_at(b)),
421
422 // Name resolution affects semantics, but location is only cosmetic
423 #[cfg(not(hygiene))]
424 (Span::Compiler(_), Span::Compiler(_)) => *self,
425
426 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.located_at(b)),
427 _ => mismatch(),
428 }
429 }
430
unwrap(self) -> proc_macro::Span431 pub fn unwrap(self) -> proc_macro::Span {
432 match self {
433 Span::Compiler(s) => s,
434 Span::Fallback(_) => panic!("proc_macro::Span is only available in procedural macros"),
435 }
436 }
437
438 #[cfg(super_unstable)]
source_file(&self) -> SourceFile439 pub fn source_file(&self) -> SourceFile {
440 match self {
441 Span::Compiler(s) => SourceFile::nightly(s.source_file()),
442 Span::Fallback(s) => SourceFile::Fallback(s.source_file()),
443 }
444 }
445
446 #[cfg(any(super_unstable, feature = "span-locations"))]
start(&self) -> LineColumn447 pub fn start(&self) -> LineColumn {
448 match self {
449 #[cfg(proc_macro_span)]
450 Span::Compiler(s) => {
451 let proc_macro::LineColumn { line, column } = s.start();
452 LineColumn { line, column }
453 }
454 #[cfg(not(proc_macro_span))]
455 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
456 Span::Fallback(s) => {
457 let fallback::LineColumn { line, column } = s.start();
458 LineColumn { line, column }
459 }
460 }
461 }
462
463 #[cfg(any(super_unstable, feature = "span-locations"))]
end(&self) -> LineColumn464 pub fn end(&self) -> LineColumn {
465 match self {
466 #[cfg(proc_macro_span)]
467 Span::Compiler(s) => {
468 let proc_macro::LineColumn { line, column } = s.end();
469 LineColumn { line, column }
470 }
471 #[cfg(not(proc_macro_span))]
472 Span::Compiler(_) => LineColumn { line: 0, column: 0 },
473 Span::Fallback(s) => {
474 let fallback::LineColumn { line, column } = s.end();
475 LineColumn { line, column }
476 }
477 }
478 }
479
join(&self, other: Span) -> Option<Span>480 pub fn join(&self, other: Span) -> Option<Span> {
481 let ret = match (self, other) {
482 #[cfg(proc_macro_span)]
483 (Span::Compiler(a), Span::Compiler(b)) => Span::Compiler(a.join(b)?),
484 (Span::Fallback(a), Span::Fallback(b)) => Span::Fallback(a.join(b)?),
485 _ => return None,
486 };
487 Some(ret)
488 }
489
490 #[cfg(super_unstable)]
eq(&self, other: &Span) -> bool491 pub fn eq(&self, other: &Span) -> bool {
492 match (self, other) {
493 (Span::Compiler(a), Span::Compiler(b)) => a.eq(b),
494 (Span::Fallback(a), Span::Fallback(b)) => a.eq(b),
495 _ => false,
496 }
497 }
498
unwrap_nightly(self) -> proc_macro::Span499 fn unwrap_nightly(self) -> proc_macro::Span {
500 match self {
501 Span::Compiler(s) => s,
502 Span::Fallback(_) => mismatch(),
503 }
504 }
505 }
506
507 impl From<proc_macro::Span> for crate::Span {
from(proc_span: proc_macro::Span) -> crate::Span508 fn from(proc_span: proc_macro::Span) -> crate::Span {
509 crate::Span::_new(Span::Compiler(proc_span))
510 }
511 }
512
513 impl From<fallback::Span> for Span {
from(inner: fallback::Span) -> Span514 fn from(inner: fallback::Span) -> Span {
515 Span::Fallback(inner)
516 }
517 }
518
519 impl Debug for Span {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result520 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
521 match self {
522 Span::Compiler(s) => Debug::fmt(s, f),
523 Span::Fallback(s) => Debug::fmt(s, f),
524 }
525 }
526 }
527
debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span)528 pub(crate) fn debug_span_field_if_nontrivial(debug: &mut fmt::DebugStruct, span: Span) {
529 match span {
530 Span::Compiler(s) => {
531 debug.field("span", &s);
532 }
533 Span::Fallback(s) => fallback::debug_span_field_if_nontrivial(debug, s),
534 }
535 }
536
537 #[derive(Clone)]
538 pub(crate) enum Group {
539 Compiler(proc_macro::Group),
540 Fallback(fallback::Group),
541 }
542
543 impl Group {
new(delimiter: Delimiter, stream: TokenStream) -> Group544 pub fn new(delimiter: Delimiter, stream: TokenStream) -> Group {
545 match stream {
546 TokenStream::Compiler(tts) => {
547 let delimiter = match delimiter {
548 Delimiter::Parenthesis => proc_macro::Delimiter::Parenthesis,
549 Delimiter::Bracket => proc_macro::Delimiter::Bracket,
550 Delimiter::Brace => proc_macro::Delimiter::Brace,
551 Delimiter::None => proc_macro::Delimiter::None,
552 };
553 Group::Compiler(proc_macro::Group::new(delimiter, tts.into_token_stream()))
554 }
555 TokenStream::Fallback(stream) => {
556 Group::Fallback(fallback::Group::new(delimiter, stream))
557 }
558 }
559 }
560
delimiter(&self) -> Delimiter561 pub fn delimiter(&self) -> Delimiter {
562 match self {
563 Group::Compiler(g) => match g.delimiter() {
564 proc_macro::Delimiter::Parenthesis => Delimiter::Parenthesis,
565 proc_macro::Delimiter::Bracket => Delimiter::Bracket,
566 proc_macro::Delimiter::Brace => Delimiter::Brace,
567 proc_macro::Delimiter::None => Delimiter::None,
568 },
569 Group::Fallback(g) => g.delimiter(),
570 }
571 }
572
stream(&self) -> TokenStream573 pub fn stream(&self) -> TokenStream {
574 match self {
575 Group::Compiler(g) => TokenStream::Compiler(DeferredTokenStream::new(g.stream())),
576 Group::Fallback(g) => TokenStream::Fallback(g.stream()),
577 }
578 }
579
span(&self) -> Span580 pub fn span(&self) -> Span {
581 match self {
582 Group::Compiler(g) => Span::Compiler(g.span()),
583 Group::Fallback(g) => Span::Fallback(g.span()),
584 }
585 }
586
span_open(&self) -> Span587 pub fn span_open(&self) -> Span {
588 match self {
589 #[cfg(proc_macro_span)]
590 Group::Compiler(g) => Span::Compiler(g.span_open()),
591 #[cfg(not(proc_macro_span))]
592 Group::Compiler(g) => Span::Compiler(g.span()),
593 Group::Fallback(g) => Span::Fallback(g.span_open()),
594 }
595 }
596
span_close(&self) -> Span597 pub fn span_close(&self) -> Span {
598 match self {
599 #[cfg(proc_macro_span)]
600 Group::Compiler(g) => Span::Compiler(g.span_close()),
601 #[cfg(not(proc_macro_span))]
602 Group::Compiler(g) => Span::Compiler(g.span()),
603 Group::Fallback(g) => Span::Fallback(g.span_close()),
604 }
605 }
606
set_span(&mut self, span: Span)607 pub fn set_span(&mut self, span: Span) {
608 match (self, span) {
609 (Group::Compiler(g), Span::Compiler(s)) => g.set_span(s),
610 (Group::Fallback(g), Span::Fallback(s)) => g.set_span(s),
611 _ => mismatch(),
612 }
613 }
614
unwrap_nightly(self) -> proc_macro::Group615 fn unwrap_nightly(self) -> proc_macro::Group {
616 match self {
617 Group::Compiler(g) => g,
618 Group::Fallback(_) => mismatch(),
619 }
620 }
621 }
622
623 impl From<fallback::Group> for Group {
from(g: fallback::Group) -> Self624 fn from(g: fallback::Group) -> Self {
625 Group::Fallback(g)
626 }
627 }
628
629 impl Display for Group {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result630 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
631 match self {
632 Group::Compiler(group) => Display::fmt(group, formatter),
633 Group::Fallback(group) => Display::fmt(group, formatter),
634 }
635 }
636 }
637
638 impl Debug for Group {
fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result639 fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
640 match self {
641 Group::Compiler(group) => Debug::fmt(group, formatter),
642 Group::Fallback(group) => Debug::fmt(group, formatter),
643 }
644 }
645 }
646
647 #[derive(Clone)]
648 pub(crate) enum Ident {
649 Compiler(proc_macro::Ident),
650 Fallback(fallback::Ident),
651 }
652
653 impl Ident {
new(string: &str, span: Span) -> Ident654 pub fn new(string: &str, span: Span) -> Ident {
655 match span {
656 Span::Compiler(s) => Ident::Compiler(proc_macro::Ident::new(string, s)),
657 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new(string, s)),
658 }
659 }
660
new_raw(string: &str, span: Span) -> Ident661 pub fn new_raw(string: &str, span: Span) -> Ident {
662 match span {
663 Span::Compiler(s) => {
664 let p: proc_macro::TokenStream = string.parse().unwrap();
665 let ident = match p.into_iter().next() {
666 Some(proc_macro::TokenTree::Ident(mut i)) => {
667 i.set_span(s);
668 i
669 }
670 _ => panic!(),
671 };
672 Ident::Compiler(ident)
673 }
674 Span::Fallback(s) => Ident::Fallback(fallback::Ident::new_raw(string, s)),
675 }
676 }
677
span(&self) -> Span678 pub fn span(&self) -> Span {
679 match self {
680 Ident::Compiler(t) => Span::Compiler(t.span()),
681 Ident::Fallback(t) => Span::Fallback(t.span()),
682 }
683 }
684
set_span(&mut self, span: Span)685 pub fn set_span(&mut self, span: Span) {
686 match (self, span) {
687 (Ident::Compiler(t), Span::Compiler(s)) => t.set_span(s),
688 (Ident::Fallback(t), Span::Fallback(s)) => t.set_span(s),
689 _ => mismatch(),
690 }
691 }
692
unwrap_nightly(self) -> proc_macro::Ident693 fn unwrap_nightly(self) -> proc_macro::Ident {
694 match self {
695 Ident::Compiler(s) => s,
696 Ident::Fallback(_) => mismatch(),
697 }
698 }
699 }
700
701 impl PartialEq for Ident {
eq(&self, other: &Ident) -> bool702 fn eq(&self, other: &Ident) -> bool {
703 match (self, other) {
704 (Ident::Compiler(t), Ident::Compiler(o)) => t.to_string() == o.to_string(),
705 (Ident::Fallback(t), Ident::Fallback(o)) => t == o,
706 _ => mismatch(),
707 }
708 }
709 }
710
711 impl<T> PartialEq<T> for Ident
712 where
713 T: ?Sized + AsRef<str>,
714 {
eq(&self, other: &T) -> bool715 fn eq(&self, other: &T) -> bool {
716 let other = other.as_ref();
717 match self {
718 Ident::Compiler(t) => t.to_string() == other,
719 Ident::Fallback(t) => t == other,
720 }
721 }
722 }
723
724 impl Display for Ident {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result725 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
726 match self {
727 Ident::Compiler(t) => Display::fmt(t, f),
728 Ident::Fallback(t) => Display::fmt(t, f),
729 }
730 }
731 }
732
733 impl Debug for Ident {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result734 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
735 match self {
736 Ident::Compiler(t) => Debug::fmt(t, f),
737 Ident::Fallback(t) => Debug::fmt(t, f),
738 }
739 }
740 }
741
742 #[derive(Clone)]
743 pub(crate) enum Literal {
744 Compiler(proc_macro::Literal),
745 Fallback(fallback::Literal),
746 }
747
748 macro_rules! suffixed_numbers {
749 ($($name:ident => $kind:ident,)*) => ($(
750 pub fn $name(n: $kind) -> Literal {
751 if inside_proc_macro() {
752 Literal::Compiler(proc_macro::Literal::$name(n))
753 } else {
754 Literal::Fallback(fallback::Literal::$name(n))
755 }
756 }
757 )*)
758 }
759
760 macro_rules! unsuffixed_integers {
761 ($($name:ident => $kind:ident,)*) => ($(
762 pub fn $name(n: $kind) -> Literal {
763 if inside_proc_macro() {
764 Literal::Compiler(proc_macro::Literal::$name(n))
765 } else {
766 Literal::Fallback(fallback::Literal::$name(n))
767 }
768 }
769 )*)
770 }
771
772 impl Literal {
773 suffixed_numbers! {
774 u8_suffixed => u8,
775 u16_suffixed => u16,
776 u32_suffixed => u32,
777 u64_suffixed => u64,
778 u128_suffixed => u128,
779 usize_suffixed => usize,
780 i8_suffixed => i8,
781 i16_suffixed => i16,
782 i32_suffixed => i32,
783 i64_suffixed => i64,
784 i128_suffixed => i128,
785 isize_suffixed => isize,
786
787 f32_suffixed => f32,
788 f64_suffixed => f64,
789 }
790
791 unsuffixed_integers! {
792 u8_unsuffixed => u8,
793 u16_unsuffixed => u16,
794 u32_unsuffixed => u32,
795 u64_unsuffixed => u64,
796 u128_unsuffixed => u128,
797 usize_unsuffixed => usize,
798 i8_unsuffixed => i8,
799 i16_unsuffixed => i16,
800 i32_unsuffixed => i32,
801 i64_unsuffixed => i64,
802 i128_unsuffixed => i128,
803 isize_unsuffixed => isize,
804 }
805
f32_unsuffixed(f: f32) -> Literal806 pub fn f32_unsuffixed(f: f32) -> Literal {
807 if inside_proc_macro() {
808 Literal::Compiler(proc_macro::Literal::f32_unsuffixed(f))
809 } else {
810 Literal::Fallback(fallback::Literal::f32_unsuffixed(f))
811 }
812 }
813
f64_unsuffixed(f: f64) -> Literal814 pub fn f64_unsuffixed(f: f64) -> Literal {
815 if inside_proc_macro() {
816 Literal::Compiler(proc_macro::Literal::f64_unsuffixed(f))
817 } else {
818 Literal::Fallback(fallback::Literal::f64_unsuffixed(f))
819 }
820 }
821
string(t: &str) -> Literal822 pub fn string(t: &str) -> Literal {
823 if inside_proc_macro() {
824 Literal::Compiler(proc_macro::Literal::string(t))
825 } else {
826 Literal::Fallback(fallback::Literal::string(t))
827 }
828 }
829
character(t: char) -> Literal830 pub fn character(t: char) -> Literal {
831 if inside_proc_macro() {
832 Literal::Compiler(proc_macro::Literal::character(t))
833 } else {
834 Literal::Fallback(fallback::Literal::character(t))
835 }
836 }
837
byte_string(bytes: &[u8]) -> Literal838 pub fn byte_string(bytes: &[u8]) -> Literal {
839 if inside_proc_macro() {
840 Literal::Compiler(proc_macro::Literal::byte_string(bytes))
841 } else {
842 Literal::Fallback(fallback::Literal::byte_string(bytes))
843 }
844 }
845
span(&self) -> Span846 pub fn span(&self) -> Span {
847 match self {
848 Literal::Compiler(lit) => Span::Compiler(lit.span()),
849 Literal::Fallback(lit) => Span::Fallback(lit.span()),
850 }
851 }
852
set_span(&mut self, span: Span)853 pub fn set_span(&mut self, span: Span) {
854 match (self, span) {
855 (Literal::Compiler(lit), Span::Compiler(s)) => lit.set_span(s),
856 (Literal::Fallback(lit), Span::Fallback(s)) => lit.set_span(s),
857 _ => mismatch(),
858 }
859 }
860
subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span>861 pub fn subspan<R: RangeBounds<usize>>(&self, range: R) -> Option<Span> {
862 match self {
863 #[cfg(proc_macro_span)]
864 Literal::Compiler(lit) => lit.subspan(range).map(Span::Compiler),
865 #[cfg(not(proc_macro_span))]
866 Literal::Compiler(_lit) => None,
867 Literal::Fallback(lit) => lit.subspan(range).map(Span::Fallback),
868 }
869 }
870
unwrap_nightly(self) -> proc_macro::Literal871 fn unwrap_nightly(self) -> proc_macro::Literal {
872 match self {
873 Literal::Compiler(s) => s,
874 Literal::Fallback(_) => mismatch(),
875 }
876 }
877 }
878
879 impl From<fallback::Literal> for Literal {
from(s: fallback::Literal) -> Literal880 fn from(s: fallback::Literal) -> Literal {
881 Literal::Fallback(s)
882 }
883 }
884
885 impl Display for Literal {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result886 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
887 match self {
888 Literal::Compiler(t) => Display::fmt(t, f),
889 Literal::Fallback(t) => Display::fmt(t, f),
890 }
891 }
892 }
893
894 impl Debug for Literal {
fmt(&self, f: &mut fmt::Formatter) -> fmt::Result895 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
896 match self {
897 Literal::Compiler(t) => Debug::fmt(t, f),
898 Literal::Fallback(t) => Debug::fmt(t, f),
899 }
900 }
901 }
902