]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/borrow_check/location.rs
Rollup merge of #55930 - ljedrz:hir_bonuses, r=cramertj
[rust.git] / src / librustc_mir / borrow_check / location.rs
1 // Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use rustc::mir::{BasicBlock, Location, Mir};
12 use rustc_data_structures::indexed_vec::{Idx, IndexVec};
13
14 /// Maps between a MIR Location, which identifies a particular
15 /// statement within a basic block, to a "rich location", which
16 /// identifies at a finer granularity. In particular, we distinguish
17 /// the *start* of a statement and the *mid-point*. The mid-point is
18 /// the point *just* before the statement takes effect; in particular,
19 /// for an assignment `A = B`, it is the point where B is about to be
20 /// written into A. This mid-point is a kind of hack to work around
21 /// our inability to track the position information at sufficient
22 /// granularity through outlives relations; however, the rich location
23 /// table serves another purpose: it compresses locations from
24 /// multiple words into a single u32.
25 crate struct LocationTable {
26     num_points: usize,
27     statements_before_block: IndexVec<BasicBlock, usize>,
28 }
29
30 newtype_index! {
31     pub struct LocationIndex {
32         DEBUG_FORMAT = "LocationIndex({})"
33     }
34 }
35
36 #[derive(Copy, Clone, Debug)]
37 crate enum RichLocation {
38     Start(Location),
39     Mid(Location),
40 }
41
42 impl LocationTable {
43     crate fn new(mir: &Mir<'_>) -> Self {
44         let mut num_points = 0;
45         let statements_before_block = mir.basic_blocks()
46             .iter()
47             .map(|block_data| {
48                 let v = num_points;
49                 num_points += (block_data.statements.len() + 1) * 2;
50                 v
51             })
52             .collect();
53
54         debug!(
55             "LocationTable(statements_before_block={:#?})",
56             statements_before_block
57         );
58         debug!("LocationTable: num_points={:#?}", num_points);
59
60         Self {
61             num_points,
62             statements_before_block,
63         }
64     }
65
66     crate fn all_points(&self) -> impl Iterator<Item = LocationIndex> {
67         (0..self.num_points).map(LocationIndex::new)
68     }
69
70     crate fn start_index(&self, location: Location) -> LocationIndex {
71         let Location {
72             block,
73             statement_index,
74         } = location;
75         let start_index = self.statements_before_block[block];
76         LocationIndex::new(start_index + statement_index * 2)
77     }
78
79     crate fn mid_index(&self, location: Location) -> LocationIndex {
80         let Location {
81             block,
82             statement_index,
83         } = location;
84         let start_index = self.statements_before_block[block];
85         LocationIndex::new(start_index + statement_index * 2 + 1)
86     }
87
88     crate fn to_location(&self, index: LocationIndex) -> RichLocation {
89         let point_index = index.index();
90
91         // Find the basic block. We have a vector with the
92         // starting index of the statement in each block. Imagine
93         // we have statement #22, and we have a vector like:
94         //
95         // [0, 10, 20]
96         //
97         // In that case, this represents point_index 2 of
98         // basic block BB2. We know this because BB0 accounts for
99         // 0..10, BB1 accounts for 11..20, and BB2 accounts for
100         // 20...
101         //
102         // To compute this, we could do a binary search, but
103         // because I am lazy we instead iterate through to find
104         // the last point where the "first index" (0, 10, or 20)
105         // was less than the statement index (22). In our case, this will
106         // be (BB2, 20).
107         let (block, &first_index) = self.statements_before_block
108             .iter_enumerated()
109             .filter(|(_, first_index)| **first_index <= point_index)
110             .last()
111             .unwrap();
112
113         let statement_index = (point_index - first_index) / 2;
114         if index.is_start() {
115             RichLocation::Start(Location { block, statement_index })
116         } else {
117             RichLocation::Mid(Location { block, statement_index })
118         }
119     }
120 }
121
122 impl LocationIndex {
123     fn is_start(&self) -> bool {
124         // even indices are start points; odd indices are mid points
125         (self.index() % 2) == 0
126     }
127 }