diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2020-03-10 16:52:34 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-10 16:52:34 (GMT) |
commit | 13d52c268699f199a8e917a0f1dc4c51e5346c42 (patch) | |
tree | d602c97d77e3222d38c6300ed822021e51bd9dce /Python/ast_opt.c | |
parent | e5e56328afac50aad6d8893185d8e7ba8928afe2 (diff) | |
download | cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.zip cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.tar.gz cpython-13d52c268699f199a8e917a0f1dc4c51e5346c42.tar.bz2 |
bpo-34822: Simplify AST for subscription. (GH-9605)
* Remove the slice type.
* Make Slice a kind of the expr type instead of the slice type.
* Replace ExtSlice(slices) with Tuple(slices, Load()).
* Replace Index(value) with a value itself.
All non-terminal nodes in AST for expressions are now of the expr type.
Diffstat (limited to 'Python/ast_opt.c')
-rw-r--r-- | Python/ast_opt.c | 37 |
1 files changed, 8 insertions, 29 deletions
diff --git a/Python/ast_opt.c b/Python/ast_opt.c index 39e164a..7a2b6e6 100644 --- a/Python/ast_opt.c +++ b/Python/ast_opt.c @@ -310,20 +310,16 @@ fold_subscr(expr_ty node, PyArena *arena, int optimize) { PyObject *newval; expr_ty arg, idx; - slice_ty slice; arg = node->v.Subscript.value; - slice = node->v.Subscript.slice; + idx = node->v.Subscript.slice; if (node->v.Subscript.ctx != Load || arg->kind != Constant_kind || - /* TODO: handle other types of slices */ - slice->kind != Index_kind || - slice->v.Index.value->kind != Constant_kind) + idx->kind != Constant_kind) { return 1; } - idx = slice->v.Index.value; newval = PyObject_GetItem(arg->v.Constant.value, idx->v.Constant.value); return make_const(node, newval, arena); } @@ -395,7 +391,6 @@ 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_); @@ -548,12 +543,17 @@ astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_) break; case Subscript_kind: CALL(astfold_expr, expr_ty, node_->v.Subscript.value); - CALL(astfold_slice, slice_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 List_kind: CALL_SEQ(astfold_expr, expr_ty, node_->v.List.elts); break; @@ -573,27 +573,6 @@ astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_) } static int -astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_) -{ - switch (node_->kind) { - 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 ExtSlice_kind: - CALL_SEQ(astfold_slice, slice_ty, node_->v.ExtSlice.dims); - break; - case Index_kind: - CALL(astfold_expr, expr_ty, node_->v.Index.value); - break; - default: - break; - } - return 1; -} - -static int astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_) { CALL(astfold_expr, expr_ty, node_->value); |