]>
Commit | Line | Data |
---|---|---|
e9174d1e SL |
1 | //! Miscellaneous builder routines that are not specific to building any particular |
2 | //! kind of thing. | |
3 | ||
9fa01778 | 4 | use crate::build::Builder; |
3157f602 | 5 | |
3157f602 XL |
6 | use rustc::ty::{self, Ty}; |
7 | ||
c30ab7b3 | 8 | use rustc::mir::*; |
ff7c6d11 | 9 | use syntax_pos::{Span, DUMMY_SP}; |
e9174d1e | 10 | |
a7813a04 | 11 | impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, '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. |
ff7c6d11 | 17 | pub fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> { |
cc61c64b | 18 | let temp = self.local_decls.push(LocalDecl::new_temp(ty, span)); |
532ac7d7 | 19 | let place = Place::Base(PlaceBase::Local(temp)); |
e9174d1e | 20 | debug!("temp: created temp {:?} with type {:?}", |
ff7c6d11 XL |
21 | place, self.local_decls[temp].ty); |
22 | place | |
e9174d1e SL |
23 | } |
24 | ||
b7449926 XL |
25 | /// Convenience function for creating a literal operand, one |
26 | /// without any user type annotation. | |
92a42be0 SL |
27 | pub fn literal_operand(&mut self, |
28 | span: Span, | |
29 | ty: Ty<'tcx>, | |
0731742a | 30 | literal: ty::Const<'tcx>) |
92a42be0 | 31 | -> Operand<'tcx> { |
cc61c64b | 32 | let constant = box Constant { |
3b2f2976 XL |
33 | span, |
34 | ty, | |
b7449926 | 35 | user_ty: None, |
532ac7d7 | 36 | literal: self.hir.tcx().mk_const(literal), |
b039eaaf | 37 | }; |
92a42be0 | 38 | Operand::Constant(constant) |
e9174d1e SL |
39 | } |
40 | ||
a7813a04 | 41 | pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> { |
cc61c64b | 42 | Rvalue::Aggregate(box AggregateKind::Tuple, vec![]) |
a7813a04 XL |
43 | } |
44 | ||
3157f602 XL |
45 | // Returns a zero literal operand for the appropriate type, works for |
46 | // bool, char and integers. | |
47 | pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> { | |
8faf50e0 | 48 | let literal = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty)); |
3157f602 XL |
49 | |
50 | self.literal_operand(span, ty, literal) | |
51 | } | |
52 | ||
54a0048b SL |
53 | pub fn push_usize(&mut self, |
54 | block: BasicBlock, | |
3157f602 | 55 | source_info: SourceInfo, |
54a0048b | 56 | value: u64) |
ff7c6d11 | 57 | -> Place<'tcx> { |
e9174d1e | 58 | let usize_ty = self.hir.usize_ty(); |
cc61c64b | 59 | let temp = self.temp(usize_ty, source_info.span); |
e9174d1e | 60 | self.cfg.push_assign_constant( |
3157f602 | 61 | block, source_info, &temp, |
e9174d1e | 62 | Constant { |
3157f602 | 63 | span: source_info.span, |
b039eaaf | 64 | ty: self.hir.usize_ty(), |
b7449926 | 65 | user_ty: None, |
b039eaaf | 66 | literal: self.hir.usize_literal(value), |
e9174d1e SL |
67 | }); |
68 | temp | |
69 | } | |
ff7c6d11 XL |
70 | |
71 | pub fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> { | |
72 | let tcx = self.hir.tcx(); | |
532ac7d7 | 73 | let ty = place.ty(&self.local_decls, tcx).ty; |
0731742a | 74 | if !self.hir.type_is_copy_modulo_regions(ty, DUMMY_SP) { |
ff7c6d11 XL |
75 | Operand::Move(place) |
76 | } else { | |
77 | Operand::Copy(place) | |
78 | } | |
79 | } | |
e9174d1e | 80 | } |