|
| 1 | +use std::{fmt, marker::PhantomData}; |
| 2 | + |
| 3 | +use syntax::ast; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + hir::{self, def_id::DefId}, |
| 7 | + ty::{ |
| 8 | + self, AdtDef, Binder, BoundTy, ExistentialPredicate, InferTy, List, ParamTy, PolyFnSig, |
| 9 | + ProjectionTy, Region, SubstsRef, Ty, TypeAndMut, |
| 10 | + }, |
| 11 | +}; |
| 12 | + |
| 13 | +pub use self::ViewKind::*; |
| 14 | + |
| 15 | +// TODO Forward eq/hash? |
| 16 | +#[derive(Eq, PartialEq, Hash, TypeFoldable, Lift)] |
| 17 | +pub struct View<'tcx, T> { |
| 18 | + ty: Ty<'tcx>, |
| 19 | + _marker: PhantomData<T>, |
| 20 | +} |
| 21 | + |
| 22 | +impl<T> Copy for View<'_, T> {} |
| 23 | +impl<T> Clone for View<'_, T> { |
| 24 | + fn clone(&self) -> Self { |
| 25 | + View { ty: self.ty, _marker: PhantomData } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +impl<'tcx, T> fmt::Debug for View<'tcx, T> |
| 30 | +where |
| 31 | + T: fmt::Debug, |
| 32 | +{ |
| 33 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 34 | + self.ty.fmt(f) |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +impl<'tcx, T> fmt::Display for View<'tcx, T> |
| 39 | +where |
| 40 | + T: fmt::Display + TyDeref<'tcx> + 'tcx, |
| 41 | +{ |
| 42 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 43 | + (**self).fmt(f) |
| 44 | + } |
| 45 | +} |
| 46 | +impl<'tcx, T> std::ops::Deref for View<'tcx, T> |
| 47 | +where |
| 48 | + T: TyDeref<'tcx> + 'tcx, |
| 49 | +{ |
| 50 | + type Target = T; |
| 51 | + fn deref(&self) -> &Self::Target { |
| 52 | + match T::ty_deref(self.ty) { |
| 53 | + Some(t) => t, |
| 54 | + // SAFETY verified by `View::new` |
| 55 | + None => unsafe { std::hint::unreachable_unchecked() }, |
| 56 | + } |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +impl<'tcx, T> View<'tcx, T> |
| 61 | +where |
| 62 | + T: TyDeref<'tcx> + 'tcx, |
| 63 | +{ |
| 64 | + pub fn new(ty: Ty<'tcx>) -> Option<Self> { |
| 65 | + T::ty_deref(ty)?; |
| 66 | + Some(View { ty, _marker: PhantomData }) |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +impl<'tcx, T> View<'tcx, T> { |
| 71 | + pub fn as_ty(&self) -> Ty<'tcx> { |
| 72 | + self.ty |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +/// SAFETY If `Some` is returned for `ty` then `Some` must always be returned for any subsequent |
| 77 | +/// call with the same `Ty` value |
| 78 | +pub unsafe trait TyDeref<'tcx>: Sized { |
| 79 | + fn ty_deref(ty: Ty<'tcx>) -> Option<&'tcx Self>; |
| 80 | +} |
| 81 | + |
| 82 | +unsafe impl<'tcx> TyDeref<'tcx> for ty::ParamTy { |
| 83 | + fn ty_deref(ty: Ty<'tcx>) -> Option<&'tcx Self> { |
| 84 | + match &ty.kind { |
| 85 | + ty::Param(p) => Some(p), |
| 86 | + _ => None, |
| 87 | + } |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +pub enum ViewKind<'tcx> { |
| 92 | + Bool, |
| 93 | + |
| 94 | + Char, |
| 95 | + |
| 96 | + Int(ast::IntTy), |
| 97 | + |
| 98 | + Uint(ast::UintTy), |
| 99 | + |
| 100 | + Float(ast::FloatTy), |
| 101 | + |
| 102 | + Adt(&'tcx AdtDef, SubstsRef<'tcx>), |
| 103 | + |
| 104 | + Foreign(DefId), |
| 105 | + |
| 106 | + Str, |
| 107 | + |
| 108 | + Array(Ty<'tcx>, &'tcx ty::Const<'tcx>), |
| 109 | + |
| 110 | + Slice(Ty<'tcx>), |
| 111 | + |
| 112 | + RawPtr(TypeAndMut<'tcx>), |
| 113 | + |
| 114 | + Ref(Region<'tcx>, Ty<'tcx>, hir::Mutability), |
| 115 | + |
| 116 | + FnDef(DefId, SubstsRef<'tcx>), |
| 117 | + |
| 118 | + FnPtr(PolyFnSig<'tcx>), |
| 119 | + |
| 120 | + Dynamic(Binder<&'tcx List<ExistentialPredicate<'tcx>>>, ty::Region<'tcx>), |
| 121 | + |
| 122 | + Closure(DefId, SubstsRef<'tcx>), |
| 123 | + |
| 124 | + Generator(DefId, SubstsRef<'tcx>, hir::Movability), |
| 125 | + |
| 126 | + GeneratorWitness(Binder<&'tcx List<Ty<'tcx>>>), |
| 127 | + |
| 128 | + Never, |
| 129 | + |
| 130 | + Tuple(SubstsRef<'tcx>), |
| 131 | + |
| 132 | + Projection(ProjectionTy<'tcx>), |
| 133 | + |
| 134 | + UnnormalizedProjection(ProjectionTy<'tcx>), |
| 135 | + |
| 136 | + Opaque(DefId, SubstsRef<'tcx>), |
| 137 | + |
| 138 | + Param(View<'tcx, ParamTy>), |
| 139 | + |
| 140 | + Bound(ty::DebruijnIndex, BoundTy), |
| 141 | + |
| 142 | + Placeholder(ty::PlaceholderType), |
| 143 | + |
| 144 | + Infer(InferTy), |
| 145 | + |
| 146 | + Error, |
| 147 | +} |
| 148 | + |
| 149 | +impl<'tcx> From<Ty<'tcx>> for ViewKind<'tcx> { |
| 150 | + fn from(ty: Ty<'tcx>) -> Self { |
| 151 | + match ty.kind { |
| 152 | + ty::RawPtr(tm) => Self::RawPtr(tm), |
| 153 | + ty::Array(typ, sz) => Self::Array(typ, sz), |
| 154 | + ty::Slice(typ) => Self::Slice(typ), |
| 155 | + ty::Adt(tid, substs) => Self::Adt(tid, substs), |
| 156 | + ty::Dynamic(trait_ty, region) => Self::Dynamic(trait_ty, region), |
| 157 | + ty::Tuple(ts) => Self::Tuple(ts), |
| 158 | + ty::FnDef(def_id, substs) => Self::FnDef(def_id, substs), |
| 159 | + ty::FnPtr(f) => Self::FnPtr(f), |
| 160 | + ty::Ref(r, ty, mutbl) => Self::Ref(r, ty, mutbl), |
| 161 | + ty::Generator(did, substs, movability) => Self::Generator(did, substs, movability), |
| 162 | + ty::GeneratorWitness(types) => Self::GeneratorWitness(types), |
| 163 | + ty::Closure(did, substs) => Self::Closure(did, substs), |
| 164 | + ty::Projection(data) => Self::Projection(data), |
| 165 | + ty::UnnormalizedProjection(data) => Self::UnnormalizedProjection(data), |
| 166 | + ty::Opaque(did, substs) => Self::Opaque(did, substs), |
| 167 | + ty::Bool => Self::Bool, |
| 168 | + ty::Char => Self::Char, |
| 169 | + ty::Str => Self::Str, |
| 170 | + ty::Int(i) => Self::Int(i), |
| 171 | + ty::Uint(i) => Self::Uint(i), |
| 172 | + ty::Float(f) => Self::Float(f), |
| 173 | + ty::Error => Self::Error, |
| 174 | + ty::Infer(i) => Self::Infer(i), |
| 175 | + ty::Param(_) => Self::Param(View::new(ty).unwrap()), |
| 176 | + ty::Bound(b, c) => Self::Bound(b, c), |
| 177 | + ty::Placeholder(p) => Self::Placeholder(p), |
| 178 | + ty::Never => Self::Never, |
| 179 | + ty::Foreign(f) => Self::Foreign(f), |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments