blob: 722d52dbcefb1a7ddfdd9fe77ad6ed17180cb196 [file] [log] [blame]
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001/*
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
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00008 * 2. Builds a symbol table. See symtable.c.
Thomas Wouters89f507f2006-12-13 04:49:30 +00009 * 3. Generate code for basic blocks. See compiler_mod() in this file.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000010 * 4. Assemble the basic blocks into final code. See assemble() in
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000011 * this file.
Thomas Wouters89f507f2006-12-13 04:49:30 +000012 * 5. Optimize the byte code (peephole optimizations). See peephole.c
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000013 *
14 * Note that compiler_mod() suggests module, but the module ast type
15 * (mod_ty) has cases for expressions and interactive statements.
Nick Coghlan944d3eb2005-11-16 12:46:55 +000016 *
Jeremy Hyltone9357b22006-03-01 15:47:05 +000017 * 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
Thomas Wouters89f507f2006-12-13 04:49:30 +000020 * structure takes care of releasing those. Use the arena to manage
21 * objects.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022 */
Guido van Rossum10dc2e81990-11-18 17:27:39 +000023
Guido van Rossum79f25d91997-04-29 20:08:16 +000024#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000025
Ammar Askare92d3932020-01-15 11:48:40 -050026#include "Python-ast.h"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000027#include "ast.h"
28#include "code.h"
Jeremy Hylton4b38da62001-02-02 18:19:15 +000029#include "symtable.h"
Guido van Rossum10dc2e81990-11-18 17:27:39 +000030#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030031#include "wordcode_helpers.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000032
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000033#define DEFAULT_BLOCK_SIZE 16
34#define DEFAULT_BLOCKS 8
35#define DEFAULT_CODE_SIZE 128
36#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000037
Nick Coghlan650f0d02007-04-15 12:05:43 +000038#define COMP_GENEXP 0
39#define COMP_LISTCOMP 1
40#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000041#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000042
Pablo Galindo90235812020-03-15 04:29:22 +000043#define IS_TOP_LEVEL_AWAIT(c) ( \
44 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
45 && (c->u->u_ste->ste_type == ModuleBlock))
46
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000047struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000048 unsigned i_jabs : 1;
49 unsigned i_jrel : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 unsigned char i_opcode;
51 int i_oparg;
52 struct basicblock_ *i_target; /* target block (if jump instruction) */
53 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000054};
55
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000056typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000057 /* Each basicblock in a compilation unit is linked via b_list in the
58 reverse order that the block are allocated. b_list points to the next
59 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 struct basicblock_ *b_list;
61 /* number of instructions used */
62 int b_iused;
63 /* length of instruction array (b_instr) */
64 int b_ialloc;
65 /* pointer to an array of instructions, initially NULL */
66 struct instr *b_instr;
67 /* If b_next is non-NULL, it is a pointer to the next
68 block reached by normal control flow. */
69 struct basicblock_ *b_next;
70 /* b_seen is used to perform a DFS of basicblocks. */
71 unsigned b_seen : 1;
72 /* b_return is true if a RETURN_VALUE opcode is inserted. */
73 unsigned b_return : 1;
74 /* depth of stack upon entry of block, computed by stackdepth() */
75 int b_startdepth;
76 /* instruction offset for block, computed by assemble_jump_offsets() */
77 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000078} basicblock;
79
80/* fblockinfo tracks the current frame block.
81
Jeremy Hyltone9357b22006-03-01 15:47:05 +000082A frame block is used to handle loops, try/except, and try/finally.
83It's called a frame block to distinguish it from a basic block in the
84compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000085*/
86
Irit Katriel48a9c0e2020-11-17 19:31:55 +000087enum fblocktype { WHILE_LOOP, FOR_LOOP, TRY_EXCEPT, FINALLY_TRY, FINALLY_END,
88 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE, EXCEPTION_HANDLER };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000089
90struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 enum fblocktype fb_type;
92 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +020093 /* (optional) type-specific exit or cleanup block */
94 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +000095 /* (optional) additional information required for unwinding */
96 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000097};
98
Antoine Pitrou86a36b52011-11-25 18:56:07 +010099enum {
100 COMPILER_SCOPE_MODULE,
101 COMPILER_SCOPE_CLASS,
102 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400103 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400104 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100105 COMPILER_SCOPE_COMPREHENSION,
106};
107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000108/* The following items change on entry and exit of code blocks.
109 They must be saved and restored when returning to a block.
110*/
111struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000112 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400115 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100116 int u_scope_type;
117
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 /* The following fields are dicts that map objects to
119 the index of them in co_XXX. The index is used as
120 the argument for opcodes that refer to those collections.
121 */
122 PyObject *u_consts; /* all constants */
123 PyObject *u_names; /* all names */
124 PyObject *u_varnames; /* local variables */
125 PyObject *u_cellvars; /* cell variables */
126 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000129
Victor Stinnerf8e32212013-11-19 23:56:34 +0100130 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100131 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100132 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000133 /* Pointer to the most recently allocated block. By following b_list
134 members, you can reach all early allocated blocks. */
135 basicblock *u_blocks;
136 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 int u_nfblocks;
139 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 int u_firstlineno; /* the first lineno of the block */
142 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000143 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000144};
145
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000147
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000148The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000150managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000151
152Note that we don't track recursion levels during compilation - the
153task of detecting and rejecting excessive levels of nesting is
154handled by the symbol analysis pass.
155
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000156*/
157
158struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200159 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 struct symtable *c_st;
161 PyFutureFeatures *c_future; /* pointer to module's __future__ */
162 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000163
Georg Brandl8334fd92010-12-04 10:26:46 +0000164 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 int c_interactive; /* true if in interactive mode */
166 int c_nestlevel;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100167 int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode
168 if this value is different from zero.
169 This can be used to temporarily visit
170 nodes without emitting bytecode to
171 check only errors. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000172
INADA Naokic2e16072018-11-26 21:23:22 +0900173 PyObject *c_const_cache; /* Python dict holding all constants,
174 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 struct compiler_unit *u; /* compiler state for current block */
176 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
177 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178};
179
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100180static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000181static void compiler_free(struct compiler *);
182static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500183static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000184static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100185static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000186static int compiler_addop_j(struct compiler *, int, basicblock *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187static int compiler_error(struct compiler *, const char *);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200188static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
190
191static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
192static int compiler_visit_stmt(struct compiler *, stmt_ty);
193static int compiler_visit_keyword(struct compiler *, keyword_ty);
194static int compiler_visit_expr(struct compiler *, expr_ty);
195static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700196static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200197static int compiler_subscript(struct compiler *, expr_ty);
198static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000199
Andy Lester76d58772020-03-10 21:18:12 -0500200static int inplace_binop(operator_ty);
Brandt Bucher6dd9b642019-11-25 22:16:53 -0800201static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200202static int expr_constant(expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000203
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500204static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400205static int compiler_async_with(struct compiler *, stmt_ty, int);
206static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100207static int compiler_call_helper(struct compiler *c, int n,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000208 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -0400209 asdl_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500210static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400211static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000212
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700213static int compiler_sync_comprehension_generator(
214 struct compiler *c,
215 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200216 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700217 expr_ty elt, expr_ty val, int type);
218
219static int compiler_async_comprehension_generator(
220 struct compiler *c,
221 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200222 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700223 expr_ty elt, expr_ty val, int type);
224
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000225static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000226static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400228#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000229
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000230PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000231_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 /* Name mangling: __private becomes _classname__private.
234 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200235 PyObject *result;
236 size_t nlen, plen, ipriv;
237 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200239 PyUnicode_READ_CHAR(ident, 0) != '_' ||
240 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 Py_INCREF(ident);
242 return ident;
243 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200244 nlen = PyUnicode_GET_LENGTH(ident);
245 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000247
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 The only time a name with a dot can occur is when
249 we are compiling an import statement that has a
250 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000251
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 TODO(jhylton): Decide whether we want to support
253 mangling of the module name, e.g. __M.X.
254 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200255 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
256 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
257 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000258 Py_INCREF(ident);
259 return ident; /* Don't mangle __whatever__ */
260 }
261 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200262 ipriv = 0;
263 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
264 ipriv++;
265 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 Py_INCREF(ident);
267 return ident; /* Don't mangle if class is just underscores */
268 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200269 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000270
Antoine Pitrou55bff892013-04-06 21:21:04 +0200271 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
272 PyErr_SetString(PyExc_OverflowError,
273 "private identifier too large to be mangled");
274 return NULL;
275 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000276
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200277 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
278 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
279 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
280
281 result = PyUnicode_New(1 + nlen + plen, maxchar);
282 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000283 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200284 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
285 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200286 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
287 Py_DECREF(result);
288 return NULL;
289 }
290 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
291 Py_DECREF(result);
292 return NULL;
293 }
Victor Stinner8f825062012-04-27 13:55:39 +0200294 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200295 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000296}
297
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000298static int
299compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000300{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000302
INADA Naokic2e16072018-11-26 21:23:22 +0900303 c->c_const_cache = PyDict_New();
304 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900306 }
307
308 c->c_stack = PyList_New(0);
309 if (!c->c_stack) {
310 Py_CLEAR(c->c_const_cache);
311 return 0;
312 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000313
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000315}
316
317PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200318PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
319 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 struct compiler c;
322 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200323 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000325
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 if (!__doc__) {
327 __doc__ = PyUnicode_InternFromString("__doc__");
328 if (!__doc__)
329 return NULL;
330 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000331 if (!__annotations__) {
332 __annotations__ = PyUnicode_InternFromString("__annotations__");
333 if (!__annotations__)
334 return NULL;
335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000336 if (!compiler_init(&c))
337 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200338 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 c.c_filename = filename;
340 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200341 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000342 if (c.c_future == NULL)
343 goto finally;
344 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 flags = &local_flags;
346 }
347 merged = c.c_future->ff_features | flags->cf_flags;
348 c.c_future->ff_features = merged;
349 flags->cf_flags = merged;
350 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200351 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 c.c_nestlevel = 0;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100353 c.c_do_not_emit_bytecode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354
Pablo Galindod112c602020-03-18 23:02:09 +0000355 _PyASTOptimizeState state;
356 state.optimize = c.c_optimize;
357 state.ff_features = merged;
358
359 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900360 goto finally;
361 }
362
Victor Stinner14e461d2013-08-26 22:28:21 +0200363 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 if (c.c_st == NULL) {
365 if (!PyErr_Occurred())
366 PyErr_SetString(PyExc_SystemError, "no symtable");
367 goto finally;
368 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371
Thomas Wouters1175c432006-02-27 22:49:54 +0000372 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 compiler_free(&c);
374 assert(co || PyErr_Occurred());
375 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
378PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200379PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
380 int optimize, PyArena *arena)
381{
382 PyObject *filename;
383 PyCodeObject *co;
384 filename = PyUnicode_DecodeFSDefault(filename_str);
385 if (filename == NULL)
386 return NULL;
387 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
388 Py_DECREF(filename);
389 return co;
390
391}
392
393PyCodeObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394PyNode_Compile(struct _node *n, const char *filename)
395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 PyCodeObject *co = NULL;
397 mod_ty mod;
398 PyArena *arena = PyArena_New();
399 if (!arena)
400 return NULL;
401 mod = PyAST_FromNode(n, NULL, filename, arena);
402 if (mod)
403 co = PyAST_Compile(mod, filename, NULL, arena);
404 PyArena_Free(arena);
405 return co;
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000406}
407
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000408static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 if (c->c_st)
412 PySymtable_Free(c->c_st);
413 if (c->c_future)
414 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200415 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900416 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000418}
419
Guido van Rossum79f25d91997-04-29 20:08:16 +0000420static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000421list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000422{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000423 Py_ssize_t i, n;
424 PyObject *v, *k;
425 PyObject *dict = PyDict_New();
426 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000427
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 n = PyList_Size(list);
429 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100430 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 if (!v) {
432 Py_DECREF(dict);
433 return NULL;
434 }
435 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300436 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 Py_DECREF(v);
438 Py_DECREF(dict);
439 return NULL;
440 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000441 Py_DECREF(v);
442 }
443 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444}
445
446/* Return new dict containing names from src that match scope(s).
447
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000448src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000449either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000450values are integers, starting at offset and increasing by one for
451each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452*/
453
454static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100455dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700457 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500459 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000461 assert(offset >= 0);
462 if (dest == NULL)
463 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464
Meador Inge2ca63152012-07-18 14:20:11 -0500465 /* Sort the keys so that we have a deterministic order on the indexes
466 saved in the returned dictionary. These indexes are used as indexes
467 into the free and cell var storage. Therefore if they aren't
468 deterministic, then the generated bytecode is not deterministic.
469 */
470 sorted_keys = PyDict_Keys(src);
471 if (sorted_keys == NULL)
472 return NULL;
473 if (PyList_Sort(sorted_keys) != 0) {
474 Py_DECREF(sorted_keys);
475 return NULL;
476 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500477 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500478
479 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000480 /* XXX this should probably be a macro in symtable.h */
481 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500482 k = PyList_GET_ITEM(sorted_keys, key_i);
483 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 assert(PyLong_Check(v));
485 vi = PyLong_AS_LONG(v);
486 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300489 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500491 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 Py_DECREF(dest);
493 return NULL;
494 }
495 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300496 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500497 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 Py_DECREF(item);
499 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 return NULL;
501 }
502 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 }
504 }
Meador Inge2ca63152012-07-18 14:20:11 -0500505 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000507}
508
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000509static void
510compiler_unit_check(struct compiler_unit *u)
511{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 basicblock *block;
513 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700514 assert((uintptr_t)block != 0xcbcbcbcbU);
515 assert((uintptr_t)block != 0xfbfbfbfbU);
516 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 if (block->b_instr != NULL) {
518 assert(block->b_ialloc > 0);
519 assert(block->b_iused > 0);
520 assert(block->b_ialloc >= block->b_iused);
521 }
522 else {
523 assert (block->b_iused == 0);
524 assert (block->b_ialloc == 0);
525 }
526 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000527}
528
529static void
530compiler_unit_free(struct compiler_unit *u)
531{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000533
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 compiler_unit_check(u);
535 b = u->u_blocks;
536 while (b != NULL) {
537 if (b->b_instr)
538 PyObject_Free((void *)b->b_instr);
539 next = b->b_list;
540 PyObject_Free((void *)b);
541 b = next;
542 }
543 Py_CLEAR(u->u_ste);
544 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400545 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000546 Py_CLEAR(u->u_consts);
547 Py_CLEAR(u->u_names);
548 Py_CLEAR(u->u_varnames);
549 Py_CLEAR(u->u_freevars);
550 Py_CLEAR(u->u_cellvars);
551 Py_CLEAR(u->u_private);
552 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553}
554
555static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100556compiler_enter_scope(struct compiler *c, identifier name,
557 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000558{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100560 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561
Andy Lester7668a8b2020-03-24 23:26:44 -0500562 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000563 struct compiler_unit));
564 if (!u) {
565 PyErr_NoMemory();
566 return 0;
567 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100568 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000569 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100570 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 u->u_kwonlyargcount = 0;
572 u->u_ste = PySymtable_Lookup(c->c_st, key);
573 if (!u->u_ste) {
574 compiler_unit_free(u);
575 return 0;
576 }
577 Py_INCREF(name);
578 u->u_name = name;
579 u->u_varnames = list2dict(u->u_ste->ste_varnames);
580 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
581 if (!u->u_varnames || !u->u_cellvars) {
582 compiler_unit_free(u);
583 return 0;
584 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500585 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000586 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500587 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300588 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500589 int res;
590 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200591 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500592 name = _PyUnicode_FromId(&PyId___class__);
593 if (!name) {
594 compiler_unit_free(u);
595 return 0;
596 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300597 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500598 if (res < 0) {
599 compiler_unit_free(u);
600 return 0;
601 }
602 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200605 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 if (!u->u_freevars) {
607 compiler_unit_free(u);
608 return 0;
609 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611 u->u_blocks = NULL;
612 u->u_nfblocks = 0;
613 u->u_firstlineno = lineno;
614 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000615 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616 u->u_consts = PyDict_New();
617 if (!u->u_consts) {
618 compiler_unit_free(u);
619 return 0;
620 }
621 u->u_names = PyDict_New();
622 if (!u->u_names) {
623 compiler_unit_free(u);
624 return 0;
625 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 /* Push the old compiler_unit on the stack. */
630 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400631 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
633 Py_XDECREF(capsule);
634 compiler_unit_free(u);
635 return 0;
636 }
637 Py_DECREF(capsule);
638 u->u_private = c->u->u_private;
639 Py_XINCREF(u->u_private);
640 }
641 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100644
645 block = compiler_new_block(c);
646 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100648 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400650 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
651 if (!compiler_set_qualname(c))
652 return 0;
653 }
654
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000656}
657
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000658static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000659compiler_exit_scope(struct compiler *c)
660{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100661 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 c->c_nestlevel--;
665 compiler_unit_free(c->u);
666 /* Restore c->u to the parent unit. */
667 n = PyList_GET_SIZE(c->c_stack) - 1;
668 if (n >= 0) {
669 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400670 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 assert(c->u);
672 /* we are deleting from a list so this really shouldn't fail */
673 if (PySequence_DelItem(c->c_stack, n) < 0)
674 Py_FatalError("compiler_exit_scope()");
675 compiler_unit_check(c->u);
676 }
677 else
678 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680}
681
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400682static int
683compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100684{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100685 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400686 _Py_static_string(dot_locals, ".<locals>");
687 Py_ssize_t stack_size;
688 struct compiler_unit *u = c->u;
689 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100690
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400691 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100692 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400693 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400694 if (stack_size > 1) {
695 int scope, force_global = 0;
696 struct compiler_unit *parent;
697 PyObject *mangled, *capsule;
698
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400699 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400700 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400701 assert(parent);
702
Yury Selivanov75445082015-05-11 22:57:16 -0400703 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
704 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
705 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400706 assert(u->u_name);
707 mangled = _Py_Mangle(parent->u_private, u->u_name);
708 if (!mangled)
709 return 0;
710 scope = PyST_GetScope(parent->u_ste, mangled);
711 Py_DECREF(mangled);
712 assert(scope != GLOBAL_IMPLICIT);
713 if (scope == GLOBAL_EXPLICIT)
714 force_global = 1;
715 }
716
717 if (!force_global) {
718 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400719 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400720 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
721 dot_locals_str = _PyUnicode_FromId(&dot_locals);
722 if (dot_locals_str == NULL)
723 return 0;
724 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
725 if (base == NULL)
726 return 0;
727 }
728 else {
729 Py_INCREF(parent->u_qualname);
730 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400731 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100732 }
733 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400734
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400735 if (base != NULL) {
736 dot_str = _PyUnicode_FromId(&dot);
737 if (dot_str == NULL) {
738 Py_DECREF(base);
739 return 0;
740 }
741 name = PyUnicode_Concat(base, dot_str);
742 Py_DECREF(base);
743 if (name == NULL)
744 return 0;
745 PyUnicode_Append(&name, u->u_name);
746 if (name == NULL)
747 return 0;
748 }
749 else {
750 Py_INCREF(u->u_name);
751 name = u->u_name;
752 }
753 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100754
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400755 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100756}
757
Eric V. Smith235a6f02015-09-19 14:51:32 -0400758
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759/* Allocate a new block and return a pointer to it.
760 Returns NULL on error.
761*/
762
763static basicblock *
764compiler_new_block(struct compiler *c)
765{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 basicblock *b;
767 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500770 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000771 if (b == NULL) {
772 PyErr_NoMemory();
773 return NULL;
774 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 /* Extend the singly linked list of blocks with new block. */
776 b->b_list = u->u_blocks;
777 u->u_blocks = b;
778 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000779}
780
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000781static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000782compiler_next_block(struct compiler *c)
783{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 basicblock *block = compiler_new_block(c);
785 if (block == NULL)
786 return NULL;
787 c->u->u_curblock->b_next = block;
788 c->u->u_curblock = block;
789 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790}
791
792static basicblock *
793compiler_use_next_block(struct compiler *c, basicblock *block)
794{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 assert(block != NULL);
796 c->u->u_curblock->b_next = block;
797 c->u->u_curblock = block;
798 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000799}
800
801/* Returns the offset of the next instruction in the current block's
802 b_instr array. Resizes the b_instr as necessary.
803 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000804*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000805
806static int
Andy Lester76d58772020-03-10 21:18:12 -0500807compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000809 assert(b != NULL);
810 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500811 b->b_instr = (struct instr *)PyObject_Calloc(
812 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000813 if (b->b_instr == NULL) {
814 PyErr_NoMemory();
815 return -1;
816 }
817 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 }
819 else if (b->b_iused == b->b_ialloc) {
820 struct instr *tmp;
821 size_t oldsize, newsize;
822 oldsize = b->b_ialloc * sizeof(struct instr);
823 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000824
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700825 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 PyErr_NoMemory();
827 return -1;
828 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000829
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 if (newsize == 0) {
831 PyErr_NoMemory();
832 return -1;
833 }
834 b->b_ialloc <<= 1;
835 tmp = (struct instr *)PyObject_Realloc(
836 (void *)b->b_instr, newsize);
837 if (tmp == NULL) {
838 PyErr_NoMemory();
839 return -1;
840 }
841 b->b_instr = tmp;
842 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
843 }
844 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000845}
846
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200847/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000848
Christian Heimes2202f872008-02-06 14:31:34 +0000849 The line number is reset in the following cases:
850 - when entering a new scope
851 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200852 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200853 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000854*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000855
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200856#define SET_LOC(c, x) \
857 (c)->u->u_lineno = (x)->lineno; \
858 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200860/* Return the stack effect of opcode with argument oparg.
861
862 Some opcodes have different stack effect when jump to the target and
863 when not jump. The 'jump' parameter specifies the case:
864
865 * 0 -- when not jump
866 * 1 -- when jump
867 * -1 -- maximal
868 */
869/* XXX Make the stack effect of WITH_CLEANUP_START and
870 WITH_CLEANUP_FINISH deterministic. */
871static int
872stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300875 case NOP:
876 case EXTENDED_ARG:
877 return 0;
878
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200879 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 case POP_TOP:
881 return -1;
882 case ROT_TWO:
883 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200884 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 return 0;
886 case DUP_TOP:
887 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000888 case DUP_TOP_TWO:
889 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000890
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200891 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 case UNARY_POSITIVE:
893 case UNARY_NEGATIVE:
894 case UNARY_NOT:
895 case UNARY_INVERT:
896 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 case SET_ADD:
899 case LIST_APPEND:
900 return -1;
901 case MAP_ADD:
902 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000903
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200904 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 case BINARY_POWER:
906 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400907 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000908 case BINARY_MODULO:
909 case BINARY_ADD:
910 case BINARY_SUBTRACT:
911 case BINARY_SUBSCR:
912 case BINARY_FLOOR_DIVIDE:
913 case BINARY_TRUE_DIVIDE:
914 return -1;
915 case INPLACE_FLOOR_DIVIDE:
916 case INPLACE_TRUE_DIVIDE:
917 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 case INPLACE_ADD:
920 case INPLACE_SUBTRACT:
921 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400922 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 case INPLACE_MODULO:
924 return -1;
925 case STORE_SUBSCR:
926 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 case DELETE_SUBSCR:
928 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 case BINARY_LSHIFT:
931 case BINARY_RSHIFT:
932 case BINARY_AND:
933 case BINARY_XOR:
934 case BINARY_OR:
935 return -1;
936 case INPLACE_POWER:
937 return -1;
938 case GET_ITER:
939 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 case PRINT_EXPR:
942 return -1;
943 case LOAD_BUILD_CLASS:
944 return 1;
945 case INPLACE_LSHIFT:
946 case INPLACE_RSHIFT:
947 case INPLACE_AND:
948 case INPLACE_XOR:
949 case INPLACE_OR:
950 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200953 /* 1 in the normal flow.
954 * Restore the stack position and push 6 values before jumping to
955 * the handler if an exception be raised. */
956 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000957 case RETURN_VALUE:
958 return -1;
959 case IMPORT_STAR:
960 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700961 case SETUP_ANNOTATIONS:
962 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 case YIELD_VALUE:
964 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500965 case YIELD_FROM:
966 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 case POP_BLOCK:
968 return 0;
969 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200970 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 case STORE_NAME:
973 return -1;
974 case DELETE_NAME:
975 return 0;
976 case UNPACK_SEQUENCE:
977 return oparg-1;
978 case UNPACK_EX:
979 return (oparg&0xFF) + (oparg>>8);
980 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200981 /* -1 at end of iterator, 1 if continue iterating. */
982 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 case STORE_ATTR:
985 return -2;
986 case DELETE_ATTR:
987 return -1;
988 case STORE_GLOBAL:
989 return -1;
990 case DELETE_GLOBAL:
991 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 case LOAD_CONST:
993 return 1;
994 case LOAD_NAME:
995 return 1;
996 case BUILD_TUPLE:
997 case BUILD_LIST:
998 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +0300999 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001000 return 1-oparg;
1001 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001002 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001003 case BUILD_CONST_KEY_MAP:
1004 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 case LOAD_ATTR:
1006 return 0;
1007 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001008 case IS_OP:
1009 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001011 case JUMP_IF_NOT_EXC_MATCH:
1012 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 case IMPORT_NAME:
1014 return -1;
1015 case IMPORT_FROM:
1016 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001017
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001018 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 case JUMP_ABSOLUTE:
1021 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001022
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001023 case JUMP_IF_TRUE_OR_POP:
1024 case JUMP_IF_FALSE_OR_POP:
1025 return jump ? 0 : -1;
1026
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 case POP_JUMP_IF_FALSE:
1028 case POP_JUMP_IF_TRUE:
1029 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 case LOAD_GLOBAL:
1032 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001034 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001036 /* 0 in the normal flow.
1037 * Restore the stack position and push 6 values before jumping to
1038 * the handler if an exception be raised. */
1039 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001040 case RERAISE:
1041 return -3;
1042
1043 case WITH_EXCEPT_START:
1044 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001045
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 case LOAD_FAST:
1047 return 1;
1048 case STORE_FAST:
1049 return -1;
1050 case DELETE_FAST:
1051 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 case RAISE_VARARGS:
1054 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001055
1056 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001057 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001058 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001059 case CALL_METHOD:
1060 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001062 return -oparg-1;
1063 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001064 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001065 case MAKE_FUNCTION:
1066 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1067 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 case BUILD_SLICE:
1069 if (oparg == 3)
1070 return -2;
1071 else
1072 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001074 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 case LOAD_CLOSURE:
1076 return 1;
1077 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001078 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001079 return 1;
1080 case STORE_DEREF:
1081 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001082 case DELETE_DEREF:
1083 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001084
1085 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001086 case GET_AWAITABLE:
1087 return 0;
1088 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001089 /* 0 in the normal flow.
1090 * Restore the stack position to the position before the result
1091 * of __aenter__ and push 6 values before jumping to the handler
1092 * if an exception be raised. */
1093 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001094 case BEFORE_ASYNC_WITH:
1095 return 1;
1096 case GET_AITER:
1097 return 0;
1098 case GET_ANEXT:
1099 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001100 case GET_YIELD_FROM_ITER:
1101 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001102 case END_ASYNC_FOR:
1103 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001104 case FORMAT_VALUE:
1105 /* If there's a fmt_spec on the stack, we go from 2->1,
1106 else 1->1. */
1107 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001108 case LOAD_METHOD:
1109 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001110 case LOAD_ASSERTION_ERROR:
1111 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001112 case LIST_TO_TUPLE:
1113 return 0;
1114 case LIST_EXTEND:
1115 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001116 case DICT_MERGE:
1117 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001118 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001120 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 }
Larry Hastings3a907972013-11-23 14:49:22 -08001122 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123}
1124
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001125int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001126PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1127{
1128 return stack_effect(opcode, oparg, jump);
1129}
1130
1131int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001132PyCompile_OpcodeStackEffect(int opcode, int oparg)
1133{
1134 return stack_effect(opcode, oparg, -1);
1135}
1136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001137/* Add an opcode with no argument.
1138 Returns 0 on failure, 1 on success.
1139*/
1140
1141static int
1142compiler_addop(struct compiler *c, int opcode)
1143{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 basicblock *b;
1145 struct instr *i;
1146 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001147 assert(!HAS_ARG(opcode));
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001148 if (c->c_do_not_emit_bytecode) {
1149 return 1;
1150 }
Andy Lester76d58772020-03-10 21:18:12 -05001151 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 if (off < 0)
1153 return 0;
1154 b = c->u->u_curblock;
1155 i = &b->b_instr[off];
1156 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001157 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001158 if (opcode == RETURN_VALUE)
1159 b->b_return = 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001160 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001162}
1163
Victor Stinnerf8e32212013-11-19 23:56:34 +01001164static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001165compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001167 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001168 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001169
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001170 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001172 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001174 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001175 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001176 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001178 return -1;
1179 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001180 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 Py_DECREF(v);
1182 return -1;
1183 }
1184 Py_DECREF(v);
1185 }
1186 else
1187 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001188 return arg;
1189}
1190
INADA Naokic2e16072018-11-26 21:23:22 +09001191// Merge const *o* recursively and return constant key object.
1192static PyObject*
1193merge_consts_recursive(struct compiler *c, PyObject *o)
1194{
1195 // None and Ellipsis are singleton, and key is the singleton.
1196 // No need to merge object and key.
1197 if (o == Py_None || o == Py_Ellipsis) {
1198 Py_INCREF(o);
1199 return o;
1200 }
1201
1202 PyObject *key = _PyCode_ConstantKey(o);
1203 if (key == NULL) {
1204 return NULL;
1205 }
1206
1207 // t is borrowed reference
1208 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1209 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001210 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001211 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001212 Py_DECREF(key);
1213 return t;
1214 }
1215
INADA Naokif7e4d362018-11-29 00:58:46 +09001216 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001217 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001218 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001219 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001220 Py_ssize_t len = PyTuple_GET_SIZE(o);
1221 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001222 PyObject *item = PyTuple_GET_ITEM(o, i);
1223 PyObject *u = merge_consts_recursive(c, item);
1224 if (u == NULL) {
1225 Py_DECREF(key);
1226 return NULL;
1227 }
1228
1229 // See _PyCode_ConstantKey()
1230 PyObject *v; // borrowed
1231 if (PyTuple_CheckExact(u)) {
1232 v = PyTuple_GET_ITEM(u, 1);
1233 }
1234 else {
1235 v = u;
1236 }
1237 if (v != item) {
1238 Py_INCREF(v);
1239 PyTuple_SET_ITEM(o, i, v);
1240 Py_DECREF(item);
1241 }
1242
1243 Py_DECREF(u);
1244 }
1245 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001246 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001247 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001248 // constant keys.
1249 // See _PyCode_ConstantKey() for detail.
1250 assert(PyTuple_CheckExact(key));
1251 assert(PyTuple_GET_SIZE(key) == 2);
1252
1253 Py_ssize_t len = PySet_GET_SIZE(o);
1254 if (len == 0) { // empty frozenset should not be re-created.
1255 return key;
1256 }
1257 PyObject *tuple = PyTuple_New(len);
1258 if (tuple == NULL) {
1259 Py_DECREF(key);
1260 return NULL;
1261 }
1262 Py_ssize_t i = 0, pos = 0;
1263 PyObject *item;
1264 Py_hash_t hash;
1265 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1266 PyObject *k = merge_consts_recursive(c, item);
1267 if (k == NULL) {
1268 Py_DECREF(tuple);
1269 Py_DECREF(key);
1270 return NULL;
1271 }
1272 PyObject *u;
1273 if (PyTuple_CheckExact(k)) {
1274 u = PyTuple_GET_ITEM(k, 1);
1275 Py_INCREF(u);
1276 Py_DECREF(k);
1277 }
1278 else {
1279 u = k;
1280 }
1281 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1282 i++;
1283 }
1284
1285 // Instead of rewriting o, we create new frozenset and embed in the
1286 // key tuple. Caller should get merged frozenset from the key tuple.
1287 PyObject *new = PyFrozenSet_New(tuple);
1288 Py_DECREF(tuple);
1289 if (new == NULL) {
1290 Py_DECREF(key);
1291 return NULL;
1292 }
1293 assert(PyTuple_GET_ITEM(key, 1) == o);
1294 Py_DECREF(o);
1295 PyTuple_SET_ITEM(key, 1, new);
1296 }
INADA Naokic2e16072018-11-26 21:23:22 +09001297
1298 return key;
1299}
1300
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001301static Py_ssize_t
1302compiler_add_const(struct compiler *c, PyObject *o)
1303{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001304 if (c->c_do_not_emit_bytecode) {
1305 return 0;
1306 }
1307
INADA Naokic2e16072018-11-26 21:23:22 +09001308 PyObject *key = merge_consts_recursive(c, o);
1309 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001310 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001311 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001312
Andy Lester76d58772020-03-10 21:18:12 -05001313 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001314 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001316}
1317
1318static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001319compiler_addop_load_const(struct compiler *c, PyObject *o)
1320{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001321 if (c->c_do_not_emit_bytecode) {
1322 return 1;
1323 }
1324
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001325 Py_ssize_t arg = compiler_add_const(c, o);
1326 if (arg < 0)
1327 return 0;
1328 return compiler_addop_i(c, LOAD_CONST, arg);
1329}
1330
1331static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001332compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001334{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001335 if (c->c_do_not_emit_bytecode) {
1336 return 1;
1337 }
1338
Andy Lester76d58772020-03-10 21:18:12 -05001339 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001340 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001341 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001342 return compiler_addop_i(c, opcode, arg);
1343}
1344
1345static int
1346compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001348{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001349 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001350
1351 if (c->c_do_not_emit_bytecode) {
1352 return 1;
1353 }
1354
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001355 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1356 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001357 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001358 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001359 Py_DECREF(mangled);
1360 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001361 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001362 return compiler_addop_i(c, opcode, arg);
1363}
1364
1365/* Add an opcode with an integer argument.
1366 Returns 0 on failure, 1 on success.
1367*/
1368
1369static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001370compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 struct instr *i;
1373 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001374
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001375 if (c->c_do_not_emit_bytecode) {
1376 return 1;
1377 }
1378
Victor Stinner2ad474b2016-03-01 23:34:47 +01001379 /* oparg value is unsigned, but a signed C int is usually used to store
1380 it in the C code (like Python/ceval.c).
1381
1382 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1383
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001384 The argument of a concrete bytecode instruction is limited to 8-bit.
1385 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1386 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001387 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001388
Andy Lester76d58772020-03-10 21:18:12 -05001389 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 if (off < 0)
1391 return 0;
1392 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001393 i->i_opcode = opcode;
1394 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001395 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001397}
1398
1399static int
1400compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute)
1401{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 struct instr *i;
1403 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001404
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001405 if (c->c_do_not_emit_bytecode) {
1406 return 1;
1407 }
1408
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001409 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001410 assert(b != NULL);
Andy Lester76d58772020-03-10 21:18:12 -05001411 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 if (off < 0)
1413 return 0;
1414 i = &c->u->u_curblock->b_instr[off];
1415 i->i_opcode = opcode;
1416 i->i_target = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001417 if (absolute)
1418 i->i_jabs = 1;
1419 else
1420 i->i_jrel = 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001421 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001423}
1424
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001425/* NEXT_BLOCK() creates an implicit jump from the current block
1426 to the new block.
1427
1428 The returns inside this macro make it impossible to decref objects
1429 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 if (compiler_next_block((C)) == NULL) \
1433 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001434}
1435
1436#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (!compiler_addop((C), (OP))) \
1438 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439}
1440
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001441#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (!compiler_addop((C), (OP))) { \
1443 compiler_exit_scope(c); \
1444 return 0; \
1445 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001446}
1447
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001448#define ADDOP_LOAD_CONST(C, O) { \
1449 if (!compiler_addop_load_const((C), (O))) \
1450 return 0; \
1451}
1452
1453/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1454#define ADDOP_LOAD_CONST_NEW(C, O) { \
1455 PyObject *__new_const = (O); \
1456 if (__new_const == NULL) { \
1457 return 0; \
1458 } \
1459 if (!compiler_addop_load_const((C), __new_const)) { \
1460 Py_DECREF(__new_const); \
1461 return 0; \
1462 } \
1463 Py_DECREF(__new_const); \
1464}
1465
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001466#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1468 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001469}
1470
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001471/* Same as ADDOP_O, but steals a reference. */
1472#define ADDOP_N(C, OP, O, TYPE) { \
1473 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1474 Py_DECREF((O)); \
1475 return 0; \
1476 } \
1477 Py_DECREF((O)); \
1478}
1479
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001480#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001481 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1482 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001483}
1484
1485#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (!compiler_addop_i((C), (OP), (O))) \
1487 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490#define ADDOP_JABS(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (!compiler_addop_j((C), (OP), (O), 1)) \
1492 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
1495#define ADDOP_JREL(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 if (!compiler_addop_j((C), (OP), (O), 0)) \
1497 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498}
1499
Mark Shannon9af0e472020-01-14 10:12:45 +00001500
1501#define ADDOP_COMPARE(C, CMP) { \
1502 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1503 return 0; \
1504}
1505
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1507 the ASDL name to synthesize the name of the C type and the visit function.
1508*/
1509
1510#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 if (!compiler_visit_ ## TYPE((C), (V))) \
1512 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001513}
1514
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001515#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516 if (!compiler_visit_ ## TYPE((C), (V))) { \
1517 compiler_exit_scope(c); \
1518 return 0; \
1519 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001520}
1521
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (!compiler_visit_slice((C), (V), (CTX))) \
1524 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525}
1526
1527#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 int _i; \
1529 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1530 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1531 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1532 if (!compiler_visit_ ## TYPE((C), elt)) \
1533 return 0; \
1534 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001537#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 int _i; \
1539 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1540 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1541 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1542 if (!compiler_visit_ ## TYPE((C), elt)) { \
1543 compiler_exit_scope(c); \
1544 return 0; \
1545 } \
1546 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001547}
1548
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001549/* These macros allows to check only for errors and not emmit bytecode
1550 * while visiting nodes.
1551*/
1552
1553#define BEGIN_DO_NOT_EMIT_BYTECODE { \
1554 c->c_do_not_emit_bytecode++;
1555
1556#define END_DO_NOT_EMIT_BYTECODE \
1557 c->c_do_not_emit_bytecode--; \
1558}
1559
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001560/* Search if variable annotations are present statically in a block. */
1561
1562static int
1563find_ann(asdl_seq *stmts)
1564{
1565 int i, j, res = 0;
1566 stmt_ty st;
1567
1568 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1569 st = (stmt_ty)asdl_seq_GET(stmts, i);
1570 switch (st->kind) {
1571 case AnnAssign_kind:
1572 return 1;
1573 case For_kind:
1574 res = find_ann(st->v.For.body) ||
1575 find_ann(st->v.For.orelse);
1576 break;
1577 case AsyncFor_kind:
1578 res = find_ann(st->v.AsyncFor.body) ||
1579 find_ann(st->v.AsyncFor.orelse);
1580 break;
1581 case While_kind:
1582 res = find_ann(st->v.While.body) ||
1583 find_ann(st->v.While.orelse);
1584 break;
1585 case If_kind:
1586 res = find_ann(st->v.If.body) ||
1587 find_ann(st->v.If.orelse);
1588 break;
1589 case With_kind:
1590 res = find_ann(st->v.With.body);
1591 break;
1592 case AsyncWith_kind:
1593 res = find_ann(st->v.AsyncWith.body);
1594 break;
1595 case Try_kind:
1596 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1597 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1598 st->v.Try.handlers, j);
1599 if (find_ann(handler->v.ExceptHandler.body)) {
1600 return 1;
1601 }
1602 }
1603 res = find_ann(st->v.Try.body) ||
1604 find_ann(st->v.Try.finalbody) ||
1605 find_ann(st->v.Try.orelse);
1606 break;
1607 default:
1608 res = 0;
1609 }
1610 if (res) {
1611 break;
1612 }
1613 }
1614 return res;
1615}
1616
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001617/*
1618 * Frame block handling functions
1619 */
1620
1621static int
1622compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001623 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001624{
1625 struct fblockinfo *f;
1626 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
Irit Katriel48a9c0e2020-11-17 19:31:55 +00001627 return compiler_error(c, "too many statically nested blocks");
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001628 }
1629 f = &c->u->u_fblock[c->u->u_nfblocks++];
1630 f->fb_type = t;
1631 f->fb_block = b;
1632 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001633 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001634 return 1;
1635}
1636
1637static void
1638compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1639{
1640 struct compiler_unit *u = c->u;
1641 assert(u->u_nfblocks > 0);
1642 u->u_nfblocks--;
1643 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1644 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1645}
1646
Mark Shannonfee55262019-11-21 09:11:43 +00001647static int
1648compiler_call_exit_with_nones(struct compiler *c) {
1649 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1650 ADDOP(c, DUP_TOP);
1651 ADDOP(c, DUP_TOP);
1652 ADDOP_I(c, CALL_FUNCTION, 3);
1653 return 1;
1654}
1655
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001656/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001657 * popping the blocks will be restored afterwards, unless another
1658 * return, break or continue is found. In which case, the TOS will
1659 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001660 */
1661static int
1662compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1663 int preserve_tos)
1664{
1665 switch (info->fb_type) {
1666 case WHILE_LOOP:
Irit Katriel48a9c0e2020-11-17 19:31:55 +00001667 case EXCEPTION_HANDLER:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001668 return 1;
1669
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001670 case FOR_LOOP:
1671 /* Pop the iterator */
1672 if (preserve_tos) {
1673 ADDOP(c, ROT_TWO);
1674 }
1675 ADDOP(c, POP_TOP);
1676 return 1;
1677
Irit Katriel48a9c0e2020-11-17 19:31:55 +00001678 case TRY_EXCEPT:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001679 ADDOP(c, POP_BLOCK);
1680 return 1;
1681
1682 case FINALLY_TRY:
1683 ADDOP(c, POP_BLOCK);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001684 if (preserve_tos) {
Mark Shannonfee55262019-11-21 09:11:43 +00001685 if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) {
1686 return 0;
1687 }
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001688 }
Mark Shannon88dce262019-12-30 09:53:36 +00001689 /* Emit the finally block, restoring the line number when done */
1690 int saved_lineno = c->u->u_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001691 VISIT_SEQ(c, stmt, info->fb_datum);
Mark Shannon88dce262019-12-30 09:53:36 +00001692 c->u->u_lineno = saved_lineno;
Mark Shannonfee55262019-11-21 09:11:43 +00001693 if (preserve_tos) {
1694 compiler_pop_fblock(c, POP_VALUE, NULL);
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001695 }
1696 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001697
Mark Shannonfee55262019-11-21 09:11:43 +00001698 case FINALLY_END:
1699 if (preserve_tos) {
1700 ADDOP(c, ROT_FOUR);
1701 }
1702 ADDOP(c, POP_TOP);
1703 ADDOP(c, POP_TOP);
1704 ADDOP(c, POP_TOP);
1705 if (preserve_tos) {
1706 ADDOP(c, ROT_FOUR);
1707 }
1708 ADDOP(c, POP_EXCEPT);
1709 return 1;
Serhiy Storchakaef61c522019-08-24 13:11:52 +03001710
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001711 case WITH:
1712 case ASYNC_WITH:
1713 ADDOP(c, POP_BLOCK);
1714 if (preserve_tos) {
1715 ADDOP(c, ROT_TWO);
1716 }
Mark Shannonfee55262019-11-21 09:11:43 +00001717 if(!compiler_call_exit_with_nones(c)) {
1718 return 0;
1719 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001720 if (info->fb_type == ASYNC_WITH) {
1721 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001722 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001723 ADDOP(c, YIELD_FROM);
1724 }
Mark Shannonfee55262019-11-21 09:11:43 +00001725 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001726 return 1;
1727
1728 case HANDLER_CLEANUP:
Mark Shannonfee55262019-11-21 09:11:43 +00001729 if (info->fb_datum) {
1730 ADDOP(c, POP_BLOCK);
1731 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001732 if (preserve_tos) {
1733 ADDOP(c, ROT_FOUR);
1734 }
Mark Shannonfee55262019-11-21 09:11:43 +00001735 ADDOP(c, POP_EXCEPT);
1736 if (info->fb_datum) {
1737 ADDOP_LOAD_CONST(c, Py_None);
1738 compiler_nameop(c, info->fb_datum, Store);
1739 compiler_nameop(c, info->fb_datum, Del);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001740 }
Mark Shannonfee55262019-11-21 09:11:43 +00001741 return 1;
1742
1743 case POP_VALUE:
1744 if (preserve_tos) {
1745 ADDOP(c, ROT_TWO);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001746 }
Mark Shannonfee55262019-11-21 09:11:43 +00001747 ADDOP(c, POP_TOP);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001748 return 1;
1749 }
1750 Py_UNREACHABLE();
1751}
1752
Mark Shannonfee55262019-11-21 09:11:43 +00001753/** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */
1754static int
1755compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) {
1756 if (c->u->u_nfblocks == 0) {
1757 return 1;
1758 }
1759 struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1];
1760 if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) {
1761 *loop = top;
1762 return 1;
1763 }
1764 struct fblockinfo copy = *top;
1765 c->u->u_nfblocks--;
1766 if (!compiler_unwind_fblock(c, &copy, preserve_tos)) {
1767 return 0;
1768 }
1769 if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) {
1770 return 0;
1771 }
1772 c->u->u_fblock[c->u->u_nfblocks] = copy;
1773 c->u->u_nfblocks++;
1774 return 1;
1775}
1776
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001777/* Compile a sequence of statements, checking for a docstring
1778 and for annotations. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001779
1780static int
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001781compiler_body(struct compiler *c, asdl_seq *stmts)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001782{
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001783 int i = 0;
1784 stmt_ty st;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001785 PyObject *docstring;
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001786
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001787 /* Set current line number to the line number of first statement.
1788 This way line number for SETUP_ANNOTATIONS will always
1789 coincide with the line number of first "real" statement in module.
Hansraj Das01171eb2019-10-09 07:54:02 +05301790 If body is empty, then lineno will be set later in assemble. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001791 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) {
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001792 st = (stmt_ty)asdl_seq_GET(stmts, 0);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001793 SET_LOC(c, st);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001794 }
1795 /* Every annotated class and module should have __annotations__. */
1796 if (find_ann(stmts)) {
1797 ADDOP(c, SETUP_ANNOTATIONS);
1798 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001799 if (!asdl_seq_LEN(stmts))
1800 return 1;
INADA Naokicb41b272017-02-23 00:31:59 +09001801 /* if not -OO mode, set docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03001802 if (c->c_optimize < 2) {
1803 docstring = _PyAST_GetDocString(stmts);
1804 if (docstring) {
1805 i = 1;
1806 st = (stmt_ty)asdl_seq_GET(stmts, 0);
1807 assert(st->kind == Expr_kind);
1808 VISIT(c, expr, st->v.Expr.value);
1809 if (!compiler_nameop(c, __doc__, Store))
1810 return 0;
1811 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812 }
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001813 for (; i < asdl_seq_LEN(stmts); i++)
1814 VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816}
1817
1818static PyCodeObject *
1819compiler_mod(struct compiler *c, mod_ty mod)
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 PyCodeObject *co;
1822 int addNone = 1;
1823 static PyObject *module;
1824 if (!module) {
1825 module = PyUnicode_InternFromString("<module>");
1826 if (!module)
1827 return NULL;
1828 }
1829 /* Use 0 for firstlineno initially, will fixup in assemble(). */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001830 if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831 return NULL;
1832 switch (mod->kind) {
1833 case Module_kind:
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03001834 if (!compiler_body(c, mod->v.Module.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001835 compiler_exit_scope(c);
1836 return 0;
1837 }
1838 break;
1839 case Interactive_kind:
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001840 if (find_ann(mod->v.Interactive.body)) {
1841 ADDOP(c, SETUP_ANNOTATIONS);
1842 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001843 c->c_interactive = 1;
1844 VISIT_SEQ_IN_SCOPE(c, stmt,
1845 mod->v.Interactive.body);
1846 break;
1847 case Expression_kind:
1848 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1849 addNone = 0;
1850 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851 default:
1852 PyErr_Format(PyExc_SystemError,
1853 "module kind %d should not be possible",
1854 mod->kind);
1855 return 0;
1856 }
1857 co = assemble(c, addNone);
1858 compiler_exit_scope(c);
1859 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001860}
1861
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001862/* The test for LOCAL must come before the test for FREE in order to
1863 handle classes where name is both local and free. The local var is
1864 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001865*/
1866
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867static int
1868get_ref_type(struct compiler *c, PyObject *name)
1869{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001870 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001871 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001872 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001873 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001874 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001875 if (scope == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001876 _Py_FatalErrorFormat(__func__,
1877 "unknown scope for %.100s in %.100s(%s)\n"
1878 "symbols: %s\nlocals: %s\nglobals: %s",
1879 PyUnicode_AsUTF8(name),
1880 PyUnicode_AsUTF8(c->u->u_name),
1881 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1882 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1883 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1884 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001885 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001888}
1889
1890static int
1891compiler_lookup_arg(PyObject *dict, PyObject *name)
1892{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001893 PyObject *v;
1894 v = PyDict_GetItem(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001895 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001896 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001897 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001898}
1899
1900static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001901compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001902{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001903 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001904 if (qualname == NULL)
1905 qualname = co->co_name;
1906
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001907 if (free) {
1908 for (i = 0; i < free; ++i) {
1909 /* Bypass com_addop_varname because it will generate
1910 LOAD_DEREF but LOAD_CLOSURE is needed.
1911 */
1912 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1913 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001914
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001915 /* Special case: If a class contains a method with a
1916 free variable that has the same name as a method,
1917 the name will be considered free *and* local in the
1918 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001919 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001920 */
1921 reftype = get_ref_type(c, name);
1922 if (reftype == CELL)
1923 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1924 else /* (reftype == FREE) */
1925 arg = compiler_lookup_arg(c->u->u_freevars, name);
1926 if (arg == -1) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001927 _Py_FatalErrorFormat(__func__,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001928 "lookup %s in %s %d %d\n"
1929 "freevars of %s: %s\n",
1930 PyUnicode_AsUTF8(PyObject_Repr(name)),
1931 PyUnicode_AsUTF8(c->u->u_name),
1932 reftype, arg,
1933 PyUnicode_AsUTF8(co->co_name),
1934 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001935 }
1936 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001937 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001938 flags |= 0x08;
1939 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001941 ADDOP_LOAD_CONST(c, (PyObject*)co);
1942 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001943 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001944 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001945}
1946
1947static int
1948compiler_decorators(struct compiler *c, asdl_seq* decos)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001951
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 if (!decos)
1953 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001954
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001955 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1956 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1957 }
1958 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001959}
1960
1961static int
Guido van Rossum4f72a782006-10-27 23:31:49 +00001962compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 asdl_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001964{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001965 /* Push a dict of keyword-only default values.
1966
1967 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1968 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001969 int i;
1970 PyObject *keys = NULL;
1971
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001972 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1973 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1974 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1975 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001976 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001977 if (!mangled) {
1978 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001979 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001980 if (keys == NULL) {
1981 keys = PyList_New(1);
1982 if (keys == NULL) {
1983 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001984 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001985 }
1986 PyList_SET_ITEM(keys, 0, mangled);
1987 }
1988 else {
1989 int res = PyList_Append(keys, mangled);
1990 Py_DECREF(mangled);
1991 if (res == -1) {
1992 goto error;
1993 }
1994 }
1995 if (!compiler_visit_expr(c, default_)) {
1996 goto error;
1997 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001998 }
1999 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002000 if (keys != NULL) {
2001 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2002 PyObject *keys_tuple = PyList_AsTuple(keys);
2003 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002004 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002005 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002006 assert(default_count > 0);
2007 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002008 }
2009 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002010 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002011 }
2012
2013error:
2014 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002015 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002016}
2017
2018static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002019compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2020{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002021 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002022 return 1;
2023}
2024
2025static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002026compiler_visit_argannotation(struct compiler *c, identifier id,
2027 expr_ty annotation, PyObject *names)
2028{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002029 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01002030 PyObject *mangled;
Guido van Rossum95e4d582018-01-26 08:20:18 -08002031 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2032 VISIT(c, annexpr, annotation)
2033 }
2034 else {
2035 VISIT(c, expr, annotation);
2036 }
Victor Stinner065efc32014-02-18 22:07:56 +01002037 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002038 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002039 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002040 if (PyList_Append(names, mangled) < 0) {
2041 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002042 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002043 }
2044 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002045 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002046 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002047}
2048
2049static int
2050compiler_visit_argannotations(struct compiler *c, asdl_seq* args,
2051 PyObject *names)
2052{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002053 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002054 for (i = 0; i < asdl_seq_LEN(args); i++) {
2055 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002056 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002057 c,
2058 arg->arg,
2059 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002060 names))
2061 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002062 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002063 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002064}
2065
2066static int
2067compiler_visit_annotations(struct compiler *c, arguments_ty args,
2068 expr_ty returns)
2069{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002070 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002071 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002072
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002073 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002074 */
2075 static identifier return_str;
2076 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002077 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002078 names = PyList_New(0);
2079 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002080 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002081
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002082 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002083 goto error;
Pablo Galindoa0c01bf2019-05-31 15:19:50 +01002084 if (!compiler_visit_argannotations(c, args->posonlyargs, names))
2085 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002086 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002087 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002088 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002089 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002090 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002091 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002092 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002093 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002094 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002095 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00002096
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002097 if (!return_str) {
2098 return_str = PyUnicode_InternFromString("return");
2099 if (!return_str)
2100 goto error;
2101 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002102 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002103 goto error;
2104 }
2105
2106 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002107 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002108 PyObject *keytuple = PyList_AsTuple(names);
2109 Py_DECREF(names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002110 ADDOP_LOAD_CONST_NEW(c, keytuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002111 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002112 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002113 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002114 else {
2115 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002116 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002117 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002118
2119error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002120 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002121 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002122}
2123
2124static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002125compiler_visit_defaults(struct compiler *c, arguments_ty args)
2126{
2127 VISIT_SEQ(c, expr, args->defaults);
2128 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2129 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002130}
2131
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002132static Py_ssize_t
2133compiler_default_arguments(struct compiler *c, arguments_ty args)
2134{
2135 Py_ssize_t funcflags = 0;
2136 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002137 if (!compiler_visit_defaults(c, args))
2138 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002139 funcflags |= 0x01;
2140 }
2141 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002142 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002143 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002144 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002145 return -1;
2146 }
2147 else if (res > 0) {
2148 funcflags |= 0x02;
2149 }
2150 }
2151 return funcflags;
2152}
2153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002154static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002155forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2156{
2157
2158 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2159 compiler_error(c, "cannot assign to __debug__");
2160 return 1;
2161 }
2162 return 0;
2163}
2164
2165static int
2166compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2167{
2168 if (arg != NULL) {
2169 if (forbidden_name(c, arg->arg, Store))
2170 return 0;
2171 }
2172 return 1;
2173}
2174
2175static int
2176compiler_check_debug_args_seq(struct compiler *c, asdl_seq *args)
2177{
2178 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002179 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002180 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2181 return 0;
2182 }
2183 }
2184 return 1;
2185}
2186
2187static int
2188compiler_check_debug_args(struct compiler *c, arguments_ty args)
2189{
2190 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2191 return 0;
2192 if (!compiler_check_debug_args_seq(c, args->args))
2193 return 0;
2194 if (!compiler_check_debug_one_arg(c, args->vararg))
2195 return 0;
2196 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2197 return 0;
2198 if (!compiler_check_debug_one_arg(c, args->kwarg))
2199 return 0;
2200 return 1;
2201}
2202
2203static int
Yury Selivanov75445082015-05-11 22:57:16 -04002204compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002205{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002206 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002207 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002208 arguments_ty args;
2209 expr_ty returns;
2210 identifier name;
2211 asdl_seq* decos;
2212 asdl_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002213 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002214 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002215 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002216 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002217
Yury Selivanov75445082015-05-11 22:57:16 -04002218 if (is_async) {
2219 assert(s->kind == AsyncFunctionDef_kind);
2220
2221 args = s->v.AsyncFunctionDef.args;
2222 returns = s->v.AsyncFunctionDef.returns;
2223 decos = s->v.AsyncFunctionDef.decorator_list;
2224 name = s->v.AsyncFunctionDef.name;
2225 body = s->v.AsyncFunctionDef.body;
2226
2227 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2228 } else {
2229 assert(s->kind == FunctionDef_kind);
2230
2231 args = s->v.FunctionDef.args;
2232 returns = s->v.FunctionDef.returns;
2233 decos = s->v.FunctionDef.decorator_list;
2234 name = s->v.FunctionDef.name;
2235 body = s->v.FunctionDef.body;
2236
2237 scope_type = COMPILER_SCOPE_FUNCTION;
2238 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002239
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002240 if (!compiler_check_debug_args(c, args))
2241 return 0;
2242
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002243 if (!compiler_decorators(c, decos))
2244 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002245
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002246 firstlineno = s->lineno;
2247 if (asdl_seq_LEN(decos)) {
2248 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2249 }
2250
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002251 funcflags = compiler_default_arguments(c, args);
2252 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002253 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002254 }
2255
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002256 annotations = compiler_visit_annotations(c, args, returns);
2257 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002258 return 0;
2259 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002260 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002261 funcflags |= 0x04;
2262 }
2263
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002264 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002265 return 0;
2266 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002267
INADA Naokicb41b272017-02-23 00:31:59 +09002268 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002269 if (c->c_optimize < 2) {
2270 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002271 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002272 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002273 compiler_exit_scope(c);
2274 return 0;
2275 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002277 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002278 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002279 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
INADA Naokicb41b272017-02-23 00:31:59 +09002280 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002281 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002282 qualname = c->u->u_qualname;
2283 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002284 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002285 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002286 Py_XDECREF(qualname);
2287 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002288 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002289 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002290
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002291 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002292 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002293 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002295 /* decorators */
2296 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2297 ADDOP_I(c, CALL_FUNCTION, 1);
2298 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002299
Yury Selivanov75445082015-05-11 22:57:16 -04002300 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002301}
2302
2303static int
2304compiler_class(struct compiler *c, stmt_ty s)
2305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002306 PyCodeObject *co;
2307 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002308 int i, firstlineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002309 asdl_seq* decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002310
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002311 if (!compiler_decorators(c, decos))
2312 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002313
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002314 firstlineno = s->lineno;
2315 if (asdl_seq_LEN(decos)) {
2316 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2317 }
2318
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002319 /* ultimately generate code for:
2320 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2321 where:
2322 <func> is a function/closure created from the class body;
2323 it has a single argument (__locals__) where the dict
2324 (or MutableSequence) representing the locals is passed
2325 <name> is the class name
2326 <bases> is the positional arguments and *varargs argument
2327 <keywords> is the keyword arguments and **kwds argument
2328 This borrows from compiler_call.
2329 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002330
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002331 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002332 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002333 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002334 return 0;
2335 /* this block represents what we do in the new scope */
2336 {
2337 /* use the class name for name mangling */
2338 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002339 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002340 /* load (global) __name__ ... */
2341 str = PyUnicode_InternFromString("__name__");
2342 if (!str || !compiler_nameop(c, str, Load)) {
2343 Py_XDECREF(str);
2344 compiler_exit_scope(c);
2345 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002346 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002347 Py_DECREF(str);
2348 /* ... and store it as __module__ */
2349 str = PyUnicode_InternFromString("__module__");
2350 if (!str || !compiler_nameop(c, str, Store)) {
2351 Py_XDECREF(str);
2352 compiler_exit_scope(c);
2353 return 0;
2354 }
2355 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002356 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002357 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002358 str = PyUnicode_InternFromString("__qualname__");
2359 if (!str || !compiler_nameop(c, str, Store)) {
2360 Py_XDECREF(str);
2361 compiler_exit_scope(c);
2362 return 0;
2363 }
2364 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002365 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002366 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002367 compiler_exit_scope(c);
2368 return 0;
2369 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002370 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002371 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002372 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002373 str = PyUnicode_InternFromString("__class__");
2374 if (str == NULL) {
2375 compiler_exit_scope(c);
2376 return 0;
2377 }
2378 i = compiler_lookup_arg(c->u->u_cellvars, str);
2379 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002380 if (i < 0) {
2381 compiler_exit_scope(c);
2382 return 0;
2383 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002384 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002385
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002386 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002387 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002388 str = PyUnicode_InternFromString("__classcell__");
2389 if (!str || !compiler_nameop(c, str, Store)) {
2390 Py_XDECREF(str);
2391 compiler_exit_scope(c);
2392 return 0;
2393 }
2394 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002395 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002396 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002397 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002398 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002399 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002400 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002401 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002402 /* create the code object */
2403 co = assemble(c, 1);
2404 }
2405 /* leave the new scope */
2406 compiler_exit_scope(c);
2407 if (co == NULL)
2408 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002410 /* 2. load the 'build_class' function */
2411 ADDOP(c, LOAD_BUILD_CLASS);
2412
2413 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002414 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002415 Py_DECREF(co);
2416
2417 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002418 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002419
2420 /* 5. generate the rest of the code for the call */
2421 if (!compiler_call_helper(c, 2,
2422 s->v.ClassDef.bases,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04002423 s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002424 return 0;
2425
2426 /* 6. apply decorators */
2427 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2428 ADDOP_I(c, CALL_FUNCTION, 1);
2429 }
2430
2431 /* 7. store into <name> */
2432 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2433 return 0;
2434 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002435}
2436
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002437/* Return 0 if the expression is a constant value except named singletons.
2438 Return 1 otherwise. */
2439static int
2440check_is_arg(expr_ty e)
2441{
2442 if (e->kind != Constant_kind) {
2443 return 1;
2444 }
2445 PyObject *value = e->v.Constant.value;
2446 return (value == Py_None
2447 || value == Py_False
2448 || value == Py_True
2449 || value == Py_Ellipsis);
2450}
2451
2452/* Check operands of identity chacks ("is" and "is not").
2453 Emit a warning if any operand is a constant except named singletons.
2454 Return 0 on error.
2455 */
2456static int
2457check_compare(struct compiler *c, expr_ty e)
2458{
2459 Py_ssize_t i, n;
2460 int left = check_is_arg(e->v.Compare.left);
2461 n = asdl_seq_LEN(e->v.Compare.ops);
2462 for (i = 0; i < n; i++) {
2463 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2464 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2465 if (op == Is || op == IsNot) {
2466 if (!right || !left) {
2467 const char *msg = (op == Is)
2468 ? "\"is\" with a literal. Did you mean \"==\"?"
2469 : "\"is not\" with a literal. Did you mean \"!=\"?";
2470 return compiler_warn(c, msg);
2471 }
2472 }
2473 left = right;
2474 }
2475 return 1;
2476}
2477
Mark Shannon9af0e472020-01-14 10:12:45 +00002478static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002479{
Mark Shannon9af0e472020-01-14 10:12:45 +00002480 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002481 switch (op) {
2482 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002483 cmp = Py_EQ;
2484 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002485 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002486 cmp = Py_NE;
2487 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002488 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002489 cmp = Py_LT;
2490 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002491 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002492 cmp = Py_LE;
2493 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002494 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002495 cmp = Py_GT;
2496 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002497 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002498 cmp = Py_GE;
2499 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002500 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002501 ADDOP_I(c, IS_OP, 0);
2502 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002503 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002504 ADDOP_I(c, IS_OP, 1);
2505 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002506 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002507 ADDOP_I(c, CONTAINS_OP, 0);
2508 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002509 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002510 ADDOP_I(c, CONTAINS_OP, 1);
2511 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002512 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002513 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002514 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002515 ADDOP_I(c, COMPARE_OP, cmp);
2516 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002517}
2518
Mark Shannon9af0e472020-01-14 10:12:45 +00002519
2520
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002521static int
2522compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2523{
2524 switch (e->kind) {
2525 case UnaryOp_kind:
2526 if (e->v.UnaryOp.op == Not)
2527 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2528 /* fallback to general implementation */
2529 break;
2530 case BoolOp_kind: {
2531 asdl_seq *s = e->v.BoolOp.values;
2532 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2533 assert(n >= 0);
2534 int cond2 = e->v.BoolOp.op == Or;
2535 basicblock *next2 = next;
2536 if (!cond2 != !cond) {
2537 next2 = compiler_new_block(c);
2538 if (next2 == NULL)
2539 return 0;
2540 }
2541 for (i = 0; i < n; ++i) {
2542 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2543 return 0;
2544 }
2545 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2546 return 0;
2547 if (next2 != next)
2548 compiler_use_next_block(c, next2);
2549 return 1;
2550 }
2551 case IfExp_kind: {
2552 basicblock *end, *next2;
2553 end = compiler_new_block(c);
2554 if (end == NULL)
2555 return 0;
2556 next2 = compiler_new_block(c);
2557 if (next2 == NULL)
2558 return 0;
2559 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2560 return 0;
2561 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2562 return 0;
2563 ADDOP_JREL(c, JUMP_FORWARD, end);
2564 compiler_use_next_block(c, next2);
2565 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2566 return 0;
2567 compiler_use_next_block(c, end);
2568 return 1;
2569 }
2570 case Compare_kind: {
2571 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2572 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002573 if (!check_compare(c, e)) {
2574 return 0;
2575 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002576 basicblock *cleanup = compiler_new_block(c);
2577 if (cleanup == NULL)
2578 return 0;
2579 VISIT(c, expr, e->v.Compare.left);
2580 for (i = 0; i < n; i++) {
2581 VISIT(c, expr,
2582 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2583 ADDOP(c, DUP_TOP);
2584 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002585 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002586 ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup);
2587 NEXT_BLOCK(c);
2588 }
2589 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002590 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002591 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2592 basicblock *end = compiler_new_block(c);
2593 if (end == NULL)
2594 return 0;
2595 ADDOP_JREL(c, JUMP_FORWARD, end);
2596 compiler_use_next_block(c, cleanup);
2597 ADDOP(c, POP_TOP);
2598 if (!cond) {
2599 ADDOP_JREL(c, JUMP_FORWARD, next);
2600 }
2601 compiler_use_next_block(c, end);
2602 return 1;
2603 }
2604 /* fallback to general implementation */
2605 break;
2606 }
2607 default:
2608 /* fallback to general implementation */
2609 break;
2610 }
2611
2612 /* general implementation */
2613 VISIT(c, expr, e);
2614 ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
2615 return 1;
2616}
2617
2618static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002619compiler_ifexp(struct compiler *c, expr_ty e)
2620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002621 basicblock *end, *next;
2622
2623 assert(e->kind == IfExp_kind);
2624 end = compiler_new_block(c);
2625 if (end == NULL)
2626 return 0;
2627 next = compiler_new_block(c);
2628 if (next == NULL)
2629 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002630 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2631 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002632 VISIT(c, expr, e->v.IfExp.body);
2633 ADDOP_JREL(c, JUMP_FORWARD, end);
2634 compiler_use_next_block(c, next);
2635 VISIT(c, expr, e->v.IfExp.orelse);
2636 compiler_use_next_block(c, end);
2637 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002638}
2639
2640static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002641compiler_lambda(struct compiler *c, expr_ty e)
2642{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002643 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002644 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002645 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002646 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002647 arguments_ty args = e->v.Lambda.args;
2648 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002649
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002650 if (!compiler_check_debug_args(c, args))
2651 return 0;
2652
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002653 if (!name) {
2654 name = PyUnicode_InternFromString("<lambda>");
2655 if (!name)
2656 return 0;
2657 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002658
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002659 funcflags = compiler_default_arguments(c, args);
2660 if (funcflags == -1) {
2661 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002662 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002663
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002664 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002665 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002666 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002667
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 /* Make None the first constant, so the lambda can't have a
2669 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002670 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002671 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002673 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002674 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002675 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2676 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2677 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002678 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002679 }
2680 else {
2681 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002682 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002684 qualname = c->u->u_qualname;
2685 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002686 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002687 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002688 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002689
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002690 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002691 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002692 Py_DECREF(co);
2693
2694 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695}
2696
2697static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002698compiler_if(struct compiler *c, stmt_ty s)
2699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002700 basicblock *end, *next;
2701 int constant;
2702 assert(s->kind == If_kind);
2703 end = compiler_new_block(c);
2704 if (end == NULL)
2705 return 0;
2706
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002707 constant = expr_constant(s->v.If.test);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002708 /* constant = 0: "if 0"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002709 * constant = 1: "if 1", "if 2", ...
2710 * constant = -1: rest */
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002711 if (constant == 0) {
2712 BEGIN_DO_NOT_EMIT_BYTECODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002713 VISIT_SEQ(c, stmt, s->v.If.body);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002714 END_DO_NOT_EMIT_BYTECODE
2715 if (s->v.If.orelse) {
2716 VISIT_SEQ(c, stmt, s->v.If.orelse);
2717 }
2718 } else if (constant == 1) {
2719 VISIT_SEQ(c, stmt, s->v.If.body);
2720 if (s->v.If.orelse) {
2721 BEGIN_DO_NOT_EMIT_BYTECODE
2722 VISIT_SEQ(c, stmt, s->v.If.orelse);
2723 END_DO_NOT_EMIT_BYTECODE
2724 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002725 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002726 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002727 next = compiler_new_block(c);
2728 if (next == NULL)
2729 return 0;
2730 }
Mark Shannonfee55262019-11-21 09:11:43 +00002731 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002732 next = end;
Mark Shannonfee55262019-11-21 09:11:43 +00002733 }
2734 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002735 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002736 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002738 if (asdl_seq_LEN(s->v.If.orelse)) {
2739 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002740 compiler_use_next_block(c, next);
2741 VISIT_SEQ(c, stmt, s->v.If.orelse);
2742 }
2743 }
2744 compiler_use_next_block(c, end);
2745 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002746}
2747
2748static int
2749compiler_for(struct compiler *c, stmt_ty s)
2750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002751 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002753 start = compiler_new_block(c);
2754 cleanup = compiler_new_block(c);
2755 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002756 if (start == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002758 }
2759 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002760 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002761 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002762 VISIT(c, expr, s->v.For.iter);
2763 ADDOP(c, GET_ITER);
2764 compiler_use_next_block(c, start);
2765 ADDOP_JREL(c, FOR_ITER, cleanup);
2766 VISIT(c, expr, s->v.For.target);
2767 VISIT_SEQ(c, stmt, s->v.For.body);
2768 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2769 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002770
2771 compiler_pop_fblock(c, FOR_LOOP, start);
2772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002773 VISIT_SEQ(c, stmt, s->v.For.orelse);
2774 compiler_use_next_block(c, end);
2775 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002776}
2777
Yury Selivanov75445082015-05-11 22:57:16 -04002778
2779static int
2780compiler_async_for(struct compiler *c, stmt_ty s)
2781{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002782 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002783 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002784 c->u->u_ste->ste_coroutine = 1;
2785 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002786 return compiler_error(c, "'async for' outside async function");
2787 }
2788
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002789 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002790 except = compiler_new_block(c);
2791 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002792
Mark Shannonfee55262019-11-21 09:11:43 +00002793 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002794 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002795 }
Yury Selivanov75445082015-05-11 22:57:16 -04002796 VISIT(c, expr, s->v.AsyncFor.iter);
2797 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002798
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002799 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002800 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002801 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002802 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002803 /* SETUP_FINALLY to guard the __anext__ call */
2804 ADDOP_JREL(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002805 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002806 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002807 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002808 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002809
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002810 /* Success block for __anext__ */
2811 VISIT(c, expr, s->v.AsyncFor.target);
2812 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
2813 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
2814
2815 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002816
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002817 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002818 compiler_use_next_block(c, except);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002819 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002820
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002821 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002822 VISIT_SEQ(c, stmt, s->v.For.orelse);
2823
2824 compiler_use_next_block(c, end);
2825
2826 return 1;
2827}
2828
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002829static int
2830compiler_while(struct compiler *c, stmt_ty s)
2831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002833 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002835 if (constant == 0) {
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002836 BEGIN_DO_NOT_EMIT_BYTECODE
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002837 // Push a dummy block so the VISIT_SEQ knows that we are
2838 // inside a while loop so it can correctly evaluate syntax
2839 // errors.
Mark Shannonfee55262019-11-21 09:11:43 +00002840 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) {
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002841 return 0;
2842 }
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002843 VISIT_SEQ(c, stmt, s->v.While.body);
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002844 // Remove the dummy block now that is not needed.
2845 compiler_pop_fblock(c, WHILE_LOOP, NULL);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002846 END_DO_NOT_EMIT_BYTECODE
2847 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002848 VISIT_SEQ(c, stmt, s->v.While.orelse);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002849 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002850 return 1;
2851 }
2852 loop = compiler_new_block(c);
2853 end = compiler_new_block(c);
2854 if (constant == -1) {
2855 anchor = compiler_new_block(c);
2856 if (anchor == NULL)
2857 return 0;
2858 }
2859 if (loop == NULL || end == NULL)
2860 return 0;
2861 if (s->v.While.orelse) {
2862 orelse = compiler_new_block(c);
2863 if (orelse == NULL)
2864 return 0;
2865 }
2866 else
2867 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002869 compiler_use_next_block(c, loop);
Mark Shannonfee55262019-11-21 09:11:43 +00002870 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002871 return 0;
2872 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002873 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2874 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002875 }
2876 VISIT_SEQ(c, stmt, s->v.While.body);
2877 ADDOP_JABS(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002878
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002879 /* XXX should the two POP instructions be in a separate block
2880 if there is no else clause ?
2881 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002882
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002883 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002885 compiler_pop_fblock(c, WHILE_LOOP, loop);
2886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002887 if (orelse != NULL) /* what if orelse is just pass? */
2888 VISIT_SEQ(c, stmt, s->v.While.orelse);
2889 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002890
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002891 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002892}
2893
2894static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002895compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002896{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002897 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002898 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002899 if (c->u->u_ste->ste_type != FunctionBlock)
2900 return compiler_error(c, "'return' outside function");
2901 if (s->v.Return.value != NULL &&
2902 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2903 {
2904 return compiler_error(
2905 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002906 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002907 if (preserve_tos) {
2908 VISIT(c, expr, s->v.Return.value);
2909 }
Mark Shannonfee55262019-11-21 09:11:43 +00002910 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2911 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002912 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002913 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002914 }
2915 else if (!preserve_tos) {
2916 VISIT(c, expr, s->v.Return.value);
2917 }
2918 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002919
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002920 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002921}
2922
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002923static int
2924compiler_break(struct compiler *c)
2925{
Mark Shannonfee55262019-11-21 09:11:43 +00002926 struct fblockinfo *loop = NULL;
2927 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2928 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002929 }
Mark Shannonfee55262019-11-21 09:11:43 +00002930 if (loop == NULL) {
2931 return compiler_error(c, "'break' outside loop");
2932 }
2933 if (!compiler_unwind_fblock(c, loop, 0)) {
2934 return 0;
2935 }
2936 ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_exit);
2937 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002938}
2939
2940static int
2941compiler_continue(struct compiler *c)
2942{
Mark Shannonfee55262019-11-21 09:11:43 +00002943 struct fblockinfo *loop = NULL;
2944 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2945 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002946 }
Mark Shannonfee55262019-11-21 09:11:43 +00002947 if (loop == NULL) {
2948 return compiler_error(c, "'continue' not properly in loop");
2949 }
2950 ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_block);
2951 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002952}
2953
2954
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002955/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002956
2957 SETUP_FINALLY L
2958 <code for body>
2959 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002960 <code for finalbody>
2961 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002962 L:
2963 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002964 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002965
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002966 The special instructions use the block stack. Each block
2967 stack entry contains the instruction that created it (here
2968 SETUP_FINALLY), the level of the value stack at the time the
2969 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002970
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002972 Pushes the current value stack level and the label
2973 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002975 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002976
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002977 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002978 when a SETUP_FINALLY entry is found, the raised and the caught
2979 exceptions are pushed onto the value stack (and the exception
2980 condition is cleared), and the interpreter jumps to the label
2981 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002982*/
2983
2984static int
2985compiler_try_finally(struct compiler *c, stmt_ty s)
2986{
Mark Shannonfee55262019-11-21 09:11:43 +00002987 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002988
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002989 body = compiler_new_block(c);
2990 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002991 exit = compiler_new_block(c);
2992 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002993 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002994
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002995 /* `try` block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 ADDOP_JREL(c, SETUP_FINALLY, end);
2997 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002998 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002999 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003000 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
3001 if (!compiler_try_except(c, s))
3002 return 0;
3003 }
3004 else {
3005 VISIT_SEQ(c, stmt, s->v.Try.body);
3006 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003007 ADDOP(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003008 compiler_pop_fblock(c, FINALLY_TRY, body);
3009 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3010 ADDOP_JREL(c, JUMP_FORWARD, exit);
3011 /* `finally` block */
3012 compiler_use_next_block(c, end);
3013 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3014 return 0;
3015 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3016 compiler_pop_fblock(c, FINALLY_END, end);
3017 ADDOP(c, RERAISE);
3018 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003019 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003020}
3021
3022/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003023 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003024 (The contents of the value stack is shown in [], with the top
3025 at the right; 'tb' is trace-back info, 'val' the exception's
3026 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027
3028 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003029 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003030 [] <code for S>
3031 [] POP_BLOCK
3032 [] JUMP_FORWARD L0
3033
3034 [tb, val, exc] L1: DUP )
3035 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003036 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003037 [tb, val, exc] POP
3038 [tb, val] <assign to V1> (or POP if no V1)
3039 [tb] POP
3040 [] <code for S1>
3041 JUMP_FORWARD L0
3042
3043 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003044 .............................etc.......................
3045
Mark Shannonfee55262019-11-21 09:11:43 +00003046 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003047
3048 [] L0: <next statement>
3049
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003050 Of course, parts are not generated if Vi or Ei is not present.
3051*/
3052static int
3053compiler_try_except(struct compiler *c, stmt_ty s)
3054{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003056 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003058 body = compiler_new_block(c);
3059 except = compiler_new_block(c);
3060 orelse = compiler_new_block(c);
3061 end = compiler_new_block(c);
3062 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3063 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003064 ADDOP_JREL(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003065 compiler_use_next_block(c, body);
Irit Katriel48a9c0e2020-11-17 19:31:55 +00003066 if (!compiler_push_fblock(c, TRY_EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003067 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003068 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003069 ADDOP(c, POP_BLOCK);
Irit Katriel48a9c0e2020-11-17 19:31:55 +00003070 compiler_pop_fblock(c, TRY_EXCEPT, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003071 ADDOP_JREL(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003072 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003073 compiler_use_next_block(c, except);
Irit Katriel48a9c0e2020-11-17 19:31:55 +00003074 /* Runtime will push a block here, so we need to account for that */
3075 if (!compiler_push_fblock(c, EXCEPTION_HANDLER, NULL, NULL, NULL))
3076 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 for (i = 0; i < n; i++) {
3078 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003079 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003080 if (!handler->v.ExceptHandler.type && i < n-1)
3081 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003082 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003083 except = compiler_new_block(c);
3084 if (except == NULL)
3085 return 0;
3086 if (handler->v.ExceptHandler.type) {
3087 ADDOP(c, DUP_TOP);
3088 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon9af0e472020-01-14 10:12:45 +00003089 ADDOP_JABS(c, JUMP_IF_NOT_EXC_MATCH, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003090 }
3091 ADDOP(c, POP_TOP);
3092 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003093 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003094
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003095 cleanup_end = compiler_new_block(c);
3096 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003097 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003098 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003099 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003100
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003101 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3102 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003103
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003104 /*
3105 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003106 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003107 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003108 try:
3109 # body
3110 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003111 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003112 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003113 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003115 /* second try: */
3116 ADDOP_JREL(c, SETUP_FINALLY, cleanup_end);
3117 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003118 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003119 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003120
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003121 /* second # body */
3122 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003123 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003124 ADDOP(c, POP_BLOCK);
3125 ADDOP(c, POP_EXCEPT);
3126 /* name = None; del name */
3127 ADDOP_LOAD_CONST(c, Py_None);
3128 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3129 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
3130 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003131
Mark Shannonfee55262019-11-21 09:11:43 +00003132 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003133 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003134
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003135 /* name = None; del name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003136 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003137 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003138 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003139
Mark Shannonfee55262019-11-21 09:11:43 +00003140 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003141 }
3142 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003143 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003144
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003145 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003146 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003147 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148
Guido van Rossumb940e112007-01-10 16:19:56 +00003149 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003150 ADDOP(c, POP_TOP);
3151 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003152 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003153 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003154 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003155 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003156 ADDOP(c, POP_EXCEPT);
3157 ADDOP_JREL(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003159 compiler_use_next_block(c, except);
3160 }
Irit Katriel48a9c0e2020-11-17 19:31:55 +00003161 compiler_pop_fblock(c, EXCEPTION_HANDLER, NULL);
Mark Shannonfee55262019-11-21 09:11:43 +00003162 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003163 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003164 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003165 compiler_use_next_block(c, end);
3166 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003167}
3168
3169static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003170compiler_try(struct compiler *c, stmt_ty s) {
3171 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3172 return compiler_try_finally(c, s);
3173 else
3174 return compiler_try_except(c, s);
3175}
3176
3177
3178static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003179compiler_import_as(struct compiler *c, identifier name, identifier asname)
3180{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003181 /* The IMPORT_NAME opcode was already generated. This function
3182 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003183
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003184 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003185 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003186 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003187 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3188 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003189 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003190 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003191 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003192 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003193 while (1) {
3194 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003195 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003196 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003197 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003198 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003199 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003200 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003201 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003202 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003203 if (dot == -1) {
3204 break;
3205 }
3206 ADDOP(c, ROT_TWO);
3207 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003208 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003209 if (!compiler_nameop(c, asname, Store)) {
3210 return 0;
3211 }
3212 ADDOP(c, POP_TOP);
3213 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 }
3215 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003216}
3217
3218static int
3219compiler_import(struct compiler *c, stmt_ty s)
3220{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003221 /* The Import node stores a module name like a.b.c as a single
3222 string. This is convenient for all cases except
3223 import a.b.c as d
3224 where we need to parse that string to extract the individual
3225 module names.
3226 XXX Perhaps change the representation to make this case simpler?
3227 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003228 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003230 for (i = 0; i < n; i++) {
3231 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3232 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003233
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003234 ADDOP_LOAD_CONST(c, _PyLong_Zero);
3235 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003236 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003238 if (alias->asname) {
3239 r = compiler_import_as(c, alias->name, alias->asname);
3240 if (!r)
3241 return r;
3242 }
3243 else {
3244 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003245 Py_ssize_t dot = PyUnicode_FindChar(
3246 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003247 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003248 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003249 if (tmp == NULL)
3250 return 0;
3251 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003252 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003253 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003254 Py_DECREF(tmp);
3255 }
3256 if (!r)
3257 return r;
3258 }
3259 }
3260 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003261}
3262
3263static int
3264compiler_from_import(struct compiler *c, stmt_ty s)
3265{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003266 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003267 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003268 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003270 if (!empty_string) {
3271 empty_string = PyUnicode_FromString("");
3272 if (!empty_string)
3273 return 0;
3274 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003275
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003276 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003277
3278 names = PyTuple_New(n);
3279 if (!names)
3280 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 /* build up the names */
3283 for (i = 0; i < n; i++) {
3284 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3285 Py_INCREF(alias->name);
3286 PyTuple_SET_ITEM(names, i, alias->name);
3287 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003288
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003289 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003290 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003291 Py_DECREF(names);
3292 return compiler_error(c, "from __future__ imports must occur "
3293 "at the beginning of the file");
3294 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003295 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003297 if (s->v.ImportFrom.module) {
3298 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3299 }
3300 else {
3301 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3302 }
3303 for (i = 0; i < n; i++) {
3304 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3305 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003306
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003307 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003308 assert(n == 1);
3309 ADDOP(c, IMPORT_STAR);
3310 return 1;
3311 }
3312
3313 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3314 store_name = alias->name;
3315 if (alias->asname)
3316 store_name = alias->asname;
3317
3318 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003319 return 0;
3320 }
3321 }
3322 /* remove imported module */
3323 ADDOP(c, POP_TOP);
3324 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003325}
3326
3327static int
3328compiler_assert(struct compiler *c, stmt_ty s)
3329{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003330 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003331
Georg Brandl8334fd92010-12-04 10:26:46 +00003332 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003333 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003334 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003335 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3336 {
3337 if (!compiler_warn(c, "assertion is always true, "
3338 "perhaps remove parentheses?"))
3339 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003340 return 0;
3341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003343 end = compiler_new_block(c);
3344 if (end == NULL)
3345 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003346 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3347 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003348 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003349 if (s->v.Assert.msg) {
3350 VISIT(c, expr, s->v.Assert.msg);
3351 ADDOP_I(c, CALL_FUNCTION, 1);
3352 }
3353 ADDOP_I(c, RAISE_VARARGS, 1);
3354 compiler_use_next_block(c, end);
3355 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003356}
3357
3358static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003359compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3360{
3361 if (c->c_interactive && c->c_nestlevel <= 1) {
3362 VISIT(c, expr, value);
3363 ADDOP(c, PRINT_EXPR);
3364 return 1;
3365 }
3366
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003367 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003368 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003369 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003370 }
3371
3372 VISIT(c, expr, value);
3373 ADDOP(c, POP_TOP);
3374 return 1;
3375}
3376
3377static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003378compiler_visit_stmt(struct compiler *c, stmt_ty s)
3379{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003380 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003381
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003382 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003383 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 switch (s->kind) {
3386 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003387 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003388 case ClassDef_kind:
3389 return compiler_class(c, s);
3390 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003391 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003392 case Delete_kind:
3393 VISIT_SEQ(c, expr, s->v.Delete.targets)
3394 break;
3395 case Assign_kind:
3396 n = asdl_seq_LEN(s->v.Assign.targets);
3397 VISIT(c, expr, s->v.Assign.value);
3398 for (i = 0; i < n; i++) {
3399 if (i < n - 1)
3400 ADDOP(c, DUP_TOP);
3401 VISIT(c, expr,
3402 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3403 }
3404 break;
3405 case AugAssign_kind:
3406 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003407 case AnnAssign_kind:
3408 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003409 case For_kind:
3410 return compiler_for(c, s);
3411 case While_kind:
3412 return compiler_while(c, s);
3413 case If_kind:
3414 return compiler_if(c, s);
3415 case Raise_kind:
3416 n = 0;
3417 if (s->v.Raise.exc) {
3418 VISIT(c, expr, s->v.Raise.exc);
3419 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003420 if (s->v.Raise.cause) {
3421 VISIT(c, expr, s->v.Raise.cause);
3422 n++;
3423 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003424 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003425 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003426 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003427 case Try_kind:
3428 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003429 case Assert_kind:
3430 return compiler_assert(c, s);
3431 case Import_kind:
3432 return compiler_import(c, s);
3433 case ImportFrom_kind:
3434 return compiler_from_import(c, s);
3435 case Global_kind:
3436 case Nonlocal_kind:
3437 break;
3438 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003439 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003440 case Pass_kind:
3441 break;
3442 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003443 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003444 case Continue_kind:
3445 return compiler_continue(c);
3446 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003447 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003448 case AsyncFunctionDef_kind:
3449 return compiler_function(c, s, 1);
3450 case AsyncWith_kind:
3451 return compiler_async_with(c, s, 0);
3452 case AsyncFor_kind:
3453 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003454 }
Yury Selivanov75445082015-05-11 22:57:16 -04003455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003456 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003457}
3458
3459static int
3460unaryop(unaryop_ty op)
3461{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003462 switch (op) {
3463 case Invert:
3464 return UNARY_INVERT;
3465 case Not:
3466 return UNARY_NOT;
3467 case UAdd:
3468 return UNARY_POSITIVE;
3469 case USub:
3470 return UNARY_NEGATIVE;
3471 default:
3472 PyErr_Format(PyExc_SystemError,
3473 "unary op %d should not be possible", op);
3474 return 0;
3475 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003476}
3477
3478static int
Andy Lester76d58772020-03-10 21:18:12 -05003479binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003480{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003481 switch (op) {
3482 case Add:
3483 return BINARY_ADD;
3484 case Sub:
3485 return BINARY_SUBTRACT;
3486 case Mult:
3487 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003488 case MatMult:
3489 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003490 case Div:
3491 return BINARY_TRUE_DIVIDE;
3492 case Mod:
3493 return BINARY_MODULO;
3494 case Pow:
3495 return BINARY_POWER;
3496 case LShift:
3497 return BINARY_LSHIFT;
3498 case RShift:
3499 return BINARY_RSHIFT;
3500 case BitOr:
3501 return BINARY_OR;
3502 case BitXor:
3503 return BINARY_XOR;
3504 case BitAnd:
3505 return BINARY_AND;
3506 case FloorDiv:
3507 return BINARY_FLOOR_DIVIDE;
3508 default:
3509 PyErr_Format(PyExc_SystemError,
3510 "binary op %d should not be possible", op);
3511 return 0;
3512 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003513}
3514
3515static int
Andy Lester76d58772020-03-10 21:18:12 -05003516inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003517{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003518 switch (op) {
3519 case Add:
3520 return INPLACE_ADD;
3521 case Sub:
3522 return INPLACE_SUBTRACT;
3523 case Mult:
3524 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003525 case MatMult:
3526 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003527 case Div:
3528 return INPLACE_TRUE_DIVIDE;
3529 case Mod:
3530 return INPLACE_MODULO;
3531 case Pow:
3532 return INPLACE_POWER;
3533 case LShift:
3534 return INPLACE_LSHIFT;
3535 case RShift:
3536 return INPLACE_RSHIFT;
3537 case BitOr:
3538 return INPLACE_OR;
3539 case BitXor:
3540 return INPLACE_XOR;
3541 case BitAnd:
3542 return INPLACE_AND;
3543 case FloorDiv:
3544 return INPLACE_FLOOR_DIVIDE;
3545 default:
3546 PyErr_Format(PyExc_SystemError,
3547 "inplace binary op %d should not be possible", op);
3548 return 0;
3549 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003550}
3551
3552static int
3553compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3554{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003555 int op, scope;
3556 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003557 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003558
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003559 PyObject *dict = c->u->u_names;
3560 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003561
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003562 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3563 !_PyUnicode_EqualToASCIIString(name, "True") &&
3564 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003565
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003566 if (forbidden_name(c, name, ctx))
3567 return 0;
3568
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003569 mangled = _Py_Mangle(c->u->u_private, name);
3570 if (!mangled)
3571 return 0;
3572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003573 op = 0;
3574 optype = OP_NAME;
3575 scope = PyST_GetScope(c->u->u_ste, mangled);
3576 switch (scope) {
3577 case FREE:
3578 dict = c->u->u_freevars;
3579 optype = OP_DEREF;
3580 break;
3581 case CELL:
3582 dict = c->u->u_cellvars;
3583 optype = OP_DEREF;
3584 break;
3585 case LOCAL:
3586 if (c->u->u_ste->ste_type == FunctionBlock)
3587 optype = OP_FAST;
3588 break;
3589 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003590 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003591 optype = OP_GLOBAL;
3592 break;
3593 case GLOBAL_EXPLICIT:
3594 optype = OP_GLOBAL;
3595 break;
3596 default:
3597 /* scope can be 0 */
3598 break;
3599 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003601 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003602 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003603
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003604 switch (optype) {
3605 case OP_DEREF:
3606 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003607 case Load:
3608 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3609 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003610 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003611 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 }
3613 break;
3614 case OP_FAST:
3615 switch (ctx) {
3616 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003617 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003618 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003620 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003621 return 1;
3622 case OP_GLOBAL:
3623 switch (ctx) {
3624 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003625 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 }
3628 break;
3629 case OP_NAME:
3630 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003631 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003632 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003634 }
3635 break;
3636 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003638 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003639 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003640 Py_DECREF(mangled);
3641 if (arg < 0)
3642 return 0;
3643 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003644}
3645
3646static int
3647compiler_boolop(struct compiler *c, expr_ty e)
3648{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003649 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003650 int jumpi;
3651 Py_ssize_t i, n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003652 asdl_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003654 assert(e->kind == BoolOp_kind);
3655 if (e->v.BoolOp.op == And)
3656 jumpi = JUMP_IF_FALSE_OR_POP;
3657 else
3658 jumpi = JUMP_IF_TRUE_OR_POP;
3659 end = compiler_new_block(c);
3660 if (end == NULL)
3661 return 0;
3662 s = e->v.BoolOp.values;
3663 n = asdl_seq_LEN(s) - 1;
3664 assert(n >= 0);
3665 for (i = 0; i < n; ++i) {
3666 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
3667 ADDOP_JABS(c, jumpi, end);
3668 }
3669 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3670 compiler_use_next_block(c, end);
3671 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003672}
3673
3674static int
Mark Shannon13bc1392020-01-23 09:25:17 +00003675starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed,
3676 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003677{
3678 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003679 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003680 if (n > 2 && are_all_items_const(elts, 0, n)) {
3681 PyObject *folded = PyTuple_New(n);
3682 if (folded == NULL) {
3683 return 0;
3684 }
3685 PyObject *val;
3686 for (i = 0; i < n; i++) {
3687 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3688 Py_INCREF(val);
3689 PyTuple_SET_ITEM(folded, i, val);
3690 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003691 if (tuple) {
3692 ADDOP_LOAD_CONST_NEW(c, folded);
3693 } else {
3694 if (add == SET_ADD) {
3695 Py_SETREF(folded, PyFrozenSet_New(folded));
3696 if (folded == NULL) {
3697 return 0;
3698 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003699 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003700 ADDOP_I(c, build, pushed);
3701 ADDOP_LOAD_CONST_NEW(c, folded);
3702 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003703 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003704 return 1;
3705 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003706
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003707 for (i = 0; i < n; i++) {
3708 expr_ty elt = asdl_seq_GET(elts, i);
3709 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003710 seen_star = 1;
3711 }
3712 }
3713 if (seen_star) {
3714 seen_star = 0;
3715 for (i = 0; i < n; i++) {
3716 expr_ty elt = asdl_seq_GET(elts, i);
3717 if (elt->kind == Starred_kind) {
3718 if (seen_star == 0) {
3719 ADDOP_I(c, build, i+pushed);
3720 seen_star = 1;
3721 }
3722 VISIT(c, expr, elt->v.Starred.value);
3723 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003724 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003725 else {
3726 VISIT(c, expr, elt);
3727 if (seen_star) {
3728 ADDOP_I(c, add, 1);
3729 }
3730 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003731 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003732 assert(seen_star);
3733 if (tuple) {
3734 ADDOP(c, LIST_TO_TUPLE);
3735 }
3736 }
3737 else {
3738 for (i = 0; i < n; i++) {
3739 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003740 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003741 }
3742 if (tuple) {
3743 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3744 } else {
3745 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003746 }
3747 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003748 return 1;
3749}
3750
3751static int
3752assignment_helper(struct compiler *c, asdl_seq *elts)
3753{
3754 Py_ssize_t n = asdl_seq_LEN(elts);
3755 Py_ssize_t i;
3756 int seen_star = 0;
3757 for (i = 0; i < n; i++) {
3758 expr_ty elt = asdl_seq_GET(elts, i);
3759 if (elt->kind == Starred_kind && !seen_star) {
3760 if ((i >= (1 << 8)) ||
3761 (n-i-1 >= (INT_MAX >> 8)))
3762 return compiler_error(c,
3763 "too many expressions in "
3764 "star-unpacking assignment");
3765 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3766 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003767 }
3768 else if (elt->kind == Starred_kind) {
3769 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003770 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003771 }
3772 }
3773 if (!seen_star) {
3774 ADDOP_I(c, UNPACK_SEQUENCE, n);
3775 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003776 for (i = 0; i < n; i++) {
3777 expr_ty elt = asdl_seq_GET(elts, i);
3778 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3779 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003780 return 1;
3781}
3782
3783static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003784compiler_list(struct compiler *c, expr_ty e)
3785{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 asdl_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003787 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003788 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003789 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003790 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003791 return starunpack_helper(c, elts, 0, BUILD_LIST,
3792 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003793 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003794 else
3795 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003796 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003797}
3798
3799static int
3800compiler_tuple(struct compiler *c, expr_ty e)
3801{
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003802 asdl_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003803 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003804 return assignment_helper(c, elts);
3805 }
3806 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003807 return starunpack_helper(c, elts, 0, BUILD_LIST,
3808 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003809 }
3810 else
3811 VISIT_SEQ(c, expr, elts);
3812 return 1;
3813}
3814
3815static int
3816compiler_set(struct compiler *c, expr_ty e)
3817{
Mark Shannon13bc1392020-01-23 09:25:17 +00003818 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3819 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003820}
3821
3822static int
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003823are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end)
3824{
3825 Py_ssize_t i;
3826 for (i = begin; i < end; i++) {
3827 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003828 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003829 return 0;
3830 }
3831 return 1;
3832}
3833
3834static int
3835compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3836{
3837 Py_ssize_t i, n = end - begin;
3838 PyObject *keys, *key;
3839 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3840 for (i = begin; i < end; i++) {
3841 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3842 }
3843 keys = PyTuple_New(n);
3844 if (keys == NULL) {
3845 return 0;
3846 }
3847 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003848 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003849 Py_INCREF(key);
3850 PyTuple_SET_ITEM(keys, i - begin, key);
3851 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003852 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003853 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3854 }
3855 else {
3856 for (i = begin; i < end; i++) {
3857 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3858 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3859 }
3860 ADDOP_I(c, BUILD_MAP, n);
3861 }
3862 return 1;
3863}
3864
3865static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003866compiler_dict(struct compiler *c, expr_ty e)
3867{
Victor Stinner976bb402016-03-23 11:36:19 +01003868 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003869 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003870 int is_unpacking = 0;
3871 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003872 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003873 elements = 0;
3874 for (i = 0; i < n; i++) {
3875 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003876 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003877 if (elements) {
3878 if (!compiler_subdict(c, e, i - elements, i)) {
3879 return 0;
3880 }
3881 if (have_dict) {
3882 ADDOP_I(c, DICT_UPDATE, 1);
3883 }
3884 have_dict = 1;
3885 elements = 0;
3886 }
3887 if (have_dict == 0) {
3888 ADDOP_I(c, BUILD_MAP, 0);
3889 have_dict = 1;
3890 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003891 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003892 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003893 }
3894 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003895 if (elements == 0xFFFF) {
Miss Islington (bot)d64d78b2020-09-04 16:38:50 -07003896 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003897 return 0;
3898 }
3899 if (have_dict) {
3900 ADDOP_I(c, DICT_UPDATE, 1);
3901 }
3902 have_dict = 1;
3903 elements = 0;
3904 }
3905 else {
3906 elements++;
3907 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003908 }
3909 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003910 if (elements) {
3911 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003912 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003913 }
3914 if (have_dict) {
3915 ADDOP_I(c, DICT_UPDATE, 1);
3916 }
3917 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003918 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003919 if (!have_dict) {
3920 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003921 }
3922 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003923}
3924
3925static int
3926compiler_compare(struct compiler *c, expr_ty e)
3927{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003928 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003929
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003930 if (!check_compare(c, e)) {
3931 return 0;
3932 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003933 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003934 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3935 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3936 if (n == 0) {
3937 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003938 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003939 }
3940 else {
3941 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003942 if (cleanup == NULL)
3943 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003944 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003945 VISIT(c, expr,
3946 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003947 ADDOP(c, DUP_TOP);
3948 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003949 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003950 ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup);
3951 NEXT_BLOCK(c);
3952 }
3953 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003954 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003955 basicblock *end = compiler_new_block(c);
3956 if (end == NULL)
3957 return 0;
3958 ADDOP_JREL(c, JUMP_FORWARD, end);
3959 compiler_use_next_block(c, cleanup);
3960 ADDOP(c, ROT_TWO);
3961 ADDOP(c, POP_TOP);
3962 compiler_use_next_block(c, end);
3963 }
3964 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003965}
3966
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003967static PyTypeObject *
3968infer_type(expr_ty e)
3969{
3970 switch (e->kind) {
3971 case Tuple_kind:
3972 return &PyTuple_Type;
3973 case List_kind:
3974 case ListComp_kind:
3975 return &PyList_Type;
3976 case Dict_kind:
3977 case DictComp_kind:
3978 return &PyDict_Type;
3979 case Set_kind:
3980 case SetComp_kind:
3981 return &PySet_Type;
3982 case GeneratorExp_kind:
3983 return &PyGen_Type;
3984 case Lambda_kind:
3985 return &PyFunction_Type;
3986 case JoinedStr_kind:
3987 case FormattedValue_kind:
3988 return &PyUnicode_Type;
3989 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003990 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003991 default:
3992 return NULL;
3993 }
3994}
3995
3996static int
3997check_caller(struct compiler *c, expr_ty e)
3998{
3999 switch (e->kind) {
4000 case Constant_kind:
4001 case Tuple_kind:
4002 case List_kind:
4003 case ListComp_kind:
4004 case Dict_kind:
4005 case DictComp_kind:
4006 case Set_kind:
4007 case SetComp_kind:
4008 case GeneratorExp_kind:
4009 case JoinedStr_kind:
4010 case FormattedValue_kind:
4011 return compiler_warn(c, "'%.200s' object is not callable; "
4012 "perhaps you missed a comma?",
4013 infer_type(e)->tp_name);
4014 default:
4015 return 1;
4016 }
4017}
4018
4019static int
4020check_subscripter(struct compiler *c, expr_ty e)
4021{
4022 PyObject *v;
4023
4024 switch (e->kind) {
4025 case Constant_kind:
4026 v = e->v.Constant.value;
4027 if (!(v == Py_None || v == Py_Ellipsis ||
4028 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4029 PyAnySet_Check(v)))
4030 {
4031 return 1;
4032 }
4033 /* fall through */
4034 case Set_kind:
4035 case SetComp_kind:
4036 case GeneratorExp_kind:
4037 case Lambda_kind:
4038 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4039 "perhaps you missed a comma?",
4040 infer_type(e)->tp_name);
4041 default:
4042 return 1;
4043 }
4044}
4045
4046static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004047check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004048{
4049 PyObject *v;
4050
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004051 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004052 if (index_type == NULL
4053 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4054 || index_type == &PySlice_Type) {
4055 return 1;
4056 }
4057
4058 switch (e->kind) {
4059 case Constant_kind:
4060 v = e->v.Constant.value;
4061 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4062 return 1;
4063 }
4064 /* fall through */
4065 case Tuple_kind:
4066 case List_kind:
4067 case ListComp_kind:
4068 case JoinedStr_kind:
4069 case FormattedValue_kind:
4070 return compiler_warn(c, "%.200s indices must be integers or slices, "
4071 "not %.200s; "
4072 "perhaps you missed a comma?",
4073 infer_type(e)->tp_name,
4074 index_type->tp_name);
4075 default:
4076 return 1;
4077 }
4078}
4079
Zackery Spytz97f5de02019-03-22 01:30:32 -06004080// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004081static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004082maybe_optimize_method_call(struct compiler *c, expr_ty e)
4083{
4084 Py_ssize_t argsl, i;
4085 expr_ty meth = e->v.Call.func;
4086 asdl_seq *args = e->v.Call.args;
4087
4088 /* Check that the call node is an attribute access, and that
4089 the call doesn't have keyword parameters. */
4090 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4091 asdl_seq_LEN(e->v.Call.keywords))
4092 return -1;
4093
4094 /* Check that there are no *varargs types of arguments. */
4095 argsl = asdl_seq_LEN(args);
4096 for (i = 0; i < argsl; i++) {
4097 expr_ty elt = asdl_seq_GET(args, i);
4098 if (elt->kind == Starred_kind) {
4099 return -1;
4100 }
4101 }
4102
4103 /* Alright, we can optimize the code. */
4104 VISIT(c, expr, meth->v.Attribute.value);
4105 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4106 VISIT_SEQ(c, expr, e->v.Call.args);
4107 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4108 return 1;
4109}
4110
4111static int
Zackery Spytz08050e92020-04-06 00:47:47 -06004112validate_keywords(struct compiler *c, asdl_seq *keywords)
4113{
4114 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4115 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004116 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4117 if (key->arg == NULL) {
4118 continue;
4119 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004120 if (forbidden_name(c, key->arg, Store)) {
4121 return -1;
4122 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004123 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004124 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4125 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4126 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4127 if (msg == NULL) {
4128 return -1;
4129 }
4130 c->u->u_col_offset = other->col_offset;
4131 compiler_error(c, PyUnicode_AsUTF8(msg));
4132 Py_DECREF(msg);
4133 return -1;
4134 }
4135 }
4136 }
4137 return 0;
4138}
4139
4140static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004141compiler_call(struct compiler *c, expr_ty e)
4142{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004143 int ret = maybe_optimize_method_call(c, e);
4144 if (ret >= 0) {
4145 return ret;
4146 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004147 if (!check_caller(c, e->v.Call.func)) {
4148 return 0;
4149 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004150 VISIT(c, expr, e->v.Call.func);
4151 return compiler_call_helper(c, 0,
4152 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004153 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004154}
4155
Eric V. Smith235a6f02015-09-19 14:51:32 -04004156static int
4157compiler_joined_str(struct compiler *c, expr_ty e)
4158{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004159 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004160 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4161 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004162 return 1;
4163}
4164
Eric V. Smitha78c7952015-11-03 12:45:05 -05004165/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004166static int
4167compiler_formatted_value(struct compiler *c, expr_ty e)
4168{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004169 /* Our oparg encodes 2 pieces of information: the conversion
4170 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004171
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004172 Convert the conversion char to 3 bits:
4173 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004174 !s : 001 0x1 FVC_STR
4175 !r : 010 0x2 FVC_REPR
4176 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004177
Eric V. Smitha78c7952015-11-03 12:45:05 -05004178 next bit is whether or not we have a format spec:
4179 yes : 100 0x4
4180 no : 000 0x0
4181 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004182
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004183 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004184 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004185
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004186 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004187 VISIT(c, expr, e->v.FormattedValue.value);
4188
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004189 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004190 case 's': oparg = FVC_STR; break;
4191 case 'r': oparg = FVC_REPR; break;
4192 case 'a': oparg = FVC_ASCII; break;
4193 case -1: oparg = FVC_NONE; break;
4194 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004195 PyErr_Format(PyExc_SystemError,
4196 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004197 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004198 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004199 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004200 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004201 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004202 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004203 }
4204
Eric V. Smitha78c7952015-11-03 12:45:05 -05004205 /* And push our opcode and oparg */
4206 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004207
Eric V. Smith235a6f02015-09-19 14:51:32 -04004208 return 1;
4209}
4210
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004211static int
4212compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
4213{
4214 Py_ssize_t i, n = end - begin;
4215 keyword_ty kw;
4216 PyObject *keys, *key;
4217 assert(n > 0);
4218 if (n > 1) {
4219 for (i = begin; i < end; i++) {
4220 kw = asdl_seq_GET(keywords, i);
4221 VISIT(c, expr, kw->value);
4222 }
4223 keys = PyTuple_New(n);
4224 if (keys == NULL) {
4225 return 0;
4226 }
4227 for (i = begin; i < end; i++) {
4228 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4229 Py_INCREF(key);
4230 PyTuple_SET_ITEM(keys, i - begin, key);
4231 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004232 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004233 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4234 }
4235 else {
4236 /* a for loop only executes once */
4237 for (i = begin; i < end; i++) {
4238 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004239 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004240 VISIT(c, expr, kw->value);
4241 }
4242 ADDOP_I(c, BUILD_MAP, n);
4243 }
4244 return 1;
4245}
4246
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004247/* shared code between compiler_call and compiler_class */
4248static int
4249compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004250 int n, /* Args already pushed */
Victor Stinnerad9a0662013-11-19 22:23:20 +01004251 asdl_seq *args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004252 asdl_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004253{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004254 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004255
Pablo Galindo254ec782020-04-03 20:37:13 +01004256 if (validate_keywords(c, keywords) == -1) {
4257 return 0;
4258 }
4259
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004260 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004261 nkwelts = asdl_seq_LEN(keywords);
4262
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004263 for (i = 0; i < nelts; i++) {
4264 expr_ty elt = asdl_seq_GET(args, i);
4265 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004266 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004267 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004268 }
4269 for (i = 0; i < nkwelts; i++) {
4270 keyword_ty kw = asdl_seq_GET(keywords, i);
4271 if (kw->arg == NULL) {
4272 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004273 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004274 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004275
Mark Shannon13bc1392020-01-23 09:25:17 +00004276 /* No * or ** args, so can use faster calling sequence */
4277 for (i = 0; i < nelts; i++) {
4278 expr_ty elt = asdl_seq_GET(args, i);
4279 assert(elt->kind != Starred_kind);
4280 VISIT(c, expr, elt);
4281 }
4282 if (nkwelts) {
4283 PyObject *names;
4284 VISIT_SEQ(c, keyword, keywords);
4285 names = PyTuple_New(nkwelts);
4286 if (names == NULL) {
4287 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004288 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004289 for (i = 0; i < nkwelts; i++) {
4290 keyword_ty kw = asdl_seq_GET(keywords, i);
4291 Py_INCREF(kw->arg);
4292 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004293 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004294 ADDOP_LOAD_CONST_NEW(c, names);
4295 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4296 return 1;
4297 }
4298 else {
4299 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4300 return 1;
4301 }
4302
4303ex_call:
4304
4305 /* Do positional arguments. */
4306 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4307 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4308 }
4309 else if (starunpack_helper(c, args, n, BUILD_LIST,
4310 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4311 return 0;
4312 }
4313 /* Then keyword arguments */
4314 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004315 /* Has a new dict been pushed */
4316 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004317
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004318 nseen = 0; /* the number of keyword arguments on the stack following */
4319 for (i = 0; i < nkwelts; i++) {
4320 keyword_ty kw = asdl_seq_GET(keywords, i);
4321 if (kw->arg == NULL) {
4322 /* A keyword argument unpacking. */
4323 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004324 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004325 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004326 }
Miss Islington (bot)410b7302020-06-01 09:07:32 -07004327 if (have_dict) {
4328 ADDOP_I(c, DICT_MERGE, 1);
4329 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004330 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004331 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004332 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004333 if (!have_dict) {
4334 ADDOP_I(c, BUILD_MAP, 0);
4335 have_dict = 1;
4336 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004337 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004338 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004339 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004340 else {
4341 nseen++;
4342 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004343 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004344 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004345 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004346 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004347 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004348 }
4349 if (have_dict) {
4350 ADDOP_I(c, DICT_MERGE, 1);
4351 }
4352 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004353 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004354 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004355 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004356 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4357 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004358}
4359
Nick Coghlan650f0d02007-04-15 12:05:43 +00004360
4361/* List and set comprehensions and generator expressions work by creating a
4362 nested function to perform the actual iteration. This means that the
4363 iteration variables don't leak into the current scope.
4364 The defined function is called immediately following its definition, with the
4365 result of that call being the result of the expression.
4366 The LC/SC version returns the populated container, while the GE version is
4367 flagged in symtable.c as a generator, so it returns the generator object
4368 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004369
4370 Possible cleanups:
4371 - iterate over the generator sequence instead of using recursion
4372*/
4373
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004374
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004375static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004376compiler_comprehension_generator(struct compiler *c,
4377 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004378 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004379 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004380{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004381 comprehension_ty gen;
4382 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4383 if (gen->is_async) {
4384 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004385 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004386 } else {
4387 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004388 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004389 }
4390}
4391
4392static int
4393compiler_sync_comprehension_generator(struct compiler *c,
4394 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004395 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004396 expr_ty elt, expr_ty val, int type)
4397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004398 /* generate code for the iterator, then each of the ifs,
4399 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004401 comprehension_ty gen;
4402 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004403 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004404
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004405 start = compiler_new_block(c);
4406 skip = compiler_new_block(c);
4407 if_cleanup = compiler_new_block(c);
4408 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004409
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004410 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4411 anchor == NULL)
4412 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004415
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004416 if (gen_index == 0) {
4417 /* Receive outermost iter as an implicit argument */
4418 c->u->u_argcount = 1;
4419 ADDOP_I(c, LOAD_FAST, 0);
4420 }
4421 else {
4422 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004423 /* Fast path for the temporary variable assignment idiom:
4424 for y in [f(x)]
4425 */
4426 asdl_seq *elts;
4427 switch (gen->iter->kind) {
4428 case List_kind:
4429 elts = gen->iter->v.List.elts;
4430 break;
4431 case Tuple_kind:
4432 elts = gen->iter->v.Tuple.elts;
4433 break;
4434 default:
4435 elts = NULL;
4436 }
4437 if (asdl_seq_LEN(elts) == 1) {
4438 expr_ty elt = asdl_seq_GET(elts, 0);
4439 if (elt->kind != Starred_kind) {
4440 VISIT(c, expr, elt);
4441 start = NULL;
4442 }
4443 }
4444 if (start) {
4445 VISIT(c, expr, gen->iter);
4446 ADDOP(c, GET_ITER);
4447 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004448 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004449 if (start) {
4450 depth++;
4451 compiler_use_next_block(c, start);
4452 ADDOP_JREL(c, FOR_ITER, anchor);
4453 NEXT_BLOCK(c);
4454 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004456
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004457 /* XXX this needs to be cleaned up...a lot! */
4458 n = asdl_seq_LEN(gen->ifs);
4459 for (i = 0; i < n; i++) {
4460 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004461 if (!compiler_jump_if(c, e, if_cleanup, 0))
4462 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004463 NEXT_BLOCK(c);
4464 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004465
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004466 if (++gen_index < asdl_seq_LEN(generators))
4467 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004468 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004469 elt, val, type))
4470 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004471
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004472 /* only append after the last for generator */
4473 if (gen_index >= asdl_seq_LEN(generators)) {
4474 /* comprehension specific code */
4475 switch (type) {
4476 case COMP_GENEXP:
4477 VISIT(c, expr, elt);
4478 ADDOP(c, YIELD_VALUE);
4479 ADDOP(c, POP_TOP);
4480 break;
4481 case COMP_LISTCOMP:
4482 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004483 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004484 break;
4485 case COMP_SETCOMP:
4486 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004487 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004488 break;
4489 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004490 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004491 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004492 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004493 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004494 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004495 break;
4496 default:
4497 return 0;
4498 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004499
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004500 compiler_use_next_block(c, skip);
4501 }
4502 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004503 if (start) {
4504 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
4505 compiler_use_next_block(c, anchor);
4506 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004507
4508 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004509}
4510
4511static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004512compiler_async_comprehension_generator(struct compiler *c,
4513 asdl_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004514 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004515 expr_ty elt, expr_ty val, int type)
4516{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004517 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004518 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004519 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004520 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004521 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004522 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004523
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004524 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004525 return 0;
4526 }
4527
4528 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4529
4530 if (gen_index == 0) {
4531 /* Receive outermost iter as an implicit argument */
4532 c->u->u_argcount = 1;
4533 ADDOP_I(c, LOAD_FAST, 0);
4534 }
4535 else {
4536 /* Sub-iter - calculate on the fly */
4537 VISIT(c, expr, gen->iter);
4538 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004539 }
4540
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004541 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004542
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004543 ADDOP_JREL(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004544 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004545 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004546 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004547 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004548 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004549
4550 n = asdl_seq_LEN(gen->ifs);
4551 for (i = 0; i < n; i++) {
4552 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004553 if (!compiler_jump_if(c, e, if_cleanup, 0))
4554 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004555 NEXT_BLOCK(c);
4556 }
4557
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004558 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004559 if (++gen_index < asdl_seq_LEN(generators))
4560 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004561 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004562 elt, val, type))
4563 return 0;
4564
4565 /* only append after the last for generator */
4566 if (gen_index >= asdl_seq_LEN(generators)) {
4567 /* comprehension specific code */
4568 switch (type) {
4569 case COMP_GENEXP:
4570 VISIT(c, expr, elt);
4571 ADDOP(c, YIELD_VALUE);
4572 ADDOP(c, POP_TOP);
4573 break;
4574 case COMP_LISTCOMP:
4575 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004576 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004577 break;
4578 case COMP_SETCOMP:
4579 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004580 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004581 break;
4582 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004583 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004584 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004585 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004586 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004587 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004588 break;
4589 default:
4590 return 0;
4591 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004592 }
4593 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004594 ADDOP_JABS(c, JUMP_ABSOLUTE, start);
4595
4596 compiler_use_next_block(c, except);
4597 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004598
4599 return 1;
4600}
4601
4602static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004603compiler_comprehension(struct compiler *c, expr_ty e, int type,
4604 identifier name, asdl_seq *generators, expr_ty elt,
4605 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004606{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004607 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004608 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004609 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004610 int is_async_generator = 0;
Pablo Galindo6488a4a2020-07-06 23:30:20 +01004611 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004612
Pablo Galindo6488a4a2020-07-06 23:30:20 +01004613
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004614 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004615
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004616 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004617 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4618 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004619 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004620 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004621 }
4622
4623 is_async_generator = c->u->u_ste->ste_coroutine;
4624
Pablo Galindo6488a4a2020-07-06 23:30:20 +01004625 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004626 compiler_error(c, "asynchronous comprehension outside of "
4627 "an asynchronous function");
4628 goto error_in_scope;
4629 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004631 if (type != COMP_GENEXP) {
4632 int op;
4633 switch (type) {
4634 case COMP_LISTCOMP:
4635 op = BUILD_LIST;
4636 break;
4637 case COMP_SETCOMP:
4638 op = BUILD_SET;
4639 break;
4640 case COMP_DICTCOMP:
4641 op = BUILD_MAP;
4642 break;
4643 default:
4644 PyErr_Format(PyExc_SystemError,
4645 "unknown comprehension type %d", type);
4646 goto error_in_scope;
4647 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004648
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004649 ADDOP_I(c, op, 0);
4650 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004651
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004652 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004653 val, type))
4654 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004656 if (type != COMP_GENEXP) {
4657 ADDOP(c, RETURN_VALUE);
4658 }
4659
4660 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004661 qualname = c->u->u_qualname;
4662 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004663 compiler_exit_scope(c);
Pablo Galindo6488a4a2020-07-06 23:30:20 +01004664 if (top_level_await && is_async_generator){
4665 c->u->u_ste->ste_coroutine = 1;
4666 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004667 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004668 goto error;
4669
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004670 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004672 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004673 Py_DECREF(co);
4674
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004675 VISIT(c, expr, outermost->iter);
4676
4677 if (outermost->is_async) {
4678 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004679 } else {
4680 ADDOP(c, GET_ITER);
4681 }
4682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004683 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004684
4685 if (is_async_generator && type != COMP_GENEXP) {
4686 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004687 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004688 ADDOP(c, YIELD_FROM);
4689 }
4690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004692error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004693 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004694error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004695 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004696 Py_XDECREF(co);
4697 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004698}
4699
4700static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004701compiler_genexp(struct compiler *c, expr_ty e)
4702{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004703 static identifier name;
4704 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004705 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004706 if (!name)
4707 return 0;
4708 }
4709 assert(e->kind == GeneratorExp_kind);
4710 return compiler_comprehension(c, e, COMP_GENEXP, name,
4711 e->v.GeneratorExp.generators,
4712 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004713}
4714
4715static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004716compiler_listcomp(struct compiler *c, expr_ty e)
4717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004718 static identifier name;
4719 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004720 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004721 if (!name)
4722 return 0;
4723 }
4724 assert(e->kind == ListComp_kind);
4725 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4726 e->v.ListComp.generators,
4727 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004728}
4729
4730static int
4731compiler_setcomp(struct compiler *c, expr_ty e)
4732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004733 static identifier name;
4734 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004735 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004736 if (!name)
4737 return 0;
4738 }
4739 assert(e->kind == SetComp_kind);
4740 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4741 e->v.SetComp.generators,
4742 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004743}
4744
4745
4746static int
4747compiler_dictcomp(struct compiler *c, expr_ty e)
4748{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004749 static identifier name;
4750 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004751 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004752 if (!name)
4753 return 0;
4754 }
4755 assert(e->kind == DictComp_kind);
4756 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4757 e->v.DictComp.generators,
4758 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004759}
4760
4761
4762static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004763compiler_visit_keyword(struct compiler *c, keyword_ty k)
4764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004765 VISIT(c, expr, k->value);
4766 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004767}
4768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004769/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004770 whether they are true or false.
4771
4772 Return values: 1 for true, 0 for false, -1 for non-constant.
4773 */
4774
4775static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004776expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004777{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004778 if (e->kind == Constant_kind) {
4779 return PyObject_IsTrue(e->v.Constant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004780 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004781 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004782}
4783
Mark Shannonfee55262019-11-21 09:11:43 +00004784static int
4785compiler_with_except_finish(struct compiler *c) {
4786 basicblock *exit;
4787 exit = compiler_new_block(c);
4788 if (exit == NULL)
4789 return 0;
4790 ADDOP_JABS(c, POP_JUMP_IF_TRUE, exit);
4791 ADDOP(c, RERAISE);
4792 compiler_use_next_block(c, exit);
4793 ADDOP(c, POP_TOP);
4794 ADDOP(c, POP_TOP);
4795 ADDOP(c, POP_TOP);
4796 ADDOP(c, POP_EXCEPT);
4797 ADDOP(c, POP_TOP);
4798 return 1;
4799}
Yury Selivanov75445082015-05-11 22:57:16 -04004800
4801/*
4802 Implements the async with statement.
4803
4804 The semantics outlined in that PEP are as follows:
4805
4806 async with EXPR as VAR:
4807 BLOCK
4808
4809 It is implemented roughly as:
4810
4811 context = EXPR
4812 exit = context.__aexit__ # not calling it
4813 value = await context.__aenter__()
4814 try:
4815 VAR = value # if VAR present in the syntax
4816 BLOCK
4817 finally:
4818 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004819 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004820 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004821 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004822 if not (await exit(*exc)):
4823 raise
4824 */
4825static int
4826compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4827{
Mark Shannonfee55262019-11-21 09:11:43 +00004828 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004829 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4830
4831 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004832 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004833 c->u->u_ste->ste_coroutine = 1;
4834 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004835 return compiler_error(c, "'async with' outside async function");
4836 }
Yury Selivanov75445082015-05-11 22:57:16 -04004837
4838 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004839 final = compiler_new_block(c);
4840 exit = compiler_new_block(c);
4841 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004842 return 0;
4843
4844 /* Evaluate EXPR */
4845 VISIT(c, expr, item->context_expr);
4846
4847 ADDOP(c, BEFORE_ASYNC_WITH);
4848 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004849 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004850 ADDOP(c, YIELD_FROM);
4851
Mark Shannonfee55262019-11-21 09:11:43 +00004852 ADDOP_JREL(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004853
4854 /* SETUP_ASYNC_WITH pushes a finally block. */
4855 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004856 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004857 return 0;
4858 }
4859
4860 if (item->optional_vars) {
4861 VISIT(c, expr, item->optional_vars);
4862 }
4863 else {
4864 /* Discard result from context.__aenter__() */
4865 ADDOP(c, POP_TOP);
4866 }
4867
4868 pos++;
4869 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4870 /* BLOCK code */
4871 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4872 else if (!compiler_async_with(c, s, pos))
4873 return 0;
4874
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004875 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004876 ADDOP(c, POP_BLOCK);
4877 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004878
Mark Shannonfee55262019-11-21 09:11:43 +00004879 /* For successful outcome:
4880 * call __exit__(None, None, None)
4881 */
4882 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004883 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004884 ADDOP(c, GET_AWAITABLE);
4885 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4886 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004887
Mark Shannonfee55262019-11-21 09:11:43 +00004888 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004889
Mark Shannonfee55262019-11-21 09:11:43 +00004890 ADDOP_JABS(c, JUMP_ABSOLUTE, exit);
4891
4892 /* For exceptional outcome: */
4893 compiler_use_next_block(c, final);
4894
4895 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004896 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004897 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004898 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004899 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004900
Mark Shannonfee55262019-11-21 09:11:43 +00004901compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004902 return 1;
4903}
4904
4905
Guido van Rossumc2e20742006-02-27 22:32:47 +00004906/*
4907 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004908 with EXPR as VAR:
4909 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004910 is implemented as:
4911 <code for EXPR>
4912 SETUP_WITH E
4913 <code to store to VAR> or POP_TOP
4914 <code for BLOCK>
4915 LOAD_CONST (None, None, None)
4916 CALL_FUNCTION_EX 0
4917 JUMP_FORWARD EXIT
4918 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4919 POP_JUMP_IF_TRUE T:
4920 RERAISE
4921 T: POP_TOP * 3 (remove exception from stack)
4922 POP_EXCEPT
4923 POP_TOP
4924 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004925 */
Mark Shannonfee55262019-11-21 09:11:43 +00004926
Guido van Rossumc2e20742006-02-27 22:32:47 +00004927static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004928compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004929{
Mark Shannonfee55262019-11-21 09:11:43 +00004930 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004931 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004932
4933 assert(s->kind == With_kind);
4934
Guido van Rossumc2e20742006-02-27 22:32:47 +00004935 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004936 final = compiler_new_block(c);
4937 exit = compiler_new_block(c);
4938 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004939 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004940
Thomas Wouters477c8d52006-05-27 19:21:47 +00004941 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004942 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004943 /* Will push bound __exit__ */
4944 ADDOP_JREL(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004945
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004946 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004947 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004948 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004949 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004950 }
4951
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004952 if (item->optional_vars) {
4953 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004954 }
4955 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004956 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004957 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004958 }
4959
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004960 pos++;
4961 if (pos == asdl_seq_LEN(s->v.With.items))
4962 /* BLOCK code */
4963 VISIT_SEQ(c, stmt, s->v.With.body)
4964 else if (!compiler_with(c, s, pos))
4965 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004966
Guido van Rossumc2e20742006-02-27 22:32:47 +00004967 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004968 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004969
Mark Shannonfee55262019-11-21 09:11:43 +00004970 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004971
Mark Shannonfee55262019-11-21 09:11:43 +00004972 /* For successful outcome:
4973 * call __exit__(None, None, None)
4974 */
4975 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004976 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004977 ADDOP(c, POP_TOP);
4978 ADDOP_JREL(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004979
Mark Shannonfee55262019-11-21 09:11:43 +00004980 /* For exceptional outcome: */
4981 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004982
Mark Shannonfee55262019-11-21 09:11:43 +00004983 ADDOP(c, WITH_EXCEPT_START);
4984 compiler_with_except_finish(c);
4985
4986 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004987 return 1;
4988}
4989
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004990static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004991compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004992{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004993 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004994 case NamedExpr_kind:
4995 VISIT(c, expr, e->v.NamedExpr.value);
4996 ADDOP(c, DUP_TOP);
4997 VISIT(c, expr, e->v.NamedExpr.target);
4998 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004999 case BoolOp_kind:
5000 return compiler_boolop(c, e);
5001 case BinOp_kind:
5002 VISIT(c, expr, e->v.BinOp.left);
5003 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005004 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005005 break;
5006 case UnaryOp_kind:
5007 VISIT(c, expr, e->v.UnaryOp.operand);
5008 ADDOP(c, unaryop(e->v.UnaryOp.op));
5009 break;
5010 case Lambda_kind:
5011 return compiler_lambda(c, e);
5012 case IfExp_kind:
5013 return compiler_ifexp(c, e);
5014 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005015 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005017 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005018 case GeneratorExp_kind:
5019 return compiler_genexp(c, e);
5020 case ListComp_kind:
5021 return compiler_listcomp(c, e);
5022 case SetComp_kind:
5023 return compiler_setcomp(c, e);
5024 case DictComp_kind:
5025 return compiler_dictcomp(c, e);
5026 case Yield_kind:
5027 if (c->u->u_ste->ste_type != FunctionBlock)
5028 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005029 if (e->v.Yield.value) {
5030 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005031 }
5032 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005033 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005035 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005036 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005037 case YieldFrom_kind:
5038 if (c->u->u_ste->ste_type != FunctionBlock)
5039 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005040
5041 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5042 return compiler_error(c, "'yield from' inside async function");
5043
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005044 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005045 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005046 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005047 ADDOP(c, YIELD_FROM);
5048 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005049 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005050 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005051 if (c->u->u_ste->ste_type != FunctionBlock){
5052 return compiler_error(c, "'await' outside function");
5053 }
Yury Selivanov75445082015-05-11 22:57:16 -04005054
Victor Stinner331a6a52019-05-27 16:39:22 +02005055 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005056 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5057 return compiler_error(c, "'await' outside async function");
5058 }
5059 }
Yury Selivanov75445082015-05-11 22:57:16 -04005060
5061 VISIT(c, expr, e->v.Await.value);
5062 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005063 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005064 ADDOP(c, YIELD_FROM);
5065 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005066 case Compare_kind:
5067 return compiler_compare(c, e);
5068 case Call_kind:
5069 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005070 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005071 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005072 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005073 case JoinedStr_kind:
5074 return compiler_joined_str(c, e);
5075 case FormattedValue_kind:
5076 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005077 /* The following exprs can be assignment targets. */
5078 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005079 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005080 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005081 case Load:
5082 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5083 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005084 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005085 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5086 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005087 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5088 break;
5089 case Del:
5090 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5091 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005092 }
5093 break;
5094 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005095 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005096 case Starred_kind:
5097 switch (e->v.Starred.ctx) {
5098 case Store:
5099 /* In all legitimate cases, the Starred node was already replaced
5100 * by compiler_list/compiler_tuple. XXX: is that okay? */
5101 return compiler_error(c,
5102 "starred assignment target must be in a list or tuple");
5103 default:
5104 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005105 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005106 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005107 break;
5108 case Slice_kind:
5109 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005110 case Name_kind:
5111 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5112 /* child nodes of List and Tuple will have expr_context set */
5113 case List_kind:
5114 return compiler_list(c, e);
5115 case Tuple_kind:
5116 return compiler_tuple(c, e);
5117 }
5118 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005119}
5120
5121static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005122compiler_visit_expr(struct compiler *c, expr_ty e)
5123{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005124 int old_lineno = c->u->u_lineno;
5125 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005126 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005127 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005128 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005129 c->u->u_col_offset = old_col_offset;
5130 return res;
5131}
5132
5133static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005134compiler_augassign(struct compiler *c, stmt_ty s)
5135{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005136 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005137 expr_ty e = s->v.AugAssign.target;
5138
5139 int old_lineno = c->u->u_lineno;
5140 int old_col_offset = c->u->u_col_offset;
5141 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005142
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005143 switch (e->kind) {
5144 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005145 VISIT(c, expr, e->v.Attribute.value);
5146 ADDOP(c, DUP_TOP);
5147 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005148 break;
5149 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005150 VISIT(c, expr, e->v.Subscript.value);
5151 VISIT(c, expr, e->v.Subscript.slice);
5152 ADDOP(c, DUP_TOP_TWO);
5153 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005154 break;
5155 case Name_kind:
5156 if (!compiler_nameop(c, e->v.Name.id, Load))
5157 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005158 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005159 default:
5160 PyErr_Format(PyExc_SystemError,
5161 "invalid node type (%d) for augmented assignment",
5162 e->kind);
5163 return 0;
5164 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005165
5166 c->u->u_lineno = old_lineno;
5167 c->u->u_col_offset = old_col_offset;
5168
5169 VISIT(c, expr, s->v.AugAssign.value);
5170 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5171
5172 SET_LOC(c, e);
5173
5174 switch (e->kind) {
5175 case Attribute_kind:
5176 ADDOP(c, ROT_TWO);
5177 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5178 break;
5179 case Subscript_kind:
5180 ADDOP(c, ROT_THREE);
5181 ADDOP(c, STORE_SUBSCR);
5182 break;
5183 case Name_kind:
5184 return compiler_nameop(c, e->v.Name.id, Store);
5185 default:
5186 Py_UNREACHABLE();
5187 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005188 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005189}
5190
5191static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005192check_ann_expr(struct compiler *c, expr_ty e)
5193{
5194 VISIT(c, expr, e);
5195 ADDOP(c, POP_TOP);
5196 return 1;
5197}
5198
5199static int
5200check_annotation(struct compiler *c, stmt_ty s)
5201{
5202 /* Annotations are only evaluated in a module or class. */
5203 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5204 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5205 return check_ann_expr(c, s->v.AnnAssign.annotation);
5206 }
5207 return 1;
5208}
5209
5210static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005211check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005212{
5213 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005214 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005215 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005216 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005217 return 0;
5218 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005219 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5220 return 0;
5221 }
5222 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5223 return 0;
5224 }
5225 return 1;
5226 case Tuple_kind: {
5227 /* extended slice */
5228 asdl_seq *elts = e->v.Tuple.elts;
5229 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005230 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005231 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005232 return 0;
5233 }
5234 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005235 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005236 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005237 default:
5238 return check_ann_expr(c, e);
5239 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005240}
5241
5242static int
5243compiler_annassign(struct compiler *c, stmt_ty s)
5244{
5245 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005246 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005247
5248 assert(s->kind == AnnAssign_kind);
5249
5250 /* We perform the actual assignment first. */
5251 if (s->v.AnnAssign.value) {
5252 VISIT(c, expr, s->v.AnnAssign.value);
5253 VISIT(c, expr, targ);
5254 }
5255 switch (targ->kind) {
5256 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005257 if (forbidden_name(c, targ->v.Name.id, Store))
5258 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005259 /* If we have a simple name in a module or class, store annotation. */
5260 if (s->v.AnnAssign.simple &&
5261 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5262 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum95e4d582018-01-26 08:20:18 -08005263 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5264 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5265 }
5266 else {
5267 VISIT(c, expr, s->v.AnnAssign.annotation);
5268 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005269 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005270 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005271 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005272 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005273 }
5274 break;
5275 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005276 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5277 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005278 if (!s->v.AnnAssign.value &&
5279 !check_ann_expr(c, targ->v.Attribute.value)) {
5280 return 0;
5281 }
5282 break;
5283 case Subscript_kind:
5284 if (!s->v.AnnAssign.value &&
5285 (!check_ann_expr(c, targ->v.Subscript.value) ||
5286 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5287 return 0;
5288 }
5289 break;
5290 default:
5291 PyErr_Format(PyExc_SystemError,
5292 "invalid node type (%d) for annotated assignment",
5293 targ->kind);
5294 return 0;
5295 }
5296 /* Annotation is evaluated last. */
5297 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5298 return 0;
5299 }
5300 return 1;
5301}
5302
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005303/* Raises a SyntaxError and returns 0.
5304 If something goes wrong, a different exception may be raised.
5305*/
5306
5307static int
5308compiler_error(struct compiler *c, const char *errstr)
5309{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005310 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005311 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005312
Victor Stinner14e461d2013-08-26 22:28:21 +02005313 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005314 if (!loc) {
5315 Py_INCREF(Py_None);
5316 loc = Py_None;
5317 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005318 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005319 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005320 if (!u)
5321 goto exit;
5322 v = Py_BuildValue("(zO)", errstr, u);
5323 if (!v)
5324 goto exit;
5325 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005326 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005327 Py_DECREF(loc);
5328 Py_XDECREF(u);
5329 Py_XDECREF(v);
5330 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005331}
5332
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005333/* Emits a SyntaxWarning and returns 1 on success.
5334 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5335 and returns 0.
5336*/
5337static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005338compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005339{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005340 va_list vargs;
5341#ifdef HAVE_STDARG_PROTOTYPES
5342 va_start(vargs, format);
5343#else
5344 va_start(vargs);
5345#endif
5346 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5347 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005348 if (msg == NULL) {
5349 return 0;
5350 }
5351 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5352 c->u->u_lineno, NULL, NULL) < 0)
5353 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005354 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005355 /* Replace the SyntaxWarning exception with a SyntaxError
5356 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005357 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005358 assert(PyUnicode_AsUTF8(msg) != NULL);
5359 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005360 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005361 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005362 return 0;
5363 }
5364 Py_DECREF(msg);
5365 return 1;
5366}
5367
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005368static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005369compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005370{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005371 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005372 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005373
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005374 if (ctx == Load) {
5375 if (!check_subscripter(c, e->v.Subscript.value)) {
5376 return 0;
5377 }
5378 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5379 return 0;
5380 }
5381 }
5382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005384 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 case Store: op = STORE_SUBSCR; break;
5386 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005387 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005388 assert(op);
5389 VISIT(c, expr, e->v.Subscript.value);
5390 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005391 ADDOP(c, op);
5392 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005393}
5394
5395static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005396compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005397{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005398 int n = 2;
5399 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005400
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005401 /* only handles the cases where BUILD_SLICE is emitted */
5402 if (s->v.Slice.lower) {
5403 VISIT(c, expr, s->v.Slice.lower);
5404 }
5405 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005406 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005409 if (s->v.Slice.upper) {
5410 VISIT(c, expr, s->v.Slice.upper);
5411 }
5412 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005413 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005414 }
5415
5416 if (s->v.Slice.step) {
5417 n++;
5418 VISIT(c, expr, s->v.Slice.step);
5419 }
5420 ADDOP_I(c, BUILD_SLICE, n);
5421 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005422}
5423
Thomas Wouters89f507f2006-12-13 04:49:30 +00005424/* End of the compiler section, beginning of the assembler section */
5425
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005426/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005427 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005428
5429 XXX must handle implicit jumps from one block to next
5430*/
5431
Thomas Wouters89f507f2006-12-13 04:49:30 +00005432struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005433 PyObject *a_bytecode; /* string containing bytecode */
5434 int a_offset; /* offset into bytecode */
5435 int a_nblocks; /* number of reachable blocks */
T. Wouters99b54d62019-09-12 07:05:33 -07005436 basicblock **a_postorder; /* list of blocks in dfs postorder */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005437 PyObject *a_lnotab; /* string containing lnotab */
5438 int a_lnotab_off; /* offset into lnotab */
5439 int a_lineno; /* last lineno of emitted instruction */
5440 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005441};
5442
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005443static void
T. Wouters99b54d62019-09-12 07:05:33 -07005444dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005445{
T. Wouters99b54d62019-09-12 07:05:33 -07005446 int i, j;
5447
5448 /* Get rid of recursion for normal control flow.
5449 Since the number of blocks is limited, unused space in a_postorder
5450 (from a_nblocks to end) can be used as a stack for still not ordered
5451 blocks. */
5452 for (j = end; b && !b->b_seen; b = b->b_next) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005453 b->b_seen = 1;
T. Wouters99b54d62019-09-12 07:05:33 -07005454 assert(a->a_nblocks < j);
5455 a->a_postorder[--j] = b;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005456 }
T. Wouters99b54d62019-09-12 07:05:33 -07005457 while (j < end) {
5458 b = a->a_postorder[j++];
5459 for (i = 0; i < b->b_iused; i++) {
5460 struct instr *instr = &b->b_instr[i];
5461 if (instr->i_jrel || instr->i_jabs)
5462 dfs(c, instr->i_target, a, j);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005463 }
T. Wouters99b54d62019-09-12 07:05:33 -07005464 assert(a->a_nblocks < j);
5465 a->a_postorder[a->a_nblocks++] = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005466 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005467}
5468
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005469Py_LOCAL_INLINE(void)
5470stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005471{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005472 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005473 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005474 assert(b->b_startdepth < 0);
5475 b->b_startdepth = depth;
5476 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005477 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005478}
5479
5480/* Find the flow path that needs the largest stack. We assume that
5481 * cycles in the flow graph have no net effect on the stack depth.
5482 */
5483static int
5484stackdepth(struct compiler *c)
5485{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005486 basicblock *b, *entryblock = NULL;
5487 basicblock **stack, **sp;
5488 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005489 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005490 b->b_startdepth = INT_MIN;
5491 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005492 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005493 }
5494 if (!entryblock)
5495 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005496 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5497 if (!stack) {
5498 PyErr_NoMemory();
5499 return -1;
5500 }
5501
5502 sp = stack;
5503 stackdepth_push(&sp, entryblock, 0);
5504 while (sp != stack) {
5505 b = *--sp;
5506 int depth = b->b_startdepth;
5507 assert(depth >= 0);
5508 basicblock *next = b->b_next;
5509 for (int i = 0; i < b->b_iused; i++) {
5510 struct instr *instr = &b->b_instr[i];
5511 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5512 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005513 _Py_FatalErrorFormat(__func__,
5514 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005515 }
5516 int new_depth = depth + effect;
5517 if (new_depth > maxdepth) {
5518 maxdepth = new_depth;
5519 }
5520 assert(depth >= 0); /* invalid code or bug in stackdepth() */
5521 if (instr->i_jrel || instr->i_jabs) {
5522 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5523 assert(effect != PY_INVALID_STACK_EFFECT);
5524 int target_depth = depth + effect;
5525 if (target_depth > maxdepth) {
5526 maxdepth = target_depth;
5527 }
5528 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005529 stackdepth_push(&sp, instr->i_target, target_depth);
5530 }
5531 depth = new_depth;
5532 if (instr->i_opcode == JUMP_ABSOLUTE ||
5533 instr->i_opcode == JUMP_FORWARD ||
5534 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005535 instr->i_opcode == RAISE_VARARGS ||
5536 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005537 {
5538 /* remaining code is dead */
5539 next = NULL;
5540 break;
5541 }
5542 }
5543 if (next != NULL) {
5544 stackdepth_push(&sp, next, depth);
5545 }
5546 }
5547 PyObject_Free(stack);
5548 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005549}
5550
5551static int
5552assemble_init(struct assembler *a, int nblocks, int firstlineno)
5553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005554 memset(a, 0, sizeof(struct assembler));
5555 a->a_lineno = firstlineno;
5556 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5557 if (!a->a_bytecode)
5558 return 0;
5559 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5560 if (!a->a_lnotab)
5561 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005562 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005563 PyErr_NoMemory();
5564 return 0;
5565 }
T. Wouters99b54d62019-09-12 07:05:33 -07005566 a->a_postorder = (basicblock **)PyObject_Malloc(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005567 sizeof(basicblock *) * nblocks);
T. Wouters99b54d62019-09-12 07:05:33 -07005568 if (!a->a_postorder) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005569 PyErr_NoMemory();
5570 return 0;
5571 }
5572 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005573}
5574
5575static void
5576assemble_free(struct assembler *a)
5577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005578 Py_XDECREF(a->a_bytecode);
5579 Py_XDECREF(a->a_lnotab);
T. Wouters99b54d62019-09-12 07:05:33 -07005580 if (a->a_postorder)
5581 PyObject_Free(a->a_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005582}
5583
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005584static int
5585blocksize(basicblock *b)
5586{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005587 int i;
5588 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005589
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005591 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005592 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005593}
5594
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005595/* Appends a pair to the end of the line number table, a_lnotab, representing
5596 the instruction's bytecode offset and line number. See
5597 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005598
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005599static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005600assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005601{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005602 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005603 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005604 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005605
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005606 d_lineno = i->i_lineno - a->a_lineno;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005607 if (d_lineno == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005608 return 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005609 }
5610
5611 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
5612 assert(d_bytecode >= 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005614 if (d_bytecode > 255) {
5615 int j, nbytes, ncodes = d_bytecode / 255;
5616 nbytes = a->a_lnotab_off + 2 * ncodes;
5617 len = PyBytes_GET_SIZE(a->a_lnotab);
5618 if (nbytes >= len) {
5619 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5620 len = nbytes;
5621 else if (len <= INT_MAX / 2)
5622 len *= 2;
5623 else {
5624 PyErr_NoMemory();
5625 return 0;
5626 }
5627 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5628 return 0;
5629 }
5630 lnotab = (unsigned char *)
5631 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5632 for (j = 0; j < ncodes; j++) {
5633 *lnotab++ = 255;
5634 *lnotab++ = 0;
5635 }
5636 d_bytecode -= ncodes * 255;
5637 a->a_lnotab_off += ncodes * 2;
5638 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005639 assert(0 <= d_bytecode && d_bytecode <= 255);
5640
5641 if (d_lineno < -128 || 127 < d_lineno) {
5642 int j, nbytes, ncodes, k;
5643 if (d_lineno < 0) {
5644 k = -128;
5645 /* use division on positive numbers */
5646 ncodes = (-d_lineno) / 128;
5647 }
5648 else {
5649 k = 127;
5650 ncodes = d_lineno / 127;
5651 }
5652 d_lineno -= ncodes * k;
5653 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005654 nbytes = a->a_lnotab_off + 2 * ncodes;
5655 len = PyBytes_GET_SIZE(a->a_lnotab);
5656 if (nbytes >= len) {
5657 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5658 len = nbytes;
5659 else if (len <= INT_MAX / 2)
5660 len *= 2;
5661 else {
5662 PyErr_NoMemory();
5663 return 0;
5664 }
5665 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5666 return 0;
5667 }
5668 lnotab = (unsigned char *)
5669 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5670 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005671 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005672 d_bytecode = 0;
5673 for (j = 1; j < ncodes; j++) {
5674 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005675 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005676 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005677 a->a_lnotab_off += ncodes * 2;
5678 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005679 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005681 len = PyBytes_GET_SIZE(a->a_lnotab);
5682 if (a->a_lnotab_off + 2 >= len) {
5683 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5684 return 0;
5685 }
5686 lnotab = (unsigned char *)
5687 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005689 a->a_lnotab_off += 2;
5690 if (d_bytecode) {
5691 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005692 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005693 }
5694 else { /* First line of a block; def stmt, etc. */
5695 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005696 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005697 }
5698 a->a_lineno = i->i_lineno;
5699 a->a_lineno_off = a->a_offset;
5700 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005701}
5702
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005703/* assemble_emit()
5704 Extend the bytecode with a new instruction.
5705 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005706*/
5707
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005708static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005709assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005710{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005711 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005712 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005713 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005714
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005715 arg = i->i_oparg;
5716 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005717 if (i->i_lineno && !assemble_lnotab(a, i))
5718 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005719 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005720 if (len > PY_SSIZE_T_MAX / 2)
5721 return 0;
5722 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5723 return 0;
5724 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005725 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005726 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005727 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005728 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005729}
5730
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005731static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005732assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005733{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005734 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005735 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005736 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005738 /* Compute the size of each block and fixup jump args.
5739 Replace block pointer with position in bytecode. */
5740 do {
5741 totsize = 0;
T. Wouters99b54d62019-09-12 07:05:33 -07005742 for (i = a->a_nblocks - 1; i >= 0; i--) {
5743 b = a->a_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005744 bsize = blocksize(b);
5745 b->b_offset = totsize;
5746 totsize += bsize;
5747 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005748 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005749 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5750 bsize = b->b_offset;
5751 for (i = 0; i < b->b_iused; i++) {
5752 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005753 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005754 /* Relative jumps are computed relative to
5755 the instruction pointer after fetching
5756 the jump instruction.
5757 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005758 bsize += isize;
5759 if (instr->i_jabs || instr->i_jrel) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005760 instr->i_oparg = instr->i_target->b_offset;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005761 if (instr->i_jrel) {
5762 instr->i_oparg -= bsize;
5763 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005764 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005765 if (instrsize(instr->i_oparg) != isize) {
5766 extended_arg_recompile = 1;
5767 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005768 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005769 }
5770 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005772 /* XXX: This is an awful hack that could hurt performance, but
5773 on the bright side it should work until we come up
5774 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005776 The issue is that in the first loop blocksize() is called
5777 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005778 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005779 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005781 So we loop until we stop seeing new EXTENDED_ARGs.
5782 The only EXTENDED_ARGs that could be popping up are
5783 ones in jump instructions. So this should converge
5784 fairly quickly.
5785 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005786 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005787}
5788
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005789static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005790dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005792 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005793 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005794
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005795 tuple = PyTuple_New(size);
5796 if (tuple == NULL)
5797 return NULL;
5798 while (PyDict_Next(dict, &pos, &k, &v)) {
5799 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005800 Py_INCREF(k);
5801 assert((i - offset) < size);
5802 assert((i - offset) >= 0);
5803 PyTuple_SET_ITEM(tuple, i - offset, k);
5804 }
5805 return tuple;
5806}
5807
5808static PyObject *
5809consts_dict_keys_inorder(PyObject *dict)
5810{
5811 PyObject *consts, *k, *v;
5812 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5813
5814 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5815 if (consts == NULL)
5816 return NULL;
5817 while (PyDict_Next(dict, &pos, &k, &v)) {
5818 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005819 /* The keys of the dictionary can be tuples wrapping a contant.
5820 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5821 * the object we want is always second. */
5822 if (PyTuple_CheckExact(k)) {
5823 k = PyTuple_GET_ITEM(k, 1);
5824 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005825 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005826 assert(i < size);
5827 assert(i >= 0);
5828 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005830 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005831}
5832
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005833static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005834compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005835{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005836 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005837 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005838 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005839 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005840 if (ste->ste_nested)
5841 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005842 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005843 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005844 if (!ste->ste_generator && ste->ste_coroutine)
5845 flags |= CO_COROUTINE;
5846 if (ste->ste_generator && ste->ste_coroutine)
5847 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005848 if (ste->ste_varargs)
5849 flags |= CO_VARARGS;
5850 if (ste->ste_varkeywords)
5851 flags |= CO_VARKEYWORDS;
5852 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005854 /* (Only) inherit compilerflags in PyCF_MASK */
5855 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005856
Pablo Galindo90235812020-03-15 04:29:22 +00005857 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005858 ste->ste_coroutine &&
5859 !ste->ste_generator) {
5860 flags |= CO_COROUTINE;
5861 }
5862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005863 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005864}
5865
INADA Naokic2e16072018-11-26 21:23:22 +09005866// Merge *tuple* with constant cache.
5867// Unlike merge_consts_recursive(), this function doesn't work recursively.
5868static int
5869merge_const_tuple(struct compiler *c, PyObject **tuple)
5870{
5871 assert(PyTuple_CheckExact(*tuple));
5872
5873 PyObject *key = _PyCode_ConstantKey(*tuple);
5874 if (key == NULL) {
5875 return 0;
5876 }
5877
5878 // t is borrowed reference
5879 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5880 Py_DECREF(key);
5881 if (t == NULL) {
5882 return 0;
5883 }
5884 if (t == key) { // tuple is new constant.
5885 return 1;
5886 }
5887
5888 PyObject *u = PyTuple_GET_ITEM(t, 1);
5889 Py_INCREF(u);
5890 Py_DECREF(*tuple);
5891 *tuple = u;
5892 return 1;
5893}
5894
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005895static PyCodeObject *
5896makecode(struct compiler *c, struct assembler *a)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005897{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005898 PyObject *tmp;
5899 PyCodeObject *co = NULL;
5900 PyObject *consts = NULL;
5901 PyObject *names = NULL;
5902 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005903 PyObject *name = NULL;
5904 PyObject *freevars = NULL;
5905 PyObject *cellvars = NULL;
5906 PyObject *bytecode = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005907 Py_ssize_t nlocals;
5908 int nlocals_int;
5909 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005910 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005911
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005912 consts = consts_dict_keys_inorder(c->u->u_consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005913 names = dict_keys_inorder(c->u->u_names, 0);
5914 varnames = dict_keys_inorder(c->u->u_varnames, 0);
5915 if (!consts || !names || !varnames)
5916 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005917
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005918 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5919 if (!cellvars)
5920 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005921 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005922 if (!freevars)
5923 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005924
INADA Naokic2e16072018-11-26 21:23:22 +09005925 if (!merge_const_tuple(c, &names) ||
5926 !merge_const_tuple(c, &varnames) ||
5927 !merge_const_tuple(c, &cellvars) ||
5928 !merge_const_tuple(c, &freevars))
5929 {
5930 goto error;
5931 }
5932
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005933 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005934 assert(nlocals < INT_MAX);
5935 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5936
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005937 flags = compute_code_flags(c);
5938 if (flags < 0)
5939 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005940
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005941 bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab);
5942 if (!bytecode)
5943 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005944
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005945 tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5946 if (!tmp)
5947 goto error;
5948 Py_DECREF(consts);
5949 consts = tmp;
INADA Naokic2e16072018-11-26 21:23:22 +09005950 if (!merge_const_tuple(c, &consts)) {
5951 goto error;
5952 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005953
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005954 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005955 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005956 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005957 maxdepth = stackdepth(c);
5958 if (maxdepth < 0) {
5959 goto error;
5960 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005961 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005962 posonlyargcount, kwonlyargcount, nlocals_int,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005963 maxdepth, flags, bytecode, consts, names,
5964 varnames, freevars, cellvars, c->c_filename,
5965 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005966 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005967 Py_XDECREF(consts);
5968 Py_XDECREF(names);
5969 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005970 Py_XDECREF(name);
5971 Py_XDECREF(freevars);
5972 Py_XDECREF(cellvars);
5973 Py_XDECREF(bytecode);
5974 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005975}
5976
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005977
5978/* For debugging purposes only */
5979#if 0
5980static void
5981dump_instr(const struct instr *i)
5982{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005983 const char *jrel = i->i_jrel ? "jrel " : "";
5984 const char *jabs = i->i_jabs ? "jabs " : "";
5985 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005986
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005987 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005988 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005989 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005990 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005991 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5992 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005993}
5994
5995static void
5996dump_basicblock(const basicblock *b)
5997{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005998 const char *seen = b->b_seen ? "seen " : "";
5999 const char *b_return = b->b_return ? "return " : "";
6000 fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n",
6001 b->b_iused, b->b_startdepth, b->b_offset, seen, b_return);
6002 if (b->b_instr) {
6003 int i;
6004 for (i = 0; i < b->b_iused; i++) {
6005 fprintf(stderr, " [%02d] ", i);
6006 dump_instr(b->b_instr + i);
6007 }
6008 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00006009}
6010#endif
6011
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006012static PyCodeObject *
6013assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006015 basicblock *b, *entryblock;
6016 struct assembler a;
6017 int i, j, nblocks;
6018 PyCodeObject *co = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006020 /* Make sure every block that falls off the end returns None.
6021 XXX NEXT_BLOCK() isn't quite right, because if the last
6022 block ends with a jump or return b_next shouldn't set.
6023 */
6024 if (!c->u->u_curblock->b_return) {
6025 NEXT_BLOCK(c);
6026 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006027 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006028 ADDOP(c, RETURN_VALUE);
6029 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006030
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006031 nblocks = 0;
6032 entryblock = NULL;
6033 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6034 nblocks++;
6035 entryblock = b;
6036 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006038 /* Set firstlineno if it wasn't explicitly set. */
6039 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006040 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006041 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
6042 else
6043 c->u->u_firstlineno = 1;
6044 }
6045 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6046 goto error;
T. Wouters99b54d62019-09-12 07:05:33 -07006047 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006049 /* Can't modify the bytecode after computing jump offsets. */
6050 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006051
T. Wouters99b54d62019-09-12 07:05:33 -07006052 /* Emit code in reverse postorder from dfs. */
6053 for (i = a.a_nblocks - 1; i >= 0; i--) {
6054 b = a.a_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006055 for (j = 0; j < b->b_iused; j++)
6056 if (!assemble_emit(&a, &b->b_instr[j]))
6057 goto error;
6058 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006059
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006060 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6061 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006062 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006063 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006065 co = makecode(c, &a);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006066 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006067 assemble_free(&a);
6068 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006069}
Georg Brandl8334fd92010-12-04 10:26:46 +00006070
6071#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006072PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006073PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6074 PyArena *arena)
6075{
6076 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6077}