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