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
#![deny(rustc::untranslatable_diagnostic)]
#![deny(rustc::diagnostic_outside_of_impl)]
use rustc_index::vec::{Idx, IndexVec};
use rustc_middle::mir::{BasicBlock, Body, Location};
pub struct LocationTable {
num_points: usize,
statements_before_block: IndexVec<BasicBlock, usize>,
}
rustc_index::newtype_index! {
pub struct LocationIndex {
DEBUG_FORMAT = "LocationIndex({})"
}
}
#[derive(Copy, Clone, Debug)]
pub enum RichLocation {
Start(Location),
Mid(Location),
}
impl LocationTable {
pub(crate) fn new(body: &Body<'_>) -> Self {
let mut num_points = 0;
let statements_before_block = body
.basic_blocks
.iter()
.map(|block_data| {
let v = num_points;
num_points += (block_data.statements.len() + 1) * 2;
v
})
.collect();
debug!("LocationTable(statements_before_block={:#?})", statements_before_block);
debug!("LocationTable: num_points={:#?}", num_points);
Self { num_points, statements_before_block }
}
pub fn all_points(&self) -> impl Iterator<Item = LocationIndex> {
(0..self.num_points).map(LocationIndex::new)
}
pub fn start_index(&self, location: Location) -> LocationIndex {
let Location { block, statement_index } = location;
let start_index = self.statements_before_block[block];
LocationIndex::new(start_index + statement_index * 2)
}
pub fn mid_index(&self, location: Location) -> LocationIndex {
let Location { block, statement_index } = location;
let start_index = self.statements_before_block[block];
LocationIndex::new(start_index + statement_index * 2 + 1)
}
pub fn to_location(&self, index: LocationIndex) -> RichLocation {
let point_index = index.index();
let (block, &first_index) = self
.statements_before_block
.iter_enumerated()
.rfind(|&(_, &first_index)| first_index <= point_index)
.unwrap();
let statement_index = (point_index - first_index) / 2;
if index.is_start() {
RichLocation::Start(Location { block, statement_index })
} else {
RichLocation::Mid(Location { block, statement_index })
}
}
}
impl LocationIndex {
fn is_start(self) -> bool {
(self.index() % 2) == 0
}
}