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