]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/misc.rs
Various minor/cosmetic improvements to code
[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     /// N.B., **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     /// Convenience function for creating a literal operand, one
36     /// without any user type annotation.
37     pub fn literal_operand(&mut self,
38                            span: Span,
39                            ty: Ty<'tcx>,
40                            literal: &'tcx ty::Const<'tcx>)
41                            -> Operand<'tcx> {
42         let constant = box Constant {
43             span,
44             ty,
45             user_ty: None,
46             literal,
47         };
48         Operand::Constant(constant)
49     }
50
51     pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
52         Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
53     }
54
55     // Returns a zero literal operand for the appropriate type, works for
56     // bool, char and integers.
57     pub fn zero_literal(&mut self, span: Span, ty: Ty<'tcx>) -> Operand<'tcx> {
58         let literal = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
59
60         self.literal_operand(span, ty, literal)
61     }
62
63     pub fn push_usize(&mut self,
64                       block: BasicBlock,
65                       source_info: SourceInfo,
66                       value: u64)
67                       -> Place<'tcx> {
68         let usize_ty = self.hir.usize_ty();
69         let temp = self.temp(usize_ty, source_info.span);
70         self.cfg.push_assign_constant(
71             block, source_info, &temp,
72             Constant {
73                 span: source_info.span,
74                 ty: self.hir.usize_ty(),
75                 user_ty: None,
76                 literal: self.hir.usize_literal(value),
77             });
78         temp
79     }
80
81     pub fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
82         let tcx = self.hir.tcx();
83         let ty = place.ty(&self.local_decls, tcx).to_ty(tcx);
84         if self.hir.type_moves_by_default(ty, DUMMY_SP) {
85             Operand::Move(place)
86         } else {
87             Operand::Copy(place)
88         }
89     }
90 }