-
Notifications
You must be signed in to change notification settings - Fork 13.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Move hir::Place to librustc_middle/hir #74424
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use crate::ty; | ||
use crate::ty::Ty; | ||
|
||
use rustc_hir::HirId; | ||
use rustc_target::abi::VariantIdx; | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)] | ||
pub enum PlaceBase { | ||
/// A temporary variable | ||
Rvalue, | ||
/// A named `static` item | ||
StaticItem, | ||
/// A named local variable | ||
Local(HirId), | ||
/// An upvar referenced by closure env | ||
Upvar(ty::UpvarId), | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)] | ||
pub enum ProjectionKind { | ||
/// A dereference of a pointer, reference or `Box<T>` of the given type | ||
Deref, | ||
|
||
/// `B.F` where `B` is the base expression and `F` is | ||
/// the field. The field is identified by which variant | ||
/// it appears in along with a field index. The variant | ||
/// is used for enums. | ||
Field(u32, VariantIdx), | ||
|
||
/// Some index like `B[x]`, where `B` is the base | ||
/// expression. We don't preserve the index `x` because | ||
/// we won't need it. | ||
Index, | ||
|
||
/// A subslice covering a range of values like `B[x..y]`. | ||
Subslice, | ||
} | ||
|
||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)] | ||
pub struct Projection<'tcx> { | ||
/// Type after the projection is being applied. | ||
pub ty: Ty<'tcx>, | ||
|
||
/// Defines the type of access | ||
pub kind: ProjectionKind, | ||
} | ||
|
||
/// A `Place` represents how a value is located in memory. | ||
/// | ||
/// This is an HIR version of `mir::Place` | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)] | ||
pub struct Place<'tcx> { | ||
/// The type of the `PlaceBase` | ||
pub base_ty: Ty<'tcx>, | ||
/// The "outermost" place that holds this value. | ||
pub base: PlaceBase, | ||
/// How this place is derived from the base place. | ||
pub projections: Vec<Projection<'tcx>>, | ||
} | ||
|
||
/// A `PlaceWithHirId` represents how a value is located in memory. | ||
/// | ||
/// This is an HIR version of `mir::Place` | ||
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, HashStable)] | ||
pub struct PlaceWithHirId<'tcx> { | ||
/// `HirId` of the expression or pattern producing this value. | ||
pub hir_id: HirId, | ||
|
||
/// Information about the `Place` | ||
pub place: Place<'tcx>, | ||
} | ||
|
||
impl<'tcx> PlaceWithHirId<'tcx> { | ||
pub fn new( | ||
hir_id: HirId, | ||
base_ty: Ty<'tcx>, | ||
base: PlaceBase, | ||
projections: Vec<Projection<'tcx>>, | ||
) -> PlaceWithHirId<'tcx> { | ||
PlaceWithHirId { | ||
hir_id: hir_id, | ||
place: Place { base_ty: base_ty, base: base, projections: projections }, | ||
} | ||
} | ||
} | ||
|
||
impl<'tcx> Place<'tcx> { | ||
/// Returns an iterator of the types that have to be dereferenced to access | ||
/// the `Place`. | ||
/// | ||
/// The types are in the reverse order that they are applied. So if | ||
/// `x: &*const u32` and the `Place` is `**x`, then the types returned are | ||
///`*const u32` then `&*const u32`. | ||
pub fn deref_tys(&self) -> impl Iterator<Item = Ty<'tcx>> + '_ { | ||
self.projections.iter().enumerate().rev().filter_map(move |(index, proj)| { | ||
if ProjectionKind::Deref == proj.kind { | ||
Some(self.ty_before_projection(index)) | ||
} else { | ||
None | ||
} | ||
}) | ||
} | ||
|
||
/// Returns the type of this `Place` after all projections have been applied. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doc string fix |
||
pub fn ty(&self) -> Ty<'tcx> { | ||
self.projections.last().map_or_else(|| self.base_ty, |proj| proj.ty) | ||
} | ||
|
||
/// Returns the type of this `Place` immediately before `projection_index`th projection | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. doc string fix |
||
/// is applied. | ||
pub fn ty_before_projection(&self, projection_index: usize) -> Ty<'tcx> { | ||
assert!(projection_index < self.projections.len()); | ||
if projection_index == 0 { self.base_ty } else { self.projections[projection_index - 1].ty } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
doc string fix