]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir_build/build/misc.rs
Auto merge of #68037 - msizanoen1:riscv-ci, r=alexcrichton
[rust.git] / src / librustc_mir_build / build / misc.rs
1 //! Miscellaneous builder routines that are not specific to building any particular
2 //! kind of thing.
3
4 use crate::build::Builder;
5
6 use rustc::ty::{self, Ty};
7
8 use rustc::mir::*;
9 use rustc_span::{Span, DUMMY_SP};
10
11 impl<'a, 'tcx> Builder<'a, 'tcx> {
12     /// Adds a new temporary value of type `ty` storing the result of
13     /// evaluating `expr`.
14     ///
15     /// N.B., **No cleanup is scheduled for this temporary.** You should
16     /// call `schedule_drop` once the temporary is initialized.
17     crate fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
18         let temp = self.local_decls.push(LocalDecl::new_temp(ty, span));
19         let place = Place::from(temp);
20         debug!("temp: created temp {:?} with type {:?}", place, self.local_decls[temp].ty);
21         place
22     }
23
24     /// Convenience function for creating a literal operand, one
25     /// without any user type annotation.
26     crate fn literal_operand(
27         &mut self,
28         span: Span,
29         literal: &'tcx ty::Const<'tcx>,
30     ) -> Operand<'tcx> {
31         let constant = box Constant { span, user_ty: None, literal };
32         Operand::Constant(constant)
33     }
34
35     crate fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
36         Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
37     }
38
39     // Returns a zero literal operand for the appropriate type, works for
40     // bool, char and integers.
41     crate fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
42         let literal = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
43
44         self.literal_operand(span, literal)
45     }
46
47     crate fn push_usize(
48         &mut self,
49         block: BasicBlock,
50         source_info: SourceInfo,
51         value: u64,
52     ) -> Place<'tcx> {
53         let usize_ty = self.hir.usize_ty();
54         let temp = self.temp(usize_ty, source_info.span);
55         self.cfg.push_assign_constant(
56             block,
57             source_info,
58             &temp,
59             Constant {
60                 span: source_info.span,
61                 user_ty: None,
62                 literal: self.hir.usize_literal(value),
63             },
64         );
65         temp
66     }
67
68     crate fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
69         let tcx = self.hir.tcx();
70         let ty = place.ty(&self.local_decls, tcx).ty;
71         if !self.hir.type_is_copy_modulo_regions(ty, DUMMY_SP) {
72             Operand::Move(place)
73         } else {
74             Operand::Copy(place)
75         }
76     }
77 }