]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/misc.rs
Auto merge of #50521 - gnzlbg:simd_float, r=alexcrichton
[rust.git] / src / librustc_mir / build / misc.rs
1 // Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 //! Miscellaneous builder routines that are not specific to building any particular
12 //! kind of thing.
13
14 use build::Builder;
15
16 use rustc::ty::{self, Ty};
17
18 use rustc::mir::*;
19 use syntax_pos::{Span, DUMMY_SP};
20
21 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
22     /// Add a new temporary value of type `ty` storing the result of
23     /// evaluating `expr`.
24     ///
25     /// NB: **No cleanup is scheduled for this temporary.** You should
26     /// call `schedule_drop` once the temporary is initialized.
27     pub fn temp(&mut self, ty: Ty<'tcx>, span: Span) -> Place<'tcx> {
28         let temp = self.local_decls.push(LocalDecl::new_temp(ty, span));
29         let place = Place::Local(temp);
30         debug!("temp: created temp {:?} with type {:?}",
31                place, self.local_decls[temp].ty);
32         place
33     }
34
35     pub fn literal_operand(&mut self,
36                            span: Span,
37                            ty: Ty<'tcx>,
38                            literal: Literal<'tcx>)
39                            -> Operand<'tcx> {
40         let constant = box Constant {
41             span,
42             ty,
43             literal,
44         };
45         Operand::Constant(constant)
46     }
47
48     pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
49         Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
50     }
51
52     // Returns a zero literal operand for the appropriate type, works for
53     // bool, char and integers.
54     pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
55         let literal = Literal::Value {
56             value: ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty))
57         };
58
59         self.literal_operand(span, ty, literal)
60     }
61
62     pub fn push_usize(&mut self,
63                       block: BasicBlock,
64                       source_info: SourceInfo,
65                       value: u64)
66                       -> Place<'tcx> {
67         let usize_ty = self.hir.usize_ty();
68         let temp = self.temp(usize_ty, source_info.span);
69         self.cfg.push_assign_constant(
70             block, source_info, &temp,
71             Constant {
72                 span: source_info.span,
73                 ty: self.hir.usize_ty(),
74                 literal: self.hir.usize_literal(value),
75             });
76         temp
77     }
78
79     pub fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
80         let tcx = self.hir.tcx();
81         let ty = place.ty(&self.local_decls, tcx).to_ty(tcx);
82         if self.hir.type_moves_by_default(ty, DUMMY_SP) {
83             Operand::Move(place)
84         } else {
85             Operand::Copy(place)
86         }
87     }
88 }