|
| 1 | +// Copyright 2017 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::mir::Mir; |
| 12 | +use rustc::infer::InferCtxt; |
| 13 | +use util::liveness::LivenessResult; |
| 14 | + |
| 15 | +use super::ToRegionIndex; |
| 16 | +use super::region_infer::RegionInferenceContext; |
| 17 | + |
| 18 | +pub fn generate_constraints<'a, 'gcx, 'tcx>(infcx: &InferCtxt<'a, 'gcx, 'tcx>, |
| 19 | + regioncx: &mut RegionInferenceContext, |
| 20 | + mir: &Mir<'tcx>, |
| 21 | + liveness: &LivenessResult) |
| 22 | +{ |
| 23 | + ConstraintGeneration { infcx, regioncx, mir, liveness }.add_constraints(); |
| 24 | +} |
| 25 | + |
| 26 | +struct ConstraintGeneration<'constrain, 'gcx: 'tcx, 'tcx: 'constrain> { |
| 27 | + infcx: &'constrain InferCtxt<'constrain, 'gcx, 'tcx>, |
| 28 | + regioncx: &'constrain mut RegionInferenceContext, |
| 29 | + mir: &'constrain Mir<'tcx>, |
| 30 | + liveness: &'constrain LivenessResult, |
| 31 | +} |
| 32 | + |
| 33 | +impl<'constrain, 'gcx, 'tcx> ConstraintGeneration<'constrain, 'gcx, 'tcx> { |
| 34 | + fn add_constraints(&mut self) { |
| 35 | + // To start, add the liveness constraints. |
| 36 | + self.add_liveness_constraints(); |
| 37 | + } |
| 38 | + |
| 39 | + /// Liveness constraints: |
| 40 | + /// |
| 41 | + /// > If a variable V is live at point P, then all regions R in the type of V |
| 42 | + /// > must include the point P. |
| 43 | + fn add_liveness_constraints(&mut self) { |
| 44 | + let tcx = self.infcx.tcx; |
| 45 | + |
| 46 | + debug!("add_liveness_constraints()"); |
| 47 | + for bb in self.mir.basic_blocks().indices() { |
| 48 | + debug!("add_liveness_constraints: bb={:?}", bb); |
| 49 | + |
| 50 | + self.liveness.simulate_block(self.mir, bb, |location, live_locals| { |
| 51 | + debug!("add_liveness_constraints: location={:?} live_locals={:?}", |
| 52 | + location, live_locals); |
| 53 | + |
| 54 | + for live_local in live_locals.iter() { |
| 55 | + let live_local_ty = self.mir.local_decls[live_local].ty; |
| 56 | + tcx.for_each_free_region(&live_local_ty, |live_region| { |
| 57 | + let vid = live_region.to_region_index(); |
| 58 | + self.regioncx.add_live_point(vid, location); |
| 59 | + }) |
| 60 | + } |
| 61 | + }); |
| 62 | + } |
| 63 | + } |
| 64 | +} |
0 commit comments