]> git.proxmox.com Git - rustc.git/blame - compiler/rustc_mir_build/src/build/misc.rs
Merge tag 'debian/1.52.1+dfsg1-1_exp2' into proxmox/buster
[rustc.git] / compiler / rustc_mir_build / src / build / misc.rs
CommitLineData
e9174d1e
SL
1//! Miscellaneous builder routines that are not specific to building any particular
2//! kind of thing.
3
9fa01778 4use crate::build::Builder;
3157f602 5
ba9703b0 6use rustc_middle::mir::*;
6a06907d 7use rustc_middle::ty::{self, Ty};
dfeec247 8use rustc_span::{Span, DUMMY_SP};
6a06907d 9use rustc_trait_selection::infer::InferCtxtExt;
e9174d1e 10
dc9dc135 11impl<'a, 'tcx> Builder<'a, 'tcx> {
9fa01778 12 /// Adds a new temporary value of type `ty` storing the result of
e9174d1e
SL
13 /// evaluating `expr`.
14 ///
0731742a 15 /// N.B., **No cleanup is scheduled for this temporary.** You should
e9174d1e 16 /// call `schedule_drop` once the temporary is initialized.
dfeec247 17 crate fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
f035d41b
XL
18 // Mark this local as internal to avoid temporaries with types not present in the
19 // user's code resulting in ICEs from the generator transform.
20 let temp = self.local_decls.push(LocalDecl::new(ty, span).internal());
dc9dc135 21 let place = Place::from(temp);
dfeec247 22 debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
ff7c6d11 23 place
e9174d1e
SL
24 }
25
b7449926
XL
26 /// Convenience function for creating a literal operand, one
27 /// without any user type annotation.
dfeec247
XL
28 crate fn literal_operand(
29 &mut self,
30 span: Span,
31 literal: &'tcx ty::Const<'tcx>,
32 ) -> Operand<'tcx> {
6a06907d 33 let literal = literal.into();
dfeec247 34 let constant = box Constant { span, user_ty: None, literal };
92a42be0 35 Operand::Constant(constant)
e9174d1e
SL
36 }
37
3157f602
XL
38 // Returns a zero literal operand for the appropriate type, works for
39 // bool, char and integers.
dfeec247 40 crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
6a06907d 41 let literal = ty::Const::from_bits(self.tcx, 0, ty::ParamEnv::empty().and(ty));
3157f602 42
e1599b0c 43 self.literal_operand(span, literal)
3157f602
XL
44 }
45
dfeec247
XL
46 crate fn push_usize(
47 &mut self,
48 block: BasicBlock,
49 source_info: SourceInfo,
50 value: u64,
51 ) -> Place<'tcx> {
6a06907d 52 let usize_ty = self.tcx.types.usize;
cc61c64b 53 let temp = self.temp(usize_ty, source_info.span);
e9174d1e 54 self.cfg.push_assign_constant(
dfeec247
XL
55 block,
56 source_info,
ba9703b0 57 temp,
e9174d1e 58 Constant {
3157f602 59 span: source_info.span,
b7449926 60 user_ty: None,
6a06907d 61 literal: ty::Const::from_usize(self.tcx, value).into(),
dfeec247
XL
62 },
63 );
e9174d1e
SL
64 temp
65 }
ff7c6d11 66
dfeec247 67 crate fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
6a06907d 68 let tcx = self.tcx;
532ac7d7 69 let ty = place.ty(&self.local_decls, tcx).ty;
6a06907d 70 if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, DUMMY_SP) {
ff7c6d11
XL
71 Operand::Move(place)
72 } else {
73 Operand::Copy(place)
74 }
75 }
e9174d1e 76}