]>
Commit | Line | Data |
---|---|---|
ba9703b0 XL |
1 | use rustc_middle::mir::visit::PlaceContext; |
2 | use rustc_middle::mir::visit::Visitor; | |
3 | use rustc_middle::mir::{Body, Local, Location}; | |
83c7162d | 4 | |
c295e0f8 | 5 | pub trait FindAssignments { |
83c7162d XL |
6 | // Finds all statements that assign directly to local (i.e., X = ...) |
7 | // and returns their locations. | |
8 | fn find_assignments(&self, local: Local) -> Vec<Location>; | |
9 | } | |
10 | ||
ba9703b0 | 11 | impl<'tcx> FindAssignments for Body<'tcx> { |
dfeec247 XL |
12 | fn find_assignments(&self, local: Local) -> Vec<Location> { |
13 | let mut visitor = FindLocalAssignmentVisitor { needle: local, locations: vec![] }; | |
ba9703b0 | 14 | visitor.visit_body(self); |
dfeec247 | 15 | visitor.locations |
83c7162d XL |
16 | } |
17 | } | |
18 | ||
19 | // The Visitor walks the MIR to return the assignment statements corresponding | |
20 | // to a Local. | |
21 | struct FindLocalAssignmentVisitor { | |
22 | needle: Local, | |
23 | locations: Vec<Location>, | |
24 | } | |
25 | ||
26 | impl<'tcx> Visitor<'tcx> for FindLocalAssignmentVisitor { | |
064997fb FG |
27 | fn visit_local(&mut self, local: Local, place_context: PlaceContext, location: Location) { |
28 | if self.needle != local { | |
83c7162d XL |
29 | return; |
30 | } | |
31 | ||
13cf67c4 | 32 | if place_context.is_place_assignment() { |
8faf50e0 | 33 | self.locations.push(location); |
83c7162d XL |
34 | } |
35 | } | |
8faf50e0 | 36 | } |