You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

768 lines
23 KiB

  1. /* AST Optimizer */
  2. #include "Python.h"
  3. #include "Python-ast.h"
  4. /* TODO: is_const and get_const_value are copied from Python/compile.c.
  5. It should be deduped in the future. Maybe, we can include this file
  6. from compile.c?
  7. */
  8. static int
  9. is_const(expr_ty e)
  10. {
  11. switch (e->kind) {
  12. case Constant_kind:
  13. case Num_kind:
  14. case Str_kind:
  15. case Bytes_kind:
  16. case Ellipsis_kind:
  17. case NameConstant_kind:
  18. return 1;
  19. default:
  20. return 0;
  21. }
  22. }
  23. static PyObject *
  24. get_const_value(expr_ty e)
  25. {
  26. switch (e->kind) {
  27. case Constant_kind:
  28. return e->v.Constant.value;
  29. case Num_kind:
  30. return e->v.Num.n;
  31. case Str_kind:
  32. return e->v.Str.s;
  33. case Bytes_kind:
  34. return e->v.Bytes.s;
  35. case Ellipsis_kind:
  36. return Py_Ellipsis;
  37. case NameConstant_kind:
  38. return e->v.NameConstant.value;
  39. default:
  40. Py_UNREACHABLE();
  41. }
  42. }
  43. static int
  44. make_const(expr_ty node, PyObject *val, PyArena *arena)
  45. {
  46. if (val == NULL) {
  47. if (PyErr_ExceptionMatches(PyExc_KeyboardInterrupt)) {
  48. return 0;
  49. }
  50. PyErr_Clear();
  51. return 1;
  52. }
  53. if (PyArena_AddPyObject(arena, val) < 0) {
  54. Py_DECREF(val);
  55. return 0;
  56. }
  57. node->kind = Constant_kind;
  58. node->v.Constant.value = val;
  59. return 1;
  60. }
  61. #define COPY_NODE(TO, FROM) (memcpy((TO), (FROM), sizeof(struct _expr)))
  62. static PyObject*
  63. unary_not(PyObject *v)
  64. {
  65. int r = PyObject_IsTrue(v);
  66. if (r < 0)
  67. return NULL;
  68. return PyBool_FromLong(!r);
  69. }
  70. static int
  71. fold_unaryop(expr_ty node, PyArena *arena, int optimize)
  72. {
  73. expr_ty arg = node->v.UnaryOp.operand;
  74. if (!is_const(arg)) {
  75. /* Fold not into comparison */
  76. if (node->v.UnaryOp.op == Not && arg->kind == Compare_kind &&
  77. asdl_seq_LEN(arg->v.Compare.ops) == 1) {
  78. /* Eq and NotEq are often implemented in terms of one another, so
  79. folding not (self == other) into self != other breaks implementation
  80. of !=. Detecting such cases doesn't seem worthwhile.
  81. Python uses </> for 'is subset'/'is superset' operations on sets.
  82. They don't satisfy not folding laws. */
  83. int op = asdl_seq_GET(arg->v.Compare.ops, 0);
  84. switch (op) {
  85. case Is:
  86. op = IsNot;
  87. break;
  88. case IsNot:
  89. op = Is;
  90. break;
  91. case In:
  92. op = NotIn;
  93. break;
  94. case NotIn:
  95. op = In;
  96. break;
  97. default:
  98. op = 0;
  99. }
  100. if (op) {
  101. asdl_seq_SET(arg->v.Compare.ops, 0, op);
  102. COPY_NODE(node, arg);
  103. return 1;
  104. }
  105. }
  106. return 1;
  107. }
  108. typedef PyObject *(*unary_op)(PyObject*);
  109. static const unary_op ops[] = {
  110. [Invert] = PyNumber_Invert,
  111. [Not] = unary_not,
  112. [UAdd] = PyNumber_Positive,
  113. [USub] = PyNumber_Negative,
  114. };
  115. PyObject *newval = ops[node->v.UnaryOp.op](get_const_value(arg));
  116. return make_const(node, newval, arena);
  117. }
  118. /* Check whether a collection doesn't containing too much items (including
  119. subcollections). This protects from creating a constant that needs
  120. too much time for calculating a hash.
  121. "limit" is the maximal number of items.
  122. Returns the negative number if the total number of items exceeds the
  123. limit. Otherwise returns the limit minus the total number of items.
  124. */
  125. static Py_ssize_t
  126. check_complexity(PyObject *obj, Py_ssize_t limit)
  127. {
  128. if (PyTuple_Check(obj)) {
  129. Py_ssize_t i;
  130. limit -= PyTuple_GET_SIZE(obj);
  131. for (i = 0; limit >= 0 && i < PyTuple_GET_SIZE(obj); i++) {
  132. limit = check_complexity(PyTuple_GET_ITEM(obj, i), limit);
  133. }
  134. return limit;
  135. }
  136. else if (PyFrozenSet_Check(obj)) {
  137. Py_ssize_t i = 0;
  138. PyObject *item;
  139. Py_hash_t hash;
  140. limit -= PySet_GET_SIZE(obj);
  141. while (limit >= 0 && _PySet_NextEntry(obj, &i, &item, &hash)) {
  142. limit = check_complexity(item, limit);
  143. }
  144. }
  145. return limit;
  146. }
  147. #define MAX_INT_SIZE 128 /* bits */
  148. #define MAX_COLLECTION_SIZE 256 /* items */
  149. #define MAX_STR_SIZE 4096 /* characters */
  150. #define MAX_TOTAL_ITEMS 1024 /* including nested collections */
  151. static PyObject *
  152. safe_multiply(PyObject *v, PyObject *w)
  153. {
  154. if (PyLong_Check(v) && PyLong_Check(w) && Py_SIZE(v) && Py_SIZE(w)) {
  155. size_t vbits = _PyLong_NumBits(v);
  156. size_t wbits = _PyLong_NumBits(w);
  157. if (vbits == (size_t)-1 || wbits == (size_t)-1) {
  158. return NULL;
  159. }
  160. if (vbits + wbits > MAX_INT_SIZE) {
  161. return NULL;
  162. }
  163. }
  164. else if (PyLong_Check(v) && (PyTuple_Check(w) || PyFrozenSet_Check(w))) {
  165. Py_ssize_t size = PyTuple_Check(w) ? PyTuple_GET_SIZE(w) :
  166. PySet_GET_SIZE(w);
  167. if (size) {
  168. long n = PyLong_AsLong(v);
  169. if (n < 0 || n > MAX_COLLECTION_SIZE / size) {
  170. return NULL;
  171. }
  172. if (n && check_complexity(w, MAX_TOTAL_ITEMS / n) < 0) {
  173. return NULL;
  174. }
  175. }
  176. }
  177. else if (PyLong_Check(v) && (PyUnicode_Check(w) || PyBytes_Check(w))) {
  178. Py_ssize_t size = PyUnicode_Check(w) ? PyUnicode_GET_LENGTH(w) :
  179. PyBytes_GET_SIZE(w);
  180. if (size) {
  181. long n = PyLong_AsLong(v);
  182. if (n < 0 || n > MAX_STR_SIZE / size) {
  183. return NULL;
  184. }
  185. }
  186. }
  187. else if (PyLong_Check(w) &&
  188. (PyTuple_Check(v) || PyFrozenSet_Check(v) ||
  189. PyUnicode_Check(v) || PyBytes_Check(v)))
  190. {
  191. return safe_multiply(w, v);
  192. }
  193. return PyNumber_Multiply(v, w);
  194. }
  195. static PyObject *
  196. safe_power(PyObject *v, PyObject *w)
  197. {
  198. if (PyLong_Check(v) && PyLong_Check(w) && Py_SIZE(v) && Py_SIZE(w) > 0) {
  199. size_t vbits = _PyLong_NumBits(v);
  200. size_t wbits = PyLong_AsSize_t(w);
  201. if (vbits == (size_t)-1 || wbits == (size_t)-1) {
  202. return NULL;
  203. }
  204. if (vbits > MAX_INT_SIZE / wbits) {
  205. return NULL;
  206. }
  207. }
  208. return PyNumber_Power(v, w, Py_None);
  209. }
  210. static PyObject *
  211. safe_lshift(PyObject *v, PyObject *w)
  212. {
  213. if (PyLong_Check(v) && PyLong_Check(w) && Py_SIZE(v) && Py_SIZE(w)) {
  214. size_t vbits = _PyLong_NumBits(v);
  215. size_t wbits = PyLong_AsSize_t(w);
  216. if (vbits == (size_t)-1 || wbits == (size_t)-1) {
  217. return NULL;
  218. }
  219. if (wbits > MAX_INT_SIZE || vbits > MAX_INT_SIZE - wbits) {
  220. return NULL;
  221. }
  222. }
  223. return PyNumber_Lshift(v, w);
  224. }
  225. static PyObject *
  226. safe_mod(PyObject *v, PyObject *w)
  227. {
  228. if (PyUnicode_Check(v) || PyBytes_Check(v)) {
  229. return NULL;
  230. }
  231. return PyNumber_Remainder(v, w);
  232. }
  233. static int
  234. fold_binop(expr_ty node, PyArena *arena, int optimize)
  235. {
  236. expr_ty lhs, rhs;
  237. lhs = node->v.BinOp.left;
  238. rhs = node->v.BinOp.right;
  239. if (!is_const(lhs) || !is_const(rhs)) {
  240. return 1;
  241. }
  242. PyObject *lv = get_const_value(lhs);
  243. PyObject *rv = get_const_value(rhs);
  244. PyObject *newval;
  245. switch (node->v.BinOp.op) {
  246. case Add:
  247. newval = PyNumber_Add(lv, rv);
  248. break;
  249. case Sub:
  250. newval = PyNumber_Subtract(lv, rv);
  251. break;
  252. case Mult:
  253. newval = safe_multiply(lv, rv);
  254. break;
  255. case Div:
  256. newval = PyNumber_TrueDivide(lv, rv);
  257. break;
  258. case FloorDiv:
  259. newval = PyNumber_FloorDivide(lv, rv);
  260. break;
  261. case Mod:
  262. newval = safe_mod(lv, rv);
  263. break;
  264. case Pow:
  265. newval = safe_power(lv, rv);
  266. break;
  267. case LShift:
  268. newval = safe_lshift(lv, rv);
  269. break;
  270. case RShift:
  271. newval = PyNumber_Rshift(lv, rv);
  272. break;
  273. case BitOr:
  274. newval = PyNumber_Or(lv, rv);
  275. break;
  276. case BitXor:
  277. newval = PyNumber_Xor(lv, rv);
  278. break;
  279. case BitAnd:
  280. newval = PyNumber_And(lv, rv);
  281. break;
  282. default: // Unknown operator
  283. return 1;
  284. }
  285. return make_const(node, newval, arena);
  286. }
  287. static PyObject*
  288. make_const_tuple(asdl_seq *elts)
  289. {
  290. for (int i = 0; i < asdl_seq_LEN(elts); i++) {
  291. expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
  292. if (!is_const(e)) {
  293. return NULL;
  294. }
  295. }
  296. PyObject *newval = PyTuple_New(asdl_seq_LEN(elts));
  297. if (newval == NULL) {
  298. return NULL;
  299. }
  300. for (int i = 0; i < asdl_seq_LEN(elts); i++) {
  301. expr_ty e = (expr_ty)asdl_seq_GET(elts, i);
  302. PyObject *v = get_const_value(e);
  303. Py_INCREF(v);
  304. PyTuple_SET_ITEM(newval, i, v);
  305. }
  306. return newval;
  307. }
  308. static int
  309. fold_tuple(expr_ty node, PyArena *arena, int optimize)
  310. {
  311. PyObject *newval;
  312. if (node->v.Tuple.ctx != Load)
  313. return 1;
  314. newval = make_const_tuple(node->v.Tuple.elts);
  315. return make_const(node, newval, arena);
  316. }
  317. static int
  318. fold_subscr(expr_ty node, PyArena *arena, int optimize)
  319. {
  320. PyObject *newval;
  321. expr_ty arg, idx;
  322. slice_ty slice;
  323. arg = node->v.Subscript.value;
  324. slice = node->v.Subscript.slice;
  325. if (node->v.Subscript.ctx != Load ||
  326. !is_const(arg) ||
  327. /* TODO: handle other types of slices */
  328. slice->kind != Index_kind ||
  329. !is_const(slice->v.Index.value))
  330. {
  331. return 1;
  332. }
  333. idx = slice->v.Index.value;
  334. newval = PyObject_GetItem(get_const_value(arg), get_const_value(idx));
  335. return make_const(node, newval, arena);
  336. }
  337. /* Change literal list or set of constants into constant
  338. tuple or frozenset respectively.
  339. Used for right operand of "in" and "not in" tests and for iterable
  340. in "for" loop and comprehensions.
  341. */
  342. static int
  343. fold_iter(expr_ty arg, PyArena *arena, int optimize)
  344. {
  345. PyObject *newval;
  346. if (arg->kind == List_kind) {
  347. newval = make_const_tuple(arg->v.List.elts);
  348. }
  349. else if (arg->kind == Set_kind) {
  350. newval = make_const_tuple(arg->v.Set.elts);
  351. if (newval) {
  352. Py_SETREF(newval, PyFrozenSet_New(newval));
  353. }
  354. }
  355. else {
  356. return 1;
  357. }
  358. return make_const(arg, newval, arena);
  359. }
  360. static int
  361. fold_compare(expr_ty node, PyArena *arena, int optimize)
  362. {
  363. asdl_int_seq *ops;
  364. asdl_seq *args;
  365. Py_ssize_t i;
  366. ops = node->v.Compare.ops;
  367. args = node->v.Compare.comparators;
  368. /* TODO: optimize cases with literal arguments. */
  369. /* Change literal list or set in 'in' or 'not in' into
  370. tuple or frozenset respectively. */
  371. i = asdl_seq_LEN(ops) - 1;
  372. int op = asdl_seq_GET(ops, i);
  373. if (op == In || op == NotIn) {
  374. if (!fold_iter((expr_ty)asdl_seq_GET(args, i), arena, optimize)) {
  375. return 0;
  376. }
  377. }
  378. return 1;
  379. }
  380. static int astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_);
  381. static int astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_);
  382. static int astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_);
  383. static int astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_);
  384. static int astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_);
  385. static int astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_);
  386. static int astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_);
  387. static int astfold_arg(arg_ty node_, PyArena *ctx_, int optimize_);
  388. static int astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_);
  389. static int astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_);
  390. #define CALL(FUNC, TYPE, ARG) \
  391. if (!FUNC((ARG), ctx_, optimize_)) \
  392. return 0;
  393. #define CALL_OPT(FUNC, TYPE, ARG) \
  394. if ((ARG) != NULL && !FUNC((ARG), ctx_, optimize_)) \
  395. return 0;
  396. #define CALL_SEQ(FUNC, TYPE, ARG) { \
  397. int i; \
  398. asdl_seq *seq = (ARG); /* avoid variable capture */ \
  399. for (i = 0; i < asdl_seq_LEN(seq); i++) { \
  400. TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
  401. if (elt != NULL && !FUNC(elt, ctx_, optimize_)) \
  402. return 0; \
  403. } \
  404. }
  405. #define CALL_INT_SEQ(FUNC, TYPE, ARG) { \
  406. int i; \
  407. asdl_int_seq *seq = (ARG); /* avoid variable capture */ \
  408. for (i = 0; i < asdl_seq_LEN(seq); i++) { \
  409. TYPE elt = (TYPE)asdl_seq_GET(seq, i); \
  410. if (!FUNC(elt, ctx_, optimize_)) \
  411. return 0; \
  412. } \
  413. }
  414. static int
  415. astfold_mod(mod_ty node_, PyArena *ctx_, int optimize_)
  416. {
  417. switch (node_->kind) {
  418. case Module_kind:
  419. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Module.body);
  420. break;
  421. case Interactive_kind:
  422. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Interactive.body);
  423. break;
  424. case Expression_kind:
  425. CALL(astfold_expr, expr_ty, node_->v.Expression.body);
  426. break;
  427. case Suite_kind:
  428. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Suite.body);
  429. break;
  430. default:
  431. break;
  432. }
  433. return 1;
  434. }
  435. static int
  436. astfold_expr(expr_ty node_, PyArena *ctx_, int optimize_)
  437. {
  438. switch (node_->kind) {
  439. case BoolOp_kind:
  440. CALL_SEQ(astfold_expr, expr_ty, node_->v.BoolOp.values);
  441. break;
  442. case BinOp_kind:
  443. CALL(astfold_expr, expr_ty, node_->v.BinOp.left);
  444. CALL(astfold_expr, expr_ty, node_->v.BinOp.right);
  445. CALL(fold_binop, expr_ty, node_);
  446. break;
  447. case UnaryOp_kind:
  448. CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand);
  449. CALL(fold_unaryop, expr_ty, node_);
  450. break;
  451. case Lambda_kind:
  452. CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args);
  453. CALL(astfold_expr, expr_ty, node_->v.Lambda.body);
  454. break;
  455. case IfExp_kind:
  456. CALL(astfold_expr, expr_ty, node_->v.IfExp.test);
  457. CALL(astfold_expr, expr_ty, node_->v.IfExp.body);
  458. CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse);
  459. break;
  460. case Dict_kind:
  461. CALL_SEQ(astfold_expr, expr_ty, node_->v.Dict.keys);
  462. CALL_SEQ(astfold_expr, expr_ty, node_->v.Dict.values);
  463. break;
  464. case Set_kind:
  465. CALL_SEQ(astfold_expr, expr_ty, node_->v.Set.elts);
  466. break;
  467. case ListComp_kind:
  468. CALL(astfold_expr, expr_ty, node_->v.ListComp.elt);
  469. CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.ListComp.generators);
  470. break;
  471. case SetComp_kind:
  472. CALL(astfold_expr, expr_ty, node_->v.SetComp.elt);
  473. CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.SetComp.generators);
  474. break;
  475. case DictComp_kind:
  476. CALL(astfold_expr, expr_ty, node_->v.DictComp.key);
  477. CALL(astfold_expr, expr_ty, node_->v.DictComp.value);
  478. CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.DictComp.generators);
  479. break;
  480. case GeneratorExp_kind:
  481. CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt);
  482. CALL_SEQ(astfold_comprehension, comprehension_ty, node_->v.GeneratorExp.generators);
  483. break;
  484. case Await_kind:
  485. CALL(astfold_expr, expr_ty, node_->v.Await.value);
  486. break;
  487. case Yield_kind:
  488. CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value);
  489. break;
  490. case YieldFrom_kind:
  491. CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value);
  492. break;
  493. case Compare_kind:
  494. CALL(astfold_expr, expr_ty, node_->v.Compare.left);
  495. CALL_SEQ(astfold_expr, expr_ty, node_->v.Compare.comparators);
  496. CALL(fold_compare, expr_ty, node_);
  497. break;
  498. case Call_kind:
  499. CALL(astfold_expr, expr_ty, node_->v.Call.func);
  500. CALL_SEQ(astfold_expr, expr_ty, node_->v.Call.args);
  501. CALL_SEQ(astfold_keyword, keyword_ty, node_->v.Call.keywords);
  502. break;
  503. case FormattedValue_kind:
  504. CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value);
  505. CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec);
  506. break;
  507. case JoinedStr_kind:
  508. CALL_SEQ(astfold_expr, expr_ty, node_->v.JoinedStr.values);
  509. break;
  510. case Attribute_kind:
  511. CALL(astfold_expr, expr_ty, node_->v.Attribute.value);
  512. break;
  513. case Subscript_kind:
  514. CALL(astfold_expr, expr_ty, node_->v.Subscript.value);
  515. CALL(astfold_slice, slice_ty, node_->v.Subscript.slice);
  516. CALL(fold_subscr, expr_ty, node_);
  517. break;
  518. case Starred_kind:
  519. CALL(astfold_expr, expr_ty, node_->v.Starred.value);
  520. break;
  521. case List_kind:
  522. CALL_SEQ(astfold_expr, expr_ty, node_->v.List.elts);
  523. break;
  524. case Tuple_kind:
  525. CALL_SEQ(astfold_expr, expr_ty, node_->v.Tuple.elts);
  526. CALL(fold_tuple, expr_ty, node_);
  527. break;
  528. case Name_kind:
  529. if (_PyUnicode_EqualToASCIIString(node_->v.Name.id, "__debug__")) {
  530. return make_const(node_, PyBool_FromLong(!optimize_), ctx_);
  531. }
  532. break;
  533. default:
  534. break;
  535. }
  536. return 1;
  537. }
  538. static int
  539. astfold_slice(slice_ty node_, PyArena *ctx_, int optimize_)
  540. {
  541. switch (node_->kind) {
  542. case Slice_kind:
  543. CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower);
  544. CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper);
  545. CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step);
  546. break;
  547. case ExtSlice_kind:
  548. CALL_SEQ(astfold_slice, slice_ty, node_->v.ExtSlice.dims);
  549. break;
  550. case Index_kind:
  551. CALL(astfold_expr, expr_ty, node_->v.Index.value);
  552. break;
  553. default:
  554. break;
  555. }
  556. return 1;
  557. }
  558. static int
  559. astfold_keyword(keyword_ty node_, PyArena *ctx_, int optimize_)
  560. {
  561. CALL(astfold_expr, expr_ty, node_->value);
  562. return 1;
  563. }
  564. static int
  565. astfold_comprehension(comprehension_ty node_, PyArena *ctx_, int optimize_)
  566. {
  567. CALL(astfold_expr, expr_ty, node_->target);
  568. CALL(astfold_expr, expr_ty, node_->iter);
  569. CALL_SEQ(astfold_expr, expr_ty, node_->ifs);
  570. CALL(fold_iter, expr_ty, node_->iter);
  571. return 1;
  572. }
  573. static int
  574. astfold_arguments(arguments_ty node_, PyArena *ctx_, int optimize_)
  575. {
  576. CALL_SEQ(astfold_arg, arg_ty, node_->args);
  577. CALL_OPT(astfold_arg, arg_ty, node_->vararg);
  578. CALL_SEQ(astfold_arg, arg_ty, node_->kwonlyargs);
  579. CALL_SEQ(astfold_expr, expr_ty, node_->kw_defaults);
  580. CALL_OPT(astfold_arg, arg_ty, node_->kwarg);
  581. CALL_SEQ(astfold_expr, expr_ty, node_->defaults);
  582. return 1;
  583. }
  584. static int
  585. astfold_arg(arg_ty node_, PyArena *ctx_, int optimize_)
  586. {
  587. CALL_OPT(astfold_expr, expr_ty, node_->annotation);
  588. return 1;
  589. }
  590. static int
  591. astfold_stmt(stmt_ty node_, PyArena *ctx_, int optimize_)
  592. {
  593. switch (node_->kind) {
  594. case FunctionDef_kind:
  595. CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args);
  596. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.FunctionDef.body);
  597. CALL_SEQ(astfold_expr, expr_ty, node_->v.FunctionDef.decorator_list);
  598. CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns);
  599. break;
  600. case AsyncFunctionDef_kind:
  601. CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args);
  602. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncFunctionDef.body);
  603. CALL_SEQ(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.decorator_list);
  604. CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns);
  605. break;
  606. case ClassDef_kind:
  607. CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.bases);
  608. CALL_SEQ(astfold_keyword, keyword_ty, node_->v.ClassDef.keywords);
  609. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.ClassDef.body);
  610. CALL_SEQ(astfold_expr, expr_ty, node_->v.ClassDef.decorator_list);
  611. break;
  612. case Return_kind:
  613. CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value);
  614. break;
  615. case Delete_kind:
  616. CALL_SEQ(astfold_expr, expr_ty, node_->v.Delete.targets);
  617. break;
  618. case Assign_kind:
  619. CALL_SEQ(astfold_expr, expr_ty, node_->v.Assign.targets);
  620. CALL(astfold_expr, expr_ty, node_->v.Assign.value);
  621. break;
  622. case AugAssign_kind:
  623. CALL(astfold_expr, expr_ty, node_->v.AugAssign.target);
  624. CALL(astfold_expr, expr_ty, node_->v.AugAssign.value);
  625. break;
  626. case AnnAssign_kind:
  627. CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target);
  628. CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation);
  629. CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value);
  630. break;
  631. case For_kind:
  632. CALL(astfold_expr, expr_ty, node_->v.For.target);
  633. CALL(astfold_expr, expr_ty, node_->v.For.iter);
  634. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.For.body);
  635. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.For.orelse);
  636. CALL(fold_iter, expr_ty, node_->v.For.iter);
  637. break;
  638. case AsyncFor_kind:
  639. CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target);
  640. CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter);
  641. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncFor.body);
  642. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncFor.orelse);
  643. break;
  644. case While_kind:
  645. CALL(astfold_expr, expr_ty, node_->v.While.test);
  646. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.While.body);
  647. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.While.orelse);
  648. break;
  649. case If_kind:
  650. CALL(astfold_expr, expr_ty, node_->v.If.test);
  651. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.If.body);
  652. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.If.orelse);
  653. break;
  654. case With_kind:
  655. CALL_SEQ(astfold_withitem, withitem_ty, node_->v.With.items);
  656. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.With.body);
  657. break;
  658. case AsyncWith_kind:
  659. CALL_SEQ(astfold_withitem, withitem_ty, node_->v.AsyncWith.items);
  660. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.AsyncWith.body);
  661. break;
  662. case Raise_kind:
  663. CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc);
  664. CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause);
  665. break;
  666. case Try_kind:
  667. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Try.body);
  668. CALL_SEQ(astfold_excepthandler, excepthandler_ty, node_->v.Try.handlers);
  669. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Try.orelse);
  670. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.Try.finalbody);
  671. break;
  672. case Assert_kind:
  673. CALL(astfold_expr, expr_ty, node_->v.Assert.test);
  674. CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg);
  675. break;
  676. case Expr_kind:
  677. CALL(astfold_expr, expr_ty, node_->v.Expr.value);
  678. break;
  679. default:
  680. break;
  681. }
  682. return 1;
  683. }
  684. static int
  685. astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, int optimize_)
  686. {
  687. switch (node_->kind) {
  688. case ExceptHandler_kind:
  689. CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type);
  690. CALL_SEQ(astfold_stmt, stmt_ty, node_->v.ExceptHandler.body);
  691. break;
  692. default:
  693. break;
  694. }
  695. return 1;
  696. }
  697. static int
  698. astfold_withitem(withitem_ty node_, PyArena *ctx_, int optimize_)
  699. {
  700. CALL(astfold_expr, expr_ty, node_->context_expr);
  701. CALL_OPT(astfold_expr, expr_ty, node_->optional_vars);
  702. return 1;
  703. }
  704. #undef CALL
  705. #undef CALL_OPT
  706. #undef CALL_SEQ
  707. #undef CALL_INT_SEQ
  708. int
  709. _PyAST_Optimize(mod_ty mod, PyArena *arena, int optimize)
  710. {
  711. int ret = astfold_mod(mod, arena, optimize);
  712. assert(ret || PyErr_Occurred());
  713. return ret;
  714. }