summaryrefslogtreecommitdiff
path: root/Python/ast_opt.c
diff options
context:
space:
mode:
Diffstat (limited to 'Python/ast_opt.c')
-rw-r--r--Python/ast_opt.c61
1 files changed, 10 insertions, 51 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c
index 5e57638e3d..1f9cb773ea 100644
--- a/Python/ast_opt.c
+++ b/Python/ast_opt.c
@@ -5,47 +5,6 @@
#include "ast.h"
-/* TODO: is_const and get_const_value are copied from Python/compile.c.
- It should be deduped in the future. Maybe, we can include this file
- from compile.c?
-*/
-static int
-is_const(expr_ty e)
-{
- switch (e->kind) {
- case Constant_kind:
- case Num_kind:
- case Str_kind:
- case Bytes_kind:
- case Ellipsis_kind:
- case NameConstant_kind:
- return 1;
- default:
- return 0;
- }
-}
-
-static PyObject *
-get_const_value(expr_ty e)
-{
- switch (e->kind) {
- case Constant_kind:
- return e->v.Constant.value;
- case Num_kind:
- return e->v.Num.n;
- case Str_kind:
- return e->v.Str.s;
- case Bytes_kind:
- return e->v.Bytes.s;
- case Ellipsis_kind:
- return Py_Ellipsis;
- case NameConstant_kind:
- return e->v.NameConstant.value;
- default:
- Py_UNREACHABLE();
- }
-}
-
static int
make_const(expr_ty node, PyObject *val, PyArena *arena)
{
@@ -81,7 +40,7 @@ fold_unaryop(expr_ty node, PyArena *arena, int optimize)
{
expr_ty arg = node->v.UnaryOp.operand;
- if (!is_const(arg)) {
+ if (arg->kind != Constant_kind) {
/* Fold not into comparison */
if (node->v.UnaryOp.op == Not && arg->kind == Compare_kind &&
asdl_seq_LEN(arg->v.Compare.ops) == 1) {
@@ -123,7 +82,7 @@ fold_unaryop(expr_ty node, PyArena *arena, int optimize)
[UAdd] = PyNumber_Positive,
[USub] = PyNumber_Negative,
};
- PyObject *newval = ops[node->v.UnaryOp.op](get_const_value(arg));
+ PyObject *newval = ops[node->v.UnaryOp.op](arg->v.Constant.value);
return make_const(node, newval, arena);
}
@@ -259,12 +218,12 @@ fold_binop(expr_ty node, PyArena *arena, int optimize)
expr_ty lhs, rhs;
lhs = node->v.BinOp.left;
rhs = node->v.BinOp.right;
- if (!is_const(lhs) || !is_const(rhs)) {
+ if (lhs->kind != Constant_kind || rhs->kind != Constant_kind) {
return 1;
}
- PyObject *lv = get_const_value(lhs);
- PyObject *rv = get_const_value(rhs);
+ PyObject *lv = lhs->v.Constant.value;
+ PyObject *rv = rhs->v.Constant.value;
PyObject *newval;
switch (node->v.BinOp.op) {
@@ -316,7 +275,7 @@ make_const_tuple(asdl_seq *elts)
{
for (int i = 0; i < asdl_seq_LEN(elts); i++) {
expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
- if (!is_const(e)) {
+ if (e->kind != Constant_kind) {
return NULL;
}
}
@@ -328,7 +287,7 @@ make_const_tuple(asdl_seq *elts)
for (int i = 0; i < asdl_seq_LEN(elts); i++) {
expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
- PyObject *v = get_const_value(e);
+ PyObject *v = e->v.Constant.value;
Py_INCREF(v);
PyTuple_SET_ITEM(newval, i, v);
}
@@ -357,16 +316,16 @@ fold_subscr(expr_ty node, PyArena *arena, int optimize)
arg = node->v.Subscript.value;
slice = node->v.Subscript.slice;
if (node->v.Subscript.ctx != Load ||
- !is_const(arg) ||
+ arg->kind != Constant_kind ||
/* TODO: handle other types of slices */
slice->kind != Index_kind ||
- !is_const(slice->v.Index.value))
+ slice->v.Index.value->kind != Constant_kind)
{
return 1;
}
idx = slice->v.Index.value;
- newval = PyObject_GetItem(get_const_value(arg), get_const_value(idx));
+ newval = PyObject_GetItem(arg->v.Constant.value, idx->v.Constant.value);
return make_const(node, newval, arena);
}