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        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_mir_build/src/builder/misc.rs:21",
                        "rustc_mir_build::builder::misc", ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_mir_build/src/builder/misc.rs"),
                        ::tracing_core::__macro_support::Option::Some(21u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_mir_build::builder::misc"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&format_args!("temp: created temp {0:?} with type {1:?}",
                                                    place, self.local_decls[temp].ty) as &dyn Value))])
            });
    } else { ; }
};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}