/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 | 689k | #define ENTER_RECURSIVE() \ |
28 | 689k | if (Py_EnterRecursiveCall(" during compilation")) { \ |
29 | 0 | return 0; \ |
30 | 0 | } |
31 | | |
32 | 689k | #define LEAVE_RECURSIVE() Py_LeaveRecursiveCall(); |
33 | | |
34 | | static ControlFlowInFinallyContext* |
35 | | get_cf_finally_top(_PyASTPreprocessState *state) |
36 | 17.5k | { |
37 | 17.5k | int idx = state->cf_finally_used; |
38 | 17.5k | return ((ControlFlowInFinallyContext*)state->cf_finally.array) + idx; |
39 | 17.5k | } |
40 | | |
41 | | static int |
42 | | push_cf_context(_PyASTPreprocessState *state, stmt_ty node, bool finally, bool funcdef, bool loop) |
43 | 10.0k | { |
44 | 10.0k | if (_Py_CArray_EnsureCapacity(&state->cf_finally, state->cf_finally_used+1) < 0) { |
45 | 0 | return 0; |
46 | 0 | } |
47 | | |
48 | 10.0k | state->cf_finally_used++; |
49 | 10.0k | ControlFlowInFinallyContext *ctx = get_cf_finally_top(state); |
50 | | |
51 | 10.0k | ctx->in_finally = finally; |
52 | 10.0k | ctx->in_funcdef = funcdef; |
53 | 10.0k | ctx->in_loop = loop; |
54 | 10.0k | return 1; |
55 | 10.0k | } |
56 | | |
57 | | static void |
58 | | pop_cf_context(_PyASTPreprocessState *state) |
59 | 10.0k | { |
60 | 10.0k | assert(state->cf_finally_used > 0); |
61 | 10.0k | state->cf_finally_used--; |
62 | 10.0k | } |
63 | | |
64 | | static int |
65 | | control_flow_in_finally_warning(const char *kw, stmt_ty n, _PyASTPreprocessState *state) |
66 | 2.35k | { |
67 | 2.35k | PyObject *msg = PyUnicode_FromFormat("'%s' in a 'finally' block", kw); |
68 | 2.35k | if (msg == NULL) { |
69 | 0 | return 0; |
70 | 0 | } |
71 | 2.35k | int ret = _PyErr_EmitSyntaxWarning(msg, state->filename, n->lineno, |
72 | 2.35k | n->col_offset + 1, n->end_lineno, |
73 | 2.35k | n->end_col_offset + 1); |
74 | 2.35k | Py_DECREF(msg); |
75 | 2.35k | return ret < 0 ? 0 : 1; |
76 | 2.35k | } |
77 | | |
78 | | static int |
79 | | before_return(_PyASTPreprocessState *state, stmt_ty node_) |
80 | 7.33k | { |
81 | 7.33k | if (state->cf_finally_used > 0) { |
82 | 6.86k | ControlFlowInFinallyContext *ctx = get_cf_finally_top(state); |
83 | 6.86k | if (ctx->in_finally && ! ctx->in_funcdef) { |
84 | 2.28k | if (!control_flow_in_finally_warning("return", node_, state)) { |
85 | 0 | return 0; |
86 | 0 | } |
87 | 2.28k | } |
88 | 6.86k | } |
89 | 7.33k | return 1; |
90 | 7.33k | } |
91 | | |
92 | | static int |
93 | | before_loop_exit(_PyASTPreprocessState *state, stmt_ty node_, const char *kw) |
94 | 949 | { |
95 | 949 | if (state->cf_finally_used > 0) { |
96 | 551 | ControlFlowInFinallyContext *ctx = get_cf_finally_top(state); |
97 | 551 | if (ctx->in_finally && ! ctx->in_loop) { |
98 | 77 | if (!control_flow_in_finally_warning(kw, node_, state)) { |
99 | 0 | return 0; |
100 | 0 | } |
101 | 77 | } |
102 | 551 | } |
103 | 949 | return 1; |
104 | 949 | } |
105 | | |
106 | | #define PUSH_CONTEXT(S, N, FINALLY, FUNCDEF, LOOP) \ |
107 | 10.0k | if (!push_cf_context((S), (N), (FINALLY), (FUNCDEF), (LOOP))) { \ |
108 | 0 | return 0; \ |
109 | 0 | } |
110 | | |
111 | 10.0k | #define POP_CONTEXT(S) pop_cf_context(S) |
112 | | |
113 | 1.91k | #define BEFORE_FINALLY(S, N) PUSH_CONTEXT((S), (N), true, false, false) |
114 | 1.91k | #define AFTER_FINALLY(S) POP_CONTEXT(S) |
115 | 6.36k | #define BEFORE_FUNC_BODY(S, N) PUSH_CONTEXT((S), (N), false, true, false) |
116 | 6.36k | #define AFTER_FUNC_BODY(S) POP_CONTEXT(S) |
117 | 1.81k | #define BEFORE_LOOP_BODY(S, N) PUSH_CONTEXT((S), (N), false, false, true) |
118 | 1.81k | #define AFTER_LOOP_BODY(S) POP_CONTEXT(S) |
119 | | |
120 | | #define BEFORE_RETURN(S, N) \ |
121 | 7.33k | if (!before_return((S), (N))) { \ |
122 | 0 | return 0; \ |
123 | 0 | } |
124 | | |
125 | | #define BEFORE_LOOP_EXIT(S, N, KW) \ |
126 | 949 | 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 | 389 | #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.44k | for (Py_ssize_t i = 0; i < n; i++) { |
159 | 970 | expr_ty e = (expr_ty)asdl_seq_GET(elts, i); |
160 | 970 | if (e->kind == Starred_kind) { |
161 | 0 | return 1; |
162 | 0 | } |
163 | 970 | } |
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.70k | if (PyUnicode_READ(kind, data, pos) != '%') { |
178 | 7.80k | pos++; |
179 | 7.80k | } |
180 | 895 | else if (pos+1 < size && PyUnicode_READ(kind, data, pos+1) == '%') { |
181 | 2 | has_percents = 1; |
182 | 2 | pos += 2; |
183 | 2 | } |
184 | 893 | else { |
185 | 893 | break; |
186 | 893 | } |
187 | 8.70k | } |
188 | 1.28k | *ppos = pos; |
189 | 1.28k | if (pos == start) { |
190 | 537 | return NULL; |
191 | 537 | } |
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 | 893 | { |
216 | 893 | Py_ssize_t pos = *ppos, len = PyUnicode_GET_LENGTH(fmt); |
217 | 893 | Py_UCS4 ch; |
218 | | |
219 | 947 | #define NEXTC do { \ |
220 | 947 | if (pos >= len) { \ |
221 | 0 | return 0; \ |
222 | 0 | } \ |
223 | 947 | ch = PyUnicode_READ_CHAR(fmt, pos); \ |
224 | 947 | pos++; \ |
225 | 947 | } while (0) |
226 | | |
227 | 893 | *flags = 0; |
228 | 921 | while (1) { |
229 | 921 | NEXTC; |
230 | 921 | 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 | 921 | } |
237 | 893 | break; |
238 | 921 | } |
239 | 893 | 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 | 890 | 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 | 890 | *spec = ch; |
266 | 890 | *ppos = pos; |
267 | 890 | return 1; |
268 | | |
269 | 890 | #undef NEXTC |
270 | 890 | } |
271 | | |
272 | | static expr_ty |
273 | | parse_format(PyObject *fmt, Py_ssize_t *ppos, expr_ty arg, PyArena *arena) |
274 | 893 | { |
275 | 893 | int spec, flags, width = -1, prec = -1; |
276 | 893 | if (!simple_format_arg_parse(fmt, ppos, &spec, &flags, &width, &prec)) { |
277 | | // Unsupported format. |
278 | 3 | return NULL; |
279 | 3 | } |
280 | 890 | if (spec == 's' || spec == 'r' || spec == 'a') { |
281 | 812 | char buf[1 + MAXDIGITS + 1 + MAXDIGITS + 1], *p = buf; |
282 | 812 | if (!(flags & F_LJUST) && width > 0) { |
283 | 0 | *p++ = '>'; |
284 | 0 | } |
285 | 812 | if (width >= 0) { |
286 | 3 | p += snprintf(p, MAXDIGITS + 1, "%d", width); |
287 | 3 | } |
288 | 812 | if (prec >= 0) { |
289 | 1 | p += snprintf(p, MAXDIGITS + 2, ".%d", prec); |
290 | 1 | } |
291 | 812 | expr_ty format_spec = NULL; |
292 | 812 | 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 | 812 | return _PyAST_FormattedValue(arg, spec, format_spec, |
307 | 812 | arg->lineno, arg->col_offset, |
308 | 812 | arg->end_lineno, arg->end_col_offset, |
309 | 812 | arena); |
310 | 812 | } |
311 | | // Unsupported format. |
312 | 78 | return NULL; |
313 | 890 | } |
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 | 537 | else if (PyErr_Occurred()) { |
332 | 0 | return 0; |
333 | 0 | } |
334 | | |
335 | 1.28k | if (pos >= PyUnicode_GET_LENGTH(fmt)) { |
336 | 389 | break; |
337 | 389 | } |
338 | 893 | if (cnt >= asdl_seq_LEN(elts)) { |
339 | | // More format units than items. |
340 | 0 | return 1; |
341 | 0 | } |
342 | 893 | assert(PyUnicode_READ_CHAR(fmt, pos) == '%'); |
343 | 893 | pos++; |
344 | 893 | expr_ty expr = parse_format(fmt, &pos, asdl_seq_GET(elts, cnt), arena); |
345 | 893 | cnt++; |
346 | 893 | if (!expr) { |
347 | 81 | return !PyErr_Occurred(); |
348 | 81 | } |
349 | 812 | asdl_seq_SET(seq, seq->size++, expr); |
350 | 812 | } |
351 | 389 | if (cnt < asdl_seq_LEN(elts)) { |
352 | | // More items than format units. |
353 | 0 | return 1; |
354 | 0 | } |
355 | 389 | expr_ty res = _PyAST_JoinedStr(seq, |
356 | 389 | node->lineno, node->col_offset, |
357 | 389 | node->end_lineno, node->end_col_offset, |
358 | 389 | arena); |
359 | 389 | if (!res) { |
360 | 0 | return 0; |
361 | 0 | } |
362 | 389 | COPY_NODE(node, res); |
363 | | // PySys_FormatStderr("format = %R\n", fmt); |
364 | 389 | return 1; |
365 | 389 | } |
366 | | |
367 | | static int |
368 | | fold_binop(expr_ty node, PyArena *arena, _PyASTPreprocessState *state) |
369 | 29.9k | { |
370 | 29.9k | if (state->syntax_check_only) { |
371 | 26.3k | return 1; |
372 | 26.3k | } |
373 | 3.57k | expr_ty lhs, rhs; |
374 | 3.57k | lhs = node->v.BinOp.left; |
375 | 3.57k | rhs = node->v.BinOp.right; |
376 | 3.57k | if (lhs->kind != Constant_kind) { |
377 | 2.45k | return 1; |
378 | 2.45k | } |
379 | 1.12k | PyObject *lv = lhs->v.Constant.value; |
380 | | |
381 | 1.12k | if (node->v.BinOp.op == Mod && |
382 | 1.12k | rhs->kind == Tuple_kind && |
383 | 1.12k | PyUnicode_Check(lv) && |
384 | 1.12k | !has_starred(rhs->v.Tuple.elts)) |
385 | 470 | { |
386 | 470 | return optimize_format(node, lv, rhs->v.Tuple.elts, arena); |
387 | 470 | } |
388 | | |
389 | 656 | return 1; |
390 | 1.12k | } |
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 | 463k | if (!FUNC((ARG), ctx_, state)) \ |
407 | 463k | return 0; |
408 | | |
409 | | #define CALL_OPT(FUNC, TYPE, ARG) \ |
410 | 96.0k | if ((ARG) != NULL && !FUNC((ARG), ctx_, state)) \ |
411 | 96.0k | return 0; |
412 | | |
413 | 224k | #define CALL_SEQ(FUNC, TYPE, ARG) { \ |
414 | 224k | Py_ssize_t i; \ |
415 | 224k | asdl_ ## TYPE ## _seq *seq = (ARG); /* avoid variable capture */ \ |
416 | 526k | for (i = 0; i < asdl_seq_LEN(seq); i++) { \ |
417 | 301k | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
418 | 301k | if (elt != NULL && !FUNC(elt, ctx_, state)) \ |
419 | 301k | return 0; \ |
420 | 301k | } \ |
421 | 224k | } |
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 | 16.1k | { |
441 | 16.1k | int docstring = _PyAST_GetDocString(stmts) != NULL; |
442 | 16.1k | 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 | 16.1k | CALL_SEQ(astfold_stmt, stmt, stmts); |
450 | 16.1k | 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 | 16.1k | return 1; |
466 | 16.1k | } |
467 | | |
468 | | static int |
469 | | astfold_mod(mod_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
470 | 7.88k | { |
471 | 7.88k | switch (node_->kind) { |
472 | 7.83k | case Module_kind: |
473 | 7.83k | CALL(astfold_body, asdl_seq, node_->v.Module.body); |
474 | 7.83k | 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 | 7.88k | } |
487 | 7.88k | return 1; |
488 | 7.88k | } |
489 | | |
490 | | static int |
491 | | astfold_expr(expr_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
492 | 583k | { |
493 | 583k | ENTER_RECURSIVE(); |
494 | 583k | switch (node_->kind) { |
495 | 2.00k | case BoolOp_kind: |
496 | 2.00k | CALL_SEQ(astfold_expr, expr, node_->v.BoolOp.values); |
497 | 2.00k | break; |
498 | 29.9k | case BinOp_kind: |
499 | 29.9k | CALL(astfold_expr, expr_ty, node_->v.BinOp.left); |
500 | 29.9k | CALL(astfold_expr, expr_ty, node_->v.BinOp.right); |
501 | 29.9k | CALL(fold_binop, expr_ty, node_); |
502 | 29.9k | break; |
503 | 179k | case UnaryOp_kind: |
504 | 179k | CALL(astfold_expr, expr_ty, node_->v.UnaryOp.operand); |
505 | 179k | break; |
506 | 1.46k | case Lambda_kind: |
507 | 1.46k | CALL(astfold_arguments, arguments_ty, node_->v.Lambda.args); |
508 | 1.46k | CALL(astfold_expr, expr_ty, node_->v.Lambda.body); |
509 | 1.46k | break; |
510 | 342 | case IfExp_kind: |
511 | 342 | CALL(astfold_expr, expr_ty, node_->v.IfExp.test); |
512 | 342 | CALL(astfold_expr, expr_ty, node_->v.IfExp.body); |
513 | 342 | CALL(astfold_expr, expr_ty, node_->v.IfExp.orelse); |
514 | 342 | break; |
515 | 1.92k | case Dict_kind: |
516 | 1.92k | CALL_SEQ(astfold_expr, expr, node_->v.Dict.keys); |
517 | 1.92k | CALL_SEQ(astfold_expr, expr, node_->v.Dict.values); |
518 | 1.92k | break; |
519 | 463 | case Set_kind: |
520 | 463 | CALL_SEQ(astfold_expr, expr, node_->v.Set.elts); |
521 | 463 | break; |
522 | 490 | case ListComp_kind: |
523 | 490 | CALL(astfold_expr, expr_ty, node_->v.ListComp.elt); |
524 | 490 | CALL_SEQ(astfold_comprehension, comprehension, node_->v.ListComp.generators); |
525 | 490 | break; |
526 | 158 | case SetComp_kind: |
527 | 158 | CALL(astfold_expr, expr_ty, node_->v.SetComp.elt); |
528 | 158 | CALL_SEQ(astfold_comprehension, comprehension, node_->v.SetComp.generators); |
529 | 158 | break; |
530 | 591 | case DictComp_kind: |
531 | 591 | CALL(astfold_expr, expr_ty, node_->v.DictComp.key); |
532 | 591 | CALL(astfold_expr, expr_ty, node_->v.DictComp.value); |
533 | 591 | CALL_SEQ(astfold_comprehension, comprehension, node_->v.DictComp.generators); |
534 | 591 | break; |
535 | 799 | case GeneratorExp_kind: |
536 | 799 | CALL(astfold_expr, expr_ty, node_->v.GeneratorExp.elt); |
537 | 799 | CALL_SEQ(astfold_comprehension, comprehension, node_->v.GeneratorExp.generators); |
538 | 799 | break; |
539 | 205 | case Await_kind: |
540 | 205 | CALL(astfold_expr, expr_ty, node_->v.Await.value); |
541 | 205 | break; |
542 | 667 | case Yield_kind: |
543 | 667 | CALL_OPT(astfold_expr, expr_ty, node_->v.Yield.value); |
544 | 667 | break; |
545 | 96 | case YieldFrom_kind: |
546 | 96 | CALL(astfold_expr, expr_ty, node_->v.YieldFrom.value); |
547 | 96 | break; |
548 | 8.09k | case Compare_kind: |
549 | 8.09k | CALL(astfold_expr, expr_ty, node_->v.Compare.left); |
550 | 8.09k | CALL_SEQ(astfold_expr, expr, node_->v.Compare.comparators); |
551 | 8.09k | break; |
552 | 25.1k | case Call_kind: |
553 | 25.1k | CALL(astfold_expr, expr_ty, node_->v.Call.func); |
554 | 25.1k | CALL_SEQ(astfold_expr, expr, node_->v.Call.args); |
555 | 25.1k | CALL_SEQ(astfold_keyword, keyword, node_->v.Call.keywords); |
556 | 25.1k | break; |
557 | 13.8k | case FormattedValue_kind: |
558 | 13.8k | CALL(astfold_expr, expr_ty, node_->v.FormattedValue.value); |
559 | 13.8k | CALL_OPT(astfold_expr, expr_ty, node_->v.FormattedValue.format_spec); |
560 | 13.8k | break; |
561 | 874 | case Interpolation_kind: |
562 | 874 | CALL(astfold_expr, expr_ty, node_->v.Interpolation.value); |
563 | 874 | CALL_OPT(astfold_expr, expr_ty, node_->v.Interpolation.format_spec); |
564 | 874 | break; |
565 | 6.83k | case JoinedStr_kind: |
566 | 6.83k | CALL_SEQ(astfold_expr, expr, node_->v.JoinedStr.values); |
567 | 6.83k | break; |
568 | 357 | case TemplateStr_kind: |
569 | 357 | CALL_SEQ(astfold_expr, expr, node_->v.TemplateStr.values); |
570 | 357 | break; |
571 | 19.8k | case Attribute_kind: |
572 | 19.8k | CALL(astfold_expr, expr_ty, node_->v.Attribute.value); |
573 | 19.8k | 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 | 1.02k | case Starred_kind: |
579 | 1.02k | CALL(astfold_expr, expr_ty, node_->v.Starred.value); |
580 | 1.02k | break; |
581 | 4.16k | case Slice_kind: |
582 | 4.16k | CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.lower); |
583 | 4.16k | CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.upper); |
584 | 4.16k | CALL_OPT(astfold_expr, expr_ty, node_->v.Slice.step); |
585 | 4.16k | break; |
586 | 3.93k | case List_kind: |
587 | 3.93k | CALL_SEQ(astfold_expr, expr, node_->v.List.elts); |
588 | 3.93k | break; |
589 | 18.0k | case Tuple_kind: |
590 | 18.0k | CALL_SEQ(astfold_expr, expr, node_->v.Tuple.elts); |
591 | 18.0k | break; |
592 | 151k | case Name_kind: |
593 | 151k | if (state->syntax_check_only) { |
594 | 80.8k | break; |
595 | 80.8k | } |
596 | 70.2k | if (node_->v.Name.ctx == Load && |
597 | 70.2k | _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 | 70.2k | break; |
602 | 70.2k | case NamedExpr_kind: |
603 | 247 | CALL(astfold_expr, expr_ty, node_->v.NamedExpr.value); |
604 | 247 | break; |
605 | 107k | case Constant_kind: |
606 | | // Already a constant, nothing further to do |
607 | 107k | break; |
608 | | // No default case, so the compiler will emit a warning if new expression |
609 | | // kinds are added without being handled here |
610 | 583k | } |
611 | 583k | LEAVE_RECURSIVE(); |
612 | 583k | return 1; |
613 | 583k | } |
614 | | |
615 | | static int |
616 | | astfold_keyword(keyword_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
617 | 3.65k | { |
618 | 3.65k | CALL(astfold_expr, expr_ty, node_->value); |
619 | 3.65k | return 1; |
620 | 3.65k | } |
621 | | |
622 | | static int |
623 | | astfold_comprehension(comprehension_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
624 | 2.15k | { |
625 | 2.15k | CALL(astfold_expr, expr_ty, node_->target); |
626 | 2.15k | CALL(astfold_expr, expr_ty, node_->iter); |
627 | 2.15k | CALL_SEQ(astfold_expr, expr, node_->ifs); |
628 | 2.15k | return 1; |
629 | 2.15k | } |
630 | | |
631 | | static int |
632 | | astfold_arguments(arguments_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
633 | 7.83k | { |
634 | 7.83k | CALL_SEQ(astfold_arg, arg, node_->posonlyargs); |
635 | 7.83k | CALL_SEQ(astfold_arg, arg, node_->args); |
636 | 7.83k | CALL_OPT(astfold_arg, arg_ty, node_->vararg); |
637 | 7.83k | CALL_SEQ(astfold_arg, arg, node_->kwonlyargs); |
638 | 7.83k | CALL_SEQ(astfold_expr, expr, node_->kw_defaults); |
639 | 7.83k | CALL_OPT(astfold_arg, arg_ty, node_->kwarg); |
640 | 7.83k | CALL_SEQ(astfold_expr, expr, node_->defaults); |
641 | 7.83k | return 1; |
642 | 7.83k | } |
643 | | |
644 | | static int |
645 | | astfold_arg(arg_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
646 | 19.9k | { |
647 | 19.9k | if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { |
648 | 19.5k | CALL_OPT(astfold_expr, expr_ty, node_->annotation); |
649 | 19.5k | } |
650 | 19.9k | return 1; |
651 | 19.9k | } |
652 | | |
653 | | static int |
654 | | astfold_stmt(stmt_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
655 | 95.1k | { |
656 | 95.1k | ENTER_RECURSIVE(); |
657 | 95.1k | switch (node_->kind) { |
658 | 5.95k | case FunctionDef_kind: { |
659 | 5.95k | CALL_SEQ(astfold_type_param, type_param, node_->v.FunctionDef.type_params); |
660 | 5.95k | CALL(astfold_arguments, arguments_ty, node_->v.FunctionDef.args); |
661 | 5.95k | BEFORE_FUNC_BODY(state, node_); |
662 | 5.95k | CALL(astfold_body, asdl_seq, node_->v.FunctionDef.body); |
663 | 5.95k | AFTER_FUNC_BODY(state); |
664 | 5.95k | CALL_SEQ(astfold_expr, expr, node_->v.FunctionDef.decorator_list); |
665 | 5.95k | if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { |
666 | 5.80k | CALL_OPT(astfold_expr, expr_ty, node_->v.FunctionDef.returns); |
667 | 5.80k | } |
668 | 5.95k | break; |
669 | 5.95k | } |
670 | 5.95k | case AsyncFunctionDef_kind: { |
671 | 403 | CALL_SEQ(astfold_type_param, type_param, node_->v.AsyncFunctionDef.type_params); |
672 | 403 | CALL(astfold_arguments, arguments_ty, node_->v.AsyncFunctionDef.args); |
673 | 403 | BEFORE_FUNC_BODY(state, node_); |
674 | 403 | CALL(astfold_body, asdl_seq, node_->v.AsyncFunctionDef.body); |
675 | 403 | AFTER_FUNC_BODY(state); |
676 | 403 | CALL_SEQ(astfold_expr, expr, node_->v.AsyncFunctionDef.decorator_list); |
677 | 403 | if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { |
678 | 337 | CALL_OPT(astfold_expr, expr_ty, node_->v.AsyncFunctionDef.returns); |
679 | 337 | } |
680 | 403 | break; |
681 | 403 | } |
682 | 1.95k | case ClassDef_kind: |
683 | 1.95k | CALL_SEQ(astfold_type_param, type_param, node_->v.ClassDef.type_params); |
684 | 1.95k | CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.bases); |
685 | 1.95k | CALL_SEQ(astfold_keyword, keyword, node_->v.ClassDef.keywords); |
686 | 1.95k | CALL(astfold_body, asdl_seq, node_->v.ClassDef.body); |
687 | 1.95k | CALL_SEQ(astfold_expr, expr, node_->v.ClassDef.decorator_list); |
688 | 1.95k | break; |
689 | 7.33k | case Return_kind: |
690 | 7.33k | BEFORE_RETURN(state, node_); |
691 | 7.33k | CALL_OPT(astfold_expr, expr_ty, node_->v.Return.value); |
692 | 7.33k | break; |
693 | 461 | case Delete_kind: |
694 | 461 | CALL_SEQ(astfold_expr, expr, node_->v.Delete.targets); |
695 | 461 | break; |
696 | 12.4k | case Assign_kind: |
697 | 12.4k | CALL_SEQ(astfold_expr, expr, node_->v.Assign.targets); |
698 | 12.4k | CALL(astfold_expr, expr_ty, node_->v.Assign.value); |
699 | 12.4k | break; |
700 | 815 | case AugAssign_kind: |
701 | 815 | CALL(astfold_expr, expr_ty, node_->v.AugAssign.target); |
702 | 815 | CALL(astfold_expr, expr_ty, node_->v.AugAssign.value); |
703 | 815 | break; |
704 | 1.18k | case AnnAssign_kind: |
705 | 1.18k | CALL(astfold_expr, expr_ty, node_->v.AnnAssign.target); |
706 | 1.18k | if (!(state->ff_features & CO_FUTURE_ANNOTATIONS)) { |
707 | 1.11k | CALL(astfold_expr, expr_ty, node_->v.AnnAssign.annotation); |
708 | 1.11k | } |
709 | 1.18k | CALL_OPT(astfold_expr, expr_ty, node_->v.AnnAssign.value); |
710 | 1.18k | break; |
711 | 217 | case TypeAlias_kind: |
712 | 217 | CALL(astfold_expr, expr_ty, node_->v.TypeAlias.name); |
713 | 217 | CALL_SEQ(astfold_type_param, type_param, node_->v.TypeAlias.type_params); |
714 | 217 | CALL(astfold_expr, expr_ty, node_->v.TypeAlias.value); |
715 | 217 | break; |
716 | 1.13k | case For_kind: { |
717 | 1.13k | CALL(astfold_expr, expr_ty, node_->v.For.target); |
718 | 1.13k | CALL(astfold_expr, expr_ty, node_->v.For.iter); |
719 | 1.13k | BEFORE_LOOP_BODY(state, node_); |
720 | 1.13k | CALL_SEQ(astfold_stmt, stmt, node_->v.For.body); |
721 | 1.13k | AFTER_LOOP_BODY(state); |
722 | 1.13k | CALL_SEQ(astfold_stmt, stmt, node_->v.For.orelse); |
723 | 1.13k | break; |
724 | 1.13k | } |
725 | 138 | case AsyncFor_kind: { |
726 | 138 | CALL(astfold_expr, expr_ty, node_->v.AsyncFor.target); |
727 | 138 | CALL(astfold_expr, expr_ty, node_->v.AsyncFor.iter); |
728 | 138 | BEFORE_LOOP_BODY(state, node_); |
729 | 138 | CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.body); |
730 | 138 | AFTER_LOOP_BODY(state); |
731 | 138 | CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncFor.orelse); |
732 | 138 | break; |
733 | 138 | } |
734 | 539 | case While_kind: { |
735 | 539 | CALL(astfold_expr, expr_ty, node_->v.While.test); |
736 | 539 | BEFORE_LOOP_BODY(state, node_); |
737 | 539 | CALL_SEQ(astfold_stmt, stmt, node_->v.While.body); |
738 | 539 | AFTER_LOOP_BODY(state); |
739 | 539 | CALL_SEQ(astfold_stmt, stmt, node_->v.While.orelse); |
740 | 539 | break; |
741 | 539 | } |
742 | 7.10k | case If_kind: |
743 | 7.10k | CALL(astfold_expr, expr_ty, node_->v.If.test); |
744 | 7.10k | CALL_SEQ(astfold_stmt, stmt, node_->v.If.body); |
745 | 7.10k | CALL_SEQ(astfold_stmt, stmt, node_->v.If.orelse); |
746 | 7.10k | break; |
747 | 451 | case With_kind: |
748 | 451 | CALL_SEQ(astfold_withitem, withitem, node_->v.With.items); |
749 | 451 | CALL_SEQ(astfold_stmt, stmt, node_->v.With.body); |
750 | 451 | break; |
751 | 128 | case AsyncWith_kind: |
752 | 128 | CALL_SEQ(astfold_withitem, withitem, node_->v.AsyncWith.items); |
753 | 128 | CALL_SEQ(astfold_stmt, stmt, node_->v.AsyncWith.body); |
754 | 128 | break; |
755 | 2.13k | case Raise_kind: |
756 | 2.13k | CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.exc); |
757 | 2.13k | CALL_OPT(astfold_expr, expr_ty, node_->v.Raise.cause); |
758 | 2.13k | break; |
759 | 1.47k | case Try_kind: { |
760 | 1.47k | CALL_SEQ(astfold_stmt, stmt, node_->v.Try.body); |
761 | 1.47k | CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.Try.handlers); |
762 | 1.47k | CALL_SEQ(astfold_stmt, stmt, node_->v.Try.orelse); |
763 | 1.47k | BEFORE_FINALLY(state, node_); |
764 | 1.47k | CALL_SEQ(astfold_stmt, stmt, node_->v.Try.finalbody); |
765 | 1.47k | AFTER_FINALLY(state); |
766 | 1.47k | break; |
767 | 1.47k | } |
768 | 432 | case TryStar_kind: { |
769 | 432 | CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.body); |
770 | 432 | CALL_SEQ(astfold_excepthandler, excepthandler, node_->v.TryStar.handlers); |
771 | 432 | CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.orelse); |
772 | 432 | BEFORE_FINALLY(state, node_); |
773 | 432 | CALL_SEQ(astfold_stmt, stmt, node_->v.TryStar.finalbody); |
774 | 432 | AFTER_FINALLY(state); |
775 | 432 | break; |
776 | 432 | } |
777 | 369 | case Assert_kind: |
778 | 369 | CALL(astfold_expr, expr_ty, node_->v.Assert.test); |
779 | 369 | CALL_OPT(astfold_expr, expr_ty, node_->v.Assert.msg); |
780 | 369 | break; |
781 | 45.5k | case Expr_kind: |
782 | 45.5k | CALL(astfold_expr, expr_ty, node_->v.Expr.value); |
783 | 45.5k | break; |
784 | 300 | case Match_kind: |
785 | 300 | CALL(astfold_expr, expr_ty, node_->v.Match.subject); |
786 | 300 | CALL_SEQ(astfold_match_case, match_case, node_->v.Match.cases); |
787 | 300 | break; |
788 | 530 | case Break_kind: |
789 | 530 | BEFORE_LOOP_EXIT(state, node_, "break"); |
790 | 530 | break; |
791 | 419 | case Continue_kind: |
792 | 419 | BEFORE_LOOP_EXIT(state, node_, "continue"); |
793 | 419 | break; |
794 | | // The following statements don't contain any subexpressions to be folded |
795 | 932 | case Import_kind: |
796 | 2.06k | case ImportFrom_kind: |
797 | 2.26k | case Global_kind: |
798 | 3.03k | case Nonlocal_kind: |
799 | 3.64k | case Pass_kind: |
800 | 3.64k | break; |
801 | | // No default case, so the compiler will emit a warning if new statement |
802 | | // kinds are added without being handled here |
803 | 95.1k | } |
804 | 95.1k | LEAVE_RECURSIVE(); |
805 | 95.1k | return 1; |
806 | 95.1k | } |
807 | | |
808 | | static int |
809 | | astfold_excepthandler(excepthandler_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
810 | 2.56k | { |
811 | 2.56k | switch (node_->kind) { |
812 | 2.56k | case ExceptHandler_kind: |
813 | 2.56k | CALL_OPT(astfold_expr, expr_ty, node_->v.ExceptHandler.type); |
814 | 2.56k | CALL_SEQ(astfold_stmt, stmt, node_->v.ExceptHandler.body); |
815 | 2.56k | break; |
816 | | // No default case, so the compiler will emit a warning if new handler |
817 | | // kinds are added without being handled here |
818 | 2.56k | } |
819 | 2.56k | return 1; |
820 | 2.56k | } |
821 | | |
822 | | static int |
823 | | astfold_withitem(withitem_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
824 | 2.39k | { |
825 | 2.39k | CALL(astfold_expr, expr_ty, node_->context_expr); |
826 | 2.39k | CALL_OPT(astfold_expr, expr_ty, node_->optional_vars); |
827 | 2.39k | return 1; |
828 | 2.39k | } |
829 | | |
830 | | static int |
831 | | fold_const_match_patterns(expr_ty node, PyArena *ctx_, _PyASTPreprocessState *state) |
832 | 1.54k | { |
833 | 1.54k | if (state->syntax_check_only) { |
834 | 1.54k | return 1; |
835 | 1.54k | } |
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 | 10.9k | { |
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 | 10.9k | ENTER_RECURSIVE(); |
878 | 10.9k | switch (node_->kind) { |
879 | 961 | case MatchValue_kind: |
880 | 961 | CALL(fold_const_match_patterns, expr_ty, node_->v.MatchValue.value); |
881 | 961 | break; |
882 | 88 | case MatchSingleton_kind: |
883 | 88 | break; |
884 | 765 | case MatchSequence_kind: |
885 | 765 | CALL_SEQ(astfold_pattern, pattern, node_->v.MatchSequence.patterns); |
886 | 765 | break; |
887 | 1.38k | case MatchMapping_kind: |
888 | 1.38k | CALL_SEQ(fold_const_match_patterns, expr, node_->v.MatchMapping.keys); |
889 | 1.38k | CALL_SEQ(astfold_pattern, pattern, node_->v.MatchMapping.patterns); |
890 | 1.38k | break; |
891 | 861 | case MatchClass_kind: |
892 | 861 | CALL(astfold_expr, expr_ty, node_->v.MatchClass.cls); |
893 | 861 | CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.patterns); |
894 | 861 | CALL_SEQ(astfold_pattern, pattern, node_->v.MatchClass.kwd_patterns); |
895 | 861 | break; |
896 | 544 | case MatchStar_kind: |
897 | 544 | break; |
898 | 4.88k | case MatchAs_kind: |
899 | 4.88k | if (node_->v.MatchAs.pattern) { |
900 | 74 | CALL(astfold_pattern, pattern_ty, node_->v.MatchAs.pattern); |
901 | 74 | } |
902 | 4.88k | break; |
903 | 4.88k | case MatchOr_kind: |
904 | 1.45k | CALL_SEQ(astfold_pattern, pattern, node_->v.MatchOr.patterns); |
905 | 1.45k | break; |
906 | | // No default case, so the compiler will emit a warning if new pattern |
907 | | // kinds are added without being handled here |
908 | 10.9k | } |
909 | 10.9k | LEAVE_RECURSIVE(); |
910 | 10.9k | return 1; |
911 | 10.9k | } |
912 | | |
913 | | static int |
914 | | astfold_match_case(match_case_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
915 | 1.57k | { |
916 | 1.57k | CALL(astfold_pattern, expr_ty, node_->pattern); |
917 | 1.57k | CALL_OPT(astfold_expr, expr_ty, node_->guard); |
918 | 1.57k | CALL_SEQ(astfold_stmt, stmt, node_->body); |
919 | 1.57k | return 1; |
920 | 1.57k | } |
921 | | |
922 | | static int |
923 | | astfold_type_param(type_param_ty node_, PyArena *ctx_, _PyASTPreprocessState *state) |
924 | 4.21k | { |
925 | 4.21k | switch (node_->kind) { |
926 | 2.88k | case TypeVar_kind: |
927 | 2.88k | CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.bound); |
928 | 2.88k | CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVar.default_value); |
929 | 2.88k | break; |
930 | 485 | case ParamSpec_kind: |
931 | 485 | CALL_OPT(astfold_expr, expr_ty, node_->v.ParamSpec.default_value); |
932 | 485 | break; |
933 | 843 | case TypeVarTuple_kind: |
934 | 843 | CALL_OPT(astfold_expr, expr_ty, node_->v.TypeVarTuple.default_value); |
935 | 843 | break; |
936 | 4.21k | } |
937 | 4.21k | return 1; |
938 | 4.21k | } |
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 | 7.88k | { |
948 | 7.88k | _PyASTPreprocessState state; |
949 | 7.88k | memset(&state, 0, sizeof(_PyASTPreprocessState)); |
950 | 7.88k | state.filename = filename; |
951 | 7.88k | state.optimize = optimize; |
952 | 7.88k | state.ff_features = ff_features; |
953 | 7.88k | state.syntax_check_only = syntax_check_only; |
954 | 7.88k | if (_Py_CArray_Init(&state.cf_finally, sizeof(ControlFlowInFinallyContext), 20) < 0) { |
955 | 0 | return -1; |
956 | 0 | } |
957 | | |
958 | 7.88k | int ret = astfold_mod(mod, arena, &state); |
959 | 7.88k | assert(ret || PyErr_Occurred()); |
960 | | |
961 | 7.88k | _Py_CArray_Fini(&state.cf_finally); |
962 | 7.88k | return ret; |
963 | 7.88k | } |