Files
rust/compiler/rustc_mir_build/src/build/misc.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

71 lines
2.4 KiB
Rust
Raw Normal View History

2015-08-18 17:59:21 -04:00
//! Miscellaneous builder routines that are not specific to building any particular
//! kind of thing.
2019-02-08 06:28:15 +09:00
use crate::build::Builder;
2020-03-29 16:41:09 +02:00
use rustc_middle::mir::*;
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;
use rustc_trait_selection::infer::InferCtxtExt;
use tracing::debug;
2015-08-18 17:59:21 -04:00
impl<'a, 'tcx> Builder<'a, 'tcx> {
2019-02-08 14:53:55 +01:00
/// Adds a new temporary value of type `ty` storing the result of
2015-08-18 17:59:21 -04:00
/// evaluating `expr`.
///
/// N.B., **No cleanup is scheduled for this temporary.** You should
2015-08-18 17:59:21 -04:00
/// call `schedule_drop` once the temporary is initialized.
pub(crate) fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
2023-10-04 17:50:03 +00:00
let temp = self.local_decls.push(LocalDecl::new(ty, span));
let place = Place::from(temp);
2015-08-18 17:59:21 -04:00
debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
place
2015-08-18 17:59:21 -04:00
}
/// Convenience function for creating a literal operand, one
/// without any user type annotation.
pub(crate) fn literal_operand(&mut self, span: Span, const_: Const<'tcx>) -> Operand<'tcx> {
let constant = Box::new(ConstOperand { span, user_ty: None, const_ });
Operand::Constant(constant)
2015-08-18 17:59:21 -04:00
}
/// Returns a zero literal operand for the appropriate type, works for
/// bool, char and integers.
pub(crate) fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
let literal = Const::from_bits(self.tcx, 0, ty::ParamEnv::empty().and(ty));
self.literal_operand(span, literal)
}
pub(crate) fn push_usize(
&mut self,
block: BasicBlock,
source_info: SourceInfo,
value: u64,
2017-12-01 14:31:47 +02:00
) -> Place<'tcx> {
let usize_ty = self.tcx.types.usize;
2017-04-11 23:52:51 +03:00
let temp = self.temp(usize_ty, source_info.span);
2015-08-18 17:59:21 -04:00
self.cfg.push_assign_constant(
block,
source_info,
temp,
ConstOperand {
span: source_info.span,
user_ty: None,
const_: Const::from_usize(self.tcx, value),
2015-08-18 17:59:21 -04:00
},
);
temp
}
pub(crate) fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
let tcx = self.tcx;
let ty = place.ty(&self.local_decls, tcx).ty;
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty) {
Operand::Move(place)
} else {
Operand::Copy(place)
}
}
2015-08-18 17:59:21 -04:00
}