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 |
| 2739 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2740 | END_DO_NOT_EMIT_BYTECODE |
| 2741 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2742 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Miss Islington (bot) | 9ea738e | 2019-07-29 07:47:30 -0700 | [diff] [blame] | 2743 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2744 | return 1; |
| 2745 | } |
| 2746 | loop = compiler_new_block(c); |
| 2747 | end = compiler_new_block(c); |
| 2748 | if (constant == -1) { |
| 2749 | anchor = compiler_new_block(c); |
| 2750 | if (anchor == NULL) |
| 2751 | return 0; |
| 2752 | } |
| 2753 | if (loop == NULL || end == NULL) |
| 2754 | return 0; |
| 2755 | if (s->v.While.orelse) { |
| 2756 | orelse = compiler_new_block(c); |
| 2757 | if (orelse == NULL) |
| 2758 | return 0; |
| 2759 | } |
| 2760 | else |
| 2761 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2762 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | compiler_use_next_block(c, loop); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2764 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2765 | return 0; |
| 2766 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2767 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2768 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2769 | } |
| 2770 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2771 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2773 | /* XXX should the two POP instructions be in a separate block |
| 2774 | if there is no else clause ? |
| 2775 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2776 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2777 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2778 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2779 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2780 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2781 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2782 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2783 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2784 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2785 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2786 | } |
| 2787 | |
| 2788 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2789 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2790 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2791 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2792 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2793 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2794 | return compiler_error(c, "'return' outside function"); |
| 2795 | if (s->v.Return.value != NULL && |
| 2796 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2797 | { |
| 2798 | return compiler_error( |
| 2799 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2801 | if (preserve_tos) { |
| 2802 | VISIT(c, expr, s->v.Return.value); |
| 2803 | } |
| 2804 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2805 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2806 | |
| 2807 | if (!compiler_unwind_fblock(c, info, preserve_tos)) |
| 2808 | return 0; |
| 2809 | } |
| 2810 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2811 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2812 | } |
| 2813 | else if (!preserve_tos) { |
| 2814 | VISIT(c, expr, s->v.Return.value); |
| 2815 | } |
| 2816 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2817 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2818 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2819 | } |
| 2820 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2821 | static int |
| 2822 | compiler_break(struct compiler *c) |
| 2823 | { |
| 2824 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2825 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2826 | |
| 2827 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2828 | return 0; |
| 2829 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2830 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_exit); |
| 2831 | return 1; |
| 2832 | } |
| 2833 | } |
| 2834 | return compiler_error(c, "'break' outside loop"); |
| 2835 | } |
| 2836 | |
| 2837 | static int |
| 2838 | compiler_continue(struct compiler *c) |
| 2839 | { |
| 2840 | for (int depth = c->u->u_nfblocks; depth--;) { |
| 2841 | struct fblockinfo *info = &c->u->u_fblock[depth]; |
| 2842 | |
| 2843 | if (info->fb_type == WHILE_LOOP || info->fb_type == FOR_LOOP) { |
| 2844 | ADDOP_JABS(c, JUMP_ABSOLUTE, info->fb_block); |
| 2845 | return 1; |
| 2846 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2847 | if (!compiler_unwind_fblock(c, info, 0)) |
| 2848 | return 0; |
| 2849 | } |
| 2850 | return compiler_error(c, "'continue' not properly in loop"); |
| 2851 | } |
| 2852 | |
| 2853 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2854 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2855 | |
| 2856 | SETUP_FINALLY L |
| 2857 | <code for body> |
| 2858 | POP_BLOCK |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2859 | BEGIN_FINALLY |
| 2860 | L: |
| 2861 | <code for finalbody> |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2862 | END_FINALLY |
| 2863 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2864 | The special instructions use the block stack. Each block |
| 2865 | stack entry contains the instruction that created it (here |
| 2866 | SETUP_FINALLY), the level of the value stack at the time the |
| 2867 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2868 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | Pushes the current value stack level and the label |
| 2871 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2872 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2873 | Pops en entry from the block stack. |
| 2874 | BEGIN_FINALLY |
| 2875 | Pushes NULL onto the value stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2876 | END_FINALLY: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2877 | Pops 1 (NULL or int) or 6 entries from the *value* stack and restore |
| 2878 | the raised and the caught exceptions they specify. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2879 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2880 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2881 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2882 | exceptions are pushed onto the value stack (and the exception |
| 2883 | condition is cleared), and the interpreter jumps to the label |
| 2884 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2885 | */ |
| 2886 | |
| 2887 | static int |
| 2888 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2889 | { |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2890 | basicblock *start, *newcurblock, *body, *end; |
| 2891 | int break_finally = 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2893 | body = compiler_new_block(c); |
| 2894 | end = compiler_new_block(c); |
| 2895 | if (body == NULL || end == NULL) |
| 2896 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2897 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2898 | start = c->u->u_curblock; |
| 2899 | |
| 2900 | /* `finally` block. Compile it first to determine if any of "break", |
| 2901 | "continue" or "return" are used in it. */ |
| 2902 | compiler_use_next_block(c, end); |
| 2903 | if (!compiler_push_fblock(c, FINALLY_END, end, end)) |
| 2904 | return 0; |
| 2905 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2906 | ADDOP(c, END_FINALLY); |
| 2907 | break_finally = (c->u->u_fblock[c->u->u_nfblocks - 1].fb_exit == NULL); |
| 2908 | if (break_finally) { |
| 2909 | /* Pops a placeholder. See below */ |
| 2910 | ADDOP(c, POP_TOP); |
| 2911 | } |
| 2912 | compiler_pop_fblock(c, FINALLY_END, end); |
| 2913 | |
| 2914 | newcurblock = c->u->u_curblock; |
| 2915 | c->u->u_curblock = start; |
| 2916 | start->b_next = NULL; |
| 2917 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2918 | /* `try` block */ |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2919 | c->u->u_lineno_set = 0; |
| 2920 | c->u->u_lineno = s->lineno; |
| 2921 | c->u->u_col_offset = s->col_offset; |
| 2922 | if (break_finally) { |
| 2923 | /* Pushes a placeholder for the value of "return" in the "try" block |
| 2924 | to balance the stack for "break", "continue" and "return" in |
| 2925 | the "finally" block. */ |
| 2926 | ADDOP_LOAD_CONST(c, Py_None); |
| 2927 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2929 | compiler_use_next_block(c, body); |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2930 | 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] | 2931 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2932 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2933 | if (!compiler_try_except(c, s)) |
| 2934 | return 0; |
| 2935 | } |
| 2936 | else { |
| 2937 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2938 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2939 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2940 | ADDOP(c, BEGIN_FINALLY); |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2941 | compiler_pop_fblock(c, break_finally ? FINALLY_TRY2 : FINALLY_TRY, body); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2942 | |
Serhiy Storchaka | ed146b5 | 2019-08-24 13:41:53 +0300 | [diff] [blame] | 2943 | c->u->u_curblock->b_next = end; |
| 2944 | c->u->u_curblock = newcurblock; |
| 2945 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2946 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2947 | } |
| 2948 | |
| 2949 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2950 | 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] | 2951 | (The contents of the value stack is shown in [], with the top |
| 2952 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2953 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2954 | |
| 2955 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2956 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | [] <code for S> |
| 2958 | [] POP_BLOCK |
| 2959 | [] JUMP_FORWARD L0 |
| 2960 | |
| 2961 | [tb, val, exc] L1: DUP ) |
| 2962 | [tb, val, exc, exc] <evaluate E1> ) |
| 2963 | [tb, val, exc, exc, E1] COMPARE_OP EXC_MATCH ) only if E1 |
| 2964 | [tb, val, exc, 1-or-0] POP_JUMP_IF_FALSE L2 ) |
| 2965 | [tb, val, exc] POP |
| 2966 | [tb, val] <assign to V1> (or POP if no V1) |
| 2967 | [tb] POP |
| 2968 | [] <code for S1> |
| 2969 | JUMP_FORWARD L0 |
| 2970 | |
| 2971 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | .............................etc....................... |
| 2973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2974 | [tb, val, exc] Ln+1: END_FINALLY # re-raise exception |
| 2975 | |
| 2976 | [] L0: <next statement> |
| 2977 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2978 | Of course, parts are not generated if Vi or Ei is not present. |
| 2979 | */ |
| 2980 | static int |
| 2981 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 2982 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2983 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2984 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2986 | body = compiler_new_block(c); |
| 2987 | except = compiler_new_block(c); |
| 2988 | orelse = compiler_new_block(c); |
| 2989 | end = compiler_new_block(c); |
| 2990 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 2991 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2992 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2993 | compiler_use_next_block(c, body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2994 | if (!compiler_push_fblock(c, EXCEPT, body, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2995 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2996 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | ADDOP(c, POP_BLOCK); |
| 2998 | compiler_pop_fblock(c, EXCEPT, body); |
| 2999 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3000 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3001 | compiler_use_next_block(c, except); |
| 3002 | for (i = 0; i < n; i++) { |
| 3003 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3004 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3005 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3006 | return compiler_error(c, "default 'except:' must be last"); |
| 3007 | c->u->u_lineno_set = 0; |
| 3008 | c->u->u_lineno = handler->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3009 | c->u->u_col_offset = handler->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | except = compiler_new_block(c); |
| 3011 | if (except == NULL) |
| 3012 | return 0; |
| 3013 | if (handler->v.ExceptHandler.type) { |
| 3014 | ADDOP(c, DUP_TOP); |
| 3015 | VISIT(c, expr, handler->v.ExceptHandler.type); |
| 3016 | ADDOP_I(c, COMPARE_OP, PyCmp_EXC_MATCH); |
| 3017 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, except); |
| 3018 | } |
| 3019 | ADDOP(c, POP_TOP); |
| 3020 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3021 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3022 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3023 | cleanup_end = compiler_new_block(c); |
| 3024 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3025 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3026 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3027 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3028 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3029 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3030 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3032 | /* |
| 3033 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3034 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3035 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3036 | try: |
| 3037 | # body |
| 3038 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3039 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3040 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3041 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3042 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3043 | /* second try: */ |
| 3044 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 3045 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3046 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, cleanup_end)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3047 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3049 | /* second # body */ |
| 3050 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
| 3051 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3052 | ADDOP(c, BEGIN_FINALLY); |
| 3053 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3054 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3055 | /* finally: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3056 | compiler_use_next_block(c, cleanup_end); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3057 | if (!compiler_push_fblock(c, FINALLY_END, cleanup_end, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3058 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3060 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3061 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3062 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3063 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3064 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3065 | ADDOP(c, END_FINALLY); |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 3066 | ADDOP(c, POP_EXCEPT); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3067 | compiler_pop_fblock(c, FINALLY_END, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | } |
| 3069 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3070 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3071 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3072 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3073 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3074 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3075 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3076 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3077 | ADDOP(c, POP_TOP); |
| 3078 | compiler_use_next_block(c, cleanup_body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3079 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3080 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3082 | ADDOP(c, POP_EXCEPT); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3083 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3084 | } |
| 3085 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3086 | compiler_use_next_block(c, except); |
| 3087 | } |
| 3088 | ADDOP(c, END_FINALLY); |
| 3089 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3090 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3091 | compiler_use_next_block(c, end); |
| 3092 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
| 3095 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3096 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3097 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3098 | return compiler_try_finally(c, s); |
| 3099 | else |
| 3100 | return compiler_try_except(c, s); |
| 3101 | } |
| 3102 | |
| 3103 | |
| 3104 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3105 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3106 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | /* The IMPORT_NAME opcode was already generated. This function |
| 3108 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3109 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3110 | 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] | 3111 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3113 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3114 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3115 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3116 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3117 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3119 | while (1) { |
| 3120 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3121 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3122 | dot = PyUnicode_FindChar(name, '.', pos, 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; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3125 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3126 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3127 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3128 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3129 | if (dot == -1) { |
| 3130 | break; |
| 3131 | } |
| 3132 | ADDOP(c, ROT_TWO); |
| 3133 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3134 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3135 | if (!compiler_nameop(c, asname, Store)) { |
| 3136 | return 0; |
| 3137 | } |
| 3138 | ADDOP(c, POP_TOP); |
| 3139 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | } |
| 3141 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3142 | } |
| 3143 | |
| 3144 | static int |
| 3145 | compiler_import(struct compiler *c, stmt_ty s) |
| 3146 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3147 | /* The Import node stores a module name like a.b.c as a single |
| 3148 | string. This is convenient for all cases except |
| 3149 | import a.b.c as d |
| 3150 | where we need to parse that string to extract the individual |
| 3151 | module names. |
| 3152 | XXX Perhaps change the representation to make this case simpler? |
| 3153 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3154 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3155 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | for (i = 0; i < n; i++) { |
| 3157 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3158 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3159 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3160 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3161 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3163 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3164 | if (alias->asname) { |
| 3165 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3166 | if (!r) |
| 3167 | return r; |
| 3168 | } |
| 3169 | else { |
| 3170 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3171 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3172 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3173 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3174 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3175 | if (tmp == NULL) |
| 3176 | return 0; |
| 3177 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3179 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3180 | Py_DECREF(tmp); |
| 3181 | } |
| 3182 | if (!r) |
| 3183 | return r; |
| 3184 | } |
| 3185 | } |
| 3186 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3187 | } |
| 3188 | |
| 3189 | static int |
| 3190 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3191 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3192 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3193 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3194 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3195 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3196 | if (!empty_string) { |
| 3197 | empty_string = PyUnicode_FromString(""); |
| 3198 | if (!empty_string) |
| 3199 | return 0; |
| 3200 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3201 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3202 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3203 | |
| 3204 | names = PyTuple_New(n); |
| 3205 | if (!names) |
| 3206 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3207 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3208 | /* build up the names */ |
| 3209 | for (i = 0; i < n; i++) { |
| 3210 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3211 | Py_INCREF(alias->name); |
| 3212 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3213 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3214 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3215 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3216 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3217 | Py_DECREF(names); |
| 3218 | return compiler_error(c, "from __future__ imports must occur " |
| 3219 | "at the beginning of the file"); |
| 3220 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3221 | ADDOP_LOAD_CONST_NEW(c, names); |
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->v.ImportFrom.module) { |
| 3224 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3225 | } |
| 3226 | else { |
| 3227 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3228 | } |
| 3229 | for (i = 0; i < n; i++) { |
| 3230 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3231 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3232 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3233 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3234 | assert(n == 1); |
| 3235 | ADDOP(c, IMPORT_STAR); |
| 3236 | return 1; |
| 3237 | } |
| 3238 | |
| 3239 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3240 | store_name = alias->name; |
| 3241 | if (alias->asname) |
| 3242 | store_name = alias->asname; |
| 3243 | |
| 3244 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | return 0; |
| 3246 | } |
| 3247 | } |
| 3248 | /* remove imported module */ |
| 3249 | ADDOP(c, POP_TOP); |
| 3250 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | static int |
| 3254 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3255 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | static PyObject *assertion_error = NULL; |
| 3257 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3258 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3259 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3260 | return 1; |
| 3261 | if (assertion_error == NULL) { |
| 3262 | assertion_error = PyUnicode_InternFromString("AssertionError"); |
| 3263 | if (assertion_error == NULL) |
| 3264 | return 0; |
| 3265 | } |
| 3266 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3267 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3268 | { |
| 3269 | if (!compiler_warn(c, "assertion is always true, " |
| 3270 | "perhaps remove parentheses?")) |
| 3271 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3272 | return 0; |
| 3273 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3274 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3275 | end = compiler_new_block(c); |
| 3276 | if (end == NULL) |
| 3277 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3278 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3279 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3280 | ADDOP_O(c, LOAD_GLOBAL, assertion_error, names); |
| 3281 | if (s->v.Assert.msg) { |
| 3282 | VISIT(c, expr, s->v.Assert.msg); |
| 3283 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3284 | } |
| 3285 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3286 | compiler_use_next_block(c, end); |
| 3287 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3288 | } |
| 3289 | |
| 3290 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3291 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3292 | { |
| 3293 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3294 | VISIT(c, expr, value); |
| 3295 | ADDOP(c, PRINT_EXPR); |
| 3296 | return 1; |
| 3297 | } |
| 3298 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3299 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3300 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3301 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3302 | } |
| 3303 | |
| 3304 | VISIT(c, expr, value); |
| 3305 | ADDOP(c, POP_TOP); |
| 3306 | return 1; |
| 3307 | } |
| 3308 | |
| 3309 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3310 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3311 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3312 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3314 | /* Always assign a lineno to the next instruction for a stmt. */ |
| 3315 | c->u->u_lineno = s->lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 3316 | c->u->u_col_offset = s->col_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3317 | c->u->u_lineno_set = 0; |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3319 | switch (s->kind) { |
| 3320 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3321 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3322 | case ClassDef_kind: |
| 3323 | return compiler_class(c, s); |
| 3324 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3325 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3326 | case Delete_kind: |
| 3327 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3328 | break; |
| 3329 | case Assign_kind: |
| 3330 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3331 | VISIT(c, expr, s->v.Assign.value); |
| 3332 | for (i = 0; i < n; i++) { |
| 3333 | if (i < n - 1) |
| 3334 | ADDOP(c, DUP_TOP); |
| 3335 | VISIT(c, expr, |
| 3336 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3337 | } |
| 3338 | break; |
| 3339 | case AugAssign_kind: |
| 3340 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3341 | case AnnAssign_kind: |
| 3342 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3343 | case For_kind: |
| 3344 | return compiler_for(c, s); |
| 3345 | case While_kind: |
| 3346 | return compiler_while(c, s); |
| 3347 | case If_kind: |
| 3348 | return compiler_if(c, s); |
| 3349 | case Raise_kind: |
| 3350 | n = 0; |
| 3351 | if (s->v.Raise.exc) { |
| 3352 | VISIT(c, expr, s->v.Raise.exc); |
| 3353 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3354 | if (s->v.Raise.cause) { |
| 3355 | VISIT(c, expr, s->v.Raise.cause); |
| 3356 | n++; |
| 3357 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3358 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3359 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3360 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3361 | case Try_kind: |
| 3362 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3363 | case Assert_kind: |
| 3364 | return compiler_assert(c, s); |
| 3365 | case Import_kind: |
| 3366 | return compiler_import(c, s); |
| 3367 | case ImportFrom_kind: |
| 3368 | return compiler_from_import(c, s); |
| 3369 | case Global_kind: |
| 3370 | case Nonlocal_kind: |
| 3371 | break; |
| 3372 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3373 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3374 | case Pass_kind: |
| 3375 | break; |
| 3376 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3377 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3378 | case Continue_kind: |
| 3379 | return compiler_continue(c); |
| 3380 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3381 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3382 | case AsyncFunctionDef_kind: |
| 3383 | return compiler_function(c, s, 1); |
| 3384 | case AsyncWith_kind: |
| 3385 | return compiler_async_with(c, s, 0); |
| 3386 | case AsyncFor_kind: |
| 3387 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3389 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3390 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3391 | } |
| 3392 | |
| 3393 | static int |
| 3394 | unaryop(unaryop_ty op) |
| 3395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3396 | switch (op) { |
| 3397 | case Invert: |
| 3398 | return UNARY_INVERT; |
| 3399 | case Not: |
| 3400 | return UNARY_NOT; |
| 3401 | case UAdd: |
| 3402 | return UNARY_POSITIVE; |
| 3403 | case USub: |
| 3404 | return UNARY_NEGATIVE; |
| 3405 | default: |
| 3406 | PyErr_Format(PyExc_SystemError, |
| 3407 | "unary op %d should not be possible", op); |
| 3408 | return 0; |
| 3409 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3410 | } |
| 3411 | |
| 3412 | static int |
| 3413 | binop(struct compiler *c, operator_ty op) |
| 3414 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3415 | switch (op) { |
| 3416 | case Add: |
| 3417 | return BINARY_ADD; |
| 3418 | case Sub: |
| 3419 | return BINARY_SUBTRACT; |
| 3420 | case Mult: |
| 3421 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3422 | case MatMult: |
| 3423 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3424 | case Div: |
| 3425 | return BINARY_TRUE_DIVIDE; |
| 3426 | case Mod: |
| 3427 | return BINARY_MODULO; |
| 3428 | case Pow: |
| 3429 | return BINARY_POWER; |
| 3430 | case LShift: |
| 3431 | return BINARY_LSHIFT; |
| 3432 | case RShift: |
| 3433 | return BINARY_RSHIFT; |
| 3434 | case BitOr: |
| 3435 | return BINARY_OR; |
| 3436 | case BitXor: |
| 3437 | return BINARY_XOR; |
| 3438 | case BitAnd: |
| 3439 | return BINARY_AND; |
| 3440 | case FloorDiv: |
| 3441 | return BINARY_FLOOR_DIVIDE; |
| 3442 | default: |
| 3443 | PyErr_Format(PyExc_SystemError, |
| 3444 | "binary op %d should not be possible", op); |
| 3445 | return 0; |
| 3446 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3447 | } |
| 3448 | |
| 3449 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3450 | inplace_binop(struct compiler *c, operator_ty op) |
| 3451 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3452 | switch (op) { |
| 3453 | case Add: |
| 3454 | return INPLACE_ADD; |
| 3455 | case Sub: |
| 3456 | return INPLACE_SUBTRACT; |
| 3457 | case Mult: |
| 3458 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3459 | case MatMult: |
| 3460 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3461 | case Div: |
| 3462 | return INPLACE_TRUE_DIVIDE; |
| 3463 | case Mod: |
| 3464 | return INPLACE_MODULO; |
| 3465 | case Pow: |
| 3466 | return INPLACE_POWER; |
| 3467 | case LShift: |
| 3468 | return INPLACE_LSHIFT; |
| 3469 | case RShift: |
| 3470 | return INPLACE_RSHIFT; |
| 3471 | case BitOr: |
| 3472 | return INPLACE_OR; |
| 3473 | case BitXor: |
| 3474 | return INPLACE_XOR; |
| 3475 | case BitAnd: |
| 3476 | return INPLACE_AND; |
| 3477 | case FloorDiv: |
| 3478 | return INPLACE_FLOOR_DIVIDE; |
| 3479 | default: |
| 3480 | PyErr_Format(PyExc_SystemError, |
| 3481 | "inplace binary op %d should not be possible", op); |
| 3482 | return 0; |
| 3483 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3484 | } |
| 3485 | |
| 3486 | static int |
| 3487 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3488 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3489 | int op, scope; |
| 3490 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3491 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3492 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3493 | PyObject *dict = c->u->u_names; |
| 3494 | PyObject *mangled; |
| 3495 | /* XXX AugStore isn't used anywhere! */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3496 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3497 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3498 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3499 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3500 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3501 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3502 | if (!mangled) |
| 3503 | return 0; |
| 3504 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | op = 0; |
| 3506 | optype = OP_NAME; |
| 3507 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3508 | switch (scope) { |
| 3509 | case FREE: |
| 3510 | dict = c->u->u_freevars; |
| 3511 | optype = OP_DEREF; |
| 3512 | break; |
| 3513 | case CELL: |
| 3514 | dict = c->u->u_cellvars; |
| 3515 | optype = OP_DEREF; |
| 3516 | break; |
| 3517 | case LOCAL: |
| 3518 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3519 | optype = OP_FAST; |
| 3520 | break; |
| 3521 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3522 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3523 | optype = OP_GLOBAL; |
| 3524 | break; |
| 3525 | case GLOBAL_EXPLICIT: |
| 3526 | optype = OP_GLOBAL; |
| 3527 | break; |
| 3528 | default: |
| 3529 | /* scope can be 0 */ |
| 3530 | break; |
| 3531 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3532 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3533 | /* 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] | 3534 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | switch (optype) { |
| 3537 | case OP_DEREF: |
| 3538 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3539 | case Load: |
| 3540 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3541 | break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3542 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3543 | op = STORE_DEREF; |
| 3544 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3545 | case AugLoad: |
| 3546 | case AugStore: |
| 3547 | break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3548 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3549 | case Param: |
| 3550 | default: |
| 3551 | PyErr_SetString(PyExc_SystemError, |
| 3552 | "param invalid for deref variable"); |
| 3553 | return 0; |
| 3554 | } |
| 3555 | break; |
| 3556 | case OP_FAST: |
| 3557 | switch (ctx) { |
| 3558 | case Load: op = LOAD_FAST; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3559 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3560 | op = STORE_FAST; |
| 3561 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3562 | case Del: op = DELETE_FAST; break; |
| 3563 | case AugLoad: |
| 3564 | case AugStore: |
| 3565 | break; |
| 3566 | case Param: |
| 3567 | default: |
| 3568 | PyErr_SetString(PyExc_SystemError, |
| 3569 | "param invalid for local variable"); |
| 3570 | return 0; |
| 3571 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3572 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3573 | return 1; |
| 3574 | case OP_GLOBAL: |
| 3575 | switch (ctx) { |
| 3576 | case Load: op = LOAD_GLOBAL; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3577 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3578 | op = STORE_GLOBAL; |
| 3579 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3580 | case Del: op = DELETE_GLOBAL; break; |
| 3581 | case AugLoad: |
| 3582 | case AugStore: |
| 3583 | break; |
| 3584 | case Param: |
| 3585 | default: |
| 3586 | PyErr_SetString(PyExc_SystemError, |
| 3587 | "param invalid for global variable"); |
| 3588 | return 0; |
| 3589 | } |
| 3590 | break; |
| 3591 | case OP_NAME: |
| 3592 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3593 | case Load: op = LOAD_NAME; break; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3594 | case Store: |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 3595 | op = STORE_NAME; |
| 3596 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3597 | case Del: op = DELETE_NAME; break; |
| 3598 | case AugLoad: |
| 3599 | case AugStore: |
| 3600 | break; |
| 3601 | case Param: |
| 3602 | default: |
| 3603 | PyErr_SetString(PyExc_SystemError, |
| 3604 | "param invalid for name variable"); |
| 3605 | return 0; |
| 3606 | } |
| 3607 | break; |
| 3608 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3609 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3610 | assert(op); |
| 3611 | arg = compiler_add_o(c, dict, mangled); |
| 3612 | Py_DECREF(mangled); |
| 3613 | if (arg < 0) |
| 3614 | return 0; |
| 3615 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3616 | } |
| 3617 | |
| 3618 | static int |
| 3619 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3620 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3621 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3622 | int jumpi; |
| 3623 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3624 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3625 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3626 | assert(e->kind == BoolOp_kind); |
| 3627 | if (e->v.BoolOp.op == And) |
| 3628 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3629 | else |
| 3630 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3631 | end = compiler_new_block(c); |
| 3632 | if (end == NULL) |
| 3633 | return 0; |
| 3634 | s = e->v.BoolOp.values; |
| 3635 | n = asdl_seq_LEN(s) - 1; |
| 3636 | assert(n >= 0); |
| 3637 | for (i = 0; i < n; ++i) { |
| 3638 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3639 | ADDOP_JABS(c, jumpi, end); |
| 3640 | } |
| 3641 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3642 | compiler_use_next_block(c, end); |
| 3643 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3644 | } |
| 3645 | |
| 3646 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3647 | starunpack_helper(struct compiler *c, asdl_seq *elts, |
| 3648 | int single_op, int inner_op, int outer_op) |
| 3649 | { |
| 3650 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3651 | Py_ssize_t i, nsubitems = 0, nseen = 0; |
| 3652 | for (i = 0; i < n; i++) { |
| 3653 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3654 | if (elt->kind == Starred_kind) { |
| 3655 | if (nseen) { |
| 3656 | ADDOP_I(c, inner_op, nseen); |
| 3657 | nseen = 0; |
| 3658 | nsubitems++; |
| 3659 | } |
| 3660 | VISIT(c, expr, elt->v.Starred.value); |
| 3661 | nsubitems++; |
| 3662 | } |
| 3663 | else { |
| 3664 | VISIT(c, expr, elt); |
| 3665 | nseen++; |
| 3666 | } |
| 3667 | } |
| 3668 | if (nsubitems) { |
| 3669 | if (nseen) { |
| 3670 | ADDOP_I(c, inner_op, nseen); |
| 3671 | nsubitems++; |
| 3672 | } |
| 3673 | ADDOP_I(c, outer_op, nsubitems); |
| 3674 | } |
| 3675 | else |
| 3676 | ADDOP_I(c, single_op, nseen); |
| 3677 | return 1; |
| 3678 | } |
| 3679 | |
| 3680 | static int |
| 3681 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3682 | { |
| 3683 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3684 | Py_ssize_t i; |
| 3685 | int seen_star = 0; |
| 3686 | for (i = 0; i < n; i++) { |
| 3687 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3688 | if (elt->kind == Starred_kind && !seen_star) { |
| 3689 | if ((i >= (1 << 8)) || |
| 3690 | (n-i-1 >= (INT_MAX >> 8))) |
| 3691 | return compiler_error(c, |
| 3692 | "too many expressions in " |
| 3693 | "star-unpacking assignment"); |
| 3694 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3695 | seen_star = 1; |
| 3696 | asdl_seq_SET(elts, i, elt->v.Starred.value); |
| 3697 | } |
| 3698 | else if (elt->kind == Starred_kind) { |
| 3699 | return compiler_error(c, |
| 3700 | "two starred expressions in assignment"); |
| 3701 | } |
| 3702 | } |
| 3703 | if (!seen_star) { |
| 3704 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3705 | } |
| 3706 | VISIT_SEQ(c, expr, elts); |
| 3707 | return 1; |
| 3708 | } |
| 3709 | |
| 3710 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3711 | compiler_list(struct compiler *c, expr_ty e) |
| 3712 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3713 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3714 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3715 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3716 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3717 | else if (e->v.List.ctx == Load) { |
| 3718 | return starunpack_helper(c, elts, |
| 3719 | BUILD_LIST, BUILD_TUPLE, BUILD_LIST_UNPACK); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3720 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3721 | else |
| 3722 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3723 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3724 | } |
| 3725 | |
| 3726 | static int |
| 3727 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3728 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3729 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3730 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3731 | return assignment_helper(c, elts); |
| 3732 | } |
| 3733 | else if (e->v.Tuple.ctx == Load) { |
| 3734 | return starunpack_helper(c, elts, |
| 3735 | BUILD_TUPLE, BUILD_TUPLE, BUILD_TUPLE_UNPACK); |
| 3736 | } |
| 3737 | else |
| 3738 | VISIT_SEQ(c, expr, elts); |
| 3739 | return 1; |
| 3740 | } |
| 3741 | |
| 3742 | static int |
| 3743 | compiler_set(struct compiler *c, expr_ty e) |
| 3744 | { |
| 3745 | return starunpack_helper(c, e->v.Set.elts, BUILD_SET, |
| 3746 | BUILD_SET, BUILD_SET_UNPACK); |
| 3747 | } |
| 3748 | |
| 3749 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3750 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3751 | { |
| 3752 | Py_ssize_t i; |
| 3753 | for (i = begin; i < end; i++) { |
| 3754 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3755 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3756 | return 0; |
| 3757 | } |
| 3758 | return 1; |
| 3759 | } |
| 3760 | |
| 3761 | static int |
| 3762 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3763 | { |
| 3764 | Py_ssize_t i, n = end - begin; |
| 3765 | PyObject *keys, *key; |
| 3766 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3767 | for (i = begin; i < end; i++) { |
| 3768 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3769 | } |
| 3770 | keys = PyTuple_New(n); |
| 3771 | if (keys == NULL) { |
| 3772 | return 0; |
| 3773 | } |
| 3774 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3775 | 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] | 3776 | Py_INCREF(key); |
| 3777 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3778 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3779 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3780 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3781 | } |
| 3782 | else { |
| 3783 | for (i = begin; i < end; i++) { |
| 3784 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3785 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3786 | } |
| 3787 | ADDOP_I(c, BUILD_MAP, n); |
| 3788 | } |
| 3789 | return 1; |
| 3790 | } |
| 3791 | |
| 3792 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3793 | compiler_dict(struct compiler *c, expr_ty e) |
| 3794 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3795 | Py_ssize_t i, n, elements; |
| 3796 | int containers; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3797 | int is_unpacking = 0; |
| 3798 | n = asdl_seq_LEN(e->v.Dict.values); |
| 3799 | containers = 0; |
| 3800 | elements = 0; |
| 3801 | for (i = 0; i < n; i++) { |
| 3802 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
| 3803 | if (elements == 0xFFFF || (elements && is_unpacking)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3804 | if (!compiler_subdict(c, e, i - elements, i)) |
| 3805 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3806 | containers++; |
| 3807 | elements = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3808 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3809 | if (is_unpacking) { |
| 3810 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3811 | containers++; |
| 3812 | } |
| 3813 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3814 | elements++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3815 | } |
| 3816 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3817 | if (elements || containers == 0) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3818 | if (!compiler_subdict(c, e, n - elements, n)) |
| 3819 | return 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3820 | containers++; |
| 3821 | } |
| 3822 | /* If there is more than one dict, they need to be merged into a new |
| 3823 | * dict. If there is one dict and it's an unpacking, then it needs |
| 3824 | * to be copied into a new dict." */ |
Serhiy Storchaka | 3d85fae | 2016-11-28 20:56:37 +0200 | [diff] [blame] | 3825 | if (containers > 1 || is_unpacking) { |
| 3826 | ADDOP_I(c, BUILD_MAP_UNPACK, containers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3827 | } |
| 3828 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3829 | } |
| 3830 | |
| 3831 | static int |
| 3832 | compiler_compare(struct compiler *c, expr_ty e) |
| 3833 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3834 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3835 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3836 | if (!check_compare(c, e)) { |
| 3837 | return 0; |
| 3838 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3839 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3840 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3841 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3842 | if (n == 0) { |
| 3843 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
| 3844 | ADDOP_I(c, COMPARE_OP, |
| 3845 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, 0)))); |
| 3846 | } |
| 3847 | else { |
| 3848 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3849 | if (cleanup == NULL) |
| 3850 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3851 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3852 | VISIT(c, expr, |
| 3853 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3854 | ADDOP(c, DUP_TOP); |
| 3855 | ADDOP(c, ROT_THREE); |
| 3856 | ADDOP_I(c, COMPARE_OP, |
| 3857 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, i)))); |
| 3858 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3859 | NEXT_BLOCK(c); |
| 3860 | } |
| 3861 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
| 3862 | ADDOP_I(c, COMPARE_OP, |
| 3863 | cmpop((cmpop_ty)(asdl_seq_GET(e->v.Compare.ops, n)))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3864 | basicblock *end = compiler_new_block(c); |
| 3865 | if (end == NULL) |
| 3866 | return 0; |
| 3867 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3868 | compiler_use_next_block(c, cleanup); |
| 3869 | ADDOP(c, ROT_TWO); |
| 3870 | ADDOP(c, POP_TOP); |
| 3871 | compiler_use_next_block(c, end); |
| 3872 | } |
| 3873 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3874 | } |
| 3875 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3876 | static PyTypeObject * |
| 3877 | infer_type(expr_ty e) |
| 3878 | { |
| 3879 | switch (e->kind) { |
| 3880 | case Tuple_kind: |
| 3881 | return &PyTuple_Type; |
| 3882 | case List_kind: |
| 3883 | case ListComp_kind: |
| 3884 | return &PyList_Type; |
| 3885 | case Dict_kind: |
| 3886 | case DictComp_kind: |
| 3887 | return &PyDict_Type; |
| 3888 | case Set_kind: |
| 3889 | case SetComp_kind: |
| 3890 | return &PySet_Type; |
| 3891 | case GeneratorExp_kind: |
| 3892 | return &PyGen_Type; |
| 3893 | case Lambda_kind: |
| 3894 | return &PyFunction_Type; |
| 3895 | case JoinedStr_kind: |
| 3896 | case FormattedValue_kind: |
| 3897 | return &PyUnicode_Type; |
| 3898 | case Constant_kind: |
| 3899 | return e->v.Constant.value->ob_type; |
| 3900 | default: |
| 3901 | return NULL; |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | static int |
| 3906 | check_caller(struct compiler *c, expr_ty e) |
| 3907 | { |
| 3908 | switch (e->kind) { |
| 3909 | case Constant_kind: |
| 3910 | case Tuple_kind: |
| 3911 | case List_kind: |
| 3912 | case ListComp_kind: |
| 3913 | case Dict_kind: |
| 3914 | case DictComp_kind: |
| 3915 | case Set_kind: |
| 3916 | case SetComp_kind: |
| 3917 | case GeneratorExp_kind: |
| 3918 | case JoinedStr_kind: |
| 3919 | case FormattedValue_kind: |
| 3920 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3921 | "perhaps you missed a comma?", |
| 3922 | infer_type(e)->tp_name); |
| 3923 | default: |
| 3924 | return 1; |
| 3925 | } |
| 3926 | } |
| 3927 | |
| 3928 | static int |
| 3929 | check_subscripter(struct compiler *c, expr_ty e) |
| 3930 | { |
| 3931 | PyObject *v; |
| 3932 | |
| 3933 | switch (e->kind) { |
| 3934 | case Constant_kind: |
| 3935 | v = e->v.Constant.value; |
| 3936 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3937 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3938 | PyAnySet_Check(v))) |
| 3939 | { |
| 3940 | return 1; |
| 3941 | } |
| 3942 | /* fall through */ |
| 3943 | case Set_kind: |
| 3944 | case SetComp_kind: |
| 3945 | case GeneratorExp_kind: |
| 3946 | case Lambda_kind: |
| 3947 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3948 | "perhaps you missed a comma?", |
| 3949 | infer_type(e)->tp_name); |
| 3950 | default: |
| 3951 | return 1; |
| 3952 | } |
| 3953 | } |
| 3954 | |
| 3955 | static int |
| 3956 | check_index(struct compiler *c, expr_ty e, slice_ty s) |
| 3957 | { |
| 3958 | PyObject *v; |
| 3959 | |
| 3960 | if (s->kind != Index_kind) { |
| 3961 | return 1; |
| 3962 | } |
| 3963 | PyTypeObject *index_type = infer_type(s->v.Index.value); |
| 3964 | if (index_type == NULL |
| 3965 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 3966 | || index_type == &PySlice_Type) { |
| 3967 | return 1; |
| 3968 | } |
| 3969 | |
| 3970 | switch (e->kind) { |
| 3971 | case Constant_kind: |
| 3972 | v = e->v.Constant.value; |
| 3973 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 3974 | return 1; |
| 3975 | } |
| 3976 | /* fall through */ |
| 3977 | case Tuple_kind: |
| 3978 | case List_kind: |
| 3979 | case ListComp_kind: |
| 3980 | case JoinedStr_kind: |
| 3981 | case FormattedValue_kind: |
| 3982 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 3983 | "not %.200s; " |
| 3984 | "perhaps you missed a comma?", |
| 3985 | infer_type(e)->tp_name, |
| 3986 | index_type->tp_name); |
| 3987 | default: |
| 3988 | return 1; |
| 3989 | } |
| 3990 | } |
| 3991 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 3992 | // 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] | 3993 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 3994 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 3995 | { |
| 3996 | Py_ssize_t argsl, i; |
| 3997 | expr_ty meth = e->v.Call.func; |
| 3998 | asdl_seq *args = e->v.Call.args; |
| 3999 | |
| 4000 | /* Check that the call node is an attribute access, and that |
| 4001 | the call doesn't have keyword parameters. */ |
| 4002 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4003 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4004 | return -1; |
| 4005 | |
| 4006 | /* Check that there are no *varargs types of arguments. */ |
| 4007 | argsl = asdl_seq_LEN(args); |
| 4008 | for (i = 0; i < argsl; i++) { |
| 4009 | expr_ty elt = asdl_seq_GET(args, i); |
| 4010 | if (elt->kind == Starred_kind) { |
| 4011 | return -1; |
| 4012 | } |
| 4013 | } |
| 4014 | |
| 4015 | /* Alright, we can optimize the code. */ |
| 4016 | VISIT(c, expr, meth->v.Attribute.value); |
| 4017 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4018 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4019 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4020 | return 1; |
| 4021 | } |
| 4022 | |
| 4023 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4024 | compiler_call(struct compiler *c, expr_ty e) |
| 4025 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4026 | int ret = maybe_optimize_method_call(c, e); |
| 4027 | if (ret >= 0) { |
| 4028 | return ret; |
| 4029 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4030 | if (!check_caller(c, e->v.Call.func)) { |
| 4031 | return 0; |
| 4032 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4033 | VISIT(c, expr, e->v.Call.func); |
| 4034 | return compiler_call_helper(c, 0, |
| 4035 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4036 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4037 | } |
| 4038 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4039 | static int |
| 4040 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4041 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4042 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4043 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4044 | 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] | 4045 | return 1; |
| 4046 | } |
| 4047 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4048 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4049 | static int |
| 4050 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4051 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4052 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4053 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4054 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4055 | Convert the conversion char to 3 bits: |
| 4056 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4057 | !s : 001 0x1 FVC_STR |
| 4058 | !r : 010 0x2 FVC_REPR |
| 4059 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4060 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4061 | next bit is whether or not we have a format spec: |
| 4062 | yes : 100 0x4 |
| 4063 | no : 000 0x0 |
| 4064 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4065 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4066 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4067 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4068 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4069 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4070 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4071 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4072 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4073 | case 's': oparg = FVC_STR; break; |
| 4074 | case 'r': oparg = FVC_REPR; break; |
| 4075 | case 'a': oparg = FVC_ASCII; break; |
| 4076 | case -1: oparg = FVC_NONE; break; |
| 4077 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4078 | PyErr_Format(PyExc_SystemError, |
| 4079 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4080 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4081 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4082 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4083 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4084 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4085 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4086 | } |
| 4087 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4088 | /* And push our opcode and oparg */ |
| 4089 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4090 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4091 | return 1; |
| 4092 | } |
| 4093 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4094 | static int |
| 4095 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 4096 | { |
| 4097 | Py_ssize_t i, n = end - begin; |
| 4098 | keyword_ty kw; |
| 4099 | PyObject *keys, *key; |
| 4100 | assert(n > 0); |
| 4101 | if (n > 1) { |
| 4102 | for (i = begin; i < end; i++) { |
| 4103 | kw = asdl_seq_GET(keywords, i); |
| 4104 | VISIT(c, expr, kw->value); |
| 4105 | } |
| 4106 | keys = PyTuple_New(n); |
| 4107 | if (keys == NULL) { |
| 4108 | return 0; |
| 4109 | } |
| 4110 | for (i = begin; i < end; i++) { |
| 4111 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4112 | Py_INCREF(key); |
| 4113 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4114 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4115 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4116 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4117 | } |
| 4118 | else { |
| 4119 | /* a for loop only executes once */ |
| 4120 | for (i = begin; i < end; i++) { |
| 4121 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4122 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4123 | VISIT(c, expr, kw->value); |
| 4124 | } |
| 4125 | ADDOP_I(c, BUILD_MAP, n); |
| 4126 | } |
| 4127 | return 1; |
| 4128 | } |
| 4129 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4130 | /* shared code between compiler_call and compiler_class */ |
| 4131 | static int |
| 4132 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4133 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4134 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4135 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4136 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4137 | Py_ssize_t i, nseen, nelts, nkwelts; |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4138 | int mustdictunpack = 0; |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4139 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4140 | /* the number of tuples and dictionaries on the stack */ |
| 4141 | Py_ssize_t nsubargs = 0, nsubkwargs = 0; |
| 4142 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4143 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4144 | nkwelts = asdl_seq_LEN(keywords); |
| 4145 | |
| 4146 | for (i = 0; i < nkwelts; i++) { |
| 4147 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4148 | if (kw->arg == NULL) { |
| 4149 | mustdictunpack = 1; |
| 4150 | break; |
| 4151 | } |
| 4152 | } |
| 4153 | |
| 4154 | nseen = n; /* the number of positional arguments on the stack */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4155 | for (i = 0; i < nelts; i++) { |
| 4156 | expr_ty elt = asdl_seq_GET(args, i); |
| 4157 | if (elt->kind == Starred_kind) { |
| 4158 | /* A star-arg. If we've seen positional arguments, |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4159 | pack the positional arguments into a tuple. */ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4160 | if (nseen) { |
| 4161 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4162 | nseen = 0; |
| 4163 | nsubargs++; |
| 4164 | } |
| 4165 | VISIT(c, expr, elt->v.Starred.value); |
| 4166 | nsubargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4167 | } |
| 4168 | else { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4169 | VISIT(c, expr, elt); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4170 | nseen++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4171 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4172 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4173 | |
| 4174 | /* Same dance again for keyword arguments */ |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4175 | if (nsubargs || mustdictunpack) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4176 | if (nseen) { |
| 4177 | /* Pack up any trailing positional arguments. */ |
| 4178 | ADDOP_I(c, BUILD_TUPLE, nseen); |
| 4179 | nsubargs++; |
| 4180 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4181 | if (nsubargs > 1) { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4182 | /* If we ended up with more than one stararg, we need |
| 4183 | to concatenate them into a single sequence. */ |
Serhiy Storchaka | 7344285 | 2016-10-02 10:33:46 +0300 | [diff] [blame] | 4184 | ADDOP_I(c, BUILD_TUPLE_UNPACK_WITH_CALL, nsubargs); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4185 | } |
| 4186 | else if (nsubargs == 0) { |
| 4187 | ADDOP_I(c, BUILD_TUPLE, 0); |
| 4188 | } |
| 4189 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4190 | for (i = 0; i < nkwelts; i++) { |
| 4191 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4192 | if (kw->arg == NULL) { |
| 4193 | /* A keyword argument unpacking. */ |
| 4194 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4195 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) |
| 4196 | return 0; |
| 4197 | nsubkwargs++; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4198 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4199 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4200 | VISIT(c, expr, kw->value); |
| 4201 | nsubkwargs++; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4202 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4203 | else { |
| 4204 | nseen++; |
| 4205 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4206 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4207 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4208 | /* Pack up any trailing keyword arguments. */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4209 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4210 | return 0; |
| 4211 | nsubkwargs++; |
| 4212 | } |
Serhiy Storchaka | b728105 | 2016-09-12 00:52:40 +0300 | [diff] [blame] | 4213 | if (nsubkwargs > 1) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4214 | /* Pack it all up */ |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4215 | ADDOP_I(c, BUILD_MAP_UNPACK_WITH_CALL, nsubkwargs); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4216 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4217 | ADDOP_I(c, CALL_FUNCTION_EX, nsubkwargs > 0); |
| 4218 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4219 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4220 | else if (nkwelts) { |
| 4221 | PyObject *names; |
| 4222 | VISIT_SEQ(c, keyword, keywords); |
| 4223 | names = PyTuple_New(nkwelts); |
| 4224 | if (names == NULL) { |
| 4225 | return 0; |
| 4226 | } |
| 4227 | for (i = 0; i < nkwelts; i++) { |
| 4228 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4229 | Py_INCREF(kw->arg); |
| 4230 | PyTuple_SET_ITEM(names, i, kw->arg); |
| 4231 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4232 | ADDOP_LOAD_CONST_NEW(c, names); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4233 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4234 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4235 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4236 | else { |
| 4237 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4238 | return 1; |
| 4239 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4240 | } |
| 4241 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4242 | |
| 4243 | /* List and set comprehensions and generator expressions work by creating a |
| 4244 | nested function to perform the actual iteration. This means that the |
| 4245 | iteration variables don't leak into the current scope. |
| 4246 | The defined function is called immediately following its definition, with the |
| 4247 | result of that call being the result of the expression. |
| 4248 | The LC/SC version returns the populated container, while the GE version is |
| 4249 | flagged in symtable.c as a generator, so it returns the generator object |
| 4250 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4251 | |
| 4252 | Possible cleanups: |
| 4253 | - iterate over the generator sequence instead of using recursion |
| 4254 | */ |
| 4255 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4256 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4257 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4258 | compiler_comprehension_generator(struct compiler *c, |
| 4259 | asdl_seq *generators, int gen_index, |
| 4260 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4261 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4262 | comprehension_ty gen; |
| 4263 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4264 | if (gen->is_async) { |
| 4265 | return compiler_async_comprehension_generator( |
| 4266 | c, generators, gen_index, elt, val, type); |
| 4267 | } else { |
| 4268 | return compiler_sync_comprehension_generator( |
| 4269 | c, generators, gen_index, elt, val, type); |
| 4270 | } |
| 4271 | } |
| 4272 | |
| 4273 | static int |
| 4274 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4275 | asdl_seq *generators, int gen_index, |
| 4276 | expr_ty elt, expr_ty val, int type) |
| 4277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4278 | /* generate code for the iterator, then each of the ifs, |
| 4279 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4280 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4281 | comprehension_ty gen; |
| 4282 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4283 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4284 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | start = compiler_new_block(c); |
| 4286 | skip = compiler_new_block(c); |
| 4287 | if_cleanup = compiler_new_block(c); |
| 4288 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4289 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4290 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4291 | anchor == NULL) |
| 4292 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4294 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4296 | if (gen_index == 0) { |
| 4297 | /* Receive outermost iter as an implicit argument */ |
| 4298 | c->u->u_argcount = 1; |
| 4299 | ADDOP_I(c, LOAD_FAST, 0); |
| 4300 | } |
| 4301 | else { |
| 4302 | /* Sub-iter - calculate on the fly */ |
| 4303 | VISIT(c, expr, gen->iter); |
| 4304 | ADDOP(c, GET_ITER); |
| 4305 | } |
| 4306 | compiler_use_next_block(c, start); |
| 4307 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4308 | NEXT_BLOCK(c); |
| 4309 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4310 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4311 | /* XXX this needs to be cleaned up...a lot! */ |
| 4312 | n = asdl_seq_LEN(gen->ifs); |
| 4313 | for (i = 0; i < n; i++) { |
| 4314 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4315 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4316 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4317 | NEXT_BLOCK(c); |
| 4318 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4320 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4321 | if (!compiler_comprehension_generator(c, |
| 4322 | generators, gen_index, |
| 4323 | elt, val, type)) |
| 4324 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4326 | /* only append after the last for generator */ |
| 4327 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4328 | /* comprehension specific code */ |
| 4329 | switch (type) { |
| 4330 | case COMP_GENEXP: |
| 4331 | VISIT(c, expr, elt); |
| 4332 | ADDOP(c, YIELD_VALUE); |
| 4333 | ADDOP(c, POP_TOP); |
| 4334 | break; |
| 4335 | case COMP_LISTCOMP: |
| 4336 | VISIT(c, expr, elt); |
| 4337 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4338 | break; |
| 4339 | case COMP_SETCOMP: |
| 4340 | VISIT(c, expr, elt); |
| 4341 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4342 | break; |
| 4343 | case COMP_DICTCOMP: |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4344 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4345 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4346 | VISIT(c, expr, elt); |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4347 | VISIT(c, expr, val); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4348 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4349 | break; |
| 4350 | default: |
| 4351 | return 0; |
| 4352 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4353 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4354 | compiler_use_next_block(c, skip); |
| 4355 | } |
| 4356 | compiler_use_next_block(c, if_cleanup); |
| 4357 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4358 | compiler_use_next_block(c, anchor); |
| 4359 | |
| 4360 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4361 | } |
| 4362 | |
| 4363 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4364 | compiler_async_comprehension_generator(struct compiler *c, |
| 4365 | asdl_seq *generators, int gen_index, |
| 4366 | expr_ty elt, expr_ty val, int type) |
| 4367 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4368 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4369 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4370 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4371 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4372 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4373 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4374 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4375 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4376 | return 0; |
| 4377 | } |
| 4378 | |
| 4379 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4380 | |
| 4381 | if (gen_index == 0) { |
| 4382 | /* Receive outermost iter as an implicit argument */ |
| 4383 | c->u->u_argcount = 1; |
| 4384 | ADDOP_I(c, LOAD_FAST, 0); |
| 4385 | } |
| 4386 | else { |
| 4387 | /* Sub-iter - calculate on the fly */ |
| 4388 | VISIT(c, expr, gen->iter); |
| 4389 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4390 | } |
| 4391 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4392 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4393 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4394 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4395 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4396 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4397 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4398 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4399 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4400 | |
| 4401 | n = asdl_seq_LEN(gen->ifs); |
| 4402 | for (i = 0; i < n; i++) { |
| 4403 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4404 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4405 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4406 | NEXT_BLOCK(c); |
| 4407 | } |
| 4408 | |
| 4409 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4410 | if (!compiler_comprehension_generator(c, |
| 4411 | generators, gen_index, |
| 4412 | elt, val, type)) |
| 4413 | return 0; |
| 4414 | |
| 4415 | /* only append after the last for generator */ |
| 4416 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4417 | /* comprehension specific code */ |
| 4418 | switch (type) { |
| 4419 | case COMP_GENEXP: |
| 4420 | VISIT(c, expr, elt); |
| 4421 | ADDOP(c, YIELD_VALUE); |
| 4422 | ADDOP(c, POP_TOP); |
| 4423 | break; |
| 4424 | case COMP_LISTCOMP: |
| 4425 | VISIT(c, expr, elt); |
| 4426 | ADDOP_I(c, LIST_APPEND, gen_index + 1); |
| 4427 | break; |
| 4428 | case COMP_SETCOMP: |
| 4429 | VISIT(c, expr, elt); |
| 4430 | ADDOP_I(c, SET_ADD, gen_index + 1); |
| 4431 | break; |
| 4432 | case COMP_DICTCOMP: |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4433 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4434 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4435 | VISIT(c, expr, elt); |
Miss Islington (bot) | 874ff65 | 2019-06-22 15:34:03 -0700 | [diff] [blame] | 4436 | VISIT(c, expr, val); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4437 | ADDOP_I(c, MAP_ADD, gen_index + 1); |
| 4438 | break; |
| 4439 | default: |
| 4440 | return 0; |
| 4441 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4442 | } |
| 4443 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4444 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4445 | |
| 4446 | compiler_use_next_block(c, except); |
| 4447 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4448 | |
| 4449 | return 1; |
| 4450 | } |
| 4451 | |
| 4452 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4453 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4454 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4455 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4456 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4457 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4458 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4459 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4460 | int is_async_function = c->u->u_ste->ste_coroutine; |
| 4461 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4462 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4463 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4464 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4465 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4466 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4467 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4468 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4469 | } |
| 4470 | |
| 4471 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4472 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4473 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4474 | compiler_error(c, "asynchronous comprehension outside of " |
| 4475 | "an asynchronous function"); |
| 4476 | goto error_in_scope; |
| 4477 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4478 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4479 | if (type != COMP_GENEXP) { |
| 4480 | int op; |
| 4481 | switch (type) { |
| 4482 | case COMP_LISTCOMP: |
| 4483 | op = BUILD_LIST; |
| 4484 | break; |
| 4485 | case COMP_SETCOMP: |
| 4486 | op = BUILD_SET; |
| 4487 | break; |
| 4488 | case COMP_DICTCOMP: |
| 4489 | op = BUILD_MAP; |
| 4490 | break; |
| 4491 | default: |
| 4492 | PyErr_Format(PyExc_SystemError, |
| 4493 | "unknown comprehension type %d", type); |
| 4494 | goto error_in_scope; |
| 4495 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | ADDOP_I(c, op, 0); |
| 4498 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4499 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4500 | if (!compiler_comprehension_generator(c, generators, 0, elt, |
| 4501 | val, type)) |
| 4502 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4503 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | if (type != COMP_GENEXP) { |
| 4505 | ADDOP(c, RETURN_VALUE); |
| 4506 | } |
| 4507 | |
| 4508 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4509 | qualname = c->u->u_qualname; |
| 4510 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4511 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4512 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4513 | goto error; |
| 4514 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4515 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4517 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4518 | Py_DECREF(co); |
| 4519 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4520 | VISIT(c, expr, outermost->iter); |
| 4521 | |
| 4522 | if (outermost->is_async) { |
| 4523 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4524 | } else { |
| 4525 | ADDOP(c, GET_ITER); |
| 4526 | } |
| 4527 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4528 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4529 | |
| 4530 | if (is_async_generator && type != COMP_GENEXP) { |
| 4531 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4532 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4533 | ADDOP(c, YIELD_FROM); |
| 4534 | } |
| 4535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4536 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4537 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4538 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4539 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4540 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | Py_XDECREF(co); |
| 4542 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4543 | } |
| 4544 | |
| 4545 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4546 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4547 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4548 | static identifier name; |
| 4549 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4550 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4551 | if (!name) |
| 4552 | return 0; |
| 4553 | } |
| 4554 | assert(e->kind == GeneratorExp_kind); |
| 4555 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4556 | e->v.GeneratorExp.generators, |
| 4557 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4558 | } |
| 4559 | |
| 4560 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4561 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4562 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | static identifier name; |
| 4564 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4565 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | if (!name) |
| 4567 | return 0; |
| 4568 | } |
| 4569 | assert(e->kind == ListComp_kind); |
| 4570 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4571 | e->v.ListComp.generators, |
| 4572 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4573 | } |
| 4574 | |
| 4575 | static int |
| 4576 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4577 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | static identifier name; |
| 4579 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4580 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4581 | if (!name) |
| 4582 | return 0; |
| 4583 | } |
| 4584 | assert(e->kind == SetComp_kind); |
| 4585 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4586 | e->v.SetComp.generators, |
| 4587 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4588 | } |
| 4589 | |
| 4590 | |
| 4591 | static int |
| 4592 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4594 | static identifier name; |
| 4595 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4596 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4597 | if (!name) |
| 4598 | return 0; |
| 4599 | } |
| 4600 | assert(e->kind == DictComp_kind); |
| 4601 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4602 | e->v.DictComp.generators, |
| 4603 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4604 | } |
| 4605 | |
| 4606 | |
| 4607 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4608 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4610 | VISIT(c, expr, k->value); |
| 4611 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4612 | } |
| 4613 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4614 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4615 | whether they are true or false. |
| 4616 | |
| 4617 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4618 | */ |
| 4619 | |
| 4620 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4621 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4622 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4623 | if (e->kind == Constant_kind) { |
| 4624 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4625 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4626 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4627 | } |
| 4628 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4629 | |
| 4630 | /* |
| 4631 | Implements the async with statement. |
| 4632 | |
| 4633 | The semantics outlined in that PEP are as follows: |
| 4634 | |
| 4635 | async with EXPR as VAR: |
| 4636 | BLOCK |
| 4637 | |
| 4638 | It is implemented roughly as: |
| 4639 | |
| 4640 | context = EXPR |
| 4641 | exit = context.__aexit__ # not calling it |
| 4642 | value = await context.__aenter__() |
| 4643 | try: |
| 4644 | VAR = value # if VAR present in the syntax |
| 4645 | BLOCK |
| 4646 | finally: |
| 4647 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4648 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4649 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4650 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4651 | if not (await exit(*exc)): |
| 4652 | raise |
| 4653 | */ |
| 4654 | static int |
| 4655 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4656 | { |
| 4657 | basicblock *block, *finally; |
| 4658 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4659 | |
| 4660 | assert(s->kind == AsyncWith_kind); |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4661 | if (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT){ |
| 4662 | c->u->u_ste->ste_coroutine = 1; |
| 4663 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4664 | return compiler_error(c, "'async with' outside async function"); |
| 4665 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4666 | |
| 4667 | block = compiler_new_block(c); |
| 4668 | finally = compiler_new_block(c); |
| 4669 | if (!block || !finally) |
| 4670 | return 0; |
| 4671 | |
| 4672 | /* Evaluate EXPR */ |
| 4673 | VISIT(c, expr, item->context_expr); |
| 4674 | |
| 4675 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4676 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4677 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4678 | ADDOP(c, YIELD_FROM); |
| 4679 | |
| 4680 | ADDOP_JREL(c, SETUP_ASYNC_WITH, finally); |
| 4681 | |
| 4682 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4683 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4684 | if (!compiler_push_fblock(c, ASYNC_WITH, block, finally)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4685 | return 0; |
| 4686 | } |
| 4687 | |
| 4688 | if (item->optional_vars) { |
| 4689 | VISIT(c, expr, item->optional_vars); |
| 4690 | } |
| 4691 | else { |
| 4692 | /* Discard result from context.__aenter__() */ |
| 4693 | ADDOP(c, POP_TOP); |
| 4694 | } |
| 4695 | |
| 4696 | pos++; |
| 4697 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4698 | /* BLOCK code */ |
| 4699 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4700 | else if (!compiler_async_with(c, s, pos)) |
| 4701 | return 0; |
| 4702 | |
| 4703 | /* End of try block; start the finally block */ |
| 4704 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4705 | ADDOP(c, BEGIN_FINALLY); |
| 4706 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4707 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4708 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4709 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4710 | return 0; |
| 4711 | |
| 4712 | /* Finally block starts; context.__exit__ is on the stack under |
| 4713 | the exception or return information. Just issue our magic |
| 4714 | opcode. */ |
| 4715 | ADDOP(c, WITH_CLEANUP_START); |
| 4716 | |
| 4717 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4718 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4719 | ADDOP(c, YIELD_FROM); |
| 4720 | |
| 4721 | ADDOP(c, WITH_CLEANUP_FINISH); |
| 4722 | |
| 4723 | /* Finally block ends. */ |
| 4724 | ADDOP(c, END_FINALLY); |
| 4725 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4726 | return 1; |
| 4727 | } |
| 4728 | |
| 4729 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4730 | /* |
| 4731 | Implements the with statement from PEP 343. |
| 4732 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | The semantics outlined in that PEP are as follows: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4734 | |
| 4735 | with EXPR as VAR: |
| 4736 | BLOCK |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4737 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4738 | It is implemented roughly as: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4739 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4740 | context = EXPR |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4741 | exit = context.__exit__ # not calling it |
| 4742 | value = context.__enter__() |
| 4743 | try: |
| 4744 | VAR = value # if VAR present in the syntax |
| 4745 | BLOCK |
| 4746 | finally: |
| 4747 | if an exception was raised: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4748 | exc = copy of (exception, instance, traceback) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4749 | else: |
Serhiy Storchaka | d741a88 | 2015-06-11 00:06:39 +0300 | [diff] [blame] | 4750 | exc = (None, None, None) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4751 | exit(*exc) |
| 4752 | */ |
| 4753 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4754 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4755 | { |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4756 | basicblock *block, *finally; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4757 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4758 | |
| 4759 | assert(s->kind == With_kind); |
| 4760 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4761 | block = compiler_new_block(c); |
| 4762 | finally = compiler_new_block(c); |
| 4763 | if (!block || !finally) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4764 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4765 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4766 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4767 | VISIT(c, expr, item->context_expr); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4768 | ADDOP_JREL(c, SETUP_WITH, finally); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4769 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4770 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4771 | compiler_use_next_block(c, block); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4772 | if (!compiler_push_fblock(c, WITH, block, finally)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4773 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4774 | } |
| 4775 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4776 | if (item->optional_vars) { |
| 4777 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4778 | } |
| 4779 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4780 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4781 | ADDOP(c, POP_TOP); |
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 | pos++; |
| 4785 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4786 | /* BLOCK code */ |
| 4787 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4788 | else if (!compiler_with(c, s, pos)) |
| 4789 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4790 | |
| 4791 | /* End of try block; start the finally block */ |
| 4792 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4793 | ADDOP(c, BEGIN_FINALLY); |
| 4794 | compiler_pop_fblock(c, WITH, block); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4795 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4796 | compiler_use_next_block(c, finally); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4797 | if (!compiler_push_fblock(c, FINALLY_END, finally, NULL)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4798 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4799 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 4800 | /* Finally block starts; context.__exit__ is on the stack under |
| 4801 | the exception or return information. Just issue our magic |
| 4802 | opcode. */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4803 | ADDOP(c, WITH_CLEANUP_START); |
| 4804 | ADDOP(c, WITH_CLEANUP_FINISH); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4805 | |
| 4806 | /* Finally block ends. */ |
| 4807 | ADDOP(c, END_FINALLY); |
| 4808 | compiler_pop_fblock(c, FINALLY_END, finally); |
| 4809 | return 1; |
| 4810 | } |
| 4811 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4812 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4813 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4814 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4815 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4816 | case NamedExpr_kind: |
| 4817 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4818 | ADDOP(c, DUP_TOP); |
| 4819 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4820 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4821 | case BoolOp_kind: |
| 4822 | return compiler_boolop(c, e); |
| 4823 | case BinOp_kind: |
| 4824 | VISIT(c, expr, e->v.BinOp.left); |
| 4825 | VISIT(c, expr, e->v.BinOp.right); |
| 4826 | ADDOP(c, binop(c, e->v.BinOp.op)); |
| 4827 | break; |
| 4828 | case UnaryOp_kind: |
| 4829 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4830 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4831 | break; |
| 4832 | case Lambda_kind: |
| 4833 | return compiler_lambda(c, e); |
| 4834 | case IfExp_kind: |
| 4835 | return compiler_ifexp(c, e); |
| 4836 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4837 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4838 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4839 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4840 | case GeneratorExp_kind: |
| 4841 | return compiler_genexp(c, e); |
| 4842 | case ListComp_kind: |
| 4843 | return compiler_listcomp(c, e); |
| 4844 | case SetComp_kind: |
| 4845 | return compiler_setcomp(c, e); |
| 4846 | case DictComp_kind: |
| 4847 | return compiler_dictcomp(c, e); |
| 4848 | case Yield_kind: |
| 4849 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4850 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4851 | if (e->v.Yield.value) { |
| 4852 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4853 | } |
| 4854 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4855 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4856 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4857 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4858 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4859 | case YieldFrom_kind: |
| 4860 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4861 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4862 | |
| 4863 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4864 | return compiler_error(c, "'yield from' inside async function"); |
| 4865 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4866 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4867 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4868 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4869 | ADDOP(c, YIELD_FROM); |
| 4870 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4871 | case Await_kind: |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4872 | if (!(c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT)){ |
| 4873 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 4874 | return compiler_error(c, "'await' outside function"); |
| 4875 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4876 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 4877 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4878 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 4879 | return compiler_error(c, "'await' outside async function"); |
| 4880 | } |
| 4881 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4882 | |
| 4883 | VISIT(c, expr, e->v.Await.value); |
| 4884 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4885 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4886 | ADDOP(c, YIELD_FROM); |
| 4887 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4888 | case Compare_kind: |
| 4889 | return compiler_compare(c, e); |
| 4890 | case Call_kind: |
| 4891 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4892 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4893 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4894 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4895 | case JoinedStr_kind: |
| 4896 | return compiler_joined_str(c, e); |
| 4897 | case FormattedValue_kind: |
| 4898 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4899 | /* The following exprs can be assignment targets. */ |
| 4900 | case Attribute_kind: |
| 4901 | if (e->v.Attribute.ctx != AugStore) |
| 4902 | VISIT(c, expr, e->v.Attribute.value); |
| 4903 | switch (e->v.Attribute.ctx) { |
| 4904 | case AugLoad: |
| 4905 | ADDOP(c, DUP_TOP); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4906 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4907 | case Load: |
| 4908 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4909 | break; |
| 4910 | case AugStore: |
| 4911 | ADDOP(c, ROT_TWO); |
Stefan Krah | f432a32 | 2017-08-21 13:09:59 +0200 | [diff] [blame] | 4912 | /* Fall through */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4913 | case Store: |
| 4914 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4915 | break; |
| 4916 | case Del: |
| 4917 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4918 | break; |
| 4919 | case Param: |
| 4920 | default: |
| 4921 | PyErr_SetString(PyExc_SystemError, |
| 4922 | "param invalid in attribute expression"); |
| 4923 | return 0; |
| 4924 | } |
| 4925 | break; |
| 4926 | case Subscript_kind: |
| 4927 | switch (e->v.Subscript.ctx) { |
| 4928 | case AugLoad: |
| 4929 | VISIT(c, expr, e->v.Subscript.value); |
| 4930 | VISIT_SLICE(c, e->v.Subscript.slice, AugLoad); |
| 4931 | break; |
| 4932 | case Load: |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4933 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 4934 | return 0; |
| 4935 | } |
| 4936 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 4937 | return 0; |
| 4938 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4939 | VISIT(c, expr, e->v.Subscript.value); |
| 4940 | VISIT_SLICE(c, e->v.Subscript.slice, Load); |
| 4941 | break; |
| 4942 | case AugStore: |
| 4943 | VISIT_SLICE(c, e->v.Subscript.slice, AugStore); |
| 4944 | break; |
| 4945 | case Store: |
| 4946 | VISIT(c, expr, e->v.Subscript.value); |
| 4947 | VISIT_SLICE(c, e->v.Subscript.slice, Store); |
| 4948 | break; |
| 4949 | case Del: |
| 4950 | VISIT(c, expr, e->v.Subscript.value); |
| 4951 | VISIT_SLICE(c, e->v.Subscript.slice, Del); |
| 4952 | break; |
| 4953 | case Param: |
| 4954 | default: |
| 4955 | PyErr_SetString(PyExc_SystemError, |
| 4956 | "param invalid in subscript expression"); |
| 4957 | return 0; |
| 4958 | } |
| 4959 | break; |
| 4960 | case Starred_kind: |
| 4961 | switch (e->v.Starred.ctx) { |
| 4962 | case Store: |
| 4963 | /* In all legitimate cases, the Starred node was already replaced |
| 4964 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 4965 | return compiler_error(c, |
| 4966 | "starred assignment target must be in a list or tuple"); |
| 4967 | default: |
| 4968 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4969 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4970 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4971 | case Name_kind: |
| 4972 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 4973 | /* child nodes of List and Tuple will have expr_context set */ |
| 4974 | case List_kind: |
| 4975 | return compiler_list(c, e); |
| 4976 | case Tuple_kind: |
| 4977 | return compiler_tuple(c, e); |
| 4978 | } |
| 4979 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4980 | } |
| 4981 | |
| 4982 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4983 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 4984 | { |
| 4985 | /* If expr e has a different line number than the last expr/stmt, |
| 4986 | set a new line number for the next instruction. |
| 4987 | */ |
| 4988 | int old_lineno = c->u->u_lineno; |
| 4989 | int old_col_offset = c->u->u_col_offset; |
| 4990 | if (e->lineno != c->u->u_lineno) { |
| 4991 | c->u->u_lineno = e->lineno; |
| 4992 | c->u->u_lineno_set = 0; |
| 4993 | } |
| 4994 | /* Updating the column offset is always harmless. */ |
| 4995 | c->u->u_col_offset = e->col_offset; |
| 4996 | |
| 4997 | int res = compiler_visit_expr1(c, e); |
| 4998 | |
| 4999 | if (old_lineno != c->u->u_lineno) { |
| 5000 | c->u->u_lineno = old_lineno; |
| 5001 | c->u->u_lineno_set = 0; |
| 5002 | } |
| 5003 | c->u->u_col_offset = old_col_offset; |
| 5004 | return res; |
| 5005 | } |
| 5006 | |
| 5007 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5008 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5009 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5010 | expr_ty e = s->v.AugAssign.target; |
| 5011 | expr_ty auge; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5012 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | assert(s->kind == AugAssign_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5014 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | switch (e->kind) { |
| 5016 | case Attribute_kind: |
| 5017 | auge = Attribute(e->v.Attribute.value, e->v.Attribute.attr, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5018 | AugLoad, e->lineno, e->col_offset, |
| 5019 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5020 | if (auge == NULL) |
| 5021 | return 0; |
| 5022 | VISIT(c, expr, auge); |
| 5023 | VISIT(c, expr, s->v.AugAssign.value); |
| 5024 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 5025 | auge->v.Attribute.ctx = AugStore; |
| 5026 | VISIT(c, expr, auge); |
| 5027 | break; |
| 5028 | case Subscript_kind: |
| 5029 | auge = Subscript(e->v.Subscript.value, e->v.Subscript.slice, |
Ivan Levkivskyi | 9932a22 | 2019-01-22 11:18:22 +0000 | [diff] [blame] | 5030 | AugLoad, e->lineno, e->col_offset, |
| 5031 | e->end_lineno, e->end_col_offset, c->c_arena); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5032 | if (auge == NULL) |
| 5033 | return 0; |
| 5034 | VISIT(c, expr, auge); |
| 5035 | VISIT(c, expr, s->v.AugAssign.value); |
| 5036 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 5037 | auge->v.Subscript.ctx = AugStore; |
| 5038 | VISIT(c, expr, auge); |
| 5039 | break; |
| 5040 | case Name_kind: |
| 5041 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5042 | return 0; |
| 5043 | VISIT(c, expr, s->v.AugAssign.value); |
| 5044 | ADDOP(c, inplace_binop(c, s->v.AugAssign.op)); |
| 5045 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5046 | default: |
| 5047 | PyErr_Format(PyExc_SystemError, |
| 5048 | "invalid node type (%d) for augmented assignment", |
| 5049 | e->kind); |
| 5050 | return 0; |
| 5051 | } |
| 5052 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5053 | } |
| 5054 | |
| 5055 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5056 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5057 | { |
| 5058 | VISIT(c, expr, e); |
| 5059 | ADDOP(c, POP_TOP); |
| 5060 | return 1; |
| 5061 | } |
| 5062 | |
| 5063 | static int |
| 5064 | check_annotation(struct compiler *c, stmt_ty s) |
| 5065 | { |
| 5066 | /* Annotations are only evaluated in a module or class. */ |
| 5067 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5068 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5069 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5070 | } |
| 5071 | return 1; |
| 5072 | } |
| 5073 | |
| 5074 | static int |
| 5075 | check_ann_slice(struct compiler *c, slice_ty sl) |
| 5076 | { |
| 5077 | switch(sl->kind) { |
| 5078 | case Index_kind: |
| 5079 | return check_ann_expr(c, sl->v.Index.value); |
| 5080 | case Slice_kind: |
| 5081 | if (sl->v.Slice.lower && !check_ann_expr(c, sl->v.Slice.lower)) { |
| 5082 | return 0; |
| 5083 | } |
| 5084 | if (sl->v.Slice.upper && !check_ann_expr(c, sl->v.Slice.upper)) { |
| 5085 | return 0; |
| 5086 | } |
| 5087 | if (sl->v.Slice.step && !check_ann_expr(c, sl->v.Slice.step)) { |
| 5088 | return 0; |
| 5089 | } |
| 5090 | break; |
| 5091 | default: |
| 5092 | PyErr_SetString(PyExc_SystemError, |
| 5093 | "unexpected slice kind"); |
| 5094 | return 0; |
| 5095 | } |
| 5096 | return 1; |
| 5097 | } |
| 5098 | |
| 5099 | static int |
| 5100 | check_ann_subscr(struct compiler *c, slice_ty sl) |
| 5101 | { |
| 5102 | /* We check that everything in a subscript is defined at runtime. */ |
| 5103 | Py_ssize_t i, n; |
| 5104 | |
| 5105 | switch (sl->kind) { |
| 5106 | case Index_kind: |
| 5107 | case Slice_kind: |
| 5108 | if (!check_ann_slice(c, sl)) { |
| 5109 | return 0; |
| 5110 | } |
| 5111 | break; |
| 5112 | case ExtSlice_kind: |
| 5113 | n = asdl_seq_LEN(sl->v.ExtSlice.dims); |
| 5114 | for (i = 0; i < n; i++) { |
| 5115 | slice_ty subsl = (slice_ty)asdl_seq_GET(sl->v.ExtSlice.dims, i); |
| 5116 | switch (subsl->kind) { |
| 5117 | case Index_kind: |
| 5118 | case Slice_kind: |
| 5119 | if (!check_ann_slice(c, subsl)) { |
| 5120 | return 0; |
| 5121 | } |
| 5122 | break; |
| 5123 | case ExtSlice_kind: |
| 5124 | default: |
| 5125 | PyErr_SetString(PyExc_SystemError, |
| 5126 | "extended slice invalid in nested slice"); |
| 5127 | return 0; |
| 5128 | } |
| 5129 | } |
| 5130 | break; |
| 5131 | default: |
| 5132 | PyErr_Format(PyExc_SystemError, |
| 5133 | "invalid subscript kind %d", sl->kind); |
| 5134 | return 0; |
| 5135 | } |
| 5136 | return 1; |
| 5137 | } |
| 5138 | |
| 5139 | static int |
| 5140 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5141 | { |
| 5142 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5143 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5144 | |
| 5145 | assert(s->kind == AnnAssign_kind); |
| 5146 | |
| 5147 | /* We perform the actual assignment first. */ |
| 5148 | if (s->v.AnnAssign.value) { |
| 5149 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5150 | VISIT(c, expr, targ); |
| 5151 | } |
| 5152 | switch (targ->kind) { |
| 5153 | case Name_kind: |
| 5154 | /* If we have a simple name in a module or class, store annotation. */ |
| 5155 | if (s->v.AnnAssign.simple && |
| 5156 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5157 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5158 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5159 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5160 | } |
| 5161 | else { |
| 5162 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5163 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5164 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5165 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5166 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5167 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5168 | } |
| 5169 | break; |
| 5170 | case Attribute_kind: |
| 5171 | if (!s->v.AnnAssign.value && |
| 5172 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5173 | return 0; |
| 5174 | } |
| 5175 | break; |
| 5176 | case Subscript_kind: |
| 5177 | if (!s->v.AnnAssign.value && |
| 5178 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5179 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5180 | return 0; |
| 5181 | } |
| 5182 | break; |
| 5183 | default: |
| 5184 | PyErr_Format(PyExc_SystemError, |
| 5185 | "invalid node type (%d) for annotated assignment", |
| 5186 | targ->kind); |
| 5187 | return 0; |
| 5188 | } |
| 5189 | /* Annotation is evaluated last. */ |
| 5190 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5191 | return 0; |
| 5192 | } |
| 5193 | return 1; |
| 5194 | } |
| 5195 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5196 | /* Raises a SyntaxError and returns 0. |
| 5197 | If something goes wrong, a different exception may be raised. |
| 5198 | */ |
| 5199 | |
| 5200 | static int |
| 5201 | compiler_error(struct compiler *c, const char *errstr) |
| 5202 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5203 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5204 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5205 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5206 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5207 | if (!loc) { |
| 5208 | Py_INCREF(Py_None); |
| 5209 | loc = Py_None; |
| 5210 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5211 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5212 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5213 | if (!u) |
| 5214 | goto exit; |
| 5215 | v = Py_BuildValue("(zO)", errstr, u); |
| 5216 | if (!v) |
| 5217 | goto exit; |
| 5218 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5219 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5220 | Py_DECREF(loc); |
| 5221 | Py_XDECREF(u); |
| 5222 | Py_XDECREF(v); |
| 5223 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5224 | } |
| 5225 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5226 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5227 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5228 | and returns 0. |
| 5229 | */ |
| 5230 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5231 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5232 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5233 | va_list vargs; |
| 5234 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5235 | va_start(vargs, format); |
| 5236 | #else |
| 5237 | va_start(vargs); |
| 5238 | #endif |
| 5239 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5240 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5241 | if (msg == NULL) { |
| 5242 | return 0; |
| 5243 | } |
| 5244 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5245 | c->u->u_lineno, NULL, NULL) < 0) |
| 5246 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5247 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5248 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5249 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5250 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5251 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5252 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5253 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5254 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5255 | return 0; |
| 5256 | } |
| 5257 | Py_DECREF(msg); |
| 5258 | return 1; |
| 5259 | } |
| 5260 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5261 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5262 | compiler_handle_subscr(struct compiler *c, const char *kind, |
| 5263 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5264 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5265 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5267 | /* XXX this code is duplicated */ |
| 5268 | switch (ctx) { |
| 5269 | case AugLoad: /* fall through to Load */ |
| 5270 | case Load: op = BINARY_SUBSCR; break; |
| 5271 | case AugStore:/* fall through to Store */ |
| 5272 | case Store: op = STORE_SUBSCR; break; |
| 5273 | case Del: op = DELETE_SUBSCR; break; |
| 5274 | case Param: |
| 5275 | PyErr_Format(PyExc_SystemError, |
| 5276 | "invalid %s kind %d in subscript\n", |
| 5277 | kind, ctx); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | if (ctx == AugLoad) { |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 5281 | ADDOP(c, DUP_TOP_TWO); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5282 | } |
| 5283 | else if (ctx == AugStore) { |
| 5284 | ADDOP(c, ROT_THREE); |
| 5285 | } |
| 5286 | ADDOP(c, op); |
| 5287 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5288 | } |
| 5289 | |
| 5290 | static int |
| 5291 | compiler_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5292 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5293 | int n = 2; |
| 5294 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5296 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5297 | if (s->v.Slice.lower) { |
| 5298 | VISIT(c, expr, s->v.Slice.lower); |
| 5299 | } |
| 5300 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5301 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5302 | } |
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 | if (s->v.Slice.upper) { |
| 5305 | VISIT(c, expr, s->v.Slice.upper); |
| 5306 | } |
| 5307 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5308 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5309 | } |
| 5310 | |
| 5311 | if (s->v.Slice.step) { |
| 5312 | n++; |
| 5313 | VISIT(c, expr, s->v.Slice.step); |
| 5314 | } |
| 5315 | ADDOP_I(c, BUILD_SLICE, n); |
| 5316 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5317 | } |
| 5318 | |
| 5319 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5320 | compiler_visit_nested_slice(struct compiler *c, slice_ty s, |
| 5321 | expr_context_ty ctx) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5322 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5323 | switch (s->kind) { |
| 5324 | case Slice_kind: |
| 5325 | return compiler_slice(c, s, ctx); |
| 5326 | case Index_kind: |
| 5327 | VISIT(c, expr, s->v.Index.value); |
| 5328 | break; |
| 5329 | case ExtSlice_kind: |
| 5330 | default: |
| 5331 | PyErr_SetString(PyExc_SystemError, |
| 5332 | "extended slice invalid in nested slice"); |
| 5333 | return 0; |
| 5334 | } |
| 5335 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5336 | } |
| 5337 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5338 | static int |
| 5339 | compiler_visit_slice(struct compiler *c, slice_ty s, expr_context_ty ctx) |
| 5340 | { |
Serhiy Storchaka | e2f92de | 2017-11-11 13:06:26 +0200 | [diff] [blame] | 5341 | const char * kindname = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5342 | switch (s->kind) { |
| 5343 | case Index_kind: |
| 5344 | kindname = "index"; |
| 5345 | if (ctx != AugStore) { |
| 5346 | VISIT(c, expr, s->v.Index.value); |
| 5347 | } |
| 5348 | break; |
| 5349 | case Slice_kind: |
| 5350 | kindname = "slice"; |
| 5351 | if (ctx != AugStore) { |
| 5352 | if (!compiler_slice(c, s, ctx)) |
| 5353 | return 0; |
| 5354 | } |
| 5355 | break; |
| 5356 | case ExtSlice_kind: |
| 5357 | kindname = "extended slice"; |
| 5358 | if (ctx != AugStore) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5359 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ExtSlice.dims); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5360 | for (i = 0; i < n; i++) { |
| 5361 | slice_ty sub = (slice_ty)asdl_seq_GET( |
| 5362 | s->v.ExtSlice.dims, i); |
| 5363 | if (!compiler_visit_nested_slice(c, sub, ctx)) |
| 5364 | return 0; |
| 5365 | } |
| 5366 | ADDOP_I(c, BUILD_TUPLE, n); |
| 5367 | } |
| 5368 | break; |
| 5369 | default: |
| 5370 | PyErr_Format(PyExc_SystemError, |
| 5371 | "invalid subscript kind %d", s->kind); |
| 5372 | return 0; |
| 5373 | } |
| 5374 | return compiler_handle_subscr(c, kindname, ctx); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5375 | } |
| 5376 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5377 | /* End of the compiler section, beginning of the assembler section */ |
| 5378 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5379 | /* do depth-first search of basic block graph, starting with block. |
| 5380 | post records the block indices in post-order. |
| 5381 | |
| 5382 | XXX must handle implicit jumps from one block to next |
| 5383 | */ |
| 5384 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5385 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5386 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5387 | int a_offset; /* offset into bytecode */ |
| 5388 | int a_nblocks; /* number of reachable blocks */ |
| 5389 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
| 5390 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5391 | int a_lnotab_off; /* offset into lnotab */ |
| 5392 | int a_lineno; /* last lineno of emitted instruction */ |
| 5393 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5394 | }; |
| 5395 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5396 | static void |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5397 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5398 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5399 | int i, j; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5400 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5401 | /* Get rid of recursion for normal control flow. |
| 5402 | Since the number of blocks is limited, unused space in a_postorder |
| 5403 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5404 | blocks. */ |
| 5405 | for (j = end; b && !b->b_seen; b = b->b_next) { |
| 5406 | b->b_seen = 1; |
| 5407 | assert(a->a_nblocks < j); |
| 5408 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5409 | } |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5410 | while (j < end) { |
| 5411 | b = a->a_postorder[j++]; |
| 5412 | for (i = 0; i < b->b_iused; i++) { |
| 5413 | struct instr *instr = &b->b_instr[i]; |
| 5414 | if (instr->i_jrel || instr->i_jabs) |
| 5415 | dfs(c, instr->i_target, a, j); |
| 5416 | } |
| 5417 | assert(a->a_nblocks < j); |
| 5418 | a->a_postorder[a->a_nblocks++] = b; |
| 5419 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5420 | } |
| 5421 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5422 | Py_LOCAL_INLINE(void) |
| 5423 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5424 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5425 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5426 | if (b->b_startdepth < depth) { |
| 5427 | assert(b->b_startdepth < 0); |
| 5428 | b->b_startdepth = depth; |
| 5429 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5430 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5431 | } |
| 5432 | |
| 5433 | /* Find the flow path that needs the largest stack. We assume that |
| 5434 | * cycles in the flow graph have no net effect on the stack depth. |
| 5435 | */ |
| 5436 | static int |
| 5437 | stackdepth(struct compiler *c) |
| 5438 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5439 | basicblock *b, *entryblock = NULL; |
| 5440 | basicblock **stack, **sp; |
| 5441 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5442 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5443 | b->b_startdepth = INT_MIN; |
| 5444 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5445 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5446 | } |
| 5447 | if (!entryblock) |
| 5448 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5449 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5450 | if (!stack) { |
| 5451 | PyErr_NoMemory(); |
| 5452 | return -1; |
| 5453 | } |
| 5454 | |
| 5455 | sp = stack; |
| 5456 | stackdepth_push(&sp, entryblock, 0); |
| 5457 | while (sp != stack) { |
| 5458 | b = *--sp; |
| 5459 | int depth = b->b_startdepth; |
| 5460 | assert(depth >= 0); |
| 5461 | basicblock *next = b->b_next; |
| 5462 | for (int i = 0; i < b->b_iused; i++) { |
| 5463 | struct instr *instr = &b->b_instr[i]; |
| 5464 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5465 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5466 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5467 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5468 | } |
| 5469 | int new_depth = depth + effect; |
| 5470 | if (new_depth > maxdepth) { |
| 5471 | maxdepth = new_depth; |
| 5472 | } |
| 5473 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5474 | if (instr->i_jrel || instr->i_jabs) { |
| 5475 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5476 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5477 | int target_depth = depth + effect; |
| 5478 | if (target_depth > maxdepth) { |
| 5479 | maxdepth = target_depth; |
| 5480 | } |
| 5481 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5482 | if (instr->i_opcode == CALL_FINALLY) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5483 | assert(instr->i_target->b_startdepth >= 0); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5484 | assert(instr->i_target->b_startdepth >= target_depth); |
| 5485 | depth = new_depth; |
| 5486 | continue; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5487 | } |
| 5488 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5489 | } |
| 5490 | depth = new_depth; |
| 5491 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5492 | instr->i_opcode == JUMP_FORWARD || |
| 5493 | instr->i_opcode == RETURN_VALUE || |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5494 | instr->i_opcode == RAISE_VARARGS) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5495 | { |
| 5496 | /* remaining code is dead */ |
| 5497 | next = NULL; |
| 5498 | break; |
| 5499 | } |
| 5500 | } |
| 5501 | if (next != NULL) { |
| 5502 | stackdepth_push(&sp, next, depth); |
| 5503 | } |
| 5504 | } |
| 5505 | PyObject_Free(stack); |
| 5506 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5507 | } |
| 5508 | |
| 5509 | static int |
| 5510 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5512 | memset(a, 0, sizeof(struct assembler)); |
| 5513 | a->a_lineno = firstlineno; |
| 5514 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5515 | if (!a->a_bytecode) |
| 5516 | return 0; |
| 5517 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5518 | if (!a->a_lnotab) |
| 5519 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5520 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5521 | PyErr_NoMemory(); |
| 5522 | return 0; |
| 5523 | } |
| 5524 | a->a_postorder = (basicblock **)PyObject_Malloc( |
| 5525 | sizeof(basicblock *) * nblocks); |
| 5526 | if (!a->a_postorder) { |
| 5527 | PyErr_NoMemory(); |
| 5528 | return 0; |
| 5529 | } |
| 5530 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5531 | } |
| 5532 | |
| 5533 | static void |
| 5534 | assemble_free(struct assembler *a) |
| 5535 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5536 | Py_XDECREF(a->a_bytecode); |
| 5537 | Py_XDECREF(a->a_lnotab); |
| 5538 | if (a->a_postorder) |
| 5539 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5540 | } |
| 5541 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5542 | static int |
| 5543 | blocksize(basicblock *b) |
| 5544 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5545 | int i; |
| 5546 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5547 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5548 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5549 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5550 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5551 | } |
| 5552 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5553 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5554 | the instruction's bytecode offset and line number. See |
| 5555 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5556 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5557 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5558 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5559 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5561 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5562 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5563 | |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5564 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5565 | d_lineno = i->i_lineno - a->a_lineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5566 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5567 | assert(d_bytecode >= 0); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5568 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5569 | if(d_bytecode == 0 && d_lineno == 0) |
| 5570 | return 1; |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5571 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5572 | if (d_bytecode > 255) { |
| 5573 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5574 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5575 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5576 | if (nbytes >= len) { |
| 5577 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5578 | len = nbytes; |
| 5579 | else if (len <= INT_MAX / 2) |
| 5580 | len *= 2; |
| 5581 | else { |
| 5582 | PyErr_NoMemory(); |
| 5583 | return 0; |
| 5584 | } |
| 5585 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5586 | return 0; |
| 5587 | } |
| 5588 | lnotab = (unsigned char *) |
| 5589 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5590 | for (j = 0; j < ncodes; j++) { |
| 5591 | *lnotab++ = 255; |
| 5592 | *lnotab++ = 0; |
| 5593 | } |
| 5594 | d_bytecode -= ncodes * 255; |
| 5595 | a->a_lnotab_off += ncodes * 2; |
| 5596 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5597 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5598 | |
| 5599 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5600 | int j, nbytes, ncodes, k; |
| 5601 | if (d_lineno < 0) { |
| 5602 | k = -128; |
| 5603 | /* use division on positive numbers */ |
| 5604 | ncodes = (-d_lineno) / 128; |
| 5605 | } |
| 5606 | else { |
| 5607 | k = 127; |
| 5608 | ncodes = d_lineno / 127; |
| 5609 | } |
| 5610 | d_lineno -= ncodes * k; |
| 5611 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5612 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5613 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5614 | if (nbytes >= len) { |
| 5615 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5616 | len = nbytes; |
| 5617 | else if (len <= INT_MAX / 2) |
| 5618 | len *= 2; |
| 5619 | else { |
| 5620 | PyErr_NoMemory(); |
| 5621 | return 0; |
| 5622 | } |
| 5623 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5624 | return 0; |
| 5625 | } |
| 5626 | lnotab = (unsigned char *) |
| 5627 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5628 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5629 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5630 | d_bytecode = 0; |
| 5631 | for (j = 1; j < ncodes; j++) { |
| 5632 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5633 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5634 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5635 | a->a_lnotab_off += ncodes * 2; |
| 5636 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5637 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5639 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5640 | if (a->a_lnotab_off + 2 >= len) { |
| 5641 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5642 | return 0; |
| 5643 | } |
| 5644 | lnotab = (unsigned char *) |
| 5645 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5646 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5647 | a->a_lnotab_off += 2; |
| 5648 | if (d_bytecode) { |
| 5649 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5650 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | } |
| 5652 | else { /* First line of a block; def stmt, etc. */ |
| 5653 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5654 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5655 | } |
| 5656 | a->a_lineno = i->i_lineno; |
| 5657 | a->a_lineno_off = a->a_offset; |
| 5658 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5659 | } |
| 5660 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5661 | /* assemble_emit() |
| 5662 | Extend the bytecode with a new instruction. |
| 5663 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5664 | */ |
| 5665 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5666 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5667 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5668 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5669 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5670 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5671 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5672 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5673 | arg = i->i_oparg; |
| 5674 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5675 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5676 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5677 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | if (len > PY_SSIZE_T_MAX / 2) |
| 5679 | return 0; |
| 5680 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5681 | return 0; |
| 5682 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5683 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5684 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5685 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5686 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5687 | } |
| 5688 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5689 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5690 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5691 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5692 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5693 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | /* Compute the size of each block and fixup jump args. |
| 5697 | Replace block pointer with position in bytecode. */ |
| 5698 | do { |
| 5699 | totsize = 0; |
| 5700 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5701 | b = a->a_postorder[i]; |
| 5702 | bsize = blocksize(b); |
| 5703 | b->b_offset = totsize; |
| 5704 | totsize += bsize; |
| 5705 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5706 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5707 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5708 | bsize = b->b_offset; |
| 5709 | for (i = 0; i < b->b_iused; i++) { |
| 5710 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5711 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5712 | /* Relative jumps are computed relative to |
| 5713 | the instruction pointer after fetching |
| 5714 | the jump instruction. |
| 5715 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5716 | bsize += isize; |
| 5717 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5718 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5719 | if (instr->i_jrel) { |
| 5720 | instr->i_oparg -= bsize; |
| 5721 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5722 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5723 | if (instrsize(instr->i_oparg) != isize) { |
| 5724 | extended_arg_recompile = 1; |
| 5725 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5726 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5727 | } |
| 5728 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5730 | /* XXX: This is an awful hack that could hurt performance, but |
| 5731 | on the bright side it should work until we come up |
| 5732 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5733 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5734 | The issue is that in the first loop blocksize() is called |
| 5735 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5736 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5737 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5738 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5739 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5740 | The only EXTENDED_ARGs that could be popping up are |
| 5741 | ones in jump instructions. So this should converge |
| 5742 | fairly quickly. |
| 5743 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5744 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5745 | } |
| 5746 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5747 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5748 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5750 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5751 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5752 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5753 | tuple = PyTuple_New(size); |
| 5754 | if (tuple == NULL) |
| 5755 | return NULL; |
| 5756 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5757 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5758 | Py_INCREF(k); |
| 5759 | assert((i - offset) < size); |
| 5760 | assert((i - offset) >= 0); |
| 5761 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5762 | } |
| 5763 | return tuple; |
| 5764 | } |
| 5765 | |
| 5766 | static PyObject * |
| 5767 | consts_dict_keys_inorder(PyObject *dict) |
| 5768 | { |
| 5769 | PyObject *consts, *k, *v; |
| 5770 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5771 | |
| 5772 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5773 | if (consts == NULL) |
| 5774 | return NULL; |
| 5775 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5776 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5777 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5778 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5779 | * the object we want is always second. */ |
| 5780 | if (PyTuple_CheckExact(k)) { |
| 5781 | k = PyTuple_GET_ITEM(k, 1); |
| 5782 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5783 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5784 | assert(i < size); |
| 5785 | assert(i >= 0); |
| 5786 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5787 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5788 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5789 | } |
| 5790 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5791 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5792 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5793 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5794 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5795 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5796 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5797 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5798 | if (ste->ste_nested) |
| 5799 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5800 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5801 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5802 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5803 | flags |= CO_COROUTINE; |
| 5804 | if (ste->ste_generator && ste->ste_coroutine) |
| 5805 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5806 | if (ste->ste_varargs) |
| 5807 | flags |= CO_VARARGS; |
| 5808 | if (ste->ste_varkeywords) |
| 5809 | flags |= CO_VARKEYWORDS; |
| 5810 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5811 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5812 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5813 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5814 | |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5815 | if ((c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) && |
| 5816 | ste->ste_coroutine && |
| 5817 | !ste->ste_generator) { |
| 5818 | flags |= CO_COROUTINE; |
| 5819 | } |
| 5820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5821 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5822 | } |
| 5823 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5824 | // Merge *tuple* with constant cache. |
| 5825 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5826 | static int |
| 5827 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5828 | { |
| 5829 | assert(PyTuple_CheckExact(*tuple)); |
| 5830 | |
| 5831 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5832 | if (key == NULL) { |
| 5833 | return 0; |
| 5834 | } |
| 5835 | |
| 5836 | // t is borrowed reference |
| 5837 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5838 | Py_DECREF(key); |
| 5839 | if (t == NULL) { |
| 5840 | return 0; |
| 5841 | } |
| 5842 | if (t == key) { // tuple is new constant. |
| 5843 | return 1; |
| 5844 | } |
| 5845 | |
| 5846 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5847 | Py_INCREF(u); |
| 5848 | Py_DECREF(*tuple); |
| 5849 | *tuple = u; |
| 5850 | return 1; |
| 5851 | } |
| 5852 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5853 | static PyCodeObject * |
| 5854 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5855 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5856 | PyObject *tmp; |
| 5857 | PyCodeObject *co = NULL; |
| 5858 | PyObject *consts = NULL; |
| 5859 | PyObject *names = NULL; |
| 5860 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5861 | PyObject *name = NULL; |
| 5862 | PyObject *freevars = NULL; |
| 5863 | PyObject *cellvars = NULL; |
| 5864 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5865 | Py_ssize_t nlocals; |
| 5866 | int nlocals_int; |
| 5867 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5868 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5869 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5870 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5871 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5872 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5873 | if (!consts || !names || !varnames) |
| 5874 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5875 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5876 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5877 | if (!cellvars) |
| 5878 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5879 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5880 | if (!freevars) |
| 5881 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5882 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5883 | if (!merge_const_tuple(c, &names) || |
| 5884 | !merge_const_tuple(c, &varnames) || |
| 5885 | !merge_const_tuple(c, &cellvars) || |
| 5886 | !merge_const_tuple(c, &freevars)) |
| 5887 | { |
| 5888 | goto error; |
| 5889 | } |
| 5890 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5891 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5892 | assert(nlocals < INT_MAX); |
| 5893 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5894 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | flags = compute_code_flags(c); |
| 5896 | if (flags < 0) |
| 5897 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5898 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5899 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5900 | if (!bytecode) |
| 5901 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5902 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5903 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5904 | if (!tmp) |
| 5905 | goto error; |
| 5906 | Py_DECREF(consts); |
| 5907 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5908 | if (!merge_const_tuple(c, &consts)) { |
| 5909 | goto error; |
| 5910 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5911 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5912 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5913 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5914 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5915 | maxdepth = stackdepth(c); |
| 5916 | if (maxdepth < 0) { |
| 5917 | goto error; |
| 5918 | } |
Miss Islington (bot) | cb083f7 | 2019-07-01 04:29:14 -0700 | [diff] [blame] | 5919 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
| 5920 | posonlyargcount, kwonlyargcount, nlocals_int, |
| 5921 | maxdepth, flags, bytecode, consts, names, |
| 5922 | varnames, freevars, cellvars, c->c_filename, |
| 5923 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5924 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5925 | Py_XDECREF(consts); |
| 5926 | Py_XDECREF(names); |
| 5927 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5928 | Py_XDECREF(name); |
| 5929 | Py_XDECREF(freevars); |
| 5930 | Py_XDECREF(cellvars); |
| 5931 | Py_XDECREF(bytecode); |
| 5932 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5933 | } |
| 5934 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5935 | |
| 5936 | /* For debugging purposes only */ |
| 5937 | #if 0 |
| 5938 | static void |
| 5939 | dump_instr(const struct instr *i) |
| 5940 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5941 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5942 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5943 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5944 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5945 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5946 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5947 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5948 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5949 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5950 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5951 | } |
| 5952 | |
| 5953 | static void |
| 5954 | dump_basicblock(const basicblock *b) |
| 5955 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5956 | const char *seen = b->b_seen ? "seen " : ""; |
| 5957 | const char *b_return = b->b_return ? "return " : ""; |
| 5958 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5959 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5960 | if (b->b_instr) { |
| 5961 | int i; |
| 5962 | for (i = 0; i < b->b_iused; i++) { |
| 5963 | fprintf(stderr, " [%02d] ", i); |
| 5964 | dump_instr(b->b_instr + i); |
| 5965 | } |
| 5966 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5967 | } |
| 5968 | #endif |
| 5969 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5970 | static PyCodeObject * |
| 5971 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5972 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5973 | basicblock *b, *entryblock; |
| 5974 | struct assembler a; |
| 5975 | int i, j, nblocks; |
| 5976 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5977 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5978 | /* Make sure every block that falls off the end returns None. |
| 5979 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5980 | block ends with a jump or return b_next shouldn't set. |
| 5981 | */ |
| 5982 | if (!c->u->u_curblock->b_return) { |
| 5983 | NEXT_BLOCK(c); |
| 5984 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5985 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | ADDOP(c, RETURN_VALUE); |
| 5987 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5988 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5989 | nblocks = 0; |
| 5990 | entryblock = NULL; |
| 5991 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5992 | nblocks++; |
| 5993 | entryblock = b; |
| 5994 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5995 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5996 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5997 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5998 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5999 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 6000 | else |
| 6001 | c->u->u_firstlineno = 1; |
| 6002 | } |
| 6003 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6004 | goto error; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 6005 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6006 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6007 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6008 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6010 | /* Emit code in reverse postorder from dfs. */ |
| 6011 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 6012 | b = a.a_postorder[i]; |
| 6013 | for (j = 0; j < b->b_iused; j++) |
| 6014 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6015 | goto error; |
| 6016 | } |
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 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6019 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6020 | 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] | 6021 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6022 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6023 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6024 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6025 | assemble_free(&a); |
| 6026 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6027 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6028 | |
| 6029 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6030 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6031 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6032 | PyArena *arena) |
| 6033 | { |
| 6034 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6035 | } |