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