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.
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 use rustc::ty::TyCtxt;
13 use rustc_data_structures::indexed_vec::Idx;
14 use transform::{MirPass, MirSource};
16 pub struct Deaggregator;
18 impl MirPass for Deaggregator {
19 fn run_pass<'a, 'tcx>(&self,
20 tcx: TyCtxt<'a, 'tcx, 'tcx>,
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 {
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),
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 {
60 lhs = lhs.downcast(adt_def, variant_index);
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 {
74 // FIXME(eddyb) `min_length` doesn't appear to be used.
75 min_length: offset + 1,
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)
85 kind: StatementKind::Assign(lhs_field, Rvalue::Use(op)),
87 }).chain(set_discriminant))