Skip to content

Commit 013a890

Browse files
committed
add macros
1 parent ac38c1b commit 013a890

File tree

1 file changed

+83
-71
lines changed

1 file changed

+83
-71
lines changed

Python/codegen.c

Lines changed: 83 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -5359,72 +5359,86 @@ codegen_slice(compiler *c, expr_ty s)
53595359
#define MATCH_VALUE_EXPR(N) \
53605360
((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
53615361

5362-
static bool
5363-
is_unary_or_complex_expr(expr_ty e)
5364-
{
5365-
if (e->kind != UnaryOp_kind) {
5366-
return false;
5367-
}
5368-
if (e->v.UnaryOp.op != USub) {
5369-
return false;
5370-
}
5371-
if (e->v.UnaryOp.operand->kind != Constant_kind) {
5372-
return false;
5373-
}
5374-
PyObject *constant = e->v.UnaryOp.operand->v.Constant.value;
5375-
return PyLong_CheckExact(constant) || PyFloat_CheckExact(constant) || PyComplex_CheckExact(constant);
5376-
}
5362+
#define IS_CONST_EXPR(N) \
5363+
((N)->kind == Constant_kind)
53775364

5378-
static bool
5379-
is_complex_binop_expr(expr_ty e)
5380-
{
5381-
if (e->kind != BinOp_kind) {
5382-
return false;
5383-
}
5384-
if (e->v.BinOp.op != Add && e->v.BinOp.op != Sub) {
5385-
return false;
5386-
}
5387-
if (e->v.BinOp.right->kind != Constant_kind) {
5388-
return false;
5389-
}
5390-
if (e->v.BinOp.left->kind != Constant_kind && e->v.BinOp.left->kind != UnaryOp_kind) {
5391-
return false;
5392-
}
5393-
PyObject *leftconst;
5394-
if (e->v.BinOp.left->kind == UnaryOp_kind) {
5395-
if (e->v.BinOp.left->v.UnaryOp.operand->kind != Constant_kind) {
5396-
return false;
5397-
}
5398-
if (e->v.BinOp.left->v.UnaryOp.op != USub) {
5399-
return false;
5400-
}
5401-
leftconst = e->v.BinOp.left->v.UnaryOp.operand->v.Constant.value;
5402-
}
5403-
else {
5404-
leftconst = e->v.BinOp.left->v.Constant.value;
5405-
}
5406-
PyObject *rightconst = e->v.BinOp.right->v.Constant.value;
5407-
return (PyLong_CheckExact(leftconst) || PyFloat_CheckExact(leftconst)) && PyComplex_CheckExact(rightconst);
5408-
}
5365+
#define CONST_EXPR_VALUE(N) \
5366+
((N)->v.Constant.value)
5367+
5368+
#define IS_COMPLEX_CONST_EXPR(N) \
5369+
(IS_CONST_EXPR(N) && PyComplex_CheckExact(CONST_EXPR_VALUE(N)))
5370+
5371+
#define IS_NUMERIC_CONST_EXPR(N) \
5372+
(IS_CONST_EXPR(N) && (PyLong_CheckExact(CONST_EXPR_VALUE(N)) || PyFloat_CheckExact(CONST_EXPR_VALUE(N))))
5373+
5374+
#define IS_UNARY_EXPR(N) \
5375+
((N)->kind == UnaryOp_kind)
5376+
5377+
#define UNARY_EXPR_OP(N) \
5378+
((N)->v.UnaryOp.op)
5379+
5380+
#define UNARY_EXPR_OPERAND(N) \
5381+
((N)->v.UnaryOp.operand)
5382+
5383+
#define UNARY_EXPR_OPERAND_CONST_VALUE(N) \
5384+
(CONST_EXPR_VALUE(UNARY_EXPR_OPERAND(N)))
5385+
5386+
#define IS_UNARY_SUB_EXPR(N) \
5387+
(IS_UNARY_EXPR(N) && UNARY_EXPR_OP(N) == USub)
5388+
5389+
#define IS_MATCH_NUMERIC_UNARY_CONST_EXPR(N) \
5390+
(IS_UNARY_SUB_EXPR(N) && IS_NUMERIC_CONST_EXPR(UNARY_EXPR_OPERAND(N)))
5391+
5392+
#define IS_MATCH_COMPLEX_UNARY_CONST_EXPR(N) \
5393+
(IS_UNARY_SUB_EXPR(N) && IS_COMPLEX_CONST_EXPR(UNARY_EXPR_OPERAND(N)))
5394+
5395+
#define IS_MATCH_NUMERIC_OR_COMPLEX_UNARY_CONST_EXPR(N) \
5396+
(IS_MATCH_NUMERIC_UNARY_CONST_EXPR(N) || IS_MATCH_COMPLEX_UNARY_CONST_EXPR(N))
5397+
5398+
#define BINARY_EXPR(N) \
5399+
((N)->v.BinOp)
5400+
5401+
#define BINARY_EXPR_OP(N) \
5402+
(BINARY_EXPR(N).op)
5403+
5404+
#define BINARY_EXPR_LEFT(N) \
5405+
(BINARY_EXPR(N).left)
5406+
5407+
#define BINARY_EXPR_RIGHT(N) \
5408+
(BINARY_EXPR(N).right)
5409+
5410+
#define IS_BINARY_EXPR(N) \
5411+
((N)->kind == BinOp_kind)
5412+
5413+
#define IS_BINARY_ADD_EXPR(N) \
5414+
(IS_BINARY_EXPR(N) && BINARY_EXPR_OP(N) == Add)
5415+
5416+
#define IS_BINARY_SUB_EXPR(N) \
5417+
(IS_BINARY_EXPR(N) && BINARY_EXPR_OP(N) == Sub)
5418+
5419+
#define IS_MATCH_COMPLEX_BINARY_CONST_EXPR(N) \
5420+
( \
5421+
(IS_BINARY_ADD_EXPR(N) || IS_BINARY_SUB_EXPR(N)) \
5422+
&& (IS_MATCH_NUMERIC_UNARY_CONST_EXPR(BINARY_EXPR_LEFT(N)) || IS_CONST_EXPR(BINARY_EXPR_LEFT(N))) \
5423+
&& IS_COMPLEX_CONST_EXPR(BINARY_EXPR_RIGHT(N)) \
5424+
)
54095425

54105426
static void
54115427
fold_node(expr_ty node, PyObject *folded)
54125428
{
5413-
assert(node->kind != Constant_kind);
5429+
assert(!IS_CONST_EXPR(node));
54145430
node->kind = Constant_kind;
54155431
node->v.Constant.kind = NULL;
54165432
node->v.Constant.value = folded;
54175433
}
54185434

54195435
static int
5420-
fold_unary_or_complex_expr(expr_ty e)
5436+
fold_const_unary_or_complex_expr(expr_ty e)
54215437
{
5422-
assert(e->kind == UnaryOp_kind);
5423-
assert(e->v.UnaryOp.op == USub);
5424-
assert(e->v.UnaryOp.operand->kind == Constant_kind);
5425-
PyObject *operand = e->v.UnaryOp.operand->v.Constant.value;
5426-
assert(PyLong_CheckExact(operand) || PyFloat_CheckExact(operand) || PyComplex_CheckExact(operand));
5427-
PyObject* folded = PyNumber_Negative(operand);
5438+
assert(IS_MATCH_NUMERIC_OR_COMPLEX_UNARY_CONST_EXPR(e));
5439+
PyObject *constant = UNARY_EXPR_OPERAND_CONST_VALUE(e);
5440+
assert(UNARY_EXPR_OP(e) == USub);
5441+
PyObject* folded = PyNumber_Negative(constant);
54285442
if (folded == NULL) {
54295443
return ERROR;
54305444
}
@@ -5433,21 +5447,19 @@ fold_unary_or_complex_expr(expr_ty e)
54335447
}
54345448

54355449
static int
5436-
fold_binary_complex_expr(expr_ty e)
5450+
fold_const_binary_complex_expr(expr_ty e)
54375451
{
5438-
assert(e->kind == BinOp_kind);
5439-
assert(e->v.BinOp.right->kind == Constant_kind);
5440-
assert(e->v.BinOp.left->kind == UnaryOp_kind || e->v.BinOp.left->kind == Constant_kind);
5441-
if (e->v.BinOp.left->kind == UnaryOp_kind) {
5442-
RETURN_IF_ERROR(fold_unary_or_complex_expr(e->v.BinOp.left));
5452+
assert(IS_MATCH_COMPLEX_BINARY_CONST_EXPR(e));
5453+
expr_ty left_expr = BINARY_EXPR_LEFT(e);
5454+
if (IS_UNARY_EXPR(left_expr)) {
5455+
assert(IS_MATCH_NUMERIC_UNARY_CONST_EXPR(left_expr));
5456+
RETURN_IF_ERROR(fold_const_unary_or_complex_expr(left_expr));
54435457
}
5444-
assert(e->v.BinOp.left->kind == Constant_kind);
5445-
operator_ty op = e->v.BinOp.op;
5446-
PyObject *left = e->v.BinOp.left->v.Constant.value;
5447-
PyObject *right = e->v.BinOp.right->v.Constant.value;
5458+
assert(IS_CONST_EXPR(BINARY_EXPR_LEFT(e)));
5459+
operator_ty op = BINARY_EXPR_OP(e);
5460+
PyObject *left = CONST_EXPR_VALUE(BINARY_EXPR_LEFT(e));
5461+
PyObject *right = CONST_EXPR_VALUE(BINARY_EXPR_RIGHT(e));
54485462
assert(op == Add || op == Sub);
5449-
assert(PyLong_CheckExact(left) || PyFloat_CheckExact(left));
5450-
assert(PyComplex_CheckExact(right));
54515463
PyObject *folded = op == Add ? PyNumber_Add(left, right) : PyNumber_Subtract(left, right);
54525464
if (folded == NULL) {
54535465
return ERROR;
@@ -5457,13 +5469,13 @@ fold_binary_complex_expr(expr_ty e)
54575469
}
54585470

54595471
static int
5460-
try_fold_unary_or_binary_complex_const_expr(expr_ty key)
5472+
try_fold_unary_or_binary_complex_const_expr(expr_ty e)
54615473
{
5462-
if (is_unary_or_complex_expr(key)) {
5463-
return fold_unary_or_complex_expr(key);
5474+
if (IS_MATCH_NUMERIC_OR_COMPLEX_UNARY_CONST_EXPR(e)) {
5475+
return fold_const_unary_or_complex_expr(e);
54645476
}
5465-
if (is_complex_binop_expr(key)) {
5466-
return fold_binary_complex_expr(key);
5477+
if (IS_MATCH_COMPLEX_BINARY_CONST_EXPR(e)) {
5478+
return fold_const_binary_complex_expr(e);
54675479
}
54685480
return SUCCESS;
54695481
}

0 commit comments

Comments
 (0)