]>
Commit | Line | Data |
---|---|---|
3dfed10e | 1 | use rustc_ast as ast; |
74b04a01 XL |
2 | use rustc_ast::ptr::P; |
3 | use rustc_ast::token::{self, Token}; | |
4 | use rustc_ast::tokenstream::{TokenStream, TokenTree}; | |
dfeec247 | 5 | use rustc_expand::base::{self, *}; |
f9f354fc | 6 | use rustc_span::symbol::{Ident, Symbol}; |
dfeec247 | 7 | use rustc_span::Span; |
223e47cc | 8 | |
dfeec247 XL |
9 | pub fn expand_concat_idents<'cx>( |
10 | cx: &'cx mut ExtCtxt<'_>, | |
11 | sp: Span, | |
12 | tts: TokenStream, | |
13 | ) -> Box<dyn base::MacResult + 'cx> { | |
83c7162d XL |
14 | if tts.is_empty() { |
15 | cx.span_err(sp, "concat_idents! takes 1 or more arguments."); | |
0731742a | 16 | return DummyResult::any(sp); |
83c7162d XL |
17 | } |
18 | ||
1a4d82fc | 19 | let mut res_str = String::new(); |
e1599b0c | 20 | for (i, e) in tts.into_trees().enumerate() { |
223e47cc | 21 | if i & 1 == 1 { |
e1599b0c | 22 | match e { |
dc9dc135 | 23 | TokenTree::Token(Token { kind: token::Comma, .. }) => {} |
1a4d82fc JJ |
24 | _ => { |
25 | cx.span_err(sp, "concat_idents! expecting comma."); | |
0731742a | 26 | return DummyResult::any(sp); |
9e0c209e | 27 | } |
223e47cc LB |
28 | } |
29 | } else { | |
e1599b0c | 30 | match e { |
dfeec247 XL |
31 | TokenTree::Token(Token { kind: token::Ident(name, _), .. }) => { |
32 | res_str.push_str(&name.as_str()) | |
33 | } | |
1a4d82fc JJ |
34 | _ => { |
35 | cx.span_err(sp, "concat_idents! requires ident args."); | |
0731742a | 36 | return DummyResult::any(sp); |
9e0c209e | 37 | } |
223e47cc LB |
38 | } |
39 | } | |
40 | } | |
223e47cc | 41 | |
f9f354fc | 42 | let ident = Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp)); |
3157f602 | 43 | |
dfeec247 | 44 | struct ConcatIdentsResult { |
f9f354fc | 45 | ident: Ident, |
dfeec247 | 46 | } |
3157f602 | 47 | |
83c7162d | 48 | impl base::MacResult for ConcatIdentsResult { |
3157f602 XL |
49 | fn make_expr(self: Box<Self>) -> Option<P<ast::Expr>> { |
50 | Some(P(ast::Expr { | |
51 | id: ast::DUMMY_NODE_ID, | |
e74abb32 | 52 | kind: ast::ExprKind::Path(None, ast::Path::from_ident(self.ident)), |
83c7162d | 53 | span: self.ident.span, |
dfeec247 | 54 | attrs: ast::AttrVec::new(), |
f9f354fc | 55 | tokens: None, |
3157f602 XL |
56 | })) |
57 | } | |
58 | ||
59 | fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> { | |
60 | Some(P(ast::Ty { | |
61 | id: ast::DUMMY_NODE_ID, | |
e74abb32 | 62 | kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)), |
83c7162d | 63 | span: self.ident.span, |
3157f602 XL |
64 | })) |
65 | } | |
66 | } | |
67 | ||
83c7162d | 68 | Box::new(ConcatIdentsResult { ident }) |
223e47cc | 69 | } |