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;
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2020-03-29 16:41:09 +02:00
|
|
|
use rustc_middle::mir::*;
|
2021-03-03 16:35:54 +01:00
|
|
|
use rustc_middle::ty::{self, Ty};
|
2019-12-31 20:15:40 +03:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2021-03-03 16:35:54 +01:00
|
|
|
use rustc_trait_selection::infer::InferCtxtExt;
|
2015-08-18 17:59:21 -04:00
|
|
|
|
2019-06-01 13:38:36 +02: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`.
|
|
|
|
|
///
|
2018-11-27 02:59:49 +00:00
|
|
|
/// 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.
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
|
2020-07-02 16:20:59 +01:00
|
|
|
// Mark this local as internal to avoid temporaries with types not present in the
|
|
|
|
|
// user's code resulting in ICEs from the generator transform.
|
|
|
|
|
let temp = self.local_decls.push(LocalDecl::new(ty, span).internal());
|
2019-06-24 17:46:09 +02:00
|
|
|
let place = Place::from(temp);
|
2019-12-22 17:42:04 -05:00
|
|
|
debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
|
2017-12-01 14:39:51 +02:00
|
|
|
place
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
2018-08-09 06:18:00 -04:00
|
|
|
/// Convenience function for creating a literal operand, one
|
|
|
|
|
/// without any user type annotation.
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn literal_operand(
|
|
|
|
|
&mut self,
|
|
|
|
|
span: Span,
|
|
|
|
|
literal: &'tcx ty::Const<'tcx>,
|
|
|
|
|
) -> Operand<'tcx> {
|
2021-03-08 16:18:03 +00:00
|
|
|
let literal = literal.into();
|
2019-12-22 17:42:04 -05:00
|
|
|
let constant = box Constant { span, user_ty: None, literal };
|
2015-10-21 17:29:50 -04:00
|
|
|
Operand::Constant(constant)
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|
|
|
|
|
|
2016-03-31 18:50:07 +13:00
|
|
|
// Returns a zero literal operand for the appropriate type, works for
|
2016-06-05 22:32:11 +03:00
|
|
|
// bool, char and integers.
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
|
2021-03-03 16:35:54 +01:00
|
|
|
let literal = ty::Const::from_bits(self.tcx, 0, ty::ParamEnv::empty().and(ty));
|
2016-03-31 18:50:07 +13:00
|
|
|
|
2019-08-12 18:15:13 +03:00
|
|
|
self.literal_operand(span, literal)
|
2016-03-31 18:50:07 +13:00
|
|
|
}
|
|
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn push_usize(
|
2019-12-22 17:42:04 -05:00
|
|
|
&mut self,
|
|
|
|
|
block: BasicBlock,
|
|
|
|
|
source_info: SourceInfo,
|
|
|
|
|
value: u64,
|
|
|
|
|
) -> Place<'tcx> {
|
2021-03-03 16:35:54 +01:00
|
|
|
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(
|
2019-12-22 17:42:04 -05:00
|
|
|
block,
|
|
|
|
|
source_info,
|
2020-03-31 14:08:48 -03:00
|
|
|
temp,
|
2015-08-18 17:59:21 -04:00
|
|
|
Constant {
|
2016-06-07 19:21:56 +03:00
|
|
|
span: source_info.span,
|
2018-08-09 06:18:00 -04:00
|
|
|
user_ty: None,
|
2021-03-08 16:18:03 +00:00
|
|
|
literal: ty::Const::from_usize(self.tcx, value).into(),
|
2019-12-22 17:42:04 -05:00
|
|
|
},
|
|
|
|
|
);
|
2015-08-18 17:59:21 -04:00
|
|
|
temp
|
|
|
|
|
}
|
2017-11-17 17:19:57 +02:00
|
|
|
|
2020-01-05 15:46:44 +00:00
|
|
|
crate fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
|
2021-03-03 16:35:54 +01:00
|
|
|
let tcx = self.tcx;
|
2019-03-28 19:08:31 -07:00
|
|
|
let ty = place.ty(&self.local_decls, tcx).ty;
|
2021-03-03 16:35:54 +01:00
|
|
|
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, DUMMY_SP) {
|
2017-12-01 14:39:51 +02:00
|
|
|
Operand::Move(place)
|
2017-11-17 17:19:57 +02:00
|
|
|
} else {
|
2017-12-01 14:39:51 +02:00
|
|
|
Operand::Copy(place)
|
2017-11-17 17:19:57 +02:00
|
|
|
}
|
|
|
|
|
}
|
2015-08-18 17:59:21 -04:00
|
|
|
}
|