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