]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/as_rvalue.rs
2f57dd22454cb50c3841216fe1512b27e86fd6df
[rust.git] / src / librustc_mir / build / expr / as_rvalue.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 //! See docs in build/expr/mod.rs
12
13 use rustc_data_structures::fnv::FnvHashMap;
14
15 use build::{BlockAnd, BlockAndExtension, Builder};
16 use build::expr::category::{Category, RvalueFunc};
17 use hair::*;
18 use rustc::mir::repr::*;
19
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>>
24     {
25         let expr = self.hir.mirror(expr);
26         self.expr_as_rvalue(block, expr)
27     }
28
29     fn expr_as_rvalue(&mut self,
30                       mut block: BasicBlock,
31                       expr: Expr<'tcx>)
32                       -> BlockAnd<Rvalue<'tcx>> {
33         debug!("expr_as_rvalue(block={:?}, expr={:?})", block, expr);
34
35         let this = self;
36         let expr_span = expr.span;
37
38         match expr.kind {
39             ExprKind::Scope { extent, value } => {
40                 this.in_scope(extent, block, |this| this.as_rvalue(block, value))
41             }
42             ExprKind::InlineAsm { asm } => {
43                 block.and(Rvalue::InlineAsm(asm.clone()))
44             }
45             ExprKind::Repeat { value, count } => {
46                 let value_operand = unpack!(block = this.as_operand(block, value));
47                 let count = this.as_constant(count);
48                 block.and(Rvalue::Repeat(value_operand, count))
49             }
50             ExprKind::Borrow { region, borrow_kind, arg } => {
51                 let arg_lvalue = unpack!(block = this.as_lvalue(block, arg));
52                 block.and(Rvalue::Ref(region, borrow_kind, arg_lvalue))
53             }
54             ExprKind::Binary { op, lhs, rhs } => {
55                 let lhs = unpack!(block = this.as_operand(block, lhs));
56                 let rhs = unpack!(block = this.as_operand(block, rhs));
57                 block.and(Rvalue::BinaryOp(op, lhs, rhs))
58             }
59             ExprKind::Unary { op, arg } => {
60                 let arg = unpack!(block = this.as_operand(block, arg));
61                 block.and(Rvalue::UnaryOp(op, arg))
62             }
63             ExprKind::Box { value } => {
64                 let value = this.hir.mirror(value);
65                 let value_ty = value.ty.clone();
66                 let result = this.temp(value_ty.clone());
67
68                 // to start, malloc some memory of suitable type (thus far, uninitialized):
69                 let rvalue = Rvalue::Box(value.ty.clone());
70                 this.cfg.push_assign(block, expr_span, &result, rvalue);
71
72                 // schedule a shallow free of that memory, lest we unwind:
73                 let extent = this.extent_of_innermost_scope();
74                 this.schedule_drop(expr_span, extent, DropKind::Free, &result, value_ty);
75
76                 // initialize the box contents:
77                 let contents = result.clone().deref();
78                 unpack!(block = this.into(&contents, block, value));
79
80                 // now that the result is fully initialized, cancel the drop
81                 // by "using" the result (which is linear):
82                 block.and(Rvalue::Use(Operand::Consume(result)))
83             }
84             ExprKind::Cast { source } => {
85                 let source = unpack!(block = this.as_operand(block, source));
86                 block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
87             }
88             ExprKind::ReifyFnPointer { source } => {
89                 let source = unpack!(block = this.as_operand(block, source));
90                 block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
91             }
92             ExprKind::UnsafeFnPointer { source } => {
93                 let source = unpack!(block = this.as_operand(block, source));
94                 block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
95             }
96             ExprKind::Unsize { source } => {
97                 let source = unpack!(block = this.as_operand(block, source));
98                 block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
99             }
100             ExprKind::Vec { fields } => {
101                 // (*) We would (maybe) be closer to trans if we
102                 // handled this and other aggregate cases via
103                 // `into()`, not `as_rvalue` -- in that case, instead
104                 // of generating
105                 //
106                 //     let tmp1 = ...1;
107                 //     let tmp2 = ...2;
108                 //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
109                 //
110                 // we could just generate
111                 //
112                 //     dest.f = ...1;
113                 //     dest.g = ...2;
114                 //
115                 // The problem is that then we would need to:
116                 //
117                 // (a) have a more complex mechanism for handling
118                 //     partial cleanup;
119                 // (b) distinguish the case where the type `Foo` has a
120                 //     destructor, in which case creating an instance
121                 //     as a whole "arms" the destructor, and you can't
122                 //     write individual fields; and,
123                 // (c) handle the case where the type Foo has no
124                 //     fields. We don't want `let x: ();` to compile
125                 //     to the same MIR as `let x = ();`.
126
127                 // first process the set of fields
128                 let fields: Vec<_> =
129                     fields.into_iter()
130                           .map(|f| unpack!(block = this.as_operand(block, f)))
131                           .collect();
132
133                 block.and(Rvalue::Aggregate(AggregateKind::Vec, fields))
134             }
135             ExprKind::Tuple { fields } => { // see (*) above
136                 // first process the set of fields
137                 let fields: Vec<_> =
138                     fields.into_iter()
139                           .map(|f| unpack!(block = this.as_operand(block, f)))
140                           .collect();
141
142                 block.and(Rvalue::Aggregate(AggregateKind::Tuple, fields))
143             }
144             ExprKind::Closure { closure_id, substs, upvars } => { // see (*) above
145                 let upvars =
146                     upvars.into_iter()
147                           .map(|upvar| unpack!(block = this.as_operand(block, upvar)))
148                           .collect();
149                 block.and(Rvalue::Aggregate(AggregateKind::Closure(closure_id, substs), upvars))
150             }
151             ExprKind::Adt { adt_def, variant_index, substs, fields, base } => { // see (*) above
152                 // first process the set of fields that were provided
153                 // (evaluating them in order given by user)
154                 let fields_map: FnvHashMap<_, _> =
155                     fields.into_iter()
156                           .map(|f| (f.name, unpack!(block = this.as_operand(block, f.expr))))
157                           .collect();
158
159                 // if base expression is given, evaluate it now
160                 let base = base.map(|base| unpack!(block = this.as_lvalue(block, base)));
161
162                 // get list of all fields that we will need
163                 let field_names = this.hir.all_fields(adt_def, variant_index);
164
165                 // for the actual values we use, take either the
166                 // expr the user specified or, if they didn't
167                 // specify something for this field name, create a
168                 // path relative to the base (which must have been
169                 // supplied, or the IR is internally
170                 // inconsistent).
171                 let fields: Vec<_> =
172                     field_names.into_iter()
173                                .map(|n| match fields_map.get(&n) {
174                                    Some(v) => v.clone(),
175                                    None => Operand::Consume(base.clone().unwrap().field(n)),
176                                })
177                                .collect();
178
179                 block.and(Rvalue::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs),
180                                             fields))
181             }
182             ExprKind::Literal { .. } |
183             ExprKind::Block { .. } |
184             ExprKind::Match { .. } |
185             ExprKind::If { .. } |
186             ExprKind::Loop { .. } |
187             ExprKind::LogicalOp { .. } |
188             ExprKind::Call { .. } |
189             ExprKind::Field { .. } |
190             ExprKind::Deref { .. } |
191             ExprKind::Index { .. } |
192             ExprKind::VarRef { .. } |
193             ExprKind::SelfRef |
194             ExprKind::Assign { .. } |
195             ExprKind::AssignOp { .. } |
196             ExprKind::Break { .. } |
197             ExprKind::Continue { .. } |
198             ExprKind::Return { .. } |
199             ExprKind::StaticRef { .. } => {
200                 // these do not have corresponding `Rvalue` variants,
201                 // so make an operand and then return that
202                 debug_assert!(match Category::of(&expr.kind) {
203                     Some(Category::Rvalue(RvalueFunc::AsRvalue)) => false,
204                     _ => true,
205                 });
206                 let operand = unpack!(block = this.as_operand(block, expr));
207                 block.and(Rvalue::Use(operand))
208             }
209         }
210     }
211 }