]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/transform/deaggregator.rs
introduce Guard enum
[rust.git] / src / librustc_mir / transform / deaggregator.rs
1 // Copyright 2016 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 use rustc::ty::TyCtxt;
12 use rustc::mir::*;
13 use rustc_data_structures::indexed_vec::Idx;
14 use transform::{MirPass, MirSource};
15
16 pub struct Deaggregator;
17
18 impl MirPass for Deaggregator {
19     fn run_pass<'a, 'tcx>(&self,
20                           tcx: TyCtxt<'a, 'tcx, 'tcx>,
21                           _source: MirSource,
22                           mir: &mut Mir<'tcx>) {
23         let (basic_blocks, local_decls) = mir.basic_blocks_and_local_decls_mut();
24         let local_decls = &*local_decls;
25         for bb in basic_blocks {
26             bb.expand_statements(|stmt| {
27                 // FIXME(eddyb) don't match twice on `stmt.kind` (post-NLL).
28                 if let StatementKind::Assign(_, ref rhs) = stmt.kind {
29                     if let Rvalue::Aggregate(ref kind, _) = *rhs {
30                         // FIXME(#48193) Deaggregate arrays when it's cheaper to do so.
31                         if let AggregateKind::Array(_) = **kind {
32                             return None;
33                         }
34                     } else {
35                         return None;
36                     }
37                 } else {
38                     return None;
39                 }
40
41                 let stmt = stmt.replace_nop();
42                 let source_info = stmt.source_info;
43                 let (mut lhs, kind, operands) = match stmt.kind {
44                     StatementKind::Assign(lhs, Rvalue::Aggregate(kind, operands))
45                         => (lhs, kind, operands),
46                     _ => bug!()
47                 };
48
49                 let mut set_discriminant = None;
50                 let active_field_index = match *kind {
51                     AggregateKind::Adt(adt_def, variant_index, _, active_field_index) => {
52                         if adt_def.is_enum() {
53                             set_discriminant = Some(Statement {
54                                 kind: StatementKind::SetDiscriminant {
55                                     place: lhs.clone(),
56                                     variant_index,
57                                 },
58                                 source_info,
59                             });
60                             lhs = lhs.downcast(adt_def, variant_index);
61                         }
62                         active_field_index
63                     }
64                     _ => None
65                 };
66
67                 Some(operands.into_iter().enumerate().map(move |(i, op)| {
68                     let lhs_field = if let AggregateKind::Array(_) = *kind {
69                         // FIXME(eddyb) `offset` should be u64.
70                         let offset = i as u32;
71                         assert_eq!(offset as usize, i);
72                         lhs.clone().elem(ProjectionElem::ConstantIndex {
73                             offset,
74                             // FIXME(eddyb) `min_length` doesn't appear to be used.
75                             min_length: offset + 1,
76                             from_end: false
77                         })
78                     } else {
79                         let ty = op.ty(local_decls, tcx);
80                         let field = Field::new(active_field_index.unwrap_or(i));
81                         lhs.clone().field(field, ty)
82                     };
83                     Statement {
84                         source_info,
85                         kind: StatementKind::Assign(lhs_field, Rvalue::Use(op)),
86                     }
87                 }).chain(set_discriminant))
88             });
89         }
90     }
91 }