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