Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1 | /* |
| 2 | * This file compiles an abstract syntax tree (AST) into Python bytecode. |
| 3 | * |
| 4 | * The primary entry point is PyAST_Compile(), which returns a |
| 5 | * PyCodeObject. The compiler makes several passes to build the code |
| 6 | * object: |
| 7 | * 1. Checks for future statements. See future.c |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 8 | * 2. Builds a symbol table. See symtable.c. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | * 3. Generate code for basic blocks. See compiler_mod() in this file. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 10 | * 4. Assemble the basic blocks into final code. See assemble() in |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 11 | * this file. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 12 | * 5. Optimize the byte code (peephole optimizations). See peephole.c |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 13 | * |
| 14 | * Note that compiler_mod() suggests module, but the module ast type |
| 15 | * (mod_ty) has cases for expressions and interactive statements. |
Nick Coghlan | 944d3eb | 2005-11-16 12:46:55 +0000 | [diff] [blame] | 16 | * |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 17 | * CAUTION: The VISIT_* macros abort the current function when they |
| 18 | * encounter a problem. So don't invoke them when there is memory |
| 19 | * which needs to be released. Code blocks are OK, as the compiler |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 20 | * structure takes care of releasing those. Use the arena to manage |
| 21 | * objects. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | */ |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 23 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 24 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 26 | #include "Python-ast.h" |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 27 | #include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 28 | #include "ast.h" |
| 29 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 30 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 31 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 32 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 33 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 34 | #define DEFAULT_BLOCK_SIZE 16 |
| 35 | #define DEFAULT_BLOCKS 8 |
| 36 | #define DEFAULT_CODE_SIZE 128 |
| 37 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 38 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 39 | #define COMP_GENEXP 0 |
| 40 | #define COMP_LISTCOMP 1 |
| 41 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 42 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 43 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 44 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 45 | unsigned i_jabs : 1; |
| 46 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 47 | unsigned char i_opcode; |
| 48 | int i_oparg; |
| 49 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 50 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 51 | }; |
| 52 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 53 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 54 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 55 | reverse order that the block are allocated. b_list points to the next |
| 56 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 57 | struct basicblock_ *b_list; |
| 58 | /* number of instructions used */ |
| 59 | int b_iused; |
| 60 | /* length of instruction array (b_instr) */ |
| 61 | int b_ialloc; |
| 62 | /* pointer to an array of instructions, initially NULL */ |
| 63 | struct instr *b_instr; |
| 64 | /* If b_next is non-NULL, it is a pointer to the next |
| 65 | block reached by normal control flow. */ |
| 66 | struct basicblock_ *b_next; |
| 67 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 68 | unsigned b_seen : 1; |
| 69 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 70 | unsigned b_return : 1; |
| 71 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 72 | int b_startdepth; |
| 73 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 74 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 75 | } basicblock; |
| 76 | |
| 77 | /* fblockinfo tracks the current frame block. |
| 78 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 79 | A frame block is used to handle loops, try/except, and try/finally. |
| 80 | It's called a frame block to distinguish it from a basic block in the |
| 81 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 82 | */ |
| 83 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 84 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_TRY2, FINALLY_END, |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 85 | WITH, ASYNC_WITH, HANDLER_CLEANUP }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 86 | |
| 87 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | enum fblocktype fb_type; |
| 89 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 90 | /* (optional) type-specific exit or cleanup block */ |
| 91 | basicblock *fb_exit; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 94 | enum { |
| 95 | COMPILER_SCOPE_MODULE, |
| 96 | COMPILER_SCOPE_CLASS, |
| 97 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 98 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 99 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 100 | COMPILER_SCOPE_COMPREHENSION, |
| 101 | }; |
| 102 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 103 | /* The following items change on entry and exit of code blocks. |
| 104 | They must be saved and restored when returning to a block. |
| 105 | */ |
| 106 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 107 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 110 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 111 | int u_scope_type; |
| 112 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | /* The following fields are dicts that map objects to |
| 114 | the index of them in co_XXX. The index is used as |
| 115 | the argument for opcodes that refer to those collections. |
| 116 | */ |
| 117 | PyObject *u_consts; /* all constants */ |
| 118 | PyObject *u_names; /* all names */ |
| 119 | PyObject *u_varnames; /* local variables */ |
| 120 | PyObject *u_cellvars; /* cell variables */ |
| 121 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 122 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 123 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 124 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 125 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 126 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 127 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | /* Pointer to the most recently allocated block. By following b_list |
| 129 | members, you can reach all early allocated blocks. */ |
| 130 | basicblock *u_blocks; |
| 131 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 132 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | int u_nfblocks; |
| 134 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 135 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 136 | int u_firstlineno; /* the first lineno of the block */ |
| 137 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 138 | int u_col_offset; /* the offset of the current stmt */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | int u_lineno_set; /* boolean to indicate whether instr |
| 140 | has been generated with current lineno */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 141 | }; |
| 142 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 143 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 144 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 145 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | for enclosing blocks are stored in c_stack. The u and c_stack are |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 147 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 148 | |
| 149 | Note that we don't track recursion levels during compilation - the |
| 150 | task of detecting and rejecting excessive levels of nesting is |
| 151 | handled by the symbol analysis pass. |
| 152 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | */ |
| 154 | |
| 155 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 156 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 157 | struct symtable *c_st; |
| 158 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 159 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 160 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 161 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 162 | int c_interactive; /* true if in interactive mode */ |
| 163 | int c_nestlevel; |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 164 | int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode |
| 165 | if this value is different from zero. |
| 166 | This can be used to temporarily visit |
| 167 | nodes without emitting bytecode to |
| 168 | check only errors. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 169 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 170 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 171 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 172 | struct compiler_unit *u; /* compiler state for current block */ |
| 173 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 174 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 175 | }; |
| 176 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 177 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 178 | static void compiler_free(struct compiler *); |
| 179 | static basicblock *compiler_new_block(struct compiler *); |
| 180 | static int compiler_next_instr(struct compiler *, basicblock *); |
| 181 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 182 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 183 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 184 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 185 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 186 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 187 | |
| 188 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 189 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 190 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 191 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 192 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 193 | static int compiler_annassign(struct compiler *, stmt_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 194 | static int compiler_visit_slice(struct compiler *, slice_ty, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | expr_context_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 196 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 197 | static int inplace_binop(struct compiler *, operator_ty); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 198 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 199 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 200 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 201 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 202 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 203 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 205 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 206 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 207 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 208 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 209 | static int compiler_sync_comprehension_generator( |
| 210 | struct compiler *c, |
| 211 | asdl_seq *generators, int gen_index, |
| 212 | expr_ty elt, expr_ty val, int type); |
| 213 | |
| 214 | static int compiler_async_comprehension_generator( |
| 215 | struct compiler *c, |
| 216 | asdl_seq *generators, int gen_index, |
| 217 | expr_ty elt, expr_ty val, int type); |
| 218 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 219 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 220 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 221 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 222 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 223 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 224 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 225 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 227 | /* Name mangling: __private becomes _classname__private. |
| 228 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 229 | PyObject *result; |
| 230 | size_t nlen, plen, ipriv; |
| 231 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 233 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 234 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 235 | Py_INCREF(ident); |
| 236 | return ident; |
| 237 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 238 | nlen = PyUnicode_GET_LENGTH(ident); |
| 239 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 241 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | The only time a name with a dot can occur is when |
| 243 | we are compiling an import statement that has a |
| 244 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 245 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | TODO(jhylton): Decide whether we want to support |
| 247 | mangling of the module name, e.g. __M.X. |
| 248 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 249 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 250 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 251 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | Py_INCREF(ident); |
| 253 | return ident; /* Don't mangle __whatever__ */ |
| 254 | } |
| 255 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 256 | ipriv = 0; |
| 257 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 258 | ipriv++; |
| 259 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 260 | Py_INCREF(ident); |
| 261 | return ident; /* Don't mangle if class is just underscores */ |
| 262 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 263 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 264 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 265 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 266 | PyErr_SetString(PyExc_OverflowError, |
| 267 | "private identifier too large to be mangled"); |
| 268 | return NULL; |
| 269 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 270 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 271 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 272 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 273 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 274 | |
| 275 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 276 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 277 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 278 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 279 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 280 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 281 | Py_DECREF(result); |
| 282 | return NULL; |
| 283 | } |
| 284 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 285 | Py_DECREF(result); |
| 286 | return NULL; |
| 287 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 288 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 289 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 290 | } |
| 291 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 292 | static int |
| 293 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 294 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 295 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 296 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 297 | c->c_const_cache = PyDict_New(); |
| 298 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 299 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | c->c_stack = PyList_New(0); |
| 303 | if (!c->c_stack) { |
| 304 | Py_CLEAR(c->c_const_cache); |
| 305 | return 0; |
| 306 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 312 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 313 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 314 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 315 | struct compiler c; |
| 316 | PyCodeObject *co = NULL; |
Miss Islington (bot) | 92e836c | 2019-06-12 17:36:03 -0700 | [diff] [blame] | 317 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 318 | int merged; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 319 | PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 320 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | if (!__doc__) { |
| 322 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 323 | if (!__doc__) |
| 324 | return NULL; |
| 325 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 326 | if (!__annotations__) { |
| 327 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 328 | if (!__annotations__) |
| 329 | return NULL; |
| 330 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 331 | if (!compiler_init(&c)) |
| 332 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 333 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 334 | c.c_filename = filename; |
| 335 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 336 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 337 | if (c.c_future == NULL) |
| 338 | goto finally; |
| 339 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 340 | flags = &local_flags; |
| 341 | } |
| 342 | merged = c.c_future->ff_features | flags->cf_flags; |
| 343 | c.c_future->ff_features = merged; |
| 344 | flags->cf_flags = merged; |
| 345 | c.c_flags = flags; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 346 | c.c_optimize = (optimize == -1) ? config->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | c.c_nestlevel = 0; |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 348 | c.c_do_not_emit_bytecode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 349 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 350 | if (!_PyAST_Optimize(mod, arena, c.c_optimize)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 351 | goto finally; |
| 352 | } |
| 353 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 354 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 355 | if (c.c_st == NULL) { |
| 356 | if (!PyErr_Occurred()) |
| 357 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 358 | goto finally; |
| 359 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 360 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 362 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 363 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | compiler_free(&c); |
| 365 | assert(co || PyErr_Occurred()); |
| 366 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 367 | } |
| 368 | |
| 369 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 370 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 371 | int optimize, PyArena *arena) |
| 372 | { |
| 373 | PyObject *filename; |
| 374 | PyCodeObject *co; |
| 375 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 376 | if (filename == NULL) |
| 377 | return NULL; |
| 378 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 379 | Py_DECREF(filename); |
| 380 | return co; |
| 381 | |
| 382 | } |
| 383 | |
| 384 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 385 | PyNode_Compile(struct _node *n, const char *filename) |
| 386 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 387 | PyCodeObject *co = NULL; |
| 388 | mod_ty mod; |
| 389 | PyArena *arena = PyArena_New(); |
| 390 | if (!arena) |
| 391 | return NULL; |
| 392 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 393 | if (mod) |
| 394 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 395 | PyArena_Free(arena); |
| 396 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 399 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 400 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 402 | if (c->c_st) |
| 403 | PySymtable_Free(c->c_st); |
| 404 | if (c->c_future) |
| 405 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 406 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 407 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 408 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 409 | } |
| 410 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 411 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 412 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 413 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 414 | Py_ssize_t i, n; |
| 415 | PyObject *v, *k; |
| 416 | PyObject *dict = PyDict_New(); |
| 417 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 418 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | n = PyList_Size(list); |
| 420 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 421 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 422 | if (!v) { |
| 423 | Py_DECREF(dict); |
| 424 | return NULL; |
| 425 | } |
| 426 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 427 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | Py_DECREF(v); |
| 429 | Py_DECREF(dict); |
| 430 | return NULL; |
| 431 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 432 | Py_DECREF(v); |
| 433 | } |
| 434 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | /* Return new dict containing names from src that match scope(s). |
| 438 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 439 | src is a symbol table dictionary. If the scope of a name matches |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 440 | either scope_type or flag is set, insert it into the new dict. The |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 441 | values are integers, starting at offset and increasing by one for |
| 442 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 443 | */ |
| 444 | |
| 445 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 446 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 447 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 448 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 449 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 450 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 451 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 452 | assert(offset >= 0); |
| 453 | if (dest == NULL) |
| 454 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 455 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 456 | /* Sort the keys so that we have a deterministic order on the indexes |
| 457 | saved in the returned dictionary. These indexes are used as indexes |
| 458 | into the free and cell var storage. Therefore if they aren't |
| 459 | deterministic, then the generated bytecode is not deterministic. |
| 460 | */ |
| 461 | sorted_keys = PyDict_Keys(src); |
| 462 | if (sorted_keys == NULL) |
| 463 | return NULL; |
| 464 | if (PyList_Sort(sorted_keys) != 0) { |
| 465 | Py_DECREF(sorted_keys); |
| 466 | return NULL; |
| 467 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 468 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 469 | |
| 470 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 471 | /* XXX this should probably be a macro in symtable.h */ |
| 472 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 473 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 474 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 475 | assert(PyLong_Check(v)); |
| 476 | vi = PyLong_AS_LONG(v); |
| 477 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 479 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 480 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 481 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 482 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 483 | Py_DECREF(dest); |
| 484 | return NULL; |
| 485 | } |
| 486 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 487 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 488 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 489 | Py_DECREF(item); |
| 490 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 491 | return NULL; |
| 492 | } |
| 493 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | } |
| 495 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 496 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 497 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 498 | } |
| 499 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 500 | static void |
| 501 | compiler_unit_check(struct compiler_unit *u) |
| 502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | basicblock *block; |
| 504 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 505 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 506 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 507 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | if (block->b_instr != NULL) { |
| 509 | assert(block->b_ialloc > 0); |
| 510 | assert(block->b_iused > 0); |
| 511 | assert(block->b_ialloc >= block->b_iused); |
| 512 | } |
| 513 | else { |
| 514 | assert (block->b_iused == 0); |
| 515 | assert (block->b_ialloc == 0); |
| 516 | } |
| 517 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 518 | } |
| 519 | |
| 520 | static void |
| 521 | compiler_unit_free(struct compiler_unit *u) |
| 522 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 524 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | compiler_unit_check(u); |
| 526 | b = u->u_blocks; |
| 527 | while (b != NULL) { |
| 528 | if (b->b_instr) |
| 529 | PyObject_Free((void *)b->b_instr); |
| 530 | next = b->b_list; |
| 531 | PyObject_Free((void *)b); |
| 532 | b = next; |
| 533 | } |
| 534 | Py_CLEAR(u->u_ste); |
| 535 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 536 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 537 | Py_CLEAR(u->u_consts); |
| 538 | Py_CLEAR(u->u_names); |
| 539 | Py_CLEAR(u->u_varnames); |
| 540 | Py_CLEAR(u->u_freevars); |
| 541 | Py_CLEAR(u->u_cellvars); |
| 542 | Py_CLEAR(u->u_private); |
| 543 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 544 | } |
| 545 | |
| 546 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 547 | compiler_enter_scope(struct compiler *c, identifier name, |
| 548 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 550 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 551 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 552 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 553 | u = (struct compiler_unit *)PyObject_Malloc(sizeof( |
| 554 | struct compiler_unit)); |
| 555 | if (!u) { |
| 556 | PyErr_NoMemory(); |
| 557 | return 0; |
| 558 | } |
| 559 | memset(u, 0, sizeof(struct compiler_unit)); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 560 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 562 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | u->u_kwonlyargcount = 0; |
| 564 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 565 | if (!u->u_ste) { |
| 566 | compiler_unit_free(u); |
| 567 | return 0; |
| 568 | } |
| 569 | Py_INCREF(name); |
| 570 | u->u_name = name; |
| 571 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 572 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 573 | if (!u->u_varnames || !u->u_cellvars) { |
| 574 | compiler_unit_free(u); |
| 575 | return 0; |
| 576 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 577 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 578 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 579 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 580 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 581 | int res; |
| 582 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 583 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 584 | name = _PyUnicode_FromId(&PyId___class__); |
| 585 | if (!name) { |
| 586 | compiler_unit_free(u); |
| 587 | return 0; |
| 588 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 589 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 590 | if (res < 0) { |
| 591 | compiler_unit_free(u); |
| 592 | return 0; |
| 593 | } |
| 594 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 595 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 596 | u->u_freevars = dictbytype(u->u_ste->ste_symbols, FREE, DEF_FREE_CLASS, |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 597 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 598 | if (!u->u_freevars) { |
| 599 | compiler_unit_free(u); |
| 600 | return 0; |
| 601 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 603 | u->u_blocks = NULL; |
| 604 | u->u_nfblocks = 0; |
| 605 | u->u_firstlineno = lineno; |
| 606 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 607 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | u->u_lineno_set = 0; |
| 609 | u->u_consts = PyDict_New(); |
| 610 | if (!u->u_consts) { |
| 611 | compiler_unit_free(u); |
| 612 | return 0; |
| 613 | } |
| 614 | u->u_names = PyDict_New(); |
| 615 | if (!u->u_names) { |
| 616 | compiler_unit_free(u); |
| 617 | return 0; |
| 618 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 620 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 622 | /* Push the old compiler_unit on the stack. */ |
| 623 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 624 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 625 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 626 | Py_XDECREF(capsule); |
| 627 | compiler_unit_free(u); |
| 628 | return 0; |
| 629 | } |
| 630 | Py_DECREF(capsule); |
| 631 | u->u_private = c->u->u_private; |
| 632 | Py_XINCREF(u->u_private); |
| 633 | } |
| 634 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 635 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 637 | |
| 638 | block = compiler_new_block(c); |
| 639 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 640 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 641 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 642 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 643 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 644 | if (!compiler_set_qualname(c)) |
| 645 | return 0; |
| 646 | } |
| 647 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 648 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 651 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 652 | compiler_exit_scope(struct compiler *c) |
| 653 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 654 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | c->c_nestlevel--; |
| 658 | compiler_unit_free(c->u); |
| 659 | /* Restore c->u to the parent unit. */ |
| 660 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 661 | if (n >= 0) { |
| 662 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 663 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | assert(c->u); |
| 665 | /* we are deleting from a list so this really shouldn't fail */ |
| 666 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 667 | Py_FatalError("compiler_exit_scope()"); |
| 668 | compiler_unit_check(c->u); |
| 669 | } |
| 670 | else |
| 671 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 672 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 673 | } |
| 674 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 675 | static int |
| 676 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 677 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 678 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 679 | _Py_static_string(dot_locals, ".<locals>"); |
| 680 | Py_ssize_t stack_size; |
| 681 | struct compiler_unit *u = c->u; |
| 682 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 683 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 684 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 685 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 686 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 687 | if (stack_size > 1) { |
| 688 | int scope, force_global = 0; |
| 689 | struct compiler_unit *parent; |
| 690 | PyObject *mangled, *capsule; |
| 691 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 692 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 693 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 694 | assert(parent); |
| 695 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 696 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 697 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 698 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 699 | assert(u->u_name); |
| 700 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 701 | if (!mangled) |
| 702 | return 0; |
| 703 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 704 | Py_DECREF(mangled); |
| 705 | assert(scope != GLOBAL_IMPLICIT); |
| 706 | if (scope == GLOBAL_EXPLICIT) |
| 707 | force_global = 1; |
| 708 | } |
| 709 | |
| 710 | if (!force_global) { |
| 711 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 712 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 713 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 714 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 715 | if (dot_locals_str == NULL) |
| 716 | return 0; |
| 717 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 718 | if (base == NULL) |
| 719 | return 0; |
| 720 | } |
| 721 | else { |
| 722 | Py_INCREF(parent->u_qualname); |
| 723 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 724 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 725 | } |
| 726 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 727 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 728 | if (base != NULL) { |
| 729 | dot_str = _PyUnicode_FromId(&dot); |
| 730 | if (dot_str == NULL) { |
| 731 | Py_DECREF(base); |
| 732 | return 0; |
| 733 | } |
| 734 | name = PyUnicode_Concat(base, dot_str); |
| 735 | Py_DECREF(base); |
| 736 | if (name == NULL) |
| 737 | return 0; |
| 738 | PyUnicode_Append(&name, u->u_name); |
| 739 | if (name == NULL) |
| 740 | return 0; |
| 741 | } |
| 742 | else { |
| 743 | Py_INCREF(u->u_name); |
| 744 | name = u->u_name; |
| 745 | } |
| 746 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 747 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 748 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 749 | } |
| 750 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 751 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 752 | /* Allocate a new block and return a pointer to it. |
| 753 | Returns NULL on error. |
| 754 | */ |
| 755 | |
| 756 | static basicblock * |
| 757 | compiler_new_block(struct compiler *c) |
| 758 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | basicblock *b; |
| 760 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 761 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 762 | u = c->u; |
| 763 | b = (basicblock *)PyObject_Malloc(sizeof(basicblock)); |
| 764 | if (b == NULL) { |
| 765 | PyErr_NoMemory(); |
| 766 | return NULL; |
| 767 | } |
| 768 | memset((void *)b, 0, sizeof(basicblock)); |
| 769 | /* Extend the singly linked list of blocks with new block. */ |
| 770 | b->b_list = u->u_blocks; |
| 771 | u->u_blocks = b; |
| 772 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 775 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 776 | compiler_next_block(struct compiler *c) |
| 777 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 778 | basicblock *block = compiler_new_block(c); |
| 779 | if (block == NULL) |
| 780 | return NULL; |
| 781 | c->u->u_curblock->b_next = block; |
| 782 | c->u->u_curblock = block; |
| 783 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | } |
| 785 | |
| 786 | static basicblock * |
| 787 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 789 | assert(block != NULL); |
| 790 | c->u->u_curblock->b_next = block; |
| 791 | c->u->u_curblock = block; |
| 792 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 793 | } |
| 794 | |
| 795 | /* Returns the offset of the next instruction in the current block's |
| 796 | b_instr array. Resizes the b_instr as necessary. |
| 797 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 798 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | |
| 800 | static int |
| 801 | compiler_next_instr(struct compiler *c, basicblock *b) |
| 802 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 803 | assert(b != NULL); |
| 804 | if (b->b_instr == NULL) { |
| 805 | b->b_instr = (struct instr *)PyObject_Malloc( |
| 806 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 807 | if (b->b_instr == NULL) { |
| 808 | PyErr_NoMemory(); |
| 809 | return -1; |
| 810 | } |
| 811 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
| 812 | memset((char *)b->b_instr, 0, |
| 813 | sizeof(struct instr) * DEFAULT_BLOCK_SIZE); |
| 814 | } |
| 815 | else if (b->b_iused == b->b_ialloc) { |
| 816 | struct instr *tmp; |
| 817 | size_t oldsize, newsize; |
| 818 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 819 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 820 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 821 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 822 | PyErr_NoMemory(); |
| 823 | return -1; |
| 824 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | if (newsize == 0) { |
| 827 | PyErr_NoMemory(); |
| 828 | return -1; |
| 829 | } |
| 830 | b->b_ialloc <<= 1; |
| 831 | tmp = (struct instr *)PyObject_Realloc( |
| 832 | (void *)b->b_instr, newsize); |
| 833 | if (tmp == NULL) { |
| 834 | PyErr_NoMemory(); |
| 835 | return -1; |
| 836 | } |
| 837 | b->b_instr = tmp; |
| 838 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 839 | } |
| 840 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 841 | } |
| 842 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 843 | /* Set the i_lineno member of the instruction at offset off if the |
| 844 | line number for the current expression/statement has not |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 845 | already been set. If it has been set, the call has no effect. |
| 846 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 847 | The line number is reset in the following cases: |
| 848 | - when entering a new scope |
| 849 | - on each statement |
| 850 | - on each expression that start a new line |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 851 | - before the "except" and "finally" clauses |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 852 | - before the "for" and "while" expressions |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 853 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 854 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 855 | static void |
| 856 | compiler_set_lineno(struct compiler *c, int off) |
| 857 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | basicblock *b; |
| 859 | if (c->u->u_lineno_set) |
| 860 | return; |
| 861 | c->u->u_lineno_set = 1; |
| 862 | b = c->u->u_curblock; |
| 863 | b->b_instr[off].i_lineno = c->u->u_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 864 | } |
| 865 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 866 | /* Return the stack effect of opcode with argument oparg. |
| 867 | |
| 868 | Some opcodes have different stack effect when jump to the target and |
| 869 | when not jump. The 'jump' parameter specifies the case: |
| 870 | |
| 871 | * 0 -- when not jump |
| 872 | * 1 -- when jump |
| 873 | * -1 -- maximal |
| 874 | */ |
| 875 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 876 | WITH_CLEANUP_FINISH deterministic. */ |
| 877 | static int |
| 878 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 879 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 881 | case NOP: |
| 882 | case EXTENDED_ARG: |
| 883 | return 0; |
| 884 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 885 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 886 | case POP_TOP: |
| 887 | return -1; |
| 888 | case ROT_TWO: |
| 889 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 890 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 891 | return 0; |
| 892 | case DUP_TOP: |
| 893 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 894 | case DUP_TOP_TWO: |
| 895 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 896 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 897 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | case UNARY_POSITIVE: |
| 899 | case UNARY_NEGATIVE: |
| 900 | case UNARY_NOT: |
| 901 | case UNARY_INVERT: |
| 902 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 903 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 904 | case SET_ADD: |
| 905 | case LIST_APPEND: |
| 906 | return -1; |
| 907 | case MAP_ADD: |
| 908 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 909 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 910 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 911 | case BINARY_POWER: |
| 912 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 913 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 914 | case BINARY_MODULO: |
| 915 | case BINARY_ADD: |
| 916 | case BINARY_SUBTRACT: |
| 917 | case BINARY_SUBSCR: |
| 918 | case BINARY_FLOOR_DIVIDE: |
| 919 | case BINARY_TRUE_DIVIDE: |
| 920 | return -1; |
| 921 | case INPLACE_FLOOR_DIVIDE: |
| 922 | case INPLACE_TRUE_DIVIDE: |
| 923 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 924 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | case INPLACE_ADD: |
| 926 | case INPLACE_SUBTRACT: |
| 927 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 928 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | case INPLACE_MODULO: |
| 930 | return -1; |
| 931 | case STORE_SUBSCR: |
| 932 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 933 | case DELETE_SUBSCR: |
| 934 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 935 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 936 | case BINARY_LSHIFT: |
| 937 | case BINARY_RSHIFT: |
| 938 | case BINARY_AND: |
| 939 | case BINARY_XOR: |
| 940 | case BINARY_OR: |
| 941 | return -1; |
| 942 | case INPLACE_POWER: |
| 943 | return -1; |
| 944 | case GET_ITER: |
| 945 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 946 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 947 | case PRINT_EXPR: |
| 948 | return -1; |
| 949 | case LOAD_BUILD_CLASS: |
| 950 | return 1; |
| 951 | case INPLACE_LSHIFT: |
| 952 | case INPLACE_RSHIFT: |
| 953 | case INPLACE_AND: |
| 954 | case INPLACE_XOR: |
| 955 | case INPLACE_OR: |
| 956 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 957 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 959 | /* 1 in the normal flow. |
| 960 | * Restore the stack position and push 6 values before jumping to |
| 961 | * the handler if an exception be raised. */ |
| 962 | return jump ? 6 : 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 963 | case WITH_CLEANUP_START: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 964 | return 2; /* or 1, depending on TOS */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 965 | case WITH_CLEANUP_FINISH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 966 | /* Pop a variable number of values pushed by WITH_CLEANUP_START |
| 967 | * + __exit__ or __aexit__. */ |
| 968 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | case RETURN_VALUE: |
| 970 | return -1; |
| 971 | case IMPORT_STAR: |
| 972 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 973 | case SETUP_ANNOTATIONS: |
| 974 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 975 | case YIELD_VALUE: |
| 976 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 977 | case YIELD_FROM: |
| 978 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 979 | case POP_BLOCK: |
| 980 | return 0; |
| 981 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 982 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 983 | case END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 984 | case POP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 985 | /* Pop 6 values when an exception was raised. */ |
| 986 | return -6; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 987 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 988 | case STORE_NAME: |
| 989 | return -1; |
| 990 | case DELETE_NAME: |
| 991 | return 0; |
| 992 | case UNPACK_SEQUENCE: |
| 993 | return oparg-1; |
| 994 | case UNPACK_EX: |
| 995 | return (oparg&0xFF) + (oparg>>8); |
| 996 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 997 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 998 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 999 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | case STORE_ATTR: |
| 1001 | return -2; |
| 1002 | case DELETE_ATTR: |
| 1003 | return -1; |
| 1004 | case STORE_GLOBAL: |
| 1005 | return -1; |
| 1006 | case DELETE_GLOBAL: |
| 1007 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1008 | case LOAD_CONST: |
| 1009 | return 1; |
| 1010 | case LOAD_NAME: |
| 1011 | return 1; |
| 1012 | case BUILD_TUPLE: |
| 1013 | case BUILD_LIST: |
| 1014 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1015 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1016 | return 1-oparg; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1017 | case BUILD_LIST_UNPACK: |
| 1018 | case BUILD_TUPLE_UNPACK: |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 1019 | case BUILD_TUPLE_UNPACK_WITH_CALL: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1020 | case BUILD_SET_UNPACK: |
| 1021 | case BUILD_MAP_UNPACK: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1022 | case BUILD_MAP_UNPACK_WITH_CALL: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1023 | return 1 - oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1025 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1026 | case BUILD_CONST_KEY_MAP: |
| 1027 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1028 | case LOAD_ATTR: |
| 1029 | return 0; |
| 1030 | case COMPARE_OP: |
| 1031 | return -1; |
| 1032 | case IMPORT_NAME: |
| 1033 | return -1; |
| 1034 | case IMPORT_FROM: |
| 1035 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1036 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1037 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1038 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1039 | case JUMP_ABSOLUTE: |
| 1040 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1041 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1042 | case JUMP_IF_TRUE_OR_POP: |
| 1043 | case JUMP_IF_FALSE_OR_POP: |
| 1044 | return jump ? 0 : -1; |
| 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | case POP_JUMP_IF_FALSE: |
| 1047 | case POP_JUMP_IF_TRUE: |
| 1048 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1049 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1050 | case LOAD_GLOBAL: |
| 1051 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1052 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1053 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1054 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1055 | /* 0 in the normal flow. |
| 1056 | * Restore the stack position and push 6 values before jumping to |
| 1057 | * the handler if an exception be raised. */ |
| 1058 | return jump ? 6 : 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1059 | case BEGIN_FINALLY: |
| 1060 | /* Actually pushes 1 value, but count 6 for balancing with |
| 1061 | * END_FINALLY and POP_FINALLY. |
| 1062 | * This is the main reason of using this opcode instead of |
| 1063 | * "LOAD_CONST None". */ |
| 1064 | return 6; |
| 1065 | case CALL_FINALLY: |
| 1066 | return jump ? 1 : 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | case LOAD_FAST: |
| 1069 | return 1; |
| 1070 | case STORE_FAST: |
| 1071 | return -1; |
| 1072 | case DELETE_FAST: |
| 1073 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1074 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case RAISE_VARARGS: |
| 1076 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1077 | |
| 1078 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1080 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1081 | case CALL_METHOD: |
| 1082 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1084 | return -oparg-1; |
| 1085 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1086 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1087 | case MAKE_FUNCTION: |
| 1088 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1089 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1090 | case BUILD_SLICE: |
| 1091 | if (oparg == 3) |
| 1092 | return -2; |
| 1093 | else |
| 1094 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1095 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1096 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1097 | case LOAD_CLOSURE: |
| 1098 | return 1; |
| 1099 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1100 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1101 | return 1; |
| 1102 | case STORE_DEREF: |
| 1103 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1104 | case DELETE_DEREF: |
| 1105 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1106 | |
| 1107 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1108 | case GET_AWAITABLE: |
| 1109 | return 0; |
| 1110 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1111 | /* 0 in the normal flow. |
| 1112 | * Restore the stack position to the position before the result |
| 1113 | * of __aenter__ and push 6 values before jumping to the handler |
| 1114 | * if an exception be raised. */ |
| 1115 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1116 | case BEFORE_ASYNC_WITH: |
| 1117 | return 1; |
| 1118 | case GET_AITER: |
| 1119 | return 0; |
| 1120 | case GET_ANEXT: |
| 1121 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1122 | case GET_YIELD_FROM_ITER: |
| 1123 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1124 | case END_ASYNC_FOR: |
| 1125 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1126 | case FORMAT_VALUE: |
| 1127 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1128 | else 1->1. */ |
| 1129 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1130 | case LOAD_METHOD: |
| 1131 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1132 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1133 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1135 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1136 | } |
| 1137 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1138 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1139 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1140 | { |
| 1141 | return stack_effect(opcode, oparg, jump); |
| 1142 | } |
| 1143 | |
| 1144 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1145 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1146 | { |
| 1147 | return stack_effect(opcode, oparg, -1); |
| 1148 | } |
| 1149 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1150 | /* Add an opcode with no argument. |
| 1151 | Returns 0 on failure, 1 on success. |
| 1152 | */ |
| 1153 | |
| 1154 | static int |
| 1155 | compiler_addop(struct compiler *c, int opcode) |
| 1156 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1157 | basicblock *b; |
| 1158 | struct instr *i; |
| 1159 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1160 | assert(!HAS_ARG(opcode)); |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1161 | if (c->c_do_not_emit_bytecode) { |
| 1162 | return 1; |
| 1163 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1164 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1165 | if (off < 0) |
| 1166 | return 0; |
| 1167 | b = c->u->u_curblock; |
| 1168 | i = &b->b_instr[off]; |
| 1169 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1170 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | if (opcode == RETURN_VALUE) |
| 1172 | b->b_return = 1; |
| 1173 | compiler_set_lineno(c, off); |
| 1174 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1177 | static Py_ssize_t |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | compiler_add_o(struct compiler *c, PyObject *dict, PyObject *o) |
| 1179 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1180 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1182 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1183 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1184 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1185 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1186 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1187 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1188 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1189 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1190 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1191 | return -1; |
| 1192 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1193 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1194 | Py_DECREF(v); |
| 1195 | return -1; |
| 1196 | } |
| 1197 | Py_DECREF(v); |
| 1198 | } |
| 1199 | else |
| 1200 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1201 | return arg; |
| 1202 | } |
| 1203 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1204 | // Merge const *o* recursively and return constant key object. |
| 1205 | static PyObject* |
| 1206 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1207 | { |
| 1208 | // None and Ellipsis are singleton, and key is the singleton. |
| 1209 | // No need to merge object and key. |
| 1210 | if (o == Py_None || o == Py_Ellipsis) { |
| 1211 | Py_INCREF(o); |
| 1212 | return o; |
| 1213 | } |
| 1214 | |
| 1215 | PyObject *key = _PyCode_ConstantKey(o); |
| 1216 | if (key == NULL) { |
| 1217 | return NULL; |
| 1218 | } |
| 1219 | |
| 1220 | // t is borrowed reference |
| 1221 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1222 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1223 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1224 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1225 | Py_DECREF(key); |
| 1226 | return t; |
| 1227 | } |
| 1228 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1229 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1230 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1231 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1232 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1233 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1234 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1235 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1236 | PyObject *u = merge_consts_recursive(c, item); |
| 1237 | if (u == NULL) { |
| 1238 | Py_DECREF(key); |
| 1239 | return NULL; |
| 1240 | } |
| 1241 | |
| 1242 | // See _PyCode_ConstantKey() |
| 1243 | PyObject *v; // borrowed |
| 1244 | if (PyTuple_CheckExact(u)) { |
| 1245 | v = PyTuple_GET_ITEM(u, 1); |
| 1246 | } |
| 1247 | else { |
| 1248 | v = u; |
| 1249 | } |
| 1250 | if (v != item) { |
| 1251 | Py_INCREF(v); |
| 1252 | PyTuple_SET_ITEM(o, i, v); |
| 1253 | Py_DECREF(item); |
| 1254 | } |
| 1255 | |
| 1256 | Py_DECREF(u); |
| 1257 | } |
| 1258 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1259 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1260 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1261 | // constant keys. |
| 1262 | // See _PyCode_ConstantKey() for detail. |
| 1263 | assert(PyTuple_CheckExact(key)); |
| 1264 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1265 | |
| 1266 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1267 | if (len == 0) { // empty frozenset should not be re-created. |
| 1268 | return key; |
| 1269 | } |
| 1270 | PyObject *tuple = PyTuple_New(len); |
| 1271 | if (tuple == NULL) { |
| 1272 | Py_DECREF(key); |
| 1273 | return NULL; |
| 1274 | } |
| 1275 | Py_ssize_t i = 0, pos = 0; |
| 1276 | PyObject *item; |
| 1277 | Py_hash_t hash; |
| 1278 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1279 | PyObject *k = merge_consts_recursive(c, item); |
| 1280 | if (k == NULL) { |
| 1281 | Py_DECREF(tuple); |
| 1282 | Py_DECREF(key); |
| 1283 | return NULL; |
| 1284 | } |
| 1285 | PyObject *u; |
| 1286 | if (PyTuple_CheckExact(k)) { |
| 1287 | u = PyTuple_GET_ITEM(k, 1); |
| 1288 | Py_INCREF(u); |
| 1289 | Py_DECREF(k); |
| 1290 | } |
| 1291 | else { |
| 1292 | u = k; |
| 1293 | } |
| 1294 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1295 | i++; |
| 1296 | } |
| 1297 | |
| 1298 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1299 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1300 | PyObject *new = PyFrozenSet_New(tuple); |
| 1301 | Py_DECREF(tuple); |
| 1302 | if (new == NULL) { |
| 1303 | Py_DECREF(key); |
| 1304 | return NULL; |
| 1305 | } |
| 1306 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1307 | Py_DECREF(o); |
| 1308 | PyTuple_SET_ITEM(key, 1, new); |
| 1309 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1310 | |
| 1311 | return key; |
| 1312 | } |
| 1313 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1314 | static Py_ssize_t |
| 1315 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1316 | { |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1317 | if (c->c_do_not_emit_bytecode) { |
| 1318 | return 0; |
| 1319 | } |
| 1320 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1321 | PyObject *key = merge_consts_recursive(c, o); |
| 1322 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1323 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1324 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1325 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1326 | Py_ssize_t arg = compiler_add_o(c, c->u->u_consts, key); |
| 1327 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1328 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1332 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1333 | { |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1334 | if (c->c_do_not_emit_bytecode) { |
| 1335 | return 1; |
| 1336 | } |
| 1337 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1338 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1339 | if (arg < 0) |
| 1340 | return 0; |
| 1341 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1342 | } |
| 1343 | |
| 1344 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1345 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1346 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1347 | { |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1348 | if (c->c_do_not_emit_bytecode) { |
| 1349 | return 1; |
| 1350 | } |
| 1351 | |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1352 | Py_ssize_t arg = compiler_add_o(c, dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1353 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1354 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1355 | return compiler_addop_i(c, opcode, arg); |
| 1356 | } |
| 1357 | |
| 1358 | static int |
| 1359 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1360 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1361 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1362 | Py_ssize_t arg; |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1363 | |
| 1364 | if (c->c_do_not_emit_bytecode) { |
| 1365 | return 1; |
| 1366 | } |
| 1367 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1368 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1369 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1370 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1371 | arg = compiler_add_o(c, dict, mangled); |
| 1372 | Py_DECREF(mangled); |
| 1373 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1374 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1375 | return compiler_addop_i(c, opcode, arg); |
| 1376 | } |
| 1377 | |
| 1378 | /* Add an opcode with an integer argument. |
| 1379 | Returns 0 on failure, 1 on success. |
| 1380 | */ |
| 1381 | |
| 1382 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1383 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1384 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1385 | struct instr *i; |
| 1386 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1387 | |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1388 | if (c->c_do_not_emit_bytecode) { |
| 1389 | return 1; |
| 1390 | } |
| 1391 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1392 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1393 | it in the C code (like Python/ceval.c). |
| 1394 | |
| 1395 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1396 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1397 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1398 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1399 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1400 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1403 | if (off < 0) |
| 1404 | return 0; |
| 1405 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1406 | i->i_opcode = opcode; |
| 1407 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1408 | compiler_set_lineno(c, off); |
| 1409 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1410 | } |
| 1411 | |
| 1412 | static int |
| 1413 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | struct instr *i; |
| 1416 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1417 | |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1418 | if (c->c_do_not_emit_bytecode) { |
| 1419 | return 1; |
| 1420 | } |
| 1421 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1422 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1423 | assert(b != NULL); |
| 1424 | off = compiler_next_instr(c, c->u->u_curblock); |
| 1425 | if (off < 0) |
| 1426 | return 0; |
| 1427 | i = &c->u->u_curblock->b_instr[off]; |
| 1428 | i->i_opcode = opcode; |
| 1429 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1430 | if (absolute) |
| 1431 | i->i_jabs = 1; |
| 1432 | else |
| 1433 | i->i_jrel = 1; |
| 1434 | compiler_set_lineno(c, off); |
| 1435 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1438 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1439 | to the new block. |
| 1440 | |
| 1441 | The returns inside this macro make it impossible to decref objects |
| 1442 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1443 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1444 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1445 | if (compiler_next_block((C)) == NULL) \ |
| 1446 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1447 | } |
| 1448 | |
| 1449 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1450 | if (!compiler_addop((C), (OP))) \ |
| 1451 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1452 | } |
| 1453 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1454 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1455 | if (!compiler_addop((C), (OP))) { \ |
| 1456 | compiler_exit_scope(c); \ |
| 1457 | return 0; \ |
| 1458 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1461 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1462 | if (!compiler_addop_load_const((C), (O))) \ |
| 1463 | return 0; \ |
| 1464 | } |
| 1465 | |
| 1466 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1467 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1468 | PyObject *__new_const = (O); \ |
| 1469 | if (__new_const == NULL) { \ |
| 1470 | return 0; \ |
| 1471 | } \ |
| 1472 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1473 | Py_DECREF(__new_const); \ |
| 1474 | return 0; \ |
| 1475 | } \ |
| 1476 | Py_DECREF(__new_const); \ |
| 1477 | } |
| 1478 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1479 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1480 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1481 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1482 | } |
| 1483 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1484 | /* Same as ADDOP_O, but steals a reference. */ |
| 1485 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1486 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1487 | Py_DECREF((O)); \ |
| 1488 | return 0; \ |
| 1489 | } \ |
| 1490 | Py_DECREF((O)); \ |
| 1491 | } |
| 1492 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1494 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1495 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1496 | } |
| 1497 | |
| 1498 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1499 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1500 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1504 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1505 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | } |
| 1507 | |
| 1508 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1509 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1510 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1511 | } |
| 1512 | |
| 1513 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1514 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1515 | */ |
| 1516 | |
| 1517 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1519 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1522 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1524 | compiler_exit_scope(c); \ |
| 1525 | return 0; \ |
| 1526 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1529 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1531 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1532 | } |
| 1533 | |
| 1534 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1535 | int _i; \ |
| 1536 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1537 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1538 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1539 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1540 | return 0; \ |
| 1541 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1542 | } |
| 1543 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1544 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1545 | int _i; \ |
| 1546 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1547 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1548 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1549 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1550 | compiler_exit_scope(c); \ |
| 1551 | return 0; \ |
| 1552 | } \ |
| 1553 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1554 | } |
| 1555 | |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 1556 | /* These macros allows to check only for errors and not emmit bytecode |
| 1557 | * while visiting nodes. |
| 1558 | */ |
| 1559 | |
| 1560 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1561 | c->c_do_not_emit_bytecode++; |
| 1562 | |
| 1563 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1564 | c->c_do_not_emit_bytecode--; \ |
| 1565 | } |
| 1566 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1567 | /* Search if variable annotations are present statically in a block. */ |
| 1568 | |
| 1569 | static int |
| 1570 | find_ann(asdl_seq *stmts) |
| 1571 | { |
| 1572 | int i, j, res = 0; |
| 1573 | stmt_ty st; |
| 1574 | |
| 1575 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1576 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1577 | switch (st->kind) { |
| 1578 | case AnnAssign_kind: |
| 1579 | return 1; |
| 1580 | case For_kind: |
| 1581 | res = find_ann(st->v.For.body) || |
| 1582 | find_ann(st->v.For.orelse); |
| 1583 | break; |
| 1584 | case AsyncFor_kind: |
| 1585 | res = find_ann(st->v.AsyncFor.body) || |
| 1586 | find_ann(st->v.AsyncFor.orelse); |
| 1587 | break; |
| 1588 | case While_kind: |
| 1589 | res = find_ann(st->v.While.body) || |
| 1590 | find_ann(st->v.While.orelse); |
| 1591 | break; |
| 1592 | case If_kind: |
| 1593 | res = find_ann(st->v.If.body) || |
| 1594 | find_ann(st->v.If.orelse); |
| 1595 | break; |
| 1596 | case With_kind: |
| 1597 | res = find_ann(st->v.With.body); |
| 1598 | break; |
| 1599 | case AsyncWith_kind: |
| 1600 | res = find_ann(st->v.AsyncWith.body); |
| 1601 | break; |
| 1602 | case Try_kind: |
| 1603 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1604 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1605 | st->v.Try.handlers, j); |
| 1606 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1607 | return 1; |
| 1608 | } |
| 1609 | } |
| 1610 | res = find_ann(st->v.Try.body) || |
| 1611 | find_ann(st->v.Try.finalbody) || |
| 1612 | find_ann(st->v.Try.orelse); |
| 1613 | break; |
| 1614 | default: |
| 1615 | res = 0; |
| 1616 | } |
| 1617 | if (res) { |
| 1618 | break; |
| 1619 | } |
| 1620 | } |
| 1621 | return res; |
| 1622 | } |
| 1623 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1624 | /* |
| 1625 | * Frame block handling functions |
| 1626 | */ |
| 1627 | |
| 1628 | static int |
| 1629 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
| 1630 | basicblock *exit) |
| 1631 | { |
| 1632 | struct fblockinfo *f; |
| 1633 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1634 | PyErr_SetString(PyExc_SyntaxError, |
| 1635 | "too many statically nested blocks"); |
| 1636 | return 0; |
| 1637 | } |
| 1638 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1639 | f->fb_type = t; |
| 1640 | f->fb_block = b; |
| 1641 | f->fb_exit = exit; |
| 1642 | return 1; |
| 1643 | } |
| 1644 | |
| 1645 | static void |
| 1646 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1647 | { |
| 1648 | struct compiler_unit *u = c->u; |
| 1649 | assert(u->u_nfblocks > 0); |
| 1650 | u->u_nfblocks--; |
| 1651 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1652 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1653 | } |
| 1654 | |
| 1655 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
| 1656 | * popping the blocks will be restored afterwards. |
| 1657 | */ |
| 1658 | static int |
| 1659 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1660 | int preserve_tos) |
| 1661 | { |
| 1662 | switch (info->fb_type) { |
| 1663 | case WHILE_LOOP: |
| 1664 | return 1; |
| 1665 | |
| 1666 | case FINALLY_END: |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 1667 | info->fb_exit = NULL; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1668 | ADDOP_I(c, POP_FINALLY, preserve_tos); |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 1669 | if (preserve_tos) { |
| 1670 | ADDOP(c, ROT_TWO); |
| 1671 | } |
| 1672 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1673 | return 1; |
| 1674 | |
| 1675 | case FOR_LOOP: |
| 1676 | /* Pop the iterator */ |
| 1677 | if (preserve_tos) { |
| 1678 | ADDOP(c, ROT_TWO); |
| 1679 | } |
| 1680 | ADDOP(c, POP_TOP); |
| 1681 | return 1; |
| 1682 | |
| 1683 | case EXCEPT: |
| 1684 | ADDOP(c, POP_BLOCK); |
| 1685 | return 1; |
| 1686 | |
| 1687 | case FINALLY_TRY: |
| 1688 | ADDOP(c, POP_BLOCK); |
| 1689 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1690 | return 1; |
| 1691 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 1692 | case FINALLY_TRY2: |
| 1693 | ADDOP(c, POP_BLOCK); |
| 1694 | if (preserve_tos) { |
| 1695 | ADDOP(c, ROT_TWO); |
| 1696 | ADDOP(c, POP_TOP); |
| 1697 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1698 | } |
| 1699 | else { |
| 1700 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1701 | ADDOP(c, POP_TOP); |
| 1702 | } |
| 1703 | return 1; |
| 1704 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1705 | case WITH: |
| 1706 | case ASYNC_WITH: |
| 1707 | ADDOP(c, POP_BLOCK); |
| 1708 | if (preserve_tos) { |
| 1709 | ADDOP(c, ROT_TWO); |
| 1710 | } |
| 1711 | ADDOP(c, BEGIN_FINALLY); |
| 1712 | ADDOP(c, WITH_CLEANUP_START); |
| 1713 | if (info->fb_type == ASYNC_WITH) { |
| 1714 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1715 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1716 | ADDOP(c, YIELD_FROM); |
| 1717 | } |
| 1718 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 1719 | ADDOP_I(c, POP_FINALLY, 0); |
| 1720 | return 1; |
| 1721 | |
| 1722 | case HANDLER_CLEANUP: |
| 1723 | if (preserve_tos) { |
| 1724 | ADDOP(c, ROT_FOUR); |
| 1725 | } |
| 1726 | if (info->fb_exit) { |
| 1727 | ADDOP(c, POP_BLOCK); |
| 1728 | ADDOP(c, POP_EXCEPT); |
| 1729 | ADDOP_JREL(c, CALL_FINALLY, info->fb_exit); |
| 1730 | } |
| 1731 | else { |
| 1732 | ADDOP(c, POP_EXCEPT); |
| 1733 | } |
| 1734 | return 1; |
| 1735 | } |
| 1736 | Py_UNREACHABLE(); |
| 1737 | } |
| 1738 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1739 | /* Compile a sequence of statements, checking for a docstring |
| 1740 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1741 | |
| 1742 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1743 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1744 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1745 | int i = 0; |
| 1746 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1747 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1748 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1749 | /* Set current line number to the line number of first statement. |
| 1750 | This way line number for SETUP_ANNOTATIONS will always |
| 1751 | coincide with the line number of first "real" statement in module. |
Miss Islington (bot) | d8071cb | 2019-10-08 19:42:22 -0700 | [diff] [blame] | 1752 | If body is empty, then lineno will be set later in assemble. */ |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1753 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && |
| 1754 | !c->u->u_lineno && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1755 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1756 | c->u->u_lineno = st->lineno; |
| 1757 | } |
| 1758 | /* Every annotated class and module should have __annotations__. */ |
| 1759 | if (find_ann(stmts)) { |
| 1760 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1761 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1762 | if (!asdl_seq_LEN(stmts)) |
| 1763 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1764 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1765 | if (c->c_optimize < 2) { |
| 1766 | docstring = _PyAST_GetDocString(stmts); |
| 1767 | if (docstring) { |
| 1768 | i = 1; |
| 1769 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1770 | assert(st->kind == Expr_kind); |
| 1771 | VISIT(c, expr, st->v.Expr.value); |
| 1772 | if (!compiler_nameop(c, __doc__, Store)) |
| 1773 | return 0; |
| 1774 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1776 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1777 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1778 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
| 1781 | static PyCodeObject * |
| 1782 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1784 | PyCodeObject *co; |
| 1785 | int addNone = 1; |
| 1786 | static PyObject *module; |
| 1787 | if (!module) { |
| 1788 | module = PyUnicode_InternFromString("<module>"); |
| 1789 | if (!module) |
| 1790 | return NULL; |
| 1791 | } |
| 1792 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1793 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1794 | return NULL; |
| 1795 | switch (mod->kind) { |
| 1796 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1797 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1798 | compiler_exit_scope(c); |
| 1799 | return 0; |
| 1800 | } |
| 1801 | break; |
| 1802 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1803 | if (find_ann(mod->v.Interactive.body)) { |
| 1804 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1805 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1806 | c->c_interactive = 1; |
| 1807 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1808 | mod->v.Interactive.body); |
| 1809 | break; |
| 1810 | case Expression_kind: |
| 1811 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1812 | addNone = 0; |
| 1813 | break; |
| 1814 | case Suite_kind: |
| 1815 | PyErr_SetString(PyExc_SystemError, |
| 1816 | "suite should not be possible"); |
| 1817 | return 0; |
| 1818 | default: |
| 1819 | PyErr_Format(PyExc_SystemError, |
| 1820 | "module kind %d should not be possible", |
| 1821 | mod->kind); |
| 1822 | return 0; |
| 1823 | } |
| 1824 | co = assemble(c, addNone); |
| 1825 | compiler_exit_scope(c); |
| 1826 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1827 | } |
| 1828 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1829 | /* The test for LOCAL must come before the test for FREE in order to |
| 1830 | handle classes where name is both local and free. The local var is |
| 1831 | a method and the free var is a free var referenced within a method. |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 1832 | */ |
| 1833 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1834 | static int |
| 1835 | get_ref_type(struct compiler *c, PyObject *name) |
| 1836 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1837 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1838 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1839 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1840 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1841 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1842 | if (scope == 0) { |
| 1843 | char buf[350]; |
| 1844 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1845 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1847 | PyUnicode_AsUTF8(name), |
| 1848 | PyUnicode_AsUTF8(c->u->u_name), |
| 1849 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1850 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1851 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1852 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1853 | ); |
| 1854 | Py_FatalError(buf); |
| 1855 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1856 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1857 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1858 | } |
| 1859 | |
| 1860 | static int |
| 1861 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1862 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1863 | PyObject *v; |
| 1864 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1866 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1867 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1868 | } |
| 1869 | |
| 1870 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1871 | compiler_make_closure(struct compiler *c, PyCodeObject *co, Py_ssize_t flags, PyObject *qualname) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1872 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1873 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1874 | if (qualname == NULL) |
| 1875 | qualname = co->co_name; |
| 1876 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1877 | if (free) { |
| 1878 | for (i = 0; i < free; ++i) { |
| 1879 | /* Bypass com_addop_varname because it will generate |
| 1880 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1881 | */ |
| 1882 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1883 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1884 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1885 | /* Special case: If a class contains a method with a |
| 1886 | free variable that has the same name as a method, |
| 1887 | the name will be considered free *and* local in the |
| 1888 | class. It should be handled by the closure, as |
| 1889 | well as by the normal name loookup logic. |
| 1890 | */ |
| 1891 | reftype = get_ref_type(c, name); |
| 1892 | if (reftype == CELL) |
| 1893 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1894 | else /* (reftype == FREE) */ |
| 1895 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1896 | if (arg == -1) { |
| 1897 | fprintf(stderr, |
| 1898 | "lookup %s in %s %d %d\n" |
| 1899 | "freevars of %s: %s\n", |
| 1900 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1901 | PyUnicode_AsUTF8(c->u->u_name), |
| 1902 | reftype, arg, |
| 1903 | PyUnicode_AsUTF8(co->co_name), |
| 1904 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1905 | Py_FatalError("compiler_make_closure()"); |
| 1906 | } |
| 1907 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1908 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1909 | flags |= 0x08; |
| 1910 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1911 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1912 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1913 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1914 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1915 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1916 | } |
| 1917 | |
| 1918 | static int |
| 1919 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1920 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1921 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1922 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1923 | if (!decos) |
| 1924 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1925 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1927 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1928 | } |
| 1929 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1930 | } |
| 1931 | |
| 1932 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1933 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1934 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1935 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1936 | /* Push a dict of keyword-only default values. |
| 1937 | |
| 1938 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1939 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1940 | int i; |
| 1941 | PyObject *keys = NULL; |
| 1942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1943 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1944 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1945 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1946 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1947 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1948 | if (!mangled) { |
| 1949 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1951 | if (keys == NULL) { |
| 1952 | keys = PyList_New(1); |
| 1953 | if (keys == NULL) { |
| 1954 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1955 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1956 | } |
| 1957 | PyList_SET_ITEM(keys, 0, mangled); |
| 1958 | } |
| 1959 | else { |
| 1960 | int res = PyList_Append(keys, mangled); |
| 1961 | Py_DECREF(mangled); |
| 1962 | if (res == -1) { |
| 1963 | goto error; |
| 1964 | } |
| 1965 | } |
| 1966 | if (!compiler_visit_expr(c, default_)) { |
| 1967 | goto error; |
| 1968 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1969 | } |
| 1970 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1971 | if (keys != NULL) { |
| 1972 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 1973 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 1974 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1975 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1976 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1977 | assert(default_count > 0); |
| 1978 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1979 | } |
| 1980 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1981 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | error: |
| 1985 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1986 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1987 | } |
| 1988 | |
| 1989 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1990 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 1991 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 1992 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 1993 | return 1; |
| 1994 | } |
| 1995 | |
| 1996 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1997 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 1998 | expr_ty annotation, PyObject *names) |
| 1999 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2000 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2001 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2002 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2003 | VISIT(c, annexpr, annotation) |
| 2004 | } |
| 2005 | else { |
| 2006 | VISIT(c, expr, annotation); |
| 2007 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2008 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2009 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2010 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2011 | if (PyList_Append(names, mangled) < 0) { |
| 2012 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2013 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2014 | } |
| 2015 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2016 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2017 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | static int |
| 2021 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 2022 | PyObject *names) |
| 2023 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2024 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2025 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2026 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2027 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2028 | c, |
| 2029 | arg->arg, |
| 2030 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2031 | names)) |
| 2032 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2033 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2034 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2035 | } |
| 2036 | |
| 2037 | static int |
| 2038 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2039 | expr_ty returns) |
| 2040 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2041 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2042 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2043 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2044 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2045 | */ |
| 2046 | static identifier return_str; |
| 2047 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2048 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2049 | names = PyList_New(0); |
| 2050 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2051 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2052 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2053 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2054 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 2055 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 2056 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2057 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2058 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2059 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2060 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2061 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2062 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2063 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2064 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2065 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2066 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2067 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2068 | if (!return_str) { |
| 2069 | return_str = PyUnicode_InternFromString("return"); |
| 2070 | if (!return_str) |
| 2071 | goto error; |
| 2072 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2073 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2074 | goto error; |
| 2075 | } |
| 2076 | |
| 2077 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2078 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2079 | PyObject *keytuple = PyList_AsTuple(names); |
| 2080 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2081 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2082 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2083 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2085 | else { |
| 2086 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2087 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2088 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2089 | |
| 2090 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2091 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2092 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2093 | } |
| 2094 | |
| 2095 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2096 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2097 | { |
| 2098 | VISIT_SEQ(c, expr, args->defaults); |
| 2099 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2100 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2101 | } |
| 2102 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2103 | static Py_ssize_t |
| 2104 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2105 | { |
| 2106 | Py_ssize_t funcflags = 0; |
| 2107 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2108 | if (!compiler_visit_defaults(c, args)) |
| 2109 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2110 | funcflags |= 0x01; |
| 2111 | } |
| 2112 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2113 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2114 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2115 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2116 | return -1; |
| 2117 | } |
| 2118 | else if (res > 0) { |
| 2119 | funcflags |= 0x02; |
| 2120 | } |
| 2121 | } |
| 2122 | return funcflags; |
| 2123 | } |
| 2124 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2125 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2126 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2127 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2128 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2129 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2130 | arguments_ty args; |
| 2131 | expr_ty returns; |
| 2132 | identifier name; |
| 2133 | asdl_seq* decos; |
| 2134 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2135 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2136 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2137 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2138 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2139 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2140 | if (is_async) { |
| 2141 | assert(s->kind == AsyncFunctionDef_kind); |
| 2142 | |
| 2143 | args = s->v.AsyncFunctionDef.args; |
| 2144 | returns = s->v.AsyncFunctionDef.returns; |
| 2145 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2146 | name = s->v.AsyncFunctionDef.name; |
| 2147 | body = s->v.AsyncFunctionDef.body; |
| 2148 | |
| 2149 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2150 | } else { |
| 2151 | assert(s->kind == FunctionDef_kind); |
| 2152 | |
| 2153 | args = s->v.FunctionDef.args; |
| 2154 | returns = s->v.FunctionDef.returns; |
| 2155 | decos = s->v.FunctionDef.decorator_list; |
| 2156 | name = s->v.FunctionDef.name; |
| 2157 | body = s->v.FunctionDef.body; |
| 2158 | |
| 2159 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2160 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2161 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2162 | if (!compiler_decorators(c, decos)) |
| 2163 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2164 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2165 | firstlineno = s->lineno; |
| 2166 | if (asdl_seq_LEN(decos)) { |
| 2167 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2168 | } |
| 2169 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2170 | funcflags = compiler_default_arguments(c, args); |
| 2171 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2172 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2173 | } |
| 2174 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2175 | annotations = compiler_visit_annotations(c, args, returns); |
| 2176 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2177 | return 0; |
| 2178 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2179 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2180 | funcflags |= 0x04; |
| 2181 | } |
| 2182 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2183 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2184 | return 0; |
| 2185 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2186 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2187 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2188 | if (c->c_optimize < 2) { |
| 2189 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2190 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2191 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2192 | compiler_exit_scope(c); |
| 2193 | return 0; |
| 2194 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2196 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2197 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2199 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2200 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2201 | qualname = c->u->u_qualname; |
| 2202 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2203 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2204 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2205 | Py_XDECREF(qualname); |
| 2206 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2208 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2209 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2210 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2211 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2212 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2213 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2214 | /* decorators */ |
| 2215 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2216 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2217 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2218 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2219 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | static int |
| 2223 | compiler_class(struct compiler *c, stmt_ty s) |
| 2224 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2225 | PyCodeObject *co; |
| 2226 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2227 | int i, firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2230 | if (!compiler_decorators(c, decos)) |
| 2231 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2232 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2233 | firstlineno = s->lineno; |
| 2234 | if (asdl_seq_LEN(decos)) { |
| 2235 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2236 | } |
| 2237 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2238 | /* ultimately generate code for: |
| 2239 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2240 | where: |
| 2241 | <func> is a function/closure created from the class body; |
| 2242 | it has a single argument (__locals__) where the dict |
| 2243 | (or MutableSequence) representing the locals is passed |
| 2244 | <name> is the class name |
| 2245 | <bases> is the positional arguments and *varargs argument |
| 2246 | <keywords> is the keyword arguments and **kwds argument |
| 2247 | This borrows from compiler_call. |
| 2248 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2251 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2252 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2253 | return 0; |
| 2254 | /* this block represents what we do in the new scope */ |
| 2255 | { |
| 2256 | /* use the class name for name mangling */ |
| 2257 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2258 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2259 | /* load (global) __name__ ... */ |
| 2260 | str = PyUnicode_InternFromString("__name__"); |
| 2261 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2262 | Py_XDECREF(str); |
| 2263 | compiler_exit_scope(c); |
| 2264 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2265 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2266 | Py_DECREF(str); |
| 2267 | /* ... and store it as __module__ */ |
| 2268 | str = PyUnicode_InternFromString("__module__"); |
| 2269 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2270 | Py_XDECREF(str); |
| 2271 | compiler_exit_scope(c); |
| 2272 | return 0; |
| 2273 | } |
| 2274 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2275 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2276 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2277 | str = PyUnicode_InternFromString("__qualname__"); |
| 2278 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2279 | Py_XDECREF(str); |
| 2280 | compiler_exit_scope(c); |
| 2281 | return 0; |
| 2282 | } |
| 2283 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2284 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2285 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | compiler_exit_scope(c); |
| 2287 | return 0; |
| 2288 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2289 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2290 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2291 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2292 | str = PyUnicode_InternFromString("__class__"); |
| 2293 | if (str == NULL) { |
| 2294 | compiler_exit_scope(c); |
| 2295 | return 0; |
| 2296 | } |
| 2297 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2298 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2299 | if (i < 0) { |
| 2300 | compiler_exit_scope(c); |
| 2301 | return 0; |
| 2302 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2303 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2304 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2305 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2306 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2307 | str = PyUnicode_InternFromString("__classcell__"); |
| 2308 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2309 | Py_XDECREF(str); |
| 2310 | compiler_exit_scope(c); |
| 2311 | return 0; |
| 2312 | } |
| 2313 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2314 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2315 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2316 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2317 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2318 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2319 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2320 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2321 | /* create the code object */ |
| 2322 | co = assemble(c, 1); |
| 2323 | } |
| 2324 | /* leave the new scope */ |
| 2325 | compiler_exit_scope(c); |
| 2326 | if (co == NULL) |
| 2327 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2328 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2329 | /* 2. load the 'build_class' function */ |
| 2330 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2331 | |
| 2332 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2333 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2334 | Py_DECREF(co); |
| 2335 | |
| 2336 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2337 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2338 | |
| 2339 | /* 5. generate the rest of the code for the call */ |
| 2340 | if (!compiler_call_helper(c, 2, |
| 2341 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2342 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2343 | return 0; |
| 2344 | |
| 2345 | /* 6. apply decorators */ |
| 2346 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2347 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2348 | } |
| 2349 | |
| 2350 | /* 7. store into <name> */ |
| 2351 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2352 | return 0; |
| 2353 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2356 | /* Return 0 if the expression is a constant value except named singletons. |
| 2357 | Return 1 otherwise. */ |
| 2358 | static int |
| 2359 | check_is_arg(expr_ty e) |
| 2360 | { |
| 2361 | if (e->kind != Constant_kind) { |
| 2362 | return 1; |
| 2363 | } |
| 2364 | PyObject *value = e->v.Constant.value; |
| 2365 | return (value == Py_None |
| 2366 | || value == Py_False |
| 2367 | || value == Py_True |
| 2368 | || value == Py_Ellipsis); |
| 2369 | } |
| 2370 | |
| 2371 | /* Check operands of identity chacks ("is" and "is not"). |
| 2372 | Emit a warning if any operand is a constant except named singletons. |
| 2373 | Return 0 on error. |
| 2374 | */ |
| 2375 | static int |
| 2376 | check_compare(struct compiler *c, expr_ty e) |
| 2377 | { |
| 2378 | Py_ssize_t i, n; |
| 2379 | int left = check_is_arg(e->v.Compare.left); |
| 2380 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2381 | for (i = 0; i < n; i++) { |
| 2382 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2383 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2384 | if (op == Is || op == IsNot) { |
| 2385 | if (!right || !left) { |
| 2386 | const char *msg = (op == Is) |
| 2387 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2388 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2389 | return compiler_warn(c, msg); |
| 2390 | } |
| 2391 | } |
| 2392 | left = right; |
| 2393 | } |
| 2394 | return 1; |
| 2395 | } |
| 2396 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2397 | static int |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2398 | cmpop(cmpop_ty op) |
| 2399 | { |
| 2400 | switch (op) { |
| 2401 | case Eq: |
| 2402 | return PyCmp_EQ; |
| 2403 | case NotEq: |
| 2404 | return PyCmp_NE; |
| 2405 | case Lt: |
| 2406 | return PyCmp_LT; |
| 2407 | case LtE: |
| 2408 | return PyCmp_LE; |
| 2409 | case Gt: |
| 2410 | return PyCmp_GT; |
| 2411 | case GtE: |
| 2412 | return PyCmp_GE; |
| 2413 | case Is: |
| 2414 | return PyCmp_IS; |
| 2415 | case IsNot: |
| 2416 | return PyCmp_IS_NOT; |
| 2417 | case In: |
| 2418 | return PyCmp_IN; |
| 2419 | case NotIn: |
| 2420 | return PyCmp_NOT_IN; |
| 2421 | default: |
| 2422 | return PyCmp_BAD; |
| 2423 | } |
| 2424 | } |
| 2425 | |
| 2426 | static int |
| 2427 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2428 | { |
| 2429 | switch (e->kind) { |
| 2430 | case UnaryOp_kind: |
| 2431 | if (e->v.UnaryOp.op == Not) |
| 2432 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2433 | /* fallback to general implementation */ |
| 2434 | break; |
| 2435 | case BoolOp_kind: { |
| 2436 | asdl_seq *s = e->v.BoolOp.values; |
| 2437 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2438 | assert(n >= 0); |
| 2439 | int cond2 = e->v.BoolOp.op == Or; |
| 2440 | basicblock *next2 = next; |
| 2441 | if (!cond2 != !cond) { |
| 2442 | next2 = compiler_new_block(c); |
| 2443 | if (next2 == NULL) |
| 2444 | return 0; |
| 2445 | } |
| 2446 | for (i = 0; i < n; ++i) { |
| 2447 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2448 | return 0; |
| 2449 | } |
| 2450 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2451 | return 0; |
| 2452 | if (next2 != next) |
| 2453 | compiler_use_next_block(c, next2); |
| 2454 | return 1; |
| 2455 | } |
| 2456 | case IfExp_kind: { |
| 2457 | basicblock *end, *next2; |
| 2458 | end = compiler_new_block(c); |
| 2459 | if (end == NULL) |
| 2460 | return 0; |
| 2461 | next2 = compiler_new_block(c); |
| 2462 | if (next2 == NULL) |
| 2463 | return 0; |
| 2464 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2465 | return 0; |
| 2466 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2467 | return 0; |
| 2468 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2469 | compiler_use_next_block(c, next2); |
| 2470 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2471 | return 0; |
| 2472 | compiler_use_next_block(c, end); |
| 2473 | return 1; |
| 2474 | } |
| 2475 | case Compare_kind: { |
| 2476 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2477 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2478 | if (!check_compare(c, e)) { |
| 2479 | return 0; |
| 2480 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2481 | basicblock *cleanup = compiler_new_block(c); |
| 2482 | if (cleanup == NULL) |
| 2483 | return 0; |
| 2484 | VISIT(c, expr, e->v.Compare.left); |
| 2485 | for (i = 0; i < n; i++) { |
| 2486 | VISIT(c, expr, |
| 2487 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2488 | ADDOP(c, DUP_TOP); |
| 2489 | ADDOP(c, ROT_THREE); |
| 2490 | ADDOP_I(c, COMPARE_OP, |
| 2491 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 2492 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2493 | NEXT_BLOCK(c); |
| 2494 | } |
| 2495 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 2496 | ADDOP_I(c, COMPARE_OP, |
| 2497 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
| 2498 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2499 | basicblock *end = compiler_new_block(c); |
| 2500 | if (end == NULL) |
| 2501 | return 0; |
| 2502 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2503 | compiler_use_next_block(c, cleanup); |
| 2504 | ADDOP(c, POP_TOP); |
| 2505 | if (!cond) { |
| 2506 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2507 | } |
| 2508 | compiler_use_next_block(c, end); |
| 2509 | return 1; |
| 2510 | } |
| 2511 | /* fallback to general implementation */ |
| 2512 | break; |
| 2513 | } |
| 2514 | default: |
| 2515 | /* fallback to general implementation */ |
| 2516 | break; |
| 2517 | } |
| 2518 | |
| 2519 | /* general implementation */ |
| 2520 | VISIT(c, expr, e); |
| 2521 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2522 | return 1; |
| 2523 | } |
| 2524 | |
| 2525 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2526 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2527 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2528 | basicblock *end, *next; |
| 2529 | |
| 2530 | assert(e->kind == IfExp_kind); |
| 2531 | end = compiler_new_block(c); |
| 2532 | if (end == NULL) |
| 2533 | return 0; |
| 2534 | next = compiler_new_block(c); |
| 2535 | if (next == NULL) |
| 2536 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2537 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2538 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2539 | VISIT(c, expr, e->v.IfExp.body); |
| 2540 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2541 | compiler_use_next_block(c, next); |
| 2542 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2543 | compiler_use_next_block(c, end); |
| 2544 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2545 | } |
| 2546 | |
| 2547 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2548 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2549 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2550 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2551 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2552 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2553 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2554 | arguments_ty args = e->v.Lambda.args; |
| 2555 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2556 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2557 | if (!name) { |
| 2558 | name = PyUnicode_InternFromString("<lambda>"); |
| 2559 | if (!name) |
| 2560 | return 0; |
| 2561 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2562 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2563 | funcflags = compiler_default_arguments(c, args); |
| 2564 | if (funcflags == -1) { |
| 2565 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2566 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2567 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2568 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2569 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2570 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2572 | /* Make None the first constant, so the lambda can't have a |
| 2573 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2574 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2575 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2577 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2578 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2579 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2580 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2581 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2582 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2583 | } |
| 2584 | else { |
| 2585 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2586 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2588 | qualname = c->u->u_qualname; |
| 2589 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2590 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2591 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2592 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2593 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2594 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2595 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2596 | Py_DECREF(co); |
| 2597 | |
| 2598 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2599 | } |
| 2600 | |
| 2601 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2602 | compiler_if(struct compiler *c, stmt_ty s) |
| 2603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2604 | basicblock *end, *next; |
| 2605 | int constant; |
| 2606 | assert(s->kind == If_kind); |
| 2607 | end = compiler_new_block(c); |
| 2608 | if (end == NULL) |
| 2609 | return 0; |
| 2610 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2611 | constant = expr_constant(s->v.If.test); |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2612 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2613 | * constant = 1: "if 1", "if 2", ... |
| 2614 | * constant = -1: rest */ |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2615 | if (constant == 0) { |
| 2616 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2617 | VISIT_SEQ(c, stmt, s->v.If.body); |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2618 | END_DO_NOT_EMIT_BYTECODE |
| 2619 | if (s->v.If.orelse) { |
| 2620 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2621 | } |
| 2622 | } else if (constant == 1) { |
| 2623 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2624 | if (s->v.If.orelse) { |
| 2625 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2626 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2627 | END_DO_NOT_EMIT_BYTECODE |
| 2628 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2629 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2630 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | next = compiler_new_block(c); |
| 2632 | if (next == NULL) |
| 2633 | return 0; |
| 2634 | } |
| 2635 | else |
| 2636 | next = end; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2637 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) |
| 2638 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2639 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2640 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2641 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2642 | compiler_use_next_block(c, next); |
| 2643 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2644 | } |
| 2645 | } |
| 2646 | compiler_use_next_block(c, end); |
| 2647 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2648 | } |
| 2649 | |
| 2650 | static int |
| 2651 | compiler_for(struct compiler *c, stmt_ty s) |
| 2652 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2653 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2655 | start = compiler_new_block(c); |
| 2656 | cleanup = compiler_new_block(c); |
| 2657 | end = compiler_new_block(c); |
| 2658 | if (start == NULL || end == NULL || cleanup == NULL) |
| 2659 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2660 | |
| 2661 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2662 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2664 | VISIT(c, expr, s->v.For.iter); |
| 2665 | ADDOP(c, GET_ITER); |
| 2666 | compiler_use_next_block(c, start); |
| 2667 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2668 | VISIT(c, expr, s->v.For.target); |
| 2669 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2670 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2671 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2672 | |
| 2673 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2674 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2675 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2676 | compiler_use_next_block(c, end); |
| 2677 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2680 | |
| 2681 | static int |
| 2682 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2683 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2684 | basicblock *start, *except, *end; |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2685 | if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ |
| 2686 | c->u->u_ste->ste_coroutine = 1; |
| 2687 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2688 | return compiler_error(c, "'async for' outside async function"); |
| 2689 | } |
| 2690 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2691 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2692 | except = compiler_new_block(c); |
| 2693 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2694 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2695 | if (start == NULL || except == NULL || end == NULL) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2696 | return 0; |
| 2697 | |
| 2698 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2699 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2700 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2701 | compiler_use_next_block(c, start); |
| 2702 | if (!compiler_push_fblock(c, FOR_LOOP, start, end)) |
| 2703 | return 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2704 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2705 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2706 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2707 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2708 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2709 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2710 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2711 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2712 | /* Success block for __anext__ */ |
| 2713 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2714 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2715 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2716 | |
| 2717 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2718 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2719 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2720 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2721 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2722 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2723 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2724 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2725 | |
| 2726 | compiler_use_next_block(c, end); |
| 2727 | |
| 2728 | return 1; |
| 2729 | } |
| 2730 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2731 | static int |
| 2732 | compiler_while(struct compiler *c, stmt_ty s) |
| 2733 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2734 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2735 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2736 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2737 | if (constant == 0) { |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2738 | BEGIN_DO_NOT_EMIT_BYTECODE |
Miss Skeleton (bot) | dcb338e | 2019-10-30 05:11:41 -0700 | [diff] [blame] | 2739 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2740 | // inside a while loop so it can correctly evaluate syntax |
| 2741 | // errors. |
| 2742 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL)) { |
| 2743 | return 0; |
| 2744 | } |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2745 | VISIT_SEQ(c, stmt, s->v.While.body); |
Miss Skeleton (bot) | dcb338e | 2019-10-30 05:11:41 -0700 | [diff] [blame] | 2746 | // Remove the dummy block now that is not needed. |
| 2747 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2748 | END_DO_NOT_EMIT_BYTECODE |
| 2749 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2750 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2751 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | return 1; |
| 2753 | } |
| 2754 | loop = compiler_new_block(c); |
| 2755 | end = compiler_new_block(c); |
| 2756 | if (constant == -1) { |
| 2757 | anchor = compiler_new_block(c); |
| 2758 | if (anchor == NULL) |
| 2759 | return 0; |
| 2760 | } |
| 2761 | if (loop == NULL || end == NULL) |
| 2762 | return 0; |
| 2763 | if (s->v.While.orelse) { |
| 2764 | orelse = compiler_new_block(c); |
| 2765 | if (orelse == NULL) |
| 2766 | return 0; |
| 2767 | } |
| 2768 | else |
| 2769 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2771 | compiler_use_next_block(c, loop); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2772 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | return 0; |
| 2774 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2775 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2776 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2777 | } |
| 2778 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2779 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | /* XXX should the two POP instructions be in a separate block |
| 2782 | if there is no else clause ? |
| 2783 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2784 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2785 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2786 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2787 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2788 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2789 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2790 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2791 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2792 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2793 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2794 | } |
| 2795 | |
| 2796 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2797 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2798 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2799 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2800 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2801 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2802 | return compiler_error(c, "'return' outside function"); |
| 2803 | if (s->v.Return.value != NULL && |
| 2804 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2805 | { |
| 2806 | return compiler_error( |
| 2807 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2808 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2809 | if (preserve_tos) { |
| 2810 | VISIT(c, expr, s->v.Return.value); |
| 2811 | } |
| 2812 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2813 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2814 | |
| 2815 | if (!compiler_unwind_fblock(c, info, preserve_tos)) |
| 2816 | return 0; |
| 2817 | } |
| 2818 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2819 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2820 | } |
| 2821 | else if (!preserve_tos) { |
| 2822 | VISIT(c, expr, s->v.Return.value); |
| 2823 | } |
| 2824 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2825 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2826 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2829 | static int |
| 2830 | compiler_break(struct compiler *c) |
| 2831 | { |
| 2832 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2833 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2834 | |
| 2835 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2836 | return 0; |
| 2837 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2838 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit); |
| 2839 | return 1; |
| 2840 | } |
| 2841 | } |
| 2842 | return compiler_error(c, "'break' outside loop"); |
| 2843 | } |
| 2844 | |
| 2845 | static int |
| 2846 | compiler_continue(struct compiler *c) |
| 2847 | { |
| 2848 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2849 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2850 | |
| 2851 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2852 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block); |
| 2853 | return 1; |
| 2854 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2855 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2856 | return 0; |
| 2857 | } |
| 2858 | return compiler_error(c, "'continue' not properly in loop"); |
| 2859 | } |
| 2860 | |
| 2861 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2862 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2863 | |
| 2864 | SETUP_FINALLY L |
| 2865 | <code for body> |
| 2866 | POP_BLOCK |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2867 | BEGIN_FINALLY |
| 2868 | L: |
| 2869 | <code for finalbody> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | END_FINALLY |
| 2871 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 | The special instructions use the block stack. Each block |
| 2873 | stack entry contains the instruction that created it (here |
| 2874 | SETUP_FINALLY), the level of the value stack at the time the |
| 2875 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2877 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2878 | Pushes the current value stack level and the label |
| 2879 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2881 | Pops en entry from the block stack. |
| 2882 | BEGIN_FINALLY |
| 2883 | Pushes NULL onto the value stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2884 | END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2885 | Pops 1 (NULL or int) or 6 entries from the *value* stack and restore |
| 2886 | the raised and the caught exceptions they specify. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2887 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2888 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2889 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2890 | exceptions are pushed onto the value stack (and the exception |
| 2891 | condition is cleared), and the interpreter jumps to the label |
| 2892 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2893 | */ |
| 2894 | |
| 2895 | static int |
| 2896 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2897 | { |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2898 | basicblock *start, *newcurblock, *body, *end; |
| 2899 | int break_finally = 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2900 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2901 | body = compiler_new_block(c); |
| 2902 | end = compiler_new_block(c); |
| 2903 | if (body == NULL || end == NULL) |
| 2904 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2905 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2906 | start = c->u->u_curblock; |
| 2907 | |
| 2908 | /* `finally` block. Compile it first to determine if any of "break", |
| 2909 | "continue" or "return" are used in it. */ |
| 2910 | compiler_use_next_block(c, end); |
| 2911 | if (!compiler_push_fblock(c, FINALLY_END, end, end)) |
| 2912 | return 0; |
| 2913 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2914 | ADDOP(c, END_FINALLY); |
| 2915 | break_finally = (c->u->u_fblock[c->u->u_nfblocks - 1].fb_exit == NULL); |
| 2916 | if (break_finally) { |
| 2917 | /* Pops a placeholder. See below */ |
| 2918 | ADDOP(c, POP_TOP); |
| 2919 | } |
| 2920 | compiler_pop_fblock(c, FINALLY_END, end); |
| 2921 | |
| 2922 | newcurblock = c->u->u_curblock; |
| 2923 | c->u->u_curblock = start; |
| 2924 | start->b_next = NULL; |
| 2925 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2926 | /* `try` block */ |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2927 | c->u->u_lineno_set = 0; |
| 2928 | c->u->u_lineno = s->lineno; |
| 2929 | c->u->u_col_offset = s->col_offset; |
| 2930 | if (break_finally) { |
| 2931 | /* Pushes a placeholder for the value of "return" in the "try" block |
| 2932 | to balance the stack for "break", "continue" and "return" in |
| 2933 | the "finally" block. */ |
| 2934 | ADDOP_LOAD_CONST(c, Py_None); |
| 2935 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2936 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2937 | compiler_use_next_block(c, body); |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2938 | if (!compiler_push_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2940 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2941 | if (!compiler_try_except(c, s)) |
| 2942 | return 0; |
| 2943 | } |
| 2944 | else { |
| 2945 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2946 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2947 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2948 | ADDOP(c, BEGIN_FINALLY); |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2949 | compiler_pop_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2950 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2951 | c->u->u_curblock->b_next = end; |
| 2952 | c->u->u_curblock = newcurblock; |
| 2953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2955 | } |
| 2956 | |
| 2957 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2958 | Code generated for "try: S except E1 as V1: S1 except E2 as V2: S2 ...": |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2959 | (The contents of the value stack is shown in [], with the top |
| 2960 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2961 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2962 | |
| 2963 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2964 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2965 | [] <code for S> |
| 2966 | [] POP_BLOCK |
| 2967 | [] JUMP_FORWARD L0 |
| 2968 | |
| 2969 | [tb, val, exc] L1: DUP ) |
| 2970 | [tb, val, exc, exc] <evaluate E1> ) |
| 2971 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2972 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2973 | [tb, val, exc] POP |
| 2974 | [tb, val] <assign to V1> (or POP if no V1) |
| 2975 | [tb] POP |
| 2976 | [] <code for S1> |
| 2977 | JUMP_FORWARD L0 |
| 2978 | |
| 2979 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2980 | .............................etc....................... |
| 2981 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2983 | |
| 2984 | [] L0: <next statement> |
| 2985 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2986 | Of course, parts are not generated if Vi or Ei is not present. |
| 2987 | */ |
| 2988 | static int |
| 2989 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2990 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2991 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2992 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2993 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | body = compiler_new_block(c); |
| 2995 | except = compiler_new_block(c); |
| 2996 | orelse = compiler_new_block(c); |
| 2997 | end = compiler_new_block(c); |
| 2998 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2999 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3000 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3002 | if (!compiler_push_fblock(c, EXCEPT, body, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3003 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3004 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | ADDOP(c, POP_BLOCK); |
| 3006 | compiler_pop_fblock(c, EXCEPT, body); |
| 3007 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3008 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3009 | compiler_use_next_block(c, except); |
| 3010 | for (i = 0; i < n; i++) { |
| 3011 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3012 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3013 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3014 | return compiler_error(c, "default 'except:' must be last"); |
| 3015 | c->u->u_lineno_set = 0; |
| 3016 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3017 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3018 | except = compiler_new_block(c); |
| 3019 | if (except == NULL) |
| 3020 | return 0; |
| 3021 | if (handler->v.ExceptHandler.type) { |
| 3022 | ADDOP(c, DUP_TOP); |
| 3023 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 3024 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 3025 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 3026 | } |
| 3027 | ADDOP(c, POP_TOP); |
| 3028 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3029 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3030 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3031 | cleanup_end = compiler_new_block(c); |
| 3032 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3033 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3034 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3035 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3036 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3037 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3038 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3040 | /* |
| 3041 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3042 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3043 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3044 | try: |
| 3045 | # body |
| 3046 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3047 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3048 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3049 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3050 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3051 | /* second try: */ |
| 3052 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 3053 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3054 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3055 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3057 | /* second # body */ |
| 3058 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 3059 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3060 | ADDOP(c, BEGIN_FINALLY); |
| 3061 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3062 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3063 | /* finally: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3064 | compiler_use_next_block(c, cleanup_end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3065 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3066 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3067 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3068 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3069 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3070 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3071 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3072 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3073 | ADDOP(c, END_FINALLY); |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 3074 | ADDOP(c, POP_EXCEPT); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3075 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3076 | } |
| 3077 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3078 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3079 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3080 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3081 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3082 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3084 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3085 | ADDOP(c, POP_TOP); |
| 3086 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3087 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3088 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3089 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3090 | ADDOP(c, POP_EXCEPT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3091 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3092 | } |
| 3093 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3094 | compiler_use_next_block(c, except); |
| 3095 | } |
| 3096 | ADDOP(c, END_FINALLY); |
| 3097 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3098 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3099 | compiler_use_next_block(c, end); |
| 3100 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3101 | } |
| 3102 | |
| 3103 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3104 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3105 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3106 | return compiler_try_finally(c, s); |
| 3107 | else |
| 3108 | return compiler_try_except(c, s); |
| 3109 | } |
| 3110 | |
| 3111 | |
| 3112 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3113 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3114 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3115 | /* The IMPORT_NAME opcode was already generated. This function |
| 3116 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | If there is a dot in name, we need to split it and emit a |
Serhiy Storchaka | f93234b | 2017-05-09 22:31:05 +0300 | [diff] [blame] | 3119 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3120 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3121 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3122 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3123 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3124 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3125 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3127 | while (1) { |
| 3128 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3130 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3131 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3132 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3133 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3135 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3136 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3137 | if (dot == -1) { |
| 3138 | break; |
| 3139 | } |
| 3140 | ADDOP(c, ROT_TWO); |
| 3141 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3143 | if (!compiler_nameop(c, asname, Store)) { |
| 3144 | return 0; |
| 3145 | } |
| 3146 | ADDOP(c, POP_TOP); |
| 3147 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | } |
| 3149 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3150 | } |
| 3151 | |
| 3152 | static int |
| 3153 | compiler_import(struct compiler *c, stmt_ty s) |
| 3154 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3155 | /* The Import node stores a module name like a.b.c as a single |
| 3156 | string. This is convenient for all cases except |
| 3157 | import a.b.c as d |
| 3158 | where we need to parse that string to extract the individual |
| 3159 | module names. |
| 3160 | XXX Perhaps change the representation to make this case simpler? |
| 3161 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3162 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | for (i = 0; i < n; i++) { |
| 3165 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3166 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3167 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3168 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3169 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3170 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3171 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3172 | if (alias->asname) { |
| 3173 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3174 | if (!r) |
| 3175 | return r; |
| 3176 | } |
| 3177 | else { |
| 3178 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3179 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3180 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3181 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3182 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3183 | if (tmp == NULL) |
| 3184 | return 0; |
| 3185 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3187 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3188 | Py_DECREF(tmp); |
| 3189 | } |
| 3190 | if (!r) |
| 3191 | return r; |
| 3192 | } |
| 3193 | } |
| 3194 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
| 3197 | static int |
| 3198 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3199 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3200 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3201 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3203 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3204 | if (!empty_string) { |
| 3205 | empty_string = PyUnicode_FromString(""); |
| 3206 | if (!empty_string) |
| 3207 | return 0; |
| 3208 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3209 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3210 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3211 | |
| 3212 | names = PyTuple_New(n); |
| 3213 | if (!names) |
| 3214 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3215 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | /* build up the names */ |
| 3217 | for (i = 0; i < n; i++) { |
| 3218 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3219 | Py_INCREF(alias->name); |
| 3220 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3221 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3222 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3223 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3224 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3225 | Py_DECREF(names); |
| 3226 | return compiler_error(c, "from __future__ imports must occur " |
| 3227 | "at the beginning of the file"); |
| 3228 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3229 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3230 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3231 | if (s->v.ImportFrom.module) { |
| 3232 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3233 | } |
| 3234 | else { |
| 3235 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3236 | } |
| 3237 | for (i = 0; i < n; i++) { |
| 3238 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3239 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3240 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3241 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3242 | assert(n == 1); |
| 3243 | ADDOP(c, IMPORT_STAR); |
| 3244 | return 1; |
| 3245 | } |
| 3246 | |
| 3247 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3248 | store_name = alias->name; |
| 3249 | if (alias->asname) |
| 3250 | store_name = alias->asname; |
| 3251 | |
| 3252 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3253 | return 0; |
| 3254 | } |
| 3255 | } |
| 3256 | /* remove imported module */ |
| 3257 | ADDOP(c, POP_TOP); |
| 3258 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3259 | } |
| 3260 | |
| 3261 | static int |
| 3262 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3263 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3264 | static PyObject *assertion_error = NULL; |
| 3265 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3266 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3267 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3268 | return 1; |
| 3269 | if (assertion_error == NULL) { |
| 3270 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 3271 | if (assertion_error == NULL) |
| 3272 | return 0; |
| 3273 | } |
| 3274 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3275 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3276 | { |
| 3277 | if (!compiler_warn(c, "assertion is always true, " |
| 3278 | "perhaps remove parentheses?")) |
| 3279 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3280 | return 0; |
| 3281 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3283 | end = compiler_new_block(c); |
| 3284 | if (end == NULL) |
| 3285 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3286 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3287 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 3289 | if (s->v.Assert.msg) { |
| 3290 | VISIT(c, expr, s->v.Assert.msg); |
| 3291 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3292 | } |
| 3293 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3294 | compiler_use_next_block(c, end); |
| 3295 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3296 | } |
| 3297 | |
| 3298 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3299 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3300 | { |
| 3301 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3302 | VISIT(c, expr, value); |
| 3303 | ADDOP(c, PRINT_EXPR); |
| 3304 | return 1; |
| 3305 | } |
| 3306 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3307 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3308 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3309 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3310 | } |
| 3311 | |
| 3312 | VISIT(c, expr, value); |
| 3313 | ADDOP(c, POP_TOP); |
| 3314 | return 1; |
| 3315 | } |
| 3316 | |
| 3317 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3318 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3319 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3320 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3321 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 3323 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3324 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3325 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3326 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3327 | switch (s->kind) { |
| 3328 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3329 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | case ClassDef_kind: |
| 3331 | return compiler_class(c, s); |
| 3332 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3333 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3334 | case Delete_kind: |
| 3335 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3336 | break; |
| 3337 | case Assign_kind: |
| 3338 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3339 | VISIT(c, expr, s->v.Assign.value); |
| 3340 | for (i = 0; i < n; i++) { |
| 3341 | if (i < n - 1) |
| 3342 | ADDOP(c, DUP_TOP); |
| 3343 | VISIT(c, expr, |
| 3344 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3345 | } |
| 3346 | break; |
| 3347 | case AugAssign_kind: |
| 3348 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3349 | case AnnAssign_kind: |
| 3350 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3351 | case For_kind: |
| 3352 | return compiler_for(c, s); |
| 3353 | case While_kind: |
| 3354 | return compiler_while(c, s); |
| 3355 | case If_kind: |
| 3356 | return compiler_if(c, s); |
| 3357 | case Raise_kind: |
| 3358 | n = 0; |
| 3359 | if (s->v.Raise.exc) { |
| 3360 | VISIT(c, expr, s->v.Raise.exc); |
| 3361 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3362 | if (s->v.Raise.cause) { |
| 3363 | VISIT(c, expr, s->v.Raise.cause); |
| 3364 | n++; |
| 3365 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3366 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3367 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3368 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3369 | case Try_kind: |
| 3370 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3371 | case Assert_kind: |
| 3372 | return compiler_assert(c, s); |
| 3373 | case Import_kind: |
| 3374 | return compiler_import(c, s); |
| 3375 | case ImportFrom_kind: |
| 3376 | return compiler_from_import(c, s); |
| 3377 | case Global_kind: |
| 3378 | case Nonlocal_kind: |
| 3379 | break; |
| 3380 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3381 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3382 | case Pass_kind: |
| 3383 | break; |
| 3384 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3385 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3386 | case Continue_kind: |
| 3387 | return compiler_continue(c); |
| 3388 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3389 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3390 | case AsyncFunctionDef_kind: |
| 3391 | return compiler_function(c, s, 1); |
| 3392 | case AsyncWith_kind: |
| 3393 | return compiler_async_with(c, s, 0); |
| 3394 | case AsyncFor_kind: |
| 3395 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3398 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3399 | } |
| 3400 | |
| 3401 | static int |
| 3402 | unaryop(unaryop_ty op) |
| 3403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | switch (op) { |
| 3405 | case Invert: |
| 3406 | return UNARY_INVERT; |
| 3407 | case Not: |
| 3408 | return UNARY_NOT; |
| 3409 | case UAdd: |
| 3410 | return UNARY_POSITIVE; |
| 3411 | case USub: |
| 3412 | return UNARY_NEGATIVE; |
| 3413 | default: |
| 3414 | PyErr_Format(PyExc_SystemError, |
| 3415 | "unary op %d should not be possible", op); |
| 3416 | return 0; |
| 3417 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3418 | } |
| 3419 | |
| 3420 | static int |
| 3421 | binop(struct compiler *c, operator_ty op) |
| 3422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | switch (op) { |
| 3424 | case Add: |
| 3425 | return BINARY_ADD; |
| 3426 | case Sub: |
| 3427 | return BINARY_SUBTRACT; |
| 3428 | case Mult: |
| 3429 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3430 | case MatMult: |
| 3431 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3432 | case Div: |
| 3433 | return BINARY_TRUE_DIVIDE; |
| 3434 | case Mod: |
| 3435 | return BINARY_MODULO; |
| 3436 | case Pow: |
| 3437 | return BINARY_POWER; |
| 3438 | case LShift: |
| 3439 | return BINARY_LSHIFT; |
| 3440 | case RShift: |
| 3441 | return BINARY_RSHIFT; |
| 3442 | case BitOr: |
| 3443 | return BINARY_OR; |
| 3444 | case BitXor: |
| 3445 | return BINARY_XOR; |
| 3446 | case BitAnd: |
| 3447 | return BINARY_AND; |
| 3448 | case FloorDiv: |
| 3449 | return BINARY_FLOOR_DIVIDE; |
| 3450 | default: |
| 3451 | PyErr_Format(PyExc_SystemError, |
| 3452 | "binary op %d should not be possible", op); |
| 3453 | return 0; |
| 3454 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3455 | } |
| 3456 | |
| 3457 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3458 | inplace_binop(struct compiler *c, operator_ty op) |
| 3459 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3460 | switch (op) { |
| 3461 | case Add: |
| 3462 | return INPLACE_ADD; |
| 3463 | case Sub: |
| 3464 | return INPLACE_SUBTRACT; |
| 3465 | case Mult: |
| 3466 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3467 | case MatMult: |
| 3468 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3469 | case Div: |
| 3470 | return INPLACE_TRUE_DIVIDE; |
| 3471 | case Mod: |
| 3472 | return INPLACE_MODULO; |
| 3473 | case Pow: |
| 3474 | return INPLACE_POWER; |
| 3475 | case LShift: |
| 3476 | return INPLACE_LSHIFT; |
| 3477 | case RShift: |
| 3478 | return INPLACE_RSHIFT; |
| 3479 | case BitOr: |
| 3480 | return INPLACE_OR; |
| 3481 | case BitXor: |
| 3482 | return INPLACE_XOR; |
| 3483 | case BitAnd: |
| 3484 | return INPLACE_AND; |
| 3485 | case FloorDiv: |
| 3486 | return INPLACE_FLOOR_DIVIDE; |
| 3487 | default: |
| 3488 | PyErr_Format(PyExc_SystemError, |
| 3489 | "inplace binary op %d should not be possible", op); |
| 3490 | return 0; |
| 3491 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3492 | } |
| 3493 | |
| 3494 | static int |
| 3495 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3496 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3497 | int op, scope; |
| 3498 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3499 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3500 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3501 | PyObject *dict = c->u->u_names; |
| 3502 | PyObject *mangled; |
| 3503 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3504 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3505 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3506 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3507 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3508 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3509 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3510 | if (!mangled) |
| 3511 | return 0; |
| 3512 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3513 | op = 0; |
| 3514 | optype = OP_NAME; |
| 3515 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3516 | switch (scope) { |
| 3517 | case FREE: |
| 3518 | dict = c->u->u_freevars; |
| 3519 | optype = OP_DEREF; |
| 3520 | break; |
| 3521 | case CELL: |
| 3522 | dict = c->u->u_cellvars; |
| 3523 | optype = OP_DEREF; |
| 3524 | break; |
| 3525 | case LOCAL: |
| 3526 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3527 | optype = OP_FAST; |
| 3528 | break; |
| 3529 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3530 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3531 | optype = OP_GLOBAL; |
| 3532 | break; |
| 3533 | case GLOBAL_EXPLICIT: |
| 3534 | optype = OP_GLOBAL; |
| 3535 | break; |
| 3536 | default: |
| 3537 | /* scope can be 0 */ |
| 3538 | break; |
| 3539 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3541 | /* XXX Leave assert here, but handle __doc__ and the like better */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3542 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3544 | switch (optype) { |
| 3545 | case OP_DEREF: |
| 3546 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3547 | case Load: |
| 3548 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3549 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3550 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3551 | op = STORE_DEREF; |
| 3552 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3553 | case AugLoad: |
| 3554 | case AugStore: |
| 3555 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3556 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3557 | case Param: |
| 3558 | default: |
| 3559 | PyErr_SetString(PyExc_SystemError, |
| 3560 | "param invalid for deref variable"); |
| 3561 | return 0; |
| 3562 | } |
| 3563 | break; |
| 3564 | case OP_FAST: |
| 3565 | switch (ctx) { |
| 3566 | case Load: op = LOAD_FAST; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3567 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3568 | op = STORE_FAST; |
| 3569 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | case Del: op = DELETE_FAST; break; |
| 3571 | case AugLoad: |
| 3572 | case AugStore: |
| 3573 | break; |
| 3574 | case Param: |
| 3575 | default: |
| 3576 | PyErr_SetString(PyExc_SystemError, |
| 3577 | "param invalid for local variable"); |
| 3578 | return 0; |
| 3579 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3580 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3581 | return 1; |
| 3582 | case OP_GLOBAL: |
| 3583 | switch (ctx) { |
| 3584 | case Load: op = LOAD_GLOBAL; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3585 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3586 | op = STORE_GLOBAL; |
| 3587 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3588 | case Del: op = DELETE_GLOBAL; break; |
| 3589 | case AugLoad: |
| 3590 | case AugStore: |
| 3591 | break; |
| 3592 | case Param: |
| 3593 | default: |
| 3594 | PyErr_SetString(PyExc_SystemError, |
| 3595 | "param invalid for global variable"); |
| 3596 | return 0; |
| 3597 | } |
| 3598 | break; |
| 3599 | case OP_NAME: |
| 3600 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3601 | case Load: op = LOAD_NAME; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3602 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3603 | op = STORE_NAME; |
| 3604 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3605 | case Del: op = DELETE_NAME; break; |
| 3606 | case AugLoad: |
| 3607 | case AugStore: |
| 3608 | break; |
| 3609 | case Param: |
| 3610 | default: |
| 3611 | PyErr_SetString(PyExc_SystemError, |
| 3612 | "param invalid for name variable"); |
| 3613 | return 0; |
| 3614 | } |
| 3615 | break; |
| 3616 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | assert(op); |
| 3619 | arg = compiler_add_o(c, dict, mangled); |
| 3620 | Py_DECREF(mangled); |
| 3621 | if (arg < 0) |
| 3622 | return 0; |
| 3623 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3624 | } |
| 3625 | |
| 3626 | static int |
| 3627 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3628 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3629 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3630 | int jumpi; |
| 3631 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3632 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3633 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3634 | assert(e->kind == BoolOp_kind); |
| 3635 | if (e->v.BoolOp.op == And) |
| 3636 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3637 | else |
| 3638 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3639 | end = compiler_new_block(c); |
| 3640 | if (end == NULL) |
| 3641 | return 0; |
| 3642 | s = e->v.BoolOp.values; |
| 3643 | n = asdl_seq_LEN(s) - 1; |
| 3644 | assert(n >= 0); |
| 3645 | for (i = 0; i < n; ++i) { |
| 3646 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3647 | ADDOP_JABS(c, jumpi, end); |
| 3648 | } |
| 3649 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3650 | compiler_use_next_block(c, end); |
| 3651 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3652 | } |
| 3653 | |
| 3654 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3655 | starunpack_helper(struct compiler *c, asdl_seq *elts, |
| 3656 | int single_op, int inner_op, int outer_op) |
| 3657 | { |
| 3658 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3659 | Py_ssize_t i, nsubitems = 0, nseen = 0; |
| 3660 | for (i = 0; i < n; i++) { |
| 3661 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3662 | if (elt->kind == Starred_kind) { |
| 3663 | if (nseen) { |
| 3664 | ADDOP_I(c, inner_op, nseen); |
| 3665 | nseen = 0; |
| 3666 | nsubitems++; |
| 3667 | } |
| 3668 | VISIT(c, expr, elt->v.Starred.value); |
| 3669 | nsubitems++; |
| 3670 | } |
| 3671 | else { |
| 3672 | VISIT(c, expr, elt); |
| 3673 | nseen++; |
| 3674 | } |
| 3675 | } |
| 3676 | if (nsubitems) { |
| 3677 | if (nseen) { |
| 3678 | ADDOP_I(c, inner_op, nseen); |
| 3679 | nsubitems++; |
| 3680 | } |
| 3681 | ADDOP_I(c, outer_op, nsubitems); |
| 3682 | } |
| 3683 | else |
| 3684 | ADDOP_I(c, single_op, nseen); |
| 3685 | return 1; |
| 3686 | } |
| 3687 | |
| 3688 | static int |
| 3689 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3690 | { |
| 3691 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3692 | Py_ssize_t i; |
| 3693 | int seen_star = 0; |
| 3694 | for (i = 0; i < n; i++) { |
| 3695 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3696 | if (elt->kind == Starred_kind && !seen_star) { |
| 3697 | if ((i >= (1 << 8)) || |
| 3698 | (n-i-1 >= (INT_MAX >> 8))) |
| 3699 | return compiler_error(c, |
| 3700 | "too many expressions in " |
| 3701 | "star-unpacking assignment"); |
| 3702 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3703 | seen_star = 1; |
| 3704 | asdl_seq_SET(elts, i, elt->v.Starred.value); |
| 3705 | } |
| 3706 | else if (elt->kind == Starred_kind) { |
| 3707 | return compiler_error(c, |
| 3708 | "two starred expressions in assignment"); |
| 3709 | } |
| 3710 | } |
| 3711 | if (!seen_star) { |
| 3712 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3713 | } |
| 3714 | VISIT_SEQ(c, expr, elts); |
| 3715 | return 1; |
| 3716 | } |
| 3717 | |
| 3718 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3719 | compiler_list(struct compiler *c, expr_ty e) |
| 3720 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3721 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3722 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3723 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3724 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3725 | else if (e->v.List.ctx == Load) { |
| 3726 | return starunpack_helper(c, elts, |
| 3727 | BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3728 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3729 | else |
| 3730 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3731 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3732 | } |
| 3733 | |
| 3734 | static int |
| 3735 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3736 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3737 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3738 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3739 | return assignment_helper(c, elts); |
| 3740 | } |
| 3741 | else if (e->v.Tuple.ctx == Load) { |
| 3742 | return starunpack_helper(c, elts, |
| 3743 | BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK); |
| 3744 | } |
| 3745 | else |
| 3746 | VISIT_SEQ(c, expr, elts); |
| 3747 | return 1; |
| 3748 | } |
| 3749 | |
| 3750 | static int |
| 3751 | compiler_set(struct compiler *c, expr_ty e) |
| 3752 | { |
| 3753 | return starunpack_helper(c, e->v.Set.elts, BUILD_SET, |
| 3754 | BUILD_SET, BUILD_SET_UNPACK); |
| 3755 | } |
| 3756 | |
| 3757 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3758 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3759 | { |
| 3760 | Py_ssize_t i; |
| 3761 | for (i = begin; i < end; i++) { |
| 3762 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3763 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3764 | return 0; |
| 3765 | } |
| 3766 | return 1; |
| 3767 | } |
| 3768 | |
| 3769 | static int |
| 3770 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3771 | { |
| 3772 | Py_ssize_t i, n = end - begin; |
| 3773 | PyObject *keys, *key; |
| 3774 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3775 | for (i = begin; i < end; i++) { |
| 3776 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3777 | } |
| 3778 | keys = PyTuple_New(n); |
| 3779 | if (keys == NULL) { |
| 3780 | return 0; |
| 3781 | } |
| 3782 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3783 | key = ((expr_ty)asdl_seq_GET(e->v.Dict.keys, i))->v.Constant.value; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3784 | Py_INCREF(key); |
| 3785 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3786 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3787 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3788 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3789 | } |
| 3790 | else { |
| 3791 | for (i = begin; i < end; i++) { |
| 3792 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3793 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3794 | } |
| 3795 | ADDOP_I(c, BUILD_MAP, n); |
| 3796 | } |
| 3797 | return 1; |
| 3798 | } |
| 3799 | |
| 3800 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3801 | compiler_dict(struct compiler *c, expr_ty e) |
| 3802 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3803 | Py_ssize_t i, n, elements; |
| 3804 | int containers; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3805 | int is_unpacking = 0; |
| 3806 | n = asdl_seq_LEN(e->v.Dict.values); |
| 3807 | containers = 0; |
| 3808 | elements = 0; |
| 3809 | for (i = 0; i < n; i++) { |
| 3810 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
| 3811 | if (elements == 0xFFFF || (elements && is_unpacking)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3812 | if (!compiler_subdict(c, e, i - elements, i)) |
| 3813 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3814 | containers++; |
| 3815 | elements = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3816 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3817 | if (is_unpacking) { |
| 3818 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3819 | containers++; |
| 3820 | } |
| 3821 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3822 | elements++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3823 | } |
| 3824 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3825 | if (elements || containers == 0) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3826 | if (!compiler_subdict(c, e, n - elements, n)) |
| 3827 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3828 | containers++; |
| 3829 | } |
| 3830 | /* If there is more than one dict, they need to be merged into a new |
| 3831 | * dict. If there is one dict and it's an unpacking, then it needs |
| 3832 | * to be copied into a new dict." */ |
Serhiy Storchaka | 3d85fae | 2016-11-28 20:56:37 +0200 | [diff] [blame] | 3833 | if (containers > 1 || is_unpacking) { |
| 3834 | ADDOP_I(c, BUILD_MAP_UNPACK, containers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3835 | } |
| 3836 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3837 | } |
| 3838 | |
| 3839 | static int |
| 3840 | compiler_compare(struct compiler *c, expr_ty e) |
| 3841 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3842 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3843 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3844 | if (!check_compare(c, e)) { |
| 3845 | return 0; |
| 3846 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3847 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3848 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3849 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3850 | if (n == 0) { |
| 3851 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 3852 | ADDOP_I(c, COMPARE_OP, |
| 3853 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0)))); |
| 3854 | } |
| 3855 | else { |
| 3856 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3857 | if (cleanup == NULL) |
| 3858 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3859 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3860 | VISIT(c, expr, |
| 3861 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3862 | ADDOP(c, DUP_TOP); |
| 3863 | ADDOP(c, ROT_THREE); |
| 3864 | ADDOP_I(c, COMPARE_OP, |
| 3865 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 3866 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3867 | NEXT_BLOCK(c); |
| 3868 | } |
| 3869 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 3870 | ADDOP_I(c, COMPARE_OP, |
| 3871 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3872 | basicblock *end = compiler_new_block(c); |
| 3873 | if (end == NULL) |
| 3874 | return 0; |
| 3875 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3876 | compiler_use_next_block(c, cleanup); |
| 3877 | ADDOP(c, ROT_TWO); |
| 3878 | ADDOP(c, POP_TOP); |
| 3879 | compiler_use_next_block(c, end); |
| 3880 | } |
| 3881 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3882 | } |
| 3883 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3884 | static PyTypeObject * |
| 3885 | infer_type(expr_ty e) |
| 3886 | { |
| 3887 | switch (e->kind) { |
| 3888 | case Tuple_kind: |
| 3889 | return &PyTuple_Type; |
| 3890 | case List_kind: |
| 3891 | case ListComp_kind: |
| 3892 | return &PyList_Type; |
| 3893 | case Dict_kind: |
| 3894 | case DictComp_kind: |
| 3895 | return &PyDict_Type; |
| 3896 | case Set_kind: |
| 3897 | case SetComp_kind: |
| 3898 | return &PySet_Type; |
| 3899 | case GeneratorExp_kind: |
| 3900 | return &PyGen_Type; |
| 3901 | case Lambda_kind: |
| 3902 | return &PyFunction_Type; |
| 3903 | case JoinedStr_kind: |
| 3904 | case FormattedValue_kind: |
| 3905 | return &PyUnicode_Type; |
| 3906 | case Constant_kind: |
| 3907 | return e->v.Constant.value->ob_type; |
| 3908 | default: |
| 3909 | return NULL; |
| 3910 | } |
| 3911 | } |
| 3912 | |
| 3913 | static int |
| 3914 | check_caller(struct compiler *c, expr_ty e) |
| 3915 | { |
| 3916 | switch (e->kind) { |
| 3917 | case Constant_kind: |
| 3918 | case Tuple_kind: |
| 3919 | case List_kind: |
| 3920 | case ListComp_kind: |
| 3921 | case Dict_kind: |
| 3922 | case DictComp_kind: |
| 3923 | case Set_kind: |
| 3924 | case SetComp_kind: |
| 3925 | case GeneratorExp_kind: |
| 3926 | case JoinedStr_kind: |
| 3927 | case FormattedValue_kind: |
| 3928 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3929 | "perhaps you missed a comma?", |
| 3930 | infer_type(e)->tp_name); |
| 3931 | default: |
| 3932 | return 1; |
| 3933 | } |
| 3934 | } |
| 3935 | |
| 3936 | static int |
| 3937 | check_subscripter(struct compiler *c, expr_ty e) |
| 3938 | { |
| 3939 | PyObject *v; |
| 3940 | |
| 3941 | switch (e->kind) { |
| 3942 | case Constant_kind: |
| 3943 | v = e->v.Constant.value; |
| 3944 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3945 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3946 | PyAnySet_Check(v))) |
| 3947 | { |
| 3948 | return 1; |
| 3949 | } |
| 3950 | /* fall through */ |
| 3951 | case Set_kind: |
| 3952 | case SetComp_kind: |
| 3953 | case GeneratorExp_kind: |
| 3954 | case Lambda_kind: |
| 3955 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3956 | "perhaps you missed a comma?", |
| 3957 | infer_type(e)->tp_name); |
| 3958 | default: |
| 3959 | return 1; |
| 3960 | } |
| 3961 | } |
| 3962 | |
| 3963 | static int |
| 3964 | check_index(struct compiler *c, expr_ty e, slice_ty s) |
| 3965 | { |
| 3966 | PyObject *v; |
| 3967 | |
| 3968 | if (s->kind != Index_kind) { |
| 3969 | return 1; |
| 3970 | } |
| 3971 | PyTypeObject *index_type = infer_type(s->v.Index.value); |
| 3972 | if (index_type == NULL |
| 3973 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 3974 | || index_type == &PySlice_Type) { |
| 3975 | return 1; |
| 3976 | } |
| 3977 | |
| 3978 | switch (e->kind) { |
| 3979 | case Constant_kind: |
| 3980 | v = e->v.Constant.value; |
| 3981 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 3982 | return 1; |
| 3983 | } |
| 3984 | /* fall through */ |
| 3985 | case Tuple_kind: |
| 3986 | case List_kind: |
| 3987 | case ListComp_kind: |
| 3988 | case JoinedStr_kind: |
| 3989 | case FormattedValue_kind: |
| 3990 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 3991 | "not %.200s; " |
| 3992 | "perhaps you missed a comma?", |
| 3993 | infer_type(e)->tp_name, |
| 3994 | index_type->tp_name); |
| 3995 | default: |
| 3996 | return 1; |
| 3997 | } |
| 3998 | } |
| 3999 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4000 | // Return 1 if the method call was optimized, -1 if not, and 0 on error. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4001 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4002 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4003 | { |
| 4004 | Py_ssize_t argsl, i; |
| 4005 | expr_ty meth = e->v.Call.func; |
| 4006 | asdl_seq *args = e->v.Call.args; |
| 4007 | |
| 4008 | /* Check that the call node is an attribute access, and that |
| 4009 | the call doesn't have keyword parameters. */ |
| 4010 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4011 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4012 | return -1; |
| 4013 | |
| 4014 | /* Check that there are no *varargs types of arguments. */ |
| 4015 | argsl = asdl_seq_LEN(args); |
| 4016 | for (i = 0; i < argsl; i++) { |
| 4017 | expr_ty elt = asdl_seq_GET(args, i); |
| 4018 | if (elt->kind == Starred_kind) { |
| 4019 | return -1; |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | /* Alright, we can optimize the code. */ |
| 4024 | VISIT(c, expr, meth->v.Attribute.value); |
| 4025 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4026 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4027 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4028 | return 1; |
| 4029 | } |
| 4030 | |
| 4031 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4032 | compiler_call(struct compiler *c, expr_ty e) |
| 4033 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4034 | int ret = maybe_optimize_method_call(c, e); |
| 4035 | if (ret >= 0) { |
| 4036 | return ret; |
| 4037 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4038 | if (!check_caller(c, e->v.Call.func)) { |
| 4039 | return 0; |
| 4040 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4041 | VISIT(c, expr, e->v.Call.func); |
| 4042 | return compiler_call_helper(c, 0, |
| 4043 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4044 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4047 | static int |
| 4048 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4049 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4050 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4051 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4052 | ADDOP_I(c, BUILD_STRING, asdl_seq_LEN(e->v.JoinedStr.values)); |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4053 | return 1; |
| 4054 | } |
| 4055 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4056 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4057 | static int |
| 4058 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4059 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4060 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4061 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4062 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4063 | Convert the conversion char to 3 bits: |
| 4064 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4065 | !s : 001 0x1 FVC_STR |
| 4066 | !r : 010 0x2 FVC_REPR |
| 4067 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4068 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4069 | next bit is whether or not we have a format spec: |
| 4070 | yes : 100 0x4 |
| 4071 | no : 000 0x0 |
| 4072 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4073 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4074 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4075 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4076 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4077 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4078 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4079 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4080 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4081 | case 's': oparg = FVC_STR; break; |
| 4082 | case 'r': oparg = FVC_REPR; break; |
| 4083 | case 'a': oparg = FVC_ASCII; break; |
| 4084 | case -1: oparg = FVC_NONE; break; |
| 4085 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4086 | PyErr_Format(PyExc_SystemError, |
| 4087 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4088 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4089 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4090 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4091 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4092 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4093 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4094 | } |
| 4095 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4096 | /* And push our opcode and oparg */ |
| 4097 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4098 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4099 | return 1; |
| 4100 | } |
| 4101 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4102 | static int |
| 4103 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 4104 | { |
| 4105 | Py_ssize_t i, n = end - begin; |
| 4106 | keyword_ty kw; |
| 4107 | PyObject *keys, *key; |
| 4108 | assert(n > 0); |
| 4109 | if (n > 1) { |
| 4110 | for (i = begin; i < end; i++) { |
| 4111 | kw = asdl_seq_GET(keywords, i); |
| 4112 | VISIT(c, expr, kw->value); |
| 4113 | } |
| 4114 | keys = PyTuple_New(n); |
| 4115 | if (keys == NULL) { |
| 4116 | return 0; |
| 4117 | } |
| 4118 | for (i = begin; i < end; i++) { |
| 4119 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4120 | Py_INCREF(key); |
| 4121 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4122 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4123 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4124 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4125 | } |
| 4126 | else { |
| 4127 | /* a for loop only executes once */ |
| 4128 | for (i = begin; i < end; i++) { |
| 4129 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4130 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4131 | VISIT(c, expr, kw->value); |
| 4132 | } |
| 4133 | ADDOP_I(c, BUILD_MAP, n); |
| 4134 | } |
| 4135 | return 1; |
| 4136 | } |
| 4137 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4138 | /* shared code between compiler_call and compiler_class */ |
| 4139 | static int |
| 4140 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4141 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4142 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4143 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4144 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4145 | Py_ssize_t i, nseen, nelts, nkwelts; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4146 | int mustdictunpack = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4147 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4148 | /* the number of tuples and dictionaries on the stack */ |
| 4149 | Py_ssize_t nsubargs = 0, nsubkwargs = 0; |
| 4150 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4151 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4152 | nkwelts = asdl_seq_LEN(keywords); |
| 4153 | |
| 4154 | for (i = 0; i < nkwelts; i++) { |
| 4155 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4156 | if (kw->arg == NULL) { |
| 4157 | mustdictunpack = 1; |
| 4158 | break; |
| 4159 | } |
| 4160 | } |
| 4161 | |
| 4162 | nseen = n; /* the number of positional arguments on the stack */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4163 | for (i = 0; i < nelts; i++) { |
| 4164 | expr_ty elt = asdl_seq_GET(args, i); |
| 4165 | if (elt->kind == Starred_kind) { |
| 4166 | /* A star-arg. If we've seen positional arguments, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4167 | pack the positional arguments into a tuple. */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4168 | if (nseen) { |
| 4169 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4170 | nseen = 0; |
| 4171 | nsubargs++; |
| 4172 | } |
| 4173 | VISIT(c, expr, elt->v.Starred.value); |
| 4174 | nsubargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4175 | } |
| 4176 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4177 | VISIT(c, expr, elt); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4178 | nseen++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4179 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4180 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4181 | |
| 4182 | /* Same dance again for keyword arguments */ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4183 | if (nsubargs || mustdictunpack) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4184 | if (nseen) { |
| 4185 | /* Pack up any trailing positional arguments. */ |
| 4186 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4187 | nsubargs++; |
| 4188 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4189 | if (nsubargs > 1) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4190 | /* If we ended up with more than one stararg, we need |
| 4191 | to concatenate them into a single sequence. */ |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 4192 | ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4193 | } |
| 4194 | else if (nsubargs == 0) { |
| 4195 | ADDOP_I(c, BUILD_TUPLE, 0); |
| 4196 | } |
| 4197 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4198 | for (i = 0; i < nkwelts; i++) { |
| 4199 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4200 | if (kw->arg == NULL) { |
| 4201 | /* A keyword argument unpacking. */ |
| 4202 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4203 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) |
| 4204 | return 0; |
| 4205 | nsubkwargs++; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4206 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4207 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4208 | VISIT(c, expr, kw->value); |
| 4209 | nsubkwargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4210 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4211 | else { |
| 4212 | nseen++; |
| 4213 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4214 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4215 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4216 | /* Pack up any trailing keyword arguments. */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4217 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4218 | return 0; |
| 4219 | nsubkwargs++; |
| 4220 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4221 | if (nsubkwargs > 1) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4222 | /* Pack it all up */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4223 | ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4224 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4225 | ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); |
| 4226 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4227 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4228 | else if (nkwelts) { |
| 4229 | PyObject *names; |
| 4230 | VISIT_SEQ(c, keyword, keywords); |
| 4231 | names = PyTuple_New(nkwelts); |
| 4232 | if (names == NULL) { |
| 4233 | return 0; |
| 4234 | } |
| 4235 | for (i = 0; i < nkwelts; i++) { |
| 4236 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4237 | Py_INCREF(kw->arg); |
| 4238 | PyTuple_SET_ITEM(names, i, kw->arg); |
| 4239 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4240 | ADDOP_LOAD_CONST_NEW(c, names); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4241 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4242 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4243 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4244 | else { |
| 4245 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4246 | return 1; |
| 4247 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4248 | } |
| 4249 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4250 | |
| 4251 | /* List and set comprehensions and generator expressions work by creating a |
| 4252 | nested function to perform the actual iteration. This means that the |
| 4253 | iteration variables don't leak into the current scope. |
| 4254 | The defined function is called immediately following its definition, with the |
| 4255 | result of that call being the result of the expression. |
| 4256 | The LC/SC version returns the populated container, while the GE version is |
| 4257 | flagged in symtable.c as a generator, so it returns the generator object |
| 4258 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4259 | |
| 4260 | Possible cleanups: |
| 4261 | - iterate over the generator sequence instead of using recursion |
| 4262 | */ |
| 4263 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4264 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4265 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4266 | compiler_comprehension_generator(struct compiler *c, |
| 4267 | asdl_seq *generators, int gen_index, |
| 4268 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4269 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4270 | comprehension_ty gen; |
| 4271 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4272 | if (gen->is_async) { |
| 4273 | return compiler_async_comprehension_generator( |
| 4274 | c, generators, gen_index, elt, val, type); |
| 4275 | } else { |
| 4276 | return compiler_sync_comprehension_generator( |
| 4277 | c, generators, gen_index, elt, val, type); |
| 4278 | } |
| 4279 | } |
| 4280 | |
| 4281 | static int |
| 4282 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4283 | asdl_seq *generators, int gen_index, |
| 4284 | expr_ty elt, expr_ty val, int type) |
| 4285 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4286 | /* generate code for the iterator, then each of the ifs, |
| 4287 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4288 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4289 | comprehension_ty gen; |
| 4290 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4291 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4292 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4293 | start = compiler_new_block(c); |
| 4294 | skip = compiler_new_block(c); |
| 4295 | if_cleanup = compiler_new_block(c); |
| 4296 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4297 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4298 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4299 | anchor == NULL) |
| 4300 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4302 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4304 | if (gen_index == 0) { |
| 4305 | /* Receive outermost iter as an implicit argument */ |
| 4306 | c->u->u_argcount = 1; |
| 4307 | ADDOP_I(c, LOAD_FAST, 0); |
| 4308 | } |
| 4309 | else { |
| 4310 | /* Sub-iter - calculate on the fly */ |
| 4311 | VISIT(c, expr, gen->iter); |
| 4312 | ADDOP(c, GET_ITER); |
| 4313 | } |
| 4314 | compiler_use_next_block(c, start); |
| 4315 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4316 | NEXT_BLOCK(c); |
| 4317 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | /* XXX this needs to be cleaned up...a lot! */ |
| 4320 | n = asdl_seq_LEN(gen->ifs); |
| 4321 | for (i = 0; i < n; i++) { |
| 4322 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4323 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4324 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | NEXT_BLOCK(c); |
| 4326 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4328 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4329 | if (!compiler_comprehension_generator(c, |
| 4330 | generators, gen_index, |
| 4331 | elt, val, type)) |
| 4332 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4333 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4334 | /* only append after the last for generator */ |
| 4335 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4336 | /* comprehension specific code */ |
| 4337 | switch (type) { |
| 4338 | case COMP_GENEXP: |
| 4339 | VISIT(c, expr, elt); |
| 4340 | ADDOP(c, YIELD_VALUE); |
| 4341 | ADDOP(c, POP_TOP); |
| 4342 | break; |
| 4343 | case COMP_LISTCOMP: |
| 4344 | VISIT(c, expr, elt); |
| 4345 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4346 | break; |
| 4347 | case COMP_SETCOMP: |
| 4348 | VISIT(c, expr, elt); |
| 4349 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4350 | break; |
| 4351 | case COMP_DICTCOMP: |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4352 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4353 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | VISIT(c, expr, elt); |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4355 | VISIT(c, expr, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4356 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4357 | break; |
| 4358 | default: |
| 4359 | return 0; |
| 4360 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4361 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4362 | compiler_use_next_block(c, skip); |
| 4363 | } |
| 4364 | compiler_use_next_block(c, if_cleanup); |
| 4365 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4366 | compiler_use_next_block(c, anchor); |
| 4367 | |
| 4368 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4369 | } |
| 4370 | |
| 4371 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4372 | compiler_async_comprehension_generator(struct compiler *c, |
| 4373 | asdl_seq *generators, int gen_index, |
| 4374 | expr_ty elt, expr_ty val, int type) |
| 4375 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4376 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4377 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4378 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4379 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4380 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4381 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4382 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4383 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4384 | return 0; |
| 4385 | } |
| 4386 | |
| 4387 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4388 | |
| 4389 | if (gen_index == 0) { |
| 4390 | /* Receive outermost iter as an implicit argument */ |
| 4391 | c->u->u_argcount = 1; |
| 4392 | ADDOP_I(c, LOAD_FAST, 0); |
| 4393 | } |
| 4394 | else { |
| 4395 | /* Sub-iter - calculate on the fly */ |
| 4396 | VISIT(c, expr, gen->iter); |
| 4397 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4398 | } |
| 4399 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4400 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4401 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4402 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4403 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4404 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4405 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4406 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4407 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4408 | |
| 4409 | n = asdl_seq_LEN(gen->ifs); |
| 4410 | for (i = 0; i < n; i++) { |
| 4411 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4412 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4413 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4414 | NEXT_BLOCK(c); |
| 4415 | } |
| 4416 | |
| 4417 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4418 | if (!compiler_comprehension_generator(c, |
| 4419 | generators, gen_index, |
| 4420 | elt, val, type)) |
| 4421 | return 0; |
| 4422 | |
| 4423 | /* only append after the last for generator */ |
| 4424 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4425 | /* comprehension specific code */ |
| 4426 | switch (type) { |
| 4427 | case COMP_GENEXP: |
| 4428 | VISIT(c, expr, elt); |
| 4429 | ADDOP(c, YIELD_VALUE); |
| 4430 | ADDOP(c, POP_TOP); |
| 4431 | break; |
| 4432 | case COMP_LISTCOMP: |
| 4433 | VISIT(c, expr, elt); |
| 4434 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4435 | break; |
| 4436 | case COMP_SETCOMP: |
| 4437 | VISIT(c, expr, elt); |
| 4438 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4439 | break; |
| 4440 | case COMP_DICTCOMP: |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4441 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4442 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4443 | VISIT(c, expr, elt); |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4444 | VISIT(c, expr, val); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4445 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4446 | break; |
| 4447 | default: |
| 4448 | return 0; |
| 4449 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4450 | } |
| 4451 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4452 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4453 | |
| 4454 | compiler_use_next_block(c, except); |
| 4455 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4456 | |
| 4457 | return 1; |
| 4458 | } |
| 4459 | |
| 4460 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4461 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4462 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4463 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4464 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4465 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4466 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4467 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4468 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4469 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4470 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4471 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4472 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4473 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4474 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4475 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4476 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4477 | } |
| 4478 | |
| 4479 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4480 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4481 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4482 | compiler_error(c, "asynchronous comprehension outside of " |
| 4483 | "an asynchronous function"); |
| 4484 | goto error_in_scope; |
| 4485 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4486 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4487 | if (type != COMP_GENEXP) { |
| 4488 | int op; |
| 4489 | switch (type) { |
| 4490 | case COMP_LISTCOMP: |
| 4491 | op = BUILD_LIST; |
| 4492 | break; |
| 4493 | case COMP_SETCOMP: |
| 4494 | op = BUILD_SET; |
| 4495 | break; |
| 4496 | case COMP_DICTCOMP: |
| 4497 | op = BUILD_MAP; |
| 4498 | break; |
| 4499 | default: |
| 4500 | PyErr_Format(PyExc_SystemError, |
| 4501 | "unknown comprehension type %d", type); |
| 4502 | goto error_in_scope; |
| 4503 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4505 | ADDOP_I(c, op, 0); |
| 4506 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4507 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4508 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 4509 | val, type)) |
| 4510 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4511 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4512 | if (type != COMP_GENEXP) { |
| 4513 | ADDOP(c, RETURN_VALUE); |
| 4514 | } |
| 4515 | |
| 4516 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4517 | qualname = c->u->u_qualname; |
| 4518 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4519 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4520 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4521 | goto error; |
| 4522 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4523 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4524 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4525 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4526 | Py_DECREF(co); |
| 4527 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4528 | VISIT(c, expr, outermost->iter); |
| 4529 | |
| 4530 | if (outermost->is_async) { |
| 4531 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4532 | } else { |
| 4533 | ADDOP(c, GET_ITER); |
| 4534 | } |
| 4535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4536 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4537 | |
| 4538 | if (is_async_generator && type != COMP_GENEXP) { |
| 4539 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4540 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4541 | ADDOP(c, YIELD_FROM); |
| 4542 | } |
| 4543 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4544 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4545 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4546 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4547 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4548 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4549 | Py_XDECREF(co); |
| 4550 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4551 | } |
| 4552 | |
| 4553 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4554 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4555 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4556 | static identifier name; |
| 4557 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4558 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | if (!name) |
| 4560 | return 0; |
| 4561 | } |
| 4562 | assert(e->kind == GeneratorExp_kind); |
| 4563 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4564 | e->v.GeneratorExp.generators, |
| 4565 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4566 | } |
| 4567 | |
| 4568 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4569 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4570 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4571 | static identifier name; |
| 4572 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4573 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4574 | if (!name) |
| 4575 | return 0; |
| 4576 | } |
| 4577 | assert(e->kind == ListComp_kind); |
| 4578 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4579 | e->v.ListComp.generators, |
| 4580 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4581 | } |
| 4582 | |
| 4583 | static int |
| 4584 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4585 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4586 | static identifier name; |
| 4587 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4588 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4589 | if (!name) |
| 4590 | return 0; |
| 4591 | } |
| 4592 | assert(e->kind == SetComp_kind); |
| 4593 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4594 | e->v.SetComp.generators, |
| 4595 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4596 | } |
| 4597 | |
| 4598 | |
| 4599 | static int |
| 4600 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4601 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4602 | static identifier name; |
| 4603 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4604 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4605 | if (!name) |
| 4606 | return 0; |
| 4607 | } |
| 4608 | assert(e->kind == DictComp_kind); |
| 4609 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4610 | e->v.DictComp.generators, |
| 4611 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
| 4614 | |
| 4615 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4616 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4617 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4618 | VISIT(c, expr, k->value); |
| 4619 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4622 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4623 | whether they are true or false. |
| 4624 | |
| 4625 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4626 | */ |
| 4627 | |
| 4628 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4629 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4630 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4631 | if (e->kind == Constant_kind) { |
| 4632 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4633 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4634 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4637 | |
| 4638 | /* |
| 4639 | Implements the async with statement. |
| 4640 | |
| 4641 | The semantics outlined in that PEP are as follows: |
| 4642 | |
| 4643 | async with EXPR as VAR: |
| 4644 | BLOCK |
| 4645 | |
| 4646 | It is implemented roughly as: |
| 4647 | |
| 4648 | context = EXPR |
| 4649 | exit = context.__aexit__ # not calling it |
| 4650 | value = await context.__aenter__() |
| 4651 | try: |
| 4652 | VAR = value # if VAR present in the syntax |
| 4653 | BLOCK |
| 4654 | finally: |
| 4655 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4656 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4657 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4658 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4659 | if not (await exit(*exc)): |
| 4660 | raise |
| 4661 | */ |
| 4662 | static int |
| 4663 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4664 | { |
| 4665 | basicblock *block, *finally; |
| 4666 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4667 | |
| 4668 | assert(s->kind == AsyncWith_kind); |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4669 | if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ |
| 4670 | c->u->u_ste->ste_coroutine = 1; |
| 4671 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4672 | return compiler_error(c, "'async with' outside async function"); |
| 4673 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4674 | |
| 4675 | block = compiler_new_block(c); |
| 4676 | finally = compiler_new_block(c); |
| 4677 | if (!block || !finally) |
| 4678 | return 0; |
| 4679 | |
| 4680 | /* Evaluate EXPR */ |
| 4681 | VISIT(c, expr, item->context_expr); |
| 4682 | |
| 4683 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4684 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4685 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4686 | ADDOP(c, YIELD_FROM); |
| 4687 | |
| 4688 | ADDOP_JREL(c, SETUP_ASYNC_WITH, finally); |
| 4689 | |
| 4690 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4691 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4692 | if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4693 | return 0; |
| 4694 | } |
| 4695 | |
| 4696 | if (item->optional_vars) { |
| 4697 | VISIT(c, expr, item->optional_vars); |
| 4698 | } |
| 4699 | else { |
| 4700 | /* Discard result from context.__aenter__() */ |
| 4701 | ADDOP(c, POP_TOP); |
| 4702 | } |
| 4703 | |
| 4704 | pos++; |
| 4705 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4706 | /* BLOCK code */ |
| 4707 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4708 | else if (!compiler_async_with(c, s, pos)) |
| 4709 | return 0; |
| 4710 | |
| 4711 | /* End of try block; start the finally block */ |
| 4712 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4713 | ADDOP(c, BEGIN_FINALLY); |
| 4714 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4715 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4716 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4717 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4718 | return 0; |
| 4719 | |
| 4720 | /* Finally block starts; context.__exit__ is on the stack under |
| 4721 | the exception or return information. Just issue our magic |
| 4722 | opcode. */ |
| 4723 | ADDOP(c, WITH_CLEANUP_START); |
| 4724 | |
| 4725 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4726 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4727 | ADDOP(c, YIELD_FROM); |
| 4728 | |
| 4729 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 4730 | |
| 4731 | /* Finally block ends. */ |
| 4732 | ADDOP(c, END_FINALLY); |
| 4733 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4734 | return 1; |
| 4735 | } |
| 4736 | |
| 4737 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4738 | /* |
| 4739 | Implements the with statement from PEP 343. |
| 4740 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4741 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4742 | |
| 4743 | with EXPR as VAR: |
| 4744 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4745 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4746 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4747 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4748 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4749 | exit = context.__exit__ # not calling it |
| 4750 | value = context.__enter__() |
| 4751 | try: |
| 4752 | VAR = value # if VAR present in the syntax |
| 4753 | BLOCK |
| 4754 | finally: |
| 4755 | if an exception was raised: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4756 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4757 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4758 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4759 | exit(*exc) |
| 4760 | */ |
| 4761 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4762 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4763 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4764 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4765 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4766 | |
| 4767 | assert(s->kind == With_kind); |
| 4768 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4769 | block = compiler_new_block(c); |
| 4770 | finally = compiler_new_block(c); |
| 4771 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4772 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4773 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4774 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4775 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4776 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4777 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4778 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4779 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4780 | if (!compiler_push_fblock(c, WITH, block, finally)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4781 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4782 | } |
| 4783 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4784 | if (item->optional_vars) { |
| 4785 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4786 | } |
| 4787 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4788 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4789 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4790 | } |
| 4791 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4792 | pos++; |
| 4793 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4794 | /* BLOCK code */ |
| 4795 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4796 | else if (!compiler_with(c, s, pos)) |
| 4797 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4798 | |
| 4799 | /* End of try block; start the finally block */ |
| 4800 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4801 | ADDOP(c, BEGIN_FINALLY); |
| 4802 | compiler_pop_fblock(c, WITH, block); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4803 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4804 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4805 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4806 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4807 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 4808 | /* Finally block starts; context.__exit__ is on the stack under |
| 4809 | the exception or return information. Just issue our magic |
| 4810 | opcode. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4811 | ADDOP(c, WITH_CLEANUP_START); |
| 4812 | ADDOP(c, WITH_CLEANUP_FINISH); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4813 | |
| 4814 | /* Finally block ends. */ |
| 4815 | ADDOP(c, END_FINALLY); |
| 4816 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4817 | return 1; |
| 4818 | } |
| 4819 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4820 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4821 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4822 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4823 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4824 | case NamedExpr_kind: |
| 4825 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4826 | ADDOP(c, DUP_TOP); |
| 4827 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4828 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4829 | case BoolOp_kind: |
| 4830 | return compiler_boolop(c, e); |
| 4831 | case BinOp_kind: |
| 4832 | VISIT(c, expr, e->v.BinOp.left); |
| 4833 | VISIT(c, expr, e->v.BinOp.right); |
| 4834 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 4835 | break; |
| 4836 | case UnaryOp_kind: |
| 4837 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4838 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4839 | break; |
| 4840 | case Lambda_kind: |
| 4841 | return compiler_lambda(c, e); |
| 4842 | case IfExp_kind: |
| 4843 | return compiler_ifexp(c, e); |
| 4844 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4845 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4846 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4847 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4848 | case GeneratorExp_kind: |
| 4849 | return compiler_genexp(c, e); |
| 4850 | case ListComp_kind: |
| 4851 | return compiler_listcomp(c, e); |
| 4852 | case SetComp_kind: |
| 4853 | return compiler_setcomp(c, e); |
| 4854 | case DictComp_kind: |
| 4855 | return compiler_dictcomp(c, e); |
| 4856 | case Yield_kind: |
| 4857 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4858 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4859 | if (e->v.Yield.value) { |
| 4860 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4861 | } |
| 4862 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4863 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4864 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4865 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4866 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4867 | case YieldFrom_kind: |
| 4868 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4869 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4870 | |
| 4871 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4872 | return compiler_error(c, "'yield from' inside async function"); |
| 4873 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4874 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4875 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4876 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4877 | ADDOP(c, YIELD_FROM); |
| 4878 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4879 | case Await_kind: |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4880 | if (!(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT)){ |
| 4881 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 4882 | return compiler_error(c, "'await' outside function"); |
| 4883 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4884 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 4885 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4886 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 4887 | return compiler_error(c, "'await' outside async function"); |
| 4888 | } |
| 4889 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4890 | |
| 4891 | VISIT(c, expr, e->v.Await.value); |
| 4892 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4893 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4894 | ADDOP(c, YIELD_FROM); |
| 4895 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4896 | case Compare_kind: |
| 4897 | return compiler_compare(c, e); |
| 4898 | case Call_kind: |
| 4899 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4900 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4901 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4902 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4903 | case JoinedStr_kind: |
| 4904 | return compiler_joined_str(c, e); |
| 4905 | case FormattedValue_kind: |
| 4906 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | /* The following exprs can be assignment targets. */ |
| 4908 | case Attribute_kind: |
| 4909 | if (e->v.Attribute.ctx != AugStore) |
| 4910 | VISIT(c, expr, e->v.Attribute.value); |
| 4911 | switch (e->v.Attribute.ctx) { |
| 4912 | case AugLoad: |
| 4913 | ADDOP(c, DUP_TOP); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4914 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4915 | case Load: |
| 4916 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4917 | break; |
| 4918 | case AugStore: |
| 4919 | ADDOP(c, ROT_TWO); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4920 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4921 | case Store: |
| 4922 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4923 | break; |
| 4924 | case Del: |
| 4925 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4926 | break; |
| 4927 | case Param: |
| 4928 | default: |
| 4929 | PyErr_SetString(PyExc_SystemError, |
| 4930 | "param invalid in attribute expression"); |
| 4931 | return 0; |
| 4932 | } |
| 4933 | break; |
| 4934 | case Subscript_kind: |
| 4935 | switch (e->v.Subscript.ctx) { |
| 4936 | case AugLoad: |
| 4937 | VISIT(c, expr, e->v.Subscript.value); |
| 4938 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 4939 | break; |
| 4940 | case Load: |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4941 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 4942 | return 0; |
| 4943 | } |
| 4944 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 4945 | return 0; |
| 4946 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4947 | VISIT(c, expr, e->v.Subscript.value); |
| 4948 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 4949 | break; |
| 4950 | case AugStore: |
| 4951 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 4952 | break; |
| 4953 | case Store: |
| 4954 | VISIT(c, expr, e->v.Subscript.value); |
| 4955 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 4956 | break; |
| 4957 | case Del: |
| 4958 | VISIT(c, expr, e->v.Subscript.value); |
| 4959 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 4960 | break; |
| 4961 | case Param: |
| 4962 | default: |
| 4963 | PyErr_SetString(PyExc_SystemError, |
| 4964 | "param invalid in subscript expression"); |
| 4965 | return 0; |
| 4966 | } |
| 4967 | break; |
| 4968 | case Starred_kind: |
| 4969 | switch (e->v.Starred.ctx) { |
| 4970 | case Store: |
| 4971 | /* In all legitimate cases, the Starred node was already replaced |
| 4972 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 4973 | return compiler_error(c, |
| 4974 | "starred assignment target must be in a list or tuple"); |
| 4975 | default: |
| 4976 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4977 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4978 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4979 | case Name_kind: |
| 4980 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 4981 | /* child nodes of List and Tuple will have expr_context set */ |
| 4982 | case List_kind: |
| 4983 | return compiler_list(c, e); |
| 4984 | case Tuple_kind: |
| 4985 | return compiler_tuple(c, e); |
| 4986 | } |
| 4987 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4988 | } |
| 4989 | |
| 4990 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4991 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 4992 | { |
| 4993 | /* If expr e has a different line number than the last expr/stmt, |
| 4994 | set a new line number for the next instruction. |
| 4995 | */ |
| 4996 | int old_lineno = c->u->u_lineno; |
| 4997 | int old_col_offset = c->u->u_col_offset; |
| 4998 | if (e->lineno != c->u->u_lineno) { |
| 4999 | c->u->u_lineno = e->lineno; |
| 5000 | c->u->u_lineno_set = 0; |
| 5001 | } |
| 5002 | /* Updating the column offset is always harmless. */ |
| 5003 | c->u->u_col_offset = e->col_offset; |
| 5004 | |
| 5005 | int res = compiler_visit_expr1(c, e); |
| 5006 | |
| 5007 | if (old_lineno != c->u->u_lineno) { |
| 5008 | c->u->u_lineno = old_lineno; |
| 5009 | c->u->u_lineno_set = 0; |
| 5010 | } |
| 5011 | c->u->u_col_offset = old_col_offset; |
| 5012 | return res; |
| 5013 | } |
| 5014 | |
| 5015 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5016 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5017 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5018 | expr_ty e = s->v.AugAssign.target; |
| 5019 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5020 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5021 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5023 | switch (e->kind) { |
| 5024 | case Attribute_kind: |
| 5025 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5026 | AugLoad, e->lineno, e->col_offset, |
| 5027 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | if (auge == NULL) |
| 5029 | return 0; |
| 5030 | VISIT(c, expr, auge); |
| 5031 | VISIT(c, expr, s->v.AugAssign.value); |
| 5032 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 5033 | auge->v.Attribute.ctx = AugStore; |
| 5034 | VISIT(c, expr, auge); |
| 5035 | break; |
| 5036 | case Subscript_kind: |
| 5037 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5038 | AugLoad, e->lineno, e->col_offset, |
| 5039 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5040 | if (auge == NULL) |
| 5041 | return 0; |
| 5042 | VISIT(c, expr, auge); |
| 5043 | VISIT(c, expr, s->v.AugAssign.value); |
| 5044 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 5045 | auge->v.Subscript.ctx = AugStore; |
| 5046 | VISIT(c, expr, auge); |
| 5047 | break; |
| 5048 | case Name_kind: |
| 5049 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5050 | return 0; |
| 5051 | VISIT(c, expr, s->v.AugAssign.value); |
| 5052 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 5053 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5054 | default: |
| 5055 | PyErr_Format(PyExc_SystemError, |
| 5056 | "invalid node type (%d) for augmented assignment", |
| 5057 | e->kind); |
| 5058 | return 0; |
| 5059 | } |
| 5060 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5061 | } |
| 5062 | |
| 5063 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5064 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5065 | { |
| 5066 | VISIT(c, expr, e); |
| 5067 | ADDOP(c, POP_TOP); |
| 5068 | return 1; |
| 5069 | } |
| 5070 | |
| 5071 | static int |
| 5072 | check_annotation(struct compiler *c, stmt_ty s) |
| 5073 | { |
| 5074 | /* Annotations are only evaluated in a module or class. */ |
| 5075 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5076 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5077 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5078 | } |
| 5079 | return 1; |
| 5080 | } |
| 5081 | |
| 5082 | static int |
| 5083 | check_ann_slice(struct compiler *c, slice_ty sl) |
| 5084 | { |
| 5085 | switch(sl->kind) { |
| 5086 | case Index_kind: |
| 5087 | return check_ann_expr(c, sl->v.Index.value); |
| 5088 | case Slice_kind: |
| 5089 | if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { |
| 5090 | return 0; |
| 5091 | } |
| 5092 | if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { |
| 5093 | return 0; |
| 5094 | } |
| 5095 | if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { |
| 5096 | return 0; |
| 5097 | } |
| 5098 | break; |
| 5099 | default: |
| 5100 | PyErr_SetString(PyExc_SystemError, |
| 5101 | "unexpected slice kind"); |
| 5102 | return 0; |
| 5103 | } |
| 5104 | return 1; |
| 5105 | } |
| 5106 | |
| 5107 | static int |
| 5108 | check_ann_subscr(struct compiler *c, slice_ty sl) |
| 5109 | { |
| 5110 | /* We check that everything in a subscript is defined at runtime. */ |
| 5111 | Py_ssize_t i, n; |
| 5112 | |
| 5113 | switch (sl->kind) { |
| 5114 | case Index_kind: |
| 5115 | case Slice_kind: |
| 5116 | if (!check_ann_slice(c, sl)) { |
| 5117 | return 0; |
| 5118 | } |
| 5119 | break; |
| 5120 | case ExtSlice_kind: |
| 5121 | n = asdl_seq_LEN(sl->v.ExtSlice.dims); |
| 5122 | for (i = 0; i < n; i++) { |
| 5123 | slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i); |
| 5124 | switch (subsl->kind) { |
| 5125 | case Index_kind: |
| 5126 | case Slice_kind: |
| 5127 | if (!check_ann_slice(c, subsl)) { |
| 5128 | return 0; |
| 5129 | } |
| 5130 | break; |
| 5131 | case ExtSlice_kind: |
| 5132 | default: |
| 5133 | PyErr_SetString(PyExc_SystemError, |
| 5134 | "extended slice invalid in nested slice"); |
| 5135 | return 0; |
| 5136 | } |
| 5137 | } |
| 5138 | break; |
| 5139 | default: |
| 5140 | PyErr_Format(PyExc_SystemError, |
| 5141 | "invalid subscript kind %d", sl->kind); |
| 5142 | return 0; |
| 5143 | } |
| 5144 | return 1; |
| 5145 | } |
| 5146 | |
| 5147 | static int |
| 5148 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5149 | { |
| 5150 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5151 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5152 | |
| 5153 | assert(s->kind == AnnAssign_kind); |
| 5154 | |
| 5155 | /* We perform the actual assignment first. */ |
| 5156 | if (s->v.AnnAssign.value) { |
| 5157 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5158 | VISIT(c, expr, targ); |
| 5159 | } |
| 5160 | switch (targ->kind) { |
| 5161 | case Name_kind: |
| 5162 | /* If we have a simple name in a module or class, store annotation. */ |
| 5163 | if (s->v.AnnAssign.simple && |
| 5164 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5165 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5166 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5167 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5168 | } |
| 5169 | else { |
| 5170 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5171 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5172 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5173 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5174 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5175 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5176 | } |
| 5177 | break; |
| 5178 | case Attribute_kind: |
| 5179 | if (!s->v.AnnAssign.value && |
| 5180 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5181 | return 0; |
| 5182 | } |
| 5183 | break; |
| 5184 | case Subscript_kind: |
| 5185 | if (!s->v.AnnAssign.value && |
| 5186 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5187 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5188 | return 0; |
| 5189 | } |
| 5190 | break; |
| 5191 | default: |
| 5192 | PyErr_Format(PyExc_SystemError, |
| 5193 | "invalid node type (%d) for annotated assignment", |
| 5194 | targ->kind); |
| 5195 | return 0; |
| 5196 | } |
| 5197 | /* Annotation is evaluated last. */ |
| 5198 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5199 | return 0; |
| 5200 | } |
| 5201 | return 1; |
| 5202 | } |
| 5203 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5204 | /* Raises a SyntaxError and returns 0. |
| 5205 | If something goes wrong, a different exception may be raised. |
| 5206 | */ |
| 5207 | |
| 5208 | static int |
| 5209 | compiler_error(struct compiler *c, const char *errstr) |
| 5210 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5211 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5212 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5213 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5214 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5215 | if (!loc) { |
| 5216 | Py_INCREF(Py_None); |
| 5217 | loc = Py_None; |
| 5218 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5219 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5220 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | if (!u) |
| 5222 | goto exit; |
| 5223 | v = Py_BuildValue("(zO)", errstr, u); |
| 5224 | if (!v) |
| 5225 | goto exit; |
| 5226 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5227 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | Py_DECREF(loc); |
| 5229 | Py_XDECREF(u); |
| 5230 | Py_XDECREF(v); |
| 5231 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5234 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5235 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5236 | and returns 0. |
| 5237 | */ |
| 5238 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5239 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5240 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5241 | va_list vargs; |
| 5242 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5243 | va_start(vargs, format); |
| 5244 | #else |
| 5245 | va_start(vargs); |
| 5246 | #endif |
| 5247 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5248 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5249 | if (msg == NULL) { |
| 5250 | return 0; |
| 5251 | } |
| 5252 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5253 | c->u->u_lineno, NULL, NULL) < 0) |
| 5254 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5255 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5256 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5257 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5258 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5259 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5260 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5261 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5262 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5263 | return 0; |
| 5264 | } |
| 5265 | Py_DECREF(msg); |
| 5266 | return 1; |
| 5267 | } |
| 5268 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5269 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5270 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 5271 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5272 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5274 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5275 | /* XXX this code is duplicated */ |
| 5276 | switch (ctx) { |
| 5277 | case AugLoad: /* fall through to Load */ |
| 5278 | case Load: op = BINARY_SUBSCR; break; |
| 5279 | case AugStore:/* fall through to Store */ |
| 5280 | case Store: op = STORE_SUBSCR; break; |
| 5281 | case Del: op = DELETE_SUBSCR; break; |
| 5282 | case Param: |
| 5283 | PyErr_Format(PyExc_SystemError, |
| 5284 | "invalid %s kind %d in subscript\n", |
| 5285 | kind, ctx); |
| 5286 | return 0; |
| 5287 | } |
| 5288 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 5289 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5290 | } |
| 5291 | else if (ctx == AugStore) { |
| 5292 | ADDOP(c, ROT_THREE); |
| 5293 | } |
| 5294 | ADDOP(c, op); |
| 5295 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5296 | } |
| 5297 | |
| 5298 | static int |
| 5299 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5301 | int n = 2; |
| 5302 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5303 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5304 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5305 | if (s->v.Slice.lower) { |
| 5306 | VISIT(c, expr, s->v.Slice.lower); |
| 5307 | } |
| 5308 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5309 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5312 | if (s->v.Slice.upper) { |
| 5313 | VISIT(c, expr, s->v.Slice.upper); |
| 5314 | } |
| 5315 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5316 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | if (s->v.Slice.step) { |
| 5320 | n++; |
| 5321 | VISIT(c, expr, s->v.Slice.step); |
| 5322 | } |
| 5323 | ADDOP_I(c, BUILD_SLICE, n); |
| 5324 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5325 | } |
| 5326 | |
| 5327 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5328 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 5329 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5330 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5331 | switch (s->kind) { |
| 5332 | case Slice_kind: |
| 5333 | return compiler_slice(c, s, ctx); |
| 5334 | case Index_kind: |
| 5335 | VISIT(c, expr, s->v.Index.value); |
| 5336 | break; |
| 5337 | case ExtSlice_kind: |
| 5338 | default: |
| 5339 | PyErr_SetString(PyExc_SystemError, |
| 5340 | "extended slice invalid in nested slice"); |
| 5341 | return 0; |
| 5342 | } |
| 5343 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5344 | } |
| 5345 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5346 | static int |
| 5347 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5348 | { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 5349 | const char * kindname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5350 | switch (s->kind) { |
| 5351 | case Index_kind: |
| 5352 | kindname = "index"; |
| 5353 | if (ctx != AugStore) { |
| 5354 | VISIT(c, expr, s->v.Index.value); |
| 5355 | } |
| 5356 | break; |
| 5357 | case Slice_kind: |
| 5358 | kindname = "slice"; |
| 5359 | if (ctx != AugStore) { |
| 5360 | if (!compiler_slice(c, s, ctx)) |
| 5361 | return 0; |
| 5362 | } |
| 5363 | break; |
| 5364 | case ExtSlice_kind: |
| 5365 | kindname = "extended slice"; |
| 5366 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5367 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5368 | for (i = 0; i < n; i++) { |
| 5369 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 5370 | s->v.ExtSlice.dims, i); |
| 5371 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 5372 | return 0; |
| 5373 | } |
| 5374 | ADDOP_I(c, BUILD_TUPLE, n); |
| 5375 | } |
| 5376 | break; |
| 5377 | default: |
| 5378 | PyErr_Format(PyExc_SystemError, |
| 5379 | "invalid subscript kind %d", s->kind); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5383 | } |
| 5384 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5385 | /* End of the compiler section, beginning of the assembler section */ |
| 5386 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5387 | /* do depth-first search of basic block graph, starting with block. |
| 5388 | post records the block indices in post-order. |
| 5389 | |
| 5390 | XXX must handle implicit jumps from one block to next |
| 5391 | */ |
| 5392 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5393 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5395 | int a_offset; /* offset into bytecode */ |
| 5396 | int a_nblocks; /* number of reachable blocks */ |
| 5397 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 5398 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5399 | int a_lnotab_off; /* offset into lnotab */ |
| 5400 | int a_lineno; /* last lineno of emitted instruction */ |
| 5401 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5402 | }; |
| 5403 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5404 | static void |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5405 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5406 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5407 | int i, j; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5408 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5409 | /* Get rid of recursion for normal control flow. |
| 5410 | Since the number of blocks is limited, unused space in a_postorder |
| 5411 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5412 | blocks. */ |
| 5413 | for (j = end; b && !b->b_seen; b = b->b_next) { |
| 5414 | b->b_seen = 1; |
| 5415 | assert(a->a_nblocks < j); |
| 5416 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5417 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5418 | while (j < end) { |
| 5419 | b = a->a_postorder[j++]; |
| 5420 | for (i = 0; i < b->b_iused; i++) { |
| 5421 | struct instr *instr = &b->b_instr[i]; |
| 5422 | if (instr->i_jrel || instr->i_jabs) |
| 5423 | dfs(c, instr->i_target, a, j); |
| 5424 | } |
| 5425 | assert(a->a_nblocks < j); |
| 5426 | a->a_postorder[a->a_nblocks++] = b; |
| 5427 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5428 | } |
| 5429 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5430 | Py_LOCAL_INLINE(void) |
| 5431 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5432 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5433 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5434 | if (b->b_startdepth < depth) { |
| 5435 | assert(b->b_startdepth < 0); |
| 5436 | b->b_startdepth = depth; |
| 5437 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5438 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5439 | } |
| 5440 | |
| 5441 | /* Find the flow path that needs the largest stack. We assume that |
| 5442 | * cycles in the flow graph have no net effect on the stack depth. |
| 5443 | */ |
| 5444 | static int |
| 5445 | stackdepth(struct compiler *c) |
| 5446 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5447 | basicblock *b, *entryblock = NULL; |
| 5448 | basicblock **stack, **sp; |
| 5449 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5450 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5451 | b->b_startdepth = INT_MIN; |
| 5452 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5453 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5454 | } |
| 5455 | if (!entryblock) |
| 5456 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5457 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5458 | if (!stack) { |
| 5459 | PyErr_NoMemory(); |
| 5460 | return -1; |
| 5461 | } |
| 5462 | |
| 5463 | sp = stack; |
| 5464 | stackdepth_push(&sp, entryblock, 0); |
| 5465 | while (sp != stack) { |
| 5466 | b = *--sp; |
| 5467 | int depth = b->b_startdepth; |
| 5468 | assert(depth >= 0); |
| 5469 | basicblock *next = b->b_next; |
| 5470 | for (int i = 0; i < b->b_iused; i++) { |
| 5471 | struct instr *instr = &b->b_instr[i]; |
| 5472 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5473 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5474 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5475 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5476 | } |
| 5477 | int new_depth = depth + effect; |
| 5478 | if (new_depth > maxdepth) { |
| 5479 | maxdepth = new_depth; |
| 5480 | } |
| 5481 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5482 | if (instr->i_jrel || instr->i_jabs) { |
| 5483 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5484 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5485 | int target_depth = depth + effect; |
| 5486 | if (target_depth > maxdepth) { |
| 5487 | maxdepth = target_depth; |
| 5488 | } |
| 5489 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5490 | if (instr->i_opcode == CALL_FINALLY) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5491 | assert(instr->i_target->b_startdepth >= 0); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5492 | assert(instr->i_target->b_startdepth >= target_depth); |
| 5493 | depth = new_depth; |
| 5494 | continue; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5495 | } |
| 5496 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5497 | } |
| 5498 | depth = new_depth; |
| 5499 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5500 | instr->i_opcode == JUMP_FORWARD || |
| 5501 | instr->i_opcode == RETURN_VALUE || |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5502 | instr->i_opcode == RAISE_VARARGS) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5503 | { |
| 5504 | /* remaining code is dead */ |
| 5505 | next = NULL; |
| 5506 | break; |
| 5507 | } |
| 5508 | } |
| 5509 | if (next != NULL) { |
| 5510 | stackdepth_push(&sp, next, depth); |
| 5511 | } |
| 5512 | } |
| 5513 | PyObject_Free(stack); |
| 5514 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5515 | } |
| 5516 | |
| 5517 | static int |
| 5518 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5519 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5520 | memset(a, 0, sizeof(struct assembler)); |
| 5521 | a->a_lineno = firstlineno; |
| 5522 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5523 | if (!a->a_bytecode) |
| 5524 | return 0; |
| 5525 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5526 | if (!a->a_lnotab) |
| 5527 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5528 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5529 | PyErr_NoMemory(); |
| 5530 | return 0; |
| 5531 | } |
| 5532 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 5533 | sizeof(basicblock *) * nblocks); |
| 5534 | if (!a->a_postorder) { |
| 5535 | PyErr_NoMemory(); |
| 5536 | return 0; |
| 5537 | } |
| 5538 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5539 | } |
| 5540 | |
| 5541 | static void |
| 5542 | assemble_free(struct assembler *a) |
| 5543 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5544 | Py_XDECREF(a->a_bytecode); |
| 5545 | Py_XDECREF(a->a_lnotab); |
| 5546 | if (a->a_postorder) |
| 5547 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5548 | } |
| 5549 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5550 | static int |
| 5551 | blocksize(basicblock *b) |
| 5552 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5553 | int i; |
| 5554 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5556 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5557 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5558 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5559 | } |
| 5560 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5561 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5562 | the instruction's bytecode offset and line number. See |
| 5563 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5564 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5565 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5566 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5567 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5568 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5569 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5570 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5571 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5572 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5573 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5574 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5575 | assert(d_bytecode >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5576 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | if(d_bytecode == 0 && d_lineno == 0) |
| 5578 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5579 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5580 | if (d_bytecode > 255) { |
| 5581 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5582 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5583 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5584 | if (nbytes >= len) { |
| 5585 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5586 | len = nbytes; |
| 5587 | else if (len <= INT_MAX / 2) |
| 5588 | len *= 2; |
| 5589 | else { |
| 5590 | PyErr_NoMemory(); |
| 5591 | return 0; |
| 5592 | } |
| 5593 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5594 | return 0; |
| 5595 | } |
| 5596 | lnotab = (unsigned char *) |
| 5597 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5598 | for (j = 0; j < ncodes; j++) { |
| 5599 | *lnotab++ = 255; |
| 5600 | *lnotab++ = 0; |
| 5601 | } |
| 5602 | d_bytecode -= ncodes * 255; |
| 5603 | a->a_lnotab_off += ncodes * 2; |
| 5604 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5605 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5606 | |
| 5607 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5608 | int j, nbytes, ncodes, k; |
| 5609 | if (d_lineno < 0) { |
| 5610 | k = -128; |
| 5611 | /* use division on positive numbers */ |
| 5612 | ncodes = (-d_lineno) / 128; |
| 5613 | } |
| 5614 | else { |
| 5615 | k = 127; |
| 5616 | ncodes = d_lineno / 127; |
| 5617 | } |
| 5618 | d_lineno -= ncodes * k; |
| 5619 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5620 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5621 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5622 | if (nbytes >= len) { |
| 5623 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5624 | len = nbytes; |
| 5625 | else if (len <= INT_MAX / 2) |
| 5626 | len *= 2; |
| 5627 | else { |
| 5628 | PyErr_NoMemory(); |
| 5629 | return 0; |
| 5630 | } |
| 5631 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5632 | return 0; |
| 5633 | } |
| 5634 | lnotab = (unsigned char *) |
| 5635 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5636 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5637 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5638 | d_bytecode = 0; |
| 5639 | for (j = 1; j < ncodes; j++) { |
| 5640 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5641 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5642 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5643 | a->a_lnotab_off += ncodes * 2; |
| 5644 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5645 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5647 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5648 | if (a->a_lnotab_off + 2 >= len) { |
| 5649 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5650 | return 0; |
| 5651 | } |
| 5652 | lnotab = (unsigned char *) |
| 5653 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5655 | a->a_lnotab_off += 2; |
| 5656 | if (d_bytecode) { |
| 5657 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5658 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5659 | } |
| 5660 | else { /* First line of a block; def stmt, etc. */ |
| 5661 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5662 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5663 | } |
| 5664 | a->a_lineno = i->i_lineno; |
| 5665 | a->a_lineno_off = a->a_offset; |
| 5666 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5667 | } |
| 5668 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5669 | /* assemble_emit() |
| 5670 | Extend the bytecode with a new instruction. |
| 5671 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5672 | */ |
| 5673 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5674 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5675 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5676 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5677 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5679 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5680 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5681 | arg = i->i_oparg; |
| 5682 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5683 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5684 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5685 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5686 | if (len > PY_SSIZE_T_MAX / 2) |
| 5687 | return 0; |
| 5688 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5689 | return 0; |
| 5690 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5691 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5693 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5695 | } |
| 5696 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5697 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5698 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5700 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5701 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5702 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5703 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5704 | /* Compute the size of each block and fixup jump args. |
| 5705 | Replace block pointer with position in bytecode. */ |
| 5706 | do { |
| 5707 | totsize = 0; |
| 5708 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5709 | b = a->a_postorder[i]; |
| 5710 | bsize = blocksize(b); |
| 5711 | b->b_offset = totsize; |
| 5712 | totsize += bsize; |
| 5713 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5714 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5715 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5716 | bsize = b->b_offset; |
| 5717 | for (i = 0; i < b->b_iused; i++) { |
| 5718 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5719 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5720 | /* Relative jumps are computed relative to |
| 5721 | the instruction pointer after fetching |
| 5722 | the jump instruction. |
| 5723 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5724 | bsize += isize; |
| 5725 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5726 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5727 | if (instr->i_jrel) { |
| 5728 | instr->i_oparg -= bsize; |
| 5729 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5730 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5731 | if (instrsize(instr->i_oparg) != isize) { |
| 5732 | extended_arg_recompile = 1; |
| 5733 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5734 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5735 | } |
| 5736 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5738 | /* XXX: This is an awful hack that could hurt performance, but |
| 5739 | on the bright side it should work until we come up |
| 5740 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5741 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5742 | The issue is that in the first loop blocksize() is called |
| 5743 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5744 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5745 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5747 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5748 | The only EXTENDED_ARGs that could be popping up are |
| 5749 | ones in jump instructions. So this should converge |
| 5750 | fairly quickly. |
| 5751 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5752 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5753 | } |
| 5754 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5755 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5756 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5757 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5758 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5759 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5760 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5761 | tuple = PyTuple_New(size); |
| 5762 | if (tuple == NULL) |
| 5763 | return NULL; |
| 5764 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5765 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5766 | Py_INCREF(k); |
| 5767 | assert((i - offset) < size); |
| 5768 | assert((i - offset) >= 0); |
| 5769 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5770 | } |
| 5771 | return tuple; |
| 5772 | } |
| 5773 | |
| 5774 | static PyObject * |
| 5775 | consts_dict_keys_inorder(PyObject *dict) |
| 5776 | { |
| 5777 | PyObject *consts, *k, *v; |
| 5778 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5779 | |
| 5780 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5781 | if (consts == NULL) |
| 5782 | return NULL; |
| 5783 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5784 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5785 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5786 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5787 | * the object we want is always second. */ |
| 5788 | if (PyTuple_CheckExact(k)) { |
| 5789 | k = PyTuple_GET_ITEM(k, 1); |
| 5790 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5791 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5792 | assert(i < size); |
| 5793 | assert(i >= 0); |
| 5794 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5795 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5796 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5797 | } |
| 5798 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5799 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5800 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5801 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5802 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5803 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5804 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5805 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5806 | if (ste->ste_nested) |
| 5807 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5808 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5809 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5810 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5811 | flags |= CO_COROUTINE; |
| 5812 | if (ste->ste_generator && ste->ste_coroutine) |
| 5813 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | if (ste->ste_varargs) |
| 5815 | flags |= CO_VARARGS; |
| 5816 | if (ste->ste_varkeywords) |
| 5817 | flags |= CO_VARKEYWORDS; |
| 5818 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5819 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5820 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5821 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5822 | |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5823 | if ((c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) && |
| 5824 | ste->ste_coroutine && |
| 5825 | !ste->ste_generator) { |
| 5826 | flags |= CO_COROUTINE; |
| 5827 | } |
| 5828 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5829 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5830 | } |
| 5831 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5832 | // Merge *tuple* with constant cache. |
| 5833 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5834 | static int |
| 5835 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5836 | { |
| 5837 | assert(PyTuple_CheckExact(*tuple)); |
| 5838 | |
| 5839 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5840 | if (key == NULL) { |
| 5841 | return 0; |
| 5842 | } |
| 5843 | |
| 5844 | // t is borrowed reference |
| 5845 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5846 | Py_DECREF(key); |
| 5847 | if (t == NULL) { |
| 5848 | return 0; |
| 5849 | } |
| 5850 | if (t == key) { // tuple is new constant. |
| 5851 | return 1; |
| 5852 | } |
| 5853 | |
| 5854 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5855 | Py_INCREF(u); |
| 5856 | Py_DECREF(*tuple); |
| 5857 | *tuple = u; |
| 5858 | return 1; |
| 5859 | } |
| 5860 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5861 | static PyCodeObject * |
| 5862 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5863 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5864 | PyObject *tmp; |
| 5865 | PyCodeObject *co = NULL; |
| 5866 | PyObject *consts = NULL; |
| 5867 | PyObject *names = NULL; |
| 5868 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5869 | PyObject *name = NULL; |
| 5870 | PyObject *freevars = NULL; |
| 5871 | PyObject *cellvars = NULL; |
| 5872 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5873 | Py_ssize_t nlocals; |
| 5874 | int nlocals_int; |
| 5875 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5876 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5877 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5878 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5879 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5880 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5881 | if (!consts || !names || !varnames) |
| 5882 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5883 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5884 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5885 | if (!cellvars) |
| 5886 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5887 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5888 | if (!freevars) |
| 5889 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5890 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5891 | if (!merge_const_tuple(c, &names) || |
| 5892 | !merge_const_tuple(c, &varnames) || |
| 5893 | !merge_const_tuple(c, &cellvars) || |
| 5894 | !merge_const_tuple(c, &freevars)) |
| 5895 | { |
| 5896 | goto error; |
| 5897 | } |
| 5898 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5899 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5900 | assert(nlocals < INT_MAX); |
| 5901 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5903 | flags = compute_code_flags(c); |
| 5904 | if (flags < 0) |
| 5905 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5907 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5908 | if (!bytecode) |
| 5909 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5910 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5911 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5912 | if (!tmp) |
| 5913 | goto error; |
| 5914 | Py_DECREF(consts); |
| 5915 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5916 | if (!merge_const_tuple(c, &consts)) { |
| 5917 | goto error; |
| 5918 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5920 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5921 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5922 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5923 | maxdepth = stackdepth(c); |
| 5924 | if (maxdepth < 0) { |
| 5925 | goto error; |
| 5926 | } |
Miss Islington (bot) | cb083f7 | 2019-07-01 04:29:14 -0700 | [diff] [blame] | 5927 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
| 5928 | posonlyargcount, kwonlyargcount, nlocals_int, |
| 5929 | maxdepth, flags, bytecode, consts, names, |
| 5930 | varnames, freevars, cellvars, c->c_filename, |
| 5931 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5932 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5933 | Py_XDECREF(consts); |
| 5934 | Py_XDECREF(names); |
| 5935 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5936 | Py_XDECREF(name); |
| 5937 | Py_XDECREF(freevars); |
| 5938 | Py_XDECREF(cellvars); |
| 5939 | Py_XDECREF(bytecode); |
| 5940 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5941 | } |
| 5942 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5943 | |
| 5944 | /* For debugging purposes only */ |
| 5945 | #if 0 |
| 5946 | static void |
| 5947 | dump_instr(const struct instr *i) |
| 5948 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5949 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5950 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5951 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5953 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5954 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5955 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5956 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5957 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5958 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5959 | } |
| 5960 | |
| 5961 | static void |
| 5962 | dump_basicblock(const basicblock *b) |
| 5963 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5964 | const char *seen = b->b_seen ? "seen " : ""; |
| 5965 | const char *b_return = b->b_return ? "return " : ""; |
| 5966 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5967 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5968 | if (b->b_instr) { |
| 5969 | int i; |
| 5970 | for (i = 0; i < b->b_iused; i++) { |
| 5971 | fprintf(stderr, " [%02d] ", i); |
| 5972 | dump_instr(b->b_instr + i); |
| 5973 | } |
| 5974 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5975 | } |
| 5976 | #endif |
| 5977 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5978 | static PyCodeObject * |
| 5979 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5980 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5981 | basicblock *b, *entryblock; |
| 5982 | struct assembler a; |
| 5983 | int i, j, nblocks; |
| 5984 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | /* Make sure every block that falls off the end returns None. |
| 5987 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5988 | block ends with a jump or return b_next shouldn't set. |
| 5989 | */ |
| 5990 | if (!c->u->u_curblock->b_return) { |
| 5991 | NEXT_BLOCK(c); |
| 5992 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5993 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5994 | ADDOP(c, RETURN_VALUE); |
| 5995 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5996 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5997 | nblocks = 0; |
| 5998 | entryblock = NULL; |
| 5999 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6000 | nblocks++; |
| 6001 | entryblock = b; |
| 6002 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6003 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6004 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6005 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 6006 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6007 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 6008 | else |
| 6009 | c->u->u_firstlineno = 1; |
| 6010 | } |
| 6011 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6012 | goto error; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6013 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6015 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6016 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6017 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6018 | /* Emit code in reverse postorder from dfs. */ |
| 6019 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 6020 | b = a.a_postorder[i]; |
| 6021 | for (j = 0; j < b->b_iused; j++) |
| 6022 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6023 | goto error; |
| 6024 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6025 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6026 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6027 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6028 | if (_PyBytes_Resize(&a.a_bytecode, a.a_offset * sizeof(_Py_CODEUNIT)) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6029 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6031 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6032 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6033 | assemble_free(&a); |
| 6034 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6035 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6036 | |
| 6037 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6038 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6039 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6040 | PyArena *arena) |
| 6041 | { |
| 6042 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6043 | } |