/src/Python-3.8.3/Python/compile.c
Line  | Count  | Source  | 
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 code for basic blocks.  See compiler_mod() in this file.  | 
10  |  |  *   4. Assemble the basic blocks into final code.  See assemble() in  | 
11  |  |  *      this file.  | 
12  |  |  *   5. Optimize the byte code (peephole optimizations).  See peephole.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 "Python.h"  | 
25  |  |  | 
26  |  | #include "pycore_pystate.h"   /* _PyInterpreterState_GET_UNSAFE() */  | 
27  |  | #include "Python-ast.h"  | 
28  |  | #include "ast.h"  | 
29  |  | #include "code.h"  | 
30  |  | #include "symtable.h"  | 
31  |  | #include "opcode.h"  | 
32  |  | #include "wordcode_helpers.h"  | 
33  |  |  | 
34  | 492  | #define DEFAULT_BLOCK_SIZE 16  | 
35  |  | #define DEFAULT_BLOCKS 8  | 
36  | 22  | #define DEFAULT_CODE_SIZE 128  | 
37  | 22  | #define DEFAULT_LNOTAB_SIZE 16  | 
38  |  |  | 
39  | 0  | #define COMP_GENEXP   0  | 
40  | 0  | #define COMP_LISTCOMP 1  | 
41  | 0  | #define COMP_SETCOMP  2  | 
42  | 0  | #define COMP_DICTCOMP 3  | 
43  |  |  | 
44  | 22  | #define IS_TOP_LEVEL_AWAIT(c) ( \  | 
45  | 22  |         (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \  | 
46  | 22  |         && (c->u->u_ste->ste_type == ModuleBlock))  | 
47  |  |  | 
48  |  | struct instr { | 
49  |  |     unsigned i_jabs : 1;  | 
50  |  |     unsigned i_jrel : 1;  | 
51  |  |     unsigned char i_opcode;  | 
52  |  |     int i_oparg;  | 
53  |  |     struct basicblock_ *i_target; /* target block (if jump instruction) */  | 
54  |  |     int i_lineno;  | 
55  |  | };  | 
56  |  |  | 
57  |  | typedef struct basicblock_ { | 
58  |  |     /* Each basicblock in a compilation unit is linked via b_list in the  | 
59  |  |        reverse order that the block are allocated.  b_list points to the next  | 
60  |  |        block, not to be confused with b_next, which is next by control flow. */  | 
61  |  |     struct basicblock_ *b_list;  | 
62  |  |     /* number of instructions used */  | 
63  |  |     int b_iused;  | 
64  |  |     /* length of instruction array (b_instr) */  | 
65  |  |     int b_ialloc;  | 
66  |  |     /* pointer to an array of instructions, initially NULL */  | 
67  |  |     struct instr *b_instr;  | 
68  |  |     /* If b_next is non-NULL, it is a pointer to the next  | 
69  |  |        block reached by normal control flow. */  | 
70  |  |     struct basicblock_ *b_next;  | 
71  |  |     /* b_seen is used to perform a DFS of basicblocks. */  | 
72  |  |     unsigned b_seen : 1;  | 
73  |  |     /* b_return is true if a RETURN_VALUE opcode is inserted. */  | 
74  |  |     unsigned b_return : 1;  | 
75  |  |     /* depth of stack upon entry of block, computed by stackdepth() */  | 
76  |  |     int b_startdepth;  | 
77  |  |     /* instruction offset for block, computed by assemble_jump_offsets() */  | 
78  |  |     int b_offset;  | 
79  |  | } basicblock;  | 
80  |  |  | 
81  |  | /* fblockinfo tracks the current frame block.  | 
82  |  |  | 
83  |  | A frame block is used to handle loops, try/except, and try/finally.  | 
84  |  | It's called a frame block to distinguish it from a basic block in the  | 
85  |  | compiler IR.  | 
86  |  | */  | 
87  |  |  | 
88  |  | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_TRY2, FINALLY_END, | 
89  |  |                   WITH, ASYNC_WITH, HANDLER_CLEANUP };  | 
90  |  |  | 
91  |  | struct fblockinfo { | 
92  |  |     enum fblocktype fb_type;  | 
93  |  |     basicblock *fb_block;  | 
94  |  |     /* (optional) type-specific exit or cleanup block */  | 
95  |  |     basicblock *fb_exit;  | 
96  |  | };  | 
97  |  |  | 
98  |  | enum { | 
99  |  |     COMPILER_SCOPE_MODULE,  | 
100  |  |     COMPILER_SCOPE_CLASS,  | 
101  |  |     COMPILER_SCOPE_FUNCTION,  | 
102  |  |     COMPILER_SCOPE_ASYNC_FUNCTION,  | 
103  |  |     COMPILER_SCOPE_LAMBDA,  | 
104  |  |     COMPILER_SCOPE_COMPREHENSION,  | 
105  |  | };  | 
106  |  |  | 
107  |  | /* The following items change on entry and exit of code blocks.  | 
108  |  |    They must be saved and restored when returning to a block.  | 
109  |  | */  | 
110  |  | struct compiler_unit { | 
111  |  |     PySTEntryObject *u_ste;  | 
112  |  |  | 
113  |  |     PyObject *u_name;  | 
114  |  |     PyObject *u_qualname;  /* dot-separated qualified name (lazy) */  | 
115  |  |     int u_scope_type;  | 
116  |  |  | 
117  |  |     /* The following fields are dicts that map objects to  | 
118  |  |        the index of them in co_XXX.      The index is used as  | 
119  |  |        the argument for opcodes that refer to those collections.  | 
120  |  |     */  | 
121  |  |     PyObject *u_consts;    /* all constants */  | 
122  |  |     PyObject *u_names;     /* all names */  | 
123  |  |     PyObject *u_varnames;  /* local variables */  | 
124  |  |     PyObject *u_cellvars;  /* cell variables */  | 
125  |  |     PyObject *u_freevars;  /* free variables */  | 
126  |  |  | 
127  |  |     PyObject *u_private;        /* for private name mangling */  | 
128  |  |  | 
129  |  |     Py_ssize_t u_argcount;        /* number of arguments for block */  | 
130  |  |     Py_ssize_t u_posonlyargcount;        /* number of positional only arguments for block */  | 
131  |  |     Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */  | 
132  |  |     /* Pointer to the most recently allocated block.  By following b_list  | 
133  |  |        members, you can reach all early allocated blocks. */  | 
134  |  |     basicblock *u_blocks;  | 
135  |  |     basicblock *u_curblock; /* pointer to current block */  | 
136  |  |  | 
137  |  |     int u_nfblocks;  | 
138  |  |     struct fblockinfo u_fblock[CO_MAXBLOCKS];  | 
139  |  |  | 
140  |  |     int u_firstlineno; /* the first lineno of the block */  | 
141  |  |     int u_lineno;          /* the lineno for the current stmt */  | 
142  |  |     int u_col_offset;      /* the offset of the current stmt */  | 
143  |  |     int u_lineno_set;  /* boolean to indicate whether instr  | 
144  |  |                           has been generated with current lineno */  | 
145  |  | };  | 
146  |  |  | 
147  |  | /* This struct captures the global state of a compilation.  | 
148  |  |  | 
149  |  | The u pointer points to the current compilation unit, while units  | 
150  |  | for enclosing blocks are stored in c_stack.     The u and c_stack are  | 
151  |  | managed by compiler_enter_scope() and compiler_exit_scope().  | 
152  |  |  | 
153  |  | Note that we don't track recursion levels during compilation - the  | 
154  |  | task of detecting and rejecting excessive levels of nesting is  | 
155  |  | handled by the symbol analysis pass.  | 
156  |  |  | 
157  |  | */  | 
158  |  |  | 
159  |  | struct compiler { | 
160  |  |     PyObject *c_filename;  | 
161  |  |     struct symtable *c_st;  | 
162  |  |     PyFutureFeatures *c_future; /* pointer to module's __future__ */  | 
163  |  |     PyCompilerFlags *c_flags;  | 
164  |  |  | 
165  |  |     int c_optimize;              /* optimization level */  | 
166  |  |     int c_interactive;           /* true if in interactive mode */  | 
167  |  |     int c_nestlevel;  | 
168  |  |     int c_do_not_emit_bytecode;  /* The compiler won't emit any bytecode  | 
169  |  |                                     if this value is different from zero.  | 
170  |  |                                     This can be used to temporarily visit  | 
171  |  |                                     nodes without emitting bytecode to  | 
172  |  |                                     check only errors. */  | 
173  |  |  | 
174  |  |     PyObject *c_const_cache;     /* Python dict holding all constants,  | 
175  |  |                                     including names tuple */  | 
176  |  |     struct compiler_unit *u; /* compiler state for current block */  | 
177  |  |     PyObject *c_stack;           /* Python list holding compiler_unit ptrs */  | 
178  |  |     PyArena *c_arena;            /* pointer to memory allocation arena */  | 
179  |  | };  | 
180  |  |  | 
181  |  | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);  | 
182  |  | static void compiler_free(struct compiler *);  | 
183  |  | static basicblock *compiler_new_block(struct compiler *);  | 
184  |  | static int compiler_next_instr(struct compiler *, basicblock *);  | 
185  |  | static int compiler_addop(struct compiler *, int);  | 
186  |  | static int compiler_addop_i(struct compiler *, int, Py_ssize_t);  | 
187  |  | static int compiler_addop_j(struct compiler *, int, basicblock *, int);  | 
188  |  | static int compiler_error(struct compiler *, const char *);  | 
189  |  | static int compiler_warn(struct compiler *, const char *, ...);  | 
190  |  | static int compiler_nameop(struct compiler *, identifier, expr_context_ty);  | 
191  |  |  | 
192  |  | static PyCodeObject *compiler_mod(struct compiler *, mod_ty);  | 
193  |  | static int compiler_visit_stmt(struct compiler *, stmt_ty);  | 
194  |  | static int compiler_visit_keyword(struct compiler *, keyword_ty);  | 
195  |  | static int compiler_visit_expr(struct compiler *, expr_ty);  | 
196  |  | static int compiler_augassign(struct compiler *, stmt_ty);  | 
197  |  | static int compiler_annassign(struct compiler *, stmt_ty);  | 
198  |  | static int compiler_visit_slice(struct compiler *, slice_ty,  | 
199  |  |                                 expr_context_ty);  | 
200  |  |  | 
201  |  | static int inplace_binop(struct compiler *, operator_ty);  | 
202  |  | static int expr_constant(expr_ty);  | 
203  |  |  | 
204  |  | static int compiler_with(struct compiler *, stmt_ty, int);  | 
205  |  | static int compiler_async_with(struct compiler *, stmt_ty, int);  | 
206  |  | static int compiler_async_for(struct compiler *, stmt_ty);  | 
207  |  | static int compiler_call_helper(struct compiler *c, int n,  | 
208  |  |                                 asdl_seq *args,  | 
209  |  |                                 asdl_seq *keywords);  | 
210  |  | static int compiler_try_except(struct compiler *, stmt_ty);  | 
211  |  | static int compiler_set_qualname(struct compiler *);  | 
212  |  |  | 
213  |  | static int compiler_sync_comprehension_generator(  | 
214  |  |                                       struct compiler *c,  | 
215  |  |                                       asdl_seq *generators, int gen_index,  | 
216  |  |                                       expr_ty elt, expr_ty val, int type);  | 
217  |  |  | 
218  |  | static int compiler_async_comprehension_generator(  | 
219  |  |                                       struct compiler *c,  | 
220  |  |                                       asdl_seq *generators, int gen_index,  | 
221  |  |                                       expr_ty elt, expr_ty val, int type);  | 
222  |  |  | 
223  |  | static PyCodeObject *assemble(struct compiler *, int addNone);  | 
224  |  | static PyObject *__doc__, *__annotations__;  | 
225  |  |  | 
226  | 12  | #define CAPSULE_NAME "compile.c compiler unit"  | 
227  |  |  | 
228  |  | PyObject *  | 
229  |  | _Py_Mangle(PyObject *privateobj, PyObject *ident)  | 
230  | 774  | { | 
231  |  |     /* Name mangling: __private becomes _classname__private.  | 
232  |  |        This is independent from how the name is used. */  | 
233  | 774  |     PyObject *result;  | 
234  | 774  |     size_t nlen, plen, ipriv;  | 
235  | 774  |     Py_UCS4 maxchar;  | 
236  | 774  |     if (privateobj == NULL || !PyUnicode_Check(privateobj) ||  | 
237  | 43  |         PyUnicode_READ_CHAR(ident, 0) != '_' ||  | 
238  | 774  |         PyUnicode_READ_CHAR(ident, 1) != '_') { | 
239  | 774  |         Py_INCREF(ident);  | 
240  | 774  |         return ident;  | 
241  | 774  |     }  | 
242  | 0  |     nlen = PyUnicode_GET_LENGTH(ident);  | 
243  | 0  |     plen = PyUnicode_GET_LENGTH(privateobj);  | 
244  |  |     /* Don't mangle __id__ or names with dots.  | 
245  |  |  | 
246  |  |        The only time a name with a dot can occur is when  | 
247  |  |        we are compiling an import statement that has a  | 
248  |  |        package name.  | 
249  |  |  | 
250  |  |        TODO(jhylton): Decide whether we want to support  | 
251  |  |        mangling of the module name, e.g. __M.X.  | 
252  |  |     */  | 
253  | 0  |     if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&  | 
254  | 0  |          PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||  | 
255  | 0  |         PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { | 
256  | 0  |         Py_INCREF(ident);  | 
257  | 0  |         return ident; /* Don't mangle __whatever__ */  | 
258  | 0  |     }  | 
259  |  |     /* Strip leading underscores from class name */  | 
260  | 0  |     ipriv = 0;  | 
261  | 0  |     while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')  | 
262  | 0  |         ipriv++;  | 
263  | 0  |     if (ipriv == plen) { | 
264  | 0  |         Py_INCREF(ident);  | 
265  | 0  |         return ident; /* Don't mangle if class is just underscores */  | 
266  | 0  |     }  | 
267  | 0  |     plen -= ipriv;  | 
268  |  | 
  | 
269  | 0  |     if (plen + nlen >= PY_SSIZE_T_MAX - 1) { | 
270  | 0  |         PyErr_SetString(PyExc_OverflowError,  | 
271  | 0  |                         "private identifier too large to be mangled");  | 
272  | 0  |         return NULL;  | 
273  | 0  |     }  | 
274  |  |  | 
275  | 0  |     maxchar = PyUnicode_MAX_CHAR_VALUE(ident);  | 
276  | 0  |     if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)  | 
277  | 0  |         maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);  | 
278  |  | 
  | 
279  | 0  |     result = PyUnicode_New(1 + nlen + plen, maxchar);  | 
280  | 0  |     if (!result)  | 
281  | 0  |         return 0;  | 
282  |  |     /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */  | 
283  | 0  |     PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');  | 
284  | 0  |     if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { | 
285  | 0  |         Py_DECREF(result);  | 
286  | 0  |         return NULL;  | 
287  | 0  |     }  | 
288  | 0  |     if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { | 
289  | 0  |         Py_DECREF(result);  | 
290  | 0  |         return NULL;  | 
291  | 0  |     }  | 
292  | 0  |     assert(_PyUnicode_CheckConsistency(result, 1));  | 
293  | 0  |     return result;  | 
294  | 0  | }  | 
295  |  |  | 
296  |  | static int  | 
297  |  | compiler_init(struct compiler *c)  | 
298  | 16  | { | 
299  | 16  |     memset(c, 0, sizeof(struct compiler));  | 
300  |  |  | 
301  | 16  |     c->c_const_cache = PyDict_New();  | 
302  | 16  |     if (!c->c_const_cache) { | 
303  | 0  |         return 0;  | 
304  | 0  |     }  | 
305  |  |  | 
306  | 16  |     c->c_stack = PyList_New(0);  | 
307  | 16  |     if (!c->c_stack) { | 
308  | 0  |         Py_CLEAR(c->c_const_cache);  | 
309  | 0  |         return 0;  | 
310  | 0  |     }  | 
311  |  |  | 
312  | 16  |     return 1;  | 
313  | 16  | }  | 
314  |  |  | 
315  |  | PyCodeObject *  | 
316  |  | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,  | 
317  |  |                    int optimize, PyArena *arena)  | 
318  | 16  | { | 
319  | 16  |     struct compiler c;  | 
320  | 16  |     PyCodeObject *co = NULL;  | 
321  | 16  |     PyCompilerFlags local_flags = _PyCompilerFlags_INIT;  | 
322  | 16  |     int merged;  | 
323  | 16  |     PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config;  | 
324  |  |  | 
325  | 16  |     if (!__doc__) { | 
326  | 14  |         __doc__ = PyUnicode_InternFromString("__doc__"); | 
327  | 14  |         if (!__doc__)  | 
328  | 0  |             return NULL;  | 
329  | 14  |     }  | 
330  | 16  |     if (!__annotations__) { | 
331  | 14  |         __annotations__ = PyUnicode_InternFromString("__annotations__"); | 
332  | 14  |         if (!__annotations__)  | 
333  | 0  |             return NULL;  | 
334  | 14  |     }  | 
335  | 16  |     if (!compiler_init(&c))  | 
336  | 0  |         return NULL;  | 
337  | 16  |     Py_INCREF(filename);  | 
338  | 16  |     c.c_filename = filename;  | 
339  | 16  |     c.c_arena = arena;  | 
340  | 16  |     c.c_future = PyFuture_FromASTObject(mod, filename);  | 
341  | 16  |     if (c.c_future == NULL)  | 
342  | 0  |         goto finally;  | 
343  | 16  |     if (!flags) { | 
344  | 14  |         flags = &local_flags;  | 
345  | 14  |     }  | 
346  | 16  |     merged = c.c_future->ff_features | flags->cf_flags;  | 
347  | 16  |     c.c_future->ff_features = merged;  | 
348  | 16  |     flags->cf_flags = merged;  | 
349  | 16  |     c.c_flags = flags;  | 
350  | 16  |     c.c_optimize = (optimize == -1) ? config->optimization_level : optimize;  | 
351  | 16  |     c.c_nestlevel = 0;  | 
352  | 16  |     c.c_do_not_emit_bytecode = 0;  | 
353  |  |  | 
354  | 16  |     if (!_PyAST_Optimize(mod, arena, c.c_optimize)) { | 
355  | 0  |         goto finally;  | 
356  | 0  |     }  | 
357  |  |  | 
358  | 16  |     c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);  | 
359  | 16  |     if (c.c_st == NULL) { | 
360  | 0  |         if (!PyErr_Occurred())  | 
361  | 0  |             PyErr_SetString(PyExc_SystemError, "no symtable");  | 
362  | 0  |         goto finally;  | 
363  | 0  |     }  | 
364  |  |  | 
365  | 16  |     co = compiler_mod(&c, mod);  | 
366  |  |  | 
367  | 16  |  finally:  | 
368  | 16  |     compiler_free(&c);  | 
369  | 16  |     assert(co || PyErr_Occurred());  | 
370  | 16  |     return co;  | 
371  | 16  | }  | 
372  |  |  | 
373  |  | PyCodeObject *  | 
374  |  | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,  | 
375  |  |                 int optimize, PyArena *arena)  | 
376  | 0  | { | 
377  | 0  |     PyObject *filename;  | 
378  | 0  |     PyCodeObject *co;  | 
379  | 0  |     filename = PyUnicode_DecodeFSDefault(filename_str);  | 
380  | 0  |     if (filename == NULL)  | 
381  | 0  |         return NULL;  | 
382  | 0  |     co = PyAST_CompileObject(mod, filename, flags, optimize, arena);  | 
383  | 0  |     Py_DECREF(filename);  | 
384  | 0  |     return co;  | 
385  |  | 
  | 
386  | 0  | }  | 
387  |  |  | 
388  |  | PyCodeObject *  | 
389  |  | PyNode_Compile(struct _node *n, const char *filename)  | 
390  | 0  | { | 
391  | 0  |     PyCodeObject *co = NULL;  | 
392  | 0  |     mod_ty mod;  | 
393  | 0  |     PyArena *arena = PyArena_New();  | 
394  | 0  |     if (!arena)  | 
395  | 0  |         return NULL;  | 
396  | 0  |     mod = PyAST_FromNode(n, NULL, filename, arena);  | 
397  | 0  |     if (mod)  | 
398  | 0  |         co = PyAST_Compile(mod, filename, NULL, arena);  | 
399  | 0  |     PyArena_Free(arena);  | 
400  | 0  |     return co;  | 
401  | 0  | }  | 
402  |  |  | 
403  |  | static void  | 
404  |  | compiler_free(struct compiler *c)  | 
405  | 16  | { | 
406  | 16  |     if (c->c_st)  | 
407  | 16  |         PySymtable_Free(c->c_st);  | 
408  | 16  |     if (c->c_future)  | 
409  | 16  |         PyObject_Free(c->c_future);  | 
410  | 16  |     Py_XDECREF(c->c_filename);  | 
411  | 16  |     Py_DECREF(c->c_const_cache);  | 
412  | 16  |     Py_DECREF(c->c_stack);  | 
413  | 16  | }  | 
414  |  |  | 
415  |  | static PyObject *  | 
416  |  | list2dict(PyObject *list)  | 
417  | 22  | { | 
418  | 22  |     Py_ssize_t i, n;  | 
419  | 22  |     PyObject *v, *k;  | 
420  | 22  |     PyObject *dict = PyDict_New();  | 
421  | 22  |     if (!dict) return NULL;  | 
422  |  |  | 
423  | 22  |     n = PyList_Size(list);  | 
424  | 39  |     for (i = 0; i < n; i++) { | 
425  | 17  |         v = PyLong_FromSsize_t(i);  | 
426  | 17  |         if (!v) { | 
427  | 0  |             Py_DECREF(dict);  | 
428  | 0  |             return NULL;  | 
429  | 0  |         }  | 
430  | 17  |         k = PyList_GET_ITEM(list, i);  | 
431  | 17  |         if (PyDict_SetItem(dict, k, v) < 0) { | 
432  | 0  |             Py_DECREF(v);  | 
433  | 0  |             Py_DECREF(dict);  | 
434  | 0  |             return NULL;  | 
435  | 0  |         }  | 
436  | 17  |         Py_DECREF(v);  | 
437  | 17  |     }  | 
438  | 22  |     return dict;  | 
439  | 22  | }  | 
440  |  |  | 
441  |  | /* Return new dict containing names from src that match scope(s).  | 
442  |  |  | 
443  |  | src is a symbol table dictionary.  If the scope of a name matches  | 
444  |  | either scope_type or flag is set, insert it into the new dict.  The  | 
445  |  | values are integers, starting at offset and increasing by one for  | 
446  |  | each key.  | 
447  |  | */  | 
448  |  |  | 
449  |  | static PyObject *  | 
450  |  | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)  | 
451  | 44  | { | 
452  | 44  |     Py_ssize_t i = offset, scope, num_keys, key_i;  | 
453  | 44  |     PyObject *k, *v, *dest = PyDict_New();  | 
454  | 44  |     PyObject *sorted_keys;  | 
455  |  |  | 
456  | 44  |     assert(offset >= 0);  | 
457  | 44  |     if (dest == NULL)  | 
458  | 0  |         return NULL;  | 
459  |  |  | 
460  |  |     /* Sort the keys so that we have a deterministic order on the indexes  | 
461  |  |        saved in the returned dictionary.  These indexes are used as indexes  | 
462  |  |        into the free and cell var storage.  Therefore if they aren't  | 
463  |  |        deterministic, then the generated bytecode is not deterministic.  | 
464  |  |     */  | 
465  | 44  |     sorted_keys = PyDict_Keys(src);  | 
466  | 44  |     if (sorted_keys == NULL)  | 
467  | 0  |         return NULL;  | 
468  | 44  |     if (PyList_Sort(sorted_keys) != 0) { | 
469  | 0  |         Py_DECREF(sorted_keys);  | 
470  | 0  |         return NULL;  | 
471  | 0  |     }  | 
472  | 44  |     num_keys = PyList_GET_SIZE(sorted_keys);  | 
473  |  |  | 
474  | 322  |     for (key_i = 0; key_i < num_keys; key_i++) { | 
475  |  |         /* XXX this should probably be a macro in symtable.h */  | 
476  | 278  |         long vi;  | 
477  | 278  |         k = PyList_GET_ITEM(sorted_keys, key_i);  | 
478  | 278  |         v = PyDict_GetItem(src, k);  | 
479  | 278  |         assert(PyLong_Check(v));  | 
480  | 278  |         vi = PyLong_AS_LONG(v);  | 
481  | 278  |         scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;  | 
482  |  |  | 
483  | 278  |         if (scope == scope_type || vi & flag) { | 
484  | 0  |             PyObject *item = PyLong_FromSsize_t(i);  | 
485  | 0  |             if (item == NULL) { | 
486  | 0  |                 Py_DECREF(sorted_keys);  | 
487  | 0  |                 Py_DECREF(dest);  | 
488  | 0  |                 return NULL;  | 
489  | 0  |             }  | 
490  | 0  |             i++;  | 
491  | 0  |             if (PyDict_SetItem(dest, k, item) < 0) { | 
492  | 0  |                 Py_DECREF(sorted_keys);  | 
493  | 0  |                 Py_DECREF(item);  | 
494  | 0  |                 Py_DECREF(dest);  | 
495  | 0  |                 return NULL;  | 
496  | 0  |             }  | 
497  | 0  |             Py_DECREF(item);  | 
498  | 0  |         }  | 
499  | 278  |     }  | 
500  | 44  |     Py_DECREF(sorted_keys);  | 
501  | 44  |     return dest;  | 
502  | 44  | }  | 
503  |  |  | 
504  |  | static void  | 
505  |  | compiler_unit_check(struct compiler_unit *u)  | 
506  | 28  | { | 
507  | 28  |     basicblock *block;  | 
508  | 258  |     for (block = u->u_blocks; block != NULL; block = block->b_list) { | 
509  | 230  |         assert((uintptr_t)block != 0xcbcbcbcbU);  | 
510  | 230  |         assert((uintptr_t)block != 0xfbfbfbfbU);  | 
511  | 230  |         assert((uintptr_t)block != 0xdbdbdbdbU);  | 
512  | 230  |         if (block->b_instr != NULL) { | 
513  | 174  |             assert(block->b_ialloc > 0);  | 
514  | 174  |             assert(block->b_iused > 0);  | 
515  | 174  |             assert(block->b_ialloc >= block->b_iused);  | 
516  | 174  |         }  | 
517  | 56  |         else { | 
518  | 56  |             assert (block->b_iused == 0);  | 
519  | 56  |             assert (block->b_ialloc == 0);  | 
520  | 56  |         }  | 
521  | 230  |     }  | 
522  | 28  | }  | 
523  |  |  | 
524  |  | static void  | 
525  |  | compiler_unit_free(struct compiler_unit *u)  | 
526  | 22  | { | 
527  | 22  |     basicblock *b, *next;  | 
528  |  |  | 
529  | 22  |     compiler_unit_check(u);  | 
530  | 22  |     b = u->u_blocks;  | 
531  | 230  |     while (b != NULL) { | 
532  | 208  |         if (b->b_instr)  | 
533  | 164  |             PyObject_Free((void *)b->b_instr);  | 
534  | 208  |         next = b->b_list;  | 
535  | 208  |         PyObject_Free((void *)b);  | 
536  | 208  |         b = next;  | 
537  | 208  |     }  | 
538  | 22  |     Py_CLEAR(u->u_ste);  | 
539  | 22  |     Py_CLEAR(u->u_name);  | 
540  | 22  |     Py_CLEAR(u->u_qualname);  | 
541  | 22  |     Py_CLEAR(u->u_consts);  | 
542  | 22  |     Py_CLEAR(u->u_names);  | 
543  | 22  |     Py_CLEAR(u->u_varnames);  | 
544  | 22  |     Py_CLEAR(u->u_freevars);  | 
545  | 22  |     Py_CLEAR(u->u_cellvars);  | 
546  | 22  |     Py_CLEAR(u->u_private);  | 
547  | 22  |     PyObject_Free(u);  | 
548  | 22  | }  | 
549  |  |  | 
550  |  | static int  | 
551  |  | compiler_enter_scope(struct compiler *c, identifier name,  | 
552  |  |                      int scope_type, void *key, int lineno)  | 
553  | 22  | { | 
554  | 22  |     struct compiler_unit *u;  | 
555  | 22  |     basicblock *block;  | 
556  |  |  | 
557  | 22  |     u = (struct compiler_unit *)PyObject_Malloc(sizeof(  | 
558  | 22  |                                             struct compiler_unit));  | 
559  | 22  |     if (!u) { | 
560  | 0  |         PyErr_NoMemory();  | 
561  | 0  |         return 0;  | 
562  | 0  |     }  | 
563  | 22  |     memset(u, 0, sizeof(struct compiler_unit));  | 
564  | 22  |     u->u_scope_type = scope_type;  | 
565  | 22  |     u->u_argcount = 0;  | 
566  | 22  |     u->u_posonlyargcount = 0;  | 
567  | 22  |     u->u_kwonlyargcount = 0;  | 
568  | 22  |     u->u_ste = PySymtable_Lookup(c->c_st, key);  | 
569  | 22  |     if (!u->u_ste) { | 
570  | 0  |         compiler_unit_free(u);  | 
571  | 0  |         return 0;  | 
572  | 0  |     }  | 
573  | 22  |     Py_INCREF(name);  | 
574  | 22  |     u->u_name = name;  | 
575  | 22  |     u->u_varnames = list2dict(u->u_ste->ste_varnames);  | 
576  | 22  |     u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);  | 
577  | 22  |     if (!u->u_varnames || !u->u_cellvars) { | 
578  | 0  |         compiler_unit_free(u);  | 
579  | 0  |         return 0;  | 
580  | 0  |     }  | 
581  | 22  |     if (u->u_ste->ste_needs_class_closure) { | 
582  |  |         /* Cook up an implicit __class__ cell. */  | 
583  | 0  |         _Py_IDENTIFIER(__class__);  | 
584  | 0  |         PyObject *name;  | 
585  | 0  |         int res;  | 
586  | 0  |         assert(u->u_scope_type == COMPILER_SCOPE_CLASS);  | 
587  | 0  |         assert(PyDict_GET_SIZE(u->u_cellvars) == 0);  | 
588  | 0  |         name = _PyUnicode_FromId(&PyId___class__);  | 
589  | 0  |         if (!name) { | 
590  | 0  |             compiler_unit_free(u);  | 
591  | 0  |             return 0;  | 
592  | 0  |         }  | 
593  | 0  |         res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero);  | 
594  | 0  |         if (res < 0) { | 
595  | 0  |             compiler_unit_free(u);  | 
596  | 0  |             return 0;  | 
597  | 0  |         }  | 
598  | 0  |     }  | 
599  |  |  | 
600  | 22  |     u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,  | 
601  | 22  |                                PyDict_GET_SIZE(u->u_cellvars));  | 
602  | 22  |     if (!u->u_freevars) { | 
603  | 0  |         compiler_unit_free(u);  | 
604  | 0  |         return 0;  | 
605  | 0  |     }  | 
606  |  |  | 
607  | 22  |     u->u_blocks = NULL;  | 
608  | 22  |     u->u_nfblocks = 0;  | 
609  | 22  |     u->u_firstlineno = lineno;  | 
610  | 22  |     u->u_lineno = 0;  | 
611  | 22  |     u->u_col_offset = 0;  | 
612  | 22  |     u->u_lineno_set = 0;  | 
613  | 22  |     u->u_consts = PyDict_New();  | 
614  | 22  |     if (!u->u_consts) { | 
615  | 0  |         compiler_unit_free(u);  | 
616  | 0  |         return 0;  | 
617  | 0  |     }  | 
618  | 22  |     u->u_names = PyDict_New();  | 
619  | 22  |     if (!u->u_names) { | 
620  | 0  |         compiler_unit_free(u);  | 
621  | 0  |         return 0;  | 
622  | 0  |     }  | 
623  |  |  | 
624  | 22  |     u->u_private = NULL;  | 
625  |  |  | 
626  |  |     /* Push the old compiler_unit on the stack. */  | 
627  | 22  |     if (c->u) { | 
628  | 6  |         PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);  | 
629  | 6  |         if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { | 
630  | 0  |             Py_XDECREF(capsule);  | 
631  | 0  |             compiler_unit_free(u);  | 
632  | 0  |             return 0;  | 
633  | 0  |         }  | 
634  | 6  |         Py_DECREF(capsule);  | 
635  | 6  |         u->u_private = c->u->u_private;  | 
636  | 6  |         Py_XINCREF(u->u_private);  | 
637  | 6  |     }  | 
638  | 22  |     c->u = u;  | 
639  |  |  | 
640  | 22  |     c->c_nestlevel++;  | 
641  |  |  | 
642  | 22  |     block = compiler_new_block(c);  | 
643  | 22  |     if (block == NULL)  | 
644  | 0  |         return 0;  | 
645  | 22  |     c->u->u_curblock = block;  | 
646  |  |  | 
647  | 22  |     if (u->u_scope_type != COMPILER_SCOPE_MODULE) { | 
648  | 6  |         if (!compiler_set_qualname(c))  | 
649  | 0  |             return 0;  | 
650  | 6  |     }  | 
651  |  |  | 
652  | 22  |     return 1;  | 
653  | 22  | }  | 
654  |  |  | 
655  |  | static void  | 
656  |  | compiler_exit_scope(struct compiler *c)  | 
657  | 22  | { | 
658  | 22  |     Py_ssize_t n;  | 
659  | 22  |     PyObject *capsule;  | 
660  |  |  | 
661  | 22  |     c->c_nestlevel--;  | 
662  | 22  |     compiler_unit_free(c->u);  | 
663  |  |     /* Restore c->u to the parent unit. */  | 
664  | 22  |     n = PyList_GET_SIZE(c->c_stack) - 1;  | 
665  | 22  |     if (n >= 0) { | 
666  | 6  |         capsule = PyList_GET_ITEM(c->c_stack, n);  | 
667  | 6  |         c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);  | 
668  | 6  |         assert(c->u);  | 
669  |  |         /* we are deleting from a list so this really shouldn't fail */  | 
670  | 6  |         if (PySequence_DelItem(c->c_stack, n) < 0)  | 
671  | 0  |             Py_FatalError("compiler_exit_scope()"); | 
672  | 6  |         compiler_unit_check(c->u);  | 
673  | 6  |     }  | 
674  | 16  |     else  | 
675  | 16  |         c->u = NULL;  | 
676  |  |  | 
677  | 22  | }  | 
678  |  |  | 
679  |  | static int  | 
680  |  | compiler_set_qualname(struct compiler *c)  | 
681  | 6  | { | 
682  | 6  |     _Py_static_string(dot, ".");  | 
683  | 6  |     _Py_static_string(dot_locals, ".<locals>");  | 
684  | 6  |     Py_ssize_t stack_size;  | 
685  | 6  |     struct compiler_unit *u = c->u;  | 
686  | 6  |     PyObject *name, *base, *dot_str, *dot_locals_str;  | 
687  |  |  | 
688  | 6  |     base = NULL;  | 
689  | 6  |     stack_size = PyList_GET_SIZE(c->c_stack);  | 
690  | 6  |     assert(stack_size >= 1);  | 
691  | 6  |     if (stack_size > 1) { | 
692  | 0  |         int scope, force_global = 0;  | 
693  | 0  |         struct compiler_unit *parent;  | 
694  | 0  |         PyObject *mangled, *capsule;  | 
695  |  | 
  | 
696  | 0  |         capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);  | 
697  | 0  |         parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);  | 
698  | 0  |         assert(parent);  | 
699  |  | 
  | 
700  | 0  |         if (u->u_scope_type == COMPILER_SCOPE_FUNCTION  | 
701  | 0  |             || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION  | 
702  | 0  |             || u->u_scope_type == COMPILER_SCOPE_CLASS) { | 
703  | 0  |             assert(u->u_name);  | 
704  | 0  |             mangled = _Py_Mangle(parent->u_private, u->u_name);  | 
705  | 0  |             if (!mangled)  | 
706  | 0  |                 return 0;  | 
707  | 0  |             scope = PyST_GetScope(parent->u_ste, mangled);  | 
708  | 0  |             Py_DECREF(mangled);  | 
709  | 0  |             assert(scope != GLOBAL_IMPLICIT);  | 
710  | 0  |             if (scope == GLOBAL_EXPLICIT)  | 
711  | 0  |                 force_global = 1;  | 
712  | 0  |         }  | 
713  |  |  | 
714  | 0  |         if (!force_global) { | 
715  | 0  |             if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION  | 
716  | 0  |                 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION  | 
717  | 0  |                 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { | 
718  | 0  |                 dot_locals_str = _PyUnicode_FromId(&dot_locals);  | 
719  | 0  |                 if (dot_locals_str == NULL)  | 
720  | 0  |                     return 0;  | 
721  | 0  |                 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);  | 
722  | 0  |                 if (base == NULL)  | 
723  | 0  |                     return 0;  | 
724  | 0  |             }  | 
725  | 0  |             else { | 
726  | 0  |                 Py_INCREF(parent->u_qualname);  | 
727  | 0  |                 base = parent->u_qualname;  | 
728  | 0  |             }  | 
729  | 0  |         }  | 
730  | 0  |     }  | 
731  |  |  | 
732  | 6  |     if (base != NULL) { | 
733  | 0  |         dot_str = _PyUnicode_FromId(&dot);  | 
734  | 0  |         if (dot_str == NULL) { | 
735  | 0  |             Py_DECREF(base);  | 
736  | 0  |             return 0;  | 
737  | 0  |         }  | 
738  | 0  |         name = PyUnicode_Concat(base, dot_str);  | 
739  | 0  |         Py_DECREF(base);  | 
740  | 0  |         if (name == NULL)  | 
741  | 0  |             return 0;  | 
742  | 0  |         PyUnicode_Append(&name, u->u_name);  | 
743  | 0  |         if (name == NULL)  | 
744  | 0  |             return 0;  | 
745  | 0  |     }  | 
746  | 6  |     else { | 
747  | 6  |         Py_INCREF(u->u_name);  | 
748  | 6  |         name = u->u_name;  | 
749  | 6  |     }  | 
750  | 6  |     u->u_qualname = name;  | 
751  |  |  | 
752  | 6  |     return 1;  | 
753  | 6  | }  | 
754  |  |  | 
755  |  |  | 
756  |  | /* Allocate a new block and return a pointer to it.  | 
757  |  |    Returns NULL on error.  | 
758  |  | */  | 
759  |  |  | 
760  |  | static basicblock *  | 
761  |  | compiler_new_block(struct compiler *c)  | 
762  | 208  | { | 
763  | 208  |     basicblock *b;  | 
764  | 208  |     struct compiler_unit *u;  | 
765  |  |  | 
766  | 208  |     u = c->u;  | 
767  | 208  |     b = (basicblock *)PyObject_Malloc(sizeof(basicblock));  | 
768  | 208  |     if (b == NULL) { | 
769  | 0  |         PyErr_NoMemory();  | 
770  | 0  |         return NULL;  | 
771  | 0  |     }  | 
772  | 208  |     memset((void *)b, 0, sizeof(basicblock));  | 
773  |  |     /* Extend the singly linked list of blocks with new block. */  | 
774  | 208  |     b->b_list = u->u_blocks;  | 
775  | 208  |     u->u_blocks = b;  | 
776  | 208  |     return b;  | 
777  | 208  | }  | 
778  |  |  | 
779  |  | static basicblock *  | 
780  |  | compiler_next_block(struct compiler *c)  | 
781  | 18  | { | 
782  | 18  |     basicblock *block = compiler_new_block(c);  | 
783  | 18  |     if (block == NULL)  | 
784  | 0  |         return NULL;  | 
785  | 18  |     c->u->u_curblock->b_next = block;  | 
786  | 18  |     c->u->u_curblock = block;  | 
787  | 18  |     return block;  | 
788  | 18  | }  | 
789  |  |  | 
790  |  | static basicblock *  | 
791  |  | compiler_use_next_block(struct compiler *c, basicblock *block)  | 
792  | 168  | { | 
793  | 168  |     assert(block != NULL);  | 
794  | 168  |     c->u->u_curblock->b_next = block;  | 
795  | 168  |     c->u->u_curblock = block;  | 
796  | 168  |     return block;  | 
797  | 168  | }  | 
798  |  |  | 
799  |  | /* Returns the offset of the next instruction in the current block's  | 
800  |  |    b_instr array.  Resizes the b_instr as necessary.  | 
801  |  |    Returns -1 on failure.  | 
802  |  | */  | 
803  |  |  | 
804  |  | static int  | 
805  |  | compiler_next_instr(struct compiler *c, basicblock *b)  | 
806  | 1.08k  | { | 
807  | 1.08k  |     assert(b != NULL);  | 
808  | 1.08k  |     if (b->b_instr == NULL) { | 
809  | 164  |         b->b_instr = (struct instr *)PyObject_Malloc(  | 
810  | 164  |                          sizeof(struct instr) * DEFAULT_BLOCK_SIZE);  | 
811  | 164  |         if (b->b_instr == NULL) { | 
812  | 0  |             PyErr_NoMemory();  | 
813  | 0  |             return -1;  | 
814  | 0  |         }  | 
815  | 164  |         b->b_ialloc = DEFAULT_BLOCK_SIZE;  | 
816  | 164  |         memset((char *)b->b_instr, 0,  | 
817  | 164  |                sizeof(struct instr) * DEFAULT_BLOCK_SIZE);  | 
818  | 164  |     }  | 
819  | 919  |     else if (b->b_iused == b->b_ialloc) { | 
820  | 22  |         struct instr *tmp;  | 
821  | 22  |         size_t oldsize, newsize;  | 
822  | 22  |         oldsize = b->b_ialloc * sizeof(struct instr);  | 
823  | 22  |         newsize = oldsize << 1;  | 
824  |  |  | 
825  | 22  |         if (oldsize > (SIZE_MAX >> 1)) { | 
826  | 0  |             PyErr_NoMemory();  | 
827  | 0  |             return -1;  | 
828  | 0  |         }  | 
829  |  |  | 
830  | 22  |         if (newsize == 0) { | 
831  | 0  |             PyErr_NoMemory();  | 
832  | 0  |             return -1;  | 
833  | 0  |         }  | 
834  | 22  |         b->b_ialloc <<= 1;  | 
835  | 22  |         tmp = (struct instr *)PyObject_Realloc(  | 
836  | 22  |                                         (void *)b->b_instr, newsize);  | 
837  | 22  |         if (tmp == NULL) { | 
838  | 0  |             PyErr_NoMemory();  | 
839  | 0  |             return -1;  | 
840  | 0  |         }  | 
841  | 22  |         b->b_instr = tmp;  | 
842  | 22  |         memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);  | 
843  | 22  |     }  | 
844  | 1.08k  |     return b->b_iused++;  | 
845  | 1.08k  | }  | 
846  |  |  | 
847  |  | /* Set the i_lineno member of the instruction at offset off if the  | 
848  |  |    line number for the current expression/statement has not  | 
849  |  |    already been set.  If it has been set, the call has no effect.  | 
850  |  |  | 
851  |  |    The line number is reset in the following cases:  | 
852  |  |    - when entering a new scope  | 
853  |  |    - on each statement  | 
854  |  |    - on each expression that start a new line  | 
855  |  |    - before the "except" and "finally" clauses  | 
856  |  |    - before the "for" and "while" expressions  | 
857  |  | */  | 
858  |  |  | 
859  |  | static void  | 
860  |  | compiler_set_lineno(struct compiler *c, int off)  | 
861  | 1.08k  | { | 
862  | 1.08k  |     basicblock *b;  | 
863  | 1.08k  |     if (c->u->u_lineno_set)  | 
864  | 907  |         return;  | 
865  | 176  |     c->u->u_lineno_set = 1;  | 
866  | 176  |     b = c->u->u_curblock;  | 
867  | 176  |     b->b_instr[off].i_lineno = c->u->u_lineno;  | 
868  | 176  | }  | 
869  |  |  | 
870  |  | /* Return the stack effect of opcode with argument oparg.  | 
871  |  |  | 
872  |  |    Some opcodes have different stack effect when jump to the target and  | 
873  |  |    when not jump. The 'jump' parameter specifies the case:  | 
874  |  |  | 
875  |  |    * 0 -- when not jump  | 
876  |  |    * 1 -- when jump  | 
877  |  |    * -1 -- maximal  | 
878  |  |  */  | 
879  |  | /* XXX Make the stack effect of WITH_CLEANUP_START and  | 
880  |  |    WITH_CLEANUP_FINISH deterministic. */  | 
881  |  | static int  | 
882  |  | stack_effect(int opcode, int oparg, int jump)  | 
883  | 1.19k  | { | 
884  | 1.19k  |     switch (opcode) { | 
885  | 0  |         case NOP:  | 
886  | 0  |         case EXTENDED_ARG:  | 
887  | 0  |             return 0;  | 
888  |  |  | 
889  |  |         /* Stack manipulation */  | 
890  | 82  |         case POP_TOP:  | 
891  | 82  |             return -1;  | 
892  | 0  |         case ROT_TWO:  | 
893  | 0  |         case ROT_THREE:  | 
894  | 2  |         case ROT_FOUR:  | 
895  | 2  |             return 0;  | 
896  | 22  |         case DUP_TOP:  | 
897  | 22  |             return 1;  | 
898  | 0  |         case DUP_TOP_TWO:  | 
899  | 0  |             return 2;  | 
900  |  |  | 
901  |  |         /* Unary operators */  | 
902  | 0  |         case UNARY_POSITIVE:  | 
903  | 0  |         case UNARY_NEGATIVE:  | 
904  | 0  |         case UNARY_NOT:  | 
905  | 0  |         case UNARY_INVERT:  | 
906  | 0  |             return 0;  | 
907  |  |  | 
908  | 0  |         case SET_ADD:  | 
909  | 0  |         case LIST_APPEND:  | 
910  | 0  |             return -1;  | 
911  | 0  |         case MAP_ADD:  | 
912  | 0  |             return -2;  | 
913  |  |  | 
914  |  |         /* Binary operators */  | 
915  | 0  |         case BINARY_POWER:  | 
916  | 2  |         case BINARY_MULTIPLY:  | 
917  | 2  |         case BINARY_MATRIX_MULTIPLY:  | 
918  | 6  |         case BINARY_MODULO:  | 
919  | 8  |         case BINARY_ADD:  | 
920  | 12  |         case BINARY_SUBTRACT:  | 
921  | 22  |         case BINARY_SUBSCR:  | 
922  | 22  |         case BINARY_FLOOR_DIVIDE:  | 
923  | 22  |         case BINARY_TRUE_DIVIDE:  | 
924  | 22  |             return -1;  | 
925  | 0  |         case INPLACE_FLOOR_DIVIDE:  | 
926  | 0  |         case INPLACE_TRUE_DIVIDE:  | 
927  | 0  |             return -1;  | 
928  |  |  | 
929  | 0  |         case INPLACE_ADD:  | 
930  | 0  |         case INPLACE_SUBTRACT:  | 
931  | 0  |         case INPLACE_MULTIPLY:  | 
932  | 0  |         case INPLACE_MATRIX_MULTIPLY:  | 
933  | 0  |         case INPLACE_MODULO:  | 
934  | 0  |             return -1;  | 
935  | 2  |         case STORE_SUBSCR:  | 
936  | 2  |             return -3;  | 
937  | 2  |         case DELETE_SUBSCR:  | 
938  | 2  |             return -2;  | 
939  |  |  | 
940  | 0  |         case BINARY_LSHIFT:  | 
941  | 0  |         case BINARY_RSHIFT:  | 
942  | 0  |         case BINARY_AND:  | 
943  | 0  |         case BINARY_XOR:  | 
944  | 0  |         case BINARY_OR:  | 
945  | 0  |             return -1;  | 
946  | 0  |         case INPLACE_POWER:  | 
947  | 0  |             return -1;  | 
948  | 8  |         case GET_ITER:  | 
949  | 8  |             return 0;  | 
950  |  |  | 
951  | 0  |         case PRINT_EXPR:  | 
952  | 0  |             return -1;  | 
953  | 0  |         case LOAD_BUILD_CLASS:  | 
954  | 0  |             return 1;  | 
955  | 0  |         case INPLACE_LSHIFT:  | 
956  | 0  |         case INPLACE_RSHIFT:  | 
957  | 0  |         case INPLACE_AND:  | 
958  | 0  |         case INPLACE_XOR:  | 
959  | 0  |         case INPLACE_OR:  | 
960  | 0  |             return -1;  | 
961  |  |  | 
962  | 0  |         case SETUP_WITH:  | 
963  |  |             /* 1 in the normal flow.  | 
964  |  |              * Restore the stack position and push 6 values before jumping to  | 
965  |  |              * the handler if an exception be raised. */  | 
966  | 0  |             return jump ? 6 : 1;  | 
967  | 0  |         case WITH_CLEANUP_START:  | 
968  | 0  |             return 2; /* or 1, depending on TOS */  | 
969  | 0  |         case WITH_CLEANUP_FINISH:  | 
970  |  |             /* Pop a variable number of values pushed by WITH_CLEANUP_START  | 
971  |  |              * + __exit__ or __aexit__. */  | 
972  | 0  |             return -3;  | 
973  | 26  |         case RETURN_VALUE:  | 
974  | 26  |             return -1;  | 
975  | 0  |         case IMPORT_STAR:  | 
976  | 0  |             return -1;  | 
977  | 0  |         case SETUP_ANNOTATIONS:  | 
978  | 0  |             return 0;  | 
979  | 0  |         case YIELD_VALUE:  | 
980  | 0  |             return 0;  | 
981  | 0  |         case YIELD_FROM:  | 
982  | 0  |             return -1;  | 
983  | 32  |         case POP_BLOCK:  | 
984  | 32  |             return 0;  | 
985  | 20  |         case POP_EXCEPT:  | 
986  | 20  |             return -3;  | 
987  | 32  |         case END_FINALLY:  | 
988  | 32  |         case POP_FINALLY:  | 
989  |  |             /* Pop 6 values when an exception was raised. */  | 
990  | 32  |             return -6;  | 
991  |  |  | 
992  | 88  |         case STORE_NAME:  | 
993  | 88  |             return -1;  | 
994  | 14  |         case DELETE_NAME:  | 
995  | 14  |             return 0;  | 
996  | 10  |         case UNPACK_SEQUENCE:  | 
997  | 10  |             return oparg-1;  | 
998  | 0  |         case UNPACK_EX:  | 
999  | 0  |             return (oparg&0xFF) + (oparg>>8);  | 
1000  | 16  |         case FOR_ITER:  | 
1001  |  |             /* -1 at end of iterator, 1 if continue iterating. */  | 
1002  | 16  |             return jump > 0 ? -1 : 1;  | 
1003  |  |  | 
1004  | 0  |         case STORE_ATTR:  | 
1005  | 0  |             return -2;  | 
1006  | 0  |         case DELETE_ATTR:  | 
1007  | 0  |             return -1;  | 
1008  | 0  |         case STORE_GLOBAL:  | 
1009  | 0  |             return -1;  | 
1010  | 0  |         case DELETE_GLOBAL:  | 
1011  | 0  |             return 0;  | 
1012  | 106  |         case LOAD_CONST:  | 
1013  | 106  |             return 1;  | 
1014  | 216  |         case LOAD_NAME:  | 
1015  | 216  |             return 1;  | 
1016  | 12  |         case BUILD_TUPLE:  | 
1017  | 16  |         case BUILD_LIST:  | 
1018  | 16  |         case BUILD_SET:  | 
1019  | 16  |         case BUILD_STRING:  | 
1020  | 16  |             return 1-oparg;  | 
1021  | 0  |         case BUILD_LIST_UNPACK:  | 
1022  | 0  |         case BUILD_TUPLE_UNPACK:  | 
1023  | 0  |         case BUILD_TUPLE_UNPACK_WITH_CALL:  | 
1024  | 0  |         case BUILD_SET_UNPACK:  | 
1025  | 0  |         case BUILD_MAP_UNPACK:  | 
1026  | 0  |         case BUILD_MAP_UNPACK_WITH_CALL:  | 
1027  | 0  |             return 1 - oparg;  | 
1028  | 6  |         case BUILD_MAP:  | 
1029  | 6  |             return 1 - 2*oparg;  | 
1030  | 0  |         case BUILD_CONST_KEY_MAP:  | 
1031  | 0  |             return -oparg;  | 
1032  | 12  |         case LOAD_ATTR:  | 
1033  | 12  |             return 0;  | 
1034  | 44  |         case COMPARE_OP:  | 
1035  | 44  |             return -1;  | 
1036  | 0  |         case IMPORT_NAME:  | 
1037  | 0  |             return -1;  | 
1038  | 0  |         case IMPORT_FROM:  | 
1039  | 0  |             return 1;  | 
1040  |  |  | 
1041  |  |         /* Jumps */  | 
1042  | 76  |         case JUMP_FORWARD:  | 
1043  | 92  |         case JUMP_ABSOLUTE:  | 
1044  | 92  |             return 0;  | 
1045  |  |  | 
1046  | 0  |         case JUMP_IF_TRUE_OR_POP:  | 
1047  | 0  |         case JUMP_IF_FALSE_OR_POP:  | 
1048  | 0  |             return jump ? 0 : -1;  | 
1049  |  |  | 
1050  | 56  |         case POP_JUMP_IF_FALSE:  | 
1051  | 68  |         case POP_JUMP_IF_TRUE:  | 
1052  | 68  |             return -1;  | 
1053  |  |  | 
1054  | 12  |         case LOAD_GLOBAL:  | 
1055  | 12  |             return 1;  | 
1056  |  |  | 
1057  |  |         /* Exception handling */  | 
1058  | 64  |         case SETUP_FINALLY:  | 
1059  |  |             /* 0 in the normal flow.  | 
1060  |  |              * Restore the stack position and push 6 values before jumping to  | 
1061  |  |              * the handler if an exception be raised. */  | 
1062  | 64  |             return jump ? 6 : 0;  | 
1063  | 14  |         case BEGIN_FINALLY:  | 
1064  |  |             /* Actually pushes 1 value, but count 6 for balancing with  | 
1065  |  |              * END_FINALLY and POP_FINALLY.  | 
1066  |  |              * This is the main reason of using this opcode instead of  | 
1067  |  |              * "LOAD_CONST None". */  | 
1068  | 14  |             return 6;  | 
1069  | 0  |         case CALL_FINALLY:  | 
1070  | 0  |             return jump ? 1 : 0;  | 
1071  |  |  | 
1072  | 17  |         case LOAD_FAST:  | 
1073  | 17  |             return 1;  | 
1074  | 0  |         case STORE_FAST:  | 
1075  | 0  |             return -1;  | 
1076  | 0  |         case DELETE_FAST:  | 
1077  | 0  |             return 0;  | 
1078  |  |  | 
1079  | 8  |         case RAISE_VARARGS:  | 
1080  | 8  |             return -oparg;  | 
1081  |  |  | 
1082  |  |         /* Functions and calls */  | 
1083  | 38  |         case CALL_FUNCTION:  | 
1084  | 38  |             return -oparg;  | 
1085  | 48  |         case CALL_METHOD:  | 
1086  | 48  |             return -oparg-1;  | 
1087  | 0  |         case CALL_FUNCTION_KW:  | 
1088  | 0  |             return -oparg-1;  | 
1089  | 0  |         case CALL_FUNCTION_EX:  | 
1090  | 0  |             return -1 - ((oparg & 0x01) != 0);  | 
1091  | 6  |         case MAKE_FUNCTION:  | 
1092  | 6  |             return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -  | 
1093  | 6  |                 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);  | 
1094  | 2  |         case BUILD_SLICE:  | 
1095  | 2  |             if (oparg == 3)  | 
1096  | 0  |                 return -2;  | 
1097  | 2  |             else  | 
1098  | 2  |                 return -1;  | 
1099  |  |  | 
1100  |  |         /* Closures */  | 
1101  | 0  |         case LOAD_CLOSURE:  | 
1102  | 0  |             return 1;  | 
1103  | 0  |         case LOAD_DEREF:  | 
1104  | 0  |         case LOAD_CLASSDEREF:  | 
1105  | 0  |             return 1;  | 
1106  | 0  |         case STORE_DEREF:  | 
1107  | 0  |             return -1;  | 
1108  | 0  |         case DELETE_DEREF:  | 
1109  | 0  |             return 0;  | 
1110  |  |  | 
1111  |  |         /* Iterators and generators */  | 
1112  | 0  |         case GET_AWAITABLE:  | 
1113  | 0  |             return 0;  | 
1114  | 0  |         case SETUP_ASYNC_WITH:  | 
1115  |  |             /* 0 in the normal flow.  | 
1116  |  |              * Restore the stack position to the position before the result  | 
1117  |  |              * of __aenter__ and push 6 values before jumping to the handler  | 
1118  |  |              * if an exception be raised. */  | 
1119  | 0  |             return jump ? -1 + 6 : 0;  | 
1120  | 0  |         case BEFORE_ASYNC_WITH:  | 
1121  | 0  |             return 1;  | 
1122  | 0  |         case GET_AITER:  | 
1123  | 0  |             return 0;  | 
1124  | 0  |         case GET_ANEXT:  | 
1125  | 0  |             return 1;  | 
1126  | 0  |         case GET_YIELD_FROM_ITER:  | 
1127  | 0  |             return 0;  | 
1128  | 0  |         case END_ASYNC_FOR:  | 
1129  | 0  |             return -7;  | 
1130  | 0  |         case FORMAT_VALUE:  | 
1131  |  |             /* If there's a fmt_spec on the stack, we go from 2->1,  | 
1132  |  |                else 1->1. */  | 
1133  | 0  |             return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;  | 
1134  | 48  |         case LOAD_METHOD:  | 
1135  | 48  |             return 1;  | 
1136  | 0  |         default:  | 
1137  | 0  |             return PY_INVALID_STACK_EFFECT;  | 
1138  | 1.19k  |     }  | 
1139  | 0  |     return PY_INVALID_STACK_EFFECT; /* not reachable */  | 
1140  | 1.19k  | }  | 
1141  |  |  | 
1142  |  | int  | 
1143  |  | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)  | 
1144  | 0  | { | 
1145  | 0  |     return stack_effect(opcode, oparg, jump);  | 
1146  | 0  | }  | 
1147  |  |  | 
1148  |  | int  | 
1149  |  | PyCompile_OpcodeStackEffect(int opcode, int oparg)  | 
1150  | 0  | { | 
1151  | 0  |     return stack_effect(opcode, oparg, -1);  | 
1152  | 0  | }  | 
1153  |  |  | 
1154  |  | /* Add an opcode with no argument.  | 
1155  |  |    Returns 0 on failure, 1 on success.  | 
1156  |  | */  | 
1157  |  |  | 
1158  |  | static int  | 
1159  |  | compiler_addop(struct compiler *c, int opcode)  | 
1160  | 268  | { | 
1161  | 268  |     basicblock *b;  | 
1162  | 268  |     struct instr *i;  | 
1163  | 268  |     int off;  | 
1164  | 268  |     assert(!HAS_ARG(opcode));  | 
1165  | 268  |     if (c->c_do_not_emit_bytecode) { | 
1166  | 0  |         return 1;  | 
1167  | 0  |     }  | 
1168  | 268  |     off = compiler_next_instr(c, c->u->u_curblock);  | 
1169  | 268  |     if (off < 0)  | 
1170  | 0  |         return 0;  | 
1171  | 268  |     b = c->u->u_curblock;  | 
1172  | 268  |     i = &b->b_instr[off];  | 
1173  | 268  |     i->i_opcode = opcode;  | 
1174  | 268  |     i->i_oparg = 0;  | 
1175  | 268  |     if (opcode == RETURN_VALUE)  | 
1176  | 26  |         b->b_return = 1;  | 
1177  | 268  |     compiler_set_lineno(c, off);  | 
1178  | 268  |     return 1;  | 
1179  | 268  | }  | 
1180  |  |  | 
1181  |  | static Py_ssize_t  | 
1182  |  | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o)  | 
1183  | 519  | { | 
1184  | 519  |     PyObject *v;  | 
1185  | 519  |     Py_ssize_t arg;  | 
1186  |  |  | 
1187  | 519  |     v = PyDict_GetItemWithError(dict, o);  | 
1188  | 519  |     if (!v) { | 
1189  | 252  |         if (PyErr_Occurred()) { | 
1190  | 0  |             return -1;  | 
1191  | 0  |         }  | 
1192  | 252  |         arg = PyDict_GET_SIZE(dict);  | 
1193  | 252  |         v = PyLong_FromSsize_t(arg);  | 
1194  | 252  |         if (!v) { | 
1195  | 0  |             return -1;  | 
1196  | 0  |         }  | 
1197  | 252  |         if (PyDict_SetItem(dict, o, v) < 0) { | 
1198  | 0  |             Py_DECREF(v);  | 
1199  | 0  |             return -1;  | 
1200  | 0  |         }  | 
1201  | 252  |         Py_DECREF(v);  | 
1202  | 252  |     }  | 
1203  | 267  |     else  | 
1204  | 267  |         arg = PyLong_AsLong(v);  | 
1205  | 519  |     return arg;  | 
1206  | 519  | }  | 
1207  |  |  | 
1208  |  | // Merge const *o* recursively and return constant key object.  | 
1209  |  | static PyObject*  | 
1210  |  | merge_consts_recursive(struct compiler *c, PyObject *o)  | 
1211  | 116  | { | 
1212  |  |     // None and Ellipsis are singleton, and key is the singleton.  | 
1213  |  |     // No need to merge object and key.  | 
1214  | 116  |     if (o == Py_None || o == Py_Ellipsis) { | 
1215  | 42  |         Py_INCREF(o);  | 
1216  | 42  |         return o;  | 
1217  | 42  |     }  | 
1218  |  |  | 
1219  | 74  |     PyObject *key = _PyCode_ConstantKey(o);  | 
1220  | 74  |     if (key == NULL) { | 
1221  | 0  |         return NULL;  | 
1222  | 0  |     }  | 
1223  |  |  | 
1224  |  |     // t is borrowed reference  | 
1225  | 74  |     PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);  | 
1226  | 74  |     if (t != key) { | 
1227  |  |         // o is registered in c_const_cache.  Just use it.  | 
1228  | 4  |         Py_XINCREF(t);  | 
1229  | 4  |         Py_DECREF(key);  | 
1230  | 4  |         return t;  | 
1231  | 4  |     }  | 
1232  |  |  | 
1233  |  |     // We registered o in c_const_cache.  | 
1234  |  |     // When o is a tuple or frozenset, we want to merge its  | 
1235  |  |     // items too.  | 
1236  | 70  |     if (PyTuple_CheckExact(o)) { | 
1237  | 2  |         Py_ssize_t len = PyTuple_GET_SIZE(o);  | 
1238  | 6  |         for (Py_ssize_t i = 0; i < len; i++) { | 
1239  | 4  |             PyObject *item = PyTuple_GET_ITEM(o, i);  | 
1240  | 4  |             PyObject *u = merge_consts_recursive(c, item);  | 
1241  | 4  |             if (u == NULL) { | 
1242  | 0  |                 Py_DECREF(key);  | 
1243  | 0  |                 return NULL;  | 
1244  | 0  |             }  | 
1245  |  |  | 
1246  |  |             // See _PyCode_ConstantKey()  | 
1247  | 4  |             PyObject *v;  // borrowed  | 
1248  | 4  |             if (PyTuple_CheckExact(u)) { | 
1249  | 4  |                 v = PyTuple_GET_ITEM(u, 1);  | 
1250  | 4  |             }  | 
1251  | 0  |             else { | 
1252  | 0  |                 v = u;  | 
1253  | 0  |             }  | 
1254  | 4  |             if (v != item) { | 
1255  | 0  |                 Py_INCREF(v);  | 
1256  | 0  |                 PyTuple_SET_ITEM(o, i, v);  | 
1257  | 0  |                 Py_DECREF(item);  | 
1258  | 0  |             }  | 
1259  |  |  | 
1260  | 4  |             Py_DECREF(u);  | 
1261  | 4  |         }  | 
1262  | 2  |     }  | 
1263  | 68  |     else if (PyFrozenSet_CheckExact(o)) { | 
1264  |  |         // *key* is tuple. And its first item is frozenset of  | 
1265  |  |         // constant keys.  | 
1266  |  |         // See _PyCode_ConstantKey() for detail.  | 
1267  | 0  |         assert(PyTuple_CheckExact(key));  | 
1268  | 0  |         assert(PyTuple_GET_SIZE(key) == 2);  | 
1269  |  | 
  | 
1270  | 0  |         Py_ssize_t len = PySet_GET_SIZE(o);  | 
1271  | 0  |         if (len == 0) {  // empty frozenset should not be re-created. | 
1272  | 0  |             return key;  | 
1273  | 0  |         }  | 
1274  | 0  |         PyObject *tuple = PyTuple_New(len);  | 
1275  | 0  |         if (tuple == NULL) { | 
1276  | 0  |             Py_DECREF(key);  | 
1277  | 0  |             return NULL;  | 
1278  | 0  |         }  | 
1279  | 0  |         Py_ssize_t i = 0, pos = 0;  | 
1280  | 0  |         PyObject *item;  | 
1281  | 0  |         Py_hash_t hash;  | 
1282  | 0  |         while (_PySet_NextEntry(o, &pos, &item, &hash)) { | 
1283  | 0  |             PyObject *k = merge_consts_recursive(c, item);  | 
1284  | 0  |             if (k == NULL) { | 
1285  | 0  |                 Py_DECREF(tuple);  | 
1286  | 0  |                 Py_DECREF(key);  | 
1287  | 0  |                 return NULL;  | 
1288  | 0  |             }  | 
1289  | 0  |             PyObject *u;  | 
1290  | 0  |             if (PyTuple_CheckExact(k)) { | 
1291  | 0  |                 u = PyTuple_GET_ITEM(k, 1);  | 
1292  | 0  |                 Py_INCREF(u);  | 
1293  | 0  |                 Py_DECREF(k);  | 
1294  | 0  |             }  | 
1295  | 0  |             else { | 
1296  | 0  |                 u = k;  | 
1297  | 0  |             }  | 
1298  | 0  |             PyTuple_SET_ITEM(tuple, i, u);  // Steals reference of u.  | 
1299  | 0  |             i++;  | 
1300  | 0  |         }  | 
1301  |  |  | 
1302  |  |         // Instead of rewriting o, we create new frozenset and embed in the  | 
1303  |  |         // key tuple.  Caller should get merged frozenset from the key tuple.  | 
1304  | 0  |         PyObject *new = PyFrozenSet_New(tuple);  | 
1305  | 0  |         Py_DECREF(tuple);  | 
1306  | 0  |         if (new == NULL) { | 
1307  | 0  |             Py_DECREF(key);  | 
1308  | 0  |             return NULL;  | 
1309  | 0  |         }  | 
1310  | 0  |         assert(PyTuple_GET_ITEM(key, 1) == o);  | 
1311  | 0  |         Py_DECREF(o);  | 
1312  | 0  |         PyTuple_SET_ITEM(key, 1, new);  | 
1313  | 0  |     }  | 
1314  |  |  | 
1315  | 70  |     return key;  | 
1316  | 70  | }  | 
1317  |  |  | 
1318  |  | static Py_ssize_t  | 
1319  |  | compiler_add_const(struct compiler *c, PyObject *o)  | 
1320  | 112  | { | 
1321  | 112  |     if (c->c_do_not_emit_bytecode) { | 
1322  | 0  |         return 0;  | 
1323  | 0  |     }  | 
1324  |  |  | 
1325  | 112  |     PyObject *key = merge_consts_recursive(c, o);  | 
1326  | 112  |     if (key == NULL) { | 
1327  | 0  |         return -1;  | 
1328  | 0  |     }  | 
1329  |  |  | 
1330  | 112  |     Py_ssize_t arg = compiler_add_o(c, c->u->u_consts, key);  | 
1331  | 112  |     Py_DECREF(key);  | 
1332  | 112  |     return arg;  | 
1333  | 112  | }  | 
1334  |  |  | 
1335  |  | static int  | 
1336  |  | compiler_addop_load_const(struct compiler *c, PyObject *o)  | 
1337  | 106  | { | 
1338  | 106  |     if (c->c_do_not_emit_bytecode) { | 
1339  | 0  |         return 1;  | 
1340  | 0  |     }  | 
1341  |  |  | 
1342  | 106  |     Py_ssize_t arg = compiler_add_const(c, o);  | 
1343  | 106  |     if (arg < 0)  | 
1344  | 0  |         return 0;  | 
1345  | 106  |     return compiler_addop_i(c, LOAD_CONST, arg);  | 
1346  | 106  | }  | 
1347  |  |  | 
1348  |  | static int  | 
1349  |  | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,  | 
1350  |  |                      PyObject *o)  | 
1351  | 23  | { | 
1352  | 23  |     if (c->c_do_not_emit_bytecode) { | 
1353  | 0  |         return 1;  | 
1354  | 0  |     }  | 
1355  |  |  | 
1356  | 23  |     Py_ssize_t arg = compiler_add_o(c, dict, o);  | 
1357  | 23  |     if (arg < 0)  | 
1358  | 0  |         return 0;  | 
1359  | 23  |     return compiler_addop_i(c, opcode, arg);  | 
1360  | 23  | }  | 
1361  |  |  | 
1362  |  | static int  | 
1363  |  | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,  | 
1364  |  |                     PyObject *o)  | 
1365  | 60  | { | 
1366  | 60  |     Py_ssize_t arg;  | 
1367  |  |  | 
1368  | 60  |     if (c->c_do_not_emit_bytecode) { | 
1369  | 0  |         return 1;  | 
1370  | 0  |     }  | 
1371  |  |  | 
1372  | 60  |     PyObject *mangled = _Py_Mangle(c->u->u_private, o);  | 
1373  | 60  |     if (!mangled)  | 
1374  | 0  |         return 0;  | 
1375  | 60  |     arg = compiler_add_o(c, dict, mangled);  | 
1376  | 60  |     Py_DECREF(mangled);  | 
1377  | 60  |     if (arg < 0)  | 
1378  | 0  |         return 0;  | 
1379  | 60  |     return compiler_addop_i(c, opcode, arg);  | 
1380  | 60  | }  | 
1381  |  |  | 
1382  |  | /* Add an opcode with an integer argument.  | 
1383  |  |    Returns 0 on failure, 1 on success.  | 
1384  |  | */  | 
1385  |  |  | 
1386  |  | static int  | 
1387  |  | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)  | 
1388  | 691  | { | 
1389  | 691  |     struct instr *i;  | 
1390  | 691  |     int off;  | 
1391  |  |  | 
1392  | 691  |     if (c->c_do_not_emit_bytecode) { | 
1393  | 0  |         return 1;  | 
1394  | 0  |     }  | 
1395  |  |  | 
1396  |  |     /* oparg value is unsigned, but a signed C int is usually used to store  | 
1397  |  |        it in the C code (like Python/ceval.c).  | 
1398  |  |  | 
1399  |  |        Limit to 32-bit signed C int (rather than INT_MAX) for portability.  | 
1400  |  |  | 
1401  |  |        The argument of a concrete bytecode instruction is limited to 8-bit.  | 
1402  |  |        EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */  | 
1403  | 691  |     assert(HAS_ARG(opcode));  | 
1404  | 691  |     assert(0 <= oparg && oparg <= 2147483647);  | 
1405  |  |  | 
1406  | 691  |     off = compiler_next_instr(c, c->u->u_curblock);  | 
1407  | 691  |     if (off < 0)  | 
1408  | 0  |         return 0;  | 
1409  | 691  |     i = &c->u->u_curblock->b_instr[off];  | 
1410  | 691  |     i->i_opcode = opcode;  | 
1411  | 691  |     i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);  | 
1412  | 691  |     compiler_set_lineno(c, off);  | 
1413  | 691  |     return 1;  | 
1414  | 691  | }  | 
1415  |  |  | 
1416  |  | static int  | 
1417  |  | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)  | 
1418  | 124  | { | 
1419  | 124  |     struct instr *i;  | 
1420  | 124  |     int off;  | 
1421  |  |  | 
1422  | 124  |     if (c->c_do_not_emit_bytecode) { | 
1423  | 0  |         return 1;  | 
1424  | 0  |     }  | 
1425  |  |  | 
1426  | 124  |     assert(HAS_ARG(opcode));  | 
1427  | 124  |     assert(b != NULL);  | 
1428  | 124  |     off = compiler_next_instr(c, c->u->u_curblock);  | 
1429  | 124  |     if (off < 0)  | 
1430  | 0  |         return 0;  | 
1431  | 124  |     i = &c->u->u_curblock->b_instr[off];  | 
1432  | 124  |     i->i_opcode = opcode;  | 
1433  | 124  |     i->i_target = b;  | 
1434  | 124  |     if (absolute)  | 
1435  | 42  |         i->i_jabs = 1;  | 
1436  | 82  |     else  | 
1437  | 82  |         i->i_jrel = 1;  | 
1438  | 124  |     compiler_set_lineno(c, off);  | 
1439  | 124  |     return 1;  | 
1440  | 124  | }  | 
1441  |  |  | 
1442  |  | /* NEXT_BLOCK() creates an implicit jump from the current block  | 
1443  |  |    to the new block.  | 
1444  |  |  | 
1445  |  |    The returns inside this macro make it impossible to decref objects  | 
1446  |  |    created in the local function. Local objects should use the arena.  | 
1447  |  | */  | 
1448  | 18  | #define NEXT_BLOCK(C) { \ | 
1449  | 18  |     if (compiler_next_block((C)) == NULL) \  | 
1450  | 18  |         return 0; \  | 
1451  | 18  | }  | 
1452  |  |  | 
1453  | 266  | #define ADDOP(C, OP) { \ | 
1454  | 266  |     if (!compiler_addop((C), (OP))) \  | 
1455  | 266  |         return 0; \  | 
1456  | 266  | }  | 
1457  |  |  | 
1458  | 2  | #define ADDOP_IN_SCOPE(C, OP) { \ | 
1459  | 2  |     if (!compiler_addop((C), (OP))) { \ | 
1460  | 0  |         compiler_exit_scope(c); \  | 
1461  | 0  |         return 0; \  | 
1462  | 0  |     } \  | 
1463  | 2  | }  | 
1464  |  |  | 
1465  | 106  | #define ADDOP_LOAD_CONST(C, O) { \ | 
1466  | 106  |     if (!compiler_addop_load_const((C), (O))) \  | 
1467  | 106  |         return 0; \  | 
1468  | 106  | }  | 
1469  |  |  | 
1470  |  | /* Same as ADDOP_LOAD_CONST, but steals a reference. */  | 
1471  | 0  | #define ADDOP_LOAD_CONST_NEW(C, O) { \ | 
1472  | 0  |     PyObject *__new_const = (O); \  | 
1473  | 0  |     if (__new_const == NULL) { \ | 
1474  | 0  |         return 0; \  | 
1475  | 0  |     } \  | 
1476  | 0  |     if (!compiler_addop_load_const((C), __new_const)) { \ | 
1477  | 0  |         Py_DECREF(__new_const); \  | 
1478  | 0  |         return 0; \  | 
1479  | 0  |     } \  | 
1480  | 0  |     Py_DECREF(__new_const); \  | 
1481  | 0  | }  | 
1482  |  |  | 
1483  | 6  | #define ADDOP_O(C, OP, O, TYPE) { \ | 
1484  | 6  |     if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \  | 
1485  | 6  |         return 0; \  | 
1486  | 6  | }  | 
1487  |  |  | 
1488  |  | /* Same as ADDOP_O, but steals a reference. */  | 
1489  | 17  | #define ADDOP_N(C, OP, O, TYPE) { \ | 
1490  | 17  |     if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ | 
1491  | 0  |         Py_DECREF((O)); \  | 
1492  | 0  |         return 0; \  | 
1493  | 0  |     } \  | 
1494  | 17  |     Py_DECREF((O)); \  | 
1495  | 17  | }  | 
1496  |  |  | 
1497  | 60  | #define ADDOP_NAME(C, OP, O, TYPE) { \ | 
1498  | 60  |     if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \  | 
1499  | 60  |         return 0; \  | 
1500  | 60  | }  | 
1501  |  |  | 
1502  | 178  | #define ADDOP_I(C, OP, O) { \ | 
1503  | 178  |     if (!compiler_addop_i((C), (OP), (O))) \  | 
1504  | 178  |         return 0; \  | 
1505  | 178  | }  | 
1506  |  |  | 
1507  | 42  | #define ADDOP_JABS(C, OP, O) { \ | 
1508  | 42  |     if (!compiler_addop_j((C), (OP), (O), 1)) \  | 
1509  | 42  |         return 0; \  | 
1510  | 42  | }  | 
1511  |  |  | 
1512  | 82  | #define ADDOP_JREL(C, OP, O) { \ | 
1513  | 82  |     if (!compiler_addop_j((C), (OP), (O), 0)) \  | 
1514  | 82  |         return 0; \  | 
1515  | 82  | }  | 
1516  |  |  | 
1517  |  | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument.  They use  | 
1518  |  |    the ASDL name to synthesize the name of the C type and the visit function.  | 
1519  |  | */  | 
1520  |  |  | 
1521  | 473  | #define VISIT(C, TYPE, V) {\ | 
1522  | 473  |     if (!compiler_visit_ ## TYPE((C), (V))) \  | 
1523  | 473  |         return 0; \  | 
1524  | 473  | }  | 
1525  |  |  | 
1526  | 2  | #define VISIT_IN_SCOPE(C, TYPE, V) {\ | 
1527  | 2  |     if (!compiler_visit_ ## TYPE((C), (V))) { \ | 
1528  | 0  |         compiler_exit_scope(c); \  | 
1529  | 0  |         return 0; \  | 
1530  | 0  |     } \  | 
1531  | 2  | }  | 
1532  |  |  | 
1533  | 14  | #define VISIT_SLICE(C, V, CTX) {\ | 
1534  | 14  |     if (!compiler_visit_slice((C), (V), (CTX))) \  | 
1535  | 14  |         return 0; \  | 
1536  | 14  | }  | 
1537  |  |  | 
1538  | 144  | #define VISIT_SEQ(C, TYPE, SEQ) { \ | 
1539  | 144  |     int _i; \  | 
1540  | 144  |     asdl_seq *seq = (SEQ); /* avoid variable capture */ \  | 
1541  | 352  |     for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ | 
1542  | 208  |         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \  | 
1543  | 208  |         if (!compiler_visit_ ## TYPE((C), elt)) \  | 
1544  | 208  |             return 0; \  | 
1545  | 208  |     } \  | 
1546  | 144  | }  | 
1547  |  |  | 
1548  | 4  | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ | 
1549  | 4  |     int _i; \  | 
1550  | 4  |     asdl_seq *seq = (SEQ); /* avoid variable capture */ \  | 
1551  | 8  |     for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ | 
1552  | 4  |         TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \  | 
1553  | 4  |         if (!compiler_visit_ ## TYPE((C), elt)) { \ | 
1554  | 0  |             compiler_exit_scope(c); \  | 
1555  | 0  |             return 0; \  | 
1556  | 0  |         } \  | 
1557  | 4  |     } \  | 
1558  | 4  | }  | 
1559  |  |  | 
1560  |  | /* These macros allows to check only for errors and not emmit bytecode  | 
1561  |  |  * while visiting nodes.  | 
1562  |  | */  | 
1563  |  |  | 
1564  | 0  | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ | 
1565  | 0  |     c->c_do_not_emit_bytecode++;  | 
1566  |  |  | 
1567  |  | #define END_DO_NOT_EMIT_BYTECODE \  | 
1568  | 0  |     c->c_do_not_emit_bytecode--; \  | 
1569  | 0  | }  | 
1570  |  |  | 
1571  |  | /* Search if variable annotations are present statically in a block. */  | 
1572  |  |  | 
1573  |  | static int  | 
1574  |  | find_ann(asdl_seq *stmts)  | 
1575  | 114  | { | 
1576  | 114  |     int i, j, res = 0;  | 
1577  | 114  |     stmt_ty st;  | 
1578  |  |  | 
1579  | 246  |     for (i = 0; i < asdl_seq_LEN(stmts); i++) { | 
1580  | 132  |         st = (stmt_ty)asdl_seq_GET(stmts, i);  | 
1581  | 132  |         switch (st->kind) { | 
1582  | 0  |         case AnnAssign_kind:  | 
1583  | 0  |             return 1;  | 
1584  | 8  |         case For_kind:  | 
1585  | 8  |             res = find_ann(st->v.For.body) ||  | 
1586  | 8  |                   find_ann(st->v.For.orelse);  | 
1587  | 8  |             break;  | 
1588  | 0  |         case AsyncFor_kind:  | 
1589  | 0  |             res = find_ann(st->v.AsyncFor.body) ||  | 
1590  | 0  |                   find_ann(st->v.AsyncFor.orelse);  | 
1591  | 0  |             break;  | 
1592  | 0  |         case While_kind:  | 
1593  | 0  |             res = find_ann(st->v.While.body) ||  | 
1594  | 0  |                   find_ann(st->v.While.orelse);  | 
1595  | 0  |             break;  | 
1596  | 8  |         case If_kind:  | 
1597  | 8  |             res = find_ann(st->v.If.body) ||  | 
1598  | 8  |                   find_ann(st->v.If.orelse);  | 
1599  | 8  |             break;  | 
1600  | 0  |         case With_kind:  | 
1601  | 0  |             res = find_ann(st->v.With.body);  | 
1602  | 0  |             break;  | 
1603  | 0  |         case AsyncWith_kind:  | 
1604  | 0  |             res = find_ann(st->v.AsyncWith.body);  | 
1605  | 0  |             break;  | 
1606  | 16  |         case Try_kind:  | 
1607  | 34  |             for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { | 
1608  | 18  |                 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(  | 
1609  | 18  |                     st->v.Try.handlers, j);  | 
1610  | 18  |                 if (find_ann(handler->v.ExceptHandler.body)) { | 
1611  | 0  |                     return 1;  | 
1612  | 0  |                 }  | 
1613  | 18  |             }  | 
1614  | 16  |             res = find_ann(st->v.Try.body) ||  | 
1615  | 16  |                   find_ann(st->v.Try.finalbody) ||  | 
1616  | 16  |                   find_ann(st->v.Try.orelse);  | 
1617  | 16  |             break;  | 
1618  | 100  |         default:  | 
1619  | 100  |             res = 0;  | 
1620  | 132  |         }  | 
1621  | 132  |         if (res) { | 
1622  | 0  |             break;  | 
1623  | 0  |         }  | 
1624  | 132  |     }  | 
1625  | 114  |     return res;  | 
1626  | 114  | }  | 
1627  |  |  | 
1628  |  | /*  | 
1629  |  |  * Frame block handling functions  | 
1630  |  |  */  | 
1631  |  |  | 
1632  |  | static int  | 
1633  |  | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,  | 
1634  |  |                      basicblock *exit)  | 
1635  | 60  | { | 
1636  | 60  |     struct fblockinfo *f;  | 
1637  | 60  |     if (c->u->u_nfblocks >= CO_MAXBLOCKS) { | 
1638  | 0  |         PyErr_SetString(PyExc_SyntaxError,  | 
1639  | 0  |                         "too many statically nested blocks");  | 
1640  | 0  |         return 0;  | 
1641  | 0  |     }  | 
1642  | 60  |     f = &c->u->u_fblock[c->u->u_nfblocks++];  | 
1643  | 60  |     f->fb_type = t;  | 
1644  | 60  |     f->fb_block = b;  | 
1645  | 60  |     f->fb_exit = exit;  | 
1646  | 60  |     return 1;  | 
1647  | 60  | }  | 
1648  |  |  | 
1649  |  | static void  | 
1650  |  | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)  | 
1651  | 60  | { | 
1652  | 60  |     struct compiler_unit *u = c->u;  | 
1653  | 60  |     assert(u->u_nfblocks > 0);  | 
1654  | 60  |     u->u_nfblocks--;  | 
1655  | 60  |     assert(u->u_fblock[u->u_nfblocks].fb_type == t);  | 
1656  | 60  |     assert(u->u_fblock[u->u_nfblocks].fb_block == b);  | 
1657  | 60  | }  | 
1658  |  |  | 
1659  |  | /* Unwind a frame block.  If preserve_tos is true, the TOS before  | 
1660  |  |  * popping the blocks will be restored afterwards.  | 
1661  |  |  */  | 
1662  |  | static int  | 
1663  |  | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,  | 
1664  |  |                        int preserve_tos)  | 
1665  | 4  | { | 
1666  | 4  |     switch (info->fb_type) { | 
1667  | 0  |         case WHILE_LOOP:  | 
1668  | 0  |             return 1;  | 
1669  |  |  | 
1670  | 0  |         case FINALLY_END:  | 
1671  | 0  |             info->fb_exit = NULL;  | 
1672  | 0  |             ADDOP_I(c, POP_FINALLY, preserve_tos);  | 
1673  | 0  |             if (preserve_tos) { | 
1674  | 0  |                 ADDOP(c, ROT_TWO);  | 
1675  | 0  |             }  | 
1676  | 0  |             ADDOP(c, POP_TOP);  | 
1677  | 0  |             return 1;  | 
1678  |  |  | 
1679  | 0  |         case FOR_LOOP:  | 
1680  |  |             /* Pop the iterator */  | 
1681  | 0  |             if (preserve_tos) { | 
1682  | 0  |                 ADDOP(c, ROT_TWO);  | 
1683  | 0  |             }  | 
1684  | 0  |             ADDOP(c, POP_TOP);  | 
1685  | 0  |             return 1;  | 
1686  |  |  | 
1687  | 2  |         case EXCEPT:  | 
1688  | 2  |             ADDOP(c, POP_BLOCK);  | 
1689  | 2  |             return 1;  | 
1690  |  |  | 
1691  | 0  |         case FINALLY_TRY:  | 
1692  | 0  |             ADDOP(c, POP_BLOCK);  | 
1693  | 0  |             ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);  | 
1694  | 0  |             return 1;  | 
1695  |  |  | 
1696  | 0  |         case FINALLY_TRY2:  | 
1697  | 0  |             ADDOP(c, POP_BLOCK);  | 
1698  | 0  |             if (preserve_tos) { | 
1699  | 0  |                 ADDOP(c, ROT_TWO);  | 
1700  | 0  |                 ADDOP(c, POP_TOP);  | 
1701  | 0  |                 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);  | 
1702  | 0  |             }  | 
1703  | 0  |             else { | 
1704  | 0  |                 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);  | 
1705  | 0  |                 ADDOP(c, POP_TOP);  | 
1706  | 0  |             }  | 
1707  | 0  |             return 1;  | 
1708  |  |  | 
1709  | 0  |         case WITH:  | 
1710  | 0  |         case ASYNC_WITH:  | 
1711  | 0  |             ADDOP(c, POP_BLOCK);  | 
1712  | 0  |             if (preserve_tos) { | 
1713  | 0  |                 ADDOP(c, ROT_TWO);  | 
1714  | 0  |             }  | 
1715  | 0  |             ADDOP(c, BEGIN_FINALLY);  | 
1716  | 0  |             ADDOP(c, WITH_CLEANUP_START);  | 
1717  | 0  |             if (info->fb_type == ASYNC_WITH) { | 
1718  | 0  |                 ADDOP(c, GET_AWAITABLE);  | 
1719  | 0  |                 ADDOP_LOAD_CONST(c, Py_None);  | 
1720  | 0  |                 ADDOP(c, YIELD_FROM);  | 
1721  | 0  |             }  | 
1722  | 0  |             ADDOP(c, WITH_CLEANUP_FINISH);  | 
1723  | 0  |             ADDOP_I(c, POP_FINALLY, 0);  | 
1724  | 0  |             return 1;  | 
1725  |  |  | 
1726  | 2  |         case HANDLER_CLEANUP:  | 
1727  | 2  |             if (preserve_tos) { | 
1728  | 2  |                 ADDOP(c, ROT_FOUR);  | 
1729  | 2  |             }  | 
1730  | 2  |             if (info->fb_exit) { | 
1731  | 0  |                 ADDOP(c, POP_BLOCK);  | 
1732  | 0  |                 ADDOP(c, POP_EXCEPT);  | 
1733  | 0  |                 ADDOP_JREL(c, CALL_FINALLY, info->fb_exit);  | 
1734  | 0  |             }  | 
1735  | 2  |             else { | 
1736  | 2  |                 ADDOP(c, POP_EXCEPT);  | 
1737  | 2  |             }  | 
1738  | 2  |             return 1;  | 
1739  | 4  |     }  | 
1740  | 4  |     Py_UNREACHABLE();  | 
1741  | 4  | }  | 
1742  |  |  | 
1743  |  | /* Compile a sequence of statements, checking for a docstring  | 
1744  |  |    and for annotations. */  | 
1745  |  |  | 
1746  |  | static int  | 
1747  |  | compiler_body(struct compiler *c, asdl_seq *stmts)  | 
1748  | 16  | { | 
1749  | 16  |     int i = 0;  | 
1750  | 16  |     stmt_ty st;  | 
1751  | 16  |     PyObject *docstring;  | 
1752  |  |  | 
1753  |  |     /* Set current line number to the line number of first statement.  | 
1754  |  |        This way line number for SETUP_ANNOTATIONS will always  | 
1755  |  |        coincide with the line number of first "real" statement in module.  | 
1756  |  |        If body is empty, then lineno will be set later in assemble. */  | 
1757  | 16  |     if (c->u->u_scope_type == COMPILER_SCOPE_MODULE &&  | 
1758  | 16  |         !c->u->u_lineno && asdl_seq_LEN(stmts)) { | 
1759  | 16  |         st = (stmt_ty)asdl_seq_GET(stmts, 0);  | 
1760  | 16  |         c->u->u_lineno = st->lineno;  | 
1761  | 16  |     }  | 
1762  |  |     /* Every annotated class and module should have __annotations__. */  | 
1763  | 16  |     if (find_ann(stmts)) { | 
1764  | 0  |         ADDOP(c, SETUP_ANNOTATIONS);  | 
1765  | 0  |     }  | 
1766  | 16  |     if (!asdl_seq_LEN(stmts))  | 
1767  | 0  |         return 1;  | 
1768  |  |     /* if not -OO mode, set docstring */  | 
1769  | 16  |     if (c->c_optimize < 2) { | 
1770  | 16  |         docstring = _PyAST_GetDocString(stmts);  | 
1771  | 16  |         if (docstring) { | 
1772  | 0  |             i = 1;  | 
1773  | 0  |             st = (stmt_ty)asdl_seq_GET(stmts, 0);  | 
1774  | 0  |             assert(st->kind == Expr_kind);  | 
1775  | 0  |             VISIT(c, expr, st->v.Expr.value);  | 
1776  | 0  |             if (!compiler_nameop(c, __doc__, Store))  | 
1777  | 0  |                 return 0;  | 
1778  | 0  |         }  | 
1779  | 16  |     }  | 
1780  | 34  |     for (; i < asdl_seq_LEN(stmts); i++)  | 
1781  | 18  |         VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));  | 
1782  | 16  |     return 1;  | 
1783  | 16  | }  | 
1784  |  |  | 
1785  |  | static PyCodeObject *  | 
1786  |  | compiler_mod(struct compiler *c, mod_ty mod)  | 
1787  | 16  | { | 
1788  | 16  |     PyCodeObject *co;  | 
1789  | 16  |     int addNone = 1;  | 
1790  | 16  |     static PyObject *module;  | 
1791  | 16  |     if (!module) { | 
1792  | 14  |         module = PyUnicode_InternFromString("<module>"); | 
1793  | 14  |         if (!module)  | 
1794  | 0  |             return NULL;  | 
1795  | 14  |     }  | 
1796  |  |     /* Use 0 for firstlineno initially, will fixup in assemble(). */  | 
1797  | 16  |     if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))  | 
1798  | 0  |         return NULL;  | 
1799  | 16  |     switch (mod->kind) { | 
1800  | 16  |     case Module_kind:  | 
1801  | 16  |         if (!compiler_body(c, mod->v.Module.body)) { | 
1802  | 0  |             compiler_exit_scope(c);  | 
1803  | 0  |             return 0;  | 
1804  | 0  |         }  | 
1805  | 16  |         break;  | 
1806  | 16  |     case Interactive_kind:  | 
1807  | 0  |         if (find_ann(mod->v.Interactive.body)) { | 
1808  | 0  |             ADDOP(c, SETUP_ANNOTATIONS);  | 
1809  | 0  |         }  | 
1810  | 0  |         c->c_interactive = 1;  | 
1811  | 0  |         VISIT_SEQ_IN_SCOPE(c, stmt,  | 
1812  | 0  |                                 mod->v.Interactive.body);  | 
1813  | 0  |         break;  | 
1814  | 0  |     case Expression_kind:  | 
1815  | 0  |         VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);  | 
1816  | 0  |         addNone = 0;  | 
1817  | 0  |         break;  | 
1818  | 0  |     case Suite_kind:  | 
1819  | 0  |         PyErr_SetString(PyExc_SystemError,  | 
1820  | 0  |                         "suite should not be possible");  | 
1821  | 0  |         return 0;  | 
1822  | 0  |     default:  | 
1823  | 0  |         PyErr_Format(PyExc_SystemError,  | 
1824  | 0  |                      "module kind %d should not be possible",  | 
1825  | 0  |                      mod->kind);  | 
1826  | 0  |         return 0;  | 
1827  | 16  |     }  | 
1828  | 16  |     co = assemble(c, addNone);  | 
1829  | 16  |     compiler_exit_scope(c);  | 
1830  | 16  |     return co;  | 
1831  | 16  | }  | 
1832  |  |  | 
1833  |  | /* The test for LOCAL must come before the test for FREE in order to  | 
1834  |  |    handle classes where name is both local and free.  The local var is  | 
1835  |  |    a method and the free var is a free var referenced within a method.  | 
1836  |  | */  | 
1837  |  |  | 
1838  |  | static int  | 
1839  |  | get_ref_type(struct compiler *c, PyObject *name)  | 
1840  | 0  | { | 
1841  | 0  |     int scope;  | 
1842  | 0  |     if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&  | 
1843  | 0  |         _PyUnicode_EqualToASCIIString(name, "__class__"))  | 
1844  | 0  |         return CELL;  | 
1845  | 0  |     scope = PyST_GetScope(c->u->u_ste, name);  | 
1846  | 0  |     if (scope == 0) { | 
1847  | 0  |         char buf[350];  | 
1848  | 0  |         PyOS_snprintf(buf, sizeof(buf),  | 
1849  | 0  |                       "unknown scope for %.100s in %.100s(%s)\n"  | 
1850  | 0  |                       "symbols: %s\nlocals: %s\nglobals: %s",  | 
1851  | 0  |                       PyUnicode_AsUTF8(name),  | 
1852  | 0  |                       PyUnicode_AsUTF8(c->u->u_name),  | 
1853  | 0  |                       PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),  | 
1854  | 0  |                       PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),  | 
1855  | 0  |                       PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),  | 
1856  | 0  |                       PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))  | 
1857  | 0  |         );  | 
1858  | 0  |         Py_FatalError(buf);  | 
1859  | 0  |     }  | 
1860  |  |  | 
1861  | 0  |     return scope;  | 
1862  | 0  | }  | 
1863  |  |  | 
1864  |  | static int  | 
1865  |  | compiler_lookup_arg(PyObject *dict, PyObject *name)  | 
1866  | 0  | { | 
1867  | 0  |     PyObject *v;  | 
1868  | 0  |     v = PyDict_GetItem(dict, name);  | 
1869  | 0  |     if (v == NULL)  | 
1870  | 0  |         return -1;  | 
1871  | 0  |     return PyLong_AS_LONG(v);  | 
1872  | 0  | }  | 
1873  |  |  | 
1874  |  | static int  | 
1875  |  | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)  | 
1876  | 6  | { | 
1877  | 6  |     Py_ssize_t i, free = PyCode_GetNumFree(co);  | 
1878  | 6  |     if (qualname == NULL)  | 
1879  | 0  |         qualname = co->co_name;  | 
1880  |  |  | 
1881  | 6  |     if (free) { | 
1882  | 0  |         for (i = 0; i < free; ++i) { | 
1883  |  |             /* Bypass com_addop_varname because it will generate  | 
1884  |  |                LOAD_DEREF but LOAD_CLOSURE is needed.  | 
1885  |  |             */  | 
1886  | 0  |             PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);  | 
1887  | 0  |             int arg, reftype;  | 
1888  |  |  | 
1889  |  |             /* Special case: If a class contains a method with a  | 
1890  |  |                free variable that has the same name as a method,  | 
1891  |  |                the name will be considered free *and* local in the  | 
1892  |  |                class.  It should be handled by the closure, as  | 
1893  |  |                well as by the normal name loookup logic.  | 
1894  |  |             */  | 
1895  | 0  |             reftype = get_ref_type(c, name);  | 
1896  | 0  |             if (reftype == CELL)  | 
1897  | 0  |                 arg = compiler_lookup_arg(c->u->u_cellvars, name);  | 
1898  | 0  |             else /* (reftype == FREE) */  | 
1899  | 0  |                 arg = compiler_lookup_arg(c->u->u_freevars, name);  | 
1900  | 0  |             if (arg == -1) { | 
1901  | 0  |                 fprintf(stderr,  | 
1902  | 0  |                     "lookup %s in %s %d %d\n"  | 
1903  | 0  |                     "freevars of %s: %s\n",  | 
1904  | 0  |                     PyUnicode_AsUTF8(PyObject_Repr(name)),  | 
1905  | 0  |                     PyUnicode_AsUTF8(c->u->u_name),  | 
1906  | 0  |                     reftype, arg,  | 
1907  | 0  |                     PyUnicode_AsUTF8(co->co_name),  | 
1908  | 0  |                     PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));  | 
1909  | 0  |                 Py_FatalError("compiler_make_closure()"); | 
1910  | 0  |             }  | 
1911  | 0  |             ADDOP_I(c, LOAD_CLOSURE, arg);  | 
1912  | 0  |         }  | 
1913  | 0  |         flags |= 0x08;  | 
1914  | 0  |         ADDOP_I(c, BUILD_TUPLE, free);  | 
1915  | 0  |     }  | 
1916  | 6  |     ADDOP_LOAD_CONST(c, (PyObject*)co);  | 
1917  | 6  |     ADDOP_LOAD_CONST(c, qualname);  | 
1918  | 6  |     ADDOP_I(c, MAKE_FUNCTION, flags);  | 
1919  | 6  |     return 1;  | 
1920  | 6  | }  | 
1921  |  |  | 
1922  |  | static int  | 
1923  |  | compiler_decorators(struct compiler *c, asdl_seq* decos)  | 
1924  | 4  | { | 
1925  | 4  |     int i;  | 
1926  |  |  | 
1927  | 4  |     if (!decos)  | 
1928  | 4  |         return 1;  | 
1929  |  |  | 
1930  | 0  |     for (i = 0; i < asdl_seq_LEN(decos); i++) { | 
1931  | 0  |         VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));  | 
1932  | 0  |     }  | 
1933  | 0  |     return 1;  | 
1934  | 0  | }  | 
1935  |  |  | 
1936  |  | static int  | 
1937  |  | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,  | 
1938  |  |                               asdl_seq *kw_defaults)  | 
1939  | 0  | { | 
1940  |  |     /* Push a dict of keyword-only default values.  | 
1941  |  |  | 
1942  |  |        Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.  | 
1943  |  |        */  | 
1944  | 0  |     int i;  | 
1945  | 0  |     PyObject *keys = NULL;  | 
1946  |  | 
  | 
1947  | 0  |     for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { | 
1948  | 0  |         arg_ty arg = asdl_seq_GET(kwonlyargs, i);  | 
1949  | 0  |         expr_ty default_ = asdl_seq_GET(kw_defaults, i);  | 
1950  | 0  |         if (default_) { | 
1951  | 0  |             PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);  | 
1952  | 0  |             if (!mangled) { | 
1953  | 0  |                 goto error;  | 
1954  | 0  |             }  | 
1955  | 0  |             if (keys == NULL) { | 
1956  | 0  |                 keys = PyList_New(1);  | 
1957  | 0  |                 if (keys == NULL) { | 
1958  | 0  |                     Py_DECREF(mangled);  | 
1959  | 0  |                     return 0;  | 
1960  | 0  |                 }  | 
1961  | 0  |                 PyList_SET_ITEM(keys, 0, mangled);  | 
1962  | 0  |             }  | 
1963  | 0  |             else { | 
1964  | 0  |                 int res = PyList_Append(keys, mangled);  | 
1965  | 0  |                 Py_DECREF(mangled);  | 
1966  | 0  |                 if (res == -1) { | 
1967  | 0  |                     goto error;  | 
1968  | 0  |                 }  | 
1969  | 0  |             }  | 
1970  | 0  |             if (!compiler_visit_expr(c, default_)) { | 
1971  | 0  |                 goto error;  | 
1972  | 0  |             }  | 
1973  | 0  |         }  | 
1974  | 0  |     }  | 
1975  | 0  |     if (keys != NULL) { | 
1976  | 0  |         Py_ssize_t default_count = PyList_GET_SIZE(keys);  | 
1977  | 0  |         PyObject *keys_tuple = PyList_AsTuple(keys);  | 
1978  | 0  |         Py_DECREF(keys);  | 
1979  | 0  |         ADDOP_LOAD_CONST_NEW(c, keys_tuple);  | 
1980  | 0  |         ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);  | 
1981  | 0  |         assert(default_count > 0);  | 
1982  | 0  |         return 1;  | 
1983  | 0  |     }  | 
1984  | 0  |     else { | 
1985  | 0  |         return -1;  | 
1986  | 0  |     }  | 
1987  |  |  | 
1988  | 0  | error:  | 
1989  | 0  |     Py_XDECREF(keys);  | 
1990  | 0  |     return 0;  | 
1991  | 0  | }  | 
1992  |  |  | 
1993  |  | static int  | 
1994  |  | compiler_visit_annexpr(struct compiler *c, expr_ty annotation)  | 
1995  | 0  | { | 
1996  | 0  |     ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));  | 
1997  | 0  |     return 1;  | 
1998  | 0  | }  | 
1999  |  |  | 
2000  |  | static int  | 
2001  |  | compiler_visit_argannotation(struct compiler *c, identifier id,  | 
2002  |  |     expr_ty annotation, PyObject *names)  | 
2003  | 19  | { | 
2004  | 19  |     if (annotation) { | 
2005  | 0  |         PyObject *mangled;  | 
2006  | 0  |         if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { | 
2007  | 0  |             VISIT(c, annexpr, annotation)  | 
2008  | 0  |         }  | 
2009  | 0  |         else { | 
2010  | 0  |             VISIT(c, expr, annotation);  | 
2011  | 0  |         }  | 
2012  | 0  |         mangled = _Py_Mangle(c->u->u_private, id);  | 
2013  | 0  |         if (!mangled)  | 
2014  | 0  |             return 0;  | 
2015  | 0  |         if (PyList_Append(names, mangled) < 0) { | 
2016  | 0  |             Py_DECREF(mangled);  | 
2017  | 0  |             return 0;  | 
2018  | 0  |         }  | 
2019  | 0  |         Py_DECREF(mangled);  | 
2020  | 0  |     }  | 
2021  | 19  |     return 1;  | 
2022  | 19  | }  | 
2023  |  |  | 
2024  |  | static int  | 
2025  |  | compiler_visit_argannotations(struct compiler *c, asdl_seq* args,  | 
2026  |  |                               PyObject *names)  | 
2027  | 12  | { | 
2028  | 12  |     int i;  | 
2029  | 27  |     for (i = 0; i < asdl_seq_LEN(args); i++) { | 
2030  | 15  |         arg_ty arg = (arg_ty)asdl_seq_GET(args, i);  | 
2031  | 15  |         if (!compiler_visit_argannotation(  | 
2032  | 15  |                         c,  | 
2033  | 15  |                         arg->arg,  | 
2034  | 15  |                         arg->annotation,  | 
2035  | 15  |                         names))  | 
2036  | 0  |             return 0;  | 
2037  | 15  |     }  | 
2038  | 12  |     return 1;  | 
2039  | 12  | }  | 
2040  |  |  | 
2041  |  | static int  | 
2042  |  | compiler_visit_annotations(struct compiler *c, arguments_ty args,  | 
2043  |  |                            expr_ty returns)  | 
2044  | 4  | { | 
2045  |  |     /* Push arg annotation dict.  | 
2046  |  |        The expressions are evaluated out-of-order wrt the source code.  | 
2047  |  |  | 
2048  |  |        Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.  | 
2049  |  |        */  | 
2050  | 4  |     static identifier return_str;  | 
2051  | 4  |     PyObject *names;  | 
2052  | 4  |     Py_ssize_t len;  | 
2053  | 4  |     names = PyList_New(0);  | 
2054  | 4  |     if (!names)  | 
2055  | 0  |         return 0;  | 
2056  |  |  | 
2057  | 4  |     if (!compiler_visit_argannotations(c, args->args, names))  | 
2058  | 0  |         goto error;  | 
2059  | 4  |     if (!compiler_visit_argannotations(c, args->posonlyargs, names))  | 
2060  | 0  |         goto error;  | 
2061  | 4  |     if (args->vararg && args->vararg->annotation &&  | 
2062  | 0  |         !compiler_visit_argannotation(c, args->vararg->arg,  | 
2063  | 0  |                                      args->vararg->annotation, names))  | 
2064  | 0  |         goto error;  | 
2065  | 4  |     if (!compiler_visit_argannotations(c, args->kwonlyargs, names))  | 
2066  | 0  |         goto error;  | 
2067  | 4  |     if (args->kwarg && args->kwarg->annotation &&  | 
2068  | 0  |         !compiler_visit_argannotation(c, args->kwarg->arg,  | 
2069  | 0  |                                      args->kwarg->annotation, names))  | 
2070  | 0  |         goto error;  | 
2071  |  |  | 
2072  | 4  |     if (!return_str) { | 
2073  | 3  |         return_str = PyUnicode_InternFromString("return"); | 
2074  | 3  |         if (!return_str)  | 
2075  | 0  |             goto error;  | 
2076  | 3  |     }  | 
2077  | 4  |     if (!compiler_visit_argannotation(c, return_str, returns, names)) { | 
2078  | 0  |         goto error;  | 
2079  | 0  |     }  | 
2080  |  |  | 
2081  | 4  |     len = PyList_GET_SIZE(names);  | 
2082  | 4  |     if (len) { | 
2083  | 0  |         PyObject *keytuple = PyList_AsTuple(names);  | 
2084  | 0  |         Py_DECREF(names);  | 
2085  | 0  |         ADDOP_LOAD_CONST_NEW(c, keytuple);  | 
2086  | 0  |         ADDOP_I(c, BUILD_CONST_KEY_MAP, len);  | 
2087  | 0  |         return 1;  | 
2088  | 0  |     }  | 
2089  | 4  |     else { | 
2090  | 4  |         Py_DECREF(names);  | 
2091  | 4  |         return -1;  | 
2092  | 4  |     }  | 
2093  |  |  | 
2094  | 0  | error:  | 
2095  | 0  |     Py_DECREF(names);  | 
2096  | 0  |     return 0;  | 
2097  | 4  | }  | 
2098  |  |  | 
2099  |  | static int  | 
2100  |  | compiler_visit_defaults(struct compiler *c, arguments_ty args)  | 
2101  | 0  | { | 
2102  | 0  |     VISIT_SEQ(c, expr, args->defaults);  | 
2103  | 0  |     ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));  | 
2104  | 0  |     return 1;  | 
2105  | 0  | }  | 
2106  |  |  | 
2107  |  | static Py_ssize_t  | 
2108  |  | compiler_default_arguments(struct compiler *c, arguments_ty args)  | 
2109  | 6  | { | 
2110  | 6  |     Py_ssize_t funcflags = 0;  | 
2111  | 6  |     if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { | 
2112  | 0  |         if (!compiler_visit_defaults(c, args))  | 
2113  | 0  |             return -1;  | 
2114  | 0  |         funcflags |= 0x01;  | 
2115  | 0  |     }  | 
2116  | 6  |     if (args->kwonlyargs) { | 
2117  | 0  |         int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,  | 
2118  | 0  |                                                 args->kw_defaults);  | 
2119  | 0  |         if (res == 0) { | 
2120  | 0  |             return -1;  | 
2121  | 0  |         }  | 
2122  | 0  |         else if (res > 0) { | 
2123  | 0  |             funcflags |= 0x02;  | 
2124  | 0  |         }  | 
2125  | 0  |     }  | 
2126  | 6  |     return funcflags;  | 
2127  | 6  | }  | 
2128  |  |  | 
2129  |  | static int  | 
2130  |  | compiler_function(struct compiler *c, stmt_ty s, int is_async)  | 
2131  | 4  | { | 
2132  | 4  |     PyCodeObject *co;  | 
2133  | 4  |     PyObject *qualname, *docstring = NULL;  | 
2134  | 4  |     arguments_ty args;  | 
2135  | 4  |     expr_ty returns;  | 
2136  | 4  |     identifier name;  | 
2137  | 4  |     asdl_seq* decos;  | 
2138  | 4  |     asdl_seq *body;  | 
2139  | 4  |     Py_ssize_t i, funcflags;  | 
2140  | 4  |     int annotations;  | 
2141  | 4  |     int scope_type;  | 
2142  | 4  |     int firstlineno;  | 
2143  |  |  | 
2144  | 4  |     if (is_async) { | 
2145  | 0  |         assert(s->kind == AsyncFunctionDef_kind);  | 
2146  |  | 
  | 
2147  | 0  |         args = s->v.AsyncFunctionDef.args;  | 
2148  | 0  |         returns = s->v.AsyncFunctionDef.returns;  | 
2149  | 0  |         decos = s->v.AsyncFunctionDef.decorator_list;  | 
2150  | 0  |         name = s->v.AsyncFunctionDef.name;  | 
2151  | 0  |         body = s->v.AsyncFunctionDef.body;  | 
2152  |  | 
  | 
2153  | 0  |         scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;  | 
2154  | 4  |     } else { | 
2155  | 4  |         assert(s->kind == FunctionDef_kind);  | 
2156  |  |  | 
2157  | 4  |         args = s->v.FunctionDef.args;  | 
2158  | 4  |         returns = s->v.FunctionDef.returns;  | 
2159  | 4  |         decos = s->v.FunctionDef.decorator_list;  | 
2160  | 4  |         name = s->v.FunctionDef.name;  | 
2161  | 4  |         body = s->v.FunctionDef.body;  | 
2162  |  |  | 
2163  | 4  |         scope_type = COMPILER_SCOPE_FUNCTION;  | 
2164  | 4  |     }  | 
2165  |  |  | 
2166  | 4  |     if (!compiler_decorators(c, decos))  | 
2167  | 0  |         return 0;  | 
2168  |  |  | 
2169  | 4  |     firstlineno = s->lineno;  | 
2170  | 4  |     if (asdl_seq_LEN(decos)) { | 
2171  | 0  |         firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;  | 
2172  | 0  |     }  | 
2173  |  |  | 
2174  | 4  |     funcflags = compiler_default_arguments(c, args);  | 
2175  | 4  |     if (funcflags == -1) { | 
2176  | 0  |         return 0;  | 
2177  | 0  |     }  | 
2178  |  |  | 
2179  | 4  |     annotations = compiler_visit_annotations(c, args, returns);  | 
2180  | 4  |     if (annotations == 0) { | 
2181  | 0  |         return 0;  | 
2182  | 0  |     }  | 
2183  | 4  |     else if (annotations > 0) { | 
2184  | 0  |         funcflags |= 0x04;  | 
2185  | 0  |     }  | 
2186  |  |  | 
2187  | 4  |     if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { | 
2188  | 0  |         return 0;  | 
2189  | 0  |     }  | 
2190  |  |  | 
2191  |  |     /* if not -OO mode, add docstring */  | 
2192  | 4  |     if (c->c_optimize < 2) { | 
2193  | 4  |         docstring = _PyAST_GetDocString(body);  | 
2194  | 4  |     }  | 
2195  | 4  |     if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { | 
2196  | 0  |         compiler_exit_scope(c);  | 
2197  | 0  |         return 0;  | 
2198  | 0  |     }  | 
2199  |  |  | 
2200  | 4  |     c->u->u_argcount = asdl_seq_LEN(args->args);  | 
2201  | 4  |     c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);  | 
2202  | 4  |     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);  | 
2203  | 4  |     VISIT_SEQ_IN_SCOPE(c, stmt, body);  | 
2204  | 4  |     co = assemble(c, 1);  | 
2205  | 4  |     qualname = c->u->u_qualname;  | 
2206  | 4  |     Py_INCREF(qualname);  | 
2207  | 4  |     compiler_exit_scope(c);  | 
2208  | 4  |     if (co == NULL) { | 
2209  | 0  |         Py_XDECREF(qualname);  | 
2210  | 0  |         Py_XDECREF(co);  | 
2211  | 0  |         return 0;  | 
2212  | 0  |     }  | 
2213  |  |  | 
2214  | 4  |     compiler_make_closure(c, co, funcflags, qualname);  | 
2215  | 4  |     Py_DECREF(qualname);  | 
2216  | 4  |     Py_DECREF(co);  | 
2217  |  |  | 
2218  |  |     /* decorators */  | 
2219  | 4  |     for (i = 0; i < asdl_seq_LEN(decos); i++) { | 
2220  | 0  |         ADDOP_I(c, CALL_FUNCTION, 1);  | 
2221  | 0  |     }  | 
2222  |  |  | 
2223  | 4  |     return compiler_nameop(c, name, Store);  | 
2224  | 4  | }  | 
2225  |  |  | 
2226  |  | static int  | 
2227  |  | compiler_class(struct compiler *c, stmt_ty s)  | 
2228  | 0  | { | 
2229  | 0  |     PyCodeObject *co;  | 
2230  | 0  |     PyObject *str;  | 
2231  | 0  |     int i, firstlineno;  | 
2232  | 0  |     asdl_seq* decos = s->v.ClassDef.decorator_list;  | 
2233  |  | 
  | 
2234  | 0  |     if (!compiler_decorators(c, decos))  | 
2235  | 0  |         return 0;  | 
2236  |  |  | 
2237  | 0  |     firstlineno = s->lineno;  | 
2238  | 0  |     if (asdl_seq_LEN(decos)) { | 
2239  | 0  |         firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;  | 
2240  | 0  |     }  | 
2241  |  |  | 
2242  |  |     /* ultimately generate code for:  | 
2243  |  |          <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)  | 
2244  |  |        where:  | 
2245  |  |          <func> is a function/closure created from the class body;  | 
2246  |  |             it has a single argument (__locals__) where the dict  | 
2247  |  |             (or MutableSequence) representing the locals is passed  | 
2248  |  |          <name> is the class name  | 
2249  |  |          <bases> is the positional arguments and *varargs argument  | 
2250  |  |          <keywords> is the keyword arguments and **kwds argument  | 
2251  |  |        This borrows from compiler_call.  | 
2252  |  |     */  | 
2253  |  |  | 
2254  |  |     /* 1. compile the class body into a code object */  | 
2255  | 0  |     if (!compiler_enter_scope(c, s->v.ClassDef.name,  | 
2256  | 0  |                               COMPILER_SCOPE_CLASS, (void *)s, firstlineno))  | 
2257  | 0  |         return 0;  | 
2258  |  |     /* this block represents what we do in the new scope */  | 
2259  | 0  |     { | 
2260  |  |         /* use the class name for name mangling */  | 
2261  | 0  |         Py_INCREF(s->v.ClassDef.name);  | 
2262  | 0  |         Py_XSETREF(c->u->u_private, s->v.ClassDef.name);  | 
2263  |  |         /* load (global) __name__ ... */  | 
2264  | 0  |         str = PyUnicode_InternFromString("__name__"); | 
2265  | 0  |         if (!str || !compiler_nameop(c, str, Load)) { | 
2266  | 0  |             Py_XDECREF(str);  | 
2267  | 0  |             compiler_exit_scope(c);  | 
2268  | 0  |             return 0;  | 
2269  | 0  |         }  | 
2270  | 0  |         Py_DECREF(str);  | 
2271  |  |         /* ... and store it as __module__ */  | 
2272  | 0  |         str = PyUnicode_InternFromString("__module__"); | 
2273  | 0  |         if (!str || !compiler_nameop(c, str, Store)) { | 
2274  | 0  |             Py_XDECREF(str);  | 
2275  | 0  |             compiler_exit_scope(c);  | 
2276  | 0  |             return 0;  | 
2277  | 0  |         }  | 
2278  | 0  |         Py_DECREF(str);  | 
2279  | 0  |         assert(c->u->u_qualname);  | 
2280  | 0  |         ADDOP_LOAD_CONST(c, c->u->u_qualname);  | 
2281  | 0  |         str = PyUnicode_InternFromString("__qualname__"); | 
2282  | 0  |         if (!str || !compiler_nameop(c, str, Store)) { | 
2283  | 0  |             Py_XDECREF(str);  | 
2284  | 0  |             compiler_exit_scope(c);  | 
2285  | 0  |             return 0;  | 
2286  | 0  |         }  | 
2287  | 0  |         Py_DECREF(str);  | 
2288  |  |         /* compile the body proper */  | 
2289  | 0  |         if (!compiler_body(c, s->v.ClassDef.body)) { | 
2290  | 0  |             compiler_exit_scope(c);  | 
2291  | 0  |             return 0;  | 
2292  | 0  |         }  | 
2293  |  |         /* Return __classcell__ if it is referenced, otherwise return None */  | 
2294  | 0  |         if (c->u->u_ste->ste_needs_class_closure) { | 
2295  |  |             /* Store __classcell__ into class namespace & return it */  | 
2296  | 0  |             str = PyUnicode_InternFromString("__class__"); | 
2297  | 0  |             if (str == NULL) { | 
2298  | 0  |                 compiler_exit_scope(c);  | 
2299  | 0  |                 return 0;  | 
2300  | 0  |             }  | 
2301  | 0  |             i = compiler_lookup_arg(c->u->u_cellvars, str);  | 
2302  | 0  |             Py_DECREF(str);  | 
2303  | 0  |             if (i < 0) { | 
2304  | 0  |                 compiler_exit_scope(c);  | 
2305  | 0  |                 return 0;  | 
2306  | 0  |             }  | 
2307  | 0  |             assert(i == 0);  | 
2308  |  | 
  | 
2309  | 0  |             ADDOP_I(c, LOAD_CLOSURE, i);  | 
2310  | 0  |             ADDOP(c, DUP_TOP);  | 
2311  | 0  |             str = PyUnicode_InternFromString("__classcell__"); | 
2312  | 0  |             if (!str || !compiler_nameop(c, str, Store)) { | 
2313  | 0  |                 Py_XDECREF(str);  | 
2314  | 0  |                 compiler_exit_scope(c);  | 
2315  | 0  |                 return 0;  | 
2316  | 0  |             }  | 
2317  | 0  |             Py_DECREF(str);  | 
2318  | 0  |         }  | 
2319  | 0  |         else { | 
2320  |  |             /* No methods referenced __class__, so just return None */  | 
2321  | 0  |             assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);  | 
2322  | 0  |             ADDOP_LOAD_CONST(c, Py_None);  | 
2323  | 0  |         }  | 
2324  | 0  |         ADDOP_IN_SCOPE(c, RETURN_VALUE);  | 
2325  |  |         /* create the code object */  | 
2326  | 0  |         co = assemble(c, 1);  | 
2327  | 0  |     }  | 
2328  |  |     /* leave the new scope */  | 
2329  | 0  |     compiler_exit_scope(c);  | 
2330  | 0  |     if (co == NULL)  | 
2331  | 0  |         return 0;  | 
2332  |  |  | 
2333  |  |     /* 2. load the 'build_class' function */  | 
2334  | 0  |     ADDOP(c, LOAD_BUILD_CLASS);  | 
2335  |  |  | 
2336  |  |     /* 3. load a function (or closure) made from the code object */  | 
2337  | 0  |     compiler_make_closure(c, co, 0, NULL);  | 
2338  | 0  |     Py_DECREF(co);  | 
2339  |  |  | 
2340  |  |     /* 4. load class name */  | 
2341  | 0  |     ADDOP_LOAD_CONST(c, s->v.ClassDef.name);  | 
2342  |  |  | 
2343  |  |     /* 5. generate the rest of the code for the call */  | 
2344  | 0  |     if (!compiler_call_helper(c, 2,  | 
2345  | 0  |                               s->v.ClassDef.bases,  | 
2346  | 0  |                               s->v.ClassDef.keywords))  | 
2347  | 0  |         return 0;  | 
2348  |  |  | 
2349  |  |     /* 6. apply decorators */  | 
2350  | 0  |     for (i = 0; i < asdl_seq_LEN(decos); i++) { | 
2351  | 0  |         ADDOP_I(c, CALL_FUNCTION, 1);  | 
2352  | 0  |     }  | 
2353  |  |  | 
2354  |  |     /* 7. store into <name> */  | 
2355  | 0  |     if (!compiler_nameop(c, s->v.ClassDef.name, Store))  | 
2356  | 0  |         return 0;  | 
2357  | 0  |     return 1;  | 
2358  | 0  | }  | 
2359  |  |  | 
2360  |  | /* Return 0 if the expression is a constant value except named singletons.  | 
2361  |  |    Return 1 otherwise. */  | 
2362  |  | static int  | 
2363  |  | check_is_arg(expr_ty e)  | 
2364  | 48  | { | 
2365  | 48  |     if (e->kind != Constant_kind) { | 
2366  | 38  |         return 1;  | 
2367  | 38  |     }  | 
2368  | 10  |     PyObject *value = e->v.Constant.value;  | 
2369  | 10  |     return (value == Py_None  | 
2370  | 10  |          || value == Py_False  | 
2371  | 10  |          || value == Py_True  | 
2372  | 10  |          || value == Py_Ellipsis);  | 
2373  | 48  | }  | 
2374  |  |  | 
2375  |  | /* Check operands of identity chacks ("is" and "is not"). | 
2376  |  |    Emit a warning if any operand is a constant except named singletons.  | 
2377  |  |    Return 0 on error.  | 
2378  |  |  */  | 
2379  |  | static int  | 
2380  |  | check_compare(struct compiler *c, expr_ty e)  | 
2381  | 24  | { | 
2382  | 24  |     Py_ssize_t i, n;  | 
2383  | 24  |     int left = check_is_arg(e->v.Compare.left);  | 
2384  | 24  |     n = asdl_seq_LEN(e->v.Compare.ops);  | 
2385  | 48  |     for (i = 0; i < n; i++) { | 
2386  | 24  |         cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);  | 
2387  | 24  |         int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));  | 
2388  | 24  |         if (op == Is || op == IsNot) { | 
2389  | 0  |             if (!right || !left) { | 
2390  | 0  |                 const char *msg = (op == Is)  | 
2391  | 0  |                         ? "\"is\" with a literal. Did you mean \"==\"?"  | 
2392  | 0  |                         : "\"is not\" with a literal. Did you mean \"!=\"?";  | 
2393  | 0  |                 return compiler_warn(c, msg);  | 
2394  | 0  |             }  | 
2395  | 0  |         }  | 
2396  | 24  |         left = right;  | 
2397  | 24  |     }  | 
2398  | 24  |     return 1;  | 
2399  | 24  | }  | 
2400  |  |  | 
2401  |  | static int  | 
2402  |  | cmpop(cmpop_ty op)  | 
2403  | 24  | { | 
2404  | 24  |     switch (op) { | 
2405  | 8  |     case Eq:  | 
2406  | 8  |         return PyCmp_EQ;  | 
2407  | 6  |     case NotEq:  | 
2408  | 6  |         return PyCmp_NE;  | 
2409  | 0  |     case Lt:  | 
2410  | 0  |         return PyCmp_LT;  | 
2411  | 0  |     case LtE:  | 
2412  | 0  |         return PyCmp_LE;  | 
2413  | 0  |     case Gt:  | 
2414  | 0  |         return PyCmp_GT;  | 
2415  | 0  |     case GtE:  | 
2416  | 0  |         return PyCmp_GE;  | 
2417  | 0  |     case Is:  | 
2418  | 0  |         return PyCmp_IS;  | 
2419  | 0  |     case IsNot:  | 
2420  | 0  |         return PyCmp_IS_NOT;  | 
2421  | 10  |     case In:  | 
2422  | 10  |         return PyCmp_IN;  | 
2423  | 0  |     case NotIn:  | 
2424  | 0  |         return PyCmp_NOT_IN;  | 
2425  | 0  |     default:  | 
2426  | 0  |         return PyCmp_BAD;  | 
2427  | 24  |     }  | 
2428  | 24  | }  | 
2429  |  |  | 
2430  |  | static int  | 
2431  |  | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)  | 
2432  | 14  | { | 
2433  | 14  |     switch (e->kind) { | 
2434  | 0  |     case UnaryOp_kind:  | 
2435  | 0  |         if (e->v.UnaryOp.op == Not)  | 
2436  | 0  |             return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);  | 
2437  |  |         /* fallback to general implementation */  | 
2438  | 0  |         break;  | 
2439  | 0  |     case BoolOp_kind: { | 
2440  | 0  |         asdl_seq *s = e->v.BoolOp.values;  | 
2441  | 0  |         Py_ssize_t i, n = asdl_seq_LEN(s) - 1;  | 
2442  | 0  |         assert(n >= 0);  | 
2443  | 0  |         int cond2 = e->v.BoolOp.op == Or;  | 
2444  | 0  |         basicblock *next2 = next;  | 
2445  | 0  |         if (!cond2 != !cond) { | 
2446  | 0  |             next2 = compiler_new_block(c);  | 
2447  | 0  |             if (next2 == NULL)  | 
2448  | 0  |                 return 0;  | 
2449  | 0  |         }  | 
2450  | 0  |         for (i = 0; i < n; ++i) { | 
2451  | 0  |             if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))  | 
2452  | 0  |                 return 0;  | 
2453  | 0  |         }  | 
2454  | 0  |         if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))  | 
2455  | 0  |             return 0;  | 
2456  | 0  |         if (next2 != next)  | 
2457  | 0  |             compiler_use_next_block(c, next2);  | 
2458  | 0  |         return 1;  | 
2459  | 0  |     }  | 
2460  | 0  |     case IfExp_kind: { | 
2461  | 0  |         basicblock *end, *next2;  | 
2462  | 0  |         end = compiler_new_block(c);  | 
2463  | 0  |         if (end == NULL)  | 
2464  | 0  |             return 0;  | 
2465  | 0  |         next2 = compiler_new_block(c);  | 
2466  | 0  |         if (next2 == NULL)  | 
2467  | 0  |             return 0;  | 
2468  | 0  |         if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))  | 
2469  | 0  |             return 0;  | 
2470  | 0  |         if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))  | 
2471  | 0  |             return 0;  | 
2472  | 0  |         ADDOP_JREL(c, JUMP_FORWARD, end);  | 
2473  | 0  |         compiler_use_next_block(c, next2);  | 
2474  | 0  |         if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))  | 
2475  | 0  |             return 0;  | 
2476  | 0  |         compiler_use_next_block(c, end);  | 
2477  | 0  |         return 1;  | 
2478  | 0  |     }  | 
2479  | 12  |     case Compare_kind: { | 
2480  | 12  |         Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;  | 
2481  | 12  |         if (n > 0) { | 
2482  | 0  |             if (!check_compare(c, e)) { | 
2483  | 0  |                 return 0;  | 
2484  | 0  |             }  | 
2485  | 0  |             basicblock *cleanup = compiler_new_block(c);  | 
2486  | 0  |             if (cleanup == NULL)  | 
2487  | 0  |                 return 0;  | 
2488  | 0  |             VISIT(c, expr, e->v.Compare.left);  | 
2489  | 0  |             for (i = 0; i < n; i++) { | 
2490  | 0  |                 VISIT(c, expr,  | 
2491  | 0  |                     (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));  | 
2492  | 0  |                 ADDOP(c, DUP_TOP);  | 
2493  | 0  |                 ADDOP(c, ROT_THREE);  | 
2494  | 0  |                 ADDOP_I(c, COMPARE_OP,  | 
2495  | 0  |                     cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));  | 
2496  | 0  |                 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);  | 
2497  | 0  |                 NEXT_BLOCK(c);  | 
2498  | 0  |             }  | 
2499  | 0  |             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));  | 
2500  | 0  |             ADDOP_I(c, COMPARE_OP,  | 
2501  | 0  |                    cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));  | 
2502  | 0  |             ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);  | 
2503  | 0  |             basicblock *end = compiler_new_block(c);  | 
2504  | 0  |             if (end == NULL)  | 
2505  | 0  |                 return 0;  | 
2506  | 0  |             ADDOP_JREL(c, JUMP_FORWARD, end);  | 
2507  | 0  |             compiler_use_next_block(c, cleanup);  | 
2508  | 0  |             ADDOP(c, POP_TOP);  | 
2509  | 0  |             if (!cond) { | 
2510  | 0  |                 ADDOP_JREL(c, JUMP_FORWARD, next);  | 
2511  | 0  |             }  | 
2512  | 0  |             compiler_use_next_block(c, end);  | 
2513  | 0  |             return 1;  | 
2514  | 0  |         }  | 
2515  |  |         /* fallback to general implementation */  | 
2516  | 12  |         break;  | 
2517  | 12  |     }  | 
2518  | 12  |     default:  | 
2519  |  |         /* fallback to general implementation */  | 
2520  | 2  |         break;  | 
2521  | 14  |     }  | 
2522  |  |  | 
2523  |  |     /* general implementation */  | 
2524  | 14  |     VISIT(c, expr, e);  | 
2525  | 14  |     ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);  | 
2526  | 14  |     return 1;  | 
2527  | 14  | }  | 
2528  |  |  | 
2529  |  | static int  | 
2530  |  | compiler_ifexp(struct compiler *c, expr_ty e)  | 
2531  | 0  | { | 
2532  | 0  |     basicblock *end, *next;  | 
2533  |  | 
  | 
2534  | 0  |     assert(e->kind == IfExp_kind);  | 
2535  | 0  |     end = compiler_new_block(c);  | 
2536  | 0  |     if (end == NULL)  | 
2537  | 0  |         return 0;  | 
2538  | 0  |     next = compiler_new_block(c);  | 
2539  | 0  |     if (next == NULL)  | 
2540  | 0  |         return 0;  | 
2541  | 0  |     if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))  | 
2542  | 0  |         return 0;  | 
2543  | 0  |     VISIT(c, expr, e->v.IfExp.body);  | 
2544  | 0  |     ADDOP_JREL(c, JUMP_FORWARD, end);  | 
2545  | 0  |     compiler_use_next_block(c, next);  | 
2546  | 0  |     VISIT(c, expr, e->v.IfExp.orelse);  | 
2547  | 0  |     compiler_use_next_block(c, end);  | 
2548  | 0  |     return 1;  | 
2549  | 0  | }  | 
2550  |  |  | 
2551  |  | static int  | 
2552  |  | compiler_lambda(struct compiler *c, expr_ty e)  | 
2553  | 2  | { | 
2554  | 2  |     PyCodeObject *co;  | 
2555  | 2  |     PyObject *qualname;  | 
2556  | 2  |     static identifier name;  | 
2557  | 2  |     Py_ssize_t funcflags;  | 
2558  | 2  |     arguments_ty args = e->v.Lambda.args;  | 
2559  | 2  |     assert(e->kind == Lambda_kind);  | 
2560  |  |  | 
2561  | 2  |     if (!name) { | 
2562  | 2  |         name = PyUnicode_InternFromString("<lambda>"); | 
2563  | 2  |         if (!name)  | 
2564  | 0  |             return 0;  | 
2565  | 2  |     }  | 
2566  |  |  | 
2567  | 2  |     funcflags = compiler_default_arguments(c, args);  | 
2568  | 2  |     if (funcflags == -1) { | 
2569  | 0  |         return 0;  | 
2570  | 0  |     }  | 
2571  |  |  | 
2572  | 2  |     if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,  | 
2573  | 2  |                               (void *)e, e->lineno))  | 
2574  | 0  |         return 0;  | 
2575  |  |  | 
2576  |  |     /* Make None the first constant, so the lambda can't have a  | 
2577  |  |        docstring. */  | 
2578  | 2  |     if (compiler_add_const(c, Py_None) < 0)  | 
2579  | 0  |         return 0;  | 
2580  |  |  | 
2581  | 2  |     c->u->u_argcount = asdl_seq_LEN(args->args);  | 
2582  | 2  |     c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);  | 
2583  | 2  |     c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);  | 
2584  | 2  |     VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);  | 
2585  | 2  |     if (c->u->u_ste->ste_generator) { | 
2586  | 0  |         co = assemble(c, 0);  | 
2587  | 0  |     }  | 
2588  | 2  |     else { | 
2589  | 2  |         ADDOP_IN_SCOPE(c, RETURN_VALUE);  | 
2590  | 2  |         co = assemble(c, 1);  | 
2591  | 2  |     }  | 
2592  | 2  |     qualname = c->u->u_qualname;  | 
2593  | 2  |     Py_INCREF(qualname);  | 
2594  | 2  |     compiler_exit_scope(c);  | 
2595  | 2  |     if (co == NULL)  | 
2596  | 0  |         return 0;  | 
2597  |  |  | 
2598  | 2  |     compiler_make_closure(c, co, funcflags, qualname);  | 
2599  | 2  |     Py_DECREF(qualname);  | 
2600  | 2  |     Py_DECREF(co);  | 
2601  |  |  | 
2602  | 2  |     return 1;  | 
2603  | 2  | }  | 
2604  |  |  | 
2605  |  | static int  | 
2606  |  | compiler_if(struct compiler *c, stmt_ty s)  | 
2607  | 8  | { | 
2608  | 8  |     basicblock *end, *next;  | 
2609  | 8  |     int constant;  | 
2610  | 8  |     assert(s->kind == If_kind);  | 
2611  | 8  |     end = compiler_new_block(c);  | 
2612  | 8  |     if (end == NULL)  | 
2613  | 0  |         return 0;  | 
2614  |  |  | 
2615  | 8  |     constant = expr_constant(s->v.If.test);  | 
2616  |  |     /* constant = 0: "if 0"  | 
2617  |  |      * constant = 1: "if 1", "if 2", ...  | 
2618  |  |      * constant = -1: rest */  | 
2619  | 8  |     if (constant == 0) { | 
2620  | 0  |         BEGIN_DO_NOT_EMIT_BYTECODE  | 
2621  | 0  |         VISIT_SEQ(c, stmt, s->v.If.body);  | 
2622  | 0  |         END_DO_NOT_EMIT_BYTECODE  | 
2623  | 0  |         if (s->v.If.orelse) { | 
2624  | 0  |             VISIT_SEQ(c, stmt, s->v.If.orelse);  | 
2625  | 0  |         }  | 
2626  | 8  |     } else if (constant == 1) { | 
2627  | 0  |         VISIT_SEQ(c, stmt, s->v.If.body);  | 
2628  | 0  |         if (s->v.If.orelse) { | 
2629  | 0  |             BEGIN_DO_NOT_EMIT_BYTECODE  | 
2630  | 0  |             VISIT_SEQ(c, stmt, s->v.If.orelse);  | 
2631  | 0  |             END_DO_NOT_EMIT_BYTECODE  | 
2632  | 0  |         }  | 
2633  | 8  |     } else { | 
2634  | 8  |         if (asdl_seq_LEN(s->v.If.orelse)) { | 
2635  | 4  |             next = compiler_new_block(c);  | 
2636  | 4  |             if (next == NULL)  | 
2637  | 0  |                 return 0;  | 
2638  | 4  |         }  | 
2639  | 4  |         else  | 
2640  | 4  |             next = end;  | 
2641  | 8  |         if (!compiler_jump_if(c, s->v.If.test, next, 0))  | 
2642  | 0  |             return 0;  | 
2643  | 8  |         VISIT_SEQ(c, stmt, s->v.If.body);  | 
2644  | 8  |         if (asdl_seq_LEN(s->v.If.orelse)) { | 
2645  | 4  |             ADDOP_JREL(c, JUMP_FORWARD, end);  | 
2646  | 4  |             compiler_use_next_block(c, next);  | 
2647  | 4  |             VISIT_SEQ(c, stmt, s->v.If.orelse);  | 
2648  | 4  |         }  | 
2649  | 8  |     }  | 
2650  | 8  |     compiler_use_next_block(c, end);  | 
2651  | 8  |     return 1;  | 
2652  | 8  | }  | 
2653  |  |  | 
2654  |  | static int  | 
2655  |  | compiler_for(struct compiler *c, stmt_ty s)  | 
2656  | 8  | { | 
2657  | 8  |     basicblock *start, *cleanup, *end;  | 
2658  |  |  | 
2659  | 8  |     start = compiler_new_block(c);  | 
2660  | 8  |     cleanup = compiler_new_block(c);  | 
2661  | 8  |     end = compiler_new_block(c);  | 
2662  | 8  |     if (start == NULL || end == NULL || cleanup == NULL)  | 
2663  | 0  |         return 0;  | 
2664  |  |  | 
2665  | 8  |     if (!compiler_push_fblock(c, FOR_LOOP, start, end))  | 
2666  | 0  |         return 0;  | 
2667  |  |  | 
2668  | 8  |     VISIT(c, expr, s->v.For.iter);  | 
2669  | 8  |     ADDOP(c, GET_ITER);  | 
2670  | 8  |     compiler_use_next_block(c, start);  | 
2671  | 8  |     ADDOP_JREL(c, FOR_ITER, cleanup);  | 
2672  | 8  |     VISIT(c, expr, s->v.For.target);  | 
2673  | 8  |     VISIT_SEQ(c, stmt, s->v.For.body);  | 
2674  | 8  |     ADDOP_JABS(c, JUMP_ABSOLUTE, start);  | 
2675  | 8  |     compiler_use_next_block(c, cleanup);  | 
2676  |  |  | 
2677  | 8  |     compiler_pop_fblock(c, FOR_LOOP, start);  | 
2678  |  |  | 
2679  | 8  |     VISIT_SEQ(c, stmt, s->v.For.orelse);  | 
2680  | 8  |     compiler_use_next_block(c, end);  | 
2681  | 8  |     return 1;  | 
2682  | 8  | }  | 
2683  |  |  | 
2684  |  |  | 
2685  |  | static int  | 
2686  |  | compiler_async_for(struct compiler *c, stmt_ty s)  | 
2687  | 0  | { | 
2688  | 0  |     basicblock *start, *except, *end;  | 
2689  | 0  |     if (IS_TOP_LEVEL_AWAIT(c)){ | 
2690  | 0  |         c->u->u_ste->ste_coroutine = 1;  | 
2691  | 0  |     } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { | 
2692  | 0  |         return compiler_error(c, "'async for' outside async function");  | 
2693  | 0  |     }  | 
2694  |  |  | 
2695  | 0  |     start = compiler_new_block(c);  | 
2696  | 0  |     except = compiler_new_block(c);  | 
2697  | 0  |     end = compiler_new_block(c);  | 
2698  |  | 
  | 
2699  | 0  |     if (start == NULL || except == NULL || end == NULL)  | 
2700  | 0  |         return 0;  | 
2701  |  |  | 
2702  | 0  |     VISIT(c, expr, s->v.AsyncFor.iter);  | 
2703  | 0  |     ADDOP(c, GET_AITER);  | 
2704  |  | 
  | 
2705  | 0  |     compiler_use_next_block(c, start);  | 
2706  | 0  |     if (!compiler_push_fblock(c, FOR_LOOP, start, end))  | 
2707  | 0  |         return 0;  | 
2708  |  |  | 
2709  |  |     /* SETUP_FINALLY to guard the __anext__ call */  | 
2710  | 0  |     ADDOP_JREL(c, SETUP_FINALLY, except);  | 
2711  | 0  |     ADDOP(c, GET_ANEXT);  | 
2712  | 0  |     ADDOP_LOAD_CONST(c, Py_None);  | 
2713  | 0  |     ADDOP(c, YIELD_FROM);  | 
2714  | 0  |     ADDOP(c, POP_BLOCK);  /* for SETUP_FINALLY */  | 
2715  |  |  | 
2716  |  |     /* Success block for __anext__ */  | 
2717  | 0  |     VISIT(c, expr, s->v.AsyncFor.target);  | 
2718  | 0  |     VISIT_SEQ(c, stmt, s->v.AsyncFor.body);  | 
2719  | 0  |     ADDOP_JABS(c, JUMP_ABSOLUTE, start);  | 
2720  |  | 
  | 
2721  | 0  |     compiler_pop_fblock(c, FOR_LOOP, start);  | 
2722  |  |  | 
2723  |  |     /* Except block for __anext__ */  | 
2724  | 0  |     compiler_use_next_block(c, except);  | 
2725  | 0  |     ADDOP(c, END_ASYNC_FOR);  | 
2726  |  |  | 
2727  |  |     /* `else` block */  | 
2728  | 0  |     VISIT_SEQ(c, stmt, s->v.For.orelse);  | 
2729  |  | 
  | 
2730  | 0  |     compiler_use_next_block(c, end);  | 
2731  |  | 
  | 
2732  | 0  |     return 1;  | 
2733  | 0  | }  | 
2734  |  |  | 
2735  |  | static int  | 
2736  |  | compiler_while(struct compiler *c, stmt_ty s)  | 
2737  | 0  | { | 
2738  | 0  |     basicblock *loop, *orelse, *end, *anchor = NULL;  | 
2739  | 0  |     int constant = expr_constant(s->v.While.test);  | 
2740  |  | 
  | 
2741  | 0  |     if (constant == 0) { | 
2742  | 0  |         BEGIN_DO_NOT_EMIT_BYTECODE  | 
2743  |  |         // Push a dummy block so the VISIT_SEQ knows that we are  | 
2744  |  |         // inside a while loop so it can correctly evaluate syntax  | 
2745  |  |         // errors.  | 
2746  | 0  |         if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL)) { | 
2747  | 0  |             return 0;  | 
2748  | 0  |         }  | 
2749  | 0  |         VISIT_SEQ(c, stmt, s->v.While.body);  | 
2750  |  |         // Remove the dummy block now that is not needed.  | 
2751  | 0  |         compiler_pop_fblock(c, WHILE_LOOP, NULL);  | 
2752  | 0  |         END_DO_NOT_EMIT_BYTECODE  | 
2753  | 0  |         if (s->v.While.orelse) { | 
2754  | 0  |             VISIT_SEQ(c, stmt, s->v.While.orelse);  | 
2755  | 0  |         }  | 
2756  | 0  |         return 1;  | 
2757  | 0  |     }  | 
2758  | 0  |     loop = compiler_new_block(c);  | 
2759  | 0  |     end = compiler_new_block(c);  | 
2760  | 0  |     if (constant == -1) { | 
2761  | 0  |         anchor = compiler_new_block(c);  | 
2762  | 0  |         if (anchor == NULL)  | 
2763  | 0  |             return 0;  | 
2764  | 0  |     }  | 
2765  | 0  |     if (loop == NULL || end == NULL)  | 
2766  | 0  |         return 0;  | 
2767  | 0  |     if (s->v.While.orelse) { | 
2768  | 0  |         orelse = compiler_new_block(c);  | 
2769  | 0  |         if (orelse == NULL)  | 
2770  | 0  |             return 0;  | 
2771  | 0  |     }  | 
2772  | 0  |     else  | 
2773  | 0  |         orelse = NULL;  | 
2774  |  |  | 
2775  | 0  |     compiler_use_next_block(c, loop);  | 
2776  | 0  |     if (!compiler_push_fblock(c, WHILE_LOOP, loop, end))  | 
2777  | 0  |         return 0;  | 
2778  | 0  |     if (constant == -1) { | 
2779  | 0  |         if (!compiler_jump_if(c, s->v.While.test, anchor, 0))  | 
2780  | 0  |             return 0;  | 
2781  | 0  |     }  | 
2782  | 0  |     VISIT_SEQ(c, stmt, s->v.While.body);  | 
2783  | 0  |     ADDOP_JABS(c, JUMP_ABSOLUTE, loop);  | 
2784  |  |  | 
2785  |  |     /* XXX should the two POP instructions be in a separate block  | 
2786  |  |        if there is no else clause ?  | 
2787  |  |     */  | 
2788  |  | 
  | 
2789  | 0  |     if (constant == -1)  | 
2790  | 0  |         compiler_use_next_block(c, anchor);  | 
2791  | 0  |     compiler_pop_fblock(c, WHILE_LOOP, loop);  | 
2792  |  | 
  | 
2793  | 0  |     if (orelse != NULL) /* what if orelse is just pass? */  | 
2794  | 0  |         VISIT_SEQ(c, stmt, s->v.While.orelse);  | 
2795  | 0  |     compiler_use_next_block(c, end);  | 
2796  |  | 
  | 
2797  | 0  |     return 1;  | 
2798  | 0  | }  | 
2799  |  |  | 
2800  |  | static int  | 
2801  |  | compiler_return(struct compiler *c, stmt_ty s)  | 
2802  | 6  | { | 
2803  | 6  |     int preserve_tos = ((s->v.Return.value != NULL) &&  | 
2804  | 6  |                         (s->v.Return.value->kind != Constant_kind));  | 
2805  | 6  |     if (c->u->u_ste->ste_type != FunctionBlock)  | 
2806  | 0  |         return compiler_error(c, "'return' outside function");  | 
2807  | 6  |     if (s->v.Return.value != NULL &&  | 
2808  | 6  |         c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)  | 
2809  | 0  |     { | 
2810  | 0  |             return compiler_error(  | 
2811  | 0  |                 c, "'return' with value in async generator");  | 
2812  | 0  |     }  | 
2813  | 6  |     if (preserve_tos) { | 
2814  | 6  |         VISIT(c, expr, s->v.Return.value);  | 
2815  | 6  |     }  | 
2816  | 10  |     for (int depth = c->u->u_nfblocks; depth--;) { | 
2817  | 4  |         struct fblockinfo *info = &c->u->u_fblock[depth];  | 
2818  |  |  | 
2819  | 4  |         if (!compiler_unwind_fblock(c, info, preserve_tos))  | 
2820  | 0  |             return 0;  | 
2821  | 4  |     }  | 
2822  | 6  |     if (s->v.Return.value == NULL) { | 
2823  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
2824  | 0  |     }  | 
2825  | 6  |     else if (!preserve_tos) { | 
2826  | 0  |         VISIT(c, expr, s->v.Return.value);  | 
2827  | 0  |     }  | 
2828  | 6  |     ADDOP(c, RETURN_VALUE);  | 
2829  |  |  | 
2830  | 6  |     return 1;  | 
2831  | 6  | }  | 
2832  |  |  | 
2833  |  | static int  | 
2834  |  | compiler_break(struct compiler *c)  | 
2835  | 0  | { | 
2836  | 0  |     for (int depth = c->u->u_nfblocks; depth--;) { | 
2837  | 0  |         struct fblockinfo *info = &c->u->u_fblock[depth];  | 
2838  |  | 
  | 
2839  | 0  |         if (!compiler_unwind_fblock(c, info, 0))  | 
2840  | 0  |             return 0;  | 
2841  | 0  |         if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { | 
2842  | 0  |             ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit);  | 
2843  | 0  |             return 1;  | 
2844  | 0  |         }  | 
2845  | 0  |     }  | 
2846  | 0  |     return compiler_error(c, "'break' outside loop");  | 
2847  | 0  | }  | 
2848  |  |  | 
2849  |  | static int  | 
2850  |  | compiler_continue(struct compiler *c)  | 
2851  | 0  | { | 
2852  | 0  |     for (int depth = c->u->u_nfblocks; depth--;) { | 
2853  | 0  |         struct fblockinfo *info = &c->u->u_fblock[depth];  | 
2854  |  | 
  | 
2855  | 0  |         if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { | 
2856  | 0  |             ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block);  | 
2857  | 0  |             return 1;  | 
2858  | 0  |         }  | 
2859  | 0  |         if (!compiler_unwind_fblock(c, info, 0))  | 
2860  | 0  |             return 0;  | 
2861  | 0  |     }  | 
2862  | 0  |     return compiler_error(c, "'continue' not properly in loop");  | 
2863  | 0  | }  | 
2864  |  |  | 
2865  |  |  | 
2866  |  | /* Code generated for "try: <body> finally: <finalbody>" is as follows:  | 
2867  |  |  | 
2868  |  |         SETUP_FINALLY           L  | 
2869  |  |         <code for body>  | 
2870  |  |         POP_BLOCK  | 
2871  |  |         BEGIN_FINALLY  | 
2872  |  |     L:  | 
2873  |  |         <code for finalbody>  | 
2874  |  |         END_FINALLY  | 
2875  |  |  | 
2876  |  |    The special instructions use the block stack.  Each block  | 
2877  |  |    stack entry contains the instruction that created it (here  | 
2878  |  |    SETUP_FINALLY), the level of the value stack at the time the  | 
2879  |  |    block stack entry was created, and a label (here L).  | 
2880  |  |  | 
2881  |  |    SETUP_FINALLY:  | 
2882  |  |     Pushes the current value stack level and the label  | 
2883  |  |     onto the block stack.  | 
2884  |  |    POP_BLOCK:  | 
2885  |  |     Pops en entry from the block stack.  | 
2886  |  |    BEGIN_FINALLY  | 
2887  |  |     Pushes NULL onto the value stack.  | 
2888  |  |    END_FINALLY:  | 
2889  |  |     Pops 1 (NULL or int) or 6 entries from the *value* stack and restore  | 
2890  |  |     the raised and the caught exceptions they specify.  | 
2891  |  |  | 
2892  |  |    The block stack is unwound when an exception is raised:  | 
2893  |  |    when a SETUP_FINALLY entry is found, the raised and the caught  | 
2894  |  |    exceptions are pushed onto the value stack (and the exception  | 
2895  |  |    condition is cleared), and the interpreter jumps to the label  | 
2896  |  |    gotten from the block stack.  | 
2897  |  | */  | 
2898  |  |  | 
2899  |  | static int  | 
2900  |  | compiler_try_finally(struct compiler *c, stmt_ty s)  | 
2901  | 0  | { | 
2902  | 0  |     basicblock *start, *newcurblock, *body, *end;  | 
2903  | 0  |     int break_finally = 1;  | 
2904  |  | 
  | 
2905  | 0  |     body = compiler_new_block(c);  | 
2906  | 0  |     end = compiler_new_block(c);  | 
2907  | 0  |     if (body == NULL || end == NULL)  | 
2908  | 0  |         return 0;  | 
2909  |  |  | 
2910  | 0  |     start = c->u->u_curblock;  | 
2911  |  |  | 
2912  |  |     /* `finally` block. Compile it first to determine if any of "break",  | 
2913  |  |        "continue" or "return" are used in it. */  | 
2914  | 0  |     compiler_use_next_block(c, end);  | 
2915  | 0  |     if (!compiler_push_fblock(c, FINALLY_END, end, end))  | 
2916  | 0  |         return 0;  | 
2917  | 0  |     VISIT_SEQ(c, stmt, s->v.Try.finalbody);  | 
2918  | 0  |     ADDOP(c, END_FINALLY);  | 
2919  | 0  |     break_finally = (c->u->u_fblock[c->u->u_nfblocks - 1].fb_exit == NULL);  | 
2920  | 0  |     if (break_finally) { | 
2921  |  |         /* Pops a placeholder. See below */  | 
2922  | 0  |         ADDOP(c, POP_TOP);  | 
2923  | 0  |     }  | 
2924  | 0  |     compiler_pop_fblock(c, FINALLY_END, end);  | 
2925  |  | 
  | 
2926  | 0  |     newcurblock = c->u->u_curblock;  | 
2927  | 0  |     c->u->u_curblock = start;  | 
2928  | 0  |     start->b_next = NULL;  | 
2929  |  |  | 
2930  |  |     /* `try` block */  | 
2931  | 0  |     c->u->u_lineno_set = 0;  | 
2932  | 0  |     c->u->u_lineno = s->lineno;  | 
2933  | 0  |     c->u->u_col_offset = s->col_offset;  | 
2934  | 0  |     if (break_finally) { | 
2935  |  |         /* Pushes a placeholder for the value of "return" in the "try" block  | 
2936  |  |            to balance the stack for "break", "continue" and "return" in  | 
2937  |  |            the "finally" block. */  | 
2938  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
2939  | 0  |     }  | 
2940  | 0  |     ADDOP_JREL(c, SETUP_FINALLY, end);  | 
2941  | 0  |     compiler_use_next_block(c, body);  | 
2942  | 0  |     if (!compiler_push_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body, end))  | 
2943  | 0  |         return 0;  | 
2944  | 0  |     if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { | 
2945  | 0  |         if (!compiler_try_except(c, s))  | 
2946  | 0  |             return 0;  | 
2947  | 0  |     }  | 
2948  | 0  |     else { | 
2949  | 0  |         VISIT_SEQ(c, stmt, s->v.Try.body);  | 
2950  | 0  |     }  | 
2951  | 0  |     ADDOP(c, POP_BLOCK);  | 
2952  | 0  |     ADDOP(c, BEGIN_FINALLY);  | 
2953  | 0  |     compiler_pop_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body);  | 
2954  |  | 
  | 
2955  | 0  |     c->u->u_curblock->b_next = end;  | 
2956  | 0  |     c->u->u_curblock = newcurblock;  | 
2957  |  | 
  | 
2958  | 0  |     return 1;  | 
2959  | 0  | }  | 
2960  |  |  | 
2961  |  | /*  | 
2962  |  |    Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":  | 
2963  |  |    (The contents of the value stack is shown in [], with the top  | 
2964  |  |    at the right; 'tb' is trace-back info, 'val' the exception's  | 
2965  |  |    associated value, and 'exc' the exception.)  | 
2966  |  |  | 
2967  |  |    Value stack          Label   Instruction     Argument  | 
2968  |  |    []                           SETUP_FINALLY   L1  | 
2969  |  |    []                           <code for S>  | 
2970  |  |    []                           POP_BLOCK  | 
2971  |  |    []                           JUMP_FORWARD    L0  | 
2972  |  |  | 
2973  |  |    [tb, val, exc]       L1:     DUP                             )  | 
2974  |  |    [tb, val, exc, exc]          <evaluate E1>                   )  | 
2975  |  |    [tb, val, exc, exc, E1]      COMPARE_OP      EXC_MATCH       ) only if E1  | 
2976  |  |    [tb, val, exc, 1-or-0]       POP_JUMP_IF_FALSE       L2      )  | 
2977  |  |    [tb, val, exc]               POP  | 
2978  |  |    [tb, val]                    <assign to V1>  (or POP if no V1)  | 
2979  |  |    [tb]                         POP  | 
2980  |  |    []                           <code for S1>  | 
2981  |  |                                 JUMP_FORWARD    L0  | 
2982  |  |  | 
2983  |  |    [tb, val, exc]       L2:     DUP  | 
2984  |  |    .............................etc.......................  | 
2985  |  |  | 
2986  |  |    [tb, val, exc]       Ln+1:   END_FINALLY     # re-raise exception  | 
2987  |  |  | 
2988  |  |    []                   L0:     <next statement>  | 
2989  |  |  | 
2990  |  |    Of course, parts are not generated if Vi or Ei is not present.  | 
2991  |  | */  | 
2992  |  | static int  | 
2993  |  | compiler_try_except(struct compiler *c, stmt_ty s)  | 
2994  | 18  | { | 
2995  | 18  |     basicblock *body, *orelse, *except, *end;  | 
2996  | 18  |     Py_ssize_t i, n;  | 
2997  |  |  | 
2998  | 18  |     body = compiler_new_block(c);  | 
2999  | 18  |     except = compiler_new_block(c);  | 
3000  | 18  |     orelse = compiler_new_block(c);  | 
3001  | 18  |     end = compiler_new_block(c);  | 
3002  | 18  |     if (body == NULL || except == NULL || orelse == NULL || end == NULL)  | 
3003  | 0  |         return 0;  | 
3004  | 18  |     ADDOP_JREL(c, SETUP_FINALLY, except);  | 
3005  | 18  |     compiler_use_next_block(c, body);  | 
3006  | 18  |     if (!compiler_push_fblock(c, EXCEPT, body, NULL))  | 
3007  | 0  |         return 0;  | 
3008  | 18  |     VISIT_SEQ(c, stmt, s->v.Try.body);  | 
3009  | 18  |     ADDOP(c, POP_BLOCK);  | 
3010  | 18  |     compiler_pop_fblock(c, EXCEPT, body);  | 
3011  | 18  |     ADDOP_JREL(c, JUMP_FORWARD, orelse);  | 
3012  | 18  |     n = asdl_seq_LEN(s->v.Try.handlers);  | 
3013  | 18  |     compiler_use_next_block(c, except);  | 
3014  | 38  |     for (i = 0; i < n; i++) { | 
3015  | 20  |         excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(  | 
3016  | 20  |             s->v.Try.handlers, i);  | 
3017  | 20  |         if (!handler->v.ExceptHandler.type && i < n-1)  | 
3018  | 0  |             return compiler_error(c, "default 'except:' must be last");  | 
3019  | 20  |         c->u->u_lineno_set = 0;  | 
3020  | 20  |         c->u->u_lineno = handler->lineno;  | 
3021  | 20  |         c->u->u_col_offset = handler->col_offset;  | 
3022  | 20  |         except = compiler_new_block(c);  | 
3023  | 20  |         if (except == NULL)  | 
3024  | 0  |             return 0;  | 
3025  | 20  |         if (handler->v.ExceptHandler.type) { | 
3026  | 20  |             ADDOP(c, DUP_TOP);  | 
3027  | 20  |             VISIT(c, expr, handler->v.ExceptHandler.type);  | 
3028  | 20  |             ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH);  | 
3029  | 20  |             ADDOP_JABS(c, POP_JUMP_IF_FALSE, except);  | 
3030  | 20  |         }  | 
3031  | 20  |         ADDOP(c, POP_TOP);  | 
3032  | 20  |         if (handler->v.ExceptHandler.name) { | 
3033  | 14  |             basicblock *cleanup_end, *cleanup_body;  | 
3034  |  |  | 
3035  | 14  |             cleanup_end = compiler_new_block(c);  | 
3036  | 14  |             cleanup_body = compiler_new_block(c);  | 
3037  | 14  |             if (cleanup_end == NULL || cleanup_body == NULL) { | 
3038  | 0  |                 return 0;  | 
3039  | 0  |             }  | 
3040  |  |  | 
3041  | 14  |             compiler_nameop(c, handler->v.ExceptHandler.name, Store);  | 
3042  | 14  |             ADDOP(c, POP_TOP);  | 
3043  |  |  | 
3044  |  |             /*  | 
3045  |  |               try:  | 
3046  |  |                   # body  | 
3047  |  |               except type as name:  | 
3048  |  |                   try:  | 
3049  |  |                       # body  | 
3050  |  |                   finally:  | 
3051  |  |                       name = None # in case body contains "del name"  | 
3052  |  |                       del name  | 
3053  |  |             */  | 
3054  |  |  | 
3055  |  |             /* second try: */  | 
3056  | 14  |             ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);  | 
3057  | 14  |             compiler_use_next_block(c, cleanup_body);  | 
3058  | 14  |             if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end))  | 
3059  | 0  |                 return 0;  | 
3060  |  |  | 
3061  |  |             /* second # body */  | 
3062  | 14  |             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);  | 
3063  | 14  |             ADDOP(c, POP_BLOCK);  | 
3064  | 14  |             ADDOP(c, BEGIN_FINALLY);  | 
3065  | 14  |             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);  | 
3066  |  |  | 
3067  |  |             /* finally: */  | 
3068  | 14  |             compiler_use_next_block(c, cleanup_end);  | 
3069  | 14  |             if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL))  | 
3070  | 0  |                 return 0;  | 
3071  |  |  | 
3072  |  |             /* name = None; del name */  | 
3073  | 14  |             ADDOP_LOAD_CONST(c, Py_None);  | 
3074  | 14  |             compiler_nameop(c, handler->v.ExceptHandler.name, Store);  | 
3075  | 14  |             compiler_nameop(c, handler->v.ExceptHandler.name, Del);  | 
3076  |  |  | 
3077  | 14  |             ADDOP(c, END_FINALLY);  | 
3078  | 14  |             ADDOP(c, POP_EXCEPT);  | 
3079  | 14  |             compiler_pop_fblock(c, FINALLY_END, cleanup_end);  | 
3080  | 14  |         }  | 
3081  | 6  |         else { | 
3082  | 6  |             basicblock *cleanup_body;  | 
3083  |  |  | 
3084  | 6  |             cleanup_body = compiler_new_block(c);  | 
3085  | 6  |             if (!cleanup_body)  | 
3086  | 0  |                 return 0;  | 
3087  |  |  | 
3088  | 6  |             ADDOP(c, POP_TOP);  | 
3089  | 6  |             ADDOP(c, POP_TOP);  | 
3090  | 6  |             compiler_use_next_block(c, cleanup_body);  | 
3091  | 6  |             if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL))  | 
3092  | 0  |                 return 0;  | 
3093  | 6  |             VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);  | 
3094  | 6  |             ADDOP(c, POP_EXCEPT);  | 
3095  | 6  |             compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);  | 
3096  | 6  |         }  | 
3097  | 20  |         ADDOP_JREL(c, JUMP_FORWARD, end);  | 
3098  | 20  |         compiler_use_next_block(c, except);  | 
3099  | 20  |     }  | 
3100  | 18  |     ADDOP(c, END_FINALLY);  | 
3101  | 18  |     compiler_use_next_block(c, orelse);  | 
3102  | 18  |     VISIT_SEQ(c, stmt, s->v.Try.orelse);  | 
3103  | 18  |     compiler_use_next_block(c, end);  | 
3104  | 18  |     return 1;  | 
3105  | 18  | }  | 
3106  |  |  | 
3107  |  | static int  | 
3108  | 18  | compiler_try(struct compiler *c, stmt_ty s) { | 
3109  | 18  |     if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))  | 
3110  | 0  |         return compiler_try_finally(c, s);  | 
3111  | 18  |     else  | 
3112  | 18  |         return compiler_try_except(c, s);  | 
3113  | 18  | }  | 
3114  |  |  | 
3115  |  |  | 
3116  |  | static int  | 
3117  |  | compiler_import_as(struct compiler *c, identifier name, identifier asname)  | 
3118  | 0  | { | 
3119  |  |     /* The IMPORT_NAME opcode was already generated.  This function  | 
3120  |  |        merely needs to bind the result to a name.  | 
3121  |  |  | 
3122  |  |        If there is a dot in name, we need to split it and emit a  | 
3123  |  |        IMPORT_FROM for each name.  | 
3124  |  |     */  | 
3125  | 0  |     Py_ssize_t len = PyUnicode_GET_LENGTH(name);  | 
3126  | 0  |     Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);  | 
3127  | 0  |     if (dot == -2)  | 
3128  | 0  |         return 0;  | 
3129  | 0  |     if (dot != -1) { | 
3130  |  |         /* Consume the base module name to get the first attribute */  | 
3131  | 0  |         while (1) { | 
3132  | 0  |             Py_ssize_t pos = dot + 1;  | 
3133  | 0  |             PyObject *attr;  | 
3134  | 0  |             dot = PyUnicode_FindChar(name, '.', pos, len, 1);  | 
3135  | 0  |             if (dot == -2)  | 
3136  | 0  |                 return 0;  | 
3137  | 0  |             attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);  | 
3138  | 0  |             if (!attr)  | 
3139  | 0  |                 return 0;  | 
3140  | 0  |             ADDOP_N(c, IMPORT_FROM, attr, names);  | 
3141  | 0  |             if (dot == -1) { | 
3142  | 0  |                 break;  | 
3143  | 0  |             }  | 
3144  | 0  |             ADDOP(c, ROT_TWO);  | 
3145  | 0  |             ADDOP(c, POP_TOP);  | 
3146  | 0  |         }  | 
3147  | 0  |         if (!compiler_nameop(c, asname, Store)) { | 
3148  | 0  |             return 0;  | 
3149  | 0  |         }  | 
3150  | 0  |         ADDOP(c, POP_TOP);  | 
3151  | 0  |         return 1;  | 
3152  | 0  |     }  | 
3153  | 0  |     return compiler_nameop(c, asname, Store);  | 
3154  | 0  | }  | 
3155  |  |  | 
3156  |  | static int  | 
3157  |  | compiler_import(struct compiler *c, stmt_ty s)  | 
3158  | 0  | { | 
3159  |  |     /* The Import node stores a module name like a.b.c as a single  | 
3160  |  |        string.  This is convenient for all cases except  | 
3161  |  |          import a.b.c as d  | 
3162  |  |        where we need to parse that string to extract the individual  | 
3163  |  |        module names.  | 
3164  |  |        XXX Perhaps change the representation to make this case simpler?  | 
3165  |  |      */  | 
3166  | 0  |     Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);  | 
3167  |  | 
  | 
3168  | 0  |     for (i = 0; i < n; i++) { | 
3169  | 0  |         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);  | 
3170  | 0  |         int r;  | 
3171  |  | 
  | 
3172  | 0  |         ADDOP_LOAD_CONST(c, _PyLong_Zero);  | 
3173  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
3174  | 0  |         ADDOP_NAME(c, IMPORT_NAME, alias->name, names);  | 
3175  |  | 
  | 
3176  | 0  |         if (alias->asname) { | 
3177  | 0  |             r = compiler_import_as(c, alias->name, alias->asname);  | 
3178  | 0  |             if (!r)  | 
3179  | 0  |                 return r;  | 
3180  | 0  |         }  | 
3181  | 0  |         else { | 
3182  | 0  |             identifier tmp = alias->name;  | 
3183  | 0  |             Py_ssize_t dot = PyUnicode_FindChar(  | 
3184  | 0  |                 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);  | 
3185  | 0  |             if (dot != -1) { | 
3186  | 0  |                 tmp = PyUnicode_Substring(alias->name, 0, dot);  | 
3187  | 0  |                 if (tmp == NULL)  | 
3188  | 0  |                     return 0;  | 
3189  | 0  |             }  | 
3190  | 0  |             r = compiler_nameop(c, tmp, Store);  | 
3191  | 0  |             if (dot != -1) { | 
3192  | 0  |                 Py_DECREF(tmp);  | 
3193  | 0  |             }  | 
3194  | 0  |             if (!r)  | 
3195  | 0  |                 return r;  | 
3196  | 0  |         }  | 
3197  | 0  |     }  | 
3198  | 0  |     return 1;  | 
3199  | 0  | }  | 
3200  |  |  | 
3201  |  | static int  | 
3202  |  | compiler_from_import(struct compiler *c, stmt_ty s)  | 
3203  | 0  | { | 
3204  | 0  |     Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);  | 
3205  | 0  |     PyObject *names;  | 
3206  | 0  |     static PyObject *empty_string;  | 
3207  |  | 
  | 
3208  | 0  |     if (!empty_string) { | 
3209  | 0  |         empty_string = PyUnicode_FromString(""); | 
3210  | 0  |         if (!empty_string)  | 
3211  | 0  |             return 0;  | 
3212  | 0  |     }  | 
3213  |  |  | 
3214  | 0  |     ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));  | 
3215  |  | 
  | 
3216  | 0  |     names = PyTuple_New(n);  | 
3217  | 0  |     if (!names)  | 
3218  | 0  |         return 0;  | 
3219  |  |  | 
3220  |  |     /* build up the names */  | 
3221  | 0  |     for (i = 0; i < n; i++) { | 
3222  | 0  |         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);  | 
3223  | 0  |         Py_INCREF(alias->name);  | 
3224  | 0  |         PyTuple_SET_ITEM(names, i, alias->name);  | 
3225  | 0  |     }  | 
3226  |  | 
  | 
3227  | 0  |     if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&  | 
3228  | 0  |         _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { | 
3229  | 0  |         Py_DECREF(names);  | 
3230  | 0  |         return compiler_error(c, "from __future__ imports must occur "  | 
3231  | 0  |                               "at the beginning of the file");  | 
3232  | 0  |     }  | 
3233  | 0  |     ADDOP_LOAD_CONST_NEW(c, names);  | 
3234  |  | 
  | 
3235  | 0  |     if (s->v.ImportFrom.module) { | 
3236  | 0  |         ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);  | 
3237  | 0  |     }  | 
3238  | 0  |     else { | 
3239  | 0  |         ADDOP_NAME(c, IMPORT_NAME, empty_string, names);  | 
3240  | 0  |     }  | 
3241  | 0  |     for (i = 0; i < n; i++) { | 
3242  | 0  |         alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);  | 
3243  | 0  |         identifier store_name;  | 
3244  |  | 
  | 
3245  | 0  |         if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { | 
3246  | 0  |             assert(n == 1);  | 
3247  | 0  |             ADDOP(c, IMPORT_STAR);  | 
3248  | 0  |             return 1;  | 
3249  | 0  |         }  | 
3250  |  |  | 
3251  | 0  |         ADDOP_NAME(c, IMPORT_FROM, alias->name, names);  | 
3252  | 0  |         store_name = alias->name;  | 
3253  | 0  |         if (alias->asname)  | 
3254  | 0  |             store_name = alias->asname;  | 
3255  |  | 
  | 
3256  | 0  |         if (!compiler_nameop(c, store_name, Store)) { | 
3257  | 0  |             return 0;  | 
3258  | 0  |         }  | 
3259  | 0  |     }  | 
3260  |  |     /* remove imported module */  | 
3261  | 0  |     ADDOP(c, POP_TOP);  | 
3262  | 0  |     return 1;  | 
3263  | 0  | }  | 
3264  |  |  | 
3265  |  | static int  | 
3266  |  | compiler_assert(struct compiler *c, stmt_ty s)  | 
3267  | 6  | { | 
3268  | 6  |     static PyObject *assertion_error = NULL;  | 
3269  | 6  |     basicblock *end;  | 
3270  |  |  | 
3271  | 6  |     if (c->c_optimize)  | 
3272  | 0  |         return 1;  | 
3273  | 6  |     if (assertion_error == NULL) { | 
3274  | 2  |         assertion_error = PyUnicode_InternFromString("AssertionError"); | 
3275  | 2  |         if (assertion_error == NULL)  | 
3276  | 0  |             return 0;  | 
3277  | 2  |     }  | 
3278  | 6  |     if (s->v.Assert.test->kind == Tuple_kind &&  | 
3279  | 0  |         asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)  | 
3280  | 0  |     { | 
3281  | 0  |         if (!compiler_warn(c, "assertion is always true, "  | 
3282  | 0  |                               "perhaps remove parentheses?"))  | 
3283  | 0  |         { | 
3284  | 0  |             return 0;  | 
3285  | 0  |         }  | 
3286  | 0  |     }  | 
3287  | 6  |     end = compiler_new_block(c);  | 
3288  | 6  |     if (end == NULL)  | 
3289  | 0  |         return 0;  | 
3290  | 6  |     if (!compiler_jump_if(c, s->v.Assert.test, end, 1))  | 
3291  | 0  |         return 0;  | 
3292  | 6  |     ADDOP_O(c, LOAD_GLOBAL, assertion_error, names);  | 
3293  | 6  |     if (s->v.Assert.msg) { | 
3294  | 6  |         VISIT(c, expr, s->v.Assert.msg);  | 
3295  | 6  |         ADDOP_I(c, CALL_FUNCTION, 1);  | 
3296  | 6  |     }  | 
3297  | 6  |     ADDOP_I(c, RAISE_VARARGS, 1);  | 
3298  | 6  |     compiler_use_next_block(c, end);  | 
3299  | 6  |     return 1;  | 
3300  | 6  | }  | 
3301  |  |  | 
3302  |  | static int  | 
3303  |  | compiler_visit_stmt_expr(struct compiler *c, expr_ty value)  | 
3304  | 36  | { | 
3305  | 36  |     if (c->c_interactive && c->c_nestlevel <= 1) { | 
3306  | 0  |         VISIT(c, expr, value);  | 
3307  | 0  |         ADDOP(c, PRINT_EXPR);  | 
3308  | 0  |         return 1;  | 
3309  | 0  |     }  | 
3310  |  |  | 
3311  | 36  |     if (value->kind == Constant_kind) { | 
3312  |  |         /* ignore constant statement */  | 
3313  | 0  |         return 1;  | 
3314  | 0  |     }  | 
3315  |  |  | 
3316  | 36  |     VISIT(c, expr, value);  | 
3317  | 36  |     ADDOP(c, POP_TOP);  | 
3318  | 36  |     return 1;  | 
3319  | 36  | }  | 
3320  |  |  | 
3321  |  | static int  | 
3322  |  | compiler_visit_stmt(struct compiler *c, stmt_ty s)  | 
3323  | 140  | { | 
3324  | 140  |     Py_ssize_t i, n;  | 
3325  |  |  | 
3326  |  |     /* Always assign a lineno to the next instruction for a stmt. */  | 
3327  | 140  |     c->u->u_lineno = s->lineno;  | 
3328  | 140  |     c->u->u_col_offset = s->col_offset;  | 
3329  | 140  |     c->u->u_lineno_set = 0;  | 
3330  |  |  | 
3331  | 140  |     switch (s->kind) { | 
3332  | 4  |     case FunctionDef_kind:  | 
3333  | 4  |         return compiler_function(c, s, 0);  | 
3334  | 0  |     case ClassDef_kind:  | 
3335  | 0  |         return compiler_class(c, s);  | 
3336  | 6  |     case Return_kind:  | 
3337  | 6  |         return compiler_return(c, s);  | 
3338  | 2  |     case Delete_kind:  | 
3339  | 2  |         VISIT_SEQ(c, expr, s->v.Delete.targets)  | 
3340  | 2  |         break;  | 
3341  | 34  |     case Assign_kind:  | 
3342  | 34  |         n = asdl_seq_LEN(s->v.Assign.targets);  | 
3343  | 34  |         VISIT(c, expr, s->v.Assign.value);  | 
3344  | 70  |         for (i = 0; i < n; i++) { | 
3345  | 36  |             if (i < n - 1)  | 
3346  | 36  |                 ADDOP(c, DUP_TOP);  | 
3347  | 36  |             VISIT(c, expr,  | 
3348  | 36  |                   (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));  | 
3349  | 36  |         }  | 
3350  | 34  |         break;  | 
3351  | 34  |     case AugAssign_kind:  | 
3352  | 0  |         return compiler_augassign(c, s);  | 
3353  | 0  |     case AnnAssign_kind:  | 
3354  | 0  |         return compiler_annassign(c, s);  | 
3355  | 8  |     case For_kind:  | 
3356  | 8  |         return compiler_for(c, s);  | 
3357  | 0  |     case While_kind:  | 
3358  | 0  |         return compiler_while(c, s);  | 
3359  | 8  |     case If_kind:  | 
3360  | 8  |         return compiler_if(c, s);  | 
3361  | 2  |     case Raise_kind:  | 
3362  | 2  |         n = 0;  | 
3363  | 2  |         if (s->v.Raise.exc) { | 
3364  | 0  |             VISIT(c, expr, s->v.Raise.exc);  | 
3365  | 0  |             n++;  | 
3366  | 0  |             if (s->v.Raise.cause) { | 
3367  | 0  |                 VISIT(c, expr, s->v.Raise.cause);  | 
3368  | 0  |                 n++;  | 
3369  | 0  |             }  | 
3370  | 0  |         }  | 
3371  | 2  |         ADDOP_I(c, RAISE_VARARGS, (int)n);  | 
3372  | 2  |         break;  | 
3373  | 18  |     case Try_kind:  | 
3374  | 18  |         return compiler_try(c, s);  | 
3375  | 6  |     case Assert_kind:  | 
3376  | 6  |         return compiler_assert(c, s);  | 
3377  | 0  |     case Import_kind:  | 
3378  | 0  |         return compiler_import(c, s);  | 
3379  | 0  |     case ImportFrom_kind:  | 
3380  | 0  |         return compiler_from_import(c, s);  | 
3381  | 0  |     case Global_kind:  | 
3382  | 0  |     case Nonlocal_kind:  | 
3383  | 0  |         break;  | 
3384  | 36  |     case Expr_kind:  | 
3385  | 36  |         return compiler_visit_stmt_expr(c, s->v.Expr.value);  | 
3386  | 16  |     case Pass_kind:  | 
3387  | 16  |         break;  | 
3388  | 0  |     case Break_kind:  | 
3389  | 0  |         return compiler_break(c);  | 
3390  | 0  |     case Continue_kind:  | 
3391  | 0  |         return compiler_continue(c);  | 
3392  | 0  |     case With_kind:  | 
3393  | 0  |         return compiler_with(c, s, 0);  | 
3394  | 0  |     case AsyncFunctionDef_kind:  | 
3395  | 0  |         return compiler_function(c, s, 1);  | 
3396  | 0  |     case AsyncWith_kind:  | 
3397  | 0  |         return compiler_async_with(c, s, 0);  | 
3398  | 0  |     case AsyncFor_kind:  | 
3399  | 0  |         return compiler_async_for(c, s);  | 
3400  | 140  |     }  | 
3401  |  |  | 
3402  | 54  |     return 1;  | 
3403  | 140  | }  | 
3404  |  |  | 
3405  |  | static int  | 
3406  |  | unaryop(unaryop_ty op)  | 
3407  | 0  | { | 
3408  | 0  |     switch (op) { | 
3409  | 0  |     case Invert:  | 
3410  | 0  |         return UNARY_INVERT;  | 
3411  | 0  |     case Not:  | 
3412  | 0  |         return UNARY_NOT;  | 
3413  | 0  |     case UAdd:  | 
3414  | 0  |         return UNARY_POSITIVE;  | 
3415  | 0  |     case USub:  | 
3416  | 0  |         return UNARY_NEGATIVE;  | 
3417  | 0  |     default:  | 
3418  | 0  |         PyErr_Format(PyExc_SystemError,  | 
3419  | 0  |             "unary op %d should not be possible", op);  | 
3420  | 0  |         return 0;  | 
3421  | 0  |     }  | 
3422  | 0  | }  | 
3423  |  |  | 
3424  |  | static int  | 
3425  |  | binop(struct compiler *c, operator_ty op)  | 
3426  | 12  | { | 
3427  | 12  |     switch (op) { | 
3428  | 2  |     case Add:  | 
3429  | 2  |         return BINARY_ADD;  | 
3430  | 4  |     case Sub:  | 
3431  | 4  |         return BINARY_SUBTRACT;  | 
3432  | 2  |     case Mult:  | 
3433  | 2  |         return BINARY_MULTIPLY;  | 
3434  | 0  |     case MatMult:  | 
3435  | 0  |         return BINARY_MATRIX_MULTIPLY;  | 
3436  | 0  |     case Div:  | 
3437  | 0  |         return BINARY_TRUE_DIVIDE;  | 
3438  | 4  |     case Mod:  | 
3439  | 4  |         return BINARY_MODULO;  | 
3440  | 0  |     case Pow:  | 
3441  | 0  |         return BINARY_POWER;  | 
3442  | 0  |     case LShift:  | 
3443  | 0  |         return BINARY_LSHIFT;  | 
3444  | 0  |     case RShift:  | 
3445  | 0  |         return BINARY_RSHIFT;  | 
3446  | 0  |     case BitOr:  | 
3447  | 0  |         return BINARY_OR;  | 
3448  | 0  |     case BitXor:  | 
3449  | 0  |         return BINARY_XOR;  | 
3450  | 0  |     case BitAnd:  | 
3451  | 0  |         return BINARY_AND;  | 
3452  | 0  |     case FloorDiv:  | 
3453  | 0  |         return BINARY_FLOOR_DIVIDE;  | 
3454  | 0  |     default:  | 
3455  | 0  |         PyErr_Format(PyExc_SystemError,  | 
3456  | 0  |             "binary op %d should not be possible", op);  | 
3457  | 0  |         return 0;  | 
3458  | 12  |     }  | 
3459  | 12  | }  | 
3460  |  |  | 
3461  |  | static int  | 
3462  |  | inplace_binop(struct compiler *c, operator_ty op)  | 
3463  | 0  | { | 
3464  | 0  |     switch (op) { | 
3465  | 0  |     case Add:  | 
3466  | 0  |         return INPLACE_ADD;  | 
3467  | 0  |     case Sub:  | 
3468  | 0  |         return INPLACE_SUBTRACT;  | 
3469  | 0  |     case Mult:  | 
3470  | 0  |         return INPLACE_MULTIPLY;  | 
3471  | 0  |     case MatMult:  | 
3472  | 0  |         return INPLACE_MATRIX_MULTIPLY;  | 
3473  | 0  |     case Div:  | 
3474  | 0  |         return INPLACE_TRUE_DIVIDE;  | 
3475  | 0  |     case Mod:  | 
3476  | 0  |         return INPLACE_MODULO;  | 
3477  | 0  |     case Pow:  | 
3478  | 0  |         return INPLACE_POWER;  | 
3479  | 0  |     case LShift:  | 
3480  | 0  |         return INPLACE_LSHIFT;  | 
3481  | 0  |     case RShift:  | 
3482  | 0  |         return INPLACE_RSHIFT;  | 
3483  | 0  |     case BitOr:  | 
3484  | 0  |         return INPLACE_OR;  | 
3485  | 0  |     case BitXor:  | 
3486  | 0  |         return INPLACE_XOR;  | 
3487  | 0  |     case BitAnd:  | 
3488  | 0  |         return INPLACE_AND;  | 
3489  | 0  |     case FloorDiv:  | 
3490  | 0  |         return INPLACE_FLOOR_DIVIDE;  | 
3491  | 0  |     default:  | 
3492  | 0  |         PyErr_Format(PyExc_SystemError,  | 
3493  | 0  |             "inplace binary op %d should not be possible", op);  | 
3494  | 0  |         return 0;  | 
3495  | 0  |     }  | 
3496  | 0  | }  | 
3497  |  |  | 
3498  |  | static int  | 
3499  |  | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)  | 
3500  | 341  | { | 
3501  | 341  |     int op, scope;  | 
3502  | 341  |     Py_ssize_t arg;  | 
3503  | 341  |     enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; | 
3504  |  |  | 
3505  | 341  |     PyObject *dict = c->u->u_names;  | 
3506  | 341  |     PyObject *mangled;  | 
3507  |  |     /* XXX AugStore isn't used anywhere! */  | 
3508  |  |  | 
3509  | 341  |     assert(!_PyUnicode_EqualToASCIIString(name, "None") &&  | 
3510  | 341  |            !_PyUnicode_EqualToASCIIString(name, "True") &&  | 
3511  | 341  |            !_PyUnicode_EqualToASCIIString(name, "False"));  | 
3512  |  |  | 
3513  | 341  |     mangled = _Py_Mangle(c->u->u_private, name);  | 
3514  | 341  |     if (!mangled)  | 
3515  | 0  |         return 0;  | 
3516  |  |  | 
3517  | 341  |     op = 0;  | 
3518  | 341  |     optype = OP_NAME;  | 
3519  | 341  |     scope = PyST_GetScope(c->u->u_ste, mangled);  | 
3520  | 341  |     switch (scope) { | 
3521  | 0  |     case FREE:  | 
3522  | 0  |         dict = c->u->u_freevars;  | 
3523  | 0  |         optype = OP_DEREF;  | 
3524  | 0  |         break;  | 
3525  | 0  |     case CELL:  | 
3526  | 0  |         dict = c->u->u_cellvars;  | 
3527  | 0  |         optype = OP_DEREF;  | 
3528  | 0  |         break;  | 
3529  | 243  |     case LOCAL:  | 
3530  | 243  |         if (c->u->u_ste->ste_type == FunctionBlock)  | 
3531  | 17  |             optype = OP_FAST;  | 
3532  | 243  |         break;  | 
3533  | 98  |     case GLOBAL_IMPLICIT:  | 
3534  | 98  |         if (c->u->u_ste->ste_type == FunctionBlock)  | 
3535  | 6  |             optype = OP_GLOBAL;  | 
3536  | 98  |         break;  | 
3537  | 0  |     case GLOBAL_EXPLICIT:  | 
3538  | 0  |         optype = OP_GLOBAL;  | 
3539  | 0  |         break;  | 
3540  | 0  |     default:  | 
3541  |  |         /* scope can be 0 */  | 
3542  | 0  |         break;  | 
3543  | 341  |     }  | 
3544  |  |  | 
3545  |  |     /* XXX Leave assert here, but handle __doc__ and the like better */  | 
3546  | 341  |     assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');  | 
3547  |  |  | 
3548  | 341  |     switch (optype) { | 
3549  | 0  |     case OP_DEREF:  | 
3550  | 0  |         switch (ctx) { | 
3551  | 0  |         case Load:  | 
3552  | 0  |             op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;  | 
3553  | 0  |             break;  | 
3554  | 0  |         case Store:  | 
3555  | 0  |             op = STORE_DEREF;  | 
3556  | 0  |             break;  | 
3557  | 0  |         case AugLoad:  | 
3558  | 0  |         case AugStore:  | 
3559  | 0  |             break;  | 
3560  | 0  |         case Del: op = DELETE_DEREF; break;  | 
3561  | 0  |         case Param:  | 
3562  | 0  |         default:  | 
3563  | 0  |             PyErr_SetString(PyExc_SystemError,  | 
3564  | 0  |                             "param invalid for deref variable");  | 
3565  | 0  |             return 0;  | 
3566  | 0  |         }  | 
3567  | 0  |         break;  | 
3568  | 17  |     case OP_FAST:  | 
3569  | 17  |         switch (ctx) { | 
3570  | 17  |         case Load: op = LOAD_FAST; break;  | 
3571  | 0  |         case Store:  | 
3572  | 0  |             op = STORE_FAST;  | 
3573  | 0  |             break;  | 
3574  | 0  |         case Del: op = DELETE_FAST; break;  | 
3575  | 0  |         case AugLoad:  | 
3576  | 0  |         case AugStore:  | 
3577  | 0  |             break;  | 
3578  | 0  |         case Param:  | 
3579  | 0  |         default:  | 
3580  | 0  |             PyErr_SetString(PyExc_SystemError,  | 
3581  | 0  |                             "param invalid for local variable");  | 
3582  | 0  |             return 0;  | 
3583  | 17  |         }  | 
3584  | 17  |         ADDOP_N(c, op, mangled, varnames);  | 
3585  | 17  |         return 1;  | 
3586  | 6  |     case OP_GLOBAL:  | 
3587  | 6  |         switch (ctx) { | 
3588  | 6  |         case Load: op = LOAD_GLOBAL; break;  | 
3589  | 0  |         case Store:  | 
3590  | 0  |             op = STORE_GLOBAL;  | 
3591  | 0  |             break;  | 
3592  | 0  |         case Del: op = DELETE_GLOBAL; break;  | 
3593  | 0  |         case AugLoad:  | 
3594  | 0  |         case AugStore:  | 
3595  | 0  |             break;  | 
3596  | 0  |         case Param:  | 
3597  | 0  |         default:  | 
3598  | 0  |             PyErr_SetString(PyExc_SystemError,  | 
3599  | 0  |                             "param invalid for global variable");  | 
3600  | 0  |             return 0;  | 
3601  | 6  |         }  | 
3602  | 6  |         break;  | 
3603  | 318  |     case OP_NAME:  | 
3604  | 318  |         switch (ctx) { | 
3605  | 216  |         case Load: op = LOAD_NAME; break;  | 
3606  | 88  |         case Store:  | 
3607  | 88  |             op = STORE_NAME;  | 
3608  | 88  |             break;  | 
3609  | 14  |         case Del: op = DELETE_NAME; break;  | 
3610  | 0  |         case AugLoad:  | 
3611  | 0  |         case AugStore:  | 
3612  | 0  |             break;  | 
3613  | 0  |         case Param:  | 
3614  | 0  |         default:  | 
3615  | 0  |             PyErr_SetString(PyExc_SystemError,  | 
3616  | 0  |                             "param invalid for name variable");  | 
3617  | 0  |             return 0;  | 
3618  | 318  |         }  | 
3619  | 318  |         break;  | 
3620  | 341  |     }  | 
3621  |  |  | 
3622  | 341  |     assert(op);  | 
3623  | 324  |     arg = compiler_add_o(c, dict, mangled);  | 
3624  | 324  |     Py_DECREF(mangled);  | 
3625  | 324  |     if (arg < 0)  | 
3626  | 0  |         return 0;  | 
3627  | 324  |     return compiler_addop_i(c, op, arg);  | 
3628  | 324  | }  | 
3629  |  |  | 
3630  |  | static int  | 
3631  |  | compiler_boolop(struct compiler *c, expr_ty e)  | 
3632  | 0  | { | 
3633  | 0  |     basicblock *end;  | 
3634  | 0  |     int jumpi;  | 
3635  | 0  |     Py_ssize_t i, n;  | 
3636  | 0  |     asdl_seq *s;  | 
3637  |  | 
  | 
3638  | 0  |     assert(e->kind == BoolOp_kind);  | 
3639  | 0  |     if (e->v.BoolOp.op == And)  | 
3640  | 0  |         jumpi = JUMP_IF_FALSE_OR_POP;  | 
3641  | 0  |     else  | 
3642  | 0  |         jumpi = JUMP_IF_TRUE_OR_POP;  | 
3643  | 0  |     end = compiler_new_block(c);  | 
3644  | 0  |     if (end == NULL)  | 
3645  | 0  |         return 0;  | 
3646  | 0  |     s = e->v.BoolOp.values;  | 
3647  | 0  |     n = asdl_seq_LEN(s) - 1;  | 
3648  | 0  |     assert(n >= 0);  | 
3649  | 0  |     for (i = 0; i < n; ++i) { | 
3650  | 0  |         VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));  | 
3651  | 0  |         ADDOP_JABS(c, jumpi, end);  | 
3652  | 0  |     }  | 
3653  | 0  |     VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));  | 
3654  | 0  |     compiler_use_next_block(c, end);  | 
3655  | 0  |     return 1;  | 
3656  | 0  | }  | 
3657  |  |  | 
3658  |  | static int  | 
3659  |  | starunpack_helper(struct compiler *c, asdl_seq *elts,  | 
3660  |  |                   int single_op, int inner_op, int outer_op)  | 
3661  | 16  | { | 
3662  | 16  |     Py_ssize_t n = asdl_seq_LEN(elts);  | 
3663  | 16  |     Py_ssize_t i, nsubitems = 0, nseen = 0;  | 
3664  | 57  |     for (i = 0; i < n; i++) { | 
3665  | 41  |         expr_ty elt = asdl_seq_GET(elts, i);  | 
3666  | 41  |         if (elt->kind == Starred_kind) { | 
3667  | 0  |             if (nseen) { | 
3668  | 0  |                 ADDOP_I(c, inner_op, nseen);  | 
3669  | 0  |                 nseen = 0;  | 
3670  | 0  |                 nsubitems++;  | 
3671  | 0  |             }  | 
3672  | 0  |             VISIT(c, expr, elt->v.Starred.value);  | 
3673  | 0  |             nsubitems++;  | 
3674  | 0  |         }  | 
3675  | 41  |         else { | 
3676  | 41  |             VISIT(c, expr, elt);  | 
3677  | 41  |             nseen++;  | 
3678  | 41  |         }  | 
3679  | 41  |     }  | 
3680  | 16  |     if (nsubitems) { | 
3681  | 0  |         if (nseen) { | 
3682  | 0  |             ADDOP_I(c, inner_op, nseen);  | 
3683  | 0  |             nsubitems++;  | 
3684  | 0  |         }  | 
3685  | 0  |         ADDOP_I(c, outer_op, nsubitems);  | 
3686  | 0  |     }  | 
3687  | 16  |     else  | 
3688  | 16  |         ADDOP_I(c, single_op, nseen);  | 
3689  | 16  |     return 1;  | 
3690  | 16  | }  | 
3691  |  |  | 
3692  |  | static int  | 
3693  |  | assignment_helper(struct compiler *c, asdl_seq *elts)  | 
3694  | 10  | { | 
3695  | 10  |     Py_ssize_t n = asdl_seq_LEN(elts);  | 
3696  | 10  |     Py_ssize_t i;  | 
3697  | 10  |     int seen_star = 0;  | 
3698  | 34  |     for (i = 0; i < n; i++) { | 
3699  | 24  |         expr_ty elt = asdl_seq_GET(elts, i);  | 
3700  | 24  |         if (elt->kind == Starred_kind && !seen_star) { | 
3701  | 0  |             if ((i >= (1 << 8)) ||  | 
3702  | 0  |                 (n-i-1 >= (INT_MAX >> 8)))  | 
3703  | 0  |                 return compiler_error(c,  | 
3704  | 0  |                     "too many expressions in "  | 
3705  | 0  |                     "star-unpacking assignment");  | 
3706  | 0  |             ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));  | 
3707  | 0  |             seen_star = 1;  | 
3708  | 0  |             asdl_seq_SET(elts, i, elt->v.Starred.value);  | 
3709  | 0  |         }  | 
3710  | 24  |         else if (elt->kind == Starred_kind) { | 
3711  | 0  |             return compiler_error(c,  | 
3712  | 0  |                 "two starred expressions in assignment");  | 
3713  | 0  |         }  | 
3714  | 24  |     }  | 
3715  | 10  |     if (!seen_star) { | 
3716  | 10  |         ADDOP_I(c, UNPACK_SEQUENCE, n);  | 
3717  | 10  |     }  | 
3718  | 10  |     VISIT_SEQ(c, expr, elts);  | 
3719  | 10  |     return 1;  | 
3720  | 10  | }  | 
3721  |  |  | 
3722  |  | static int  | 
3723  |  | compiler_list(struct compiler *c, expr_ty e)  | 
3724  | 4  | { | 
3725  | 4  |     asdl_seq *elts = e->v.List.elts;  | 
3726  | 4  |     if (e->v.List.ctx == Store) { | 
3727  | 0  |         return assignment_helper(c, elts);  | 
3728  | 0  |     }  | 
3729  | 4  |     else if (e->v.List.ctx == Load) { | 
3730  | 4  |         return starunpack_helper(c, elts,  | 
3731  | 4  |                                  BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK);  | 
3732  | 4  |     }  | 
3733  | 0  |     else  | 
3734  | 0  |         VISIT_SEQ(c, expr, elts);  | 
3735  | 0  |     return 1;  | 
3736  | 4  | }  | 
3737  |  |  | 
3738  |  | static int  | 
3739  |  | compiler_tuple(struct compiler *c, expr_ty e)  | 
3740  | 22  | { | 
3741  | 22  |     asdl_seq *elts = e->v.Tuple.elts;  | 
3742  | 22  |     if (e->v.Tuple.ctx == Store) { | 
3743  | 10  |         return assignment_helper(c, elts);  | 
3744  | 10  |     }  | 
3745  | 12  |     else if (e->v.Tuple.ctx == Load) { | 
3746  | 12  |         return starunpack_helper(c, elts,  | 
3747  | 12  |                                  BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK);  | 
3748  | 12  |     }  | 
3749  | 0  |     else  | 
3750  | 0  |         VISIT_SEQ(c, expr, elts);  | 
3751  | 0  |     return 1;  | 
3752  | 22  | }  | 
3753  |  |  | 
3754  |  | static int  | 
3755  |  | compiler_set(struct compiler *c, expr_ty e)  | 
3756  | 0  | { | 
3757  | 0  |     return starunpack_helper(c, e->v.Set.elts, BUILD_SET,  | 
3758  | 0  |                              BUILD_SET, BUILD_SET_UNPACK);  | 
3759  | 0  | }  | 
3760  |  |  | 
3761  |  | static int  | 
3762  |  | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)  | 
3763  | 0  | { | 
3764  | 0  |     Py_ssize_t i;  | 
3765  | 0  |     for (i = begin; i < end; i++) { | 
3766  | 0  |         expr_ty key = (expr_ty)asdl_seq_GET(seq, i);  | 
3767  | 0  |         if (key == NULL || key->kind != Constant_kind)  | 
3768  | 0  |             return 0;  | 
3769  | 0  |     }  | 
3770  | 0  |     return 1;  | 
3771  | 0  | }  | 
3772  |  |  | 
3773  |  | static int  | 
3774  |  | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)  | 
3775  | 6  | { | 
3776  | 6  |     Py_ssize_t i, n = end - begin;  | 
3777  | 6  |     PyObject *keys, *key;  | 
3778  | 6  |     if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { | 
3779  | 0  |         for (i = begin; i < end; i++) { | 
3780  | 0  |             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));  | 
3781  | 0  |         }  | 
3782  | 0  |         keys = PyTuple_New(n);  | 
3783  | 0  |         if (keys == NULL) { | 
3784  | 0  |             return 0;  | 
3785  | 0  |         }  | 
3786  | 0  |         for (i = begin; i < end; i++) { | 
3787  | 0  |             key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;  | 
3788  | 0  |             Py_INCREF(key);  | 
3789  | 0  |             PyTuple_SET_ITEM(keys, i - begin, key);  | 
3790  | 0  |         }  | 
3791  | 0  |         ADDOP_LOAD_CONST_NEW(c, keys);  | 
3792  | 0  |         ADDOP_I(c, BUILD_CONST_KEY_MAP, n);  | 
3793  | 0  |     }  | 
3794  | 6  |     else { | 
3795  | 6  |         for (i = begin; i < end; i++) { | 
3796  | 0  |             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));  | 
3797  | 0  |             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));  | 
3798  | 0  |         }  | 
3799  | 6  |         ADDOP_I(c, BUILD_MAP, n);  | 
3800  | 6  |     }  | 
3801  | 6  |     return 1;  | 
3802  | 6  | }  | 
3803  |  |  | 
3804  |  | static int  | 
3805  |  | compiler_dict(struct compiler *c, expr_ty e)  | 
3806  | 6  | { | 
3807  | 6  |     Py_ssize_t i, n, elements;  | 
3808  | 6  |     int containers;  | 
3809  | 6  |     int is_unpacking = 0;  | 
3810  | 6  |     n = asdl_seq_LEN(e->v.Dict.values);  | 
3811  | 6  |     containers = 0;  | 
3812  | 6  |     elements = 0;  | 
3813  | 6  |     for (i = 0; i < n; i++) { | 
3814  | 0  |         is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;  | 
3815  | 0  |         if (elements == 0xFFFF || (elements && is_unpacking)) { | 
3816  | 0  |             if (!compiler_subdict(c, e, i - elements, i))  | 
3817  | 0  |                 return 0;  | 
3818  | 0  |             containers++;  | 
3819  | 0  |             elements = 0;  | 
3820  | 0  |         }  | 
3821  | 0  |         if (is_unpacking) { | 
3822  | 0  |             VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));  | 
3823  | 0  |             containers++;  | 
3824  | 0  |         }  | 
3825  | 0  |         else { | 
3826  | 0  |             elements++;  | 
3827  | 0  |         }  | 
3828  | 0  |     }  | 
3829  | 6  |     if (elements || containers == 0) { | 
3830  | 6  |         if (!compiler_subdict(c, e, n - elements, n))  | 
3831  | 0  |             return 0;  | 
3832  | 6  |         containers++;  | 
3833  | 6  |     }  | 
3834  |  |     /* If there is more than one dict, they need to be merged into a new  | 
3835  |  |      * dict.  If there is one dict and it's an unpacking, then it needs  | 
3836  |  |      * to be copied into a new dict." */  | 
3837  | 6  |     if (containers > 1 || is_unpacking) { | 
3838  | 0  |         ADDOP_I(c, BUILD_MAP_UNPACK, containers);  | 
3839  | 0  |     }  | 
3840  | 6  |     return 1;  | 
3841  | 6  | }  | 
3842  |  |  | 
3843  |  | static int  | 
3844  |  | compiler_compare(struct compiler *c, expr_ty e)  | 
3845  | 24  | { | 
3846  | 24  |     Py_ssize_t i, n;  | 
3847  |  |  | 
3848  | 24  |     if (!check_compare(c, e)) { | 
3849  | 0  |         return 0;  | 
3850  | 0  |     }  | 
3851  | 24  |     VISIT(c, expr, e->v.Compare.left);  | 
3852  | 24  |     assert(asdl_seq_LEN(e->v.Compare.ops) > 0);  | 
3853  | 24  |     n = asdl_seq_LEN(e->v.Compare.ops) - 1;  | 
3854  | 24  |     if (n == 0) { | 
3855  | 24  |         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));  | 
3856  | 24  |         ADDOP_I(c, COMPARE_OP,  | 
3857  | 24  |             cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0))));  | 
3858  | 24  |     }  | 
3859  | 0  |     else { | 
3860  | 0  |         basicblock *cleanup = compiler_new_block(c);  | 
3861  | 0  |         if (cleanup == NULL)  | 
3862  | 0  |             return 0;  | 
3863  | 0  |         for (i = 0; i < n; i++) { | 
3864  | 0  |             VISIT(c, expr,  | 
3865  | 0  |                 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));  | 
3866  | 0  |             ADDOP(c, DUP_TOP);  | 
3867  | 0  |             ADDOP(c, ROT_THREE);  | 
3868  | 0  |             ADDOP_I(c, COMPARE_OP,  | 
3869  | 0  |                 cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i))));  | 
3870  | 0  |             ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);  | 
3871  | 0  |             NEXT_BLOCK(c);  | 
3872  | 0  |         }  | 
3873  | 0  |         VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));  | 
3874  | 0  |         ADDOP_I(c, COMPARE_OP,  | 
3875  | 0  |             cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n))));  | 
3876  | 0  |         basicblock *end = compiler_new_block(c);  | 
3877  | 0  |         if (end == NULL)  | 
3878  | 0  |             return 0;  | 
3879  | 0  |         ADDOP_JREL(c, JUMP_FORWARD, end);  | 
3880  | 0  |         compiler_use_next_block(c, cleanup);  | 
3881  | 0  |         ADDOP(c, ROT_TWO);  | 
3882  | 0  |         ADDOP(c, POP_TOP);  | 
3883  | 0  |         compiler_use_next_block(c, end);  | 
3884  | 0  |     }  | 
3885  | 24  |     return 1;  | 
3886  | 24  | }  | 
3887  |  |  | 
3888  |  | static PyTypeObject *  | 
3889  |  | infer_type(expr_ty e)  | 
3890  | 8  | { | 
3891  | 8  |     switch (e->kind) { | 
3892  | 0  |     case Tuple_kind:  | 
3893  | 0  |         return &PyTuple_Type;  | 
3894  | 0  |     case List_kind:  | 
3895  | 0  |     case ListComp_kind:  | 
3896  | 0  |         return &PyList_Type;  | 
3897  | 0  |     case Dict_kind:  | 
3898  | 0  |     case DictComp_kind:  | 
3899  | 0  |         return &PyDict_Type;  | 
3900  | 0  |     case Set_kind:  | 
3901  | 0  |     case SetComp_kind:  | 
3902  | 0  |         return &PySet_Type;  | 
3903  | 0  |     case GeneratorExp_kind:  | 
3904  | 0  |         return &PyGen_Type;  | 
3905  | 0  |     case Lambda_kind:  | 
3906  | 0  |         return &PyFunction_Type;  | 
3907  | 0  |     case JoinedStr_kind:  | 
3908  | 0  |     case FormattedValue_kind:  | 
3909  | 0  |         return &PyUnicode_Type;  | 
3910  | 4  |     case Constant_kind:  | 
3911  | 4  |         return e->v.Constant.value->ob_type;  | 
3912  | 4  |     default:  | 
3913  | 4  |         return NULL;  | 
3914  | 8  |     }  | 
3915  | 8  | }  | 
3916  |  |  | 
3917  |  | static int  | 
3918  |  | check_caller(struct compiler *c, expr_ty e)  | 
3919  | 32  | { | 
3920  | 32  |     switch (e->kind) { | 
3921  | 0  |     case Constant_kind:  | 
3922  | 0  |     case Tuple_kind:  | 
3923  | 0  |     case List_kind:  | 
3924  | 0  |     case ListComp_kind:  | 
3925  | 0  |     case Dict_kind:  | 
3926  | 0  |     case DictComp_kind:  | 
3927  | 0  |     case Set_kind:  | 
3928  | 0  |     case SetComp_kind:  | 
3929  | 0  |     case GeneratorExp_kind:  | 
3930  | 0  |     case JoinedStr_kind:  | 
3931  | 0  |     case FormattedValue_kind:  | 
3932  | 0  |         return compiler_warn(c, "'%.200s' object is not callable; "  | 
3933  | 0  |                                 "perhaps you missed a comma?",  | 
3934  | 0  |                                 infer_type(e)->tp_name);  | 
3935  | 32  |     default:  | 
3936  | 32  |         return 1;  | 
3937  | 32  |     }  | 
3938  | 32  | }  | 
3939  |  |  | 
3940  |  | static int  | 
3941  |  | check_subscripter(struct compiler *c, expr_ty e)  | 
3942  | 10  | { | 
3943  | 10  |     PyObject *v;  | 
3944  |  |  | 
3945  | 10  |     switch (e->kind) { | 
3946  | 0  |     case Constant_kind:  | 
3947  | 0  |         v = e->v.Constant.value;  | 
3948  | 0  |         if (!(v == Py_None || v == Py_Ellipsis ||  | 
3949  | 0  |               PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||  | 
3950  | 0  |               PyAnySet_Check(v)))  | 
3951  | 0  |         { | 
3952  | 0  |             return 1;  | 
3953  | 0  |         }  | 
3954  |  |         /* fall through */  | 
3955  | 0  |     case Set_kind:  | 
3956  | 0  |     case SetComp_kind:  | 
3957  | 0  |     case GeneratorExp_kind:  | 
3958  | 0  |     case Lambda_kind:  | 
3959  | 0  |         return compiler_warn(c, "'%.200s' object is not subscriptable; "  | 
3960  | 0  |                                 "perhaps you missed a comma?",  | 
3961  | 0  |                                 infer_type(e)->tp_name);  | 
3962  | 10  |     default:  | 
3963  | 10  |         return 1;  | 
3964  | 10  |     }  | 
3965  | 10  | }  | 
3966  |  |  | 
3967  |  | static int  | 
3968  |  | check_index(struct compiler *c, expr_ty e, slice_ty s)  | 
3969  | 10  | { | 
3970  | 10  |     PyObject *v;  | 
3971  |  |  | 
3972  | 10  |     if (s->kind != Index_kind) { | 
3973  | 2  |         return 1;  | 
3974  | 2  |     }  | 
3975  | 8  |     PyTypeObject *index_type = infer_type(s->v.Index.value);  | 
3976  | 8  |     if (index_type == NULL  | 
3977  | 8  |         || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)  | 
3978  | 8  |         || index_type == &PySlice_Type) { | 
3979  | 8  |         return 1;  | 
3980  | 8  |     }  | 
3981  |  |  | 
3982  | 0  |     switch (e->kind) { | 
3983  | 0  |     case Constant_kind:  | 
3984  | 0  |         v = e->v.Constant.value;  | 
3985  | 0  |         if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { | 
3986  | 0  |             return 1;  | 
3987  | 0  |         }  | 
3988  |  |         /* fall through */  | 
3989  | 0  |     case Tuple_kind:  | 
3990  | 0  |     case List_kind:  | 
3991  | 0  |     case ListComp_kind:  | 
3992  | 0  |     case JoinedStr_kind:  | 
3993  | 0  |     case FormattedValue_kind:  | 
3994  | 0  |         return compiler_warn(c, "%.200s indices must be integers or slices, "  | 
3995  | 0  |                                 "not %.200s; "  | 
3996  | 0  |                                 "perhaps you missed a comma?",  | 
3997  | 0  |                                 infer_type(e)->tp_name,  | 
3998  | 0  |                                 index_type->tp_name);  | 
3999  | 0  |     default:  | 
4000  | 0  |         return 1;  | 
4001  | 0  |     }  | 
4002  | 0  | }  | 
4003  |  |  | 
4004  |  | // Return 1 if the method call was optimized, -1 if not, and 0 on error.  | 
4005  |  | static int  | 
4006  |  | maybe_optimize_method_call(struct compiler *c, expr_ty e)  | 
4007  | 80  | { | 
4008  | 80  |     Py_ssize_t argsl, i;  | 
4009  | 80  |     expr_ty meth = e->v.Call.func;  | 
4010  | 80  |     asdl_seq *args = e->v.Call.args;  | 
4011  |  |  | 
4012  |  |     /* Check that the call node is an attribute access, and that  | 
4013  |  |        the call doesn't have keyword parameters. */  | 
4014  | 80  |     if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||  | 
4015  | 48  |             asdl_seq_LEN(e->v.Call.keywords))  | 
4016  | 32  |         return -1;  | 
4017  |  |  | 
4018  |  |     /* Check that there are no *varargs types of arguments. */  | 
4019  | 48  |     argsl = asdl_seq_LEN(args);  | 
4020  | 112  |     for (i = 0; i < argsl; i++) { | 
4021  | 64  |         expr_ty elt = asdl_seq_GET(args, i);  | 
4022  | 64  |         if (elt->kind == Starred_kind) { | 
4023  | 0  |             return -1;  | 
4024  | 0  |         }  | 
4025  | 64  |     }  | 
4026  |  |  | 
4027  |  |     /* Alright, we can optimize the code. */  | 
4028  | 48  |     VISIT(c, expr, meth->v.Attribute.value);  | 
4029  | 48  |     ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);  | 
4030  | 48  |     VISIT_SEQ(c, expr, e->v.Call.args);  | 
4031  | 48  |     ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));  | 
4032  | 48  |     return 1;  | 
4033  | 48  | }  | 
4034  |  |  | 
4035  |  | static int  | 
4036  |  | compiler_call(struct compiler *c, expr_ty e)  | 
4037  | 80  | { | 
4038  | 80  |     int ret = maybe_optimize_method_call(c, e);  | 
4039  | 80  |     if (ret >= 0) { | 
4040  | 48  |         return ret;  | 
4041  | 48  |     }  | 
4042  | 32  |     if (!check_caller(c, e->v.Call.func)) { | 
4043  | 0  |         return 0;  | 
4044  | 0  |     }  | 
4045  | 32  |     VISIT(c, expr, e->v.Call.func);  | 
4046  | 32  |     return compiler_call_helper(c, 0,  | 
4047  | 32  |                                 e->v.Call.args,  | 
4048  | 32  |                                 e->v.Call.keywords);  | 
4049  | 32  | }  | 
4050  |  |  | 
4051  |  | static int  | 
4052  |  | compiler_joined_str(struct compiler *c, expr_ty e)  | 
4053  | 0  | { | 
4054  | 0  |     VISIT_SEQ(c, expr, e->v.JoinedStr.values);  | 
4055  | 0  |     if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)  | 
4056  | 0  |         ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));  | 
4057  | 0  |     return 1;  | 
4058  | 0  | }  | 
4059  |  |  | 
4060  |  | /* Used to implement f-strings. Format a single value. */  | 
4061  |  | static int  | 
4062  |  | compiler_formatted_value(struct compiler *c, expr_ty e)  | 
4063  | 0  | { | 
4064  |  |     /* Our oparg encodes 2 pieces of information: the conversion  | 
4065  |  |        character, and whether or not a format_spec was provided.  | 
4066  |  |  | 
4067  |  |        Convert the conversion char to 3 bits:  | 
4068  |  |            : 000  0x0  FVC_NONE   The default if nothing specified.  | 
4069  |  |        !s  : 001  0x1  FVC_STR  | 
4070  |  |        !r  : 010  0x2  FVC_REPR  | 
4071  |  |        !a  : 011  0x3  FVC_ASCII  | 
4072  |  |  | 
4073  |  |        next bit is whether or not we have a format spec:  | 
4074  |  |        yes : 100  0x4  | 
4075  |  |        no  : 000  0x0  | 
4076  |  |     */  | 
4077  |  | 
  | 
4078  | 0  |     int conversion = e->v.FormattedValue.conversion;  | 
4079  | 0  |     int oparg;  | 
4080  |  |  | 
4081  |  |     /* The expression to be formatted. */  | 
4082  | 0  |     VISIT(c, expr, e->v.FormattedValue.value);  | 
4083  |  | 
  | 
4084  | 0  |     switch (conversion) { | 
4085  | 0  |     case 's': oparg = FVC_STR;   break;  | 
4086  | 0  |     case 'r': oparg = FVC_REPR;  break;  | 
4087  | 0  |     case 'a': oparg = FVC_ASCII; break;  | 
4088  | 0  |     case -1:  oparg = FVC_NONE;  break;  | 
4089  | 0  |     default:  | 
4090  | 0  |         PyErr_Format(PyExc_SystemError,  | 
4091  | 0  |                      "Unrecognized conversion character %d", conversion);  | 
4092  | 0  |         return 0;  | 
4093  | 0  |     }  | 
4094  | 0  |     if (e->v.FormattedValue.format_spec) { | 
4095  |  |         /* Evaluate the format spec, and update our opcode arg. */  | 
4096  | 0  |         VISIT(c, expr, e->v.FormattedValue.format_spec);  | 
4097  | 0  |         oparg |= FVS_HAVE_SPEC;  | 
4098  | 0  |     }  | 
4099  |  |  | 
4100  |  |     /* And push our opcode and oparg */  | 
4101  | 0  |     ADDOP_I(c, FORMAT_VALUE, oparg);  | 
4102  |  | 
  | 
4103  | 0  |     return 1;  | 
4104  | 0  | }  | 
4105  |  |  | 
4106  |  | static int  | 
4107  |  | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)  | 
4108  | 0  | { | 
4109  | 0  |     Py_ssize_t i, n = end - begin;  | 
4110  | 0  |     keyword_ty kw;  | 
4111  | 0  |     PyObject *keys, *key;  | 
4112  | 0  |     assert(n > 0);  | 
4113  | 0  |     if (n > 1) { | 
4114  | 0  |         for (i = begin; i < end; i++) { | 
4115  | 0  |             kw = asdl_seq_GET(keywords, i);  | 
4116  | 0  |             VISIT(c, expr, kw->value);  | 
4117  | 0  |         }  | 
4118  | 0  |         keys = PyTuple_New(n);  | 
4119  | 0  |         if (keys == NULL) { | 
4120  | 0  |             return 0;  | 
4121  | 0  |         }  | 
4122  | 0  |         for (i = begin; i < end; i++) { | 
4123  | 0  |             key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;  | 
4124  | 0  |             Py_INCREF(key);  | 
4125  | 0  |             PyTuple_SET_ITEM(keys, i - begin, key);  | 
4126  | 0  |         }  | 
4127  | 0  |         ADDOP_LOAD_CONST_NEW(c, keys);  | 
4128  | 0  |         ADDOP_I(c, BUILD_CONST_KEY_MAP, n);  | 
4129  | 0  |     }  | 
4130  | 0  |     else { | 
4131  |  |         /* a for loop only executes once */  | 
4132  | 0  |         for (i = begin; i < end; i++) { | 
4133  | 0  |             kw = asdl_seq_GET(keywords, i);  | 
4134  | 0  |             ADDOP_LOAD_CONST(c, kw->arg);  | 
4135  | 0  |             VISIT(c, expr, kw->value);  | 
4136  | 0  |         }  | 
4137  | 0  |         ADDOP_I(c, BUILD_MAP, n);  | 
4138  | 0  |     }  | 
4139  | 0  |     return 1;  | 
4140  | 0  | }  | 
4141  |  |  | 
4142  |  | /* shared code between compiler_call and compiler_class */  | 
4143  |  | static int  | 
4144  |  | compiler_call_helper(struct compiler *c,  | 
4145  |  |                      int n, /* Args already pushed */  | 
4146  |  |                      asdl_seq *args,  | 
4147  |  |                      asdl_seq *keywords)  | 
4148  | 32  | { | 
4149  | 32  |     Py_ssize_t i, nseen, nelts, nkwelts;  | 
4150  | 32  |     int mustdictunpack = 0;  | 
4151  |  |  | 
4152  |  |     /* the number of tuples and dictionaries on the stack */  | 
4153  | 32  |     Py_ssize_t nsubargs = 0, nsubkwargs = 0;  | 
4154  |  |  | 
4155  | 32  |     nelts = asdl_seq_LEN(args);  | 
4156  | 32  |     nkwelts = asdl_seq_LEN(keywords);  | 
4157  |  |  | 
4158  | 32  |     for (i = 0; i < nkwelts; i++) { | 
4159  | 0  |         keyword_ty kw = asdl_seq_GET(keywords, i);  | 
4160  | 0  |         if (kw->arg == NULL) { | 
4161  | 0  |             mustdictunpack = 1;  | 
4162  | 0  |             break;  | 
4163  | 0  |         }  | 
4164  | 0  |     }  | 
4165  |  |  | 
4166  | 32  |     nseen = n;  /* the number of positional arguments on the stack */  | 
4167  | 70  |     for (i = 0; i < nelts; i++) { | 
4168  | 38  |         expr_ty elt = asdl_seq_GET(args, i);  | 
4169  | 38  |         if (elt->kind == Starred_kind) { | 
4170  |  |             /* A star-arg. If we've seen positional arguments,  | 
4171  |  |                pack the positional arguments into a tuple. */  | 
4172  | 0  |             if (nseen) { | 
4173  | 0  |                 ADDOP_I(c, BUILD_TUPLE, nseen);  | 
4174  | 0  |                 nseen = 0;  | 
4175  | 0  |                 nsubargs++;  | 
4176  | 0  |             }  | 
4177  | 0  |             VISIT(c, expr, elt->v.Starred.value);  | 
4178  | 0  |             nsubargs++;  | 
4179  | 0  |         }  | 
4180  | 38  |         else { | 
4181  | 38  |             VISIT(c, expr, elt);  | 
4182  | 38  |             nseen++;  | 
4183  | 38  |         }  | 
4184  | 38  |     }  | 
4185  |  |  | 
4186  |  |     /* Same dance again for keyword arguments */  | 
4187  | 32  |     if (nsubargs || mustdictunpack) { | 
4188  | 0  |         if (nseen) { | 
4189  |  |             /* Pack up any trailing positional arguments. */  | 
4190  | 0  |             ADDOP_I(c, BUILD_TUPLE, nseen);  | 
4191  | 0  |             nsubargs++;  | 
4192  | 0  |         }  | 
4193  | 0  |         if (nsubargs > 1) { | 
4194  |  |             /* If we ended up with more than one stararg, we need  | 
4195  |  |                to concatenate them into a single sequence. */  | 
4196  | 0  |             ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs);  | 
4197  | 0  |         }  | 
4198  | 0  |         else if (nsubargs == 0) { | 
4199  | 0  |             ADDOP_I(c, BUILD_TUPLE, 0);  | 
4200  | 0  |         }  | 
4201  | 0  |         nseen = 0;  /* the number of keyword arguments on the stack following */  | 
4202  | 0  |         for (i = 0; i < nkwelts; i++) { | 
4203  | 0  |             keyword_ty kw = asdl_seq_GET(keywords, i);  | 
4204  | 0  |             if (kw->arg == NULL) { | 
4205  |  |                 /* A keyword argument unpacking. */  | 
4206  | 0  |                 if (nseen) { | 
4207  | 0  |                     if (!compiler_subkwargs(c, keywords, i - nseen, i))  | 
4208  | 0  |                         return 0;  | 
4209  | 0  |                     nsubkwargs++;  | 
4210  | 0  |                     nseen = 0;  | 
4211  | 0  |                 }  | 
4212  | 0  |                 VISIT(c, expr, kw->value);  | 
4213  | 0  |                 nsubkwargs++;  | 
4214  | 0  |             }  | 
4215  | 0  |             else { | 
4216  | 0  |                 nseen++;  | 
4217  | 0  |             }  | 
4218  | 0  |         }  | 
4219  | 0  |         if (nseen) { | 
4220  |  |             /* Pack up any trailing keyword arguments. */  | 
4221  | 0  |             if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts))  | 
4222  | 0  |                 return 0;  | 
4223  | 0  |             nsubkwargs++;  | 
4224  | 0  |         }  | 
4225  | 0  |         if (nsubkwargs > 1) { | 
4226  |  |             /* Pack it all up */  | 
4227  | 0  |             ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs);  | 
4228  | 0  |         }  | 
4229  | 0  |         ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0);  | 
4230  | 0  |         return 1;  | 
4231  | 0  |     }  | 
4232  | 32  |     else if (nkwelts) { | 
4233  | 0  |         PyObject *names;  | 
4234  | 0  |         VISIT_SEQ(c, keyword, keywords);  | 
4235  | 0  |         names = PyTuple_New(nkwelts);  | 
4236  | 0  |         if (names == NULL) { | 
4237  | 0  |             return 0;  | 
4238  | 0  |         }  | 
4239  | 0  |         for (i = 0; i < nkwelts; i++) { | 
4240  | 0  |             keyword_ty kw = asdl_seq_GET(keywords, i);  | 
4241  | 0  |             Py_INCREF(kw->arg);  | 
4242  | 0  |             PyTuple_SET_ITEM(names, i, kw->arg);  | 
4243  | 0  |         }  | 
4244  | 0  |         ADDOP_LOAD_CONST_NEW(c, names);  | 
4245  | 0  |         ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);  | 
4246  | 0  |         return 1;  | 
4247  | 0  |     }  | 
4248  | 32  |     else { | 
4249  | 32  |         ADDOP_I(c, CALL_FUNCTION, n + nelts);  | 
4250  | 32  |         return 1;  | 
4251  | 32  |     }  | 
4252  | 32  | }  | 
4253  |  |  | 
4254  |  |  | 
4255  |  | /* List and set comprehensions and generator expressions work by creating a  | 
4256  |  |   nested function to perform the actual iteration. This means that the  | 
4257  |  |   iteration variables don't leak into the current scope.  | 
4258  |  |   The defined function is called immediately following its definition, with the  | 
4259  |  |   result of that call being the result of the expression.  | 
4260  |  |   The LC/SC version returns the populated container, while the GE version is  | 
4261  |  |   flagged in symtable.c as a generator, so it returns the generator object  | 
4262  |  |   when the function is called.  | 
4263  |  |  | 
4264  |  |   Possible cleanups:  | 
4265  |  |     - iterate over the generator sequence instead of using recursion  | 
4266  |  | */  | 
4267  |  |  | 
4268  |  |  | 
4269  |  | static int  | 
4270  |  | compiler_comprehension_generator(struct compiler *c,  | 
4271  |  |                                  asdl_seq *generators, int gen_index,  | 
4272  |  |                                  expr_ty elt, expr_ty val, int type)  | 
4273  | 0  | { | 
4274  | 0  |     comprehension_ty gen;  | 
4275  | 0  |     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);  | 
4276  | 0  |     if (gen->is_async) { | 
4277  | 0  |         return compiler_async_comprehension_generator(  | 
4278  | 0  |             c, generators, gen_index, elt, val, type);  | 
4279  | 0  |     } else { | 
4280  | 0  |         return compiler_sync_comprehension_generator(  | 
4281  | 0  |             c, generators, gen_index, elt, val, type);  | 
4282  | 0  |     }  | 
4283  | 0  | }  | 
4284  |  |  | 
4285  |  | static int  | 
4286  |  | compiler_sync_comprehension_generator(struct compiler *c,  | 
4287  |  |                                       asdl_seq *generators, int gen_index,  | 
4288  |  |                                       expr_ty elt, expr_ty val, int type)  | 
4289  | 0  | { | 
4290  |  |     /* generate code for the iterator, then each of the ifs,  | 
4291  |  |        and then write to the element */  | 
4292  |  | 
  | 
4293  | 0  |     comprehension_ty gen;  | 
4294  | 0  |     basicblock *start, *anchor, *skip, *if_cleanup;  | 
4295  | 0  |     Py_ssize_t i, n;  | 
4296  |  | 
  | 
4297  | 0  |     start = compiler_new_block(c);  | 
4298  | 0  |     skip = compiler_new_block(c);  | 
4299  | 0  |     if_cleanup = compiler_new_block(c);  | 
4300  | 0  |     anchor = compiler_new_block(c);  | 
4301  |  | 
  | 
4302  | 0  |     if (start == NULL || skip == NULL || if_cleanup == NULL ||  | 
4303  | 0  |         anchor == NULL)  | 
4304  | 0  |         return 0;  | 
4305  |  |  | 
4306  | 0  |     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);  | 
4307  |  | 
  | 
4308  | 0  |     if (gen_index == 0) { | 
4309  |  |         /* Receive outermost iter as an implicit argument */  | 
4310  | 0  |         c->u->u_argcount = 1;  | 
4311  | 0  |         ADDOP_I(c, LOAD_FAST, 0);  | 
4312  | 0  |     }  | 
4313  | 0  |     else { | 
4314  |  |         /* Sub-iter - calculate on the fly */  | 
4315  | 0  |         VISIT(c, expr, gen->iter);  | 
4316  | 0  |         ADDOP(c, GET_ITER);  | 
4317  | 0  |     }  | 
4318  | 0  |     compiler_use_next_block(c, start);  | 
4319  | 0  |     ADDOP_JREL(c, FOR_ITER, anchor);  | 
4320  | 0  |     NEXT_BLOCK(c);  | 
4321  | 0  |     VISIT(c, expr, gen->target);  | 
4322  |  |  | 
4323  |  |     /* XXX this needs to be cleaned up...a lot! */  | 
4324  | 0  |     n = asdl_seq_LEN(gen->ifs);  | 
4325  | 0  |     for (i = 0; i < n; i++) { | 
4326  | 0  |         expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);  | 
4327  | 0  |         if (!compiler_jump_if(c, e, if_cleanup, 0))  | 
4328  | 0  |             return 0;  | 
4329  | 0  |         NEXT_BLOCK(c);  | 
4330  | 0  |     }  | 
4331  |  |  | 
4332  | 0  |     if (++gen_index < asdl_seq_LEN(generators))  | 
4333  | 0  |         if (!compiler_comprehension_generator(c,  | 
4334  | 0  |                                               generators, gen_index,  | 
4335  | 0  |                                               elt, val, type))  | 
4336  | 0  |         return 0;  | 
4337  |  |  | 
4338  |  |     /* only append after the last for generator */  | 
4339  | 0  |     if (gen_index >= asdl_seq_LEN(generators)) { | 
4340  |  |         /* comprehension specific code */  | 
4341  | 0  |         switch (type) { | 
4342  | 0  |         case COMP_GENEXP:  | 
4343  | 0  |             VISIT(c, expr, elt);  | 
4344  | 0  |             ADDOP(c, YIELD_VALUE);  | 
4345  | 0  |             ADDOP(c, POP_TOP);  | 
4346  | 0  |             break;  | 
4347  | 0  |         case COMP_LISTCOMP:  | 
4348  | 0  |             VISIT(c, expr, elt);  | 
4349  | 0  |             ADDOP_I(c, LIST_APPEND, gen_index + 1);  | 
4350  | 0  |             break;  | 
4351  | 0  |         case COMP_SETCOMP:  | 
4352  | 0  |             VISIT(c, expr, elt);  | 
4353  | 0  |             ADDOP_I(c, SET_ADD, gen_index + 1);  | 
4354  | 0  |             break;  | 
4355  | 0  |         case COMP_DICTCOMP:  | 
4356  |  |             /* With '{k: v}', k is evaluated before v, so we do | 
4357  |  |                the same. */  | 
4358  | 0  |             VISIT(c, expr, elt);  | 
4359  | 0  |             VISIT(c, expr, val);  | 
4360  | 0  |             ADDOP_I(c, MAP_ADD, gen_index + 1);  | 
4361  | 0  |             break;  | 
4362  | 0  |         default:  | 
4363  | 0  |             return 0;  | 
4364  | 0  |         }  | 
4365  |  |  | 
4366  | 0  |         compiler_use_next_block(c, skip);  | 
4367  | 0  |     }  | 
4368  | 0  |     compiler_use_next_block(c, if_cleanup);  | 
4369  | 0  |     ADDOP_JABS(c, JUMP_ABSOLUTE, start);  | 
4370  | 0  |     compiler_use_next_block(c, anchor);  | 
4371  |  | 
  | 
4372  | 0  |     return 1;  | 
4373  | 0  | }  | 
4374  |  |  | 
4375  |  | static int  | 
4376  |  | compiler_async_comprehension_generator(struct compiler *c,  | 
4377  |  |                                       asdl_seq *generators, int gen_index,  | 
4378  |  |                                       expr_ty elt, expr_ty val, int type)  | 
4379  | 0  | { | 
4380  | 0  |     comprehension_ty gen;  | 
4381  | 0  |     basicblock *start, *if_cleanup, *except;  | 
4382  | 0  |     Py_ssize_t i, n;  | 
4383  | 0  |     start = compiler_new_block(c);  | 
4384  | 0  |     except = compiler_new_block(c);  | 
4385  | 0  |     if_cleanup = compiler_new_block(c);  | 
4386  |  | 
  | 
4387  | 0  |     if (start == NULL || if_cleanup == NULL || except == NULL) { | 
4388  | 0  |         return 0;  | 
4389  | 0  |     }  | 
4390  |  |  | 
4391  | 0  |     gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);  | 
4392  |  | 
  | 
4393  | 0  |     if (gen_index == 0) { | 
4394  |  |         /* Receive outermost iter as an implicit argument */  | 
4395  | 0  |         c->u->u_argcount = 1;  | 
4396  | 0  |         ADDOP_I(c, LOAD_FAST, 0);  | 
4397  | 0  |     }  | 
4398  | 0  |     else { | 
4399  |  |         /* Sub-iter - calculate on the fly */  | 
4400  | 0  |         VISIT(c, expr, gen->iter);  | 
4401  | 0  |         ADDOP(c, GET_AITER);  | 
4402  | 0  |     }  | 
4403  |  |  | 
4404  | 0  |     compiler_use_next_block(c, start);  | 
4405  |  | 
  | 
4406  | 0  |     ADDOP_JREL(c, SETUP_FINALLY, except);  | 
4407  | 0  |     ADDOP(c, GET_ANEXT);  | 
4408  | 0  |     ADDOP_LOAD_CONST(c, Py_None);  | 
4409  | 0  |     ADDOP(c, YIELD_FROM);  | 
4410  | 0  |     ADDOP(c, POP_BLOCK);  | 
4411  | 0  |     VISIT(c, expr, gen->target);  | 
4412  |  | 
  | 
4413  | 0  |     n = asdl_seq_LEN(gen->ifs);  | 
4414  | 0  |     for (i = 0; i < n; i++) { | 
4415  | 0  |         expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);  | 
4416  | 0  |         if (!compiler_jump_if(c, e, if_cleanup, 0))  | 
4417  | 0  |             return 0;  | 
4418  | 0  |         NEXT_BLOCK(c);  | 
4419  | 0  |     }  | 
4420  |  |  | 
4421  | 0  |     if (++gen_index < asdl_seq_LEN(generators))  | 
4422  | 0  |         if (!compiler_comprehension_generator(c,  | 
4423  | 0  |                                               generators, gen_index,  | 
4424  | 0  |                                               elt, val, type))  | 
4425  | 0  |         return 0;  | 
4426  |  |  | 
4427  |  |     /* only append after the last for generator */  | 
4428  | 0  |     if (gen_index >= asdl_seq_LEN(generators)) { | 
4429  |  |         /* comprehension specific code */  | 
4430  | 0  |         switch (type) { | 
4431  | 0  |         case COMP_GENEXP:  | 
4432  | 0  |             VISIT(c, expr, elt);  | 
4433  | 0  |             ADDOP(c, YIELD_VALUE);  | 
4434  | 0  |             ADDOP(c, POP_TOP);  | 
4435  | 0  |             break;  | 
4436  | 0  |         case COMP_LISTCOMP:  | 
4437  | 0  |             VISIT(c, expr, elt);  | 
4438  | 0  |             ADDOP_I(c, LIST_APPEND, gen_index + 1);  | 
4439  | 0  |             break;  | 
4440  | 0  |         case COMP_SETCOMP:  | 
4441  | 0  |             VISIT(c, expr, elt);  | 
4442  | 0  |             ADDOP_I(c, SET_ADD, gen_index + 1);  | 
4443  | 0  |             break;  | 
4444  | 0  |         case COMP_DICTCOMP:  | 
4445  |  |             /* With '{k: v}', k is evaluated before v, so we do | 
4446  |  |                the same. */  | 
4447  | 0  |             VISIT(c, expr, elt);  | 
4448  | 0  |             VISIT(c, expr, val);  | 
4449  | 0  |             ADDOP_I(c, MAP_ADD, gen_index + 1);  | 
4450  | 0  |             break;  | 
4451  | 0  |         default:  | 
4452  | 0  |             return 0;  | 
4453  | 0  |         }  | 
4454  | 0  |     }  | 
4455  | 0  |     compiler_use_next_block(c, if_cleanup);  | 
4456  | 0  |     ADDOP_JABS(c, JUMP_ABSOLUTE, start);  | 
4457  |  | 
  | 
4458  | 0  |     compiler_use_next_block(c, except);  | 
4459  | 0  |     ADDOP(c, END_ASYNC_FOR);  | 
4460  |  | 
  | 
4461  | 0  |     return 1;  | 
4462  | 0  | }  | 
4463  |  |  | 
4464  |  | static int  | 
4465  |  | compiler_comprehension(struct compiler *c, expr_ty e, int type,  | 
4466  |  |                        identifier name, asdl_seq *generators, expr_ty elt,  | 
4467  |  |                        expr_ty val)  | 
4468  | 0  | { | 
4469  | 0  |     PyCodeObject *co = NULL;  | 
4470  | 0  |     comprehension_ty outermost;  | 
4471  | 0  |     PyObject *qualname = NULL;  | 
4472  | 0  |     int is_async_generator = 0;  | 
4473  |  | 
  | 
4474  | 0  |     if (IS_TOP_LEVEL_AWAIT(c)) { | 
4475  | 0  |         c->u->u_ste->ste_coroutine = 1;  | 
4476  | 0  |     }  | 
4477  | 0  |     int is_async_function = c->u->u_ste->ste_coroutine;  | 
4478  |  | 
  | 
4479  | 0  |     outermost = (comprehension_ty) asdl_seq_GET(generators, 0);  | 
4480  | 0  |     if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,  | 
4481  | 0  |                               (void *)e, e->lineno))  | 
4482  | 0  |     { | 
4483  | 0  |         goto error;  | 
4484  | 0  |     }  | 
4485  |  |  | 
4486  | 0  |     is_async_generator = c->u->u_ste->ste_coroutine;  | 
4487  |  | 
  | 
4488  | 0  |     if (is_async_generator && !is_async_function && type != COMP_GENEXP) { | 
4489  | 0  |         compiler_error(c, "asynchronous comprehension outside of "  | 
4490  | 0  |                           "an asynchronous function");  | 
4491  | 0  |         goto error_in_scope;  | 
4492  | 0  |     }  | 
4493  |  |  | 
4494  | 0  |     if (type != COMP_GENEXP) { | 
4495  | 0  |         int op;  | 
4496  | 0  |         switch (type) { | 
4497  | 0  |         case COMP_LISTCOMP:  | 
4498  | 0  |             op = BUILD_LIST;  | 
4499  | 0  |             break;  | 
4500  | 0  |         case COMP_SETCOMP:  | 
4501  | 0  |             op = BUILD_SET;  | 
4502  | 0  |             break;  | 
4503  | 0  |         case COMP_DICTCOMP:  | 
4504  | 0  |             op = BUILD_MAP;  | 
4505  | 0  |             break;  | 
4506  | 0  |         default:  | 
4507  | 0  |             PyErr_Format(PyExc_SystemError,  | 
4508  | 0  |                          "unknown comprehension type %d", type);  | 
4509  | 0  |             goto error_in_scope;  | 
4510  | 0  |         }  | 
4511  |  |  | 
4512  | 0  |         ADDOP_I(c, op, 0);  | 
4513  | 0  |     }  | 
4514  |  |  | 
4515  | 0  |     if (!compiler_comprehension_generator(c, generators, 0, elt,  | 
4516  | 0  |                                           val, type))  | 
4517  | 0  |         goto error_in_scope;  | 
4518  |  |  | 
4519  | 0  |     if (type != COMP_GENEXP) { | 
4520  | 0  |         ADDOP(c, RETURN_VALUE);  | 
4521  | 0  |     }  | 
4522  |  |  | 
4523  | 0  |     co = assemble(c, 1);  | 
4524  | 0  |     qualname = c->u->u_qualname;  | 
4525  | 0  |     Py_INCREF(qualname);  | 
4526  | 0  |     compiler_exit_scope(c);  | 
4527  | 0  |     if (co == NULL)  | 
4528  | 0  |         goto error;  | 
4529  |  |  | 
4530  | 0  |     if (!compiler_make_closure(c, co, 0, qualname))  | 
4531  | 0  |         goto error;  | 
4532  | 0  |     Py_DECREF(qualname);  | 
4533  | 0  |     Py_DECREF(co);  | 
4534  |  | 
  | 
4535  | 0  |     VISIT(c, expr, outermost->iter);  | 
4536  |  | 
  | 
4537  | 0  |     if (outermost->is_async) { | 
4538  | 0  |         ADDOP(c, GET_AITER);  | 
4539  | 0  |     } else { | 
4540  | 0  |         ADDOP(c, GET_ITER);  | 
4541  | 0  |     }  | 
4542  |  |  | 
4543  | 0  |     ADDOP_I(c, CALL_FUNCTION, 1);  | 
4544  |  | 
  | 
4545  | 0  |     if (is_async_generator && type != COMP_GENEXP) { | 
4546  | 0  |         ADDOP(c, GET_AWAITABLE);  | 
4547  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
4548  | 0  |         ADDOP(c, YIELD_FROM);  | 
4549  | 0  |     }  | 
4550  |  |  | 
4551  | 0  |     return 1;  | 
4552  | 0  | error_in_scope:  | 
4553  | 0  |     compiler_exit_scope(c);  | 
4554  | 0  | error:  | 
4555  | 0  |     Py_XDECREF(qualname);  | 
4556  | 0  |     Py_XDECREF(co);  | 
4557  | 0  |     return 0;  | 
4558  | 0  | }  | 
4559  |  |  | 
4560  |  | static int  | 
4561  |  | compiler_genexp(struct compiler *c, expr_ty e)  | 
4562  | 0  | { | 
4563  | 0  |     static identifier name;  | 
4564  | 0  |     if (!name) { | 
4565  | 0  |         name = PyUnicode_InternFromString("<genexpr>"); | 
4566  | 0  |         if (!name)  | 
4567  | 0  |             return 0;  | 
4568  | 0  |     }  | 
4569  | 0  |     assert(e->kind == GeneratorExp_kind);  | 
4570  | 0  |     return compiler_comprehension(c, e, COMP_GENEXP, name,  | 
4571  | 0  |                                   e->v.GeneratorExp.generators,  | 
4572  | 0  |                                   e->v.GeneratorExp.elt, NULL);  | 
4573  | 0  | }  | 
4574  |  |  | 
4575  |  | static int  | 
4576  |  | compiler_listcomp(struct compiler *c, expr_ty e)  | 
4577  | 0  | { | 
4578  | 0  |     static identifier name;  | 
4579  | 0  |     if (!name) { | 
4580  | 0  |         name = PyUnicode_InternFromString("<listcomp>"); | 
4581  | 0  |         if (!name)  | 
4582  | 0  |             return 0;  | 
4583  | 0  |     }  | 
4584  | 0  |     assert(e->kind == ListComp_kind);  | 
4585  | 0  |     return compiler_comprehension(c, e, COMP_LISTCOMP, name,  | 
4586  | 0  |                                   e->v.ListComp.generators,  | 
4587  | 0  |                                   e->v.ListComp.elt, NULL);  | 
4588  | 0  | }  | 
4589  |  |  | 
4590  |  | static int  | 
4591  |  | compiler_setcomp(struct compiler *c, expr_ty e)  | 
4592  | 0  | { | 
4593  | 0  |     static identifier name;  | 
4594  | 0  |     if (!name) { | 
4595  | 0  |         name = PyUnicode_InternFromString("<setcomp>"); | 
4596  | 0  |         if (!name)  | 
4597  | 0  |             return 0;  | 
4598  | 0  |     }  | 
4599  | 0  |     assert(e->kind == SetComp_kind);  | 
4600  | 0  |     return compiler_comprehension(c, e, COMP_SETCOMP, name,  | 
4601  | 0  |                                   e->v.SetComp.generators,  | 
4602  | 0  |                                   e->v.SetComp.elt, NULL);  | 
4603  | 0  | }  | 
4604  |  |  | 
4605  |  |  | 
4606  |  | static int  | 
4607  |  | compiler_dictcomp(struct compiler *c, expr_ty e)  | 
4608  | 0  | { | 
4609  | 0  |     static identifier name;  | 
4610  | 0  |     if (!name) { | 
4611  | 0  |         name = PyUnicode_InternFromString("<dictcomp>"); | 
4612  | 0  |         if (!name)  | 
4613  | 0  |             return 0;  | 
4614  | 0  |     }  | 
4615  | 0  |     assert(e->kind == DictComp_kind);  | 
4616  | 0  |     return compiler_comprehension(c, e, COMP_DICTCOMP, name,  | 
4617  | 0  |                                   e->v.DictComp.generators,  | 
4618  | 0  |                                   e->v.DictComp.key, e->v.DictComp.value);  | 
4619  | 0  | }  | 
4620  |  |  | 
4621  |  |  | 
4622  |  | static int  | 
4623  |  | compiler_visit_keyword(struct compiler *c, keyword_ty k)  | 
4624  | 0  | { | 
4625  | 0  |     VISIT(c, expr, k->value);  | 
4626  | 0  |     return 1;  | 
4627  | 0  | }  | 
4628  |  |  | 
4629  |  | /* Test whether expression is constant.  For constants, report  | 
4630  |  |    whether they are true or false.  | 
4631  |  |  | 
4632  |  |    Return values: 1 for true, 0 for false, -1 for non-constant.  | 
4633  |  |  */  | 
4634  |  |  | 
4635  |  | static int  | 
4636  |  | expr_constant(expr_ty e)  | 
4637  | 8  | { | 
4638  | 8  |     if (e->kind == Constant_kind) { | 
4639  | 0  |         return PyObject_IsTrue(e->v.Constant.value);  | 
4640  | 0  |     }  | 
4641  | 8  |     return -1;  | 
4642  | 8  | }  | 
4643  |  |  | 
4644  |  |  | 
4645  |  | /*  | 
4646  |  |    Implements the async with statement.  | 
4647  |  |  | 
4648  |  |    The semantics outlined in that PEP are as follows:  | 
4649  |  |  | 
4650  |  |    async with EXPR as VAR:  | 
4651  |  |        BLOCK  | 
4652  |  |  | 
4653  |  |    It is implemented roughly as:  | 
4654  |  |  | 
4655  |  |    context = EXPR  | 
4656  |  |    exit = context.__aexit__  # not calling it  | 
4657  |  |    value = await context.__aenter__()  | 
4658  |  |    try:  | 
4659  |  |        VAR = value  # if VAR present in the syntax  | 
4660  |  |        BLOCK  | 
4661  |  |    finally:  | 
4662  |  |        if an exception was raised:  | 
4663  |  |            exc = copy of (exception, instance, traceback)  | 
4664  |  |        else:  | 
4665  |  |            exc = (None, None, None)  | 
4666  |  |        if not (await exit(*exc)):  | 
4667  |  |            raise  | 
4668  |  |  */  | 
4669  |  | static int  | 
4670  |  | compiler_async_with(struct compiler *c, stmt_ty s, int pos)  | 
4671  | 0  | { | 
4672  | 0  |     basicblock *block, *finally;  | 
4673  | 0  |     withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);  | 
4674  |  | 
  | 
4675  | 0  |     assert(s->kind == AsyncWith_kind);  | 
4676  | 0  |     if (IS_TOP_LEVEL_AWAIT(c)){ | 
4677  | 0  |         c->u->u_ste->ste_coroutine = 1;  | 
4678  | 0  |     } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ | 
4679  | 0  |         return compiler_error(c, "'async with' outside async function");  | 
4680  | 0  |     }  | 
4681  |  |  | 
4682  | 0  |     block = compiler_new_block(c);  | 
4683  | 0  |     finally = compiler_new_block(c);  | 
4684  | 0  |     if (!block || !finally)  | 
4685  | 0  |         return 0;  | 
4686  |  |  | 
4687  |  |     /* Evaluate EXPR */  | 
4688  | 0  |     VISIT(c, expr, item->context_expr);  | 
4689  |  | 
  | 
4690  | 0  |     ADDOP(c, BEFORE_ASYNC_WITH);  | 
4691  | 0  |     ADDOP(c, GET_AWAITABLE);  | 
4692  | 0  |     ADDOP_LOAD_CONST(c, Py_None);  | 
4693  | 0  |     ADDOP(c, YIELD_FROM);  | 
4694  |  | 
  | 
4695  | 0  |     ADDOP_JREL(c, SETUP_ASYNC_WITH, finally);  | 
4696  |  |  | 
4697  |  |     /* SETUP_ASYNC_WITH pushes a finally block. */  | 
4698  | 0  |     compiler_use_next_block(c, block);  | 
4699  | 0  |     if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { | 
4700  | 0  |         return 0;  | 
4701  | 0  |     }  | 
4702  |  |  | 
4703  | 0  |     if (item->optional_vars) { | 
4704  | 0  |         VISIT(c, expr, item->optional_vars);  | 
4705  | 0  |     }  | 
4706  | 0  |     else { | 
4707  |  |     /* Discard result from context.__aenter__() */  | 
4708  | 0  |         ADDOP(c, POP_TOP);  | 
4709  | 0  |     }  | 
4710  |  |  | 
4711  | 0  |     pos++;  | 
4712  | 0  |     if (pos == asdl_seq_LEN(s->v.AsyncWith.items))  | 
4713  |  |         /* BLOCK code */  | 
4714  | 0  |         VISIT_SEQ(c, stmt, s->v.AsyncWith.body)  | 
4715  | 0  |     else if (!compiler_async_with(c, s, pos))  | 
4716  | 0  |             return 0;  | 
4717  |  |  | 
4718  |  |     /* End of try block; start the finally block */  | 
4719  | 0  |     ADDOP(c, POP_BLOCK);  | 
4720  | 0  |     ADDOP(c, BEGIN_FINALLY);  | 
4721  | 0  |     compiler_pop_fblock(c, ASYNC_WITH, block);  | 
4722  |  | 
  | 
4723  | 0  |     compiler_use_next_block(c, finally);  | 
4724  | 0  |     if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))  | 
4725  | 0  |         return 0;  | 
4726  |  |  | 
4727  |  |     /* Finally block starts; context.__exit__ is on the stack under  | 
4728  |  |        the exception or return information. Just issue our magic  | 
4729  |  |        opcode. */  | 
4730  | 0  |     ADDOP(c, WITH_CLEANUP_START);  | 
4731  |  | 
  | 
4732  | 0  |     ADDOP(c, GET_AWAITABLE);  | 
4733  | 0  |     ADDOP_LOAD_CONST(c, Py_None);  | 
4734  | 0  |     ADDOP(c, YIELD_FROM);  | 
4735  |  | 
  | 
4736  | 0  |     ADDOP(c, WITH_CLEANUP_FINISH);  | 
4737  |  |  | 
4738  |  |     /* Finally block ends. */  | 
4739  | 0  |     ADDOP(c, END_FINALLY);  | 
4740  | 0  |     compiler_pop_fblock(c, FINALLY_END, finally);  | 
4741  | 0  |     return 1;  | 
4742  | 0  | }  | 
4743  |  |  | 
4744  |  |  | 
4745  |  | /*  | 
4746  |  |    Implements the with statement from PEP 343.  | 
4747  |  |  | 
4748  |  |    The semantics outlined in that PEP are as follows:  | 
4749  |  |  | 
4750  |  |    with EXPR as VAR:  | 
4751  |  |        BLOCK  | 
4752  |  |  | 
4753  |  |    It is implemented roughly as:  | 
4754  |  |  | 
4755  |  |    context = EXPR  | 
4756  |  |    exit = context.__exit__  # not calling it  | 
4757  |  |    value = context.__enter__()  | 
4758  |  |    try:  | 
4759  |  |        VAR = value  # if VAR present in the syntax  | 
4760  |  |        BLOCK  | 
4761  |  |    finally:  | 
4762  |  |        if an exception was raised:  | 
4763  |  |            exc = copy of (exception, instance, traceback)  | 
4764  |  |        else:  | 
4765  |  |            exc = (None, None, None)  | 
4766  |  |        exit(*exc)  | 
4767  |  |  */  | 
4768  |  | static int  | 
4769  |  | compiler_with(struct compiler *c, stmt_ty s, int pos)  | 
4770  | 0  | { | 
4771  | 0  |     basicblock *block, *finally;  | 
4772  | 0  |     withitem_ty item = asdl_seq_GET(s->v.With.items, pos);  | 
4773  |  | 
  | 
4774  | 0  |     assert(s->kind == With_kind);  | 
4775  |  | 
  | 
4776  | 0  |     block = compiler_new_block(c);  | 
4777  | 0  |     finally = compiler_new_block(c);  | 
4778  | 0  |     if (!block || !finally)  | 
4779  | 0  |         return 0;  | 
4780  |  |  | 
4781  |  |     /* Evaluate EXPR */  | 
4782  | 0  |     VISIT(c, expr, item->context_expr);  | 
4783  | 0  |     ADDOP_JREL(c, SETUP_WITH, finally);  | 
4784  |  |  | 
4785  |  |     /* SETUP_WITH pushes a finally block. */  | 
4786  | 0  |     compiler_use_next_block(c, block);  | 
4787  | 0  |     if (!compiler_push_fblock(c, WITH, block, finally)) { | 
4788  | 0  |         return 0;  | 
4789  | 0  |     }  | 
4790  |  |  | 
4791  | 0  |     if (item->optional_vars) { | 
4792  | 0  |         VISIT(c, expr, item->optional_vars);  | 
4793  | 0  |     }  | 
4794  | 0  |     else { | 
4795  |  |     /* Discard result from context.__enter__() */  | 
4796  | 0  |         ADDOP(c, POP_TOP);  | 
4797  | 0  |     }  | 
4798  |  |  | 
4799  | 0  |     pos++;  | 
4800  | 0  |     if (pos == asdl_seq_LEN(s->v.With.items))  | 
4801  |  |         /* BLOCK code */  | 
4802  | 0  |         VISIT_SEQ(c, stmt, s->v.With.body)  | 
4803  | 0  |     else if (!compiler_with(c, s, pos))  | 
4804  | 0  |             return 0;  | 
4805  |  |  | 
4806  |  |     /* End of try block; start the finally block */  | 
4807  | 0  |     ADDOP(c, POP_BLOCK);  | 
4808  | 0  |     ADDOP(c, BEGIN_FINALLY);  | 
4809  | 0  |     compiler_pop_fblock(c, WITH, block);  | 
4810  |  | 
  | 
4811  | 0  |     compiler_use_next_block(c, finally);  | 
4812  | 0  |     if (!compiler_push_fblock(c, FINALLY_END, finally, NULL))  | 
4813  | 0  |         return 0;  | 
4814  |  |  | 
4815  |  |     /* Finally block starts; context.__exit__ is on the stack under  | 
4816  |  |        the exception or return information. Just issue our magic  | 
4817  |  |        opcode. */  | 
4818  | 0  |     ADDOP(c, WITH_CLEANUP_START);  | 
4819  | 0  |     ADDOP(c, WITH_CLEANUP_FINISH);  | 
4820  |  |  | 
4821  |  |     /* Finally block ends. */  | 
4822  | 0  |     ADDOP(c, END_FINALLY);  | 
4823  | 0  |     compiler_pop_fblock(c, FINALLY_END, finally);  | 
4824  | 0  |     return 1;  | 
4825  | 0  | }  | 
4826  |  |  | 
4827  |  | static int  | 
4828  |  | compiler_visit_expr1(struct compiler *c, expr_ty e)  | 
4829  | 531  | { | 
4830  | 531  |     switch (e->kind) { | 
4831  | 0  |     case NamedExpr_kind:  | 
4832  | 0  |         VISIT(c, expr, e->v.NamedExpr.value);  | 
4833  | 0  |         ADDOP(c, DUP_TOP);  | 
4834  | 0  |         VISIT(c, expr, e->v.NamedExpr.target);  | 
4835  | 0  |         break;  | 
4836  | 0  |     case BoolOp_kind:  | 
4837  | 0  |         return compiler_boolop(c, e);  | 
4838  | 12  |     case BinOp_kind:  | 
4839  | 12  |         VISIT(c, expr, e->v.BinOp.left);  | 
4840  | 12  |         VISIT(c, expr, e->v.BinOp.right);  | 
4841  | 12  |         ADDOP(c, binop(c, e->v.BinOp.op));  | 
4842  | 12  |         break;  | 
4843  | 0  |     case UnaryOp_kind:  | 
4844  | 0  |         VISIT(c, expr, e->v.UnaryOp.operand);  | 
4845  | 0  |         ADDOP(c, unaryop(e->v.UnaryOp.op));  | 
4846  | 0  |         break;  | 
4847  | 2  |     case Lambda_kind:  | 
4848  | 2  |         return compiler_lambda(c, e);  | 
4849  | 0  |     case IfExp_kind:  | 
4850  | 0  |         return compiler_ifexp(c, e);  | 
4851  | 6  |     case Dict_kind:  | 
4852  | 6  |         return compiler_dict(c, e);  | 
4853  | 0  |     case Set_kind:  | 
4854  | 0  |         return compiler_set(c, e);  | 
4855  | 0  |     case GeneratorExp_kind:  | 
4856  | 0  |         return compiler_genexp(c, e);  | 
4857  | 0  |     case ListComp_kind:  | 
4858  | 0  |         return compiler_listcomp(c, e);  | 
4859  | 0  |     case SetComp_kind:  | 
4860  | 0  |         return compiler_setcomp(c, e);  | 
4861  | 0  |     case DictComp_kind:  | 
4862  | 0  |         return compiler_dictcomp(c, e);  | 
4863  | 0  |     case Yield_kind:  | 
4864  | 0  |         if (c->u->u_ste->ste_type != FunctionBlock)  | 
4865  | 0  |             return compiler_error(c, "'yield' outside function");  | 
4866  | 0  |         if (e->v.Yield.value) { | 
4867  | 0  |             VISIT(c, expr, e->v.Yield.value);  | 
4868  | 0  |         }  | 
4869  | 0  |         else { | 
4870  | 0  |             ADDOP_LOAD_CONST(c, Py_None);  | 
4871  | 0  |         }  | 
4872  | 0  |         ADDOP(c, YIELD_VALUE);  | 
4873  | 0  |         break;  | 
4874  | 0  |     case YieldFrom_kind:  | 
4875  | 0  |         if (c->u->u_ste->ste_type != FunctionBlock)  | 
4876  | 0  |             return compiler_error(c, "'yield' outside function");  | 
4877  |  |  | 
4878  | 0  |         if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)  | 
4879  | 0  |             return compiler_error(c, "'yield from' inside async function");  | 
4880  |  |  | 
4881  | 0  |         VISIT(c, expr, e->v.YieldFrom.value);  | 
4882  | 0  |         ADDOP(c, GET_YIELD_FROM_ITER);  | 
4883  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
4884  | 0  |         ADDOP(c, YIELD_FROM);  | 
4885  | 0  |         break;  | 
4886  | 0  |     case Await_kind:  | 
4887  | 0  |         if (!IS_TOP_LEVEL_AWAIT(c)){ | 
4888  | 0  |             if (c->u->u_ste->ste_type != FunctionBlock){ | 
4889  | 0  |                 return compiler_error(c, "'await' outside function");  | 
4890  | 0  |             }  | 
4891  |  |  | 
4892  | 0  |             if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&  | 
4893  | 0  |                     c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ | 
4894  | 0  |                 return compiler_error(c, "'await' outside async function");  | 
4895  | 0  |             }  | 
4896  | 0  |         }  | 
4897  |  |  | 
4898  | 0  |         VISIT(c, expr, e->v.Await.value);  | 
4899  | 0  |         ADDOP(c, GET_AWAITABLE);  | 
4900  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
4901  | 0  |         ADDOP(c, YIELD_FROM);  | 
4902  | 0  |         break;  | 
4903  | 24  |     case Compare_kind:  | 
4904  | 24  |         return compiler_compare(c, e);  | 
4905  | 80  |     case Call_kind:  | 
4906  | 80  |         return compiler_call(c, e);  | 
4907  | 60  |     case Constant_kind:  | 
4908  | 60  |         ADDOP_LOAD_CONST(c, e->v.Constant.value);  | 
4909  | 60  |         break;  | 
4910  | 0  |     case JoinedStr_kind:  | 
4911  | 0  |         return compiler_joined_str(c, e);  | 
4912  | 0  |     case FormattedValue_kind:  | 
4913  | 0  |         return compiler_formatted_value(c, e);  | 
4914  |  |     /* The following exprs can be assignment targets. */  | 
4915  | 12  |     case Attribute_kind:  | 
4916  | 12  |         if (e->v.Attribute.ctx != AugStore)  | 
4917  | 12  |             VISIT(c, expr, e->v.Attribute.value);  | 
4918  | 12  |         switch (e->v.Attribute.ctx) { | 
4919  | 0  |         case AugLoad:  | 
4920  | 0  |             ADDOP(c, DUP_TOP);  | 
4921  |  |             /* Fall through */  | 
4922  | 12  |         case Load:  | 
4923  | 12  |             ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);  | 
4924  | 12  |             break;  | 
4925  | 0  |         case AugStore:  | 
4926  | 0  |             ADDOP(c, ROT_TWO);  | 
4927  |  |             /* Fall through */  | 
4928  | 0  |         case Store:  | 
4929  | 0  |             ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);  | 
4930  | 0  |             break;  | 
4931  | 0  |         case Del:  | 
4932  | 0  |             ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);  | 
4933  | 0  |             break;  | 
4934  | 0  |         case Param:  | 
4935  | 0  |         default:  | 
4936  | 0  |             PyErr_SetString(PyExc_SystemError,  | 
4937  | 0  |                             "param invalid in attribute expression");  | 
4938  | 0  |             return 0;  | 
4939  | 12  |         }  | 
4940  | 12  |         break;  | 
4941  | 14  |     case Subscript_kind:  | 
4942  | 14  |         switch (e->v.Subscript.ctx) { | 
4943  | 0  |         case AugLoad:  | 
4944  | 0  |             VISIT(c, expr, e->v.Subscript.value);  | 
4945  | 0  |             VISIT_SLICE(c, e->v.Subscript.slice, AugLoad);  | 
4946  | 0  |             break;  | 
4947  | 10  |         case Load:  | 
4948  | 10  |             if (!check_subscripter(c, e->v.Subscript.value)) { | 
4949  | 0  |                 return 0;  | 
4950  | 0  |             }  | 
4951  | 10  |             if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { | 
4952  | 0  |                 return 0;  | 
4953  | 0  |             }  | 
4954  | 10  |             VISIT(c, expr, e->v.Subscript.value);  | 
4955  | 10  |             VISIT_SLICE(c, e->v.Subscript.slice, Load);  | 
4956  | 10  |             break;  | 
4957  | 0  |         case AugStore:  | 
4958  | 0  |             VISIT_SLICE(c, e->v.Subscript.slice, AugStore);  | 
4959  | 0  |             break;  | 
4960  | 2  |         case Store:  | 
4961  | 2  |             VISIT(c, expr, e->v.Subscript.value);  | 
4962  | 2  |             VISIT_SLICE(c, e->v.Subscript.slice, Store);  | 
4963  | 2  |             break;  | 
4964  | 2  |         case Del:  | 
4965  | 2  |             VISIT(c, expr, e->v.Subscript.value);  | 
4966  | 2  |             VISIT_SLICE(c, e->v.Subscript.slice, Del);  | 
4967  | 2  |             break;  | 
4968  | 0  |         case Param:  | 
4969  | 0  |         default:  | 
4970  | 0  |             PyErr_SetString(PyExc_SystemError,  | 
4971  | 0  |                 "param invalid in subscript expression");  | 
4972  | 0  |             return 0;  | 
4973  | 14  |         }  | 
4974  | 14  |         break;  | 
4975  | 14  |     case Starred_kind:  | 
4976  | 0  |         switch (e->v.Starred.ctx) { | 
4977  | 0  |         case Store:  | 
4978  |  |             /* In all legitimate cases, the Starred node was already replaced  | 
4979  |  |              * by compiler_list/compiler_tuple. XXX: is that okay? */  | 
4980  | 0  |             return compiler_error(c,  | 
4981  | 0  |                 "starred assignment target must be in a list or tuple");  | 
4982  | 0  |         default:  | 
4983  | 0  |             return compiler_error(c,  | 
4984  | 0  |                 "can't use starred expression here");  | 
4985  | 0  |         }  | 
4986  | 295  |     case Name_kind:  | 
4987  | 295  |         return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);  | 
4988  |  |     /* child nodes of List and Tuple will have expr_context set */  | 
4989  | 4  |     case List_kind:  | 
4990  | 4  |         return compiler_list(c, e);  | 
4991  | 22  |     case Tuple_kind:  | 
4992  | 22  |         return compiler_tuple(c, e);  | 
4993  | 531  |     }  | 
4994  | 98  |     return 1;  | 
4995  | 531  | }  | 
4996  |  |  | 
4997  |  | static int  | 
4998  |  | compiler_visit_expr(struct compiler *c, expr_ty e)  | 
4999  | 531  | { | 
5000  |  |     /* If expr e has a different line number than the last expr/stmt,  | 
5001  |  |        set a new line number for the next instruction.  | 
5002  |  |     */  | 
5003  | 531  |     int old_lineno = c->u->u_lineno;  | 
5004  | 531  |     int old_col_offset = c->u->u_col_offset;  | 
5005  | 531  |     if (e->lineno != c->u->u_lineno) { | 
5006  | 14  |         c->u->u_lineno = e->lineno;  | 
5007  | 14  |         c->u->u_lineno_set = 0;  | 
5008  | 14  |     }  | 
5009  |  |     /* Updating the column offset is always harmless. */  | 
5010  | 531  |     c->u->u_col_offset = e->col_offset;  | 
5011  |  |  | 
5012  | 531  |     int res = compiler_visit_expr1(c, e);  | 
5013  |  |  | 
5014  | 531  |     if (old_lineno != c->u->u_lineno) { | 
5015  | 14  |         c->u->u_lineno = old_lineno;  | 
5016  | 14  |         c->u->u_lineno_set = 0;  | 
5017  | 14  |     }  | 
5018  | 531  |     c->u->u_col_offset = old_col_offset;  | 
5019  | 531  |     return res;  | 
5020  | 531  | }  | 
5021  |  |  | 
5022  |  | static int  | 
5023  |  | compiler_augassign(struct compiler *c, stmt_ty s)  | 
5024  | 0  | { | 
5025  | 0  |     expr_ty e = s->v.AugAssign.target;  | 
5026  | 0  |     expr_ty auge;  | 
5027  |  | 
  | 
5028  | 0  |     assert(s->kind == AugAssign_kind);  | 
5029  |  | 
  | 
5030  | 0  |     switch (e->kind) { | 
5031  | 0  |     case Attribute_kind:  | 
5032  | 0  |         auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr,  | 
5033  | 0  |                          AugLoad, e->lineno, e->col_offset,  | 
5034  | 0  |                          e->end_lineno, e->end_col_offset, c->c_arena);  | 
5035  | 0  |         if (auge == NULL)  | 
5036  | 0  |             return 0;  | 
5037  | 0  |         VISIT(c, expr, auge);  | 
5038  | 0  |         VISIT(c, expr, s->v.AugAssign.value);  | 
5039  | 0  |         ADDOP(c, inplace_binop(c, s->v.AugAssign.op));  | 
5040  | 0  |         auge->v.Attribute.ctx = AugStore;  | 
5041  | 0  |         VISIT(c, expr, auge);  | 
5042  | 0  |         break;  | 
5043  | 0  |     case Subscript_kind:  | 
5044  | 0  |         auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice,  | 
5045  | 0  |                          AugLoad, e->lineno, e->col_offset,  | 
5046  | 0  |                          e->end_lineno, e->end_col_offset, c->c_arena);  | 
5047  | 0  |         if (auge == NULL)  | 
5048  | 0  |             return 0;  | 
5049  | 0  |         VISIT(c, expr, auge);  | 
5050  | 0  |         VISIT(c, expr, s->v.AugAssign.value);  | 
5051  | 0  |         ADDOP(c, inplace_binop(c, s->v.AugAssign.op));  | 
5052  | 0  |         auge->v.Subscript.ctx = AugStore;  | 
5053  | 0  |         VISIT(c, expr, auge);  | 
5054  | 0  |         break;  | 
5055  | 0  |     case Name_kind:  | 
5056  | 0  |         if (!compiler_nameop(c, e->v.Name.id, Load))  | 
5057  | 0  |             return 0;  | 
5058  | 0  |         VISIT(c, expr, s->v.AugAssign.value);  | 
5059  | 0  |         ADDOP(c, inplace_binop(c, s->v.AugAssign.op));  | 
5060  | 0  |         return compiler_nameop(c, e->v.Name.id, Store);  | 
5061  | 0  |     default:  | 
5062  | 0  |         PyErr_Format(PyExc_SystemError,  | 
5063  | 0  |             "invalid node type (%d) for augmented assignment",  | 
5064  | 0  |             e->kind);  | 
5065  | 0  |         return 0;  | 
5066  | 0  |     }  | 
5067  | 0  |     return 1;  | 
5068  | 0  | }  | 
5069  |  |  | 
5070  |  | static int  | 
5071  |  | check_ann_expr(struct compiler *c, expr_ty e)  | 
5072  | 0  | { | 
5073  | 0  |     VISIT(c, expr, e);  | 
5074  | 0  |     ADDOP(c, POP_TOP);  | 
5075  | 0  |     return 1;  | 
5076  | 0  | }  | 
5077  |  |  | 
5078  |  | static int  | 
5079  |  | check_annotation(struct compiler *c, stmt_ty s)  | 
5080  | 0  | { | 
5081  |  |     /* Annotations are only evaluated in a module or class. */  | 
5082  | 0  |     if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||  | 
5083  | 0  |         c->u->u_scope_type == COMPILER_SCOPE_CLASS) { | 
5084  | 0  |         return check_ann_expr(c, s->v.AnnAssign.annotation);  | 
5085  | 0  |     }  | 
5086  | 0  |     return 1;  | 
5087  | 0  | }  | 
5088  |  |  | 
5089  |  | static int  | 
5090  |  | check_ann_slice(struct compiler *c, slice_ty sl)  | 
5091  | 0  | { | 
5092  | 0  |     switch(sl->kind) { | 
5093  | 0  |     case Index_kind:  | 
5094  | 0  |         return check_ann_expr(c, sl->v.Index.value);  | 
5095  | 0  |     case Slice_kind:  | 
5096  | 0  |         if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { | 
5097  | 0  |             return 0;  | 
5098  | 0  |         }  | 
5099  | 0  |         if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { | 
5100  | 0  |             return 0;  | 
5101  | 0  |         }  | 
5102  | 0  |         if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { | 
5103  | 0  |             return 0;  | 
5104  | 0  |         }  | 
5105  | 0  |         break;  | 
5106  | 0  |     default:  | 
5107  | 0  |         PyErr_SetString(PyExc_SystemError,  | 
5108  | 0  |                         "unexpected slice kind");  | 
5109  | 0  |         return 0;  | 
5110  | 0  |     }  | 
5111  | 0  |     return 1;  | 
5112  | 0  | }  | 
5113  |  |  | 
5114  |  | static int  | 
5115  |  | check_ann_subscr(struct compiler *c, slice_ty sl)  | 
5116  | 0  | { | 
5117  |  |     /* We check that everything in a subscript is defined at runtime. */  | 
5118  | 0  |     Py_ssize_t i, n;  | 
5119  |  | 
  | 
5120  | 0  |     switch (sl->kind) { | 
5121  | 0  |     case Index_kind:  | 
5122  | 0  |     case Slice_kind:  | 
5123  | 0  |         if (!check_ann_slice(c, sl)) { | 
5124  | 0  |             return 0;  | 
5125  | 0  |         }  | 
5126  | 0  |         break;  | 
5127  | 0  |     case ExtSlice_kind:  | 
5128  | 0  |         n = asdl_seq_LEN(sl->v.ExtSlice.dims);  | 
5129  | 0  |         for (i = 0; i < n; i++) { | 
5130  | 0  |             slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i);  | 
5131  | 0  |             switch (subsl->kind) { | 
5132  | 0  |             case Index_kind:  | 
5133  | 0  |             case Slice_kind:  | 
5134  | 0  |                 if (!check_ann_slice(c, subsl)) { | 
5135  | 0  |                     return 0;  | 
5136  | 0  |                 }  | 
5137  | 0  |                 break;  | 
5138  | 0  |             case ExtSlice_kind:  | 
5139  | 0  |             default:  | 
5140  | 0  |                 PyErr_SetString(PyExc_SystemError,  | 
5141  | 0  |                                 "extended slice invalid in nested slice");  | 
5142  | 0  |                 return 0;  | 
5143  | 0  |             }  | 
5144  | 0  |         }  | 
5145  | 0  |         break;  | 
5146  | 0  |     default:  | 
5147  | 0  |         PyErr_Format(PyExc_SystemError,  | 
5148  | 0  |                      "invalid subscript kind %d", sl->kind);  | 
5149  | 0  |         return 0;  | 
5150  | 0  |     }  | 
5151  | 0  |     return 1;  | 
5152  | 0  | }  | 
5153  |  |  | 
5154  |  | static int  | 
5155  |  | compiler_annassign(struct compiler *c, stmt_ty s)  | 
5156  | 0  | { | 
5157  | 0  |     expr_ty targ = s->v.AnnAssign.target;  | 
5158  | 0  |     PyObject* mangled;  | 
5159  |  | 
  | 
5160  | 0  |     assert(s->kind == AnnAssign_kind);  | 
5161  |  |  | 
5162  |  |     /* We perform the actual assignment first. */  | 
5163  | 0  |     if (s->v.AnnAssign.value) { | 
5164  | 0  |         VISIT(c, expr, s->v.AnnAssign.value);  | 
5165  | 0  |         VISIT(c, expr, targ);  | 
5166  | 0  |     }  | 
5167  | 0  |     switch (targ->kind) { | 
5168  | 0  |     case Name_kind:  | 
5169  |  |         /* If we have a simple name in a module or class, store annotation. */  | 
5170  | 0  |         if (s->v.AnnAssign.simple &&  | 
5171  | 0  |             (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||  | 
5172  | 0  |              c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { | 
5173  | 0  |             if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { | 
5174  | 0  |                 VISIT(c, annexpr, s->v.AnnAssign.annotation)  | 
5175  | 0  |             }  | 
5176  | 0  |             else { | 
5177  | 0  |                 VISIT(c, expr, s->v.AnnAssign.annotation);  | 
5178  | 0  |             }  | 
5179  | 0  |             ADDOP_NAME(c, LOAD_NAME, __annotations__, names);  | 
5180  | 0  |             mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);  | 
5181  | 0  |             ADDOP_LOAD_CONST_NEW(c, mangled);  | 
5182  | 0  |             ADDOP(c, STORE_SUBSCR);  | 
5183  | 0  |         }  | 
5184  | 0  |         break;  | 
5185  | 0  |     case Attribute_kind:  | 
5186  | 0  |         if (!s->v.AnnAssign.value &&  | 
5187  | 0  |             !check_ann_expr(c, targ->v.Attribute.value)) { | 
5188  | 0  |             return 0;  | 
5189  | 0  |         }  | 
5190  | 0  |         break;  | 
5191  | 0  |     case Subscript_kind:  | 
5192  | 0  |         if (!s->v.AnnAssign.value &&  | 
5193  | 0  |             (!check_ann_expr(c, targ->v.Subscript.value) ||  | 
5194  | 0  |              !check_ann_subscr(c, targ->v.Subscript.slice))) { | 
5195  | 0  |                 return 0;  | 
5196  | 0  |         }  | 
5197  | 0  |         break;  | 
5198  | 0  |     default:  | 
5199  | 0  |         PyErr_Format(PyExc_SystemError,  | 
5200  | 0  |                      "invalid node type (%d) for annotated assignment",  | 
5201  | 0  |                      targ->kind);  | 
5202  | 0  |             return 0;  | 
5203  | 0  |     }  | 
5204  |  |     /* Annotation is evaluated last. */  | 
5205  | 0  |     if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { | 
5206  | 0  |         return 0;  | 
5207  | 0  |     }  | 
5208  | 0  |     return 1;  | 
5209  | 0  | }  | 
5210  |  |  | 
5211  |  | /* Raises a SyntaxError and returns 0.  | 
5212  |  |    If something goes wrong, a different exception may be raised.  | 
5213  |  | */  | 
5214  |  |  | 
5215  |  | static int  | 
5216  |  | compiler_error(struct compiler *c, const char *errstr)  | 
5217  | 0  | { | 
5218  | 0  |     PyObject *loc;  | 
5219  | 0  |     PyObject *u = NULL, *v = NULL;  | 
5220  |  | 
  | 
5221  | 0  |     loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);  | 
5222  | 0  |     if (!loc) { | 
5223  | 0  |         Py_INCREF(Py_None);  | 
5224  | 0  |         loc = Py_None;  | 
5225  | 0  |     }  | 
5226  | 0  |     u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, | 
5227  | 0  |                       c->u->u_col_offset + 1, loc);  | 
5228  | 0  |     if (!u)  | 
5229  | 0  |         goto exit;  | 
5230  | 0  |     v = Py_BuildValue("(zO)", errstr, u); | 
5231  | 0  |     if (!v)  | 
5232  | 0  |         goto exit;  | 
5233  | 0  |     PyErr_SetObject(PyExc_SyntaxError, v);  | 
5234  | 0  |  exit:  | 
5235  | 0  |     Py_DECREF(loc);  | 
5236  | 0  |     Py_XDECREF(u);  | 
5237  | 0  |     Py_XDECREF(v);  | 
5238  | 0  |     return 0;  | 
5239  | 0  | }  | 
5240  |  |  | 
5241  |  | /* Emits a SyntaxWarning and returns 1 on success.  | 
5242  |  |    If a SyntaxWarning raised as error, replaces it with a SyntaxError  | 
5243  |  |    and returns 0.  | 
5244  |  | */  | 
5245  |  | static int  | 
5246  |  | compiler_warn(struct compiler *c, const char *format, ...)  | 
5247  | 0  | { | 
5248  | 0  |     va_list vargs;  | 
5249  | 0  | #ifdef HAVE_STDARG_PROTOTYPES  | 
5250  | 0  |     va_start(vargs, format);  | 
5251  |  | #else  | 
5252  |  |     va_start(vargs);  | 
5253  |  | #endif  | 
5254  | 0  |     PyObject *msg = PyUnicode_FromFormatV(format, vargs);  | 
5255  | 0  |     va_end(vargs);  | 
5256  | 0  |     if (msg == NULL) { | 
5257  | 0  |         return 0;  | 
5258  | 0  |     }  | 
5259  | 0  |     if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,  | 
5260  | 0  |                                  c->u->u_lineno, NULL, NULL) < 0)  | 
5261  | 0  |     { | 
5262  | 0  |         if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { | 
5263  |  |             /* Replace the SyntaxWarning exception with a SyntaxError  | 
5264  |  |                to get a more accurate error report */  | 
5265  | 0  |             PyErr_Clear();  | 
5266  | 0  |             assert(PyUnicode_AsUTF8(msg) != NULL);  | 
5267  | 0  |             compiler_error(c, PyUnicode_AsUTF8(msg));  | 
5268  | 0  |         }  | 
5269  | 0  |         Py_DECREF(msg);  | 
5270  | 0  |         return 0;  | 
5271  | 0  |     }  | 
5272  | 0  |     Py_DECREF(msg);  | 
5273  | 0  |     return 1;  | 
5274  | 0  | }  | 
5275  |  |  | 
5276  |  | static int  | 
5277  |  | compiler_handle_subscr(struct compiler *c, const char *kind,  | 
5278  |  |                        expr_context_ty ctx)  | 
5279  | 14  | { | 
5280  | 14  |     int op = 0;  | 
5281  |  |  | 
5282  |  |     /* XXX this code is duplicated */  | 
5283  | 14  |     switch (ctx) { | 
5284  | 0  |         case AugLoad: /* fall through to Load */  | 
5285  | 10  |         case Load:    op = BINARY_SUBSCR; break;  | 
5286  | 0  |         case AugStore:/* fall through to Store */  | 
5287  | 2  |         case Store:   op = STORE_SUBSCR; break;  | 
5288  | 2  |         case Del:     op = DELETE_SUBSCR; break;  | 
5289  | 0  |         case Param:  | 
5290  | 0  |             PyErr_Format(PyExc_SystemError,  | 
5291  | 0  |                          "invalid %s kind %d in subscript\n",  | 
5292  | 0  |                          kind, ctx);  | 
5293  | 0  |             return 0;  | 
5294  | 14  |     }  | 
5295  | 14  |     if (ctx == AugLoad) { | 
5296  | 0  |         ADDOP(c, DUP_TOP_TWO);  | 
5297  | 0  |     }  | 
5298  | 14  |     else if (ctx == AugStore) { | 
5299  | 0  |         ADDOP(c, ROT_THREE);  | 
5300  | 0  |     }  | 
5301  | 14  |     ADDOP(c, op);  | 
5302  | 14  |     return 1;  | 
5303  | 14  | }  | 
5304  |  |  | 
5305  |  | static int  | 
5306  |  | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)  | 
5307  | 2  | { | 
5308  | 2  |     int n = 2;  | 
5309  | 2  |     assert(s->kind == Slice_kind);  | 
5310  |  |  | 
5311  |  |     /* only handles the cases where BUILD_SLICE is emitted */  | 
5312  | 2  |     if (s->v.Slice.lower) { | 
5313  | 0  |         VISIT(c, expr, s->v.Slice.lower);  | 
5314  | 0  |     }  | 
5315  | 2  |     else { | 
5316  | 2  |         ADDOP_LOAD_CONST(c, Py_None);  | 
5317  | 2  |     }  | 
5318  |  |  | 
5319  | 2  |     if (s->v.Slice.upper) { | 
5320  | 2  |         VISIT(c, expr, s->v.Slice.upper);  | 
5321  | 2  |     }  | 
5322  | 0  |     else { | 
5323  | 0  |         ADDOP_LOAD_CONST(c, Py_None);  | 
5324  | 0  |     }  | 
5325  |  |  | 
5326  | 2  |     if (s->v.Slice.step) { | 
5327  | 0  |         n++;  | 
5328  | 0  |         VISIT(c, expr, s->v.Slice.step);  | 
5329  | 0  |     }  | 
5330  | 2  |     ADDOP_I(c, BUILD_SLICE, n);  | 
5331  | 2  |     return 1;  | 
5332  | 2  | }  | 
5333  |  |  | 
5334  |  | static int  | 
5335  |  | compiler_visit_nested_slice(struct compiler *c, slice_ty s,  | 
5336  |  |                             expr_context_ty ctx)  | 
5337  | 0  | { | 
5338  | 0  |     switch (s->kind) { | 
5339  | 0  |     case Slice_kind:  | 
5340  | 0  |         return compiler_slice(c, s, ctx);  | 
5341  | 0  |     case Index_kind:  | 
5342  | 0  |         VISIT(c, expr, s->v.Index.value);  | 
5343  | 0  |         break;  | 
5344  | 0  |     case ExtSlice_kind:  | 
5345  | 0  |     default:  | 
5346  | 0  |         PyErr_SetString(PyExc_SystemError,  | 
5347  | 0  |                         "extended slice invalid in nested slice");  | 
5348  | 0  |         return 0;  | 
5349  | 0  |     }  | 
5350  | 0  |     return 1;  | 
5351  | 0  | }  | 
5352  |  |  | 
5353  |  | static int  | 
5354  |  | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx)  | 
5355  | 14  | { | 
5356  | 14  |     const char * kindname = NULL;  | 
5357  | 14  |     switch (s->kind) { | 
5358  | 12  |     case Index_kind:  | 
5359  | 12  |         kindname = "index";  | 
5360  | 12  |         if (ctx != AugStore) { | 
5361  | 12  |             VISIT(c, expr, s->v.Index.value);  | 
5362  | 12  |         }  | 
5363  | 12  |         break;  | 
5364  | 12  |     case Slice_kind:  | 
5365  | 2  |         kindname = "slice";  | 
5366  | 2  |         if (ctx != AugStore) { | 
5367  | 2  |             if (!compiler_slice(c, s, ctx))  | 
5368  | 0  |                 return 0;  | 
5369  | 2  |         }  | 
5370  | 2  |         break;  | 
5371  | 2  |     case ExtSlice_kind:  | 
5372  | 0  |         kindname = "extended slice";  | 
5373  | 0  |         if (ctx != AugStore) { | 
5374  | 0  |             Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims);  | 
5375  | 0  |             for (i = 0; i < n; i++) { | 
5376  | 0  |                 slice_ty sub = (slice_ty)asdl_seq_GET(  | 
5377  | 0  |                     s->v.ExtSlice.dims, i);  | 
5378  | 0  |                 if (!compiler_visit_nested_slice(c, sub, ctx))  | 
5379  | 0  |                     return 0;  | 
5380  | 0  |             }  | 
5381  | 0  |             ADDOP_I(c, BUILD_TUPLE, n);  | 
5382  | 0  |         }  | 
5383  | 0  |         break;  | 
5384  | 0  |     default:  | 
5385  | 0  |         PyErr_Format(PyExc_SystemError,  | 
5386  | 0  |                      "invalid subscript kind %d", s->kind);  | 
5387  | 0  |         return 0;  | 
5388  | 14  |     }  | 
5389  | 14  |     return compiler_handle_subscr(c, kindname, ctx);  | 
5390  | 14  | }  | 
5391  |  |  | 
5392  |  | /* End of the compiler section, beginning of the assembler section */  | 
5393  |  |  | 
5394  |  | /* do depth-first search of basic block graph, starting with block.  | 
5395  |  |    post records the block indices in post-order.  | 
5396  |  |  | 
5397  |  |    XXX must handle implicit jumps from one block to next  | 
5398  |  | */  | 
5399  |  |  | 
5400  |  | struct assembler { | 
5401  |  |     PyObject *a_bytecode;  /* string containing bytecode */  | 
5402  |  |     int a_offset;              /* offset into bytecode */  | 
5403  |  |     int a_nblocks;             /* number of reachable blocks */  | 
5404  |  |     basicblock **a_postorder; /* list of blocks in dfs postorder */  | 
5405  |  |     PyObject *a_lnotab;    /* string containing lnotab */  | 
5406  |  |     int a_lnotab_off;      /* offset into lnotab */  | 
5407  |  |     int a_lineno;              /* last lineno of emitted instruction */  | 
5408  |  |     int a_lineno_off;      /* bytecode offset of last lineno */  | 
5409  |  | };  | 
5410  |  |  | 
5411  |  | static void  | 
5412  |  | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)  | 
5413  | 146  | { | 
5414  | 146  |     int i, j;  | 
5415  |  |  | 
5416  |  |     /* Get rid of recursion for normal control flow.  | 
5417  |  |        Since the number of blocks is limited, unused space in a_postorder  | 
5418  |  |        (from a_nblocks to end) can be used as a stack for still not ordered  | 
5419  |  |        blocks. */  | 
5420  | 354  |     for (j = end; b && !b->b_seen; b = b->b_next) { | 
5421  | 208  |         b->b_seen = 1;  | 
5422  | 208  |         assert(a->a_nblocks < j);  | 
5423  | 208  |         a->a_postorder[--j] = b;  | 
5424  | 208  |     }  | 
5425  | 354  |     while (j < end) { | 
5426  | 208  |         b = a->a_postorder[j++];  | 
5427  | 1.29k  |         for (i = 0; i < b->b_iused; i++) { | 
5428  | 1.08k  |             struct instr *instr = &b->b_instr[i];  | 
5429  | 1.08k  |             if (instr->i_jrel || instr->i_jabs)  | 
5430  | 124  |                 dfs(c, instr->i_target, a, j);  | 
5431  | 1.08k  |         }  | 
5432  | 208  |         assert(a->a_nblocks < j);  | 
5433  | 208  |         a->a_postorder[a->a_nblocks++] = b;  | 
5434  | 208  |     }  | 
5435  | 146  | }  | 
5436  |  |  | 
5437  |  | Py_LOCAL_INLINE(void)  | 
5438  |  | stackdepth_push(basicblock ***sp, basicblock *b, int depth)  | 
5439  | 270  | { | 
5440  | 270  |     assert(b->b_startdepth < 0 || b->b_startdepth == depth);  | 
5441  | 270  |     if (b->b_startdepth < depth) { | 
5442  | 208  |         assert(b->b_startdepth < 0);  | 
5443  | 208  |         b->b_startdepth = depth;  | 
5444  | 208  |         *(*sp)++ = b;  | 
5445  | 208  |     }  | 
5446  | 270  | }  | 
5447  |  |  | 
5448  |  | /* Find the flow path that needs the largest stack.  We assume that  | 
5449  |  |  * cycles in the flow graph have no net effect on the stack depth.  | 
5450  |  |  */  | 
5451  |  | static int  | 
5452  |  | stackdepth(struct compiler *c)  | 
5453  | 22  | { | 
5454  | 22  |     basicblock *b, *entryblock = NULL;  | 
5455  | 22  |     basicblock **stack, **sp;  | 
5456  | 22  |     int nblocks = 0, maxdepth = 0;  | 
5457  | 230  |     for (b = c->u->u_blocks; b != NULL; b = b->b_list) { | 
5458  | 208  |         b->b_startdepth = INT_MIN;  | 
5459  | 208  |         entryblock = b;  | 
5460  | 208  |         nblocks++;  | 
5461  | 208  |     }  | 
5462  | 22  |     if (!entryblock)  | 
5463  | 0  |         return 0;  | 
5464  | 22  |     stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);  | 
5465  | 22  |     if (!stack) { | 
5466  | 0  |         PyErr_NoMemory();  | 
5467  | 0  |         return -1;  | 
5468  | 0  |     }  | 
5469  |  |  | 
5470  | 22  |     sp = stack;  | 
5471  | 22  |     stackdepth_push(&sp, entryblock, 0);  | 
5472  | 230  |     while (sp != stack) { | 
5473  | 208  |         b = *--sp;  | 
5474  | 208  |         int depth = b->b_startdepth;  | 
5475  | 208  |         assert(depth >= 0);  | 
5476  | 208  |         basicblock *next = b->b_next;  | 
5477  | 1.20k  |         for (int i = 0; i < b->b_iused; i++) { | 
5478  | 1.07k  |             struct instr *instr = &b->b_instr[i];  | 
5479  | 1.07k  |             int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);  | 
5480  | 1.07k  |             if (effect == PY_INVALID_STACK_EFFECT) { | 
5481  | 0  |                 fprintf(stderr, "opcode = %d\n", instr->i_opcode);  | 
5482  | 0  |                 Py_FatalError("PyCompile_OpcodeStackEffect()"); | 
5483  | 0  |             }  | 
5484  | 1.07k  |             int new_depth = depth + effect;  | 
5485  | 1.07k  |             if (new_depth > maxdepth) { | 
5486  | 75  |                 maxdepth = new_depth;  | 
5487  | 75  |             }  | 
5488  | 1.07k  |             assert(depth >= 0); /* invalid code or bug in stackdepth() */  | 
5489  | 1.07k  |             if (instr->i_jrel || instr->i_jabs) { | 
5490  | 120  |                 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);  | 
5491  | 120  |                 assert(effect != PY_INVALID_STACK_EFFECT);  | 
5492  | 120  |                 int target_depth = depth + effect;  | 
5493  | 120  |                 if (target_depth > maxdepth) { | 
5494  | 30  |                     maxdepth = target_depth;  | 
5495  | 30  |                 }  | 
5496  | 120  |                 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */  | 
5497  | 120  |                 if (instr->i_opcode == CALL_FINALLY) { | 
5498  | 0  |                     assert(instr->i_target->b_startdepth >= 0);  | 
5499  | 0  |                     assert(instr->i_target->b_startdepth >= target_depth);  | 
5500  | 0  |                     depth = new_depth;  | 
5501  | 0  |                     continue;  | 
5502  | 0  |                 }  | 
5503  | 120  |                 stackdepth_push(&sp, instr->i_target, target_depth);  | 
5504  | 120  |             }  | 
5505  | 1.07k  |             depth = new_depth;  | 
5506  | 1.07k  |             if (instr->i_opcode == JUMP_ABSOLUTE ||  | 
5507  | 1.06k  |                 instr->i_opcode == JUMP_FORWARD ||  | 
5508  | 1.02k  |                 instr->i_opcode == RETURN_VALUE ||  | 
5509  | 1.00k  |                 instr->i_opcode == RAISE_VARARGS)  | 
5510  | 80  |             { | 
5511  |  |                 /* remaining code is dead */  | 
5512  | 80  |                 next = NULL;  | 
5513  | 80  |                 break;  | 
5514  | 80  |             }  | 
5515  | 1.07k  |         }  | 
5516  | 208  |         if (next != NULL) { | 
5517  | 128  |             stackdepth_push(&sp, next, depth);  | 
5518  | 128  |         }  | 
5519  | 208  |     }  | 
5520  | 22  |     PyObject_Free(stack);  | 
5521  | 22  |     return maxdepth;  | 
5522  | 22  | }  | 
5523  |  |  | 
5524  |  | static int  | 
5525  |  | assemble_init(struct assembler *a, int nblocks, int firstlineno)  | 
5526  | 22  | { | 
5527  | 22  |     memset(a, 0, sizeof(struct assembler));  | 
5528  | 22  |     a->a_lineno = firstlineno;  | 
5529  | 22  |     a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);  | 
5530  | 22  |     if (!a->a_bytecode)  | 
5531  | 0  |         return 0;  | 
5532  | 22  |     a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);  | 
5533  | 22  |     if (!a->a_lnotab)  | 
5534  | 0  |         return 0;  | 
5535  | 22  |     if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { | 
5536  | 0  |         PyErr_NoMemory();  | 
5537  | 0  |         return 0;  | 
5538  | 0  |     }  | 
5539  | 22  |     a->a_postorder = (basicblock **)PyObject_Malloc(  | 
5540  | 22  |                                         sizeof(basicblock *) * nblocks);  | 
5541  | 22  |     if (!a->a_postorder) { | 
5542  | 0  |         PyErr_NoMemory();  | 
5543  | 0  |         return 0;  | 
5544  | 0  |     }  | 
5545  | 22  |     return 1;  | 
5546  | 22  | }  | 
5547  |  |  | 
5548  |  | static void  | 
5549  |  | assemble_free(struct assembler *a)  | 
5550  | 22  | { | 
5551  | 22  |     Py_XDECREF(a->a_bytecode);  | 
5552  | 22  |     Py_XDECREF(a->a_lnotab);  | 
5553  | 22  |     if (a->a_postorder)  | 
5554  | 22  |         PyObject_Free(a->a_postorder);  | 
5555  | 22  | }  | 
5556  |  |  | 
5557  |  | static int  | 
5558  |  | blocksize(basicblock *b)  | 
5559  | 208  | { | 
5560  | 208  |     int i;  | 
5561  | 208  |     int size = 0;  | 
5562  |  |  | 
5563  | 1.29k  |     for (i = 0; i < b->b_iused; i++)  | 
5564  | 1.08k  |         size += instrsize(b->b_instr[i].i_oparg);  | 
5565  | 208  |     return size;  | 
5566  | 208  | }  | 
5567  |  |  | 
5568  |  | /* Appends a pair to the end of the line number table, a_lnotab, representing  | 
5569  |  |    the instruction's bytecode offset and line number.  See  | 
5570  |  |    Objects/lnotab_notes.txt for the description of the line number table. */  | 
5571  |  |  | 
5572  |  | static int  | 
5573  |  | assemble_lnotab(struct assembler *a, struct instr *i)  | 
5574  | 174  | { | 
5575  | 174  |     int d_bytecode, d_lineno;  | 
5576  | 174  |     Py_ssize_t len;  | 
5577  | 174  |     unsigned char *lnotab;  | 
5578  |  |  | 
5579  | 174  |     d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);  | 
5580  | 174  |     d_lineno = i->i_lineno - a->a_lineno;  | 
5581  |  |  | 
5582  | 174  |     assert(d_bytecode >= 0);  | 
5583  |  |  | 
5584  | 174  |     if(d_bytecode == 0 && d_lineno == 0)  | 
5585  | 20  |         return 1;  | 
5586  |  |  | 
5587  | 154  |     if (d_bytecode > 255) { | 
5588  | 0  |         int j, nbytes, ncodes = d_bytecode / 255;  | 
5589  | 0  |         nbytes = a->a_lnotab_off + 2 * ncodes;  | 
5590  | 0  |         len = PyBytes_GET_SIZE(a->a_lnotab);  | 
5591  | 0  |         if (nbytes >= len) { | 
5592  | 0  |             if ((len <= INT_MAX / 2) && (len * 2 < nbytes))  | 
5593  | 0  |                 len = nbytes;  | 
5594  | 0  |             else if (len <= INT_MAX / 2)  | 
5595  | 0  |                 len *= 2;  | 
5596  | 0  |             else { | 
5597  | 0  |                 PyErr_NoMemory();  | 
5598  | 0  |                 return 0;  | 
5599  | 0  |             }  | 
5600  | 0  |             if (_PyBytes_Resize(&a->a_lnotab, len) < 0)  | 
5601  | 0  |                 return 0;  | 
5602  | 0  |         }  | 
5603  | 0  |         lnotab = (unsigned char *)  | 
5604  | 0  |                    PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;  | 
5605  | 0  |         for (j = 0; j < ncodes; j++) { | 
5606  | 0  |             *lnotab++ = 255;  | 
5607  | 0  |             *lnotab++ = 0;  | 
5608  | 0  |         }  | 
5609  | 0  |         d_bytecode -= ncodes * 255;  | 
5610  | 0  |         a->a_lnotab_off += ncodes * 2;  | 
5611  | 0  |     }  | 
5612  | 154  |     assert(0 <= d_bytecode && d_bytecode <= 255);  | 
5613  |  |  | 
5614  | 154  |     if (d_lineno < -128 || 127 < d_lineno) { | 
5615  | 0  |         int j, nbytes, ncodes, k;  | 
5616  | 0  |         if (d_lineno < 0) { | 
5617  | 0  |             k = -128;  | 
5618  |  |             /* use division on positive numbers */  | 
5619  | 0  |             ncodes = (-d_lineno) / 128;  | 
5620  | 0  |         }  | 
5621  | 0  |         else { | 
5622  | 0  |             k = 127;  | 
5623  | 0  |             ncodes = d_lineno / 127;  | 
5624  | 0  |         }  | 
5625  | 0  |         d_lineno -= ncodes * k;  | 
5626  | 0  |         assert(ncodes >= 1);  | 
5627  | 0  |         nbytes = a->a_lnotab_off + 2 * ncodes;  | 
5628  | 0  |         len = PyBytes_GET_SIZE(a->a_lnotab);  | 
5629  | 0  |         if (nbytes >= len) { | 
5630  | 0  |             if ((len <= INT_MAX / 2) && len * 2 < nbytes)  | 
5631  | 0  |                 len = nbytes;  | 
5632  | 0  |             else if (len <= INT_MAX / 2)  | 
5633  | 0  |                 len *= 2;  | 
5634  | 0  |             else { | 
5635  | 0  |                 PyErr_NoMemory();  | 
5636  | 0  |                 return 0;  | 
5637  | 0  |             }  | 
5638  | 0  |             if (_PyBytes_Resize(&a->a_lnotab, len) < 0)  | 
5639  | 0  |                 return 0;  | 
5640  | 0  |         }  | 
5641  | 0  |         lnotab = (unsigned char *)  | 
5642  | 0  |                    PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;  | 
5643  | 0  |         *lnotab++ = d_bytecode;  | 
5644  | 0  |         *lnotab++ = k;  | 
5645  | 0  |         d_bytecode = 0;  | 
5646  | 0  |         for (j = 1; j < ncodes; j++) { | 
5647  | 0  |             *lnotab++ = 0;  | 
5648  | 0  |             *lnotab++ = k;  | 
5649  | 0  |         }  | 
5650  | 0  |         a->a_lnotab_off += ncodes * 2;  | 
5651  | 0  |     }  | 
5652  | 154  |     assert(-128 <= d_lineno && d_lineno <= 127);  | 
5653  |  |  | 
5654  | 154  |     len = PyBytes_GET_SIZE(a->a_lnotab);  | 
5655  | 154  |     if (a->a_lnotab_off + 2 >= len) { | 
5656  | 12  |         if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)  | 
5657  | 0  |             return 0;  | 
5658  | 12  |     }  | 
5659  | 154  |     lnotab = (unsigned char *)  | 
5660  | 154  |                     PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;  | 
5661  |  |  | 
5662  | 154  |     a->a_lnotab_off += 2;  | 
5663  | 154  |     if (d_bytecode) { | 
5664  | 152  |         *lnotab++ = d_bytecode;  | 
5665  | 152  |         *lnotab++ = d_lineno;  | 
5666  | 152  |     }  | 
5667  | 2  |     else {      /* First line of a block; def stmt, etc. */ | 
5668  | 2  |         *lnotab++ = 0;  | 
5669  | 2  |         *lnotab++ = d_lineno;  | 
5670  | 2  |     }  | 
5671  | 154  |     a->a_lineno = i->i_lineno;  | 
5672  | 154  |     a->a_lineno_off = a->a_offset;  | 
5673  | 154  |     return 1;  | 
5674  | 154  | }  | 
5675  |  |  | 
5676  |  | /* assemble_emit()  | 
5677  |  |    Extend the bytecode with a new instruction.  | 
5678  |  |    Update lnotab if necessary.  | 
5679  |  | */  | 
5680  |  |  | 
5681  |  | static int  | 
5682  |  | assemble_emit(struct assembler *a, struct instr *i)  | 
5683  | 1.08k  | { | 
5684  | 1.08k  |     int size, arg = 0;  | 
5685  | 1.08k  |     Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);  | 
5686  | 1.08k  |     _Py_CODEUNIT *code;  | 
5687  |  |  | 
5688  | 1.08k  |     arg = i->i_oparg;  | 
5689  | 1.08k  |     size = instrsize(arg);  | 
5690  | 1.08k  |     if (i->i_lineno && !assemble_lnotab(a, i))  | 
5691  | 0  |         return 0;  | 
5692  | 1.08k  |     if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { | 
5693  | 8  |         if (len > PY_SSIZE_T_MAX / 2)  | 
5694  | 0  |             return 0;  | 
5695  | 8  |         if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)  | 
5696  | 0  |             return 0;  | 
5697  | 8  |     }  | 
5698  | 1.08k  |     code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;  | 
5699  | 1.08k  |     a->a_offset += size;  | 
5700  | 1.08k  |     write_op_arg(code, i->i_opcode, arg, size);  | 
5701  | 1.08k  |     return 1;  | 
5702  | 1.08k  | }  | 
5703  |  |  | 
5704  |  | static void  | 
5705  |  | assemble_jump_offsets(struct assembler *a, struct compiler *c)  | 
5706  | 22  | { | 
5707  | 22  |     basicblock *b;  | 
5708  | 22  |     int bsize, totsize, extended_arg_recompile;  | 
5709  | 22  |     int i;  | 
5710  |  |  | 
5711  |  |     /* Compute the size of each block and fixup jump args.  | 
5712  |  |        Replace block pointer with position in bytecode. */  | 
5713  | 22  |     do { | 
5714  | 22  |         totsize = 0;  | 
5715  | 230  |         for (i = a->a_nblocks - 1; i >= 0; i--) { | 
5716  | 208  |             b = a->a_postorder[i];  | 
5717  | 208  |             bsize = blocksize(b);  | 
5718  | 208  |             b->b_offset = totsize;  | 
5719  | 208  |             totsize += bsize;  | 
5720  | 208  |         }  | 
5721  | 22  |         extended_arg_recompile = 0;  | 
5722  | 230  |         for (b = c->u->u_blocks; b != NULL; b = b->b_list) { | 
5723  | 208  |             bsize = b->b_offset;  | 
5724  | 1.29k  |             for (i = 0; i < b->b_iused; i++) { | 
5725  | 1.08k  |                 struct instr *instr = &b->b_instr[i];  | 
5726  | 1.08k  |                 int isize = instrsize(instr->i_oparg);  | 
5727  |  |                 /* Relative jumps are computed relative to  | 
5728  |  |                    the instruction pointer after fetching  | 
5729  |  |                    the jump instruction.  | 
5730  |  |                 */  | 
5731  | 1.08k  |                 bsize += isize;  | 
5732  | 1.08k  |                 if (instr->i_jabs || instr->i_jrel) { | 
5733  | 124  |                     instr->i_oparg = instr->i_target->b_offset;  | 
5734  | 124  |                     if (instr->i_jrel) { | 
5735  | 82  |                         instr->i_oparg -= bsize;  | 
5736  | 82  |                     }  | 
5737  | 124  |                     instr->i_oparg *= sizeof(_Py_CODEUNIT);  | 
5738  | 124  |                     if (instrsize(instr->i_oparg) != isize) { | 
5739  | 0  |                         extended_arg_recompile = 1;  | 
5740  | 0  |                     }  | 
5741  | 124  |                 }  | 
5742  | 1.08k  |             }  | 
5743  | 208  |         }  | 
5744  |  |  | 
5745  |  |     /* XXX: This is an awful hack that could hurt performance, but  | 
5746  |  |         on the bright side it should work until we come up  | 
5747  |  |         with a better solution.  | 
5748  |  |  | 
5749  |  |         The issue is that in the first loop blocksize() is called  | 
5750  |  |         which calls instrsize() which requires i_oparg be set  | 
5751  |  |         appropriately. There is a bootstrap problem because  | 
5752  |  |         i_oparg is calculated in the second loop above.  | 
5753  |  |  | 
5754  |  |         So we loop until we stop seeing new EXTENDED_ARGs.  | 
5755  |  |         The only EXTENDED_ARGs that could be popping up are  | 
5756  |  |         ones in jump instructions.  So this should converge  | 
5757  |  |         fairly quickly.  | 
5758  |  |     */  | 
5759  | 22  |     } while (extended_arg_recompile);  | 
5760  | 22  | }  | 
5761  |  |  | 
5762  |  | static PyObject *  | 
5763  |  | dict_keys_inorder(PyObject *dict, Py_ssize_t offset)  | 
5764  | 88  | { | 
5765  | 88  |     PyObject *tuple, *k, *v;  | 
5766  | 88  |     Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);  | 
5767  |  |  | 
5768  | 88  |     tuple = PyTuple_New(size);  | 
5769  | 88  |     if (tuple == NULL)  | 
5770  | 0  |         return NULL;  | 
5771  | 277  |     while (PyDict_Next(dict, &pos, &k, &v)) { | 
5772  | 189  |         i = PyLong_AS_LONG(v);  | 
5773  | 189  |         Py_INCREF(k);  | 
5774  | 189  |         assert((i - offset) < size);  | 
5775  | 189  |         assert((i - offset) >= 0);  | 
5776  | 189  |         PyTuple_SET_ITEM(tuple, i - offset, k);  | 
5777  | 189  |     }  | 
5778  | 88  |     return tuple;  | 
5779  | 88  | }  | 
5780  |  |  | 
5781  |  | static PyObject *  | 
5782  |  | consts_dict_keys_inorder(PyObject *dict)  | 
5783  | 22  | { | 
5784  | 22  |     PyObject *consts, *k, *v;  | 
5785  | 22  |     Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);  | 
5786  |  |  | 
5787  | 22  |     consts = PyList_New(size);   /* PyCode_Optimize() requires a list */  | 
5788  | 22  |     if (consts == NULL)  | 
5789  | 0  |         return NULL;  | 
5790  | 102  |     while (PyDict_Next(dict, &pos, &k, &v)) { | 
5791  | 80  |         i = PyLong_AS_LONG(v);  | 
5792  |  |         /* The keys of the dictionary can be tuples wrapping a contant.  | 
5793  |  |          * (see compiler_add_o and _PyCode_ConstantKey). In that case  | 
5794  |  |          * the object we want is always second. */  | 
5795  | 80  |         if (PyTuple_CheckExact(k)) { | 
5796  | 10  |             k = PyTuple_GET_ITEM(k, 1);  | 
5797  | 10  |         }  | 
5798  | 80  |         Py_INCREF(k);  | 
5799  | 80  |         assert(i < size);  | 
5800  | 80  |         assert(i >= 0);  | 
5801  | 80  |         PyList_SET_ITEM(consts, i, k);  | 
5802  | 80  |     }  | 
5803  | 22  |     return consts;  | 
5804  | 22  | }  | 
5805  |  |  | 
5806  |  | static int  | 
5807  |  | compute_code_flags(struct compiler *c)  | 
5808  | 22  | { | 
5809  | 22  |     PySTEntryObject *ste = c->u->u_ste;  | 
5810  | 22  |     int flags = 0;  | 
5811  | 22  |     if (ste->ste_type == FunctionBlock) { | 
5812  | 6  |         flags |= CO_NEWLOCALS | CO_OPTIMIZED;  | 
5813  | 6  |         if (ste->ste_nested)  | 
5814  | 0  |             flags |= CO_NESTED;  | 
5815  | 6  |         if (ste->ste_generator && !ste->ste_coroutine)  | 
5816  | 0  |             flags |= CO_GENERATOR;  | 
5817  | 6  |         if (!ste->ste_generator && ste->ste_coroutine)  | 
5818  | 0  |             flags |= CO_COROUTINE;  | 
5819  | 6  |         if (ste->ste_generator && ste->ste_coroutine)  | 
5820  | 0  |             flags |= CO_ASYNC_GENERATOR;  | 
5821  | 6  |         if (ste->ste_varargs)  | 
5822  | 0  |             flags |= CO_VARARGS;  | 
5823  | 6  |         if (ste->ste_varkeywords)  | 
5824  | 0  |             flags |= CO_VARKEYWORDS;  | 
5825  | 6  |     }  | 
5826  |  |  | 
5827  |  |     /* (Only) inherit compilerflags in PyCF_MASK */  | 
5828  | 22  |     flags |= (c->c_flags->cf_flags & PyCF_MASK);  | 
5829  |  |  | 
5830  | 22  |     if ((IS_TOP_LEVEL_AWAIT(c)) &&  | 
5831  | 0  |          ste->ste_coroutine &&  | 
5832  | 0  |          !ste->ste_generator) { | 
5833  | 0  |         flags |= CO_COROUTINE;  | 
5834  | 0  |     }  | 
5835  |  |  | 
5836  | 22  |     return flags;  | 
5837  | 22  | }  | 
5838  |  |  | 
5839  |  | // Merge *tuple* with constant cache.  | 
5840  |  | // Unlike merge_consts_recursive(), this function doesn't work recursively.  | 
5841  |  | static int  | 
5842  |  | merge_const_tuple(struct compiler *c, PyObject **tuple)  | 
5843  | 110  | { | 
5844  | 110  |     assert(PyTuple_CheckExact(*tuple));  | 
5845  |  |  | 
5846  | 110  |     PyObject *key = _PyCode_ConstantKey(*tuple);  | 
5847  | 110  |     if (key == NULL) { | 
5848  | 0  |         return 0;  | 
5849  | 0  |     }  | 
5850  |  |  | 
5851  |  |     // t is borrowed reference  | 
5852  | 110  |     PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);  | 
5853  | 110  |     Py_DECREF(key);  | 
5854  | 110  |     if (t == NULL) { | 
5855  | 0  |         return 0;  | 
5856  | 0  |     }  | 
5857  | 110  |     if (t == key) {  // tuple is new constant. | 
5858  | 64  |         return 1;  | 
5859  | 64  |     }  | 
5860  |  |  | 
5861  | 46  |     PyObject *u = PyTuple_GET_ITEM(t, 1);  | 
5862  | 46  |     Py_INCREF(u);  | 
5863  | 46  |     Py_DECREF(*tuple);  | 
5864  | 46  |     *tuple = u;  | 
5865  | 46  |     return 1;  | 
5866  | 110  | }  | 
5867  |  |  | 
5868  |  | static PyCodeObject *  | 
5869  |  | makecode(struct compiler *c, struct assembler *a)  | 
5870  | 22  | { | 
5871  | 22  |     PyObject *tmp;  | 
5872  | 22  |     PyCodeObject *co = NULL;  | 
5873  | 22  |     PyObject *consts = NULL;  | 
5874  | 22  |     PyObject *names = NULL;  | 
5875  | 22  |     PyObject *varnames = NULL;  | 
5876  | 22  |     PyObject *name = NULL;  | 
5877  | 22  |     PyObject *freevars = NULL;  | 
5878  | 22  |     PyObject *cellvars = NULL;  | 
5879  | 22  |     PyObject *bytecode = NULL;  | 
5880  | 22  |     Py_ssize_t nlocals;  | 
5881  | 22  |     int nlocals_int;  | 
5882  | 22  |     int flags;  | 
5883  | 22  |     int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;  | 
5884  |  |  | 
5885  | 22  |     consts = consts_dict_keys_inorder(c->u->u_consts);  | 
5886  | 22  |     names = dict_keys_inorder(c->u->u_names, 0);  | 
5887  | 22  |     varnames = dict_keys_inorder(c->u->u_varnames, 0);  | 
5888  | 22  |     if (!consts || !names || !varnames)  | 
5889  | 0  |         goto error;  | 
5890  |  |  | 
5891  | 22  |     cellvars = dict_keys_inorder(c->u->u_cellvars, 0);  | 
5892  | 22  |     if (!cellvars)  | 
5893  | 0  |         goto error;  | 
5894  | 22  |     freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));  | 
5895  | 22  |     if (!freevars)  | 
5896  | 0  |         goto error;  | 
5897  |  |  | 
5898  | 22  |     if (!merge_const_tuple(c, &names) ||  | 
5899  | 22  |             !merge_const_tuple(c, &varnames) ||  | 
5900  | 22  |             !merge_const_tuple(c, &cellvars) ||  | 
5901  | 22  |             !merge_const_tuple(c, &freevars))  | 
5902  | 0  |     { | 
5903  | 0  |         goto error;  | 
5904  | 0  |     }  | 
5905  |  |  | 
5906  | 22  |     nlocals = PyDict_GET_SIZE(c->u->u_varnames);  | 
5907  | 22  |     assert(nlocals < INT_MAX);  | 
5908  | 22  |     nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);  | 
5909  |  |  | 
5910  | 22  |     flags = compute_code_flags(c);  | 
5911  | 22  |     if (flags < 0)  | 
5912  | 0  |         goto error;  | 
5913  |  |  | 
5914  | 22  |     bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);  | 
5915  | 22  |     if (!bytecode)  | 
5916  | 0  |         goto error;  | 
5917  |  |  | 
5918  | 22  |     tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */  | 
5919  | 22  |     if (!tmp)  | 
5920  | 0  |         goto error;  | 
5921  | 22  |     Py_DECREF(consts);  | 
5922  | 22  |     consts = tmp;  | 
5923  | 22  |     if (!merge_const_tuple(c, &consts)) { | 
5924  | 0  |         goto error;  | 
5925  | 0  |     }  | 
5926  |  |  | 
5927  | 22  |     posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);  | 
5928  | 22  |     posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);  | 
5929  | 22  |     kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);  | 
5930  | 22  |     maxdepth = stackdepth(c);  | 
5931  | 22  |     if (maxdepth < 0) { | 
5932  | 0  |         goto error;  | 
5933  | 0  |     }  | 
5934  | 22  |     co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,  | 
5935  | 22  |                                    posonlyargcount, kwonlyargcount, nlocals_int,   | 
5936  | 22  |                                    maxdepth, flags, bytecode, consts, names,  | 
5937  | 22  |                                    varnames, freevars, cellvars, c->c_filename,  | 
5938  | 22  |                                    c->u->u_name, c->u->u_firstlineno, a->a_lnotab);  | 
5939  | 22  |  error:  | 
5940  | 22  |     Py_XDECREF(consts);  | 
5941  | 22  |     Py_XDECREF(names);  | 
5942  | 22  |     Py_XDECREF(varnames);  | 
5943  | 22  |     Py_XDECREF(name);  | 
5944  | 22  |     Py_XDECREF(freevars);  | 
5945  | 22  |     Py_XDECREF(cellvars);  | 
5946  | 22  |     Py_XDECREF(bytecode);  | 
5947  | 22  |     return co;  | 
5948  | 22  | }  | 
5949  |  |  | 
5950  |  |  | 
5951  |  | /* For debugging purposes only */  | 
5952  |  | #if 0  | 
5953  |  | static void  | 
5954  |  | dump_instr(const struct instr *i)  | 
5955  |  | { | 
5956  |  |     const char *jrel = i->i_jrel ? "jrel " : "";  | 
5957  |  |     const char *jabs = i->i_jabs ? "jabs " : "";  | 
5958  |  |     char arg[128];  | 
5959  |  |  | 
5960  |  |     *arg = '\0';  | 
5961  |  |     if (HAS_ARG(i->i_opcode)) { | 
5962  |  |         sprintf(arg, "arg: %d ", i->i_oparg);  | 
5963  |  |     }  | 
5964  |  |     fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",  | 
5965  |  |                     i->i_lineno, i->i_opcode, arg, jabs, jrel);  | 
5966  |  | }  | 
5967  |  |  | 
5968  |  | static void  | 
5969  |  | dump_basicblock(const basicblock *b)  | 
5970  |  | { | 
5971  |  |     const char *seen = b->b_seen ? "seen " : "";  | 
5972  |  |     const char *b_return = b->b_return ? "return " : "";  | 
5973  |  |     fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",  | 
5974  |  |         b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);  | 
5975  |  |     if (b->b_instr) { | 
5976  |  |         int i;  | 
5977  |  |         for (i = 0; i < b->b_iused; i++) { | 
5978  |  |             fprintf(stderr, "  [%02d] ", i);  | 
5979  |  |             dump_instr(b->b_instr + i);  | 
5980  |  |         }  | 
5981  |  |     }  | 
5982  |  | }  | 
5983  |  | #endif  | 
5984  |  |  | 
5985  |  | static PyCodeObject *  | 
5986  |  | assemble(struct compiler *c, int addNone)  | 
5987  | 22  | { | 
5988  | 22  |     basicblock *b, *entryblock;  | 
5989  | 22  |     struct assembler a;  | 
5990  | 22  |     int i, j, nblocks;  | 
5991  | 22  |     PyCodeObject *co = NULL;  | 
5992  |  |  | 
5993  |  |     /* Make sure every block that falls off the end returns None.  | 
5994  |  |        XXX NEXT_BLOCK() isn't quite right, because if the last  | 
5995  |  |        block ends with a jump or return b_next shouldn't set.  | 
5996  |  |      */  | 
5997  | 22  |     if (!c->u->u_curblock->b_return) { | 
5998  | 18  |         NEXT_BLOCK(c);  | 
5999  | 18  |         if (addNone)  | 
6000  | 18  |             ADDOP_LOAD_CONST(c, Py_None);  | 
6001  | 18  |         ADDOP(c, RETURN_VALUE);  | 
6002  | 18  |     }  | 
6003  |  |  | 
6004  | 22  |     nblocks = 0;  | 
6005  | 22  |     entryblock = NULL;  | 
6006  | 230  |     for (b = c->u->u_blocks; b != NULL; b = b->b_list) { | 
6007  | 208  |         nblocks++;  | 
6008  | 208  |         entryblock = b;  | 
6009  | 208  |     }  | 
6010  |  |  | 
6011  |  |     /* Set firstlineno if it wasn't explicitly set. */  | 
6012  | 22  |     if (!c->u->u_firstlineno) { | 
6013  | 16  |         if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)  | 
6014  | 16  |             c->u->u_firstlineno = entryblock->b_instr->i_lineno;  | 
6015  | 0  |         else  | 
6016  | 0  |             c->u->u_firstlineno = 1;  | 
6017  | 16  |     }  | 
6018  | 22  |     if (!assemble_init(&a, nblocks, c->u->u_firstlineno))  | 
6019  | 0  |         goto error;  | 
6020  | 22  |     dfs(c, entryblock, &a, nblocks);  | 
6021  |  |  | 
6022  |  |     /* Can't modify the bytecode after computing jump offsets. */  | 
6023  | 22  |     assemble_jump_offsets(&a, c);  | 
6024  |  |  | 
6025  |  |     /* Emit code in reverse postorder from dfs. */  | 
6026  | 230  |     for (i = a.a_nblocks - 1; i >= 0; i--) { | 
6027  | 208  |         b = a.a_postorder[i];  | 
6028  | 1.29k  |         for (j = 0; j < b->b_iused; j++)  | 
6029  | 1.08k  |             if (!assemble_emit(&a, &b->b_instr[j]))  | 
6030  | 0  |                 goto error;  | 
6031  | 208  |     }  | 
6032  |  |  | 
6033  | 22  |     if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)  | 
6034  | 0  |         goto error;  | 
6035  | 22  |     if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)  | 
6036  | 0  |         goto error;  | 
6037  |  |  | 
6038  | 22  |     co = makecode(c, &a);  | 
6039  | 22  |  error:  | 
6040  | 22  |     assemble_free(&a);  | 
6041  | 22  |     return co;  | 
6042  | 22  | }  | 
6043  |  |  | 
6044  |  | #undef PyAST_Compile  | 
6045  |  | PyCodeObject *  | 
6046  |  | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,  | 
6047  |  |               PyArena *arena)  | 
6048  | 0  | { | 
6049  | 0  |     return PyAST_CompileEx(mod, filename, flags, -1, arena);  | 
6050  | 0  | }  |