|
| 1 | +use rustc_ast as ast; |
| 2 | +use rustc_ast::ptr::P; |
| 3 | +use rustc_ast::token; |
| 4 | +use rustc_ast::tokenstream::TokenStream; |
| 5 | +use rustc_errors::PResult; |
| 6 | +use rustc_expand::base::{self, *}; |
| 7 | +use rustc_macros::Diagnostic; |
| 8 | +use rustc_parse::parser::Parser; |
| 9 | +use rustc_span::{symbol::Ident, Span}; |
| 10 | + |
| 11 | +#[derive(Diagnostic)] |
| 12 | +#[diag(builtin_macros_offset_of_expected_field)] |
| 13 | +struct ExpectedField { |
| 14 | + #[primary_span] |
| 15 | + span: Span, |
| 16 | +} |
| 17 | + |
| 18 | +#[derive(Diagnostic)] |
| 19 | +#[diag(builtin_macros_offset_of_expected_two_args)] |
| 20 | +struct ExpectedTwoArgs { |
| 21 | + #[primary_span] |
| 22 | + span: Span, |
| 23 | +} |
| 24 | + |
| 25 | +fn parse_field<'a>(cx: &ExtCtxt<'a>, p: &mut Parser<'a>) -> PResult<'a, Ident> { |
| 26 | + let token = p.token.uninterpolate(); |
| 27 | + let field = match token.kind { |
| 28 | + token::Ident(name, _) => Ident::new(name, token.span), |
| 29 | + token::Literal(token::Lit { kind: token::Integer, symbol, suffix: None }) => { |
| 30 | + Ident::new(symbol, token.span) |
| 31 | + } |
| 32 | + _ => return Err(cx.create_err(ExpectedField { span: p.token.span })), |
| 33 | + }; |
| 34 | + |
| 35 | + p.bump(); |
| 36 | + |
| 37 | + Ok(field) |
| 38 | +} |
| 39 | + |
| 40 | +fn parse_args<'a>( |
| 41 | + cx: &mut ExtCtxt<'a>, |
| 42 | + sp: Span, |
| 43 | + tts: TokenStream, |
| 44 | +) -> PResult<'a, (P<ast::Ty>, P<[Ident]>)> { |
| 45 | + let mut p = cx.new_parser_from_tts(tts); |
| 46 | + |
| 47 | + let container = p.parse_ty()?; |
| 48 | + |
| 49 | + p.expect(&token::Comma)?; |
| 50 | + |
| 51 | + if p.eat(&token::Eof) { |
| 52 | + return Err(cx.create_err(ExpectedTwoArgs { span: sp })); |
| 53 | + } |
| 54 | + |
| 55 | + let mut fields = Vec::new(); |
| 56 | + |
| 57 | + loop { |
| 58 | + let field = parse_field(cx, &mut p)?; |
| 59 | + fields.push(field); |
| 60 | + |
| 61 | + if p.eat(&token::Dot) { |
| 62 | + continue; |
| 63 | + } |
| 64 | + |
| 65 | + p.eat(&token::Comma); |
| 66 | + |
| 67 | + if !p.eat(&token::Eof) { |
| 68 | + return Err(cx.create_err(ExpectedTwoArgs { span: sp })); |
| 69 | + } |
| 70 | + |
| 71 | + break; |
| 72 | + } |
| 73 | + |
| 74 | + Ok((container, fields.into())) |
| 75 | +} |
| 76 | + |
| 77 | +pub fn expand_offset_of<'cx>( |
| 78 | + cx: &'cx mut ExtCtxt<'_>, |
| 79 | + sp: Span, |
| 80 | + tts: TokenStream, |
| 81 | +) -> Box<dyn base::MacResult + 'cx> { |
| 82 | + match parse_args(cx, sp, tts) { |
| 83 | + Ok((container, fields)) => { |
| 84 | + let expr = P(ast::Expr { |
| 85 | + id: ast::DUMMY_NODE_ID, |
| 86 | + kind: ast::ExprKind::OffsetOf(container, fields), |
| 87 | + span: sp, |
| 88 | + attrs: ast::AttrVec::new(), |
| 89 | + tokens: None, |
| 90 | + }); |
| 91 | + |
| 92 | + MacEager::expr(expr) |
| 93 | + } |
| 94 | + Err(mut err) => { |
| 95 | + err.emit(); |
| 96 | + DummyResult::any(sp) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments