diff options
| author | shadchin <[email protected]> | 2022-02-10 16:44:39 +0300 |
|---|---|---|
| committer | Daniil Cherednik <[email protected]> | 2022-02-10 16:44:39 +0300 |
| commit | e9656aae26e0358d5378e5b63dcac5c8dbe0e4d0 (patch) | |
| tree | 64175d5cadab313b3e7039ebaa06c5bc3295e274 /contrib/tools/python3/src/Python/ast_opt.c | |
| parent | 2598ef1d0aee359b4b6d5fdd1758916d5907d04f (diff) | |
Restoring authorship annotation for <[email protected]>. Commit 2 of 2.
Diffstat (limited to 'contrib/tools/python3/src/Python/ast_opt.c')
| -rw-r--r-- | contrib/tools/python3/src/Python/ast_opt.c | 178 |
1 files changed, 89 insertions, 89 deletions
diff --git a/contrib/tools/python3/src/Python/ast_opt.c b/contrib/tools/python3/src/Python/ast_opt.c index 36e1d64f2e8..ff786d6f8d6 100644 --- a/contrib/tools/python3/src/Python/ast_opt.c +++ b/contrib/tools/python3/src/Python/ast_opt.c @@ -1,7 +1,7 @@ /* AST Optimizer */ #include "Python.h" #include "Python-ast.h" -#include "ast.h" +#include "ast.h" static int @@ -19,7 +19,7 @@ make_const(expr_ty node, PyObject *val, PyArena *arena) return 0; } node->kind = Constant_kind; - node->v.Constant.kind = NULL; + node->v.Constant.kind = NULL; node->v.Constant.value = val; return 1; } @@ -36,11 +36,11 @@ unary_not(PyObject *v) } static int -fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) +fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) { expr_ty arg = node->v.UnaryOp.operand; - if (arg->kind != Constant_kind) { + 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) { @@ -82,7 +82,7 @@ fold_unaryop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) [UAdd] = PyNumber_Positive, [USub] = PyNumber_Negative, }; - PyObject *newval = ops[node->v.UnaryOp.op](arg->v.Constant.value); + PyObject *newval = ops[node->v.UnaryOp.op](arg->v.Constant.value); return make_const(node, newval, arena); } @@ -213,17 +213,17 @@ safe_mod(PyObject *v, PyObject *w) } static int -fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) +fold_binop(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) { expr_ty lhs, rhs; lhs = node->v.BinOp.left; rhs = node->v.BinOp.right; - if (lhs->kind != Constant_kind || rhs->kind != Constant_kind) { + if (lhs->kind != Constant_kind || rhs->kind != Constant_kind) { return 1; } - PyObject *lv = lhs->v.Constant.value; - PyObject *rv = rhs->v.Constant.value; + PyObject *lv = lhs->v.Constant.value; + PyObject *rv = rhs->v.Constant.value; PyObject *newval; switch (node->v.BinOp.op) { @@ -275,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 (e->kind != Constant_kind) { + if (e->kind != Constant_kind) { return NULL; } } @@ -287,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 = e->v.Constant.value; + PyObject *v = e->v.Constant.value; Py_INCREF(v); PyTuple_SET_ITEM(newval, i, v); } @@ -295,7 +295,7 @@ make_const_tuple(asdl_seq *elts) } static int -fold_tuple(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) +fold_tuple(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) { PyObject *newval; @@ -307,50 +307,50 @@ fold_tuple(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) } static int -fold_subscr(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) +fold_subscr(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) { PyObject *newval; expr_ty arg, idx; arg = node->v.Subscript.value; - idx = node->v.Subscript.slice; + idx = node->v.Subscript.slice; if (node->v.Subscript.ctx != Load || - arg->kind != Constant_kind || - idx->kind != Constant_kind) + arg->kind != Constant_kind || + idx->kind != Constant_kind) { return 1; } - newval = PyObject_GetItem(arg->v.Constant.value, idx->v.Constant.value); + newval = PyObject_GetItem(arg->v.Constant.value, idx->v.Constant.value); return make_const(node, newval, arena); } /* Change literal list or set of constants into constant - tuple or frozenset respectively. Change literal list of - non-constants into tuple. + tuple or frozenset respectively. Change literal list of + non-constants into tuple. Used for right operand of "in" and "not in" tests and for iterable in "for" loop and comprehensions. */ static int -fold_iter(expr_ty arg, PyArena *arena, _PyASTOptimizeState *state) +fold_iter(expr_ty arg, PyArena *arena, _PyASTOptimizeState *state) { PyObject *newval; if (arg->kind == List_kind) { - /* First change a list into tuple. */ - asdl_seq *elts = arg->v.List.elts; - Py_ssize_t n = asdl_seq_LEN(elts); - for (Py_ssize_t i = 0; i < n; i++) { - expr_ty e = (expr_ty)asdl_seq_GET(elts, i); - if (e->kind == Starred_kind) { - return 1; - } - } - expr_context_ty ctx = arg->v.List.ctx; - arg->kind = Tuple_kind; - arg->v.Tuple.elts = elts; - arg->v.Tuple.ctx = ctx; - /* Try to create a constant tuple. */ - newval = make_const_tuple(elts); + /* First change a list into tuple. */ + asdl_seq *elts = arg->v.List.elts; + Py_ssize_t n = asdl_seq_LEN(elts); + for (Py_ssize_t i = 0; i < n; i++) { + expr_ty e = (expr_ty)asdl_seq_GET(elts, i); + if (e->kind == Starred_kind) { + return 1; + } + } + expr_context_ty ctx = arg->v.List.ctx; + arg->kind = Tuple_kind; + arg->v.Tuple.elts = elts; + arg->v.Tuple.ctx = ctx; + /* Try to create a constant tuple. */ + newval = make_const_tuple(elts); } else if (arg->kind == Set_kind) { newval = make_const_tuple(arg->v.Set.elts); @@ -365,7 +365,7 @@ fold_iter(expr_ty arg, PyArena *arena, _PyASTOptimizeState *state) } static int -fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) +fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) { asdl_int_seq *ops; asdl_seq *args; @@ -379,28 +379,28 @@ fold_compare(expr_ty node, PyArena *arena, _PyASTOptimizeState *state) 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, state)) { + if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena, state)) { return 0; } } return 1; } -static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); -static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); +static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state); #define CALL(FUNC, TYPE, ARG) \ - if (!FUNC((ARG), ctx_, state)) \ + if (!FUNC((ARG), ctx_, state)) \ return 0; #define CALL_OPT(FUNC, TYPE, ARG) \ - if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \ + if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \ return 0; #define CALL_SEQ(FUNC, TYPE, ARG) { \ @@ -408,7 +408,7 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOp 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_, state)) \ + if (elt != NULL && !FUNC(elt, ctx_, state)) \ return 0; \ } \ } @@ -418,25 +418,25 @@ static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOp 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_, state)) \ + if (!FUNC(elt, ctx_, state)) \ return 0; \ } \ } static int -astfold_body(asdl_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_body(asdl_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state) { - int docstring = _PyAST_GetDocString(stmts) != NULL; + int docstring = _PyAST_GetDocString(stmts) != NULL; CALL_SEQ(astfold_stmt, stmt_ty, stmts); - if (!docstring && _PyAST_GetDocString(stmts) != NULL) { - stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0); + if (!docstring && _PyAST_GetDocString(stmts) != NULL) { + stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0); asdl_seq *values = _Py_asdl_seq_new(1, ctx_); if (!values) { return 0; } asdl_seq_SET(values, 0, st->v.Expr.value); - expr_ty expr = JoinedStr(values, st->lineno, st->col_offset, - st->end_lineno, st->end_col_offset, ctx_); + expr_ty expr = JoinedStr(values, st->lineno, st->col_offset, + st->end_lineno, st->end_col_offset, ctx_); if (!expr) { return 0; } @@ -446,7 +446,7 @@ astfold_body(asdl_seq *stmts, PyArena *ctx_, _PyASTOptimizeState *state) } static int -astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { switch (node_->kind) { case Module_kind: @@ -465,7 +465,7 @@ astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) } static int -astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { switch (node_->kind) { case BoolOp_kind: @@ -544,17 +544,17 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) break; case Subscript_kind: CALL(astfold_expr, expr_ty, node_->v.Subscript.value); - CALL(astfold_expr, expr_ty, node_->v.Subscript.slice); + CALL(astfold_expr, expr_ty, node_->v.Subscript.slice); CALL(fold_subscr, expr_ty, node_); break; case Starred_kind: CALL(astfold_expr, expr_ty, node_->v.Starred.value); break; - case Slice_kind: - CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower); - CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper); - CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step); - break; + case Slice_kind: + CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower); + CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper); + CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step); + break; case List_kind: CALL_SEQ(astfold_expr, expr_ty, node_->v.List.elts); break; @@ -563,9 +563,9 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) CALL(fold_tuple, expr_ty, node_); break; case Name_kind: - if (node_->v.Name.ctx == Load && - _PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) { - return make_const(node_, PyBool_FromLong(!state->optimize), ctx_); + if (node_->v.Name.ctx == Load && + _PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) { + return make_const(node_, PyBool_FromLong(!state->optimize), ctx_); } break; default: @@ -575,14 +575,14 @@ astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) } static int -astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { CALL(astfold_expr, expr_ty, node_->value); return 1; } static int -astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { CALL(astfold_expr, expr_ty, node_->target); CALL(astfold_expr, expr_ty, node_->iter); @@ -593,9 +593,9 @@ astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTOptimizeState } static int -astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { - CALL_SEQ(astfold_arg, arg_ty, node_->posonlyargs); + CALL_SEQ(astfold_arg, arg_ty, node_->posonlyargs); CALL_SEQ(astfold_arg, arg_ty, node_->args); CALL_OPT(astfold_arg, arg_ty, node_->vararg); CALL_SEQ(astfold_arg, arg_ty, node_->kwonlyargs); @@ -606,33 +606,33 @@ astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) } static int -astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { - if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { - CALL_OPT(astfold_expr, expr_ty, node_->annotation); - } + if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { + CALL_OPT(astfold_expr, expr_ty, node_->annotation); + } return 1; } static int -astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { switch (node_->kind) { case FunctionDef_kind: CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args); CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body); CALL_SEQ(astfold_expr, expr_ty, node_->v.FunctionDef.decorator_list); - if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { - CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns); - } + if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { + CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns); + } break; case AsyncFunctionDef_kind: CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args); CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body); CALL_SEQ(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.decorator_list); - if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { - CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns); - } + if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { + CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns); + } break; case ClassDef_kind: CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.bases); @@ -656,9 +656,9 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) break; case AnnAssign_kind: CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target); - if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { - CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation); - } + if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { + CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation); + } CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value); break; case For_kind: @@ -717,7 +717,7 @@ astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) } static int -astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { switch (node_->kind) { case ExceptHandler_kind: @@ -731,7 +731,7 @@ astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTOptimizeState } static int -astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) +astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) { CALL(astfold_expr, expr_ty, node_->context_expr); CALL_OPT(astfold_expr, expr_ty, node_->optional_vars); @@ -744,9 +744,9 @@ astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTOptimizeState *state) #undef CALL_INT_SEQ int -_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state) +_PyAST_Optimize(mod_ty mod, PyArena *arena, _PyASTOptimizeState *state) { - int ret = astfold_mod(mod, arena, state); + int ret = astfold_mod(mod, arena, state); assert(ret || PyErr_Occurred()); return ret; } |
