]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/misc.rs
79a4cf73041d7c6e6d120018d37e2297d8ac1a6b
[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_const_math::{ConstInt, ConstUsize, ConstIsize};
17 use rustc::middle::const_val::ConstVal;
18 use rustc::ty::{self, Ty};
19
20 use rustc::mir::repr::*;
21 use syntax::ast;
22 use syntax_pos::Span;
23
24 impl<'a, 'gcx, 'tcx> Builder<'a, 'gcx, 'tcx> {
25     /// Add a new temporary value of type `ty` storing the result of
26     /// evaluating `expr`.
27     ///
28     /// NB: **No cleanup is scheduled for this temporary.** You should
29     /// call `schedule_drop` once the temporary is initialized.
30     pub fn temp(&mut self, ty: Ty<'tcx>) -> Lvalue<'tcx> {
31         let temp = self.temp_decls.push(TempDecl { ty: ty });
32         let lvalue = Lvalue::Temp(temp);
33         debug!("temp: created temp {:?} with type {:?}",
34                lvalue, self.temp_decls[temp].ty);
35         lvalue
36     }
37
38     pub fn literal_operand(&mut self,
39                            span: Span,
40                            ty: Ty<'tcx>,
41                            literal: Literal<'tcx>)
42                            -> Operand<'tcx> {
43         let constant = Constant {
44             span: span,
45             ty: ty,
46             literal: literal,
47         };
48         Operand::Constant(constant)
49     }
50
51     pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
52         Rvalue::Aggregate(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 = match ty.sty {
59             ty::TyBool => {
60                 self.hir.false_literal()
61             }
62             ty::TyChar => Literal::Value { value: ConstVal::Char('\0') },
63             ty::TyUint(ity) => {
64                 let val = match ity {
65                     ast::UintTy::U8  => ConstInt::U8(0),
66                     ast::UintTy::U16 => ConstInt::U16(0),
67                     ast::UintTy::U32 => ConstInt::U32(0),
68                     ast::UintTy::U64 => ConstInt::U64(0),
69                     ast::UintTy::Us => {
70                         let uint_ty = self.hir.tcx().sess.target.uint_type;
71                         let val = ConstUsize::new(0, uint_ty).unwrap();
72                         ConstInt::Usize(val)
73                     }
74                 };
75
76                 Literal::Value { value: ConstVal::Integral(val) }
77             }
78             ty::TyInt(ity) => {
79                 let val = match ity {
80                     ast::IntTy::I8  => ConstInt::I8(0),
81                     ast::IntTy::I16 => ConstInt::I16(0),
82                     ast::IntTy::I32 => ConstInt::I32(0),
83                     ast::IntTy::I64 => ConstInt::I64(0),
84                     ast::IntTy::Is => {
85                         let int_ty = self.hir.tcx().sess.target.int_type;
86                         let val = ConstIsize::new(0, int_ty).unwrap();
87                         ConstInt::Isize(val)
88                     }
89                 };
90
91                 Literal::Value { value: ConstVal::Integral(val) }
92             }
93             _ => {
94                 span_bug!(span, "Invalid type for zero_literal: `{:?}`", ty)
95             }
96         };
97
98         self.literal_operand(span, ty, literal)
99     }
100
101     pub fn push_usize(&mut self,
102                       block: BasicBlock,
103                       source_info: SourceInfo,
104                       value: u64)
105                       -> Lvalue<'tcx> {
106         let usize_ty = self.hir.usize_ty();
107         let temp = self.temp(usize_ty);
108         self.cfg.push_assign_constant(
109             block, source_info, &temp,
110             Constant {
111                 span: source_info.span,
112                 ty: self.hir.usize_ty(),
113                 literal: self.hir.usize_literal(value),
114             });
115         temp
116     }
117 }