@@ -274,6 +274,32 @@ pub enum BinOp {
274274 Offset ,
275275}
276276
277+ impl BinOp {
278+ /// Return the type of this operation for the given input Ty.
279+ /// This function does not perform type checking, and it currently doesn't handle SIMD.
280+ pub fn ty ( & self , lhs_ty : Ty , _rhs_ty : Ty ) -> Ty {
281+ match self {
282+ BinOp :: Add
283+ | BinOp :: AddUnchecked
284+ | BinOp :: Sub
285+ | BinOp :: SubUnchecked
286+ | BinOp :: Mul
287+ | BinOp :: MulUnchecked
288+ | BinOp :: Div
289+ | BinOp :: Rem
290+ | BinOp :: BitXor
291+ | BinOp :: BitAnd
292+ | BinOp :: BitOr
293+ | BinOp :: Shl
294+ | BinOp :: ShlUnchecked
295+ | BinOp :: Shr
296+ | BinOp :: ShrUnchecked
297+ | BinOp :: Offset => lhs_ty,
298+ BinOp :: Eq | BinOp :: Lt | BinOp :: Le | BinOp :: Ne | BinOp :: Ge | BinOp :: Gt => Ty :: bool_ty ( ) ,
299+ }
300+ }
301+ }
302+
277303#[ derive( Clone , Debug , Eq , PartialEq ) ]
278304pub enum UnOp {
279305 Not ,
@@ -475,6 +501,63 @@ pub enum Rvalue {
475501 Use ( Operand ) ,
476502}
477503
504+ impl Rvalue {
505+ pub fn ty ( & self , locals : & [ LocalDecl ] ) -> Result < Ty , Error > {
506+ match self {
507+ Rvalue :: Use ( operand) => operand. ty ( locals) ,
508+ Rvalue :: Repeat ( operand, count) => {
509+ Ok ( Ty :: new_array_with_const_len ( operand. ty ( locals) ?, count. clone ( ) ) )
510+ }
511+ Rvalue :: ThreadLocalRef ( did) => Ok ( did. ty ( ) ) ,
512+ Rvalue :: Ref ( reg, bk, place) => {
513+ let place_ty = place. ty ( locals) ?;
514+ Ok ( Ty :: new_ref ( reg. clone ( ) , place_ty, bk. to_mutable_lossy ( ) ) )
515+ }
516+ Rvalue :: AddressOf ( mutability, place) => {
517+ let place_ty = place. ty ( locals) ?;
518+ Ok ( Ty :: new_ptr ( place_ty, * mutability) )
519+ }
520+ Rvalue :: Len ( ..) => Ok ( Ty :: usize_ty ( ) ) ,
521+ Rvalue :: Cast ( .., ty) => Ok ( * ty) ,
522+ Rvalue :: BinaryOp ( op, lhs, rhs) => {
523+ let lhs_ty = lhs. ty ( locals) ?;
524+ let rhs_ty = rhs. ty ( locals) ?;
525+ Ok ( op. ty ( lhs_ty, rhs_ty) )
526+ }
527+ Rvalue :: CheckedBinaryOp ( op, lhs, rhs) => {
528+ let lhs_ty = lhs. ty ( locals) ?;
529+ let rhs_ty = rhs. ty ( locals) ?;
530+ let ty = op. ty ( lhs_ty, rhs_ty) ;
531+ Ok ( Ty :: new_tuple ( & [ ty, Ty :: bool_ty ( ) ] ) )
532+ }
533+ Rvalue :: UnaryOp ( UnOp :: Not | UnOp :: Neg , operand) => operand. ty ( locals) ,
534+ Rvalue :: Discriminant ( place) => {
535+ let place_ty = place. ty ( locals) ?;
536+ place_ty
537+ . kind ( )
538+ . discriminant_ty ( )
539+ . ok_or_else ( || error ! ( "Expected a `RigidTy` but found: {place_ty:?}" ) )
540+ }
541+ Rvalue :: NullaryOp ( NullOp :: SizeOf | NullOp :: AlignOf | NullOp :: OffsetOf ( ..) , _) => {
542+ Ok ( Ty :: usize_ty ( ) )
543+ }
544+ Rvalue :: Aggregate ( ak, ops) => match * ak {
545+ AggregateKind :: Array ( ty) => Ty :: try_new_array ( ty, ops. len ( ) as u64 ) ,
546+ AggregateKind :: Tuple => Ok ( Ty :: new_tuple (
547+ & ops. iter ( ) . map ( |op| op. ty ( locals) ) . collect :: < Result < Vec < _ > , _ > > ( ) ?,
548+ ) ) ,
549+ AggregateKind :: Adt ( def, _, ref args, _, _) => Ok ( def. ty_with_args ( args) ) ,
550+ AggregateKind :: Closure ( def, ref args) => Ok ( Ty :: new_closure ( def, args. clone ( ) ) ) ,
551+ AggregateKind :: Coroutine ( def, ref args, mov) => {
552+ Ok ( Ty :: new_coroutine ( def, args. clone ( ) , mov) )
553+ }
554+ } ,
555+ Rvalue :: ShallowInitBox ( _, ty) => Ok ( Ty :: new_box ( * ty) ) ,
556+ Rvalue :: CopyForDeref ( place) => place. ty ( locals) ,
557+ }
558+ }
559+ }
560+
478561#[ derive( Clone , Debug , Eq , PartialEq ) ]
479562pub enum AggregateKind {
480563 Array ( Ty ) ,
@@ -725,6 +808,17 @@ pub enum BorrowKind {
725808 } ,
726809}
727810
811+ impl BorrowKind {
812+ pub fn to_mutable_lossy ( self ) -> Mutability {
813+ match self {
814+ BorrowKind :: Mut { .. } => Mutability :: Mut ,
815+ BorrowKind :: Shared => Mutability :: Not ,
816+ // There's no type corresponding to a shallow borrow, so use `&` as an approximation.
817+ BorrowKind :: Fake => Mutability :: Not ,
818+ }
819+ }
820+ }
821+
728822#[ derive( Copy , Clone , Debug , Eq , PartialEq ) ]
729823pub enum MutBorrowKind {
730824 Default ,
0 commit comments