1use std::ops::{Bound, Range};
2
3use ast::token::IdentIsRaw;
4use rustc_ast as ast;
5use rustc_ast::token;
6use rustc_ast::tokenstream::{self, DelimSpacing, Spacing, TokenStream};
7use rustc_ast::util::literal::escape_byte_str_symbol;
8use rustc_ast_pretty::pprust;
9use rustc_data_structures::fx::FxHashMap;
10use rustc_errors::{Diag, ErrorGuaranteed, MultiSpan};
11use rustc_parse::lexer::{StripTokens, nfc_normalize};
12use rustc_parse::parser::Parser;
13use rustc_parse::{exp, new_parser_from_source_str, source_str_to_stream};
14use rustc_proc_macro::bridge::{
15 DelimSpan, Diagnostic, ExpnGlobals, Group, Ident, LitKind, Literal, Punct, TokenTree, server,
16};
17use rustc_proc_macro::{Delimiter, Level};
18use rustc_session::parse::ParseSess;
19use rustc_span::def_id::CrateNum;
20use rustc_span::{BytePos, FileName, Pos, Span, Symbol, sym};
21use smallvec::{SmallVec, smallvec};
22
23use crate::base::ExtCtxt;
24
25trait FromInternal<T> {
26 fn from_internal(x: T) -> Self;
27}
28
29trait ToInternal<T> {
30 fn to_internal(self) -> T;
31}
32
33impl FromInternal<token::Delimiter> for Delimiter {
34 fn from_internal(delim: token::Delimiter) -> Delimiter {
35 match delim {
36 token::Delimiter::Parenthesis => Delimiter::Parenthesis,
37 token::Delimiter::Brace => Delimiter::Brace,
38 token::Delimiter::Bracket => Delimiter::Bracket,
39 token::Delimiter::Invisible(_) => Delimiter::None,
40 }
41 }
42}
43
44impl ToInternal<token::Delimiter> for Delimiter {
45 fn to_internal(self) -> token::Delimiter {
46 match self {
47 Delimiter::Parenthesis => token::Delimiter::Parenthesis,
48 Delimiter::Brace => token::Delimiter::Brace,
49 Delimiter::Bracket => token::Delimiter::Bracket,
50 Delimiter::None => token::Delimiter::Invisible(token::InvisibleOrigin::ProcMacro),
51 }
52 }
53}
54
55impl FromInternal<token::LitKind> for LitKind {
56 fn from_internal(kind: token::LitKind) -> Self {
57 match kind {
58 token::Byte => LitKind::Byte,
59 token::Char => LitKind::Char,
60 token::Integer => LitKind::Integer,
61 token::Float => LitKind::Float,
62 token::Str => LitKind::Str,
63 token::StrRaw(n) => LitKind::StrRaw(n),
64 token::ByteStr => LitKind::ByteStr,
65 token::ByteStrRaw(n) => LitKind::ByteStrRaw(n),
66 token::CStr => LitKind::CStr,
67 token::CStrRaw(n) => LitKind::CStrRaw(n),
68 token::Err(_guar) => {
69 LitKind::ErrWithGuar
73 }
74 token::Bool => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
75 }
76 }
77}
78
79impl ToInternal<token::LitKind> for LitKind {
80 fn to_internal(self) -> token::LitKind {
81 match self {
82 LitKind::Byte => token::Byte,
83 LitKind::Char => token::Char,
84 LitKind::Integer => token::Integer,
85 LitKind::Float => token::Float,
86 LitKind::Str => token::Str,
87 LitKind::StrRaw(n) => token::StrRaw(n),
88 LitKind::ByteStr => token::ByteStr,
89 LitKind::ByteStrRaw(n) => token::ByteStrRaw(n),
90 LitKind::CStr => token::CStr,
91 LitKind::CStrRaw(n) => token::CStrRaw(n),
92 LitKind::ErrWithGuar => {
93 #[allow(deprecated)]
99 let guar = ErrorGuaranteed::unchecked_error_guaranteed();
100 token::Err(guar)
101 }
102 }
103 }
104}
105
106impl FromInternal<TokenStream> for Vec<TokenTree<TokenStream, Span, Symbol>> {
107 fn from_internal(stream: TokenStream) -> Self {
108 use rustc_ast::token::*;
109
110 let mut trees = Vec::with_capacity(stream.len().next_power_of_two());
113 let mut iter = stream.iter();
114
115 while let Some(tree) = iter.next() {
116 let (Token { kind, span }, joint) = match tree.clone() {
117 tokenstream::TokenTree::Delimited(span, _, mut delim, mut stream) => {
118 while let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim {
123 if stream.len() == 1
124 && let tree = stream.iter().next().unwrap()
125 && let tokenstream::TokenTree::Delimited(_, _, delim2, stream2) = tree
126 && let Delimiter::Invisible(InvisibleOrigin::MetaVar(_)) = delim2
127 {
128 delim = *delim2;
129 stream = stream2.clone();
130 } else {
131 break;
132 }
133 }
134
135 trees.push(TokenTree::Group(Group {
136 delimiter: rustc_proc_macro::Delimiter::from_internal(delim),
137 stream: Some(stream),
138 span: DelimSpan {
139 open: span.open,
140 close: span.close,
141 entire: span.entire(),
142 },
143 }));
144 continue;
145 }
146 tokenstream::TokenTree::Token(token, spacing) => {
147 let joint = match spacing {
157 Spacing::Alone | Spacing::JointHidden => false,
158 Spacing::Joint => true,
159 };
160 (token, joint)
161 }
162 };
163
164 let mut op = |s: &str| {
168 if !s.is_ascii() {
::core::panicking::panic("assertion failed: s.is_ascii()")
};assert!(s.is_ascii());
169 trees.extend(s.bytes().enumerate().map(|(i, ch)| {
170 let is_final = i == s.len() - 1;
171 let span = if (span.hi() - span.lo()).to_usize() == s.len() {
177 let lo = span.lo() + BytePos::from_usize(i);
178 let hi = lo + BytePos::from_usize(1);
179 span.with_lo(lo).with_hi(hi)
180 } else {
181 span
182 };
183 let joint = if is_final { joint } else { true };
184 TokenTree::Punct(Punct { ch, joint, span })
185 }));
186 };
187
188 match kind {
189 Eq => op("="),
190 Lt => op("<"),
191 Le => op("<="),
192 EqEq => op("=="),
193 Ne => op("!="),
194 Ge => op(">="),
195 Gt => op(">"),
196 AndAnd => op("&&"),
197 OrOr => op("||"),
198 Bang => op("!"),
199 Tilde => op("~"),
200 Plus => op("+"),
201 Minus => op("-"),
202 Star => op("*"),
203 Slash => op("/"),
204 Percent => op("%"),
205 Caret => op("^"),
206 And => op("&"),
207 Or => op("|"),
208 Shl => op("<<"),
209 Shr => op(">>"),
210 PlusEq => op("+="),
211 MinusEq => op("-="),
212 StarEq => op("*="),
213 SlashEq => op("/="),
214 PercentEq => op("%="),
215 CaretEq => op("^="),
216 AndEq => op("&="),
217 OrEq => op("|="),
218 ShlEq => op("<<="),
219 ShrEq => op(">>="),
220 At => op("@"),
221 Dot => op("."),
222 DotDot => op(".."),
223 DotDotDot => op("..."),
224 DotDotEq => op("..="),
225 Comma => op(","),
226 Semi => op(";"),
227 Colon => op(":"),
228 PathSep => op("::"),
229 RArrow => op("->"),
230 LArrow => op("<-"),
231 FatArrow => op("=>"),
232 Pound => op("#"),
233 Dollar => op("$"),
234 Question => op("?"),
235 SingleQuote => op("'"),
236
237 Ident(sym, is_raw) => trees.push(TokenTree::Ident(Ident {
238 sym,
239 is_raw: #[allow(non_exhaustive_omitted_patterns)] match is_raw {
IdentIsRaw::Yes => true,
_ => false,
}matches!(is_raw, IdentIsRaw::Yes),
240 span,
241 })),
242 NtIdent(ident, is_raw) => trees.push(TokenTree::Ident(Ident {
243 sym: ident.name,
244 is_raw: #[allow(non_exhaustive_omitted_patterns)] match is_raw {
IdentIsRaw::Yes => true,
_ => false,
}matches!(is_raw, IdentIsRaw::Yes),
245 span: ident.span,
246 })),
247
248 Lifetime(name, is_raw) => {
249 let ident = rustc_span::Ident::new(name, span).without_first_quote();
250 trees.extend([
251 TokenTree::Punct(Punct { ch: b'\'', joint: true, span }),
252 TokenTree::Ident(Ident {
253 sym: ident.name,
254 is_raw: #[allow(non_exhaustive_omitted_patterns)] match is_raw {
IdentIsRaw::Yes => true,
_ => false,
}matches!(is_raw, IdentIsRaw::Yes),
255 span,
256 }),
257 ]);
258 }
259 NtLifetime(ident, is_raw) => {
260 let stream =
261 TokenStream::token_alone(token::Lifetime(ident.name, is_raw), ident.span);
262 trees.push(TokenTree::Group(Group {
263 delimiter: rustc_proc_macro::Delimiter::None,
264 stream: Some(stream),
265 span: DelimSpan::from_single(span),
266 }))
267 }
268
269 Literal(token::Lit { kind, symbol, suffix }) => {
270 trees.push(TokenTree::Literal(self::Literal {
271 kind: FromInternal::from_internal(kind),
272 symbol,
273 suffix,
274 span,
275 }));
276 }
277 DocComment(_, attr_style, data) => {
278 let mut escaped = String::new();
279 for ch in data.as_str().chars() {
280 escaped.extend(ch.escape_debug());
281 }
282 let stream = [
283 Ident(sym::doc, IdentIsRaw::No),
284 Eq,
285 TokenKind::lit(token::Str, Symbol::intern(&escaped), None),
286 ]
287 .into_iter()
288 .map(|kind| tokenstream::TokenTree::token_alone(kind, span))
289 .collect();
290 trees.push(TokenTree::Punct(Punct { ch: b'#', joint: false, span }));
291 if attr_style == ast::AttrStyle::Inner {
292 trees.push(TokenTree::Punct(Punct { ch: b'!', joint: false, span }));
293 }
294 trees.push(TokenTree::Group(Group {
295 delimiter: rustc_proc_macro::Delimiter::Bracket,
296 stream: Some(stream),
297 span: DelimSpan::from_single(span),
298 }));
299 }
300
301 OpenParen | CloseParen | OpenBrace | CloseBrace | OpenBracket | CloseBracket
302 | OpenInvisible(_) | CloseInvisible(_) | Eof => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
303 }
304 }
305 trees
306 }
307}
308
309impl ToInternal<SmallVec<[tokenstream::TokenTree; 2]>>
311 for (TokenTree<TokenStream, Span, Symbol>, &mut Rustc<'_, '_>)
312{
313 fn to_internal(self) -> SmallVec<[tokenstream::TokenTree; 2]> {
314 use rustc_ast::token::*;
315
316 let (tree, rustc) = self;
322 match tree {
323 TokenTree::Punct(Punct { ch, joint, span }) => {
324 let kind = match ch {
325 b'=' => Eq,
326 b'<' => Lt,
327 b'>' => Gt,
328 b'!' => Bang,
329 b'~' => Tilde,
330 b'+' => Plus,
331 b'-' => Minus,
332 b'*' => Star,
333 b'/' => Slash,
334 b'%' => Percent,
335 b'^' => Caret,
336 b'&' => And,
337 b'|' => Or,
338 b'@' => At,
339 b'.' => Dot,
340 b',' => Comma,
341 b';' => Semi,
342 b':' => Colon,
343 b'#' => Pound,
344 b'$' => Dollar,
345 b'?' => Question,
346 b'\'' => SingleQuote,
347 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
348 };
349 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(if joint {
tokenstream::TokenTree::token_joint(kind, span)
} else { tokenstream::TokenTree::token_alone(kind, span) });
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[if joint {
tokenstream::TokenTree::token_joint(kind, span)
} else {
tokenstream::TokenTree::token_alone(kind, span)
}])))
}
}smallvec![if joint {
355 tokenstream::TokenTree::token_joint(kind, span)
356 } else {
357 tokenstream::TokenTree::token_alone(kind, span)
358 }]
359 }
360 TokenTree::Group(Group { delimiter, stream, span: DelimSpan { open, close, .. } }) => {
361 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(tokenstream::TokenTree::Delimited(tokenstream::DelimSpan {
open,
close,
}, DelimSpacing::new(Spacing::Alone, Spacing::Alone),
delimiter.to_internal(), stream.unwrap_or_default()));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[tokenstream::TokenTree::Delimited(tokenstream::DelimSpan {
open,
close,
}, DelimSpacing::new(Spacing::Alone, Spacing::Alone),
delimiter.to_internal(), stream.unwrap_or_default())])))
}
}smallvec![tokenstream::TokenTree::Delimited(
362 tokenstream::DelimSpan { open, close },
363 DelimSpacing::new(Spacing::Alone, Spacing::Alone),
364 delimiter.to_internal(),
365 stream.unwrap_or_default(),
366 )]
367 }
368 TokenTree::Ident(self::Ident { sym, is_raw, span }) => {
369 rustc.psess().symbol_gallery.insert(sym, span);
370 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(tokenstream::TokenTree::token_alone(Ident(sym,
is_raw.into()), span));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[tokenstream::TokenTree::token_alone(Ident(sym,
is_raw.into()), span)])))
}
}smallvec![tokenstream::TokenTree::token_alone(Ident(sym, is_raw.into()), span)]
371 }
372 TokenTree::Literal(self::Literal {
373 kind: self::LitKind::Integer,
374 symbol,
375 suffix,
376 span,
377 }) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
378 let symbol = Symbol::intern(symbol);
379 let integer = TokenKind::lit(token::Integer, symbol, suffix);
380 let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
381 let b = tokenstream::TokenTree::token_alone(integer, span);
382 {
let count = 0usize + 1usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(a);
vec.push(b);
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[a, b])))
}
}smallvec![a, b]
383 }
384 TokenTree::Literal(self::Literal {
385 kind: self::LitKind::Float,
386 symbol,
387 suffix,
388 span,
389 }) if let Some(symbol) = symbol.as_str().strip_prefix('-') => {
390 let symbol = Symbol::intern(symbol);
391 let float = TokenKind::lit(token::Float, symbol, suffix);
392 let a = tokenstream::TokenTree::token_joint_hidden(Minus, span);
393 let b = tokenstream::TokenTree::token_alone(float, span);
394 {
let count = 0usize + 1usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(a);
vec.push(b);
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[a, b])))
}
}smallvec![a, b]
395 }
396 TokenTree::Literal(self::Literal { kind, symbol, suffix, span }) => {
397 {
let count = 0usize + 1usize;
let mut vec = ::smallvec::SmallVec::new();
if count <= vec.inline_size() {
vec.push(tokenstream::TokenTree::token_alone(TokenKind::lit(kind.to_internal(),
symbol, suffix), span));
vec
} else {
::smallvec::SmallVec::from_vec(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[tokenstream::TokenTree::token_alone(TokenKind::lit(kind.to_internal(),
symbol, suffix), span)])))
}
}smallvec![tokenstream::TokenTree::token_alone(
398 TokenKind::lit(kind.to_internal(), symbol, suffix),
399 span,
400 )]
401 }
402 }
403 }
404}
405
406impl ToInternal<rustc_errors::Level> for Level {
407 fn to_internal(self) -> rustc_errors::Level {
408 match self {
409 Level::Error => rustc_errors::Level::Error,
410 Level::Warning => rustc_errors::Level::Warning,
411 Level::Note => rustc_errors::Level::Note,
412 Level::Help => rustc_errors::Level::Help,
413 _ => {
::core::panicking::panic_fmt(format_args!("internal error: entered unreachable code: {0}",
format_args!("unknown proc_macro::Level variant: {0:?}", self)));
}unreachable!("unknown proc_macro::Level variant: {:?}", self),
414 }
415 }
416}
417
418fn cancel_diags_into_string(diags: Vec<Diag<'_>>) -> String {
419 let mut messages = diags.into_iter().flat_map(Diag::cancel_into_message);
420 let msg = messages.next().expect("no diagnostic has a message");
421 messages.for_each(|_| ()); msg
423}
424
425pub(crate) struct Rustc<'a, 'b> {
426 ecx: &'a mut ExtCtxt<'b>,
427 def_site: Span,
428 call_site: Span,
429 mixed_site: Span,
430 krate: CrateNum,
431 rebased_spans: FxHashMap<usize, Span>,
432}
433
434impl<'a, 'b> Rustc<'a, 'b> {
435 pub(crate) fn new(ecx: &'a mut ExtCtxt<'b>) -> Self {
436 let expn_data = ecx.current_expansion.id.expn_data();
437 Rustc {
438 def_site: ecx.with_def_site_ctxt(expn_data.def_site),
439 call_site: ecx.with_call_site_ctxt(expn_data.call_site),
440 mixed_site: ecx.with_mixed_site_ctxt(expn_data.call_site),
441 krate: expn_data.macro_def_id.unwrap().krate,
442 rebased_spans: FxHashMap::default(),
443 ecx,
444 }
445 }
446
447 fn psess(&self) -> &ParseSess {
448 self.ecx.psess()
449 }
450}
451
452impl server::Server for Rustc<'_, '_> {
453 type TokenStream = TokenStream;
454 type Span = Span;
455 type Symbol = Symbol;
456
457 fn globals(&mut self) -> ExpnGlobals<Self::Span> {
458 ExpnGlobals {
459 def_site: self.def_site,
460 call_site: self.call_site,
461 mixed_site: self.mixed_site,
462 }
463 }
464
465 fn intern_symbol(string: &str) -> Self::Symbol {
466 Symbol::intern(string)
467 }
468
469 fn with_symbol_string(symbol: &Self::Symbol, f: impl FnOnce(&str)) {
470 f(symbol.as_str())
471 }
472
473 fn injected_env_var(&mut self, var: &str) -> Option<String> {
474 self.ecx.sess.opts.logical_env.get(var).cloned()
475 }
476
477 fn track_env_var(&mut self, var: &str, value: Option<&str>) {
478 self.ecx
479 .sess
480 .env_depinfo
481 .borrow_mut()
482 .insert((Symbol::intern(var), value.map(Symbol::intern)));
483 }
484
485 fn track_path(&mut self, path: &str) {
486 self.ecx.sess.file_depinfo.borrow_mut().insert(Symbol::intern(path));
487 }
488
489 fn literal_from_str(&mut self, s: &str) -> Result<Literal<Self::Span, Self::Symbol>, String> {
490 let name = FileName::proc_macro_source_code(s);
491
492 let mut parser =
493 new_parser_from_source_str(self.psess(), name, s.to_owned(), StripTokens::Nothing)
494 .map_err(cancel_diags_into_string)?;
495
496 let first_span = parser.token.span.data();
497 let minus_present = parser.eat(::rustc_parse::parser::token_type::ExpTokenPair {
tok: rustc_ast::token::Minus,
token_type: ::rustc_parse::parser::token_type::TokenType::Minus,
}exp!(Minus));
498
499 let lit_span = parser.token.span.data();
500 let token::Literal(mut lit) = parser.token.kind else {
501 return Err("not a literal".to_string());
502 };
503
504 if (lit_span.hi.0 - first_span.lo.0) as usize != s.len() {
507 return Err("comment or whitespace around literal".to_string());
508 }
509
510 if minus_present {
511 if first_span.hi.0 != lit_span.lo.0 {
514 return Err("comment or whitespace after minus".to_string());
515 }
516
517 match lit.kind {
519 token::LitKind::Bool
520 | token::LitKind::Byte
521 | token::LitKind::Char
522 | token::LitKind::Str
523 | token::LitKind::StrRaw(_)
524 | token::LitKind::ByteStr
525 | token::LitKind::ByteStrRaw(_)
526 | token::LitKind::CStr
527 | token::LitKind::CStrRaw(_)
528 | token::LitKind::Err(_) => {
529 return Err("non-numeric literal may not be negated".to_string());
530 }
531 token::LitKind::Integer | token::LitKind::Float => {}
532 }
533
534 let symbol = Symbol::intern(&s[..1 + lit.symbol.as_str().len()]);
536 lit = token::Lit::new(lit.kind, symbol, lit.suffix);
537 }
538 let token::Lit { kind, symbol, suffix } = lit;
539 Ok(Literal {
540 kind: FromInternal::from_internal(kind),
541 symbol,
542 suffix,
543 span: self.call_site,
544 })
545 }
546
547 fn emit_diagnostic(&mut self, diagnostic: Diagnostic<Self::Span>) {
548 let message = rustc_errors::DiagMessage::from(diagnostic.message);
549 let mut diag: Diag<'_, ()> =
550 Diag::new(self.psess().dcx(), diagnostic.level.to_internal(), message);
551 diag.span(MultiSpan::from_spans(diagnostic.spans));
552 for child in diagnostic.children {
553 diag.sub(child.level.to_internal(), child.message, MultiSpan::from_spans(child.spans));
554 }
555 diag.emit();
556 }
557
558 fn ts_drop(&mut self, stream: Self::TokenStream) {
559 drop(stream);
560 }
561
562 fn ts_clone(&mut self, stream: &Self::TokenStream) -> Self::TokenStream {
563 stream.clone()
564 }
565
566 fn ts_is_empty(&mut self, stream: &Self::TokenStream) -> bool {
567 stream.is_empty()
568 }
569
570 fn ts_from_str(&mut self, src: &str) -> Result<Self::TokenStream, String> {
571 source_str_to_stream(
572 self.psess(),
573 FileName::proc_macro_source_code(src),
574 src.to_string(),
575 Some(self.call_site),
576 )
577 .map_err(cancel_diags_into_string)
578 }
579
580 fn ts_to_string(&mut self, stream: &Self::TokenStream) -> String {
581 pprust::tts_to_string(stream)
582 }
583
584 fn ts_expand_expr(&mut self, stream: &Self::TokenStream) -> Result<Self::TokenStream, ()> {
585 let expr = try {
587 let mut p = Parser::new(self.psess(), stream.clone(), Some("proc_macro expand expr"));
588 let expr = p.parse_expr()?;
589 if p.token != token::Eof {
590 p.unexpected()?;
591 }
592 expr
593 };
594 let expr = expr.map_err(|err| {
595 err.emit();
596 })?;
597
598 let expr = self
600 .ecx
601 .expander()
602 .fully_expand_fragment(crate::expand::AstFragment::Expr(expr))
603 .make_expr();
604
605 match &expr.kind {
610 ast::ExprKind::Lit(token_lit) if token_lit.kind == token::Bool => {
611 Ok(tokenstream::TokenStream::token_alone(
612 token::Ident(token_lit.symbol, IdentIsRaw::No),
613 expr.span,
614 ))
615 }
616 ast::ExprKind::Lit(token_lit) => {
617 Ok(tokenstream::TokenStream::token_alone(token::Literal(*token_lit), expr.span))
618 }
619 ast::ExprKind::IncludedBytes(byte_sym) => {
620 let lit = token::Lit::new(
621 token::ByteStr,
622 escape_byte_str_symbol(byte_sym.as_byte_str()),
623 None,
624 );
625 Ok(tokenstream::TokenStream::token_alone(token::TokenKind::Literal(lit), expr.span))
626 }
627 ast::ExprKind::Unary(ast::UnOp::Neg, e) => match &e.kind {
628 ast::ExprKind::Lit(token_lit) => match token_lit {
629 token::Lit { kind: token::Integer | token::Float, .. } => {
630 Ok(Self::TokenStream::from_iter([
631 tokenstream::TokenTree::token_joint_hidden(token::Minus, e.span),
634 tokenstream::TokenTree::token_alone(token::Literal(*token_lit), e.span),
635 ]))
636 }
637 _ => Err(()),
638 },
639 _ => Err(()),
640 },
641 _ => Err(()),
642 }
643 }
644
645 fn ts_from_token_tree(
646 &mut self,
647 tree: TokenTree<Self::TokenStream, Self::Span, Self::Symbol>,
648 ) -> Self::TokenStream {
649 Self::TokenStream::new((tree, &mut *self).to_internal().into_iter().collect::<Vec<_>>())
650 }
651
652 fn ts_concat_trees(
653 &mut self,
654 base: Option<Self::TokenStream>,
655 trees: Vec<TokenTree<Self::TokenStream, Self::Span, Self::Symbol>>,
656 ) -> Self::TokenStream {
657 let mut stream = base.unwrap_or_default();
658 for tree in trees {
659 for tt in (tree, &mut *self).to_internal() {
660 stream.push_tree(tt);
661 }
662 }
663 stream
664 }
665
666 fn ts_concat_streams(
667 &mut self,
668 base: Option<Self::TokenStream>,
669 streams: Vec<Self::TokenStream>,
670 ) -> Self::TokenStream {
671 let mut stream = base.unwrap_or_default();
672 for s in streams {
673 stream.push_stream(s);
674 }
675 stream
676 }
677
678 fn ts_into_trees(
679 &mut self,
680 stream: Self::TokenStream,
681 ) -> Vec<TokenTree<Self::TokenStream, Self::Span, Self::Symbol>> {
682 FromInternal::from_internal(stream)
683 }
684
685 fn span_debug(&mut self, span: Self::Span) -> String {
686 if self.ecx.ecfg.span_debug {
687 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?}", span))
})format!("{span:?}")
688 } else {
689 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0:?} bytes({1}..{2})",
span.ctxt(), span.lo().0, span.hi().0))
})format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
690 }
691 }
692
693 fn span_file(&mut self, span: Self::Span) -> String {
694 self.psess()
695 .source_map()
696 .lookup_char_pos(span.lo())
697 .file
698 .name
699 .prefer_remapped_unconditionally()
700 .to_string()
701 }
702
703 fn span_local_file(&mut self, span: Self::Span) -> Option<String> {
704 self.psess()
705 .source_map()
706 .lookup_char_pos(span.lo())
707 .file
708 .name
709 .clone()
710 .into_local_path()
711 .map(|p| {
712 p.to_str()
713 .expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
714 .to_string()
715 })
716 }
717
718 fn span_parent(&mut self, span: Self::Span) -> Option<Self::Span> {
719 span.parent_callsite()
720 }
721
722 fn span_source(&mut self, span: Self::Span) -> Self::Span {
723 span.source_callsite()
724 }
725
726 fn span_byte_range(&mut self, span: Self::Span) -> Range<usize> {
727 let source_map = self.psess().source_map();
728
729 let relative_start_pos = source_map.lookup_byte_offset(span.lo()).pos;
730 let relative_end_pos = source_map.lookup_byte_offset(span.hi()).pos;
731
732 Range { start: relative_start_pos.0 as usize, end: relative_end_pos.0 as usize }
733 }
734 fn span_start(&mut self, span: Self::Span) -> Self::Span {
735 span.shrink_to_lo()
736 }
737
738 fn span_end(&mut self, span: Self::Span) -> Self::Span {
739 span.shrink_to_hi()
740 }
741
742 fn span_line(&mut self, span: Self::Span) -> usize {
743 let loc = self.psess().source_map().lookup_char_pos(span.lo());
744 loc.line
745 }
746
747 fn span_column(&mut self, span: Self::Span) -> usize {
748 let loc = self.psess().source_map().lookup_char_pos(span.lo());
749 loc.col.to_usize() + 1
750 }
751
752 fn span_join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
753 let self_loc = self.psess().source_map().lookup_char_pos(first.lo());
754 let other_loc = self.psess().source_map().lookup_char_pos(second.lo());
755
756 if self_loc.file.stable_id != other_loc.file.stable_id {
757 return None;
758 }
759
760 Some(first.to(second))
761 }
762
763 fn span_subspan(
764 &mut self,
765 span: Self::Span,
766 start: Bound<usize>,
767 end: Bound<usize>,
768 ) -> Option<Self::Span> {
769 let length = span.hi().to_usize() - span.lo().to_usize();
770
771 let start = match start {
772 Bound::Included(lo) => lo,
773 Bound::Excluded(lo) => lo.checked_add(1)?,
774 Bound::Unbounded => 0,
775 };
776
777 let end = match end {
778 Bound::Included(hi) => hi.checked_add(1)?,
779 Bound::Excluded(hi) => hi,
780 Bound::Unbounded => length,
781 };
782
783 if start > u32::MAX as usize
785 || end > u32::MAX as usize
786 || (u32::MAX - start as u32) < span.lo().to_u32()
787 || (u32::MAX - end as u32) < span.lo().to_u32()
788 || start >= end
789 || end > length
790 {
791 return None;
792 }
793
794 let new_lo = span.lo() + BytePos::from_usize(start);
795 let new_hi = span.lo() + BytePos::from_usize(end);
796 Some(span.with_lo(new_lo).with_hi(new_hi))
797 }
798
799 fn span_resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
800 span.with_ctxt(at.ctxt())
801 }
802
803 fn span_source_text(&mut self, span: Self::Span) -> Option<String> {
804 self.psess().source_map().span_to_snippet(span).ok()
805 }
806
807 fn span_save_span(&mut self, span: Self::Span) -> usize {
832 self.psess().save_proc_macro_span(span)
833 }
834
835 fn span_recover_proc_macro_span(&mut self, id: usize) -> Self::Span {
836 let (resolver, krate, def_site) = (&*self.ecx.resolver, self.krate, self.def_site);
837 *self.rebased_spans.entry(id).or_insert_with(|| {
838 resolver.get_proc_macro_quoted_span(krate, id).with_ctxt(def_site.ctxt())
841 })
842 }
843
844 fn symbol_normalize_and_validate_ident(&mut self, string: &str) -> Result<Self::Symbol, ()> {
845 let sym = nfc_normalize(string);
846 if rustc_lexer::is_ident(sym.as_str()) { Ok(sym) } else { Err(()) }
847 }
848}