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 | |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 26 | #include "pycore_pystate.h" /* _PyInterpreterState_GET_UNSAFE() */ |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 27 | #include "Python-ast.h" |
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 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 44 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 45 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 46 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 47 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 48 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 49 | unsigned i_jabs : 1; |
| 50 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 51 | unsigned char i_opcode; |
| 52 | int i_oparg; |
| 53 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 54 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 55 | }; |
| 56 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 57 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 58 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 59 | reverse order that the block are allocated. b_list points to the next |
| 60 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 61 | struct basicblock_ *b_list; |
| 62 | /* number of instructions used */ |
| 63 | int b_iused; |
| 64 | /* length of instruction array (b_instr) */ |
| 65 | int b_ialloc; |
| 66 | /* pointer to an array of instructions, initially NULL */ |
| 67 | struct instr *b_instr; |
| 68 | /* If b_next is non-NULL, it is a pointer to the next |
| 69 | block reached by normal control flow. */ |
| 70 | struct basicblock_ *b_next; |
| 71 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 72 | unsigned b_seen : 1; |
| 73 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 74 | unsigned b_return : 1; |
| 75 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 76 | int b_startdepth; |
| 77 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 78 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 79 | } basicblock; |
| 80 | |
| 81 | /* fblockinfo tracks the current frame block. |
| 82 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 83 | A frame block is used to handle loops, try/except, and try/finally. |
| 84 | It's called a frame block to distinguish it from a basic block in the |
| 85 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 86 | */ |
| 87 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 88 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END, |
| 89 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 90 | |
| 91 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 92 | enum fblocktype fb_type; |
| 93 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 94 | /* (optional) type-specific exit or cleanup block */ |
| 95 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 96 | /* (optional) additional information required for unwinding */ |
| 97 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 100 | enum { |
| 101 | COMPILER_SCOPE_MODULE, |
| 102 | COMPILER_SCOPE_CLASS, |
| 103 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 104 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 105 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 106 | COMPILER_SCOPE_COMPREHENSION, |
| 107 | }; |
| 108 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 109 | /* The following items change on entry and exit of code blocks. |
| 110 | They must be saved and restored when returning to a block. |
| 111 | */ |
| 112 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 113 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 115 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 116 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 117 | int u_scope_type; |
| 118 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 119 | /* The following fields are dicts that map objects to |
| 120 | the index of them in co_XXX. The index is used as |
| 121 | the argument for opcodes that refer to those collections. |
| 122 | */ |
| 123 | PyObject *u_consts; /* all constants */ |
| 124 | PyObject *u_names; /* all names */ |
| 125 | PyObject *u_varnames; /* local variables */ |
| 126 | PyObject *u_cellvars; /* cell variables */ |
| 127 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 128 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 130 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 131 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 132 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 133 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 134 | /* Pointer to the most recently allocated block. By following b_list |
| 135 | members, you can reach all early allocated blocks. */ |
| 136 | basicblock *u_blocks; |
| 137 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 138 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 139 | int u_nfblocks; |
| 140 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 141 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | int u_firstlineno; /* the first lineno of the block */ |
| 143 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 144 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 148 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 149 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 150 | 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] | 151 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 152 | |
| 153 | Note that we don't track recursion levels during compilation - the |
| 154 | task of detecting and rejecting excessive levels of nesting is |
| 155 | handled by the symbol analysis pass. |
| 156 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 157 | */ |
| 158 | |
| 159 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 160 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 161 | struct symtable *c_st; |
| 162 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 163 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 164 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 165 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 166 | int c_interactive; /* true if in interactive mode */ |
| 167 | int c_nestlevel; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 168 | int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode |
| 169 | if this value is different from zero. |
| 170 | This can be used to temporarily visit |
| 171 | nodes without emitting bytecode to |
| 172 | check only errors. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 173 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 174 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 175 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 176 | struct compiler_unit *u; /* compiler state for current block */ |
| 177 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 178 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 179 | }; |
| 180 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 181 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 182 | static void compiler_free(struct compiler *); |
| 183 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 184 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 185 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 186 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 188 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 189 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 190 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 191 | |
| 192 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 193 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 194 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 195 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 196 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 197 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 198 | static int compiler_subscript(struct compiler *, expr_ty); |
| 199 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 200 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 201 | static int inplace_binop(operator_ty); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 202 | static int are_all_items_const(asdl_seq *, Py_ssize_t, Py_ssize_t); |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 203 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 204 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 205 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 206 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 207 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 208 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 209 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 210 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 211 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 212 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 213 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 214 | static int compiler_sync_comprehension_generator( |
| 215 | struct compiler *c, |
| 216 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 217 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 218 | expr_ty elt, expr_ty val, int type); |
| 219 | |
| 220 | static int compiler_async_comprehension_generator( |
| 221 | struct compiler *c, |
| 222 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 223 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 224 | expr_ty elt, expr_ty val, int type); |
| 225 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 226 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 227 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 228 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 229 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 230 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 231 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 232 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 233 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | /* Name mangling: __private becomes _classname__private. |
| 235 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 236 | PyObject *result; |
| 237 | size_t nlen, plen, ipriv; |
| 238 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 240 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 241 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 242 | Py_INCREF(ident); |
| 243 | return ident; |
| 244 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 245 | nlen = PyUnicode_GET_LENGTH(ident); |
| 246 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 247 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 248 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 249 | The only time a name with a dot can occur is when |
| 250 | we are compiling an import statement that has a |
| 251 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 252 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | TODO(jhylton): Decide whether we want to support |
| 254 | mangling of the module name, e.g. __M.X. |
| 255 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 256 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 257 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 258 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 259 | Py_INCREF(ident); |
| 260 | return ident; /* Don't mangle __whatever__ */ |
| 261 | } |
| 262 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 263 | ipriv = 0; |
| 264 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 265 | ipriv++; |
| 266 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 267 | Py_INCREF(ident); |
| 268 | return ident; /* Don't mangle if class is just underscores */ |
| 269 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 270 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 271 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 272 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 273 | PyErr_SetString(PyExc_OverflowError, |
| 274 | "private identifier too large to be mangled"); |
| 275 | return NULL; |
| 276 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 277 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 278 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 279 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 280 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 281 | |
| 282 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 283 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 284 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 285 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 286 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 287 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 288 | Py_DECREF(result); |
| 289 | return NULL; |
| 290 | } |
| 291 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 292 | Py_DECREF(result); |
| 293 | return NULL; |
| 294 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 295 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 296 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 297 | } |
| 298 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 299 | static int |
| 300 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 301 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 302 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 303 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 304 | c->c_const_cache = PyDict_New(); |
| 305 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 306 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | c->c_stack = PyList_New(0); |
| 310 | if (!c->c_stack) { |
| 311 | Py_CLEAR(c->c_const_cache); |
| 312 | return 0; |
| 313 | } |
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 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 319 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 320 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 321 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 322 | struct compiler c; |
| 323 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 324 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 325 | int merged; |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 326 | PyConfig *config = &_PyInterpreterState_GET_UNSAFE()->config; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 327 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 328 | if (!__doc__) { |
| 329 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 330 | if (!__doc__) |
| 331 | return NULL; |
| 332 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 333 | if (!__annotations__) { |
| 334 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 335 | if (!__annotations__) |
| 336 | return NULL; |
| 337 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 338 | if (!compiler_init(&c)) |
| 339 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 340 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 341 | c.c_filename = filename; |
| 342 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 343 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 344 | if (c.c_future == NULL) |
| 345 | goto finally; |
| 346 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 347 | flags = &local_flags; |
| 348 | } |
| 349 | merged = c.c_future->ff_features | flags->cf_flags; |
| 350 | c.c_future->ff_features = merged; |
| 351 | flags->cf_flags = merged; |
| 352 | c.c_flags = flags; |
Victor Stinner | c96be81 | 2019-05-14 17:34:56 +0200 | [diff] [blame] | 353 | c.c_optimize = (optimize == -1) ? config->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 354 | c.c_nestlevel = 0; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 355 | c.c_do_not_emit_bytecode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 356 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 357 | _PyASTOptimizeState state; |
| 358 | state.optimize = c.c_optimize; |
| 359 | state.ff_features = merged; |
| 360 | |
| 361 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 362 | goto finally; |
| 363 | } |
| 364 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 365 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 366 | if (c.c_st == NULL) { |
| 367 | if (!PyErr_Occurred()) |
| 368 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 369 | goto finally; |
| 370 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 371 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 372 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 373 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 374 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | compiler_free(&c); |
| 376 | assert(co || PyErr_Occurred()); |
| 377 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 378 | } |
| 379 | |
| 380 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 381 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 382 | int optimize, PyArena *arena) |
| 383 | { |
| 384 | PyObject *filename; |
| 385 | PyCodeObject *co; |
| 386 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 387 | if (filename == NULL) |
| 388 | return NULL; |
| 389 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 390 | Py_DECREF(filename); |
| 391 | return co; |
| 392 | |
| 393 | } |
| 394 | |
| 395 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 396 | PyNode_Compile(struct _node *n, const char *filename) |
| 397 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 398 | PyCodeObject *co = NULL; |
| 399 | mod_ty mod; |
| 400 | PyArena *arena = PyArena_New(); |
| 401 | if (!arena) |
| 402 | return NULL; |
| 403 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 404 | if (mod) |
| 405 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 406 | PyArena_Free(arena); |
| 407 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 408 | } |
| 409 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 410 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 411 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 412 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | if (c->c_st) |
| 414 | PySymtable_Free(c->c_st); |
| 415 | if (c->c_future) |
| 416 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 417 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 418 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 419 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 422 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 423 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 424 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 425 | Py_ssize_t i, n; |
| 426 | PyObject *v, *k; |
| 427 | PyObject *dict = PyDict_New(); |
| 428 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 429 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 430 | n = PyList_Size(list); |
| 431 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 432 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 433 | if (!v) { |
| 434 | Py_DECREF(dict); |
| 435 | return NULL; |
| 436 | } |
| 437 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 438 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 439 | Py_DECREF(v); |
| 440 | Py_DECREF(dict); |
| 441 | return NULL; |
| 442 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 443 | Py_DECREF(v); |
| 444 | } |
| 445 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Return new dict containing names from src that match scope(s). |
| 449 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 450 | 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] | 451 | 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] | 452 | values are integers, starting at offset and increasing by one for |
| 453 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 454 | */ |
| 455 | |
| 456 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 457 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 458 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 459 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 460 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 461 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 463 | assert(offset >= 0); |
| 464 | if (dest == NULL) |
| 465 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 466 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 467 | /* Sort the keys so that we have a deterministic order on the indexes |
| 468 | saved in the returned dictionary. These indexes are used as indexes |
| 469 | into the free and cell var storage. Therefore if they aren't |
| 470 | deterministic, then the generated bytecode is not deterministic. |
| 471 | */ |
| 472 | sorted_keys = PyDict_Keys(src); |
| 473 | if (sorted_keys == NULL) |
| 474 | return NULL; |
| 475 | if (PyList_Sort(sorted_keys) != 0) { |
| 476 | Py_DECREF(sorted_keys); |
| 477 | return NULL; |
| 478 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 479 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 480 | |
| 481 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 482 | /* XXX this should probably be a macro in symtable.h */ |
| 483 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 484 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 485 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 486 | assert(PyLong_Check(v)); |
| 487 | vi = PyLong_AS_LONG(v); |
| 488 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 489 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 491 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 493 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | Py_DECREF(dest); |
| 495 | return NULL; |
| 496 | } |
| 497 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 498 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 499 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | Py_DECREF(item); |
| 501 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 502 | return NULL; |
| 503 | } |
| 504 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | } |
| 506 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 507 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 508 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 509 | } |
| 510 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 511 | static void |
| 512 | compiler_unit_check(struct compiler_unit *u) |
| 513 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | basicblock *block; |
| 515 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 516 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 517 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 518 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | if (block->b_instr != NULL) { |
| 520 | assert(block->b_ialloc > 0); |
| 521 | assert(block->b_iused > 0); |
| 522 | assert(block->b_ialloc >= block->b_iused); |
| 523 | } |
| 524 | else { |
| 525 | assert (block->b_iused == 0); |
| 526 | assert (block->b_ialloc == 0); |
| 527 | } |
| 528 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 529 | } |
| 530 | |
| 531 | static void |
| 532 | compiler_unit_free(struct compiler_unit *u) |
| 533 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 535 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 536 | compiler_unit_check(u); |
| 537 | b = u->u_blocks; |
| 538 | while (b != NULL) { |
| 539 | if (b->b_instr) |
| 540 | PyObject_Free((void *)b->b_instr); |
| 541 | next = b->b_list; |
| 542 | PyObject_Free((void *)b); |
| 543 | b = next; |
| 544 | } |
| 545 | Py_CLEAR(u->u_ste); |
| 546 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 547 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 548 | Py_CLEAR(u->u_consts); |
| 549 | Py_CLEAR(u->u_names); |
| 550 | Py_CLEAR(u->u_varnames); |
| 551 | Py_CLEAR(u->u_freevars); |
| 552 | Py_CLEAR(u->u_cellvars); |
| 553 | Py_CLEAR(u->u_private); |
| 554 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 555 | } |
| 556 | |
| 557 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 558 | compiler_enter_scope(struct compiler *c, identifier name, |
| 559 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 560 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 561 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 562 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 563 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 564 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 565 | struct compiler_unit)); |
| 566 | if (!u) { |
| 567 | PyErr_NoMemory(); |
| 568 | return 0; |
| 569 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 570 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 572 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 573 | u->u_kwonlyargcount = 0; |
| 574 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 575 | if (!u->u_ste) { |
| 576 | compiler_unit_free(u); |
| 577 | return 0; |
| 578 | } |
| 579 | Py_INCREF(name); |
| 580 | u->u_name = name; |
| 581 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 582 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 583 | if (!u->u_varnames || !u->u_cellvars) { |
| 584 | compiler_unit_free(u); |
| 585 | return 0; |
| 586 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 587 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 588 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 589 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 590 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 591 | int res; |
| 592 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 593 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 594 | name = _PyUnicode_FromId(&PyId___class__); |
| 595 | if (!name) { |
| 596 | compiler_unit_free(u); |
| 597 | return 0; |
| 598 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 599 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 600 | if (res < 0) { |
| 601 | compiler_unit_free(u); |
| 602 | return 0; |
| 603 | } |
| 604 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 605 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | 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] | 607 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 608 | if (!u->u_freevars) { |
| 609 | compiler_unit_free(u); |
| 610 | return 0; |
| 611 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 612 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 613 | u->u_blocks = NULL; |
| 614 | u->u_nfblocks = 0; |
| 615 | u->u_firstlineno = lineno; |
| 616 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 617 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | u->u_consts = PyDict_New(); |
| 619 | if (!u->u_consts) { |
| 620 | compiler_unit_free(u); |
| 621 | return 0; |
| 622 | } |
| 623 | u->u_names = PyDict_New(); |
| 624 | if (!u->u_names) { |
| 625 | compiler_unit_free(u); |
| 626 | return 0; |
| 627 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 628 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 629 | u->u_private = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 630 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 631 | /* Push the old compiler_unit on the stack. */ |
| 632 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 633 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 634 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 635 | Py_XDECREF(capsule); |
| 636 | compiler_unit_free(u); |
| 637 | return 0; |
| 638 | } |
| 639 | Py_DECREF(capsule); |
| 640 | u->u_private = c->u->u_private; |
| 641 | Py_XINCREF(u->u_private); |
| 642 | } |
| 643 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 644 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 645 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 646 | |
| 647 | block = compiler_new_block(c); |
| 648 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 649 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 650 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 651 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 652 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 653 | if (!compiler_set_qualname(c)) |
| 654 | return 0; |
| 655 | } |
| 656 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 657 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 658 | } |
| 659 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 660 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 661 | compiler_exit_scope(struct compiler *c) |
| 662 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 663 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 665 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 666 | c->c_nestlevel--; |
| 667 | compiler_unit_free(c->u); |
| 668 | /* Restore c->u to the parent unit. */ |
| 669 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 670 | if (n >= 0) { |
| 671 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 672 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | assert(c->u); |
| 674 | /* we are deleting from a list so this really shouldn't fail */ |
| 675 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 676 | Py_FatalError("compiler_exit_scope()"); |
| 677 | compiler_unit_check(c->u); |
| 678 | } |
| 679 | else |
| 680 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 681 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 682 | } |
| 683 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 684 | static int |
| 685 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 686 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 687 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 688 | _Py_static_string(dot_locals, ".<locals>"); |
| 689 | Py_ssize_t stack_size; |
| 690 | struct compiler_unit *u = c->u; |
| 691 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 692 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 693 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 694 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 695 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 696 | if (stack_size > 1) { |
| 697 | int scope, force_global = 0; |
| 698 | struct compiler_unit *parent; |
| 699 | PyObject *mangled, *capsule; |
| 700 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 701 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 702 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 703 | assert(parent); |
| 704 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 705 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 706 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 707 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 708 | assert(u->u_name); |
| 709 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 710 | if (!mangled) |
| 711 | return 0; |
| 712 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 713 | Py_DECREF(mangled); |
| 714 | assert(scope != GLOBAL_IMPLICIT); |
| 715 | if (scope == GLOBAL_EXPLICIT) |
| 716 | force_global = 1; |
| 717 | } |
| 718 | |
| 719 | if (!force_global) { |
| 720 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 721 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 722 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 723 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 724 | if (dot_locals_str == NULL) |
| 725 | return 0; |
| 726 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 727 | if (base == NULL) |
| 728 | return 0; |
| 729 | } |
| 730 | else { |
| 731 | Py_INCREF(parent->u_qualname); |
| 732 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 733 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 734 | } |
| 735 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 736 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 737 | if (base != NULL) { |
| 738 | dot_str = _PyUnicode_FromId(&dot); |
| 739 | if (dot_str == NULL) { |
| 740 | Py_DECREF(base); |
| 741 | return 0; |
| 742 | } |
| 743 | name = PyUnicode_Concat(base, dot_str); |
| 744 | Py_DECREF(base); |
| 745 | if (name == NULL) |
| 746 | return 0; |
| 747 | PyUnicode_Append(&name, u->u_name); |
| 748 | if (name == NULL) |
| 749 | return 0; |
| 750 | } |
| 751 | else { |
| 752 | Py_INCREF(u->u_name); |
| 753 | name = u->u_name; |
| 754 | } |
| 755 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 756 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 757 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 758 | } |
| 759 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 760 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 761 | /* Allocate a new block and return a pointer to it. |
| 762 | Returns NULL on error. |
| 763 | */ |
| 764 | |
| 765 | static basicblock * |
| 766 | compiler_new_block(struct compiler *c) |
| 767 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 768 | basicblock *b; |
| 769 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 770 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 772 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 773 | if (b == NULL) { |
| 774 | PyErr_NoMemory(); |
| 775 | return NULL; |
| 776 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 777 | /* Extend the singly linked list of blocks with new block. */ |
| 778 | b->b_list = u->u_blocks; |
| 779 | u->u_blocks = b; |
| 780 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 781 | } |
| 782 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 783 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 784 | compiler_next_block(struct compiler *c) |
| 785 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 786 | basicblock *block = compiler_new_block(c); |
| 787 | if (block == NULL) |
| 788 | return NULL; |
| 789 | c->u->u_curblock->b_next = block; |
| 790 | c->u->u_curblock = block; |
| 791 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 792 | } |
| 793 | |
| 794 | static basicblock * |
| 795 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 796 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 797 | assert(block != NULL); |
| 798 | c->u->u_curblock->b_next = block; |
| 799 | c->u->u_curblock = block; |
| 800 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 801 | } |
| 802 | |
| 803 | /* Returns the offset of the next instruction in the current block's |
| 804 | b_instr array. Resizes the b_instr as necessary. |
| 805 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 806 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 807 | |
| 808 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 809 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 811 | assert(b != NULL); |
| 812 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 813 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 814 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 815 | if (b->b_instr == NULL) { |
| 816 | PyErr_NoMemory(); |
| 817 | return -1; |
| 818 | } |
| 819 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 820 | } |
| 821 | else if (b->b_iused == b->b_ialloc) { |
| 822 | struct instr *tmp; |
| 823 | size_t oldsize, newsize; |
| 824 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 825 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 826 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 827 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 828 | PyErr_NoMemory(); |
| 829 | return -1; |
| 830 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 831 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 832 | if (newsize == 0) { |
| 833 | PyErr_NoMemory(); |
| 834 | return -1; |
| 835 | } |
| 836 | b->b_ialloc <<= 1; |
| 837 | tmp = (struct instr *)PyObject_Realloc( |
| 838 | (void *)b->b_instr, newsize); |
| 839 | if (tmp == NULL) { |
| 840 | PyErr_NoMemory(); |
| 841 | return -1; |
| 842 | } |
| 843 | b->b_instr = tmp; |
| 844 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 845 | } |
| 846 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 847 | } |
| 848 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 849 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 850 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 851 | The line number is reset in the following cases: |
| 852 | - when entering a new scope |
| 853 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 854 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 855 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 856 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 857 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 858 | #define SET_LOC(c, x) \ |
| 859 | (c)->u->u_lineno = (x)->lineno; \ |
| 860 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 861 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 862 | /* Return the stack effect of opcode with argument oparg. |
| 863 | |
| 864 | Some opcodes have different stack effect when jump to the target and |
| 865 | when not jump. The 'jump' parameter specifies the case: |
| 866 | |
| 867 | * 0 -- when not jump |
| 868 | * 1 -- when jump |
| 869 | * -1 -- maximal |
| 870 | */ |
| 871 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 872 | WITH_CLEANUP_FINISH deterministic. */ |
| 873 | static int |
| 874 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 875 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 876 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 877 | case NOP: |
| 878 | case EXTENDED_ARG: |
| 879 | return 0; |
| 880 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 881 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 882 | case POP_TOP: |
| 883 | return -1; |
| 884 | case ROT_TWO: |
| 885 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 886 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | return 0; |
| 888 | case DUP_TOP: |
| 889 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 890 | case DUP_TOP_TWO: |
| 891 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 892 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 893 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 894 | case UNARY_POSITIVE: |
| 895 | case UNARY_NEGATIVE: |
| 896 | case UNARY_NOT: |
| 897 | case UNARY_INVERT: |
| 898 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 899 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 900 | case SET_ADD: |
| 901 | case LIST_APPEND: |
| 902 | return -1; |
| 903 | case MAP_ADD: |
| 904 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 905 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 906 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | case BINARY_POWER: |
| 908 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 909 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 910 | case BINARY_MODULO: |
| 911 | case BINARY_ADD: |
| 912 | case BINARY_SUBTRACT: |
| 913 | case BINARY_SUBSCR: |
| 914 | case BINARY_FLOOR_DIVIDE: |
| 915 | case BINARY_TRUE_DIVIDE: |
| 916 | return -1; |
| 917 | case INPLACE_FLOOR_DIVIDE: |
| 918 | case INPLACE_TRUE_DIVIDE: |
| 919 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 921 | case INPLACE_ADD: |
| 922 | case INPLACE_SUBTRACT: |
| 923 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 924 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 925 | case INPLACE_MODULO: |
| 926 | return -1; |
| 927 | case STORE_SUBSCR: |
| 928 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 929 | case DELETE_SUBSCR: |
| 930 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 932 | case BINARY_LSHIFT: |
| 933 | case BINARY_RSHIFT: |
| 934 | case BINARY_AND: |
| 935 | case BINARY_XOR: |
| 936 | case BINARY_OR: |
| 937 | return -1; |
| 938 | case INPLACE_POWER: |
| 939 | return -1; |
| 940 | case GET_ITER: |
| 941 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 943 | case PRINT_EXPR: |
| 944 | return -1; |
| 945 | case LOAD_BUILD_CLASS: |
| 946 | return 1; |
| 947 | case INPLACE_LSHIFT: |
| 948 | case INPLACE_RSHIFT: |
| 949 | case INPLACE_AND: |
| 950 | case INPLACE_XOR: |
| 951 | case INPLACE_OR: |
| 952 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 953 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 954 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 955 | /* 1 in the normal flow. |
| 956 | * Restore the stack position and push 6 values before jumping to |
| 957 | * the handler if an exception be raised. */ |
| 958 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 959 | case RETURN_VALUE: |
| 960 | return -1; |
| 961 | case IMPORT_STAR: |
| 962 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 963 | case SETUP_ANNOTATIONS: |
| 964 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 965 | case YIELD_VALUE: |
| 966 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 967 | case YIELD_FROM: |
| 968 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 969 | case POP_BLOCK: |
| 970 | return 0; |
| 971 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 972 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 973 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | case STORE_NAME: |
| 975 | return -1; |
| 976 | case DELETE_NAME: |
| 977 | return 0; |
| 978 | case UNPACK_SEQUENCE: |
| 979 | return oparg-1; |
| 980 | case UNPACK_EX: |
| 981 | return (oparg&0xFF) + (oparg>>8); |
| 982 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 983 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 984 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 985 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 986 | case STORE_ATTR: |
| 987 | return -2; |
| 988 | case DELETE_ATTR: |
| 989 | return -1; |
| 990 | case STORE_GLOBAL: |
| 991 | return -1; |
| 992 | case DELETE_GLOBAL: |
| 993 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | case LOAD_CONST: |
| 995 | return 1; |
| 996 | case LOAD_NAME: |
| 997 | return 1; |
| 998 | case BUILD_TUPLE: |
| 999 | case BUILD_LIST: |
| 1000 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 1001 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1002 | return 1-oparg; |
| 1003 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1004 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1005 | case BUILD_CONST_KEY_MAP: |
| 1006 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1007 | case LOAD_ATTR: |
| 1008 | return 0; |
| 1009 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1010 | case IS_OP: |
| 1011 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1012 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1013 | case JUMP_IF_NOT_EXC_MATCH: |
| 1014 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1015 | case IMPORT_NAME: |
| 1016 | return -1; |
| 1017 | case IMPORT_FROM: |
| 1018 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1019 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1020 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1021 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1022 | case JUMP_ABSOLUTE: |
| 1023 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1024 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1025 | case JUMP_IF_TRUE_OR_POP: |
| 1026 | case JUMP_IF_FALSE_OR_POP: |
| 1027 | return jump ? 0 : -1; |
| 1028 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1029 | case POP_JUMP_IF_FALSE: |
| 1030 | case POP_JUMP_IF_TRUE: |
| 1031 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1032 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | case LOAD_GLOBAL: |
| 1034 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1035 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1036 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1037 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1038 | /* 0 in the normal flow. |
| 1039 | * Restore the stack position and push 6 values before jumping to |
| 1040 | * the handler if an exception be raised. */ |
| 1041 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1042 | case RERAISE: |
| 1043 | return -3; |
| 1044 | |
| 1045 | case WITH_EXCEPT_START: |
| 1046 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1048 | case LOAD_FAST: |
| 1049 | return 1; |
| 1050 | case STORE_FAST: |
| 1051 | return -1; |
| 1052 | case DELETE_FAST: |
| 1053 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1054 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1055 | case RAISE_VARARGS: |
| 1056 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1057 | |
| 1058 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1059 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1060 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1061 | case CALL_METHOD: |
| 1062 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1063 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1064 | return -oparg-1; |
| 1065 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1066 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1067 | case MAKE_FUNCTION: |
| 1068 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1069 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1070 | case BUILD_SLICE: |
| 1071 | if (oparg == 3) |
| 1072 | return -2; |
| 1073 | else |
| 1074 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1075 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1076 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1077 | case LOAD_CLOSURE: |
| 1078 | return 1; |
| 1079 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1080 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1081 | return 1; |
| 1082 | case STORE_DEREF: |
| 1083 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1084 | case DELETE_DEREF: |
| 1085 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1086 | |
| 1087 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1088 | case GET_AWAITABLE: |
| 1089 | return 0; |
| 1090 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1091 | /* 0 in the normal flow. |
| 1092 | * Restore the stack position to the position before the result |
| 1093 | * of __aenter__ and push 6 values before jumping to the handler |
| 1094 | * if an exception be raised. */ |
| 1095 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1096 | case BEFORE_ASYNC_WITH: |
| 1097 | return 1; |
| 1098 | case GET_AITER: |
| 1099 | return 0; |
| 1100 | case GET_ANEXT: |
| 1101 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1102 | case GET_YIELD_FROM_ITER: |
| 1103 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1104 | case END_ASYNC_FOR: |
| 1105 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1106 | case FORMAT_VALUE: |
| 1107 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1108 | else 1->1. */ |
| 1109 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1110 | case LOAD_METHOD: |
| 1111 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1112 | case LOAD_ASSERTION_ERROR: |
| 1113 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1114 | case LIST_TO_TUPLE: |
| 1115 | return 0; |
| 1116 | case LIST_EXTEND: |
| 1117 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1118 | case DICT_MERGE: |
| 1119 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1120 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1122 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1123 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1124 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1125 | } |
| 1126 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1127 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1128 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1129 | { |
| 1130 | return stack_effect(opcode, oparg, jump); |
| 1131 | } |
| 1132 | |
| 1133 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1134 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1135 | { |
| 1136 | return stack_effect(opcode, oparg, -1); |
| 1137 | } |
| 1138 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1139 | /* Add an opcode with no argument. |
| 1140 | Returns 0 on failure, 1 on success. |
| 1141 | */ |
| 1142 | |
| 1143 | static int |
| 1144 | compiler_addop(struct compiler *c, int opcode) |
| 1145 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | basicblock *b; |
| 1147 | struct instr *i; |
| 1148 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1149 | assert(!HAS_ARG(opcode)); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1150 | if (c->c_do_not_emit_bytecode) { |
| 1151 | return 1; |
| 1152 | } |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1153 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1154 | if (off < 0) |
| 1155 | return 0; |
| 1156 | b = c->u->u_curblock; |
| 1157 | i = &b->b_instr[off]; |
| 1158 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1159 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1160 | if (opcode == RETURN_VALUE) |
| 1161 | b->b_return = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1162 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1163 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1164 | } |
| 1165 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1166 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1167 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1168 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1169 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1170 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1171 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1172 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1174 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1175 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1176 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1177 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1178 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1179 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1180 | return -1; |
| 1181 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1182 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1183 | Py_DECREF(v); |
| 1184 | return -1; |
| 1185 | } |
| 1186 | Py_DECREF(v); |
| 1187 | } |
| 1188 | else |
| 1189 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1190 | return arg; |
| 1191 | } |
| 1192 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1193 | // Merge const *o* recursively and return constant key object. |
| 1194 | static PyObject* |
| 1195 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1196 | { |
| 1197 | // None and Ellipsis are singleton, and key is the singleton. |
| 1198 | // No need to merge object and key. |
| 1199 | if (o == Py_None || o == Py_Ellipsis) { |
| 1200 | Py_INCREF(o); |
| 1201 | return o; |
| 1202 | } |
| 1203 | |
| 1204 | PyObject *key = _PyCode_ConstantKey(o); |
| 1205 | if (key == NULL) { |
| 1206 | return NULL; |
| 1207 | } |
| 1208 | |
| 1209 | // t is borrowed reference |
| 1210 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1211 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1212 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1213 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1214 | Py_DECREF(key); |
| 1215 | return t; |
| 1216 | } |
| 1217 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1218 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1219 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1220 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1221 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1222 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1223 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1224 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1225 | PyObject *u = merge_consts_recursive(c, item); |
| 1226 | if (u == NULL) { |
| 1227 | Py_DECREF(key); |
| 1228 | return NULL; |
| 1229 | } |
| 1230 | |
| 1231 | // See _PyCode_ConstantKey() |
| 1232 | PyObject *v; // borrowed |
| 1233 | if (PyTuple_CheckExact(u)) { |
| 1234 | v = PyTuple_GET_ITEM(u, 1); |
| 1235 | } |
| 1236 | else { |
| 1237 | v = u; |
| 1238 | } |
| 1239 | if (v != item) { |
| 1240 | Py_INCREF(v); |
| 1241 | PyTuple_SET_ITEM(o, i, v); |
| 1242 | Py_DECREF(item); |
| 1243 | } |
| 1244 | |
| 1245 | Py_DECREF(u); |
| 1246 | } |
| 1247 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1248 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1249 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1250 | // constant keys. |
| 1251 | // See _PyCode_ConstantKey() for detail. |
| 1252 | assert(PyTuple_CheckExact(key)); |
| 1253 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1254 | |
| 1255 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1256 | if (len == 0) { // empty frozenset should not be re-created. |
| 1257 | return key; |
| 1258 | } |
| 1259 | PyObject *tuple = PyTuple_New(len); |
| 1260 | if (tuple == NULL) { |
| 1261 | Py_DECREF(key); |
| 1262 | return NULL; |
| 1263 | } |
| 1264 | Py_ssize_t i = 0, pos = 0; |
| 1265 | PyObject *item; |
| 1266 | Py_hash_t hash; |
| 1267 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1268 | PyObject *k = merge_consts_recursive(c, item); |
| 1269 | if (k == NULL) { |
| 1270 | Py_DECREF(tuple); |
| 1271 | Py_DECREF(key); |
| 1272 | return NULL; |
| 1273 | } |
| 1274 | PyObject *u; |
| 1275 | if (PyTuple_CheckExact(k)) { |
| 1276 | u = PyTuple_GET_ITEM(k, 1); |
| 1277 | Py_INCREF(u); |
| 1278 | Py_DECREF(k); |
| 1279 | } |
| 1280 | else { |
| 1281 | u = k; |
| 1282 | } |
| 1283 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1284 | i++; |
| 1285 | } |
| 1286 | |
| 1287 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1288 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1289 | PyObject *new = PyFrozenSet_New(tuple); |
| 1290 | Py_DECREF(tuple); |
| 1291 | if (new == NULL) { |
| 1292 | Py_DECREF(key); |
| 1293 | return NULL; |
| 1294 | } |
| 1295 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1296 | Py_DECREF(o); |
| 1297 | PyTuple_SET_ITEM(key, 1, new); |
| 1298 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1299 | |
| 1300 | return key; |
| 1301 | } |
| 1302 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1303 | static Py_ssize_t |
| 1304 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1305 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1306 | if (c->c_do_not_emit_bytecode) { |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1310 | PyObject *key = merge_consts_recursive(c, o); |
| 1311 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1312 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1313 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1314 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1315 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1316 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1317 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1318 | } |
| 1319 | |
| 1320 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1321 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1322 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1323 | if (c->c_do_not_emit_bytecode) { |
| 1324 | return 1; |
| 1325 | } |
| 1326 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1327 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1328 | if (arg < 0) |
| 1329 | return 0; |
| 1330 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1331 | } |
| 1332 | |
| 1333 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1334 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1335 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1336 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1337 | if (c->c_do_not_emit_bytecode) { |
| 1338 | return 1; |
| 1339 | } |
| 1340 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1341 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1342 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1343 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1344 | return compiler_addop_i(c, opcode, arg); |
| 1345 | } |
| 1346 | |
| 1347 | static int |
| 1348 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1349 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1350 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1351 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1352 | |
| 1353 | if (c->c_do_not_emit_bytecode) { |
| 1354 | return 1; |
| 1355 | } |
| 1356 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1357 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1358 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1359 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1360 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1361 | Py_DECREF(mangled); |
| 1362 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1363 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1364 | return compiler_addop_i(c, opcode, arg); |
| 1365 | } |
| 1366 | |
| 1367 | /* Add an opcode with an integer argument. |
| 1368 | Returns 0 on failure, 1 on success. |
| 1369 | */ |
| 1370 | |
| 1371 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1372 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1373 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1374 | struct instr *i; |
| 1375 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1376 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1377 | if (c->c_do_not_emit_bytecode) { |
| 1378 | return 1; |
| 1379 | } |
| 1380 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1381 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1382 | it in the C code (like Python/ceval.c). |
| 1383 | |
| 1384 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1385 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1386 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1387 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1388 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1389 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1390 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1391 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1392 | if (off < 0) |
| 1393 | return 0; |
| 1394 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1395 | i->i_opcode = opcode; |
| 1396 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1397 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1398 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | static int |
| 1402 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1403 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1404 | struct instr *i; |
| 1405 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1406 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1407 | if (c->c_do_not_emit_bytecode) { |
| 1408 | return 1; |
| 1409 | } |
| 1410 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1411 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | assert(b != NULL); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1413 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1414 | if (off < 0) |
| 1415 | return 0; |
| 1416 | i = &c->u->u_curblock->b_instr[off]; |
| 1417 | i->i_opcode = opcode; |
| 1418 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1419 | if (absolute) |
| 1420 | i->i_jabs = 1; |
| 1421 | else |
| 1422 | i->i_jrel = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1423 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1424 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1425 | } |
| 1426 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1427 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1428 | to the new block. |
| 1429 | |
| 1430 | The returns inside this macro make it impossible to decref objects |
| 1431 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1432 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1433 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1434 | if (compiler_next_block((C)) == NULL) \ |
| 1435 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1436 | } |
| 1437 | |
| 1438 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1439 | if (!compiler_addop((C), (OP))) \ |
| 1440 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1443 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1444 | if (!compiler_addop((C), (OP))) { \ |
| 1445 | compiler_exit_scope(c); \ |
| 1446 | return 0; \ |
| 1447 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1448 | } |
| 1449 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1450 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1451 | if (!compiler_addop_load_const((C), (O))) \ |
| 1452 | return 0; \ |
| 1453 | } |
| 1454 | |
| 1455 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1456 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1457 | PyObject *__new_const = (O); \ |
| 1458 | if (__new_const == NULL) { \ |
| 1459 | return 0; \ |
| 1460 | } \ |
| 1461 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1462 | Py_DECREF(__new_const); \ |
| 1463 | return 0; \ |
| 1464 | } \ |
| 1465 | Py_DECREF(__new_const); \ |
| 1466 | } |
| 1467 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1468 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1469 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1470 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1473 | /* Same as ADDOP_O, but steals a reference. */ |
| 1474 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1475 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1476 | Py_DECREF((O)); \ |
| 1477 | return 0; \ |
| 1478 | } \ |
| 1479 | Py_DECREF((O)); \ |
| 1480 | } |
| 1481 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1482 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1483 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1484 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1485 | } |
| 1486 | |
| 1487 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1488 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1489 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1490 | } |
| 1491 | |
| 1492 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1493 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1494 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
| 1497 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1498 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1499 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1500 | } |
| 1501 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1502 | |
| 1503 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1504 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1505 | return 0; \ |
| 1506 | } |
| 1507 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1508 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1509 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1510 | */ |
| 1511 | |
| 1512 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1513 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1514 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1515 | } |
| 1516 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1517 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1518 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1519 | compiler_exit_scope(c); \ |
| 1520 | return 0; \ |
| 1521 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1522 | } |
| 1523 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1524 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1525 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1526 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1530 | int _i; \ |
| 1531 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1532 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1533 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1534 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1535 | return 0; \ |
| 1536 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1537 | } |
| 1538 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1539 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1540 | int _i; \ |
| 1541 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1542 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1543 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1544 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1545 | compiler_exit_scope(c); \ |
| 1546 | return 0; \ |
| 1547 | } \ |
| 1548 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1549 | } |
| 1550 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1551 | /* These macros allows to check only for errors and not emmit bytecode |
| 1552 | * while visiting nodes. |
| 1553 | */ |
| 1554 | |
| 1555 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1556 | c->c_do_not_emit_bytecode++; |
| 1557 | |
| 1558 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1559 | c->c_do_not_emit_bytecode--; \ |
| 1560 | } |
| 1561 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1562 | /* Search if variable annotations are present statically in a block. */ |
| 1563 | |
| 1564 | static int |
| 1565 | find_ann(asdl_seq *stmts) |
| 1566 | { |
| 1567 | int i, j, res = 0; |
| 1568 | stmt_ty st; |
| 1569 | |
| 1570 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1571 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1572 | switch (st->kind) { |
| 1573 | case AnnAssign_kind: |
| 1574 | return 1; |
| 1575 | case For_kind: |
| 1576 | res = find_ann(st->v.For.body) || |
| 1577 | find_ann(st->v.For.orelse); |
| 1578 | break; |
| 1579 | case AsyncFor_kind: |
| 1580 | res = find_ann(st->v.AsyncFor.body) || |
| 1581 | find_ann(st->v.AsyncFor.orelse); |
| 1582 | break; |
| 1583 | case While_kind: |
| 1584 | res = find_ann(st->v.While.body) || |
| 1585 | find_ann(st->v.While.orelse); |
| 1586 | break; |
| 1587 | case If_kind: |
| 1588 | res = find_ann(st->v.If.body) || |
| 1589 | find_ann(st->v.If.orelse); |
| 1590 | break; |
| 1591 | case With_kind: |
| 1592 | res = find_ann(st->v.With.body); |
| 1593 | break; |
| 1594 | case AsyncWith_kind: |
| 1595 | res = find_ann(st->v.AsyncWith.body); |
| 1596 | break; |
| 1597 | case Try_kind: |
| 1598 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1599 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1600 | st->v.Try.handlers, j); |
| 1601 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1602 | return 1; |
| 1603 | } |
| 1604 | } |
| 1605 | res = find_ann(st->v.Try.body) || |
| 1606 | find_ann(st->v.Try.finalbody) || |
| 1607 | find_ann(st->v.Try.orelse); |
| 1608 | break; |
| 1609 | default: |
| 1610 | res = 0; |
| 1611 | } |
| 1612 | if (res) { |
| 1613 | break; |
| 1614 | } |
| 1615 | } |
| 1616 | return res; |
| 1617 | } |
| 1618 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1619 | /* |
| 1620 | * Frame block handling functions |
| 1621 | */ |
| 1622 | |
| 1623 | static int |
| 1624 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1625 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1626 | { |
| 1627 | struct fblockinfo *f; |
| 1628 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1629 | PyErr_SetString(PyExc_SyntaxError, |
| 1630 | "too many statically nested blocks"); |
| 1631 | return 0; |
| 1632 | } |
| 1633 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1634 | f->fb_type = t; |
| 1635 | f->fb_block = b; |
| 1636 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1637 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1638 | return 1; |
| 1639 | } |
| 1640 | |
| 1641 | static void |
| 1642 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1643 | { |
| 1644 | struct compiler_unit *u = c->u; |
| 1645 | assert(u->u_nfblocks > 0); |
| 1646 | u->u_nfblocks--; |
| 1647 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1648 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1649 | } |
| 1650 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1651 | static int |
| 1652 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1653 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1654 | ADDOP(c, DUP_TOP); |
| 1655 | ADDOP(c, DUP_TOP); |
| 1656 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1657 | return 1; |
| 1658 | } |
| 1659 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1660 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1661 | * popping the blocks will be restored afterwards, unless another |
| 1662 | * return, break or continue is found. In which case, the TOS will |
| 1663 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1664 | */ |
| 1665 | static int |
| 1666 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1667 | int preserve_tos) |
| 1668 | { |
| 1669 | switch (info->fb_type) { |
| 1670 | case WHILE_LOOP: |
| 1671 | return 1; |
| 1672 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1673 | case FOR_LOOP: |
| 1674 | /* Pop the iterator */ |
| 1675 | if (preserve_tos) { |
| 1676 | ADDOP(c, ROT_TWO); |
| 1677 | } |
| 1678 | ADDOP(c, POP_TOP); |
| 1679 | return 1; |
| 1680 | |
| 1681 | case EXCEPT: |
| 1682 | ADDOP(c, POP_BLOCK); |
| 1683 | return 1; |
| 1684 | |
| 1685 | case FINALLY_TRY: |
| 1686 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1687 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1688 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1689 | return 0; |
| 1690 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1691 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1692 | /* Emit the finally block, restoring the line number when done */ |
| 1693 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1694 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1695 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1696 | if (preserve_tos) { |
| 1697 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1698 | } |
| 1699 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1700 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1701 | case FINALLY_END: |
| 1702 | if (preserve_tos) { |
| 1703 | ADDOP(c, ROT_FOUR); |
| 1704 | } |
| 1705 | ADDOP(c, POP_TOP); |
| 1706 | ADDOP(c, POP_TOP); |
| 1707 | ADDOP(c, POP_TOP); |
| 1708 | if (preserve_tos) { |
| 1709 | ADDOP(c, ROT_FOUR); |
| 1710 | } |
| 1711 | ADDOP(c, POP_EXCEPT); |
| 1712 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1713 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1714 | case WITH: |
| 1715 | case ASYNC_WITH: |
| 1716 | ADDOP(c, POP_BLOCK); |
| 1717 | if (preserve_tos) { |
| 1718 | ADDOP(c, ROT_TWO); |
| 1719 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1720 | if(!compiler_call_exit_with_nones(c)) { |
| 1721 | return 0; |
| 1722 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1723 | if (info->fb_type == ASYNC_WITH) { |
| 1724 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1725 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1726 | ADDOP(c, YIELD_FROM); |
| 1727 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1728 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1729 | return 1; |
| 1730 | |
| 1731 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1732 | if (info->fb_datum) { |
| 1733 | ADDOP(c, POP_BLOCK); |
| 1734 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1735 | if (preserve_tos) { |
| 1736 | ADDOP(c, ROT_FOUR); |
| 1737 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1738 | ADDOP(c, POP_EXCEPT); |
| 1739 | if (info->fb_datum) { |
| 1740 | ADDOP_LOAD_CONST(c, Py_None); |
| 1741 | compiler_nameop(c, info->fb_datum, Store); |
| 1742 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1743 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1744 | return 1; |
| 1745 | |
| 1746 | case POP_VALUE: |
| 1747 | if (preserve_tos) { |
| 1748 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1749 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1750 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1751 | return 1; |
| 1752 | } |
| 1753 | Py_UNREACHABLE(); |
| 1754 | } |
| 1755 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1756 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1757 | static int |
| 1758 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1759 | if (c->u->u_nfblocks == 0) { |
| 1760 | return 1; |
| 1761 | } |
| 1762 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1763 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1764 | *loop = top; |
| 1765 | return 1; |
| 1766 | } |
| 1767 | struct fblockinfo copy = *top; |
| 1768 | c->u->u_nfblocks--; |
| 1769 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1770 | return 0; |
| 1771 | } |
| 1772 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1773 | return 0; |
| 1774 | } |
| 1775 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1776 | c->u->u_nfblocks++; |
| 1777 | return 1; |
| 1778 | } |
| 1779 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1780 | /* Compile a sequence of statements, checking for a docstring |
| 1781 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1782 | |
| 1783 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1784 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1785 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1786 | int i = 0; |
| 1787 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1788 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1789 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1790 | /* Set current line number to the line number of first statement. |
| 1791 | This way line number for SETUP_ANNOTATIONS will always |
| 1792 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1793 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1794 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE && asdl_seq_LEN(stmts)) { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1795 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1796 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1797 | } |
| 1798 | /* Every annotated class and module should have __annotations__. */ |
| 1799 | if (find_ann(stmts)) { |
| 1800 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1801 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1802 | if (!asdl_seq_LEN(stmts)) |
| 1803 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1804 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1805 | if (c->c_optimize < 2) { |
| 1806 | docstring = _PyAST_GetDocString(stmts); |
| 1807 | if (docstring) { |
| 1808 | i = 1; |
| 1809 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1810 | assert(st->kind == Expr_kind); |
| 1811 | VISIT(c, expr, st->v.Expr.value); |
| 1812 | if (!compiler_nameop(c, __doc__, Store)) |
| 1813 | return 0; |
| 1814 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1815 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1816 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1817 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1818 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | static PyCodeObject * |
| 1822 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1823 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1824 | PyCodeObject *co; |
| 1825 | int addNone = 1; |
| 1826 | static PyObject *module; |
| 1827 | if (!module) { |
| 1828 | module = PyUnicode_InternFromString("<module>"); |
| 1829 | if (!module) |
| 1830 | return NULL; |
| 1831 | } |
| 1832 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1833 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1834 | return NULL; |
| 1835 | switch (mod->kind) { |
| 1836 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1837 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1838 | compiler_exit_scope(c); |
| 1839 | return 0; |
| 1840 | } |
| 1841 | break; |
| 1842 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1843 | if (find_ann(mod->v.Interactive.body)) { |
| 1844 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1845 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1846 | c->c_interactive = 1; |
| 1847 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1848 | mod->v.Interactive.body); |
| 1849 | break; |
| 1850 | case Expression_kind: |
| 1851 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1852 | addNone = 0; |
| 1853 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1854 | default: |
| 1855 | PyErr_Format(PyExc_SystemError, |
| 1856 | "module kind %d should not be possible", |
| 1857 | mod->kind); |
| 1858 | return 0; |
| 1859 | } |
| 1860 | co = assemble(c, addNone); |
| 1861 | compiler_exit_scope(c); |
| 1862 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1863 | } |
| 1864 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1865 | /* The test for LOCAL must come before the test for FREE in order to |
| 1866 | handle classes where name is both local and free. The local var is |
| 1867 | 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] | 1868 | */ |
| 1869 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1870 | static int |
| 1871 | get_ref_type(struct compiler *c, PyObject *name) |
| 1872 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1873 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1874 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1875 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1876 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1877 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1878 | if (scope == 0) { |
| 1879 | char buf[350]; |
| 1880 | PyOS_snprintf(buf, sizeof(buf), |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 1881 | "unknown scope for %.100s in %.100s(%s)\n" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | "symbols: %s\nlocals: %s\nglobals: %s", |
Serhiy Storchaka | df4518c | 2014-11-18 23:34:33 +0200 | [diff] [blame] | 1883 | PyUnicode_AsUTF8(name), |
| 1884 | PyUnicode_AsUTF8(c->u->u_name), |
| 1885 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1886 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1887 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1888 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | ); |
| 1890 | Py_FatalError(buf); |
| 1891 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1892 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1894 | } |
| 1895 | |
| 1896 | static int |
| 1897 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1898 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1899 | PyObject *v; |
| 1900 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1901 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1902 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1903 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1907 | 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] | 1908 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1909 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1910 | if (qualname == NULL) |
| 1911 | qualname = co->co_name; |
| 1912 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1913 | if (free) { |
| 1914 | for (i = 0; i < free; ++i) { |
| 1915 | /* Bypass com_addop_varname because it will generate |
| 1916 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1917 | */ |
| 1918 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1919 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1920 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1921 | /* Special case: If a class contains a method with a |
| 1922 | free variable that has the same name as a method, |
| 1923 | the name will be considered free *and* local in the |
| 1924 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1925 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1926 | */ |
| 1927 | reftype = get_ref_type(c, name); |
| 1928 | if (reftype == CELL) |
| 1929 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1930 | else /* (reftype == FREE) */ |
| 1931 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1932 | if (arg == -1) { |
| 1933 | fprintf(stderr, |
| 1934 | "lookup %s in %s %d %d\n" |
| 1935 | "freevars of %s: %s\n", |
| 1936 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1937 | PyUnicode_AsUTF8(c->u->u_name), |
| 1938 | reftype, arg, |
| 1939 | PyUnicode_AsUTF8(co->co_name), |
| 1940 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
| 1941 | Py_FatalError("compiler_make_closure()"); |
| 1942 | } |
| 1943 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1944 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1945 | flags |= 0x08; |
| 1946 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1947 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1948 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1949 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1950 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | } |
| 1953 | |
| 1954 | static int |
| 1955 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1956 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1957 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1958 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1959 | if (!decos) |
| 1960 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1961 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1962 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1963 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1964 | } |
| 1965 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1966 | } |
| 1967 | |
| 1968 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1969 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1970 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1971 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1972 | /* Push a dict of keyword-only default values. |
| 1973 | |
| 1974 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1975 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1976 | int i; |
| 1977 | PyObject *keys = NULL; |
| 1978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1980 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1981 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1982 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1983 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1984 | if (!mangled) { |
| 1985 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1986 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1987 | if (keys == NULL) { |
| 1988 | keys = PyList_New(1); |
| 1989 | if (keys == NULL) { |
| 1990 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1991 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1992 | } |
| 1993 | PyList_SET_ITEM(keys, 0, mangled); |
| 1994 | } |
| 1995 | else { |
| 1996 | int res = PyList_Append(keys, mangled); |
| 1997 | Py_DECREF(mangled); |
| 1998 | if (res == -1) { |
| 1999 | goto error; |
| 2000 | } |
| 2001 | } |
| 2002 | if (!compiler_visit_expr(c, default_)) { |
| 2003 | goto error; |
| 2004 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2005 | } |
| 2006 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2007 | if (keys != NULL) { |
| 2008 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2009 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2010 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2011 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2012 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2013 | assert(default_count > 0); |
| 2014 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2015 | } |
| 2016 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2017 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2018 | } |
| 2019 | |
| 2020 | error: |
| 2021 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2022 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2023 | } |
| 2024 | |
| 2025 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2026 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2027 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2028 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2029 | return 1; |
| 2030 | } |
| 2031 | |
| 2032 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2033 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 2034 | expr_ty annotation, PyObject *names) |
| 2035 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2036 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2037 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2038 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2039 | VISIT(c, annexpr, annotation) |
| 2040 | } |
| 2041 | else { |
| 2042 | VISIT(c, expr, annotation); |
| 2043 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2044 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2045 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2046 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2047 | if (PyList_Append(names, mangled) < 0) { |
| 2048 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2049 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2050 | } |
| 2051 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2052 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2053 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2054 | } |
| 2055 | |
| 2056 | static int |
| 2057 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 2058 | PyObject *names) |
| 2059 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2060 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2061 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2062 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2063 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2064 | c, |
| 2065 | arg->arg, |
| 2066 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2067 | names)) |
| 2068 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2069 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2070 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2071 | } |
| 2072 | |
| 2073 | static int |
| 2074 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2075 | expr_ty returns) |
| 2076 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2077 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2078 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2079 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2080 | 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] | 2081 | */ |
| 2082 | static identifier return_str; |
| 2083 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2084 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2085 | names = PyList_New(0); |
| 2086 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2087 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2088 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2089 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 2091 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 2092 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2093 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2094 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2095 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2097 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2099 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2100 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2101 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2102 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2103 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | if (!return_str) { |
| 2105 | return_str = PyUnicode_InternFromString("return"); |
| 2106 | if (!return_str) |
| 2107 | goto error; |
| 2108 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2109 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2110 | goto error; |
| 2111 | } |
| 2112 | |
| 2113 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2115 | PyObject *keytuple = PyList_AsTuple(names); |
| 2116 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2117 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2118 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2119 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2120 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2121 | else { |
| 2122 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2123 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2124 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2125 | |
| 2126 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2127 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2128 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2129 | } |
| 2130 | |
| 2131 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2132 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2133 | { |
| 2134 | VISIT_SEQ(c, expr, args->defaults); |
| 2135 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2136 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2137 | } |
| 2138 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2139 | static Py_ssize_t |
| 2140 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2141 | { |
| 2142 | Py_ssize_t funcflags = 0; |
| 2143 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2144 | if (!compiler_visit_defaults(c, args)) |
| 2145 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2146 | funcflags |= 0x01; |
| 2147 | } |
| 2148 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2149 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2150 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2151 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2152 | return -1; |
| 2153 | } |
| 2154 | else if (res > 0) { |
| 2155 | funcflags |= 0x02; |
| 2156 | } |
| 2157 | } |
| 2158 | return funcflags; |
| 2159 | } |
| 2160 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2161 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2162 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2163 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2164 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2165 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2166 | arguments_ty args; |
| 2167 | expr_ty returns; |
| 2168 | identifier name; |
| 2169 | asdl_seq* decos; |
| 2170 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2171 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2172 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2173 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2174 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2175 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2176 | if (is_async) { |
| 2177 | assert(s->kind == AsyncFunctionDef_kind); |
| 2178 | |
| 2179 | args = s->v.AsyncFunctionDef.args; |
| 2180 | returns = s->v.AsyncFunctionDef.returns; |
| 2181 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2182 | name = s->v.AsyncFunctionDef.name; |
| 2183 | body = s->v.AsyncFunctionDef.body; |
| 2184 | |
| 2185 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2186 | } else { |
| 2187 | assert(s->kind == FunctionDef_kind); |
| 2188 | |
| 2189 | args = s->v.FunctionDef.args; |
| 2190 | returns = s->v.FunctionDef.returns; |
| 2191 | decos = s->v.FunctionDef.decorator_list; |
| 2192 | name = s->v.FunctionDef.name; |
| 2193 | body = s->v.FunctionDef.body; |
| 2194 | |
| 2195 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2196 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2197 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2198 | if (!compiler_decorators(c, decos)) |
| 2199 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2200 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2201 | firstlineno = s->lineno; |
| 2202 | if (asdl_seq_LEN(decos)) { |
| 2203 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2204 | } |
| 2205 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2206 | funcflags = compiler_default_arguments(c, args); |
| 2207 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2208 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2209 | } |
| 2210 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2211 | annotations = compiler_visit_annotations(c, args, returns); |
| 2212 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2213 | return 0; |
| 2214 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2215 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2216 | funcflags |= 0x04; |
| 2217 | } |
| 2218 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2219 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2220 | return 0; |
| 2221 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2222 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2223 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2224 | if (c->c_optimize < 2) { |
| 2225 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2226 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2227 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2228 | compiler_exit_scope(c); |
| 2229 | return 0; |
| 2230 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2231 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2232 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2233 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2234 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2235 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2236 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2237 | qualname = c->u->u_qualname; |
| 2238 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2239 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2240 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2241 | Py_XDECREF(qualname); |
| 2242 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2243 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2244 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2245 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2246 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2247 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2248 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2249 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2250 | /* decorators */ |
| 2251 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2252 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2253 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2254 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2255 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2256 | } |
| 2257 | |
| 2258 | static int |
| 2259 | compiler_class(struct compiler *c, stmt_ty s) |
| 2260 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2261 | PyCodeObject *co; |
| 2262 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2263 | int i, firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2264 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
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 | if (!compiler_decorators(c, decos)) |
| 2267 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2268 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2269 | firstlineno = s->lineno; |
| 2270 | if (asdl_seq_LEN(decos)) { |
| 2271 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2272 | } |
| 2273 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | /* ultimately generate code for: |
| 2275 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2276 | where: |
| 2277 | <func> is a function/closure created from the class body; |
| 2278 | it has a single argument (__locals__) where the dict |
| 2279 | (or MutableSequence) representing the locals is passed |
| 2280 | <name> is the class name |
| 2281 | <bases> is the positional arguments and *varargs argument |
| 2282 | <keywords> is the keyword arguments and **kwds argument |
| 2283 | This borrows from compiler_call. |
| 2284 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2286 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2287 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2288 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | return 0; |
| 2290 | /* this block represents what we do in the new scope */ |
| 2291 | { |
| 2292 | /* use the class name for name mangling */ |
| 2293 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2294 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2295 | /* load (global) __name__ ... */ |
| 2296 | str = PyUnicode_InternFromString("__name__"); |
| 2297 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2298 | Py_XDECREF(str); |
| 2299 | compiler_exit_scope(c); |
| 2300 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2301 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2302 | Py_DECREF(str); |
| 2303 | /* ... and store it as __module__ */ |
| 2304 | str = PyUnicode_InternFromString("__module__"); |
| 2305 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2306 | Py_XDECREF(str); |
| 2307 | compiler_exit_scope(c); |
| 2308 | return 0; |
| 2309 | } |
| 2310 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2311 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2312 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2313 | str = PyUnicode_InternFromString("__qualname__"); |
| 2314 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2315 | Py_XDECREF(str); |
| 2316 | compiler_exit_scope(c); |
| 2317 | return 0; |
| 2318 | } |
| 2319 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2321 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2322 | compiler_exit_scope(c); |
| 2323 | return 0; |
| 2324 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2325 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2326 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2327 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2328 | str = PyUnicode_InternFromString("__class__"); |
| 2329 | if (str == NULL) { |
| 2330 | compiler_exit_scope(c); |
| 2331 | return 0; |
| 2332 | } |
| 2333 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2334 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2335 | if (i < 0) { |
| 2336 | compiler_exit_scope(c); |
| 2337 | return 0; |
| 2338 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2339 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2340 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2342 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2343 | str = PyUnicode_InternFromString("__classcell__"); |
| 2344 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2345 | Py_XDECREF(str); |
| 2346 | compiler_exit_scope(c); |
| 2347 | return 0; |
| 2348 | } |
| 2349 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2350 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2351 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2352 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2353 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2354 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2355 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2356 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2357 | /* create the code object */ |
| 2358 | co = assemble(c, 1); |
| 2359 | } |
| 2360 | /* leave the new scope */ |
| 2361 | compiler_exit_scope(c); |
| 2362 | if (co == NULL) |
| 2363 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2364 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2365 | /* 2. load the 'build_class' function */ |
| 2366 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2367 | |
| 2368 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2369 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2370 | Py_DECREF(co); |
| 2371 | |
| 2372 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2373 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2374 | |
| 2375 | /* 5. generate the rest of the code for the call */ |
| 2376 | if (!compiler_call_helper(c, 2, |
| 2377 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2378 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2379 | return 0; |
| 2380 | |
| 2381 | /* 6. apply decorators */ |
| 2382 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2383 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2384 | } |
| 2385 | |
| 2386 | /* 7. store into <name> */ |
| 2387 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2388 | return 0; |
| 2389 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2390 | } |
| 2391 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2392 | /* Return 0 if the expression is a constant value except named singletons. |
| 2393 | Return 1 otherwise. */ |
| 2394 | static int |
| 2395 | check_is_arg(expr_ty e) |
| 2396 | { |
| 2397 | if (e->kind != Constant_kind) { |
| 2398 | return 1; |
| 2399 | } |
| 2400 | PyObject *value = e->v.Constant.value; |
| 2401 | return (value == Py_None |
| 2402 | || value == Py_False |
| 2403 | || value == Py_True |
| 2404 | || value == Py_Ellipsis); |
| 2405 | } |
| 2406 | |
| 2407 | /* Check operands of identity chacks ("is" and "is not"). |
| 2408 | Emit a warning if any operand is a constant except named singletons. |
| 2409 | Return 0 on error. |
| 2410 | */ |
| 2411 | static int |
| 2412 | check_compare(struct compiler *c, expr_ty e) |
| 2413 | { |
| 2414 | Py_ssize_t i, n; |
| 2415 | int left = check_is_arg(e->v.Compare.left); |
| 2416 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2417 | for (i = 0; i < n; i++) { |
| 2418 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2419 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2420 | if (op == Is || op == IsNot) { |
| 2421 | if (!right || !left) { |
| 2422 | const char *msg = (op == Is) |
| 2423 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2424 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2425 | return compiler_warn(c, msg); |
| 2426 | } |
| 2427 | } |
| 2428 | left = right; |
| 2429 | } |
| 2430 | return 1; |
| 2431 | } |
| 2432 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2433 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2434 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2435 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2436 | switch (op) { |
| 2437 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2438 | cmp = Py_EQ; |
| 2439 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2440 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2441 | cmp = Py_NE; |
| 2442 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2443 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2444 | cmp = Py_LT; |
| 2445 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2446 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2447 | cmp = Py_LE; |
| 2448 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2449 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2450 | cmp = Py_GT; |
| 2451 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2452 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2453 | cmp = Py_GE; |
| 2454 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2455 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2456 | ADDOP_I(c, IS_OP, 0); |
| 2457 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2458 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2459 | ADDOP_I(c, IS_OP, 1); |
| 2460 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2461 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2462 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2463 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2464 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2465 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2466 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2467 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2468 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2469 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2470 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2471 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2472 | } |
| 2473 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2474 | |
| 2475 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2476 | static int |
| 2477 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2478 | { |
| 2479 | switch (e->kind) { |
| 2480 | case UnaryOp_kind: |
| 2481 | if (e->v.UnaryOp.op == Not) |
| 2482 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2483 | /* fallback to general implementation */ |
| 2484 | break; |
| 2485 | case BoolOp_kind: { |
| 2486 | asdl_seq *s = e->v.BoolOp.values; |
| 2487 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2488 | assert(n >= 0); |
| 2489 | int cond2 = e->v.BoolOp.op == Or; |
| 2490 | basicblock *next2 = next; |
| 2491 | if (!cond2 != !cond) { |
| 2492 | next2 = compiler_new_block(c); |
| 2493 | if (next2 == NULL) |
| 2494 | return 0; |
| 2495 | } |
| 2496 | for (i = 0; i < n; ++i) { |
| 2497 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2498 | return 0; |
| 2499 | } |
| 2500 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2501 | return 0; |
| 2502 | if (next2 != next) |
| 2503 | compiler_use_next_block(c, next2); |
| 2504 | return 1; |
| 2505 | } |
| 2506 | case IfExp_kind: { |
| 2507 | basicblock *end, *next2; |
| 2508 | end = compiler_new_block(c); |
| 2509 | if (end == NULL) |
| 2510 | return 0; |
| 2511 | next2 = compiler_new_block(c); |
| 2512 | if (next2 == NULL) |
| 2513 | return 0; |
| 2514 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2515 | return 0; |
| 2516 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2517 | return 0; |
| 2518 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2519 | compiler_use_next_block(c, next2); |
| 2520 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2521 | return 0; |
| 2522 | compiler_use_next_block(c, end); |
| 2523 | return 1; |
| 2524 | } |
| 2525 | case Compare_kind: { |
| 2526 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2527 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2528 | if (!check_compare(c, e)) { |
| 2529 | return 0; |
| 2530 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2531 | basicblock *cleanup = compiler_new_block(c); |
| 2532 | if (cleanup == NULL) |
| 2533 | return 0; |
| 2534 | VISIT(c, expr, e->v.Compare.left); |
| 2535 | for (i = 0; i < n; i++) { |
| 2536 | VISIT(c, expr, |
| 2537 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2538 | ADDOP(c, DUP_TOP); |
| 2539 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2540 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2541 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2542 | NEXT_BLOCK(c); |
| 2543 | } |
| 2544 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2545 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2546 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2547 | basicblock *end = compiler_new_block(c); |
| 2548 | if (end == NULL) |
| 2549 | return 0; |
| 2550 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2551 | compiler_use_next_block(c, cleanup); |
| 2552 | ADDOP(c, POP_TOP); |
| 2553 | if (!cond) { |
| 2554 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2555 | } |
| 2556 | compiler_use_next_block(c, end); |
| 2557 | return 1; |
| 2558 | } |
| 2559 | /* fallback to general implementation */ |
| 2560 | break; |
| 2561 | } |
| 2562 | default: |
| 2563 | /* fallback to general implementation */ |
| 2564 | break; |
| 2565 | } |
| 2566 | |
| 2567 | /* general implementation */ |
| 2568 | VISIT(c, expr, e); |
| 2569 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2570 | return 1; |
| 2571 | } |
| 2572 | |
| 2573 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2574 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2575 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2576 | basicblock *end, *next; |
| 2577 | |
| 2578 | assert(e->kind == IfExp_kind); |
| 2579 | end = compiler_new_block(c); |
| 2580 | if (end == NULL) |
| 2581 | return 0; |
| 2582 | next = compiler_new_block(c); |
| 2583 | if (next == NULL) |
| 2584 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2585 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2586 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2587 | VISIT(c, expr, e->v.IfExp.body); |
| 2588 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2589 | compiler_use_next_block(c, next); |
| 2590 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2591 | compiler_use_next_block(c, end); |
| 2592 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2593 | } |
| 2594 | |
| 2595 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2596 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2597 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2598 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2599 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2600 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2601 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2602 | arguments_ty args = e->v.Lambda.args; |
| 2603 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2604 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2605 | if (!name) { |
| 2606 | name = PyUnicode_InternFromString("<lambda>"); |
| 2607 | if (!name) |
| 2608 | return 0; |
| 2609 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2610 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2611 | funcflags = compiler_default_arguments(c, args); |
| 2612 | if (funcflags == -1) { |
| 2613 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2614 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2615 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2616 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2617 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2618 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2619 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2620 | /* Make None the first constant, so the lambda can't have a |
| 2621 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2622 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2623 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2624 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2625 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2626 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2627 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2628 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2629 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2630 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2631 | } |
| 2632 | else { |
| 2633 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2634 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2635 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2636 | qualname = c->u->u_qualname; |
| 2637 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2638 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2639 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2640 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2641 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2642 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2643 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | Py_DECREF(co); |
| 2645 | |
| 2646 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2647 | } |
| 2648 | |
| 2649 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2650 | compiler_if(struct compiler *c, stmt_ty s) |
| 2651 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2652 | basicblock *end, *next; |
| 2653 | int constant; |
| 2654 | assert(s->kind == If_kind); |
| 2655 | end = compiler_new_block(c); |
| 2656 | if (end == NULL) |
| 2657 | return 0; |
| 2658 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2659 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2660 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2661 | * constant = 1: "if 1", "if 2", ... |
| 2662 | * constant = -1: rest */ |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2663 | if (constant == 0) { |
| 2664 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2665 | VISIT_SEQ(c, stmt, s->v.If.body); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2666 | END_DO_NOT_EMIT_BYTECODE |
| 2667 | if (s->v.If.orelse) { |
| 2668 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2669 | } |
| 2670 | } else if (constant == 1) { |
| 2671 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2672 | if (s->v.If.orelse) { |
| 2673 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2674 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2675 | END_DO_NOT_EMIT_BYTECODE |
| 2676 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2677 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2678 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2679 | next = compiler_new_block(c); |
| 2680 | if (next == NULL) |
| 2681 | return 0; |
| 2682 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2683 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | next = end; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2685 | } |
| 2686 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2687 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2688 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2690 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2691 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2692 | compiler_use_next_block(c, next); |
| 2693 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2694 | } |
| 2695 | } |
| 2696 | compiler_use_next_block(c, end); |
| 2697 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2698 | } |
| 2699 | |
| 2700 | static int |
| 2701 | compiler_for(struct compiler *c, stmt_ty s) |
| 2702 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2703 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2704 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2705 | start = compiler_new_block(c); |
| 2706 | cleanup = compiler_new_block(c); |
| 2707 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2708 | if (start == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2709 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2710 | } |
| 2711 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2712 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2713 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 | VISIT(c, expr, s->v.For.iter); |
| 2715 | ADDOP(c, GET_ITER); |
| 2716 | compiler_use_next_block(c, start); |
| 2717 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2718 | VISIT(c, expr, s->v.For.target); |
| 2719 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2720 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2721 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2722 | |
| 2723 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2724 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2725 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2726 | compiler_use_next_block(c, end); |
| 2727 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2728 | } |
| 2729 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2730 | |
| 2731 | static int |
| 2732 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2733 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2734 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2735 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2736 | c->u->u_ste->ste_coroutine = 1; |
| 2737 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2738 | return compiler_error(c, "'async for' outside async function"); |
| 2739 | } |
| 2740 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2741 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2742 | except = compiler_new_block(c); |
| 2743 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2744 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2745 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2746 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2747 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2748 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2749 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2750 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2751 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2752 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2753 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2754 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2755 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2756 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2757 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2758 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2759 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2760 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2761 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2762 | /* Success block for __anext__ */ |
| 2763 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2764 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2765 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2766 | |
| 2767 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2768 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2769 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2770 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2771 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2772 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2773 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2774 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2775 | |
| 2776 | compiler_use_next_block(c, end); |
| 2777 | |
| 2778 | return 1; |
| 2779 | } |
| 2780 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2781 | static int |
| 2782 | compiler_while(struct compiler *c, stmt_ty s) |
| 2783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2784 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2785 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2786 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2787 | if (constant == 0) { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2788 | BEGIN_DO_NOT_EMIT_BYTECODE |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2789 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2790 | // inside a while loop so it can correctly evaluate syntax |
| 2791 | // errors. |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2792 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) { |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2795 | VISIT_SEQ(c, stmt, s->v.While.body); |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2796 | // Remove the dummy block now that is not needed. |
| 2797 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2798 | END_DO_NOT_EMIT_BYTECODE |
| 2799 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2800 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2801 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2802 | return 1; |
| 2803 | } |
| 2804 | loop = compiler_new_block(c); |
| 2805 | end = compiler_new_block(c); |
| 2806 | if (constant == -1) { |
| 2807 | anchor = compiler_new_block(c); |
| 2808 | if (anchor == NULL) |
| 2809 | return 0; |
| 2810 | } |
| 2811 | if (loop == NULL || end == NULL) |
| 2812 | return 0; |
| 2813 | if (s->v.While.orelse) { |
| 2814 | orelse = compiler_new_block(c); |
| 2815 | if (orelse == NULL) |
| 2816 | return 0; |
| 2817 | } |
| 2818 | else |
| 2819 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2820 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2821 | compiler_use_next_block(c, loop); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2822 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2823 | return 0; |
| 2824 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2825 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2826 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2827 | } |
| 2828 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2829 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2831 | /* XXX should the two POP instructions be in a separate block |
| 2832 | if there is no else clause ? |
| 2833 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2834 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2835 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2837 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2838 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2839 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2840 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2841 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2842 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2843 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2844 | } |
| 2845 | |
| 2846 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2847 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2848 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2849 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2850 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2851 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2852 | return compiler_error(c, "'return' outside function"); |
| 2853 | if (s->v.Return.value != NULL && |
| 2854 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2855 | { |
| 2856 | return compiler_error( |
| 2857 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2858 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2859 | if (preserve_tos) { |
| 2860 | VISIT(c, expr, s->v.Return.value); |
| 2861 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2862 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2863 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2864 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2865 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2866 | } |
| 2867 | else if (!preserve_tos) { |
| 2868 | VISIT(c, expr, s->v.Return.value); |
| 2869 | } |
| 2870 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2871 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2873 | } |
| 2874 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2875 | static int |
| 2876 | compiler_break(struct compiler *c) |
| 2877 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2878 | struct fblockinfo *loop = NULL; |
| 2879 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2880 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2881 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2882 | if (loop == NULL) { |
| 2883 | return compiler_error(c, "'break' outside loop"); |
| 2884 | } |
| 2885 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2886 | return 0; |
| 2887 | } |
| 2888 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_exit); |
| 2889 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2890 | } |
| 2891 | |
| 2892 | static int |
| 2893 | compiler_continue(struct compiler *c) |
| 2894 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2895 | struct fblockinfo *loop = NULL; |
| 2896 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2897 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2898 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2899 | if (loop == NULL) { |
| 2900 | return compiler_error(c, "'continue' not properly in loop"); |
| 2901 | } |
| 2902 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_block); |
| 2903 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2904 | } |
| 2905 | |
| 2906 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2907 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2908 | |
| 2909 | SETUP_FINALLY L |
| 2910 | <code for body> |
| 2911 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2912 | <code for finalbody> |
| 2913 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2914 | L: |
| 2915 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2916 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2917 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2918 | The special instructions use the block stack. Each block |
| 2919 | stack entry contains the instruction that created it (here |
| 2920 | SETUP_FINALLY), the level of the value stack at the time the |
| 2921 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2922 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2923 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2924 | Pushes the current value stack level and the label |
| 2925 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2926 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2927 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2928 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2929 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2930 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2931 | exceptions are pushed onto the value stack (and the exception |
| 2932 | condition is cleared), and the interpreter jumps to the label |
| 2933 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2934 | */ |
| 2935 | |
| 2936 | static int |
| 2937 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2938 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2939 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2941 | body = compiler_new_block(c); |
| 2942 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2943 | exit = compiler_new_block(c); |
| 2944 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2945 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2946 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2947 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2948 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2949 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2950 | if (!compiler_push_fblock(c, FINALLY_TRY, body, end, s->v.Try.finalbody)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2951 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 2952 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 2953 | if (!compiler_try_except(c, s)) |
| 2954 | return 0; |
| 2955 | } |
| 2956 | else { |
| 2957 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 2958 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2959 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2960 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 2961 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2962 | ADDOP_JREL(c, JUMP_FORWARD, exit); |
| 2963 | /* `finally` block */ |
| 2964 | compiler_use_next_block(c, end); |
| 2965 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 2966 | return 0; |
| 2967 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 2968 | compiler_pop_fblock(c, FINALLY_END, end); |
| 2969 | ADDOP(c, RERAISE); |
| 2970 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | } |
| 2973 | |
| 2974 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 2975 | 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] | 2976 | (The contents of the value stack is shown in [], with the top |
| 2977 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 2978 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2979 | |
| 2980 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2981 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2982 | [] <code for S> |
| 2983 | [] POP_BLOCK |
| 2984 | [] JUMP_FORWARD L0 |
| 2985 | |
| 2986 | [tb, val, exc] L1: DUP ) |
| 2987 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2988 | [tb, val, exc, exc, E1] JUMP_IF_NOT_EXC_MATCH L2 ) only if E1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2989 | [tb, val, exc] POP |
| 2990 | [tb, val] <assign to V1> (or POP if no V1) |
| 2991 | [tb] POP |
| 2992 | [] <code for S1> |
| 2993 | JUMP_FORWARD L0 |
| 2994 | |
| 2995 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2996 | .............................etc....................... |
| 2997 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2998 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2999 | |
| 3000 | [] L0: <next statement> |
| 3001 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3002 | Of course, parts are not generated if Vi or Ei is not present. |
| 3003 | */ |
| 3004 | static int |
| 3005 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3006 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3007 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3008 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3009 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3010 | body = compiler_new_block(c); |
| 3011 | except = compiler_new_block(c); |
| 3012 | orelse = compiler_new_block(c); |
| 3013 | end = compiler_new_block(c); |
| 3014 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3015 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3016 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3017 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3018 | if (!compiler_push_fblock(c, EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3019 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3020 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3021 | ADDOP(c, POP_BLOCK); |
| 3022 | compiler_pop_fblock(c, EXCEPT, body); |
| 3023 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3024 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3025 | compiler_use_next_block(c, except); |
| 3026 | for (i = 0; i < n; i++) { |
| 3027 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3028 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3029 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3030 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3031 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3032 | except = compiler_new_block(c); |
| 3033 | if (except == NULL) |
| 3034 | return 0; |
| 3035 | if (handler->v.ExceptHandler.type) { |
| 3036 | ADDOP(c, DUP_TOP); |
| 3037 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3038 | ADDOP_JABS(c, JUMP_IF_NOT_EXC_MATCH, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3039 | } |
| 3040 | ADDOP(c, POP_TOP); |
| 3041 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3042 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3043 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3044 | cleanup_end = compiler_new_block(c); |
| 3045 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3046 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3047 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3048 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3049 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3050 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3051 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3052 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3053 | /* |
| 3054 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3055 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3056 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3057 | try: |
| 3058 | # body |
| 3059 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3060 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3061 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3062 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3063 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3064 | /* second try: */ |
| 3065 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 3066 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3067 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, handler->v.ExceptHandler.name)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3068 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3069 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3070 | /* second # body */ |
| 3071 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3072 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3073 | ADDOP(c, POP_BLOCK); |
| 3074 | ADDOP(c, POP_EXCEPT); |
| 3075 | /* name = None; del name */ |
| 3076 | ADDOP_LOAD_CONST(c, Py_None); |
| 3077 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3078 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
| 3079 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3080 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3081 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3082 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3083 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3084 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3085 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3086 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3087 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3089 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3090 | } |
| 3091 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3092 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3093 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3094 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3095 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3096 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3097 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3098 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3099 | ADDOP(c, POP_TOP); |
| 3100 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3101 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3102 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3103 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3104 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3105 | ADDOP(c, POP_EXCEPT); |
| 3106 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3107 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3108 | compiler_use_next_block(c, except); |
| 3109 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3110 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3111 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3112 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3113 | compiler_use_next_block(c, end); |
| 3114 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3115 | } |
| 3116 | |
| 3117 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3118 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3119 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3120 | return compiler_try_finally(c, s); |
| 3121 | else |
| 3122 | return compiler_try_except(c, s); |
| 3123 | } |
| 3124 | |
| 3125 | |
| 3126 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3127 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | /* The IMPORT_NAME opcode was already generated. This function |
| 3130 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3131 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | 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] | 3133 | IMPORT_FROM for each name. |
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 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3136 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3137 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3138 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3139 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3140 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3141 | while (1) { |
| 3142 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3143 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3144 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3145 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3146 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3147 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3148 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3149 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3150 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3151 | if (dot == -1) { |
| 3152 | break; |
| 3153 | } |
| 3154 | ADDOP(c, ROT_TWO); |
| 3155 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3157 | if (!compiler_nameop(c, asname, Store)) { |
| 3158 | return 0; |
| 3159 | } |
| 3160 | ADDOP(c, POP_TOP); |
| 3161 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | } |
| 3163 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | static int |
| 3167 | compiler_import(struct compiler *c, stmt_ty s) |
| 3168 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3169 | /* The Import node stores a module name like a.b.c as a single |
| 3170 | string. This is convenient for all cases except |
| 3171 | import a.b.c as d |
| 3172 | where we need to parse that string to extract the individual |
| 3173 | module names. |
| 3174 | XXX Perhaps change the representation to make this case simpler? |
| 3175 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3176 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3177 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | for (i = 0; i < n; i++) { |
| 3179 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3180 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3181 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3182 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3183 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3184 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3185 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3186 | if (alias->asname) { |
| 3187 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3188 | if (!r) |
| 3189 | return r; |
| 3190 | } |
| 3191 | else { |
| 3192 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3193 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3194 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3195 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3196 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3197 | if (tmp == NULL) |
| 3198 | return 0; |
| 3199 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3200 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3201 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3202 | Py_DECREF(tmp); |
| 3203 | } |
| 3204 | if (!r) |
| 3205 | return r; |
| 3206 | } |
| 3207 | } |
| 3208 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3209 | } |
| 3210 | |
| 3211 | static int |
| 3212 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3213 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3214 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3215 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3216 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3217 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | if (!empty_string) { |
| 3219 | empty_string = PyUnicode_FromString(""); |
| 3220 | if (!empty_string) |
| 3221 | return 0; |
| 3222 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3223 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3224 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3225 | |
| 3226 | names = PyTuple_New(n); |
| 3227 | if (!names) |
| 3228 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3229 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3230 | /* build up the names */ |
| 3231 | for (i = 0; i < n; i++) { |
| 3232 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3233 | Py_INCREF(alias->name); |
| 3234 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3235 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3236 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3237 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3238 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3239 | Py_DECREF(names); |
| 3240 | return compiler_error(c, "from __future__ imports must occur " |
| 3241 | "at the beginning of the file"); |
| 3242 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3243 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3244 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3245 | if (s->v.ImportFrom.module) { |
| 3246 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3247 | } |
| 3248 | else { |
| 3249 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3250 | } |
| 3251 | for (i = 0; i < n; i++) { |
| 3252 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3253 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3254 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3255 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3256 | assert(n == 1); |
| 3257 | ADDOP(c, IMPORT_STAR); |
| 3258 | return 1; |
| 3259 | } |
| 3260 | |
| 3261 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3262 | store_name = alias->name; |
| 3263 | if (alias->asname) |
| 3264 | store_name = alias->asname; |
| 3265 | |
| 3266 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | return 0; |
| 3268 | } |
| 3269 | } |
| 3270 | /* remove imported module */ |
| 3271 | ADDOP(c, POP_TOP); |
| 3272 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
| 3275 | static int |
| 3276 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3277 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3278 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3279 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3280 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3281 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3282 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3283 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3284 | { |
| 3285 | if (!compiler_warn(c, "assertion is always true, " |
| 3286 | "perhaps remove parentheses?")) |
| 3287 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3288 | return 0; |
| 3289 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3290 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3291 | end = compiler_new_block(c); |
| 3292 | if (end == NULL) |
| 3293 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3294 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3295 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3296 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3297 | if (s->v.Assert.msg) { |
| 3298 | VISIT(c, expr, s->v.Assert.msg); |
| 3299 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3300 | } |
| 3301 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3302 | compiler_use_next_block(c, end); |
| 3303 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3304 | } |
| 3305 | |
| 3306 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3307 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3308 | { |
| 3309 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3310 | VISIT(c, expr, value); |
| 3311 | ADDOP(c, PRINT_EXPR); |
| 3312 | return 1; |
| 3313 | } |
| 3314 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3315 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3316 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3317 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3318 | } |
| 3319 | |
| 3320 | VISIT(c, expr, value); |
| 3321 | ADDOP(c, POP_TOP); |
| 3322 | return 1; |
| 3323 | } |
| 3324 | |
| 3325 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3326 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3327 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3328 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3329 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3331 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3332 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3333 | switch (s->kind) { |
| 3334 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3335 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3336 | case ClassDef_kind: |
| 3337 | return compiler_class(c, s); |
| 3338 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3339 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | case Delete_kind: |
| 3341 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3342 | break; |
| 3343 | case Assign_kind: |
| 3344 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3345 | VISIT(c, expr, s->v.Assign.value); |
| 3346 | for (i = 0; i < n; i++) { |
| 3347 | if (i < n - 1) |
| 3348 | ADDOP(c, DUP_TOP); |
| 3349 | VISIT(c, expr, |
| 3350 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3351 | } |
| 3352 | break; |
| 3353 | case AugAssign_kind: |
| 3354 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3355 | case AnnAssign_kind: |
| 3356 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3357 | case For_kind: |
| 3358 | return compiler_for(c, s); |
| 3359 | case While_kind: |
| 3360 | return compiler_while(c, s); |
| 3361 | case If_kind: |
| 3362 | return compiler_if(c, s); |
| 3363 | case Raise_kind: |
| 3364 | n = 0; |
| 3365 | if (s->v.Raise.exc) { |
| 3366 | VISIT(c, expr, s->v.Raise.exc); |
| 3367 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3368 | if (s->v.Raise.cause) { |
| 3369 | VISIT(c, expr, s->v.Raise.cause); |
| 3370 | n++; |
| 3371 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3372 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3373 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3374 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3375 | case Try_kind: |
| 3376 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3377 | case Assert_kind: |
| 3378 | return compiler_assert(c, s); |
| 3379 | case Import_kind: |
| 3380 | return compiler_import(c, s); |
| 3381 | case ImportFrom_kind: |
| 3382 | return compiler_from_import(c, s); |
| 3383 | case Global_kind: |
| 3384 | case Nonlocal_kind: |
| 3385 | break; |
| 3386 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3387 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3388 | case Pass_kind: |
| 3389 | break; |
| 3390 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3391 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3392 | case Continue_kind: |
| 3393 | return compiler_continue(c); |
| 3394 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3395 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3396 | case AsyncFunctionDef_kind: |
| 3397 | return compiler_function(c, s, 1); |
| 3398 | case AsyncWith_kind: |
| 3399 | return compiler_async_with(c, s, 0); |
| 3400 | case AsyncFor_kind: |
| 3401 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3402 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3403 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3404 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3405 | } |
| 3406 | |
| 3407 | static int |
| 3408 | unaryop(unaryop_ty op) |
| 3409 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3410 | switch (op) { |
| 3411 | case Invert: |
| 3412 | return UNARY_INVERT; |
| 3413 | case Not: |
| 3414 | return UNARY_NOT; |
| 3415 | case UAdd: |
| 3416 | return UNARY_POSITIVE; |
| 3417 | case USub: |
| 3418 | return UNARY_NEGATIVE; |
| 3419 | default: |
| 3420 | PyErr_Format(PyExc_SystemError, |
| 3421 | "unary op %d should not be possible", op); |
| 3422 | return 0; |
| 3423 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3424 | } |
| 3425 | |
| 3426 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3427 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3428 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3429 | switch (op) { |
| 3430 | case Add: |
| 3431 | return BINARY_ADD; |
| 3432 | case Sub: |
| 3433 | return BINARY_SUBTRACT; |
| 3434 | case Mult: |
| 3435 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3436 | case MatMult: |
| 3437 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3438 | case Div: |
| 3439 | return BINARY_TRUE_DIVIDE; |
| 3440 | case Mod: |
| 3441 | return BINARY_MODULO; |
| 3442 | case Pow: |
| 3443 | return BINARY_POWER; |
| 3444 | case LShift: |
| 3445 | return BINARY_LSHIFT; |
| 3446 | case RShift: |
| 3447 | return BINARY_RSHIFT; |
| 3448 | case BitOr: |
| 3449 | return BINARY_OR; |
| 3450 | case BitXor: |
| 3451 | return BINARY_XOR; |
| 3452 | case BitAnd: |
| 3453 | return BINARY_AND; |
| 3454 | case FloorDiv: |
| 3455 | return BINARY_FLOOR_DIVIDE; |
| 3456 | default: |
| 3457 | PyErr_Format(PyExc_SystemError, |
| 3458 | "binary op %d should not be possible", op); |
| 3459 | return 0; |
| 3460 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3461 | } |
| 3462 | |
| 3463 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3464 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3465 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3466 | switch (op) { |
| 3467 | case Add: |
| 3468 | return INPLACE_ADD; |
| 3469 | case Sub: |
| 3470 | return INPLACE_SUBTRACT; |
| 3471 | case Mult: |
| 3472 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3473 | case MatMult: |
| 3474 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3475 | case Div: |
| 3476 | return INPLACE_TRUE_DIVIDE; |
| 3477 | case Mod: |
| 3478 | return INPLACE_MODULO; |
| 3479 | case Pow: |
| 3480 | return INPLACE_POWER; |
| 3481 | case LShift: |
| 3482 | return INPLACE_LSHIFT; |
| 3483 | case RShift: |
| 3484 | return INPLACE_RSHIFT; |
| 3485 | case BitOr: |
| 3486 | return INPLACE_OR; |
| 3487 | case BitXor: |
| 3488 | return INPLACE_XOR; |
| 3489 | case BitAnd: |
| 3490 | return INPLACE_AND; |
| 3491 | case FloorDiv: |
| 3492 | return INPLACE_FLOOR_DIVIDE; |
| 3493 | default: |
| 3494 | PyErr_Format(PyExc_SystemError, |
| 3495 | "inplace binary op %d should not be possible", op); |
| 3496 | return 0; |
| 3497 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3498 | } |
| 3499 | |
| 3500 | static int |
| 3501 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3502 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3503 | int op, scope; |
| 3504 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3505 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3507 | PyObject *dict = c->u->u_names; |
| 3508 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3509 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3510 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3511 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3512 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3513 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3514 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3515 | if (!mangled) |
| 3516 | return 0; |
| 3517 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3518 | op = 0; |
| 3519 | optype = OP_NAME; |
| 3520 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3521 | switch (scope) { |
| 3522 | case FREE: |
| 3523 | dict = c->u->u_freevars; |
| 3524 | optype = OP_DEREF; |
| 3525 | break; |
| 3526 | case CELL: |
| 3527 | dict = c->u->u_cellvars; |
| 3528 | optype = OP_DEREF; |
| 3529 | break; |
| 3530 | case LOCAL: |
| 3531 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3532 | optype = OP_FAST; |
| 3533 | break; |
| 3534 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3535 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3536 | optype = OP_GLOBAL; |
| 3537 | break; |
| 3538 | case GLOBAL_EXPLICIT: |
| 3539 | optype = OP_GLOBAL; |
| 3540 | break; |
| 3541 | default: |
| 3542 | /* scope can be 0 */ |
| 3543 | break; |
| 3544 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3545 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3546 | /* 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] | 3547 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3548 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3549 | switch (optype) { |
| 3550 | case OP_DEREF: |
| 3551 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3552 | case Load: |
| 3553 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3554 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3555 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3556 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3557 | } |
| 3558 | break; |
| 3559 | case OP_FAST: |
| 3560 | switch (ctx) { |
| 3561 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3562 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3563 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3564 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3565 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3566 | return 1; |
| 3567 | case OP_GLOBAL: |
| 3568 | switch (ctx) { |
| 3569 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3570 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3571 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3572 | } |
| 3573 | break; |
| 3574 | case OP_NAME: |
| 3575 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3576 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3577 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3578 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3579 | } |
| 3580 | break; |
| 3581 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3582 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3583 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3584 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3585 | Py_DECREF(mangled); |
| 3586 | if (arg < 0) |
| 3587 | return 0; |
| 3588 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3589 | } |
| 3590 | |
| 3591 | static int |
| 3592 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3593 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3594 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3595 | int jumpi; |
| 3596 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3597 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3598 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3599 | assert(e->kind == BoolOp_kind); |
| 3600 | if (e->v.BoolOp.op == And) |
| 3601 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3602 | else |
| 3603 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3604 | end = compiler_new_block(c); |
| 3605 | if (end == NULL) |
| 3606 | return 0; |
| 3607 | s = e->v.BoolOp.values; |
| 3608 | n = asdl_seq_LEN(s) - 1; |
| 3609 | assert(n >= 0); |
| 3610 | for (i = 0; i < n; ++i) { |
| 3611 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3612 | ADDOP_JABS(c, jumpi, end); |
| 3613 | } |
| 3614 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3615 | compiler_use_next_block(c, end); |
| 3616 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3617 | } |
| 3618 | |
| 3619 | static int |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3620 | starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed, |
| 3621 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3622 | { |
| 3623 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3624 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3625 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3626 | PyObject *folded = PyTuple_New(n); |
| 3627 | if (folded == NULL) { |
| 3628 | return 0; |
| 3629 | } |
| 3630 | PyObject *val; |
| 3631 | for (i = 0; i < n; i++) { |
| 3632 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3633 | Py_INCREF(val); |
| 3634 | PyTuple_SET_ITEM(folded, i, val); |
| 3635 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3636 | if (tuple) { |
| 3637 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3638 | } else { |
| 3639 | if (add == SET_ADD) { |
| 3640 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3641 | if (folded == NULL) { |
| 3642 | return 0; |
| 3643 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3644 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3645 | ADDOP_I(c, build, pushed); |
| 3646 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3647 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3648 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3649 | return 1; |
| 3650 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3651 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3652 | for (i = 0; i < n; i++) { |
| 3653 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3654 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3655 | seen_star = 1; |
| 3656 | } |
| 3657 | } |
| 3658 | if (seen_star) { |
| 3659 | seen_star = 0; |
| 3660 | for (i = 0; i < n; i++) { |
| 3661 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3662 | if (elt->kind == Starred_kind) { |
| 3663 | if (seen_star == 0) { |
| 3664 | ADDOP_I(c, build, i+pushed); |
| 3665 | seen_star = 1; |
| 3666 | } |
| 3667 | VISIT(c, expr, elt->v.Starred.value); |
| 3668 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3669 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3670 | else { |
| 3671 | VISIT(c, expr, elt); |
| 3672 | if (seen_star) { |
| 3673 | ADDOP_I(c, add, 1); |
| 3674 | } |
| 3675 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3676 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3677 | assert(seen_star); |
| 3678 | if (tuple) { |
| 3679 | ADDOP(c, LIST_TO_TUPLE); |
| 3680 | } |
| 3681 | } |
| 3682 | else { |
| 3683 | for (i = 0; i < n; i++) { |
| 3684 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3685 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3686 | } |
| 3687 | if (tuple) { |
| 3688 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3689 | } else { |
| 3690 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3691 | } |
| 3692 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3693 | return 1; |
| 3694 | } |
| 3695 | |
| 3696 | static int |
| 3697 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3698 | { |
| 3699 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3700 | Py_ssize_t i; |
| 3701 | int seen_star = 0; |
| 3702 | for (i = 0; i < n; i++) { |
| 3703 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3704 | if (elt->kind == Starred_kind && !seen_star) { |
| 3705 | if ((i >= (1 << 8)) || |
| 3706 | (n-i-1 >= (INT_MAX >> 8))) |
| 3707 | return compiler_error(c, |
| 3708 | "too many expressions in " |
| 3709 | "star-unpacking assignment"); |
| 3710 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3711 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3712 | } |
| 3713 | else if (elt->kind == Starred_kind) { |
| 3714 | return compiler_error(c, |
| 3715 | "two starred expressions in assignment"); |
| 3716 | } |
| 3717 | } |
| 3718 | if (!seen_star) { |
| 3719 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3720 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3721 | for (i = 0; i < n; i++) { |
| 3722 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3723 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3724 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3725 | return 1; |
| 3726 | } |
| 3727 | |
| 3728 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3729 | compiler_list(struct compiler *c, expr_ty e) |
| 3730 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3731 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3732 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3733 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3734 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3735 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3736 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3737 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3738 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3739 | else |
| 3740 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3741 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3742 | } |
| 3743 | |
| 3744 | static int |
| 3745 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3746 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3747 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3748 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3749 | return assignment_helper(c, elts); |
| 3750 | } |
| 3751 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3752 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3753 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3754 | } |
| 3755 | else |
| 3756 | VISIT_SEQ(c, expr, elts); |
| 3757 | return 1; |
| 3758 | } |
| 3759 | |
| 3760 | static int |
| 3761 | compiler_set(struct compiler *c, expr_ty e) |
| 3762 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3763 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3764 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3765 | } |
| 3766 | |
| 3767 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3768 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3769 | { |
| 3770 | Py_ssize_t i; |
| 3771 | for (i = begin; i < end; i++) { |
| 3772 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3773 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3774 | return 0; |
| 3775 | } |
| 3776 | return 1; |
| 3777 | } |
| 3778 | |
| 3779 | static int |
| 3780 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3781 | { |
| 3782 | Py_ssize_t i, n = end - begin; |
| 3783 | PyObject *keys, *key; |
| 3784 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3785 | for (i = begin; i < end; i++) { |
| 3786 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3787 | } |
| 3788 | keys = PyTuple_New(n); |
| 3789 | if (keys == NULL) { |
| 3790 | return 0; |
| 3791 | } |
| 3792 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3793 | 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] | 3794 | Py_INCREF(key); |
| 3795 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3796 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3797 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3798 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3799 | } |
| 3800 | else { |
| 3801 | for (i = begin; i < end; i++) { |
| 3802 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3803 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3804 | } |
| 3805 | ADDOP_I(c, BUILD_MAP, n); |
| 3806 | } |
| 3807 | return 1; |
| 3808 | } |
| 3809 | |
| 3810 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3811 | compiler_dict(struct compiler *c, expr_ty e) |
| 3812 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3813 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3814 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3815 | int is_unpacking = 0; |
| 3816 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3817 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3818 | elements = 0; |
| 3819 | for (i = 0; i < n; i++) { |
| 3820 | is_unpacking = (expr_ty)asdl_seq_GET(e->v.Dict.keys, i) == NULL; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3821 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3822 | if (elements) { |
| 3823 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3824 | return 0; |
| 3825 | } |
| 3826 | if (have_dict) { |
| 3827 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3828 | } |
| 3829 | have_dict = 1; |
| 3830 | elements = 0; |
| 3831 | } |
| 3832 | if (have_dict == 0) { |
| 3833 | ADDOP_I(c, BUILD_MAP, 0); |
| 3834 | have_dict = 1; |
| 3835 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3836 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3837 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3838 | } |
| 3839 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3840 | if (elements == 0xFFFF) { |
| 3841 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3842 | return 0; |
| 3843 | } |
| 3844 | if (have_dict) { |
| 3845 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3846 | } |
| 3847 | have_dict = 1; |
| 3848 | elements = 0; |
| 3849 | } |
| 3850 | else { |
| 3851 | elements++; |
| 3852 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3853 | } |
| 3854 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3855 | if (elements) { |
| 3856 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3857 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3858 | } |
| 3859 | if (have_dict) { |
| 3860 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3861 | } |
| 3862 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3863 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3864 | if (!have_dict) { |
| 3865 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3866 | } |
| 3867 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3868 | } |
| 3869 | |
| 3870 | static int |
| 3871 | compiler_compare(struct compiler *c, expr_ty e) |
| 3872 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3873 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3874 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3875 | if (!check_compare(c, e)) { |
| 3876 | return 0; |
| 3877 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3878 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3879 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3880 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3881 | if (n == 0) { |
| 3882 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, 0)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3883 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3884 | } |
| 3885 | else { |
| 3886 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3887 | if (cleanup == NULL) |
| 3888 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3889 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3890 | VISIT(c, expr, |
| 3891 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3892 | ADDOP(c, DUP_TOP); |
| 3893 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3894 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3895 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3896 | NEXT_BLOCK(c); |
| 3897 | } |
| 3898 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Compare.comparators, n)); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3899 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3900 | basicblock *end = compiler_new_block(c); |
| 3901 | if (end == NULL) |
| 3902 | return 0; |
| 3903 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3904 | compiler_use_next_block(c, cleanup); |
| 3905 | ADDOP(c, ROT_TWO); |
| 3906 | ADDOP(c, POP_TOP); |
| 3907 | compiler_use_next_block(c, end); |
| 3908 | } |
| 3909 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3910 | } |
| 3911 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3912 | static PyTypeObject * |
| 3913 | infer_type(expr_ty e) |
| 3914 | { |
| 3915 | switch (e->kind) { |
| 3916 | case Tuple_kind: |
| 3917 | return &PyTuple_Type; |
| 3918 | case List_kind: |
| 3919 | case ListComp_kind: |
| 3920 | return &PyList_Type; |
| 3921 | case Dict_kind: |
| 3922 | case DictComp_kind: |
| 3923 | return &PyDict_Type; |
| 3924 | case Set_kind: |
| 3925 | case SetComp_kind: |
| 3926 | return &PySet_Type; |
| 3927 | case GeneratorExp_kind: |
| 3928 | return &PyGen_Type; |
| 3929 | case Lambda_kind: |
| 3930 | return &PyFunction_Type; |
| 3931 | case JoinedStr_kind: |
| 3932 | case FormattedValue_kind: |
| 3933 | return &PyUnicode_Type; |
| 3934 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3935 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3936 | default: |
| 3937 | return NULL; |
| 3938 | } |
| 3939 | } |
| 3940 | |
| 3941 | static int |
| 3942 | check_caller(struct compiler *c, expr_ty e) |
| 3943 | { |
| 3944 | switch (e->kind) { |
| 3945 | case Constant_kind: |
| 3946 | case Tuple_kind: |
| 3947 | case List_kind: |
| 3948 | case ListComp_kind: |
| 3949 | case Dict_kind: |
| 3950 | case DictComp_kind: |
| 3951 | case Set_kind: |
| 3952 | case SetComp_kind: |
| 3953 | case GeneratorExp_kind: |
| 3954 | case JoinedStr_kind: |
| 3955 | case FormattedValue_kind: |
| 3956 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 3957 | "perhaps you missed a comma?", |
| 3958 | infer_type(e)->tp_name); |
| 3959 | default: |
| 3960 | return 1; |
| 3961 | } |
| 3962 | } |
| 3963 | |
| 3964 | static int |
| 3965 | check_subscripter(struct compiler *c, expr_ty e) |
| 3966 | { |
| 3967 | PyObject *v; |
| 3968 | |
| 3969 | switch (e->kind) { |
| 3970 | case Constant_kind: |
| 3971 | v = e->v.Constant.value; |
| 3972 | if (!(v == Py_None || v == Py_Ellipsis || |
| 3973 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 3974 | PyAnySet_Check(v))) |
| 3975 | { |
| 3976 | return 1; |
| 3977 | } |
| 3978 | /* fall through */ |
| 3979 | case Set_kind: |
| 3980 | case SetComp_kind: |
| 3981 | case GeneratorExp_kind: |
| 3982 | case Lambda_kind: |
| 3983 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 3984 | "perhaps you missed a comma?", |
| 3985 | infer_type(e)->tp_name); |
| 3986 | default: |
| 3987 | return 1; |
| 3988 | } |
| 3989 | } |
| 3990 | |
| 3991 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 3992 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3993 | { |
| 3994 | PyObject *v; |
| 3995 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 3996 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3997 | if (index_type == NULL |
| 3998 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 3999 | || index_type == &PySlice_Type) { |
| 4000 | return 1; |
| 4001 | } |
| 4002 | |
| 4003 | switch (e->kind) { |
| 4004 | case Constant_kind: |
| 4005 | v = e->v.Constant.value; |
| 4006 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4007 | return 1; |
| 4008 | } |
| 4009 | /* fall through */ |
| 4010 | case Tuple_kind: |
| 4011 | case List_kind: |
| 4012 | case ListComp_kind: |
| 4013 | case JoinedStr_kind: |
| 4014 | case FormattedValue_kind: |
| 4015 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4016 | "not %.200s; " |
| 4017 | "perhaps you missed a comma?", |
| 4018 | infer_type(e)->tp_name, |
| 4019 | index_type->tp_name); |
| 4020 | default: |
| 4021 | return 1; |
| 4022 | } |
| 4023 | } |
| 4024 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4025 | // 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] | 4026 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4027 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4028 | { |
| 4029 | Py_ssize_t argsl, i; |
| 4030 | expr_ty meth = e->v.Call.func; |
| 4031 | asdl_seq *args = e->v.Call.args; |
| 4032 | |
| 4033 | /* Check that the call node is an attribute access, and that |
| 4034 | the call doesn't have keyword parameters. */ |
| 4035 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4036 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4037 | return -1; |
| 4038 | |
| 4039 | /* Check that there are no *varargs types of arguments. */ |
| 4040 | argsl = asdl_seq_LEN(args); |
| 4041 | for (i = 0; i < argsl; i++) { |
| 4042 | expr_ty elt = asdl_seq_GET(args, i); |
| 4043 | if (elt->kind == Starred_kind) { |
| 4044 | return -1; |
| 4045 | } |
| 4046 | } |
| 4047 | |
| 4048 | /* Alright, we can optimize the code. */ |
| 4049 | VISIT(c, expr, meth->v.Attribute.value); |
| 4050 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4051 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4052 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4053 | return 1; |
| 4054 | } |
| 4055 | |
| 4056 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4057 | compiler_call(struct compiler *c, expr_ty e) |
| 4058 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4059 | int ret = maybe_optimize_method_call(c, e); |
| 4060 | if (ret >= 0) { |
| 4061 | return ret; |
| 4062 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4063 | if (!check_caller(c, e->v.Call.func)) { |
| 4064 | return 0; |
| 4065 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4066 | VISIT(c, expr, e->v.Call.func); |
| 4067 | return compiler_call_helper(c, 0, |
| 4068 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4069 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4070 | } |
| 4071 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4072 | static int |
| 4073 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4074 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4075 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4076 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4077 | 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] | 4078 | return 1; |
| 4079 | } |
| 4080 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4081 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4082 | static int |
| 4083 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4084 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4085 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4086 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4087 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4088 | Convert the conversion char to 3 bits: |
| 4089 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4090 | !s : 001 0x1 FVC_STR |
| 4091 | !r : 010 0x2 FVC_REPR |
| 4092 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4093 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4094 | next bit is whether or not we have a format spec: |
| 4095 | yes : 100 0x4 |
| 4096 | no : 000 0x0 |
| 4097 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4098 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4099 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4100 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4101 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4102 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4103 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4104 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4105 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4106 | case 's': oparg = FVC_STR; break; |
| 4107 | case 'r': oparg = FVC_REPR; break; |
| 4108 | case 'a': oparg = FVC_ASCII; break; |
| 4109 | case -1: oparg = FVC_NONE; break; |
| 4110 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4111 | PyErr_Format(PyExc_SystemError, |
| 4112 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4113 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4114 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4115 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4116 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4117 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4118 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4119 | } |
| 4120 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4121 | /* And push our opcode and oparg */ |
| 4122 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4123 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4124 | return 1; |
| 4125 | } |
| 4126 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4127 | static int |
| 4128 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 4129 | { |
| 4130 | Py_ssize_t i, n = end - begin; |
| 4131 | keyword_ty kw; |
| 4132 | PyObject *keys, *key; |
| 4133 | assert(n > 0); |
| 4134 | if (n > 1) { |
| 4135 | for (i = begin; i < end; i++) { |
| 4136 | kw = asdl_seq_GET(keywords, i); |
| 4137 | VISIT(c, expr, kw->value); |
| 4138 | } |
| 4139 | keys = PyTuple_New(n); |
| 4140 | if (keys == NULL) { |
| 4141 | return 0; |
| 4142 | } |
| 4143 | for (i = begin; i < end; i++) { |
| 4144 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4145 | Py_INCREF(key); |
| 4146 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4147 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4148 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4149 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4150 | } |
| 4151 | else { |
| 4152 | /* a for loop only executes once */ |
| 4153 | for (i = begin; i < end; i++) { |
| 4154 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4155 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4156 | VISIT(c, expr, kw->value); |
| 4157 | } |
| 4158 | ADDOP_I(c, BUILD_MAP, n); |
| 4159 | } |
| 4160 | return 1; |
| 4161 | } |
| 4162 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4163 | /* shared code between compiler_call and compiler_class */ |
| 4164 | static int |
| 4165 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4166 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4167 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4168 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4169 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4170 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4171 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4172 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4173 | nkwelts = asdl_seq_LEN(keywords); |
| 4174 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4175 | for (i = 0; i < nelts; i++) { |
| 4176 | expr_ty elt = asdl_seq_GET(args, i); |
| 4177 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4178 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4179 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4180 | } |
| 4181 | for (i = 0; i < nkwelts; i++) { |
| 4182 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4183 | if (kw->arg == NULL) { |
| 4184 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4185 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4186 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4187 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4188 | /* No * or ** args, so can use faster calling sequence */ |
| 4189 | for (i = 0; i < nelts; i++) { |
| 4190 | expr_ty elt = asdl_seq_GET(args, i); |
| 4191 | assert(elt->kind != Starred_kind); |
| 4192 | VISIT(c, expr, elt); |
| 4193 | } |
| 4194 | if (nkwelts) { |
| 4195 | PyObject *names; |
| 4196 | VISIT_SEQ(c, keyword, keywords); |
| 4197 | names = PyTuple_New(nkwelts); |
| 4198 | if (names == NULL) { |
| 4199 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4200 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4201 | for (i = 0; i < nkwelts; i++) { |
| 4202 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4203 | Py_INCREF(kw->arg); |
| 4204 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4205 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4206 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4207 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4208 | return 1; |
| 4209 | } |
| 4210 | else { |
| 4211 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4212 | return 1; |
| 4213 | } |
| 4214 | |
| 4215 | ex_call: |
| 4216 | |
| 4217 | /* Do positional arguments. */ |
| 4218 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4219 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4220 | } |
| 4221 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4222 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4223 | return 0; |
| 4224 | } |
| 4225 | /* Then keyword arguments */ |
| 4226 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4227 | /* Has a new dict been pushed */ |
| 4228 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4229 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4230 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4231 | for (i = 0; i < nkwelts; i++) { |
| 4232 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4233 | if (kw->arg == NULL) { |
| 4234 | /* A keyword argument unpacking. */ |
| 4235 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4236 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4237 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4238 | } |
| 4239 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4240 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4241 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4242 | if (!have_dict) { |
| 4243 | ADDOP_I(c, BUILD_MAP, 0); |
| 4244 | have_dict = 1; |
| 4245 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4246 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4247 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4248 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4249 | else { |
| 4250 | nseen++; |
| 4251 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4252 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4253 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4254 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4255 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4256 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4257 | } |
| 4258 | if (have_dict) { |
| 4259 | ADDOP_I(c, DICT_MERGE, 1); |
| 4260 | } |
| 4261 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4262 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4263 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4264 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4265 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4266 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4267 | } |
| 4268 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4269 | |
| 4270 | /* List and set comprehensions and generator expressions work by creating a |
| 4271 | nested function to perform the actual iteration. This means that the |
| 4272 | iteration variables don't leak into the current scope. |
| 4273 | The defined function is called immediately following its definition, with the |
| 4274 | result of that call being the result of the expression. |
| 4275 | The LC/SC version returns the populated container, while the GE version is |
| 4276 | flagged in symtable.c as a generator, so it returns the generator object |
| 4277 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4278 | |
| 4279 | Possible cleanups: |
| 4280 | - iterate over the generator sequence instead of using recursion |
| 4281 | */ |
| 4282 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4283 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4284 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4285 | compiler_comprehension_generator(struct compiler *c, |
| 4286 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4287 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4288 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4289 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4290 | comprehension_ty gen; |
| 4291 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4292 | if (gen->is_async) { |
| 4293 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4294 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4295 | } else { |
| 4296 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4297 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4298 | } |
| 4299 | } |
| 4300 | |
| 4301 | static int |
| 4302 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4303 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4304 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4305 | expr_ty elt, expr_ty val, int type) |
| 4306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4307 | /* generate code for the iterator, then each of the ifs, |
| 4308 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4310 | comprehension_ty gen; |
| 4311 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4312 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4314 | start = compiler_new_block(c); |
| 4315 | skip = compiler_new_block(c); |
| 4316 | if_cleanup = compiler_new_block(c); |
| 4317 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4318 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4319 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4320 | anchor == NULL) |
| 4321 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4322 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4323 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4324 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4325 | if (gen_index == 0) { |
| 4326 | /* Receive outermost iter as an implicit argument */ |
| 4327 | c->u->u_argcount = 1; |
| 4328 | ADDOP_I(c, LOAD_FAST, 0); |
| 4329 | } |
| 4330 | else { |
| 4331 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4332 | /* Fast path for the temporary variable assignment idiom: |
| 4333 | for y in [f(x)] |
| 4334 | */ |
| 4335 | asdl_seq *elts; |
| 4336 | switch (gen->iter->kind) { |
| 4337 | case List_kind: |
| 4338 | elts = gen->iter->v.List.elts; |
| 4339 | break; |
| 4340 | case Tuple_kind: |
| 4341 | elts = gen->iter->v.Tuple.elts; |
| 4342 | break; |
| 4343 | default: |
| 4344 | elts = NULL; |
| 4345 | } |
| 4346 | if (asdl_seq_LEN(elts) == 1) { |
| 4347 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4348 | if (elt->kind != Starred_kind) { |
| 4349 | VISIT(c, expr, elt); |
| 4350 | start = NULL; |
| 4351 | } |
| 4352 | } |
| 4353 | if (start) { |
| 4354 | VISIT(c, expr, gen->iter); |
| 4355 | ADDOP(c, GET_ITER); |
| 4356 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4357 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4358 | if (start) { |
| 4359 | depth++; |
| 4360 | compiler_use_next_block(c, start); |
| 4361 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4362 | NEXT_BLOCK(c); |
| 4363 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4364 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4365 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4366 | /* XXX this needs to be cleaned up...a lot! */ |
| 4367 | n = asdl_seq_LEN(gen->ifs); |
| 4368 | for (i = 0; i < n; i++) { |
| 4369 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4370 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4371 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4372 | NEXT_BLOCK(c); |
| 4373 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4374 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4375 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4376 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4377 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4378 | elt, val, type)) |
| 4379 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4380 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4381 | /* only append after the last for generator */ |
| 4382 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4383 | /* comprehension specific code */ |
| 4384 | switch (type) { |
| 4385 | case COMP_GENEXP: |
| 4386 | VISIT(c, expr, elt); |
| 4387 | ADDOP(c, YIELD_VALUE); |
| 4388 | ADDOP(c, POP_TOP); |
| 4389 | break; |
| 4390 | case COMP_LISTCOMP: |
| 4391 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4392 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4393 | break; |
| 4394 | case COMP_SETCOMP: |
| 4395 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4396 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4397 | break; |
| 4398 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4399 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4400 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4401 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4402 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4403 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4404 | break; |
| 4405 | default: |
| 4406 | return 0; |
| 4407 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4408 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4409 | compiler_use_next_block(c, skip); |
| 4410 | } |
| 4411 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4412 | if (start) { |
| 4413 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4414 | compiler_use_next_block(c, anchor); |
| 4415 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4416 | |
| 4417 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4418 | } |
| 4419 | |
| 4420 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4421 | compiler_async_comprehension_generator(struct compiler *c, |
| 4422 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4423 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4424 | expr_ty elt, expr_ty val, int type) |
| 4425 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4426 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4427 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4428 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4429 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4430 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4431 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4432 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4433 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4434 | return 0; |
| 4435 | } |
| 4436 | |
| 4437 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4438 | |
| 4439 | if (gen_index == 0) { |
| 4440 | /* Receive outermost iter as an implicit argument */ |
| 4441 | c->u->u_argcount = 1; |
| 4442 | ADDOP_I(c, LOAD_FAST, 0); |
| 4443 | } |
| 4444 | else { |
| 4445 | /* Sub-iter - calculate on the fly */ |
| 4446 | VISIT(c, expr, gen->iter); |
| 4447 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4448 | } |
| 4449 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4450 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4451 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4452 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4453 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4454 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4455 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4456 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4457 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4458 | |
| 4459 | n = asdl_seq_LEN(gen->ifs); |
| 4460 | for (i = 0; i < n; i++) { |
| 4461 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4462 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4463 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4464 | NEXT_BLOCK(c); |
| 4465 | } |
| 4466 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4467 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4468 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4469 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4470 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4471 | elt, val, type)) |
| 4472 | return 0; |
| 4473 | |
| 4474 | /* only append after the last for generator */ |
| 4475 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4476 | /* comprehension specific code */ |
| 4477 | switch (type) { |
| 4478 | case COMP_GENEXP: |
| 4479 | VISIT(c, expr, elt); |
| 4480 | ADDOP(c, YIELD_VALUE); |
| 4481 | ADDOP(c, POP_TOP); |
| 4482 | break; |
| 4483 | case COMP_LISTCOMP: |
| 4484 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4485 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4486 | break; |
| 4487 | case COMP_SETCOMP: |
| 4488 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4489 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4490 | break; |
| 4491 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4492 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4493 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4494 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4495 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4496 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4497 | break; |
| 4498 | default: |
| 4499 | return 0; |
| 4500 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4501 | } |
| 4502 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4503 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4504 | |
| 4505 | compiler_use_next_block(c, except); |
| 4506 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4507 | |
| 4508 | return 1; |
| 4509 | } |
| 4510 | |
| 4511 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4512 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4513 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4514 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4515 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4516 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4517 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4518 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4519 | int is_async_generator = 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4520 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4521 | if (IS_TOP_LEVEL_AWAIT(c)) { |
| 4522 | c->u->u_ste->ste_coroutine = 1; |
| 4523 | } |
| 4524 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4525 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4526 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4527 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4528 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4529 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4530 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4534 | |
Yury Selivanov | b8ab9d3 | 2017-10-06 02:58:28 -0400 | [diff] [blame] | 4535 | if (is_async_generator && !is_async_function && type != COMP_GENEXP) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4536 | compiler_error(c, "asynchronous comprehension outside of " |
| 4537 | "an asynchronous function"); |
| 4538 | goto error_in_scope; |
| 4539 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4540 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4541 | if (type != COMP_GENEXP) { |
| 4542 | int op; |
| 4543 | switch (type) { |
| 4544 | case COMP_LISTCOMP: |
| 4545 | op = BUILD_LIST; |
| 4546 | break; |
| 4547 | case COMP_SETCOMP: |
| 4548 | op = BUILD_SET; |
| 4549 | break; |
| 4550 | case COMP_DICTCOMP: |
| 4551 | op = BUILD_MAP; |
| 4552 | break; |
| 4553 | default: |
| 4554 | PyErr_Format(PyExc_SystemError, |
| 4555 | "unknown comprehension type %d", type); |
| 4556 | goto error_in_scope; |
| 4557 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4558 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4559 | ADDOP_I(c, op, 0); |
| 4560 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4561 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4562 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4563 | val, type)) |
| 4564 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4565 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4566 | if (type != COMP_GENEXP) { |
| 4567 | ADDOP(c, RETURN_VALUE); |
| 4568 | } |
| 4569 | |
| 4570 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4571 | qualname = c->u->u_qualname; |
| 4572 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4573 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4574 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4575 | goto error; |
| 4576 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4577 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4578 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4579 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4580 | Py_DECREF(co); |
| 4581 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4582 | VISIT(c, expr, outermost->iter); |
| 4583 | |
| 4584 | if (outermost->is_async) { |
| 4585 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4586 | } else { |
| 4587 | ADDOP(c, GET_ITER); |
| 4588 | } |
| 4589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4590 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4591 | |
| 4592 | if (is_async_generator && type != COMP_GENEXP) { |
| 4593 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4594 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4595 | ADDOP(c, YIELD_FROM); |
| 4596 | } |
| 4597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4598 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4599 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4600 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4601 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4602 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4603 | Py_XDECREF(co); |
| 4604 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4605 | } |
| 4606 | |
| 4607 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4608 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4609 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4610 | static identifier name; |
| 4611 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4612 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4613 | if (!name) |
| 4614 | return 0; |
| 4615 | } |
| 4616 | assert(e->kind == GeneratorExp_kind); |
| 4617 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4618 | e->v.GeneratorExp.generators, |
| 4619 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4620 | } |
| 4621 | |
| 4622 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4623 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4624 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4625 | static identifier name; |
| 4626 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4627 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4628 | if (!name) |
| 4629 | return 0; |
| 4630 | } |
| 4631 | assert(e->kind == ListComp_kind); |
| 4632 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4633 | e->v.ListComp.generators, |
| 4634 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4635 | } |
| 4636 | |
| 4637 | static int |
| 4638 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4639 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4640 | static identifier name; |
| 4641 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4642 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4643 | if (!name) |
| 4644 | return 0; |
| 4645 | } |
| 4646 | assert(e->kind == SetComp_kind); |
| 4647 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4648 | e->v.SetComp.generators, |
| 4649 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4650 | } |
| 4651 | |
| 4652 | |
| 4653 | static int |
| 4654 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4655 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4656 | static identifier name; |
| 4657 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4658 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4659 | if (!name) |
| 4660 | return 0; |
| 4661 | } |
| 4662 | assert(e->kind == DictComp_kind); |
| 4663 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4664 | e->v.DictComp.generators, |
| 4665 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4666 | } |
| 4667 | |
| 4668 | |
| 4669 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4670 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4671 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4672 | VISIT(c, expr, k->value); |
| 4673 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4674 | } |
| 4675 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4676 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4677 | whether they are true or false. |
| 4678 | |
| 4679 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4680 | */ |
| 4681 | |
| 4682 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4683 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4684 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4685 | if (e->kind == Constant_kind) { |
| 4686 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4687 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4688 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4689 | } |
| 4690 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4691 | static int |
| 4692 | compiler_with_except_finish(struct compiler *c) { |
| 4693 | basicblock *exit; |
| 4694 | exit = compiler_new_block(c); |
| 4695 | if (exit == NULL) |
| 4696 | return 0; |
| 4697 | ADDOP_JABS(c, POP_JUMP_IF_TRUE, exit); |
| 4698 | ADDOP(c, RERAISE); |
| 4699 | compiler_use_next_block(c, exit); |
| 4700 | ADDOP(c, POP_TOP); |
| 4701 | ADDOP(c, POP_TOP); |
| 4702 | ADDOP(c, POP_TOP); |
| 4703 | ADDOP(c, POP_EXCEPT); |
| 4704 | ADDOP(c, POP_TOP); |
| 4705 | return 1; |
| 4706 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4707 | |
| 4708 | /* |
| 4709 | Implements the async with statement. |
| 4710 | |
| 4711 | The semantics outlined in that PEP are as follows: |
| 4712 | |
| 4713 | async with EXPR as VAR: |
| 4714 | BLOCK |
| 4715 | |
| 4716 | It is implemented roughly as: |
| 4717 | |
| 4718 | context = EXPR |
| 4719 | exit = context.__aexit__ # not calling it |
| 4720 | value = await context.__aenter__() |
| 4721 | try: |
| 4722 | VAR = value # if VAR present in the syntax |
| 4723 | BLOCK |
| 4724 | finally: |
| 4725 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4726 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4727 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4728 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4729 | if not (await exit(*exc)): |
| 4730 | raise |
| 4731 | */ |
| 4732 | static int |
| 4733 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4734 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4735 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4736 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4737 | |
| 4738 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4739 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4740 | c->u->u_ste->ste_coroutine = 1; |
| 4741 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4742 | return compiler_error(c, "'async with' outside async function"); |
| 4743 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4744 | |
| 4745 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4746 | final = compiler_new_block(c); |
| 4747 | exit = compiler_new_block(c); |
| 4748 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4749 | return 0; |
| 4750 | |
| 4751 | /* Evaluate EXPR */ |
| 4752 | VISIT(c, expr, item->context_expr); |
| 4753 | |
| 4754 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4755 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4756 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4757 | ADDOP(c, YIELD_FROM); |
| 4758 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4759 | ADDOP_JREL(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4760 | |
| 4761 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4762 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4763 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4764 | return 0; |
| 4765 | } |
| 4766 | |
| 4767 | if (item->optional_vars) { |
| 4768 | VISIT(c, expr, item->optional_vars); |
| 4769 | } |
| 4770 | else { |
| 4771 | /* Discard result from context.__aenter__() */ |
| 4772 | ADDOP(c, POP_TOP); |
| 4773 | } |
| 4774 | |
| 4775 | pos++; |
| 4776 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4777 | /* BLOCK code */ |
| 4778 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4779 | else if (!compiler_async_with(c, s, pos)) |
| 4780 | return 0; |
| 4781 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4782 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4783 | ADDOP(c, POP_BLOCK); |
| 4784 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4785 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4786 | /* For successful outcome: |
| 4787 | * call __exit__(None, None, None) |
| 4788 | */ |
| 4789 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4790 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4791 | ADDOP(c, GET_AWAITABLE); |
| 4792 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4793 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4794 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4795 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4796 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4797 | ADDOP_JABS(c, JUMP_ABSOLUTE, exit); |
| 4798 | |
| 4799 | /* For exceptional outcome: */ |
| 4800 | compiler_use_next_block(c, final); |
| 4801 | |
| 4802 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4803 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4804 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4805 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4806 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4807 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4808 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4809 | return 1; |
| 4810 | } |
| 4811 | |
| 4812 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4813 | /* |
| 4814 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4815 | with EXPR as VAR: |
| 4816 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4817 | is implemented as: |
| 4818 | <code for EXPR> |
| 4819 | SETUP_WITH E |
| 4820 | <code to store to VAR> or POP_TOP |
| 4821 | <code for BLOCK> |
| 4822 | LOAD_CONST (None, None, None) |
| 4823 | CALL_FUNCTION_EX 0 |
| 4824 | JUMP_FORWARD EXIT |
| 4825 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4826 | POP_JUMP_IF_TRUE T: |
| 4827 | RERAISE |
| 4828 | T: POP_TOP * 3 (remove exception from stack) |
| 4829 | POP_EXCEPT |
| 4830 | POP_TOP |
| 4831 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4832 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4833 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4834 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4835 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4836 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4837 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4838 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4839 | |
| 4840 | assert(s->kind == With_kind); |
| 4841 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4842 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4843 | final = compiler_new_block(c); |
| 4844 | exit = compiler_new_block(c); |
| 4845 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4846 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4847 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4848 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4849 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4850 | /* Will push bound __exit__ */ |
| 4851 | ADDOP_JREL(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4852 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4853 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4854 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4855 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4856 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4857 | } |
| 4858 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4859 | if (item->optional_vars) { |
| 4860 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4861 | } |
| 4862 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4863 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4864 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4865 | } |
| 4866 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4867 | pos++; |
| 4868 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4869 | /* BLOCK code */ |
| 4870 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4871 | else if (!compiler_with(c, s, pos)) |
| 4872 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4873 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4874 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4875 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4876 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4877 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4878 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4879 | /* For successful outcome: |
| 4880 | * call __exit__(None, None, None) |
| 4881 | */ |
| 4882 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4883 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4884 | ADDOP(c, POP_TOP); |
| 4885 | ADDOP_JREL(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4886 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4887 | /* For exceptional outcome: */ |
| 4888 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4889 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4890 | ADDOP(c, WITH_EXCEPT_START); |
| 4891 | compiler_with_except_finish(c); |
| 4892 | |
| 4893 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4894 | return 1; |
| 4895 | } |
| 4896 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4897 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4898 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4899 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4900 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4901 | case NamedExpr_kind: |
| 4902 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4903 | ADDOP(c, DUP_TOP); |
| 4904 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4905 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4906 | case BoolOp_kind: |
| 4907 | return compiler_boolop(c, e); |
| 4908 | case BinOp_kind: |
| 4909 | VISIT(c, expr, e->v.BinOp.left); |
| 4910 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 4911 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4912 | break; |
| 4913 | case UnaryOp_kind: |
| 4914 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 4915 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 4916 | break; |
| 4917 | case Lambda_kind: |
| 4918 | return compiler_lambda(c, e); |
| 4919 | case IfExp_kind: |
| 4920 | return compiler_ifexp(c, e); |
| 4921 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4922 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4923 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4924 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4925 | case GeneratorExp_kind: |
| 4926 | return compiler_genexp(c, e); |
| 4927 | case ListComp_kind: |
| 4928 | return compiler_listcomp(c, e); |
| 4929 | case SetComp_kind: |
| 4930 | return compiler_setcomp(c, e); |
| 4931 | case DictComp_kind: |
| 4932 | return compiler_dictcomp(c, e); |
| 4933 | case Yield_kind: |
| 4934 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4935 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4936 | if (e->v.Yield.value) { |
| 4937 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4938 | } |
| 4939 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4940 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4941 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4942 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4943 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4944 | case YieldFrom_kind: |
| 4945 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 4946 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4947 | |
| 4948 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 4949 | return compiler_error(c, "'yield from' inside async function"); |
| 4950 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4951 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 4952 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4953 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 4954 | ADDOP(c, YIELD_FROM); |
| 4955 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4956 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4957 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4958 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 4959 | return compiler_error(c, "'await' outside function"); |
| 4960 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4961 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 4962 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4963 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 4964 | return compiler_error(c, "'await' outside async function"); |
| 4965 | } |
| 4966 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4967 | |
| 4968 | VISIT(c, expr, e->v.Await.value); |
| 4969 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4970 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4971 | ADDOP(c, YIELD_FROM); |
| 4972 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4973 | case Compare_kind: |
| 4974 | return compiler_compare(c, e); |
| 4975 | case Call_kind: |
| 4976 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4977 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4978 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 4979 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4980 | case JoinedStr_kind: |
| 4981 | return compiler_joined_str(c, e); |
| 4982 | case FormattedValue_kind: |
| 4983 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4984 | /* The following exprs can be assignment targets. */ |
| 4985 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 4986 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4987 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4988 | case Load: |
| 4989 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 4990 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4991 | case Store: |
| 4992 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 4993 | break; |
| 4994 | case Del: |
| 4995 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 4996 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4997 | } |
| 4998 | break; |
| 4999 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5000 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5001 | case Starred_kind: |
| 5002 | switch (e->v.Starred.ctx) { |
| 5003 | case Store: |
| 5004 | /* In all legitimate cases, the Starred node was already replaced |
| 5005 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5006 | return compiler_error(c, |
| 5007 | "starred assignment target must be in a list or tuple"); |
| 5008 | default: |
| 5009 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5010 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5011 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5012 | break; |
| 5013 | case Slice_kind: |
| 5014 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | case Name_kind: |
| 5016 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5017 | /* child nodes of List and Tuple will have expr_context set */ |
| 5018 | case List_kind: |
| 5019 | return compiler_list(c, e); |
| 5020 | case Tuple_kind: |
| 5021 | return compiler_tuple(c, e); |
| 5022 | } |
| 5023 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5024 | } |
| 5025 | |
| 5026 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5027 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5028 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5029 | int old_lineno = c->u->u_lineno; |
| 5030 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5031 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5032 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5033 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5034 | c->u->u_col_offset = old_col_offset; |
| 5035 | return res; |
| 5036 | } |
| 5037 | |
| 5038 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5039 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5040 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5041 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5042 | expr_ty e = s->v.AugAssign.target; |
| 5043 | |
| 5044 | int old_lineno = c->u->u_lineno; |
| 5045 | int old_col_offset = c->u->u_col_offset; |
| 5046 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5047 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5048 | switch (e->kind) { |
| 5049 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5050 | VISIT(c, expr, e->v.Attribute.value); |
| 5051 | ADDOP(c, DUP_TOP); |
| 5052 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5053 | break; |
| 5054 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5055 | VISIT(c, expr, e->v.Subscript.value); |
| 5056 | VISIT(c, expr, e->v.Subscript.slice); |
| 5057 | ADDOP(c, DUP_TOP_TWO); |
| 5058 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5059 | break; |
| 5060 | case Name_kind: |
| 5061 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5062 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5063 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5064 | default: |
| 5065 | PyErr_Format(PyExc_SystemError, |
| 5066 | "invalid node type (%d) for augmented assignment", |
| 5067 | e->kind); |
| 5068 | return 0; |
| 5069 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5070 | |
| 5071 | c->u->u_lineno = old_lineno; |
| 5072 | c->u->u_col_offset = old_col_offset; |
| 5073 | |
| 5074 | VISIT(c, expr, s->v.AugAssign.value); |
| 5075 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5076 | |
| 5077 | SET_LOC(c, e); |
| 5078 | |
| 5079 | switch (e->kind) { |
| 5080 | case Attribute_kind: |
| 5081 | ADDOP(c, ROT_TWO); |
| 5082 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5083 | break; |
| 5084 | case Subscript_kind: |
| 5085 | ADDOP(c, ROT_THREE); |
| 5086 | ADDOP(c, STORE_SUBSCR); |
| 5087 | break; |
| 5088 | case Name_kind: |
| 5089 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5090 | default: |
| 5091 | Py_UNREACHABLE(); |
| 5092 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5094 | } |
| 5095 | |
| 5096 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5097 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5098 | { |
| 5099 | VISIT(c, expr, e); |
| 5100 | ADDOP(c, POP_TOP); |
| 5101 | return 1; |
| 5102 | } |
| 5103 | |
| 5104 | static int |
| 5105 | check_annotation(struct compiler *c, stmt_ty s) |
| 5106 | { |
| 5107 | /* Annotations are only evaluated in a module or class. */ |
| 5108 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5109 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5110 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5111 | } |
| 5112 | return 1; |
| 5113 | } |
| 5114 | |
| 5115 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5116 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5117 | { |
| 5118 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5119 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5120 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5121 | if (e->v.Slice.lower && !check_ann_expr(c, e->v.Slice.lower)) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5122 | return 0; |
| 5123 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5124 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5125 | return 0; |
| 5126 | } |
| 5127 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5128 | return 0; |
| 5129 | } |
| 5130 | return 1; |
| 5131 | case Tuple_kind: { |
| 5132 | /* extended slice */ |
| 5133 | asdl_seq *elts = e->v.Tuple.elts; |
| 5134 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5135 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5136 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5137 | return 0; |
| 5138 | } |
| 5139 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5140 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5141 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5142 | default: |
| 5143 | return check_ann_expr(c, e); |
| 5144 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5145 | } |
| 5146 | |
| 5147 | static int |
| 5148 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5149 | { |
| 5150 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5151 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5152 | |
| 5153 | assert(s->kind == AnnAssign_kind); |
| 5154 | |
| 5155 | /* We perform the actual assignment first. */ |
| 5156 | if (s->v.AnnAssign.value) { |
| 5157 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5158 | VISIT(c, expr, targ); |
| 5159 | } |
| 5160 | switch (targ->kind) { |
| 5161 | case Name_kind: |
| 5162 | /* If we have a simple name in a module or class, store annotation. */ |
| 5163 | if (s->v.AnnAssign.simple && |
| 5164 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5165 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5166 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5167 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5168 | } |
| 5169 | else { |
| 5170 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5171 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5172 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5173 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5174 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5175 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5176 | } |
| 5177 | break; |
| 5178 | case Attribute_kind: |
| 5179 | if (!s->v.AnnAssign.value && |
| 5180 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5181 | return 0; |
| 5182 | } |
| 5183 | break; |
| 5184 | case Subscript_kind: |
| 5185 | if (!s->v.AnnAssign.value && |
| 5186 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5187 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5188 | return 0; |
| 5189 | } |
| 5190 | break; |
| 5191 | default: |
| 5192 | PyErr_Format(PyExc_SystemError, |
| 5193 | "invalid node type (%d) for annotated assignment", |
| 5194 | targ->kind); |
| 5195 | return 0; |
| 5196 | } |
| 5197 | /* Annotation is evaluated last. */ |
| 5198 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5199 | return 0; |
| 5200 | } |
| 5201 | return 1; |
| 5202 | } |
| 5203 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5204 | /* Raises a SyntaxError and returns 0. |
| 5205 | If something goes wrong, a different exception may be raised. |
| 5206 | */ |
| 5207 | |
| 5208 | static int |
| 5209 | compiler_error(struct compiler *c, const char *errstr) |
| 5210 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5211 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5212 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5213 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5214 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5215 | if (!loc) { |
| 5216 | Py_INCREF(Py_None); |
| 5217 | loc = Py_None; |
| 5218 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5219 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5220 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5221 | if (!u) |
| 5222 | goto exit; |
| 5223 | v = Py_BuildValue("(zO)", errstr, u); |
| 5224 | if (!v) |
| 5225 | goto exit; |
| 5226 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5227 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5228 | Py_DECREF(loc); |
| 5229 | Py_XDECREF(u); |
| 5230 | Py_XDECREF(v); |
| 5231 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5232 | } |
| 5233 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5234 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5235 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5236 | and returns 0. |
| 5237 | */ |
| 5238 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5239 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5240 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5241 | va_list vargs; |
| 5242 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5243 | va_start(vargs, format); |
| 5244 | #else |
| 5245 | va_start(vargs); |
| 5246 | #endif |
| 5247 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5248 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5249 | if (msg == NULL) { |
| 5250 | return 0; |
| 5251 | } |
| 5252 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5253 | c->u->u_lineno, NULL, NULL) < 0) |
| 5254 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5255 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5256 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5257 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5258 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5259 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5260 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5261 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5262 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5263 | return 0; |
| 5264 | } |
| 5265 | Py_DECREF(msg); |
| 5266 | return 1; |
| 5267 | } |
| 5268 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5269 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5270 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5271 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5272 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5273 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5274 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5275 | if (ctx == Load) { |
| 5276 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5277 | return 0; |
| 5278 | } |
| 5279 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5280 | return 0; |
| 5281 | } |
| 5282 | } |
| 5283 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5284 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5285 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5286 | case Store: op = STORE_SUBSCR; break; |
| 5287 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5288 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5289 | assert(op); |
| 5290 | VISIT(c, expr, e->v.Subscript.value); |
| 5291 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5292 | ADDOP(c, op); |
| 5293 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5294 | } |
| 5295 | |
| 5296 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5297 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5298 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5299 | int n = 2; |
| 5300 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5301 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5302 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5303 | if (s->v.Slice.lower) { |
| 5304 | VISIT(c, expr, s->v.Slice.lower); |
| 5305 | } |
| 5306 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5307 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5309 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5310 | if (s->v.Slice.upper) { |
| 5311 | VISIT(c, expr, s->v.Slice.upper); |
| 5312 | } |
| 5313 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5314 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5315 | } |
| 5316 | |
| 5317 | if (s->v.Slice.step) { |
| 5318 | n++; |
| 5319 | VISIT(c, expr, s->v.Slice.step); |
| 5320 | } |
| 5321 | ADDOP_I(c, BUILD_SLICE, n); |
| 5322 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5323 | } |
| 5324 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5325 | /* End of the compiler section, beginning of the assembler section */ |
| 5326 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5327 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5328 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5329 | |
| 5330 | XXX must handle implicit jumps from one block to next |
| 5331 | */ |
| 5332 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5333 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5334 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5335 | int a_offset; /* offset into bytecode */ |
| 5336 | int a_nblocks; /* number of reachable blocks */ |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5337 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5338 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5339 | int a_lnotab_off; /* offset into lnotab */ |
| 5340 | int a_lineno; /* last lineno of emitted instruction */ |
| 5341 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5342 | }; |
| 5343 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5344 | static void |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5345 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5346 | { |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5347 | int i, j; |
| 5348 | |
| 5349 | /* Get rid of recursion for normal control flow. |
| 5350 | Since the number of blocks is limited, unused space in a_postorder |
| 5351 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5352 | blocks. */ |
| 5353 | for (j = end; b && !b->b_seen; b = b->b_next) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5354 | b->b_seen = 1; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5355 | assert(a->a_nblocks < j); |
| 5356 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5357 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5358 | while (j < end) { |
| 5359 | b = a->a_postorder[j++]; |
| 5360 | for (i = 0; i < b->b_iused; i++) { |
| 5361 | struct instr *instr = &b->b_instr[i]; |
| 5362 | if (instr->i_jrel || instr->i_jabs) |
| 5363 | dfs(c, instr->i_target, a, j); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5364 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5365 | assert(a->a_nblocks < j); |
| 5366 | a->a_postorder[a->a_nblocks++] = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5367 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5368 | } |
| 5369 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5370 | Py_LOCAL_INLINE(void) |
| 5371 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5372 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5373 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5374 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5375 | assert(b->b_startdepth < 0); |
| 5376 | b->b_startdepth = depth; |
| 5377 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5378 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5379 | } |
| 5380 | |
| 5381 | /* Find the flow path that needs the largest stack. We assume that |
| 5382 | * cycles in the flow graph have no net effect on the stack depth. |
| 5383 | */ |
| 5384 | static int |
| 5385 | stackdepth(struct compiler *c) |
| 5386 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5387 | basicblock *b, *entryblock = NULL; |
| 5388 | basicblock **stack, **sp; |
| 5389 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5390 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5391 | b->b_startdepth = INT_MIN; |
| 5392 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5393 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5394 | } |
| 5395 | if (!entryblock) |
| 5396 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5397 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5398 | if (!stack) { |
| 5399 | PyErr_NoMemory(); |
| 5400 | return -1; |
| 5401 | } |
| 5402 | |
| 5403 | sp = stack; |
| 5404 | stackdepth_push(&sp, entryblock, 0); |
| 5405 | while (sp != stack) { |
| 5406 | b = *--sp; |
| 5407 | int depth = b->b_startdepth; |
| 5408 | assert(depth >= 0); |
| 5409 | basicblock *next = b->b_next; |
| 5410 | for (int i = 0; i < b->b_iused; i++) { |
| 5411 | struct instr *instr = &b->b_instr[i]; |
| 5412 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5413 | if (effect == PY_INVALID_STACK_EFFECT) { |
| 5414 | fprintf(stderr, "opcode = %d\n", instr->i_opcode); |
| 5415 | Py_FatalError("PyCompile_OpcodeStackEffect()"); |
| 5416 | } |
| 5417 | int new_depth = depth + effect; |
| 5418 | if (new_depth > maxdepth) { |
| 5419 | maxdepth = new_depth; |
| 5420 | } |
| 5421 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5422 | if (instr->i_jrel || instr->i_jabs) { |
| 5423 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5424 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5425 | int target_depth = depth + effect; |
| 5426 | if (target_depth > maxdepth) { |
| 5427 | maxdepth = target_depth; |
| 5428 | } |
| 5429 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5430 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5431 | } |
| 5432 | depth = new_depth; |
| 5433 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5434 | instr->i_opcode == JUMP_FORWARD || |
| 5435 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5436 | instr->i_opcode == RAISE_VARARGS || |
| 5437 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5438 | { |
| 5439 | /* remaining code is dead */ |
| 5440 | next = NULL; |
| 5441 | break; |
| 5442 | } |
| 5443 | } |
| 5444 | if (next != NULL) { |
| 5445 | stackdepth_push(&sp, next, depth); |
| 5446 | } |
| 5447 | } |
| 5448 | PyObject_Free(stack); |
| 5449 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5450 | } |
| 5451 | |
| 5452 | static int |
| 5453 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5454 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5455 | memset(a, 0, sizeof(struct assembler)); |
| 5456 | a->a_lineno = firstlineno; |
| 5457 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5458 | if (!a->a_bytecode) |
| 5459 | return 0; |
| 5460 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5461 | if (!a->a_lnotab) |
| 5462 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5463 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5464 | PyErr_NoMemory(); |
| 5465 | return 0; |
| 5466 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5467 | a->a_postorder = (basicblock **)PyObject_Malloc( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5468 | sizeof(basicblock *) * nblocks); |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5469 | if (!a->a_postorder) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5470 | PyErr_NoMemory(); |
| 5471 | return 0; |
| 5472 | } |
| 5473 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5474 | } |
| 5475 | |
| 5476 | static void |
| 5477 | assemble_free(struct assembler *a) |
| 5478 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5479 | Py_XDECREF(a->a_bytecode); |
| 5480 | Py_XDECREF(a->a_lnotab); |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5481 | if (a->a_postorder) |
| 5482 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5483 | } |
| 5484 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5485 | static int |
| 5486 | blocksize(basicblock *b) |
| 5487 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5488 | int i; |
| 5489 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5490 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5491 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5492 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5493 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5494 | } |
| 5495 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5496 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5497 | the instruction's bytecode offset and line number. See |
| 5498 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5499 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5500 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5501 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5502 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5503 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5504 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5505 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5506 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5507 | d_lineno = i->i_lineno - a->a_lineno; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5508 | if (d_lineno == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5509 | return 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
| 5513 | assert(d_bytecode >= 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5514 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5515 | if (d_bytecode > 255) { |
| 5516 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5517 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5518 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5519 | if (nbytes >= len) { |
| 5520 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5521 | len = nbytes; |
| 5522 | else if (len <= INT_MAX / 2) |
| 5523 | len *= 2; |
| 5524 | else { |
| 5525 | PyErr_NoMemory(); |
| 5526 | return 0; |
| 5527 | } |
| 5528 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5529 | return 0; |
| 5530 | } |
| 5531 | lnotab = (unsigned char *) |
| 5532 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5533 | for (j = 0; j < ncodes; j++) { |
| 5534 | *lnotab++ = 255; |
| 5535 | *lnotab++ = 0; |
| 5536 | } |
| 5537 | d_bytecode -= ncodes * 255; |
| 5538 | a->a_lnotab_off += ncodes * 2; |
| 5539 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5540 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5541 | |
| 5542 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5543 | int j, nbytes, ncodes, k; |
| 5544 | if (d_lineno < 0) { |
| 5545 | k = -128; |
| 5546 | /* use division on positive numbers */ |
| 5547 | ncodes = (-d_lineno) / 128; |
| 5548 | } |
| 5549 | else { |
| 5550 | k = 127; |
| 5551 | ncodes = d_lineno / 127; |
| 5552 | } |
| 5553 | d_lineno -= ncodes * k; |
| 5554 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5555 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5556 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5557 | if (nbytes >= len) { |
| 5558 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5559 | len = nbytes; |
| 5560 | else if (len <= INT_MAX / 2) |
| 5561 | len *= 2; |
| 5562 | else { |
| 5563 | PyErr_NoMemory(); |
| 5564 | return 0; |
| 5565 | } |
| 5566 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5567 | return 0; |
| 5568 | } |
| 5569 | lnotab = (unsigned char *) |
| 5570 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5571 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5572 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5573 | d_bytecode = 0; |
| 5574 | for (j = 1; j < ncodes; j++) { |
| 5575 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5576 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5577 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5578 | a->a_lnotab_off += ncodes * 2; |
| 5579 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5580 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5581 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5582 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5583 | if (a->a_lnotab_off + 2 >= len) { |
| 5584 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5585 | return 0; |
| 5586 | } |
| 5587 | lnotab = (unsigned char *) |
| 5588 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5589 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5590 | a->a_lnotab_off += 2; |
| 5591 | if (d_bytecode) { |
| 5592 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5593 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5594 | } |
| 5595 | else { /* First line of a block; def stmt, etc. */ |
| 5596 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5597 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5598 | } |
| 5599 | a->a_lineno = i->i_lineno; |
| 5600 | a->a_lineno_off = a->a_offset; |
| 5601 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5602 | } |
| 5603 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5604 | /* assemble_emit() |
| 5605 | Extend the bytecode with a new instruction. |
| 5606 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5607 | */ |
| 5608 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5609 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5610 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5611 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5612 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5613 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5614 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5615 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5616 | arg = i->i_oparg; |
| 5617 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5618 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5619 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5620 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5621 | if (len > PY_SSIZE_T_MAX / 2) |
| 5622 | return 0; |
| 5623 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5624 | return 0; |
| 5625 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5626 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5627 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5628 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5629 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5630 | } |
| 5631 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5632 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5633 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5634 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5635 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5636 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5637 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5638 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5639 | /* Compute the size of each block and fixup jump args. |
| 5640 | Replace block pointer with position in bytecode. */ |
| 5641 | do { |
| 5642 | totsize = 0; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5643 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5644 | b = a->a_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5645 | bsize = blocksize(b); |
| 5646 | b->b_offset = totsize; |
| 5647 | totsize += bsize; |
| 5648 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5649 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5650 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5651 | bsize = b->b_offset; |
| 5652 | for (i = 0; i < b->b_iused; i++) { |
| 5653 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5654 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5655 | /* Relative jumps are computed relative to |
| 5656 | the instruction pointer after fetching |
| 5657 | the jump instruction. |
| 5658 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5659 | bsize += isize; |
| 5660 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5661 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5662 | if (instr->i_jrel) { |
| 5663 | instr->i_oparg -= bsize; |
| 5664 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5665 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5666 | if (instrsize(instr->i_oparg) != isize) { |
| 5667 | extended_arg_recompile = 1; |
| 5668 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5669 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5670 | } |
| 5671 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5673 | /* XXX: This is an awful hack that could hurt performance, but |
| 5674 | on the bright side it should work until we come up |
| 5675 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5676 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5677 | The issue is that in the first loop blocksize() is called |
| 5678 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5679 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5680 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5681 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5682 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5683 | The only EXTENDED_ARGs that could be popping up are |
| 5684 | ones in jump instructions. So this should converge |
| 5685 | fairly quickly. |
| 5686 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5687 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5688 | } |
| 5689 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5690 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5691 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5692 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5693 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5694 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5695 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5696 | tuple = PyTuple_New(size); |
| 5697 | if (tuple == NULL) |
| 5698 | return NULL; |
| 5699 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5700 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5701 | Py_INCREF(k); |
| 5702 | assert((i - offset) < size); |
| 5703 | assert((i - offset) >= 0); |
| 5704 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5705 | } |
| 5706 | return tuple; |
| 5707 | } |
| 5708 | |
| 5709 | static PyObject * |
| 5710 | consts_dict_keys_inorder(PyObject *dict) |
| 5711 | { |
| 5712 | PyObject *consts, *k, *v; |
| 5713 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5714 | |
| 5715 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5716 | if (consts == NULL) |
| 5717 | return NULL; |
| 5718 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5719 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5720 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5721 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5722 | * the object we want is always second. */ |
| 5723 | if (PyTuple_CheckExact(k)) { |
| 5724 | k = PyTuple_GET_ITEM(k, 1); |
| 5725 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5726 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5727 | assert(i < size); |
| 5728 | assert(i >= 0); |
| 5729 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5730 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5731 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5732 | } |
| 5733 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5734 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5735 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5736 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5737 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5738 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5739 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5740 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5741 | if (ste->ste_nested) |
| 5742 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5743 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5744 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5745 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5746 | flags |= CO_COROUTINE; |
| 5747 | if (ste->ste_generator && ste->ste_coroutine) |
| 5748 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5749 | if (ste->ste_varargs) |
| 5750 | flags |= CO_VARARGS; |
| 5751 | if (ste->ste_varkeywords) |
| 5752 | flags |= CO_VARKEYWORDS; |
| 5753 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5755 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5756 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5757 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5758 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5759 | ste->ste_coroutine && |
| 5760 | !ste->ste_generator) { |
| 5761 | flags |= CO_COROUTINE; |
| 5762 | } |
| 5763 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5764 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5765 | } |
| 5766 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5767 | // Merge *tuple* with constant cache. |
| 5768 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5769 | static int |
| 5770 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5771 | { |
| 5772 | assert(PyTuple_CheckExact(*tuple)); |
| 5773 | |
| 5774 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5775 | if (key == NULL) { |
| 5776 | return 0; |
| 5777 | } |
| 5778 | |
| 5779 | // t is borrowed reference |
| 5780 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5781 | Py_DECREF(key); |
| 5782 | if (t == NULL) { |
| 5783 | return 0; |
| 5784 | } |
| 5785 | if (t == key) { // tuple is new constant. |
| 5786 | return 1; |
| 5787 | } |
| 5788 | |
| 5789 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5790 | Py_INCREF(u); |
| 5791 | Py_DECREF(*tuple); |
| 5792 | *tuple = u; |
| 5793 | return 1; |
| 5794 | } |
| 5795 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5796 | static PyCodeObject * |
| 5797 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5798 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5799 | PyObject *tmp; |
| 5800 | PyCodeObject *co = NULL; |
| 5801 | PyObject *consts = NULL; |
| 5802 | PyObject *names = NULL; |
| 5803 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5804 | PyObject *name = NULL; |
| 5805 | PyObject *freevars = NULL; |
| 5806 | PyObject *cellvars = NULL; |
| 5807 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5808 | Py_ssize_t nlocals; |
| 5809 | int nlocals_int; |
| 5810 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5811 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5812 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5813 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5814 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5815 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5816 | if (!consts || !names || !varnames) |
| 5817 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5818 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5819 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5820 | if (!cellvars) |
| 5821 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5822 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5823 | if (!freevars) |
| 5824 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5825 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5826 | if (!merge_const_tuple(c, &names) || |
| 5827 | !merge_const_tuple(c, &varnames) || |
| 5828 | !merge_const_tuple(c, &cellvars) || |
| 5829 | !merge_const_tuple(c, &freevars)) |
| 5830 | { |
| 5831 | goto error; |
| 5832 | } |
| 5833 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5834 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5835 | assert(nlocals < INT_MAX); |
| 5836 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5837 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5838 | flags = compute_code_flags(c); |
| 5839 | if (flags < 0) |
| 5840 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5841 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5842 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5843 | if (!bytecode) |
| 5844 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5845 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5846 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5847 | if (!tmp) |
| 5848 | goto error; |
| 5849 | Py_DECREF(consts); |
| 5850 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5851 | if (!merge_const_tuple(c, &consts)) { |
| 5852 | goto error; |
| 5853 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5854 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5855 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5856 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5857 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5858 | maxdepth = stackdepth(c); |
| 5859 | if (maxdepth < 0) { |
| 5860 | goto error; |
| 5861 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5862 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5863 | posonlyargcount, kwonlyargcount, nlocals_int, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5864 | maxdepth, flags, bytecode, consts, names, |
| 5865 | varnames, freevars, cellvars, c->c_filename, |
| 5866 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5867 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5868 | Py_XDECREF(consts); |
| 5869 | Py_XDECREF(names); |
| 5870 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5871 | Py_XDECREF(name); |
| 5872 | Py_XDECREF(freevars); |
| 5873 | Py_XDECREF(cellvars); |
| 5874 | Py_XDECREF(bytecode); |
| 5875 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5876 | } |
| 5877 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5878 | |
| 5879 | /* For debugging purposes only */ |
| 5880 | #if 0 |
| 5881 | static void |
| 5882 | dump_instr(const struct instr *i) |
| 5883 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5884 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5885 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5886 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5888 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5889 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5890 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5891 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5892 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5893 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5894 | } |
| 5895 | |
| 5896 | static void |
| 5897 | dump_basicblock(const basicblock *b) |
| 5898 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5899 | const char *seen = b->b_seen ? "seen " : ""; |
| 5900 | const char *b_return = b->b_return ? "return " : ""; |
| 5901 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5902 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5903 | if (b->b_instr) { |
| 5904 | int i; |
| 5905 | for (i = 0; i < b->b_iused; i++) { |
| 5906 | fprintf(stderr, " [%02d] ", i); |
| 5907 | dump_instr(b->b_instr + i); |
| 5908 | } |
| 5909 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5910 | } |
| 5911 | #endif |
| 5912 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5913 | static PyCodeObject * |
| 5914 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5915 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5916 | basicblock *b, *entryblock; |
| 5917 | struct assembler a; |
| 5918 | int i, j, nblocks; |
| 5919 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5921 | /* Make sure every block that falls off the end returns None. |
| 5922 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 5923 | block ends with a jump or return b_next shouldn't set. |
| 5924 | */ |
| 5925 | if (!c->u->u_curblock->b_return) { |
| 5926 | NEXT_BLOCK(c); |
| 5927 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5928 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5929 | ADDOP(c, RETURN_VALUE); |
| 5930 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5931 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5932 | nblocks = 0; |
| 5933 | entryblock = NULL; |
| 5934 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5935 | nblocks++; |
| 5936 | entryblock = b; |
| 5937 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5938 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5939 | /* Set firstlineno if it wasn't explicitly set. */ |
| 5940 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 5941 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 5943 | else |
| 5944 | c->u->u_firstlineno = 1; |
| 5945 | } |
| 5946 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 5947 | goto error; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5948 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5949 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5950 | /* Can't modify the bytecode after computing jump offsets. */ |
| 5951 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5952 | |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5953 | /* Emit code in reverse postorder from dfs. */ |
| 5954 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 5955 | b = a.a_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5956 | for (j = 0; j < b->b_iused; j++) |
| 5957 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 5958 | goto error; |
| 5959 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 5960 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5961 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 5962 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5963 | 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] | 5964 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5965 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5966 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5967 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5968 | assemble_free(&a); |
| 5969 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5970 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5971 | |
| 5972 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 5973 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 5974 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 5975 | PyArena *arena) |
| 5976 | { |
| 5977 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 5978 | } |