blob: 3ebf221cf02b7165f8a1b2cf59171f0564370a46 [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.
Mark Shannon6e8128f2020-07-30 10:03:00 +010012 * 5. Optimize the byte code (peephole optimizations).
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"
Mark Shannon582aaf12020-08-04 17:30:11 +010030#define NEED_OPCODE_JUMP_TABLES
Guido van Rossum10dc2e81990-11-18 17:27:39 +000031#include "opcode.h"
Serhiy Storchakab0f80b02016-05-24 09:15:14 +030032#include "wordcode_helpers.h"
Guido van Rossumb05a5c71997-05-07 17:46:13 +000033
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000034#define DEFAULT_BLOCK_SIZE 16
35#define DEFAULT_BLOCKS 8
36#define DEFAULT_CODE_SIZE 128
37#define DEFAULT_LNOTAB_SIZE 16
Jeremy Hylton29906ee2001-02-27 04:23:34 +000038
Nick Coghlan650f0d02007-04-15 12:05:43 +000039#define COMP_GENEXP 0
40#define COMP_LISTCOMP 1
41#define COMP_SETCOMP 2
Guido van Rossum992d4a32007-07-11 13:09:30 +000042#define COMP_DICTCOMP 3
Nick Coghlan650f0d02007-04-15 12:05:43 +000043
Pablo Galindo90235812020-03-15 04:29:22 +000044#define IS_TOP_LEVEL_AWAIT(c) ( \
45 (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \
46 && (c->u->u_ste->ste_type == ModuleBlock))
47
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000048struct instr {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 unsigned char i_opcode;
50 int i_oparg;
51 struct basicblock_ *i_target; /* target block (if jump instruction) */
52 int i_lineno;
Guido van Rossum3f5da241990-12-20 15:06:42 +000053};
54
Mark Shannon582aaf12020-08-04 17:30:11 +010055#define LOG_BITS_PER_INT 5
56#define MASK_LOW_LOG_BITS 31
57
58static inline int
59is_bit_set_in_table(uint32_t *table, int bitindex) {
60 /* Is the relevant bit set in the relevant word? */
61 /* 256 bits fit into 8 32-bits words.
62 * Word is indexed by (bitindex>>ln(size of int in bits)).
63 * Bit within word is the low bits of bitindex.
64 */
65 uint32_t word = table[bitindex >> LOG_BITS_PER_INT];
66 return (word >> (bitindex & MASK_LOW_LOG_BITS)) & 1;
67}
68
69static inline int
70is_relative_jump(struct instr *i)
71{
72 return is_bit_set_in_table(_PyOpcode_RelativeJump, i->i_opcode);
73}
74
75static inline int
76is_jump(struct instr *i)
77{
78 return is_bit_set_in_table(_PyOpcode_Jump, i->i_opcode);
79}
80
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000081typedef struct basicblock_ {
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000082 /* Each basicblock in a compilation unit is linked via b_list in the
83 reverse order that the block are allocated. b_list points to the next
84 block, not to be confused with b_next, which is next by control flow. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 struct basicblock_ *b_list;
86 /* number of instructions used */
87 int b_iused;
88 /* length of instruction array (b_instr) */
89 int b_ialloc;
90 /* pointer to an array of instructions, initially NULL */
91 struct instr *b_instr;
92 /* If b_next is non-NULL, it is a pointer to the next
93 block reached by normal control flow. */
94 struct basicblock_ *b_next;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000095 /* b_return is true if a RETURN_VALUE opcode is inserted. */
96 unsigned b_return : 1;
Mark Shannon6e8128f2020-07-30 10:03:00 +010097 unsigned b_reachable : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 /* depth of stack upon entry of block, computed by stackdepth() */
99 int b_startdepth;
100 /* instruction offset for block, computed by assemble_jump_offsets() */
101 int b_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000102} basicblock;
103
104/* fblockinfo tracks the current frame block.
105
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000106A frame block is used to handle loops, try/except, and try/finally.
107It's called a frame block to distinguish it from a basic block in the
108compiler IR.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000109*/
110
Mark Shannonfee55262019-11-21 09:11:43 +0000111enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END,
112 WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE };
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000113
114struct fblockinfo {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 enum fblocktype fb_type;
116 basicblock *fb_block;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200117 /* (optional) type-specific exit or cleanup block */
118 basicblock *fb_exit;
Mark Shannonfee55262019-11-21 09:11:43 +0000119 /* (optional) additional information required for unwinding */
120 void *fb_datum;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000121};
122
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100123enum {
124 COMPILER_SCOPE_MODULE,
125 COMPILER_SCOPE_CLASS,
126 COMPILER_SCOPE_FUNCTION,
Yury Selivanov75445082015-05-11 22:57:16 -0400127 COMPILER_SCOPE_ASYNC_FUNCTION,
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400128 COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100129 COMPILER_SCOPE_COMPREHENSION,
130};
131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000132/* The following items change on entry and exit of code blocks.
133 They must be saved and restored when returning to a block.
134*/
135struct compiler_unit {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000136 PySTEntryObject *u_ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyObject *u_name;
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400139 PyObject *u_qualname; /* dot-separated qualified name (lazy) */
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100140 int u_scope_type;
141
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 /* The following fields are dicts that map objects to
143 the index of them in co_XXX. The index is used as
144 the argument for opcodes that refer to those collections.
145 */
146 PyObject *u_consts; /* all constants */
147 PyObject *u_names; /* all names */
148 PyObject *u_varnames; /* local variables */
149 PyObject *u_cellvars; /* cell variables */
150 PyObject *u_freevars; /* free variables */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000151
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000152 PyObject *u_private; /* for private name mangling */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153
Victor Stinnerf8e32212013-11-19 23:56:34 +0100154 Py_ssize_t u_argcount; /* number of arguments for block */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100155 Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */
Victor Stinnerf8e32212013-11-19 23:56:34 +0100156 Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 /* Pointer to the most recently allocated block. By following b_list
158 members, you can reach all early allocated blocks. */
159 basicblock *u_blocks;
160 basicblock *u_curblock; /* pointer to current block */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000161
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000162 int u_nfblocks;
163 struct fblockinfo u_fblock[CO_MAXBLOCKS];
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000164
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 int u_firstlineno; /* the first lineno of the block */
166 int u_lineno; /* the lineno for the current stmt */
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000167 int u_col_offset; /* the offset of the current stmt */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000168};
169
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170/* This struct captures the global state of a compilation.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000171
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000172The u pointer points to the current compilation unit, while units
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000173for enclosing blocks are stored in c_stack. The u and c_stack are
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000174managed by compiler_enter_scope() and compiler_exit_scope().
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000175
176Note that we don't track recursion levels during compilation - the
177task of detecting and rejecting excessive levels of nesting is
178handled by the symbol analysis pass.
179
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000180*/
181
182struct compiler {
Victor Stinner14e461d2013-08-26 22:28:21 +0200183 PyObject *c_filename;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000184 struct symtable *c_st;
185 PyFutureFeatures *c_future; /* pointer to module's __future__ */
186 PyCompilerFlags *c_flags;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000187
Georg Brandl8334fd92010-12-04 10:26:46 +0000188 int c_optimize; /* optimization level */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000189 int c_interactive; /* true if in interactive mode */
190 int c_nestlevel;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100191 int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode
192 if this value is different from zero.
193 This can be used to temporarily visit
194 nodes without emitting bytecode to
195 check only errors. */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
INADA Naokic2e16072018-11-26 21:23:22 +0900197 PyObject *c_const_cache; /* Python dict holding all constants,
198 including names tuple */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 struct compiler_unit *u; /* compiler state for current block */
200 PyObject *c_stack; /* Python list holding compiler_unit ptrs */
201 PyArena *c_arena; /* pointer to memory allocation arena */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202};
203
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100204static int compiler_enter_scope(struct compiler *, identifier, int, void *, int);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205static void compiler_free(struct compiler *);
206static basicblock *compiler_new_block(struct compiler *);
Andy Lester76d58772020-03-10 21:18:12 -0500207static int compiler_next_instr(basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208static int compiler_addop(struct compiler *, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +0100209static int compiler_addop_i(struct compiler *, int, Py_ssize_t);
Mark Shannon582aaf12020-08-04 17:30:11 +0100210static int compiler_addop_j(struct compiler *, int, basicblock *);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211static int compiler_error(struct compiler *, const char *);
Serhiy Storchaka62e44812019-02-16 08:12:19 +0200212static int compiler_warn(struct compiler *, const char *, ...);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213static int compiler_nameop(struct compiler *, identifier, expr_context_ty);
214
215static PyCodeObject *compiler_mod(struct compiler *, mod_ty);
216static int compiler_visit_stmt(struct compiler *, stmt_ty);
217static int compiler_visit_keyword(struct compiler *, keyword_ty);
218static int compiler_visit_expr(struct compiler *, expr_ty);
219static int compiler_augassign(struct compiler *, stmt_ty);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700220static int compiler_annassign(struct compiler *, stmt_ty);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +0200221static int compiler_subscript(struct compiler *, expr_ty);
222static int compiler_slice(struct compiler *, expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
Andy Lester76d58772020-03-10 21:18:12 -0500224static int inplace_binop(operator_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100225static int are_all_items_const(asdl_expr_seq *, Py_ssize_t, Py_ssize_t);
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +0200226static int expr_constant(expr_ty);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500228static int compiler_with(struct compiler *, stmt_ty, int);
Yury Selivanov75445082015-05-11 22:57:16 -0400229static int compiler_async_with(struct compiler *, stmt_ty, int);
230static int compiler_async_for(struct compiler *, stmt_ty);
Victor Stinner976bb402016-03-23 11:36:19 +0100231static int compiler_call_helper(struct compiler *c, int n,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100232 asdl_expr_seq *args,
233 asdl_keyword_seq *keywords);
Benjamin Peterson43af12b2011-05-29 11:43:10 -0500234static int compiler_try_except(struct compiler *, stmt_ty);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400235static int compiler_set_qualname(struct compiler *);
Guido van Rossumc2e20742006-02-27 22:32:47 +0000236
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700237static int compiler_sync_comprehension_generator(
238 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100239 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200240 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700241 expr_ty elt, expr_ty val, int type);
242
243static int compiler_async_comprehension_generator(
244 struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +0100245 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +0200246 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -0700247 expr_ty elt, expr_ty val, int type);
248
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249static PyCodeObject *assemble(struct compiler *, int addNone);
Mark Shannon332cd5e2018-01-30 00:41:04 +0000250static PyObject *__doc__, *__annotations__;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400252#define CAPSULE_NAME "compile.c compiler unit"
Benjamin Petersonb173f782009-05-05 22:31:58 +0000253
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254PyObject *
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255_Py_Mangle(PyObject *privateobj, PyObject *ident)
Michael W. Hudson60934622004-08-12 17:56:29 +0000256{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 /* Name mangling: __private becomes _classname__private.
258 This is independent from how the name is used. */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200259 PyObject *result;
260 size_t nlen, plen, ipriv;
261 Py_UCS4 maxchar;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 if (privateobj == NULL || !PyUnicode_Check(privateobj) ||
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200263 PyUnicode_READ_CHAR(ident, 0) != '_' ||
264 PyUnicode_READ_CHAR(ident, 1) != '_') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 Py_INCREF(ident);
266 return ident;
267 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200268 nlen = PyUnicode_GET_LENGTH(ident);
269 plen = PyUnicode_GET_LENGTH(privateobj);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 /* Don't mangle __id__ or names with dots.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 The only time a name with a dot can occur is when
273 we are compiling an import statement that has a
274 package name.
Guido van Rossumd8faa362007-04-27 19:54:29 +0000275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000276 TODO(jhylton): Decide whether we want to support
277 mangling of the module name, e.g. __M.X.
278 */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200279 if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' &&
280 PyUnicode_READ_CHAR(ident, nlen-2) == '_') ||
281 PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 Py_INCREF(ident);
283 return ident; /* Don't mangle __whatever__ */
284 }
285 /* Strip leading underscores from class name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200286 ipriv = 0;
287 while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_')
288 ipriv++;
289 if (ipriv == plen) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000290 Py_INCREF(ident);
291 return ident; /* Don't mangle if class is just underscores */
292 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200293 plen -= ipriv;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000294
Antoine Pitrou55bff892013-04-06 21:21:04 +0200295 if (plen + nlen >= PY_SSIZE_T_MAX - 1) {
296 PyErr_SetString(PyExc_OverflowError,
297 "private identifier too large to be mangled");
298 return NULL;
299 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000300
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200301 maxchar = PyUnicode_MAX_CHAR_VALUE(ident);
302 if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar)
303 maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj);
304
305 result = PyUnicode_New(1 + nlen + plen, maxchar);
306 if (!result)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000307 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200308 /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */
309 PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_');
Victor Stinner6c7a52a2011-09-28 21:39:17 +0200310 if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) {
311 Py_DECREF(result);
312 return NULL;
313 }
314 if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) {
315 Py_DECREF(result);
316 return NULL;
317 }
Victor Stinner8f825062012-04-27 13:55:39 +0200318 assert(_PyUnicode_CheckConsistency(result, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200319 return result;
Michael W. Hudson60934622004-08-12 17:56:29 +0000320}
321
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322static int
323compiler_init(struct compiler *c)
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000324{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000325 memset(c, 0, sizeof(struct compiler));
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000326
INADA Naokic2e16072018-11-26 21:23:22 +0900327 c->c_const_cache = PyDict_New();
328 if (!c->c_const_cache) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return 0;
INADA Naokic2e16072018-11-26 21:23:22 +0900330 }
331
332 c->c_stack = PyList_New(0);
333 if (!c->c_stack) {
334 Py_CLEAR(c->c_const_cache);
335 return 0;
336 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339}
340
341PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200342PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags,
343 int optimize, PyArena *arena)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 struct compiler c;
346 PyCodeObject *co = NULL;
Victor Stinner37d66d72019-06-13 02:16:41 +0200347 PyCompilerFlags local_flags = _PyCompilerFlags_INIT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 int merged;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 if (!__doc__) {
351 __doc__ = PyUnicode_InternFromString("__doc__");
352 if (!__doc__)
353 return NULL;
354 }
Mark Shannon332cd5e2018-01-30 00:41:04 +0000355 if (!__annotations__) {
356 __annotations__ = PyUnicode_InternFromString("__annotations__");
357 if (!__annotations__)
358 return NULL;
359 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 if (!compiler_init(&c))
361 return NULL;
Victor Stinner14e461d2013-08-26 22:28:21 +0200362 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 c.c_filename = filename;
364 c.c_arena = arena;
Victor Stinner14e461d2013-08-26 22:28:21 +0200365 c.c_future = PyFuture_FromASTObject(mod, filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 if (c.c_future == NULL)
367 goto finally;
368 if (!flags) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 flags = &local_flags;
370 }
371 merged = c.c_future->ff_features | flags->cf_flags;
372 c.c_future->ff_features = merged;
373 flags->cf_flags = merged;
374 c.c_flags = flags;
Victor Stinnerda7933e2020-04-13 03:04:28 +0200375 c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 c.c_nestlevel = 0;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +0100377 c.c_do_not_emit_bytecode = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378
Pablo Galindod112c602020-03-18 23:02:09 +0000379 _PyASTOptimizeState state;
380 state.optimize = c.c_optimize;
381 state.ff_features = merged;
382
383 if (!_PyAST_Optimize(mod, arena, &state)) {
INADA Naoki7ea143a2017-12-14 16:47:20 +0900384 goto finally;
385 }
386
Victor Stinner14e461d2013-08-26 22:28:21 +0200387 c.c_st = PySymtable_BuildObject(mod, filename, c.c_future);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (c.c_st == NULL) {
389 if (!PyErr_Occurred())
390 PyErr_SetString(PyExc_SystemError, "no symtable");
391 goto finally;
392 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000393
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 co = compiler_mod(&c, mod);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000395
Thomas Wouters1175c432006-02-27 22:49:54 +0000396 finally:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 compiler_free(&c);
398 assert(co || PyErr_Occurred());
399 return co;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000400}
401
402PyCodeObject *
Victor Stinner14e461d2013-08-26 22:28:21 +0200403PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags,
404 int optimize, PyArena *arena)
405{
406 PyObject *filename;
407 PyCodeObject *co;
408 filename = PyUnicode_DecodeFSDefault(filename_str);
409 if (filename == NULL)
410 return NULL;
411 co = PyAST_CompileObject(mod, filename, flags, optimize, arena);
412 Py_DECREF(filename);
413 return co;
414
415}
416
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000417static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418compiler_free(struct compiler *c)
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000419{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000420 if (c->c_st)
421 PySymtable_Free(c->c_st);
422 if (c->c_future)
423 PyObject_Free(c->c_future);
Victor Stinner14e461d2013-08-26 22:28:21 +0200424 Py_XDECREF(c->c_filename);
INADA Naokic2e16072018-11-26 21:23:22 +0900425 Py_DECREF(c->c_const_cache);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 Py_DECREF(c->c_stack);
Guido van Rossum10dc2e81990-11-18 17:27:39 +0000427}
428
Guido van Rossum79f25d91997-04-29 20:08:16 +0000429static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430list2dict(PyObject *list)
Guido van Rossum2dff9911992-09-03 20:50:59 +0000431{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 Py_ssize_t i, n;
433 PyObject *v, *k;
434 PyObject *dict = PyDict_New();
435 if (!dict) return NULL;
Guido van Rossumd076c731998-10-07 19:42:25 +0000436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000437 n = PyList_Size(list);
438 for (i = 0; i < n; i++) {
Victor Stinnerad9a0662013-11-19 22:23:20 +0100439 v = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 if (!v) {
441 Py_DECREF(dict);
442 return NULL;
443 }
444 k = PyList_GET_ITEM(list, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300445 if (PyDict_SetItem(dict, k, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 Py_DECREF(v);
447 Py_DECREF(dict);
448 return NULL;
449 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 Py_DECREF(v);
451 }
452 return dict;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453}
454
455/* Return new dict containing names from src that match scope(s).
456
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000457src is a symbol table dictionary. If the scope of a name matches
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458either scope_type or flag is set, insert it into the new dict. The
Jeremy Hyltone9357b22006-03-01 15:47:05 +0000459values are integers, starting at offset and increasing by one for
460each key.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000461*/
462
463static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +0100464dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465{
Benjamin Peterson51ab2832012-07-18 15:12:47 -0700466 Py_ssize_t i = offset, scope, num_keys, key_i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *k, *v, *dest = PyDict_New();
Meador Inge2ca63152012-07-18 14:20:11 -0500468 PyObject *sorted_keys;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 assert(offset >= 0);
471 if (dest == NULL)
472 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000473
Meador Inge2ca63152012-07-18 14:20:11 -0500474 /* Sort the keys so that we have a deterministic order on the indexes
475 saved in the returned dictionary. These indexes are used as indexes
476 into the free and cell var storage. Therefore if they aren't
477 deterministic, then the generated bytecode is not deterministic.
478 */
479 sorted_keys = PyDict_Keys(src);
480 if (sorted_keys == NULL)
481 return NULL;
482 if (PyList_Sort(sorted_keys) != 0) {
483 Py_DECREF(sorted_keys);
484 return NULL;
485 }
Meador Ingef69e24e2012-07-18 16:41:03 -0500486 num_keys = PyList_GET_SIZE(sorted_keys);
Meador Inge2ca63152012-07-18 14:20:11 -0500487
488 for (key_i = 0; key_i < num_keys; key_i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 /* XXX this should probably be a macro in symtable.h */
490 long vi;
Meador Inge2ca63152012-07-18 14:20:11 -0500491 k = PyList_GET_ITEM(sorted_keys, key_i);
492 v = PyDict_GetItem(src, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 assert(PyLong_Check(v));
494 vi = PyLong_AS_LONG(v);
495 scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (scope == scope_type || vi & flag) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300498 PyObject *item = PyLong_FromSsize_t(i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (item == NULL) {
Meador Inge2ca63152012-07-18 14:20:11 -0500500 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 Py_DECREF(dest);
502 return NULL;
503 }
504 i++;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300505 if (PyDict_SetItem(dest, k, item) < 0) {
Meador Inge2ca63152012-07-18 14:20:11 -0500506 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 Py_DECREF(item);
508 Py_DECREF(dest);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 return NULL;
510 }
511 Py_DECREF(item);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
513 }
Meador Inge2ca63152012-07-18 14:20:11 -0500514 Py_DECREF(sorted_keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 return dest;
Jeremy Hylton64949cb2001-01-25 20:06:59 +0000516}
517
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000518static void
519compiler_unit_check(struct compiler_unit *u)
520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000521 basicblock *block;
522 for (block = u->u_blocks; block != NULL; block = block->b_list) {
Benjamin Petersonca470632016-09-06 13:47:26 -0700523 assert((uintptr_t)block != 0xcbcbcbcbU);
524 assert((uintptr_t)block != 0xfbfbfbfbU);
525 assert((uintptr_t)block != 0xdbdbdbdbU);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (block->b_instr != NULL) {
527 assert(block->b_ialloc > 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +0100528 assert(block->b_iused >= 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 assert(block->b_ialloc >= block->b_iused);
530 }
531 else {
532 assert (block->b_iused == 0);
533 assert (block->b_ialloc == 0);
534 }
535 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000536}
537
538static void
539compiler_unit_free(struct compiler_unit *u)
540{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 basicblock *b, *next;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000543 compiler_unit_check(u);
544 b = u->u_blocks;
545 while (b != NULL) {
546 if (b->b_instr)
547 PyObject_Free((void *)b->b_instr);
548 next = b->b_list;
549 PyObject_Free((void *)b);
550 b = next;
551 }
552 Py_CLEAR(u->u_ste);
553 Py_CLEAR(u->u_name);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400554 Py_CLEAR(u->u_qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000555 Py_CLEAR(u->u_consts);
556 Py_CLEAR(u->u_names);
557 Py_CLEAR(u->u_varnames);
558 Py_CLEAR(u->u_freevars);
559 Py_CLEAR(u->u_cellvars);
560 Py_CLEAR(u->u_private);
561 PyObject_Free(u);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000562}
563
564static int
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100565compiler_enter_scope(struct compiler *c, identifier name,
566 int scope_type, void *key, int lineno)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 struct compiler_unit *u;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100569 basicblock *block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Andy Lester7668a8b2020-03-24 23:26:44 -0500571 u = (struct compiler_unit *)PyObject_Calloc(1, sizeof(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 struct compiler_unit));
573 if (!u) {
574 PyErr_NoMemory();
575 return 0;
576 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100577 u->u_scope_type = scope_type;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 u->u_argcount = 0;
Pablo Galindo8c77b8c2019-04-29 13:36:57 +0100579 u->u_posonlyargcount = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 u->u_kwonlyargcount = 0;
581 u->u_ste = PySymtable_Lookup(c->c_st, key);
582 if (!u->u_ste) {
583 compiler_unit_free(u);
584 return 0;
585 }
586 Py_INCREF(name);
587 u->u_name = name;
588 u->u_varnames = list2dict(u->u_ste->ste_varnames);
589 u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0);
590 if (!u->u_varnames || !u->u_cellvars) {
591 compiler_unit_free(u);
592 return 0;
593 }
Benjamin Peterson312595c2013-05-15 15:26:42 -0500594 if (u->u_ste->ste_needs_class_closure) {
Martin Panter7462b6492015-11-02 03:37:02 +0000595 /* Cook up an implicit __class__ cell. */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500596 _Py_IDENTIFIER(__class__);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300597 PyObject *name;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500598 int res;
599 assert(u->u_scope_type == COMPILER_SCOPE_CLASS);
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200600 assert(PyDict_GET_SIZE(u->u_cellvars) == 0);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500601 name = _PyUnicode_FromId(&PyId___class__);
602 if (!name) {
603 compiler_unit_free(u);
604 return 0;
605 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +0300606 res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero);
Benjamin Peterson312595c2013-05-15 15:26:42 -0500607 if (res < 0) {
608 compiler_unit_free(u);
609 return 0;
610 }
611 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000612
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000613 u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS,
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +0200614 PyDict_GET_SIZE(u->u_cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615 if (!u->u_freevars) {
616 compiler_unit_free(u);
617 return 0;
618 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 u->u_blocks = NULL;
621 u->u_nfblocks = 0;
622 u->u_firstlineno = lineno;
623 u->u_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000624 u->u_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 u->u_consts = PyDict_New();
626 if (!u->u_consts) {
627 compiler_unit_free(u);
628 return 0;
629 }
630 u->u_names = PyDict_New();
631 if (!u->u_names) {
632 compiler_unit_free(u);
633 return 0;
634 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000635
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 u->u_private = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 /* Push the old compiler_unit on the stack. */
639 if (c->u) {
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400640 PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000641 if (!capsule || PyList_Append(c->c_stack, capsule) < 0) {
642 Py_XDECREF(capsule);
643 compiler_unit_free(u);
644 return 0;
645 }
646 Py_DECREF(capsule);
647 u->u_private = c->u->u_private;
648 Py_XINCREF(u->u_private);
649 }
650 c->u = u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000651
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652 c->c_nestlevel++;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100653
654 block = compiler_new_block(c);
655 if (block == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 return 0;
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +0100657 c->u->u_curblock = block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000658
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400659 if (u->u_scope_type != COMPILER_SCOPE_MODULE) {
660 if (!compiler_set_qualname(c))
661 return 0;
662 }
663
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000665}
666
Neil Schemenauerc396d9e2005-10-25 06:30:14 +0000667static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000668compiler_exit_scope(struct compiler *c)
669{
Victor Stinnerad9a0662013-11-19 22:23:20 +0100670 Py_ssize_t n;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000671 PyObject *capsule;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 c->c_nestlevel--;
674 compiler_unit_free(c->u);
675 /* Restore c->u to the parent unit. */
676 n = PyList_GET_SIZE(c->c_stack) - 1;
677 if (n >= 0) {
678 capsule = PyList_GET_ITEM(c->c_stack, n);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400679 c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 assert(c->u);
681 /* we are deleting from a list so this really shouldn't fail */
682 if (PySequence_DelItem(c->c_stack, n) < 0)
683 Py_FatalError("compiler_exit_scope()");
684 compiler_unit_check(c->u);
685 }
686 else
687 c->u = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000689}
690
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400691static int
692compiler_set_qualname(struct compiler *c)
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100693{
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100694 _Py_static_string(dot, ".");
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400695 _Py_static_string(dot_locals, ".<locals>");
696 Py_ssize_t stack_size;
697 struct compiler_unit *u = c->u;
698 PyObject *name, *base, *dot_str, *dot_locals_str;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100699
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400700 base = NULL;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100701 stack_size = PyList_GET_SIZE(c->c_stack);
Benjamin Petersona8a38b82013-10-19 16:14:39 -0400702 assert(stack_size >= 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400703 if (stack_size > 1) {
704 int scope, force_global = 0;
705 struct compiler_unit *parent;
706 PyObject *mangled, *capsule;
707
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400708 capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1);
Benjamin Peterson9e77f722015-05-07 18:41:47 -0400709 parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400710 assert(parent);
711
Yury Selivanov75445082015-05-11 22:57:16 -0400712 if (u->u_scope_type == COMPILER_SCOPE_FUNCTION
713 || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
714 || u->u_scope_type == COMPILER_SCOPE_CLASS) {
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400715 assert(u->u_name);
716 mangled = _Py_Mangle(parent->u_private, u->u_name);
717 if (!mangled)
718 return 0;
719 scope = PyST_GetScope(parent->u_ste, mangled);
720 Py_DECREF(mangled);
721 assert(scope != GLOBAL_IMPLICIT);
722 if (scope == GLOBAL_EXPLICIT)
723 force_global = 1;
724 }
725
726 if (!force_global) {
727 if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION
Yury Selivanov75445082015-05-11 22:57:16 -0400728 || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400729 || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) {
730 dot_locals_str = _PyUnicode_FromId(&dot_locals);
731 if (dot_locals_str == NULL)
732 return 0;
733 base = PyUnicode_Concat(parent->u_qualname, dot_locals_str);
734 if (base == NULL)
735 return 0;
736 }
737 else {
738 Py_INCREF(parent->u_qualname);
739 base = parent->u_qualname;
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400740 }
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100741 }
742 }
Benjamin Peterson3d9e4812013-10-19 16:01:13 -0400743
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400744 if (base != NULL) {
745 dot_str = _PyUnicode_FromId(&dot);
746 if (dot_str == NULL) {
747 Py_DECREF(base);
748 return 0;
749 }
750 name = PyUnicode_Concat(base, dot_str);
751 Py_DECREF(base);
752 if (name == NULL)
753 return 0;
754 PyUnicode_Append(&name, u->u_name);
755 if (name == NULL)
756 return 0;
757 }
758 else {
759 Py_INCREF(u->u_name);
760 name = u->u_name;
761 }
762 u->u_qualname = name;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100763
Benjamin Peterson6b4f7802013-10-20 17:50:28 -0400764 return 1;
Antoine Pitrou86a36b52011-11-25 18:56:07 +0100765}
766
Eric V. Smith235a6f02015-09-19 14:51:32 -0400767
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768/* Allocate a new block and return a pointer to it.
769 Returns NULL on error.
770*/
771
772static basicblock *
773compiler_new_block(struct compiler *c)
774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 basicblock *b;
776 struct compiler_unit *u;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000777
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000778 u = c->u;
Andy Lester7668a8b2020-03-24 23:26:44 -0500779 b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000780 if (b == NULL) {
781 PyErr_NoMemory();
782 return NULL;
783 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 /* Extend the singly linked list of blocks with new block. */
785 b->b_list = u->u_blocks;
786 u->u_blocks = b;
787 return b;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788}
789
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790static basicblock *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791compiler_next_block(struct compiler *c)
792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 basicblock *block = compiler_new_block(c);
794 if (block == NULL)
795 return 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
801static basicblock *
802compiler_use_next_block(struct compiler *c, basicblock *block)
803{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000804 assert(block != NULL);
805 c->u->u_curblock->b_next = block;
806 c->u->u_curblock = block;
807 return block;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000808}
809
810/* Returns the offset of the next instruction in the current block's
811 b_instr array. Resizes the b_instr as necessary.
812 Returns -1 on failure.
Thomas Wouters89f507f2006-12-13 04:49:30 +0000813*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000814
815static int
Andy Lester76d58772020-03-10 21:18:12 -0500816compiler_next_instr(basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000817{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 assert(b != NULL);
819 if (b->b_instr == NULL) {
Andy Lester7668a8b2020-03-24 23:26:44 -0500820 b->b_instr = (struct instr *)PyObject_Calloc(
821 DEFAULT_BLOCK_SIZE, sizeof(struct instr));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (b->b_instr == NULL) {
823 PyErr_NoMemory();
824 return -1;
825 }
826 b->b_ialloc = DEFAULT_BLOCK_SIZE;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 }
828 else if (b->b_iused == b->b_ialloc) {
829 struct instr *tmp;
830 size_t oldsize, newsize;
831 oldsize = b->b_ialloc * sizeof(struct instr);
832 newsize = oldsize << 1;
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000833
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -0700834 if (oldsize > (SIZE_MAX >> 1)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 PyErr_NoMemory();
836 return -1;
837 }
Amaury Forgeot d'Arc9c74b142008-06-18 00:47:36 +0000838
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000839 if (newsize == 0) {
840 PyErr_NoMemory();
841 return -1;
842 }
843 b->b_ialloc <<= 1;
844 tmp = (struct instr *)PyObject_Realloc(
845 (void *)b->b_instr, newsize);
846 if (tmp == NULL) {
847 PyErr_NoMemory();
848 return -1;
849 }
850 b->b_instr = tmp;
851 memset((char *)b->b_instr + oldsize, 0, newsize - oldsize);
852 }
853 return b->b_iused++;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854}
855
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200856/* Set the line number and column offset for the following instructions.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000857
Christian Heimes2202f872008-02-06 14:31:34 +0000858 The line number is reset in the following cases:
859 - when entering a new scope
860 - on each statement
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200861 - on each expression and sub-expression
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200862 - before the "except" and "finally" clauses
Thomas Wouters89f507f2006-12-13 04:49:30 +0000863*/
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000864
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +0200865#define SET_LOC(c, x) \
866 (c)->u->u_lineno = (x)->lineno; \
867 (c)->u->u_col_offset = (x)->col_offset;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200869/* Return the stack effect of opcode with argument oparg.
870
871 Some opcodes have different stack effect when jump to the target and
872 when not jump. The 'jump' parameter specifies the case:
873
874 * 0 -- when not jump
875 * 1 -- when jump
876 * -1 -- maximal
877 */
878/* XXX Make the stack effect of WITH_CLEANUP_START and
879 WITH_CLEANUP_FINISH deterministic. */
880static int
881stack_effect(int opcode, int oparg, int jump)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 switch (opcode) {
Serhiy Storchaka57faf342018-04-25 22:04:06 +0300884 case NOP:
885 case EXTENDED_ARG:
886 return 0;
887
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200888 /* Stack manipulation */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 case POP_TOP:
890 return -1;
891 case ROT_TWO:
892 case ROT_THREE:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200893 case ROT_FOUR:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 return 0;
895 case DUP_TOP:
896 return 1;
Antoine Pitrou74a69fa2010-09-04 18:43:52 +0000897 case DUP_TOP_TWO:
898 return 2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200900 /* Unary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 case UNARY_POSITIVE:
902 case UNARY_NEGATIVE:
903 case UNARY_NOT:
904 case UNARY_INVERT:
905 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 case SET_ADD:
908 case LIST_APPEND:
909 return -1;
910 case MAP_ADD:
911 return -2;
Neal Norwitz10be2ea2006-03-03 20:29:11 +0000912
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200913 /* Binary operators */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 case BINARY_POWER:
915 case BINARY_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400916 case BINARY_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000917 case BINARY_MODULO:
918 case BINARY_ADD:
919 case BINARY_SUBTRACT:
920 case BINARY_SUBSCR:
921 case BINARY_FLOOR_DIVIDE:
922 case BINARY_TRUE_DIVIDE:
923 return -1;
924 case INPLACE_FLOOR_DIVIDE:
925 case INPLACE_TRUE_DIVIDE:
926 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 case INPLACE_ADD:
929 case INPLACE_SUBTRACT:
930 case INPLACE_MULTIPLY:
Benjamin Petersond51374e2014-04-09 23:55:56 -0400931 case INPLACE_MATRIX_MULTIPLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 case INPLACE_MODULO:
933 return -1;
934 case STORE_SUBSCR:
935 return -3;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936 case DELETE_SUBSCR:
937 return -2;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000939 case BINARY_LSHIFT:
940 case BINARY_RSHIFT:
941 case BINARY_AND:
942 case BINARY_XOR:
943 case BINARY_OR:
944 return -1;
945 case INPLACE_POWER:
946 return -1;
947 case GET_ITER:
948 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 case PRINT_EXPR:
951 return -1;
952 case LOAD_BUILD_CLASS:
953 return 1;
954 case INPLACE_LSHIFT:
955 case INPLACE_RSHIFT:
956 case INPLACE_AND:
957 case INPLACE_XOR:
958 case INPLACE_OR:
959 return -1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +0200960
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 case SETUP_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200962 /* 1 in the normal flow.
963 * Restore the stack position and push 6 values before jumping to
964 * the handler if an exception be raised. */
965 return jump ? 6 : 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 case RETURN_VALUE:
967 return -1;
968 case IMPORT_STAR:
969 return -1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -0700970 case SETUP_ANNOTATIONS:
971 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000972 case YIELD_VALUE:
973 return 0;
Benjamin Peterson2afe6ae2012-03-15 15:37:39 -0500974 case YIELD_FROM:
975 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000976 case POP_BLOCK:
977 return 0;
978 case POP_EXCEPT:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200979 return -3;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 case STORE_NAME:
982 return -1;
983 case DELETE_NAME:
984 return 0;
985 case UNPACK_SEQUENCE:
986 return oparg-1;
987 case UNPACK_EX:
988 return (oparg&0xFF) + (oparg>>8);
989 case FOR_ITER:
Serhiy Storchakad4864c62018-01-09 21:54:52 +0200990 /* -1 at end of iterator, 1 if continue iterating. */
991 return jump > 0 ? -1 : 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000993 case STORE_ATTR:
994 return -2;
995 case DELETE_ATTR:
996 return -1;
997 case STORE_GLOBAL:
998 return -1;
999 case DELETE_GLOBAL:
1000 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001001 case LOAD_CONST:
1002 return 1;
1003 case LOAD_NAME:
1004 return 1;
1005 case BUILD_TUPLE:
1006 case BUILD_LIST:
1007 case BUILD_SET:
Serhiy Storchakaea525a22016-09-06 22:07:53 +03001008 case BUILD_STRING:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001009 return 1-oparg;
1010 case BUILD_MAP:
Benjamin Petersonb6855152015-09-10 21:02:39 -07001011 return 1 - 2*oparg;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001012 case BUILD_CONST_KEY_MAP:
1013 return -oparg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 case LOAD_ATTR:
1015 return 0;
1016 case COMPARE_OP:
Mark Shannon9af0e472020-01-14 10:12:45 +00001017 case IS_OP:
1018 case CONTAINS_OP:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001019 return -1;
Mark Shannon9af0e472020-01-14 10:12:45 +00001020 case JUMP_IF_NOT_EXC_MATCH:
1021 return -2;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 case IMPORT_NAME:
1023 return -1;
1024 case IMPORT_FROM:
1025 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001026
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001027 /* Jumps */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 case JUMP_FORWARD:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001029 case JUMP_ABSOLUTE:
1030 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001031
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001032 case JUMP_IF_TRUE_OR_POP:
1033 case JUMP_IF_FALSE_OR_POP:
1034 return jump ? 0 : -1;
1035
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001036 case POP_JUMP_IF_FALSE:
1037 case POP_JUMP_IF_TRUE:
1038 return -1;
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00001039
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 case LOAD_GLOBAL:
1041 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001042
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001043 /* Exception handling */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001044 case SETUP_FINALLY:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001045 /* 0 in the normal flow.
1046 * Restore the stack position and push 6 values before jumping to
1047 * the handler if an exception be raised. */
1048 return jump ? 6 : 0;
Mark Shannonfee55262019-11-21 09:11:43 +00001049 case RERAISE:
1050 return -3;
1051
1052 case WITH_EXCEPT_START:
1053 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 case LOAD_FAST:
1056 return 1;
1057 case STORE_FAST:
1058 return -1;
1059 case DELETE_FAST:
1060 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 case RAISE_VARARGS:
1063 return -oparg;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001064
1065 /* Functions and calls */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 case CALL_FUNCTION:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001067 return -oparg;
Yury Selivanovf2392132016-12-13 19:03:51 -05001068 case CALL_METHOD:
1069 return -oparg-1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 case CALL_FUNCTION_KW:
Victor Stinnerf9b760f2016-09-09 10:17:08 -07001071 return -oparg-1;
1072 case CALL_FUNCTION_EX:
Matthieu Dartiailh3a9ac822017-02-21 14:25:22 +01001073 return -1 - ((oparg & 0x01) != 0);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001074 case MAKE_FUNCTION:
1075 return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) -
1076 ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 case BUILD_SLICE:
1078 if (oparg == 3)
1079 return -2;
1080 else
1081 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001082
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001083 /* Closures */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 case LOAD_CLOSURE:
1085 return 1;
1086 case LOAD_DEREF:
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04001087 case LOAD_CLASSDEREF:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001088 return 1;
1089 case STORE_DEREF:
1090 return -1;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00001091 case DELETE_DEREF:
1092 return 0;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001093
1094 /* Iterators and generators */
Yury Selivanov75445082015-05-11 22:57:16 -04001095 case GET_AWAITABLE:
1096 return 0;
1097 case SETUP_ASYNC_WITH:
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001098 /* 0 in the normal flow.
1099 * Restore the stack position to the position before the result
1100 * of __aenter__ and push 6 values before jumping to the handler
1101 * if an exception be raised. */
1102 return jump ? -1 + 6 : 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001103 case BEFORE_ASYNC_WITH:
1104 return 1;
1105 case GET_AITER:
1106 return 0;
1107 case GET_ANEXT:
1108 return 1;
Yury Selivanov5376ba92015-06-22 12:19:30 -04001109 case GET_YIELD_FROM_ITER:
1110 return 0;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02001111 case END_ASYNC_FOR:
1112 return -7;
Eric V. Smitha78c7952015-11-03 12:45:05 -05001113 case FORMAT_VALUE:
1114 /* If there's a fmt_spec on the stack, we go from 2->1,
1115 else 1->1. */
1116 return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0;
Yury Selivanovf2392132016-12-13 19:03:51 -05001117 case LOAD_METHOD:
1118 return 1;
Zackery Spytzce6a0702019-08-25 03:44:09 -06001119 case LOAD_ASSERTION_ERROR:
1120 return 1;
Mark Shannon13bc1392020-01-23 09:25:17 +00001121 case LIST_TO_TUPLE:
1122 return 0;
1123 case LIST_EXTEND:
1124 case SET_UPDATE:
Mark Shannon8a4cd702020-01-27 09:57:45 +00001125 case DICT_MERGE:
1126 case DICT_UPDATE:
Mark Shannon13bc1392020-01-23 09:25:17 +00001127 return -1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 default:
Larry Hastings3a907972013-11-23 14:49:22 -08001129 return PY_INVALID_STACK_EFFECT;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 }
Larry Hastings3a907972013-11-23 14:49:22 -08001131 return PY_INVALID_STACK_EFFECT; /* not reachable */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132}
1133
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001134int
Serhiy Storchaka7bdf2822018-09-18 09:54:26 +03001135PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump)
1136{
1137 return stack_effect(opcode, oparg, jump);
1138}
1139
1140int
Serhiy Storchakad4864c62018-01-09 21:54:52 +02001141PyCompile_OpcodeStackEffect(int opcode, int oparg)
1142{
1143 return stack_effect(opcode, oparg, -1);
1144}
1145
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001146/* Add an opcode with no argument.
1147 Returns 0 on failure, 1 on success.
1148*/
1149
1150static int
1151compiler_addop(struct compiler *c, int opcode)
1152{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 basicblock *b;
1154 struct instr *i;
1155 int off;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001156 assert(!HAS_ARG(opcode));
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001157 if (c->c_do_not_emit_bytecode) {
1158 return 1;
1159 }
Andy Lester76d58772020-03-10 21:18:12 -05001160 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 if (off < 0)
1162 return 0;
1163 b = c->u->u_curblock;
1164 i = &b->b_instr[off];
1165 i->i_opcode = opcode;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001166 i->i_oparg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001167 if (opcode == RETURN_VALUE)
1168 b->b_return = 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001169 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001170 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001171}
1172
Victor Stinnerf8e32212013-11-19 23:56:34 +01001173static Py_ssize_t
Andy Lester76d58772020-03-10 21:18:12 -05001174compiler_add_o(PyObject *dict, PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001175{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001176 PyObject *v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 Py_ssize_t arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001179 v = PyDict_GetItemWithError(dict, o);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001180 if (!v) {
Stefan Krahc0cbed12015-07-27 12:56:49 +02001181 if (PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 return -1;
Stefan Krahc0cbed12015-07-27 12:56:49 +02001183 }
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001184 arg = PyDict_GET_SIZE(dict);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001185 v = PyLong_FromSsize_t(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001186 if (!v) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 return -1;
1188 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001189 if (PyDict_SetItem(dict, o, v) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 Py_DECREF(v);
1191 return -1;
1192 }
1193 Py_DECREF(v);
1194 }
1195 else
1196 arg = PyLong_AsLong(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001197 return arg;
1198}
1199
INADA Naokic2e16072018-11-26 21:23:22 +09001200// Merge const *o* recursively and return constant key object.
1201static PyObject*
1202merge_consts_recursive(struct compiler *c, PyObject *o)
1203{
1204 // None and Ellipsis are singleton, and key is the singleton.
1205 // No need to merge object and key.
1206 if (o == Py_None || o == Py_Ellipsis) {
1207 Py_INCREF(o);
1208 return o;
1209 }
1210
1211 PyObject *key = _PyCode_ConstantKey(o);
1212 if (key == NULL) {
1213 return NULL;
1214 }
1215
1216 // t is borrowed reference
1217 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
1218 if (t != key) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001219 // o is registered in c_const_cache. Just use it.
Zackery Spytz9b4a1b12019-03-20 03:16:25 -06001220 Py_XINCREF(t);
INADA Naokic2e16072018-11-26 21:23:22 +09001221 Py_DECREF(key);
1222 return t;
1223 }
1224
INADA Naokif7e4d362018-11-29 00:58:46 +09001225 // We registered o in c_const_cache.
Simeon63b5fc52019-04-09 19:36:57 -04001226 // When o is a tuple or frozenset, we want to merge its
INADA Naokif7e4d362018-11-29 00:58:46 +09001227 // items too.
INADA Naokic2e16072018-11-26 21:23:22 +09001228 if (PyTuple_CheckExact(o)) {
INADA Naokif7e4d362018-11-29 00:58:46 +09001229 Py_ssize_t len = PyTuple_GET_SIZE(o);
1230 for (Py_ssize_t i = 0; i < len; i++) {
INADA Naokic2e16072018-11-26 21:23:22 +09001231 PyObject *item = PyTuple_GET_ITEM(o, i);
1232 PyObject *u = merge_consts_recursive(c, item);
1233 if (u == NULL) {
1234 Py_DECREF(key);
1235 return NULL;
1236 }
1237
1238 // See _PyCode_ConstantKey()
1239 PyObject *v; // borrowed
1240 if (PyTuple_CheckExact(u)) {
1241 v = PyTuple_GET_ITEM(u, 1);
1242 }
1243 else {
1244 v = u;
1245 }
1246 if (v != item) {
1247 Py_INCREF(v);
1248 PyTuple_SET_ITEM(o, i, v);
1249 Py_DECREF(item);
1250 }
1251
1252 Py_DECREF(u);
1253 }
1254 }
INADA Naokif7e4d362018-11-29 00:58:46 +09001255 else if (PyFrozenSet_CheckExact(o)) {
Simeon63b5fc52019-04-09 19:36:57 -04001256 // *key* is tuple. And its first item is frozenset of
INADA Naokif7e4d362018-11-29 00:58:46 +09001257 // constant keys.
1258 // See _PyCode_ConstantKey() for detail.
1259 assert(PyTuple_CheckExact(key));
1260 assert(PyTuple_GET_SIZE(key) == 2);
1261
1262 Py_ssize_t len = PySet_GET_SIZE(o);
1263 if (len == 0) { // empty frozenset should not be re-created.
1264 return key;
1265 }
1266 PyObject *tuple = PyTuple_New(len);
1267 if (tuple == NULL) {
1268 Py_DECREF(key);
1269 return NULL;
1270 }
1271 Py_ssize_t i = 0, pos = 0;
1272 PyObject *item;
1273 Py_hash_t hash;
1274 while (_PySet_NextEntry(o, &pos, &item, &hash)) {
1275 PyObject *k = merge_consts_recursive(c, item);
1276 if (k == NULL) {
1277 Py_DECREF(tuple);
1278 Py_DECREF(key);
1279 return NULL;
1280 }
1281 PyObject *u;
1282 if (PyTuple_CheckExact(k)) {
1283 u = PyTuple_GET_ITEM(k, 1);
1284 Py_INCREF(u);
1285 Py_DECREF(k);
1286 }
1287 else {
1288 u = k;
1289 }
1290 PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u.
1291 i++;
1292 }
1293
1294 // Instead of rewriting o, we create new frozenset and embed in the
1295 // key tuple. Caller should get merged frozenset from the key tuple.
1296 PyObject *new = PyFrozenSet_New(tuple);
1297 Py_DECREF(tuple);
1298 if (new == NULL) {
1299 Py_DECREF(key);
1300 return NULL;
1301 }
1302 assert(PyTuple_GET_ITEM(key, 1) == o);
1303 Py_DECREF(o);
1304 PyTuple_SET_ITEM(key, 1, new);
1305 }
INADA Naokic2e16072018-11-26 21:23:22 +09001306
1307 return key;
1308}
1309
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001310static Py_ssize_t
1311compiler_add_const(struct compiler *c, PyObject *o)
1312{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001313 if (c->c_do_not_emit_bytecode) {
1314 return 0;
1315 }
1316
INADA Naokic2e16072018-11-26 21:23:22 +09001317 PyObject *key = merge_consts_recursive(c, o);
1318 if (key == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001319 return -1;
INADA Naokic2e16072018-11-26 21:23:22 +09001320 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001321
Andy Lester76d58772020-03-10 21:18:12 -05001322 Py_ssize_t arg = compiler_add_o(c->u->u_consts, key);
INADA Naokic2e16072018-11-26 21:23:22 +09001323 Py_DECREF(key);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 return arg;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001325}
1326
1327static int
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001328compiler_addop_load_const(struct compiler *c, PyObject *o)
1329{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001330 if (c->c_do_not_emit_bytecode) {
1331 return 1;
1332 }
1333
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001334 Py_ssize_t arg = compiler_add_const(c, o);
1335 if (arg < 0)
1336 return 0;
1337 return compiler_addop_i(c, LOAD_CONST, arg);
1338}
1339
1340static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001341compiler_addop_o(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001343{
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001344 if (c->c_do_not_emit_bytecode) {
1345 return 1;
1346 }
1347
Andy Lester76d58772020-03-10 21:18:12 -05001348 Py_ssize_t arg = compiler_add_o(dict, o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001349 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001350 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001351 return compiler_addop_i(c, opcode, arg);
1352}
1353
1354static int
1355compiler_addop_name(struct compiler *c, int opcode, PyObject *dict,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 PyObject *o)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001357{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001358 Py_ssize_t arg;
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001359
1360 if (c->c_do_not_emit_bytecode) {
1361 return 1;
1362 }
1363
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001364 PyObject *mangled = _Py_Mangle(c->u->u_private, o);
1365 if (!mangled)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001366 return 0;
Andy Lester76d58772020-03-10 21:18:12 -05001367 arg = compiler_add_o(dict, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368 Py_DECREF(mangled);
1369 if (arg < 0)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001370 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371 return compiler_addop_i(c, opcode, arg);
1372}
1373
1374/* Add an opcode with an integer argument.
1375 Returns 0 on failure, 1 on success.
1376*/
1377
1378static int
Victor Stinnerf8e32212013-11-19 23:56:34 +01001379compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001380{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001381 struct instr *i;
1382 int off;
Victor Stinnerad9a0662013-11-19 22:23:20 +01001383
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001384 if (c->c_do_not_emit_bytecode) {
1385 return 1;
1386 }
1387
Victor Stinner2ad474b2016-03-01 23:34:47 +01001388 /* oparg value is unsigned, but a signed C int is usually used to store
1389 it in the C code (like Python/ceval.c).
1390
1391 Limit to 32-bit signed C int (rather than INT_MAX) for portability.
1392
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001393 The argument of a concrete bytecode instruction is limited to 8-bit.
1394 EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */
1395 assert(HAS_ARG(opcode));
Victor Stinner2ad474b2016-03-01 23:34:47 +01001396 assert(0 <= oparg && oparg <= 2147483647);
Victor Stinnerad9a0662013-11-19 22:23:20 +01001397
Andy Lester76d58772020-03-10 21:18:12 -05001398 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001399 if (off < 0)
1400 return 0;
1401 i = &c->u->u_curblock->b_instr[off];
Victor Stinnerf8e32212013-11-19 23:56:34 +01001402 i->i_opcode = opcode;
1403 i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001404 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001406}
1407
1408static int
Mark Shannon582aaf12020-08-04 17:30:11 +01001409compiler_addop_j(struct compiler *c, int opcode, basicblock *b)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001410{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001411 struct instr *i;
1412 int off;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001413
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001414 if (c->c_do_not_emit_bytecode) {
1415 return 1;
1416 }
1417
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03001418 assert(HAS_ARG(opcode));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001419 assert(b != NULL);
Andy Lester76d58772020-03-10 21:18:12 -05001420 off = compiler_next_instr(c->u->u_curblock);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 if (off < 0)
1422 return 0;
1423 i = &c->u->u_curblock->b_instr[off];
1424 i->i_opcode = opcode;
1425 i->i_target = b;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02001426 i->i_lineno = c->u->u_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001428}
1429
Victor Stinnerfc6f2ef2016-02-27 02:19:22 +01001430/* NEXT_BLOCK() creates an implicit jump from the current block
1431 to the new block.
1432
1433 The returns inside this macro make it impossible to decref objects
1434 created in the local function. Local objects should use the arena.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435*/
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001436#define NEXT_BLOCK(C) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 if (compiler_next_block((C)) == NULL) \
1438 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001439}
1440
1441#define ADDOP(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001442 if (!compiler_addop((C), (OP))) \
1443 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001444}
1445
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001446#define ADDOP_IN_SCOPE(C, OP) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 if (!compiler_addop((C), (OP))) { \
1448 compiler_exit_scope(c); \
1449 return 0; \
1450 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001451}
1452
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001453#define ADDOP_LOAD_CONST(C, O) { \
1454 if (!compiler_addop_load_const((C), (O))) \
1455 return 0; \
1456}
1457
1458/* Same as ADDOP_LOAD_CONST, but steals a reference. */
1459#define ADDOP_LOAD_CONST_NEW(C, O) { \
1460 PyObject *__new_const = (O); \
1461 if (__new_const == NULL) { \
1462 return 0; \
1463 } \
1464 if (!compiler_addop_load_const((C), __new_const)) { \
1465 Py_DECREF(__new_const); \
1466 return 0; \
1467 } \
1468 Py_DECREF(__new_const); \
1469}
1470
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001471#define ADDOP_O(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001472 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1473 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001474}
1475
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03001476/* Same as ADDOP_O, but steals a reference. */
1477#define ADDOP_N(C, OP, O, TYPE) { \
1478 if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \
1479 Py_DECREF((O)); \
1480 return 0; \
1481 } \
1482 Py_DECREF((O)); \
1483}
1484
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001485#define ADDOP_NAME(C, OP, O, TYPE) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \
1487 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001488}
1489
1490#define ADDOP_I(C, OP, O) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 if (!compiler_addop_i((C), (OP), (O))) \
1492 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001493}
1494
Mark Shannon582aaf12020-08-04 17:30:11 +01001495#define ADDOP_JUMP(C, OP, O) { \
1496 if (!compiler_addop_j((C), (OP), (O))) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001497 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001498}
1499
Mark Shannon9af0e472020-01-14 10:12:45 +00001500#define ADDOP_COMPARE(C, CMP) { \
1501 if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \
1502 return 0; \
1503}
1504
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001505/* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use
1506 the ASDL name to synthesize the name of the C type and the visit function.
1507*/
1508
1509#define VISIT(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001510 if (!compiler_visit_ ## TYPE((C), (V))) \
1511 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001512}
1513
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001514#define VISIT_IN_SCOPE(C, TYPE, V) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001515 if (!compiler_visit_ ## TYPE((C), (V))) { \
1516 compiler_exit_scope(c); \
1517 return 0; \
1518 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001519}
1520
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001521#define VISIT_SLICE(C, V, CTX) {\
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522 if (!compiler_visit_slice((C), (V), (CTX))) \
1523 return 0; \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524}
1525
1526#define VISIT_SEQ(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001528 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1530 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1531 if (!compiler_visit_ ## TYPE((C), elt)) \
1532 return 0; \
1533 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001534}
1535
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001536#define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 int _i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001538 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \
1540 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \
1541 if (!compiler_visit_ ## TYPE((C), elt)) { \
1542 compiler_exit_scope(c); \
1543 return 0; \
1544 } \
1545 } \
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001546}
1547
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01001548/* These macros allows to check only for errors and not emmit bytecode
1549 * while visiting nodes.
1550*/
1551
1552#define BEGIN_DO_NOT_EMIT_BYTECODE { \
1553 c->c_do_not_emit_bytecode++;
1554
1555#define END_DO_NOT_EMIT_BYTECODE \
1556 c->c_do_not_emit_bytecode--; \
1557}
1558
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001559/* Search if variable annotations are present statically in a block. */
1560
1561static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001562find_ann(asdl_stmt_seq *stmts)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001563{
1564 int i, j, res = 0;
1565 stmt_ty st;
1566
1567 for (i = 0; i < asdl_seq_LEN(stmts); i++) {
1568 st = (stmt_ty)asdl_seq_GET(stmts, i);
1569 switch (st->kind) {
1570 case AnnAssign_kind:
1571 return 1;
1572 case For_kind:
1573 res = find_ann(st->v.For.body) ||
1574 find_ann(st->v.For.orelse);
1575 break;
1576 case AsyncFor_kind:
1577 res = find_ann(st->v.AsyncFor.body) ||
1578 find_ann(st->v.AsyncFor.orelse);
1579 break;
1580 case While_kind:
1581 res = find_ann(st->v.While.body) ||
1582 find_ann(st->v.While.orelse);
1583 break;
1584 case If_kind:
1585 res = find_ann(st->v.If.body) ||
1586 find_ann(st->v.If.orelse);
1587 break;
1588 case With_kind:
1589 res = find_ann(st->v.With.body);
1590 break;
1591 case AsyncWith_kind:
1592 res = find_ann(st->v.AsyncWith.body);
1593 break;
1594 case Try_kind:
1595 for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) {
1596 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
1597 st->v.Try.handlers, j);
1598 if (find_ann(handler->v.ExceptHandler.body)) {
1599 return 1;
1600 }
1601 }
1602 res = find_ann(st->v.Try.body) ||
1603 find_ann(st->v.Try.finalbody) ||
1604 find_ann(st->v.Try.orelse);
1605 break;
1606 default:
1607 res = 0;
1608 }
1609 if (res) {
1610 break;
1611 }
1612 }
1613 return res;
1614}
1615
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001616/*
1617 * Frame block handling functions
1618 */
1619
1620static int
1621compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b,
Mark Shannonfee55262019-11-21 09:11:43 +00001622 basicblock *exit, void *datum)
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001623{
1624 struct fblockinfo *f;
1625 if (c->u->u_nfblocks >= CO_MAXBLOCKS) {
1626 PyErr_SetString(PyExc_SyntaxError,
1627 "too many statically nested blocks");
1628 return 0;
1629 }
1630 f = &c->u->u_fblock[c->u->u_nfblocks++];
1631 f->fb_type = t;
1632 f->fb_block = b;
1633 f->fb_exit = exit;
Mark Shannonfee55262019-11-21 09:11:43 +00001634 f->fb_datum = datum;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001635 return 1;
1636}
1637
1638static void
1639compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b)
1640{
1641 struct compiler_unit *u = c->u;
1642 assert(u->u_nfblocks > 0);
1643 u->u_nfblocks--;
1644 assert(u->u_fblock[u->u_nfblocks].fb_type == t);
1645 assert(u->u_fblock[u->u_nfblocks].fb_block == b);
1646}
1647
Mark Shannonfee55262019-11-21 09:11:43 +00001648static int
1649compiler_call_exit_with_nones(struct compiler *c) {
1650 ADDOP_O(c, LOAD_CONST, Py_None, consts);
1651 ADDOP(c, DUP_TOP);
1652 ADDOP(c, DUP_TOP);
1653 ADDOP_I(c, CALL_FUNCTION, 3);
1654 return 1;
1655}
1656
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001657/* Unwind a frame block. If preserve_tos is true, the TOS before
Mark Shannonfee55262019-11-21 09:11:43 +00001658 * popping the blocks will be restored afterwards, unless another
1659 * return, break or continue is found. In which case, the TOS will
1660 * be popped.
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02001661 */
1662static int
1663compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info,
1664 int preserve_tos)
1665{
1666 switch (info->fb_type) {
1667 case WHILE_LOOP:
1668 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
1678 case EXCEPT:
1679 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
Pablo Galindoa5634c42020-09-16 19:42:00 +01001781compiler_body(struct compiler *c, asdl_stmt_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;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001844 VISIT_SEQ_IN_SCOPE(c, stmt, mod->v.Interactive.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 break;
1846 case Expression_kind:
1847 VISIT_IN_SCOPE(c, expr, mod->v.Expression.body);
1848 addNone = 0;
1849 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001850 default:
1851 PyErr_Format(PyExc_SystemError,
1852 "module kind %d should not be possible",
1853 mod->kind);
1854 return 0;
1855 }
1856 co = assemble(c, addNone);
1857 compiler_exit_scope(c);
1858 return co;
Guido van Rossum10dc2e81990-11-18 17:27:39 +00001859}
1860
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001861/* The test for LOCAL must come before the test for FREE in order to
1862 handle classes where name is both local and free. The local var is
1863 a method and the free var is a free var referenced within a method.
Jeremy Hyltone36f7782001-01-19 03:21:30 +00001864*/
1865
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866static int
1867get_ref_type(struct compiler *c, PyObject *name)
1868{
Victor Stinner0b1bc562013-05-16 22:17:17 +02001869 int scope;
Benjamin Peterson312595c2013-05-15 15:26:42 -05001870 if (c->u->u_scope_type == COMPILER_SCOPE_CLASS &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001871 _PyUnicode_EqualToASCIIString(name, "__class__"))
Benjamin Peterson312595c2013-05-15 15:26:42 -05001872 return CELL;
Victor Stinner0b1bc562013-05-16 22:17:17 +02001873 scope = PyST_GetScope(c->u->u_ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001874 if (scope == 0) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001875 _Py_FatalErrorFormat(__func__,
1876 "unknown scope for %.100s in %.100s(%s)\n"
1877 "symbols: %s\nlocals: %s\nglobals: %s",
1878 PyUnicode_AsUTF8(name),
1879 PyUnicode_AsUTF8(c->u->u_name),
1880 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)),
1881 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)),
1882 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)),
1883 PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 }
Tim Peters2a7f3842001-06-09 09:26:21 +00001885
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 return scope;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001887}
1888
1889static int
1890compiler_lookup_arg(PyObject *dict, PyObject *name)
1891{
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001892 PyObject *v;
1893 v = PyDict_GetItem(dict, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001894 if (v == NULL)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00001895 return -1;
Christian Heimes217cfd12007-12-02 14:31:20 +00001896 return PyLong_AS_LONG(v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001897}
1898
1899static int
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001900compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901{
Victor Stinnerad9a0662013-11-19 22:23:20 +01001902 Py_ssize_t i, free = PyCode_GetNumFree(co);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01001903 if (qualname == NULL)
1904 qualname = co->co_name;
1905
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001906 if (free) {
1907 for (i = 0; i < free; ++i) {
1908 /* Bypass com_addop_varname because it will generate
1909 LOAD_DEREF but LOAD_CLOSURE is needed.
1910 */
1911 PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i);
1912 int arg, reftype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001913
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001914 /* Special case: If a class contains a method with a
1915 free variable that has the same name as a method,
1916 the name will be considered free *and* local in the
1917 class. It should be handled by the closure, as
Min ho Kimc4cacc82019-07-31 08:16:13 +10001918 well as by the normal name lookup logic.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001919 */
1920 reftype = get_ref_type(c, name);
1921 if (reftype == CELL)
1922 arg = compiler_lookup_arg(c->u->u_cellvars, name);
1923 else /* (reftype == FREE) */
1924 arg = compiler_lookup_arg(c->u->u_freevars, name);
1925 if (arg == -1) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01001926 _Py_FatalErrorFormat(__func__,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001927 "lookup %s in %s %d %d\n"
1928 "freevars of %s: %s\n",
1929 PyUnicode_AsUTF8(PyObject_Repr(name)),
1930 PyUnicode_AsUTF8(c->u->u_name),
1931 reftype, arg,
1932 PyUnicode_AsUTF8(co->co_name),
1933 PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars)));
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001934 }
1935 ADDOP_I(c, LOAD_CLOSURE, arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001936 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001937 flags |= 0x08;
1938 ADDOP_I(c, BUILD_TUPLE, free);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03001940 ADDOP_LOAD_CONST(c, (PyObject*)co);
1941 ADDOP_LOAD_CONST(c, qualname);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001942 ADDOP_I(c, MAKE_FUNCTION, flags);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001944}
1945
1946static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001947compiler_decorators(struct compiler *c, asdl_expr_seq* decos)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 int i;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 if (!decos)
1952 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001953
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001954 for (i = 0; i < asdl_seq_LEN(decos); i++) {
1955 VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i));
1956 }
1957 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001958}
1959
1960static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001961compiler_visit_kwonlydefaults(struct compiler *c, asdl_arg_seq *kwonlyargs,
1962 asdl_expr_seq *kw_defaults)
Guido van Rossum4f72a782006-10-27 23:31:49 +00001963{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001964 /* Push a dict of keyword-only default values.
1965
1966 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
1967 */
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001968 int i;
1969 PyObject *keys = NULL;
1970
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001971 for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) {
1972 arg_ty arg = asdl_seq_GET(kwonlyargs, i);
1973 expr_ty default_ = asdl_seq_GET(kw_defaults, i);
1974 if (default_) {
Benjamin Peterson32c59b62012-04-17 19:53:21 -04001975 PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001976 if (!mangled) {
1977 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001978 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001979 if (keys == NULL) {
1980 keys = PyList_New(1);
1981 if (keys == NULL) {
1982 Py_DECREF(mangled);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03001983 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001984 }
1985 PyList_SET_ITEM(keys, 0, mangled);
1986 }
1987 else {
1988 int res = PyList_Append(keys, mangled);
1989 Py_DECREF(mangled);
1990 if (res == -1) {
1991 goto error;
1992 }
1993 }
1994 if (!compiler_visit_expr(c, default_)) {
1995 goto error;
1996 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001997 }
1998 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03001999 if (keys != NULL) {
2000 Py_ssize_t default_count = PyList_GET_SIZE(keys);
2001 PyObject *keys_tuple = PyList_AsTuple(keys);
2002 Py_DECREF(keys);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002003 ADDOP_LOAD_CONST_NEW(c, keys_tuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002004 ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002005 assert(default_count > 0);
2006 return 1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002007 }
2008 else {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002009 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002010 }
2011
2012error:
2013 Py_XDECREF(keys);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002014 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002015}
2016
2017static int
Guido van Rossum95e4d582018-01-26 08:20:18 -08002018compiler_visit_annexpr(struct compiler *c, expr_ty annotation)
2019{
Serhiy Storchaka64fddc42018-05-17 06:17:48 +03002020 ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation));
Guido van Rossum95e4d582018-01-26 08:20:18 -08002021 return 1;
2022}
2023
2024static int
Neal Norwitzc1505362006-12-28 06:47:50 +00002025compiler_visit_argannotation(struct compiler *c, identifier id,
2026 expr_ty annotation, PyObject *names)
2027{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002028 if (annotation) {
Victor Stinner065efc32014-02-18 22:07:56 +01002029 PyObject *mangled;
Guido van Rossum95e4d582018-01-26 08:20:18 -08002030 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
2031 VISIT(c, annexpr, annotation)
2032 }
2033 else {
2034 VISIT(c, expr, annotation);
2035 }
Victor Stinner065efc32014-02-18 22:07:56 +01002036 mangled = _Py_Mangle(c->u->u_private, id);
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002037 if (!mangled)
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002038 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002039 if (PyList_Append(names, mangled) < 0) {
2040 Py_DECREF(mangled);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002041 return 0;
Yury Selivanov34ce99f2014-02-18 12:49:41 -05002042 }
2043 Py_DECREF(mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002044 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002045 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002046}
2047
2048static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002049compiler_visit_argannotations(struct compiler *c, asdl_arg_seq* args,
Neal Norwitzc1505362006-12-28 06:47:50 +00002050 PyObject *names)
2051{
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002052 int i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002053 for (i = 0; i < asdl_seq_LEN(args); i++) {
2054 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002055 if (!compiler_visit_argannotation(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002056 c,
2057 arg->arg,
2058 arg->annotation,
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002059 names))
2060 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002061 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002062 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00002063}
2064
2065static int
2066compiler_visit_annotations(struct compiler *c, arguments_ty args,
2067 expr_ty returns)
2068{
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002069 /* Push arg annotation dict.
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002070 The expressions are evaluated out-of-order wrt the source code.
Neal Norwitzc1505362006-12-28 06:47:50 +00002071
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002072 Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002073 */
2074 static identifier return_str;
2075 PyObject *names;
Victor Stinnerad9a0662013-11-19 22:23:20 +01002076 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002077 names = PyList_New(0);
2078 if (!names)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002079 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002080
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002081 if (!compiler_visit_argannotations(c, args->args, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002082 goto error;
Pablo Galindoa0c01bf2019-05-31 15:19:50 +01002083 if (!compiler_visit_argannotations(c, args->posonlyargs, names))
2084 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002085 if (args->vararg && args->vararg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002086 !compiler_visit_argannotation(c, args->vararg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002087 args->vararg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002088 goto error;
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002089 if (!compiler_visit_argannotations(c, args->kwonlyargs, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002090 goto error;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002091 if (args->kwarg && args->kwarg->annotation &&
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002092 !compiler_visit_argannotation(c, args->kwarg->arg,
Benjamin Petersoncda75be2013-03-18 10:48:58 -07002093 args->kwarg->annotation, names))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002094 goto error;
Neal Norwitzc1505362006-12-28 06:47:50 +00002095
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002096 if (!return_str) {
2097 return_str = PyUnicode_InternFromString("return");
2098 if (!return_str)
2099 goto error;
2100 }
Yury Selivanovf315c1c2015-07-23 09:10:44 +03002101 if (!compiler_visit_argannotation(c, return_str, returns, names)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002102 goto error;
2103 }
2104
2105 len = PyList_GET_SIZE(names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002106 if (len) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002107 PyObject *keytuple = PyList_AsTuple(names);
2108 Py_DECREF(names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002109 ADDOP_LOAD_CONST_NEW(c, keytuple);
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002110 ADDOP_I(c, BUILD_CONST_KEY_MAP, len);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002111 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002112 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002113 else {
2114 Py_DECREF(names);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002115 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002116 }
Neal Norwitzc1505362006-12-28 06:47:50 +00002117
2118error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002119 Py_DECREF(names);
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03002120 return 0;
Neal Norwitzc1505362006-12-28 06:47:50 +00002121}
2122
2123static int
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002124compiler_visit_defaults(struct compiler *c, arguments_ty args)
2125{
2126 VISIT_SEQ(c, expr, args->defaults);
2127 ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults));
2128 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002129}
2130
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002131static Py_ssize_t
2132compiler_default_arguments(struct compiler *c, arguments_ty args)
2133{
2134 Py_ssize_t funcflags = 0;
2135 if (args->defaults && asdl_seq_LEN(args->defaults) > 0) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002136 if (!compiler_visit_defaults(c, args))
2137 return -1;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002138 funcflags |= 0x01;
2139 }
2140 if (args->kwonlyargs) {
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002141 int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs,
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002142 args->kw_defaults);
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002143 if (res == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002144 return -1;
2145 }
2146 else if (res > 0) {
2147 funcflags |= 0x02;
2148 }
2149 }
2150 return funcflags;
2151}
2152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002153static int
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002154forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx)
2155{
2156
2157 if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) {
2158 compiler_error(c, "cannot assign to __debug__");
2159 return 1;
2160 }
2161 return 0;
2162}
2163
2164static int
2165compiler_check_debug_one_arg(struct compiler *c, arg_ty arg)
2166{
2167 if (arg != NULL) {
2168 if (forbidden_name(c, arg->arg, Store))
2169 return 0;
2170 }
2171 return 1;
2172}
2173
2174static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01002175compiler_check_debug_args_seq(struct compiler *c, asdl_arg_seq *args)
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002176{
2177 if (args != NULL) {
Pablo Galindoee40e4b2020-04-23 03:43:08 +01002178 for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) {
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002179 if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i)))
2180 return 0;
2181 }
2182 }
2183 return 1;
2184}
2185
2186static int
2187compiler_check_debug_args(struct compiler *c, arguments_ty args)
2188{
2189 if (!compiler_check_debug_args_seq(c, args->posonlyargs))
2190 return 0;
2191 if (!compiler_check_debug_args_seq(c, args->args))
2192 return 0;
2193 if (!compiler_check_debug_one_arg(c, args->vararg))
2194 return 0;
2195 if (!compiler_check_debug_args_seq(c, args->kwonlyargs))
2196 return 0;
2197 if (!compiler_check_debug_one_arg(c, args->kwarg))
2198 return 0;
2199 return 1;
2200}
2201
2202static int
Yury Selivanov75445082015-05-11 22:57:16 -04002203compiler_function(struct compiler *c, stmt_ty s, int is_async)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002204{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002205 PyCodeObject *co;
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002206 PyObject *qualname, *docstring = NULL;
Yury Selivanov75445082015-05-11 22:57:16 -04002207 arguments_ty args;
2208 expr_ty returns;
2209 identifier name;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002210 asdl_expr_seq* decos;
2211 asdl_stmt_seq *body;
INADA Naokicb41b272017-02-23 00:31:59 +09002212 Py_ssize_t i, funcflags;
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002213 int annotations;
Yury Selivanov75445082015-05-11 22:57:16 -04002214 int scope_type;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002215 int firstlineno;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002216
Yury Selivanov75445082015-05-11 22:57:16 -04002217 if (is_async) {
2218 assert(s->kind == AsyncFunctionDef_kind);
2219
2220 args = s->v.AsyncFunctionDef.args;
2221 returns = s->v.AsyncFunctionDef.returns;
2222 decos = s->v.AsyncFunctionDef.decorator_list;
2223 name = s->v.AsyncFunctionDef.name;
2224 body = s->v.AsyncFunctionDef.body;
2225
2226 scope_type = COMPILER_SCOPE_ASYNC_FUNCTION;
2227 } else {
2228 assert(s->kind == FunctionDef_kind);
2229
2230 args = s->v.FunctionDef.args;
2231 returns = s->v.FunctionDef.returns;
2232 decos = s->v.FunctionDef.decorator_list;
2233 name = s->v.FunctionDef.name;
2234 body = s->v.FunctionDef.body;
2235
2236 scope_type = COMPILER_SCOPE_FUNCTION;
2237 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002238
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002239 if (!compiler_check_debug_args(c, args))
2240 return 0;
2241
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002242 if (!compiler_decorators(c, decos))
2243 return 0;
Guido van Rossum4f72a782006-10-27 23:31:49 +00002244
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002245 firstlineno = s->lineno;
2246 if (asdl_seq_LEN(decos)) {
2247 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2248 }
2249
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002250 funcflags = compiler_default_arguments(c, args);
2251 if (funcflags == -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002252 return 0;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002253 }
2254
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002255 annotations = compiler_visit_annotations(c, args, returns);
2256 if (annotations == 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002257 return 0;
2258 }
Serhiy Storchaka607f8a52016-06-15 20:07:53 +03002259 else if (annotations > 0) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002260 funcflags |= 0x04;
2261 }
2262
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002263 if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) {
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002264 return 0;
2265 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002266
INADA Naokicb41b272017-02-23 00:31:59 +09002267 /* if not -OO mode, add docstring */
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002268 if (c->c_optimize < 2) {
2269 docstring = _PyAST_GetDocString(body);
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002270 }
Serhiy Storchaka143ce5c2018-05-30 10:56:16 +03002271 if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002272 compiler_exit_scope(c);
2273 return 0;
2274 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002275
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002276 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002277 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002278 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
INADA Naokicb41b272017-02-23 00:31:59 +09002279 VISIT_SEQ_IN_SCOPE(c, stmt, body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002280 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002281 qualname = c->u->u_qualname;
2282 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002283 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002284 if (co == NULL) {
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002285 Py_XDECREF(qualname);
2286 Py_XDECREF(co);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002287 return 0;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002288 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002289
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002290 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002291 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002292 Py_DECREF(co);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002293
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002294 /* decorators */
2295 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2296 ADDOP_I(c, CALL_FUNCTION, 1);
2297 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002298
Yury Selivanov75445082015-05-11 22:57:16 -04002299 return compiler_nameop(c, name, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002300}
2301
2302static int
2303compiler_class(struct compiler *c, stmt_ty s)
2304{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002305 PyCodeObject *co;
2306 PyObject *str;
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002307 int i, firstlineno;
Pablo Galindoa5634c42020-09-16 19:42:00 +01002308 asdl_expr_seq *decos = s->v.ClassDef.decorator_list;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002309
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002310 if (!compiler_decorators(c, decos))
2311 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002312
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002313 firstlineno = s->lineno;
2314 if (asdl_seq_LEN(decos)) {
2315 firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno;
2316 }
2317
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002318 /* ultimately generate code for:
2319 <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>)
2320 where:
2321 <func> is a function/closure created from the class body;
2322 it has a single argument (__locals__) where the dict
2323 (or MutableSequence) representing the locals is passed
2324 <name> is the class name
2325 <bases> is the positional arguments and *varargs argument
2326 <keywords> is the keyword arguments and **kwds argument
2327 This borrows from compiler_call.
2328 */
Guido van Rossum52cc1d82007-03-18 15:41:51 +00002329
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002330 /* 1. compile the class body into a code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002331 if (!compiler_enter_scope(c, s->v.ClassDef.name,
Serhiy Storchaka95b6acf2018-10-30 13:16:02 +02002332 COMPILER_SCOPE_CLASS, (void *)s, firstlineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002333 return 0;
2334 /* this block represents what we do in the new scope */
2335 {
2336 /* use the class name for name mangling */
2337 Py_INCREF(s->v.ClassDef.name);
Serhiy Storchaka48842712016-04-06 09:45:48 +03002338 Py_XSETREF(c->u->u_private, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002339 /* load (global) __name__ ... */
2340 str = PyUnicode_InternFromString("__name__");
2341 if (!str || !compiler_nameop(c, str, Load)) {
2342 Py_XDECREF(str);
2343 compiler_exit_scope(c);
2344 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002345 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002346 Py_DECREF(str);
2347 /* ... and store it as __module__ */
2348 str = PyUnicode_InternFromString("__module__");
2349 if (!str || !compiler_nameop(c, str, Store)) {
2350 Py_XDECREF(str);
2351 compiler_exit_scope(c);
2352 return 0;
2353 }
2354 Py_DECREF(str);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002355 assert(c->u->u_qualname);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002356 ADDOP_LOAD_CONST(c, c->u->u_qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002357 str = PyUnicode_InternFromString("__qualname__");
2358 if (!str || !compiler_nameop(c, str, Store)) {
2359 Py_XDECREF(str);
2360 compiler_exit_scope(c);
2361 return 0;
2362 }
2363 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002364 /* compile the body proper */
Serhiy Storchaka73cbe7a2018-05-29 12:04:55 +03002365 if (!compiler_body(c, s->v.ClassDef.body)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002366 compiler_exit_scope(c);
2367 return 0;
2368 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002369 /* Return __classcell__ if it is referenced, otherwise return None */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002370 if (c->u->u_ste->ste_needs_class_closure) {
Nick Coghlan19d24672016-12-05 16:47:55 +10002371 /* Store __classcell__ into class namespace & return it */
Benjamin Peterson312595c2013-05-15 15:26:42 -05002372 str = PyUnicode_InternFromString("__class__");
2373 if (str == NULL) {
2374 compiler_exit_scope(c);
2375 return 0;
2376 }
2377 i = compiler_lookup_arg(c->u->u_cellvars, str);
2378 Py_DECREF(str);
Victor Stinner98e818b2013-11-05 18:07:34 +01002379 if (i < 0) {
2380 compiler_exit_scope(c);
2381 return 0;
2382 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002383 assert(i == 0);
Nick Coghlan944368e2016-09-11 14:45:49 +10002384
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002385 ADDOP_I(c, LOAD_CLOSURE, i);
Nick Coghlan19d24672016-12-05 16:47:55 +10002386 ADDOP(c, DUP_TOP);
Nick Coghlan944368e2016-09-11 14:45:49 +10002387 str = PyUnicode_InternFromString("__classcell__");
2388 if (!str || !compiler_nameop(c, str, Store)) {
2389 Py_XDECREF(str);
2390 compiler_exit_scope(c);
2391 return 0;
2392 }
2393 Py_DECREF(str);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002394 }
Benjamin Peterson312595c2013-05-15 15:26:42 -05002395 else {
Nick Coghlan19d24672016-12-05 16:47:55 +10002396 /* No methods referenced __class__, so just return None */
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02002397 assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002398 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson312595c2013-05-15 15:26:42 -05002399 }
Nick Coghlan19d24672016-12-05 16:47:55 +10002400 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002401 /* create the code object */
2402 co = assemble(c, 1);
2403 }
2404 /* leave the new scope */
2405 compiler_exit_scope(c);
2406 if (co == NULL)
2407 return 0;
Guido van Rossumd59da4b2007-05-22 18:11:13 +00002408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002409 /* 2. load the 'build_class' function */
2410 ADDOP(c, LOAD_BUILD_CLASS);
2411
2412 /* 3. load a function (or closure) made from the code object */
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002413 compiler_make_closure(c, co, 0, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002414 Py_DECREF(co);
2415
2416 /* 4. load class name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002417 ADDOP_LOAD_CONST(c, s->v.ClassDef.name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002418
2419 /* 5. generate the rest of the code for the call */
Pablo Galindoa5634c42020-09-16 19:42:00 +01002420 if (!compiler_call_helper(c, 2, s->v.ClassDef.bases, s->v.ClassDef.keywords))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002421 return 0;
2422
2423 /* 6. apply decorators */
2424 for (i = 0; i < asdl_seq_LEN(decos); i++) {
2425 ADDOP_I(c, CALL_FUNCTION, 1);
2426 }
2427
2428 /* 7. store into <name> */
2429 if (!compiler_nameop(c, s->v.ClassDef.name, Store))
2430 return 0;
2431 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002432}
2433
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02002434/* Return 0 if the expression is a constant value except named singletons.
2435 Return 1 otherwise. */
2436static int
2437check_is_arg(expr_ty e)
2438{
2439 if (e->kind != Constant_kind) {
2440 return 1;
2441 }
2442 PyObject *value = e->v.Constant.value;
2443 return (value == Py_None
2444 || value == Py_False
2445 || value == Py_True
2446 || value == Py_Ellipsis);
2447}
2448
2449/* Check operands of identity chacks ("is" and "is not").
2450 Emit a warning if any operand is a constant except named singletons.
2451 Return 0 on error.
2452 */
2453static int
2454check_compare(struct compiler *c, expr_ty e)
2455{
2456 Py_ssize_t i, n;
2457 int left = check_is_arg(e->v.Compare.left);
2458 n = asdl_seq_LEN(e->v.Compare.ops);
2459 for (i = 0; i < n; i++) {
2460 cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i);
2461 int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2462 if (op == Is || op == IsNot) {
2463 if (!right || !left) {
2464 const char *msg = (op == Is)
2465 ? "\"is\" with a literal. Did you mean \"==\"?"
2466 : "\"is not\" with a literal. Did you mean \"!=\"?";
2467 return compiler_warn(c, msg);
2468 }
2469 }
2470 left = right;
2471 }
2472 return 1;
2473}
2474
Mark Shannon9af0e472020-01-14 10:12:45 +00002475static int compiler_addcompare(struct compiler *c, cmpop_ty op)
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002476{
Mark Shannon9af0e472020-01-14 10:12:45 +00002477 int cmp;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002478 switch (op) {
2479 case Eq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002480 cmp = Py_EQ;
2481 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002482 case NotEq:
Mark Shannon9af0e472020-01-14 10:12:45 +00002483 cmp = Py_NE;
2484 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002485 case Lt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002486 cmp = Py_LT;
2487 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002488 case LtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002489 cmp = Py_LE;
2490 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002491 case Gt:
Mark Shannon9af0e472020-01-14 10:12:45 +00002492 cmp = Py_GT;
2493 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002494 case GtE:
Mark Shannon9af0e472020-01-14 10:12:45 +00002495 cmp = Py_GE;
2496 break;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002497 case Is:
Mark Shannon9af0e472020-01-14 10:12:45 +00002498 ADDOP_I(c, IS_OP, 0);
2499 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002500 case IsNot:
Mark Shannon9af0e472020-01-14 10:12:45 +00002501 ADDOP_I(c, IS_OP, 1);
2502 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002503 case In:
Mark Shannon9af0e472020-01-14 10:12:45 +00002504 ADDOP_I(c, CONTAINS_OP, 0);
2505 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002506 case NotIn:
Mark Shannon9af0e472020-01-14 10:12:45 +00002507 ADDOP_I(c, CONTAINS_OP, 1);
2508 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002509 default:
Mark Shannon9af0e472020-01-14 10:12:45 +00002510 Py_UNREACHABLE();
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002511 }
Mark Shannon9af0e472020-01-14 10:12:45 +00002512 ADDOP_I(c, COMPARE_OP, cmp);
2513 return 1;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002514}
2515
Mark Shannon9af0e472020-01-14 10:12:45 +00002516
2517
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002518static int
2519compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond)
2520{
2521 switch (e->kind) {
2522 case UnaryOp_kind:
2523 if (e->v.UnaryOp.op == Not)
2524 return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond);
2525 /* fallback to general implementation */
2526 break;
2527 case BoolOp_kind: {
Pablo Galindoa5634c42020-09-16 19:42:00 +01002528 asdl_expr_seq *s = e->v.BoolOp.values;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002529 Py_ssize_t i, n = asdl_seq_LEN(s) - 1;
2530 assert(n >= 0);
2531 int cond2 = e->v.BoolOp.op == Or;
2532 basicblock *next2 = next;
2533 if (!cond2 != !cond) {
2534 next2 = compiler_new_block(c);
2535 if (next2 == NULL)
2536 return 0;
2537 }
2538 for (i = 0; i < n; ++i) {
2539 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2))
2540 return 0;
2541 }
2542 if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond))
2543 return 0;
2544 if (next2 != next)
2545 compiler_use_next_block(c, next2);
2546 return 1;
2547 }
2548 case IfExp_kind: {
2549 basicblock *end, *next2;
2550 end = compiler_new_block(c);
2551 if (end == NULL)
2552 return 0;
2553 next2 = compiler_new_block(c);
2554 if (next2 == NULL)
2555 return 0;
2556 if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0))
2557 return 0;
2558 if (!compiler_jump_if(c, e->v.IfExp.body, next, cond))
2559 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002560 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002561 compiler_use_next_block(c, next2);
2562 if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond))
2563 return 0;
2564 compiler_use_next_block(c, end);
2565 return 1;
2566 }
2567 case Compare_kind: {
2568 Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1;
2569 if (n > 0) {
Serhiy Storchaka45835252019-02-16 08:29:46 +02002570 if (!check_compare(c, e)) {
2571 return 0;
2572 }
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002573 basicblock *cleanup = compiler_new_block(c);
2574 if (cleanup == NULL)
2575 return 0;
2576 VISIT(c, expr, e->v.Compare.left);
2577 for (i = 0; i < n; i++) {
2578 VISIT(c, expr,
2579 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
2580 ADDOP(c, DUP_TOP);
2581 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00002582 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01002583 ADDOP_JUMP(c, POP_JUMP_IF_FALSE, cleanup);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002584 NEXT_BLOCK(c);
2585 }
2586 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00002587 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Mark Shannon582aaf12020-08-04 17:30:11 +01002588 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002589 basicblock *end = compiler_new_block(c);
2590 if (end == NULL)
2591 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01002592 ADDOP_JUMP(c, JUMP_FORWARD, end);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002593 compiler_use_next_block(c, cleanup);
2594 ADDOP(c, POP_TOP);
2595 if (!cond) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002596 ADDOP_JUMP(c, JUMP_FORWARD, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002597 }
2598 compiler_use_next_block(c, end);
2599 return 1;
2600 }
2601 /* fallback to general implementation */
2602 break;
2603 }
2604 default:
2605 /* fallback to general implementation */
2606 break;
2607 }
2608
2609 /* general implementation */
2610 VISIT(c, expr, e);
Mark Shannon582aaf12020-08-04 17:30:11 +01002611 ADDOP_JUMP(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002612 return 1;
2613}
2614
2615static int
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002616compiler_ifexp(struct compiler *c, expr_ty e)
2617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002618 basicblock *end, *next;
2619
2620 assert(e->kind == IfExp_kind);
2621 end = compiler_new_block(c);
2622 if (end == NULL)
2623 return 0;
2624 next = compiler_new_block(c);
2625 if (next == NULL)
2626 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002627 if (!compiler_jump_if(c, e->v.IfExp.test, next, 0))
2628 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002629 VISIT(c, expr, e->v.IfExp.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002630 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002631 compiler_use_next_block(c, next);
2632 VISIT(c, expr, e->v.IfExp.orelse);
2633 compiler_use_next_block(c, end);
2634 return 1;
Thomas Woutersdca3b9c2006-02-27 00:24:13 +00002635}
2636
2637static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002638compiler_lambda(struct compiler *c, expr_ty e)
2639{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002640 PyCodeObject *co;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002641 PyObject *qualname;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002642 static identifier name;
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002643 Py_ssize_t funcflags;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002644 arguments_ty args = e->v.Lambda.args;
2645 assert(e->kind == Lambda_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002646
Pablo Galindoc5fc1562020-04-22 23:29:27 +01002647 if (!compiler_check_debug_args(c, args))
2648 return 0;
2649
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002650 if (!name) {
2651 name = PyUnicode_InternFromString("<lambda>");
2652 if (!name)
2653 return 0;
2654 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002655
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002656 funcflags = compiler_default_arguments(c, args);
2657 if (funcflags == -1) {
2658 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002659 }
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002660
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002661 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA,
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002662 (void *)e, e->lineno))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002663 return 0;
Neal Norwitz4737b232005-11-19 23:58:29 +00002664
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002665 /* Make None the first constant, so the lambda can't have a
2666 docstring. */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002667 if (compiler_add_const(c, Py_None) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002668 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002670 c->u->u_argcount = asdl_seq_LEN(args->args);
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01002671 c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002672 c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs);
2673 VISIT_IN_SCOPE(c, expr, e->v.Lambda.body);
2674 if (c->u->u_ste->ste_generator) {
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002675 co = assemble(c, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002676 }
2677 else {
2678 ADDOP_IN_SCOPE(c, RETURN_VALUE);
Serhiy Storchakac775ad62015-03-11 18:20:35 +02002679 co = assemble(c, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002680 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002681 qualname = c->u->u_qualname;
2682 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002683 compiler_exit_scope(c);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04002684 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002685 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002686
Serhiy Storchaka64204de2016-06-12 17:36:24 +03002687 compiler_make_closure(c, co, funcflags, qualname);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01002688 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002689 Py_DECREF(co);
2690
2691 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002692}
2693
2694static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002695compiler_if(struct compiler *c, stmt_ty s)
2696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002697 basicblock *end, *next;
2698 int constant;
2699 assert(s->kind == If_kind);
2700 end = compiler_new_block(c);
2701 if (end == NULL)
2702 return 0;
2703
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002704 constant = expr_constant(s->v.If.test);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002705 /* constant = 0: "if 0"
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002706 * constant = 1: "if 1", "if 2", ...
2707 * constant = -1: rest */
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002708 if (constant == 0) {
2709 BEGIN_DO_NOT_EMIT_BYTECODE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002710 VISIT_SEQ(c, stmt, s->v.If.body);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002711 END_DO_NOT_EMIT_BYTECODE
2712 if (s->v.If.orelse) {
2713 VISIT_SEQ(c, stmt, s->v.If.orelse);
2714 }
2715 } else if (constant == 1) {
2716 VISIT_SEQ(c, stmt, s->v.If.body);
2717 if (s->v.If.orelse) {
2718 BEGIN_DO_NOT_EMIT_BYTECODE
2719 VISIT_SEQ(c, stmt, s->v.If.orelse);
2720 END_DO_NOT_EMIT_BYTECODE
2721 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002722 } else {
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002723 if (asdl_seq_LEN(s->v.If.orelse)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002724 next = compiler_new_block(c);
2725 if (next == NULL)
2726 return 0;
2727 }
Mark Shannonfee55262019-11-21 09:11:43 +00002728 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002729 next = end;
Mark Shannonfee55262019-11-21 09:11:43 +00002730 }
2731 if (!compiler_jump_if(c, s->v.If.test, next, 0)) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002732 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002733 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002734 VISIT_SEQ(c, stmt, s->v.If.body);
Antoine Pitroue7811fc2014-09-18 03:06:50 +02002735 if (asdl_seq_LEN(s->v.If.orelse)) {
Mark Shannon582aaf12020-08-04 17:30:11 +01002736 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002737 compiler_use_next_block(c, next);
2738 VISIT_SEQ(c, stmt, s->v.If.orelse);
2739 }
2740 }
2741 compiler_use_next_block(c, end);
2742 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002743}
2744
2745static int
2746compiler_for(struct compiler *c, stmt_ty s)
2747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002748 basicblock *start, *cleanup, *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002750 start = compiler_new_block(c);
2751 cleanup = compiler_new_block(c);
2752 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002753 if (start == NULL || end == NULL || cleanup == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002754 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002755 }
2756 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002757 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002758 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002759 VISIT(c, expr, s->v.For.iter);
2760 ADDOP(c, GET_ITER);
2761 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01002762 ADDOP_JUMP(c, FOR_ITER, cleanup);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002763 VISIT(c, expr, s->v.For.target);
2764 VISIT_SEQ(c, stmt, s->v.For.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002765 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002766 compiler_use_next_block(c, cleanup);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002767
2768 compiler_pop_fblock(c, FOR_LOOP, start);
2769
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002770 VISIT_SEQ(c, stmt, s->v.For.orelse);
2771 compiler_use_next_block(c, end);
2772 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002773}
2774
Yury Selivanov75445082015-05-11 22:57:16 -04002775
2776static int
2777compiler_async_for(struct compiler *c, stmt_ty s)
2778{
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002779 basicblock *start, *except, *end;
Pablo Galindo90235812020-03-15 04:29:22 +00002780 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07002781 c->u->u_ste->ste_coroutine = 1;
2782 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) {
Zsolt Dollensteine2396502018-04-27 08:58:56 -07002783 return compiler_error(c, "'async for' outside async function");
2784 }
2785
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002786 start = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002787 except = compiler_new_block(c);
2788 end = compiler_new_block(c);
Yury Selivanov75445082015-05-11 22:57:16 -04002789
Mark Shannonfee55262019-11-21 09:11:43 +00002790 if (start == NULL || except == NULL || end == NULL) {
Yury Selivanov75445082015-05-11 22:57:16 -04002791 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002792 }
Yury Selivanov75445082015-05-11 22:57:16 -04002793 VISIT(c, expr, s->v.AsyncFor.iter);
2794 ADDOP(c, GET_AITER);
Yury Selivanov75445082015-05-11 22:57:16 -04002795
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002796 compiler_use_next_block(c, start);
Mark Shannonfee55262019-11-21 09:11:43 +00002797 if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) {
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002798 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00002799 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002800 /* SETUP_FINALLY to guard the __anext__ call */
Mark Shannon582aaf12020-08-04 17:30:11 +01002801 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov75445082015-05-11 22:57:16 -04002802 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002803 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04002804 ADDOP(c, YIELD_FROM);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002805 ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */
Yury Selivanov75445082015-05-11 22:57:16 -04002806
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002807 /* Success block for __anext__ */
2808 VISIT(c, expr, s->v.AsyncFor.target);
2809 VISIT_SEQ(c, stmt, s->v.AsyncFor.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002810 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002811
2812 compiler_pop_fblock(c, FOR_LOOP, start);
Yury Selivanov75445082015-05-11 22:57:16 -04002813
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002814 /* Except block for __anext__ */
Yury Selivanov75445082015-05-11 22:57:16 -04002815 compiler_use_next_block(c, except);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02002816 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov75445082015-05-11 22:57:16 -04002817
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002818 /* `else` block */
Yury Selivanov75445082015-05-11 22:57:16 -04002819 VISIT_SEQ(c, stmt, s->v.For.orelse);
2820
2821 compiler_use_next_block(c, end);
2822
2823 return 1;
2824}
2825
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002826static int
2827compiler_while(struct compiler *c, stmt_ty s)
2828{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002829 basicblock *loop, *orelse, *end, *anchor = NULL;
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02002830 int constant = expr_constant(s->v.While.test);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002832 if (constant == 0) {
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002833 BEGIN_DO_NOT_EMIT_BYTECODE
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002834 // Push a dummy block so the VISIT_SEQ knows that we are
2835 // inside a while loop so it can correctly evaluate syntax
2836 // errors.
Mark Shannonfee55262019-11-21 09:11:43 +00002837 if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) {
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002838 return 0;
2839 }
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002840 VISIT_SEQ(c, stmt, s->v.While.body);
Pablo Galindo6c3e66a2019-10-30 11:53:26 +00002841 // Remove the dummy block now that is not needed.
2842 compiler_pop_fblock(c, WHILE_LOOP, NULL);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002843 END_DO_NOT_EMIT_BYTECODE
2844 if (s->v.While.orelse) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002845 VISIT_SEQ(c, stmt, s->v.While.orelse);
Pablo Galindo18c5f9d2019-07-15 10:15:01 +01002846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002847 return 1;
2848 }
2849 loop = compiler_new_block(c);
2850 end = compiler_new_block(c);
2851 if (constant == -1) {
2852 anchor = compiler_new_block(c);
2853 if (anchor == NULL)
2854 return 0;
2855 }
2856 if (loop == NULL || end == NULL)
2857 return 0;
2858 if (s->v.While.orelse) {
2859 orelse = compiler_new_block(c);
2860 if (orelse == NULL)
2861 return 0;
2862 }
2863 else
2864 orelse = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002866 compiler_use_next_block(c, loop);
Mark Shannonfee55262019-11-21 09:11:43 +00002867 if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002868 return 0;
2869 if (constant == -1) {
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03002870 if (!compiler_jump_if(c, s->v.While.test, anchor, 0))
2871 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002872 }
2873 VISIT_SEQ(c, stmt, s->v.While.body);
Mark Shannon582aaf12020-08-04 17:30:11 +01002874 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002876 /* XXX should the two POP instructions be in a separate block
2877 if there is no else clause ?
2878 */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002879
Benjamin Peterson3cda0ed2014-12-13 16:06:19 -05002880 if (constant == -1)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002881 compiler_use_next_block(c, anchor);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002882 compiler_pop_fblock(c, WHILE_LOOP, loop);
2883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002884 if (orelse != NULL) /* what if orelse is just pass? */
2885 VISIT_SEQ(c, stmt, s->v.While.orelse);
2886 compiler_use_next_block(c, end);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002888 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002889}
2890
2891static int
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002892compiler_return(struct compiler *c, stmt_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002893{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002894 int preserve_tos = ((s->v.Return.value != NULL) &&
Serhiy Storchaka3f228112018-09-27 17:42:37 +03002895 (s->v.Return.value->kind != Constant_kind));
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002896 if (c->u->u_ste->ste_type != FunctionBlock)
2897 return compiler_error(c, "'return' outside function");
2898 if (s->v.Return.value != NULL &&
2899 c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator)
2900 {
2901 return compiler_error(
2902 c, "'return' with value in async generator");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002903 }
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002904 if (preserve_tos) {
2905 VISIT(c, expr, s->v.Return.value);
2906 }
Mark Shannonfee55262019-11-21 09:11:43 +00002907 if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL))
2908 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002909 if (s->v.Return.value == NULL) {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03002910 ADDOP_LOAD_CONST(c, Py_None);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002911 }
2912 else if (!preserve_tos) {
2913 VISIT(c, expr, s->v.Return.value);
2914 }
2915 ADDOP(c, RETURN_VALUE);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002916
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002917 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002918}
2919
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002920static int
2921compiler_break(struct compiler *c)
2922{
Mark Shannonfee55262019-11-21 09:11:43 +00002923 struct fblockinfo *loop = NULL;
2924 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2925 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002926 }
Mark Shannonfee55262019-11-21 09:11:43 +00002927 if (loop == NULL) {
2928 return compiler_error(c, "'break' outside loop");
2929 }
2930 if (!compiler_unwind_fblock(c, loop, 0)) {
2931 return 0;
2932 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002933 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_exit);
Mark Shannonfee55262019-11-21 09:11:43 +00002934 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002935}
2936
2937static int
2938compiler_continue(struct compiler *c)
2939{
Mark Shannonfee55262019-11-21 09:11:43 +00002940 struct fblockinfo *loop = NULL;
2941 if (!compiler_unwind_fblock_stack(c, 0, &loop)) {
2942 return 0;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002943 }
Mark Shannonfee55262019-11-21 09:11:43 +00002944 if (loop == NULL) {
2945 return compiler_error(c, "'continue' not properly in loop");
2946 }
Mark Shannon582aaf12020-08-04 17:30:11 +01002947 ADDOP_JUMP(c, JUMP_ABSOLUTE, loop->fb_block);
Mark Shannonfee55262019-11-21 09:11:43 +00002948 return 1;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002949}
2950
2951
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002952/* Code generated for "try: <body> finally: <finalbody>" is as follows:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002953
2954 SETUP_FINALLY L
2955 <code for body>
2956 POP_BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00002957 <code for finalbody>
2958 JUMP E
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002959 L:
2960 <code for finalbody>
Mark Shannonfee55262019-11-21 09:11:43 +00002961 E:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002962
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002963 The special instructions use the block stack. Each block
2964 stack entry contains the instruction that created it (here
2965 SETUP_FINALLY), the level of the value stack at the time the
2966 block stack entry was created, and a label (here L).
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002967
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002968 SETUP_FINALLY:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002969 Pushes the current value stack level and the label
2970 onto the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002971 POP_BLOCK:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002972 Pops en entry from the block stack.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002973
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002974 The block stack is unwound when an exception is raised:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002975 when a SETUP_FINALLY entry is found, the raised and the caught
2976 exceptions are pushed onto the value stack (and the exception
2977 condition is cleared), and the interpreter jumps to the label
2978 gotten from the block stack.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002979*/
2980
2981static int
2982compiler_try_finally(struct compiler *c, stmt_ty s)
2983{
Mark Shannonfee55262019-11-21 09:11:43 +00002984 basicblock *body, *end, *exit;
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002985
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002986 body = compiler_new_block(c);
2987 end = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00002988 exit = compiler_new_block(c);
2989 if (body == NULL || end == NULL || exit == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002990 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002991
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02002992 /* `try` block */
Mark Shannon582aaf12020-08-04 17:30:11 +01002993 ADDOP_JUMP(c, SETUP_FINALLY, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002994 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00002995 if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002996 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05002997 if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) {
2998 if (!compiler_try_except(c, s))
2999 return 0;
3000 }
3001 else {
3002 VISIT_SEQ(c, stmt, s->v.Try.body);
3003 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003004 ADDOP(c, POP_BLOCK);
Mark Shannonfee55262019-11-21 09:11:43 +00003005 compiler_pop_fblock(c, FINALLY_TRY, body);
3006 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
Mark Shannon582aaf12020-08-04 17:30:11 +01003007 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00003008 /* `finally` block */
3009 compiler_use_next_block(c, end);
3010 if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL))
3011 return 0;
3012 VISIT_SEQ(c, stmt, s->v.Try.finalbody);
3013 compiler_pop_fblock(c, FINALLY_END, end);
3014 ADDOP(c, RERAISE);
3015 compiler_use_next_block(c, exit);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003016 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003017}
3018
3019/*
Jeffrey Yasskin9de7ec72009-02-25 02:25:04 +00003020 Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...":
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003021 (The contents of the value stack is shown in [], with the top
3022 at the right; 'tb' is trace-back info, 'val' the exception's
3023 associated value, and 'exc' the exception.)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003024
3025 Value stack Label Instruction Argument
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003026 [] SETUP_FINALLY L1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003027 [] <code for S>
3028 [] POP_BLOCK
3029 [] JUMP_FORWARD L0
3030
3031 [tb, val, exc] L1: DUP )
3032 [tb, val, exc, exc] <evaluate E1> )
Mark Shannon9af0e472020-01-14 10:12:45 +00003033 [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003034 [tb, val, exc] POP
3035 [tb, val] <assign to V1> (or POP if no V1)
3036 [tb] POP
3037 [] <code for S1>
3038 JUMP_FORWARD L0
3039
3040 [tb, val, exc] L2: DUP
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003041 .............................etc.......................
3042
Mark Shannonfee55262019-11-21 09:11:43 +00003043 [tb, val, exc] Ln+1: RERAISE # re-raise exception
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003044
3045 [] L0: <next statement>
3046
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003047 Of course, parts are not generated if Vi or Ei is not present.
3048*/
3049static int
3050compiler_try_except(struct compiler *c, stmt_ty s)
3051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003052 basicblock *body, *orelse, *except, *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003053 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003055 body = compiler_new_block(c);
3056 except = compiler_new_block(c);
3057 orelse = compiler_new_block(c);
3058 end = compiler_new_block(c);
3059 if (body == NULL || except == NULL || orelse == NULL || end == NULL)
3060 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003061 ADDOP_JUMP(c, SETUP_FINALLY, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003062 compiler_use_next_block(c, body);
Mark Shannonfee55262019-11-21 09:11:43 +00003063 if (!compiler_push_fblock(c, EXCEPT, body, NULL, NULL))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003064 return 0;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003065 VISIT_SEQ(c, stmt, s->v.Try.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003066 ADDOP(c, POP_BLOCK);
3067 compiler_pop_fblock(c, EXCEPT, body);
Mark Shannon582aaf12020-08-04 17:30:11 +01003068 ADDOP_JUMP(c, JUMP_FORWARD, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003069 n = asdl_seq_LEN(s->v.Try.handlers);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003070 compiler_use_next_block(c, except);
3071 for (i = 0; i < n; i++) {
3072 excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET(
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003073 s->v.Try.handlers, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003074 if (!handler->v.ExceptHandler.type && i < n-1)
3075 return compiler_error(c, "default 'except:' must be last");
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003076 SET_LOC(c, handler);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003077 except = compiler_new_block(c);
3078 if (except == NULL)
3079 return 0;
3080 if (handler->v.ExceptHandler.type) {
3081 ADDOP(c, DUP_TOP);
3082 VISIT(c, expr, handler->v.ExceptHandler.type);
Mark Shannon582aaf12020-08-04 17:30:11 +01003083 ADDOP_JUMP(c, JUMP_IF_NOT_EXC_MATCH, except);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003084 }
3085 ADDOP(c, POP_TOP);
3086 if (handler->v.ExceptHandler.name) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003087 basicblock *cleanup_end, *cleanup_body;
Guido van Rossumb940e112007-01-10 16:19:56 +00003088
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003089 cleanup_end = compiler_new_block(c);
3090 cleanup_body = compiler_new_block(c);
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003091 if (cleanup_end == NULL || cleanup_body == NULL) {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003092 return 0;
Zackery Spytz53ebf4b2018-10-11 23:54:03 -06003093 }
Guido van Rossumb940e112007-01-10 16:19:56 +00003094
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003095 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3096 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003097
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003098 /*
3099 try:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003100 # body
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003101 except type as name:
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003102 try:
3103 # body
3104 finally:
Chris Angelicoad098b62019-05-21 23:34:19 +10003105 name = None # in case body contains "del name"
Ezio Melotti1b6424f2013-04-19 07:10:09 +03003106 del name
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003107 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003108
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003109 /* second try: */
Mark Shannon582aaf12020-08-04 17:30:11 +01003110 ADDOP_JUMP(c, SETUP_FINALLY, cleanup_end);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003111 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003112 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003113 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003114
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003115 /* second # body */
3116 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003117 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003118 ADDOP(c, POP_BLOCK);
3119 ADDOP(c, POP_EXCEPT);
3120 /* name = None; del name */
3121 ADDOP_LOAD_CONST(c, Py_None);
3122 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
3123 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Mark Shannon582aaf12020-08-04 17:30:11 +01003124 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003125
Mark Shannonfee55262019-11-21 09:11:43 +00003126 /* except: */
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003127 compiler_use_next_block(c, cleanup_end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003128
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003129 /* name = None; del name */
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003130 ADDOP_LOAD_CONST(c, Py_None);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003131 compiler_nameop(c, handler->v.ExceptHandler.name, Store);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003132 compiler_nameop(c, handler->v.ExceptHandler.name, Del);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003133
Mark Shannonfee55262019-11-21 09:11:43 +00003134 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003135 }
3136 else {
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003137 basicblock *cleanup_body;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003138
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003139 cleanup_body = compiler_new_block(c);
Benjamin Peterson0a5dad92011-05-27 14:17:04 -05003140 if (!cleanup_body)
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003141 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003142
Guido van Rossumb940e112007-01-10 16:19:56 +00003143 ADDOP(c, POP_TOP);
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003144 ADDOP(c, POP_TOP);
3145 compiler_use_next_block(c, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003146 if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL))
Benjamin Peterson74897ba2011-05-27 14:10:24 -05003147 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003148 VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003149 compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body);
Mark Shannonfee55262019-11-21 09:11:43 +00003150 ADDOP(c, POP_EXCEPT);
Mark Shannon582aaf12020-08-04 17:30:11 +01003151 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003152 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003153 compiler_use_next_block(c, except);
3154 }
Mark Shannonfee55262019-11-21 09:11:43 +00003155 ADDOP(c, RERAISE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003156 compiler_use_next_block(c, orelse);
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003157 VISIT_SEQ(c, stmt, s->v.Try.orelse);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003158 compiler_use_next_block(c, end);
3159 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003160}
3161
3162static int
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003163compiler_try(struct compiler *c, stmt_ty s) {
3164 if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody))
3165 return compiler_try_finally(c, s);
3166 else
3167 return compiler_try_except(c, s);
3168}
3169
3170
3171static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003172compiler_import_as(struct compiler *c, identifier name, identifier asname)
3173{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003174 /* The IMPORT_NAME opcode was already generated. This function
3175 merely needs to bind the result to a name.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003176
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003177 If there is a dot in name, we need to split it and emit a
Serhiy Storchakaf93234b2017-05-09 22:31:05 +03003178 IMPORT_FROM for each name.
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003179 */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003180 Py_ssize_t len = PyUnicode_GET_LENGTH(name);
3181 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003182 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003183 return 0;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003184 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003185 /* Consume the base module name to get the first attribute */
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003186 while (1) {
3187 Py_ssize_t pos = dot + 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003188 PyObject *attr;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003189 dot = PyUnicode_FindChar(name, '.', pos, len, 1);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003190 if (dot == -2)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003191 return 0;
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003192 attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003193 if (!attr)
Serhiy Storchaka694de3b2016-06-15 20:06:07 +03003194 return 0;
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003195 ADDOP_N(c, IMPORT_FROM, attr, names);
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003196 if (dot == -1) {
3197 break;
3198 }
3199 ADDOP(c, ROT_TWO);
3200 ADDOP(c, POP_TOP);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003201 }
Serhiy Storchaka265fcc52017-08-29 15:47:44 +03003202 if (!compiler_nameop(c, asname, Store)) {
3203 return 0;
3204 }
3205 ADDOP(c, POP_TOP);
3206 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003207 }
3208 return compiler_nameop(c, asname, Store);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003209}
3210
3211static int
3212compiler_import(struct compiler *c, stmt_ty s)
3213{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003214 /* The Import node stores a module name like a.b.c as a single
3215 string. This is convenient for all cases except
3216 import a.b.c as d
3217 where we need to parse that string to extract the individual
3218 module names.
3219 XXX Perhaps change the representation to make this case simpler?
3220 */
Victor Stinnerad9a0662013-11-19 22:23:20 +01003221 Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names);
Thomas Woutersf7f438b2006-02-28 16:09:29 +00003222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003223 for (i = 0; i < n; i++) {
3224 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i);
3225 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003226
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003227 ADDOP_LOAD_CONST(c, _PyLong_Zero);
3228 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003229 ADDOP_NAME(c, IMPORT_NAME, alias->name, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003230
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003231 if (alias->asname) {
3232 r = compiler_import_as(c, alias->name, alias->asname);
3233 if (!r)
3234 return r;
3235 }
3236 else {
3237 identifier tmp = alias->name;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003238 Py_ssize_t dot = PyUnicode_FindChar(
3239 alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1);
Victor Stinner6b64a682013-07-11 22:50:45 +02003240 if (dot != -1) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003241 tmp = PyUnicode_Substring(alias->name, 0, dot);
Victor Stinner6b64a682013-07-11 22:50:45 +02003242 if (tmp == NULL)
3243 return 0;
3244 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003245 r = compiler_nameop(c, tmp, Store);
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003246 if (dot != -1) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003247 Py_DECREF(tmp);
3248 }
3249 if (!r)
3250 return r;
3251 }
3252 }
3253 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003254}
3255
3256static int
3257compiler_from_import(struct compiler *c, stmt_ty s)
3258{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003259 Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003260 PyObject *names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003261 static PyObject *empty_string;
Benjamin Peterson78565b22009-06-28 19:19:51 +00003262
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003263 if (!empty_string) {
3264 empty_string = PyUnicode_FromString("");
3265 if (!empty_string)
3266 return 0;
3267 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003268
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003269 ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level));
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02003270
3271 names = PyTuple_New(n);
3272 if (!names)
3273 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003274
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003275 /* build up the names */
3276 for (i = 0; i < n; i++) {
3277 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3278 Py_INCREF(alias->name);
3279 PyTuple_SET_ITEM(names, i, alias->name);
3280 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003281
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003282 if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003283 _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003284 Py_DECREF(names);
3285 return compiler_error(c, "from __future__ imports must occur "
3286 "at the beginning of the file");
3287 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003288 ADDOP_LOAD_CONST_NEW(c, names);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003289
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003290 if (s->v.ImportFrom.module) {
3291 ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names);
3292 }
3293 else {
3294 ADDOP_NAME(c, IMPORT_NAME, empty_string, names);
3295 }
3296 for (i = 0; i < n; i++) {
3297 alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i);
3298 identifier store_name;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003299
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003300 if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003301 assert(n == 1);
3302 ADDOP(c, IMPORT_STAR);
3303 return 1;
3304 }
3305
3306 ADDOP_NAME(c, IMPORT_FROM, alias->name, names);
3307 store_name = alias->name;
3308 if (alias->asname)
3309 store_name = alias->asname;
3310
3311 if (!compiler_nameop(c, store_name, Store)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003312 return 0;
3313 }
3314 }
3315 /* remove imported module */
3316 ADDOP(c, POP_TOP);
3317 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003318}
3319
3320static int
3321compiler_assert(struct compiler *c, stmt_ty s)
3322{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003323 basicblock *end;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003324
Georg Brandl8334fd92010-12-04 10:26:46 +00003325 if (c->c_optimize)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003326 return 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003327 if (s->v.Assert.test->kind == Tuple_kind &&
Serhiy Storchakad31e7732018-10-21 10:09:39 +03003328 asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0)
3329 {
3330 if (!compiler_warn(c, "assertion is always true, "
3331 "perhaps remove parentheses?"))
3332 {
Victor Stinner14e461d2013-08-26 22:28:21 +02003333 return 0;
3334 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003335 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003336 end = compiler_new_block(c);
3337 if (end == NULL)
3338 return 0;
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03003339 if (!compiler_jump_if(c, s->v.Assert.test, end, 1))
3340 return 0;
Zackery Spytzce6a0702019-08-25 03:44:09 -06003341 ADDOP(c, LOAD_ASSERTION_ERROR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003342 if (s->v.Assert.msg) {
3343 VISIT(c, expr, s->v.Assert.msg);
3344 ADDOP_I(c, CALL_FUNCTION, 1);
3345 }
3346 ADDOP_I(c, RAISE_VARARGS, 1);
3347 compiler_use_next_block(c, end);
3348 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003349}
3350
3351static int
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003352compiler_visit_stmt_expr(struct compiler *c, expr_ty value)
3353{
3354 if (c->c_interactive && c->c_nestlevel <= 1) {
3355 VISIT(c, expr, value);
3356 ADDOP(c, PRINT_EXPR);
3357 return 1;
3358 }
3359
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003360 if (value->kind == Constant_kind) {
Victor Stinner15a30952016-02-08 22:45:06 +01003361 /* ignore constant statement */
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003362 return 1;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003363 }
3364
3365 VISIT(c, expr, value);
3366 ADDOP(c, POP_TOP);
3367 return 1;
3368}
3369
3370static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003371compiler_visit_stmt(struct compiler *c, stmt_ty s)
3372{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003373 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003375 /* Always assign a lineno to the next instruction for a stmt. */
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02003376 SET_LOC(c, s);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +00003377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003378 switch (s->kind) {
3379 case FunctionDef_kind:
Yury Selivanov75445082015-05-11 22:57:16 -04003380 return compiler_function(c, s, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003381 case ClassDef_kind:
3382 return compiler_class(c, s);
3383 case Return_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003384 return compiler_return(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003385 case Delete_kind:
3386 VISIT_SEQ(c, expr, s->v.Delete.targets)
3387 break;
3388 case Assign_kind:
3389 n = asdl_seq_LEN(s->v.Assign.targets);
3390 VISIT(c, expr, s->v.Assign.value);
3391 for (i = 0; i < n; i++) {
3392 if (i < n - 1)
3393 ADDOP(c, DUP_TOP);
3394 VISIT(c, expr,
3395 (expr_ty)asdl_seq_GET(s->v.Assign.targets, i));
3396 }
3397 break;
3398 case AugAssign_kind:
3399 return compiler_augassign(c, s);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07003400 case AnnAssign_kind:
3401 return compiler_annassign(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003402 case For_kind:
3403 return compiler_for(c, s);
3404 case While_kind:
3405 return compiler_while(c, s);
3406 case If_kind:
3407 return compiler_if(c, s);
3408 case Raise_kind:
3409 n = 0;
3410 if (s->v.Raise.exc) {
3411 VISIT(c, expr, s->v.Raise.exc);
3412 n++;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003413 if (s->v.Raise.cause) {
3414 VISIT(c, expr, s->v.Raise.cause);
3415 n++;
3416 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003417 }
Victor Stinnerad9a0662013-11-19 22:23:20 +01003418 ADDOP_I(c, RAISE_VARARGS, (int)n);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003419 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05003420 case Try_kind:
3421 return compiler_try(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003422 case Assert_kind:
3423 return compiler_assert(c, s);
3424 case Import_kind:
3425 return compiler_import(c, s);
3426 case ImportFrom_kind:
3427 return compiler_from_import(c, s);
3428 case Global_kind:
3429 case Nonlocal_kind:
3430 break;
3431 case Expr_kind:
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01003432 return compiler_visit_stmt_expr(c, s->v.Expr.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003433 case Pass_kind:
3434 break;
3435 case Break_kind:
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02003436 return compiler_break(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003437 case Continue_kind:
3438 return compiler_continue(c);
3439 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05003440 return compiler_with(c, s, 0);
Yury Selivanov75445082015-05-11 22:57:16 -04003441 case AsyncFunctionDef_kind:
3442 return compiler_function(c, s, 1);
3443 case AsyncWith_kind:
3444 return compiler_async_with(c, s, 0);
3445 case AsyncFor_kind:
3446 return compiler_async_for(c, s);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003447 }
Yury Selivanov75445082015-05-11 22:57:16 -04003448
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003449 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003450}
3451
3452static int
3453unaryop(unaryop_ty op)
3454{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003455 switch (op) {
3456 case Invert:
3457 return UNARY_INVERT;
3458 case Not:
3459 return UNARY_NOT;
3460 case UAdd:
3461 return UNARY_POSITIVE;
3462 case USub:
3463 return UNARY_NEGATIVE;
3464 default:
3465 PyErr_Format(PyExc_SystemError,
3466 "unary op %d should not be possible", op);
3467 return 0;
3468 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003469}
3470
3471static int
Andy Lester76d58772020-03-10 21:18:12 -05003472binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003473{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003474 switch (op) {
3475 case Add:
3476 return BINARY_ADD;
3477 case Sub:
3478 return BINARY_SUBTRACT;
3479 case Mult:
3480 return BINARY_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003481 case MatMult:
3482 return BINARY_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003483 case Div:
3484 return BINARY_TRUE_DIVIDE;
3485 case Mod:
3486 return BINARY_MODULO;
3487 case Pow:
3488 return BINARY_POWER;
3489 case LShift:
3490 return BINARY_LSHIFT;
3491 case RShift:
3492 return BINARY_RSHIFT;
3493 case BitOr:
3494 return BINARY_OR;
3495 case BitXor:
3496 return BINARY_XOR;
3497 case BitAnd:
3498 return BINARY_AND;
3499 case FloorDiv:
3500 return BINARY_FLOOR_DIVIDE;
3501 default:
3502 PyErr_Format(PyExc_SystemError,
3503 "binary op %d should not be possible", op);
3504 return 0;
3505 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003506}
3507
3508static int
Andy Lester76d58772020-03-10 21:18:12 -05003509inplace_binop(operator_ty op)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003511 switch (op) {
3512 case Add:
3513 return INPLACE_ADD;
3514 case Sub:
3515 return INPLACE_SUBTRACT;
3516 case Mult:
3517 return INPLACE_MULTIPLY;
Benjamin Petersond51374e2014-04-09 23:55:56 -04003518 case MatMult:
3519 return INPLACE_MATRIX_MULTIPLY;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003520 case Div:
3521 return INPLACE_TRUE_DIVIDE;
3522 case Mod:
3523 return INPLACE_MODULO;
3524 case Pow:
3525 return INPLACE_POWER;
3526 case LShift:
3527 return INPLACE_LSHIFT;
3528 case RShift:
3529 return INPLACE_RSHIFT;
3530 case BitOr:
3531 return INPLACE_OR;
3532 case BitXor:
3533 return INPLACE_XOR;
3534 case BitAnd:
3535 return INPLACE_AND;
3536 case FloorDiv:
3537 return INPLACE_FLOOR_DIVIDE;
3538 default:
3539 PyErr_Format(PyExc_SystemError,
3540 "inplace binary op %d should not be possible", op);
3541 return 0;
3542 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003543}
3544
3545static int
3546compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx)
3547{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003548 int op, scope;
3549 Py_ssize_t arg;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003550 enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003551
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003552 PyObject *dict = c->u->u_names;
3553 PyObject *mangled;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003554
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02003555 assert(!_PyUnicode_EqualToASCIIString(name, "None") &&
3556 !_PyUnicode_EqualToASCIIString(name, "True") &&
3557 !_PyUnicode_EqualToASCIIString(name, "False"));
Benjamin Peterson70b224d2012-12-06 17:49:58 -05003558
Pablo Galindoc5fc1562020-04-22 23:29:27 +01003559 if (forbidden_name(c, name, ctx))
3560 return 0;
3561
Serhiy Storchakabd6ec4d2017-12-18 14:29:12 +02003562 mangled = _Py_Mangle(c->u->u_private, name);
3563 if (!mangled)
3564 return 0;
3565
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003566 op = 0;
3567 optype = OP_NAME;
3568 scope = PyST_GetScope(c->u->u_ste, mangled);
3569 switch (scope) {
3570 case FREE:
3571 dict = c->u->u_freevars;
3572 optype = OP_DEREF;
3573 break;
3574 case CELL:
3575 dict = c->u->u_cellvars;
3576 optype = OP_DEREF;
3577 break;
3578 case LOCAL:
3579 if (c->u->u_ste->ste_type == FunctionBlock)
3580 optype = OP_FAST;
3581 break;
3582 case GLOBAL_IMPLICIT:
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04003583 if (c->u->u_ste->ste_type == FunctionBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003584 optype = OP_GLOBAL;
3585 break;
3586 case GLOBAL_EXPLICIT:
3587 optype = OP_GLOBAL;
3588 break;
3589 default:
3590 /* scope can be 0 */
3591 break;
3592 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003593
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003594 /* XXX Leave assert here, but handle __doc__ and the like better */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02003595 assert(scope || PyUnicode_READ_CHAR(name, 0) == '_');
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003597 switch (optype) {
3598 case OP_DEREF:
3599 switch (ctx) {
Benjamin Peterson3b0431d2013-04-30 09:41:40 -04003600 case Load:
3601 op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF;
3602 break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003603 case Store: op = STORE_DEREF; break;
Amaury Forgeot d'Arcba117ef2010-09-10 21:39:53 +00003604 case Del: op = DELETE_DEREF; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003605 }
3606 break;
3607 case OP_FAST:
3608 switch (ctx) {
3609 case Load: op = LOAD_FAST; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003610 case Store: op = STORE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003611 case Del: op = DELETE_FAST; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003612 }
Serhiy Storchakaaa8e51f2018-04-01 00:29:37 +03003613 ADDOP_N(c, op, mangled, varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003614 return 1;
3615 case OP_GLOBAL:
3616 switch (ctx) {
3617 case Load: op = LOAD_GLOBAL; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003618 case Store: op = STORE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003619 case Del: op = DELETE_GLOBAL; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003620 }
3621 break;
3622 case OP_NAME:
3623 switch (ctx) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +00003624 case Load: op = LOAD_NAME; break;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02003625 case Store: op = STORE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003626 case Del: op = DELETE_NAME; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003627 }
3628 break;
3629 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003630
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003631 assert(op);
Andy Lester76d58772020-03-10 21:18:12 -05003632 arg = compiler_add_o(dict, mangled);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003633 Py_DECREF(mangled);
3634 if (arg < 0)
3635 return 0;
3636 return compiler_addop_i(c, op, arg);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003637}
3638
3639static int
3640compiler_boolop(struct compiler *c, expr_ty e)
3641{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003642 basicblock *end;
Victor Stinnerad9a0662013-11-19 22:23:20 +01003643 int jumpi;
3644 Py_ssize_t i, n;
Pablo Galindoa5634c42020-09-16 19:42:00 +01003645 asdl_expr_seq *s;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003647 assert(e->kind == BoolOp_kind);
3648 if (e->v.BoolOp.op == And)
3649 jumpi = JUMP_IF_FALSE_OR_POP;
3650 else
3651 jumpi = JUMP_IF_TRUE_OR_POP;
3652 end = compiler_new_block(c);
3653 if (end == NULL)
3654 return 0;
3655 s = e->v.BoolOp.values;
3656 n = asdl_seq_LEN(s) - 1;
3657 assert(n >= 0);
3658 for (i = 0; i < n; ++i) {
3659 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003660 ADDOP_JUMP(c, jumpi, end);
Mark Shannon6e8128f2020-07-30 10:03:00 +01003661 basicblock *next = compiler_new_block(c);
3662 if (next == NULL) {
3663 return 0;
3664 }
3665 compiler_use_next_block(c, next);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003666 }
3667 VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n));
3668 compiler_use_next_block(c, end);
3669 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003670}
3671
3672static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003673starunpack_helper(struct compiler *c, asdl_expr_seq *elts, int pushed,
Mark Shannon13bc1392020-01-23 09:25:17 +00003674 int build, int add, int extend, int tuple)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003675{
3676 Py_ssize_t n = asdl_seq_LEN(elts);
Mark Shannon13bc1392020-01-23 09:25:17 +00003677 Py_ssize_t i, seen_star = 0;
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003678 if (n > 2 && are_all_items_const(elts, 0, n)) {
3679 PyObject *folded = PyTuple_New(n);
3680 if (folded == NULL) {
3681 return 0;
3682 }
3683 PyObject *val;
3684 for (i = 0; i < n; i++) {
3685 val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value;
3686 Py_INCREF(val);
3687 PyTuple_SET_ITEM(folded, i, val);
3688 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003689 if (tuple) {
3690 ADDOP_LOAD_CONST_NEW(c, folded);
3691 } else {
3692 if (add == SET_ADD) {
3693 Py_SETREF(folded, PyFrozenSet_New(folded));
3694 if (folded == NULL) {
3695 return 0;
3696 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003697 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003698 ADDOP_I(c, build, pushed);
3699 ADDOP_LOAD_CONST_NEW(c, folded);
3700 ADDOP_I(c, extend, 1);
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003701 }
Brandt Bucher6dd9b642019-11-25 22:16:53 -08003702 return 1;
3703 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003704
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003705 for (i = 0; i < n; i++) {
3706 expr_ty elt = asdl_seq_GET(elts, i);
3707 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003708 seen_star = 1;
3709 }
3710 }
3711 if (seen_star) {
3712 seen_star = 0;
3713 for (i = 0; i < n; i++) {
3714 expr_ty elt = asdl_seq_GET(elts, i);
3715 if (elt->kind == Starred_kind) {
3716 if (seen_star == 0) {
3717 ADDOP_I(c, build, i+pushed);
3718 seen_star = 1;
3719 }
3720 VISIT(c, expr, elt->v.Starred.value);
3721 ADDOP_I(c, extend, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003722 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003723 else {
3724 VISIT(c, expr, elt);
3725 if (seen_star) {
3726 ADDOP_I(c, add, 1);
3727 }
3728 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003729 }
Mark Shannon13bc1392020-01-23 09:25:17 +00003730 assert(seen_star);
3731 if (tuple) {
3732 ADDOP(c, LIST_TO_TUPLE);
3733 }
3734 }
3735 else {
3736 for (i = 0; i < n; i++) {
3737 expr_ty elt = asdl_seq_GET(elts, i);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003738 VISIT(c, expr, elt);
Mark Shannon13bc1392020-01-23 09:25:17 +00003739 }
3740 if (tuple) {
3741 ADDOP_I(c, BUILD_TUPLE, n+pushed);
3742 } else {
3743 ADDOP_I(c, build, n+pushed);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003744 }
3745 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003746 return 1;
3747}
3748
3749static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003750assignment_helper(struct compiler *c, asdl_expr_seq *elts)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003751{
3752 Py_ssize_t n = asdl_seq_LEN(elts);
3753 Py_ssize_t i;
3754 int seen_star = 0;
3755 for (i = 0; i < n; i++) {
3756 expr_ty elt = asdl_seq_GET(elts, i);
3757 if (elt->kind == Starred_kind && !seen_star) {
3758 if ((i >= (1 << 8)) ||
3759 (n-i-1 >= (INT_MAX >> 8)))
3760 return compiler_error(c,
3761 "too many expressions in "
3762 "star-unpacking assignment");
3763 ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8)));
3764 seen_star = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003765 }
3766 else if (elt->kind == Starred_kind) {
3767 return compiler_error(c,
Furkan Öndercb6534e2020-03-26 04:54:31 +03003768 "multiple starred expressions in assignment");
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003769 }
3770 }
3771 if (!seen_star) {
3772 ADDOP_I(c, UNPACK_SEQUENCE, n);
3773 }
Brandt Bucherd5aa2e92020-03-07 19:44:18 -08003774 for (i = 0; i < n; i++) {
3775 expr_ty elt = asdl_seq_GET(elts, i);
3776 VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value);
3777 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003778 return 1;
3779}
3780
3781static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003782compiler_list(struct compiler *c, expr_ty e)
3783{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003784 asdl_expr_seq *elts = e->v.List.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003785 if (e->v.List.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003786 return assignment_helper(c, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003787 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003788 else if (e->v.List.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003789 return starunpack_helper(c, elts, 0, BUILD_LIST,
3790 LIST_APPEND, LIST_EXTEND, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003791 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003792 else
3793 VISIT_SEQ(c, expr, elts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003794 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003795}
3796
3797static int
3798compiler_tuple(struct compiler *c, expr_ty e)
3799{
Pablo Galindoa5634c42020-09-16 19:42:00 +01003800 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchakad8b3a982019-03-05 20:42:06 +02003801 if (e->v.Tuple.ctx == Store) {
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003802 return assignment_helper(c, elts);
3803 }
3804 else if (e->v.Tuple.ctx == Load) {
Mark Shannon13bc1392020-01-23 09:25:17 +00003805 return starunpack_helper(c, elts, 0, BUILD_LIST,
3806 LIST_APPEND, LIST_EXTEND, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003807 }
3808 else
3809 VISIT_SEQ(c, expr, elts);
3810 return 1;
3811}
3812
3813static int
3814compiler_set(struct compiler *c, expr_ty e)
3815{
Mark Shannon13bc1392020-01-23 09:25:17 +00003816 return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET,
3817 SET_ADD, SET_UPDATE, 0);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003818}
3819
3820static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01003821are_all_items_const(asdl_expr_seq *seq, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003822{
3823 Py_ssize_t i;
3824 for (i = begin; i < end; i++) {
3825 expr_ty key = (expr_ty)asdl_seq_GET(seq, i);
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003826 if (key == NULL || key->kind != Constant_kind)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003827 return 0;
3828 }
3829 return 1;
3830}
3831
3832static int
3833compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end)
3834{
3835 Py_ssize_t i, n = end - begin;
3836 PyObject *keys, *key;
3837 if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) {
3838 for (i = begin; i < end; i++) {
3839 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3840 }
3841 keys = PyTuple_New(n);
3842 if (keys == NULL) {
3843 return 0;
3844 }
3845 for (i = begin; i < end; i++) {
Serhiy Storchaka3f228112018-09-27 17:42:37 +03003846 key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003847 Py_INCREF(key);
3848 PyTuple_SET_ITEM(keys, i - begin, key);
3849 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03003850 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003851 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
3852 }
3853 else {
3854 for (i = begin; i < end; i++) {
3855 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i));
3856 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
3857 }
3858 ADDOP_I(c, BUILD_MAP, n);
3859 }
3860 return 1;
3861}
3862
3863static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003864compiler_dict(struct compiler *c, expr_ty e)
3865{
Victor Stinner976bb402016-03-23 11:36:19 +01003866 Py_ssize_t i, n, elements;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003867 int have_dict;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003868 int is_unpacking = 0;
3869 n = asdl_seq_LEN(e->v.Dict.values);
Mark Shannon8a4cd702020-01-27 09:57:45 +00003870 have_dict = 0;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003871 elements = 0;
3872 for (i = 0; i < n; i++) {
3873 is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003874 if (is_unpacking) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003875 if (elements) {
3876 if (!compiler_subdict(c, e, i - elements, i)) {
3877 return 0;
3878 }
3879 if (have_dict) {
3880 ADDOP_I(c, DICT_UPDATE, 1);
3881 }
3882 have_dict = 1;
3883 elements = 0;
3884 }
3885 if (have_dict == 0) {
3886 ADDOP_I(c, BUILD_MAP, 0);
3887 have_dict = 1;
3888 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003889 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i));
Mark Shannon8a4cd702020-01-27 09:57:45 +00003890 ADDOP_I(c, DICT_UPDATE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003891 }
3892 else {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003893 if (elements == 0xFFFF) {
Pablo Galindoc51db0e2020-08-13 09:48:41 +01003894 if (!compiler_subdict(c, e, i - elements, i + 1)) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00003895 return 0;
3896 }
3897 if (have_dict) {
3898 ADDOP_I(c, DICT_UPDATE, 1);
3899 }
3900 have_dict = 1;
3901 elements = 0;
3902 }
3903 else {
3904 elements++;
3905 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003906 }
3907 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003908 if (elements) {
3909 if (!compiler_subdict(c, e, n - elements, n)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03003910 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00003911 }
3912 if (have_dict) {
3913 ADDOP_I(c, DICT_UPDATE, 1);
3914 }
3915 have_dict = 1;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04003916 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00003917 if (!have_dict) {
3918 ADDOP_I(c, BUILD_MAP, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003919 }
3920 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003921}
3922
3923static int
3924compiler_compare(struct compiler *c, expr_ty e)
3925{
Victor Stinnerad9a0662013-11-19 22:23:20 +01003926 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003927
Serhiy Storchaka3bcbedc2019-01-18 07:47:48 +02003928 if (!check_compare(c, e)) {
3929 return 0;
3930 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003931 VISIT(c, expr, e->v.Compare.left);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003932 assert(asdl_seq_LEN(e->v.Compare.ops) > 0);
3933 n = asdl_seq_LEN(e->v.Compare.ops) - 1;
3934 if (n == 0) {
3935 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0));
Mark Shannon9af0e472020-01-14 10:12:45 +00003936 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003937 }
3938 else {
3939 basicblock *cleanup = compiler_new_block(c);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003940 if (cleanup == NULL)
3941 return 0;
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003942 for (i = 0; i < n; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003943 VISIT(c, expr,
3944 (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i));
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003945 ADDOP(c, DUP_TOP);
3946 ADDOP(c, ROT_THREE);
Mark Shannon9af0e472020-01-14 10:12:45 +00003947 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i));
Mark Shannon582aaf12020-08-04 17:30:11 +01003948 ADDOP_JUMP(c, JUMP_IF_FALSE_OR_POP, cleanup);
Serhiy Storchaka02b9ef22017-12-30 09:47:42 +02003949 NEXT_BLOCK(c);
3950 }
3951 VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n));
Mark Shannon9af0e472020-01-14 10:12:45 +00003952 ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003953 basicblock *end = compiler_new_block(c);
3954 if (end == NULL)
3955 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01003956 ADDOP_JUMP(c, JUMP_FORWARD, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00003957 compiler_use_next_block(c, cleanup);
3958 ADDOP(c, ROT_TWO);
3959 ADDOP(c, POP_TOP);
3960 compiler_use_next_block(c, end);
3961 }
3962 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00003963}
3964
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003965static PyTypeObject *
3966infer_type(expr_ty e)
3967{
3968 switch (e->kind) {
3969 case Tuple_kind:
3970 return &PyTuple_Type;
3971 case List_kind:
3972 case ListComp_kind:
3973 return &PyList_Type;
3974 case Dict_kind:
3975 case DictComp_kind:
3976 return &PyDict_Type;
3977 case Set_kind:
3978 case SetComp_kind:
3979 return &PySet_Type;
3980 case GeneratorExp_kind:
3981 return &PyGen_Type;
3982 case Lambda_kind:
3983 return &PyFunction_Type;
3984 case JoinedStr_kind:
3985 case FormattedValue_kind:
3986 return &PyUnicode_Type;
3987 case Constant_kind:
Victor Stinnera102ed72020-02-07 02:24:48 +01003988 return Py_TYPE(e->v.Constant.value);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02003989 default:
3990 return NULL;
3991 }
3992}
3993
3994static int
3995check_caller(struct compiler *c, expr_ty e)
3996{
3997 switch (e->kind) {
3998 case Constant_kind:
3999 case Tuple_kind:
4000 case List_kind:
4001 case ListComp_kind:
4002 case Dict_kind:
4003 case DictComp_kind:
4004 case Set_kind:
4005 case SetComp_kind:
4006 case GeneratorExp_kind:
4007 case JoinedStr_kind:
4008 case FormattedValue_kind:
4009 return compiler_warn(c, "'%.200s' object is not callable; "
4010 "perhaps you missed a comma?",
4011 infer_type(e)->tp_name);
4012 default:
4013 return 1;
4014 }
4015}
4016
4017static int
4018check_subscripter(struct compiler *c, expr_ty e)
4019{
4020 PyObject *v;
4021
4022 switch (e->kind) {
4023 case Constant_kind:
4024 v = e->v.Constant.value;
4025 if (!(v == Py_None || v == Py_Ellipsis ||
4026 PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) ||
4027 PyAnySet_Check(v)))
4028 {
4029 return 1;
4030 }
4031 /* fall through */
4032 case Set_kind:
4033 case SetComp_kind:
4034 case GeneratorExp_kind:
4035 case Lambda_kind:
4036 return compiler_warn(c, "'%.200s' object is not subscriptable; "
4037 "perhaps you missed a comma?",
4038 infer_type(e)->tp_name);
4039 default:
4040 return 1;
4041 }
4042}
4043
4044static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004045check_index(struct compiler *c, expr_ty e, expr_ty s)
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004046{
4047 PyObject *v;
4048
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02004049 PyTypeObject *index_type = infer_type(s);
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004050 if (index_type == NULL
4051 || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS)
4052 || index_type == &PySlice_Type) {
4053 return 1;
4054 }
4055
4056 switch (e->kind) {
4057 case Constant_kind:
4058 v = e->v.Constant.value;
4059 if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) {
4060 return 1;
4061 }
4062 /* fall through */
4063 case Tuple_kind:
4064 case List_kind:
4065 case ListComp_kind:
4066 case JoinedStr_kind:
4067 case FormattedValue_kind:
4068 return compiler_warn(c, "%.200s indices must be integers or slices, "
4069 "not %.200s; "
4070 "perhaps you missed a comma?",
4071 infer_type(e)->tp_name,
4072 index_type->tp_name);
4073 default:
4074 return 1;
4075 }
4076}
4077
Zackery Spytz97f5de02019-03-22 01:30:32 -06004078// Return 1 if the method call was optimized, -1 if not, and 0 on error.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004079static int
Yury Selivanovf2392132016-12-13 19:03:51 -05004080maybe_optimize_method_call(struct compiler *c, expr_ty e)
4081{
4082 Py_ssize_t argsl, i;
4083 expr_ty meth = e->v.Call.func;
Pablo Galindoa5634c42020-09-16 19:42:00 +01004084 asdl_expr_seq *args = e->v.Call.args;
Yury Selivanovf2392132016-12-13 19:03:51 -05004085
4086 /* Check that the call node is an attribute access, and that
4087 the call doesn't have keyword parameters. */
4088 if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load ||
4089 asdl_seq_LEN(e->v.Call.keywords))
4090 return -1;
4091
4092 /* Check that there are no *varargs types of arguments. */
4093 argsl = asdl_seq_LEN(args);
4094 for (i = 0; i < argsl; i++) {
4095 expr_ty elt = asdl_seq_GET(args, i);
4096 if (elt->kind == Starred_kind) {
4097 return -1;
4098 }
4099 }
4100
4101 /* Alright, we can optimize the code. */
4102 VISIT(c, expr, meth->v.Attribute.value);
4103 ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names);
4104 VISIT_SEQ(c, expr, e->v.Call.args);
4105 ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args));
4106 return 1;
4107}
4108
4109static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004110validate_keywords(struct compiler *c, asdl_keyword_seq *keywords)
Zackery Spytz08050e92020-04-06 00:47:47 -06004111{
4112 Py_ssize_t nkeywords = asdl_seq_LEN(keywords);
4113 for (Py_ssize_t i = 0; i < nkeywords; i++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004114 keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i));
4115 if (key->arg == NULL) {
4116 continue;
4117 }
Pablo Galindoc5fc1562020-04-22 23:29:27 +01004118 if (forbidden_name(c, key->arg, Store)) {
4119 return -1;
4120 }
Zackery Spytz08050e92020-04-06 00:47:47 -06004121 for (Py_ssize_t j = i + 1; j < nkeywords; j++) {
Pablo Galindo254ec782020-04-03 20:37:13 +01004122 keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j));
4123 if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) {
4124 PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg);
4125 if (msg == NULL) {
4126 return -1;
4127 }
4128 c->u->u_col_offset = other->col_offset;
4129 compiler_error(c, PyUnicode_AsUTF8(msg));
4130 Py_DECREF(msg);
4131 return -1;
4132 }
4133 }
4134 }
4135 return 0;
4136}
4137
4138static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004139compiler_call(struct compiler *c, expr_ty e)
4140{
Zackery Spytz97f5de02019-03-22 01:30:32 -06004141 int ret = maybe_optimize_method_call(c, e);
4142 if (ret >= 0) {
4143 return ret;
4144 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02004145 if (!check_caller(c, e->v.Call.func)) {
4146 return 0;
4147 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004148 VISIT(c, expr, e->v.Call.func);
4149 return compiler_call_helper(c, 0,
4150 e->v.Call.args,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004151 e->v.Call.keywords);
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004152}
4153
Eric V. Smith235a6f02015-09-19 14:51:32 -04004154static int
4155compiler_joined_str(struct compiler *c, expr_ty e)
4156{
Eric V. Smith235a6f02015-09-19 14:51:32 -04004157 VISIT_SEQ(c, expr, e->v.JoinedStr.values);
Serhiy Storchaka4cc30ae2016-12-11 19:37:19 +02004158 if (asdl_seq_LEN(e->v.JoinedStr.values) != 1)
4159 ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values));
Eric V. Smith235a6f02015-09-19 14:51:32 -04004160 return 1;
4161}
4162
Eric V. Smitha78c7952015-11-03 12:45:05 -05004163/* Used to implement f-strings. Format a single value. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004164static int
4165compiler_formatted_value(struct compiler *c, expr_ty e)
4166{
Eric V. Smitha78c7952015-11-03 12:45:05 -05004167 /* Our oparg encodes 2 pieces of information: the conversion
4168 character, and whether or not a format_spec was provided.
Eric V. Smith235a6f02015-09-19 14:51:32 -04004169
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004170 Convert the conversion char to 3 bits:
4171 : 000 0x0 FVC_NONE The default if nothing specified.
Eric V. Smitha78c7952015-11-03 12:45:05 -05004172 !s : 001 0x1 FVC_STR
4173 !r : 010 0x2 FVC_REPR
4174 !a : 011 0x3 FVC_ASCII
Eric V. Smith235a6f02015-09-19 14:51:32 -04004175
Eric V. Smitha78c7952015-11-03 12:45:05 -05004176 next bit is whether or not we have a format spec:
4177 yes : 100 0x4
4178 no : 000 0x0
4179 */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004180
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004181 int conversion = e->v.FormattedValue.conversion;
Eric V. Smitha78c7952015-11-03 12:45:05 -05004182 int oparg;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004183
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004184 /* The expression to be formatted. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004185 VISIT(c, expr, e->v.FormattedValue.value);
4186
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004187 switch (conversion) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004188 case 's': oparg = FVC_STR; break;
4189 case 'r': oparg = FVC_REPR; break;
4190 case 'a': oparg = FVC_ASCII; break;
4191 case -1: oparg = FVC_NONE; break;
4192 default:
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004193 PyErr_Format(PyExc_SystemError,
4194 "Unrecognized conversion character %d", conversion);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004195 return 0;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004196 }
Eric V. Smith235a6f02015-09-19 14:51:32 -04004197 if (e->v.FormattedValue.format_spec) {
Eric V. Smitha78c7952015-11-03 12:45:05 -05004198 /* Evaluate the format spec, and update our opcode arg. */
Eric V. Smith235a6f02015-09-19 14:51:32 -04004199 VISIT(c, expr, e->v.FormattedValue.format_spec);
Eric V. Smitha78c7952015-11-03 12:45:05 -05004200 oparg |= FVS_HAVE_SPEC;
Eric V. Smith235a6f02015-09-19 14:51:32 -04004201 }
4202
Eric V. Smitha78c7952015-11-03 12:45:05 -05004203 /* And push our opcode and oparg */
4204 ADDOP_I(c, FORMAT_VALUE, oparg);
Eric V. Smith9a4135e2019-05-08 16:28:48 -04004205
Eric V. Smith235a6f02015-09-19 14:51:32 -04004206 return 1;
4207}
4208
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004209static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01004210compiler_subkwargs(struct compiler *c, asdl_keyword_seq *keywords, Py_ssize_t begin, Py_ssize_t end)
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004211{
4212 Py_ssize_t i, n = end - begin;
4213 keyword_ty kw;
4214 PyObject *keys, *key;
4215 assert(n > 0);
4216 if (n > 1) {
4217 for (i = begin; i < end; i++) {
4218 kw = asdl_seq_GET(keywords, i);
4219 VISIT(c, expr, kw->value);
4220 }
4221 keys = PyTuple_New(n);
4222 if (keys == NULL) {
4223 return 0;
4224 }
4225 for (i = begin; i < end; i++) {
4226 key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg;
4227 Py_INCREF(key);
4228 PyTuple_SET_ITEM(keys, i - begin, key);
4229 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004230 ADDOP_LOAD_CONST_NEW(c, keys);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004231 ADDOP_I(c, BUILD_CONST_KEY_MAP, n);
4232 }
4233 else {
4234 /* a for loop only executes once */
4235 for (i = begin; i < end; i++) {
4236 kw = asdl_seq_GET(keywords, i);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004237 ADDOP_LOAD_CONST(c, kw->arg);
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004238 VISIT(c, expr, kw->value);
4239 }
4240 ADDOP_I(c, BUILD_MAP, n);
4241 }
4242 return 1;
4243}
4244
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004245/* shared code between compiler_call and compiler_class */
4246static int
4247compiler_call_helper(struct compiler *c,
Victor Stinner976bb402016-03-23 11:36:19 +01004248 int n, /* Args already pushed */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004249 asdl_expr_seq *args,
4250 asdl_keyword_seq *keywords)
Guido van Rossum52cc1d82007-03-18 15:41:51 +00004251{
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004252 Py_ssize_t i, nseen, nelts, nkwelts;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004253
Pablo Galindo254ec782020-04-03 20:37:13 +01004254 if (validate_keywords(c, keywords) == -1) {
4255 return 0;
4256 }
4257
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004258 nelts = asdl_seq_LEN(args);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004259 nkwelts = asdl_seq_LEN(keywords);
4260
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004261 for (i = 0; i < nelts; i++) {
4262 expr_ty elt = asdl_seq_GET(args, i);
4263 if (elt->kind == Starred_kind) {
Mark Shannon13bc1392020-01-23 09:25:17 +00004264 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004265 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004266 }
4267 for (i = 0; i < nkwelts; i++) {
4268 keyword_ty kw = asdl_seq_GET(keywords, i);
4269 if (kw->arg == NULL) {
4270 goto ex_call;
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004271 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004272 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004273
Mark Shannon13bc1392020-01-23 09:25:17 +00004274 /* No * or ** args, so can use faster calling sequence */
4275 for (i = 0; i < nelts; i++) {
4276 expr_ty elt = asdl_seq_GET(args, i);
4277 assert(elt->kind != Starred_kind);
4278 VISIT(c, expr, elt);
4279 }
4280 if (nkwelts) {
4281 PyObject *names;
4282 VISIT_SEQ(c, keyword, keywords);
4283 names = PyTuple_New(nkwelts);
4284 if (names == NULL) {
4285 return 0;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004286 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004287 for (i = 0; i < nkwelts; i++) {
4288 keyword_ty kw = asdl_seq_GET(keywords, i);
4289 Py_INCREF(kw->arg);
4290 PyTuple_SET_ITEM(names, i, kw->arg);
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004291 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004292 ADDOP_LOAD_CONST_NEW(c, names);
4293 ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts);
4294 return 1;
4295 }
4296 else {
4297 ADDOP_I(c, CALL_FUNCTION, n + nelts);
4298 return 1;
4299 }
4300
4301ex_call:
4302
4303 /* Do positional arguments. */
4304 if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) {
4305 VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value);
4306 }
4307 else if (starunpack_helper(c, args, n, BUILD_LIST,
4308 LIST_APPEND, LIST_EXTEND, 1) == 0) {
4309 return 0;
4310 }
4311 /* Then keyword arguments */
4312 if (nkwelts) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004313 /* Has a new dict been pushed */
4314 int have_dict = 0;
Mark Shannon13bc1392020-01-23 09:25:17 +00004315
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004316 nseen = 0; /* the number of keyword arguments on the stack following */
4317 for (i = 0; i < nkwelts; i++) {
4318 keyword_ty kw = asdl_seq_GET(keywords, i);
4319 if (kw->arg == NULL) {
4320 /* A keyword argument unpacking. */
4321 if (nseen) {
Mark Shannon8a4cd702020-01-27 09:57:45 +00004322 if (!compiler_subkwargs(c, keywords, i - nseen, i)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004323 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004324 }
Mark Shannondb64f122020-06-01 10:42:42 +01004325 if (have_dict) {
4326 ADDOP_I(c, DICT_MERGE, 1);
4327 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004328 have_dict = 1;
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004329 nseen = 0;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004330 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004331 if (!have_dict) {
4332 ADDOP_I(c, BUILD_MAP, 0);
4333 have_dict = 1;
4334 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004335 VISIT(c, expr, kw->value);
Mark Shannon8a4cd702020-01-27 09:57:45 +00004336 ADDOP_I(c, DICT_MERGE, 1);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004337 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004338 else {
4339 nseen++;
4340 }
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004341 }
Victor Stinnerf9b760f2016-09-09 10:17:08 -07004342 if (nseen) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004343 /* Pack up any trailing keyword arguments. */
Mark Shannon8a4cd702020-01-27 09:57:45 +00004344 if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) {
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004345 return 0;
Mark Shannon8a4cd702020-01-27 09:57:45 +00004346 }
4347 if (have_dict) {
4348 ADDOP_I(c, DICT_MERGE, 1);
4349 }
4350 have_dict = 1;
Serhiy Storchaka6a7506a2016-06-12 00:39:41 +03004351 }
Mark Shannon8a4cd702020-01-27 09:57:45 +00004352 assert(have_dict);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004353 }
Mark Shannon13bc1392020-01-23 09:25:17 +00004354 ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0);
4355 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004356}
4357
Nick Coghlan650f0d02007-04-15 12:05:43 +00004358
4359/* List and set comprehensions and generator expressions work by creating a
4360 nested function to perform the actual iteration. This means that the
4361 iteration variables don't leak into the current scope.
4362 The defined function is called immediately following its definition, with the
4363 result of that call being the result of the expression.
4364 The LC/SC version returns the populated container, while the GE version is
4365 flagged in symtable.c as a generator, so it returns the generator object
4366 when the function is called.
Nick Coghlan650f0d02007-04-15 12:05:43 +00004367
4368 Possible cleanups:
4369 - iterate over the generator sequence instead of using recursion
4370*/
4371
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004372
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004373static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004374compiler_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004375 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004376 int depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004377 expr_ty elt, expr_ty val, int type)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004378{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004379 comprehension_ty gen;
4380 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4381 if (gen->is_async) {
4382 return compiler_async_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004383 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004384 } else {
4385 return compiler_sync_comprehension_generator(
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004386 c, generators, gen_index, depth, elt, val, type);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004387 }
4388}
4389
4390static int
4391compiler_sync_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004392 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004393 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004394 expr_ty elt, expr_ty val, int type)
4395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004396 /* generate code for the iterator, then each of the ifs,
4397 and then write to the element */
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004399 comprehension_ty gen;
4400 basicblock *start, *anchor, *skip, *if_cleanup;
Victor Stinnerad9a0662013-11-19 22:23:20 +01004401 Py_ssize_t i, n;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004402
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004403 start = compiler_new_block(c);
4404 skip = compiler_new_block(c);
4405 if_cleanup = compiler_new_block(c);
4406 anchor = compiler_new_block(c);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004407
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004408 if (start == NULL || skip == NULL || if_cleanup == NULL ||
4409 anchor == NULL)
4410 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004411
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004412 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004413
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004414 if (gen_index == 0) {
4415 /* Receive outermost iter as an implicit argument */
4416 c->u->u_argcount = 1;
4417 ADDOP_I(c, LOAD_FAST, 0);
4418 }
4419 else {
4420 /* Sub-iter - calculate on the fly */
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004421 /* Fast path for the temporary variable assignment idiom:
4422 for y in [f(x)]
4423 */
Pablo Galindoa5634c42020-09-16 19:42:00 +01004424 asdl_expr_seq *elts;
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004425 switch (gen->iter->kind) {
4426 case List_kind:
4427 elts = gen->iter->v.List.elts;
4428 break;
4429 case Tuple_kind:
4430 elts = gen->iter->v.Tuple.elts;
4431 break;
4432 default:
4433 elts = NULL;
4434 }
4435 if (asdl_seq_LEN(elts) == 1) {
4436 expr_ty elt = asdl_seq_GET(elts, 0);
4437 if (elt->kind != Starred_kind) {
4438 VISIT(c, expr, elt);
4439 start = NULL;
4440 }
4441 }
4442 if (start) {
4443 VISIT(c, expr, gen->iter);
4444 ADDOP(c, GET_ITER);
4445 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004446 }
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004447 if (start) {
4448 depth++;
4449 compiler_use_next_block(c, start);
Mark Shannon582aaf12020-08-04 17:30:11 +01004450 ADDOP_JUMP(c, FOR_ITER, anchor);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004451 NEXT_BLOCK(c);
4452 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004453 VISIT(c, expr, gen->target);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004454
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004455 /* XXX this needs to be cleaned up...a lot! */
4456 n = asdl_seq_LEN(gen->ifs);
4457 for (i = 0; i < n; i++) {
4458 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004459 if (!compiler_jump_if(c, e, if_cleanup, 0))
4460 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004461 NEXT_BLOCK(c);
4462 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004463
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004464 if (++gen_index < asdl_seq_LEN(generators))
4465 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004466 generators, gen_index, depth,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004467 elt, val, type))
4468 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004470 /* only append after the last for generator */
4471 if (gen_index >= asdl_seq_LEN(generators)) {
4472 /* comprehension specific code */
4473 switch (type) {
4474 case COMP_GENEXP:
4475 VISIT(c, expr, elt);
4476 ADDOP(c, YIELD_VALUE);
4477 ADDOP(c, POP_TOP);
4478 break;
4479 case COMP_LISTCOMP:
4480 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004481 ADDOP_I(c, LIST_APPEND, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004482 break;
4483 case COMP_SETCOMP:
4484 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004485 ADDOP_I(c, SET_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004486 break;
4487 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004488 /* With '{k: v}', k is evaluated before v, so we do
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004489 the same. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004490 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004491 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004492 ADDOP_I(c, MAP_ADD, depth + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004493 break;
4494 default:
4495 return 0;
4496 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004498 compiler_use_next_block(c, skip);
4499 }
4500 compiler_use_next_block(c, if_cleanup);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004501 if (start) {
Mark Shannon582aaf12020-08-04 17:30:11 +01004502 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004503 compiler_use_next_block(c, anchor);
4504 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004505
4506 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004507}
4508
4509static int
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004510compiler_async_comprehension_generator(struct compiler *c,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004511 asdl_comprehension_seq *generators, int gen_index,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004512 int depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004513 expr_ty elt, expr_ty val, int type)
4514{
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004515 comprehension_ty gen;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004516 basicblock *start, *if_cleanup, *except;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004517 Py_ssize_t i, n;
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004518 start = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004519 except = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004520 if_cleanup = compiler_new_block(c);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004521
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004522 if (start == NULL || if_cleanup == NULL || except == NULL) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004523 return 0;
4524 }
4525
4526 gen = (comprehension_ty)asdl_seq_GET(generators, gen_index);
4527
4528 if (gen_index == 0) {
4529 /* Receive outermost iter as an implicit argument */
4530 c->u->u_argcount = 1;
4531 ADDOP_I(c, LOAD_FAST, 0);
4532 }
4533 else {
4534 /* Sub-iter - calculate on the fly */
4535 VISIT(c, expr, gen->iter);
4536 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004537 }
4538
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004539 compiler_use_next_block(c, start);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004540
Mark Shannon582aaf12020-08-04 17:30:11 +01004541 ADDOP_JUMP(c, SETUP_FINALLY, except);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004542 ADDOP(c, GET_ANEXT);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004543 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004544 ADDOP(c, YIELD_FROM);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004545 ADDOP(c, POP_BLOCK);
Serhiy Storchaka24d32012018-03-10 18:22:34 +02004546 VISIT(c, expr, gen->target);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004547
4548 n = asdl_seq_LEN(gen->ifs);
4549 for (i = 0; i < n; i++) {
4550 expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i);
Serhiy Storchaka36ff4512017-06-11 14:50:22 +03004551 if (!compiler_jump_if(c, e, if_cleanup, 0))
4552 return 0;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004553 NEXT_BLOCK(c);
4554 }
4555
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004556 depth++;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004557 if (++gen_index < asdl_seq_LEN(generators))
4558 if (!compiler_comprehension_generator(c,
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004559 generators, gen_index, depth,
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004560 elt, val, type))
4561 return 0;
4562
4563 /* only append after the last for generator */
4564 if (gen_index >= asdl_seq_LEN(generators)) {
4565 /* comprehension specific code */
4566 switch (type) {
4567 case COMP_GENEXP:
4568 VISIT(c, expr, elt);
4569 ADDOP(c, YIELD_VALUE);
4570 ADDOP(c, POP_TOP);
4571 break;
4572 case COMP_LISTCOMP:
4573 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004574 ADDOP_I(c, LIST_APPEND, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004575 break;
4576 case COMP_SETCOMP:
4577 VISIT(c, expr, elt);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004578 ADDOP_I(c, SET_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004579 break;
4580 case COMP_DICTCOMP:
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004581 /* With '{k: v}', k is evaluated before v, so we do
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004582 the same. */
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004583 VISIT(c, expr, elt);
Jörn Heisslerc8a35412019-06-22 16:40:55 +02004584 VISIT(c, expr, val);
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004585 ADDOP_I(c, MAP_ADD, depth + 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004586 break;
4587 default:
4588 return 0;
4589 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004590 }
4591 compiler_use_next_block(c, if_cleanup);
Mark Shannon582aaf12020-08-04 17:30:11 +01004592 ADDOP_JUMP(c, JUMP_ABSOLUTE, start);
Serhiy Storchaka702f8f32018-03-23 14:34:35 +02004593
4594 compiler_use_next_block(c, except);
4595 ADDOP(c, END_ASYNC_FOR);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004596
4597 return 1;
4598}
4599
4600static int
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004601compiler_comprehension(struct compiler *c, expr_ty e, int type,
Pablo Galindoa5634c42020-09-16 19:42:00 +01004602 identifier name, asdl_comprehension_seq *generators, expr_ty elt,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04004603 expr_ty val)
Nick Coghlan650f0d02007-04-15 12:05:43 +00004604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004605 PyCodeObject *co = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004606 comprehension_ty outermost;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004607 PyObject *qualname = NULL;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004608 int is_async_generator = 0;
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004609 int top_level_await = IS_TOP_LEVEL_AWAIT(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004610
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004611
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004612 int is_async_function = c->u->u_ste->ste_coroutine;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004613
Batuhan TaÅŸkaya9052f7a2020-03-19 14:35:44 +03004614 outermost = (comprehension_ty) asdl_seq_GET(generators, 0);
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004615 if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION,
4616 (void *)e, e->lineno))
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004617 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004618 goto error;
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004619 }
4620
4621 is_async_generator = c->u->u_ste->ste_coroutine;
4622
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004623 if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) {
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004624 compiler_error(c, "asynchronous comprehension outside of "
4625 "an asynchronous function");
4626 goto error_in_scope;
4627 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004628
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004629 if (type != COMP_GENEXP) {
4630 int op;
4631 switch (type) {
4632 case COMP_LISTCOMP:
4633 op = BUILD_LIST;
4634 break;
4635 case COMP_SETCOMP:
4636 op = BUILD_SET;
4637 break;
4638 case COMP_DICTCOMP:
4639 op = BUILD_MAP;
4640 break;
4641 default:
4642 PyErr_Format(PyExc_SystemError,
4643 "unknown comprehension type %d", type);
4644 goto error_in_scope;
4645 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004646
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004647 ADDOP_I(c, op, 0);
4648 }
Nick Coghlan650f0d02007-04-15 12:05:43 +00004649
Serhiy Storchaka8c579b12020-02-12 12:18:59 +02004650 if (!compiler_comprehension_generator(c, generators, 0, 0, elt,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004651 val, type))
4652 goto error_in_scope;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004654 if (type != COMP_GENEXP) {
4655 ADDOP(c, RETURN_VALUE);
4656 }
4657
4658 co = assemble(c, 1);
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004659 qualname = c->u->u_qualname;
4660 Py_INCREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004661 compiler_exit_scope(c);
Matthias Bussonnierbd461742020-07-06 14:26:52 -07004662 if (top_level_await && is_async_generator){
4663 c->u->u_ste->ste_coroutine = 1;
4664 }
Benjamin Peterson6b4f7802013-10-20 17:50:28 -04004665 if (co == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004666 goto error;
4667
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004668 if (!compiler_make_closure(c, co, 0, qualname))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004669 goto error;
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004670 Py_DECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004671 Py_DECREF(co);
4672
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004673 VISIT(c, expr, outermost->iter);
4674
4675 if (outermost->is_async) {
4676 ADDOP(c, GET_AITER);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004677 } else {
4678 ADDOP(c, GET_ITER);
4679 }
4680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004681 ADDOP_I(c, CALL_FUNCTION, 1);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004682
4683 if (is_async_generator && type != COMP_GENEXP) {
4684 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004685 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07004686 ADDOP(c, YIELD_FROM);
4687 }
4688
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004689 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004690error_in_scope:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004691 compiler_exit_scope(c);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004692error:
Antoine Pitrou86a36b52011-11-25 18:56:07 +01004693 Py_XDECREF(qualname);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004694 Py_XDECREF(co);
4695 return 0;
Nick Coghlan650f0d02007-04-15 12:05:43 +00004696}
4697
4698static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004699compiler_genexp(struct compiler *c, expr_ty e)
4700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004701 static identifier name;
4702 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004703 name = PyUnicode_InternFromString("<genexpr>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004704 if (!name)
4705 return 0;
4706 }
4707 assert(e->kind == GeneratorExp_kind);
4708 return compiler_comprehension(c, e, COMP_GENEXP, name,
4709 e->v.GeneratorExp.generators,
4710 e->v.GeneratorExp.elt, NULL);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004711}
4712
4713static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00004714compiler_listcomp(struct compiler *c, expr_ty e)
4715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004716 static identifier name;
4717 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004718 name = PyUnicode_InternFromString("<listcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004719 if (!name)
4720 return 0;
4721 }
4722 assert(e->kind == ListComp_kind);
4723 return compiler_comprehension(c, e, COMP_LISTCOMP, name,
4724 e->v.ListComp.generators,
4725 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004726}
4727
4728static int
4729compiler_setcomp(struct compiler *c, expr_ty e)
4730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004731 static identifier name;
4732 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004733 name = PyUnicode_InternFromString("<setcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004734 if (!name)
4735 return 0;
4736 }
4737 assert(e->kind == SetComp_kind);
4738 return compiler_comprehension(c, e, COMP_SETCOMP, name,
4739 e->v.SetComp.generators,
4740 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00004741}
4742
4743
4744static int
4745compiler_dictcomp(struct compiler *c, expr_ty e)
4746{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004747 static identifier name;
4748 if (!name) {
Zackery Spytzf3036392018-04-15 16:12:29 -06004749 name = PyUnicode_InternFromString("<dictcomp>");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004750 if (!name)
4751 return 0;
4752 }
4753 assert(e->kind == DictComp_kind);
4754 return compiler_comprehension(c, e, COMP_DICTCOMP, name,
4755 e->v.DictComp.generators,
4756 e->v.DictComp.key, e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00004757}
4758
4759
4760static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004761compiler_visit_keyword(struct compiler *c, keyword_ty k)
4762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004763 VISIT(c, expr, k->value);
4764 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004765}
4766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004767/* Test whether expression is constant. For constants, report
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004768 whether they are true or false.
4769
4770 Return values: 1 for true, 0 for false, -1 for non-constant.
4771 */
4772
4773static int
Serhiy Storchaka3dfbaf52017-12-25 12:47:50 +02004774expr_constant(expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004775{
Serhiy Storchaka3f228112018-09-27 17:42:37 +03004776 if (e->kind == Constant_kind) {
4777 return PyObject_IsTrue(e->v.Constant.value);
Benjamin Peterson442f2092012-12-06 17:41:04 -05004778 }
Serhiy Storchaka3325a672017-12-15 12:35:48 +02004779 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004780}
4781
Mark Shannonfee55262019-11-21 09:11:43 +00004782static int
4783compiler_with_except_finish(struct compiler *c) {
4784 basicblock *exit;
4785 exit = compiler_new_block(c);
4786 if (exit == NULL)
4787 return 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01004788 ADDOP_JUMP(c, POP_JUMP_IF_TRUE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004789 ADDOP(c, RERAISE);
4790 compiler_use_next_block(c, exit);
4791 ADDOP(c, POP_TOP);
4792 ADDOP(c, POP_TOP);
4793 ADDOP(c, POP_TOP);
4794 ADDOP(c, POP_EXCEPT);
4795 ADDOP(c, POP_TOP);
4796 return 1;
4797}
Yury Selivanov75445082015-05-11 22:57:16 -04004798
4799/*
4800 Implements the async with statement.
4801
4802 The semantics outlined in that PEP are as follows:
4803
4804 async with EXPR as VAR:
4805 BLOCK
4806
4807 It is implemented roughly as:
4808
4809 context = EXPR
4810 exit = context.__aexit__ # not calling it
4811 value = await context.__aenter__()
4812 try:
4813 VAR = value # if VAR present in the syntax
4814 BLOCK
4815 finally:
4816 if an exception was raised:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004817 exc = copy of (exception, instance, traceback)
Yury Selivanov75445082015-05-11 22:57:16 -04004818 else:
Serhiy Storchakaec466a12015-06-11 00:09:32 +03004819 exc = (None, None, None)
Yury Selivanov75445082015-05-11 22:57:16 -04004820 if not (await exit(*exc)):
4821 raise
4822 */
4823static int
4824compiler_async_with(struct compiler *c, stmt_ty s, int pos)
4825{
Mark Shannonfee55262019-11-21 09:11:43 +00004826 basicblock *block, *final, *exit;
Yury Selivanov75445082015-05-11 22:57:16 -04004827 withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos);
4828
4829 assert(s->kind == AsyncWith_kind);
Pablo Galindo90235812020-03-15 04:29:22 +00004830 if (IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07004831 c->u->u_ste->ste_coroutine = 1;
4832 } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){
Zsolt Dollensteine2396502018-04-27 08:58:56 -07004833 return compiler_error(c, "'async with' outside async function");
4834 }
Yury Selivanov75445082015-05-11 22:57:16 -04004835
4836 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004837 final = compiler_new_block(c);
4838 exit = compiler_new_block(c);
4839 if (!block || !final || !exit)
Yury Selivanov75445082015-05-11 22:57:16 -04004840 return 0;
4841
4842 /* Evaluate EXPR */
4843 VISIT(c, expr, item->context_expr);
4844
4845 ADDOP(c, BEFORE_ASYNC_WITH);
4846 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004847 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004848 ADDOP(c, YIELD_FROM);
4849
Mark Shannon582aaf12020-08-04 17:30:11 +01004850 ADDOP_JUMP(c, SETUP_ASYNC_WITH, final);
Yury Selivanov75445082015-05-11 22:57:16 -04004851
4852 /* SETUP_ASYNC_WITH pushes a finally block. */
4853 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004854 if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) {
Yury Selivanov75445082015-05-11 22:57:16 -04004855 return 0;
4856 }
4857
4858 if (item->optional_vars) {
4859 VISIT(c, expr, item->optional_vars);
4860 }
4861 else {
4862 /* Discard result from context.__aenter__() */
4863 ADDOP(c, POP_TOP);
4864 }
4865
4866 pos++;
4867 if (pos == asdl_seq_LEN(s->v.AsyncWith.items))
4868 /* BLOCK code */
4869 VISIT_SEQ(c, stmt, s->v.AsyncWith.body)
4870 else if (!compiler_async_with(c, s, pos))
4871 return 0;
4872
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004873 compiler_pop_fblock(c, ASYNC_WITH, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004874 ADDOP(c, POP_BLOCK);
4875 /* End of body; start the cleanup */
Yury Selivanov75445082015-05-11 22:57:16 -04004876
Mark Shannonfee55262019-11-21 09:11:43 +00004877 /* For successful outcome:
4878 * call __exit__(None, None, None)
4879 */
4880 if(!compiler_call_exit_with_nones(c))
Yury Selivanov75445082015-05-11 22:57:16 -04004881 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004882 ADDOP(c, GET_AWAITABLE);
4883 ADDOP_O(c, LOAD_CONST, Py_None, consts);
4884 ADDOP(c, YIELD_FROM);
Yury Selivanov75445082015-05-11 22:57:16 -04004885
Mark Shannonfee55262019-11-21 09:11:43 +00004886 ADDOP(c, POP_TOP);
Yury Selivanov75445082015-05-11 22:57:16 -04004887
Mark Shannon582aaf12020-08-04 17:30:11 +01004888 ADDOP_JUMP(c, JUMP_ABSOLUTE, exit);
Mark Shannonfee55262019-11-21 09:11:43 +00004889
4890 /* For exceptional outcome: */
4891 compiler_use_next_block(c, final);
4892
4893 ADDOP(c, WITH_EXCEPT_START);
Yury Selivanov75445082015-05-11 22:57:16 -04004894 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03004895 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04004896 ADDOP(c, YIELD_FROM);
Mark Shannonfee55262019-11-21 09:11:43 +00004897 compiler_with_except_finish(c);
Yury Selivanov75445082015-05-11 22:57:16 -04004898
Mark Shannonfee55262019-11-21 09:11:43 +00004899compiler_use_next_block(c, exit);
Yury Selivanov75445082015-05-11 22:57:16 -04004900 return 1;
4901}
4902
4903
Guido van Rossumc2e20742006-02-27 22:32:47 +00004904/*
4905 Implements the with statement from PEP 343.
Guido van Rossumc2e20742006-02-27 22:32:47 +00004906 with EXPR as VAR:
4907 BLOCK
Mark Shannonfee55262019-11-21 09:11:43 +00004908 is implemented as:
4909 <code for EXPR>
4910 SETUP_WITH E
4911 <code to store to VAR> or POP_TOP
4912 <code for BLOCK>
4913 LOAD_CONST (None, None, None)
4914 CALL_FUNCTION_EX 0
4915 JUMP_FORWARD EXIT
4916 E: WITH_EXCEPT_START (calls EXPR.__exit__)
4917 POP_JUMP_IF_TRUE T:
4918 RERAISE
4919 T: POP_TOP * 3 (remove exception from stack)
4920 POP_EXCEPT
4921 POP_TOP
4922 EXIT:
Guido van Rossumc2e20742006-02-27 22:32:47 +00004923 */
Mark Shannonfee55262019-11-21 09:11:43 +00004924
Guido van Rossumc2e20742006-02-27 22:32:47 +00004925static int
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004926compiler_with(struct compiler *c, stmt_ty s, int pos)
Guido van Rossumc2e20742006-02-27 22:32:47 +00004927{
Mark Shannonfee55262019-11-21 09:11:43 +00004928 basicblock *block, *final, *exit;
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004929 withitem_ty item = asdl_seq_GET(s->v.With.items, pos);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004930
4931 assert(s->kind == With_kind);
4932
Guido van Rossumc2e20742006-02-27 22:32:47 +00004933 block = compiler_new_block(c);
Mark Shannonfee55262019-11-21 09:11:43 +00004934 final = compiler_new_block(c);
4935 exit = compiler_new_block(c);
4936 if (!block || !final || !exit)
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004937 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004938
Thomas Wouters477c8d52006-05-27 19:21:47 +00004939 /* Evaluate EXPR */
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004940 VISIT(c, expr, item->context_expr);
Mark Shannonfee55262019-11-21 09:11:43 +00004941 /* Will push bound __exit__ */
Mark Shannon582aaf12020-08-04 17:30:11 +01004942 ADDOP_JUMP(c, SETUP_WITH, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004943
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004944 /* SETUP_WITH pushes a finally block. */
Guido van Rossumc2e20742006-02-27 22:32:47 +00004945 compiler_use_next_block(c, block);
Mark Shannonfee55262019-11-21 09:11:43 +00004946 if (!compiler_push_fblock(c, WITH, block, final, NULL)) {
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004947 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004948 }
4949
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004950 if (item->optional_vars) {
4951 VISIT(c, expr, item->optional_vars);
Benjamin Peterson876b2f22009-06-28 03:18:59 +00004952 }
4953 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004954 /* Discard result from context.__enter__() */
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004955 ADDOP(c, POP_TOP);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004956 }
4957
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05004958 pos++;
4959 if (pos == asdl_seq_LEN(s->v.With.items))
4960 /* BLOCK code */
4961 VISIT_SEQ(c, stmt, s->v.With.body)
4962 else if (!compiler_with(c, s, pos))
4963 return 0;
Guido van Rossumc2e20742006-02-27 22:32:47 +00004964
Guido van Rossumc2e20742006-02-27 22:32:47 +00004965 ADDOP(c, POP_BLOCK);
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02004966 compiler_pop_fblock(c, WITH, block);
Mark Shannon13bc1392020-01-23 09:25:17 +00004967
Mark Shannonfee55262019-11-21 09:11:43 +00004968 /* End of body; start the cleanup. */
Mark Shannon13bc1392020-01-23 09:25:17 +00004969
Mark Shannonfee55262019-11-21 09:11:43 +00004970 /* For successful outcome:
4971 * call __exit__(None, None, None)
4972 */
4973 if (!compiler_call_exit_with_nones(c))
Antoine Pitrou9aee2c82010-06-22 21:49:39 +00004974 return 0;
Mark Shannonfee55262019-11-21 09:11:43 +00004975 ADDOP(c, POP_TOP);
Mark Shannon582aaf12020-08-04 17:30:11 +01004976 ADDOP_JUMP(c, JUMP_FORWARD, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004977
Mark Shannonfee55262019-11-21 09:11:43 +00004978 /* For exceptional outcome: */
4979 compiler_use_next_block(c, final);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004980
Mark Shannonfee55262019-11-21 09:11:43 +00004981 ADDOP(c, WITH_EXCEPT_START);
4982 compiler_with_except_finish(c);
4983
4984 compiler_use_next_block(c, exit);
Guido van Rossumc2e20742006-02-27 22:32:47 +00004985 return 1;
4986}
4987
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004988static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03004989compiler_visit_expr1(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00004990{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004991 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07004992 case NamedExpr_kind:
4993 VISIT(c, expr, e->v.NamedExpr.value);
4994 ADDOP(c, DUP_TOP);
4995 VISIT(c, expr, e->v.NamedExpr.target);
4996 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00004997 case BoolOp_kind:
4998 return compiler_boolop(c, e);
4999 case BinOp_kind:
5000 VISIT(c, expr, e->v.BinOp.left);
5001 VISIT(c, expr, e->v.BinOp.right);
Andy Lester76d58772020-03-10 21:18:12 -05005002 ADDOP(c, binop(e->v.BinOp.op));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005003 break;
5004 case UnaryOp_kind:
5005 VISIT(c, expr, e->v.UnaryOp.operand);
5006 ADDOP(c, unaryop(e->v.UnaryOp.op));
5007 break;
5008 case Lambda_kind:
5009 return compiler_lambda(c, e);
5010 case IfExp_kind:
5011 return compiler_ifexp(c, e);
5012 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005013 return compiler_dict(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005014 case Set_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005015 return compiler_set(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005016 case GeneratorExp_kind:
5017 return compiler_genexp(c, e);
5018 case ListComp_kind:
5019 return compiler_listcomp(c, e);
5020 case SetComp_kind:
5021 return compiler_setcomp(c, e);
5022 case DictComp_kind:
5023 return compiler_dictcomp(c, e);
5024 case Yield_kind:
5025 if (c->u->u_ste->ste_type != FunctionBlock)
5026 return compiler_error(c, "'yield' outside function");
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005027 if (e->v.Yield.value) {
5028 VISIT(c, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005029 }
5030 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005031 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005032 }
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005033 ADDOP(c, YIELD_VALUE);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005034 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005035 case YieldFrom_kind:
5036 if (c->u->u_ste->ste_type != FunctionBlock)
5037 return compiler_error(c, "'yield' outside function");
Yury Selivanov75445082015-05-11 22:57:16 -04005038
5039 if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION)
5040 return compiler_error(c, "'yield from' inside async function");
5041
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005042 VISIT(c, expr, e->v.YieldFrom.value);
Yury Selivanov5376ba92015-06-22 12:19:30 -04005043 ADDOP(c, GET_YIELD_FROM_ITER);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005044 ADDOP_LOAD_CONST(c, Py_None);
Mark Dickinsonded35ae2012-11-25 14:36:26 +00005045 ADDOP(c, YIELD_FROM);
5046 break;
Yury Selivanov75445082015-05-11 22:57:16 -04005047 case Await_kind:
Pablo Galindo90235812020-03-15 04:29:22 +00005048 if (!IS_TOP_LEVEL_AWAIT(c)){
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005049 if (c->u->u_ste->ste_type != FunctionBlock){
5050 return compiler_error(c, "'await' outside function");
5051 }
Yury Selivanov75445082015-05-11 22:57:16 -04005052
Victor Stinner331a6a52019-05-27 16:39:22 +02005053 if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005054 c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){
5055 return compiler_error(c, "'await' outside async function");
5056 }
5057 }
Yury Selivanov75445082015-05-11 22:57:16 -04005058
5059 VISIT(c, expr, e->v.Await.value);
5060 ADDOP(c, GET_AWAITABLE);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005061 ADDOP_LOAD_CONST(c, Py_None);
Yury Selivanov75445082015-05-11 22:57:16 -04005062 ADDOP(c, YIELD_FROM);
5063 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005064 case Compare_kind:
5065 return compiler_compare(c, e);
5066 case Call_kind:
5067 return compiler_call(c, e);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005068 case Constant_kind:
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005069 ADDOP_LOAD_CONST(c, e->v.Constant.value);
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01005070 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04005071 case JoinedStr_kind:
5072 return compiler_joined_str(c, e);
5073 case FormattedValue_kind:
5074 return compiler_formatted_value(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005075 /* The following exprs can be assignment targets. */
5076 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005077 VISIT(c, expr, e->v.Attribute.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005078 switch (e->v.Attribute.ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005079 case Load:
5080 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
5081 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005082 case Store:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005083 if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx))
5084 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005085 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5086 break;
5087 case Del:
5088 ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names);
5089 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005090 }
5091 break;
5092 case Subscript_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005093 return compiler_subscript(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005094 case Starred_kind:
5095 switch (e->v.Starred.ctx) {
5096 case Store:
5097 /* In all legitimate cases, the Starred node was already replaced
5098 * by compiler_list/compiler_tuple. XXX: is that okay? */
5099 return compiler_error(c,
5100 "starred assignment target must be in a list or tuple");
5101 default:
5102 return compiler_error(c,
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04005103 "can't use starred expression here");
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005104 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005105 break;
5106 case Slice_kind:
5107 return compiler_slice(c, e);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005108 case Name_kind:
5109 return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx);
5110 /* child nodes of List and Tuple will have expr_context set */
5111 case List_kind:
5112 return compiler_list(c, e);
5113 case Tuple_kind:
5114 return compiler_tuple(c, e);
5115 }
5116 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005117}
5118
5119static int
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005120compiler_visit_expr(struct compiler *c, expr_ty e)
5121{
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005122 int old_lineno = c->u->u_lineno;
5123 int old_col_offset = c->u->u_col_offset;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005124 SET_LOC(c, e);
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005125 int res = compiler_visit_expr1(c, e);
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005126 c->u->u_lineno = old_lineno;
Serhiy Storchakada8d72c2018-09-17 15:17:29 +03005127 c->u->u_col_offset = old_col_offset;
5128 return res;
5129}
5130
5131static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005132compiler_augassign(struct compiler *c, stmt_ty s)
5133{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005134 assert(s->kind == AugAssign_kind);
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005135 expr_ty e = s->v.AugAssign.target;
5136
5137 int old_lineno = c->u->u_lineno;
5138 int old_col_offset = c->u->u_col_offset;
5139 SET_LOC(c, e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005140
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005141 switch (e->kind) {
5142 case Attribute_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005143 VISIT(c, expr, e->v.Attribute.value);
5144 ADDOP(c, DUP_TOP);
5145 ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005146 break;
5147 case Subscript_kind:
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005148 VISIT(c, expr, e->v.Subscript.value);
5149 VISIT(c, expr, e->v.Subscript.slice);
5150 ADDOP(c, DUP_TOP_TWO);
5151 ADDOP(c, BINARY_SUBSCR);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005152 break;
5153 case Name_kind:
5154 if (!compiler_nameop(c, e->v.Name.id, Load))
5155 return 0;
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005156 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005157 default:
5158 PyErr_Format(PyExc_SystemError,
5159 "invalid node type (%d) for augmented assignment",
5160 e->kind);
5161 return 0;
5162 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005163
5164 c->u->u_lineno = old_lineno;
5165 c->u->u_col_offset = old_col_offset;
5166
5167 VISIT(c, expr, s->v.AugAssign.value);
5168 ADDOP(c, inplace_binop(s->v.AugAssign.op));
5169
5170 SET_LOC(c, e);
5171
5172 switch (e->kind) {
5173 case Attribute_kind:
5174 ADDOP(c, ROT_TWO);
5175 ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names);
5176 break;
5177 case Subscript_kind:
5178 ADDOP(c, ROT_THREE);
5179 ADDOP(c, STORE_SUBSCR);
5180 break;
5181 case Name_kind:
5182 return compiler_nameop(c, e->v.Name.id, Store);
5183 default:
5184 Py_UNREACHABLE();
5185 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005186 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005187}
5188
5189static int
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005190check_ann_expr(struct compiler *c, expr_ty e)
5191{
5192 VISIT(c, expr, e);
5193 ADDOP(c, POP_TOP);
5194 return 1;
5195}
5196
5197static int
5198check_annotation(struct compiler *c, stmt_ty s)
5199{
5200 /* Annotations are only evaluated in a module or class. */
5201 if (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5202 c->u->u_scope_type == COMPILER_SCOPE_CLASS) {
5203 return check_ann_expr(c, s->v.AnnAssign.annotation);
5204 }
5205 return 1;
5206}
5207
5208static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005209check_ann_subscr(struct compiler *c, expr_ty e)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005210{
5211 /* We check that everything in a subscript is defined at runtime. */
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005212 switch (e->kind) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005213 case Slice_kind:
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005214 if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005215 return 0;
5216 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005217 if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) {
5218 return 0;
5219 }
5220 if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) {
5221 return 0;
5222 }
5223 return 1;
5224 case Tuple_kind: {
5225 /* extended slice */
Pablo Galindoa5634c42020-09-16 19:42:00 +01005226 asdl_expr_seq *elts = e->v.Tuple.elts;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005227 Py_ssize_t i, n = asdl_seq_LEN(elts);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005228 for (i = 0; i < n; i++) {
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005229 if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) {
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005230 return 0;
5231 }
5232 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005233 return 1;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005234 }
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005235 default:
5236 return check_ann_expr(c, e);
5237 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005238}
5239
5240static int
5241compiler_annassign(struct compiler *c, stmt_ty s)
5242{
5243 expr_ty targ = s->v.AnnAssign.target;
Guido van Rossum015d8742016-09-11 09:45:24 -07005244 PyObject* mangled;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005245
5246 assert(s->kind == AnnAssign_kind);
5247
5248 /* We perform the actual assignment first. */
5249 if (s->v.AnnAssign.value) {
5250 VISIT(c, expr, s->v.AnnAssign.value);
5251 VISIT(c, expr, targ);
5252 }
5253 switch (targ->kind) {
5254 case Name_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005255 if (forbidden_name(c, targ->v.Name.id, Store))
5256 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005257 /* If we have a simple name in a module or class, store annotation. */
5258 if (s->v.AnnAssign.simple &&
5259 (c->u->u_scope_type == COMPILER_SCOPE_MODULE ||
5260 c->u->u_scope_type == COMPILER_SCOPE_CLASS)) {
Guido van Rossum95e4d582018-01-26 08:20:18 -08005261 if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) {
5262 VISIT(c, annexpr, s->v.AnnAssign.annotation)
5263 }
5264 else {
5265 VISIT(c, expr, s->v.AnnAssign.annotation);
5266 }
Mark Shannon332cd5e2018-01-30 00:41:04 +00005267 ADDOP_NAME(c, LOAD_NAME, __annotations__, names);
Serhiy Storchakaa95d9862018-03-24 22:42:35 +02005268 mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005269 ADDOP_LOAD_CONST_NEW(c, mangled);
Mark Shannon332cd5e2018-01-30 00:41:04 +00005270 ADDOP(c, STORE_SUBSCR);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005271 }
5272 break;
5273 case Attribute_kind:
Pablo Galindoc5fc1562020-04-22 23:29:27 +01005274 if (forbidden_name(c, targ->v.Attribute.attr, Store))
5275 return 0;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07005276 if (!s->v.AnnAssign.value &&
5277 !check_ann_expr(c, targ->v.Attribute.value)) {
5278 return 0;
5279 }
5280 break;
5281 case Subscript_kind:
5282 if (!s->v.AnnAssign.value &&
5283 (!check_ann_expr(c, targ->v.Subscript.value) ||
5284 !check_ann_subscr(c, targ->v.Subscript.slice))) {
5285 return 0;
5286 }
5287 break;
5288 default:
5289 PyErr_Format(PyExc_SystemError,
5290 "invalid node type (%d) for annotated assignment",
5291 targ->kind);
5292 return 0;
5293 }
5294 /* Annotation is evaluated last. */
5295 if (!s->v.AnnAssign.simple && !check_annotation(c, s)) {
5296 return 0;
5297 }
5298 return 1;
5299}
5300
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005301/* Raises a SyntaxError and returns 0.
5302 If something goes wrong, a different exception may be raised.
5303*/
5304
5305static int
5306compiler_error(struct compiler *c, const char *errstr)
5307{
Benjamin Peterson43b06862011-05-27 09:08:01 -05005308 PyObject *loc;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005309 PyObject *u = NULL, *v = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005310
Victor Stinner14e461d2013-08-26 22:28:21 +02005311 loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005312 if (!loc) {
5313 Py_INCREF(Py_None);
5314 loc = Py_None;
5315 }
Victor Stinner14e461d2013-08-26 22:28:21 +02005316 u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04005317 c->u->u_col_offset + 1, loc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005318 if (!u)
5319 goto exit;
5320 v = Py_BuildValue("(zO)", errstr, u);
5321 if (!v)
5322 goto exit;
5323 PyErr_SetObject(PyExc_SyntaxError, v);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005324 exit:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005325 Py_DECREF(loc);
5326 Py_XDECREF(u);
5327 Py_XDECREF(v);
5328 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005329}
5330
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005331/* Emits a SyntaxWarning and returns 1 on success.
5332 If a SyntaxWarning raised as error, replaces it with a SyntaxError
5333 and returns 0.
5334*/
5335static int
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005336compiler_warn(struct compiler *c, const char *format, ...)
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005337{
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005338 va_list vargs;
5339#ifdef HAVE_STDARG_PROTOTYPES
5340 va_start(vargs, format);
5341#else
5342 va_start(vargs);
5343#endif
5344 PyObject *msg = PyUnicode_FromFormatV(format, vargs);
5345 va_end(vargs);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005346 if (msg == NULL) {
5347 return 0;
5348 }
5349 if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename,
5350 c->u->u_lineno, NULL, NULL) < 0)
5351 {
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005352 if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) {
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005353 /* Replace the SyntaxWarning exception with a SyntaxError
5354 to get a more accurate error report */
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005355 PyErr_Clear();
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005356 assert(PyUnicode_AsUTF8(msg) != NULL);
5357 compiler_error(c, PyUnicode_AsUTF8(msg));
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005358 }
Serhiy Storchaka62e44812019-02-16 08:12:19 +02005359 Py_DECREF(msg);
Serhiy Storchakad31e7732018-10-21 10:09:39 +03005360 return 0;
5361 }
5362 Py_DECREF(msg);
5363 return 1;
5364}
5365
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005366static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005367compiler_subscript(struct compiler *c, expr_ty e)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005368{
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005369 expr_context_ty ctx = e->v.Subscript.ctx;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005370 int op = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005371
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005372 if (ctx == Load) {
5373 if (!check_subscripter(c, e->v.Subscript.value)) {
5374 return 0;
5375 }
5376 if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) {
5377 return 0;
5378 }
5379 }
5380
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005381 switch (ctx) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005382 case Load: op = BINARY_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005383 case Store: op = STORE_SUBSCR; break;
5384 case Del: op = DELETE_SUBSCR; break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005385 }
Serhiy Storchaka6b975982020-03-17 23:41:08 +02005386 assert(op);
5387 VISIT(c, expr, e->v.Subscript.value);
5388 VISIT(c, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005389 ADDOP(c, op);
5390 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005391}
5392
5393static int
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02005394compiler_slice(struct compiler *c, expr_ty s)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005395{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005396 int n = 2;
5397 assert(s->kind == Slice_kind);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005398
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005399 /* only handles the cases where BUILD_SLICE is emitted */
5400 if (s->v.Slice.lower) {
5401 VISIT(c, expr, s->v.Slice.lower);
5402 }
5403 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005404 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005405 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005406
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005407 if (s->v.Slice.upper) {
5408 VISIT(c, expr, s->v.Slice.upper);
5409 }
5410 else {
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005411 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005412 }
5413
5414 if (s->v.Slice.step) {
5415 n++;
5416 VISIT(c, expr, s->v.Slice.step);
5417 }
5418 ADDOP_I(c, BUILD_SLICE, n);
5419 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005420}
5421
Thomas Wouters89f507f2006-12-13 04:49:30 +00005422/* End of the compiler section, beginning of the assembler section */
5423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005424/* do depth-first search of basic block graph, starting with block.
T. Wouters99b54d62019-09-12 07:05:33 -07005425 post records the block indices in post-order.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005426
5427 XXX must handle implicit jumps from one block to next
5428*/
5429
Thomas Wouters89f507f2006-12-13 04:49:30 +00005430struct assembler {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005431 PyObject *a_bytecode; /* string containing bytecode */
5432 int a_offset; /* offset into bytecode */
5433 int a_nblocks; /* number of reachable blocks */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005434 basicblock **a_reverse_postorder; /* list of blocks in dfs postorder */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005435 PyObject *a_lnotab; /* string containing lnotab */
5436 int a_lnotab_off; /* offset into lnotab */
5437 int a_lineno; /* last lineno of emitted instruction */
5438 int a_lineno_off; /* bytecode offset of last lineno */
Thomas Wouters89f507f2006-12-13 04:49:30 +00005439};
5440
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005441static void
T. Wouters99b54d62019-09-12 07:05:33 -07005442dfs(struct compiler *c, basicblock *b, struct assembler *a, int end)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005443{
T. Wouters99b54d62019-09-12 07:05:33 -07005444
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005445 /* There is no real depth-first-search to do here because all the
5446 * blocks are emitted in topological order already, so we just need to
5447 * follow the b_next pointers and place them in a->a_reverse_postorder in
5448 * reverse order and make sure that the first one starts at 0. */
5449
5450 for (a->a_nblocks = 0; b != NULL; b = b->b_next) {
5451 a->a_reverse_postorder[a->a_nblocks++] = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005452 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005453}
5454
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005455Py_LOCAL_INLINE(void)
5456stackdepth_push(basicblock ***sp, basicblock *b, int depth)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005457{
Serhiy Storchaka520b7ae2018-02-22 23:33:30 +02005458 assert(b->b_startdepth < 0 || b->b_startdepth == depth);
Mark Shannonfee55262019-11-21 09:11:43 +00005459 if (b->b_startdepth < depth && b->b_startdepth < 100) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005460 assert(b->b_startdepth < 0);
5461 b->b_startdepth = depth;
5462 *(*sp)++ = b;
Serhiy Storchakad4864c62018-01-09 21:54:52 +02005463 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005464}
5465
5466/* Find the flow path that needs the largest stack. We assume that
5467 * cycles in the flow graph have no net effect on the stack depth.
5468 */
5469static int
5470stackdepth(struct compiler *c)
5471{
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005472 basicblock *b, *entryblock = NULL;
5473 basicblock **stack, **sp;
5474 int nblocks = 0, maxdepth = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005475 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005476 b->b_startdepth = INT_MIN;
5477 entryblock = b;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005478 nblocks++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005479 }
5480 if (!entryblock)
5481 return 0;
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005482 stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks);
5483 if (!stack) {
5484 PyErr_NoMemory();
5485 return -1;
5486 }
5487
5488 sp = stack;
5489 stackdepth_push(&sp, entryblock, 0);
5490 while (sp != stack) {
5491 b = *--sp;
5492 int depth = b->b_startdepth;
5493 assert(depth >= 0);
5494 basicblock *next = b->b_next;
5495 for (int i = 0; i < b->b_iused; i++) {
5496 struct instr *instr = &b->b_instr[i];
5497 int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0);
5498 if (effect == PY_INVALID_STACK_EFFECT) {
Victor Stinner87d3b9d2020-03-25 19:27:36 +01005499 _Py_FatalErrorFormat(__func__,
5500 "opcode = %d", instr->i_opcode);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005501 }
5502 int new_depth = depth + effect;
5503 if (new_depth > maxdepth) {
5504 maxdepth = new_depth;
5505 }
5506 assert(depth >= 0); /* invalid code or bug in stackdepth() */
Mark Shannon582aaf12020-08-04 17:30:11 +01005507 if (is_jump(instr)) {
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005508 effect = stack_effect(instr->i_opcode, instr->i_oparg, 1);
5509 assert(effect != PY_INVALID_STACK_EFFECT);
5510 int target_depth = depth + effect;
5511 if (target_depth > maxdepth) {
5512 maxdepth = target_depth;
5513 }
5514 assert(target_depth >= 0); /* invalid code or bug in stackdepth() */
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005515 stackdepth_push(&sp, instr->i_target, target_depth);
5516 }
5517 depth = new_depth;
5518 if (instr->i_opcode == JUMP_ABSOLUTE ||
5519 instr->i_opcode == JUMP_FORWARD ||
5520 instr->i_opcode == RETURN_VALUE ||
Mark Shannonfee55262019-11-21 09:11:43 +00005521 instr->i_opcode == RAISE_VARARGS ||
5522 instr->i_opcode == RERAISE)
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005523 {
5524 /* remaining code is dead */
5525 next = NULL;
5526 break;
5527 }
5528 }
5529 if (next != NULL) {
5530 stackdepth_push(&sp, next, depth);
5531 }
5532 }
5533 PyObject_Free(stack);
5534 return maxdepth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005535}
5536
5537static int
5538assemble_init(struct assembler *a, int nblocks, int firstlineno)
5539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005540 memset(a, 0, sizeof(struct assembler));
5541 a->a_lineno = firstlineno;
5542 a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE);
5543 if (!a->a_bytecode)
5544 return 0;
5545 a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE);
5546 if (!a->a_lnotab)
5547 return 0;
Benjamin Peterson2f8bfef2016-09-07 09:26:18 -07005548 if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005549 PyErr_NoMemory();
5550 return 0;
5551 }
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005552 a->a_reverse_postorder = (basicblock **)PyObject_Malloc(
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005553 sizeof(basicblock *) * nblocks);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005554 if (!a->a_reverse_postorder) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005555 PyErr_NoMemory();
5556 return 0;
5557 }
5558 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005559}
5560
5561static void
5562assemble_free(struct assembler *a)
5563{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005564 Py_XDECREF(a->a_bytecode);
5565 Py_XDECREF(a->a_lnotab);
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005566 if (a->a_reverse_postorder)
5567 PyObject_Free(a->a_reverse_postorder);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005568}
5569
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005570static int
5571blocksize(basicblock *b)
5572{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005573 int i;
5574 int size = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005575
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005576 for (i = 0; i < b->b_iused; i++)
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005577 size += instrsize(b->b_instr[i].i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005578 return size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005579}
5580
Alexandre Vassalotti7b82b402009-07-21 04:30:03 +00005581/* Appends a pair to the end of the line number table, a_lnotab, representing
5582 the instruction's bytecode offset and line number. See
5583 Objects/lnotab_notes.txt for the description of the line number table. */
Tim Peters2a7f3842001-06-09 09:26:21 +00005584
Guido van Rossumf68d8e52001-04-14 17:55:09 +00005585static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005586assemble_lnotab(struct assembler *a, struct instr *i)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005587{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005588 int d_bytecode, d_lineno;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005589 Py_ssize_t len;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005590 unsigned char *lnotab;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005592 d_lineno = i->i_lineno - a->a_lineno;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005593 if (d_lineno == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005594 return 1;
Serhiy Storchaka61cb3d02020-03-17 18:07:30 +02005595 }
5596
5597 d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT);
5598 assert(d_bytecode >= 0);
Guido van Rossum4bad92c1991-07-27 21:34:52 +00005599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005600 if (d_bytecode > 255) {
5601 int j, nbytes, ncodes = d_bytecode / 255;
5602 nbytes = a->a_lnotab_off + 2 * ncodes;
5603 len = PyBytes_GET_SIZE(a->a_lnotab);
5604 if (nbytes >= len) {
5605 if ((len <= INT_MAX / 2) && (len * 2 < nbytes))
5606 len = nbytes;
5607 else if (len <= INT_MAX / 2)
5608 len *= 2;
5609 else {
5610 PyErr_NoMemory();
5611 return 0;
5612 }
5613 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5614 return 0;
5615 }
5616 lnotab = (unsigned char *)
5617 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5618 for (j = 0; j < ncodes; j++) {
5619 *lnotab++ = 255;
5620 *lnotab++ = 0;
5621 }
5622 d_bytecode -= ncodes * 255;
5623 a->a_lnotab_off += ncodes * 2;
5624 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005625 assert(0 <= d_bytecode && d_bytecode <= 255);
5626
5627 if (d_lineno < -128 || 127 < d_lineno) {
5628 int j, nbytes, ncodes, k;
5629 if (d_lineno < 0) {
5630 k = -128;
5631 /* use division on positive numbers */
5632 ncodes = (-d_lineno) / 128;
5633 }
5634 else {
5635 k = 127;
5636 ncodes = d_lineno / 127;
5637 }
5638 d_lineno -= ncodes * k;
5639 assert(ncodes >= 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005640 nbytes = a->a_lnotab_off + 2 * ncodes;
5641 len = PyBytes_GET_SIZE(a->a_lnotab);
5642 if (nbytes >= len) {
5643 if ((len <= INT_MAX / 2) && len * 2 < nbytes)
5644 len = nbytes;
5645 else if (len <= INT_MAX / 2)
5646 len *= 2;
5647 else {
5648 PyErr_NoMemory();
5649 return 0;
5650 }
5651 if (_PyBytes_Resize(&a->a_lnotab, len) < 0)
5652 return 0;
5653 }
5654 lnotab = (unsigned char *)
5655 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
5656 *lnotab++ = d_bytecode;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005657 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005658 d_bytecode = 0;
5659 for (j = 1; j < ncodes; j++) {
5660 *lnotab++ = 0;
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005661 *lnotab++ = k;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005662 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005663 a->a_lnotab_off += ncodes * 2;
5664 }
Victor Stinnerf3914eb2016-01-20 12:16:21 +01005665 assert(-128 <= d_lineno && d_lineno <= 127);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005667 len = PyBytes_GET_SIZE(a->a_lnotab);
5668 if (a->a_lnotab_off + 2 >= len) {
5669 if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0)
5670 return 0;
5671 }
5672 lnotab = (unsigned char *)
5673 PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off;
Tim Peters51e26512001-09-07 08:45:55 +00005674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005675 a->a_lnotab_off += 2;
5676 if (d_bytecode) {
5677 *lnotab++ = d_bytecode;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005678 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005679 }
5680 else { /* First line of a block; def stmt, etc. */
5681 *lnotab++ = 0;
Victor Stinner4f2dab52011-05-27 16:46:51 +02005682 *lnotab++ = d_lineno;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005683 }
5684 a->a_lineno = i->i_lineno;
5685 a->a_lineno_off = a->a_offset;
5686 return 1;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005687}
5688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005689/* assemble_emit()
5690 Extend the bytecode with a new instruction.
5691 Update lnotab if necessary.
Jeremy Hylton376e63d2003-08-28 14:42:14 +00005692*/
5693
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005694static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005695assemble_emit(struct assembler *a, struct instr *i)
Guido van Rossum4ca6c9d1994-08-29 12:16:12 +00005696{
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005697 int size, arg = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005698 Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode);
Serhiy Storchakaab874002016-09-11 13:48:15 +03005699 _Py_CODEUNIT *code;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005700
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005701 arg = i->i_oparg;
5702 size = instrsize(arg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005703 if (i->i_lineno && !assemble_lnotab(a, i))
5704 return 0;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005705 if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005706 if (len > PY_SSIZE_T_MAX / 2)
5707 return 0;
5708 if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0)
5709 return 0;
5710 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005711 code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005712 a->a_offset += size;
Serhiy Storchakaab874002016-09-11 13:48:15 +03005713 write_op_arg(code, i->i_opcode, arg, size);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005714 return 1;
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005715}
5716
Neal Norwitz7d37f2f2005-10-23 22:40:47 +00005717static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005718assemble_jump_offsets(struct assembler *a, struct compiler *c)
Anthony Baxterc2a5a632004-08-02 06:10:11 +00005719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005720 basicblock *b;
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005721 int bsize, totsize, extended_arg_recompile;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005722 int i;
Guido van Rossumc5e96291991-12-10 13:53:51 +00005723
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005724 /* Compute the size of each block and fixup jump args.
5725 Replace block pointer with position in bytecode. */
5726 do {
5727 totsize = 0;
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005728 for (i = 0; i < a->a_nblocks; i++) {
5729 b = a->a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005730 bsize = blocksize(b);
5731 b->b_offset = totsize;
5732 totsize += bsize;
5733 }
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005734 extended_arg_recompile = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005735 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
5736 bsize = b->b_offset;
5737 for (i = 0; i < b->b_iused; i++) {
5738 struct instr *instr = &b->b_instr[i];
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005739 int isize = instrsize(instr->i_oparg);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005740 /* Relative jumps are computed relative to
5741 the instruction pointer after fetching
5742 the jump instruction.
5743 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005744 bsize += isize;
Mark Shannon582aaf12020-08-04 17:30:11 +01005745 if (is_jump(instr)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005746 instr->i_oparg = instr->i_target->b_offset;
Mark Shannon582aaf12020-08-04 17:30:11 +01005747 if (is_relative_jump(instr)) {
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005748 instr->i_oparg -= bsize;
5749 }
Serhiy Storchakaab874002016-09-11 13:48:15 +03005750 instr->i_oparg *= sizeof(_Py_CODEUNIT);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005751 if (instrsize(instr->i_oparg) != isize) {
5752 extended_arg_recompile = 1;
5753 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005754 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005755 }
5756 }
Neal Norwitzf1d50682005-10-23 23:00:41 +00005757
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005758 /* XXX: This is an awful hack that could hurt performance, but
5759 on the bright side it should work until we come up
5760 with a better solution.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005762 The issue is that in the first loop blocksize() is called
5763 which calls instrsize() which requires i_oparg be set
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005764 appropriately. There is a bootstrap problem because
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005765 i_oparg is calculated in the second loop above.
Neal Norwitzf1d50682005-10-23 23:00:41 +00005766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005767 So we loop until we stop seeing new EXTENDED_ARGs.
5768 The only EXTENDED_ARGs that could be popping up are
5769 ones in jump instructions. So this should converge
5770 fairly quickly.
5771 */
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005772 } while (extended_arg_recompile);
Guido van Rossum10dc2e81990-11-18 17:27:39 +00005773}
5774
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005775static PyObject *
Victor Stinnerad9a0662013-11-19 22:23:20 +01005776dict_keys_inorder(PyObject *dict, Py_ssize_t offset)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005778 PyObject *tuple, *k, *v;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005779 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005780
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005781 tuple = PyTuple_New(size);
5782 if (tuple == NULL)
5783 return NULL;
5784 while (PyDict_Next(dict, &pos, &k, &v)) {
5785 i = PyLong_AS_LONG(v);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005786 Py_INCREF(k);
5787 assert((i - offset) < size);
5788 assert((i - offset) >= 0);
5789 PyTuple_SET_ITEM(tuple, i - offset, k);
5790 }
5791 return tuple;
5792}
5793
5794static PyObject *
5795consts_dict_keys_inorder(PyObject *dict)
5796{
5797 PyObject *consts, *k, *v;
5798 Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict);
5799
5800 consts = PyList_New(size); /* PyCode_Optimize() requires a list */
5801 if (consts == NULL)
5802 return NULL;
5803 while (PyDict_Next(dict, &pos, &k, &v)) {
5804 i = PyLong_AS_LONG(v);
Serhiy Storchakab7e1eff2018-04-19 08:28:04 +03005805 /* The keys of the dictionary can be tuples wrapping a contant.
5806 * (see compiler_add_o and _PyCode_ConstantKey). In that case
5807 * the object we want is always second. */
5808 if (PyTuple_CheckExact(k)) {
5809 k = PyTuple_GET_ITEM(k, 1);
5810 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005811 Py_INCREF(k);
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005812 assert(i < size);
5813 assert(i >= 0);
5814 PyList_SET_ITEM(consts, i, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005815 }
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005816 return consts;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005817}
5818
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005819static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005820compute_code_flags(struct compiler *c)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005822 PySTEntryObject *ste = c->u->u_ste;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005823 int flags = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005824 if (ste->ste_type == FunctionBlock) {
Benjamin Peterson1dfd2472015-04-27 21:44:22 -04005825 flags |= CO_NEWLOCALS | CO_OPTIMIZED;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005826 if (ste->ste_nested)
5827 flags |= CO_NESTED;
Yury Selivanoveb636452016-09-08 22:01:51 -07005828 if (ste->ste_generator && !ste->ste_coroutine)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005829 flags |= CO_GENERATOR;
Yury Selivanoveb636452016-09-08 22:01:51 -07005830 if (!ste->ste_generator && ste->ste_coroutine)
5831 flags |= CO_COROUTINE;
5832 if (ste->ste_generator && ste->ste_coroutine)
5833 flags |= CO_ASYNC_GENERATOR;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005834 if (ste->ste_varargs)
5835 flags |= CO_VARARGS;
5836 if (ste->ste_varkeywords)
5837 flags |= CO_VARKEYWORDS;
5838 }
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005839
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005840 /* (Only) inherit compilerflags in PyCF_MASK */
5841 flags |= (c->c_flags->cf_flags & PyCF_MASK);
Thomas Wouters5e9f1fa2006-02-28 20:02:27 +00005842
Pablo Galindo90235812020-03-15 04:29:22 +00005843 if ((IS_TOP_LEVEL_AWAIT(c)) &&
Matthias Bussonnier565b4f12019-05-21 13:12:03 -07005844 ste->ste_coroutine &&
5845 !ste->ste_generator) {
5846 flags |= CO_COROUTINE;
5847 }
5848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005849 return flags;
Jeremy Hylton29906ee2001-02-27 04:23:34 +00005850}
5851
INADA Naokic2e16072018-11-26 21:23:22 +09005852// Merge *tuple* with constant cache.
5853// Unlike merge_consts_recursive(), this function doesn't work recursively.
5854static int
5855merge_const_tuple(struct compiler *c, PyObject **tuple)
5856{
5857 assert(PyTuple_CheckExact(*tuple));
5858
5859 PyObject *key = _PyCode_ConstantKey(*tuple);
5860 if (key == NULL) {
5861 return 0;
5862 }
5863
5864 // t is borrowed reference
5865 PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key);
5866 Py_DECREF(key);
5867 if (t == NULL) {
5868 return 0;
5869 }
5870 if (t == key) { // tuple is new constant.
5871 return 1;
5872 }
5873
5874 PyObject *u = PyTuple_GET_ITEM(t, 1);
5875 Py_INCREF(u);
5876 Py_DECREF(*tuple);
5877 *tuple = u;
5878 return 1;
5879}
5880
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005881static PyCodeObject *
Mark Shannon6e8128f2020-07-30 10:03:00 +01005882makecode(struct compiler *c, struct assembler *a, PyObject *consts)
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005884 PyCodeObject *co = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005885 PyObject *names = NULL;
5886 PyObject *varnames = NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005887 PyObject *name = NULL;
5888 PyObject *freevars = NULL;
5889 PyObject *cellvars = NULL;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005890 Py_ssize_t nlocals;
5891 int nlocals_int;
5892 int flags;
Pablo Galindocd74e662019-06-01 18:08:04 +01005893 int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00005894
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005895 names = dict_keys_inorder(c->u->u_names, 0);
5896 varnames = dict_keys_inorder(c->u->u_varnames, 0);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005897 if (!names || !varnames) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005898 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005899 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005900 cellvars = dict_keys_inorder(c->u->u_cellvars, 0);
5901 if (!cellvars)
5902 goto error;
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03005903 freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005904 if (!freevars)
5905 goto error;
Victor Stinnerad9a0662013-11-19 22:23:20 +01005906
INADA Naokic2e16072018-11-26 21:23:22 +09005907 if (!merge_const_tuple(c, &names) ||
5908 !merge_const_tuple(c, &varnames) ||
5909 !merge_const_tuple(c, &cellvars) ||
5910 !merge_const_tuple(c, &freevars))
5911 {
5912 goto error;
5913 }
5914
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02005915 nlocals = PyDict_GET_SIZE(c->u->u_varnames);
Victor Stinnerad9a0662013-11-19 22:23:20 +01005916 assert(nlocals < INT_MAX);
5917 nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int);
5918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005919 flags = compute_code_flags(c);
5920 if (flags < 0)
5921 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005922
Mark Shannon6e8128f2020-07-30 10:03:00 +01005923 consts = PyList_AsTuple(consts); /* PyCode_New requires a tuple */
5924 if (consts == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005925 goto error;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005926 }
INADA Naokic2e16072018-11-26 21:23:22 +09005927 if (!merge_const_tuple(c, &consts)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005928 Py_DECREF(consts);
INADA Naokic2e16072018-11-26 21:23:22 +09005929 goto error;
5930 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005931
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01005932 posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int);
Pablo Galindocd74e662019-06-01 18:08:04 +01005933 posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int);
Victor Stinnerf8e32212013-11-19 23:56:34 +01005934 kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005935 maxdepth = stackdepth(c);
5936 if (maxdepth < 0) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01005937 Py_DECREF(consts);
Serhiy Storchaka782d6fe2018-01-11 20:20:13 +02005938 goto error;
5939 }
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005940 co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount,
Mark Shannon13bc1392020-01-23 09:25:17 +00005941 posonlyargcount, kwonlyargcount, nlocals_int,
Mark Shannon6e8128f2020-07-30 10:03:00 +01005942 maxdepth, flags, a->a_bytecode, consts, names,
Pablo Galindo4a2edc32019-07-01 11:35:05 +01005943 varnames, freevars, cellvars, c->c_filename,
5944 c->u->u_name, c->u->u_firstlineno, a->a_lnotab);
Mark Shannon6e8128f2020-07-30 10:03:00 +01005945 Py_DECREF(consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005946 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005947 Py_XDECREF(names);
5948 Py_XDECREF(varnames);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005949 Py_XDECREF(name);
5950 Py_XDECREF(freevars);
5951 Py_XDECREF(cellvars);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005952 return co;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005953}
5954
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005955
5956/* For debugging purposes only */
5957#if 0
5958static void
5959dump_instr(const struct instr *i)
5960{
Mark Shannon582aaf12020-08-04 17:30:11 +01005961 const char *jrel = (is_relative_jump(instr)) ? "jrel " : "";
5962 const char *jabs = (is_jump(instr) && !is_relative_jump(instr))? "jabs " : "";
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005963 char arg[128];
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005964
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005965 *arg = '\0';
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005966 if (HAS_ARG(i->i_opcode)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005967 sprintf(arg, "arg: %d ", i->i_oparg);
Serhiy Storchakab0f80b02016-05-24 09:15:14 +03005968 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005969 fprintf(stderr, "line: %d, opcode: %d %s%s%s\n",
5970 i->i_lineno, i->i_opcode, arg, jabs, jrel);
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005971}
5972
5973static void
5974dump_basicblock(const basicblock *b)
5975{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005976 const char *b_return = b->b_return ? "return " : "";
Pablo Galindo60eb9f12020-06-28 01:55:47 +01005977 fprintf(stderr, "used: %d, depth: %d, offset: %d %s\n",
5978 b->b_iused, b->b_startdepth, b->b_offset, b_return);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005979 if (b->b_instr) {
5980 int i;
5981 for (i = 0; i < b->b_iused; i++) {
5982 fprintf(stderr, " [%02d] ", i);
5983 dump_instr(b->b_instr + i);
5984 }
5985 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +00005986}
5987#endif
5988
Mark Shannon6e8128f2020-07-30 10:03:00 +01005989static int
5990optimize_cfg(struct assembler *a, PyObject *consts);
5991
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00005992static PyCodeObject *
5993assemble(struct compiler *c, int addNone)
Jeremy Hylton64949cb2001-01-25 20:06:59 +00005994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00005995 basicblock *b, *entryblock;
5996 struct assembler a;
5997 int i, j, nblocks;
5998 PyCodeObject *co = NULL;
Mark Shannon6e8128f2020-07-30 10:03:00 +01005999 PyObject *consts = NULL;
Jeremy Hylton64949cb2001-01-25 20:06:59 +00006000
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006001 /* Make sure every block that falls off the end returns None.
6002 XXX NEXT_BLOCK() isn't quite right, because if the last
6003 block ends with a jump or return b_next shouldn't set.
6004 */
6005 if (!c->u->u_curblock->b_return) {
6006 NEXT_BLOCK(c);
6007 if (addNone)
Serhiy Storchakad70c2a62018-04-20 16:01:25 +03006008 ADDOP_LOAD_CONST(c, Py_None);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006009 ADDOP(c, RETURN_VALUE);
6010 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006012 nblocks = 0;
6013 entryblock = NULL;
6014 for (b = c->u->u_blocks; b != NULL; b = b->b_list) {
6015 nblocks++;
6016 entryblock = b;
6017 }
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006018
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006019 /* Set firstlineno if it wasn't explicitly set. */
6020 if (!c->u->u_firstlineno) {
Ned Deilydc35cda2016-08-17 17:18:33 -04006021 if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006022 c->u->u_firstlineno = entryblock->b_instr->i_lineno;
6023 else
6024 c->u->u_firstlineno = 1;
6025 }
6026 if (!assemble_init(&a, nblocks, c->u->u_firstlineno))
6027 goto error;
T. Wouters99b54d62019-09-12 07:05:33 -07006028 dfs(c, entryblock, &a, nblocks);
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006029
Mark Shannon6e8128f2020-07-30 10:03:00 +01006030 consts = consts_dict_keys_inorder(c->u->u_consts);
6031 if (consts == NULL) {
6032 goto error;
6033 }
6034 if (optimize_cfg(&a, consts)) {
6035 goto error;
6036 }
6037
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006038 /* Can't modify the bytecode after computing jump offsets. */
6039 assemble_jump_offsets(&a, c);
Tim Petersb6c3cea2001-06-26 03:36:28 +00006040
T. Wouters99b54d62019-09-12 07:05:33 -07006041 /* Emit code in reverse postorder from dfs. */
Pablo Galindo60eb9f12020-06-28 01:55:47 +01006042 for (i = 0; i < a.a_nblocks; i++) {
6043 b = a.a_reverse_postorder[i];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006044 for (j = 0; j < b->b_iused; j++)
6045 if (!assemble_emit(&a, &b->b_instr[j]))
6046 goto error;
6047 }
Tim Petersb6c3cea2001-06-26 03:36:28 +00006048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006049 if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0)
6050 goto error;
Serhiy Storchakaab874002016-09-11 13:48:15 +03006051 if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006052 goto error;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006053
Mark Shannon6e8128f2020-07-30 10:03:00 +01006054 co = makecode(c, &a, consts);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006055 error:
Mark Shannon6e8128f2020-07-30 10:03:00 +01006056 Py_XDECREF(consts);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00006057 assemble_free(&a);
6058 return co;
Jeremy Hyltone36f7782001-01-19 03:21:30 +00006059}
Georg Brandl8334fd92010-12-04 10:26:46 +00006060
6061#undef PyAST_Compile
Benjamin Petersone5024512018-09-12 12:06:42 -07006062PyCodeObject *
Georg Brandl8334fd92010-12-04 10:26:46 +00006063PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags,
6064 PyArena *arena)
6065{
6066 return PyAST_CompileEx(mod, filename, flags, -1, arena);
6067}
Mark Shannon6e8128f2020-07-30 10:03:00 +01006068
6069
6070/* Replace LOAD_CONST c1, LOAD_CONST c2 ... LOAD_CONST cn, BUILD_TUPLE n
6071 with LOAD_CONST (c1, c2, ... cn).
6072 The consts table must still be in list form so that the
6073 new constant (c1, c2, ... cn) can be appended.
6074 Called with codestr pointing to the first LOAD_CONST.
6075*/
6076static int
6077fold_tuple_on_constants(struct instr *inst,
6078 int n, PyObject *consts)
6079{
6080 /* Pre-conditions */
6081 assert(PyList_CheckExact(consts));
6082 assert(inst[n].i_opcode == BUILD_TUPLE);
6083 assert(inst[n].i_oparg == n);
6084
6085 for (int i = 0; i < n; i++) {
6086 if (inst[i].i_opcode != LOAD_CONST) {
6087 return 0;
6088 }
6089 }
6090
6091 /* Buildup new tuple of constants */
6092 PyObject *newconst = PyTuple_New(n);
6093 if (newconst == NULL) {
6094 return -1;
6095 }
6096 for (int i = 0; i < n; i++) {
6097 int arg = inst[i].i_oparg;
6098 PyObject *constant = PyList_GET_ITEM(consts, arg);
6099 Py_INCREF(constant);
6100 PyTuple_SET_ITEM(newconst, i, constant);
6101 }
6102 Py_ssize_t index = PyList_GET_SIZE(consts);
6103#if SIZEOF_SIZE_T > SIZEOF_INT
6104 if ((size_t)index >= UINT_MAX - 1) {
6105 Py_DECREF(newconst);
6106 PyErr_SetString(PyExc_OverflowError, "too many constants");
6107 return -1;
6108 }
6109#endif
6110 if (PyList_Append(consts, newconst)) {
6111 Py_DECREF(newconst);
6112 return -1;
6113 }
6114 Py_DECREF(newconst);
6115 for (int i = 0; i < n; i++) {
6116 inst[i].i_opcode = NOP;
6117 }
6118 inst[n].i_opcode = LOAD_CONST;
6119 inst[n].i_oparg = index;
6120 return 0;
6121}
6122
6123
6124/* Optimization */
6125static int
6126optimize_basic_block(basicblock *bb, PyObject *consts)
6127{
6128 assert(PyList_CheckExact(consts));
6129 struct instr nop;
6130 nop.i_opcode = NOP;
6131 struct instr *target;
6132 int lineno;
6133 for (int i = 0; i < bb->b_iused; i++) {
6134 struct instr *inst = &bb->b_instr[i];
6135 int oparg = inst->i_oparg;
6136 int nextop = i+1 < bb->b_iused ? bb->b_instr[i+1].i_opcode : 0;
Mark Shannon582aaf12020-08-04 17:30:11 +01006137 if (is_jump(inst)) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006138 /* Skip over empty basic blocks. */
6139 while (inst->i_target->b_iused == 0) {
6140 inst->i_target = inst->i_target->b_next;
6141 }
6142 target = &inst->i_target->b_instr[0];
6143 }
6144 else {
6145 target = &nop;
6146 }
6147 switch (inst->i_opcode) {
6148 /* Skip over LOAD_CONST trueconst
6149 POP_JUMP_IF_FALSE xx. This improves
6150 "while 1" performance. */
6151 case LOAD_CONST:
6152 if (nextop != POP_JUMP_IF_FALSE) {
6153 break;
6154 }
6155 PyObject* cnt = PyList_GET_ITEM(consts, oparg);
6156 int is_true = PyObject_IsTrue(cnt);
6157 if (is_true == -1) {
6158 goto error;
6159 }
6160 if (is_true == 1) {
6161 inst->i_opcode = NOP;
6162 bb->b_instr[i+1].i_opcode = NOP;
Mark Shannon6e8128f2020-07-30 10:03:00 +01006163 }
6164 break;
6165
6166 /* Try to fold tuples of constants.
6167 Skip over BUILD_SEQN 1 UNPACK_SEQN 1.
6168 Replace BUILD_SEQN 2 UNPACK_SEQN 2 with ROT2.
6169 Replace BUILD_SEQN 3 UNPACK_SEQN 3 with ROT3 ROT2. */
6170 case BUILD_TUPLE:
6171 if (nextop == UNPACK_SEQUENCE && oparg == bb->b_instr[i+1].i_oparg) {
6172 switch(oparg) {
6173 case 1:
6174 inst->i_opcode = NOP;
6175 bb->b_instr[i+1].i_opcode = NOP;
6176 break;
6177 case 2:
6178 inst->i_opcode = ROT_TWO;
6179 bb->b_instr[i+1].i_opcode = NOP;
6180 break;
6181 case 3:
6182 inst->i_opcode = ROT_THREE;
6183 bb->b_instr[i+1].i_opcode = ROT_TWO;
6184 }
6185 break;
6186 }
6187 if (i >= oparg) {
6188 if (fold_tuple_on_constants(inst-oparg, oparg, consts)) {
6189 goto error;
6190 }
6191 }
6192 break;
6193
6194 /* Simplify conditional jump to conditional jump where the
6195 result of the first test implies the success of a similar
6196 test or the failure of the opposite test.
6197 Arises in code like:
6198 "a and b or c"
6199 "(a and b) and c"
6200 "(a or b) or c"
6201 "(a or b) and c"
6202 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_FALSE_OR_POP z
6203 --> x:JUMP_IF_FALSE_OR_POP z
6204 x:JUMP_IF_FALSE_OR_POP y y:JUMP_IF_TRUE_OR_POP z
6205 --> x:POP_JUMP_IF_FALSE y+1
6206 where y+1 is the instruction following the second test.
6207 */
6208 case JUMP_IF_FALSE_OR_POP:
6209 switch(target->i_opcode) {
6210 case POP_JUMP_IF_FALSE:
6211 *inst = *target;
6212 break;
6213 case JUMP_ABSOLUTE:
6214 case JUMP_FORWARD:
6215 case JUMP_IF_FALSE_OR_POP:
6216 inst->i_target = target->i_target;
6217 break;
6218 case JUMP_IF_TRUE_OR_POP:
6219 assert (inst->i_target->b_iused == 1);
6220 inst->i_opcode = POP_JUMP_IF_FALSE;
6221 inst->i_target = inst->i_target->b_next;
6222 break;
6223 }
6224 break;
6225
6226 case JUMP_IF_TRUE_OR_POP:
6227 switch(target->i_opcode) {
6228 case POP_JUMP_IF_TRUE:
6229 *inst = *target;
6230 break;
6231 case JUMP_ABSOLUTE:
6232 case JUMP_FORWARD:
6233 case JUMP_IF_TRUE_OR_POP:
6234 inst->i_target = target->i_target;
6235 break;
6236 case JUMP_IF_FALSE_OR_POP:
6237 assert (inst->i_target->b_iused == 1);
6238 inst->i_opcode = POP_JUMP_IF_TRUE;
6239 inst->i_target = inst->i_target->b_next;
6240 break;
6241 }
6242 break;
6243
6244 case POP_JUMP_IF_FALSE:
6245 switch(target->i_opcode) {
6246 case JUMP_ABSOLUTE:
6247 case JUMP_FORWARD:
6248 inst->i_target = target->i_target;
6249 break;
6250 }
6251 break;
6252
6253 case POP_JUMP_IF_TRUE:
6254 switch(target->i_opcode) {
6255 case JUMP_ABSOLUTE:
6256 case JUMP_FORWARD:
6257 inst->i_target = target->i_target;
6258 break;
6259 }
6260 break;
6261
6262 case JUMP_ABSOLUTE:
6263 case JUMP_FORWARD:
6264 switch(target->i_opcode) {
6265 case JUMP_FORWARD:
6266 inst->i_target = target->i_target;
6267 break;
6268 case JUMP_ABSOLUTE:
6269 case RETURN_VALUE:
6270 case RERAISE:
6271 case RAISE_VARARGS:
6272 lineno = inst->i_lineno;
6273 *inst = *target;
6274 inst->i_lineno = lineno;
6275 break;
6276 }
6277 break;
6278 }
6279 }
6280 return 0;
6281error:
6282 return -1;
6283}
6284
6285
6286static void
6287clean_basic_block(basicblock *bb) {
6288 /* Remove NOPs and any code following a return or re-raise. */
6289 int dest = 0;
6290 for (int src = 0; src < bb->b_iused; src++) {
6291 switch(bb->b_instr[src].i_opcode) {
6292 case NOP:
6293 /* skip */
6294 break;
6295 case RETURN_VALUE:
6296 case RERAISE:
6297 bb->b_next = NULL;
6298 bb->b_instr[dest] = bb->b_instr[src];
6299 dest++;
6300 goto end;
6301 default:
6302 if (dest != src) {
6303 bb->b_instr[dest] = bb->b_instr[src];
6304 }
6305 dest++;
6306 break;
6307 }
6308 }
6309end:
6310 assert(dest <= bb->b_iused);
6311 bb->b_iused = dest;
6312}
6313
6314static int
6315mark_reachable(struct assembler *a) {
6316 basicblock **stack, **sp;
6317 sp = stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * a->a_nblocks);
6318 if (stack == NULL) {
6319 return -1;
6320 }
6321 basicblock *entry = a->a_reverse_postorder[0];
6322 entry->b_reachable = 1;
6323 *sp++ = entry;
6324 while (sp > stack) {
6325 basicblock *b = *(--sp);
6326 if (b->b_next && b->b_next->b_reachable == 0) {
6327 b->b_next->b_reachable = 1;
6328 *sp++ = b->b_next;
6329 }
6330 for (int i = 0; i < b->b_iused; i++) {
6331 basicblock *target;
Mark Shannon582aaf12020-08-04 17:30:11 +01006332 if (is_jump(&b->b_instr[i])) {
Mark Shannon6e8128f2020-07-30 10:03:00 +01006333 target = b->b_instr[i].i_target;
6334 if (target->b_reachable == 0) {
6335 target->b_reachable = 1;
6336 *sp++ = target;
6337 }
6338 }
6339 }
6340 }
6341 PyObject_Free(stack);
6342 return 0;
6343}
6344
6345
6346/* Perform basic peephole optimizations on a control flow graph.
6347 The consts object should still be in list form to allow new constants
6348 to be appended.
6349
6350 All transformations keep the code size the same or smaller.
6351 For those that reduce size, the gaps are initially filled with
6352 NOPs. Later those NOPs are removed.
6353*/
6354
6355static int
6356optimize_cfg(struct assembler *a, PyObject *consts)
6357{
6358 for (int i = 0; i < a->a_nblocks; i++) {
6359 if (optimize_basic_block(a->a_reverse_postorder[i], consts)) {
6360 return -1;
6361 }
6362 clean_basic_block(a->a_reverse_postorder[i]);
6363 assert(a->a_reverse_postorder[i]->b_reachable == 0);
6364 }
6365 if (mark_reachable(a)) {
6366 return -1;
6367 }
6368 /* Delete unreachable instructions */
6369 for (int i = 0; i < a->a_nblocks; i++) {
6370 if (a->a_reverse_postorder[i]->b_reachable == 0) {
6371 a->a_reverse_postorder[i]->b_iused = 0;
6372 }
6373 }
6374 return 0;
6375}
6376
6377/* Retained for API compatibility.
6378 * Optimization is now done in optimize_cfg */
6379
6380PyObject *
6381PyCode_Optimize(PyObject *code, PyObject* Py_UNUSED(consts),
6382 PyObject *Py_UNUSED(names), PyObject *Py_UNUSED(lnotab_obj))
6383{
6384 Py_INCREF(code);
6385 return code;
6386}
6387