|
| 1 | +use std::ops::ControlFlow; |
| 2 | + |
| 3 | +use rustc_abi::{FIRST_VARIANT, FieldIdx, VariantIdx}; |
| 4 | +use rustc_macros::{HashStable, TyEncodable}; |
| 5 | +use rustc_span::{Symbol, sym}; |
| 6 | + |
| 7 | +use crate::ty::{self, List, Ty, TyCtxt}; |
| 8 | + |
| 9 | +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] |
| 10 | +pub enum FieldPathKind { |
| 11 | + OffsetOf, |
| 12 | + FieldOf, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Clone, Copy, PartialEq, Eq, Debug, HashStable, Hash, TyEncodable)] |
| 16 | +pub struct FieldPath<'tcx>(pub &'tcx List<(VariantIdx, FieldIdx)>); |
| 17 | + |
| 18 | +impl<'tcx> IntoIterator for FieldPath<'tcx> { |
| 19 | + type Item = <&'tcx List<(VariantIdx, FieldIdx)> as IntoIterator>::Item; |
| 20 | + |
| 21 | + type IntoIter = <&'tcx List<(VariantIdx, FieldIdx)> as IntoIterator>::IntoIter; |
| 22 | + |
| 23 | + fn into_iter(self) -> Self::IntoIter { |
| 24 | + self.0.into_iter() |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl<'tcx> IntoIterator for &FieldPath<'tcx> { |
| 29 | + type Item = <&'tcx List<(VariantIdx, FieldIdx)> as IntoIterator>::Item; |
| 30 | + |
| 31 | + type IntoIter = <&'tcx List<(VariantIdx, FieldIdx)> as IntoIterator>::IntoIter; |
| 32 | + |
| 33 | + fn into_iter(self) -> Self::IntoIter { |
| 34 | + self.0.into_iter() |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<'tcx> FieldPath<'tcx> { |
| 39 | + pub fn iter(self) -> <Self as IntoIterator>::IntoIter { |
| 40 | + self.into_iter() |
| 41 | + } |
| 42 | + |
| 43 | + pub fn walk<T>( |
| 44 | + self, |
| 45 | + tcx: TyCtxt<'tcx>, |
| 46 | + container: Ty<'tcx>, |
| 47 | + mut walker: impl FnMut(Ty<'tcx>, Symbol, Ty<'tcx>, bool) -> ControlFlow<T>, |
| 48 | + ) -> Option<T> { |
| 49 | + let mut cur = container; |
| 50 | + for (i, (variant, field)) in self.iter().enumerate() { |
| 51 | + let last = i == self.0.len() - 1; |
| 52 | + let (name, field_ty) = match cur.kind() { |
| 53 | + ty::Adt(def, args) => { |
| 54 | + let variant = def.variant(variant); |
| 55 | + let field = &variant.fields[field]; |
| 56 | + let field_ty = field.ty(tcx, args); |
| 57 | + (field.name, field_ty) |
| 58 | + } |
| 59 | + ty::Tuple(tys) => { |
| 60 | + assert_eq!(FIRST_VARIANT, variant); |
| 61 | + (sym::integer(field.index()), tys[field.index()]) |
| 62 | + } |
| 63 | + _ => bug!("only ADTs and tuples are supported by `field_of!`, found {cur}"), |
| 64 | + }; |
| 65 | + match walker(cur, name, field_ty, last) { |
| 66 | + ControlFlow::Break(val) => return Some(val), |
| 67 | + ControlFlow::Continue(()) => cur = field_ty, |
| 68 | + } |
| 69 | + } |
| 70 | + None |
| 71 | + } |
| 72 | + |
| 73 | + pub fn field_ty(self, tcx: TyCtxt<'tcx>, container: Ty<'tcx>) -> Ty<'tcx> { |
| 74 | + self.walk(tcx, container, |_, _, ty, last| { |
| 75 | + if last { ControlFlow::Break(ty) } else { ControlFlow::Continue(()) } |
| 76 | + }) |
| 77 | + .expect("field path to have a last segment") |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +impl<'tcx> rustc_type_ir::inherent::FieldPath<TyCtxt<'tcx>> for FieldPath<'tcx> { |
| 82 | + fn walk<T>( |
| 83 | + self, |
| 84 | + interner: TyCtxt<'tcx>, |
| 85 | + container: Ty<'tcx>, |
| 86 | + walker: impl FnMut(Ty<'tcx>, Symbol, Ty<'tcx>, bool) -> ControlFlow<T>, |
| 87 | + ) -> Option<T> { |
| 88 | + self.walk(interner, container, walker) |
| 89 | + } |
| 90 | + |
| 91 | + fn field_ty(self, interner: TyCtxt<'tcx>, container: Ty<'tcx>) -> Ty<'tcx> { |
| 92 | + self.field_ty(interner, container) |
| 93 | + } |
| 94 | +} |
0 commit comments