1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963
|
/* AST pre-processing */
#include "Python.h"
#include "pycore_ast.h" // _PyAST_GetDocString()
#include "pycore_c_array.h" // _Py_CArray_EnsureCapacity()
#include "pycore_format.h" // F_LJUST
#include "pycore_runtime.h" // _Py_STR()
#include "pycore_unicodeobject.h" // _PyUnicode_EqualToASCIIString()
/* See PEP 765 */
typedef struct {
bool in_finally;
bool in_funcdef;
bool in_loop;
} ControlFlowInFinallyContext;
typedef struct {
PyObject *filename;
int optimize;
int ff_features;
int syntax_check_only;
_Py_c_array_t cf_finally; /* context for PEP 765 check */
int cf_finally_used;
} _PyASTPreprocessState;
#define ENTER_RECURSIVE() \
if (Py_EnterRecursiveCall(" during compilation")) { \
return 0; \
}
#define LEAVE_RECURSIVE() Py_LeaveRecursiveCall();
static ControlFlowInFinallyContext*
get_cf_finally_top(_PyASTPreprocessState *state)
{
int idx = state->cf_finally_used;
return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx;
}
static int
push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop)
{
if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) {
return 0;
}
state->cf_finally_used++;
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
ctx->in_finally = finally;
ctx->in_funcdef = funcdef;
ctx->in_loop = loop;
return 1;
}
static void
pop_cf_context(_PyASTPreprocessState *state)
{
assert(state->cf_finally_used > 0);
state->cf_finally_used--;
}
static int
control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state)
{
PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw);
if (msg == NULL) {
return 0;
}
int ret = _PyErr_EmitSyntaxWarning(msg, state->filename, n->lineno,
n->col_offset + 1, n->end_lineno,
n->end_col_offset + 1);
Py_DECREF(msg);
return ret < 0 ? 0 : 1;
}
static int
before_return(_PyASTPreprocessState *state, stmt_ty node_)
{
if (state->cf_finally_used > 0) {
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
if (ctx->in_finally && ! ctx->in_funcdef) {
if (!control_flow_in_finally_warning("return", node_, state)) {
return 0;
}
}
}
return 1;
}
static int
before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw)
{
if (state->cf_finally_used > 0) {
ControlFlowInFinallyContext *ctx = get_cf_finally_top(state);
if (ctx->in_finally && ! ctx->in_loop) {
if (!control_flow_in_finally_warning(kw, node_, state)) {
return 0;
}
}
}
return 1;
}
#define PUSH_CONTEXT(S, N, FINALLY, FUNCDEF, LOOP) \
if (!push_cf_context((S), (N), (FINALLY), (FUNCDEF), (LOOP))) { \
return 0; \
}
#define POP_CONTEXT(S) pop_cf_context(S)
#define BEFORE_FINALLY(S, N) PUSH_CONTEXT((S), (N), true, false, false)
#define AFTER_FINALLY(S) POP_CONTEXT(S)
#define BEFORE_FUNC_BODY(S, N) PUSH_CONTEXT((S), (N), false, true, false)
#define AFTER_FUNC_BODY(S) POP_CONTEXT(S)
#define BEFORE_LOOP_BODY(S, N) PUSH_CONTEXT((S), (N), false, false, true)
#define AFTER_LOOP_BODY(S) POP_CONTEXT(S)
#define BEFORE_RETURN(S, N) \
if (!before_return((S), (N))) { \
return 0; \
}
#define BEFORE_LOOP_EXIT(S, N, KW) \
if (!before_loop_exit((S), (N), (KW))) { \
return 0; \
}
static int
make_const(expr_ty node, PyObject *val, PyArena *arena)
{
// Even if no new value was calculated, make_const may still
// need to clear an error (e.g. for division by zero)
if (val == NULL) {
if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
return 0;
}
PyErr_Clear();
return 1;
}
if (_PyArena_AddPyObject(arena, val) < 0) {
Py_DECREF(val);
return 0;
}
node->kind = Constant_kind;
node->v.Constant.kind = NULL;
node->v.Constant.value = val;
return 1;
}
#define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr)))
static int
has_starred(asdl_expr_seq *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;
}
}
return 0;
}
static expr_ty
parse_literal(PyObject *fmt, Py_ssize_t *ppos, PyArena *arena)
{
const void *data = PyUnicode_DATA(fmt);
int kind = PyUnicode_KIND(fmt);
Py_ssize_t size = PyUnicode_GET_LENGTH(fmt);
Py_ssize_t start, pos;
int has_percents = 0;
start = pos = *ppos;
while (pos < size) {
if (PyUnicode_READ(kind, data, pos) != '%') {
pos++;
}
else if (pos+1 < size && PyUnicode_READ(kind, data, pos+1) == '%') {
has_percents = 1;
pos += 2;
}
else {
break;
}
}
*ppos = pos;
if (pos == start) {
return NULL;
}
PyObject *str = PyUnicode_Substring(fmt, start, pos);
/* str = str.replace('%%', '%') */
if (str && has_percents) {
_Py_DECLARE_STR(dbl_percent, "%%");
Py_SETREF(str, PyUnicode_Replace(str, &_Py_STR(dbl_percent),
_Py_LATIN1_CHR('%'), -1));
}
if (!str) {
return NULL;
}
if (_PyArena_AddPyObject(arena, str) < 0) {
Py_DECREF(str);
return NULL;
}
return _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
}
#define MAXDIGITS 3
static int
simple_format_arg_parse(PyObject *fmt, Py_ssize_t *ppos,
int *spec, int *flags, int *width, int *prec)
{
Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt);
Py_UCS4 ch;
#define NEXTC do { \
if (pos >= len) { \
return 0; \
} \
ch = PyUnicode_READ_CHAR(fmt, pos); \
pos++; \
} while (0)
*flags = 0;
while (1) {
NEXTC;
switch (ch) {
case '-': *flags |= F_LJUST; continue;
case '+': *flags |= F_SIGN; continue;
case ' ': *flags |= F_BLANK; continue;
case '#': *flags |= F_ALT; continue;
case '0': *flags |= F_ZERO; continue;
}
break;
}
if ('0' <= ch && ch <= '9') {
*width = 0;
int digits = 0;
while ('0' <= ch && ch <= '9') {
*width = *width * 10 + (ch - '0');
NEXTC;
if (++digits >= MAXDIGITS) {
return 0;
}
}
}
if (ch == '.') {
NEXTC;
*prec = 0;
if ('0' <= ch && ch <= '9') {
int digits = 0;
while ('0' <= ch && ch <= '9') {
*prec = *prec * 10 + (ch - '0');
NEXTC;
if (++digits >= MAXDIGITS) {
return 0;
}
}
}
}
*spec = ch;
*ppos = pos;
return 1;
#undef NEXTC
}
static expr_ty
parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena)
{
int spec, flags, width = -1, prec = -1;
if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) {
// Unsupported format.
return NULL;
}
if (spec == 's' || spec == 'r' || spec == 'a') {
char buf[1 + MAXDIGITS + 1 + MAXDIGITS + 1], *p = buf;
if (!(flags & F_LJUST) && width > 0) {
*p++ = '>';
}
if (width >= 0) {
p += snprintf(p, MAXDIGITS + 1, "%d", width);
}
if (prec >= 0) {
p += snprintf(p, MAXDIGITS + 2, ".%d", prec);
}
expr_ty format_spec = NULL;
if (p != buf) {
PyObject *str = PyUnicode_FromString(buf);
if (str == NULL) {
return NULL;
}
if (_PyArena_AddPyObject(arena, str) < 0) {
Py_DECREF(str);
return NULL;
}
format_spec = _PyAST_Constant(str, NULL, -1, -1, -1, -1, arena);
if (format_spec == NULL) {
return NULL;
}
}
return _PyAST_FormattedValue(arg, spec, format_spec,
arg->lineno, arg->col_offset,
arg->end_lineno, arg->end_col_offset,
arena);
}
// Unsupported format.
return NULL;
}
static int
optimize_format(expr_ty node, PyObject *fmt, asdl_expr_seq *elts, PyArena *arena)
{
Py_ssize_t pos = 0;
Py_ssize_t cnt = 0;
asdl_expr_seq *seq = _Py_asdl_expr_seq_new(asdl_seq_LEN(elts) * 2 + 1, arena);
if (!seq) {
return 0;
}
seq->size = 0;
while (1) {
expr_ty lit = parse_literal(fmt, &pos, arena);
if (lit) {
asdl_seq_SET(seq, seq->size++, lit);
}
else if (PyErr_Occurred()) {
return 0;
}
if (pos >= PyUnicode_GET_LENGTH(fmt)) {
break;
}
if (cnt >= asdl_seq_LEN(elts)) {
// More format units than items.
return 1;
}
assert(PyUnicode_READ_CHAR(fmt, pos) == '%');
pos++;
expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena);
cnt++;
if (!expr) {
return !PyErr_Occurred();
}
asdl_seq_SET(seq, seq->size++, expr);
}
if (cnt < asdl_seq_LEN(elts)) {
// More items than format units.
return 1;
}
expr_ty res = _PyAST_JoinedStr(seq,
node->lineno, node->col_offset,
node->end_lineno, node->end_col_offset,
arena);
if (!res) {
return 0;
}
COPY_NODE(node, res);
// PySys_FormatStderr("format = %R\n", fmt);
return 1;
}
static int
fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state)
{
if (state->syntax_check_only) {
return 1;
}
expr_ty lhs, rhs;
lhs = node->v.BinOp.left;
rhs = node->v.BinOp.right;
if (lhs->kind != Constant_kind) {
return 1;
}
PyObject *lv = lhs->v.Constant.value;
if (node->v.BinOp.op == Mod &&
rhs->kind == Tuple_kind &&
PyUnicode_Check(lv) &&
!has_starred(rhs->v.Tuple.elts))
{
return optimize_format(node, lv, rhs->v.Tuple.elts, arena);
}
return 1;
}
static int astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
static int astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state);
#define CALL(FUNC, TYPE, ARG) \
if (!FUNC((ARG), ctx_, state)) \
return 0;
#define CALL_OPT(FUNC, TYPE, ARG) \
if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \
return 0;
#define CALL_SEQ(FUNC, TYPE, ARG) { \
Py_ssize_t i; \
asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \
for (i = 0; i < asdl_seq_LEN(seq); i++) { \
TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
if (elt != NULL && !FUNC(elt, ctx_, state)) \
return 0; \
} \
}
static int
stmt_seq_remove_item(asdl_stmt_seq *stmts, Py_ssize_t idx)
{
if (idx >= asdl_seq_LEN(stmts)) {
return 0;
}
for (Py_ssize_t i = idx; i < asdl_seq_LEN(stmts) - 1; i++) {
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, i+1);
asdl_seq_SET(stmts, i, st);
}
stmts->size--;
return 1;
}
static int
astfold_body(asdl_stmt_seq *stmts, PyArena *ctx_, _PyASTPreprocessState *state)
{
int docstring = _PyAST_GetDocString(stmts) != NULL;
if (docstring && (state->optimize >= 2)) {
/* remove the docstring */
if (!stmt_seq_remove_item(stmts, 0)) {
return 0;
}
docstring = 0;
}
CALL_SEQ(astfold_stmt, stmt, stmts);
if (!docstring && _PyAST_GetDocString(stmts) != NULL) {
stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
asdl_expr_seq *values = _Py_asdl_expr_seq_new(1, ctx_);
if (!values) {
return 0;
}
asdl_seq_SET(values, 0, st->v.Expr.value);
expr_ty expr = _PyAST_JoinedStr(values, st->lineno, st->col_offset,
st->end_lineno, st->end_col_offset,
ctx_);
if (!expr) {
return 0;
}
st->v.Expr.value = expr;
}
return 1;
}
static int
astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
switch (node_->kind) {
case Module_kind:
CALL(astfold_body, asdl_seq, node_->v.Module.body);
break;
case Interactive_kind:
CALL_SEQ(astfold_stmt, stmt, node_->v.Interactive.body);
break;
case Expression_kind:
CALL(astfold_expr, expr_ty, node_->v.Expression.body);
break;
// The following top level nodes don't participate in constant folding
case FunctionType_kind:
break;
// No default case, so the compiler will emit a warning if new top level
// compilation nodes are added without being handled here
}
return 1;
}
static int
astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
ENTER_RECURSIVE();
switch (node_->kind) {
case BoolOp_kind:
CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values);
break;
case BinOp_kind:
CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
CALL(astfold_expr, expr_ty, node_->v.BinOp.right);
CALL(fold_binop, expr_ty, node_);
break;
case UnaryOp_kind:
CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand);
break;
case Lambda_kind:
CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
break;
case IfExp_kind:
CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
break;
case Dict_kind:
CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys);
CALL_SEQ(astfold_expr, expr, node_->v.Dict.values);
break;
case Set_kind:
CALL_SEQ(astfold_expr, expr, node_->v.Set.elts);
break;
case ListComp_kind:
CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators);
break;
case SetComp_kind:
CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators);
break;
case DictComp_kind:
CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators);
break;
case GeneratorExp_kind:
CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators);
break;
case Await_kind:
CALL(astfold_expr, expr_ty, node_->v.Await.value);
break;
case Yield_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value);
break;
case YieldFrom_kind:
CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value);
break;
case Compare_kind:
CALL(astfold_expr, expr_ty, node_->v.Compare.left);
CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators);
break;
case Call_kind:
CALL(astfold_expr, expr_ty, node_->v.Call.func);
CALL_SEQ(astfold_expr, expr, node_->v.Call.args);
CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords);
break;
case FormattedValue_kind:
CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
break;
case Interpolation_kind:
CALL(astfold_expr, expr_ty, node_->v.Interpolation.value);
CALL_OPT(astfold_expr, expr_ty, node_->v.Interpolation.format_spec);
break;
case JoinedStr_kind:
CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values);
break;
case TemplateStr_kind:
CALL_SEQ(astfold_expr, expr, node_->v.TemplateStr.values);
break;
case Attribute_kind:
CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
break;
case Subscript_kind:
CALL(astfold_expr, expr_ty, node_->v.Subscript.value);
CALL(astfold_expr, expr_ty, node_->v.Subscript.slice);
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, node_->v.List.elts);
break;
case Tuple_kind:
CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts);
break;
case Name_kind:
if (state->syntax_check_only) {
break;
}
if (node_->v.Name.ctx == Load &&
_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
LEAVE_RECURSIVE();
return make_const(node_, PyBool_FromLong(!state->optimize), ctx_);
}
break;
case NamedExpr_kind:
CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value);
break;
case Constant_kind:
// Already a constant, nothing further to do
break;
// No default case, so the compiler will emit a warning if new expression
// kinds are added without being handled here
}
LEAVE_RECURSIVE();
return 1;
}
static int
astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_expr, expr_ty, node_->value);
return 1;
}
static int
astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_expr, expr_ty, node_->target);
CALL(astfold_expr, expr_ty, node_->iter);
CALL_SEQ(astfold_expr, expr, node_->ifs);
return 1;
}
static int
astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL_SEQ(astfold_arg, arg, node_->posonlyargs);
CALL_SEQ(astfold_arg, arg, node_->args);
CALL_OPT(astfold_arg, arg_ty, node_->vararg);
CALL_SEQ(astfold_arg, arg, node_->kwonlyargs);
CALL_SEQ(astfold_expr, expr, node_->kw_defaults);
CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
CALL_SEQ(astfold_expr, expr, node_->defaults);
return 1;
}
static int
astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
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_, _PyASTPreprocessState *state)
{
ENTER_RECURSIVE();
switch (node_->kind) {
case FunctionDef_kind: {
CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params);
CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
BEFORE_FUNC_BODY(state, node_);
CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body);
AFTER_FUNC_BODY(state);
CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list);
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
}
break;
}
case AsyncFunctionDef_kind: {
CALL_SEQ(astfold_type_param, type_param, node_->v.AsyncFunctionDef.type_params);
CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
BEFORE_FUNC_BODY(state, node_);
CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body);
AFTER_FUNC_BODY(state);
CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list);
if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) {
CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns);
}
break;
}
case ClassDef_kind:
CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params);
CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases);
CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords);
CALL(astfold_body, asdl_seq, node_->v.ClassDef.body);
CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list);
break;
case Return_kind:
BEFORE_RETURN(state, node_);
CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
break;
case Delete_kind:
CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets);
break;
case Assign_kind:
CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets);
CALL(astfold_expr, expr_ty, node_->v.Assign.value);
break;
case AugAssign_kind:
CALL(astfold_expr, expr_ty, node_->v.AugAssign.target);
CALL(astfold_expr, expr_ty, node_->v.AugAssign.value);
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);
}
CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
break;
case TypeAlias_kind:
CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name);
CALL_SEQ(astfold_type_param, type_param, node_->v.TypeAlias.type_params);
CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value);
break;
case For_kind: {
CALL(astfold_expr, expr_ty, node_->v.For.target);
CALL(astfold_expr, expr_ty, node_->v.For.iter);
BEFORE_LOOP_BODY(state, node_);
CALL_SEQ(astfold_stmt, stmt, node_->v.For.body);
AFTER_LOOP_BODY(state);
CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse);
break;
}
case AsyncFor_kind: {
CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target);
CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter);
BEFORE_LOOP_BODY(state, node_);
CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body);
AFTER_LOOP_BODY(state);
CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse);
break;
}
case While_kind: {
CALL(astfold_expr, expr_ty, node_->v.While.test);
BEFORE_LOOP_BODY(state, node_);
CALL_SEQ(astfold_stmt, stmt, node_->v.While.body);
AFTER_LOOP_BODY(state);
CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse);
break;
}
case If_kind:
CALL(astfold_expr, expr_ty, node_->v.If.test);
CALL_SEQ(astfold_stmt, stmt, node_->v.If.body);
CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse);
break;
case With_kind:
CALL_SEQ(astfold_withitem, withitem, node_->v.With.items);
CALL_SEQ(astfold_stmt, stmt, node_->v.With.body);
break;
case AsyncWith_kind:
CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items);
CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body);
break;
case Raise_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
break;
case Try_kind: {
CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body);
CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers);
CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse);
BEFORE_FINALLY(state, node_);
CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody);
AFTER_FINALLY(state);
break;
}
case TryStar_kind: {
CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body);
CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers);
CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse);
BEFORE_FINALLY(state, node_);
CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody);
AFTER_FINALLY(state);
break;
}
case Assert_kind:
CALL(astfold_expr, expr_ty, node_->v.Assert.test);
CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg);
break;
case Expr_kind:
CALL(astfold_expr, expr_ty, node_->v.Expr.value);
break;
case Match_kind:
CALL(astfold_expr, expr_ty, node_->v.Match.subject);
CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases);
break;
case Break_kind:
BEFORE_LOOP_EXIT(state, node_, "break");
break;
case Continue_kind:
BEFORE_LOOP_EXIT(state, node_, "continue");
break;
// The following statements don't contain any subexpressions to be folded
case Import_kind:
case ImportFrom_kind:
case Global_kind:
case Nonlocal_kind:
case Pass_kind:
break;
// No default case, so the compiler will emit a warning if new statement
// kinds are added without being handled here
}
LEAVE_RECURSIVE();
return 1;
}
static int
astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
switch (node_->kind) {
case ExceptHandler_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body);
break;
// No default case, so the compiler will emit a warning if new handler
// kinds are added without being handled here
}
return 1;
}
static int
astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_expr, expr_ty, node_->context_expr);
CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
return 1;
}
static int
fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state)
{
if (state->syntax_check_only) {
return 1;
}
switch (node->kind)
{
case UnaryOp_kind:
{
if (node->v.UnaryOp.op == USub &&
node->v.UnaryOp.operand->kind == Constant_kind)
{
PyObject *operand = node->v.UnaryOp.operand->v.Constant.value;
PyObject *folded = PyNumber_Negative(operand);
return make_const(node, folded, ctx_);
}
break;
}
case BinOp_kind:
{
operator_ty op = node->v.BinOp.op;
if ((op == Add || op == Sub) &&
node->v.BinOp.right->kind == Constant_kind)
{
CALL(fold_const_match_patterns, expr_ty, node->v.BinOp.left);
if (node->v.BinOp.left->kind == Constant_kind) {
PyObject *left = node->v.BinOp.left->v.Constant.value;
PyObject *right = node->v.BinOp.right->v.Constant.value;
PyObject *folded = op == Add ? PyNumber_Add(left, right) : PyNumber_Subtract(left, right);
return make_const(node, folded, ctx_);
}
}
break;
}
default:
break;
}
return 1;
}
static int
astfold_pattern(pattern_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
// Currently, this is really only used to form complex/negative numeric
// constants in MatchValue and MatchMapping nodes
// We still recurse into all subexpressions and subpatterns anyway
ENTER_RECURSIVE();
switch (node_->kind) {
case MatchValue_kind:
CALL(fold_const_match_patterns, expr_ty, node_->v.MatchValue.value);
break;
case MatchSingleton_kind:
break;
case MatchSequence_kind:
CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns);
break;
case MatchMapping_kind:
CALL_SEQ(fold_const_match_patterns, expr, node_->v.MatchMapping.keys);
CALL_SEQ(astfold_pattern, pattern, node_->v.MatchMapping.patterns);
break;
case MatchClass_kind:
CALL(astfold_expr, expr_ty, node_->v.MatchClass.cls);
CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.patterns);
CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.kwd_patterns);
break;
case MatchStar_kind:
break;
case MatchAs_kind:
if (node_->v.MatchAs.pattern) {
CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern);
}
break;
case MatchOr_kind:
CALL_SEQ(astfold_pattern, pattern, node_->v.MatchOr.patterns);
break;
// No default case, so the compiler will emit a warning if new pattern
// kinds are added without being handled here
}
LEAVE_RECURSIVE();
return 1;
}
static int
astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
CALL(astfold_pattern, expr_ty, node_->pattern);
CALL_OPT(astfold_expr, expr_ty, node_->guard);
CALL_SEQ(astfold_stmt, stmt, node_->body);
return 1;
}
static int
astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state)
{
switch (node_->kind) {
case TypeVar_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound);
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value);
break;
case ParamSpec_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value);
break;
case TypeVarTuple_kind:
CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value);
break;
}
return 1;
}
#undef CALL
#undef CALL_OPT
#undef CALL_SEQ
int
_PyAST_Preprocess(mod_ty mod, PyArena *arena, PyObject *filename, int optimize,
int ff_features, int syntax_check_only)
{
_PyASTPreprocessState state;
memset(&state, 0, sizeof(_PyASTPreprocessState));
state.filename = filename;
state.optimize = optimize;
state.ff_features = ff_features;
state.syntax_check_only = syntax_check_only;
if (_Py_CArray_Init(&state.cf_finally, sizeof(ControlFlowInFinallyContext), 20) < 0) {
return -1;
}
int ret = astfold_mod(mod, arena, &state);
assert(ret || PyErr_Occurred());
_Py_CArray_Fini(&state.cf_finally);
return ret;
}
|