@@ -1279,7 +1279,7 @@ impl TypedAstContext {
12791279 } else {
12801280 Some ( rhs_type_id)
12811281 }
1282- } else if op == CBinOp :: ShiftLeft || op == CBinOp :: ShiftRight {
1282+ } else if op. is_bitshift ( ) {
12831283 Some ( lhs_type_id)
12841284 } else {
12851285 return ;
@@ -2164,28 +2164,81 @@ impl CBinOp {
21642164 }
21652165 }
21662166
2167- /// Does the rust equivalent of this operator have type (T, T) -> U?
2168- #[ rustfmt:: skip]
2169- pub fn input_types_same ( & self ) -> bool {
2167+ /// Is this a (non-assignment) arithmetic operator?
2168+ pub fn is_arithmetic ( & self ) -> bool {
21702169 use CBinOp :: * ;
2171- self . all_types_same ( ) || matches ! ( self ,
2172- Less | Greater | LessEqual | GreaterEqual | EqualEqual | NotEqual
2173- | And | Or
2174- | AssignAdd | AssignSubtract | AssignMultiply | AssignDivide | AssignModulus
2175- | AssignBitXor | AssignShiftLeft | AssignShiftRight | AssignBitOr | AssignBitAnd
2176- | Assign
2177- )
2170+ matches ! ( self , Add | Subtract | Multiply | Divide | Modulus )
21782171 }
21792172
2180- /// Does the rust equivalent of this operator have type (T, T) -> T?
2181- /// This ignores cases where one argument is a pointer and we translate to `.offset()`.
2182- pub fn all_types_same ( & self ) -> bool {
2173+ /// Is this a (non-assignment) arithmetic operator that can be used with pointers?
2174+ pub fn is_pointer_arithmetic ( & self ) -> bool {
2175+ use CBinOp :: * ;
2176+ matches ! ( self , Add | Subtract )
2177+ }
2178+
2179+ /// Is this a (non-assignment, non-shift) bitwise operator?
2180+ pub fn is_bitwise ( & self ) -> bool {
2181+ use CBinOp :: * ;
2182+ matches ! ( self , BitAnd | BitOr | BitXor )
2183+ }
2184+
2185+ /// Is this a (non-assignment) bitshift operator?
2186+ pub fn is_bitshift ( & self ) -> bool {
2187+ use CBinOp :: * ;
2188+ matches ! ( self , ShiftLeft | ShiftRight )
2189+ }
2190+
2191+ /// Is this a logical operator?
2192+ pub fn is_logical ( & self ) -> bool {
2193+ use CBinOp :: * ;
2194+ matches ! ( self , And | Or )
2195+ }
2196+
2197+ /// Is this a comparison operator?
2198+ pub fn is_comparison ( & self ) -> bool {
21832199 use CBinOp :: * ;
21842200 matches ! (
21852201 self ,
2186- Multiply | Divide | Modulus | Add | Subtract | BitAnd | BitXor | BitOr
2202+ EqualEqual | NotEqual | Less | Greater | LessEqual | GreaterEqual
21872203 )
21882204 }
2205+
2206+ /// Is this a (simple or compound) assignment operator?
2207+ pub fn is_assignment ( & self ) -> bool {
2208+ matches ! ( self , Self :: Assign ) || self . underlying_assignment ( ) . is_some ( )
2209+ }
2210+
2211+ /// Maps compound assignment operators to operator underlying them, and returns `None` for all
2212+ /// other operators.
2213+ ///
2214+ /// For example, `AssignAdd` maps to `Some(Add)` but `Add` maps to `None`.
2215+ pub fn underlying_assignment ( & self ) -> Option < CBinOp > {
2216+ use CBinOp :: * ;
2217+ Some ( match * self {
2218+ AssignAdd => Add ,
2219+ AssignSubtract => Subtract ,
2220+ AssignMultiply => Multiply ,
2221+ AssignDivide => Divide ,
2222+ AssignModulus => Modulus ,
2223+ AssignBitXor => BitXor ,
2224+ AssignShiftLeft => ShiftLeft ,
2225+ AssignShiftRight => ShiftRight ,
2226+ AssignBitOr => BitOr ,
2227+ AssignBitAnd => BitAnd ,
2228+ _ => return None ,
2229+ } )
2230+ }
2231+
2232+ /// Does the rust equivalent of this operator have type (T, T) -> U?
2233+ pub fn input_types_same ( & self ) -> bool {
2234+ self . all_types_same ( ) || self . is_logical ( ) || self . is_comparison ( ) || self . is_assignment ( )
2235+ }
2236+
2237+ /// Does the rust equivalent of this operator have type (T, T) -> T?
2238+ /// This ignores cases where one argument is a pointer and we translate to `.offset()`.
2239+ pub fn all_types_same ( & self ) -> bool {
2240+ self . is_arithmetic ( ) || self . is_bitwise ( )
2241+ }
21892242}
21902243
21912244impl From < CBinOp > for BinOp {
@@ -2232,34 +2285,6 @@ impl Display for CBinOp {
22322285 }
22332286}
22342287
2235- impl CBinOp {
2236- /// Maps compound assignment operators to operator underlying them, and returns `None` for all
2237- /// other operators.
2238- ///
2239- /// For example, `AssignAdd` maps to `Some(Add)` but `Add` maps to `None`.
2240- pub fn underlying_assignment ( & self ) -> Option < CBinOp > {
2241- use CBinOp :: * ;
2242- Some ( match * self {
2243- AssignAdd => Add ,
2244- AssignSubtract => Subtract ,
2245- AssignMultiply => Multiply ,
2246- AssignDivide => Divide ,
2247- AssignModulus => Modulus ,
2248- AssignBitXor => BitXor ,
2249- AssignShiftLeft => ShiftLeft ,
2250- AssignShiftRight => ShiftRight ,
2251- AssignBitOr => BitOr ,
2252- AssignBitAnd => BitAnd ,
2253- _ => return None ,
2254- } )
2255- }
2256-
2257- /// Determines whether or not this is an assignment op
2258- pub fn is_assignment ( & self ) -> bool {
2259- matches ! ( self , Self :: Assign ) || self . underlying_assignment ( ) . is_some ( )
2260- }
2261- }
2262-
22632288#[ derive( Eq , PartialEq , Debug , Copy , Clone ) ]
22642289pub enum IntBase {
22652290 Dec ,
0 commit comments