]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/misc.rs
Auto merge of #58406 - Disasm:rv64-support, r=nagisa
[rust.git] / src / librustc_mir / 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 syntax_pos::{Span, DUMMY_SP};
10
11 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, '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     pub 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::Local(temp);
20         debug!("temp: created temp {:?} with type {:?}",
21                place, self.local_decls[temp].ty);
22         place
23     }
24
25     /// Convenience function for creating a literal operand, one
26     /// without any user type annotation.
27     pub fn literal_operand(&mut self,
28                            span: Span,
29                            ty: Ty<'tcx>,
30                            literal: ty::Const<'tcx>)
31                            -> Operand<'tcx> {
32         let constant = box Constant {
33             span,
34             ty,
35             user_ty: None,
36             literal: self.hir.tcx().mk_lazy_const(ty::LazyConst::Evaluated(literal)),
37         };
38         Operand::Constant(constant)
39     }
40
41     pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
42         Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
43     }
44
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> {
48         let literal = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
49
50         self.literal_operand(span, ty, literal)
51     }
52
53     pub fn push_usize(&mut self,
54                       block: BasicBlock,
55                       source_info: SourceInfo,
56                       value: u64)
57                       -> Place<'tcx> {
58         let usize_ty = self.hir.usize_ty();
59         let temp = self.temp(usize_ty, source_info.span);
60         self.cfg.push_assign_constant(
61             block, source_info, &temp,
62             Constant {
63                 span: source_info.span,
64                 ty: self.hir.usize_ty(),
65                 user_ty: None,
66                 literal: self.hir.usize_literal(value),
67             });
68         temp
69     }
70
71     pub fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
72         let tcx = self.hir.tcx();
73         let ty = place.ty(&self.local_decls, tcx).to_ty(tcx);
74         if !self.hir.type_is_copy_modulo_regions(ty, DUMMY_SP) {
75             Operand::Move(place)
76         } else {
77             Operand::Copy(place)
78         }
79     }
80 }