1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
pub use super::*;
use rustc::mir::*;
use rustc::mir::visit::Visitor;
use dataflow::BitDenotation;
#[derive(Copy, Clone)]
pub struct HaveBeenBorrowedLocals<'a, 'tcx: 'a> {
mir: &'a Mir<'tcx>,
}
impl<'a, 'tcx: 'a> HaveBeenBorrowedLocals<'a, 'tcx> {
pub fn new(mir: &'a Mir<'tcx>)
-> Self {
HaveBeenBorrowedLocals { mir: mir }
}
pub fn mir(&self) -> &Mir<'tcx> {
self.mir
}
}
impl<'a, 'tcx> BitDenotation for HaveBeenBorrowedLocals<'a, 'tcx> {
type Idx = Local;
fn name() -> &'static str { "has_been_borrowed_locals" }
fn bits_per_block(&self) -> usize {
self.mir.local_decls.len()
}
fn start_block_effect(&self, _sets: &mut IdxSet<Local>) {
}
fn statement_effect(&self,
sets: &mut BlockSets<Local>,
loc: Location) {
let stmt = &self.mir[loc.block].statements[loc.statement_index];
BorrowedLocalsVisitor {
sets,
}.visit_statement(loc.block, stmt, loc);
match stmt.kind {
StatementKind::StorageDead(l) => sets.kill(&l),
_ => (),
}
}
fn terminator_effect(&self,
sets: &mut BlockSets<Local>,
loc: Location) {
BorrowedLocalsVisitor {
sets,
}.visit_terminator(loc.block, self.mir[loc.block].terminator(), loc);
}
fn propagate_call_return(&self,
_in_out: &mut IdxSet<Local>,
_call_bb: mir::BasicBlock,
_dest_bb: mir::BasicBlock,
_dest_place: &mir::Place) {
}
}
impl<'a, 'tcx> BitwiseOperator for HaveBeenBorrowedLocals<'a, 'tcx> {
#[inline]
fn join(&self, pred1: Word, pred2: Word) -> Word {
pred1 | pred2
}
}
impl<'a, 'tcx> InitialFlow for HaveBeenBorrowedLocals<'a, 'tcx> {
#[inline]
fn bottom_value() -> bool {
false
}
}
struct BorrowedLocalsVisitor<'b, 'c: 'b> {
sets: &'b mut BlockSets<'c, Local>,
}
fn find_local<'tcx>(place: &Place<'tcx>) -> Option<Local> {
match *place {
Place::Local(l) => Some(l),
Place::Promoted(_) |
Place::Static(..) => None,
Place::Projection(ref proj) => {
match proj.elem {
ProjectionElem::Deref => None,
_ => find_local(&proj.base)
}
}
}
}
impl<'tcx, 'b, 'c> Visitor<'tcx> for BorrowedLocalsVisitor<'b, 'c> {
fn visit_rvalue(&mut self,
rvalue: &Rvalue<'tcx>,
location: Location) {
if let Rvalue::Ref(_, _, ref place) = *rvalue {
if let Some(local) = find_local(place) {
self.sets.gen(&local);
}
}
self.super_rvalue(rvalue, location)
}
}