Files
rust/compiler/rustc_mir/src/util/collect_writes.rs

37 lines
1.1 KiB
Rust
Raw Normal View History

2020-03-29 16:41:09 +02:00
use rustc_middle::mir::visit::PlaceContext;
use rustc_middle::mir::visit::Visitor;
use rustc_middle::mir::{Body, Local, Location};
2018-03-03 01:49:55 +05:30
2018-03-14 23:51:54 +05:30
crate trait FindAssignments {
// Finds all statements that assign directly to local (i.e., X = ...)
// and returns their locations.
fn find_assignments(&self, local: Local) -> Vec<Location>;
}
impl<'tcx> FindAssignments for Body<'tcx> {
2019-12-22 17:42:04 -05:00
fn find_assignments(&self, local: Local) -> Vec<Location> {
let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] };
visitor.visit_body(self);
2019-12-22 17:42:04 -05:00
visitor.locations
2018-03-14 23:51:54 +05:30
}
}
// The Visitor walks the MIR to return the assignment statements corresponding
// to a Local.
2018-03-14 23:51:54 +05:30
struct FindLocalAssignmentVisitor {
2018-03-03 01:49:55 +05:30
needle: Local,
locations: Vec<Location>,
}
impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor {
2019-12-22 17:42:04 -05:00
fn visit_local(&mut self, local: &Local, place_context: PlaceContext, location: Location) {
2018-03-03 01:49:55 +05:30
if self.needle != *local {
return;
}
2018-03-03 01:49:55 +05:30
if place_context.is_place_assignment() {
self.locations.push(location);
2018-03-03 01:49:55 +05:30
}
}
}