]> git.proxmox.com Git - rustc.git/blame - src/librustc_mir/borrow_check/nll/liveness_map.rs
New upstream version 1.29.0+dfsg1
[rustc.git] / src / librustc_mir / borrow_check / nll / liveness_map.rs
CommitLineData
8faf50e0
XL
1// Copyright 2018 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//! For the NLL computation, we need to compute liveness, but only for those
12//! local variables whose types contain regions. The others are not of interest
13//! to us. This file defines a new index type (LocalWithRegion) that indexes into
14//! a list of "variables whose type contain regions". It also defines a map from
15//! Local to LocalWithRegion and vice versa -- this map can be given to the
16//! liveness code so that it only operates over variables with regions in their
17//! types, instead of all variables.
18
19use rustc::ty::TypeFoldable;
20use rustc_data_structures::indexed_vec::IndexVec;
21use rustc::mir::{Mir, Local};
22use util::liveness::LiveVariableMap;
23
24use rustc_data_structures::indexed_vec::Idx;
25
26/// Map between Local and LocalWithRegion indices: this map is supplied to the
27/// liveness code so that it will only analyze those variables whose types
28/// contain regions.
29crate struct NllLivenessMap {
30 /// For each local variable, contains either None (if the type has no regions)
31 /// or Some(i) with a suitable index.
32 pub from_local: IndexVec<Local, Option<LocalWithRegion>>,
33 /// For each LocalWithRegion, maps back to the original Local index.
34 pub to_local: IndexVec<LocalWithRegion, Local>,
35
36}
37
38impl LiveVariableMap for NllLivenessMap {
39
40 fn from_local(&self, local: Local) -> Option<Self::LiveVar> {
41 self.from_local[local]
42 }
43
44 type LiveVar = LocalWithRegion;
45
46 fn from_live_var(&self, local: Self::LiveVar) -> Local {
47 self.to_local[local]
48 }
49
50 fn num_variables(&self) -> usize {
51 self.to_local.len()
52 }
53}
54
55impl NllLivenessMap {
56 /// Iterates over the variables in Mir and assigns each Local whose type contains
57 /// regions a LocalWithRegion index. Returns a map for converting back and forth.
58 pub fn compute(mir: &Mir) -> Self {
59 let mut to_local = IndexVec::default();
60 let from_local: IndexVec<Local,Option<_>> = mir
61 .local_decls
62 .iter_enumerated()
63 .map(|(local, local_decl)| {
64 if local_decl.ty.has_free_regions() {
65 Some(to_local.push(local))
66 }
67 else {
68 None
69 }
70 }).collect();
71
72 Self { from_local, to_local }
73 }
74}
75
76/// Index given to each local variable whose type contains a region.
77newtype_index!(LocalWithRegion);