rustc_mir_build/builder/
misc.rs

1//! Miscellaneous builder routines that are not specific to building any particular
2//! kind of thing.
3
4use rustc_middle::mir::*;
5use rustc_middle::ty::{self, Ty};
6use rustc_span::Span;
7use rustc_trait_selection::infer::InferCtxtExt;
8use tracing::debug;
9
10use crate::builder::Builder;
11
12impl<'a, 'tcx> Builder<'a, 'tcx> {
13    /// Adds a new temporary value of type `ty` storing the result of
14    /// evaluating `expr`.
15    ///
16    /// N.B., **No cleanup is scheduled for this temporary.** You should
17    /// call `schedule_drop` once the temporary is initialized.
18    pub(crate) fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
19        let temp = self.local_decls.push(LocalDecl::new(ty, span));
20        let place = Place::from(temp);
21        debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
22        place
23    }
24
25    /// Convenience function for creating a literal operand, one
26    /// without any user type annotation.
27    pub(crate) fn literal_operand(&mut self, span: Span, const_: Const<'tcx>) -> Operand<'tcx> {
28        let constant = Box::new(ConstOperand { span, user_ty: None, const_ });
29        Operand::Constant(constant)
30    }
31
32    /// Returns a zero literal operand for the appropriate type, works for
33    /// bool, char and integers.
34    pub(crate) fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
35        let literal = Const::from_bits(self.tcx, 0, ty::TypingEnv::fully_monomorphized(), ty);
36
37        self.literal_operand(span, literal)
38    }
39
40    pub(crate) fn push_usize(
41        &mut self,
42        block: BasicBlock,
43        source_info: SourceInfo,
44        value: u64,
45    ) -> Place<'tcx> {
46        let usize_ty = self.tcx.types.usize;
47        let temp = self.temp(usize_ty, source_info.span);
48        self.cfg.push_assign_constant(
49            block,
50            source_info,
51            temp,
52            ConstOperand {
53                span: source_info.span,
54                user_ty: None,
55                const_: Const::from_usize(self.tcx, value),
56            },
57        );
58        temp
59    }
60
61    pub(crate) fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
62        let tcx = self.tcx;
63        let ty = place.ty(&self.local_decls, tcx).ty;
64        if self.infcx.type_is_copy_modulo_regions(self.param_env, ty) {
65            Operand::Copy(place)
66        } else {
67            Operand::Move(place)
68        }
69    }
70}