]> git.proxmox.com Git - rustc.git/blame - src/librustc_builtin_macros/concat_idents.rs
New upstream version 1.42.0+dfsg1
[rustc.git] / src / librustc_builtin_macros / concat_idents.rs
CommitLineData
dfeec247
XL
1use rustc_expand::base::{self, *};
2use rustc_span::symbol::Symbol;
3use rustc_span::Span;
3157f602 4use syntax::ast;
9cc50fc6 5use syntax::ptr::P;
dfeec247
XL
6use syntax::token::{self, Token};
7use syntax::tokenstream::{TokenStream, TokenTree};
223e47cc 8
dfeec247
XL
9pub 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
e1599b0c 42 let ident = ast::Ident::new(Symbol::intern(&res_str), cx.with_call_site_ctxt(sp));
3157f602 43
dfeec247
XL
44 struct ConcatIdentsResult {
45 ident: ast::Ident,
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(),
3157f602
XL
55 }))
56 }
57
58 fn make_ty(self: Box<Self>) -> Option<P<ast::Ty>> {
59 Some(P(ast::Ty {
60 id: ast::DUMMY_NODE_ID,
e74abb32 61 kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
83c7162d 62 span: self.ident.span,
3157f602
XL
63 }))
64 }
65 }
66
83c7162d 67 Box::new(ConcatIdentsResult { ident })
223e47cc 68}