]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/build/expr/as_rvalue.rs
hir, mir: Separate HIR expressions / MIR operands from InlineAsm.
[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, outputs, inputs } => {
43                 let outputs = outputs.into_iter().map(|output| {
44                     unpack!(block = this.as_lvalue(block, output))
45                 }).collect();
46
47                 let inputs = inputs.into_iter().map(|input| {
48                     unpack!(block = this.as_operand(block, input))
49                 }).collect();
50
51                 block.and(Rvalue::InlineAsm {
52                     asm: asm.clone(),
53                     outputs: outputs,
54                     inputs: inputs
55                 })
56             }
57             ExprKind::Repeat { value, count } => {
58                 let value_operand = unpack!(block = this.as_operand(block, value));
59                 block.and(Rvalue::Repeat(value_operand, count))
60             }
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))
64             }
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))
69             }
70             ExprKind::Unary { op, arg } => {
71                 let arg = unpack!(block = this.as_operand(block, arg));
72                 block.and(Rvalue::UnaryOp(op, arg))
73             }
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)))
85                 })
86             }
87             ExprKind::Cast { source } => {
88                 let source = this.hir.mirror(source);
89                 if source.ty == expr.ty {
90                     this.expr_as_rvalue(block, source)
91                 } else {
92                     let source = unpack!(block = this.as_operand(block, source));
93                     block.and(Rvalue::Cast(CastKind::Misc, source, expr.ty))
94                 }
95             }
96             ExprKind::ReifyFnPointer { source } => {
97                 let source = unpack!(block = this.as_operand(block, source));
98                 block.and(Rvalue::Cast(CastKind::ReifyFnPointer, source, expr.ty))
99             }
100             ExprKind::UnsafeFnPointer { source } => {
101                 let source = unpack!(block = this.as_operand(block, source));
102                 block.and(Rvalue::Cast(CastKind::UnsafeFnPointer, source, expr.ty))
103             }
104             ExprKind::Unsize { source } => {
105                 let source = unpack!(block = this.as_operand(block, source));
106                 block.and(Rvalue::Cast(CastKind::Unsize, source, expr.ty))
107             }
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
112                 // of generating
113                 //
114                 //     let tmp1 = ...1;
115                 //     let tmp2 = ...2;
116                 //     dest = Rvalue::Aggregate(Foo, [tmp1, tmp2])
117                 //
118                 // we could just generate
119                 //
120                 //     dest.f = ...1;
121                 //     dest.g = ...2;
122                 //
123                 // The problem is that then we would need to:
124                 //
125                 // (a) have a more complex mechanism for handling
126                 //     partial cleanup;
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 = ();`.
134
135                 // first process the set of fields
136                 let fields: Vec<_> =
137                     fields.into_iter()
138                           .map(|f| unpack!(block = this.as_operand(block, f)))
139                           .collect();
140
141                 block.and(Rvalue::Aggregate(AggregateKind::Vec, fields))
142             }
143             ExprKind::Tuple { fields } => { // see (*) above
144                 // first process the set of fields
145                 let fields: Vec<_> =
146                     fields.into_iter()
147                           .map(|f| unpack!(block = this.as_operand(block, f)))
148                           .collect();
149
150                 block.and(Rvalue::Aggregate(AggregateKind::Tuple, fields))
151             }
152             ExprKind::Closure { closure_id, substs, upvars } => { // see (*) above
153                 let upvars =
154                     upvars.into_iter()
155                           .map(|upvar| unpack!(block = this.as_operand(block, upvar)))
156                           .collect();
157                 block.and(Rvalue::Aggregate(AggregateKind::Closure(closure_id, substs), upvars))
158             }
159             ExprKind::Adt {
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<_, _> =
165                     fields.into_iter()
166                           .map(|f| (f.name, unpack!(block = this.as_operand(block, f.expr))))
167                           .collect();
168
169                 let field_names = this.hir.all_fields(adt_def, variant_index);
170
171                 let fields = if let Some(FruInfo { base, field_types }) = base {
172                     let base = unpack!(block = this.as_lvalue(block, base));
173
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))
182                         })
183                         .collect()
184                 } else {
185                     field_names.iter().map(|n| fields_map[n].clone()).collect()
186                 };
187
188                 block.and(Rvalue::Aggregate(AggregateKind::Adt(adt_def, variant_index, substs),
189                                             fields))
190             }
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 { .. } |
202             ExprKind::SelfRef |
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,
213                     _ => true,
214                 });
215                 let operand = unpack!(block = this.as_operand(block, expr));
216                 block.and(Rvalue::Use(operand))
217             }
218         }
219     }
220 }