]> git.lizzy.rs Git - rust.git/blob - src/librustc_mir/dataflow/move_paths/abs_domain.rs
4d20857bc2ec80ebb70a1ead79ffa485dc99c6cb
[rust.git] / src / librustc_mir / dataflow / move_paths / abs_domain.rs
1 // Copyright 2012-2016 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 //! The move-analysis portion of borrowck needs to work in an abstract
12 //! domain of lifted Places.  Most of the Place variants fall into a
13 //! one-to-one mapping between the concrete and abstract (e.g. a
14 //! field-deref on a local-variable, `x.field`, has the same meaning
15 //! in both domains). Indexed-Projections are the exception: `a[x]`
16 //! needs to be treated as mapping to the same move path as `a[y]` as
17 //! well as `a[13]`, et cetera.
18 //!
19 //! (In theory the analysis could be extended to work with sets of
20 //! paths, so that `a[0]` and `a[13]` could be kept distinct, while
21 //! `a[x]` would still overlap them both. But that is not this
22 //! representation does today.)
23
24 use rustc::mir::{Local, PlaceElem, Operand, ProjectionElem};
25 use rustc::ty::Ty;
26
27 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
28 pub struct AbstractOperand;
29 #[derive(Copy, Clone, PartialEq, Eq, Hash, Debug)]
30 pub struct AbstractType;
31 pub type AbstractElem<'tcx> =
32     ProjectionElem<'tcx, AbstractOperand, AbstractType>;
33
34 pub trait Lift {
35     type Abstract;
36     fn lift(&self) -> Self::Abstract;
37 }
38 impl<'tcx> Lift for Operand<'tcx> {
39     type Abstract = AbstractOperand;
40     fn lift(&self) -> Self::Abstract { AbstractOperand }
41 }
42 impl Lift for Local {
43     type Abstract = AbstractOperand;
44     fn lift(&self) -> Self::Abstract { AbstractOperand }
45 }
46 impl<'tcx> Lift for Ty<'tcx> {
47     type Abstract = AbstractType;
48     fn lift(&self) -> Self::Abstract { AbstractType }
49 }
50 impl<'tcx> Lift for PlaceElem<'tcx> {
51     type Abstract = AbstractElem<'tcx>;
52     fn lift(&self) -> Self::Abstract {
53         match *self {
54             ProjectionElem::Deref =>
55                 ProjectionElem::Deref,
56             ProjectionElem::Field(ref f, ty) =>
57                 ProjectionElem::Field(f.clone(), ty.lift()),
58             ProjectionElem::Index(ref i) =>
59                 ProjectionElem::Index(i.lift()),
60             ProjectionElem::Subslice {from, to} =>
61                 ProjectionElem::Subslice { from: from, to: to },
62             ProjectionElem::ConstantIndex {offset,min_length,from_end} =>
63                 ProjectionElem::ConstantIndex {
64                     offset,
65                     min_length,
66                     from_end,
67                 },
68             ProjectionElem::Downcast(a, u) =>
69                 ProjectionElem::Downcast(a.clone(), u.clone()),
70         }
71     }
72 }