2 use crate::ext::base::ExtCtxt;
3 use crate::parse::{self, token, ParseSess};
4 use crate::parse::lexer::comments;
5 use crate::tokenstream::{self, DelimSpan, IsJoint::*, TokenStream, TreeAndJoint};
7 use errors::Diagnostic;
8 use rustc_data_structures::sync::Lrc;
9 use syntax_pos::{BytePos, FileName, MultiSpan, Pos, SourceFile, Span};
10 use syntax_pos::symbol::{kw, sym, Symbol};
12 use proc_macro::{Delimiter, Level, LineColumn, Spacing};
13 use proc_macro::bridge::{server, TokenTree};
14 use std::{ascii, panic};
17 trait FromInternal<T> {
18 fn from_internal(x: T) -> Self;
22 fn to_internal(self) -> T;
25 impl FromInternal<token::DelimToken> for Delimiter {
26 fn from_internal(delim: token::DelimToken) -> Delimiter {
28 token::Paren => Delimiter::Parenthesis,
29 token::Brace => Delimiter::Brace,
30 token::Bracket => Delimiter::Bracket,
31 token::NoDelim => Delimiter::None,
36 impl ToInternal<token::DelimToken> for Delimiter {
37 fn to_internal(self) -> token::DelimToken {
39 Delimiter::Parenthesis => token::Paren,
40 Delimiter::Brace => token::Brace,
41 Delimiter::Bracket => token::Bracket,
42 Delimiter::None => token::NoDelim,
47 impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec<Self>)>
48 for TokenTree<Group, Punct, Ident, Literal>
50 fn from_internal(((tree, is_joint), sess, stack): (TreeAndJoint, &ParseSess, &mut Vec<Self>))
52 use crate::parse::token::*;
54 let joint = is_joint == Joint;
55 let Token { kind, span } = match tree {
56 tokenstream::TokenTree::Delimited(span, delim, tts) => {
57 let delimiter = Delimiter::from_internal(delim);
58 return TokenTree::Group(Group {
64 tokenstream::TokenTree::Token(token) => token,
68 ($ty:ident { $($field:ident $(: $value:expr)*),+ $(,)? }) => (
69 TokenTree::$ty(self::$ty {
70 $($field $(: $value)*,)+
74 ($ty:ident::$method:ident($($value:expr),*)) => (
75 TokenTree::$ty(self::$ty::$method($($value,)* span))
80 tt!(Punct::new($a, joint))
82 ($a:expr, $b:expr) => {{
83 stack.push(tt!(Punct::new($b, joint)));
84 tt!(Punct::new($a, true))
86 ($a:expr, $b:expr, $c:expr) => {{
87 stack.push(tt!(Punct::new($c, joint)));
88 stack.push(tt!(Punct::new($b, true)));
89 tt!(Punct::new($a, true))
97 EqEq => op!('=', '='),
101 AndAnd => op!('&', '&'),
102 OrOr => op!('|', '|'),
105 BinOp(Plus) => op!('+'),
106 BinOp(Minus) => op!('-'),
107 BinOp(Star) => op!('*'),
108 BinOp(Slash) => op!('/'),
109 BinOp(Percent) => op!('%'),
110 BinOp(Caret) => op!('^'),
111 BinOp(And) => op!('&'),
112 BinOp(Or) => op!('|'),
113 BinOp(Shl) => op!('<', '<'),
114 BinOp(Shr) => op!('>', '>'),
115 BinOpEq(Plus) => op!('+', '='),
116 BinOpEq(Minus) => op!('-', '='),
117 BinOpEq(Star) => op!('*', '='),
118 BinOpEq(Slash) => op!('/', '='),
119 BinOpEq(Percent) => op!('%', '='),
120 BinOpEq(Caret) => op!('^', '='),
121 BinOpEq(And) => op!('&', '='),
122 BinOpEq(Or) => op!('|', '='),
123 BinOpEq(Shl) => op!('<', '<', '='),
124 BinOpEq(Shr) => op!('>', '>', '='),
127 DotDot => op!('.', '.'),
128 DotDotDot => op!('.', '.', '.'),
129 DotDotEq => op!('.', '.', '='),
133 ModSep => op!(':', ':'),
134 RArrow => op!('-', '>'),
135 LArrow => op!('<', '-'),
136 FatArrow => op!('=', '>'),
139 Question => op!('?'),
140 SingleQuote => op!('\''),
142 Ident(name, false) if name == kw::DollarCrate => tt!(Ident::dollar_crate()),
143 Ident(name, is_raw) => tt!(Ident::new(name, is_raw)),
145 let ident = ast::Ident::new(name, span).without_first_quote();
146 stack.push(tt!(Ident::new(ident.name, false)));
147 tt!(Punct::new('\'', true))
149 Literal(lit) => tt!(Literal { lit }),
151 let style = comments::doc_comment_style(&c.as_str());
152 let stripped = comments::strip_doc_comment_decoration(&c.as_str());
153 let mut escaped = String::new();
154 for ch in stripped.chars() {
155 escaped.extend(ch.escape_debug());
158 Ident(sym::doc, false),
160 TokenKind::lit(token::Str, Symbol::intern(&escaped), None),
163 .map(|kind| tokenstream::TokenTree::token(kind, span))
165 stack.push(TokenTree::Group(Group {
166 delimiter: Delimiter::Bracket,
168 span: DelimSpan::from_single(span),
170 if style == ast::AttrStyle::Inner {
171 stack.push(tt!(Punct::new('!', false)));
173 tt!(Punct::new('#', false))
176 Interpolated(nt) => {
177 let stream = nt.to_tokenstream(sess, span);
178 TokenTree::Group(Group {
179 delimiter: Delimiter::None,
181 span: DelimSpan::from_single(span),
185 OpenDelim(..) | CloseDelim(..) => unreachable!(),
186 Whitespace | Comment | Shebang(..) | Unknown(..) | Eof => unreachable!(),
191 impl ToInternal<TokenStream> for TokenTree<Group, Punct, Ident, Literal> {
192 fn to_internal(self) -> TokenStream {
193 use crate::parse::token::*;
195 let (ch, joint, span) = match self {
196 TokenTree::Punct(Punct { ch, joint, span }) => (ch, joint, span),
197 TokenTree::Group(Group {
202 return tokenstream::TokenTree::Delimited(
204 delimiter.to_internal(),
209 TokenTree::Ident(self::Ident { sym, is_raw, span }) => {
210 return tokenstream::TokenTree::token(Ident(sym, is_raw), span).into();
212 TokenTree::Literal(self::Literal {
213 lit: token::Lit { kind: token::Integer, symbol, suffix },
215 }) if symbol.as_str().starts_with("-") => {
216 let minus = BinOp(BinOpToken::Minus);
217 let symbol = Symbol::intern(&symbol.as_str()[1..]);
218 let integer = TokenKind::lit(token::Integer, symbol, suffix);
219 let a = tokenstream::TokenTree::token(minus, span);
220 let b = tokenstream::TokenTree::token(integer, span);
221 return vec![a, b].into_iter().collect();
223 TokenTree::Literal(self::Literal {
224 lit: token::Lit { kind: token::Float, symbol, suffix },
226 }) if symbol.as_str().starts_with("-") => {
227 let minus = BinOp(BinOpToken::Minus);
228 let symbol = Symbol::intern(&symbol.as_str()[1..]);
229 let float = TokenKind::lit(token::Float, symbol, suffix);
230 let a = tokenstream::TokenTree::token(minus, span);
231 let b = tokenstream::TokenTree::token(float, span);
232 return vec![a, b].into_iter().collect();
234 TokenTree::Literal(self::Literal { lit, span }) => {
235 return tokenstream::TokenTree::token(Literal(lit), span).into()
239 let kind = match ch {
249 '%' => BinOp(Percent),
265 let tree = tokenstream::TokenTree::token(kind, span);
266 TokenStream::new(vec![(tree, if joint { Joint } else { NonJoint })])
270 impl ToInternal<errors::Level> for Level {
271 fn to_internal(self) -> errors::Level {
273 Level::Error => errors::Level::Error,
274 Level::Warning => errors::Level::Warning,
275 Level::Note => errors::Level::Note,
276 Level::Help => errors::Level::Help,
277 _ => unreachable!("unknown proc_macro::Level variant: {:?}", self),
283 pub struct TokenStreamIter {
284 cursor: tokenstream::Cursor,
285 stack: Vec<TokenTree<Group, Punct, Ident, Literal>>,
290 delimiter: Delimiter,
295 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
298 // NB. not using `Spacing` here because it doesn't implement `Hash`.
304 fn new(ch: char, joint: bool, span: Span) -> Punct {
305 const LEGAL_CHARS: &[char] = &['=', '<', '>', '!', '~', '+', '-', '*', '/', '%', '^',
306 '&', '|', '@', '.', ',', ';', ':', '#', '$', '?', '\''];
307 if !LEGAL_CHARS.contains(&ch) {
308 panic!("unsupported character `{:?}`", ch)
310 Punct { ch, joint, span }
314 #[derive(Copy, Clone, PartialEq, Eq, Hash)]
322 fn is_valid(string: &str) -> bool {
323 let mut chars = string.chars();
324 if let Some(start) = chars.next() {
325 rustc_lexer::is_id_start(start) && chars.all(rustc_lexer::is_id_continue)
330 fn new(sym: Symbol, is_raw: bool, span: Span) -> Ident {
331 let string = sym.as_str();
332 if !Self::is_valid(&string) {
333 panic!("`{:?}` is not a valid identifier", string)
335 // Get rid of gensyms to conservatively check rawness on the string contents only.
336 if is_raw && !sym.as_interned_str().as_symbol().can_be_raw() {
337 panic!("`{}` cannot be a raw identifier", string);
339 Ident { sym, is_raw, span }
341 fn dollar_crate(span: Span) -> Ident {
342 // `$crate` is accepted as an ident only if it comes from the compiler.
343 Ident { sym: kw::DollarCrate, is_raw: false, span }
347 // FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
348 #[derive(Clone, Debug)]
354 pub(crate) struct Rustc<'a> {
361 pub fn new(cx: &'a ExtCtxt<'_>) -> Self {
362 let expn_data = cx.current_expansion.id.expn_data();
365 def_site: cx.with_def_site_ctxt(expn_data.def_site),
366 call_site: cx.with_call_site_ctxt(expn_data.call_site),
370 fn lit(&mut self, kind: token::LitKind, symbol: Symbol, suffix: Option<Symbol>) -> Literal {
372 lit: token::Lit::new(kind, symbol, suffix),
373 span: server::Span::call_site(self),
378 impl server::Types for Rustc<'_> {
379 type TokenStream = TokenStream;
380 type TokenStreamBuilder = tokenstream::TokenStreamBuilder;
381 type TokenStreamIter = TokenStreamIter;
385 type Literal = Literal;
386 type SourceFile = Lrc<SourceFile>;
387 type MultiSpan = Vec<Span>;
388 type Diagnostic = Diagnostic;
392 impl server::TokenStream for Rustc<'_> {
393 fn new(&mut self) -> Self::TokenStream {
396 fn is_empty(&mut self, stream: &Self::TokenStream) -> bool {
399 fn from_str(&mut self, src: &str) -> Self::TokenStream {
400 parse::parse_stream_from_source_str(
401 FileName::proc_macro_source_code(src),
404 Some(self.call_site),
407 fn to_string(&mut self, stream: &Self::TokenStream) -> String {
412 tree: TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>,
413 ) -> Self::TokenStream {
416 fn into_iter(&mut self, stream: Self::TokenStream) -> Self::TokenStreamIter {
418 cursor: stream.trees(),
424 impl server::TokenStreamBuilder for Rustc<'_> {
425 fn new(&mut self) -> Self::TokenStreamBuilder {
426 tokenstream::TokenStreamBuilder::new()
428 fn push(&mut self, builder: &mut Self::TokenStreamBuilder, stream: Self::TokenStream) {
429 builder.push(stream);
431 fn build(&mut self, builder: Self::TokenStreamBuilder) -> Self::TokenStream {
436 impl server::TokenStreamIter for Rustc<'_> {
439 iter: &mut Self::TokenStreamIter,
440 ) -> Option<TokenTree<Self::Group, Self::Punct, Self::Ident, Self::Literal>> {
442 let tree = iter.stack.pop().or_else(|| {
443 let next = iter.cursor.next_with_joint()?;
444 Some(TokenTree::from_internal((next, self.sess, &mut iter.stack)))
446 // HACK: The condition "dummy span + group with empty delimiter" represents an AST
447 // fragment approximately converted into a token stream. This may happen, for
448 // example, with inputs to proc macro attributes, including derives. Such "groups"
449 // need to flattened during iteration over stream's token trees.
450 // Eventually this needs to be removed in favor of keeping original token trees
451 // and not doing the roundtrip through AST.
452 if let TokenTree::Group(ref group) = tree {
453 if group.delimiter == Delimiter::None && group.span.entire().is_dummy() {
454 iter.cursor.append(group.stream.clone());
463 impl server::Group for Rustc<'_> {
464 fn new(&mut self, delimiter: Delimiter, stream: Self::TokenStream) -> Self::Group {
468 span: DelimSpan::from_single(server::Span::call_site(self)),
471 fn delimiter(&mut self, group: &Self::Group) -> Delimiter {
474 fn stream(&mut self, group: &Self::Group) -> Self::TokenStream {
477 fn span(&mut self, group: &Self::Group) -> Self::Span {
480 fn span_open(&mut self, group: &Self::Group) -> Self::Span {
483 fn span_close(&mut self, group: &Self::Group) -> Self::Span {
486 fn set_span(&mut self, group: &mut Self::Group, span: Self::Span) {
487 group.span = DelimSpan::from_single(span);
491 impl server::Punct for Rustc<'_> {
492 fn new(&mut self, ch: char, spacing: Spacing) -> Self::Punct {
493 Punct::new(ch, spacing == Spacing::Joint, server::Span::call_site(self))
495 fn as_char(&mut self, punct: Self::Punct) -> char {
498 fn spacing(&mut self, punct: Self::Punct) -> Spacing {
505 fn span(&mut self, punct: Self::Punct) -> Self::Span {
508 fn with_span(&mut self, punct: Self::Punct, span: Self::Span) -> Self::Punct {
509 Punct { span, ..punct }
513 impl server::Ident for Rustc<'_> {
514 fn new(&mut self, string: &str, span: Self::Span, is_raw: bool) -> Self::Ident {
515 Ident::new(Symbol::intern(string), is_raw, span)
517 fn span(&mut self, ident: Self::Ident) -> Self::Span {
520 fn with_span(&mut self, ident: Self::Ident, span: Self::Span) -> Self::Ident {
521 Ident { span, ..ident }
525 impl server::Literal for Rustc<'_> {
526 // FIXME(eddyb) `Literal` should not expose internal `Debug` impls.
527 fn debug(&mut self, literal: &Self::Literal) -> String {
528 format!("{:?}", literal)
530 fn integer(&mut self, n: &str) -> Self::Literal {
531 self.lit(token::Integer, Symbol::intern(n), None)
533 fn typed_integer(&mut self, n: &str, kind: &str) -> Self::Literal {
534 self.lit(token::Integer, Symbol::intern(n), Some(Symbol::intern(kind)))
536 fn float(&mut self, n: &str) -> Self::Literal {
537 self.lit(token::Float, Symbol::intern(n), None)
539 fn f32(&mut self, n: &str) -> Self::Literal {
540 self.lit(token::Float, Symbol::intern(n), Some(sym::f32))
542 fn f64(&mut self, n: &str) -> Self::Literal {
543 self.lit(token::Float, Symbol::intern(n), Some(sym::f64))
545 fn string(&mut self, string: &str) -> Self::Literal {
546 let mut escaped = String::new();
547 for ch in string.chars() {
548 escaped.extend(ch.escape_debug());
550 self.lit(token::Str, Symbol::intern(&escaped), None)
552 fn character(&mut self, ch: char) -> Self::Literal {
553 let mut escaped = String::new();
554 escaped.extend(ch.escape_unicode());
555 self.lit(token::Char, Symbol::intern(&escaped), None)
557 fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal {
561 .flat_map(ascii::escape_default)
562 .map(Into::<char>::into)
563 .collect::<String>();
564 self.lit(token::ByteStr, Symbol::intern(&string), None)
566 fn span(&mut self, literal: &Self::Literal) -> Self::Span {
569 fn set_span(&mut self, literal: &mut Self::Literal, span: Self::Span) {
574 literal: &Self::Literal,
577 ) -> Option<Self::Span> {
578 let span = literal.span;
579 let length = span.hi().to_usize() - span.lo().to_usize();
581 let start = match start {
582 Bound::Included(lo) => lo,
583 Bound::Excluded(lo) => lo + 1,
584 Bound::Unbounded => 0,
587 let end = match end {
588 Bound::Included(hi) => hi + 1,
589 Bound::Excluded(hi) => hi,
590 Bound::Unbounded => length,
593 // Bounds check the values, preventing addition overflow and OOB spans.
594 if start > u32::max_value() as usize
595 || end > u32::max_value() as usize
596 || (u32::max_value() - start as u32) < span.lo().to_u32()
597 || (u32::max_value() - end as u32) < span.lo().to_u32()
604 let new_lo = span.lo() + BytePos::from_usize(start);
605 let new_hi = span.lo() + BytePos::from_usize(end);
606 Some(span.with_lo(new_lo).with_hi(new_hi))
610 impl server::SourceFile for Rustc<'_> {
611 fn eq(&mut self, file1: &Self::SourceFile, file2: &Self::SourceFile) -> bool {
612 Lrc::ptr_eq(file1, file2)
614 fn path(&mut self, file: &Self::SourceFile) -> String {
616 FileName::Real(ref path) => path
618 .expect("non-UTF8 file path in `proc_macro::SourceFile::path`")
620 _ => file.name.to_string(),
623 fn is_real(&mut self, file: &Self::SourceFile) -> bool {
628 impl server::MultiSpan for Rustc<'_> {
629 fn new(&mut self) -> Self::MultiSpan {
632 fn push(&mut self, spans: &mut Self::MultiSpan, span: Self::Span) {
637 impl server::Diagnostic for Rustc<'_> {
638 fn new(&mut self, level: Level, msg: &str, spans: Self::MultiSpan) -> Self::Diagnostic {
639 let mut diag = Diagnostic::new(level.to_internal(), msg);
640 diag.set_span(MultiSpan::from_spans(spans));
645 diag: &mut Self::Diagnostic,
648 spans: Self::MultiSpan,
650 diag.sub(level.to_internal(), msg, MultiSpan::from_spans(spans), None);
652 fn emit(&mut self, diag: Self::Diagnostic) {
653 self.sess.span_diagnostic.emit_diagnostic(&diag);
657 impl server::Span for Rustc<'_> {
658 fn debug(&mut self, span: Self::Span) -> String {
659 format!("{:?} bytes({}..{})", span.ctxt(), span.lo().0, span.hi().0)
661 fn def_site(&mut self) -> Self::Span {
664 fn call_site(&mut self) -> Self::Span {
667 fn source_file(&mut self, span: Self::Span) -> Self::SourceFile {
668 self.sess.source_map().lookup_char_pos(span.lo()).file
670 fn parent(&mut self, span: Self::Span) -> Option<Self::Span> {
673 fn source(&mut self, span: Self::Span) -> Self::Span {
674 span.source_callsite()
676 fn start(&mut self, span: Self::Span) -> LineColumn {
677 let loc = self.sess.source_map().lookup_char_pos(span.lo());
680 column: loc.col.to_usize(),
683 fn end(&mut self, span: Self::Span) -> LineColumn {
684 let loc = self.sess.source_map().lookup_char_pos(span.hi());
687 column: loc.col.to_usize(),
690 fn join(&mut self, first: Self::Span, second: Self::Span) -> Option<Self::Span> {
691 let self_loc = self.sess.source_map().lookup_char_pos(first.lo());
692 let other_loc = self.sess.source_map().lookup_char_pos(second.lo());
694 if self_loc.file.name != other_loc.file.name {
698 Some(first.to(second))
700 fn resolved_at(&mut self, span: Self::Span, at: Self::Span) -> Self::Span {
701 span.with_ctxt(at.ctxt())
703 fn source_text(&mut self, span: Self::Span) -> Option<String> {
704 self.sess.source_map().span_to_snippet(span).ok()