@@ -726,98 +726,82 @@ astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state)
726726    return  1 ;
727727}
728728
729- #define  IS_CONST_EXPR (N ) \
730-     ((N)->kind == Constant_kind)
731- 
732- #define  CONST_EXPR_VALUE (N ) \
733-     ((N)->v.Constant.value)
734- 
735- #define  IS_CONST_COMPLEX_EXPR (N ) \
736-     (IS_CONST_EXPR(N) && PyComplex_CheckExact(CONST_EXPR_VALUE(N)))
737- 
738- #define  IS_NUMERIC_CONST_EXPR (N ) \
739-     (IS_CONST_EXPR(N) && (PyLong_CheckExact(CONST_EXPR_VALUE(N)) || PyFloat_CheckExact(CONST_EXPR_VALUE(N))))
740- 
741- #define  IS_UNARY_EXPR (N ) \
742-     ((N)->kind == UnaryOp_kind)
743- 
744- #define  UNARY_EXPR_OP (N ) \
745-     ((N)->v.UnaryOp.op)
746- 
747- #define  UNARY_EXPR_OPERAND (N ) \
748-     ((N)->v.UnaryOp.operand)
749- 
750- #define  UNARY_EXPR_OPERAND_CONST_VALUE (N ) \
751-     (CONST_EXPR_VALUE(UNARY_EXPR_OPERAND(N)))
752- 
753- #define  IS_UNARY_SUB_EXPR (N ) \
754-     (IS_UNARY_EXPR(N) && UNARY_EXPR_OP(N) == USub)
755- 
756- #define  IS_MATCH_NUMERIC_UNARY_CONST_EXPR (N ) \
757-     (IS_UNARY_SUB_EXPR(N) && IS_NUMERIC_CONST_EXPR(UNARY_EXPR_OPERAND(N)))
758- 
759- #define  IS_MATCH_COMPLEX_UNARY_CONST_EXPR (N ) \
760-     (IS_UNARY_SUB_EXPR(N) && IS_CONST_COMPLEX_EXPR(UNARY_EXPR_OPERAND(N)))
761- 
762- #define  BINARY_EXPR (N ) \
763-     ((N)->v.BinOp)
764- 
765- #define  BINARY_EXPR_OP (N ) \
766-     (BINARY_EXPR(N).op)
767- 
768- #define  BINARY_EXPR_LEFT (N ) \
769-     (BINARY_EXPR(N).left)
770- 
771- #define  BINARY_EXPR_RIGHT (N ) \
772-     (BINARY_EXPR(N).right)
773- 
774- #define  IS_BINARY_EXPR (N ) \
775-     ((N)->kind == BinOp_kind)
776- 
777- #define  IS_BINARY_ADD_EXPR (N ) \
778-     (IS_BINARY_EXPR(N) && BINARY_EXPR_OP(N) == Add)
779- 
780- #define  IS_BINARY_SUB_EXPR (N ) \
781-     (IS_BINARY_EXPR(N) && BINARY_EXPR_OP(N) == Sub)
782- 
783- #define  IS_MATCH_NUMERIC_OR_COMPLEX_UNARY_CONST_EXPR (N ) \
784-     (IS_MATCH_NUMERIC_UNARY_CONST_EXPR(N) || IS_MATCH_COMPLEX_UNARY_CONST_EXPR(N))
729+ static  bool 
730+ is_unarynegative_const_numeric_expr (expr_ty  node )
731+ {
732+     return  (
733+         node -> kind  ==  UnaryOp_kind 
734+         &&  node -> v .UnaryOp .op  ==  USub 
735+         &&  node -> v .UnaryOp .operand -> kind  ==  Constant_kind 
736+         &&  (
737+             PyLong_CheckExact (node -> v .UnaryOp .operand -> v .Constant .value )
738+             ||  PyFloat_CheckExact (node -> v .UnaryOp .operand -> v .Constant .value )
739+         )
740+     );
741+ }
785742
786- #define  IS_MATCH_COMPLEX_BINARY_CONST_EXPR (N ) \
787-     ( \
788-         (IS_BINARY_ADD_EXPR(N) || IS_BINARY_SUB_EXPR(N)) \
789-         && (IS_MATCH_NUMERIC_UNARY_CONST_EXPR(BINARY_EXPR_LEFT(N)) || IS_NUMERIC_CONST_EXPR(BINARY_EXPR_LEFT(N))) \
790-         && IS_CONST_COMPLEX_EXPR(BINARY_EXPR_RIGHT(N)) \
791-     )
743+ static  bool 
744+ is_unarynegative_const_complex_expr (expr_ty  node )
745+ {
746+     return  (
747+         node -> kind  ==  UnaryOp_kind 
748+         &&  node -> v .UnaryOp .op  ==  USub 
749+         &&  node -> v .UnaryOp .operand -> kind  ==  Constant_kind 
750+         &&  PyComplex_CheckExact (node -> v .UnaryOp .operand -> v .Constant .value )
751+     );
752+ }
792753
754+ static  bool 
755+ is_allowed_match_case_binary_expr (expr_ty  node )
756+ {
757+     return  (
758+         node -> kind  ==  BinOp_kind 
759+         &&  (node -> v .BinOp .op  ==  Add  ||  node -> v .BinOp .op  ==  Sub )
760+         &&  node -> v .BinOp .right -> kind  ==  Constant_kind 
761+         &&  PyComplex_CheckExact (node -> v .BinOp .right -> v .Constant .value )
762+         &&  (
763+             is_unarynegative_const_numeric_expr (node -> v .BinOp .left )
764+             ||  (
765+                 node -> v .BinOp .left -> kind  ==  Constant_kind 
766+                 &&  (
767+                     PyLong_CheckExact (node -> v .BinOp .left -> v .Constant .value )
768+                     ||  PyFloat_CheckExact (node -> v .BinOp .left -> v .Constant .value )
769+                 )
770+             )
771+         )
772+     );
773+ }
793774
794775static  int 
795776fold_const_unary_or_complex_expr (expr_ty  node , PyArena  * ctx_ ,
796777                                 _PyASTOptimizeState  *  Py_UNUSED (state ))
797778{
798-     assert (IS_MATCH_NUMERIC_OR_COMPLEX_UNARY_CONST_EXPR (node ));
799-     PyObject  * constant  =  UNARY_EXPR_OPERAND_CONST_VALUE (node );
800-     assert (UNARY_EXPR_OP (node ) ==  USub );
779+     assert (
780+         is_unarynegative_const_numeric_expr (node )
781+         ||  is_unarynegative_const_complex_expr (node )
782+     );
783+     PyObject  * constant  =  node -> v .UnaryOp .operand -> v .Constant .value ;
801784    assert (constant  !=  NULL );
785+     assert (node -> v .UnaryOp .op  ==  USub );
802786    PyObject *  folded  =  PyNumber_Negative (constant );
803787    return  make_const (node , folded , ctx_ );
804788}
805789
806790static  int 
807791fold_const_binary_complex_expr (expr_ty  node , PyArena  * ctx_ , _PyASTOptimizeState  * state )
808792{
809-     assert (IS_MATCH_COMPLEX_BINARY_CONST_EXPR (node ));
810-     expr_ty  left_expr  =  BINARY_EXPR_LEFT ( node ) ;
811-     if  (IS_MATCH_NUMERIC_UNARY_CONST_EXPR (left_expr )) {
793+     assert (is_allowed_match_case_binary_expr (node ));
794+     expr_ty  left_expr  =  node -> v . BinOp . left ;
795+     if  (is_unarynegative_const_numeric_expr (left_expr )) {
812796        CALL (fold_const_unary_or_complex_expr , expr_ty , left_expr );
813797    }
814-     /* must have folded if left was IS_MATCH_NUMERIC_UNARY_CONST_EXPR */ 
815-     assert (IS_CONST_EXPR (BINARY_EXPR_LEFT (node )));
816-     operator_ty  op  =  BINARY_EXPR_OP (node );
817-     PyObject  * left  =  CONST_EXPR_VALUE (BINARY_EXPR_LEFT (node ));
818-     PyObject  * right  =  CONST_EXPR_VALUE (BINARY_EXPR_RIGHT (node ));
819-     assert (op  ==  Add  ||  op  ==  Sub );
798+     /* must have folded left expr by now */ 
799+     assert (left_expr -> kind  ==  Constant_kind );
800+     operator_ty  op  =  node -> v .BinOp .op ;
801+     PyObject  * left  =  left_expr -> v .Constant .value ;
802+     PyObject  * right  =  node -> v .BinOp .right -> v .Constant .value ;
820803    assert (left  !=  NULL  &&  right  !=  NULL );
804+     assert (op  ==  Add  ||  op  ==  Sub );
821805    PyObject  * folded  =  op  ==  Add  ? PyNumber_Add (left , right ) : PyNumber_Subtract (left , right );
822806    return  make_const (node , folded , ctx_ );
823807}
0 commit comments