2020-04-27 23:26:11 +05:30
|
|
|
use rustc_ast as ast;
|
2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token::{self, Token};
|
|
|
|
use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
2019-12-29 17:23:55 +03:00
|
|
|
use rustc_expand::base::{self, *};
|
2020-04-19 13:00:18 +02:00
|
|
|
use rustc_span::symbol::{Ident, Symbol};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::Span;
|
2014-04-02 16:54:22 -07:00
|
|
|
|
2019-09-03 17:43:25 +03:00
|
|
|
pub fn expand_concat_idents<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2018-05-02 20:51:39 -04:00
|
|
|
if tts.is_empty() {
|
2021-10-03 15:53:02 +09:00
|
|
|
cx.span_err(sp, "concat_idents! takes 1 or more arguments");
|
2018-12-30 00:56:55 +03:00
|
|
|
return DummyResult::any(sp);
|
2018-05-02 20:51:39 -04:00
|
|
|
}
|
|
|
|
|
2014-05-22 16:57:53 -07:00
|
|
|
let mut res_str = String::new();
|
2019-08-31 20:08:06 +03:00
|
|
|
for (i, e) in tts.into_trees().enumerate() {
|
2012-12-12 17:08:09 -08:00
|
|
|
if i & 1 == 1 {
|
2019-08-31 20:08:06 +03:00
|
|
|
match e {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
TokenTree::Token(Token { kind: token::Comma, .. }, _) => {}
|
2014-01-18 01:53:10 +11:00
|
|
|
_ => {
|
2021-10-03 15:53:02 +09:00
|
|
|
cx.span_err(sp, "concat_idents! expecting comma");
|
2018-12-30 00:56:55 +03:00
|
|
|
return DummyResult::any(sp);
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
|
|
|
} else {
|
Remove `TreeAndSpacing`.
A `TokenStream` contains a `Lrc<Vec<(TokenTree, Spacing)>>`. But this is
not quite right. `Spacing` makes sense for `TokenTree::Token`, but does
not make sense for `TokenTree::Delimited`, because a
`TokenTree::Delimited` cannot be joined with another `TokenTree`.
This commit fixes this problem, by adding `Spacing` to `TokenTree::Token`,
changing `TokenStream` to contain a `Lrc<Vec<TokenTree>>`, and removing the
`TreeAndSpacing` typedef.
The commit removes these two impls:
- `impl From<TokenTree> for TokenStream`
- `impl From<TokenTree> for TreeAndSpacing`
These were useful, but also resulted in code with many `.into()` calls
that was hard to read, particularly for anyone not highly familiar with
the relevant types. This commit makes some other changes to compensate:
- `TokenTree::token()` becomes `TokenTree::token_{alone,joint}()`.
- `TokenStream::token_{alone,joint}()` are added.
- `TokenStream::delimited` is added.
This results in things like this:
```rust
TokenTree::token(token::Semi, stmt.span).into()
```
changing to this:
```rust
TokenStream::token_alone(token::Semi, stmt.span)
```
This makes the type of the result, and its spacing, clearer.
These changes also simplifies `Cursor` and `CursorRef`, because they no longer
need to distinguish between `next` and `next_with_spacing`.
2022-07-28 10:31:04 +10:00
|
|
|
if let TokenTree::Token(token, _) = e {
|
2020-09-27 19:47:52 +03:00
|
|
|
if let Some((ident, _)) = token.ident() {
|
2021-12-15 14:39:23 +11:00
|
|
|
res_str.push_str(ident.name.as_str());
|
2020-09-27 19:47:52 +03:00
|
|
|
continue;
|
2016-06-06 20:22:48 +05:30
|
|
|
}
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
2020-09-27 19:47:52 +03:00
|
|
|
|
2021-10-03 15:53:02 +09:00
|
|
|
cx.span_err(sp, "concat_idents! requires ident args");
|
2020-09-27 19:47:52 +03:00
|
|
|
return DummyResult::any(sp);
|
2012-12-12 17:08:09 -08:00
|
|
|
}
|
2011-08-03 11:46:32 -07:00
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
|
2020-04-19 13:00:18 +02:00
|
|
|
let ident = Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
|
2018-03-18 16:47:09 +03:00
|
|
|
|
|
|
|
struct ConcatIdentsResult {
|
2020-04-19 13:00:18 +02:00
|
|
|
ident: Ident,
|
2018-03-18 16:47:09 +03:00
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
|
2018-03-18 16:47:09 +03:00
|
|
|
impl base::MacResult for ConcatIdentsResult {
|
2016-05-19 05:22:42 +00:00
|
|
|
fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> {
|
|
|
|
Some(P(ast::Expr {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2019-09-26 14:39:48 +01:00
|
|
|
kind: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
|
2018-03-18 16:47:09 +03:00
|
|
|
span: self.ident.span,
|
2019-12-03 16:38:34 +01:00
|
|
|
attrs: ast::AttrVec::new(),
|
2020-05-19 16:56:20 -04:00
|
|
|
tokens: None,
|
2016-05-19 05:22:42 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
|
|
|
|
Some(P(ast::Ty {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
2019-09-26 17:25:31 +01:00
|
|
|
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
|
2018-03-18 16:47:09 +03:00
|
|
|
span: self.ident.span,
|
2020-08-21 18:18:04 -04:00
|
|
|
tokens: None,
|
2016-05-19 05:22:42 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 16:47:09 +03:00
|
|
|
Box::new(ConcatIdentsResult { ident })
|
2011-08-04 16:20:09 -07:00
|
|
|
}
|