Skip to content

Commit 6eb57e9

Browse files
nikomatsakisspastorino
authored andcommitted
introduce NeoPlaceTree
1 parent 53e604c commit 6eb57e9

File tree

2 files changed

+62
-5
lines changed

2 files changed

+62
-5
lines changed

src/librustc/mir/mod.rs

Lines changed: 51 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,13 +1907,59 @@ pub enum Place<'tcx> {
19071907
}
19081908

19091909
/// A new Place repr
1910-
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
1910+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
19111911
pub struct NeoPlace<'tcx> {
19121912
pub base: PlaceBase<'tcx>,
1913-
pub elems: &'tcx List<PlaceElem<'tcx>>,
1913+
pub elems: &'tcx [PlaceElem<'tcx>],
19141914
}
19151915

1916-
impl<'tcx> serialize::UseSpecializedDecodable for &'tcx List<PlaceElem<'tcx>> {}
1916+
// FIXME
1917+
// impl<'tcx> serialize::UseSpecializedDecodable for &'tcx List<PlaceElem<'tcx>> {}
1918+
1919+
impl NeoPlace<'tcx> {
1920+
/// Return `Some` if this place has no projections -- else return `None`.
1921+
/// So for `a` would return `Some`, but for `a.b.c` would return `None`.
1922+
pub fn as_base(&self) -> Option<&PlaceBase<'tcx>> {
1923+
if !self.elems.is_empty() {
1924+
// this is something like `a.b.c`
1925+
return None;
1926+
}
1927+
1928+
Some(&self.base)
1929+
}
1930+
1931+
/// Return `Some` if this is just a reference to a local variable
1932+
/// (e.g., `a`) and `None` if it is something else (e.g.,
1933+
/// `a.b.c`).
1934+
pub fn as_local(&self) -> Option<Local> {
1935+
match self.as_base()? {
1936+
PlaceBase::Local(l) => Some(*l),
1937+
_ => None,
1938+
}
1939+
}
1940+
1941+
pub fn into_tree(self) -> NeoPlaceTree<'tcx> {
1942+
match self.elems.split_last() {
1943+
None => NeoPlaceTree::Base(self.base),
1944+
Some((last_element, other_elements)) => {
1945+
NeoPlaceTree::Projected(Projection {
1946+
base: NeoPlace { base: self.base, elems: other_elements },
1947+
elem: *last_element,
1948+
})
1949+
}
1950+
}
1951+
}
1952+
}
1953+
1954+
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
1955+
pub enum NeoPlaceTree<'tcx> {
1956+
Base(PlaceBase<'tcx>),
1957+
Projected(NeoPlaceProjection<'tcx>),
1958+
}
1959+
1960+
/// Alias for projections as they appear in places, where the base is a place
1961+
/// and the index is a local.
1962+
pub type NeoPlaceProjection<'tcx> = Projection<'tcx, NeoPlace<'tcx>, Local, Ty<'tcx>>;
19171963

19181964
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
19191965
pub fn as_new_place(self, place: &Place<'tcx>) -> NeoPlace<'tcx> {
@@ -1977,7 +2023,7 @@ impl_stable_hash_for!(struct Static<'tcx> {
19772023
/// or `*B` or `B[index]`. Note that it is parameterized because it is
19782024
/// shared between `Constant` and `Place`. See the aliases
19792025
/// `PlaceProjection` etc below.
1980-
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
2026+
#[derive(Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, RustcEncodable, RustcDecodable)]
19812027
pub struct Projection<'tcx, B, V, T> {
19822028
pub base: B,
19832029
pub elem: ProjectionElem<'tcx, V, T>,
@@ -3483,7 +3529,7 @@ impl<'tcx> TypeFoldable<'tcx> for PlaceBase<'tcx> {
34833529
}
34843530
}
34853531

3486-
impl<'tcx> TypeFoldable<'tcx> for &'tcx List<PlaceElem<'tcx>> {
3532+
impl<'tcx> TypeFoldable<'tcx> for &'tcx [PlaceElem<'tcx>] {
34873533
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
34883534
let v = self.iter().map(|p| p.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
34893535
folder.tcx().intern_place_elems(&v)

src/librustc/ty/structural_impls.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,17 @@ impl<'tcx> TypeFoldable<'tcx> for &'tcx ty::List<ProjectionKind<'tcx>> {
649649
}
650650
}
651651

652+
impl<'tcx> TypeFoldable<'tcx> for &'tcx [ProjectionKind<'tcx>] {
653+
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
654+
let v = self.iter().map(|t| t.fold_with(folder)).collect::<SmallVec<[_; 8]>>();
655+
folder.tcx().intern_projs(&v)
656+
}
657+
658+
fn super_visit_with<V: TypeVisitor<'tcx>>(&self, visitor: &mut V) -> bool {
659+
self.iter().any(|t| t.visit_with(visitor))
660+
}
661+
}
662+
652663
impl<'tcx> TypeFoldable<'tcx> for ty::instance::Instance<'tcx> {
653664
fn super_fold_with<'gcx: 'tcx, F: TypeFolder<'gcx, 'tcx>>(&self, folder: &mut F) -> Self {
654665
use ty::InstanceDef::*;

0 commit comments

Comments
 (0)