Add some helper functions to LoweringContext.

This commit is contained in:
Mara Bos 2023-01-11 21:35:46 +01:00
parent ef4046e4f3
commit 43105c10d2

View File

@ -16,7 +16,7 @@ use rustc_hir::def::Res;
use rustc_hir::definitions::DefPathData;
use rustc_session::errors::report_lit_error;
use rustc_span::source_map::{respan, DesugaringKind, Span, Spanned};
use rustc_span::symbol::{sym, Ident};
use rustc_span::symbol::{sym, Ident, Symbol};
use rustc_span::DUMMY_SP;
use thin_vec::thin_vec;
@ -1757,6 +1757,43 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.arena.alloc(self.expr(sp, hir::ExprKind::Tup(&[])))
}
pub(super) fn expr_usize(&mut self, sp: Span, value: usize) -> hir::Expr<'hir> {
self.expr(
sp,
hir::ExprKind::Lit(hir::Lit {
span: sp,
node: ast::LitKind::Int(
value as u128,
ast::LitIntType::Unsigned(ast::UintTy::Usize),
),
}),
)
}
pub(super) fn expr_u32(&mut self, sp: Span, value: u32) -> hir::Expr<'hir> {
self.expr(
sp,
hir::ExprKind::Lit(hir::Lit {
span: sp,
node: ast::LitKind::Int(value.into(), ast::LitIntType::Unsigned(ast::UintTy::U32)),
}),
)
}
pub(super) fn expr_char(&mut self, sp: Span, value: char) -> hir::Expr<'hir> {
self.expr(sp, hir::ExprKind::Lit(hir::Lit { span: sp, node: ast::LitKind::Char(value) }))
}
pub(super) fn expr_str(&mut self, sp: Span, value: Symbol) -> hir::Expr<'hir> {
self.expr(
sp,
hir::ExprKind::Lit(hir::Lit {
span: sp,
node: ast::LitKind::Str(value, ast::StrStyle::Cooked),
}),
)
}
fn expr_call_mut(
&mut self,
span: Span,
@ -1808,6 +1845,27 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
}
/// `<LangItem>::name`
pub(super) fn expr_lang_item_type_relative(
&mut self,
span: Span,
lang_item: hir::LangItem,
name: Symbol,
) -> hir::Expr<'hir> {
let path = hir::ExprKind::Path(hir::QPath::TypeRelative(
self.arena.alloc(self.ty(
span,
hir::TyKind::Path(hir::QPath::LangItem(lang_item, self.lower_span(span), None)),
)),
self.arena.alloc(hir::PathSegment::new(
Ident::new(name, span),
self.next_id(),
Res::Err,
)),
));
self.expr(span, path)
}
pub(super) fn expr_ident(
&mut self,
sp: Span,
@ -1866,6 +1924,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
self.expr(b.span, hir::ExprKind::Block(b, None))
}
pub(super) fn expr_array_ref(
&mut self,
span: Span,
elements: &'hir [hir::Expr<'hir>],
) -> hir::Expr<'hir> {
let addrof = hir::ExprKind::AddrOf(
hir::BorrowKind::Ref,
hir::Mutability::Not,
self.arena.alloc(self.expr(span, hir::ExprKind::Array(elements))),
);
self.expr(span, addrof)
}
pub(super) fn expr(&mut self, span: Span, kind: hir::ExprKind<'hir>) -> hir::Expr<'hir> {
let hir_id = self.next_id();
hir::Expr { hir_id, kind, span: self.lower_span(span) }