|
| 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 | +//! This pass erases all early-bound regions from the types occuring in the MIR. |
| 12 | +//! We want to do this once just before trans, so trans does not have to take |
| 13 | +//! care erasing regions all over the place. |
| 14 | +
|
| 15 | +use repr::*; |
| 16 | +use rustc::middle::ty; |
| 17 | +use transform::MirPass; |
| 18 | +use mir_map::MirMap; |
| 19 | + |
| 20 | +pub fn erase_regions<'tcx>(tcx: &ty::ctxt<'tcx>, mir_map: &mut MirMap<'tcx>) { |
| 21 | + let mut eraser = EraseRegions::new(tcx); |
| 22 | + |
| 23 | + for mir in mir_map.iter_mut().map(|(_, v)| v) { |
| 24 | + eraser.run_on_mir(mir); |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +pub struct EraseRegions<'a, 'tcx: 'a> { |
| 29 | + tcx: &'a ty::ctxt<'tcx>, |
| 30 | +} |
| 31 | + |
| 32 | +impl<'a, 'tcx> MirPass<'tcx> for EraseRegions<'a, 'tcx> { |
| 33 | + |
| 34 | + fn run_on_mir(&mut self, mir: &mut Mir<'tcx>) { |
| 35 | + |
| 36 | + for basic_block in &mut mir.basic_blocks { |
| 37 | + self.erase_regions_basic_block(basic_block); |
| 38 | + } |
| 39 | + |
| 40 | + self.erase_regions_return_ty(&mut mir.return_ty); |
| 41 | + |
| 42 | + self.erase_regions_tys(mir.var_decls.iter_mut().map(|d| &mut d.ty)); |
| 43 | + self.erase_regions_tys(mir.arg_decls.iter_mut().map(|d| &mut d.ty)); |
| 44 | + self.erase_regions_tys(mir.temp_decls.iter_mut().map(|d| &mut d.ty)); |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl<'a, 'tcx> EraseRegions<'a, 'tcx> { |
| 49 | + |
| 50 | + pub fn new(tcx: &'a ty::ctxt<'tcx>) -> EraseRegions<'a, 'tcx> { |
| 51 | + EraseRegions { |
| 52 | + tcx: tcx |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + fn erase_regions_basic_block(&mut self, |
| 57 | + basic_block: &mut BasicBlockData<'tcx>) { |
| 58 | + for statement in &mut basic_block.statements { |
| 59 | + self.erase_regions_statement(statement); |
| 60 | + } |
| 61 | + |
| 62 | + self.erase_regions_terminator(&mut basic_block.terminator); |
| 63 | + } |
| 64 | + |
| 65 | + fn erase_regions_statement(&mut self, |
| 66 | + statement: &mut Statement<'tcx>) { |
| 67 | + match statement.kind { |
| 68 | + StatementKind::Assign(ref mut lvalue, ref mut rvalue) => { |
| 69 | + self.erase_regions_lvalue(lvalue); |
| 70 | + self.erase_regions_rvalue(rvalue); |
| 71 | + } |
| 72 | + StatementKind::Drop(_, ref mut lvalue) => { |
| 73 | + self.erase_regions_lvalue(lvalue); |
| 74 | + } |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + fn erase_regions_terminator(&mut self, |
| 79 | + terminator: &mut Terminator<'tcx>) { |
| 80 | + match *terminator { |
| 81 | + Terminator::Goto { .. } | |
| 82 | + Terminator::Diverge | |
| 83 | + Terminator::Return | |
| 84 | + Terminator::Panic { .. } => { |
| 85 | + /* nothing to do */ |
| 86 | + } |
| 87 | + Terminator::If { ref mut cond, .. } => { |
| 88 | + self.erase_regions_operand(cond); |
| 89 | + } |
| 90 | + Terminator::Switch { ref mut discr, .. } => { |
| 91 | + self.erase_regions_lvalue(discr); |
| 92 | + } |
| 93 | + Terminator::SwitchInt { |
| 94 | + ref mut discr, |
| 95 | + ref mut switch_ty, |
| 96 | + .. |
| 97 | + } => { |
| 98 | + self.erase_regions_lvalue(discr); |
| 99 | + *switch_ty = self.tcx.erase_regions(switch_ty); |
| 100 | + }, |
| 101 | + Terminator::Call { |
| 102 | + data: CallData { |
| 103 | + ref mut destination, |
| 104 | + ref mut func, |
| 105 | + ref mut args |
| 106 | + }, |
| 107 | + .. |
| 108 | + } => { |
| 109 | + self.erase_regions_lvalue(destination); |
| 110 | + self.erase_regions_operand(func); |
| 111 | + for arg in &mut *args { |
| 112 | + self.erase_regions_operand(arg); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + fn erase_regions_operand(&mut self, operand: &mut Operand<'tcx>) { |
| 119 | + match *operand { |
| 120 | + Operand::Consume(ref mut lvalue) => { |
| 121 | + self.erase_regions_lvalue(lvalue); |
| 122 | + } |
| 123 | + Operand::Constant(ref mut constant) => { |
| 124 | + self.erase_regions_constant(constant); |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + fn erase_regions_lvalue(&mut self, lvalue: &mut Lvalue<'tcx>) { |
| 130 | + match *lvalue { |
| 131 | + Lvalue::Var(_) | |
| 132 | + Lvalue::Temp(_) | |
| 133 | + Lvalue::Arg(_) | |
| 134 | + Lvalue::Static(_) | |
| 135 | + Lvalue::ReturnPointer => {} |
| 136 | + Lvalue::Projection(ref mut lvalue_projection) => { |
| 137 | + self.erase_regions_lvalue(&mut lvalue_projection.base); |
| 138 | + match lvalue_projection.elem { |
| 139 | + ProjectionElem::Deref | |
| 140 | + ProjectionElem::Field(_) | |
| 141 | + ProjectionElem::Downcast(..) | |
| 142 | + ProjectionElem::ConstantIndex {..} => { /* nothing to do */ } |
| 143 | + ProjectionElem::Index(ref mut index) => { |
| 144 | + self.erase_regions_operand(index); |
| 145 | + } |
| 146 | + } |
| 147 | + } |
| 148 | + } |
| 149 | + } |
| 150 | + |
| 151 | + fn erase_regions_rvalue(&mut self, rvalue: &mut Rvalue<'tcx>) { |
| 152 | + match *rvalue { |
| 153 | + Rvalue::Use(ref mut operand) => { |
| 154 | + self.erase_regions_operand(operand) |
| 155 | + } |
| 156 | + Rvalue::Repeat(ref mut operand, ref mut constant) => { |
| 157 | + self.erase_regions_operand(operand); |
| 158 | + self.erase_regions_constant(constant); |
| 159 | + } |
| 160 | + Rvalue::Ref(ref mut region, _, ref mut lvalue) => { |
| 161 | + *region = ty::ReStatic; |
| 162 | + self.erase_regions_lvalue(lvalue); |
| 163 | + } |
| 164 | + Rvalue::Len(ref mut lvalue) => self.erase_regions_lvalue(lvalue), |
| 165 | + Rvalue::Cast(_, ref mut operand, ref mut ty) => { |
| 166 | + self.erase_regions_operand(operand); |
| 167 | + *ty = self.tcx.erase_regions(ty); |
| 168 | + } |
| 169 | + Rvalue::BinaryOp(_, ref mut operand1, ref mut operand2) => { |
| 170 | + self.erase_regions_operand(operand1); |
| 171 | + self.erase_regions_operand(operand2); |
| 172 | + } |
| 173 | + Rvalue::UnaryOp(_, ref mut operand) => { |
| 174 | + self.erase_regions_operand(operand); |
| 175 | + } |
| 176 | + Rvalue::Box(ref mut ty) => *ty = self.tcx.erase_regions(ty), |
| 177 | + Rvalue::Aggregate(ref mut aggregate_kind, ref mut operands) => { |
| 178 | + match *aggregate_kind { |
| 179 | + AggregateKind::Vec | |
| 180 | + AggregateKind::Tuple => {}, |
| 181 | + AggregateKind::Adt(_, _, ref mut substs) => { |
| 182 | + let erased = self.tcx.erase_regions(*substs); |
| 183 | + *substs = self.tcx.mk_substs(erased); |
| 184 | + } |
| 185 | + AggregateKind::Closure(def_id, ref mut closure_substs) => { |
| 186 | + let cloned = Box::new(closure_substs.clone()); |
| 187 | + let ty = self.tcx.mk_closure_from_closure_substs(def_id, |
| 188 | + cloned); |
| 189 | + let erased = self.tcx.erase_regions(&ty); |
| 190 | + *closure_substs = match erased.sty { |
| 191 | + ty::TyClosure(_, ref closure_substs) => &*closure_substs, |
| 192 | + _ => unreachable!() |
| 193 | + }; |
| 194 | + } |
| 195 | + } |
| 196 | + for operand in &mut *operands { |
| 197 | + self.erase_regions_operand(operand); |
| 198 | + } |
| 199 | + } |
| 200 | + Rvalue::Slice { ref mut input, .. } => { |
| 201 | + self.erase_regions_lvalue(input); |
| 202 | + } |
| 203 | + Rvalue::InlineAsm(_) => {}, |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + fn erase_regions_constant(&mut self, constant: &mut Constant<'tcx>) { |
| 208 | + constant.ty = self.tcx.erase_regions(&constant.ty); |
| 209 | + match constant.literal { |
| 210 | + Literal::Item { ref mut substs, .. } => { |
| 211 | + *substs = self.tcx.mk_substs(self.tcx.erase_regions(substs)); |
| 212 | + } |
| 213 | + Literal::Value { .. } => { /* nothing to do */ } |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + fn erase_regions_return_ty(&mut self, fn_output: &mut ty::FnOutput<'tcx>) { |
| 218 | + match *fn_output { |
| 219 | + ty::FnConverging(ref mut ty) => { |
| 220 | + *ty = self.tcx.erase_regions(ty); |
| 221 | + }, |
| 222 | + ty::FnDiverging => {} |
| 223 | + } |
| 224 | + } |
| 225 | + |
| 226 | + fn erase_regions_tys<'b, T>(&mut self, tys: T) |
| 227 | + where T: Iterator<Item = &'b mut ty::Ty<'tcx>>, |
| 228 | + 'tcx: 'b |
| 229 | + { |
| 230 | + for ty in tys { |
| 231 | + *ty = self.tcx.erase_regions(ty); |
| 232 | + } |
| 233 | + } |
| 234 | +} |
0 commit comments