}
static int
-fold_unaryop(expr_ty node, PyArena *arena)
+fold_unaryop(expr_ty node, PyArena *arena, int optimize)
{
expr_ty arg = node->v.UnaryOp.operand;
}
static int
-fold_binop(expr_ty node, PyArena *arena)
+fold_binop(expr_ty node, PyArena *arena, int optimize)
{
expr_ty lhs, rhs;
lhs = node->v.BinOp.left;
}
static int
-fold_tuple(expr_ty node, PyArena *arena)
+fold_tuple(expr_ty node, PyArena *arena, int optimize)
{
PyObject *newval;
}
static int
-fold_subscr(expr_ty node, PyArena *arena)
+fold_subscr(expr_ty node, PyArena *arena, int optimize)
{
PyObject *newval;
expr_ty arg, idx;
in "for" loop and comprehensions.
*/
static int
-fold_iter(expr_ty arg, PyArena *arena)
+fold_iter(expr_ty arg, PyArena *arena, int optimize)
{
PyObject *newval;
if (arg->kind == List_kind) {
}
static int
-fold_compare(expr_ty node, PyArena *arena)
+fold_compare(expr_ty node, PyArena *arena, int optimize)
{
asdl_int_seq *ops;
asdl_seq *args;
i = asdl_seq_LEN(ops) - 1;
int op = asdl_seq_GET(ops, i);
if (op == In || op == NotIn) {
- if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena)) {
+ if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena, optimize)) {
return 0;
}
}
return 1;
}
-static int astfold_mod(mod_ty node_, PyArena* ctx_);
-static int astfold_stmt(stmt_ty node_, PyArena* ctx_);
-static int astfold_expr(expr_ty node_, PyArena* ctx_);
-static int astfold_arguments(arguments_ty node_, PyArena* ctx_);
-static int astfold_comprehension(comprehension_ty node_, PyArena* ctx_);
-static int astfold_keyword(keyword_ty node_, PyArena* ctx_);
-static int astfold_slice(slice_ty node_, PyArena* ctx_);
-static int astfold_arg(arg_ty node_, PyArena* ctx_);
-static int astfold_withitem(withitem_ty node_, PyArena* ctx_);
-static int astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_);
+static int astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_arg(arg_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_);
+static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_);
#define CALL(FUNC, TYPE, ARG) \
- if (!FUNC((ARG), ctx_)) \
+ if (!FUNC((ARG), ctx_, optimize_)) \
return 0;
#define CALL_OPT(FUNC, TYPE, ARG) \
- if ((ARG) != NULL && !FUNC((ARG), ctx_)) \
+ if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_)) \
return 0;
#define CALL_SEQ(FUNC, TYPE, ARG) { \
asdl_seq *seq = (ARG); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
- if (elt != NULL && !FUNC(elt, ctx_)) \
+ if (elt != NULL && !FUNC(elt, ctx_, optimize_)) \
return 0; \
} \
}
asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
- if (!FUNC(elt, ctx_)) \
+ if (!FUNC(elt, ctx_, optimize_)) \
return 0; \
} \
}
static int
-astfold_mod(mod_ty node_, PyArena* ctx_)
+astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case Module_kind:
}
static int
-astfold_expr(expr_ty node_, PyArena* ctx_)
+astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case BoolOp_kind:
CALL_SEQ(astfold_expr, expr_ty, node_->v.Tuple.elts);
CALL(fold_tuple, expr_ty, node_);
break;
+ case Name_kind:
+ if (_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
+ return make_const(node_, PyBool_FromLong(!optimize_), ctx_);
+ }
+ break;
default:
break;
}
}
static int
-astfold_slice(slice_ty node_, PyArena* ctx_)
+astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case Slice_kind:
}
static int
-astfold_keyword(keyword_ty node_, PyArena* ctx_)
+astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_)
{
CALL(astfold_expr, expr_ty, node_->value);
return 1;
}
static int
-astfold_comprehension(comprehension_ty node_, PyArena* ctx_)
+astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
{
CALL(astfold_expr, expr_ty, node_->target);
CALL(astfold_expr, expr_ty, node_->iter);
}
static int
-astfold_arguments(arguments_ty node_, PyArena* ctx_)
+astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
{
CALL_SEQ(astfold_arg, arg_ty, node_->args);
CALL_OPT(astfold_arg, arg_ty, node_->vararg);
}
static int
-astfold_arg(arg_ty node_, PyArena* ctx_)
+astfold_arg(arg_ty node_, PyArena *ctx_, int optimize_)
{
CALL_OPT(astfold_expr, expr_ty, node_->annotation);
return 1;
}
static int
-astfold_stmt(stmt_ty node_, PyArena* ctx_)
+astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case FunctionDef_kind:
}
static int
-astfold_excepthandler(excepthandler_ty node_, PyArena* ctx_)
+astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_)
{
switch (node_->kind) {
case ExceptHandler_kind:
}
static int
-astfold_withitem(withitem_ty node_, PyArena* ctx_)
+astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_)
{
CALL(astfold_expr, expr_ty, node_->context_expr);
CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
#undef CALL_INT_SEQ
int
-_PyAST_Optimize(mod_ty mod, PyArena *arena)
+_PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize)
{
- int ret = astfold_mod(mod, arena);
+ int ret = astfold_mod(mod, arena, optimize);
assert(ret || PyErr_Occurred());
return ret;
}
static int compiler_in_loop(struct compiler *);
static int inplace_binop(struct compiler *, operator_ty);
-static int expr_constant(struct compiler *, expr_ty);
+static int expr_constant(expr_ty);
static int compiler_with(struct compiler *, stmt_ty, int);
static int compiler_async_with(struct compiler *, stmt_ty, int);
c.c_optimize = (optimize == -1) ? Py_OptimizeFlag : optimize;
c.c_nestlevel = 0;
- if (!_PyAST_Optimize(mod, arena)) {
+ if (!_PyAST_Optimize(mod, arena, c.c_optimize)) {
goto finally;
}
case Ellipsis_kind:
case NameConstant_kind:
return 1;
- case Name_kind:
- return _PyUnicode_EqualToASCIIString(e->v.Name.id, "__debug__");
default:
return 0;
}
}
static PyObject *
-get_const_value(struct compiler *c, expr_ty e)
+get_const_value(expr_ty e)
{
switch (e->kind) {
case Constant_kind:
return Py_Ellipsis;
case NameConstant_kind:
return e->v.NameConstant.value;
- case Name_kind:
- assert(_PyUnicode_EqualToASCIIString(e->v.Name.id, "__debug__"));
- return c->c_optimize ? Py_False : Py_True;
default:
Py_UNREACHABLE();
}
if (end == NULL)
return 0;
- constant = expr_constant(c, s->v.If.test);
+ constant = expr_constant(s->v.If.test);
/* constant = 0: "if 0"
* constant = 1: "if 1", "if 2", ...
* constant = -1: rest */
compiler_while(struct compiler *c, stmt_ty s)
{
basicblock *loop, *orelse, *end, *anchor = NULL;
- int constant = expr_constant(c, s->v.While.test);
+ int constant = expr_constant(s->v.While.test);
if (constant == 0) {
if (s->v.While.orelse)
!_PyUnicode_EqualToASCIIString(name, "True") &&
!_PyUnicode_EqualToASCIIString(name, "False"));
- if (ctx == Load && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
- ADDOP_O(c, LOAD_CONST, c->c_optimize ? Py_False : Py_True, consts);
- return 1;
- }
-
mangled = _Py_Mangle(c->u->u_private, name);
if (!mangled)
return 0;
return 0;
}
for (i = begin; i < end; i++) {
- key = get_const_value(c, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
+ key = get_const_value((expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
Py_INCREF(key);
PyTuple_SET_ITEM(keys, i - begin, key);
}
*/
static int
-expr_constant(struct compiler *c, expr_ty e)
+expr_constant(expr_ty e)
{
if (is_const(e)) {
- return PyObject_IsTrue(get_const_value(c, e));
+ return PyObject_IsTrue(get_const_value(e));
}
return -1;
}