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 /// 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);
35 /// Convenience function for creating a literal operand, one
36 /// without any user type annotation.
37 pub fn literal_operand(&mut self,
40 literal: &'tcx ty::Const<'tcx>)
42 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 = ty::Const::from_bits(self.hir.tcx(), 0, ty::ParamEnv::empty().and(ty));
60 self.literal_operand(span, ty, literal)
63 pub fn push_usize(&mut self,
65 source_info: SourceInfo,
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,
73 span: source_info.span,
74 ty: self.hir.usize_ty(),
76 literal: self.hir.usize_literal(value),
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) {