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::ty::{self, Ty};
19 use syntax_pos::{Span, DUMMY_SP};
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`.
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);
35 pub fn literal_operand(&mut self,
38 literal: &'tcx ty::Const<'tcx>)
40 let constant = box Constant {
45 Operand::Constant(constant)
48 pub fn unit_rvalue(&mut self) -> Rvalue<'tcx> {
49 Rvalue::Aggregate(box AggregateKind::Tuple, vec![])
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 = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
57 self.literal_operand(span, ty, literal)
60 pub fn push_usize(&mut self,
62 source_info: SourceInfo,
65 let usize_ty = self.hir.usize_ty();
66 let temp = self.temp(usize_ty, source_info.span);
67 self.cfg.push_assign_constant(
68 block, source_info, &temp,
70 span: source_info.span,
71 ty: self.hir.usize_ty(),
72 literal: self.hir.usize_literal(value),
77 pub fn consume_by_copy_or_move(&self, place: Place<'tcx>) -> Operand<'tcx> {
78 let tcx = self.hir.tcx();
79 let ty = place.ty(&self.local_decls, tcx).to_ty(tcx);
80 if self.hir.type_moves_by_default(ty, DUMMY_SP) {