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-12-22 17:42:04 -05: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() {
|
|
|
|
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 {
|
2019-06-04 20:42:43 +03:00
|
|
|
TokenTree::Token(Token { kind: token::Comma, .. }) => {}
|
2014-01-18 01:53:10 +11: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 {
|
2019-08-31 20:08:06 +03:00
|
|
|
match e {
|
2019-12-22 17:42:04 -05:00
|
|
|
TokenTree::Token(Token { kind: token::Ident(name, _), .. }) => {
|
|
|
|
res_str.push_str(&name.as_str())
|
|
|
|
}
|
2014-01-18 01:53:10 +11:00
|
|
|
_ => {
|
|
|
|
cx.span_err(sp, "concat_idents! requires ident args.");
|
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
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2019-12-22 17:42:04 -05:00
|
|
|
struct ConcatIdentsResult {
|
2020-04-19 13:00:18 +02:00
|
|
|
ident: Ident,
|
2019-12-22 17:42:04 -05: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,
|
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
|
|
|
}
|