|
1 | 1 | use crate::base::ExtCtxt; |
2 | | -use rustc_ast::attr; |
3 | 2 | use rustc_ast::ptr::P; |
4 | 3 | use rustc_ast::{self as ast, AttrVec, BlockCheckMode, Expr, LocalKind, PatKind, UnOp}; |
5 | | -use rustc_data_structures::sync::Lrc; |
| 4 | +use rustc_ast::{attr, token, util::literal}; |
6 | 5 | use rustc_span::source_map::Spanned; |
7 | 6 | use rustc_span::symbol::{kw, sym, Ident, Symbol}; |
8 | 7 | use rustc_span::Span; |
@@ -332,36 +331,36 @@ impl<'a> ExtCtxt<'a> { |
332 | 331 | self.expr_struct(span, self.path_ident(span, id), fields) |
333 | 332 | } |
334 | 333 |
|
335 | | - fn expr_lit(&self, span: Span, lit_kind: ast::LitKind) -> P<ast::Expr> { |
336 | | - let token_lit = lit_kind.synthesize_token_lit(); |
337 | | - self.expr(span, ast::ExprKind::Lit(token_lit)) |
| 334 | + pub fn expr_usize(&self, span: Span, n: usize) -> P<ast::Expr> { |
| 335 | + let suffix = Some(ast::UintTy::Usize.name()); |
| 336 | + let lit = token::Lit::new(token::Integer, sym::integer(n), suffix); |
| 337 | + self.expr(span, ast::ExprKind::Lit(lit)) |
338 | 338 | } |
339 | 339 |
|
340 | | - pub fn expr_usize(&self, span: Span, i: usize) -> P<ast::Expr> { |
341 | | - self.expr_lit( |
342 | | - span, |
343 | | - ast::LitKind::Int(i as u128, ast::LitIntType::Unsigned(ast::UintTy::Usize)), |
344 | | - ) |
345 | | - } |
346 | | - |
347 | | - pub fn expr_u32(&self, sp: Span, u: u32) -> P<ast::Expr> { |
348 | | - self.expr_lit(sp, ast::LitKind::Int(u as u128, ast::LitIntType::Unsigned(ast::UintTy::U32))) |
| 340 | + pub fn expr_u32(&self, span: Span, n: u32) -> P<ast::Expr> { |
| 341 | + let suffix = Some(ast::UintTy::U32.name()); |
| 342 | + let lit = token::Lit::new(token::Integer, sym::integer(n), suffix); |
| 343 | + self.expr(span, ast::ExprKind::Lit(lit)) |
349 | 344 | } |
350 | 345 |
|
351 | | - pub fn expr_bool(&self, sp: Span, value: bool) -> P<ast::Expr> { |
352 | | - self.expr_lit(sp, ast::LitKind::Bool(value)) |
| 346 | + pub fn expr_bool(&self, span: Span, value: bool) -> P<ast::Expr> { |
| 347 | + let lit = token::Lit::new(token::Bool, if value { kw::True } else { kw::False }, None); |
| 348 | + self.expr(span, ast::ExprKind::Lit(lit)) |
353 | 349 | } |
354 | 350 |
|
355 | | - pub fn expr_str(&self, sp: Span, s: Symbol) -> P<ast::Expr> { |
356 | | - self.expr_lit(sp, ast::LitKind::Str(s, ast::StrStyle::Cooked)) |
| 351 | + pub fn expr_str(&self, span: Span, s: Symbol) -> P<ast::Expr> { |
| 352 | + let lit = token::Lit::new(token::Str, literal::escape_string_symbol(s), None); |
| 353 | + self.expr(span, ast::ExprKind::Lit(lit)) |
357 | 354 | } |
358 | 355 |
|
359 | | - pub fn expr_char(&self, sp: Span, ch: char) -> P<ast::Expr> { |
360 | | - self.expr_lit(sp, ast::LitKind::Char(ch)) |
| 356 | + pub fn expr_char(&self, span: Span, ch: char) -> P<ast::Expr> { |
| 357 | + let lit = token::Lit::new(token::Char, literal::escape_char_symbol(ch), None); |
| 358 | + self.expr(span, ast::ExprKind::Lit(lit)) |
361 | 359 | } |
362 | 360 |
|
363 | | - pub fn expr_byte_str(&self, sp: Span, bytes: Vec<u8>) -> P<ast::Expr> { |
364 | | - self.expr_lit(sp, ast::LitKind::ByteStr(Lrc::from(bytes), ast::StrStyle::Cooked)) |
| 361 | + pub fn expr_byte_str(&self, span: Span, bytes: Vec<u8>) -> P<ast::Expr> { |
| 362 | + let lit = token::Lit::new(token::ByteStr, literal::escape_byte_str_symbol(&bytes), None); |
| 363 | + self.expr(span, ast::ExprKind::Lit(lit)) |
365 | 364 | } |
366 | 365 |
|
367 | 366 | /// `[expr1, expr2, ...]` |
|
0 commit comments