1 /*
2 * This file compiles an abstract syntax tree (AST) into Python bytecode.
3 *
4 * The primary entry point is _PyAST_Compile(), which returns a
5 * PyCodeObject. The compiler makes several passes to build the code
6 * object:
7 * 1. Checks for future statements. See future.c
8 * 2. Builds a symbol table. See symtable.c.
9 * 3. Generate an instruction sequence. See compiler_mod() in this file.
10 * 4. Generate a control flow graph and run optimizations on it. See flowgraph.c.
11 * 5. Assemble the basic blocks into final code. See optimize_and_assemble() in
12 * this file, and assembler.c.
13 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
16 *
17 * CAUTION: The VISIT_* macros abort the current function when they
18 * encounter a problem. So don't invoke them when there is memory
19 * which needs to be released. Code blocks are OK, as the compiler
20 * structure takes care of releasing those. Use the arena to manage
21 * objects.
22 */
23
24 #include <stdbool.h>
25
26 #include "Python.h"
27 #include "opcode.h"
28 #include "pycore_ast.h" // _PyAST_GetDocString()
29 #define NEED_OPCODE_TABLES
30 #include "pycore_opcode_utils.h"
31 #undef NEED_OPCODE_TABLES
32 #include "pycore_code.h" // _PyCode_New()
33 #include "pycore_compile.h"
34 #include "pycore_flowgraph.h"
35 #include "pycore_instruction_sequence.h" // _PyInstructionSequence_New()
36 #include "pycore_intrinsics.h"
37 #include "pycore_long.h" // _PyLong_GetZero()
38 #include "pycore_pystate.h" // _Py_GetConfig()
39 #include "pycore_setobject.h" // _PySet_NextEntry()
40 #include "pycore_symtable.h" // PySTEntryObject, _PyFuture_FromAST()
41
42 #define NEED_OPCODE_METADATA
43 #include "pycore_opcode_metadata.h" // _PyOpcode_opcode_metadata, _PyOpcode_num_popped/pushed
44 #undef NEED_OPCODE_METADATA
45
46 #define COMP_GENEXP 0
47 #define COMP_LISTCOMP 1
48 #define COMP_SETCOMP 2
49 #define COMP_DICTCOMP 3
50
51 /* A soft limit for stack use, to avoid excessive
52 * memory use for large constants, etc.
53 *
54 * The value 30 is plucked out of thin air.
55 * Code that could use more stack than this is
56 * rare, so the exact value is unimportant.
57 */
58 #define STACK_USE_GUIDELINE 30
59
60 #undef SUCCESS
61 #undef ERROR
62 #define SUCCESS 0
63 #define ERROR -1
64
65 #define RETURN_IF_ERROR(X) \
66 if ((X) == -1) { \
67 return ERROR; \
68 }
69
70 #define IS_TOP_LEVEL_AWAIT(C) ( \
71 ((C)->c_flags.cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
72 && ((C)->u->u_ste->ste_type == ModuleBlock))
73
74 typedef _Py_SourceLocation location;
75 typedef struct _PyCfgBuilder cfg_builder;
76
77 #define LOCATION(LNO, END_LNO, COL, END_COL) \
78 ((const _Py_SourceLocation){(LNO), (END_LNO), (COL), (END_COL)})
79
80 /* Return true if loc1 starts after loc2 ends. */
81 static inline bool
location_is_after(location loc1,location loc2)82 location_is_after(location loc1, location loc2) {
83 return (loc1.lineno > loc2.end_lineno) ||
84 ((loc1.lineno == loc2.end_lineno) &&
85 (loc1.col_offset > loc2.end_col_offset));
86 }
87
88 #define LOC(x) SRC_LOCATION_FROM_AST(x)
89
90 typedef _PyJumpTargetLabel jump_target_label;
91
92 static jump_target_label NO_LABEL = {-1};
93
94 #define SAME_LABEL(L1, L2) ((L1).id == (L2).id)
95 #define IS_LABEL(L) (!SAME_LABEL((L), (NO_LABEL)))
96
97 #define NEW_JUMP_TARGET_LABEL(C, NAME) \
98 jump_target_label NAME = _PyInstructionSequence_NewLabel(INSTR_SEQUENCE(C)); \
99 if (!IS_LABEL(NAME)) { \
100 return ERROR; \
101 }
102
103 #define USE_LABEL(C, LBL) \
104 RETURN_IF_ERROR(_PyInstructionSequence_UseLabel(INSTR_SEQUENCE(C), (LBL).id))
105
106
107 /* fblockinfo tracks the current frame block.
108
109 A frame block is used to handle loops, try/except, and try/finally.
110 It's called a frame block to distinguish it from a basic block in the
111 compiler IR.
112 */
113
114 enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
115 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER,
116 EXCEPTION_GROUP_HANDLER, ASYNC_COMPREHENSION_GENERATOR,
117 STOP_ITERATION };
118
119 struct fblockinfo {
120 enum fblocktype fb_type;
121 jump_target_label fb_block;
122 location fb_loc;
123 /* (optional) type-specific exit or cleanup block */
124 jump_target_label fb_exit;
125 /* (optional) additional information required for unwinding */
126 void *fb_datum;
127 };
128
129 enum {
130 COMPILER_SCOPE_MODULE,
131 COMPILER_SCOPE_CLASS,
132 COMPILER_SCOPE_FUNCTION,
133 COMPILER_SCOPE_ASYNC_FUNCTION,
134 COMPILER_SCOPE_LAMBDA,
135 COMPILER_SCOPE_COMPREHENSION,
136 COMPILER_SCOPE_TYPEPARAMS,
137 };
138
139
140 typedef _PyInstruction instruction;
141 typedef _PyInstructionSequence instr_sequence;
142
143 #define INITIAL_INSTR_SEQUENCE_SIZE 100
144 #define INITIAL_INSTR_SEQUENCE_LABELS_MAP_SIZE 10
145
146 /*
147 * Resize the array if index is out of range.
148 *
149 * idx: the index we want to access
150 * arr: pointer to the array
151 * alloc: pointer to the capacity of the array
152 * default_alloc: initial number of items
153 * item_size: size of each item
154 *
155 */
156 int
_PyCompile_EnsureArrayLargeEnough(int idx,void ** array,int * alloc,int default_alloc,size_t item_size)157 _PyCompile_EnsureArrayLargeEnough(int idx, void **array, int *alloc,
158 int default_alloc, size_t item_size)
159 {
160 void *arr = *array;
161 if (arr == NULL) {
162 int new_alloc = default_alloc;
163 if (idx >= new_alloc) {
164 new_alloc = idx + default_alloc;
165 }
166 arr = PyMem_Calloc(new_alloc, item_size);
167 if (arr == NULL) {
168 PyErr_NoMemory();
169 return ERROR;
170 }
171 *alloc = new_alloc;
172 }
173 else if (idx >= *alloc) {
174 size_t oldsize = *alloc * item_size;
175 int new_alloc = *alloc << 1;
176 if (idx >= new_alloc) {
177 new_alloc = idx + default_alloc;
178 }
179 size_t newsize = new_alloc * item_size;
180
181 if (oldsize > (SIZE_MAX >> 1)) {
182 PyErr_NoMemory();
183 return ERROR;
184 }
185
186 assert(newsize > 0);
187 void *tmp = PyMem_Realloc(arr, newsize);
188 if (tmp == NULL) {
189 PyErr_NoMemory();
190 return ERROR;
191 }
192 *alloc = new_alloc;
193 arr = tmp;
194 memset((char *)arr + oldsize, 0, newsize - oldsize);
195 }
196
197 *array = arr;
198 return SUCCESS;
199 }
200
201 static cfg_builder*
instr_sequence_to_cfg(instr_sequence * seq)202 instr_sequence_to_cfg(instr_sequence *seq) {
203 if (_PyInstructionSequence_ApplyLabelMap(seq) < 0) {
204 return NULL;
205 }
206 cfg_builder *g = _PyCfgBuilder_New();
207 if (g == NULL) {
208 return NULL;
209 }
210 for (int i = 0; i < seq->s_used; i++) {
211 seq->s_instrs[i].i_target = 0;
212 }
213 for (int i = 0; i < seq->s_used; i++) {
214 instruction *instr = &seq->s_instrs[i];
215 if (HAS_TARGET(instr->i_opcode)) {
216 assert(instr->i_oparg >= 0 && instr->i_oparg < seq->s_used);
217 seq->s_instrs[instr->i_oparg].i_target = 1;
218 }
219 }
220 for (int i = 0; i < seq->s_used; i++) {
221 instruction *instr = &seq->s_instrs[i];
222 if (instr->i_target) {
223 jump_target_label lbl_ = {i};
224 if (_PyCfgBuilder_UseLabel(g, lbl_) < 0) {
225 goto error;
226 }
227 }
228 int opcode = instr->i_opcode;
229 int oparg = instr->i_oparg;
230 if (_PyCfgBuilder_Addop(g, opcode, oparg, instr->i_loc) < 0) {
231 goto error;
232 }
233 }
234 if (_PyCfgBuilder_CheckSize(g) < 0) {
235 goto error;
236 }
237 return g;
238 error:
239 _PyCfgBuilder_Free(g);
240 return NULL;
241 }
242
243 /* The following items change on entry and exit of code blocks.
244 They must be saved and restored when returning to a block.
245 */
246 struct compiler_unit {
247 PySTEntryObject *u_ste;
248
249 int u_scope_type;
250
251 PyObject *u_private; /* for private name mangling */
252 PyObject *u_static_attributes; /* for class: attributes accessed via self.X */
253
254 instr_sequence *u_instr_sequence; /* codegen output */
255
256 int u_nfblocks;
257 int u_in_inlined_comp;
258
259 struct fblockinfo u_fblock[CO_MAXBLOCKS];
260
261 _PyCompile_CodeUnitMetadata u_metadata;
262 };
263
264 /* This struct captures the global state of a compilation.
265
266 The u pointer points to the current compilation unit, while units
267 for enclosing blocks are stored in c_stack. The u and c_stack are
268 managed by compiler_enter_scope() and compiler_exit_scope().
269
270 Note that we don't track recursion levels during compilation - the
271 task of detecting and rejecting excessive levels of nesting is
272 handled by the symbol analysis pass.
273
274 */
275
276 struct compiler {
277 PyObject *c_filename;
278 struct symtable *c_st;
279 _PyFutureFeatures c_future; /* module's __future__ */
280 PyCompilerFlags c_flags;
281
282 int c_optimize; /* optimization level */
283 int c_interactive; /* true if in interactive mode */
284 int c_nestlevel;
285 PyObject *c_const_cache; /* Python dict holding all constants,
286 including names tuple */
287 struct compiler_unit *u; /* compiler state for current block */
288 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
289 PyArena *c_arena; /* pointer to memory allocation arena */
290
291 bool c_save_nested_seqs; /* if true, construct recursive instruction sequences
292 * (including instructions for nested code objects)
293 */
294 };
295
296 #define INSTR_SEQUENCE(C) ((C)->u->u_instr_sequence)
297
298
299 typedef struct {
300 // A list of strings corresponding to name captures. It is used to track:
301 // - Repeated name assignments in the same pattern.
302 // - Different name assignments in alternatives.
303 // - The order of name assignments in alternatives.
304 PyObject *stores;
305 // If 0, any name captures against our subject will raise.
306 int allow_irrefutable;
307 // An array of blocks to jump to on failure. Jumping to fail_pop[i] will pop
308 // i items off of the stack. The end result looks like this (with each block
309 // falling through to the next):
310 // fail_pop[4]: POP_TOP
311 // fail_pop[3]: POP_TOP
312 // fail_pop[2]: POP_TOP
313 // fail_pop[1]: POP_TOP
314 // fail_pop[0]: NOP
315 jump_target_label *fail_pop;
316 // The current length of fail_pop.
317 Py_ssize_t fail_pop_size;
318 // The number of items on top of the stack that need to *stay* on top of the
319 // stack. Variable captures go beneath these. All of them will be popped on
320 // failure.
321 Py_ssize_t on_top;
322 } pattern_context;
323
324 static int codegen_addop_i(instr_sequence *seq, int opcode, Py_ssize_t oparg, location loc);
325
326 static void compiler_free(struct compiler *);
327 static int compiler_error(struct compiler *, location loc, const char *, ...);
328 static int compiler_warn(struct compiler *, location loc, const char *, ...);
329 static int compiler_nameop(struct compiler *, location, identifier, expr_context_ty);
330
331 static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
332 static int compiler_visit_stmt(struct compiler *, stmt_ty);
333 static int compiler_visit_keyword(struct compiler *, keyword_ty);
334 static int compiler_visit_expr(struct compiler *, expr_ty);
335 static int compiler_augassign(struct compiler *, stmt_ty);
336 static int compiler_annassign(struct compiler *, stmt_ty);
337 static int compiler_subscript(struct compiler *, expr_ty);
338 static int compiler_slice(struct compiler *, expr_ty);
339
340 static bool are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
341
342
343 static int compiler_with(struct compiler *, stmt_ty, int);
344 static int compiler_async_with(struct compiler *, stmt_ty, int);
345 static int compiler_async_for(struct compiler *, stmt_ty);
346 static int compiler_call_simple_kw_helper(struct compiler *c,
347 location loc,
348 asdl_keyword_seq *keywords,
349 Py_ssize_t nkwelts);
350 static int compiler_call_helper(struct compiler *c, location loc,
351 int n, asdl_expr_seq *args,
352 asdl_keyword_seq *keywords);
353 static int compiler_try_except(struct compiler *, stmt_ty);
354 static int compiler_try_star_except(struct compiler *, stmt_ty);
355 static int compiler_set_qualname(struct compiler *);
356
357 static int compiler_sync_comprehension_generator(
358 struct compiler *c, location loc,
359 asdl_comprehension_seq *generators, int gen_index,
360 int depth,
361 expr_ty elt, expr_ty val, int type,
362 int iter_on_stack);
363
364 static int compiler_async_comprehension_generator(
365 struct compiler *c, location loc,
366 asdl_comprehension_seq *generators, int gen_index,
367 int depth,
368 expr_ty elt, expr_ty val, int type,
369 int iter_on_stack);
370
371 static int compiler_pattern(struct compiler *, pattern_ty, pattern_context *);
372 static int compiler_match(struct compiler *, stmt_ty);
373 static int compiler_pattern_subpattern(struct compiler *,
374 pattern_ty, pattern_context *);
375
376 static PyCodeObject *optimize_and_assemble(struct compiler *, int addNone);
377
378 #define CAPSULE_NAME "compile.c compiler unit"
379
380
381 static int
compiler_setup(struct compiler * c,mod_ty mod,PyObject * filename,PyCompilerFlags * flags,int optimize,PyArena * arena)382 compiler_setup(struct compiler *c, mod_ty mod, PyObject *filename,
383 PyCompilerFlags *flags, int optimize, PyArena *arena)
384 {
385 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
386
387 c->c_const_cache = PyDict_New();
388 if (!c->c_const_cache) {
389 return ERROR;
390 }
391
392 c->c_stack = PyList_New(0);
393 if (!c->c_stack) {
394 return ERROR;
395 }
396
397 c->c_filename = Py_NewRef(filename);
398 c->c_arena = arena;
399 if (!_PyFuture_FromAST(mod, filename, &c->c_future)) {
400 return ERROR;
401 }
402 if (!flags) {
403 flags = &local_flags;
404 }
405 int merged = c->c_future.ff_features | flags->cf_flags;
406 c->c_future.ff_features = merged;
407 flags->cf_flags = merged;
408 c->c_flags = *flags;
409 c->c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
410 c->c_nestlevel = 0;
411 c->c_save_nested_seqs = false;
412
413 if (!_PyAST_Optimize(mod, arena, c->c_optimize, merged)) {
414 return ERROR;
415 }
416 c->c_st = _PySymtable_Build(mod, filename, &c->c_future);
417 if (c->c_st == NULL) {
418 if (!PyErr_Occurred()) {
419 PyErr_SetString(PyExc_SystemError, "no symtable");
420 }
421 return ERROR;
422 }
423 return SUCCESS;
424 }
425
426 static struct compiler*
new_compiler(mod_ty mod,PyObject * filename,PyCompilerFlags * pflags,int optimize,PyArena * arena)427 new_compiler(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
428 int optimize, PyArena *arena)
429 {
430 struct compiler *c = PyMem_Calloc(1, sizeof(struct compiler));
431 if (c == NULL) {
432 return NULL;
433 }
434 if (compiler_setup(c, mod, filename, pflags, optimize, arena) < 0) {
435 compiler_free(c);
436 return NULL;
437 }
438 return c;
439 }
440
441 PyCodeObject *
_PyAST_Compile(mod_ty mod,PyObject * filename,PyCompilerFlags * pflags,int optimize,PyArena * arena)442 _PyAST_Compile(mod_ty mod, PyObject *filename, PyCompilerFlags *pflags,
443 int optimize, PyArena *arena)
444 {
445 assert(!PyErr_Occurred());
446 struct compiler *c = new_compiler(mod, filename, pflags, optimize, arena);
447 if (c == NULL) {
448 return NULL;
449 }
450
451 PyCodeObject *co = compiler_mod(c, mod);
452 compiler_free(c);
453 assert(co || PyErr_Occurred());
454 return co;
455 }
456
457 int
_PyCompile_AstOptimize(mod_ty mod,PyObject * filename,PyCompilerFlags * cf,int optimize,PyArena * arena)458 _PyCompile_AstOptimize(mod_ty mod, PyObject *filename, PyCompilerFlags *cf,
459 int optimize, PyArena *arena)
460 {
461 _PyFutureFeatures future;
462 if (!_PyFuture_FromAST(mod, filename, &future)) {
463 return -1;
464 }
465 int flags = future.ff_features | cf->cf_flags;
466 if (optimize == -1) {
467 optimize = _Py_GetConfig()->optimization_level;
468 }
469 if (!_PyAST_Optimize(mod, arena, optimize, flags)) {
470 return -1;
471 }
472 return 0;
473 }
474
475 static void
compiler_free(struct compiler * c)476 compiler_free(struct compiler *c)
477 {
478 if (c->c_st)
479 _PySymtable_Free(c->c_st);
480 Py_XDECREF(c->c_filename);
481 Py_XDECREF(c->c_const_cache);
482 Py_XDECREF(c->c_stack);
483 PyMem_Free(c);
484 }
485
486 static PyObject *
list2dict(PyObject * list)487 list2dict(PyObject *list)
488 {
489 Py_ssize_t i, n;
490 PyObject *v, *k;
491 PyObject *dict = PyDict_New();
492 if (!dict) return NULL;
493
494 n = PyList_Size(list);
495 for (i = 0; i < n; i++) {
496 v = PyLong_FromSsize_t(i);
497 if (!v) {
498 Py_DECREF(dict);
499 return NULL;
500 }
501 k = PyList_GET_ITEM(list, i);
502 if (PyDict_SetItem(dict, k, v) < 0) {
503 Py_DECREF(v);
504 Py_DECREF(dict);
505 return NULL;
506 }
507 Py_DECREF(v);
508 }
509 return dict;
510 }
511
512 /* Return new dict containing names from src that match scope(s).
513
514 src is a symbol table dictionary. If the scope of a name matches
515 either scope_type or flag is set, insert it into the new dict. The
516 values are integers, starting at offset and increasing by one for
517 each key.
518 */
519
520 static PyObject *
dictbytype(PyObject * src,int scope_type,int flag,Py_ssize_t offset)521 dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
522 {
523 Py_ssize_t i = offset, scope, num_keys, key_i;
524 PyObject *k, *v, *dest = PyDict_New();
525 PyObject *sorted_keys;
526
527 assert(offset >= 0);
528 if (dest == NULL)
529 return NULL;
530
531 /* Sort the keys so that we have a deterministic order on the indexes
532 saved in the returned dictionary. These indexes are used as indexes
533 into the free and cell var storage. Therefore if they aren't
534 deterministic, then the generated bytecode is not deterministic.
535 */
536 sorted_keys = PyDict_Keys(src);
537 if (sorted_keys == NULL)
538 return NULL;
539 if (PyList_Sort(sorted_keys) != 0) {
540 Py_DECREF(sorted_keys);
541 return NULL;
542 }
543 num_keys = PyList_GET_SIZE(sorted_keys);
544
545 for (key_i = 0; key_i < num_keys; key_i++) {
546 /* XXX this should probably be a macro in symtable.h */
547 long vi;
548 k = PyList_GET_ITEM(sorted_keys, key_i);
549 v = PyDict_GetItemWithError(src, k);
550 assert(v && PyLong_Check(v));
551 vi = PyLong_AS_LONG(v);
552 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
553
554 if (scope == scope_type || vi & flag) {
555 PyObject *item = PyLong_FromSsize_t(i);
556 if (item == NULL) {
557 Py_DECREF(sorted_keys);
558 Py_DECREF(dest);
559 return NULL;
560 }
561 i++;
562 if (PyDict_SetItem(dest, k, item) < 0) {
563 Py_DECREF(sorted_keys);
564 Py_DECREF(item);
565 Py_DECREF(dest);
566 return NULL;
567 }
568 Py_DECREF(item);
569 }
570 }
571 Py_DECREF(sorted_keys);
572 return dest;
573 }
574
575 static void
compiler_unit_free(struct compiler_unit * u)576 compiler_unit_free(struct compiler_unit *u)
577 {
578 Py_CLEAR(u->u_instr_sequence);
579 Py_CLEAR(u->u_ste);
580 Py_CLEAR(u->u_metadata.u_name);
581 Py_CLEAR(u->u_metadata.u_qualname);
582 Py_CLEAR(u->u_metadata.u_consts);
583 Py_CLEAR(u->u_metadata.u_names);
584 Py_CLEAR(u->u_metadata.u_varnames);
585 Py_CLEAR(u->u_metadata.u_freevars);
586 Py_CLEAR(u->u_metadata.u_cellvars);
587 Py_CLEAR(u->u_metadata.u_fasthidden);
588 Py_CLEAR(u->u_private);
589 Py_CLEAR(u->u_static_attributes);
590 PyMem_Free(u);
591 }
592
593 static int
compiler_maybe_add_static_attribute_to_class(struct compiler * c,expr_ty e)594 compiler_maybe_add_static_attribute_to_class(struct compiler *c, expr_ty e)
595 {
596 assert(e->kind == Attribute_kind);
597 expr_ty attr_value = e->v.Attribute.value;
598 if (attr_value->kind != Name_kind ||
599 e->v.Attribute.ctx != Store ||
600 !_PyUnicode_EqualToASCIIString(attr_value->v.Name.id, "self"))
601 {
602 return SUCCESS;
603 }
604 Py_ssize_t stack_size = PyList_GET_SIZE(c->c_stack);
605 for (Py_ssize_t i = stack_size - 1; i >= 0; i--) {
606 PyObject *capsule = PyList_GET_ITEM(c->c_stack, i);
607 struct compiler_unit *u = (struct compiler_unit *)PyCapsule_GetPointer(
608 capsule, CAPSULE_NAME);
609 assert(u);
610 if (u->u_scope_type == COMPILER_SCOPE_CLASS) {
611 assert(u->u_static_attributes);
612 RETURN_IF_ERROR(PySet_Add(u->u_static_attributes, e->v.Attribute.attr));
613 break;
614 }
615 }
616 return SUCCESS;
617 }
618
619 static int
compiler_set_qualname(struct compiler * c)620 compiler_set_qualname(struct compiler *c)
621 {
622 Py_ssize_t stack_size;
623 struct compiler_unit *u = c->u;
624 PyObject *name, *base;
625
626 base = NULL;
627 stack_size = PyList_GET_SIZE(c->c_stack);
628 assert(stack_size >= 1);
629 if (stack_size > 1) {
630 int scope, force_global = 0;
631 struct compiler_unit *parent;
632 PyObject *mangled, *capsule;
633
634 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
635 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
636 assert(parent);
637 if (parent->u_scope_type == COMPILER_SCOPE_TYPEPARAMS) {
638 /* The parent is a type parameter scope, so we need to
639 look at the grandparent. */
640 if (stack_size == 2) {
641 // If we're immediately within the module, we can skip
642 // the rest and just set the qualname to be the same as name.
643 u->u_metadata.u_qualname = Py_NewRef(u->u_metadata.u_name);
644 return SUCCESS;
645 }
646 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 2);
647 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
648 assert(parent);
649 }
650
651 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
652 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
653 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
654 assert(u->u_metadata.u_name);
655 mangled = _Py_Mangle(parent->u_private, u->u_metadata.u_name);
656 if (!mangled) {
657 return ERROR;
658 }
659
660 scope = _PyST_GetScope(parent->u_ste, mangled);
661 Py_DECREF(mangled);
662 assert(scope != GLOBAL_IMPLICIT);
663 if (scope == GLOBAL_EXPLICIT)
664 force_global = 1;
665 }
666
667 if (!force_global) {
668 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
669 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
670 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA)
671 {
672 _Py_DECLARE_STR(dot_locals, ".<locals>");
673 base = PyUnicode_Concat(parent->u_metadata.u_qualname,
674 &_Py_STR(dot_locals));
675 if (base == NULL) {
676 return ERROR;
677 }
678 }
679 else {
680 base = Py_NewRef(parent->u_metadata.u_qualname);
681 }
682 }
683 }
684
685 if (base != NULL) {
686 name = PyUnicode_Concat(base, _Py_LATIN1_CHR('.'));
687 Py_DECREF(base);
688 if (name == NULL) {
689 return ERROR;
690 }
691 PyUnicode_Append(&name, u->u_metadata.u_name);
692 if (name == NULL) {
693 return ERROR;
694 }
695 }
696 else {
697 name = Py_NewRef(u->u_metadata.u_name);
698 }
699 u->u_metadata.u_qualname = name;
700
701 return SUCCESS;
702 }
703
704 /* Return the stack effect of opcode with argument oparg.
705
706 Some opcodes have different stack effect when jump to the target and
707 when not jump. The 'jump' parameter specifies the case:
708
709 * 0 -- when not jump
710 * 1 -- when jump
711 * -1 -- maximal
712 */
713 static int
stack_effect(int opcode,int oparg,int jump)714 stack_effect(int opcode, int oparg, int jump)
715 {
716 if (0 <= opcode && opcode <= MAX_REAL_OPCODE) {
717 if (_PyOpcode_Deopt[opcode] != opcode) {
718 // Specialized instructions are not supported.
719 return PY_INVALID_STACK_EFFECT;
720 }
721 int popped = _PyOpcode_num_popped(opcode, oparg);
722 int pushed = _PyOpcode_num_pushed(opcode, oparg);
723 if (popped < 0 || pushed < 0) {
724 return PY_INVALID_STACK_EFFECT;
725 }
726 return pushed - popped;
727 }
728
729 // Pseudo ops
730 switch (opcode) {
731 case POP_BLOCK:
732 case JUMP:
733 case JUMP_NO_INTERRUPT:
734 return 0;
735
736 case EXIT_INIT_CHECK:
737 return -1;
738
739 /* Exception handling pseudo-instructions */
740 case SETUP_FINALLY:
741 /* 0 in the normal flow.
742 * Restore the stack position and push 1 value before jumping to
743 * the handler if an exception be raised. */
744 return jump ? 1 : 0;
745 case SETUP_CLEANUP:
746 /* As SETUP_FINALLY, but pushes lasti as well */
747 return jump ? 2 : 0;
748 case SETUP_WITH:
749 /* 0 in the normal flow.
750 * Restore the stack position to the position before the result
751 * of __(a)enter__ and push 2 values before jumping to the handler
752 * if an exception be raised. */
753 return jump ? 1 : 0;
754
755 case STORE_FAST_MAYBE_NULL:
756 return -1;
757 case LOAD_CLOSURE:
758 return 1;
759 case LOAD_METHOD:
760 return 1;
761 case LOAD_SUPER_METHOD:
762 case LOAD_ZERO_SUPER_METHOD:
763 case LOAD_ZERO_SUPER_ATTR:
764 return -1;
765 default:
766 return PY_INVALID_STACK_EFFECT;
767 }
768
769 return PY_INVALID_STACK_EFFECT; /* not reachable */
770 }
771
772 int
PyCompile_OpcodeStackEffectWithJump(int opcode,int oparg,int jump)773 PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
774 {
775 return stack_effect(opcode, oparg, jump);
776 }
777
778 int
PyCompile_OpcodeStackEffect(int opcode,int oparg)779 PyCompile_OpcodeStackEffect(int opcode, int oparg)
780 {
781 return stack_effect(opcode, oparg, -1);
782 }
783
784 int
_PyCompile_OpcodeIsValid(int opcode)785 _PyCompile_OpcodeIsValid(int opcode)
786 {
787 return IS_VALID_OPCODE(opcode);
788 }
789
790 int
_PyCompile_OpcodeHasArg(int opcode)791 _PyCompile_OpcodeHasArg(int opcode)
792 {
793 return OPCODE_HAS_ARG(opcode);
794 }
795
796 int
_PyCompile_OpcodeHasConst(int opcode)797 _PyCompile_OpcodeHasConst(int opcode)
798 {
799 return OPCODE_HAS_CONST(opcode);
800 }
801
802 int
_PyCompile_OpcodeHasName(int opcode)803 _PyCompile_OpcodeHasName(int opcode)
804 {
805 return OPCODE_HAS_NAME(opcode);
806 }
807
808 int
_PyCompile_OpcodeHasJump(int opcode)809 _PyCompile_OpcodeHasJump(int opcode)
810 {
811 return OPCODE_HAS_JUMP(opcode);
812 }
813
814 int
_PyCompile_OpcodeHasFree(int opcode)815 _PyCompile_OpcodeHasFree(int opcode)
816 {
817 return OPCODE_HAS_FREE(opcode);
818 }
819
820 int
_PyCompile_OpcodeHasLocal(int opcode)821 _PyCompile_OpcodeHasLocal(int opcode)
822 {
823 return OPCODE_HAS_LOCAL(opcode);
824 }
825
826 int
_PyCompile_OpcodeHasExc(int opcode)827 _PyCompile_OpcodeHasExc(int opcode)
828 {
829 return IS_BLOCK_PUSH_OPCODE(opcode);
830 }
831
832 static int
codegen_addop_noarg(instr_sequence * seq,int opcode,location loc)833 codegen_addop_noarg(instr_sequence *seq, int opcode, location loc)
834 {
835 assert(!OPCODE_HAS_ARG(opcode));
836 assert(!IS_ASSEMBLER_OPCODE(opcode));
837 return _PyInstructionSequence_Addop(seq, opcode, 0, loc);
838 }
839
840 static Py_ssize_t
dict_add_o(PyObject * dict,PyObject * o)841 dict_add_o(PyObject *dict, PyObject *o)
842 {
843 PyObject *v;
844 Py_ssize_t arg;
845
846 if (PyDict_GetItemRef(dict, o, &v) < 0) {
847 return ERROR;
848 }
849 if (!v) {
850 arg = PyDict_GET_SIZE(dict);
851 v = PyLong_FromSsize_t(arg);
852 if (!v) {
853 return ERROR;
854 }
855 if (PyDict_SetItem(dict, o, v) < 0) {
856 Py_DECREF(v);
857 return ERROR;
858 }
859 }
860 else
861 arg = PyLong_AsLong(v);
862 Py_DECREF(v);
863 return arg;
864 }
865
866 // Merge const *o* recursively and return constant key object.
867 static PyObject*
merge_consts_recursive(PyObject * const_cache,PyObject * o)868 merge_consts_recursive(PyObject *const_cache, PyObject *o)
869 {
870 assert(PyDict_CheckExact(const_cache));
871 // None and Ellipsis are immortal objects, and key is the singleton.
872 // No need to merge object and key.
873 if (o == Py_None || o == Py_Ellipsis) {
874 return o;
875 }
876
877 PyObject *key = _PyCode_ConstantKey(o);
878 if (key == NULL) {
879 return NULL;
880 }
881
882 PyObject *t;
883 int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
884 if (res != 0) {
885 // o was not inserted into const_cache. t is either the existing value
886 // or NULL (on error).
887 Py_DECREF(key);
888 return t;
889 }
890 Py_DECREF(t);
891
892 // We registered o in const_cache.
893 // When o is a tuple or frozenset, we want to merge its
894 // items too.
895 if (PyTuple_CheckExact(o)) {
896 Py_ssize_t len = PyTuple_GET_SIZE(o);
897 for (Py_ssize_t i = 0; i < len; i++) {
898 PyObject *item = PyTuple_GET_ITEM(o, i);
899 PyObject *u = merge_consts_recursive(const_cache, item);
900 if (u == NULL) {
901 Py_DECREF(key);
902 return NULL;
903 }
904
905 // See _PyCode_ConstantKey()
906 PyObject *v; // borrowed
907 if (PyTuple_CheckExact(u)) {
908 v = PyTuple_GET_ITEM(u, 1);
909 }
910 else {
911 v = u;
912 }
913 if (v != item) {
914 PyTuple_SET_ITEM(o, i, Py_NewRef(v));
915 Py_DECREF(item);
916 }
917
918 Py_DECREF(u);
919 }
920 }
921 else if (PyFrozenSet_CheckExact(o)) {
922 // *key* is tuple. And its first item is frozenset of
923 // constant keys.
924 // See _PyCode_ConstantKey() for detail.
925 assert(PyTuple_CheckExact(key));
926 assert(PyTuple_GET_SIZE(key) == 2);
927
928 Py_ssize_t len = PySet_GET_SIZE(o);
929 if (len == 0) { // empty frozenset should not be re-created.
930 return key;
931 }
932 PyObject *tuple = PyTuple_New(len);
933 if (tuple == NULL) {
934 Py_DECREF(key);
935 return NULL;
936 }
937 Py_ssize_t i = 0, pos = 0;
938 PyObject *item;
939 Py_hash_t hash;
940 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
941 PyObject *k = merge_consts_recursive(const_cache, item);
942 if (k == NULL) {
943 Py_DECREF(tuple);
944 Py_DECREF(key);
945 return NULL;
946 }
947 PyObject *u;
948 if (PyTuple_CheckExact(k)) {
949 u = Py_NewRef(PyTuple_GET_ITEM(k, 1));
950 Py_DECREF(k);
951 }
952 else {
953 u = k;
954 }
955 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
956 i++;
957 }
958
959 // Instead of rewriting o, we create new frozenset and embed in the
960 // key tuple. Caller should get merged frozenset from the key tuple.
961 PyObject *new = PyFrozenSet_New(tuple);
962 Py_DECREF(tuple);
963 if (new == NULL) {
964 Py_DECREF(key);
965 return NULL;
966 }
967 assert(PyTuple_GET_ITEM(key, 1) == o);
968 Py_DECREF(o);
969 PyTuple_SET_ITEM(key, 1, new);
970 }
971
972 return key;
973 }
974
975 static Py_ssize_t
compiler_add_const(PyObject * const_cache,struct compiler_unit * u,PyObject * o)976 compiler_add_const(PyObject *const_cache, struct compiler_unit *u, PyObject *o)
977 {
978 assert(PyDict_CheckExact(const_cache));
979 PyObject *key = merge_consts_recursive(const_cache, o);
980 if (key == NULL) {
981 return ERROR;
982 }
983
984 Py_ssize_t arg = dict_add_o(u->u_metadata.u_consts, key);
985 Py_DECREF(key);
986 return arg;
987 }
988
989 static int
compiler_addop_load_const(PyObject * const_cache,struct compiler_unit * u,location loc,PyObject * o)990 compiler_addop_load_const(PyObject *const_cache, struct compiler_unit *u, location loc, PyObject *o)
991 {
992 Py_ssize_t arg = compiler_add_const(const_cache, u, o);
993 if (arg < 0) {
994 return ERROR;
995 }
996 return codegen_addop_i(u->u_instr_sequence, LOAD_CONST, arg, loc);
997 }
998
999 static int
compiler_addop_o(struct compiler_unit * u,location loc,int opcode,PyObject * dict,PyObject * o)1000 compiler_addop_o(struct compiler_unit *u, location loc,
1001 int opcode, PyObject *dict, PyObject *o)
1002 {
1003 Py_ssize_t arg = dict_add_o(dict, o);
1004 if (arg < 0) {
1005 return ERROR;
1006 }
1007 return codegen_addop_i(u->u_instr_sequence, opcode, arg, loc);
1008 }
1009
1010 static int
compiler_addop_name(struct compiler_unit * u,location loc,int opcode,PyObject * dict,PyObject * o)1011 compiler_addop_name(struct compiler_unit *u, location loc,
1012 int opcode, PyObject *dict, PyObject *o)
1013 {
1014 PyObject *mangled = _Py_MaybeMangle(u->u_private, u->u_ste, o);
1015 if (!mangled) {
1016 return ERROR;
1017 }
1018 Py_ssize_t arg = dict_add_o(dict, mangled);
1019 Py_DECREF(mangled);
1020 if (arg < 0) {
1021 return ERROR;
1022 }
1023 if (opcode == LOAD_ATTR) {
1024 arg <<= 1;
1025 }
1026 if (opcode == LOAD_METHOD) {
1027 assert(is_pseudo_target(LOAD_METHOD, LOAD_ATTR));
1028 opcode = LOAD_ATTR;
1029 arg <<= 1;
1030 arg |= 1;
1031 }
1032 if (opcode == LOAD_SUPER_ATTR) {
1033 arg <<= 2;
1034 arg |= 2;
1035 }
1036 if (opcode == LOAD_SUPER_METHOD) {
1037 assert(is_pseudo_target(LOAD_SUPER_METHOD, LOAD_SUPER_ATTR));
1038 opcode = LOAD_SUPER_ATTR;
1039 arg <<= 2;
1040 arg |= 3;
1041 }
1042 if (opcode == LOAD_ZERO_SUPER_ATTR) {
1043 assert(is_pseudo_target(LOAD_ZERO_SUPER_ATTR, LOAD_SUPER_ATTR));
1044 opcode = LOAD_SUPER_ATTR;
1045 arg <<= 2;
1046 }
1047 if (opcode == LOAD_ZERO_SUPER_METHOD) {
1048 assert(is_pseudo_target(LOAD_ZERO_SUPER_METHOD, LOAD_SUPER_ATTR));
1049 opcode = LOAD_SUPER_ATTR;
1050 arg <<= 2;
1051 arg |= 1;
1052 }
1053 return codegen_addop_i(u->u_instr_sequence, opcode, arg, loc);
1054 }
1055
1056 /* Add an opcode with an integer argument */
1057 static int
codegen_addop_i(instr_sequence * seq,int opcode,Py_ssize_t oparg,location loc)1058 codegen_addop_i(instr_sequence *seq, int opcode, Py_ssize_t oparg, location loc)
1059 {
1060 /* oparg value is unsigned, but a signed C int is usually used to store
1061 it in the C code (like Python/ceval.c).
1062
1063 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1064
1065 The argument of a concrete bytecode instruction is limited to 8-bit.
1066 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1067
1068 int oparg_ = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
1069 assert(!IS_ASSEMBLER_OPCODE(opcode));
1070 return _PyInstructionSequence_Addop(seq, opcode, oparg_, loc);
1071 }
1072
1073 static int
codegen_addop_j(instr_sequence * seq,location loc,int opcode,jump_target_label target)1074 codegen_addop_j(instr_sequence *seq, location loc,
1075 int opcode, jump_target_label target)
1076 {
1077 assert(IS_LABEL(target));
1078 assert(OPCODE_HAS_JUMP(opcode) || IS_BLOCK_PUSH_OPCODE(opcode));
1079 assert(!IS_ASSEMBLER_OPCODE(opcode));
1080 return _PyInstructionSequence_Addop(seq, opcode, target.id, loc);
1081 }
1082
1083 #define RETURN_IF_ERROR_IN_SCOPE(C, CALL) { \
1084 if ((CALL) < 0) { \
1085 compiler_exit_scope((C)); \
1086 return ERROR; \
1087 } \
1088 }
1089
1090 #define ADDOP(C, LOC, OP) \
1091 RETURN_IF_ERROR(codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
1092
1093 #define ADDOP_IN_SCOPE(C, LOC, OP) RETURN_IF_ERROR_IN_SCOPE((C), codegen_addop_noarg(INSTR_SEQUENCE(C), (OP), (LOC)))
1094
1095 #define ADDOP_LOAD_CONST(C, LOC, O) \
1096 RETURN_IF_ERROR(compiler_addop_load_const((C)->c_const_cache, (C)->u, (LOC), (O)))
1097
1098 /* Same as ADDOP_LOAD_CONST, but steals a reference. */
1099 #define ADDOP_LOAD_CONST_NEW(C, LOC, O) { \
1100 PyObject *__new_const = (O); \
1101 if (__new_const == NULL) { \
1102 return ERROR; \
1103 } \
1104 if (compiler_addop_load_const((C)->c_const_cache, (C)->u, (LOC), __new_const) < 0) { \
1105 Py_DECREF(__new_const); \
1106 return ERROR; \
1107 } \
1108 Py_DECREF(__new_const); \
1109 }
1110
1111 #define ADDOP_N(C, LOC, OP, O, TYPE) { \
1112 assert(!OPCODE_HAS_CONST(OP)); /* use ADDOP_LOAD_CONST_NEW */ \
1113 if (compiler_addop_o((C)->u, (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)) < 0) { \
1114 Py_DECREF((O)); \
1115 return ERROR; \
1116 } \
1117 Py_DECREF((O)); \
1118 }
1119
1120 #define ADDOP_NAME(C, LOC, OP, O, TYPE) \
1121 RETURN_IF_ERROR(compiler_addop_name((C)->u, (LOC), (OP), (C)->u->u_metadata.u_ ## TYPE, (O)))
1122
1123 #define ADDOP_I(C, LOC, OP, O) \
1124 RETURN_IF_ERROR(codegen_addop_i(INSTR_SEQUENCE(C), (OP), (O), (LOC)))
1125
1126 #define ADDOP_JUMP(C, LOC, OP, O) \
1127 RETURN_IF_ERROR(codegen_addop_j(INSTR_SEQUENCE(C), (LOC), (OP), (O)))
1128
1129 #define ADDOP_COMPARE(C, LOC, CMP) \
1130 RETURN_IF_ERROR(compiler_addcompare((C), (LOC), (cmpop_ty)(CMP)))
1131
1132 #define ADDOP_BINARY(C, LOC, BINOP) \
1133 RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), false))
1134
1135 #define ADDOP_INPLACE(C, LOC, BINOP) \
1136 RETURN_IF_ERROR(addop_binary((C), (LOC), (BINOP), true))
1137
1138 #define ADD_YIELD_FROM(C, LOC, await) \
1139 RETURN_IF_ERROR(compiler_add_yield_from((C), (LOC), (await)))
1140
1141 #define POP_EXCEPT_AND_RERAISE(C, LOC) \
1142 RETURN_IF_ERROR(compiler_pop_except_and_reraise((C), (LOC)))
1143
1144 #define ADDOP_YIELD(C, LOC) \
1145 RETURN_IF_ERROR(addop_yield((C), (LOC)))
1146
1147 /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1148 the ASDL name to synthesize the name of the C type and the visit function.
1149 */
1150
1151 #define VISIT(C, TYPE, V) \
1152 RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), (V)));
1153
1154 #define VISIT_IN_SCOPE(C, TYPE, V) \
1155 RETURN_IF_ERROR_IN_SCOPE((C), compiler_visit_ ## TYPE((C), (V)))
1156
1157 #define VISIT_SEQ(C, TYPE, SEQ) { \
1158 int _i; \
1159 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1160 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1161 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1162 RETURN_IF_ERROR(compiler_visit_ ## TYPE((C), elt)); \
1163 } \
1164 }
1165
1166 #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
1167 int _i; \
1168 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
1169 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1170 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1171 if (compiler_visit_ ## TYPE((C), elt) < 0) { \
1172 compiler_exit_scope(C); \
1173 return ERROR; \
1174 } \
1175 } \
1176 }
1177
1178
1179 static int
compiler_enter_scope(struct compiler * c,identifier name,int scope_type,void * key,int lineno)1180 compiler_enter_scope(struct compiler *c, identifier name,
1181 int scope_type, void *key, int lineno)
1182 {
1183 location loc = LOCATION(lineno, lineno, 0, 0);
1184
1185 struct compiler_unit *u;
1186
1187 u = (struct compiler_unit *)PyMem_Calloc(1, sizeof(struct compiler_unit));
1188 if (!u) {
1189 PyErr_NoMemory();
1190 return ERROR;
1191 }
1192 u->u_scope_type = scope_type;
1193 u->u_metadata.u_argcount = 0;
1194 u->u_metadata.u_posonlyargcount = 0;
1195 u->u_metadata.u_kwonlyargcount = 0;
1196 u->u_ste = _PySymtable_Lookup(c->c_st, key);
1197 if (!u->u_ste) {
1198 compiler_unit_free(u);
1199 return ERROR;
1200 }
1201 u->u_metadata.u_name = Py_NewRef(name);
1202 u->u_metadata.u_varnames = list2dict(u->u_ste->ste_varnames);
1203 if (!u->u_metadata.u_varnames) {
1204 compiler_unit_free(u);
1205 return ERROR;
1206 }
1207 u->u_metadata.u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, DEF_COMP_CELL, 0);
1208 if (!u->u_metadata.u_cellvars) {
1209 compiler_unit_free(u);
1210 return ERROR;
1211 }
1212 if (u->u_ste->ste_needs_class_closure) {
1213 /* Cook up an implicit __class__ cell. */
1214 Py_ssize_t res;
1215 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
1216 res = dict_add_o(u->u_metadata.u_cellvars, &_Py_ID(__class__));
1217 if (res < 0) {
1218 compiler_unit_free(u);
1219 return ERROR;
1220 }
1221 }
1222 if (u->u_ste->ste_needs_classdict) {
1223 /* Cook up an implicit __classdict__ cell. */
1224 Py_ssize_t res;
1225 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
1226 res = dict_add_o(u->u_metadata.u_cellvars, &_Py_ID(__classdict__));
1227 if (res < 0) {
1228 compiler_unit_free(u);
1229 return ERROR;
1230 }
1231 }
1232
1233 u->u_metadata.u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
1234 PyDict_GET_SIZE(u->u_metadata.u_cellvars));
1235 if (!u->u_metadata.u_freevars) {
1236 compiler_unit_free(u);
1237 return ERROR;
1238 }
1239
1240 u->u_metadata.u_fasthidden = PyDict_New();
1241 if (!u->u_metadata.u_fasthidden) {
1242 compiler_unit_free(u);
1243 return ERROR;
1244 }
1245
1246 u->u_nfblocks = 0;
1247 u->u_in_inlined_comp = 0;
1248 u->u_metadata.u_firstlineno = lineno;
1249 u->u_metadata.u_consts = PyDict_New();
1250 if (!u->u_metadata.u_consts) {
1251 compiler_unit_free(u);
1252 return ERROR;
1253 }
1254 u->u_metadata.u_names = PyDict_New();
1255 if (!u->u_metadata.u_names) {
1256 compiler_unit_free(u);
1257 return ERROR;
1258 }
1259
1260 u->u_private = NULL;
1261 if (scope_type == COMPILER_SCOPE_CLASS) {
1262 u->u_static_attributes = PySet_New(0);
1263 if (!u->u_static_attributes) {
1264 compiler_unit_free(u);
1265 return ERROR;
1266 }
1267 }
1268 else {
1269 u->u_static_attributes = NULL;
1270 }
1271
1272 u->u_instr_sequence = (instr_sequence*)_PyInstructionSequence_New();
1273
1274 /* Push the old compiler_unit on the stack. */
1275 if (c->u) {
1276 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
1277 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
1278 Py_XDECREF(capsule);
1279 compiler_unit_free(u);
1280 return ERROR;
1281 }
1282 Py_DECREF(capsule);
1283 u->u_private = Py_XNewRef(c->u->u_private);
1284 }
1285 c->u = u;
1286
1287 c->c_nestlevel++;
1288
1289 if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1290 loc.lineno = 0;
1291 }
1292 else {
1293 RETURN_IF_ERROR(compiler_set_qualname(c));
1294 }
1295 ADDOP_I(c, loc, RESUME, RESUME_AT_FUNC_START);
1296
1297 if (u->u_scope_type == COMPILER_SCOPE_MODULE) {
1298 loc.lineno = -1;
1299 }
1300 return SUCCESS;
1301 }
1302
1303 static void
compiler_exit_scope(struct compiler * c)1304 compiler_exit_scope(struct compiler *c)
1305 {
1306 // Don't call PySequence_DelItem() with an exception raised
1307 PyObject *exc = PyErr_GetRaisedException();
1308
1309 instr_sequence *nested_seq = NULL;
1310 if (c->c_save_nested_seqs) {
1311 nested_seq = c->u->u_instr_sequence;
1312 Py_INCREF(nested_seq);
1313 }
1314 c->c_nestlevel--;
1315 compiler_unit_free(c->u);
1316 /* Restore c->u to the parent unit. */
1317 Py_ssize_t n = PyList_GET_SIZE(c->c_stack) - 1;
1318 if (n >= 0) {
1319 PyObject *capsule = PyList_GET_ITEM(c->c_stack, n);
1320 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
1321 assert(c->u);
1322 /* we are deleting from a list so this really shouldn't fail */
1323 if (PySequence_DelItem(c->c_stack, n) < 0) {
1324 PyErr_FormatUnraisable("Exception ignored on removing "
1325 "the last compiler stack item");
1326 }
1327 if (nested_seq != NULL) {
1328 if (_PyInstructionSequence_AddNested(c->u->u_instr_sequence, nested_seq) < 0) {
1329 PyErr_FormatUnraisable("Exception ignored on appending "
1330 "nested instruction sequence");
1331 }
1332 }
1333 }
1334 else {
1335 c->u = NULL;
1336 }
1337 Py_XDECREF(nested_seq);
1338
1339 PyErr_SetRaisedException(exc);
1340 }
1341
1342 /* Search if variable annotations are present statically in a block. */
1343
1344 static bool
find_ann(asdl_stmt_seq * stmts)1345 find_ann(asdl_stmt_seq *stmts)
1346 {
1347 int i, j, res = 0;
1348 stmt_ty st;
1349
1350 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1351 st = (stmt_ty)asdl_seq_GET(stmts, i);
1352 switch (st->kind) {
1353 case AnnAssign_kind:
1354 return true;
1355 case For_kind:
1356 res = find_ann(st->v.For.body) ||
1357 find_ann(st->v.For.orelse);
1358 break;
1359 case AsyncFor_kind:
1360 res = find_ann(st->v.AsyncFor.body) ||
1361 find_ann(st->v.AsyncFor.orelse);
1362 break;
1363 case While_kind:
1364 res = find_ann(st->v.While.body) ||
1365 find_ann(st->v.While.orelse);
1366 break;
1367 case If_kind:
1368 res = find_ann(st->v.If.body) ||
1369 find_ann(st->v.If.orelse);
1370 break;
1371 case With_kind:
1372 res = find_ann(st->v.With.body);
1373 break;
1374 case AsyncWith_kind:
1375 res = find_ann(st->v.AsyncWith.body);
1376 break;
1377 case Try_kind:
1378 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1379 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1380 st->v.Try.handlers, j);
1381 if (find_ann(handler->v.ExceptHandler.body)) {
1382 return true;
1383 }
1384 }
1385 res = find_ann(st->v.Try.body) ||
1386 find_ann(st->v.Try.finalbody) ||
1387 find_ann(st->v.Try.orelse);
1388 break;
1389 case TryStar_kind:
1390 for (j = 0; j < asdl_seq_LEN(st->v.TryStar.handlers); j++) {
1391 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1392 st->v.TryStar.handlers, j);
1393 if (find_ann(handler->v.ExceptHandler.body)) {
1394 return true;
1395 }
1396 }
1397 res = find_ann(st->v.TryStar.body) ||
1398 find_ann(st->v.TryStar.finalbody) ||
1399 find_ann(st->v.TryStar.orelse);
1400 break;
1401 case Match_kind:
1402 for (j = 0; j < asdl_seq_LEN(st->v.Match.cases); j++) {
1403 match_case_ty match_case = (match_case_ty)asdl_seq_GET(
1404 st->v.Match.cases, j);
1405 if (find_ann(match_case->body)) {
1406 return true;
1407 }
1408 }
1409 break;
1410 default:
1411 res = false;
1412 break;
1413 }
1414 if (res) {
1415 break;
1416 }
1417 }
1418 return res;
1419 }
1420
1421 /*
1422 * Frame block handling functions
1423 */
1424
1425 static int
compiler_push_fblock(struct compiler * c,location loc,enum fblocktype t,jump_target_label block_label,jump_target_label exit,void * datum)1426 compiler_push_fblock(struct compiler *c, location loc,
1427 enum fblocktype t, jump_target_label block_label,
1428 jump_target_label exit, void *datum)
1429 {
1430 struct fblockinfo *f;
1431 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1432 return compiler_error(c, loc, "too many statically nested blocks");
1433 }
1434 f = &c->u->u_fblock[c->u->u_nfblocks++];
1435 f->fb_type = t;
1436 f->fb_block = block_label;
1437 f->fb_loc = loc;
1438 f->fb_exit = exit;
1439 f->fb_datum = datum;
1440 return SUCCESS;
1441 }
1442
1443 static void
compiler_pop_fblock(struct compiler * c,enum fblocktype t,jump_target_label block_label)1444 compiler_pop_fblock(struct compiler *c, enum fblocktype t, jump_target_label block_label)
1445 {
1446 struct compiler_unit *u = c->u;
1447 assert(u->u_nfblocks > 0);
1448 u->u_nfblocks--;
1449 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1450 assert(SAME_LABEL(u->u_fblock[u->u_nfblocks].fb_block, block_label));
1451 }
1452
1453 static int
compiler_call_exit_with_nones(struct compiler * c,location loc)1454 compiler_call_exit_with_nones(struct compiler *c, location loc)
1455 {
1456 ADDOP_LOAD_CONST(c, loc, Py_None);
1457 ADDOP_LOAD_CONST(c, loc, Py_None);
1458 ADDOP_LOAD_CONST(c, loc, Py_None);
1459 ADDOP_I(c, loc, CALL, 2);
1460 return SUCCESS;
1461 }
1462
1463 static int
compiler_add_yield_from(struct compiler * c,location loc,int await)1464 compiler_add_yield_from(struct compiler *c, location loc, int await)
1465 {
1466 NEW_JUMP_TARGET_LABEL(c, send);
1467 NEW_JUMP_TARGET_LABEL(c, fail);
1468 NEW_JUMP_TARGET_LABEL(c, exit);
1469
1470 USE_LABEL(c, send);
1471 ADDOP_JUMP(c, loc, SEND, exit);
1472 // Set up a virtual try/except to handle when StopIteration is raised during
1473 // a close or throw call. The only way YIELD_VALUE raises if they do!
1474 ADDOP_JUMP(c, loc, SETUP_FINALLY, fail);
1475 ADDOP_I(c, loc, YIELD_VALUE, 1);
1476 ADDOP(c, NO_LOCATION, POP_BLOCK);
1477 ADDOP_I(c, loc, RESUME, await ? RESUME_AFTER_AWAIT : RESUME_AFTER_YIELD_FROM);
1478 ADDOP_JUMP(c, loc, JUMP_NO_INTERRUPT, send);
1479
1480 USE_LABEL(c, fail);
1481 ADDOP(c, loc, CLEANUP_THROW);
1482
1483 USE_LABEL(c, exit);
1484 ADDOP(c, loc, END_SEND);
1485 return SUCCESS;
1486 }
1487
1488 static int
compiler_pop_except_and_reraise(struct compiler * c,location loc)1489 compiler_pop_except_and_reraise(struct compiler *c, location loc)
1490 {
1491 /* Stack contents
1492 * [exc_info, lasti, exc] COPY 3
1493 * [exc_info, lasti, exc, exc_info] POP_EXCEPT
1494 * [exc_info, lasti, exc] RERAISE 1
1495 * (exception_unwind clears the stack)
1496 */
1497
1498 ADDOP_I(c, loc, COPY, 3);
1499 ADDOP(c, loc, POP_EXCEPT);
1500 ADDOP_I(c, loc, RERAISE, 1);
1501 return SUCCESS;
1502 }
1503
1504 /* Unwind a frame block. If preserve_tos is true, the TOS before
1505 * popping the blocks will be restored afterwards, unless another
1506 * return, break or continue is found. In which case, the TOS will
1507 * be popped.
1508 */
1509 static int
compiler_unwind_fblock(struct compiler * c,location * ploc,struct fblockinfo * info,int preserve_tos)1510 compiler_unwind_fblock(struct compiler *c, location *ploc,
1511 struct fblockinfo *info, int preserve_tos)
1512 {
1513 switch (info->fb_type) {
1514 case WHILE_LOOP:
1515 case EXCEPTION_HANDLER:
1516 case EXCEPTION_GROUP_HANDLER:
1517 case ASYNC_COMPREHENSION_GENERATOR:
1518 case STOP_ITERATION:
1519 return SUCCESS;
1520
1521 case FOR_LOOP:
1522 /* Pop the iterator */
1523 if (preserve_tos) {
1524 ADDOP_I(c, *ploc, SWAP, 2);
1525 }
1526 ADDOP(c, *ploc, POP_TOP);
1527 return SUCCESS;
1528
1529 case TRY_EXCEPT:
1530 ADDOP(c, *ploc, POP_BLOCK);
1531 return SUCCESS;
1532
1533 case FINALLY_TRY:
1534 /* This POP_BLOCK gets the line number of the unwinding statement */
1535 ADDOP(c, *ploc, POP_BLOCK);
1536 if (preserve_tos) {
1537 RETURN_IF_ERROR(
1538 compiler_push_fblock(c, *ploc, POP_VALUE, NO_LABEL, NO_LABEL, NULL));
1539 }
1540 /* Emit the finally block */
1541 VISIT_SEQ(c, stmt, info->fb_datum);
1542 if (preserve_tos) {
1543 compiler_pop_fblock(c, POP_VALUE, NO_LABEL);
1544 }
1545 /* The finally block should appear to execute after the
1546 * statement causing the unwinding, so make the unwinding
1547 * instruction artificial */
1548 *ploc = NO_LOCATION;
1549 return SUCCESS;
1550
1551 case FINALLY_END:
1552 if (preserve_tos) {
1553 ADDOP_I(c, *ploc, SWAP, 2);
1554 }
1555 ADDOP(c, *ploc, POP_TOP); /* exc_value */
1556 if (preserve_tos) {
1557 ADDOP_I(c, *ploc, SWAP, 2);
1558 }
1559 ADDOP(c, *ploc, POP_BLOCK);
1560 ADDOP(c, *ploc, POP_EXCEPT);
1561 return SUCCESS;
1562
1563 case WITH:
1564 case ASYNC_WITH:
1565 *ploc = info->fb_loc;
1566 ADDOP(c, *ploc, POP_BLOCK);
1567 if (preserve_tos) {
1568 ADDOP_I(c, *ploc, SWAP, 2);
1569 }
1570 RETURN_IF_ERROR(compiler_call_exit_with_nones(c, *ploc));
1571 if (info->fb_type == ASYNC_WITH) {
1572 ADDOP_I(c, *ploc, GET_AWAITABLE, 2);
1573 ADDOP_LOAD_CONST(c, *ploc, Py_None);
1574 ADD_YIELD_FROM(c, *ploc, 1);
1575 }
1576 ADDOP(c, *ploc, POP_TOP);
1577 /* The exit block should appear to execute after the
1578 * statement causing the unwinding, so make the unwinding
1579 * instruction artificial */
1580 *ploc = NO_LOCATION;
1581 return SUCCESS;
1582
1583 case HANDLER_CLEANUP: {
1584 if (info->fb_datum) {
1585 ADDOP(c, *ploc, POP_BLOCK);
1586 }
1587 if (preserve_tos) {
1588 ADDOP_I(c, *ploc, SWAP, 2);
1589 }
1590 ADDOP(c, *ploc, POP_BLOCK);
1591 ADDOP(c, *ploc, POP_EXCEPT);
1592 if (info->fb_datum) {
1593 ADDOP_LOAD_CONST(c, *ploc, Py_None);
1594 RETURN_IF_ERROR(compiler_nameop(c, *ploc, info->fb_datum, Store));
1595 RETURN_IF_ERROR(compiler_nameop(c, *ploc, info->fb_datum, Del));
1596 }
1597 return SUCCESS;
1598 }
1599 case POP_VALUE: {
1600 if (preserve_tos) {
1601 ADDOP_I(c, *ploc, SWAP, 2);
1602 }
1603 ADDOP(c, *ploc, POP_TOP);
1604 return SUCCESS;
1605 }
1606 }
1607 Py_UNREACHABLE();
1608 }
1609
1610 /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1611 static int
compiler_unwind_fblock_stack(struct compiler * c,location * ploc,int preserve_tos,struct fblockinfo ** loop)1612 compiler_unwind_fblock_stack(struct compiler *c, location *ploc,
1613 int preserve_tos, struct fblockinfo **loop)
1614 {
1615 if (c->u->u_nfblocks == 0) {
1616 return SUCCESS;
1617 }
1618 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1619 if (top->fb_type == EXCEPTION_GROUP_HANDLER) {
1620 return compiler_error(
1621 c, *ploc, "'break', 'continue' and 'return' cannot appear in an except* block");
1622 }
1623 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1624 *loop = top;
1625 return SUCCESS;
1626 }
1627 struct fblockinfo copy = *top;
1628 c->u->u_nfblocks--;
1629 RETURN_IF_ERROR(compiler_unwind_fblock(c, ploc, ©, preserve_tos));
1630 RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, ploc, preserve_tos, loop));
1631 c->u->u_fblock[c->u->u_nfblocks] = copy;
1632 c->u->u_nfblocks++;
1633 return SUCCESS;
1634 }
1635
1636 /* Compile a sequence of statements, checking for a docstring
1637 and for annotations. */
1638
1639 static int
compiler_body(struct compiler * c,location loc,asdl_stmt_seq * stmts)1640 compiler_body(struct compiler *c, location loc, asdl_stmt_seq *stmts)
1641 {
1642
1643 /* Set current line number to the line number of first statement.
1644 This way line number for SETUP_ANNOTATIONS will always
1645 coincide with the line number of first "real" statement in module.
1646 If body is empty, then lineno will be set later in optimize_and_assemble. */
1647 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
1648 stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
1649 loc = LOC(st);
1650 }
1651 /* Every annotated class and module should have __annotations__. */
1652 if (find_ann(stmts)) {
1653 ADDOP(c, loc, SETUP_ANNOTATIONS);
1654 }
1655 if (!asdl_seq_LEN(stmts)) {
1656 return SUCCESS;
1657 }
1658 Py_ssize_t first_instr = 0;
1659 PyObject *docstring = _PyAST_GetDocString(stmts);
1660 if (docstring) {
1661 first_instr = 1;
1662 /* if not -OO mode, set docstring */
1663 if (c->c_optimize < 2) {
1664 PyObject *cleandoc = _PyCompile_CleanDoc(docstring);
1665 if (cleandoc == NULL) {
1666 return ERROR;
1667 }
1668 stmt_ty st = (stmt_ty)asdl_seq_GET(stmts, 0);
1669 assert(st->kind == Expr_kind);
1670 location loc = LOC(st->v.Expr.value);
1671 ADDOP_LOAD_CONST(c, loc, cleandoc);
1672 Py_DECREF(cleandoc);
1673 RETURN_IF_ERROR(compiler_nameop(c, NO_LOCATION, &_Py_ID(__doc__), Store));
1674 }
1675 }
1676 for (Py_ssize_t i = first_instr; i < asdl_seq_LEN(stmts); i++) {
1677 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
1678 }
1679 return SUCCESS;
1680 }
1681
1682 static int
compiler_codegen(struct compiler * c,mod_ty mod)1683 compiler_codegen(struct compiler *c, mod_ty mod)
1684 {
1685 location loc = LOCATION(1, 1, 0, 0);
1686 switch (mod->kind) {
1687 case Module_kind:
1688 if (compiler_body(c, loc, mod->v.Module.body) < 0) {
1689 return ERROR;
1690 }
1691 break;
1692 case Interactive_kind:
1693 if (find_ann(mod->v.Interactive.body)) {
1694 ADDOP(c, loc, SETUP_ANNOTATIONS);
1695 }
1696 c->c_interactive = 1;
1697 VISIT_SEQ(c, stmt, mod->v.Interactive.body);
1698 break;
1699 case Expression_kind:
1700 VISIT(c, expr, mod->v.Expression.body);
1701 break;
1702 default:
1703 PyErr_Format(PyExc_SystemError,
1704 "module kind %d should not be possible",
1705 mod->kind);
1706 return ERROR;
1707 }
1708 return SUCCESS;
1709 }
1710
1711 static int
compiler_enter_anonymous_scope(struct compiler * c,mod_ty mod)1712 compiler_enter_anonymous_scope(struct compiler* c, mod_ty mod)
1713 {
1714 _Py_DECLARE_STR(anon_module, "<module>");
1715 RETURN_IF_ERROR(
1716 compiler_enter_scope(c, &_Py_STR(anon_module), COMPILER_SCOPE_MODULE,
1717 mod, 1));
1718 return SUCCESS;
1719 }
1720
1721 static PyCodeObject *
compiler_mod(struct compiler * c,mod_ty mod)1722 compiler_mod(struct compiler *c, mod_ty mod)
1723 {
1724 PyCodeObject *co = NULL;
1725 int addNone = mod->kind != Expression_kind;
1726 if (compiler_enter_anonymous_scope(c, mod) < 0) {
1727 return NULL;
1728 }
1729 if (compiler_codegen(c, mod) < 0) {
1730 goto finally;
1731 }
1732 co = optimize_and_assemble(c, addNone);
1733 finally:
1734 compiler_exit_scope(c);
1735 return co;
1736 }
1737
1738 /* The test for LOCAL must come before the test for FREE in order to
1739 handle classes where name is both local and free. The local var is
1740 a method and the free var is a free var referenced within a method.
1741 */
1742
1743 static int
get_ref_type(struct compiler * c,PyObject * name)1744 get_ref_type(struct compiler *c, PyObject *name)
1745 {
1746 int scope;
1747 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
1748 (_PyUnicode_EqualToASCIIString(name, "__class__") ||
1749 _PyUnicode_EqualToASCIIString(name, "__classdict__"))) {
1750 return CELL;
1751 }
1752 scope = _PyST_GetScope(c->u->u_ste, name);
1753 if (scope == 0) {
1754 PyErr_Format(PyExc_SystemError,
1755 "_PyST_GetScope(name=%R) failed: "
1756 "unknown scope in unit %S (%R); "
1757 "symbols: %R; locals: %R; globals: %R",
1758 name,
1759 c->u->u_metadata.u_name, c->u->u_ste->ste_id,
1760 c->u->u_ste->ste_symbols, c->u->u_metadata.u_varnames, c->u->u_metadata.u_names);
1761 return ERROR;
1762 }
1763 return scope;
1764 }
1765
1766 static int
compiler_lookup_arg(PyObject * dict,PyObject * name)1767 compiler_lookup_arg(PyObject *dict, PyObject *name)
1768 {
1769 PyObject *v = PyDict_GetItemWithError(dict, name);
1770 if (v == NULL) {
1771 return ERROR;
1772 }
1773 return PyLong_AS_LONG(v);
1774 }
1775
1776 static int
compiler_make_closure(struct compiler * c,location loc,PyCodeObject * co,Py_ssize_t flags)1777 compiler_make_closure(struct compiler *c, location loc,
1778 PyCodeObject *co, Py_ssize_t flags)
1779 {
1780 if (co->co_nfreevars) {
1781 int i = PyUnstable_Code_GetFirstFree(co);
1782 for (; i < co->co_nlocalsplus; ++i) {
1783 /* Bypass com_addop_varname because it will generate
1784 LOAD_DEREF but LOAD_CLOSURE is needed.
1785 */
1786 PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
1787
1788 /* Special case: If a class contains a method with a
1789 free variable that has the same name as a method,
1790 the name will be considered free *and* local in the
1791 class. It should be handled by the closure, as
1792 well as by the normal name lookup logic.
1793 */
1794 int reftype = get_ref_type(c, name);
1795 if (reftype == -1) {
1796 return ERROR;
1797 }
1798 int arg;
1799 if (reftype == CELL) {
1800 arg = compiler_lookup_arg(c->u->u_metadata.u_cellvars, name);
1801 }
1802 else {
1803 arg = compiler_lookup_arg(c->u->u_metadata.u_freevars, name);
1804 }
1805 if (arg == -1) {
1806 PyObject *freevars = _PyCode_GetFreevars(co);
1807 if (freevars == NULL) {
1808 PyErr_Clear();
1809 }
1810 PyErr_Format(PyExc_SystemError,
1811 "compiler_lookup_arg(name=%R) with reftype=%d failed in %S; "
1812 "freevars of code %S: %R",
1813 name,
1814 reftype,
1815 c->u->u_metadata.u_name,
1816 co->co_name,
1817 freevars);
1818 Py_XDECREF(freevars);
1819 return ERROR;
1820 }
1821 ADDOP_I(c, loc, LOAD_CLOSURE, arg);
1822 }
1823 flags |= MAKE_FUNCTION_CLOSURE;
1824 ADDOP_I(c, loc, BUILD_TUPLE, co->co_nfreevars);
1825 }
1826 ADDOP_LOAD_CONST(c, loc, (PyObject*)co);
1827
1828 ADDOP(c, loc, MAKE_FUNCTION);
1829
1830 if (flags & MAKE_FUNCTION_CLOSURE) {
1831 ADDOP_I(c, loc, SET_FUNCTION_ATTRIBUTE, MAKE_FUNCTION_CLOSURE);
1832 }
1833 if (flags & MAKE_FUNCTION_ANNOTATIONS) {
1834 ADDOP_I(c, loc, SET_FUNCTION_ATTRIBUTE, MAKE_FUNCTION_ANNOTATIONS);
1835 }
1836 if (flags & MAKE_FUNCTION_KWDEFAULTS) {
1837 ADDOP_I(c, loc, SET_FUNCTION_ATTRIBUTE, MAKE_FUNCTION_KWDEFAULTS);
1838 }
1839 if (flags & MAKE_FUNCTION_DEFAULTS) {
1840 ADDOP_I(c, loc, SET_FUNCTION_ATTRIBUTE, MAKE_FUNCTION_DEFAULTS);
1841 }
1842 return SUCCESS;
1843 }
1844
1845 static int
compiler_decorators(struct compiler * c,asdl_expr_seq * decos)1846 compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
1847 {
1848 if (!decos) {
1849 return SUCCESS;
1850 }
1851
1852 for (Py_ssize_t i = 0; i < asdl_seq_LEN(decos); i++) {
1853 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1854 }
1855 return SUCCESS;
1856 }
1857
1858 static int
compiler_apply_decorators(struct compiler * c,asdl_expr_seq * decos)1859 compiler_apply_decorators(struct compiler *c, asdl_expr_seq* decos)
1860 {
1861 if (!decos) {
1862 return SUCCESS;
1863 }
1864
1865 for (Py_ssize_t i = asdl_seq_LEN(decos) - 1; i > -1; i--) {
1866 location loc = LOC((expr_ty)asdl_seq_GET(decos, i));
1867 ADDOP_I(c, loc, CALL, 0);
1868 }
1869 return SUCCESS;
1870 }
1871
1872 static int
compiler_visit_kwonlydefaults(struct compiler * c,location loc,asdl_arg_seq * kwonlyargs,asdl_expr_seq * kw_defaults)1873 compiler_visit_kwonlydefaults(struct compiler *c, location loc,
1874 asdl_arg_seq *kwonlyargs, asdl_expr_seq *kw_defaults)
1875 {
1876 /* Push a dict of keyword-only default values.
1877
1878 Return -1 on error, 0 if no dict pushed, 1 if a dict is pushed.
1879 */
1880 int i;
1881 PyObject *keys = NULL;
1882
1883 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1884 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1885 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1886 if (default_) {
1887 PyObject *mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, arg->arg);
1888 if (!mangled) {
1889 goto error;
1890 }
1891 if (keys == NULL) {
1892 keys = PyList_New(1);
1893 if (keys == NULL) {
1894 Py_DECREF(mangled);
1895 return ERROR;
1896 }
1897 PyList_SET_ITEM(keys, 0, mangled);
1898 }
1899 else {
1900 int res = PyList_Append(keys, mangled);
1901 Py_DECREF(mangled);
1902 if (res == -1) {
1903 goto error;
1904 }
1905 }
1906 if (compiler_visit_expr(c, default_) < 0) {
1907 goto error;
1908 }
1909 }
1910 }
1911 if (keys != NULL) {
1912 Py_ssize_t default_count = PyList_GET_SIZE(keys);
1913 PyObject *keys_tuple = PyList_AsTuple(keys);
1914 Py_DECREF(keys);
1915 ADDOP_LOAD_CONST_NEW(c, loc, keys_tuple);
1916 ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, default_count);
1917 assert(default_count > 0);
1918 return 1;
1919 }
1920 else {
1921 return 0;
1922 }
1923
1924 error:
1925 Py_XDECREF(keys);
1926 return ERROR;
1927 }
1928
1929 static int
compiler_visit_annexpr(struct compiler * c,expr_ty annotation)1930 compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
1931 {
1932 location loc = LOC(annotation);
1933 ADDOP_LOAD_CONST_NEW(c, loc, _PyAST_ExprAsUnicode(annotation));
1934 return SUCCESS;
1935 }
1936
1937 static int
compiler_visit_argannotation(struct compiler * c,identifier id,expr_ty annotation,Py_ssize_t * annotations_len,location loc)1938 compiler_visit_argannotation(struct compiler *c, identifier id,
1939 expr_ty annotation, Py_ssize_t *annotations_len, location loc)
1940 {
1941 if (!annotation) {
1942 return SUCCESS;
1943 }
1944 PyObject *mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, id);
1945 if (!mangled) {
1946 return ERROR;
1947 }
1948 ADDOP_LOAD_CONST(c, loc, mangled);
1949 Py_DECREF(mangled);
1950
1951 if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
1952 VISIT(c, annexpr, annotation);
1953 }
1954 else {
1955 if (annotation->kind == Starred_kind) {
1956 // *args: *Ts (where Ts is a TypeVarTuple).
1957 // Do [annotation_value] = [*Ts].
1958 // (Note that in theory we could end up here even for an argument
1959 // other than *args, but in practice the grammar doesn't allow it.)
1960 VISIT(c, expr, annotation->v.Starred.value);
1961 ADDOP_I(c, loc, UNPACK_SEQUENCE, (Py_ssize_t) 1);
1962 }
1963 else {
1964 VISIT(c, expr, annotation);
1965 }
1966 }
1967 *annotations_len += 2;
1968 return SUCCESS;
1969 }
1970
1971 static int
compiler_visit_argannotations(struct compiler * c,asdl_arg_seq * args,Py_ssize_t * annotations_len,location loc)1972 compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
1973 Py_ssize_t *annotations_len, location loc)
1974 {
1975 int i;
1976 for (i = 0; i < asdl_seq_LEN(args); i++) {
1977 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1978 RETURN_IF_ERROR(
1979 compiler_visit_argannotation(
1980 c,
1981 arg->arg,
1982 arg->annotation,
1983 annotations_len,
1984 loc));
1985 }
1986 return SUCCESS;
1987 }
1988
1989 static int
compiler_visit_annotations(struct compiler * c,location loc,arguments_ty args,expr_ty returns)1990 compiler_visit_annotations(struct compiler *c, location loc,
1991 arguments_ty args, expr_ty returns)
1992 {
1993 /* Push arg annotation names and values.
1994 The expressions are evaluated out-of-order wrt the source code.
1995
1996 Return -1 on error, 0 if no annotations pushed, 1 if a annotations is pushed.
1997 */
1998 Py_ssize_t annotations_len = 0;
1999
2000 RETURN_IF_ERROR(
2001 compiler_visit_argannotations(c, args->args, &annotations_len, loc));
2002
2003 RETURN_IF_ERROR(
2004 compiler_visit_argannotations(c, args->posonlyargs, &annotations_len, loc));
2005
2006 if (args->vararg && args->vararg->annotation) {
2007 RETURN_IF_ERROR(
2008 compiler_visit_argannotation(c, args->vararg->arg,
2009 args->vararg->annotation, &annotations_len, loc));
2010 }
2011
2012 RETURN_IF_ERROR(
2013 compiler_visit_argannotations(c, args->kwonlyargs, &annotations_len, loc));
2014
2015 if (args->kwarg && args->kwarg->annotation) {
2016 RETURN_IF_ERROR(
2017 compiler_visit_argannotation(c, args->kwarg->arg,
2018 args->kwarg->annotation, &annotations_len, loc));
2019 }
2020
2021 RETURN_IF_ERROR(
2022 compiler_visit_argannotation(c, &_Py_ID(return), returns, &annotations_len, loc));
2023
2024 if (annotations_len) {
2025 ADDOP_I(c, loc, BUILD_TUPLE, annotations_len);
2026 return 1;
2027 }
2028
2029 return 0;
2030 }
2031
2032 static int
compiler_visit_defaults(struct compiler * c,arguments_ty args,location loc)2033 compiler_visit_defaults(struct compiler *c, arguments_ty args,
2034 location loc)
2035 {
2036 VISIT_SEQ(c, expr, args->defaults);
2037 ADDOP_I(c, loc, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2038 return SUCCESS;
2039 }
2040
2041 static Py_ssize_t
compiler_default_arguments(struct compiler * c,location loc,arguments_ty args)2042 compiler_default_arguments(struct compiler *c, location loc,
2043 arguments_ty args)
2044 {
2045 Py_ssize_t funcflags = 0;
2046 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
2047 RETURN_IF_ERROR(compiler_visit_defaults(c, args, loc));
2048 funcflags |= MAKE_FUNCTION_DEFAULTS;
2049 }
2050 if (args->kwonlyargs) {
2051 int res = compiler_visit_kwonlydefaults(c, loc,
2052 args->kwonlyargs,
2053 args->kw_defaults);
2054 RETURN_IF_ERROR(res);
2055 if (res > 0) {
2056 funcflags |= MAKE_FUNCTION_KWDEFAULTS;
2057 }
2058 }
2059 return funcflags;
2060 }
2061
2062 static bool
forbidden_name(struct compiler * c,location loc,identifier name,expr_context_ty ctx)2063 forbidden_name(struct compiler *c, location loc, identifier name,
2064 expr_context_ty ctx)
2065 {
2066 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2067 compiler_error(c, loc, "cannot assign to __debug__");
2068 return true;
2069 }
2070 if (ctx == Del && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2071 compiler_error(c, loc, "cannot delete __debug__");
2072 return true;
2073 }
2074 return false;
2075 }
2076
2077 static int
compiler_check_debug_one_arg(struct compiler * c,arg_ty arg)2078 compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2079 {
2080 if (arg != NULL) {
2081 if (forbidden_name(c, LOC(arg), arg->arg, Store)) {
2082 return ERROR;
2083 }
2084 }
2085 return SUCCESS;
2086 }
2087
2088 static int
compiler_check_debug_args_seq(struct compiler * c,asdl_arg_seq * args)2089 compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
2090 {
2091 if (args != NULL) {
2092 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
2093 RETURN_IF_ERROR(
2094 compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)));
2095 }
2096 }
2097 return SUCCESS;
2098 }
2099
2100 static int
compiler_check_debug_args(struct compiler * c,arguments_ty args)2101 compiler_check_debug_args(struct compiler *c, arguments_ty args)
2102 {
2103 RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->posonlyargs));
2104 RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->args));
2105 RETURN_IF_ERROR(compiler_check_debug_one_arg(c, args->vararg));
2106 RETURN_IF_ERROR(compiler_check_debug_args_seq(c, args->kwonlyargs));
2107 RETURN_IF_ERROR(compiler_check_debug_one_arg(c, args->kwarg));
2108 return SUCCESS;
2109 }
2110
2111 static int
wrap_in_stopiteration_handler(struct compiler * c)2112 wrap_in_stopiteration_handler(struct compiler *c)
2113 {
2114 NEW_JUMP_TARGET_LABEL(c, handler);
2115
2116 /* Insert SETUP_CLEANUP at start */
2117 RETURN_IF_ERROR(
2118 _PyInstructionSequence_InsertInstruction(
2119 INSTR_SEQUENCE(c), 0,
2120 SETUP_CLEANUP, handler.id, NO_LOCATION));
2121
2122 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2123 ADDOP(c, NO_LOCATION, RETURN_VALUE);
2124 USE_LABEL(c, handler);
2125 ADDOP_I(c, NO_LOCATION, CALL_INTRINSIC_1, INTRINSIC_STOPITERATION_ERROR);
2126 ADDOP_I(c, NO_LOCATION, RERAISE, 1);
2127 return SUCCESS;
2128 }
2129
2130 static int
compiler_type_param_bound_or_default(struct compiler * c,expr_ty e,identifier name,void * key,bool allow_starred)2131 compiler_type_param_bound_or_default(struct compiler *c, expr_ty e,
2132 identifier name, void *key,
2133 bool allow_starred)
2134 {
2135 if (compiler_enter_scope(c, name, COMPILER_SCOPE_TYPEPARAMS,
2136 key, e->lineno) == -1) {
2137 return ERROR;
2138 }
2139 if (allow_starred && e->kind == Starred_kind) {
2140 VISIT(c, expr, e->v.Starred.value);
2141 ADDOP_I(c, LOC(e), UNPACK_SEQUENCE, (Py_ssize_t)1);
2142 }
2143 else {
2144 VISIT(c, expr, e);
2145 }
2146 ADDOP_IN_SCOPE(c, LOC(e), RETURN_VALUE);
2147 PyCodeObject *co = optimize_and_assemble(c, 1);
2148 compiler_exit_scope(c);
2149 if (co == NULL) {
2150 return ERROR;
2151 }
2152 if (compiler_make_closure(c, LOC(e), co, 0) < 0) {
2153 Py_DECREF(co);
2154 return ERROR;
2155 }
2156 Py_DECREF(co);
2157 return SUCCESS;
2158 }
2159
2160 static int
compiler_type_params(struct compiler * c,asdl_type_param_seq * type_params)2161 compiler_type_params(struct compiler *c, asdl_type_param_seq *type_params)
2162 {
2163 if (!type_params) {
2164 return SUCCESS;
2165 }
2166 Py_ssize_t n = asdl_seq_LEN(type_params);
2167 bool seen_default = false;
2168
2169 for (Py_ssize_t i = 0; i < n; i++) {
2170 type_param_ty typeparam = asdl_seq_GET(type_params, i);
2171 location loc = LOC(typeparam);
2172 switch(typeparam->kind) {
2173 case TypeVar_kind:
2174 ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVar.name);
2175 if (typeparam->v.TypeVar.bound) {
2176 expr_ty bound = typeparam->v.TypeVar.bound;
2177 if (compiler_type_param_bound_or_default(c, bound, typeparam->v.TypeVar.name,
2178 (void *)typeparam, false) < 0) {
2179 return ERROR;
2180 }
2181
2182 int intrinsic = bound->kind == Tuple_kind
2183 ? INTRINSIC_TYPEVAR_WITH_CONSTRAINTS
2184 : INTRINSIC_TYPEVAR_WITH_BOUND;
2185 ADDOP_I(c, loc, CALL_INTRINSIC_2, intrinsic);
2186 }
2187 else {
2188 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVAR);
2189 }
2190 if (typeparam->v.TypeVar.default_value) {
2191 seen_default = true;
2192 expr_ty default_ = typeparam->v.TypeVar.default_value;
2193 if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVar.name,
2194 (void *)((uintptr_t)typeparam + 1), false) < 0) {
2195 return ERROR;
2196 }
2197 ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
2198 }
2199 else if (seen_default) {
2200 return compiler_error(c, loc, "non-default type parameter '%U' "
2201 "follows default type parameter",
2202 typeparam->v.TypeVar.name);
2203 }
2204 ADDOP_I(c, loc, COPY, 1);
2205 RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVar.name, Store));
2206 break;
2207 case TypeVarTuple_kind:
2208 ADDOP_LOAD_CONST(c, loc, typeparam->v.TypeVarTuple.name);
2209 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEVARTUPLE);
2210 if (typeparam->v.TypeVarTuple.default_value) {
2211 expr_ty default_ = typeparam->v.TypeVarTuple.default_value;
2212 if (compiler_type_param_bound_or_default(c, default_, typeparam->v.TypeVarTuple.name,
2213 (void *)typeparam, true) < 0) {
2214 return ERROR;
2215 }
2216 ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
2217 seen_default = true;
2218 }
2219 else if (seen_default) {
2220 return compiler_error(c, loc, "non-default type parameter '%U' "
2221 "follows default type parameter",
2222 typeparam->v.TypeVarTuple.name);
2223 }
2224 ADDOP_I(c, loc, COPY, 1);
2225 RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.TypeVarTuple.name, Store));
2226 break;
2227 case ParamSpec_kind:
2228 ADDOP_LOAD_CONST(c, loc, typeparam->v.ParamSpec.name);
2229 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PARAMSPEC);
2230 if (typeparam->v.ParamSpec.default_value) {
2231 expr_ty default_ = typeparam->v.ParamSpec.default_value;
2232 if (compiler_type_param_bound_or_default(c, default_, typeparam->v.ParamSpec.name,
2233 (void *)typeparam, false) < 0) {
2234 return ERROR;
2235 }
2236 ADDOP_I(c, loc, CALL_INTRINSIC_2, INTRINSIC_SET_TYPEPARAM_DEFAULT);
2237 seen_default = true;
2238 }
2239 else if (seen_default) {
2240 return compiler_error(c, loc, "non-default type parameter '%U' "
2241 "follows default type parameter",
2242 typeparam->v.ParamSpec.name);
2243 }
2244 ADDOP_I(c, loc, COPY, 1);
2245 RETURN_IF_ERROR(compiler_nameop(c, loc, typeparam->v.ParamSpec.name, Store));
2246 break;
2247 }
2248 }
2249 ADDOP_I(c, LOC(asdl_seq_GET(type_params, 0)), BUILD_TUPLE, n);
2250 return SUCCESS;
2251 }
2252
2253 static int
compiler_function_body(struct compiler * c,stmt_ty s,int is_async,Py_ssize_t funcflags,int firstlineno)2254 compiler_function_body(struct compiler *c, stmt_ty s, int is_async, Py_ssize_t funcflags,
2255 int firstlineno)
2256 {
2257 arguments_ty args;
2258 identifier name;
2259 asdl_stmt_seq *body;
2260 int scope_type;
2261
2262 if (is_async) {
2263 assert(s->kind == AsyncFunctionDef_kind);
2264
2265 args = s->v.AsyncFunctionDef.args;
2266 name = s->v.AsyncFunctionDef.name;
2267 body = s->v.AsyncFunctionDef.body;
2268
2269 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2270 } else {
2271 assert(s->kind == FunctionDef_kind);
2272
2273 args = s->v.FunctionDef.args;
2274 name = s->v.FunctionDef.name;
2275 body = s->v.FunctionDef.body;
2276
2277 scope_type = COMPILER_SCOPE_FUNCTION;
2278 }
2279
2280 RETURN_IF_ERROR(
2281 compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno));
2282
2283 Py_ssize_t first_instr = 0;
2284 PyObject *docstring = _PyAST_GetDocString(body);
2285 if (docstring) {
2286 first_instr = 1;
2287 /* if not -OO mode, add docstring */
2288 if (c->c_optimize < 2) {
2289 docstring = _PyCompile_CleanDoc(docstring);
2290 if (docstring == NULL) {
2291 compiler_exit_scope(c);
2292 return ERROR;
2293 }
2294 }
2295 else {
2296 docstring = NULL;
2297 }
2298 }
2299 if (compiler_add_const(c->c_const_cache, c->u, docstring ? docstring : Py_None) < 0) {
2300 Py_XDECREF(docstring);
2301 compiler_exit_scope(c);
2302 return ERROR;
2303 }
2304 Py_CLEAR(docstring);
2305
2306 c->u->u_metadata.u_argcount = asdl_seq_LEN(args->args);
2307 c->u->u_metadata.u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
2308 c->u->u_metadata.u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2309
2310 NEW_JUMP_TARGET_LABEL(c, start);
2311 USE_LABEL(c, start);
2312 bool add_stopiteration_handler = c->u->u_ste->ste_coroutine || c->u->u_ste->ste_generator;
2313 if (add_stopiteration_handler) {
2314 /* wrap_in_stopiteration_handler will push a block, so we need to account for that */
2315 RETURN_IF_ERROR(
2316 compiler_push_fblock(c, NO_LOCATION, STOP_ITERATION,
2317 start, NO_LABEL, NULL));
2318 }
2319
2320 for (Py_ssize_t i = first_instr; i < asdl_seq_LEN(body); i++) {
2321 VISIT_IN_SCOPE(c, stmt, (stmt_ty)asdl_seq_GET(body, i));
2322 }
2323 if (add_stopiteration_handler) {
2324 if (wrap_in_stopiteration_handler(c) < 0) {
2325 compiler_exit_scope(c);
2326 return ERROR;
2327 }
2328 compiler_pop_fblock(c, STOP_ITERATION, start);
2329 }
2330 PyCodeObject *co = optimize_and_assemble(c, 1);
2331 compiler_exit_scope(c);
2332 if (co == NULL) {
2333 Py_XDECREF(co);
2334 return ERROR;
2335 }
2336 location loc = LOC(s);
2337 if (compiler_make_closure(c, loc, co, funcflags) < 0) {
2338 Py_DECREF(co);
2339 return ERROR;
2340 }
2341 Py_DECREF(co);
2342 return SUCCESS;
2343 }
2344
2345 static int
compiler_function(struct compiler * c,stmt_ty s,int is_async)2346 compiler_function(struct compiler *c, stmt_ty s, int is_async)
2347 {
2348 arguments_ty args;
2349 expr_ty returns;
2350 identifier name;
2351 asdl_expr_seq *decos;
2352 asdl_type_param_seq *type_params;
2353 Py_ssize_t funcflags;
2354 int annotations;
2355 int firstlineno;
2356
2357 if (is_async) {
2358 assert(s->kind == AsyncFunctionDef_kind);
2359
2360 args = s->v.AsyncFunctionDef.args;
2361 returns = s->v.AsyncFunctionDef.returns;
2362 decos = s->v.AsyncFunctionDef.decorator_list;
2363 name = s->v.AsyncFunctionDef.name;
2364 type_params = s->v.AsyncFunctionDef.type_params;
2365 } else {
2366 assert(s->kind == FunctionDef_kind);
2367
2368 args = s->v.FunctionDef.args;
2369 returns = s->v.FunctionDef.returns;
2370 decos = s->v.FunctionDef.decorator_list;
2371 name = s->v.FunctionDef.name;
2372 type_params = s->v.FunctionDef.type_params;
2373 }
2374
2375 RETURN_IF_ERROR(compiler_check_debug_args(c, args));
2376 RETURN_IF_ERROR(compiler_decorators(c, decos));
2377
2378 firstlineno = s->lineno;
2379 if (asdl_seq_LEN(decos)) {
2380 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2381 }
2382
2383 location loc = LOC(s);
2384
2385 int is_generic = asdl_seq_LEN(type_params) > 0;
2386
2387 funcflags = compiler_default_arguments(c, loc, args);
2388 if (funcflags == -1) {
2389 return ERROR;
2390 }
2391
2392 int num_typeparam_args = 0;
2393
2394 if (is_generic) {
2395 if (funcflags & MAKE_FUNCTION_DEFAULTS) {
2396 num_typeparam_args += 1;
2397 }
2398 if (funcflags & MAKE_FUNCTION_KWDEFAULTS) {
2399 num_typeparam_args += 1;
2400 }
2401 if (num_typeparam_args == 2) {
2402 ADDOP_I(c, loc, SWAP, 2);
2403 }
2404 PyObject *type_params_name = PyUnicode_FromFormat("<generic parameters of %U>", name);
2405 if (!type_params_name) {
2406 return ERROR;
2407 }
2408 if (compiler_enter_scope(c, type_params_name, COMPILER_SCOPE_TYPEPARAMS,
2409 (void *)type_params, firstlineno) == -1) {
2410 Py_DECREF(type_params_name);
2411 return ERROR;
2412 }
2413 Py_DECREF(type_params_name);
2414 RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
2415 for (int i = 0; i < num_typeparam_args; i++) {
2416 RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(INSTR_SEQUENCE(c), LOAD_FAST, i, loc));
2417 }
2418 }
2419
2420 annotations = compiler_visit_annotations(c, loc, args, returns);
2421 if (annotations < 0) {
2422 if (is_generic) {
2423 compiler_exit_scope(c);
2424 }
2425 return ERROR;
2426 }
2427 if (annotations > 0) {
2428 funcflags |= MAKE_FUNCTION_ANNOTATIONS;
2429 }
2430
2431 if (compiler_function_body(c, s, is_async, funcflags, firstlineno) < 0) {
2432 if (is_generic) {
2433 compiler_exit_scope(c);
2434 }
2435 return ERROR;
2436 }
2437
2438 if (is_generic) {
2439 RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(
2440 INSTR_SEQUENCE(c), SWAP, 2, loc));
2441 RETURN_IF_ERROR_IN_SCOPE(c, codegen_addop_i(
2442 INSTR_SEQUENCE(c), CALL_INTRINSIC_2, INTRINSIC_SET_FUNCTION_TYPE_PARAMS, loc));
2443
2444 c->u->u_metadata.u_argcount = num_typeparam_args;
2445 PyCodeObject *co = optimize_and_assemble(c, 0);
2446 compiler_exit_scope(c);
2447 if (co == NULL) {
2448 return ERROR;
2449 }
2450 if (compiler_make_closure(c, loc, co, 0) < 0) {
2451 Py_DECREF(co);
2452 return ERROR;
2453 }
2454 Py_DECREF(co);
2455 if (num_typeparam_args > 0) {
2456 ADDOP_I(c, loc, SWAP, num_typeparam_args + 1);
2457 ADDOP_I(c, loc, CALL, num_typeparam_args - 1);
2458 }
2459 else {
2460 ADDOP(c, loc, PUSH_NULL);
2461 ADDOP_I(c, loc, CALL, 0);
2462 }
2463 }
2464
2465 RETURN_IF_ERROR(compiler_apply_decorators(c, decos));
2466 return compiler_nameop(c, loc, name, Store);
2467 }
2468
2469 static int
compiler_set_type_params_in_class(struct compiler * c,location loc)2470 compiler_set_type_params_in_class(struct compiler *c, location loc)
2471 {
2472 _Py_DECLARE_STR(type_params, ".type_params");
2473 RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_STR(type_params), Load));
2474 RETURN_IF_ERROR(compiler_nameop(c, loc, &_Py_ID(__type_params__), Store));
2475 return 1;
2476 }
2477
2478 static int
compiler_class_body(struct compiler * c,stmt_ty s,int firstlineno)2479 compiler_class_body(struct compiler *c, stmt_ty s, int firstlineno)
2480 {
2481 /* ultimately generate code for:
2482 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2483 where:
2484 <func> is a zero arg function/closure created from the class body.
2485 It mutates its locals to build the class namespace.
2486 <name> is the class name
2487 <bases> is the positional arguments and *varargs argument
2488 <keywords> is the keyword arguments and **kwds argument
2489 This borrows from compiler_call.
2490 */
2491
2492 /* 1. compile the class body into a code object */
2493 RETURN_IF_ERROR(
2494 compiler_enter_scope(c, s->v.ClassDef.name,
2495 COMPILER_SCOPE_CLASS, (void *)s, firstlineno));
2496
2497 location loc = LOCATION(firstlineno, firstlineno, 0, 0);
2498 /* use the class name for name mangling */
2499 Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
2500 /* load (global) __name__ ... */
2501 if (compiler_nameop(c, loc, &_Py_ID(__name__), Load) < 0) {
2502 compiler_exit_scope(c);
2503 return ERROR;
2504 }
2505 /* ... and store it as __module__ */
2506 if (compiler_nameop(c, loc, &_Py_ID(__module__), Store) < 0) {
2507 compiler_exit_scope(c);
2508 return ERROR;
2509 }
2510 assert(c->u->u_metadata.u_qualname);
2511 ADDOP_LOAD_CONST(c, loc, c->u->u_metadata.u_qualname);
2512 if (compiler_nameop(c, loc, &_Py_ID(__qualname__), Store) < 0) {
2513 compiler_exit_scope(c);
2514 return ERROR;
2515 }
2516 ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromLong(c->u->u_metadata.u_firstlineno));
2517 if (compiler_nameop(c, loc, &_Py_ID(__firstlineno__), Store) < 0) {
2518 compiler_exit_scope(c);
2519 return ERROR;
2520 }
2521 asdl_type_param_seq *type_params = s->v.ClassDef.type_params;
2522 if (asdl_seq_LEN(type_params) > 0) {
2523 if (!compiler_set_type_params_in_class(c, loc)) {
2524 compiler_exit_scope(c);
2525 return ERROR;
2526 }
2527 }
2528 if (c->u->u_ste->ste_needs_classdict) {
2529 ADDOP(c, loc, LOAD_LOCALS);
2530
2531 // We can't use compiler_nameop here because we need to generate a
2532 // STORE_DEREF in a class namespace, and compiler_nameop() won't do
2533 // that by default.
2534 PyObject *cellvars = c->u->u_metadata.u_cellvars;
2535 if (compiler_addop_o(c->u, loc, STORE_DEREF, cellvars,
2536 &_Py_ID(__classdict__)) < 0) {
2537 compiler_exit_scope(c);
2538 return ERROR;
2539 }
2540 }
2541 /* compile the body proper */
2542 if (compiler_body(c, loc, s->v.ClassDef.body) < 0) {
2543 compiler_exit_scope(c);
2544 return ERROR;
2545 }
2546 assert(c->u->u_static_attributes);
2547 PyObject *static_attributes_unsorted = PySequence_List(c->u->u_static_attributes);
2548 if (static_attributes_unsorted == NULL) {
2549 compiler_exit_scope(c);
2550 return ERROR;
2551 }
2552 if (PyList_Sort(static_attributes_unsorted) != 0) {
2553 compiler_exit_scope(c);
2554 Py_DECREF(static_attributes_unsorted);
2555 return ERROR;
2556 }
2557 PyObject *static_attributes = PySequence_Tuple(static_attributes_unsorted);
2558 Py_DECREF(static_attributes_unsorted);
2559 if (static_attributes == NULL) {
2560 compiler_exit_scope(c);
2561 return ERROR;
2562 }
2563 ADDOP_LOAD_CONST(c, NO_LOCATION, static_attributes);
2564 Py_CLEAR(static_attributes);
2565 if (compiler_nameop(c, NO_LOCATION, &_Py_ID(__static_attributes__), Store) < 0) {
2566 compiler_exit_scope(c);
2567 return ERROR;
2568 }
2569 /* The following code is artificial */
2570 /* Set __classdictcell__ if necessary */
2571 if (c->u->u_ste->ste_needs_classdict) {
2572 /* Store __classdictcell__ into class namespace */
2573 int i = compiler_lookup_arg(c->u->u_metadata.u_cellvars, &_Py_ID(__classdict__));
2574 if (i < 0) {
2575 compiler_exit_scope(c);
2576 return ERROR;
2577 }
2578 ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
2579 if (compiler_nameop(c, NO_LOCATION, &_Py_ID(__classdictcell__), Store) < 0) {
2580 compiler_exit_scope(c);
2581 return ERROR;
2582 }
2583 }
2584 /* Return __classcell__ if it is referenced, otherwise return None */
2585 if (c->u->u_ste->ste_needs_class_closure) {
2586 /* Store __classcell__ into class namespace & return it */
2587 int i = compiler_lookup_arg(c->u->u_metadata.u_cellvars, &_Py_ID(__class__));
2588 if (i < 0) {
2589 compiler_exit_scope(c);
2590 return ERROR;
2591 }
2592 ADDOP_I(c, NO_LOCATION, LOAD_CLOSURE, i);
2593 ADDOP_I(c, NO_LOCATION, COPY, 1);
2594 if (compiler_nameop(c, NO_LOCATION, &_Py_ID(__classcell__), Store) < 0) {
2595 compiler_exit_scope(c);
2596 return ERROR;
2597 }
2598 }
2599 else {
2600 /* No methods referenced __class__, so just return None */
2601 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
2602 }
2603 ADDOP_IN_SCOPE(c, NO_LOCATION, RETURN_VALUE);
2604 /* create the code object */
2605 PyCodeObject *co = optimize_and_assemble(c, 1);
2606
2607 /* leave the new scope */
2608 compiler_exit_scope(c);
2609 if (co == NULL) {
2610 return ERROR;
2611 }
2612
2613 /* 2. load the 'build_class' function */
2614
2615 // these instructions should be attributed to the class line,
2616 // not a decorator line
2617 loc = LOC(s);
2618 ADDOP(c, loc, LOAD_BUILD_CLASS);
2619 ADDOP(c, loc, PUSH_NULL);
2620
2621 /* 3. load a function (or closure) made from the code object */
2622 if (compiler_make_closure(c, loc, co, 0) < 0) {
2623 Py_DECREF(co);
2624 return ERROR;
2625 }
2626 Py_DECREF(co);
2627
2628 /* 4. load class name */
2629 ADDOP_LOAD_CONST(c, loc, s->v.ClassDef.name);
2630
2631 return SUCCESS;
2632 }
2633
2634 static int
compiler_class(struct compiler * c,stmt_ty s)2635 compiler_class(struct compiler *c, stmt_ty s)
2636 {
2637 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
2638
2639 RETURN_IF_ERROR(compiler_decorators(c, decos));
2640
2641 int firstlineno = s->lineno;
2642 if (asdl_seq_LEN(decos)) {
2643 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2644 }
2645 location loc = LOC(s);
2646
2647 asdl_type_param_seq *type_params = s->v.ClassDef.type_params;
2648 int is_generic = asdl_seq_LEN(type_params) > 0;
2649 if (is_generic) {
2650 PyObject *type_params_name = PyUnicode_FromFormat("<generic parameters of %U>",
2651 s->v.ClassDef.name);
2652 if (!type_params_name) {
2653 return ERROR;
2654 }
2655 if (compiler_enter_scope(c, type_params_name, COMPILER_SCOPE_TYPEPARAMS,
2656 (void *)type_params, firstlineno) == -1) {
2657 Py_DECREF(type_params_name);
2658 return ERROR;
2659 }
2660 Py_DECREF(type_params_name);
2661 Py_XSETREF(c->u->u_private, Py_NewRef(s->v.ClassDef.name));
2662 RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
2663 _Py_DECLARE_STR(type_params, ".type_params");
2664 RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Store));
2665 }
2666
2667 if (compiler_class_body(c, s, firstlineno) < 0) {
2668 if (is_generic) {
2669 compiler_exit_scope(c);
2670 }
2671 return ERROR;
2672 }
2673
2674 /* generate the rest of the code for the call */
2675
2676 if (is_generic) {
2677 _Py_DECLARE_STR(type_params, ".type_params");
2678 _Py_DECLARE_STR(generic_base, ".generic_base");
2679 RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(type_params), Load));
2680 RETURN_IF_ERROR_IN_SCOPE(
2681 c, codegen_addop_i(INSTR_SEQUENCE(c), CALL_INTRINSIC_1, INTRINSIC_SUBSCRIPT_GENERIC, loc)
2682 )
2683 RETURN_IF_ERROR_IN_SCOPE(c, compiler_nameop(c, loc, &_Py_STR(generic_base), Store));
2684
2685 Py_ssize_t original_len = asdl_seq_LEN(s->v.ClassDef.bases);
2686 asdl_expr_seq *bases = _Py_asdl_expr_seq_new(
2687 original_len + 1, c->c_arena);
2688 if (bases == NULL) {
2689 compiler_exit_scope(c);
2690 return ERROR;
2691 }
2692 for (Py_ssize_t i = 0; i < original_len; i++) {
2693 asdl_seq_SET(bases, i, asdl_seq_GET(s->v.ClassDef.bases, i));
2694 }
2695 expr_ty name_node = _PyAST_Name(
2696 &_Py_STR(generic_base), Load,
2697 loc.lineno, loc.col_offset, loc.end_lineno, loc.end_col_offset, c->c_arena
2698 );
2699 if (name_node == NULL) {
2700 compiler_exit_scope(c);
2701 return ERROR;
2702 }
2703 asdl_seq_SET(bases, original_len, name_node);
2704 RETURN_IF_ERROR_IN_SCOPE(c, compiler_call_helper(c, loc, 2,
2705 bases,
2706 s->v.ClassDef.keywords));
2707
2708 PyCodeObject *co = optimize_and_assemble(c, 0);
2709
2710 compiler_exit_scope(c);
2711 if (co == NULL) {
2712 return ERROR;
2713 }
2714 if (compiler_make_closure(c, loc, co, 0) < 0) {
2715 Py_DECREF(co);
2716 return ERROR;
2717 }
2718 Py_DECREF(co);
2719 ADDOP(c, loc, PUSH_NULL);
2720 ADDOP_I(c, loc, CALL, 0);
2721 } else {
2722 RETURN_IF_ERROR(compiler_call_helper(c, loc, 2,
2723 s->v.ClassDef.bases,
2724 s->v.ClassDef.keywords));
2725 }
2726
2727 /* 6. apply decorators */
2728 RETURN_IF_ERROR(compiler_apply_decorators(c, decos));
2729
2730 /* 7. store into <name> */
2731 RETURN_IF_ERROR(compiler_nameop(c, loc, s->v.ClassDef.name, Store));
2732 return SUCCESS;
2733 }
2734
2735 static int
compiler_typealias_body(struct compiler * c,stmt_ty s)2736 compiler_typealias_body(struct compiler *c, stmt_ty s)
2737 {
2738 location loc = LOC(s);
2739 PyObject *name = s->v.TypeAlias.name->v.Name.id;
2740 RETURN_IF_ERROR(
2741 compiler_enter_scope(c, name, COMPILER_SCOPE_FUNCTION, s, loc.lineno));
2742 /* Make None the first constant, so the evaluate function can't have a
2743 docstring. */
2744 RETURN_IF_ERROR(compiler_add_const(c->c_const_cache, c->u, Py_None));
2745 VISIT_IN_SCOPE(c, expr, s->v.TypeAlias.value);
2746 ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
2747 PyCodeObject *co = optimize_and_assemble(c, 0);
2748 compiler_exit_scope(c);
2749 if (co == NULL) {
2750 return ERROR;
2751 }
2752 if (compiler_make_closure(c, loc, co, 0) < 0) {
2753 Py_DECREF(co);
2754 return ERROR;
2755 }
2756 Py_DECREF(co);
2757 ADDOP_I(c, loc, BUILD_TUPLE, 3);
2758 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_TYPEALIAS);
2759 return SUCCESS;
2760 }
2761
2762 static int
compiler_typealias(struct compiler * c,stmt_ty s)2763 compiler_typealias(struct compiler *c, stmt_ty s)
2764 {
2765 location loc = LOC(s);
2766 asdl_type_param_seq *type_params = s->v.TypeAlias.type_params;
2767 int is_generic = asdl_seq_LEN(type_params) > 0;
2768 PyObject *name = s->v.TypeAlias.name->v.Name.id;
2769 if (is_generic) {
2770 PyObject *type_params_name = PyUnicode_FromFormat("<generic parameters of %U>",
2771 name);
2772 if (!type_params_name) {
2773 return ERROR;
2774 }
2775 if (compiler_enter_scope(c, type_params_name, COMPILER_SCOPE_TYPEPARAMS,
2776 (void *)type_params, loc.lineno) == -1) {
2777 Py_DECREF(type_params_name);
2778 return ERROR;
2779 }
2780 Py_DECREF(type_params_name);
2781 RETURN_IF_ERROR_IN_SCOPE(
2782 c, compiler_addop_load_const(c->c_const_cache, c->u, loc, name)
2783 );
2784 RETURN_IF_ERROR_IN_SCOPE(c, compiler_type_params(c, type_params));
2785 }
2786 else {
2787 ADDOP_LOAD_CONST(c, loc, name);
2788 ADDOP_LOAD_CONST(c, loc, Py_None);
2789 }
2790
2791 if (compiler_typealias_body(c, s) < 0) {
2792 if (is_generic) {
2793 compiler_exit_scope(c);
2794 }
2795 return ERROR;
2796 }
2797
2798 if (is_generic) {
2799 PyCodeObject *co = optimize_and_assemble(c, 0);
2800 compiler_exit_scope(c);
2801 if (co == NULL) {
2802 return ERROR;
2803 }
2804 if (compiler_make_closure(c, loc, co, 0) < 0) {
2805 Py_DECREF(co);
2806 return ERROR;
2807 }
2808 Py_DECREF(co);
2809 ADDOP(c, loc, PUSH_NULL);
2810 ADDOP_I(c, loc, CALL, 0);
2811 }
2812 RETURN_IF_ERROR(compiler_nameop(c, loc, name, Store));
2813 return SUCCESS;
2814 }
2815
2816 /* Return false if the expression is a constant value except named singletons.
2817 Return true otherwise. */
2818 static bool
check_is_arg(expr_ty e)2819 check_is_arg(expr_ty e)
2820 {
2821 if (e->kind != Constant_kind) {
2822 return true;
2823 }
2824 PyObject *value = e->v.Constant.value;
2825 return (value == Py_None
2826 || value == Py_False
2827 || value == Py_True
2828 || value == Py_Ellipsis);
2829 }
2830
2831 static PyTypeObject * infer_type(expr_ty e);
2832
2833 /* Check operands of identity checks ("is" and "is not").
2834 Emit a warning if any operand is a constant except named singletons.
2835 */
2836 static int
check_compare(struct compiler * c,expr_ty e)2837 check_compare(struct compiler *c, expr_ty e)
2838 {
2839 Py_ssize_t i, n;
2840 bool left = check_is_arg(e->v.Compare.left);
2841 expr_ty left_expr = e->v.Compare.left;
2842 n = asdl_seq_LEN(e->v.Compare.ops);
2843 for (i = 0; i < n; i++) {
2844 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2845 expr_ty right_expr = (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i);
2846 bool right = check_is_arg(right_expr);
2847 if (op == Is || op == IsNot) {
2848 if (!right || !left) {
2849 const char *msg = (op == Is)
2850 ? "\"is\" with '%.200s' literal. Did you mean \"==\"?"
2851 : "\"is not\" with '%.200s' literal. Did you mean \"!=\"?";
2852 expr_ty literal = !left ? left_expr : right_expr;
2853 return compiler_warn(
2854 c, LOC(e), msg, infer_type(literal)->tp_name
2855 );
2856 }
2857 }
2858 left = right;
2859 left_expr = right_expr;
2860 }
2861 return SUCCESS;
2862 }
2863
2864 static const int compare_masks[] = {
2865 [Py_LT] = COMPARISON_LESS_THAN,
2866 [Py_LE] = COMPARISON_LESS_THAN | COMPARISON_EQUALS,
2867 [Py_EQ] = COMPARISON_EQUALS,
2868 [Py_NE] = COMPARISON_NOT_EQUALS,
2869 [Py_GT] = COMPARISON_GREATER_THAN,
2870 [Py_GE] = COMPARISON_GREATER_THAN | COMPARISON_EQUALS,
2871 };
2872
compiler_addcompare(struct compiler * c,location loc,cmpop_ty op)2873 static int compiler_addcompare(struct compiler *c, location loc,
2874 cmpop_ty op)
2875 {
2876 int cmp;
2877 switch (op) {
2878 case Eq:
2879 cmp = Py_EQ;
2880 break;
2881 case NotEq:
2882 cmp = Py_NE;
2883 break;
2884 case Lt:
2885 cmp = Py_LT;
2886 break;
2887 case LtE:
2888 cmp = Py_LE;
2889 break;
2890 case Gt:
2891 cmp = Py_GT;
2892 break;
2893 case GtE:
2894 cmp = Py_GE;
2895 break;
2896 case Is:
2897 ADDOP_I(c, loc, IS_OP, 0);
2898 return SUCCESS;
2899 case IsNot:
2900 ADDOP_I(c, loc, IS_OP, 1);
2901 return SUCCESS;
2902 case In:
2903 ADDOP_I(c, loc, CONTAINS_OP, 0);
2904 return SUCCESS;
2905 case NotIn:
2906 ADDOP_I(c, loc, CONTAINS_OP, 1);
2907 return SUCCESS;
2908 default:
2909 Py_UNREACHABLE();
2910 }
2911 // cmp goes in top three bits of the oparg, while the low four bits are used
2912 // by quickened versions of this opcode to store the comparison mask. The
2913 // fifth-lowest bit indicates whether the result should be converted to bool
2914 // and is set later):
2915 ADDOP_I(c, loc, COMPARE_OP, (cmp << 5) | compare_masks[cmp]);
2916 return SUCCESS;
2917 }
2918
2919
2920
2921 static int
compiler_jump_if(struct compiler * c,location loc,expr_ty e,jump_target_label next,int cond)2922 compiler_jump_if(struct compiler *c, location loc,
2923 expr_ty e, jump_target_label next, int cond)
2924 {
2925 switch (e->kind) {
2926 case UnaryOp_kind:
2927 if (e->v.UnaryOp.op == Not) {
2928 return compiler_jump_if(c, loc, e->v.UnaryOp.operand, next, !cond);
2929 }
2930 /* fallback to general implementation */
2931 break;
2932 case BoolOp_kind: {
2933 asdl_expr_seq *s = e->v.BoolOp.values;
2934 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2935 assert(n >= 0);
2936 int cond2 = e->v.BoolOp.op == Or;
2937 jump_target_label next2 = next;
2938 if (!cond2 != !cond) {
2939 NEW_JUMP_TARGET_LABEL(c, new_next2);
2940 next2 = new_next2;
2941 }
2942 for (i = 0; i < n; ++i) {
2943 RETURN_IF_ERROR(
2944 compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, i), next2, cond2));
2945 }
2946 RETURN_IF_ERROR(
2947 compiler_jump_if(c, loc, (expr_ty)asdl_seq_GET(s, n), next, cond));
2948 if (!SAME_LABEL(next2, next)) {
2949 USE_LABEL(c, next2);
2950 }
2951 return SUCCESS;
2952 }
2953 case IfExp_kind: {
2954 NEW_JUMP_TARGET_LABEL(c, end);
2955 NEW_JUMP_TARGET_LABEL(c, next2);
2956 RETURN_IF_ERROR(
2957 compiler_jump_if(c, loc, e->v.IfExp.test, next2, 0));
2958 RETURN_IF_ERROR(
2959 compiler_jump_if(c, loc, e->v.IfExp.body, next, cond));
2960 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
2961
2962 USE_LABEL(c, next2);
2963 RETURN_IF_ERROR(
2964 compiler_jump_if(c, loc, e->v.IfExp.orelse, next, cond));
2965
2966 USE_LABEL(c, end);
2967 return SUCCESS;
2968 }
2969 case Compare_kind: {
2970 Py_ssize_t n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2971 if (n > 0) {
2972 RETURN_IF_ERROR(check_compare(c, e));
2973 NEW_JUMP_TARGET_LABEL(c, cleanup);
2974 VISIT(c, expr, e->v.Compare.left);
2975 for (Py_ssize_t i = 0; i < n; i++) {
2976 VISIT(c, expr,
2977 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2978 ADDOP_I(c, LOC(e), SWAP, 2);
2979 ADDOP_I(c, LOC(e), COPY, 2);
2980 ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, i));
2981 ADDOP(c, LOC(e), TO_BOOL);
2982 ADDOP_JUMP(c, LOC(e), POP_JUMP_IF_FALSE, cleanup);
2983 }
2984 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
2985 ADDOP_COMPARE(c, LOC(e), asdl_seq_GET(e->v.Compare.ops, n));
2986 ADDOP(c, LOC(e), TO_BOOL);
2987 ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2988 NEW_JUMP_TARGET_LABEL(c, end);
2989 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
2990
2991 USE_LABEL(c, cleanup);
2992 ADDOP(c, LOC(e), POP_TOP);
2993 if (!cond) {
2994 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, next);
2995 }
2996
2997 USE_LABEL(c, end);
2998 return SUCCESS;
2999 }
3000 /* fallback to general implementation */
3001 break;
3002 }
3003 default:
3004 /* fallback to general implementation */
3005 break;
3006 }
3007
3008 /* general implementation */
3009 VISIT(c, expr, e);
3010 ADDOP(c, LOC(e), TO_BOOL);
3011 ADDOP_JUMP(c, LOC(e), cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
3012 return SUCCESS;
3013 }
3014
3015 static int
compiler_ifexp(struct compiler * c,expr_ty e)3016 compiler_ifexp(struct compiler *c, expr_ty e)
3017 {
3018 assert(e->kind == IfExp_kind);
3019 NEW_JUMP_TARGET_LABEL(c, end);
3020 NEW_JUMP_TARGET_LABEL(c, next);
3021
3022 RETURN_IF_ERROR(
3023 compiler_jump_if(c, LOC(e), e->v.IfExp.test, next, 0));
3024
3025 VISIT(c, expr, e->v.IfExp.body);
3026 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
3027
3028 USE_LABEL(c, next);
3029 VISIT(c, expr, e->v.IfExp.orelse);
3030
3031 USE_LABEL(c, end);
3032 return SUCCESS;
3033 }
3034
3035 static int
compiler_lambda(struct compiler * c,expr_ty e)3036 compiler_lambda(struct compiler *c, expr_ty e)
3037 {
3038 PyCodeObject *co;
3039 Py_ssize_t funcflags;
3040 arguments_ty args = e->v.Lambda.args;
3041 assert(e->kind == Lambda_kind);
3042
3043 RETURN_IF_ERROR(compiler_check_debug_args(c, args));
3044
3045 location loc = LOC(e);
3046 funcflags = compiler_default_arguments(c, loc, args);
3047 if (funcflags == -1) {
3048 return ERROR;
3049 }
3050
3051 _Py_DECLARE_STR(anon_lambda, "<lambda>");
3052 RETURN_IF_ERROR(
3053 compiler_enter_scope(c, &_Py_STR(anon_lambda), COMPILER_SCOPE_LAMBDA,
3054 (void *)e, e->lineno));
3055
3056 /* Make None the first constant, so the lambda can't have a
3057 docstring. */
3058 RETURN_IF_ERROR(compiler_add_const(c->c_const_cache, c->u, Py_None));
3059
3060 c->u->u_metadata.u_argcount = asdl_seq_LEN(args->args);
3061 c->u->u_metadata.u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
3062 c->u->u_metadata.u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
3063 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
3064 if (c->u->u_ste->ste_generator) {
3065 co = optimize_and_assemble(c, 0);
3066 }
3067 else {
3068 location loc = LOC(e->v.Lambda.body);
3069 ADDOP_IN_SCOPE(c, loc, RETURN_VALUE);
3070 co = optimize_and_assemble(c, 1);
3071 }
3072 compiler_exit_scope(c);
3073 if (co == NULL) {
3074 return ERROR;
3075 }
3076
3077 if (compiler_make_closure(c, loc, co, funcflags) < 0) {
3078 Py_DECREF(co);
3079 return ERROR;
3080 }
3081 Py_DECREF(co);
3082
3083 return SUCCESS;
3084 }
3085
3086 static int
compiler_if(struct compiler * c,stmt_ty s)3087 compiler_if(struct compiler *c, stmt_ty s)
3088 {
3089 jump_target_label next;
3090 assert(s->kind == If_kind);
3091 NEW_JUMP_TARGET_LABEL(c, end);
3092 if (asdl_seq_LEN(s->v.If.orelse)) {
3093 NEW_JUMP_TARGET_LABEL(c, orelse);
3094 next = orelse;
3095 }
3096 else {
3097 next = end;
3098 }
3099 RETURN_IF_ERROR(
3100 compiler_jump_if(c, LOC(s), s->v.If.test, next, 0));
3101
3102 VISIT_SEQ(c, stmt, s->v.If.body);
3103 if (asdl_seq_LEN(s->v.If.orelse)) {
3104 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
3105
3106 USE_LABEL(c, next);
3107 VISIT_SEQ(c, stmt, s->v.If.orelse);
3108 }
3109
3110 USE_LABEL(c, end);
3111 return SUCCESS;
3112 }
3113
3114 static int
compiler_for(struct compiler * c,stmt_ty s)3115 compiler_for(struct compiler *c, stmt_ty s)
3116 {
3117 location loc = LOC(s);
3118 NEW_JUMP_TARGET_LABEL(c, start);
3119 NEW_JUMP_TARGET_LABEL(c, body);
3120 NEW_JUMP_TARGET_LABEL(c, cleanup);
3121 NEW_JUMP_TARGET_LABEL(c, end);
3122
3123 RETURN_IF_ERROR(compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL));
3124
3125 VISIT(c, expr, s->v.For.iter);
3126
3127 loc = LOC(s->v.For.iter);
3128 ADDOP(c, loc, GET_ITER);
3129
3130 USE_LABEL(c, start);
3131 ADDOP_JUMP(c, loc, FOR_ITER, cleanup);
3132
3133 /* Add NOP to ensure correct line tracing of multiline for statements.
3134 * It will be removed later if redundant.
3135 */
3136 ADDOP(c, LOC(s->v.For.target), NOP);
3137
3138 USE_LABEL(c, body);
3139 VISIT(c, expr, s->v.For.target);
3140 VISIT_SEQ(c, stmt, s->v.For.body);
3141 /* Mark jump as artificial */
3142 ADDOP_JUMP(c, NO_LOCATION, JUMP, start);
3143
3144 USE_LABEL(c, cleanup);
3145 /* It is important for instrumentation that the `END_FOR` comes first.
3146 * Iteration over a generator will jump to the first of these instructions,
3147 * but a non-generator will jump to a later instruction.
3148 */
3149 ADDOP(c, NO_LOCATION, END_FOR);
3150 ADDOP(c, NO_LOCATION, POP_TOP);
3151
3152 compiler_pop_fblock(c, FOR_LOOP, start);
3153
3154 VISIT_SEQ(c, stmt, s->v.For.orelse);
3155
3156 USE_LABEL(c, end);
3157 return SUCCESS;
3158 }
3159
3160
3161 static int
compiler_async_for(struct compiler * c,stmt_ty s)3162 compiler_async_for(struct compiler *c, stmt_ty s)
3163 {
3164 location loc = LOC(s);
3165 if (IS_TOP_LEVEL_AWAIT(c)){
3166 c->u->u_ste->ste_coroutine = 1;
3167 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
3168 return compiler_error(c, loc, "'async for' outside async function");
3169 }
3170
3171 NEW_JUMP_TARGET_LABEL(c, start);
3172 NEW_JUMP_TARGET_LABEL(c, except);
3173 NEW_JUMP_TARGET_LABEL(c, end);
3174
3175 VISIT(c, expr, s->v.AsyncFor.iter);
3176 ADDOP(c, LOC(s->v.AsyncFor.iter), GET_AITER);
3177
3178 USE_LABEL(c, start);
3179 RETURN_IF_ERROR(compiler_push_fblock(c, loc, FOR_LOOP, start, end, NULL));
3180
3181 /* SETUP_FINALLY to guard the __anext__ call */
3182 ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
3183 ADDOP(c, loc, GET_ANEXT);
3184 ADDOP_LOAD_CONST(c, loc, Py_None);
3185 ADD_YIELD_FROM(c, loc, 1);
3186 ADDOP(c, loc, POP_BLOCK); /* for SETUP_FINALLY */
3187
3188 /* Success block for __anext__ */
3189 VISIT(c, expr, s->v.AsyncFor.target);
3190 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
3191 /* Mark jump as artificial */
3192 ADDOP_JUMP(c, NO_LOCATION, JUMP, start);
3193
3194 compiler_pop_fblock(c, FOR_LOOP, start);
3195
3196 /* Except block for __anext__ */
3197 USE_LABEL(c, except);
3198
3199 /* Use same line number as the iterator,
3200 * as the END_ASYNC_FOR succeeds the `for`, not the body. */
3201 loc = LOC(s->v.AsyncFor.iter);
3202 ADDOP(c, loc, END_ASYNC_FOR);
3203
3204 /* `else` block */
3205 VISIT_SEQ(c, stmt, s->v.For.orelse);
3206
3207 USE_LABEL(c, end);
3208 return SUCCESS;
3209 }
3210
3211 static int
compiler_while(struct compiler * c,stmt_ty s)3212 compiler_while(struct compiler *c, stmt_ty s)
3213 {
3214 NEW_JUMP_TARGET_LABEL(c, loop);
3215 NEW_JUMP_TARGET_LABEL(c, body);
3216 NEW_JUMP_TARGET_LABEL(c, end);
3217 NEW_JUMP_TARGET_LABEL(c, anchor);
3218
3219 USE_LABEL(c, loop);
3220
3221 RETURN_IF_ERROR(compiler_push_fblock(c, LOC(s), WHILE_LOOP, loop, end, NULL));
3222 RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.While.test, anchor, 0));
3223
3224 USE_LABEL(c, body);
3225 VISIT_SEQ(c, stmt, s->v.While.body);
3226 RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.While.test, body, 1));
3227
3228 compiler_pop_fblock(c, WHILE_LOOP, loop);
3229
3230 USE_LABEL(c, anchor);
3231 if (s->v.While.orelse) {
3232 VISIT_SEQ(c, stmt, s->v.While.orelse);
3233 }
3234
3235 USE_LABEL(c, end);
3236 return SUCCESS;
3237 }
3238
3239 static int
compiler_return(struct compiler * c,stmt_ty s)3240 compiler_return(struct compiler *c, stmt_ty s)
3241 {
3242 location loc = LOC(s);
3243 int preserve_tos = ((s->v.Return.value != NULL) &&
3244 (s->v.Return.value->kind != Constant_kind));
3245 if (!_PyST_IsFunctionLike(c->u->u_ste)) {
3246 return compiler_error(c, loc, "'return' outside function");
3247 }
3248 if (s->v.Return.value != NULL &&
3249 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
3250 {
3251 return compiler_error(c, loc, "'return' with value in async generator");
3252 }
3253
3254 if (preserve_tos) {
3255 VISIT(c, expr, s->v.Return.value);
3256 } else {
3257 /* Emit instruction with line number for return value */
3258 if (s->v.Return.value != NULL) {
3259 loc = LOC(s->v.Return.value);
3260 ADDOP(c, loc, NOP);
3261 }
3262 }
3263 if (s->v.Return.value == NULL || s->v.Return.value->lineno != s->lineno) {
3264 loc = LOC(s);
3265 ADDOP(c, loc, NOP);
3266 }
3267
3268 RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, preserve_tos, NULL));
3269 if (s->v.Return.value == NULL) {
3270 ADDOP_LOAD_CONST(c, loc, Py_None);
3271 }
3272 else if (!preserve_tos) {
3273 ADDOP_LOAD_CONST(c, loc, s->v.Return.value->v.Constant.value);
3274 }
3275 ADDOP(c, loc, RETURN_VALUE);
3276
3277 return SUCCESS;
3278 }
3279
3280 static int
compiler_break(struct compiler * c,location loc)3281 compiler_break(struct compiler *c, location loc)
3282 {
3283 struct fblockinfo *loop = NULL;
3284 location origin_loc = loc;
3285 /* Emit instruction with line number */
3286 ADDOP(c, loc, NOP);
3287 RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
3288 if (loop == NULL) {
3289 return compiler_error(c, origin_loc, "'break' outside loop");
3290 }
3291 RETURN_IF_ERROR(compiler_unwind_fblock(c, &loc, loop, 0));
3292 ADDOP_JUMP(c, loc, JUMP, loop->fb_exit);
3293 return SUCCESS;
3294 }
3295
3296 static int
compiler_continue(struct compiler * c,location loc)3297 compiler_continue(struct compiler *c, location loc)
3298 {
3299 struct fblockinfo *loop = NULL;
3300 location origin_loc = loc;
3301 /* Emit instruction with line number */
3302 ADDOP(c, loc, NOP);
3303 RETURN_IF_ERROR(compiler_unwind_fblock_stack(c, &loc, 0, &loop));
3304 if (loop == NULL) {
3305 return compiler_error(c, origin_loc, "'continue' not properly in loop");
3306 }
3307 ADDOP_JUMP(c, loc, JUMP, loop->fb_block);
3308 return SUCCESS;
3309 }
3310
3311
3312 /* Code generated for "try: <body> finally: <finalbody>" is as follows:
3313
3314 SETUP_FINALLY L
3315 <code for body>
3316 POP_BLOCK
3317 <code for finalbody>
3318 JUMP E
3319 L:
3320 <code for finalbody>
3321 E:
3322
3323 The special instructions use the block stack. Each block
3324 stack entry contains the instruction that created it (here
3325 SETUP_FINALLY), the level of the value stack at the time the
3326 block stack entry was created, and a label (here L).
3327
3328 SETUP_FINALLY:
3329 Pushes the current value stack level and the label
3330 onto the block stack.
3331 POP_BLOCK:
3332 Pops en entry from the block stack.
3333
3334 The block stack is unwound when an exception is raised:
3335 when a SETUP_FINALLY entry is found, the raised and the caught
3336 exceptions are pushed onto the value stack (and the exception
3337 condition is cleared), and the interpreter jumps to the label
3338 gotten from the block stack.
3339 */
3340
3341 static int
compiler_try_finally(struct compiler * c,stmt_ty s)3342 compiler_try_finally(struct compiler *c, stmt_ty s)
3343 {
3344 location loc = LOC(s);
3345
3346 NEW_JUMP_TARGET_LABEL(c, body);
3347 NEW_JUMP_TARGET_LABEL(c, end);
3348 NEW_JUMP_TARGET_LABEL(c, exit);
3349 NEW_JUMP_TARGET_LABEL(c, cleanup);
3350
3351 /* `try` block */
3352 ADDOP_JUMP(c, loc, SETUP_FINALLY, end);
3353
3354 USE_LABEL(c, body);
3355 RETURN_IF_ERROR(
3356 compiler_push_fblock(c, loc, FINALLY_TRY, body, end,
3357 s->v.Try.finalbody));
3358
3359 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3360 RETURN_IF_ERROR(compiler_try_except(c, s));
3361 }
3362 else {
3363 VISIT_SEQ(c, stmt, s->v.Try.body);
3364 }
3365 ADDOP(c, NO_LOCATION, POP_BLOCK);
3366 compiler_pop_fblock(c, FINALLY_TRY, body);
3367 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3368
3369 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, exit);
3370 /* `finally` block */
3371
3372 USE_LABEL(c, end);
3373
3374 loc = NO_LOCATION;
3375 ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup);
3376 ADDOP(c, loc, PUSH_EXC_INFO);
3377 RETURN_IF_ERROR(
3378 compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
3379 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3380 compiler_pop_fblock(c, FINALLY_END, end);
3381
3382 loc = NO_LOCATION;
3383 ADDOP_I(c, loc, RERAISE, 0);
3384
3385 USE_LABEL(c, cleanup);
3386 POP_EXCEPT_AND_RERAISE(c, loc);
3387
3388 USE_LABEL(c, exit);
3389 return SUCCESS;
3390 }
3391
3392 static int
compiler_try_star_finally(struct compiler * c,stmt_ty s)3393 compiler_try_star_finally(struct compiler *c, stmt_ty s)
3394 {
3395 location loc = LOC(s);
3396
3397 NEW_JUMP_TARGET_LABEL(c, body);
3398 NEW_JUMP_TARGET_LABEL(c, end);
3399 NEW_JUMP_TARGET_LABEL(c, exit);
3400 NEW_JUMP_TARGET_LABEL(c, cleanup);
3401 /* `try` block */
3402 ADDOP_JUMP(c, loc, SETUP_FINALLY, end);
3403
3404 USE_LABEL(c, body);
3405 RETURN_IF_ERROR(
3406 compiler_push_fblock(c, loc, FINALLY_TRY, body, end,
3407 s->v.TryStar.finalbody));
3408
3409 if (s->v.TryStar.handlers && asdl_seq_LEN(s->v.TryStar.handlers)) {
3410 RETURN_IF_ERROR(compiler_try_star_except(c, s));
3411 }
3412 else {
3413 VISIT_SEQ(c, stmt, s->v.TryStar.body);
3414 }
3415 ADDOP(c, NO_LOCATION, POP_BLOCK);
3416 compiler_pop_fblock(c, FINALLY_TRY, body);
3417 VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3418
3419 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, exit);
3420
3421 /* `finally` block */
3422 USE_LABEL(c, end);
3423
3424 loc = NO_LOCATION;
3425 ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup);
3426 ADDOP(c, loc, PUSH_EXC_INFO);
3427 RETURN_IF_ERROR(
3428 compiler_push_fblock(c, loc, FINALLY_END, end, NO_LABEL, NULL));
3429
3430 VISIT_SEQ(c, stmt, s->v.TryStar.finalbody);
3431
3432 compiler_pop_fblock(c, FINALLY_END, end);
3433 loc = NO_LOCATION;
3434 ADDOP_I(c, loc, RERAISE, 0);
3435
3436 USE_LABEL(c, cleanup);
3437 POP_EXCEPT_AND_RERAISE(c, loc);
3438
3439 USE_LABEL(c, exit);
3440 return SUCCESS;
3441 }
3442
3443
3444 /*
3445 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
3446 (The contents of the value stack is shown in [], with the top
3447 at the right; 'tb' is trace-back info, 'val' the exception's
3448 associated value, and 'exc' the exception.)
3449
3450 Value stack Label Instruction Argument
3451 [] SETUP_FINALLY L1
3452 [] <code for S>
3453 [] POP_BLOCK
3454 [] JUMP L0
3455
3456 [exc] L1: <evaluate E1> )
3457 [exc, E1] CHECK_EXC_MATCH )
3458 [exc, bool] POP_JUMP_IF_FALSE L2 ) only if E1
3459 [exc] <assign to V1> (or POP if no V1)
3460 [] <code for S1>
3461 JUMP L0
3462
3463 [exc] L2: <evaluate E2>
3464 .............................etc.......................
3465
3466 [exc] Ln+1: RERAISE # re-raise exception
3467
3468 [] L0: <next statement>
3469
3470 Of course, parts are not generated if Vi or Ei is not present.
3471 */
3472 static int
compiler_try_except(struct compiler * c,stmt_ty s)3473 compiler_try_except(struct compiler *c, stmt_ty s)
3474 {
3475 location loc = LOC(s);
3476 Py_ssize_t i, n;
3477
3478 NEW_JUMP_TARGET_LABEL(c, body);
3479 NEW_JUMP_TARGET_LABEL(c, except);
3480 NEW_JUMP_TARGET_LABEL(c, end);
3481 NEW_JUMP_TARGET_LABEL(c, cleanup);
3482
3483 ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
3484
3485 USE_LABEL(c, body);
3486 RETURN_IF_ERROR(
3487 compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL));
3488 VISIT_SEQ(c, stmt, s->v.Try.body);
3489 compiler_pop_fblock(c, TRY_EXCEPT, body);
3490 ADDOP(c, NO_LOCATION, POP_BLOCK);
3491 if (s->v.Try.orelse && asdl_seq_LEN(s->v.Try.orelse)) {
3492 VISIT_SEQ(c, stmt, s->v.Try.orelse);
3493 }
3494 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
3495 n = asdl_seq_LEN(s->v.Try.handlers);
3496
3497 USE_LABEL(c, except);
3498
3499 ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup);
3500 ADDOP(c, NO_LOCATION, PUSH_EXC_INFO);
3501
3502 /* Runtime will push a block here, so we need to account for that */
3503 RETURN_IF_ERROR(
3504 compiler_push_fblock(c, loc, EXCEPTION_HANDLER, NO_LABEL, NO_LABEL, NULL));
3505
3506 for (i = 0; i < n; i++) {
3507 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
3508 s->v.Try.handlers, i);
3509 location loc = LOC(handler);
3510 if (!handler->v.ExceptHandler.type && i < n-1) {
3511 return compiler_error(c, loc, "default 'except:' must be last");
3512 }
3513 NEW_JUMP_TARGET_LABEL(c, next_except);
3514 except = next_except;
3515 if (handler->v.ExceptHandler.type) {
3516 VISIT(c, expr, handler->v.ExceptHandler.type);
3517 ADDOP(c, loc, CHECK_EXC_MATCH);
3518 ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, except);
3519 }
3520 if (handler->v.ExceptHandler.name) {
3521 NEW_JUMP_TARGET_LABEL(c, cleanup_end);
3522 NEW_JUMP_TARGET_LABEL(c, cleanup_body);
3523
3524 RETURN_IF_ERROR(
3525 compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store));
3526
3527 /*
3528 try:
3529 # body
3530 except type as name:
3531 try:
3532 # body
3533 finally:
3534 name = None # in case body contains "del name"
3535 del name
3536 */
3537
3538 /* second try: */
3539 ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end);
3540
3541 USE_LABEL(c, cleanup_body);
3542 RETURN_IF_ERROR(
3543 compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body,
3544 NO_LABEL, handler->v.ExceptHandler.name));
3545
3546 /* second # body */
3547 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3548 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3549 /* name = None; del name; # Mark as artificial */
3550 ADDOP(c, NO_LOCATION, POP_BLOCK);
3551 ADDOP(c, NO_LOCATION, POP_BLOCK);
3552 ADDOP(c, NO_LOCATION, POP_EXCEPT);
3553 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
3554 RETURN_IF_ERROR(
3555 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
3556 RETURN_IF_ERROR(
3557 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
3558 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
3559
3560 /* except: */
3561 USE_LABEL(c, cleanup_end);
3562
3563 /* name = None; del name; # artificial */
3564 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
3565 RETURN_IF_ERROR(
3566 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
3567 RETURN_IF_ERROR(
3568 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
3569
3570 ADDOP_I(c, NO_LOCATION, RERAISE, 1);
3571 }
3572 else {
3573 NEW_JUMP_TARGET_LABEL(c, cleanup_body);
3574
3575 ADDOP(c, loc, POP_TOP); /* exc_value */
3576
3577 USE_LABEL(c, cleanup_body);
3578 RETURN_IF_ERROR(
3579 compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body,
3580 NO_LABEL, NULL));
3581
3582 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3583 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3584 ADDOP(c, NO_LOCATION, POP_BLOCK);
3585 ADDOP(c, NO_LOCATION, POP_EXCEPT);
3586 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
3587 }
3588
3589 USE_LABEL(c, except);
3590 }
3591 /* artificial */
3592 compiler_pop_fblock(c, EXCEPTION_HANDLER, NO_LABEL);
3593 ADDOP_I(c, NO_LOCATION, RERAISE, 0);
3594
3595 USE_LABEL(c, cleanup);
3596 POP_EXCEPT_AND_RERAISE(c, NO_LOCATION);
3597
3598 USE_LABEL(c, end);
3599 return SUCCESS;
3600 }
3601
3602 /*
3603 Code generated for "try: S except* E1 as V1: S1 except* E2 as V2: S2 ...":
3604 (The contents of the value stack is shown in [], with the top
3605 at the right; 'tb' is trace-back info, 'val' the exception instance,
3606 and 'typ' the exception's type.)
3607
3608 Value stack Label Instruction Argument
3609 [] SETUP_FINALLY L1
3610 [] <code for S>
3611 [] POP_BLOCK
3612 [] JUMP L0
3613
3614 [exc] L1: BUILD_LIST ) list for raised/reraised excs ("result")
3615 [orig, res] COPY 2 ) make a copy of the original EG
3616
3617 [orig, res, exc] <evaluate E1>
3618 [orig, res, exc, E1] CHECK_EG_MATCH
3619 [orig, res, rest/exc, match?] COPY 1
3620 [orig, res, rest/exc, match?, match?] POP_JUMP_IF_NONE C1
3621
3622 [orig, res, rest, match] <assign to V1> (or POP if no V1)
3623
3624 [orig, res, rest] SETUP_FINALLY R1
3625 [orig, res, rest] <code for S1>
3626 [orig, res, rest] JUMP L2
3627
3628 [orig, res, rest, i, v] R1: LIST_APPEND 3 ) exc raised in except* body - add to res
3629 [orig, res, rest, i] POP
3630 [orig, res, rest] JUMP LE2
3631
3632 [orig, res, rest] L2: NOP ) for lineno
3633 [orig, res, rest] JUMP LE2
3634
3635 [orig, res, rest/exc, None] C1: POP
3636
3637 [orig, res, rest] LE2: <evaluate E2>
3638 .............................etc.......................
3639
3640 [orig, res, rest] Ln+1: LIST_APPEND 1 ) add unhandled exc to res (could be None)
3641
3642 [orig, res] CALL_INTRINSIC_2 PREP_RERAISE_STAR
3643 [exc] COPY 1
3644 [exc, exc] POP_JUMP_IF_NOT_NONE RER
3645 [exc] POP_TOP
3646 [] JUMP L0
3647
3648 [exc] RER: SWAP 2
3649 [exc, prev_exc_info] POP_EXCEPT
3650 [exc] RERAISE 0
3651
3652 [] L0: <next statement>
3653 */
3654 static int
compiler_try_star_except(struct compiler * c,stmt_ty s)3655 compiler_try_star_except(struct compiler *c, stmt_ty s)
3656 {
3657 location loc = LOC(s);
3658
3659 NEW_JUMP_TARGET_LABEL(c, body);
3660 NEW_JUMP_TARGET_LABEL(c, except);
3661 NEW_JUMP_TARGET_LABEL(c, orelse);
3662 NEW_JUMP_TARGET_LABEL(c, end);
3663 NEW_JUMP_TARGET_LABEL(c, cleanup);
3664 NEW_JUMP_TARGET_LABEL(c, reraise_star);
3665
3666 ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
3667
3668 USE_LABEL(c, body);
3669 RETURN_IF_ERROR(
3670 compiler_push_fblock(c, loc, TRY_EXCEPT, body, NO_LABEL, NULL));
3671 VISIT_SEQ(c, stmt, s->v.TryStar.body);
3672 compiler_pop_fblock(c, TRY_EXCEPT, body);
3673 ADDOP(c, NO_LOCATION, POP_BLOCK);
3674 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, orelse);
3675 Py_ssize_t n = asdl_seq_LEN(s->v.TryStar.handlers);
3676
3677 USE_LABEL(c, except);
3678
3679 ADDOP_JUMP(c, NO_LOCATION, SETUP_CLEANUP, cleanup);
3680 ADDOP(c, NO_LOCATION, PUSH_EXC_INFO);
3681
3682 /* Runtime will push a block here, so we need to account for that */
3683 RETURN_IF_ERROR(
3684 compiler_push_fblock(c, loc, EXCEPTION_GROUP_HANDLER,
3685 NO_LABEL, NO_LABEL, "except handler"));
3686
3687 for (Py_ssize_t i = 0; i < n; i++) {
3688 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
3689 s->v.TryStar.handlers, i);
3690 location loc = LOC(handler);
3691 NEW_JUMP_TARGET_LABEL(c, next_except);
3692 except = next_except;
3693 NEW_JUMP_TARGET_LABEL(c, except_with_error);
3694 NEW_JUMP_TARGET_LABEL(c, no_match);
3695 if (i == 0) {
3696 /* create empty list for exceptions raised/reraise in the except* blocks */
3697 /*
3698 [orig] BUILD_LIST
3699 */
3700 /* Create a copy of the original EG */
3701 /*
3702 [orig, []] COPY 2
3703 [orig, [], exc]
3704 */
3705 ADDOP_I(c, loc, BUILD_LIST, 0);
3706 ADDOP_I(c, loc, COPY, 2);
3707 }
3708 if (handler->v.ExceptHandler.type) {
3709 VISIT(c, expr, handler->v.ExceptHandler.type);
3710 ADDOP(c, loc, CHECK_EG_MATCH);
3711 ADDOP_I(c, loc, COPY, 1);
3712 ADDOP_JUMP(c, loc, POP_JUMP_IF_NONE, no_match);
3713 }
3714
3715 NEW_JUMP_TARGET_LABEL(c, cleanup_end);
3716 NEW_JUMP_TARGET_LABEL(c, cleanup_body);
3717
3718 if (handler->v.ExceptHandler.name) {
3719 RETURN_IF_ERROR(
3720 compiler_nameop(c, loc, handler->v.ExceptHandler.name, Store));
3721 }
3722 else {
3723 ADDOP(c, loc, POP_TOP); // match
3724 }
3725
3726 /*
3727 try:
3728 # body
3729 except type as name:
3730 try:
3731 # body
3732 finally:
3733 name = None # in case body contains "del name"
3734 del name
3735 */
3736 /* second try: */
3737 ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup_end);
3738
3739 USE_LABEL(c, cleanup_body);
3740 RETURN_IF_ERROR(
3741 compiler_push_fblock(c, loc, HANDLER_CLEANUP, cleanup_body,
3742 NO_LABEL, handler->v.ExceptHandler.name));
3743
3744 /* second # body */
3745 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
3746 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
3747 /* name = None; del name; # artificial */
3748 ADDOP(c, NO_LOCATION, POP_BLOCK);
3749 if (handler->v.ExceptHandler.name) {
3750 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
3751 RETURN_IF_ERROR(
3752 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
3753 RETURN_IF_ERROR(
3754 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
3755 }
3756 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except);
3757
3758 /* except: */
3759 USE_LABEL(c, cleanup_end);
3760
3761 /* name = None; del name; # artificial */
3762 if (handler->v.ExceptHandler.name) {
3763 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
3764 RETURN_IF_ERROR(
3765 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Store));
3766 RETURN_IF_ERROR(
3767 compiler_nameop(c, NO_LOCATION, handler->v.ExceptHandler.name, Del));
3768 }
3769
3770 /* add exception raised to the res list */
3771 ADDOP_I(c, NO_LOCATION, LIST_APPEND, 3); // exc
3772 ADDOP(c, NO_LOCATION, POP_TOP); // lasti
3773 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except_with_error);
3774
3775 USE_LABEL(c, except);
3776 ADDOP(c, NO_LOCATION, NOP); // to hold a propagated location info
3777 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, except_with_error);
3778
3779 USE_LABEL(c, no_match);
3780 ADDOP(c, loc, POP_TOP); // match (None)
3781
3782 USE_LABEL(c, except_with_error);
3783
3784 if (i == n - 1) {
3785 /* Add exc to the list (if not None it's the unhandled part of the EG) */
3786 ADDOP_I(c, NO_LOCATION, LIST_APPEND, 1);
3787 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, reraise_star);
3788 }
3789 }
3790 /* artificial */
3791 compiler_pop_fblock(c, EXCEPTION_GROUP_HANDLER, NO_LABEL);
3792 NEW_JUMP_TARGET_LABEL(c, reraise);
3793
3794 USE_LABEL(c, reraise_star);
3795 ADDOP_I(c, NO_LOCATION, CALL_INTRINSIC_2, INTRINSIC_PREP_RERAISE_STAR);
3796 ADDOP_I(c, NO_LOCATION, COPY, 1);
3797 ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_NOT_NONE, reraise);
3798
3799 /* Nothing to reraise */
3800 ADDOP(c, NO_LOCATION, POP_TOP);
3801 ADDOP(c, NO_LOCATION, POP_BLOCK);
3802 ADDOP(c, NO_LOCATION, POP_EXCEPT);
3803 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
3804
3805 USE_LABEL(c, reraise);
3806 ADDOP(c, NO_LOCATION, POP_BLOCK);
3807 ADDOP_I(c, NO_LOCATION, SWAP, 2);
3808 ADDOP(c, NO_LOCATION, POP_EXCEPT);
3809 ADDOP_I(c, NO_LOCATION, RERAISE, 0);
3810
3811 USE_LABEL(c, cleanup);
3812 POP_EXCEPT_AND_RERAISE(c, NO_LOCATION);
3813
3814 USE_LABEL(c, orelse);
3815 VISIT_SEQ(c, stmt, s->v.TryStar.orelse);
3816
3817 USE_LABEL(c, end);
3818 return SUCCESS;
3819 }
3820
3821 static int
compiler_try(struct compiler * c,stmt_ty s)3822 compiler_try(struct compiler *c, stmt_ty s) {
3823 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3824 return compiler_try_finally(c, s);
3825 else
3826 return compiler_try_except(c, s);
3827 }
3828
3829 static int
compiler_try_star(struct compiler * c,stmt_ty s)3830 compiler_try_star(struct compiler *c, stmt_ty s)
3831 {
3832 if (s->v.TryStar.finalbody && asdl_seq_LEN(s->v.TryStar.finalbody)) {
3833 return compiler_try_star_finally(c, s);
3834 }
3835 else {
3836 return compiler_try_star_except(c, s);
3837 }
3838 }
3839
3840 static int
compiler_import_as(struct compiler * c,location loc,identifier name,identifier asname)3841 compiler_import_as(struct compiler *c, location loc,
3842 identifier name, identifier asname)
3843 {
3844 /* The IMPORT_NAME opcode was already generated. This function
3845 merely needs to bind the result to a name.
3846
3847 If there is a dot in name, we need to split it and emit a
3848 IMPORT_FROM for each name.
3849 */
3850 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3851 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
3852 if (dot == -2) {
3853 return ERROR;
3854 }
3855 if (dot != -1) {
3856 /* Consume the base module name to get the first attribute */
3857 while (1) {
3858 Py_ssize_t pos = dot + 1;
3859 PyObject *attr;
3860 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
3861 if (dot == -2) {
3862 return ERROR;
3863 }
3864 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
3865 if (!attr) {
3866 return ERROR;
3867 }
3868 ADDOP_N(c, loc, IMPORT_FROM, attr, names);
3869 if (dot == -1) {
3870 break;
3871 }
3872 ADDOP_I(c, loc, SWAP, 2);
3873 ADDOP(c, loc, POP_TOP);
3874 }
3875 RETURN_IF_ERROR(compiler_nameop(c, loc, asname, Store));
3876 ADDOP(c, loc, POP_TOP);
3877 return SUCCESS;
3878 }
3879 return compiler_nameop(c, loc, asname, Store);
3880 }
3881
3882 static int
compiler_import(struct compiler * c,stmt_ty s)3883 compiler_import(struct compiler *c, stmt_ty s)
3884 {
3885 location loc = LOC(s);
3886 /* The Import node stores a module name like a.b.c as a single
3887 string. This is convenient for all cases except
3888 import a.b.c as d
3889 where we need to parse that string to extract the individual
3890 module names.
3891 XXX Perhaps change the representation to make this case simpler?
3892 */
3893 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
3894
3895 PyObject *zero = _PyLong_GetZero(); // borrowed reference
3896 for (i = 0; i < n; i++) {
3897 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3898 int r;
3899
3900 ADDOP_LOAD_CONST(c, loc, zero);
3901 ADDOP_LOAD_CONST(c, loc, Py_None);
3902 ADDOP_NAME(c, loc, IMPORT_NAME, alias->name, names);
3903
3904 if (alias->asname) {
3905 r = compiler_import_as(c, loc, alias->name, alias->asname);
3906 RETURN_IF_ERROR(r);
3907 }
3908 else {
3909 identifier tmp = alias->name;
3910 Py_ssize_t dot = PyUnicode_FindChar(
3911 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
3912 if (dot != -1) {
3913 tmp = PyUnicode_Substring(alias->name, 0, dot);
3914 if (tmp == NULL) {
3915 return ERROR;
3916 }
3917 }
3918 r = compiler_nameop(c, loc, tmp, Store);
3919 if (dot != -1) {
3920 Py_DECREF(tmp);
3921 }
3922 RETURN_IF_ERROR(r);
3923 }
3924 }
3925 return SUCCESS;
3926 }
3927
3928 static int
compiler_from_import(struct compiler * c,stmt_ty s)3929 compiler_from_import(struct compiler *c, stmt_ty s)
3930 {
3931 Py_ssize_t n = asdl_seq_LEN(s->v.ImportFrom.names);
3932
3933 ADDOP_LOAD_CONST_NEW(c, LOC(s), PyLong_FromLong(s->v.ImportFrom.level));
3934
3935 PyObject *names = PyTuple_New(n);
3936 if (!names) {
3937 return ERROR;
3938 }
3939
3940 /* build up the names */
3941 for (Py_ssize_t i = 0; i < n; i++) {
3942 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3943 PyTuple_SET_ITEM(names, i, Py_NewRef(alias->name));
3944 }
3945
3946 if (location_is_after(LOC(s), c->c_future.ff_location) &&
3947 s->v.ImportFrom.module && s->v.ImportFrom.level == 0 &&
3948 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__"))
3949 {
3950 Py_DECREF(names);
3951 return compiler_error(c, LOC(s), "from __future__ imports must occur "
3952 "at the beginning of the file");
3953 }
3954 ADDOP_LOAD_CONST_NEW(c, LOC(s), names);
3955
3956 if (s->v.ImportFrom.module) {
3957 ADDOP_NAME(c, LOC(s), IMPORT_NAME, s->v.ImportFrom.module, names);
3958 }
3959 else {
3960 _Py_DECLARE_STR(empty, "");
3961 ADDOP_NAME(c, LOC(s), IMPORT_NAME, &_Py_STR(empty), names);
3962 }
3963 for (Py_ssize_t i = 0; i < n; i++) {
3964 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3965 identifier store_name;
3966
3967 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
3968 assert(n == 1);
3969 ADDOP_I(c, LOC(s), CALL_INTRINSIC_1, INTRINSIC_IMPORT_STAR);
3970 ADDOP(c, NO_LOCATION, POP_TOP);
3971 return SUCCESS;
3972 }
3973
3974 ADDOP_NAME(c, LOC(s), IMPORT_FROM, alias->name, names);
3975 store_name = alias->name;
3976 if (alias->asname) {
3977 store_name = alias->asname;
3978 }
3979
3980 RETURN_IF_ERROR(compiler_nameop(c, LOC(s), store_name, Store));
3981 }
3982 /* remove imported module */
3983 ADDOP(c, LOC(s), POP_TOP);
3984 return SUCCESS;
3985 }
3986
3987 static int
compiler_assert(struct compiler * c,stmt_ty s)3988 compiler_assert(struct compiler *c, stmt_ty s)
3989 {
3990 /* Always emit a warning if the test is a non-zero length tuple */
3991 if ((s->v.Assert.test->kind == Tuple_kind &&
3992 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) ||
3993 (s->v.Assert.test->kind == Constant_kind &&
3994 PyTuple_Check(s->v.Assert.test->v.Constant.value) &&
3995 PyTuple_Size(s->v.Assert.test->v.Constant.value) > 0))
3996 {
3997 RETURN_IF_ERROR(
3998 compiler_warn(c, LOC(s), "assertion is always true, "
3999 "perhaps remove parentheses?"));
4000 }
4001 if (c->c_optimize) {
4002 return SUCCESS;
4003 }
4004 NEW_JUMP_TARGET_LABEL(c, end);
4005 RETURN_IF_ERROR(compiler_jump_if(c, LOC(s), s->v.Assert.test, end, 1));
4006 ADDOP(c, LOC(s), LOAD_ASSERTION_ERROR);
4007 if (s->v.Assert.msg) {
4008 VISIT(c, expr, s->v.Assert.msg);
4009 ADDOP_I(c, LOC(s), CALL, 0);
4010 }
4011 ADDOP_I(c, LOC(s->v.Assert.test), RAISE_VARARGS, 1);
4012
4013 USE_LABEL(c, end);
4014 return SUCCESS;
4015 }
4016
4017 static int
compiler_stmt_expr(struct compiler * c,location loc,expr_ty value)4018 compiler_stmt_expr(struct compiler *c, location loc, expr_ty value)
4019 {
4020 if (c->c_interactive && c->c_nestlevel <= 1) {
4021 VISIT(c, expr, value);
4022 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_PRINT);
4023 ADDOP(c, NO_LOCATION, POP_TOP);
4024 return SUCCESS;
4025 }
4026
4027 if (value->kind == Constant_kind) {
4028 /* ignore constant statement */
4029 ADDOP(c, loc, NOP);
4030 return SUCCESS;
4031 }
4032
4033 VISIT(c, expr, value);
4034 ADDOP(c, NO_LOCATION, POP_TOP); /* artificial */
4035 return SUCCESS;
4036 }
4037
4038 static int
compiler_visit_stmt(struct compiler * c,stmt_ty s)4039 compiler_visit_stmt(struct compiler *c, stmt_ty s)
4040 {
4041
4042 switch (s->kind) {
4043 case FunctionDef_kind:
4044 return compiler_function(c, s, 0);
4045 case ClassDef_kind:
4046 return compiler_class(c, s);
4047 case TypeAlias_kind:
4048 return compiler_typealias(c, s);
4049 case Return_kind:
4050 return compiler_return(c, s);
4051 case Delete_kind:
4052 VISIT_SEQ(c, expr, s->v.Delete.targets)
4053 break;
4054 case Assign_kind:
4055 {
4056 Py_ssize_t n = asdl_seq_LEN(s->v.Assign.targets);
4057 VISIT(c, expr, s->v.Assign.value);
4058 for (Py_ssize_t i = 0; i < n; i++) {
4059 if (i < n - 1) {
4060 ADDOP_I(c, LOC(s), COPY, 1);
4061 }
4062 VISIT(c, expr,
4063 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
4064 }
4065 break;
4066 }
4067 case AugAssign_kind:
4068 return compiler_augassign(c, s);
4069 case AnnAssign_kind:
4070 return compiler_annassign(c, s);
4071 case For_kind:
4072 return compiler_for(c, s);
4073 case While_kind:
4074 return compiler_while(c, s);
4075 case If_kind:
4076 return compiler_if(c, s);
4077 case Match_kind:
4078 return compiler_match(c, s);
4079 case Raise_kind:
4080 {
4081 Py_ssize_t n = 0;
4082 if (s->v.Raise.exc) {
4083 VISIT(c, expr, s->v.Raise.exc);
4084 n++;
4085 if (s->v.Raise.cause) {
4086 VISIT(c, expr, s->v.Raise.cause);
4087 n++;
4088 }
4089 }
4090 ADDOP_I(c, LOC(s), RAISE_VARARGS, (int)n);
4091 break;
4092 }
4093 case Try_kind:
4094 return compiler_try(c, s);
4095 case TryStar_kind:
4096 return compiler_try_star(c, s);
4097 case Assert_kind:
4098 return compiler_assert(c, s);
4099 case Import_kind:
4100 return compiler_import(c, s);
4101 case ImportFrom_kind:
4102 return compiler_from_import(c, s);
4103 case Global_kind:
4104 case Nonlocal_kind:
4105 break;
4106 case Expr_kind:
4107 {
4108 return compiler_stmt_expr(c, LOC(s), s->v.Expr.value);
4109 }
4110 case Pass_kind:
4111 {
4112 ADDOP(c, LOC(s), NOP);
4113 break;
4114 }
4115 case Break_kind:
4116 {
4117 return compiler_break(c, LOC(s));
4118 }
4119 case Continue_kind:
4120 {
4121 return compiler_continue(c, LOC(s));
4122 }
4123 case With_kind:
4124 return compiler_with(c, s, 0);
4125 case AsyncFunctionDef_kind:
4126 return compiler_function(c, s, 1);
4127 case AsyncWith_kind:
4128 return compiler_async_with(c, s, 0);
4129 case AsyncFor_kind:
4130 return compiler_async_for(c, s);
4131 }
4132
4133 return SUCCESS;
4134 }
4135
4136 static int
unaryop(unaryop_ty op)4137 unaryop(unaryop_ty op)
4138 {
4139 switch (op) {
4140 case Invert:
4141 return UNARY_INVERT;
4142 case USub:
4143 return UNARY_NEGATIVE;
4144 default:
4145 PyErr_Format(PyExc_SystemError,
4146 "unary op %d should not be possible", op);
4147 return 0;
4148 }
4149 }
4150
4151 static int
addop_binary(struct compiler * c,location loc,operator_ty binop,bool inplace)4152 addop_binary(struct compiler *c, location loc, operator_ty binop,
4153 bool inplace)
4154 {
4155 int oparg;
4156 switch (binop) {
4157 case Add:
4158 oparg = inplace ? NB_INPLACE_ADD : NB_ADD;
4159 break;
4160 case Sub:
4161 oparg = inplace ? NB_INPLACE_SUBTRACT : NB_SUBTRACT;
4162 break;
4163 case Mult:
4164 oparg = inplace ? NB_INPLACE_MULTIPLY : NB_MULTIPLY;
4165 break;
4166 case MatMult:
4167 oparg = inplace ? NB_INPLACE_MATRIX_MULTIPLY : NB_MATRIX_MULTIPLY;
4168 break;
4169 case Div:
4170 oparg = inplace ? NB_INPLACE_TRUE_DIVIDE : NB_TRUE_DIVIDE;
4171 break;
4172 case Mod:
4173 oparg = inplace ? NB_INPLACE_REMAINDER : NB_REMAINDER;
4174 break;
4175 case Pow:
4176 oparg = inplace ? NB_INPLACE_POWER : NB_POWER;
4177 break;
4178 case LShift:
4179 oparg = inplace ? NB_INPLACE_LSHIFT : NB_LSHIFT;
4180 break;
4181 case RShift:
4182 oparg = inplace ? NB_INPLACE_RSHIFT : NB_RSHIFT;
4183 break;
4184 case BitOr:
4185 oparg = inplace ? NB_INPLACE_OR : NB_OR;
4186 break;
4187 case BitXor:
4188 oparg = inplace ? NB_INPLACE_XOR : NB_XOR;
4189 break;
4190 case BitAnd:
4191 oparg = inplace ? NB_INPLACE_AND : NB_AND;
4192 break;
4193 case FloorDiv:
4194 oparg = inplace ? NB_INPLACE_FLOOR_DIVIDE : NB_FLOOR_DIVIDE;
4195 break;
4196 default:
4197 PyErr_Format(PyExc_SystemError, "%s op %d should not be possible",
4198 inplace ? "inplace" : "binary", binop);
4199 return ERROR;
4200 }
4201 ADDOP_I(c, loc, BINARY_OP, oparg);
4202 return SUCCESS;
4203 }
4204
4205
4206 static int
addop_yield(struct compiler * c,location loc)4207 addop_yield(struct compiler *c, location loc) {
4208 if (c->u->u_ste->ste_generator && c->u->u_ste->ste_coroutine) {
4209 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_ASYNC_GEN_WRAP);
4210 }
4211 ADDOP_I(c, loc, YIELD_VALUE, 0);
4212 ADDOP_I(c, loc, RESUME, RESUME_AFTER_YIELD);
4213 return SUCCESS;
4214 }
4215
4216 static int
compiler_nameop(struct compiler * c,location loc,identifier name,expr_context_ty ctx)4217 compiler_nameop(struct compiler *c, location loc,
4218 identifier name, expr_context_ty ctx)
4219 {
4220 int op, scope;
4221 Py_ssize_t arg;
4222 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
4223
4224 PyObject *dict = c->u->u_metadata.u_names;
4225 PyObject *mangled;
4226
4227 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
4228 !_PyUnicode_EqualToASCIIString(name, "True") &&
4229 !_PyUnicode_EqualToASCIIString(name, "False"));
4230
4231 if (forbidden_name(c, loc, name, ctx)) {
4232 return ERROR;
4233 }
4234
4235 mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, name);
4236 if (!mangled) {
4237 return ERROR;
4238 }
4239
4240 op = 0;
4241 optype = OP_NAME;
4242 scope = _PyST_GetScope(c->u->u_ste, mangled);
4243 switch (scope) {
4244 case FREE:
4245 dict = c->u->u_metadata.u_freevars;
4246 optype = OP_DEREF;
4247 break;
4248 case CELL:
4249 dict = c->u->u_metadata.u_cellvars;
4250 optype = OP_DEREF;
4251 break;
4252 case LOCAL:
4253 if (_PyST_IsFunctionLike(c->u->u_ste)) {
4254 optype = OP_FAST;
4255 }
4256 else {
4257 PyObject *item;
4258 if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, mangled,
4259 &item) < 0) {
4260 goto error;
4261 }
4262 if (item == Py_True) {
4263 optype = OP_FAST;
4264 }
4265 Py_XDECREF(item);
4266 }
4267 break;
4268 case GLOBAL_IMPLICIT:
4269 if (_PyST_IsFunctionLike(c->u->u_ste))
4270 optype = OP_GLOBAL;
4271 break;
4272 case GLOBAL_EXPLICIT:
4273 optype = OP_GLOBAL;
4274 break;
4275 default:
4276 /* scope can be 0 */
4277 break;
4278 }
4279
4280 /* XXX Leave assert here, but handle __doc__ and the like better */
4281 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
4282
4283 switch (optype) {
4284 case OP_DEREF:
4285 switch (ctx) {
4286 case Load:
4287 if (c->u->u_ste->ste_type == ClassBlock && !c->u->u_in_inlined_comp) {
4288 op = LOAD_FROM_DICT_OR_DEREF;
4289 // First load the locals
4290 if (codegen_addop_noarg(INSTR_SEQUENCE(c), LOAD_LOCALS, loc) < 0) {
4291 goto error;
4292 }
4293 }
4294 else if (c->u->u_ste->ste_can_see_class_scope) {
4295 op = LOAD_FROM_DICT_OR_DEREF;
4296 // First load the classdict
4297 if (compiler_addop_o(c->u, loc, LOAD_DEREF,
4298 c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
4299 goto error;
4300 }
4301 }
4302 else {
4303 op = LOAD_DEREF;
4304 }
4305 break;
4306 case Store: op = STORE_DEREF; break;
4307 case Del: op = DELETE_DEREF; break;
4308 }
4309 break;
4310 case OP_FAST:
4311 switch (ctx) {
4312 case Load: op = LOAD_FAST; break;
4313 case Store: op = STORE_FAST; break;
4314 case Del: op = DELETE_FAST; break;
4315 }
4316 ADDOP_N(c, loc, op, mangled, varnames);
4317 return SUCCESS;
4318 case OP_GLOBAL:
4319 switch (ctx) {
4320 case Load:
4321 if (c->u->u_ste->ste_can_see_class_scope && scope == GLOBAL_IMPLICIT) {
4322 op = LOAD_FROM_DICT_OR_GLOBALS;
4323 // First load the classdict
4324 if (compiler_addop_o(c->u, loc, LOAD_DEREF,
4325 c->u->u_metadata.u_freevars, &_Py_ID(__classdict__)) < 0) {
4326 goto error;
4327 }
4328 } else {
4329 op = LOAD_GLOBAL;
4330 }
4331 break;
4332 case Store: op = STORE_GLOBAL; break;
4333 case Del: op = DELETE_GLOBAL; break;
4334 }
4335 break;
4336 case OP_NAME:
4337 switch (ctx) {
4338 case Load:
4339 op = (c->u->u_ste->ste_type == ClassBlock
4340 && c->u->u_in_inlined_comp)
4341 ? LOAD_GLOBAL
4342 : LOAD_NAME;
4343 break;
4344 case Store: op = STORE_NAME; break;
4345 case Del: op = DELETE_NAME; break;
4346 }
4347 break;
4348 }
4349
4350 assert(op);
4351 arg = dict_add_o(dict, mangled);
4352 Py_DECREF(mangled);
4353 if (arg < 0) {
4354 return ERROR;
4355 }
4356 if (op == LOAD_GLOBAL) {
4357 arg <<= 1;
4358 }
4359 return codegen_addop_i(INSTR_SEQUENCE(c), op, arg, loc);
4360
4361 error:
4362 Py_DECREF(mangled);
4363 return ERROR;
4364 }
4365
4366 static int
compiler_boolop(struct compiler * c,expr_ty e)4367 compiler_boolop(struct compiler *c, expr_ty e)
4368 {
4369 int jumpi;
4370 Py_ssize_t i, n;
4371 asdl_expr_seq *s;
4372
4373 location loc = LOC(e);
4374 assert(e->kind == BoolOp_kind);
4375 if (e->v.BoolOp.op == And)
4376 jumpi = POP_JUMP_IF_FALSE;
4377 else
4378 jumpi = POP_JUMP_IF_TRUE;
4379 NEW_JUMP_TARGET_LABEL(c, end);
4380 s = e->v.BoolOp.values;
4381 n = asdl_seq_LEN(s) - 1;
4382 assert(n >= 0);
4383 for (i = 0; i < n; ++i) {
4384 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
4385 ADDOP_I(c, loc, COPY, 1);
4386 ADDOP(c, loc, TO_BOOL);
4387 ADDOP_JUMP(c, loc, jumpi, end);
4388 ADDOP(c, loc, POP_TOP);
4389 }
4390 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
4391
4392 USE_LABEL(c, end);
4393 return SUCCESS;
4394 }
4395
4396 static int
starunpack_helper(struct compiler * c,location loc,asdl_expr_seq * elts,int pushed,int build,int add,int extend,int tuple)4397 starunpack_helper(struct compiler *c, location loc,
4398 asdl_expr_seq *elts, int pushed,
4399 int build, int add, int extend, int tuple)
4400 {
4401 Py_ssize_t n = asdl_seq_LEN(elts);
4402 if (n > 2 && are_all_items_const(elts, 0, n)) {
4403 PyObject *folded = PyTuple_New(n);
4404 if (folded == NULL) {
4405 return ERROR;
4406 }
4407 PyObject *val;
4408 for (Py_ssize_t i = 0; i < n; i++) {
4409 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
4410 PyTuple_SET_ITEM(folded, i, Py_NewRef(val));
4411 }
4412 if (tuple && !pushed) {
4413 ADDOP_LOAD_CONST_NEW(c, loc, folded);
4414 } else {
4415 if (add == SET_ADD) {
4416 Py_SETREF(folded, PyFrozenSet_New(folded));
4417 if (folded == NULL) {
4418 return ERROR;
4419 }
4420 }
4421 ADDOP_I(c, loc, build, pushed);
4422 ADDOP_LOAD_CONST_NEW(c, loc, folded);
4423 ADDOP_I(c, loc, extend, 1);
4424 if (tuple) {
4425 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
4426 }
4427 }
4428 return SUCCESS;
4429 }
4430
4431 int big = n+pushed > STACK_USE_GUIDELINE;
4432 int seen_star = 0;
4433 for (Py_ssize_t i = 0; i < n; i++) {
4434 expr_ty elt = asdl_seq_GET(elts, i);
4435 if (elt->kind == Starred_kind) {
4436 seen_star = 1;
4437 break;
4438 }
4439 }
4440 if (!seen_star && !big) {
4441 for (Py_ssize_t i = 0; i < n; i++) {
4442 expr_ty elt = asdl_seq_GET(elts, i);
4443 VISIT(c, expr, elt);
4444 }
4445 if (tuple) {
4446 ADDOP_I(c, loc, BUILD_TUPLE, n+pushed);
4447 } else {
4448 ADDOP_I(c, loc, build, n+pushed);
4449 }
4450 return SUCCESS;
4451 }
4452 int sequence_built = 0;
4453 if (big) {
4454 ADDOP_I(c, loc, build, pushed);
4455 sequence_built = 1;
4456 }
4457 for (Py_ssize_t i = 0; i < n; i++) {
4458 expr_ty elt = asdl_seq_GET(elts, i);
4459 if (elt->kind == Starred_kind) {
4460 if (sequence_built == 0) {
4461 ADDOP_I(c, loc, build, i+pushed);
4462 sequence_built = 1;
4463 }
4464 VISIT(c, expr, elt->v.Starred.value);
4465 ADDOP_I(c, loc, extend, 1);
4466 }
4467 else {
4468 VISIT(c, expr, elt);
4469 if (sequence_built) {
4470 ADDOP_I(c, loc, add, 1);
4471 }
4472 }
4473 }
4474 assert(sequence_built);
4475 if (tuple) {
4476 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_LIST_TO_TUPLE);
4477 }
4478 return SUCCESS;
4479 }
4480
4481 static int
unpack_helper(struct compiler * c,location loc,asdl_expr_seq * elts)4482 unpack_helper(struct compiler *c, location loc, asdl_expr_seq *elts)
4483 {
4484 Py_ssize_t n = asdl_seq_LEN(elts);
4485 int seen_star = 0;
4486 for (Py_ssize_t i = 0; i < n; i++) {
4487 expr_ty elt = asdl_seq_GET(elts, i);
4488 if (elt->kind == Starred_kind && !seen_star) {
4489 if ((i >= (1 << 8)) ||
4490 (n-i-1 >= (INT_MAX >> 8))) {
4491 return compiler_error(c, loc,
4492 "too many expressions in "
4493 "star-unpacking assignment");
4494 }
4495 ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8)));
4496 seen_star = 1;
4497 }
4498 else if (elt->kind == Starred_kind) {
4499 return compiler_error(c, loc,
4500 "multiple starred expressions in assignment");
4501 }
4502 }
4503 if (!seen_star) {
4504 ADDOP_I(c, loc, UNPACK_SEQUENCE, n);
4505 }
4506 return SUCCESS;
4507 }
4508
4509 static int
assignment_helper(struct compiler * c,location loc,asdl_expr_seq * elts)4510 assignment_helper(struct compiler *c, location loc, asdl_expr_seq *elts)
4511 {
4512 Py_ssize_t n = asdl_seq_LEN(elts);
4513 RETURN_IF_ERROR(unpack_helper(c, loc, elts));
4514 for (Py_ssize_t i = 0; i < n; i++) {
4515 expr_ty elt = asdl_seq_GET(elts, i);
4516 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
4517 }
4518 return SUCCESS;
4519 }
4520
4521 static int
compiler_list(struct compiler * c,expr_ty e)4522 compiler_list(struct compiler *c, expr_ty e)
4523 {
4524 location loc = LOC(e);
4525 asdl_expr_seq *elts = e->v.List.elts;
4526 if (e->v.List.ctx == Store) {
4527 return assignment_helper(c, loc, elts);
4528 }
4529 else if (e->v.List.ctx == Load) {
4530 return starunpack_helper(c, loc, elts, 0,
4531 BUILD_LIST, LIST_APPEND, LIST_EXTEND, 0);
4532 }
4533 else {
4534 VISIT_SEQ(c, expr, elts);
4535 }
4536 return SUCCESS;
4537 }
4538
4539 static int
compiler_tuple(struct compiler * c,expr_ty e)4540 compiler_tuple(struct compiler *c, expr_ty e)
4541 {
4542 location loc = LOC(e);
4543 asdl_expr_seq *elts = e->v.Tuple.elts;
4544 if (e->v.Tuple.ctx == Store) {
4545 return assignment_helper(c, loc, elts);
4546 }
4547 else if (e->v.Tuple.ctx == Load) {
4548 return starunpack_helper(c, loc, elts, 0,
4549 BUILD_LIST, LIST_APPEND, LIST_EXTEND, 1);
4550 }
4551 else {
4552 VISIT_SEQ(c, expr, elts);
4553 }
4554 return SUCCESS;
4555 }
4556
4557 static int
compiler_set(struct compiler * c,expr_ty e)4558 compiler_set(struct compiler *c, expr_ty e)
4559 {
4560 location loc = LOC(e);
4561 return starunpack_helper(c, loc, e->v.Set.elts, 0,
4562 BUILD_SET, SET_ADD, SET_UPDATE, 0);
4563 }
4564
4565 static bool
are_all_items_const(asdl_expr_seq * seq,Py_ssize_t begin,Py_ssize_t end)4566 are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
4567 {
4568 for (Py_ssize_t i = begin; i < end; i++) {
4569 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
4570 if (key == NULL || key->kind != Constant_kind) {
4571 return false;
4572 }
4573 }
4574 return true;
4575 }
4576
4577 static int
compiler_subdict(struct compiler * c,expr_ty e,Py_ssize_t begin,Py_ssize_t end)4578 compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
4579 {
4580 Py_ssize_t i, n = end - begin;
4581 PyObject *keys, *key;
4582 int big = n*2 > STACK_USE_GUIDELINE;
4583 location loc = LOC(e);
4584 if (n > 1 && !big && are_all_items_const(e->v.Dict.keys, begin, end)) {
4585 for (i = begin; i < end; i++) {
4586 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4587 }
4588 keys = PyTuple_New(n);
4589 if (keys == NULL) {
4590 return SUCCESS;
4591 }
4592 for (i = begin; i < end; i++) {
4593 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
4594 PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key));
4595 }
4596 ADDOP_LOAD_CONST_NEW(c, loc, keys);
4597 ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n);
4598 return SUCCESS;
4599 }
4600 if (big) {
4601 ADDOP_I(c, loc, BUILD_MAP, 0);
4602 }
4603 for (i = begin; i < end; i++) {
4604 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
4605 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4606 if (big) {
4607 ADDOP_I(c, loc, MAP_ADD, 1);
4608 }
4609 }
4610 if (!big) {
4611 ADDOP_I(c, loc, BUILD_MAP, n);
4612 }
4613 return SUCCESS;
4614 }
4615
4616 static int
compiler_dict(struct compiler * c,expr_ty e)4617 compiler_dict(struct compiler *c, expr_ty e)
4618 {
4619 location loc = LOC(e);
4620 Py_ssize_t i, n, elements;
4621 int have_dict;
4622 int is_unpacking = 0;
4623 n = asdl_seq_LEN(e->v.Dict.values);
4624 have_dict = 0;
4625 elements = 0;
4626 for (i = 0; i < n; i++) {
4627 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
4628 if (is_unpacking) {
4629 if (elements) {
4630 RETURN_IF_ERROR(compiler_subdict(c, e, i - elements, i));
4631 if (have_dict) {
4632 ADDOP_I(c, loc, DICT_UPDATE, 1);
4633 }
4634 have_dict = 1;
4635 elements = 0;
4636 }
4637 if (have_dict == 0) {
4638 ADDOP_I(c, loc, BUILD_MAP, 0);
4639 have_dict = 1;
4640 }
4641 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
4642 ADDOP_I(c, loc, DICT_UPDATE, 1);
4643 }
4644 else {
4645 if (elements*2 > STACK_USE_GUIDELINE) {
4646 RETURN_IF_ERROR(compiler_subdict(c, e, i - elements, i + 1));
4647 if (have_dict) {
4648 ADDOP_I(c, loc, DICT_UPDATE, 1);
4649 }
4650 have_dict = 1;
4651 elements = 0;
4652 }
4653 else {
4654 elements++;
4655 }
4656 }
4657 }
4658 if (elements) {
4659 RETURN_IF_ERROR(compiler_subdict(c, e, n - elements, n));
4660 if (have_dict) {
4661 ADDOP_I(c, loc, DICT_UPDATE, 1);
4662 }
4663 have_dict = 1;
4664 }
4665 if (!have_dict) {
4666 ADDOP_I(c, loc, BUILD_MAP, 0);
4667 }
4668 return SUCCESS;
4669 }
4670
4671 static int
compiler_compare(struct compiler * c,expr_ty e)4672 compiler_compare(struct compiler *c, expr_ty e)
4673 {
4674 location loc = LOC(e);
4675 Py_ssize_t i, n;
4676
4677 RETURN_IF_ERROR(check_compare(c, e));
4678 VISIT(c, expr, e->v.Compare.left);
4679 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
4680 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
4681 if (n == 0) {
4682 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
4683 ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, 0));
4684 }
4685 else {
4686 NEW_JUMP_TARGET_LABEL(c, cleanup);
4687 for (i = 0; i < n; i++) {
4688 VISIT(c, expr,
4689 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
4690 ADDOP_I(c, loc, SWAP, 2);
4691 ADDOP_I(c, loc, COPY, 2);
4692 ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, i));
4693 ADDOP_I(c, loc, COPY, 1);
4694 ADDOP(c, loc, TO_BOOL);
4695 ADDOP_JUMP(c, loc, POP_JUMP_IF_FALSE, cleanup);
4696 ADDOP(c, loc, POP_TOP);
4697 }
4698 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
4699 ADDOP_COMPARE(c, loc, asdl_seq_GET(e->v.Compare.ops, n));
4700 NEW_JUMP_TARGET_LABEL(c, end);
4701 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, end);
4702
4703 USE_LABEL(c, cleanup);
4704 ADDOP_I(c, loc, SWAP, 2);
4705 ADDOP(c, loc, POP_TOP);
4706
4707 USE_LABEL(c, end);
4708 }
4709 return SUCCESS;
4710 }
4711
4712 static PyTypeObject *
infer_type(expr_ty e)4713 infer_type(expr_ty e)
4714 {
4715 switch (e->kind) {
4716 case Tuple_kind:
4717 return &PyTuple_Type;
4718 case List_kind:
4719 case ListComp_kind:
4720 return &PyList_Type;
4721 case Dict_kind:
4722 case DictComp_kind:
4723 return &PyDict_Type;
4724 case Set_kind:
4725 case SetComp_kind:
4726 return &PySet_Type;
4727 case GeneratorExp_kind:
4728 return &PyGen_Type;
4729 case Lambda_kind:
4730 return &PyFunction_Type;
4731 case JoinedStr_kind:
4732 case FormattedValue_kind:
4733 return &PyUnicode_Type;
4734 case Constant_kind:
4735 return Py_TYPE(e->v.Constant.value);
4736 default:
4737 return NULL;
4738 }
4739 }
4740
4741 static int
check_caller(struct compiler * c,expr_ty e)4742 check_caller(struct compiler *c, expr_ty e)
4743 {
4744 switch (e->kind) {
4745 case Constant_kind:
4746 case Tuple_kind:
4747 case List_kind:
4748 case ListComp_kind:
4749 case Dict_kind:
4750 case DictComp_kind:
4751 case Set_kind:
4752 case SetComp_kind:
4753 case GeneratorExp_kind:
4754 case JoinedStr_kind:
4755 case FormattedValue_kind: {
4756 location loc = LOC(e);
4757 return compiler_warn(c, loc, "'%.200s' object is not callable; "
4758 "perhaps you missed a comma?",
4759 infer_type(e)->tp_name);
4760 }
4761 default:
4762 return SUCCESS;
4763 }
4764 }
4765
4766 static int
check_subscripter(struct compiler * c,expr_ty e)4767 check_subscripter(struct compiler *c, expr_ty e)
4768 {
4769 PyObject *v;
4770
4771 switch (e->kind) {
4772 case Constant_kind:
4773 v = e->v.Constant.value;
4774 if (!(v == Py_None || v == Py_Ellipsis ||
4775 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4776 PyAnySet_Check(v)))
4777 {
4778 return SUCCESS;
4779 }
4780 /* fall through */
4781 case Set_kind:
4782 case SetComp_kind:
4783 case GeneratorExp_kind:
4784 case Lambda_kind: {
4785 location loc = LOC(e);
4786 return compiler_warn(c, loc, "'%.200s' object is not subscriptable; "
4787 "perhaps you missed a comma?",
4788 infer_type(e)->tp_name);
4789 }
4790 default:
4791 return SUCCESS;
4792 }
4793 }
4794
4795 static int
check_index(struct compiler * c,expr_ty e,expr_ty s)4796 check_index(struct compiler *c, expr_ty e, expr_ty s)
4797 {
4798 PyObject *v;
4799
4800 PyTypeObject *index_type = infer_type(s);
4801 if (index_type == NULL
4802 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4803 || index_type == &PySlice_Type) {
4804 return SUCCESS;
4805 }
4806
4807 switch (e->kind) {
4808 case Constant_kind:
4809 v = e->v.Constant.value;
4810 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4811 return SUCCESS;
4812 }
4813 /* fall through */
4814 case Tuple_kind:
4815 case List_kind:
4816 case ListComp_kind:
4817 case JoinedStr_kind:
4818 case FormattedValue_kind: {
4819 location loc = LOC(e);
4820 return compiler_warn(c, loc, "%.200s indices must be integers "
4821 "or slices, not %.200s; "
4822 "perhaps you missed a comma?",
4823 infer_type(e)->tp_name,
4824 index_type->tp_name);
4825 }
4826 default:
4827 return SUCCESS;
4828 }
4829 }
4830
4831 static int
is_import_originated(struct compiler * c,expr_ty e)4832 is_import_originated(struct compiler *c, expr_ty e)
4833 {
4834 /* Check whether the global scope has an import named
4835 e, if it is a Name object. For not traversing all the
4836 scope stack every time this function is called, it will
4837 only check the global scope to determine whether something
4838 is imported or not. */
4839
4840 if (e->kind != Name_kind) {
4841 return 0;
4842 }
4843
4844 long flags = _PyST_GetSymbol(c->c_st->st_top, e->v.Name.id);
4845 return flags & DEF_IMPORT;
4846 }
4847
4848 static int
can_optimize_super_call(struct compiler * c,expr_ty attr)4849 can_optimize_super_call(struct compiler *c, expr_ty attr)
4850 {
4851 expr_ty e = attr->v.Attribute.value;
4852 if (e->kind != Call_kind ||
4853 e->v.Call.func->kind != Name_kind ||
4854 !_PyUnicode_EqualToASCIIString(e->v.Call.func->v.Name.id, "super") ||
4855 _PyUnicode_EqualToASCIIString(attr->v.Attribute.attr, "__class__") ||
4856 asdl_seq_LEN(e->v.Call.keywords) != 0) {
4857 return 0;
4858 }
4859 Py_ssize_t num_args = asdl_seq_LEN(e->v.Call.args);
4860
4861 PyObject *super_name = e->v.Call.func->v.Name.id;
4862 // detect statically-visible shadowing of 'super' name
4863 int scope = _PyST_GetScope(c->u->u_ste, super_name);
4864 if (scope != GLOBAL_IMPLICIT) {
4865 return 0;
4866 }
4867 scope = _PyST_GetScope(c->c_st->st_top, super_name);
4868 if (scope != 0) {
4869 return 0;
4870 }
4871
4872 if (num_args == 2) {
4873 for (Py_ssize_t i = 0; i < num_args; i++) {
4874 expr_ty elt = asdl_seq_GET(e->v.Call.args, i);
4875 if (elt->kind == Starred_kind) {
4876 return 0;
4877 }
4878 }
4879 // exactly two non-starred args; we can just load
4880 // the provided args
4881 return 1;
4882 }
4883
4884 if (num_args != 0) {
4885 return 0;
4886 }
4887 // we need the following for zero-arg super():
4888
4889 // enclosing function should have at least one argument
4890 if (c->u->u_metadata.u_argcount == 0 &&
4891 c->u->u_metadata.u_posonlyargcount == 0) {
4892 return 0;
4893 }
4894 // __class__ cell should be available
4895 if (get_ref_type(c, &_Py_ID(__class__)) == FREE) {
4896 return 1;
4897 }
4898 return 0;
4899 }
4900
4901 static int
load_args_for_super(struct compiler * c,expr_ty e)4902 load_args_for_super(struct compiler *c, expr_ty e) {
4903 location loc = LOC(e);
4904
4905 // load super() global
4906 PyObject *super_name = e->v.Call.func->v.Name.id;
4907 RETURN_IF_ERROR(compiler_nameop(c, LOC(e->v.Call.func), super_name, Load));
4908
4909 if (asdl_seq_LEN(e->v.Call.args) == 2) {
4910 VISIT(c, expr, asdl_seq_GET(e->v.Call.args, 0));
4911 VISIT(c, expr, asdl_seq_GET(e->v.Call.args, 1));
4912 return SUCCESS;
4913 }
4914
4915 // load __class__ cell
4916 PyObject *name = &_Py_ID(__class__);
4917 assert(get_ref_type(c, name) == FREE);
4918 RETURN_IF_ERROR(compiler_nameop(c, loc, name, Load));
4919
4920 // load self (first argument)
4921 Py_ssize_t i = 0;
4922 PyObject *key, *value;
4923 if (!PyDict_Next(c->u->u_metadata.u_varnames, &i, &key, &value)) {
4924 return ERROR;
4925 }
4926 RETURN_IF_ERROR(compiler_nameop(c, loc, key, Load));
4927
4928 return SUCCESS;
4929 }
4930
4931 // If an attribute access spans multiple lines, update the current start
4932 // location to point to the attribute name.
4933 static location
update_start_location_to_match_attr(struct compiler * c,location loc,expr_ty attr)4934 update_start_location_to_match_attr(struct compiler *c, location loc,
4935 expr_ty attr)
4936 {
4937 assert(attr->kind == Attribute_kind);
4938 if (loc.lineno != attr->end_lineno) {
4939 loc.lineno = attr->end_lineno;
4940 int len = (int)PyUnicode_GET_LENGTH(attr->v.Attribute.attr);
4941 if (len <= attr->end_col_offset) {
4942 loc.col_offset = attr->end_col_offset - len;
4943 }
4944 else {
4945 // GH-94694: Somebody's compiling weird ASTs. Just drop the columns:
4946 loc.col_offset = -1;
4947 loc.end_col_offset = -1;
4948 }
4949 // Make sure the end position still follows the start position, even for
4950 // weird ASTs:
4951 loc.end_lineno = Py_MAX(loc.lineno, loc.end_lineno);
4952 if (loc.lineno == loc.end_lineno) {
4953 loc.end_col_offset = Py_MAX(loc.col_offset, loc.end_col_offset);
4954 }
4955 }
4956 return loc;
4957 }
4958
4959 // Return 1 if the method call was optimized, 0 if not, and -1 on error.
4960 static int
maybe_optimize_method_call(struct compiler * c,expr_ty e)4961 maybe_optimize_method_call(struct compiler *c, expr_ty e)
4962 {
4963 Py_ssize_t argsl, i, kwdsl;
4964 expr_ty meth = e->v.Call.func;
4965 asdl_expr_seq *args = e->v.Call.args;
4966 asdl_keyword_seq *kwds = e->v.Call.keywords;
4967
4968 /* Check that the call node is an attribute access */
4969 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load) {
4970 return 0;
4971 }
4972
4973 /* Check that the base object is not something that is imported */
4974 if (is_import_originated(c, meth->v.Attribute.value)) {
4975 return 0;
4976 }
4977
4978 /* Check that there aren't too many arguments */
4979 argsl = asdl_seq_LEN(args);
4980 kwdsl = asdl_seq_LEN(kwds);
4981 if (argsl + kwdsl + (kwdsl != 0) >= STACK_USE_GUIDELINE) {
4982 return 0;
4983 }
4984 /* Check that there are no *varargs types of arguments. */
4985 for (i = 0; i < argsl; i++) {
4986 expr_ty elt = asdl_seq_GET(args, i);
4987 if (elt->kind == Starred_kind) {
4988 return 0;
4989 }
4990 }
4991
4992 for (i = 0; i < kwdsl; i++) {
4993 keyword_ty kw = asdl_seq_GET(kwds, i);
4994 if (kw->arg == NULL) {
4995 return 0;
4996 }
4997 }
4998
4999 /* Alright, we can optimize the code. */
5000 location loc = LOC(meth);
5001
5002 if (can_optimize_super_call(c, meth)) {
5003 RETURN_IF_ERROR(load_args_for_super(c, meth->v.Attribute.value));
5004 int opcode = asdl_seq_LEN(meth->v.Attribute.value->v.Call.args) ?
5005 LOAD_SUPER_METHOD : LOAD_ZERO_SUPER_METHOD;
5006 ADDOP_NAME(c, loc, opcode, meth->v.Attribute.attr, names);
5007 loc = update_start_location_to_match_attr(c, loc, meth);
5008 ADDOP(c, loc, NOP);
5009 } else {
5010 VISIT(c, expr, meth->v.Attribute.value);
5011 loc = update_start_location_to_match_attr(c, loc, meth);
5012 ADDOP_NAME(c, loc, LOAD_METHOD, meth->v.Attribute.attr, names);
5013 }
5014
5015 VISIT_SEQ(c, expr, e->v.Call.args);
5016
5017 if (kwdsl) {
5018 VISIT_SEQ(c, keyword, kwds);
5019 RETURN_IF_ERROR(
5020 compiler_call_simple_kw_helper(c, loc, kwds, kwdsl));
5021 loc = update_start_location_to_match_attr(c, LOC(e), meth);
5022 ADDOP_I(c, loc, CALL_KW, argsl + kwdsl);
5023 }
5024 else {
5025 loc = update_start_location_to_match_attr(c, LOC(e), meth);
5026 ADDOP_I(c, loc, CALL, argsl);
5027 }
5028 return 1;
5029 }
5030
5031 static int
validate_keywords(struct compiler * c,asdl_keyword_seq * keywords)5032 validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
5033 {
5034 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
5035 for (Py_ssize_t i = 0; i < nkeywords; i++) {
5036 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
5037 if (key->arg == NULL) {
5038 continue;
5039 }
5040 location loc = LOC(key);
5041 if (forbidden_name(c, loc, key->arg, Store)) {
5042 return ERROR;
5043 }
5044 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
5045 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
5046 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
5047 compiler_error(c, LOC(other), "keyword argument repeated: %U", key->arg);
5048 return ERROR;
5049 }
5050 }
5051 }
5052 return SUCCESS;
5053 }
5054
5055 static int
compiler_call(struct compiler * c,expr_ty e)5056 compiler_call(struct compiler *c, expr_ty e)
5057 {
5058 RETURN_IF_ERROR(validate_keywords(c, e->v.Call.keywords));
5059 int ret = maybe_optimize_method_call(c, e);
5060 if (ret < 0) {
5061 return ERROR;
5062 }
5063 if (ret == 1) {
5064 return SUCCESS;
5065 }
5066 RETURN_IF_ERROR(check_caller(c, e->v.Call.func));
5067 VISIT(c, expr, e->v.Call.func);
5068 location loc = LOC(e->v.Call.func);
5069 ADDOP(c, loc, PUSH_NULL);
5070 loc = LOC(e);
5071 return compiler_call_helper(c, loc, 0,
5072 e->v.Call.args,
5073 e->v.Call.keywords);
5074 }
5075
5076 static int
compiler_joined_str(struct compiler * c,expr_ty e)5077 compiler_joined_str(struct compiler *c, expr_ty e)
5078 {
5079 location loc = LOC(e);
5080 Py_ssize_t value_count = asdl_seq_LEN(e->v.JoinedStr.values);
5081 if (value_count > STACK_USE_GUIDELINE) {
5082 _Py_DECLARE_STR(empty, "");
5083 ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty)));
5084 ADDOP_NAME(c, loc, LOAD_METHOD, &_Py_ID(join), names);
5085 ADDOP_I(c, loc, BUILD_LIST, 0);
5086 for (Py_ssize_t i = 0; i < asdl_seq_LEN(e->v.JoinedStr.values); i++) {
5087 VISIT(c, expr, asdl_seq_GET(e->v.JoinedStr.values, i));
5088 ADDOP_I(c, loc, LIST_APPEND, 1);
5089 }
5090 ADDOP_I(c, loc, CALL, 1);
5091 }
5092 else {
5093 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
5094 if (value_count > 1) {
5095 ADDOP_I(c, loc, BUILD_STRING, value_count);
5096 }
5097 else if (value_count == 0) {
5098 _Py_DECLARE_STR(empty, "");
5099 ADDOP_LOAD_CONST_NEW(c, loc, Py_NewRef(&_Py_STR(empty)));
5100 }
5101 }
5102 return SUCCESS;
5103 }
5104
5105 /* Used to implement f-strings. Format a single value. */
5106 static int
compiler_formatted_value(struct compiler * c,expr_ty e)5107 compiler_formatted_value(struct compiler *c, expr_ty e)
5108 {
5109 /* Our oparg encodes 2 pieces of information: the conversion
5110 character, and whether or not a format_spec was provided.
5111
5112 Convert the conversion char to 3 bits:
5113 : 000 0x0 FVC_NONE The default if nothing specified.
5114 !s : 001 0x1 FVC_STR
5115 !r : 010 0x2 FVC_REPR
5116 !a : 011 0x3 FVC_ASCII
5117
5118 next bit is whether or not we have a format spec:
5119 yes : 100 0x4
5120 no : 000 0x0
5121 */
5122
5123 int conversion = e->v.FormattedValue.conversion;
5124 int oparg;
5125
5126 /* The expression to be formatted. */
5127 VISIT(c, expr, e->v.FormattedValue.value);
5128
5129 location loc = LOC(e);
5130 if (conversion != -1) {
5131 switch (conversion) {
5132 case 's': oparg = FVC_STR; break;
5133 case 'r': oparg = FVC_REPR; break;
5134 case 'a': oparg = FVC_ASCII; break;
5135 default:
5136 PyErr_Format(PyExc_SystemError,
5137 "Unrecognized conversion character %d", conversion);
5138 return ERROR;
5139 }
5140 ADDOP_I(c, loc, CONVERT_VALUE, oparg);
5141 }
5142 if (e->v.FormattedValue.format_spec) {
5143 /* Evaluate the format spec, and update our opcode arg. */
5144 VISIT(c, expr, e->v.FormattedValue.format_spec);
5145 ADDOP(c, loc, FORMAT_WITH_SPEC);
5146 } else {
5147 ADDOP(c, loc, FORMAT_SIMPLE);
5148 }
5149 return SUCCESS;
5150 }
5151
5152 static int
compiler_subkwargs(struct compiler * c,location loc,asdl_keyword_seq * keywords,Py_ssize_t begin,Py_ssize_t end)5153 compiler_subkwargs(struct compiler *c, location loc,
5154 asdl_keyword_seq *keywords,
5155 Py_ssize_t begin, Py_ssize_t end)
5156 {
5157 Py_ssize_t i, n = end - begin;
5158 keyword_ty kw;
5159 PyObject *keys, *key;
5160 assert(n > 0);
5161 int big = n*2 > STACK_USE_GUIDELINE;
5162 if (n > 1 && !big) {
5163 for (i = begin; i < end; i++) {
5164 kw = asdl_seq_GET(keywords, i);
5165 VISIT(c, expr, kw->value);
5166 }
5167 keys = PyTuple_New(n);
5168 if (keys == NULL) {
5169 return ERROR;
5170 }
5171 for (i = begin; i < end; i++) {
5172 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
5173 PyTuple_SET_ITEM(keys, i - begin, Py_NewRef(key));
5174 }
5175 ADDOP_LOAD_CONST_NEW(c, loc, keys);
5176 ADDOP_I(c, loc, BUILD_CONST_KEY_MAP, n);
5177 return SUCCESS;
5178 }
5179 if (big) {
5180 ADDOP_I(c, NO_LOCATION, BUILD_MAP, 0);
5181 }
5182 for (i = begin; i < end; i++) {
5183 kw = asdl_seq_GET(keywords, i);
5184 ADDOP_LOAD_CONST(c, loc, kw->arg);
5185 VISIT(c, expr, kw->value);
5186 if (big) {
5187 ADDOP_I(c, NO_LOCATION, MAP_ADD, 1);
5188 }
5189 }
5190 if (!big) {
5191 ADDOP_I(c, loc, BUILD_MAP, n);
5192 }
5193 return SUCCESS;
5194 }
5195
5196 /* Used by compiler_call_helper and maybe_optimize_method_call to emit
5197 * a tuple of keyword names before CALL.
5198 */
5199 static int
compiler_call_simple_kw_helper(struct compiler * c,location loc,asdl_keyword_seq * keywords,Py_ssize_t nkwelts)5200 compiler_call_simple_kw_helper(struct compiler *c, location loc,
5201 asdl_keyword_seq *keywords, Py_ssize_t nkwelts)
5202 {
5203 PyObject *names;
5204 names = PyTuple_New(nkwelts);
5205 if (names == NULL) {
5206 return ERROR;
5207 }
5208 for (int i = 0; i < nkwelts; i++) {
5209 keyword_ty kw = asdl_seq_GET(keywords, i);
5210 PyTuple_SET_ITEM(names, i, Py_NewRef(kw->arg));
5211 }
5212 ADDOP_LOAD_CONST_NEW(c, loc, names);
5213 return SUCCESS;
5214 }
5215
5216
5217 /* shared code between compiler_call and compiler_class */
5218 static int
compiler_call_helper(struct compiler * c,location loc,int n,asdl_expr_seq * args,asdl_keyword_seq * keywords)5219 compiler_call_helper(struct compiler *c, location loc,
5220 int n, /* Args already pushed */
5221 asdl_expr_seq *args,
5222 asdl_keyword_seq *keywords)
5223 {
5224 Py_ssize_t i, nseen, nelts, nkwelts;
5225
5226 RETURN_IF_ERROR(validate_keywords(c, keywords));
5227
5228 nelts = asdl_seq_LEN(args);
5229 nkwelts = asdl_seq_LEN(keywords);
5230
5231 if (nelts + nkwelts*2 > STACK_USE_GUIDELINE) {
5232 goto ex_call;
5233 }
5234 for (i = 0; i < nelts; i++) {
5235 expr_ty elt = asdl_seq_GET(args, i);
5236 if (elt->kind == Starred_kind) {
5237 goto ex_call;
5238 }
5239 }
5240 for (i = 0; i < nkwelts; i++) {
5241 keyword_ty kw = asdl_seq_GET(keywords, i);
5242 if (kw->arg == NULL) {
5243 goto ex_call;
5244 }
5245 }
5246
5247 /* No * or ** args, so can use faster calling sequence */
5248 for (i = 0; i < nelts; i++) {
5249 expr_ty elt = asdl_seq_GET(args, i);
5250 assert(elt->kind != Starred_kind);
5251 VISIT(c, expr, elt);
5252 }
5253 if (nkwelts) {
5254 VISIT_SEQ(c, keyword, keywords);
5255 RETURN_IF_ERROR(
5256 compiler_call_simple_kw_helper(c, loc, keywords, nkwelts));
5257 ADDOP_I(c, loc, CALL_KW, n + nelts + nkwelts);
5258 }
5259 else {
5260 ADDOP_I(c, loc, CALL, n + nelts);
5261 }
5262 return SUCCESS;
5263
5264 ex_call:
5265
5266 /* Do positional arguments. */
5267 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
5268 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
5269 }
5270 else {
5271 RETURN_IF_ERROR(starunpack_helper(c, loc, args, n, BUILD_LIST,
5272 LIST_APPEND, LIST_EXTEND, 1));
5273 }
5274 /* Then keyword arguments */
5275 if (nkwelts) {
5276 /* Has a new dict been pushed */
5277 int have_dict = 0;
5278
5279 nseen = 0; /* the number of keyword arguments on the stack following */
5280 for (i = 0; i < nkwelts; i++) {
5281 keyword_ty kw = asdl_seq_GET(keywords, i);
5282 if (kw->arg == NULL) {
5283 /* A keyword argument unpacking. */
5284 if (nseen) {
5285 RETURN_IF_ERROR(compiler_subkwargs(c, loc, keywords, i - nseen, i));
5286 if (have_dict) {
5287 ADDOP_I(c, loc, DICT_MERGE, 1);
5288 }
5289 have_dict = 1;
5290 nseen = 0;
5291 }
5292 if (!have_dict) {
5293 ADDOP_I(c, loc, BUILD_MAP, 0);
5294 have_dict = 1;
5295 }
5296 VISIT(c, expr, kw->value);
5297 ADDOP_I(c, loc, DICT_MERGE, 1);
5298 }
5299 else {
5300 nseen++;
5301 }
5302 }
5303 if (nseen) {
5304 /* Pack up any trailing keyword arguments. */
5305 RETURN_IF_ERROR(compiler_subkwargs(c, loc, keywords, nkwelts - nseen, nkwelts));
5306 if (have_dict) {
5307 ADDOP_I(c, loc, DICT_MERGE, 1);
5308 }
5309 have_dict = 1;
5310 }
5311 assert(have_dict);
5312 }
5313 ADDOP_I(c, loc, CALL_FUNCTION_EX, nkwelts > 0);
5314 return SUCCESS;
5315 }
5316
5317
5318 /* List and set comprehensions work by being inlined at the location where
5319 they are defined. The isolation of iteration variables is provided by
5320 pushing/popping clashing locals on the stack. Generator expressions work
5321 by creating a nested function to perform the actual iteration.
5322 This means that the iteration variables don't leak into the current scope.
5323 See https://peps.python.org/pep-0709/ for additional information.
5324 The defined function is called immediately following its definition, with the
5325 result of that call being the result of the expression.
5326 The LC/SC version returns the populated container, while the GE version is
5327 flagged in symtable.c as a generator, so it returns the generator object
5328 when the function is called.
5329
5330 Possible cleanups:
5331 - iterate over the generator sequence instead of using recursion
5332 */
5333
5334
5335 static int
compiler_comprehension_generator(struct compiler * c,location loc,asdl_comprehension_seq * generators,int gen_index,int depth,expr_ty elt,expr_ty val,int type,int iter_on_stack)5336 compiler_comprehension_generator(struct compiler *c, location loc,
5337 asdl_comprehension_seq *generators, int gen_index,
5338 int depth,
5339 expr_ty elt, expr_ty val, int type,
5340 int iter_on_stack)
5341 {
5342 comprehension_ty gen;
5343 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
5344 if (gen->is_async) {
5345 return compiler_async_comprehension_generator(
5346 c, loc, generators, gen_index, depth, elt, val, type,
5347 iter_on_stack);
5348 } else {
5349 return compiler_sync_comprehension_generator(
5350 c, loc, generators, gen_index, depth, elt, val, type,
5351 iter_on_stack);
5352 }
5353 }
5354
5355 static int
compiler_sync_comprehension_generator(struct compiler * c,location loc,asdl_comprehension_seq * generators,int gen_index,int depth,expr_ty elt,expr_ty val,int type,int iter_on_stack)5356 compiler_sync_comprehension_generator(struct compiler *c, location loc,
5357 asdl_comprehension_seq *generators,
5358 int gen_index, int depth,
5359 expr_ty elt, expr_ty val, int type,
5360 int iter_on_stack)
5361 {
5362 /* generate code for the iterator, then each of the ifs,
5363 and then write to the element */
5364
5365 NEW_JUMP_TARGET_LABEL(c, start);
5366 NEW_JUMP_TARGET_LABEL(c, if_cleanup);
5367 NEW_JUMP_TARGET_LABEL(c, anchor);
5368
5369 comprehension_ty gen = (comprehension_ty)asdl_seq_GET(generators,
5370 gen_index);
5371
5372 if (!iter_on_stack) {
5373 if (gen_index == 0) {
5374 /* Receive outermost iter as an implicit argument */
5375 c->u->u_metadata.u_argcount = 1;
5376 ADDOP_I(c, loc, LOAD_FAST, 0);
5377 }
5378 else {
5379 /* Sub-iter - calculate on the fly */
5380 /* Fast path for the temporary variable assignment idiom:
5381 for y in [f(x)]
5382 */
5383 asdl_expr_seq *elts;
5384 switch (gen->iter->kind) {
5385 case List_kind:
5386 elts = gen->iter->v.List.elts;
5387 break;
5388 case Tuple_kind:
5389 elts = gen->iter->v.Tuple.elts;
5390 break;
5391 default:
5392 elts = NULL;
5393 }
5394 if (asdl_seq_LEN(elts) == 1) {
5395 expr_ty elt = asdl_seq_GET(elts, 0);
5396 if (elt->kind != Starred_kind) {
5397 VISIT(c, expr, elt);
5398 start = NO_LABEL;
5399 }
5400 }
5401 if (IS_LABEL(start)) {
5402 VISIT(c, expr, gen->iter);
5403 ADDOP(c, LOC(gen->iter), GET_ITER);
5404 }
5405 }
5406 }
5407
5408 if (IS_LABEL(start)) {
5409 depth++;
5410 ADDOP(c, LOC(gen->iter), GET_ITER);
5411 USE_LABEL(c, start);
5412 ADDOP_JUMP(c, LOC(gen->iter), FOR_ITER, anchor);
5413 }
5414 VISIT(c, expr, gen->target);
5415
5416 /* XXX this needs to be cleaned up...a lot! */
5417 Py_ssize_t n = asdl_seq_LEN(gen->ifs);
5418 for (Py_ssize_t i = 0; i < n; i++) {
5419 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5420 RETURN_IF_ERROR(compiler_jump_if(c, loc, e, if_cleanup, 0));
5421 }
5422
5423 if (++gen_index < asdl_seq_LEN(generators)) {
5424 RETURN_IF_ERROR(
5425 compiler_comprehension_generator(c, loc,
5426 generators, gen_index, depth,
5427 elt, val, type, 0));
5428 }
5429
5430 location elt_loc = LOC(elt);
5431
5432 /* only append after the last for generator */
5433 if (gen_index >= asdl_seq_LEN(generators)) {
5434 /* comprehension specific code */
5435 switch (type) {
5436 case COMP_GENEXP:
5437 VISIT(c, expr, elt);
5438 ADDOP_YIELD(c, elt_loc);
5439 ADDOP(c, elt_loc, POP_TOP);
5440 break;
5441 case COMP_LISTCOMP:
5442 VISIT(c, expr, elt);
5443 ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1);
5444 break;
5445 case COMP_SETCOMP:
5446 VISIT(c, expr, elt);
5447 ADDOP_I(c, elt_loc, SET_ADD, depth + 1);
5448 break;
5449 case COMP_DICTCOMP:
5450 /* With '{k: v}', k is evaluated before v, so we do
5451 the same. */
5452 VISIT(c, expr, elt);
5453 VISIT(c, expr, val);
5454 elt_loc = LOCATION(elt->lineno,
5455 val->end_lineno,
5456 elt->col_offset,
5457 val->end_col_offset);
5458 ADDOP_I(c, elt_loc, MAP_ADD, depth + 1);
5459 break;
5460 default:
5461 return ERROR;
5462 }
5463 }
5464
5465 USE_LABEL(c, if_cleanup);
5466 if (IS_LABEL(start)) {
5467 ADDOP_JUMP(c, elt_loc, JUMP, start);
5468
5469 USE_LABEL(c, anchor);
5470 /* It is important for instrumentation that the `END_FOR` comes first.
5471 * Iteration over a generator will jump to the first of these instructions,
5472 * but a non-generator will jump to a later instruction.
5473 */
5474 ADDOP(c, NO_LOCATION, END_FOR);
5475 ADDOP(c, NO_LOCATION, POP_TOP);
5476 }
5477
5478 return SUCCESS;
5479 }
5480
5481 static int
compiler_async_comprehension_generator(struct compiler * c,location loc,asdl_comprehension_seq * generators,int gen_index,int depth,expr_ty elt,expr_ty val,int type,int iter_on_stack)5482 compiler_async_comprehension_generator(struct compiler *c, location loc,
5483 asdl_comprehension_seq *generators,
5484 int gen_index, int depth,
5485 expr_ty elt, expr_ty val, int type,
5486 int iter_on_stack)
5487 {
5488 NEW_JUMP_TARGET_LABEL(c, start);
5489 NEW_JUMP_TARGET_LABEL(c, except);
5490 NEW_JUMP_TARGET_LABEL(c, if_cleanup);
5491
5492 comprehension_ty gen = (comprehension_ty)asdl_seq_GET(generators,
5493 gen_index);
5494
5495 if (!iter_on_stack) {
5496 if (gen_index == 0) {
5497 /* Receive outermost iter as an implicit argument */
5498 c->u->u_metadata.u_argcount = 1;
5499 ADDOP_I(c, loc, LOAD_FAST, 0);
5500 }
5501 else {
5502 /* Sub-iter - calculate on the fly */
5503 VISIT(c, expr, gen->iter);
5504 ADDOP(c, LOC(gen->iter), GET_AITER);
5505 }
5506 }
5507
5508 USE_LABEL(c, start);
5509 /* Runtime will push a block here, so we need to account for that */
5510 RETURN_IF_ERROR(
5511 compiler_push_fblock(c, loc, ASYNC_COMPREHENSION_GENERATOR,
5512 start, NO_LABEL, NULL));
5513
5514 ADDOP_JUMP(c, loc, SETUP_FINALLY, except);
5515 ADDOP(c, loc, GET_ANEXT);
5516 ADDOP_LOAD_CONST(c, loc, Py_None);
5517 ADD_YIELD_FROM(c, loc, 1);
5518 ADDOP(c, loc, POP_BLOCK);
5519 VISIT(c, expr, gen->target);
5520
5521 Py_ssize_t n = asdl_seq_LEN(gen->ifs);
5522 for (Py_ssize_t i = 0; i < n; i++) {
5523 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
5524 RETURN_IF_ERROR(compiler_jump_if(c, loc, e, if_cleanup, 0));
5525 }
5526
5527 depth++;
5528 if (++gen_index < asdl_seq_LEN(generators)) {
5529 RETURN_IF_ERROR(
5530 compiler_comprehension_generator(c, loc,
5531 generators, gen_index, depth,
5532 elt, val, type, 0));
5533 }
5534
5535 location elt_loc = LOC(elt);
5536 /* only append after the last for generator */
5537 if (gen_index >= asdl_seq_LEN(generators)) {
5538 /* comprehension specific code */
5539 switch (type) {
5540 case COMP_GENEXP:
5541 VISIT(c, expr, elt);
5542 ADDOP_YIELD(c, elt_loc);
5543 ADDOP(c, elt_loc, POP_TOP);
5544 break;
5545 case COMP_LISTCOMP:
5546 VISIT(c, expr, elt);
5547 ADDOP_I(c, elt_loc, LIST_APPEND, depth + 1);
5548 break;
5549 case COMP_SETCOMP:
5550 VISIT(c, expr, elt);
5551 ADDOP_I(c, elt_loc, SET_ADD, depth + 1);
5552 break;
5553 case COMP_DICTCOMP:
5554 /* With '{k: v}', k is evaluated before v, so we do
5555 the same. */
5556 VISIT(c, expr, elt);
5557 VISIT(c, expr, val);
5558 elt_loc = LOCATION(elt->lineno,
5559 val->end_lineno,
5560 elt->col_offset,
5561 val->end_col_offset);
5562 ADDOP_I(c, elt_loc, MAP_ADD, depth + 1);
5563 break;
5564 default:
5565 return ERROR;
5566 }
5567 }
5568
5569 USE_LABEL(c, if_cleanup);
5570 ADDOP_JUMP(c, elt_loc, JUMP, start);
5571
5572 compiler_pop_fblock(c, ASYNC_COMPREHENSION_GENERATOR, start);
5573
5574 USE_LABEL(c, except);
5575
5576 ADDOP(c, loc, END_ASYNC_FOR);
5577
5578 return SUCCESS;
5579 }
5580
5581 typedef struct {
5582 PyObject *pushed_locals;
5583 PyObject *temp_symbols;
5584 PyObject *fast_hidden;
5585 jump_target_label cleanup;
5586 jump_target_label end;
5587 } inlined_comprehension_state;
5588
5589 static int
push_inlined_comprehension_state(struct compiler * c,location loc,PySTEntryObject * entry,inlined_comprehension_state * state)5590 push_inlined_comprehension_state(struct compiler *c, location loc,
5591 PySTEntryObject *entry,
5592 inlined_comprehension_state *state)
5593 {
5594 int in_class_block = (c->u->u_ste->ste_type == ClassBlock) && !c->u->u_in_inlined_comp;
5595 c->u->u_in_inlined_comp++;
5596 // iterate over names bound in the comprehension and ensure we isolate
5597 // them from the outer scope as needed
5598 PyObject *k, *v;
5599 Py_ssize_t pos = 0;
5600 while (PyDict_Next(entry->ste_symbols, &pos, &k, &v)) {
5601 assert(PyLong_Check(v));
5602 long symbol = PyLong_AS_LONG(v);
5603 long scope = (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
5604 PyObject *outv = PyDict_GetItemWithError(c->u->u_ste->ste_symbols, k);
5605 if (outv == NULL) {
5606 if (PyErr_Occurred()) {
5607 return ERROR;
5608 }
5609 outv = _PyLong_GetZero();
5610 }
5611 assert(PyLong_CheckExact(outv));
5612 long outsc = (PyLong_AS_LONG(outv) >> SCOPE_OFFSET) & SCOPE_MASK;
5613 // If a name has different scope inside than outside the comprehension,
5614 // we need to temporarily handle it with the right scope while
5615 // compiling the comprehension. If it's free in the comprehension
5616 // scope, no special handling; it should be handled the same as the
5617 // enclosing scope. (If it's free in outer scope and cell in inner
5618 // scope, we can't treat it as both cell and free in the same function,
5619 // but treating it as free throughout is fine; it's *_DEREF
5620 // either way.)
5621 if ((scope != outsc && scope != FREE && !(scope == CELL && outsc == FREE))
5622 || in_class_block) {
5623 if (state->temp_symbols == NULL) {
5624 state->temp_symbols = PyDict_New();
5625 if (state->temp_symbols == NULL) {
5626 return ERROR;
5627 }
5628 }
5629 // update the symbol to the in-comprehension version and save
5630 // the outer version; we'll restore it after running the
5631 // comprehension
5632 Py_INCREF(outv);
5633 if (PyDict_SetItem(c->u->u_ste->ste_symbols, k, v) < 0) {
5634 Py_DECREF(outv);
5635 return ERROR;
5636 }
5637 if (PyDict_SetItem(state->temp_symbols, k, outv) < 0) {
5638 Py_DECREF(outv);
5639 return ERROR;
5640 }
5641 Py_DECREF(outv);
5642 }
5643 // locals handling for names bound in comprehension (DEF_LOCAL |
5644 // DEF_NONLOCAL occurs in assignment expression to nonlocal)
5645 if ((symbol & DEF_LOCAL && !(symbol & DEF_NONLOCAL)) || in_class_block) {
5646 if (!_PyST_IsFunctionLike(c->u->u_ste)) {
5647 // non-function scope: override this name to use fast locals
5648 PyObject *orig;
5649 if (PyDict_GetItemRef(c->u->u_metadata.u_fasthidden, k, &orig) < 0) {
5650 return ERROR;
5651 }
5652 int orig_is_true = (orig == Py_True);
5653 Py_XDECREF(orig);
5654 if (!orig_is_true) {
5655 if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_True) < 0) {
5656 return ERROR;
5657 }
5658 if (state->fast_hidden == NULL) {
5659 state->fast_hidden = PySet_New(NULL);
5660 if (state->fast_hidden == NULL) {
5661 return ERROR;
5662 }
5663 }
5664 if (PySet_Add(state->fast_hidden, k) < 0) {
5665 return ERROR;
5666 }
5667 }
5668 }
5669 // local names bound in comprehension must be isolated from
5670 // outer scope; push existing value (which may be NULL if
5671 // not defined) on stack
5672 if (state->pushed_locals == NULL) {
5673 state->pushed_locals = PyList_New(0);
5674 if (state->pushed_locals == NULL) {
5675 return ERROR;
5676 }
5677 }
5678 // in the case of a cell, this will actually push the cell
5679 // itself to the stack, then we'll create a new one for the
5680 // comprehension and restore the original one after
5681 ADDOP_NAME(c, loc, LOAD_FAST_AND_CLEAR, k, varnames);
5682 if (scope == CELL) {
5683 if (outsc == FREE) {
5684 ADDOP_NAME(c, loc, MAKE_CELL, k, freevars);
5685 } else {
5686 ADDOP_NAME(c, loc, MAKE_CELL, k, cellvars);
5687 }
5688 }
5689 if (PyList_Append(state->pushed_locals, k) < 0) {
5690 return ERROR;
5691 }
5692 }
5693 }
5694 if (state->pushed_locals) {
5695 // Outermost iterable expression was already evaluated and is on the
5696 // stack, we need to swap it back to TOS. This also rotates the order of
5697 // `pushed_locals` on the stack, but this will be reversed when we swap
5698 // out the comprehension result in pop_inlined_comprehension_state
5699 ADDOP_I(c, loc, SWAP, PyList_GET_SIZE(state->pushed_locals) + 1);
5700
5701 // Add our own cleanup handler to restore comprehension locals in case
5702 // of exception, so they have the correct values inside an exception
5703 // handler or finally block.
5704 NEW_JUMP_TARGET_LABEL(c, cleanup);
5705 state->cleanup = cleanup;
5706 NEW_JUMP_TARGET_LABEL(c, end);
5707 state->end = end;
5708
5709 // no need to push an fblock for this "virtual" try/finally; there can't
5710 // be return/continue/break inside a comprehension
5711 ADDOP_JUMP(c, loc, SETUP_FINALLY, cleanup);
5712 }
5713
5714 return SUCCESS;
5715 }
5716
5717 static int
restore_inlined_comprehension_locals(struct compiler * c,location loc,inlined_comprehension_state state)5718 restore_inlined_comprehension_locals(struct compiler *c, location loc,
5719 inlined_comprehension_state state)
5720 {
5721 PyObject *k;
5722 // pop names we pushed to stack earlier
5723 Py_ssize_t npops = PyList_GET_SIZE(state.pushed_locals);
5724 // Preserve the comprehension result (or exception) as TOS. This
5725 // reverses the SWAP we did in push_inlined_comprehension_state to get
5726 // the outermost iterable to TOS, so we can still just iterate
5727 // pushed_locals in simple reverse order
5728 ADDOP_I(c, loc, SWAP, npops + 1);
5729 for (Py_ssize_t i = npops - 1; i >= 0; --i) {
5730 k = PyList_GetItem(state.pushed_locals, i);
5731 if (k == NULL) {
5732 return ERROR;
5733 }
5734 ADDOP_NAME(c, loc, STORE_FAST_MAYBE_NULL, k, varnames);
5735 }
5736 return SUCCESS;
5737 }
5738
5739 static int
pop_inlined_comprehension_state(struct compiler * c,location loc,inlined_comprehension_state state)5740 pop_inlined_comprehension_state(struct compiler *c, location loc,
5741 inlined_comprehension_state state)
5742 {
5743 c->u->u_in_inlined_comp--;
5744 PyObject *k, *v;
5745 Py_ssize_t pos = 0;
5746 if (state.temp_symbols) {
5747 while (PyDict_Next(state.temp_symbols, &pos, &k, &v)) {
5748 if (PyDict_SetItem(c->u->u_ste->ste_symbols, k, v)) {
5749 return ERROR;
5750 }
5751 }
5752 Py_CLEAR(state.temp_symbols);
5753 }
5754 if (state.pushed_locals) {
5755 ADDOP(c, NO_LOCATION, POP_BLOCK);
5756 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, state.end);
5757
5758 // cleanup from an exception inside the comprehension
5759 USE_LABEL(c, state.cleanup);
5760 // discard incomplete comprehension result (beneath exc on stack)
5761 ADDOP_I(c, NO_LOCATION, SWAP, 2);
5762 ADDOP(c, NO_LOCATION, POP_TOP);
5763 if (restore_inlined_comprehension_locals(c, loc, state) < 0) {
5764 return ERROR;
5765 }
5766 ADDOP_I(c, NO_LOCATION, RERAISE, 0);
5767
5768 USE_LABEL(c, state.end);
5769 if (restore_inlined_comprehension_locals(c, loc, state) < 0) {
5770 return ERROR;
5771 }
5772 Py_CLEAR(state.pushed_locals);
5773 }
5774 if (state.fast_hidden) {
5775 while (PySet_Size(state.fast_hidden) > 0) {
5776 PyObject *k = PySet_Pop(state.fast_hidden);
5777 if (k == NULL) {
5778 return ERROR;
5779 }
5780 // we set to False instead of clearing, so we can track which names
5781 // were temporarily fast-locals and should use CO_FAST_HIDDEN
5782 if (PyDict_SetItem(c->u->u_metadata.u_fasthidden, k, Py_False)) {
5783 Py_DECREF(k);
5784 return ERROR;
5785 }
5786 Py_DECREF(k);
5787 }
5788 Py_CLEAR(state.fast_hidden);
5789 }
5790 return SUCCESS;
5791 }
5792
5793 static inline int
compiler_comprehension_iter(struct compiler * c,comprehension_ty comp)5794 compiler_comprehension_iter(struct compiler *c, comprehension_ty comp)
5795 {
5796 VISIT(c, expr, comp->iter);
5797 if (comp->is_async) {
5798 ADDOP(c, LOC(comp->iter), GET_AITER);
5799 }
5800 else {
5801 ADDOP(c, LOC(comp->iter), GET_ITER);
5802 }
5803 return SUCCESS;
5804 }
5805
5806 static int
compiler_comprehension(struct compiler * c,expr_ty e,int type,identifier name,asdl_comprehension_seq * generators,expr_ty elt,expr_ty val)5807 compiler_comprehension(struct compiler *c, expr_ty e, int type,
5808 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
5809 expr_ty val)
5810 {
5811 PyCodeObject *co = NULL;
5812 inlined_comprehension_state inline_state = {NULL, NULL, NULL, NO_LABEL, NO_LABEL};
5813 comprehension_ty outermost;
5814 int scope_type = c->u->u_scope_type;
5815 int is_top_level_await = IS_TOP_LEVEL_AWAIT(c);
5816 PySTEntryObject *entry = _PySymtable_Lookup(c->c_st, (void *)e);
5817 if (entry == NULL) {
5818 goto error;
5819 }
5820 int is_inlined = entry->ste_comp_inlined;
5821 int is_async_generator = entry->ste_coroutine;
5822
5823 location loc = LOC(e);
5824
5825 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
5826 if (is_inlined) {
5827 if (compiler_comprehension_iter(c, outermost)) {
5828 goto error;
5829 }
5830 if (push_inlined_comprehension_state(c, loc, entry, &inline_state)) {
5831 goto error;
5832 }
5833 }
5834 else {
5835 if (compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
5836 (void *)e, e->lineno) < 0)
5837 {
5838 goto error;
5839 }
5840 }
5841 Py_CLEAR(entry);
5842
5843 if (is_async_generator && type != COMP_GENEXP &&
5844 scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
5845 scope_type != COMPILER_SCOPE_COMPREHENSION &&
5846 !is_top_level_await)
5847 {
5848 compiler_error(c, loc, "asynchronous comprehension outside of "
5849 "an asynchronous function");
5850 goto error_in_scope;
5851 }
5852
5853 if (type != COMP_GENEXP) {
5854 int op;
5855 switch (type) {
5856 case COMP_LISTCOMP:
5857 op = BUILD_LIST;
5858 break;
5859 case COMP_SETCOMP:
5860 op = BUILD_SET;
5861 break;
5862 case COMP_DICTCOMP:
5863 op = BUILD_MAP;
5864 break;
5865 default:
5866 PyErr_Format(PyExc_SystemError,
5867 "unknown comprehension type %d", type);
5868 goto error_in_scope;
5869 }
5870
5871 ADDOP_I(c, loc, op, 0);
5872 if (is_inlined) {
5873 ADDOP_I(c, loc, SWAP, 2);
5874 }
5875 }
5876
5877 if (compiler_comprehension_generator(c, loc, generators, 0, 0,
5878 elt, val, type, is_inlined) < 0) {
5879 goto error_in_scope;
5880 }
5881
5882 if (is_inlined) {
5883 if (pop_inlined_comprehension_state(c, loc, inline_state)) {
5884 goto error;
5885 }
5886 return SUCCESS;
5887 }
5888
5889 if (type != COMP_GENEXP) {
5890 ADDOP(c, LOC(e), RETURN_VALUE);
5891 }
5892 if (type == COMP_GENEXP) {
5893 if (wrap_in_stopiteration_handler(c) < 0) {
5894 goto error_in_scope;
5895 }
5896 }
5897
5898 co = optimize_and_assemble(c, 1);
5899 compiler_exit_scope(c);
5900 if (is_top_level_await && is_async_generator){
5901 c->u->u_ste->ste_coroutine = 1;
5902 }
5903 if (co == NULL) {
5904 goto error;
5905 }
5906
5907 loc = LOC(e);
5908 if (compiler_make_closure(c, loc, co, 0) < 0) {
5909 goto error;
5910 }
5911 Py_CLEAR(co);
5912
5913 if (compiler_comprehension_iter(c, outermost)) {
5914 goto error;
5915 }
5916
5917 ADDOP_I(c, loc, CALL, 0);
5918
5919 if (is_async_generator && type != COMP_GENEXP) {
5920 ADDOP_I(c, loc, GET_AWAITABLE, 0);
5921 ADDOP_LOAD_CONST(c, loc, Py_None);
5922 ADD_YIELD_FROM(c, loc, 1);
5923 }
5924
5925 return SUCCESS;
5926 error_in_scope:
5927 if (!is_inlined) {
5928 compiler_exit_scope(c);
5929 }
5930 error:
5931 Py_XDECREF(co);
5932 Py_XDECREF(entry);
5933 Py_XDECREF(inline_state.pushed_locals);
5934 Py_XDECREF(inline_state.temp_symbols);
5935 Py_XDECREF(inline_state.fast_hidden);
5936 return ERROR;
5937 }
5938
5939 static int
compiler_genexp(struct compiler * c,expr_ty e)5940 compiler_genexp(struct compiler *c, expr_ty e)
5941 {
5942 assert(e->kind == GeneratorExp_kind);
5943 _Py_DECLARE_STR(anon_genexpr, "<genexpr>");
5944 return compiler_comprehension(c, e, COMP_GENEXP, &_Py_STR(anon_genexpr),
5945 e->v.GeneratorExp.generators,
5946 e->v.GeneratorExp.elt, NULL);
5947 }
5948
5949 static int
compiler_listcomp(struct compiler * c,expr_ty e)5950 compiler_listcomp(struct compiler *c, expr_ty e)
5951 {
5952 assert(e->kind == ListComp_kind);
5953 _Py_DECLARE_STR(anon_listcomp, "<listcomp>");
5954 return compiler_comprehension(c, e, COMP_LISTCOMP, &_Py_STR(anon_listcomp),
5955 e->v.ListComp.generators,
5956 e->v.ListComp.elt, NULL);
5957 }
5958
5959 static int
compiler_setcomp(struct compiler * c,expr_ty e)5960 compiler_setcomp(struct compiler *c, expr_ty e)
5961 {
5962 assert(e->kind == SetComp_kind);
5963 _Py_DECLARE_STR(anon_setcomp, "<setcomp>");
5964 return compiler_comprehension(c, e, COMP_SETCOMP, &_Py_STR(anon_setcomp),
5965 e->v.SetComp.generators,
5966 e->v.SetComp.elt, NULL);
5967 }
5968
5969
5970 static int
compiler_dictcomp(struct compiler * c,expr_ty e)5971 compiler_dictcomp(struct compiler *c, expr_ty e)
5972 {
5973 assert(e->kind == DictComp_kind);
5974 _Py_DECLARE_STR(anon_dictcomp, "<dictcomp>");
5975 return compiler_comprehension(c, e, COMP_DICTCOMP, &_Py_STR(anon_dictcomp),
5976 e->v.DictComp.generators,
5977 e->v.DictComp.key, e->v.DictComp.value);
5978 }
5979
5980
5981 static int
compiler_visit_keyword(struct compiler * c,keyword_ty k)5982 compiler_visit_keyword(struct compiler *c, keyword_ty k)
5983 {
5984 VISIT(c, expr, k->value);
5985 return SUCCESS;
5986 }
5987
5988
5989 static int
compiler_with_except_finish(struct compiler * c,jump_target_label cleanup)5990 compiler_with_except_finish(struct compiler *c, jump_target_label cleanup) {
5991 NEW_JUMP_TARGET_LABEL(c, suppress);
5992 ADDOP(c, NO_LOCATION, TO_BOOL);
5993 ADDOP_JUMP(c, NO_LOCATION, POP_JUMP_IF_TRUE, suppress);
5994 ADDOP_I(c, NO_LOCATION, RERAISE, 2);
5995
5996 USE_LABEL(c, suppress);
5997 ADDOP(c, NO_LOCATION, POP_TOP); /* exc_value */
5998 ADDOP(c, NO_LOCATION, POP_BLOCK);
5999 ADDOP(c, NO_LOCATION, POP_EXCEPT);
6000 ADDOP(c, NO_LOCATION, POP_TOP);
6001 ADDOP(c, NO_LOCATION, POP_TOP);
6002 NEW_JUMP_TARGET_LABEL(c, exit);
6003 ADDOP_JUMP(c, NO_LOCATION, JUMP_NO_INTERRUPT, exit);
6004
6005 USE_LABEL(c, cleanup);
6006 POP_EXCEPT_AND_RERAISE(c, NO_LOCATION);
6007
6008 USE_LABEL(c, exit);
6009 return SUCCESS;
6010 }
6011
6012 /*
6013 Implements the async with statement.
6014
6015 The semantics outlined in that PEP are as follows:
6016
6017 async with EXPR as VAR:
6018 BLOCK
6019
6020 It is implemented roughly as:
6021
6022 context = EXPR
6023 exit = context.__aexit__ # not calling it
6024 value = await context.__aenter__()
6025 try:
6026 VAR = value # if VAR present in the syntax
6027 BLOCK
6028 finally:
6029 if an exception was raised:
6030 exc = copy of (exception, instance, traceback)
6031 else:
6032 exc = (None, None, None)
6033 if not (await exit(*exc)):
6034 raise
6035 */
6036 static int
compiler_async_with(struct compiler * c,stmt_ty s,int pos)6037 compiler_async_with(struct compiler *c, stmt_ty s, int pos)
6038 {
6039 location loc = LOC(s);
6040 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
6041
6042 assert(s->kind == AsyncWith_kind);
6043 if (IS_TOP_LEVEL_AWAIT(c)){
6044 c->u->u_ste->ste_coroutine = 1;
6045 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
6046 return compiler_error(c, loc, "'async with' outside async function");
6047 }
6048
6049 NEW_JUMP_TARGET_LABEL(c, block);
6050 NEW_JUMP_TARGET_LABEL(c, final);
6051 NEW_JUMP_TARGET_LABEL(c, exit);
6052 NEW_JUMP_TARGET_LABEL(c, cleanup);
6053
6054 /* Evaluate EXPR */
6055 VISIT(c, expr, item->context_expr);
6056 loc = LOC(item->context_expr);
6057 ADDOP(c, loc, BEFORE_ASYNC_WITH);
6058 ADDOP_I(c, loc, GET_AWAITABLE, 1);
6059 ADDOP_LOAD_CONST(c, loc, Py_None);
6060 ADD_YIELD_FROM(c, loc, 1);
6061
6062 ADDOP_JUMP(c, loc, SETUP_WITH, final);
6063
6064 /* SETUP_WITH pushes a finally block. */
6065 USE_LABEL(c, block);
6066 RETURN_IF_ERROR(compiler_push_fblock(c, loc, ASYNC_WITH, block, final, s));
6067
6068 if (item->optional_vars) {
6069 VISIT(c, expr, item->optional_vars);
6070 }
6071 else {
6072 /* Discard result from context.__aenter__() */
6073 ADDOP(c, loc, POP_TOP);
6074 }
6075
6076 pos++;
6077 if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) {
6078 /* BLOCK code */
6079 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
6080 }
6081 else {
6082 RETURN_IF_ERROR(compiler_async_with(c, s, pos));
6083 }
6084
6085 compiler_pop_fblock(c, ASYNC_WITH, block);
6086
6087 ADDOP(c, loc, POP_BLOCK);
6088 /* End of body; start the cleanup */
6089
6090 /* For successful outcome:
6091 * call __exit__(None, None, None)
6092 */
6093 RETURN_IF_ERROR(compiler_call_exit_with_nones(c, loc));
6094 ADDOP_I(c, loc, GET_AWAITABLE, 2);
6095 ADDOP_LOAD_CONST(c, loc, Py_None);
6096 ADD_YIELD_FROM(c, loc, 1);
6097
6098 ADDOP(c, loc, POP_TOP);
6099
6100 ADDOP_JUMP(c, loc, JUMP, exit);
6101
6102 /* For exceptional outcome: */
6103 USE_LABEL(c, final);
6104
6105 ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup);
6106 ADDOP(c, loc, PUSH_EXC_INFO);
6107 ADDOP(c, loc, WITH_EXCEPT_START);
6108 ADDOP_I(c, loc, GET_AWAITABLE, 2);
6109 ADDOP_LOAD_CONST(c, loc, Py_None);
6110 ADD_YIELD_FROM(c, loc, 1);
6111 RETURN_IF_ERROR(compiler_with_except_finish(c, cleanup));
6112
6113 USE_LABEL(c, exit);
6114 return SUCCESS;
6115 }
6116
6117
6118 /*
6119 Implements the with statement from PEP 343.
6120 with EXPR as VAR:
6121 BLOCK
6122 is implemented as:
6123 <code for EXPR>
6124 SETUP_WITH E
6125 <code to store to VAR> or POP_TOP
6126 <code for BLOCK>
6127 LOAD_CONST (None, None, None)
6128 CALL_FUNCTION_EX 0
6129 JUMP EXIT
6130 E: WITH_EXCEPT_START (calls EXPR.__exit__)
6131 POP_JUMP_IF_TRUE T:
6132 RERAISE
6133 T: POP_TOP (remove exception from stack)
6134 POP_EXCEPT
6135 POP_TOP
6136 EXIT:
6137 */
6138
6139 static int
compiler_with(struct compiler * c,stmt_ty s,int pos)6140 compiler_with(struct compiler *c, stmt_ty s, int pos)
6141 {
6142 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
6143
6144 assert(s->kind == With_kind);
6145
6146 NEW_JUMP_TARGET_LABEL(c, block);
6147 NEW_JUMP_TARGET_LABEL(c, final);
6148 NEW_JUMP_TARGET_LABEL(c, exit);
6149 NEW_JUMP_TARGET_LABEL(c, cleanup);
6150
6151 /* Evaluate EXPR */
6152 VISIT(c, expr, item->context_expr);
6153 /* Will push bound __exit__ */
6154 location loc = LOC(item->context_expr);
6155 ADDOP(c, loc, BEFORE_WITH);
6156 ADDOP_JUMP(c, loc, SETUP_WITH, final);
6157
6158 /* SETUP_WITH pushes a finally block. */
6159 USE_LABEL(c, block);
6160 RETURN_IF_ERROR(compiler_push_fblock(c, loc, WITH, block, final, s));
6161
6162 if (item->optional_vars) {
6163 VISIT(c, expr, item->optional_vars);
6164 }
6165 else {
6166 /* Discard result from context.__enter__() */
6167 ADDOP(c, loc, POP_TOP);
6168 }
6169
6170 pos++;
6171 if (pos == asdl_seq_LEN(s->v.With.items)) {
6172 /* BLOCK code */
6173 VISIT_SEQ(c, stmt, s->v.With.body)
6174 }
6175 else {
6176 RETURN_IF_ERROR(compiler_with(c, s, pos));
6177 }
6178
6179 ADDOP(c, NO_LOCATION, POP_BLOCK);
6180 compiler_pop_fblock(c, WITH, block);
6181
6182 /* End of body; start the cleanup. */
6183
6184 /* For successful outcome:
6185 * call __exit__(None, None, None)
6186 */
6187 RETURN_IF_ERROR(compiler_call_exit_with_nones(c, loc));
6188 ADDOP(c, loc, POP_TOP);
6189 ADDOP_JUMP(c, loc, JUMP, exit);
6190
6191 /* For exceptional outcome: */
6192 USE_LABEL(c, final);
6193
6194 ADDOP_JUMP(c, loc, SETUP_CLEANUP, cleanup);
6195 ADDOP(c, loc, PUSH_EXC_INFO);
6196 ADDOP(c, loc, WITH_EXCEPT_START);
6197 RETURN_IF_ERROR(compiler_with_except_finish(c, cleanup));
6198
6199 USE_LABEL(c, exit);
6200 return SUCCESS;
6201 }
6202
6203 static int
compiler_visit_expr1(struct compiler * c,expr_ty e)6204 compiler_visit_expr1(struct compiler *c, expr_ty e)
6205 {
6206 location loc = LOC(e);
6207 switch (e->kind) {
6208 case NamedExpr_kind:
6209 VISIT(c, expr, e->v.NamedExpr.value);
6210 ADDOP_I(c, loc, COPY, 1);
6211 VISIT(c, expr, e->v.NamedExpr.target);
6212 break;
6213 case BoolOp_kind:
6214 return compiler_boolop(c, e);
6215 case BinOp_kind:
6216 VISIT(c, expr, e->v.BinOp.left);
6217 VISIT(c, expr, e->v.BinOp.right);
6218 ADDOP_BINARY(c, loc, e->v.BinOp.op);
6219 break;
6220 case UnaryOp_kind:
6221 VISIT(c, expr, e->v.UnaryOp.operand);
6222 if (e->v.UnaryOp.op == UAdd) {
6223 ADDOP_I(c, loc, CALL_INTRINSIC_1, INTRINSIC_UNARY_POSITIVE);
6224 }
6225 else if (e->v.UnaryOp.op == Not) {
6226 ADDOP(c, loc, TO_BOOL);
6227 ADDOP(c, loc, UNARY_NOT);
6228 }
6229 else {
6230 ADDOP(c, loc, unaryop(e->v.UnaryOp.op));
6231 }
6232 break;
6233 case Lambda_kind:
6234 return compiler_lambda(c, e);
6235 case IfExp_kind:
6236 return compiler_ifexp(c, e);
6237 case Dict_kind:
6238 return compiler_dict(c, e);
6239 case Set_kind:
6240 return compiler_set(c, e);
6241 case GeneratorExp_kind:
6242 return compiler_genexp(c, e);
6243 case ListComp_kind:
6244 return compiler_listcomp(c, e);
6245 case SetComp_kind:
6246 return compiler_setcomp(c, e);
6247 case DictComp_kind:
6248 return compiler_dictcomp(c, e);
6249 case Yield_kind:
6250 if (!_PyST_IsFunctionLike(c->u->u_ste)) {
6251 return compiler_error(c, loc, "'yield' outside function");
6252 }
6253 if (e->v.Yield.value) {
6254 VISIT(c, expr, e->v.Yield.value);
6255 }
6256 else {
6257 ADDOP_LOAD_CONST(c, loc, Py_None);
6258 }
6259 ADDOP_YIELD(c, loc);
6260 break;
6261 case YieldFrom_kind:
6262 if (!_PyST_IsFunctionLike(c->u->u_ste)) {
6263 return compiler_error(c, loc, "'yield from' outside function");
6264 }
6265 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) {
6266 return compiler_error(c, loc, "'yield from' inside async function");
6267 }
6268 VISIT(c, expr, e->v.YieldFrom.value);
6269 ADDOP(c, loc, GET_YIELD_FROM_ITER);
6270 ADDOP_LOAD_CONST(c, loc, Py_None);
6271 ADD_YIELD_FROM(c, loc, 0);
6272 break;
6273 case Await_kind:
6274 if (!IS_TOP_LEVEL_AWAIT(c)){
6275 if (!_PyST_IsFunctionLike(c->u->u_ste)) {
6276 return compiler_error(c, loc, "'await' outside function");
6277 }
6278
6279 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
6280 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION) {
6281 return compiler_error(c, loc, "'await' outside async function");
6282 }
6283 }
6284
6285 VISIT(c, expr, e->v.Await.value);
6286 ADDOP_I(c, loc, GET_AWAITABLE, 0);
6287 ADDOP_LOAD_CONST(c, loc, Py_None);
6288 ADD_YIELD_FROM(c, loc, 1);
6289 break;
6290 case Compare_kind:
6291 return compiler_compare(c, e);
6292 case Call_kind:
6293 return compiler_call(c, e);
6294 case Constant_kind:
6295 ADDOP_LOAD_CONST(c, loc, e->v.Constant.value);
6296 break;
6297 case JoinedStr_kind:
6298 return compiler_joined_str(c, e);
6299 case FormattedValue_kind:
6300 return compiler_formatted_value(c, e);
6301 /* The following exprs can be assignment targets. */
6302 case Attribute_kind:
6303 if (e->v.Attribute.ctx == Load && can_optimize_super_call(c, e)) {
6304 RETURN_IF_ERROR(load_args_for_super(c, e->v.Attribute.value));
6305 int opcode = asdl_seq_LEN(e->v.Attribute.value->v.Call.args) ?
6306 LOAD_SUPER_ATTR : LOAD_ZERO_SUPER_ATTR;
6307 ADDOP_NAME(c, loc, opcode, e->v.Attribute.attr, names);
6308 loc = update_start_location_to_match_attr(c, loc, e);
6309 ADDOP(c, loc, NOP);
6310 return SUCCESS;
6311 }
6312 RETURN_IF_ERROR(compiler_maybe_add_static_attribute_to_class(c, e));
6313 VISIT(c, expr, e->v.Attribute.value);
6314 loc = LOC(e);
6315 loc = update_start_location_to_match_attr(c, loc, e);
6316 switch (e->v.Attribute.ctx) {
6317 case Load:
6318 ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names);
6319 break;
6320 case Store:
6321 if (forbidden_name(c, loc, e->v.Attribute.attr, e->v.Attribute.ctx)) {
6322 return ERROR;
6323 }
6324 ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names);
6325 break;
6326 case Del:
6327 ADDOP_NAME(c, loc, DELETE_ATTR, e->v.Attribute.attr, names);
6328 break;
6329 }
6330 break;
6331 case Subscript_kind:
6332 return compiler_subscript(c, e);
6333 case Starred_kind:
6334 switch (e->v.Starred.ctx) {
6335 case Store:
6336 /* In all legitimate cases, the Starred node was already replaced
6337 * by compiler_list/compiler_tuple. XXX: is that okay? */
6338 return compiler_error(c, loc,
6339 "starred assignment target must be in a list or tuple");
6340 default:
6341 return compiler_error(c, loc,
6342 "can't use starred expression here");
6343 }
6344 break;
6345 case Slice_kind:
6346 {
6347 int n = compiler_slice(c, e);
6348 RETURN_IF_ERROR(n);
6349 ADDOP_I(c, loc, BUILD_SLICE, n);
6350 break;
6351 }
6352 case Name_kind:
6353 return compiler_nameop(c, loc, e->v.Name.id, e->v.Name.ctx);
6354 /* child nodes of List and Tuple will have expr_context set */
6355 case List_kind:
6356 return compiler_list(c, e);
6357 case Tuple_kind:
6358 return compiler_tuple(c, e);
6359 }
6360 return SUCCESS;
6361 }
6362
6363 static int
compiler_visit_expr(struct compiler * c,expr_ty e)6364 compiler_visit_expr(struct compiler *c, expr_ty e)
6365 {
6366 int res = compiler_visit_expr1(c, e);
6367 return res;
6368 }
6369
6370 static bool
is_two_element_slice(expr_ty s)6371 is_two_element_slice(expr_ty s)
6372 {
6373 return s->kind == Slice_kind &&
6374 s->v.Slice.step == NULL;
6375 }
6376
6377 static int
compiler_augassign(struct compiler * c,stmt_ty s)6378 compiler_augassign(struct compiler *c, stmt_ty s)
6379 {
6380 assert(s->kind == AugAssign_kind);
6381 expr_ty e = s->v.AugAssign.target;
6382
6383 location loc = LOC(e);
6384
6385 switch (e->kind) {
6386 case Attribute_kind:
6387 VISIT(c, expr, e->v.Attribute.value);
6388 ADDOP_I(c, loc, COPY, 1);
6389 loc = update_start_location_to_match_attr(c, loc, e);
6390 ADDOP_NAME(c, loc, LOAD_ATTR, e->v.Attribute.attr, names);
6391 break;
6392 case Subscript_kind:
6393 VISIT(c, expr, e->v.Subscript.value);
6394 if (is_two_element_slice(e->v.Subscript.slice)) {
6395 RETURN_IF_ERROR(compiler_slice(c, e->v.Subscript.slice));
6396 ADDOP_I(c, loc, COPY, 3);
6397 ADDOP_I(c, loc, COPY, 3);
6398 ADDOP_I(c, loc, COPY, 3);
6399 ADDOP(c, loc, BINARY_SLICE);
6400 }
6401 else {
6402 VISIT(c, expr, e->v.Subscript.slice);
6403 ADDOP_I(c, loc, COPY, 2);
6404 ADDOP_I(c, loc, COPY, 2);
6405 ADDOP(c, loc, BINARY_SUBSCR);
6406 }
6407 break;
6408 case Name_kind:
6409 RETURN_IF_ERROR(compiler_nameop(c, loc, e->v.Name.id, Load));
6410 break;
6411 default:
6412 PyErr_Format(PyExc_SystemError,
6413 "invalid node type (%d) for augmented assignment",
6414 e->kind);
6415 return ERROR;
6416 }
6417
6418 loc = LOC(s);
6419
6420 VISIT(c, expr, s->v.AugAssign.value);
6421 ADDOP_INPLACE(c, loc, s->v.AugAssign.op);
6422
6423 loc = LOC(e);
6424
6425 switch (e->kind) {
6426 case Attribute_kind:
6427 loc = update_start_location_to_match_attr(c, loc, e);
6428 ADDOP_I(c, loc, SWAP, 2);
6429 ADDOP_NAME(c, loc, STORE_ATTR, e->v.Attribute.attr, names);
6430 break;
6431 case Subscript_kind:
6432 if (is_two_element_slice(e->v.Subscript.slice)) {
6433 ADDOP_I(c, loc, SWAP, 4);
6434 ADDOP_I(c, loc, SWAP, 3);
6435 ADDOP_I(c, loc, SWAP, 2);
6436 ADDOP(c, loc, STORE_SLICE);
6437 }
6438 else {
6439 ADDOP_I(c, loc, SWAP, 3);
6440 ADDOP_I(c, loc, SWAP, 2);
6441 ADDOP(c, loc, STORE_SUBSCR);
6442 }
6443 break;
6444 case Name_kind:
6445 return compiler_nameop(c, loc, e->v.Name.id, Store);
6446 default:
6447 Py_UNREACHABLE();
6448 }
6449 return SUCCESS;
6450 }
6451
6452 static int
check_ann_expr(struct compiler * c,expr_ty e)6453 check_ann_expr(struct compiler *c, expr_ty e)
6454 {
6455 VISIT(c, expr, e);
6456 ADDOP(c, LOC(e), POP_TOP);
6457 return SUCCESS;
6458 }
6459
6460 static int
check_annotation(struct compiler * c,stmt_ty s)6461 check_annotation(struct compiler *c, stmt_ty s)
6462 {
6463 /* Annotations of complex targets does not produce anything
6464 under annotations future */
6465 if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
6466 return SUCCESS;
6467 }
6468
6469 /* Annotations are only evaluated in a module or class. */
6470 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
6471 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
6472 return check_ann_expr(c, s->v.AnnAssign.annotation);
6473 }
6474 return SUCCESS;
6475 }
6476
6477 static int
check_ann_subscr(struct compiler * c,expr_ty e)6478 check_ann_subscr(struct compiler *c, expr_ty e)
6479 {
6480 /* We check that everything in a subscript is defined at runtime. */
6481 switch (e->kind) {
6482 case Slice_kind:
6483 if (e->v.Slice.lower && check_ann_expr(c, e->v.Slice.lower) < 0) {
6484 return ERROR;
6485 }
6486 if (e->v.Slice.upper && check_ann_expr(c, e->v.Slice.upper) < 0) {
6487 return ERROR;
6488 }
6489 if (e->v.Slice.step && check_ann_expr(c, e->v.Slice.step) < 0) {
6490 return ERROR;
6491 }
6492 return SUCCESS;
6493 case Tuple_kind: {
6494 /* extended slice */
6495 asdl_expr_seq *elts = e->v.Tuple.elts;
6496 Py_ssize_t i, n = asdl_seq_LEN(elts);
6497 for (i = 0; i < n; i++) {
6498 RETURN_IF_ERROR(check_ann_subscr(c, asdl_seq_GET(elts, i)));
6499 }
6500 return SUCCESS;
6501 }
6502 default:
6503 return check_ann_expr(c, e);
6504 }
6505 }
6506
6507 static int
compiler_annassign(struct compiler * c,stmt_ty s)6508 compiler_annassign(struct compiler *c, stmt_ty s)
6509 {
6510 location loc = LOC(s);
6511 expr_ty targ = s->v.AnnAssign.target;
6512 PyObject* mangled;
6513
6514 assert(s->kind == AnnAssign_kind);
6515
6516 /* We perform the actual assignment first. */
6517 if (s->v.AnnAssign.value) {
6518 VISIT(c, expr, s->v.AnnAssign.value);
6519 VISIT(c, expr, targ);
6520 }
6521 switch (targ->kind) {
6522 case Name_kind:
6523 if (forbidden_name(c, loc, targ->v.Name.id, Store)) {
6524 return ERROR;
6525 }
6526 /* If we have a simple name in a module or class, store annotation. */
6527 if (s->v.AnnAssign.simple &&
6528 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
6529 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
6530 if (c->c_future.ff_features & CO_FUTURE_ANNOTATIONS) {
6531 VISIT(c, annexpr, s->v.AnnAssign.annotation)
6532 }
6533 else {
6534 VISIT(c, expr, s->v.AnnAssign.annotation);
6535 }
6536 ADDOP_NAME(c, loc, LOAD_NAME, &_Py_ID(__annotations__), names);
6537 mangled = _Py_MaybeMangle(c->u->u_private, c->u->u_ste, targ->v.Name.id);
6538 ADDOP_LOAD_CONST_NEW(c, loc, mangled);
6539 ADDOP(c, loc, STORE_SUBSCR);
6540 }
6541 break;
6542 case Attribute_kind:
6543 if (forbidden_name(c, loc, targ->v.Attribute.attr, Store)) {
6544 return ERROR;
6545 }
6546 if (!s->v.AnnAssign.value &&
6547 check_ann_expr(c, targ->v.Attribute.value) < 0) {
6548 return ERROR;
6549 }
6550 break;
6551 case Subscript_kind:
6552 if (!s->v.AnnAssign.value &&
6553 (check_ann_expr(c, targ->v.Subscript.value) < 0 ||
6554 check_ann_subscr(c, targ->v.Subscript.slice) < 0)) {
6555 return ERROR;
6556 }
6557 break;
6558 default:
6559 PyErr_Format(PyExc_SystemError,
6560 "invalid node type (%d) for annotated assignment",
6561 targ->kind);
6562 return ERROR;
6563 }
6564 /* Annotation is evaluated last. */
6565 if (!s->v.AnnAssign.simple && check_annotation(c, s) < 0) {
6566 return ERROR;
6567 }
6568 return SUCCESS;
6569 }
6570
6571 /* Raises a SyntaxError and returns 0.
6572 If something goes wrong, a different exception may be raised.
6573 */
6574
6575 static int
compiler_error(struct compiler * c,location loc,const char * format,...)6576 compiler_error(struct compiler *c, location loc,
6577 const char *format, ...)
6578 {
6579 va_list vargs;
6580 va_start(vargs, format);
6581 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
6582 va_end(vargs);
6583 if (msg == NULL) {
6584 return ERROR;
6585 }
6586 PyObject *loc_obj = PyErr_ProgramTextObject(c->c_filename, loc.lineno);
6587 if (loc_obj == NULL) {
6588 loc_obj = Py_None;
6589 }
6590 PyObject *args = Py_BuildValue("O(OiiOii)", msg, c->c_filename,
6591 loc.lineno, loc.col_offset + 1, loc_obj,
6592 loc.end_lineno, loc.end_col_offset + 1);
6593 Py_DECREF(msg);
6594 if (args == NULL) {
6595 goto exit;
6596 }
6597 PyErr_SetObject(PyExc_SyntaxError, args);
6598 exit:
6599 Py_DECREF(loc_obj);
6600 Py_XDECREF(args);
6601 return ERROR;
6602 }
6603
6604 /* Emits a SyntaxWarning and returns 1 on success.
6605 If a SyntaxWarning raised as error, replaces it with a SyntaxError
6606 and returns 0.
6607 */
6608 static int
compiler_warn(struct compiler * c,location loc,const char * format,...)6609 compiler_warn(struct compiler *c, location loc,
6610 const char *format, ...)
6611 {
6612 va_list vargs;
6613 va_start(vargs, format);
6614 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
6615 va_end(vargs);
6616 if (msg == NULL) {
6617 return ERROR;
6618 }
6619 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
6620 loc.lineno, NULL, NULL) < 0)
6621 {
6622 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
6623 /* Replace the SyntaxWarning exception with a SyntaxError
6624 to get a more accurate error report */
6625 PyErr_Clear();
6626 assert(PyUnicode_AsUTF8(msg) != NULL);
6627 compiler_error(c, loc, PyUnicode_AsUTF8(msg));
6628 }
6629 Py_DECREF(msg);
6630 return ERROR;
6631 }
6632 Py_DECREF(msg);
6633 return SUCCESS;
6634 }
6635
6636 static int
compiler_subscript(struct compiler * c,expr_ty e)6637 compiler_subscript(struct compiler *c, expr_ty e)
6638 {
6639 location loc = LOC(e);
6640 expr_context_ty ctx = e->v.Subscript.ctx;
6641 int op = 0;
6642
6643 if (ctx == Load) {
6644 RETURN_IF_ERROR(check_subscripter(c, e->v.Subscript.value));
6645 RETURN_IF_ERROR(check_index(c, e->v.Subscript.value, e->v.Subscript.slice));
6646 }
6647
6648 VISIT(c, expr, e->v.Subscript.value);
6649 if (is_two_element_slice(e->v.Subscript.slice) && ctx != Del) {
6650 RETURN_IF_ERROR(compiler_slice(c, e->v.Subscript.slice));
6651 if (ctx == Load) {
6652 ADDOP(c, loc, BINARY_SLICE);
6653 }
6654 else {
6655 assert(ctx == Store);
6656 ADDOP(c, loc, STORE_SLICE);
6657 }
6658 }
6659 else {
6660 VISIT(c, expr, e->v.Subscript.slice);
6661 switch (ctx) {
6662 case Load: op = BINARY_SUBSCR; break;
6663 case Store: op = STORE_SUBSCR; break;
6664 case Del: op = DELETE_SUBSCR; break;
6665 }
6666 assert(op);
6667 ADDOP(c, loc, op);
6668 }
6669 return SUCCESS;
6670 }
6671
6672 /* Returns the number of the values emitted,
6673 * thus are needed to build the slice, or -1 if there is an error. */
6674 static int
compiler_slice(struct compiler * c,expr_ty s)6675 compiler_slice(struct compiler *c, expr_ty s)
6676 {
6677 int n = 2;
6678 assert(s->kind == Slice_kind);
6679
6680 /* only handles the cases where BUILD_SLICE is emitted */
6681 if (s->v.Slice.lower) {
6682 VISIT(c, expr, s->v.Slice.lower);
6683 }
6684 else {
6685 ADDOP_LOAD_CONST(c, LOC(s), Py_None);
6686 }
6687
6688 if (s->v.Slice.upper) {
6689 VISIT(c, expr, s->v.Slice.upper);
6690 }
6691 else {
6692 ADDOP_LOAD_CONST(c, LOC(s), Py_None);
6693 }
6694
6695 if (s->v.Slice.step) {
6696 n++;
6697 VISIT(c, expr, s->v.Slice.step);
6698 }
6699 return n;
6700 }
6701
6702
6703 // PEP 634: Structural Pattern Matching
6704
6705 // To keep things simple, all compiler_pattern_* and pattern_helper_* routines
6706 // follow the convention of consuming TOS (the subject for the given pattern)
6707 // and calling jump_to_fail_pop on failure (no match).
6708
6709 // When calling into these routines, it's important that pc->on_top be kept
6710 // updated to reflect the current number of items that we are using on the top
6711 // of the stack: they will be popped on failure, and any name captures will be
6712 // stored *underneath* them on success. This lets us defer all names stores
6713 // until the *entire* pattern matches.
6714
6715 #define WILDCARD_CHECK(N) \
6716 ((N)->kind == MatchAs_kind && !(N)->v.MatchAs.name)
6717
6718 #define WILDCARD_STAR_CHECK(N) \
6719 ((N)->kind == MatchStar_kind && !(N)->v.MatchStar.name)
6720
6721 // Limit permitted subexpressions, even if the parser & AST validator let them through
6722 #define MATCH_VALUE_EXPR(N) \
6723 ((N)->kind == Constant_kind || (N)->kind == Attribute_kind)
6724
6725 // Allocate or resize pc->fail_pop to allow for n items to be popped on failure.
6726 static int
ensure_fail_pop(struct compiler * c,pattern_context * pc,Py_ssize_t n)6727 ensure_fail_pop(struct compiler *c, pattern_context *pc, Py_ssize_t n)
6728 {
6729 Py_ssize_t size = n + 1;
6730 if (size <= pc->fail_pop_size) {
6731 return SUCCESS;
6732 }
6733 Py_ssize_t needed = sizeof(jump_target_label) * size;
6734 jump_target_label *resized = PyMem_Realloc(pc->fail_pop, needed);
6735 if (resized == NULL) {
6736 PyErr_NoMemory();
6737 return ERROR;
6738 }
6739 pc->fail_pop = resized;
6740 while (pc->fail_pop_size < size) {
6741 NEW_JUMP_TARGET_LABEL(c, new_block);
6742 pc->fail_pop[pc->fail_pop_size++] = new_block;
6743 }
6744 return SUCCESS;
6745 }
6746
6747 // Use op to jump to the correct fail_pop block.
6748 static int
jump_to_fail_pop(struct compiler * c,location loc,pattern_context * pc,int op)6749 jump_to_fail_pop(struct compiler *c, location loc,
6750 pattern_context *pc, int op)
6751 {
6752 // Pop any items on the top of the stack, plus any objects we were going to
6753 // capture on success:
6754 Py_ssize_t pops = pc->on_top + PyList_GET_SIZE(pc->stores);
6755 RETURN_IF_ERROR(ensure_fail_pop(c, pc, pops));
6756 ADDOP_JUMP(c, loc, op, pc->fail_pop[pops]);
6757 return SUCCESS;
6758 }
6759
6760 // Build all of the fail_pop blocks and reset fail_pop.
6761 static int
emit_and_reset_fail_pop(struct compiler * c,location loc,pattern_context * pc)6762 emit_and_reset_fail_pop(struct compiler *c, location loc,
6763 pattern_context *pc)
6764 {
6765 if (!pc->fail_pop_size) {
6766 assert(pc->fail_pop == NULL);
6767 return SUCCESS;
6768 }
6769 while (--pc->fail_pop_size) {
6770 USE_LABEL(c, pc->fail_pop[pc->fail_pop_size]);
6771 if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, loc) < 0) {
6772 pc->fail_pop_size = 0;
6773 PyMem_Free(pc->fail_pop);
6774 pc->fail_pop = NULL;
6775 return ERROR;
6776 }
6777 }
6778 USE_LABEL(c, pc->fail_pop[0]);
6779 PyMem_Free(pc->fail_pop);
6780 pc->fail_pop = NULL;
6781 return SUCCESS;
6782 }
6783
6784 static int
compiler_error_duplicate_store(struct compiler * c,location loc,identifier n)6785 compiler_error_duplicate_store(struct compiler *c, location loc, identifier n)
6786 {
6787 return compiler_error(c, loc,
6788 "multiple assignments to name %R in pattern", n);
6789 }
6790
6791 // Duplicate the effect of 3.10's ROT_* instructions using SWAPs.
6792 static int
pattern_helper_rotate(struct compiler * c,location loc,Py_ssize_t count)6793 pattern_helper_rotate(struct compiler *c, location loc, Py_ssize_t count)
6794 {
6795 while (1 < count) {
6796 ADDOP_I(c, loc, SWAP, count--);
6797 }
6798 return SUCCESS;
6799 }
6800
6801 static int
pattern_helper_store_name(struct compiler * c,location loc,identifier n,pattern_context * pc)6802 pattern_helper_store_name(struct compiler *c, location loc,
6803 identifier n, pattern_context *pc)
6804 {
6805 if (n == NULL) {
6806 ADDOP(c, loc, POP_TOP);
6807 return SUCCESS;
6808 }
6809 if (forbidden_name(c, loc, n, Store)) {
6810 return ERROR;
6811 }
6812 // Can't assign to the same name twice:
6813 int duplicate = PySequence_Contains(pc->stores, n);
6814 RETURN_IF_ERROR(duplicate);
6815 if (duplicate) {
6816 return compiler_error_duplicate_store(c, loc, n);
6817 }
6818 // Rotate this object underneath any items we need to preserve:
6819 Py_ssize_t rotations = pc->on_top + PyList_GET_SIZE(pc->stores) + 1;
6820 RETURN_IF_ERROR(pattern_helper_rotate(c, loc, rotations));
6821 RETURN_IF_ERROR(PyList_Append(pc->stores, n));
6822 return SUCCESS;
6823 }
6824
6825
6826 static int
pattern_unpack_helper(struct compiler * c,location loc,asdl_pattern_seq * elts)6827 pattern_unpack_helper(struct compiler *c, location loc,
6828 asdl_pattern_seq *elts)
6829 {
6830 Py_ssize_t n = asdl_seq_LEN(elts);
6831 int seen_star = 0;
6832 for (Py_ssize_t i = 0; i < n; i++) {
6833 pattern_ty elt = asdl_seq_GET(elts, i);
6834 if (elt->kind == MatchStar_kind && !seen_star) {
6835 if ((i >= (1 << 8)) ||
6836 (n-i-1 >= (INT_MAX >> 8))) {
6837 return compiler_error(c, loc,
6838 "too many expressions in "
6839 "star-unpacking sequence pattern");
6840 }
6841 ADDOP_I(c, loc, UNPACK_EX, (i + ((n-i-1) << 8)));
6842 seen_star = 1;
6843 }
6844 else if (elt->kind == MatchStar_kind) {
6845 return compiler_error(c, loc,
6846 "multiple starred expressions in sequence pattern");
6847 }
6848 }
6849 if (!seen_star) {
6850 ADDOP_I(c, loc, UNPACK_SEQUENCE, n);
6851 }
6852 return SUCCESS;
6853 }
6854
6855 static int
pattern_helper_sequence_unpack(struct compiler * c,location loc,asdl_pattern_seq * patterns,Py_ssize_t star,pattern_context * pc)6856 pattern_helper_sequence_unpack(struct compiler *c, location loc,
6857 asdl_pattern_seq *patterns, Py_ssize_t star,
6858 pattern_context *pc)
6859 {
6860 RETURN_IF_ERROR(pattern_unpack_helper(c, loc, patterns));
6861 Py_ssize_t size = asdl_seq_LEN(patterns);
6862 // We've now got a bunch of new subjects on the stack. They need to remain
6863 // there after each subpattern match:
6864 pc->on_top += size;
6865 for (Py_ssize_t i = 0; i < size; i++) {
6866 // One less item to keep track of each time we loop through:
6867 pc->on_top--;
6868 pattern_ty pattern = asdl_seq_GET(patterns, i);
6869 RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc));
6870 }
6871 return SUCCESS;
6872 }
6873
6874 // Like pattern_helper_sequence_unpack, but uses BINARY_SUBSCR instead of
6875 // UNPACK_SEQUENCE / UNPACK_EX. This is more efficient for patterns with a
6876 // starred wildcard like [first, *_] / [first, *_, last] / [*_, last] / etc.
6877 static int
pattern_helper_sequence_subscr(struct compiler * c,location loc,asdl_pattern_seq * patterns,Py_ssize_t star,pattern_context * pc)6878 pattern_helper_sequence_subscr(struct compiler *c, location loc,
6879 asdl_pattern_seq *patterns, Py_ssize_t star,
6880 pattern_context *pc)
6881 {
6882 // We need to keep the subject around for extracting elements:
6883 pc->on_top++;
6884 Py_ssize_t size = asdl_seq_LEN(patterns);
6885 for (Py_ssize_t i = 0; i < size; i++) {
6886 pattern_ty pattern = asdl_seq_GET(patterns, i);
6887 if (WILDCARD_CHECK(pattern)) {
6888 continue;
6889 }
6890 if (i == star) {
6891 assert(WILDCARD_STAR_CHECK(pattern));
6892 continue;
6893 }
6894 ADDOP_I(c, loc, COPY, 1);
6895 if (i < star) {
6896 ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(i));
6897 }
6898 else {
6899 // The subject may not support negative indexing! Compute a
6900 // nonnegative index:
6901 ADDOP(c, loc, GET_LEN);
6902 ADDOP_LOAD_CONST_NEW(c, loc, PyLong_FromSsize_t(size - i));
6903 ADDOP_BINARY(c, loc, Sub);
6904 }
6905 ADDOP(c, loc, BINARY_SUBSCR);
6906 RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc));
6907 }
6908 // Pop the subject, we're done with it:
6909 pc->on_top--;
6910 ADDOP(c, loc, POP_TOP);
6911 return SUCCESS;
6912 }
6913
6914 // Like compiler_pattern, but turn off checks for irrefutability.
6915 static int
compiler_pattern_subpattern(struct compiler * c,pattern_ty p,pattern_context * pc)6916 compiler_pattern_subpattern(struct compiler *c,
6917 pattern_ty p, pattern_context *pc)
6918 {
6919 int allow_irrefutable = pc->allow_irrefutable;
6920 pc->allow_irrefutable = 1;
6921 RETURN_IF_ERROR(compiler_pattern(c, p, pc));
6922 pc->allow_irrefutable = allow_irrefutable;
6923 return SUCCESS;
6924 }
6925
6926 static int
compiler_pattern_as(struct compiler * c,pattern_ty p,pattern_context * pc)6927 compiler_pattern_as(struct compiler *c, pattern_ty p, pattern_context *pc)
6928 {
6929 assert(p->kind == MatchAs_kind);
6930 if (p->v.MatchAs.pattern == NULL) {
6931 // An irrefutable match:
6932 if (!pc->allow_irrefutable) {
6933 if (p->v.MatchAs.name) {
6934 const char *e = "name capture %R makes remaining patterns unreachable";
6935 return compiler_error(c, LOC(p), e, p->v.MatchAs.name);
6936 }
6937 const char *e = "wildcard makes remaining patterns unreachable";
6938 return compiler_error(c, LOC(p), e);
6939 }
6940 return pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc);
6941 }
6942 // Need to make a copy for (possibly) storing later:
6943 pc->on_top++;
6944 ADDOP_I(c, LOC(p), COPY, 1);
6945 RETURN_IF_ERROR(compiler_pattern(c, p->v.MatchAs.pattern, pc));
6946 // Success! Store it:
6947 pc->on_top--;
6948 RETURN_IF_ERROR(pattern_helper_store_name(c, LOC(p), p->v.MatchAs.name, pc));
6949 return SUCCESS;
6950 }
6951
6952 static int
compiler_pattern_star(struct compiler * c,pattern_ty p,pattern_context * pc)6953 compiler_pattern_star(struct compiler *c, pattern_ty p, pattern_context *pc)
6954 {
6955 assert(p->kind == MatchStar_kind);
6956 RETURN_IF_ERROR(
6957 pattern_helper_store_name(c, LOC(p), p->v.MatchStar.name, pc));
6958 return SUCCESS;
6959 }
6960
6961 static int
validate_kwd_attrs(struct compiler * c,asdl_identifier_seq * attrs,asdl_pattern_seq * patterns)6962 validate_kwd_attrs(struct compiler *c, asdl_identifier_seq *attrs, asdl_pattern_seq* patterns)
6963 {
6964 // Any errors will point to the pattern rather than the arg name as the
6965 // parser is only supplying identifiers rather than Name or keyword nodes
6966 Py_ssize_t nattrs = asdl_seq_LEN(attrs);
6967 for (Py_ssize_t i = 0; i < nattrs; i++) {
6968 identifier attr = ((identifier)asdl_seq_GET(attrs, i));
6969 location loc = LOC((pattern_ty) asdl_seq_GET(patterns, i));
6970 if (forbidden_name(c, loc, attr, Store)) {
6971 return ERROR;
6972 }
6973 for (Py_ssize_t j = i + 1; j < nattrs; j++) {
6974 identifier other = ((identifier)asdl_seq_GET(attrs, j));
6975 if (!PyUnicode_Compare(attr, other)) {
6976 location loc = LOC((pattern_ty) asdl_seq_GET(patterns, j));
6977 compiler_error(c, loc, "attribute name repeated in class pattern: %U", attr);
6978 return ERROR;
6979 }
6980 }
6981 }
6982 return SUCCESS;
6983 }
6984
6985 static int
compiler_pattern_class(struct compiler * c,pattern_ty p,pattern_context * pc)6986 compiler_pattern_class(struct compiler *c, pattern_ty p, pattern_context *pc)
6987 {
6988 assert(p->kind == MatchClass_kind);
6989 asdl_pattern_seq *patterns = p->v.MatchClass.patterns;
6990 asdl_identifier_seq *kwd_attrs = p->v.MatchClass.kwd_attrs;
6991 asdl_pattern_seq *kwd_patterns = p->v.MatchClass.kwd_patterns;
6992 Py_ssize_t nargs = asdl_seq_LEN(patterns);
6993 Py_ssize_t nattrs = asdl_seq_LEN(kwd_attrs);
6994 Py_ssize_t nkwd_patterns = asdl_seq_LEN(kwd_patterns);
6995 if (nattrs != nkwd_patterns) {
6996 // AST validator shouldn't let this happen, but if it does,
6997 // just fail, don't crash out of the interpreter
6998 const char * e = "kwd_attrs (%d) / kwd_patterns (%d) length mismatch in class pattern";
6999 return compiler_error(c, LOC(p), e, nattrs, nkwd_patterns);
7000 }
7001 if (INT_MAX < nargs || INT_MAX < nargs + nattrs - 1) {
7002 const char *e = "too many sub-patterns in class pattern %R";
7003 return compiler_error(c, LOC(p), e, p->v.MatchClass.cls);
7004 }
7005 if (nattrs) {
7006 RETURN_IF_ERROR(validate_kwd_attrs(c, kwd_attrs, kwd_patterns));
7007 }
7008 VISIT(c, expr, p->v.MatchClass.cls);
7009 PyObject *attr_names = PyTuple_New(nattrs);
7010 if (attr_names == NULL) {
7011 return ERROR;
7012 }
7013 Py_ssize_t i;
7014 for (i = 0; i < nattrs; i++) {
7015 PyObject *name = asdl_seq_GET(kwd_attrs, i);
7016 PyTuple_SET_ITEM(attr_names, i, Py_NewRef(name));
7017 }
7018 ADDOP_LOAD_CONST_NEW(c, LOC(p), attr_names);
7019 ADDOP_I(c, LOC(p), MATCH_CLASS, nargs);
7020 ADDOP_I(c, LOC(p), COPY, 1);
7021 ADDOP_LOAD_CONST(c, LOC(p), Py_None);
7022 ADDOP_I(c, LOC(p), IS_OP, 1);
7023 // TOS is now a tuple of (nargs + nattrs) attributes (or None):
7024 pc->on_top++;
7025 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7026 ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, nargs + nattrs);
7027 pc->on_top += nargs + nattrs - 1;
7028 for (i = 0; i < nargs + nattrs; i++) {
7029 pc->on_top--;
7030 pattern_ty pattern;
7031 if (i < nargs) {
7032 // Positional:
7033 pattern = asdl_seq_GET(patterns, i);
7034 }
7035 else {
7036 // Keyword:
7037 pattern = asdl_seq_GET(kwd_patterns, i - nargs);
7038 }
7039 if (WILDCARD_CHECK(pattern)) {
7040 ADDOP(c, LOC(p), POP_TOP);
7041 continue;
7042 }
7043 RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc));
7044 }
7045 // Success! Pop the tuple of attributes:
7046 return SUCCESS;
7047 }
7048
7049 static int
compiler_pattern_mapping(struct compiler * c,pattern_ty p,pattern_context * pc)7050 compiler_pattern_mapping(struct compiler *c, pattern_ty p,
7051 pattern_context *pc)
7052 {
7053 assert(p->kind == MatchMapping_kind);
7054 asdl_expr_seq *keys = p->v.MatchMapping.keys;
7055 asdl_pattern_seq *patterns = p->v.MatchMapping.patterns;
7056 Py_ssize_t size = asdl_seq_LEN(keys);
7057 Py_ssize_t npatterns = asdl_seq_LEN(patterns);
7058 if (size != npatterns) {
7059 // AST validator shouldn't let this happen, but if it does,
7060 // just fail, don't crash out of the interpreter
7061 const char * e = "keys (%d) / patterns (%d) length mismatch in mapping pattern";
7062 return compiler_error(c, LOC(p), e, size, npatterns);
7063 }
7064 // We have a double-star target if "rest" is set
7065 PyObject *star_target = p->v.MatchMapping.rest;
7066 // We need to keep the subject on top during the mapping and length checks:
7067 pc->on_top++;
7068 ADDOP(c, LOC(p), MATCH_MAPPING);
7069 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7070 if (!size && !star_target) {
7071 // If the pattern is just "{}", we're done! Pop the subject:
7072 pc->on_top--;
7073 ADDOP(c, LOC(p), POP_TOP);
7074 return SUCCESS;
7075 }
7076 if (size) {
7077 // If the pattern has any keys in it, perform a length check:
7078 ADDOP(c, LOC(p), GET_LEN);
7079 ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size));
7080 ADDOP_COMPARE(c, LOC(p), GtE);
7081 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7082 }
7083 if (INT_MAX < size - 1) {
7084 return compiler_error(c, LOC(p), "too many sub-patterns in mapping pattern");
7085 }
7086 // Collect all of the keys into a tuple for MATCH_KEYS and
7087 // **rest. They can either be dotted names or literals:
7088
7089 // Maintaining a set of Constant_kind kind keys allows us to raise a
7090 // SyntaxError in the case of duplicates.
7091 PyObject *seen = PySet_New(NULL);
7092 if (seen == NULL) {
7093 return ERROR;
7094 }
7095
7096 // NOTE: goto error on failure in the loop below to avoid leaking `seen`
7097 for (Py_ssize_t i = 0; i < size; i++) {
7098 expr_ty key = asdl_seq_GET(keys, i);
7099 if (key == NULL) {
7100 const char *e = "can't use NULL keys in MatchMapping "
7101 "(set 'rest' parameter instead)";
7102 location loc = LOC((pattern_ty) asdl_seq_GET(patterns, i));
7103 compiler_error(c, loc, e);
7104 goto error;
7105 }
7106
7107 if (key->kind == Constant_kind) {
7108 int in_seen = PySet_Contains(seen, key->v.Constant.value);
7109 if (in_seen < 0) {
7110 goto error;
7111 }
7112 if (in_seen) {
7113 const char *e = "mapping pattern checks duplicate key (%R)";
7114 compiler_error(c, LOC(p), e, key->v.Constant.value);
7115 goto error;
7116 }
7117 if (PySet_Add(seen, key->v.Constant.value)) {
7118 goto error;
7119 }
7120 }
7121
7122 else if (key->kind != Attribute_kind) {
7123 const char *e = "mapping pattern keys may only match literals and attribute lookups";
7124 compiler_error(c, LOC(p), e);
7125 goto error;
7126 }
7127 if (compiler_visit_expr(c, key) < 0) {
7128 goto error;
7129 }
7130 }
7131
7132 // all keys have been checked; there are no duplicates
7133 Py_DECREF(seen);
7134
7135 ADDOP_I(c, LOC(p), BUILD_TUPLE, size);
7136 ADDOP(c, LOC(p), MATCH_KEYS);
7137 // There's now a tuple of keys and a tuple of values on top of the subject:
7138 pc->on_top += 2;
7139 ADDOP_I(c, LOC(p), COPY, 1);
7140 ADDOP_LOAD_CONST(c, LOC(p), Py_None);
7141 ADDOP_I(c, LOC(p), IS_OP, 1);
7142 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7143 // So far so good. Use that tuple of values on the stack to match
7144 // sub-patterns against:
7145 ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size);
7146 pc->on_top += size - 1;
7147 for (Py_ssize_t i = 0; i < size; i++) {
7148 pc->on_top--;
7149 pattern_ty pattern = asdl_seq_GET(patterns, i);
7150 RETURN_IF_ERROR(compiler_pattern_subpattern(c, pattern, pc));
7151 }
7152 // If we get this far, it's a match! Whatever happens next should consume
7153 // the tuple of keys and the subject:
7154 pc->on_top -= 2;
7155 if (star_target) {
7156 // If we have a starred name, bind a dict of remaining items to it (this may
7157 // seem a bit inefficient, but keys is rarely big enough to actually impact
7158 // runtime):
7159 // rest = dict(TOS1)
7160 // for key in TOS:
7161 // del rest[key]
7162 ADDOP_I(c, LOC(p), BUILD_MAP, 0); // [subject, keys, empty]
7163 ADDOP_I(c, LOC(p), SWAP, 3); // [empty, keys, subject]
7164 ADDOP_I(c, LOC(p), DICT_UPDATE, 2); // [copy, keys]
7165 ADDOP_I(c, LOC(p), UNPACK_SEQUENCE, size); // [copy, keys...]
7166 while (size) {
7167 ADDOP_I(c, LOC(p), COPY, 1 + size--); // [copy, keys..., copy]
7168 ADDOP_I(c, LOC(p), SWAP, 2); // [copy, keys..., copy, key]
7169 ADDOP(c, LOC(p), DELETE_SUBSCR); // [copy, keys...]
7170 }
7171 RETURN_IF_ERROR(pattern_helper_store_name(c, LOC(p), star_target, pc));
7172 }
7173 else {
7174 ADDOP(c, LOC(p), POP_TOP); // Tuple of keys.
7175 ADDOP(c, LOC(p), POP_TOP); // Subject.
7176 }
7177 return SUCCESS;
7178
7179 error:
7180 Py_DECREF(seen);
7181 return ERROR;
7182 }
7183
7184 static int
compiler_pattern_or(struct compiler * c,pattern_ty p,pattern_context * pc)7185 compiler_pattern_or(struct compiler *c, pattern_ty p, pattern_context *pc)
7186 {
7187 assert(p->kind == MatchOr_kind);
7188 NEW_JUMP_TARGET_LABEL(c, end);
7189 Py_ssize_t size = asdl_seq_LEN(p->v.MatchOr.patterns);
7190 assert(size > 1);
7191 // We're going to be messing with pc. Keep the original info handy:
7192 pattern_context old_pc = *pc;
7193 Py_INCREF(pc->stores);
7194 // control is the list of names bound by the first alternative. It is used
7195 // for checking different name bindings in alternatives, and for correcting
7196 // the order in which extracted elements are placed on the stack.
7197 PyObject *control = NULL;
7198 // NOTE: We can't use returning macros anymore! goto error on error.
7199 for (Py_ssize_t i = 0; i < size; i++) {
7200 pattern_ty alt = asdl_seq_GET(p->v.MatchOr.patterns, i);
7201 PyObject *pc_stores = PyList_New(0);
7202 if (pc_stores == NULL) {
7203 goto error;
7204 }
7205 Py_SETREF(pc->stores, pc_stores);
7206 // An irrefutable sub-pattern must be last, if it is allowed at all:
7207 pc->allow_irrefutable = (i == size - 1) && old_pc.allow_irrefutable;
7208 pc->fail_pop = NULL;
7209 pc->fail_pop_size = 0;
7210 pc->on_top = 0;
7211 if (codegen_addop_i(INSTR_SEQUENCE(c), COPY, 1, LOC(alt)) < 0 ||
7212 compiler_pattern(c, alt, pc) < 0) {
7213 goto error;
7214 }
7215 // Success!
7216 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
7217 if (!i) {
7218 // This is the first alternative, so save its stores as a "control"
7219 // for the others (they can't bind a different set of names, and
7220 // might need to be reordered):
7221 assert(control == NULL);
7222 control = Py_NewRef(pc->stores);
7223 }
7224 else if (nstores != PyList_GET_SIZE(control)) {
7225 goto diff;
7226 }
7227 else if (nstores) {
7228 // There were captures. Check to see if we differ from control:
7229 Py_ssize_t icontrol = nstores;
7230 while (icontrol--) {
7231 PyObject *name = PyList_GET_ITEM(control, icontrol);
7232 Py_ssize_t istores = PySequence_Index(pc->stores, name);
7233 if (istores < 0) {
7234 PyErr_Clear();
7235 goto diff;
7236 }
7237 if (icontrol != istores) {
7238 // Reorder the names on the stack to match the order of the
7239 // names in control. There's probably a better way of doing
7240 // this; the current solution is potentially very
7241 // inefficient when each alternative subpattern binds lots
7242 // of names in different orders. It's fine for reasonable
7243 // cases, though, and the peephole optimizer will ensure
7244 // that the final code is as efficient as possible.
7245 assert(istores < icontrol);
7246 Py_ssize_t rotations = istores + 1;
7247 // Perform the same rotation on pc->stores:
7248 PyObject *rotated = PyList_GetSlice(pc->stores, 0,
7249 rotations);
7250 if (rotated == NULL ||
7251 PyList_SetSlice(pc->stores, 0, rotations, NULL) ||
7252 PyList_SetSlice(pc->stores, icontrol - istores,
7253 icontrol - istores, rotated))
7254 {
7255 Py_XDECREF(rotated);
7256 goto error;
7257 }
7258 Py_DECREF(rotated);
7259 // That just did:
7260 // rotated = pc_stores[:rotations]
7261 // del pc_stores[:rotations]
7262 // pc_stores[icontrol-istores:icontrol-istores] = rotated
7263 // Do the same thing to the stack, using several
7264 // rotations:
7265 while (rotations--) {
7266 if (pattern_helper_rotate(c, LOC(alt), icontrol + 1) < 0) {
7267 goto error;
7268 }
7269 }
7270 }
7271 }
7272 }
7273 assert(control);
7274 if (codegen_addop_j(INSTR_SEQUENCE(c), LOC(alt), JUMP, end) < 0 ||
7275 emit_and_reset_fail_pop(c, LOC(alt), pc) < 0)
7276 {
7277 goto error;
7278 }
7279 }
7280 Py_DECREF(pc->stores);
7281 *pc = old_pc;
7282 Py_INCREF(pc->stores);
7283 // Need to NULL this for the PyMem_Free call in the error block.
7284 old_pc.fail_pop = NULL;
7285 // No match. Pop the remaining copy of the subject and fail:
7286 if (codegen_addop_noarg(INSTR_SEQUENCE(c), POP_TOP, LOC(p)) < 0 ||
7287 jump_to_fail_pop(c, LOC(p), pc, JUMP) < 0) {
7288 goto error;
7289 }
7290
7291 USE_LABEL(c, end);
7292 Py_ssize_t nstores = PyList_GET_SIZE(control);
7293 // There's a bunch of stuff on the stack between where the new stores
7294 // are and where they need to be:
7295 // - The other stores.
7296 // - A copy of the subject.
7297 // - Anything else that may be on top of the stack.
7298 // - Any previous stores we've already stashed away on the stack.
7299 Py_ssize_t nrots = nstores + 1 + pc->on_top + PyList_GET_SIZE(pc->stores);
7300 for (Py_ssize_t i = 0; i < nstores; i++) {
7301 // Rotate this capture to its proper place on the stack:
7302 if (pattern_helper_rotate(c, LOC(p), nrots) < 0) {
7303 goto error;
7304 }
7305 // Update the list of previous stores with this new name, checking for
7306 // duplicates:
7307 PyObject *name = PyList_GET_ITEM(control, i);
7308 int dupe = PySequence_Contains(pc->stores, name);
7309 if (dupe < 0) {
7310 goto error;
7311 }
7312 if (dupe) {
7313 compiler_error_duplicate_store(c, LOC(p), name);
7314 goto error;
7315 }
7316 if (PyList_Append(pc->stores, name)) {
7317 goto error;
7318 }
7319 }
7320 Py_DECREF(old_pc.stores);
7321 Py_DECREF(control);
7322 // NOTE: Returning macros are safe again.
7323 // Pop the copy of the subject:
7324 ADDOP(c, LOC(p), POP_TOP);
7325 return SUCCESS;
7326 diff:
7327 compiler_error(c, LOC(p), "alternative patterns bind different names");
7328 error:
7329 PyMem_Free(old_pc.fail_pop);
7330 Py_DECREF(old_pc.stores);
7331 Py_XDECREF(control);
7332 return ERROR;
7333 }
7334
7335
7336 static int
compiler_pattern_sequence(struct compiler * c,pattern_ty p,pattern_context * pc)7337 compiler_pattern_sequence(struct compiler *c, pattern_ty p,
7338 pattern_context *pc)
7339 {
7340 assert(p->kind == MatchSequence_kind);
7341 asdl_pattern_seq *patterns = p->v.MatchSequence.patterns;
7342 Py_ssize_t size = asdl_seq_LEN(patterns);
7343 Py_ssize_t star = -1;
7344 int only_wildcard = 1;
7345 int star_wildcard = 0;
7346 // Find a starred name, if it exists. There may be at most one:
7347 for (Py_ssize_t i = 0; i < size; i++) {
7348 pattern_ty pattern = asdl_seq_GET(patterns, i);
7349 if (pattern->kind == MatchStar_kind) {
7350 if (star >= 0) {
7351 const char *e = "multiple starred names in sequence pattern";
7352 return compiler_error(c, LOC(p), e);
7353 }
7354 star_wildcard = WILDCARD_STAR_CHECK(pattern);
7355 only_wildcard &= star_wildcard;
7356 star = i;
7357 continue;
7358 }
7359 only_wildcard &= WILDCARD_CHECK(pattern);
7360 }
7361 // We need to keep the subject on top during the sequence and length checks:
7362 pc->on_top++;
7363 ADDOP(c, LOC(p), MATCH_SEQUENCE);
7364 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7365 if (star < 0) {
7366 // No star: len(subject) == size
7367 ADDOP(c, LOC(p), GET_LEN);
7368 ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size));
7369 ADDOP_COMPARE(c, LOC(p), Eq);
7370 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7371 }
7372 else if (size > 1) {
7373 // Star: len(subject) >= size - 1
7374 ADDOP(c, LOC(p), GET_LEN);
7375 ADDOP_LOAD_CONST_NEW(c, LOC(p), PyLong_FromSsize_t(size - 1));
7376 ADDOP_COMPARE(c, LOC(p), GtE);
7377 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7378 }
7379 // Whatever comes next should consume the subject:
7380 pc->on_top--;
7381 if (only_wildcard) {
7382 // Patterns like: [] / [_] / [_, _] / [*_] / [_, *_] / [_, _, *_] / etc.
7383 ADDOP(c, LOC(p), POP_TOP);
7384 }
7385 else if (star_wildcard) {
7386 RETURN_IF_ERROR(pattern_helper_sequence_subscr(c, LOC(p), patterns, star, pc));
7387 }
7388 else {
7389 RETURN_IF_ERROR(pattern_helper_sequence_unpack(c, LOC(p), patterns, star, pc));
7390 }
7391 return SUCCESS;
7392 }
7393
7394 static int
compiler_pattern_value(struct compiler * c,pattern_ty p,pattern_context * pc)7395 compiler_pattern_value(struct compiler *c, pattern_ty p, pattern_context *pc)
7396 {
7397 assert(p->kind == MatchValue_kind);
7398 expr_ty value = p->v.MatchValue.value;
7399 if (!MATCH_VALUE_EXPR(value)) {
7400 const char *e = "patterns may only match literals and attribute lookups";
7401 return compiler_error(c, LOC(p), e);
7402 }
7403 VISIT(c, expr, value);
7404 ADDOP_COMPARE(c, LOC(p), Eq);
7405 ADDOP(c, LOC(p), TO_BOOL);
7406 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7407 return SUCCESS;
7408 }
7409
7410 static int
compiler_pattern_singleton(struct compiler * c,pattern_ty p,pattern_context * pc)7411 compiler_pattern_singleton(struct compiler *c, pattern_ty p, pattern_context *pc)
7412 {
7413 assert(p->kind == MatchSingleton_kind);
7414 ADDOP_LOAD_CONST(c, LOC(p), p->v.MatchSingleton.value);
7415 ADDOP_COMPARE(c, LOC(p), Is);
7416 RETURN_IF_ERROR(jump_to_fail_pop(c, LOC(p), pc, POP_JUMP_IF_FALSE));
7417 return SUCCESS;
7418 }
7419
7420 static int
compiler_pattern(struct compiler * c,pattern_ty p,pattern_context * pc)7421 compiler_pattern(struct compiler *c, pattern_ty p, pattern_context *pc)
7422 {
7423 switch (p->kind) {
7424 case MatchValue_kind:
7425 return compiler_pattern_value(c, p, pc);
7426 case MatchSingleton_kind:
7427 return compiler_pattern_singleton(c, p, pc);
7428 case MatchSequence_kind:
7429 return compiler_pattern_sequence(c, p, pc);
7430 case MatchMapping_kind:
7431 return compiler_pattern_mapping(c, p, pc);
7432 case MatchClass_kind:
7433 return compiler_pattern_class(c, p, pc);
7434 case MatchStar_kind:
7435 return compiler_pattern_star(c, p, pc);
7436 case MatchAs_kind:
7437 return compiler_pattern_as(c, p, pc);
7438 case MatchOr_kind:
7439 return compiler_pattern_or(c, p, pc);
7440 }
7441 // AST validator shouldn't let this happen, but if it does,
7442 // just fail, don't crash out of the interpreter
7443 const char *e = "invalid match pattern node in AST (kind=%d)";
7444 return compiler_error(c, LOC(p), e, p->kind);
7445 }
7446
7447 static int
compiler_match_inner(struct compiler * c,stmt_ty s,pattern_context * pc)7448 compiler_match_inner(struct compiler *c, stmt_ty s, pattern_context *pc)
7449 {
7450 VISIT(c, expr, s->v.Match.subject);
7451 NEW_JUMP_TARGET_LABEL(c, end);
7452 Py_ssize_t cases = asdl_seq_LEN(s->v.Match.cases);
7453 assert(cases > 0);
7454 match_case_ty m = asdl_seq_GET(s->v.Match.cases, cases - 1);
7455 int has_default = WILDCARD_CHECK(m->pattern) && 1 < cases;
7456 for (Py_ssize_t i = 0; i < cases - has_default; i++) {
7457 m = asdl_seq_GET(s->v.Match.cases, i);
7458 // Only copy the subject if we're *not* on the last case:
7459 if (i != cases - has_default - 1) {
7460 ADDOP_I(c, LOC(m->pattern), COPY, 1);
7461 }
7462 pc->stores = PyList_New(0);
7463 if (pc->stores == NULL) {
7464 return ERROR;
7465 }
7466 // Irrefutable cases must be either guarded, last, or both:
7467 pc->allow_irrefutable = m->guard != NULL || i == cases - 1;
7468 pc->fail_pop = NULL;
7469 pc->fail_pop_size = 0;
7470 pc->on_top = 0;
7471 // NOTE: Can't use returning macros here (they'll leak pc->stores)!
7472 if (compiler_pattern(c, m->pattern, pc) < 0) {
7473 Py_DECREF(pc->stores);
7474 return ERROR;
7475 }
7476 assert(!pc->on_top);
7477 // It's a match! Store all of the captured names (they're on the stack).
7478 Py_ssize_t nstores = PyList_GET_SIZE(pc->stores);
7479 for (Py_ssize_t n = 0; n < nstores; n++) {
7480 PyObject *name = PyList_GET_ITEM(pc->stores, n);
7481 if (compiler_nameop(c, LOC(m->pattern), name, Store) < 0) {
7482 Py_DECREF(pc->stores);
7483 return ERROR;
7484 }
7485 }
7486 Py_DECREF(pc->stores);
7487 // NOTE: Returning macros are safe again.
7488 if (m->guard) {
7489 RETURN_IF_ERROR(ensure_fail_pop(c, pc, 0));
7490 RETURN_IF_ERROR(compiler_jump_if(c, LOC(m->pattern), m->guard, pc->fail_pop[0], 0));
7491 }
7492 // Success! Pop the subject off, we're done with it:
7493 if (i != cases - has_default - 1) {
7494 ADDOP(c, LOC(m->pattern), POP_TOP);
7495 }
7496 VISIT_SEQ(c, stmt, m->body);
7497 ADDOP_JUMP(c, NO_LOCATION, JUMP, end);
7498 // If the pattern fails to match, we want the line number of the
7499 // cleanup to be associated with the failed pattern, not the last line
7500 // of the body
7501 RETURN_IF_ERROR(emit_and_reset_fail_pop(c, LOC(m->pattern), pc));
7502 }
7503 if (has_default) {
7504 // A trailing "case _" is common, and lets us save a bit of redundant
7505 // pushing and popping in the loop above:
7506 m = asdl_seq_GET(s->v.Match.cases, cases - 1);
7507 if (cases == 1) {
7508 // No matches. Done with the subject:
7509 ADDOP(c, LOC(m->pattern), POP_TOP);
7510 }
7511 else {
7512 // Show line coverage for default case (it doesn't create bytecode)
7513 ADDOP(c, LOC(m->pattern), NOP);
7514 }
7515 if (m->guard) {
7516 RETURN_IF_ERROR(compiler_jump_if(c, LOC(m->pattern), m->guard, end, 0));
7517 }
7518 VISIT_SEQ(c, stmt, m->body);
7519 }
7520 USE_LABEL(c, end);
7521 return SUCCESS;
7522 }
7523
7524 static int
compiler_match(struct compiler * c,stmt_ty s)7525 compiler_match(struct compiler *c, stmt_ty s)
7526 {
7527 pattern_context pc;
7528 pc.fail_pop = NULL;
7529 int result = compiler_match_inner(c, s, &pc);
7530 PyMem_Free(pc.fail_pop);
7531 return result;
7532 }
7533
7534 #undef WILDCARD_CHECK
7535 #undef WILDCARD_STAR_CHECK
7536
7537 static PyObject *
consts_dict_keys_inorder(PyObject * dict)7538 consts_dict_keys_inorder(PyObject *dict)
7539 {
7540 PyObject *consts, *k, *v;
7541 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
7542
7543 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
7544 if (consts == NULL)
7545 return NULL;
7546 while (PyDict_Next(dict, &pos, &k, &v)) {
7547 i = PyLong_AS_LONG(v);
7548 /* The keys of the dictionary can be tuples wrapping a constant.
7549 * (see dict_add_o and _PyCode_ConstantKey). In that case
7550 * the object we want is always second. */
7551 if (PyTuple_CheckExact(k)) {
7552 k = PyTuple_GET_ITEM(k, 1);
7553 }
7554 assert(i < size);
7555 assert(i >= 0);
7556 PyList_SET_ITEM(consts, i, Py_NewRef(k));
7557 }
7558 return consts;
7559 }
7560
7561 static int
compute_code_flags(struct compiler * c)7562 compute_code_flags(struct compiler *c)
7563 {
7564 PySTEntryObject *ste = c->u->u_ste;
7565 int flags = 0;
7566 if (_PyST_IsFunctionLike(c->u->u_ste)) {
7567 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
7568 if (ste->ste_nested)
7569 flags |= CO_NESTED;
7570 if (ste->ste_generator && !ste->ste_coroutine)
7571 flags |= CO_GENERATOR;
7572 if (!ste->ste_generator && ste->ste_coroutine)
7573 flags |= CO_COROUTINE;
7574 if (ste->ste_generator && ste->ste_coroutine)
7575 flags |= CO_ASYNC_GENERATOR;
7576 if (ste->ste_varargs)
7577 flags |= CO_VARARGS;
7578 if (ste->ste_varkeywords)
7579 flags |= CO_VARKEYWORDS;
7580 }
7581
7582 /* (Only) inherit compilerflags in PyCF_MASK */
7583 flags |= (c->c_flags.cf_flags & PyCF_MASK);
7584
7585 if ((IS_TOP_LEVEL_AWAIT(c)) &&
7586 ste->ste_coroutine &&
7587 !ste->ste_generator) {
7588 flags |= CO_COROUTINE;
7589 }
7590
7591 return flags;
7592 }
7593
7594 // Merge *obj* with constant cache.
7595 // Unlike merge_consts_recursive(), this function doesn't work recursively.
7596 int
_PyCompile_ConstCacheMergeOne(PyObject * const_cache,PyObject ** obj)7597 _PyCompile_ConstCacheMergeOne(PyObject *const_cache, PyObject **obj)
7598 {
7599 assert(PyDict_CheckExact(const_cache));
7600 PyObject *key = _PyCode_ConstantKey(*obj);
7601 if (key == NULL) {
7602 return ERROR;
7603 }
7604
7605 PyObject *t;
7606 int res = PyDict_SetDefaultRef(const_cache, key, key, &t);
7607 Py_DECREF(key);
7608 if (res < 0) {
7609 return ERROR;
7610 }
7611 if (res == 0) { // inserted: obj is new constant.
7612 Py_DECREF(t);
7613 return SUCCESS;
7614 }
7615
7616 if (PyTuple_CheckExact(t)) {
7617 PyObject *item = PyTuple_GET_ITEM(t, 1);
7618 Py_SETREF(*obj, Py_NewRef(item));
7619 Py_DECREF(t);
7620 }
7621 else {
7622 Py_SETREF(*obj, t);
7623 }
7624
7625 return SUCCESS;
7626 }
7627
7628 static int
add_return_at_end(struct compiler * c,int addNone)7629 add_return_at_end(struct compiler *c, int addNone)
7630 {
7631 /* Make sure every instruction stream that falls off the end returns None.
7632 * This also ensures that no jump target offsets are out of bounds.
7633 */
7634 if (addNone) {
7635 ADDOP_LOAD_CONST(c, NO_LOCATION, Py_None);
7636 }
7637 ADDOP(c, NO_LOCATION, RETURN_VALUE);
7638 return SUCCESS;
7639 }
7640
7641 static PyCodeObject *
optimize_and_assemble_code_unit(struct compiler_unit * u,PyObject * const_cache,int code_flags,PyObject * filename)7642 optimize_and_assemble_code_unit(struct compiler_unit *u, PyObject *const_cache,
7643 int code_flags, PyObject *filename)
7644 {
7645 cfg_builder *g = NULL;
7646 instr_sequence optimized_instrs;
7647 memset(&optimized_instrs, 0, sizeof(instr_sequence));
7648
7649 PyCodeObject *co = NULL;
7650 PyObject *consts = consts_dict_keys_inorder(u->u_metadata.u_consts);
7651 if (consts == NULL) {
7652 goto error;
7653 }
7654 g = instr_sequence_to_cfg(u->u_instr_sequence);
7655 if (g == NULL) {
7656 goto error;
7657 }
7658 int nlocals = (int)PyDict_GET_SIZE(u->u_metadata.u_varnames);
7659 int nparams = (int)PyList_GET_SIZE(u->u_ste->ste_varnames);
7660 assert(u->u_metadata.u_firstlineno);
7661
7662 if (_PyCfg_OptimizeCodeUnit(g, consts, const_cache, nlocals,
7663 nparams, u->u_metadata.u_firstlineno) < 0) {
7664 goto error;
7665 }
7666
7667 int stackdepth;
7668 int nlocalsplus;
7669 if (_PyCfg_OptimizedCfgToInstructionSequence(g, &u->u_metadata, code_flags,
7670 &stackdepth, &nlocalsplus,
7671 &optimized_instrs) < 0) {
7672 goto error;
7673 }
7674
7675 /** Assembly **/
7676
7677 co = _PyAssemble_MakeCodeObject(&u->u_metadata, const_cache, consts,
7678 stackdepth, &optimized_instrs, nlocalsplus,
7679 code_flags, filename);
7680
7681 error:
7682 Py_XDECREF(consts);
7683 PyInstructionSequence_Fini(&optimized_instrs);
7684 _PyCfgBuilder_Free(g);
7685 return co;
7686 }
7687
7688 static PyCodeObject *
optimize_and_assemble(struct compiler * c,int addNone)7689 optimize_and_assemble(struct compiler *c, int addNone)
7690 {
7691 struct compiler_unit *u = c->u;
7692 PyObject *const_cache = c->c_const_cache;
7693 PyObject *filename = c->c_filename;
7694
7695 int code_flags = compute_code_flags(c);
7696 if (code_flags < 0) {
7697 return NULL;
7698 }
7699
7700 if (add_return_at_end(c, addNone) < 0) {
7701 return NULL;
7702 }
7703
7704 return optimize_and_assemble_code_unit(u, const_cache, code_flags, filename);
7705 }
7706
7707 /* Access to compiler optimizations for unit tests.
7708 *
7709 * _PyCompile_CodeGen takes and AST, applies code-gen and
7710 * returns the unoptimized CFG as an instruction list.
7711 *
7712 * _PyCompile_OptimizeCfg takes an instruction list, constructs
7713 * a CFG, optimizes it and converts back to an instruction list.
7714 *
7715 * An instruction list is a PyList where each item is either
7716 * a tuple describing a single instruction:
7717 * (opcode, oparg, lineno, end_lineno, col, end_col), or
7718 * a jump target label marking the beginning of a basic block.
7719 */
7720
7721
7722 static PyObject *
cfg_to_instruction_sequence(cfg_builder * g)7723 cfg_to_instruction_sequence(cfg_builder *g)
7724 {
7725 instr_sequence *seq = (instr_sequence *)_PyInstructionSequence_New();
7726 if (seq != NULL) {
7727 if (_PyCfg_ToInstructionSequence(g, seq) < 0) {
7728 goto error;
7729 }
7730 if (_PyInstructionSequence_ApplyLabelMap(seq) < 0) {
7731 goto error;
7732 }
7733 }
7734 return (PyObject*)seq;
7735 error:
7736 PyInstructionSequence_Fini(seq);
7737 return NULL;
7738 }
7739
7740 // C implementation of inspect.cleandoc()
7741 //
7742 // Difference from inspect.cleandoc():
7743 // - Do not remove leading and trailing blank lines to keep lineno.
7744 PyObject *
_PyCompile_CleanDoc(PyObject * doc)7745 _PyCompile_CleanDoc(PyObject *doc)
7746 {
7747 doc = PyObject_CallMethod(doc, "expandtabs", NULL);
7748 if (doc == NULL) {
7749 return NULL;
7750 }
7751
7752 Py_ssize_t doc_size;
7753 const char *doc_utf8 = PyUnicode_AsUTF8AndSize(doc, &doc_size);
7754 if (doc_utf8 == NULL) {
7755 Py_DECREF(doc);
7756 return NULL;
7757 }
7758 const char *p = doc_utf8;
7759 const char *pend = p + doc_size;
7760
7761 // First pass: find minimum indentation of any non-blank lines
7762 // after first line.
7763 while (p < pend && *p++ != '\n') {
7764 }
7765
7766 Py_ssize_t margin = PY_SSIZE_T_MAX;
7767 while (p < pend) {
7768 const char *s = p;
7769 while (*p == ' ') p++;
7770 if (p < pend && *p != '\n') {
7771 margin = Py_MIN(margin, p - s);
7772 }
7773 while (p < pend && *p++ != '\n') {
7774 }
7775 }
7776 if (margin == PY_SSIZE_T_MAX) {
7777 margin = 0;
7778 }
7779
7780 // Second pass: write cleandoc into buff.
7781
7782 // copy first line without leading spaces.
7783 p = doc_utf8;
7784 while (*p == ' ') {
7785 p++;
7786 }
7787 if (p == doc_utf8 && margin == 0 ) {
7788 // doc is already clean.
7789 return doc;
7790 }
7791
7792 char *buff = PyMem_Malloc(doc_size);
7793 if (buff == NULL){
7794 Py_DECREF(doc);
7795 PyErr_NoMemory();
7796 return NULL;
7797 }
7798
7799 char *w = buff;
7800
7801 while (p < pend) {
7802 int ch = *w++ = *p++;
7803 if (ch == '\n') {
7804 break;
7805 }
7806 }
7807
7808 // copy subsequent lines without margin.
7809 while (p < pend) {
7810 for (Py_ssize_t i = 0; i < margin; i++, p++) {
7811 if (*p != ' ') {
7812 assert(*p == '\n' || *p == '\0');
7813 break;
7814 }
7815 }
7816 while (p < pend) {
7817 int ch = *w++ = *p++;
7818 if (ch == '\n') {
7819 break;
7820 }
7821 }
7822 }
7823
7824 Py_DECREF(doc);
7825 PyObject *res = PyUnicode_FromStringAndSize(buff, w - buff);
7826 PyMem_Free(buff);
7827 return res;
7828 }
7829
7830
7831 PyObject *
_PyCompile_CodeGen(PyObject * ast,PyObject * filename,PyCompilerFlags * pflags,int optimize,int compile_mode)7832 _PyCompile_CodeGen(PyObject *ast, PyObject *filename, PyCompilerFlags *pflags,
7833 int optimize, int compile_mode)
7834 {
7835 PyObject *res = NULL;
7836 PyObject *metadata = NULL;
7837
7838 if (!PyAST_Check(ast)) {
7839 PyErr_SetString(PyExc_TypeError, "expected an AST");
7840 return NULL;
7841 }
7842
7843 PyArena *arena = _PyArena_New();
7844 if (arena == NULL) {
7845 return NULL;
7846 }
7847
7848 mod_ty mod = PyAST_obj2mod(ast, arena, compile_mode);
7849 if (mod == NULL || !_PyAST_Validate(mod)) {
7850 _PyArena_Free(arena);
7851 return NULL;
7852 }
7853
7854 struct compiler *c = new_compiler(mod, filename, pflags, optimize, arena);
7855 if (c == NULL) {
7856 _PyArena_Free(arena);
7857 return NULL;
7858 }
7859 c->c_save_nested_seqs = true;
7860
7861 metadata = PyDict_New();
7862 if (metadata == NULL) {
7863 return NULL;
7864 }
7865
7866 if (compiler_enter_anonymous_scope(c, mod) < 0) {
7867 return NULL;
7868 }
7869 if (compiler_codegen(c, mod) < 0) {
7870 goto finally;
7871 }
7872
7873 _PyCompile_CodeUnitMetadata *umd = &c->u->u_metadata;
7874
7875 #define SET_MATADATA_ITEM(key, value) \
7876 if (value != NULL) { \
7877 if (PyDict_SetItemString(metadata, key, value) < 0) goto finally; \
7878 }
7879
7880 SET_MATADATA_ITEM("name", umd->u_name);
7881 SET_MATADATA_ITEM("qualname", umd->u_qualname);
7882 SET_MATADATA_ITEM("consts", umd->u_consts);
7883 SET_MATADATA_ITEM("names", umd->u_names);
7884 SET_MATADATA_ITEM("varnames", umd->u_varnames);
7885 SET_MATADATA_ITEM("cellvars", umd->u_cellvars);
7886 SET_MATADATA_ITEM("freevars", umd->u_freevars);
7887 #undef SET_MATADATA_ITEM
7888
7889 #define SET_MATADATA_INT(key, value) do { \
7890 PyObject *v = PyLong_FromLong((long)value); \
7891 if (v == NULL) goto finally; \
7892 int res = PyDict_SetItemString(metadata, key, v); \
7893 Py_XDECREF(v); \
7894 if (res < 0) goto finally; \
7895 } while (0);
7896
7897 SET_MATADATA_INT("argcount", umd->u_argcount);
7898 SET_MATADATA_INT("posonlyargcount", umd->u_posonlyargcount);
7899 SET_MATADATA_INT("kwonlyargcount", umd->u_kwonlyargcount);
7900 #undef SET_MATADATA_INT
7901
7902 int addNone = mod->kind != Expression_kind;
7903 if (add_return_at_end(c, addNone) < 0) {
7904 goto finally;
7905 }
7906
7907 if (_PyInstructionSequence_ApplyLabelMap(INSTR_SEQUENCE(c)) < 0) {
7908 return NULL;
7909 }
7910 /* Allocate a copy of the instruction sequence on the heap */
7911 res = PyTuple_Pack(2, INSTR_SEQUENCE(c), metadata);
7912
7913 finally:
7914 Py_XDECREF(metadata);
7915 compiler_exit_scope(c);
7916 compiler_free(c);
7917 _PyArena_Free(arena);
7918 return res;
7919 }
7920
7921 PyObject *
_PyCompile_OptimizeCfg(PyObject * seq,PyObject * consts,int nlocals)7922 _PyCompile_OptimizeCfg(PyObject *seq, PyObject *consts, int nlocals)
7923 {
7924 if (!_PyInstructionSequence_Check(seq)) {
7925 PyErr_SetString(PyExc_ValueError, "expected an instruction sequence");
7926 return NULL;
7927 }
7928 PyObject *const_cache = PyDict_New();
7929 if (const_cache == NULL) {
7930 return NULL;
7931 }
7932
7933 PyObject *res = NULL;
7934 cfg_builder *g = instr_sequence_to_cfg((instr_sequence*)seq);
7935 if (g == NULL) {
7936 goto error;
7937 }
7938 int nparams = 0, firstlineno = 1;
7939 if (_PyCfg_OptimizeCodeUnit(g, consts, const_cache, nlocals,
7940 nparams, firstlineno) < 0) {
7941 goto error;
7942 }
7943 res = cfg_to_instruction_sequence(g);
7944 error:
7945 Py_DECREF(const_cache);
7946 _PyCfgBuilder_Free(g);
7947 return res;
7948 }
7949
7950 int _PyCfg_JumpLabelsToTargets(cfg_builder *g);
7951
7952 PyCodeObject *
_PyCompile_Assemble(_PyCompile_CodeUnitMetadata * umd,PyObject * filename,PyObject * seq)7953 _PyCompile_Assemble(_PyCompile_CodeUnitMetadata *umd, PyObject *filename,
7954 PyObject *seq)
7955 {
7956 if (!_PyInstructionSequence_Check(seq)) {
7957 PyErr_SetString(PyExc_TypeError, "expected an instruction sequence");
7958 return NULL;
7959 }
7960 cfg_builder *g = NULL;
7961 PyCodeObject *co = NULL;
7962 instr_sequence optimized_instrs;
7963 memset(&optimized_instrs, 0, sizeof(instr_sequence));
7964
7965 PyObject *const_cache = PyDict_New();
7966 if (const_cache == NULL) {
7967 return NULL;
7968 }
7969
7970 g = instr_sequence_to_cfg((instr_sequence*)seq);
7971 if (g == NULL) {
7972 goto error;
7973 }
7974
7975 if (_PyCfg_JumpLabelsToTargets(g) < 0) {
7976 goto error;
7977 }
7978
7979 int code_flags = 0;
7980 int stackdepth, nlocalsplus;
7981 if (_PyCfg_OptimizedCfgToInstructionSequence(g, umd, code_flags,
7982 &stackdepth, &nlocalsplus,
7983 &optimized_instrs) < 0) {
7984 goto error;
7985 }
7986
7987 PyObject *consts = consts_dict_keys_inorder(umd->u_consts);
7988 if (consts == NULL) {
7989 goto error;
7990 }
7991 co = _PyAssemble_MakeCodeObject(umd, const_cache,
7992 consts, stackdepth, &optimized_instrs,
7993 nlocalsplus, code_flags, filename);
7994 Py_DECREF(consts);
7995
7996 error:
7997 Py_DECREF(const_cache);
7998 _PyCfgBuilder_Free(g);
7999 PyInstructionSequence_Fini(&optimized_instrs);
8000 return co;
8001 }
8002
8003
8004 /* Retained for API compatibility.
8005 * Optimization is now done in _PyCfg_OptimizeCodeUnit */
8006
8007 PyObject *
PyCode_Optimize(PyObject * code,PyObject * Py_UNUSED (consts),PyObject * Py_UNUSED (names),PyObject * Py_UNUSED (lnotab_obj))8008 PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
8009 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
8010 {
8011 return Py_NewRef(code);
8012 }
8013