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