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 //! See docs in build/expr/mod.rs
13 use rustc_data_structures::fnv::FnvHashMap;
15 use build::{BlockAnd, BlockAndExtension, Builder};
16 use build::expr::category::{Category, RvalueFunc};
18 use rustc::mir::repr::*;
20 impl<'a,'tcx> Builder<'a,'tcx> {
21 /// Compile `expr`, yielding an rvalue.
22 pub fn as_rvalue<M>(&mut self, block: BasicBlock, expr: M) -> BlockAnd<Rvalue<'tcx>>
23 where M: Mirror<'tcx, Output = Expr<'tcx>>
25 let expr = self.hir.mirror(expr);
26 self.expr_as_rvalue(block, expr)
29 fn expr_as_rvalue(&mut self,
30 mut block: BasicBlock,
32 -> BlockAnd<Rvalue<'tcx>> {
33 debug!("expr_as_rvalue(block={:?}, expr={:?})", block, expr);
36 let expr_span = expr.span;
39 ExprKind::Scope { extent, value } => {
40 this.in_scope(extent, block, |this| this.as_rvalue(block, value))
42 ExprKind::InlineAsm { asm, outputs, inputs } => {
43 let outputs = outputs.into_iter().map(|output| {
44 unpack!(block = this.as_lvalue(block, output))
47 let inputs = inputs.into_iter().map(|input| {
48 unpack!(block = this.as_operand(block, input))
51 block.and(Rvalue::InlineAsm {
57 ExprKind::Repeat { value, count } => {
58 let value_operand = unpack!(block = this.as_operand(block, value));
59 block.and(Rvalue::Repeat(value_operand, count))
61 ExprKind::Borrow { region, borrow_kind, arg } => {
62 let arg_lvalue = unpack!(block = this.as_lvalue(block, arg));
63 block.and(Rvalue::Ref(region, borrow_kind, arg_lvalue))
65 ExprKind::Binary { op, lhs, rhs } => {
66 let lhs = unpack!(block = this.as_operand(block, lhs));
67 let rhs = unpack!(block = this.as_operand(block, rhs));
68 block.and(Rvalue::BinaryOp(op, lhs, rhs))
70 ExprKind::Unary { op, arg } => {
71 let arg = unpack!(block = this.as_operand(block, arg));
72 block.and(Rvalue::UnaryOp(op, arg))
74 ExprKind::Box { value, value_extents } => {
75 let value = this.hir.mirror(value);
76 let result = this.temp(expr.ty);
77 // to start, malloc some memory of suitable type (thus far, uninitialized):
78 this.cfg.push_assign(block, expr_span, &result, Rvalue::Box(value.ty));
79 this.in_scope(value_extents, block, |this| {
80 // schedule a shallow free of that memory, lest we unwind:
81 this.schedule_box_free(expr_span, value_extents, &result, value.ty);
82 // initialize the box contents:
83 unpack!(block = this.into(&result.clone().deref(), block, value));
84 block.and(Rvalue::Use(Operand::Consume(result)))
87 ExprKind::Cast { source } => {
88 let source = this.hir.mirror(source);
89 if source.ty == expr.ty {
90 this.expr_as_rvalue(block, source)
92 let source = unpack!(block = this.as_operand(block, source));
93 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
96 ExprKind::ReifyFnPointer { source } => {
97 let source = unpack!(block = this.as_operand(block, source));
98 block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
100 ExprKind::UnsafeFnPointer { source } => {
101 let source = unpack!(block = this.as_operand(block, source));
102 block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
104 ExprKind::Unsize { source } => {
105 let source = unpack!(block = this.as_operand(block, source));
106 block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
108 ExprKind::Vec { fields } => {
109 // (*) We would (maybe) be closer to trans if we
110 // handled this and other aggregate cases via
111 // `into()`, not `as_rvalue` -- in that case, instead
116 // dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
118 // we could just generate
123 // The problem is that then we would need to:
125 // (a) have a more complex mechanism for handling
127 // (b) distinguish the case where the type `Foo` has a
128 // destructor, in which case creating an instance
129 // as a whole "arms" the destructor, and you can't
130 // write individual fields; and,
131 // (c) handle the case where the type Foo has no
132 // fields. We don't want `let x: ();` to compile
133 // to the same MIR as `let x = ();`.
135 // first process the set of fields
138 .map(|f| unpack!(block = this.as_operand(block, f)))
141 block.and(Rvalue::Aggregate(AggregateKind::Vec, fields))
143 ExprKind::Tuple { fields } => { // see (*) above
144 // first process the set of fields
147 .map(|f| unpack!(block = this.as_operand(block, f)))
150 block.and(Rvalue::Aggregate(AggregateKind::Tuple, fields))
152 ExprKind::Closure { closure_id, substs, upvars } => { // see (*) above
155 .map(|upvar| unpack!(block = this.as_operand(block, upvar)))
157 block.and(Rvalue::Aggregate(AggregateKind::Closure(closure_id, substs), upvars))
160 adt_def, variant_index, substs, fields, base
161 } => { // see (*) above
162 // first process the set of fields that were provided
163 // (evaluating them in order given by user)
164 let fields_map: FnvHashMap<_, _> =
166 .map(|f| (f.name, unpack!(block = this.as_operand(block, f.expr))))
169 let field_names = this.hir.all_fields(adt_def, variant_index);
171 let fields = if let Some(FruInfo { base, field_types }) = base {
172 let base = unpack!(block = this.as_lvalue(block, base));
174 // MIR does not natively support FRU, so for each
175 // base-supplied field, generate an operand that
176 // reads it from the base.
177 field_names.into_iter()
178 .zip(field_types.into_iter())
179 .map(|(n, ty)| match fields_map.get(&n) {
180 Some(v) => v.clone(),
181 None => Operand::Consume(base.clone().field(n, ty))
185 field_names.iter().map(|n| fields_map[n].clone()).collect()
188 block.and(Rvalue::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs),
191 ExprKind::Literal { .. } |
192 ExprKind::Block { .. } |
193 ExprKind::Match { .. } |
194 ExprKind::If { .. } |
195 ExprKind::Loop { .. } |
196 ExprKind::LogicalOp { .. } |
197 ExprKind::Call { .. } |
198 ExprKind::Field { .. } |
199 ExprKind::Deref { .. } |
200 ExprKind::Index { .. } |
201 ExprKind::VarRef { .. } |
203 ExprKind::Assign { .. } |
204 ExprKind::AssignOp { .. } |
205 ExprKind::Break { .. } |
206 ExprKind::Continue { .. } |
207 ExprKind::Return { .. } |
208 ExprKind::StaticRef { .. } => {
209 // these do not have corresponding `Rvalue` variants,
210 // so make an operand and then return that
211 debug_assert!(match Category::of(&expr.kind) {
212 Some(Category::Rvalue(RvalueFunc::AsRvalue)) => false,
215 let operand = unpack!(block = this.as_operand(block, expr));
216 block.and(Rvalue::Use(operand))