5 use rustc_target::abi::VariantIdx;
20 /// A temporary variable
22 /// A named `static` item
24 /// A named local variable
26 /// An upvar referenced by closure env
42 pub enum ProjectionKind {
43 /// A dereference of a pointer, reference or `Box<T>` of the given type
46 /// `B.F` where `B` is the base expression and `F` is
47 /// the field. The field is identified by which variant
48 /// it appears in along with a field index. The variant
49 /// is used for enums.
50 Field(u32, VariantIdx),
52 /// Some index like `B[x]`, where `B` is the base
53 /// expression. We don't preserve the index `x` because
57 /// A subslice covering a range of values like `B[x..y]`.
73 pub struct Projection<'tcx> {
74 /// Type after the projection is being applied.
77 /// Defines the type of access
78 pub kind: ProjectionKind,
81 /// A `Place` represents how a value is located in memory.
83 /// This is an HIR version of `mir::Place`
84 #[derive(Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
85 pub struct Place<'tcx> {
86 /// The type of the `PlaceBase`
87 pub base_ty: Ty<'tcx>,
88 /// The "outermost" place that holds this value.
90 /// How this place is derived from the base place.
91 pub projections: Vec<Projection<'tcx>>,
94 /// A `PlaceWithHirId` represents how a value is located in memory.
96 /// This is an HIR version of `mir::Place`
97 #[derive(Clone, Debug, PartialEq, Eq, Hash, TyEncodable, TyDecodable, TypeFoldable, HashStable)]
98 pub struct PlaceWithHirId<'tcx> {
99 /// `HirId` of the expression or pattern producing this value.
102 /// Information about the `Place`
103 pub place: Place<'tcx>,
106 impl<'tcx> PlaceWithHirId<'tcx> {
111 projections: Vec<Projection<'tcx>>,
112 ) -> PlaceWithHirId<'tcx> {
115 place: Place { base_ty: base_ty, base: base, projections: projections },
120 impl<'tcx> Place<'tcx> {
121 /// Returns an iterator of the types that have to be dereferenced to access
124 /// The types are in the reverse order that they are applied. So if
125 /// `x: &*const u32` and the `Place` is `**x`, then the types returned are
126 ///`*const u32` then `&*const u32`.
127 pub fn deref_tys(&self) -> impl Iterator<Item = Ty<'tcx>> + '_ {
128 self.projections.iter().enumerate().rev().filter_map(move |(index, proj)| {
129 if ProjectionKind::Deref == proj.kind {
130 Some(self.ty_before_projection(index))
137 /// Returns the type of this `Place` after all projections have been applied.
138 pub fn ty(&self) -> Ty<'tcx> {
139 self.projections.last().map_or(self.base_ty, |proj| proj.ty)
142 /// Returns the type of this `Place` immediately before `projection_index`th projection
144 pub fn ty_before_projection(&self, projection_index: usize) -> Ty<'tcx> {
145 assert!(projection_index < self.projections.len());
146 if projection_index == 0 { self.base_ty } else { self.projections[projection_index - 1].ty }