2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
|
|
|
use rustc_ast::token::{self, Token};
|
|
|
|
use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
2019-12-29 14:23:55 +00:00
|
|
|
use rustc_expand::base::{self, *};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2014-04-02 23:54:22 +00:00
|
|
|
|
2019-09-03 14:43:25 +00:00
|
|
|
pub fn expand_concat_idents<'cx>(
|
|
|
|
cx: &'cx mut ExtCtxt<'_>,
|
|
|
|
sp: Span,
|
|
|
|
tts: TokenStream,
|
|
|
|
) -> Box<dyn base::MacResult + 'cx> {
|
2018-05-03 00:51:39 +00:00
|
|
|
if tts.is_empty() {
|
2021-10-03 06:53:02 +00:00
|
|
|
cx.span_err(sp, "concat_idents! takes 1 or more arguments");
|
2018-12-29 21:56:55 +00:00
|
|
|
return DummyResult::any(sp);
|
2018-05-03 00:51:39 +00:00
|
|
|
}
|
|
|
|
|
2014-05-22 23:57:53 +00:00
|
|
|
let mut res_str = String::new();
|
2019-08-31 17:08:06 +00:00
|
|
|
for (i, e) in tts.into_trees().enumerate() {
|
2012-12-13 01:08:09 +00:00
|
|
|
if i & 1 == 1 {
|
2019-08-31 17:08:06 +00:00
|
|
|
match e {
|
2019-06-04 17:42:43 +00:00
|
|
|
TokenTree::Token(Token { kind: token::Comma, .. }) => {}
|
2014-01-17 14:53:10 +00:00
|
|
|
_ => {
|
2021-10-03 06:53:02 +00:00
|
|
|
cx.span_err(sp, "concat_idents! expecting comma");
|
2018-12-29 21:56:55 +00:00
|
|
|
return DummyResult::any(sp);
|
2016-06-06 14:52:48 +00:00
|
|
|
}
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
|
|
|
} else {
|
2020-09-27 16:47:52 +00:00
|
|
|
if let TokenTree::Token(token) = e {
|
|
|
|
if let Some((ident, _)) = token.ident() {
|
2021-12-15 03:39:23 +00:00
|
|
|
res_str.push_str(ident.name.as_str());
|
2020-09-27 16:47:52 +00:00
|
|
|
continue;
|
2016-06-06 14:52:48 +00:00
|
|
|
}
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2020-09-27 16:47:52 +00:00
|
|
|
|
2021-10-03 06:53:02 +00:00
|
|
|
cx.span_err(sp, "concat_idents! requires ident args");
|
2020-09-27 16:47:52 +00:00
|
|
|
return DummyResult::any(sp);
|
2012-12-13 01:08:09 +00:00
|
|
|
}
|
2011-08-03 18:46:32 +00:00
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
|
2020-04-19 11:00:18 +00:00
|
|
|
let ident = Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
|
2018-03-18 13:47:09 +00:00
|
|
|
|
|
|
|
struct ConcatIdentsResult {
|
2020-04-19 11:00:18 +00:00
|
|
|
ident: Ident,
|
2018-03-18 13:47:09 +00:00
|
|
|
}
|
2016-05-19 05:22:42 +00:00
|
|
|
|
2018-03-18 13:47:09 +00: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 13:39:48 +00:00
|
|
|
kind: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)),
|
2018-03-18 13:47:09 +00:00
|
|
|
span: self.ident.span,
|
2019-12-03 15:38:34 +00:00
|
|
|
attrs: ast::AttrVec::new(),
|
2020-05-19 20:56:20 +00: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 16:25:31 +00:00
|
|
|
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
|
2018-03-18 13:47:09 +00:00
|
|
|
span: self.ident.span,
|
2020-08-21 22:18:04 +00:00
|
|
|
tokens: None,
|
2016-05-19 05:22:42 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-18 13:47:09 +00:00
|
|
|
Box::new(ConcatIdentsResult { ident })
|
2011-08-04 23:20:09 +00:00
|
|
|
}
|