|
| 1 | +// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
| 2 | +// option. This file may not be copied, modified, or distributed |
| 3 | +// except according to those terms. |
| 4 | + |
| 5 | +use rustc::mir::{Local, Location}; |
| 6 | +use rustc::mir::Mir; |
| 7 | +use rustc::mir::visit::PlaceContext; |
| 8 | +use rustc::mir::visit::Visitor; |
| 9 | + |
| 10 | +pub struct FindLocalAssignmentVisitor { |
| 11 | + needle: Local, |
| 12 | + locations: Vec<Location>, |
| 13 | +} |
| 14 | + |
| 15 | +impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor { |
| 16 | + fn visit_local(&mut self, |
| 17 | + local: &Local, |
| 18 | + place_context: PlaceContext<'tcx>, |
| 19 | + location: Location) { |
| 20 | + if self.needle != *local { |
| 21 | + return; |
| 22 | + } |
| 23 | + |
| 24 | + match place_context { |
| 25 | + PlaceContext::Store | PlaceContext::Call => { |
| 26 | + self.locations.push(location); |
| 27 | + } |
| 28 | + PlaceContext::AsmOutput | PlaceContext::Drop| PlaceContext::Inspect | |
| 29 | + PlaceContext::Borrow{..}| PlaceContext::Projection(..)| PlaceContext::Copy| |
| 30 | + PlaceContext::Move| PlaceContext::StorageLive| PlaceContext::StorageDead| |
| 31 | + PlaceContext::Validate => { |
| 32 | + } |
| 33 | + } |
| 34 | + |
| 35 | + Visitor::visit_local(self,local,place_context,location) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +crate trait FindAssignments { |
| 40 | + fn find_assignments(&self, local: Local) -> Vec<Location>; |
| 41 | + } |
| 42 | + |
| 43 | +impl<'tcx> FindAssignments for Mir<'tcx>{ |
| 44 | + fn find_assignments(&self, local: Local) -> Vec<Location>{ |
| 45 | + let mut visitor = FindLocalAssignmentVisitor{ needle: local, locations: vec![]}; |
| 46 | + visitor.visit_mir(self); |
| 47 | + visitor.locations |
| 48 | + } |
| 49 | +} |
| 50 | + |
0 commit comments