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.
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.
11 //! Miscellaneous builder routines that are not specific to building any particular
16 use rustc_const_math::{ConstInt, ConstUsize, ConstIsize};
17 use rustc::middle::const_val::ConstVal;
18 use rustc::ty::{self, Ty};
22 use syntax_pos::{Span, DUMMY_SP};
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`.
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>, span: Span) -> Lvalue<'tcx> {
31 let temp = self.local_decls.push(LocalDecl::new_temp(ty, span));
32 let lvalue = Lvalue::Local(temp);
33 debug!("temp: created temp {:?} with type {:?}",
34 lvalue, self.local_decls[temp].ty);
38 pub fn literal_operand(&mut self,
41 literal: Literal<'tcx>)
43 let constant = box Constant {
48 Operand::Constant(constant)
51 pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
52 Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
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 {
60 self.hir.false_literal()
64 value: self.hir.tcx().mk_const(ty::Const {
65 val: ConstVal::Char('\0'),
72 ast::UintTy::U8 => ConstInt::U8(0),
73 ast::UintTy::U16 => ConstInt::U16(0),
74 ast::UintTy::U32 => ConstInt::U32(0),
75 ast::UintTy::U64 => ConstInt::U64(0),
76 ast::UintTy::U128 => ConstInt::U128(0),
78 let uint_ty = self.hir.tcx().sess.target.usize_ty;
79 let val = ConstUsize::new(0, uint_ty).unwrap();
85 value: self.hir.tcx().mk_const(ty::Const {
86 val: ConstVal::Integral(val),
93 ast::IntTy::I8 => ConstInt::I8(0),
94 ast::IntTy::I16 => ConstInt::I16(0),
95 ast::IntTy::I32 => ConstInt::I32(0),
96 ast::IntTy::I64 => ConstInt::I64(0),
97 ast::IntTy::I128 => ConstInt::I128(0),
99 let int_ty = self.hir.tcx().sess.target.isize_ty;
100 let val = ConstIsize::new(0, int_ty).unwrap();
106 value: self.hir.tcx().mk_const(ty::Const {
107 val: ConstVal::Integral(val),
113 span_bug!(span, "Invalid type for zero_literal: `{:?}`", ty)
117 self.literal_operand(span, ty, literal)
120 pub fn push_usize(&mut self,
122 source_info: SourceInfo,
125 let usize_ty = self.hir.usize_ty();
126 let temp = self.temp(usize_ty, source_info.span);
127 self.cfg.push_assign_constant(
128 block, source_info, &temp,
130 span: source_info.span,
131 ty: self.hir.usize_ty(),
132 literal: self.hir.usize_literal(value),
137 pub fn consume_by_copy_or_move(&self, lvalue: Lvalue<'tcx>) -> Operand<'tcx> {
138 let tcx = self.hir.tcx();
139 let ty = lvalue.ty(&self.local_decls, tcx).to_ty(tcx);
140 if self.hir.type_moves_by_default(ty, DUMMY_SP) {
141 Operand::Move(lvalue)
143 Operand::Copy(lvalue)