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 | |
Ammar Askar | e92d393 | 2020-01-15 11:48:40 -0500 | [diff] [blame] | 26 | #include "Python-ast.h" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 27 | #include "ast.h" |
| 28 | #include "code.h" |
Jeremy Hylton | 4b38da6 | 2001-02-02 18:19:15 +0000 | [diff] [blame] | 29 | #include "symtable.h" |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 30 | #include "opcode.h" |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 31 | #include "wordcode_helpers.h" |
Guido van Rossum | b05a5c7 | 1997-05-07 17:46:13 +0000 | [diff] [blame] | 32 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 33 | #define DEFAULT_BLOCK_SIZE 16 |
| 34 | #define DEFAULT_BLOCKS 8 |
| 35 | #define DEFAULT_CODE_SIZE 128 |
| 36 | #define DEFAULT_LNOTAB_SIZE 16 |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 37 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 38 | #define COMP_GENEXP 0 |
| 39 | #define COMP_LISTCOMP 1 |
| 40 | #define COMP_SETCOMP 2 |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 41 | #define COMP_DICTCOMP 3 |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 42 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 43 | #define IS_TOP_LEVEL_AWAIT(c) ( \ |
| 44 | (c->c_flags->cf_flags & PyCF_ALLOW_TOP_LEVEL_AWAIT) \ |
| 45 | && (c->u->u_ste->ste_type == ModuleBlock)) |
| 46 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 47 | struct instr { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 48 | unsigned i_jabs : 1; |
| 49 | unsigned i_jrel : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 50 | unsigned char i_opcode; |
| 51 | int i_oparg; |
| 52 | struct basicblock_ *i_target; /* target block (if jump instruction) */ |
| 53 | int i_lineno; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | }; |
| 55 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 56 | typedef struct basicblock_ { |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 57 | /* Each basicblock in a compilation unit is linked via b_list in the |
| 58 | reverse order that the block are allocated. b_list points to the next |
| 59 | block, not to be confused with b_next, which is next by control flow. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 60 | struct basicblock_ *b_list; |
| 61 | /* number of instructions used */ |
| 62 | int b_iused; |
| 63 | /* length of instruction array (b_instr) */ |
| 64 | int b_ialloc; |
| 65 | /* pointer to an array of instructions, initially NULL */ |
| 66 | struct instr *b_instr; |
| 67 | /* If b_next is non-NULL, it is a pointer to the next |
| 68 | block reached by normal control flow. */ |
| 69 | struct basicblock_ *b_next; |
| 70 | /* b_seen is used to perform a DFS of basicblocks. */ |
| 71 | unsigned b_seen : 1; |
| 72 | /* b_return is true if a RETURN_VALUE opcode is inserted. */ |
| 73 | unsigned b_return : 1; |
| 74 | /* depth of stack upon entry of block, computed by stackdepth() */ |
| 75 | int b_startdepth; |
| 76 | /* instruction offset for block, computed by assemble_jump_offsets() */ |
| 77 | int b_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 78 | } basicblock; |
| 79 | |
| 80 | /* fblockinfo tracks the current frame block. |
| 81 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 82 | A frame block is used to handle loops, try/except, and try/finally. |
| 83 | It's called a frame block to distinguish it from a basic block in the |
| 84 | compiler IR. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 85 | */ |
| 86 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 87 | enum fblocktype { WHILE_LOOP, FOR_LOOP, EXCEPT, FINALLY_TRY, FINALLY_END, |
| 88 | WITH, ASYNC_WITH, HANDLER_CLEANUP, POP_VALUE }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 89 | |
| 90 | struct fblockinfo { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 91 | enum fblocktype fb_type; |
| 92 | basicblock *fb_block; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 93 | /* (optional) type-specific exit or cleanup block */ |
| 94 | basicblock *fb_exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 95 | /* (optional) additional information required for unwinding */ |
| 96 | void *fb_datum; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 97 | }; |
| 98 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 99 | enum { |
| 100 | COMPILER_SCOPE_MODULE, |
| 101 | COMPILER_SCOPE_CLASS, |
| 102 | COMPILER_SCOPE_FUNCTION, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 103 | COMPILER_SCOPE_ASYNC_FUNCTION, |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 104 | COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 105 | COMPILER_SCOPE_COMPREHENSION, |
| 106 | }; |
| 107 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 108 | /* The following items change on entry and exit of code blocks. |
| 109 | They must be saved and restored when returning to a block. |
| 110 | */ |
| 111 | struct compiler_unit { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 112 | PySTEntryObject *u_ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 113 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | PyObject *u_name; |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 115 | PyObject *u_qualname; /* dot-separated qualified name (lazy) */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 116 | int u_scope_type; |
| 117 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 118 | /* The following fields are dicts that map objects to |
| 119 | the index of them in co_XXX. The index is used as |
| 120 | the argument for opcodes that refer to those collections. |
| 121 | */ |
| 122 | PyObject *u_consts; /* all constants */ |
| 123 | PyObject *u_names; /* all names */ |
| 124 | PyObject *u_varnames; /* local variables */ |
| 125 | PyObject *u_cellvars; /* cell variables */ |
| 126 | PyObject *u_freevars; /* free variables */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 128 | PyObject *u_private; /* for private name mangling */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 129 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 130 | Py_ssize_t u_argcount; /* number of arguments for block */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 131 | Py_ssize_t u_posonlyargcount; /* number of positional only arguments for block */ |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 132 | Py_ssize_t u_kwonlyargcount; /* number of keyword only arguments for block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 133 | /* Pointer to the most recently allocated block. By following b_list |
| 134 | members, you can reach all early allocated blocks. */ |
| 135 | basicblock *u_blocks; |
| 136 | basicblock *u_curblock; /* pointer to current block */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 137 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 138 | int u_nfblocks; |
| 139 | struct fblockinfo u_fblock[CO_MAXBLOCKS]; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 140 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 141 | int u_firstlineno; /* the first lineno of the block */ |
| 142 | int u_lineno; /* the lineno for the current stmt */ |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 143 | int u_col_offset; /* the offset of the current stmt */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 144 | }; |
| 145 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 146 | /* This struct captures the global state of a compilation. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 147 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 148 | The u pointer points to the current compilation unit, while units |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 149 | 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] | 150 | managed by compiler_enter_scope() and compiler_exit_scope(). |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 151 | |
| 152 | Note that we don't track recursion levels during compilation - the |
| 153 | task of detecting and rejecting excessive levels of nesting is |
| 154 | handled by the symbol analysis pass. |
| 155 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 156 | */ |
| 157 | |
| 158 | struct compiler { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 159 | PyObject *c_filename; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | struct symtable *c_st; |
| 161 | PyFutureFeatures *c_future; /* pointer to module's __future__ */ |
| 162 | PyCompilerFlags *c_flags; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 163 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 164 | int c_optimize; /* optimization level */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 165 | int c_interactive; /* true if in interactive mode */ |
| 166 | int c_nestlevel; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 167 | int c_do_not_emit_bytecode; /* The compiler won't emit any bytecode |
| 168 | if this value is different from zero. |
| 169 | This can be used to temporarily visit |
| 170 | nodes without emitting bytecode to |
| 171 | check only errors. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 172 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 173 | PyObject *c_const_cache; /* Python dict holding all constants, |
| 174 | including names tuple */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 175 | struct compiler_unit *u; /* compiler state for current block */ |
| 176 | PyObject *c_stack; /* Python list holding compiler_unit ptrs */ |
| 177 | PyArena *c_arena; /* pointer to memory allocation arena */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 178 | }; |
| 179 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 180 | static int compiler_enter_scope(struct compiler *, identifier, int, void *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 181 | static void compiler_free(struct compiler *); |
| 182 | static basicblock *compiler_new_block(struct compiler *); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 183 | static int compiler_next_instr(basicblock *); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 184 | static int compiler_addop(struct compiler *, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 185 | static int compiler_addop_i(struct compiler *, int, Py_ssize_t); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 186 | static int compiler_addop_j(struct compiler *, int, basicblock *, int); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 187 | static int compiler_error(struct compiler *, const char *); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 188 | static int compiler_warn(struct compiler *, const char *, ...); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 189 | static int compiler_nameop(struct compiler *, identifier, expr_context_ty); |
| 190 | |
| 191 | static PyCodeObject *compiler_mod(struct compiler *, mod_ty); |
| 192 | static int compiler_visit_stmt(struct compiler *, stmt_ty); |
| 193 | static int compiler_visit_keyword(struct compiler *, keyword_ty); |
| 194 | static int compiler_visit_expr(struct compiler *, expr_ty); |
| 195 | static int compiler_augassign(struct compiler *, stmt_ty); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 196 | static int compiler_annassign(struct compiler *, stmt_ty); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 197 | static int compiler_subscript(struct compiler *, expr_ty); |
| 198 | static int compiler_slice(struct compiler *, expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 199 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 200 | static int inplace_binop(operator_ty); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 201 | 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] | 202 | static int expr_constant(expr_ty); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 203 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 204 | static int compiler_with(struct compiler *, stmt_ty, int); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 205 | static int compiler_async_with(struct compiler *, stmt_ty, int); |
| 206 | static int compiler_async_for(struct compiler *, stmt_ty); |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 207 | static int compiler_call_helper(struct compiler *c, int n, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 208 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 209 | asdl_seq *keywords); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 210 | static int compiler_try_except(struct compiler *, stmt_ty); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 211 | static int compiler_set_qualname(struct compiler *); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 212 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 213 | static int compiler_sync_comprehension_generator( |
| 214 | struct compiler *c, |
| 215 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 216 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 217 | expr_ty elt, expr_ty val, int type); |
| 218 | |
| 219 | static int compiler_async_comprehension_generator( |
| 220 | struct compiler *c, |
| 221 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 222 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 223 | expr_ty elt, expr_ty val, int type); |
| 224 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 225 | static PyCodeObject *assemble(struct compiler *, int addNone); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 226 | static PyObject *__doc__, *__annotations__; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 227 | |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 228 | #define CAPSULE_NAME "compile.c compiler unit" |
Benjamin Peterson | b173f78 | 2009-05-05 22:31:58 +0000 | [diff] [blame] | 229 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 230 | PyObject * |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 231 | _Py_Mangle(PyObject *privateobj, PyObject *ident) |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 232 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 233 | /* Name mangling: __private becomes _classname__private. |
| 234 | This is independent from how the name is used. */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 235 | PyObject *result; |
| 236 | size_t nlen, plen, ipriv; |
| 237 | Py_UCS4 maxchar; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 238 | if (privateobj == NULL || !PyUnicode_Check(privateobj) || |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 239 | PyUnicode_READ_CHAR(ident, 0) != '_' || |
| 240 | PyUnicode_READ_CHAR(ident, 1) != '_') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 241 | Py_INCREF(ident); |
| 242 | return ident; |
| 243 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 244 | nlen = PyUnicode_GET_LENGTH(ident); |
| 245 | plen = PyUnicode_GET_LENGTH(privateobj); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 246 | /* Don't mangle __id__ or names with dots. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 247 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 248 | The only time a name with a dot can occur is when |
| 249 | we are compiling an import statement that has a |
| 250 | package name. |
Guido van Rossum | d8faa36 | 2007-04-27 19:54:29 +0000 | [diff] [blame] | 251 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 252 | TODO(jhylton): Decide whether we want to support |
| 253 | mangling of the module name, e.g. __M.X. |
| 254 | */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 255 | if ((PyUnicode_READ_CHAR(ident, nlen-1) == '_' && |
| 256 | PyUnicode_READ_CHAR(ident, nlen-2) == '_') || |
| 257 | PyUnicode_FindChar(ident, '.', 0, nlen, 1) != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 258 | Py_INCREF(ident); |
| 259 | return ident; /* Don't mangle __whatever__ */ |
| 260 | } |
| 261 | /* Strip leading underscores from class name */ |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 262 | ipriv = 0; |
| 263 | while (PyUnicode_READ_CHAR(privateobj, ipriv) == '_') |
| 264 | ipriv++; |
| 265 | if (ipriv == plen) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 266 | Py_INCREF(ident); |
| 267 | return ident; /* Don't mangle if class is just underscores */ |
| 268 | } |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 269 | plen -= ipriv; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 270 | |
Antoine Pitrou | 55bff89 | 2013-04-06 21:21:04 +0200 | [diff] [blame] | 271 | if (plen + nlen >= PY_SSIZE_T_MAX - 1) { |
| 272 | PyErr_SetString(PyExc_OverflowError, |
| 273 | "private identifier too large to be mangled"); |
| 274 | return NULL; |
| 275 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 276 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 277 | maxchar = PyUnicode_MAX_CHAR_VALUE(ident); |
| 278 | if (PyUnicode_MAX_CHAR_VALUE(privateobj) > maxchar) |
| 279 | maxchar = PyUnicode_MAX_CHAR_VALUE(privateobj); |
| 280 | |
| 281 | result = PyUnicode_New(1 + nlen + plen, maxchar); |
| 282 | if (!result) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 283 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 284 | /* ident = "_" + priv[ipriv:] + ident # i.e. 1+plen+nlen bytes */ |
| 285 | PyUnicode_WRITE(PyUnicode_KIND(result), PyUnicode_DATA(result), 0, '_'); |
Victor Stinner | 6c7a52a | 2011-09-28 21:39:17 +0200 | [diff] [blame] | 286 | if (PyUnicode_CopyCharacters(result, 1, privateobj, ipriv, plen) < 0) { |
| 287 | Py_DECREF(result); |
| 288 | return NULL; |
| 289 | } |
| 290 | if (PyUnicode_CopyCharacters(result, plen+1, ident, 0, nlen) < 0) { |
| 291 | Py_DECREF(result); |
| 292 | return NULL; |
| 293 | } |
Victor Stinner | 8f82506 | 2012-04-27 13:55:39 +0200 | [diff] [blame] | 294 | assert(_PyUnicode_CheckConsistency(result, 1)); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 295 | return result; |
Michael W. Hudson | 6093462 | 2004-08-12 17:56:29 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 298 | static int |
| 299 | compiler_init(struct compiler *c) |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 300 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | memset(c, 0, sizeof(struct compiler)); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 302 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 303 | c->c_const_cache = PyDict_New(); |
| 304 | if (!c->c_const_cache) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | return 0; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 306 | } |
| 307 | |
| 308 | c->c_stack = PyList_New(0); |
| 309 | if (!c->c_stack) { |
| 310 | Py_CLEAR(c->c_const_cache); |
| 311 | return 0; |
| 312 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 313 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 314 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 315 | } |
| 316 | |
| 317 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 318 | PyAST_CompileObject(mod_ty mod, PyObject *filename, PyCompilerFlags *flags, |
| 319 | int optimize, PyArena *arena) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 320 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 321 | struct compiler c; |
| 322 | PyCodeObject *co = NULL; |
Victor Stinner | 37d66d7 | 2019-06-13 02:16:41 +0200 | [diff] [blame] | 323 | PyCompilerFlags local_flags = _PyCompilerFlags_INIT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 324 | int merged; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 325 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 326 | if (!__doc__) { |
| 327 | __doc__ = PyUnicode_InternFromString("__doc__"); |
| 328 | if (!__doc__) |
| 329 | return NULL; |
| 330 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 331 | if (!__annotations__) { |
| 332 | __annotations__ = PyUnicode_InternFromString("__annotations__"); |
| 333 | if (!__annotations__) |
| 334 | return NULL; |
| 335 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 336 | if (!compiler_init(&c)) |
| 337 | return NULL; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 338 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 339 | c.c_filename = filename; |
| 340 | c.c_arena = arena; |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 341 | c.c_future = PyFuture_FromASTObject(mod, filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 342 | if (c.c_future == NULL) |
| 343 | goto finally; |
| 344 | if (!flags) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | flags = &local_flags; |
| 346 | } |
| 347 | merged = c.c_future->ff_features | flags->cf_flags; |
| 348 | c.c_future->ff_features = merged; |
| 349 | flags->cf_flags = merged; |
| 350 | c.c_flags = flags; |
Victor Stinner | da7933e | 2020-04-13 03:04:28 +0200 | [diff] [blame] | 351 | c.c_optimize = (optimize == -1) ? _Py_GetConfig()->optimization_level : optimize; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 352 | c.c_nestlevel = 0; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 353 | c.c_do_not_emit_bytecode = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 354 | |
Pablo Galindo | d112c60 | 2020-03-18 23:02:09 +0000 | [diff] [blame] | 355 | _PyASTOptimizeState state; |
| 356 | state.optimize = c.c_optimize; |
| 357 | state.ff_features = merged; |
| 358 | |
| 359 | if (!_PyAST_Optimize(mod, arena, &state)) { |
INADA Naoki | 7ea143a | 2017-12-14 16:47:20 +0900 | [diff] [blame] | 360 | goto finally; |
| 361 | } |
| 362 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 363 | c.c_st = PySymtable_BuildObject(mod, filename, c.c_future); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 364 | if (c.c_st == NULL) { |
| 365 | if (!PyErr_Occurred()) |
| 366 | PyErr_SetString(PyExc_SystemError, "no symtable"); |
| 367 | goto finally; |
| 368 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 369 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | co = compiler_mod(&c, mod); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 371 | |
Thomas Wouters | 1175c43 | 2006-02-27 22:49:54 +0000 | [diff] [blame] | 372 | finally: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 373 | compiler_free(&c); |
| 374 | assert(co || PyErr_Occurred()); |
| 375 | return co; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | PyCodeObject * |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 379 | PyAST_CompileEx(mod_ty mod, const char *filename_str, PyCompilerFlags *flags, |
| 380 | int optimize, PyArena *arena) |
| 381 | { |
| 382 | PyObject *filename; |
| 383 | PyCodeObject *co; |
| 384 | filename = PyUnicode_DecodeFSDefault(filename_str); |
| 385 | if (filename == NULL) |
| 386 | return NULL; |
| 387 | co = PyAST_CompileObject(mod, filename, flags, optimize, arena); |
| 388 | Py_DECREF(filename); |
| 389 | return co; |
| 390 | |
| 391 | } |
| 392 | |
| 393 | PyCodeObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 394 | PyNode_Compile(struct _node *n, const char *filename) |
| 395 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | PyCodeObject *co = NULL; |
| 397 | mod_ty mod; |
| 398 | PyArena *arena = PyArena_New(); |
| 399 | if (!arena) |
| 400 | return NULL; |
| 401 | mod = PyAST_FromNode(n, NULL, filename, arena); |
| 402 | if (mod) |
| 403 | co = PyAST_Compile(mod, filename, NULL, arena); |
| 404 | PyArena_Free(arena); |
| 405 | return co; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 406 | } |
| 407 | |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 408 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 409 | compiler_free(struct compiler *c) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 410 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 411 | if (c->c_st) |
| 412 | PySymtable_Free(c->c_st); |
| 413 | if (c->c_future) |
| 414 | PyObject_Free(c->c_future); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 415 | Py_XDECREF(c->c_filename); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 416 | Py_DECREF(c->c_const_cache); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 417 | Py_DECREF(c->c_stack); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 418 | } |
| 419 | |
Guido van Rossum | 79f25d9 | 1997-04-29 20:08:16 +0000 | [diff] [blame] | 420 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 421 | list2dict(PyObject *list) |
Guido van Rossum | 2dff991 | 1992-09-03 20:50:59 +0000 | [diff] [blame] | 422 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 423 | Py_ssize_t i, n; |
| 424 | PyObject *v, *k; |
| 425 | PyObject *dict = PyDict_New(); |
| 426 | if (!dict) return NULL; |
Guido van Rossum | d076c73 | 1998-10-07 19:42:25 +0000 | [diff] [blame] | 427 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 428 | n = PyList_Size(list); |
| 429 | for (i = 0; i < n; i++) { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 430 | v = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 431 | if (!v) { |
| 432 | Py_DECREF(dict); |
| 433 | return NULL; |
| 434 | } |
| 435 | k = PyList_GET_ITEM(list, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 436 | if (PyDict_SetItem(dict, k, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 437 | Py_DECREF(v); |
| 438 | Py_DECREF(dict); |
| 439 | return NULL; |
| 440 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 441 | Py_DECREF(v); |
| 442 | } |
| 443 | return dict; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | /* Return new dict containing names from src that match scope(s). |
| 447 | |
Jeremy Hylton | e9357b2 | 2006-03-01 15:47:05 +0000 | [diff] [blame] | 448 | 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] | 449 | 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] | 450 | values are integers, starting at offset and increasing by one for |
| 451 | each key. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 452 | */ |
| 453 | |
| 454 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 455 | dictbytype(PyObject *src, int scope_type, int flag, Py_ssize_t offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 456 | { |
Benjamin Peterson | 51ab283 | 2012-07-18 15:12:47 -0700 | [diff] [blame] | 457 | Py_ssize_t i = offset, scope, num_keys, key_i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 458 | PyObject *k, *v, *dest = PyDict_New(); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 459 | PyObject *sorted_keys; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 460 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 461 | assert(offset >= 0); |
| 462 | if (dest == NULL) |
| 463 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 464 | |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 465 | /* Sort the keys so that we have a deterministic order on the indexes |
| 466 | saved in the returned dictionary. These indexes are used as indexes |
| 467 | into the free and cell var storage. Therefore if they aren't |
| 468 | deterministic, then the generated bytecode is not deterministic. |
| 469 | */ |
| 470 | sorted_keys = PyDict_Keys(src); |
| 471 | if (sorted_keys == NULL) |
| 472 | return NULL; |
| 473 | if (PyList_Sort(sorted_keys) != 0) { |
| 474 | Py_DECREF(sorted_keys); |
| 475 | return NULL; |
| 476 | } |
Meador Inge | f69e24e | 2012-07-18 16:41:03 -0500 | [diff] [blame] | 477 | num_keys = PyList_GET_SIZE(sorted_keys); |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 478 | |
| 479 | for (key_i = 0; key_i < num_keys; key_i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 480 | /* XXX this should probably be a macro in symtable.h */ |
| 481 | long vi; |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 482 | k = PyList_GET_ITEM(sorted_keys, key_i); |
| 483 | v = PyDict_GetItem(src, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 484 | assert(PyLong_Check(v)); |
| 485 | vi = PyLong_AS_LONG(v); |
| 486 | scope = (vi >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 487 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 488 | if (scope == scope_type || vi & flag) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 489 | PyObject *item = PyLong_FromSsize_t(i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 490 | if (item == NULL) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 491 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 492 | Py_DECREF(dest); |
| 493 | return NULL; |
| 494 | } |
| 495 | i++; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 496 | if (PyDict_SetItem(dest, k, item) < 0) { |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 497 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | Py_DECREF(item); |
| 499 | Py_DECREF(dest); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | return NULL; |
| 501 | } |
| 502 | Py_DECREF(item); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 503 | } |
| 504 | } |
Meador Inge | 2ca6315 | 2012-07-18 14:20:11 -0500 | [diff] [blame] | 505 | Py_DECREF(sorted_keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 506 | return dest; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 507 | } |
| 508 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 509 | static void |
| 510 | compiler_unit_check(struct compiler_unit *u) |
| 511 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 512 | basicblock *block; |
| 513 | for (block = u->u_blocks; block != NULL; block = block->b_list) { |
Benjamin Peterson | ca47063 | 2016-09-06 13:47:26 -0700 | [diff] [blame] | 514 | assert((uintptr_t)block != 0xcbcbcbcbU); |
| 515 | assert((uintptr_t)block != 0xfbfbfbfbU); |
| 516 | assert((uintptr_t)block != 0xdbdbdbdbU); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 517 | if (block->b_instr != NULL) { |
| 518 | assert(block->b_ialloc > 0); |
| 519 | assert(block->b_iused > 0); |
| 520 | assert(block->b_ialloc >= block->b_iused); |
| 521 | } |
| 522 | else { |
| 523 | assert (block->b_iused == 0); |
| 524 | assert (block->b_ialloc == 0); |
| 525 | } |
| 526 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | static void |
| 530 | compiler_unit_free(struct compiler_unit *u) |
| 531 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | basicblock *b, *next; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 533 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 534 | compiler_unit_check(u); |
| 535 | b = u->u_blocks; |
| 536 | while (b != NULL) { |
| 537 | if (b->b_instr) |
| 538 | PyObject_Free((void *)b->b_instr); |
| 539 | next = b->b_list; |
| 540 | PyObject_Free((void *)b); |
| 541 | b = next; |
| 542 | } |
| 543 | Py_CLEAR(u->u_ste); |
| 544 | Py_CLEAR(u->u_name); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 545 | Py_CLEAR(u->u_qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 546 | Py_CLEAR(u->u_consts); |
| 547 | Py_CLEAR(u->u_names); |
| 548 | Py_CLEAR(u->u_varnames); |
| 549 | Py_CLEAR(u->u_freevars); |
| 550 | Py_CLEAR(u->u_cellvars); |
| 551 | Py_CLEAR(u->u_private); |
| 552 | PyObject_Free(u); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | static int |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 556 | compiler_enter_scope(struct compiler *c, identifier name, |
| 557 | int scope_type, void *key, int lineno) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 558 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 559 | struct compiler_unit *u; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 560 | basicblock *block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 561 | |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 562 | u = (struct compiler_unit *)PyObject_Calloc(1, sizeof( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 563 | struct compiler_unit)); |
| 564 | if (!u) { |
| 565 | PyErr_NoMemory(); |
| 566 | return 0; |
| 567 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 568 | u->u_scope_type = scope_type; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 569 | u->u_argcount = 0; |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 570 | u->u_posonlyargcount = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 571 | u->u_kwonlyargcount = 0; |
| 572 | u->u_ste = PySymtable_Lookup(c->c_st, key); |
| 573 | if (!u->u_ste) { |
| 574 | compiler_unit_free(u); |
| 575 | return 0; |
| 576 | } |
| 577 | Py_INCREF(name); |
| 578 | u->u_name = name; |
| 579 | u->u_varnames = list2dict(u->u_ste->ste_varnames); |
| 580 | u->u_cellvars = dictbytype(u->u_ste->ste_symbols, CELL, 0, 0); |
| 581 | if (!u->u_varnames || !u->u_cellvars) { |
| 582 | compiler_unit_free(u); |
| 583 | return 0; |
| 584 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 585 | if (u->u_ste->ste_needs_class_closure) { |
Martin Panter | 7462b649 | 2015-11-02 03:37:02 +0000 | [diff] [blame] | 586 | /* Cook up an implicit __class__ cell. */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 587 | _Py_IDENTIFIER(__class__); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 588 | PyObject *name; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 589 | int res; |
| 590 | assert(u->u_scope_type == COMPILER_SCOPE_CLASS); |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 591 | assert(PyDict_GET_SIZE(u->u_cellvars) == 0); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 592 | name = _PyUnicode_FromId(&PyId___class__); |
| 593 | if (!name) { |
| 594 | compiler_unit_free(u); |
| 595 | return 0; |
| 596 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 597 | res = PyDict_SetItem(u->u_cellvars, name, _PyLong_Zero); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 598 | if (res < 0) { |
| 599 | compiler_unit_free(u); |
| 600 | return 0; |
| 601 | } |
| 602 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 603 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 604 | 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] | 605 | PyDict_GET_SIZE(u->u_cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 606 | if (!u->u_freevars) { |
| 607 | compiler_unit_free(u); |
| 608 | return 0; |
| 609 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 611 | u->u_blocks = NULL; |
| 612 | u->u_nfblocks = 0; |
| 613 | u->u_firstlineno = lineno; |
| 614 | u->u_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 615 | u->u_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 616 | u->u_consts = PyDict_New(); |
| 617 | if (!u->u_consts) { |
| 618 | compiler_unit_free(u); |
| 619 | return 0; |
| 620 | } |
| 621 | u->u_names = PyDict_New(); |
| 622 | if (!u->u_names) { |
| 623 | compiler_unit_free(u); |
| 624 | return 0; |
| 625 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 626 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 627 | u->u_private = NULL; |
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 | /* Push the old compiler_unit on the stack. */ |
| 630 | if (c->u) { |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 631 | PyObject *capsule = PyCapsule_New(c->u, CAPSULE_NAME, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 632 | if (!capsule || PyList_Append(c->c_stack, capsule) < 0) { |
| 633 | Py_XDECREF(capsule); |
| 634 | compiler_unit_free(u); |
| 635 | return 0; |
| 636 | } |
| 637 | Py_DECREF(capsule); |
| 638 | u->u_private = c->u->u_private; |
| 639 | Py_XINCREF(u->u_private); |
| 640 | } |
| 641 | c->u = u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 642 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 643 | c->c_nestlevel++; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 644 | |
| 645 | block = compiler_new_block(c); |
| 646 | if (block == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 647 | return 0; |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 648 | c->u->u_curblock = block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 649 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 650 | if (u->u_scope_type != COMPILER_SCOPE_MODULE) { |
| 651 | if (!compiler_set_qualname(c)) |
| 652 | return 0; |
| 653 | } |
| 654 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 655 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 656 | } |
| 657 | |
Neil Schemenauer | c396d9e | 2005-10-25 06:30:14 +0000 | [diff] [blame] | 658 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 659 | compiler_exit_scope(struct compiler *c) |
| 660 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 661 | Py_ssize_t n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | PyObject *capsule; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 663 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 664 | c->c_nestlevel--; |
| 665 | compiler_unit_free(c->u); |
| 666 | /* Restore c->u to the parent unit. */ |
| 667 | n = PyList_GET_SIZE(c->c_stack) - 1; |
| 668 | if (n >= 0) { |
| 669 | capsule = PyList_GET_ITEM(c->c_stack, n); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 670 | c->u = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | assert(c->u); |
| 672 | /* we are deleting from a list so this really shouldn't fail */ |
| 673 | if (PySequence_DelItem(c->c_stack, n) < 0) |
| 674 | Py_FatalError("compiler_exit_scope()"); |
| 675 | compiler_unit_check(c->u); |
| 676 | } |
| 677 | else |
| 678 | c->u = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 679 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 682 | static int |
| 683 | compiler_set_qualname(struct compiler *c) |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 684 | { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 685 | _Py_static_string(dot, "."); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 686 | _Py_static_string(dot_locals, ".<locals>"); |
| 687 | Py_ssize_t stack_size; |
| 688 | struct compiler_unit *u = c->u; |
| 689 | PyObject *name, *base, *dot_str, *dot_locals_str; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 690 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 691 | base = NULL; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 692 | stack_size = PyList_GET_SIZE(c->c_stack); |
Benjamin Peterson | a8a38b8 | 2013-10-19 16:14:39 -0400 | [diff] [blame] | 693 | assert(stack_size >= 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 694 | if (stack_size > 1) { |
| 695 | int scope, force_global = 0; |
| 696 | struct compiler_unit *parent; |
| 697 | PyObject *mangled, *capsule; |
| 698 | |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 699 | capsule = PyList_GET_ITEM(c->c_stack, stack_size - 1); |
Benjamin Peterson | 9e77f72 | 2015-05-07 18:41:47 -0400 | [diff] [blame] | 700 | parent = (struct compiler_unit *)PyCapsule_GetPointer(capsule, CAPSULE_NAME); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 701 | assert(parent); |
| 702 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 703 | if (u->u_scope_type == COMPILER_SCOPE_FUNCTION |
| 704 | || u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
| 705 | || u->u_scope_type == COMPILER_SCOPE_CLASS) { |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 706 | assert(u->u_name); |
| 707 | mangled = _Py_Mangle(parent->u_private, u->u_name); |
| 708 | if (!mangled) |
| 709 | return 0; |
| 710 | scope = PyST_GetScope(parent->u_ste, mangled); |
| 711 | Py_DECREF(mangled); |
| 712 | assert(scope != GLOBAL_IMPLICIT); |
| 713 | if (scope == GLOBAL_EXPLICIT) |
| 714 | force_global = 1; |
| 715 | } |
| 716 | |
| 717 | if (!force_global) { |
| 718 | if (parent->u_scope_type == COMPILER_SCOPE_FUNCTION |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 719 | || parent->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 720 | || parent->u_scope_type == COMPILER_SCOPE_LAMBDA) { |
| 721 | dot_locals_str = _PyUnicode_FromId(&dot_locals); |
| 722 | if (dot_locals_str == NULL) |
| 723 | return 0; |
| 724 | base = PyUnicode_Concat(parent->u_qualname, dot_locals_str); |
| 725 | if (base == NULL) |
| 726 | return 0; |
| 727 | } |
| 728 | else { |
| 729 | Py_INCREF(parent->u_qualname); |
| 730 | base = parent->u_qualname; |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 731 | } |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 732 | } |
| 733 | } |
Benjamin Peterson | 3d9e481 | 2013-10-19 16:01:13 -0400 | [diff] [blame] | 734 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 735 | if (base != NULL) { |
| 736 | dot_str = _PyUnicode_FromId(&dot); |
| 737 | if (dot_str == NULL) { |
| 738 | Py_DECREF(base); |
| 739 | return 0; |
| 740 | } |
| 741 | name = PyUnicode_Concat(base, dot_str); |
| 742 | Py_DECREF(base); |
| 743 | if (name == NULL) |
| 744 | return 0; |
| 745 | PyUnicode_Append(&name, u->u_name); |
| 746 | if (name == NULL) |
| 747 | return 0; |
| 748 | } |
| 749 | else { |
| 750 | Py_INCREF(u->u_name); |
| 751 | name = u->u_name; |
| 752 | } |
| 753 | u->u_qualname = name; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 754 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 755 | return 1; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 756 | } |
| 757 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 758 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 759 | /* Allocate a new block and return a pointer to it. |
| 760 | Returns NULL on error. |
| 761 | */ |
| 762 | |
| 763 | static basicblock * |
| 764 | compiler_new_block(struct compiler *c) |
| 765 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 766 | basicblock *b; |
| 767 | struct compiler_unit *u; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 769 | u = c->u; |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 770 | b = (basicblock *)PyObject_Calloc(1, sizeof(basicblock)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 771 | if (b == NULL) { |
| 772 | PyErr_NoMemory(); |
| 773 | return NULL; |
| 774 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 775 | /* Extend the singly linked list of blocks with new block. */ |
| 776 | b->b_list = u->u_blocks; |
| 777 | u->u_blocks = b; |
| 778 | return b; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 781 | static basicblock * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 782 | compiler_next_block(struct compiler *c) |
| 783 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 784 | basicblock *block = compiler_new_block(c); |
| 785 | if (block == NULL) |
| 786 | return NULL; |
| 787 | c->u->u_curblock->b_next = block; |
| 788 | c->u->u_curblock = block; |
| 789 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | static basicblock * |
| 793 | compiler_use_next_block(struct compiler *c, basicblock *block) |
| 794 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 795 | assert(block != NULL); |
| 796 | c->u->u_curblock->b_next = block; |
| 797 | c->u->u_curblock = block; |
| 798 | return block; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 799 | } |
| 800 | |
| 801 | /* Returns the offset of the next instruction in the current block's |
| 802 | b_instr array. Resizes the b_instr as necessary. |
| 803 | Returns -1 on failure. |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 804 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 805 | |
| 806 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 807 | compiler_next_instr(basicblock *b) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 808 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 809 | assert(b != NULL); |
| 810 | if (b->b_instr == NULL) { |
Andy Lester | 7668a8b | 2020-03-24 23:26:44 -0500 | [diff] [blame] | 811 | b->b_instr = (struct instr *)PyObject_Calloc( |
| 812 | DEFAULT_BLOCK_SIZE, sizeof(struct instr)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 813 | if (b->b_instr == NULL) { |
| 814 | PyErr_NoMemory(); |
| 815 | return -1; |
| 816 | } |
| 817 | b->b_ialloc = DEFAULT_BLOCK_SIZE; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 818 | } |
| 819 | else if (b->b_iused == b->b_ialloc) { |
| 820 | struct instr *tmp; |
| 821 | size_t oldsize, newsize; |
| 822 | oldsize = b->b_ialloc * sizeof(struct instr); |
| 823 | newsize = oldsize << 1; |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 824 | |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 825 | if (oldsize > (SIZE_MAX >> 1)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 826 | PyErr_NoMemory(); |
| 827 | return -1; |
| 828 | } |
Amaury Forgeot d'Arc | 9c74b14 | 2008-06-18 00:47:36 +0000 | [diff] [blame] | 829 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 830 | if (newsize == 0) { |
| 831 | PyErr_NoMemory(); |
| 832 | return -1; |
| 833 | } |
| 834 | b->b_ialloc <<= 1; |
| 835 | tmp = (struct instr *)PyObject_Realloc( |
| 836 | (void *)b->b_instr, newsize); |
| 837 | if (tmp == NULL) { |
| 838 | PyErr_NoMemory(); |
| 839 | return -1; |
| 840 | } |
| 841 | b->b_instr = tmp; |
| 842 | memset((char *)b->b_instr + oldsize, 0, newsize - oldsize); |
| 843 | } |
| 844 | return b->b_iused++; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 845 | } |
| 846 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 847 | /* Set the line number and column offset for the following instructions. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 848 | |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 849 | The line number is reset in the following cases: |
| 850 | - when entering a new scope |
| 851 | - on each statement |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 852 | - on each expression and sub-expression |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 853 | - before the "except" and "finally" clauses |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 854 | */ |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 855 | |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 856 | #define SET_LOC(c, x) \ |
| 857 | (c)->u->u_lineno = (x)->lineno; \ |
| 858 | (c)->u->u_col_offset = (x)->col_offset; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 859 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 860 | /* Return the stack effect of opcode with argument oparg. |
| 861 | |
| 862 | Some opcodes have different stack effect when jump to the target and |
| 863 | when not jump. The 'jump' parameter specifies the case: |
| 864 | |
| 865 | * 0 -- when not jump |
| 866 | * 1 -- when jump |
| 867 | * -1 -- maximal |
| 868 | */ |
| 869 | /* XXX Make the stack effect of WITH_CLEANUP_START and |
| 870 | WITH_CLEANUP_FINISH deterministic. */ |
| 871 | static int |
| 872 | stack_effect(int opcode, int oparg, int jump) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 873 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 874 | switch (opcode) { |
Serhiy Storchaka | 57faf34 | 2018-04-25 22:04:06 +0300 | [diff] [blame] | 875 | case NOP: |
| 876 | case EXTENDED_ARG: |
| 877 | return 0; |
| 878 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 879 | /* Stack manipulation */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 880 | case POP_TOP: |
| 881 | return -1; |
| 882 | case ROT_TWO: |
| 883 | case ROT_THREE: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 884 | case ROT_FOUR: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 885 | return 0; |
| 886 | case DUP_TOP: |
| 887 | return 1; |
Antoine Pitrou | 74a69fa | 2010-09-04 18:43:52 +0000 | [diff] [blame] | 888 | case DUP_TOP_TWO: |
| 889 | return 2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 890 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 891 | /* Unary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 892 | case UNARY_POSITIVE: |
| 893 | case UNARY_NEGATIVE: |
| 894 | case UNARY_NOT: |
| 895 | case UNARY_INVERT: |
| 896 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 897 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 898 | case SET_ADD: |
| 899 | case LIST_APPEND: |
| 900 | return -1; |
| 901 | case MAP_ADD: |
| 902 | return -2; |
Neal Norwitz | 10be2ea | 2006-03-03 20:29:11 +0000 | [diff] [blame] | 903 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 904 | /* Binary operators */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 905 | case BINARY_POWER: |
| 906 | case BINARY_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 907 | case BINARY_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 908 | case BINARY_MODULO: |
| 909 | case BINARY_ADD: |
| 910 | case BINARY_SUBTRACT: |
| 911 | case BINARY_SUBSCR: |
| 912 | case BINARY_FLOOR_DIVIDE: |
| 913 | case BINARY_TRUE_DIVIDE: |
| 914 | return -1; |
| 915 | case INPLACE_FLOOR_DIVIDE: |
| 916 | case INPLACE_TRUE_DIVIDE: |
| 917 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 918 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 919 | case INPLACE_ADD: |
| 920 | case INPLACE_SUBTRACT: |
| 921 | case INPLACE_MULTIPLY: |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 922 | case INPLACE_MATRIX_MULTIPLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 923 | case INPLACE_MODULO: |
| 924 | return -1; |
| 925 | case STORE_SUBSCR: |
| 926 | return -3; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | case DELETE_SUBSCR: |
| 928 | return -2; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 929 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 930 | case BINARY_LSHIFT: |
| 931 | case BINARY_RSHIFT: |
| 932 | case BINARY_AND: |
| 933 | case BINARY_XOR: |
| 934 | case BINARY_OR: |
| 935 | return -1; |
| 936 | case INPLACE_POWER: |
| 937 | return -1; |
| 938 | case GET_ITER: |
| 939 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 940 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 941 | case PRINT_EXPR: |
| 942 | return -1; |
| 943 | case LOAD_BUILD_CLASS: |
| 944 | return 1; |
| 945 | case INPLACE_LSHIFT: |
| 946 | case INPLACE_RSHIFT: |
| 947 | case INPLACE_AND: |
| 948 | case INPLACE_XOR: |
| 949 | case INPLACE_OR: |
| 950 | return -1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 951 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 952 | case SETUP_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 953 | /* 1 in the normal flow. |
| 954 | * Restore the stack position and push 6 values before jumping to |
| 955 | * the handler if an exception be raised. */ |
| 956 | return jump ? 6 : 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 957 | case RETURN_VALUE: |
| 958 | return -1; |
| 959 | case IMPORT_STAR: |
| 960 | return -1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 961 | case SETUP_ANNOTATIONS: |
| 962 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 963 | case YIELD_VALUE: |
| 964 | return 0; |
Benjamin Peterson | 2afe6ae | 2012-03-15 15:37:39 -0500 | [diff] [blame] | 965 | case YIELD_FROM: |
| 966 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 967 | case POP_BLOCK: |
| 968 | return 0; |
| 969 | case POP_EXCEPT: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 970 | return -3; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 971 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 972 | case STORE_NAME: |
| 973 | return -1; |
| 974 | case DELETE_NAME: |
| 975 | return 0; |
| 976 | case UNPACK_SEQUENCE: |
| 977 | return oparg-1; |
| 978 | case UNPACK_EX: |
| 979 | return (oparg&0xFF) + (oparg>>8); |
| 980 | case FOR_ITER: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 981 | /* -1 at end of iterator, 1 if continue iterating. */ |
| 982 | return jump > 0 ? -1 : 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 984 | case STORE_ATTR: |
| 985 | return -2; |
| 986 | case DELETE_ATTR: |
| 987 | return -1; |
| 988 | case STORE_GLOBAL: |
| 989 | return -1; |
| 990 | case DELETE_GLOBAL: |
| 991 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 992 | case LOAD_CONST: |
| 993 | return 1; |
| 994 | case LOAD_NAME: |
| 995 | return 1; |
| 996 | case BUILD_TUPLE: |
| 997 | case BUILD_LIST: |
| 998 | case BUILD_SET: |
Serhiy Storchaka | ea525a2 | 2016-09-06 22:07:53 +0300 | [diff] [blame] | 999 | case BUILD_STRING: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1000 | return 1-oparg; |
| 1001 | case BUILD_MAP: |
Benjamin Peterson | b685515 | 2015-09-10 21:02:39 -0700 | [diff] [blame] | 1002 | return 1 - 2*oparg; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1003 | case BUILD_CONST_KEY_MAP: |
| 1004 | return -oparg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1005 | case LOAD_ATTR: |
| 1006 | return 0; |
| 1007 | case COMPARE_OP: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1008 | case IS_OP: |
| 1009 | case CONTAINS_OP: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1010 | return -1; |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1011 | case JUMP_IF_NOT_EXC_MATCH: |
| 1012 | return -2; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1013 | case IMPORT_NAME: |
| 1014 | return -1; |
| 1015 | case IMPORT_FROM: |
| 1016 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1017 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1018 | /* Jumps */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1019 | case JUMP_FORWARD: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | case JUMP_ABSOLUTE: |
| 1021 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1022 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1023 | case JUMP_IF_TRUE_OR_POP: |
| 1024 | case JUMP_IF_FALSE_OR_POP: |
| 1025 | return jump ? 0 : -1; |
| 1026 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1027 | case POP_JUMP_IF_FALSE: |
| 1028 | case POP_JUMP_IF_TRUE: |
| 1029 | return -1; |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 1030 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1031 | case LOAD_GLOBAL: |
| 1032 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1033 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1034 | /* Exception handling */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1035 | case SETUP_FINALLY: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1036 | /* 0 in the normal flow. |
| 1037 | * Restore the stack position and push 6 values before jumping to |
| 1038 | * the handler if an exception be raised. */ |
| 1039 | return jump ? 6 : 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1040 | case RERAISE: |
| 1041 | return -3; |
| 1042 | |
| 1043 | case WITH_EXCEPT_START: |
| 1044 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1046 | case LOAD_FAST: |
| 1047 | return 1; |
| 1048 | case STORE_FAST: |
| 1049 | return -1; |
| 1050 | case DELETE_FAST: |
| 1051 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1052 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1053 | case RAISE_VARARGS: |
| 1054 | return -oparg; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1055 | |
| 1056 | /* Functions and calls */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1057 | case CALL_FUNCTION: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1058 | return -oparg; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1059 | case CALL_METHOD: |
| 1060 | return -oparg-1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | case CALL_FUNCTION_KW: |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 1062 | return -oparg-1; |
| 1063 | case CALL_FUNCTION_EX: |
Matthieu Dartiailh | 3a9ac82 | 2017-02-21 14:25:22 +0100 | [diff] [blame] | 1064 | return -1 - ((oparg & 0x01) != 0); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1065 | case MAKE_FUNCTION: |
| 1066 | return -1 - ((oparg & 0x01) != 0) - ((oparg & 0x02) != 0) - |
| 1067 | ((oparg & 0x04) != 0) - ((oparg & 0x08) != 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1068 | case BUILD_SLICE: |
| 1069 | if (oparg == 3) |
| 1070 | return -2; |
| 1071 | else |
| 1072 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1073 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1074 | /* Closures */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1075 | case LOAD_CLOSURE: |
| 1076 | return 1; |
| 1077 | case LOAD_DEREF: |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 1078 | case LOAD_CLASSDEREF: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1079 | return 1; |
| 1080 | case STORE_DEREF: |
| 1081 | return -1; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 1082 | case DELETE_DEREF: |
| 1083 | return 0; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1084 | |
| 1085 | /* Iterators and generators */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1086 | case GET_AWAITABLE: |
| 1087 | return 0; |
| 1088 | case SETUP_ASYNC_WITH: |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1089 | /* 0 in the normal flow. |
| 1090 | * Restore the stack position to the position before the result |
| 1091 | * of __aenter__ and push 6 values before jumping to the handler |
| 1092 | * if an exception be raised. */ |
| 1093 | return jump ? -1 + 6 : 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1094 | case BEFORE_ASYNC_WITH: |
| 1095 | return 1; |
| 1096 | case GET_AITER: |
| 1097 | return 0; |
| 1098 | case GET_ANEXT: |
| 1099 | return 1; |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 1100 | case GET_YIELD_FROM_ITER: |
| 1101 | return 0; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 1102 | case END_ASYNC_FOR: |
| 1103 | return -7; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 1104 | case FORMAT_VALUE: |
| 1105 | /* If there's a fmt_spec on the stack, we go from 2->1, |
| 1106 | else 1->1. */ |
| 1107 | return (oparg & FVS_MASK) == FVS_HAVE_SPEC ? -1 : 0; |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 1108 | case LOAD_METHOD: |
| 1109 | return 1; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 1110 | case LOAD_ASSERTION_ERROR: |
| 1111 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1112 | case LIST_TO_TUPLE: |
| 1113 | return 0; |
| 1114 | case LIST_EXTEND: |
| 1115 | case SET_UPDATE: |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 1116 | case DICT_MERGE: |
| 1117 | case DICT_UPDATE: |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1118 | return -1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1119 | default: |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1120 | return PY_INVALID_STACK_EFFECT; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1121 | } |
Larry Hastings | 3a90797 | 2013-11-23 14:49:22 -0800 | [diff] [blame] | 1122 | return PY_INVALID_STACK_EFFECT; /* not reachable */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1125 | int |
Serhiy Storchaka | 7bdf282 | 2018-09-18 09:54:26 +0300 | [diff] [blame] | 1126 | PyCompile_OpcodeStackEffectWithJump(int opcode, int oparg, int jump) |
| 1127 | { |
| 1128 | return stack_effect(opcode, oparg, jump); |
| 1129 | } |
| 1130 | |
| 1131 | int |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 1132 | PyCompile_OpcodeStackEffect(int opcode, int oparg) |
| 1133 | { |
| 1134 | return stack_effect(opcode, oparg, -1); |
| 1135 | } |
| 1136 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1137 | /* Add an opcode with no argument. |
| 1138 | Returns 0 on failure, 1 on success. |
| 1139 | */ |
| 1140 | |
| 1141 | static int |
| 1142 | compiler_addop(struct compiler *c, int opcode) |
| 1143 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | basicblock *b; |
| 1145 | struct instr *i; |
| 1146 | int off; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1147 | assert(!HAS_ARG(opcode)); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1148 | if (c->c_do_not_emit_bytecode) { |
| 1149 | return 1; |
| 1150 | } |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1151 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1152 | if (off < 0) |
| 1153 | return 0; |
| 1154 | b = c->u->u_curblock; |
| 1155 | i = &b->b_instr[off]; |
| 1156 | i->i_opcode = opcode; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1157 | i->i_oparg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1158 | if (opcode == RETURN_VALUE) |
| 1159 | b->b_return = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1160 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1161 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1162 | } |
| 1163 | |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1164 | static Py_ssize_t |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1165 | compiler_add_o(PyObject *dict, PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1166 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1167 | PyObject *v; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1168 | Py_ssize_t arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1169 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1170 | v = PyDict_GetItemWithError(dict, o); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1171 | if (!v) { |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1172 | if (PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1173 | return -1; |
Stefan Krah | c0cbed1 | 2015-07-27 12:56:49 +0200 | [diff] [blame] | 1174 | } |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 1175 | arg = PyDict_GET_SIZE(dict); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1176 | v = PyLong_FromSsize_t(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1177 | if (!v) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1178 | return -1; |
| 1179 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1180 | if (PyDict_SetItem(dict, o, v) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1181 | Py_DECREF(v); |
| 1182 | return -1; |
| 1183 | } |
| 1184 | Py_DECREF(v); |
| 1185 | } |
| 1186 | else |
| 1187 | arg = PyLong_AsLong(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1188 | return arg; |
| 1189 | } |
| 1190 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1191 | // Merge const *o* recursively and return constant key object. |
| 1192 | static PyObject* |
| 1193 | merge_consts_recursive(struct compiler *c, PyObject *o) |
| 1194 | { |
| 1195 | // None and Ellipsis are singleton, and key is the singleton. |
| 1196 | // No need to merge object and key. |
| 1197 | if (o == Py_None || o == Py_Ellipsis) { |
| 1198 | Py_INCREF(o); |
| 1199 | return o; |
| 1200 | } |
| 1201 | |
| 1202 | PyObject *key = _PyCode_ConstantKey(o); |
| 1203 | if (key == NULL) { |
| 1204 | return NULL; |
| 1205 | } |
| 1206 | |
| 1207 | // t is borrowed reference |
| 1208 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 1209 | if (t != key) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1210 | // o is registered in c_const_cache. Just use it. |
Zackery Spytz | 9b4a1b1 | 2019-03-20 03:16:25 -0600 | [diff] [blame] | 1211 | Py_XINCREF(t); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1212 | Py_DECREF(key); |
| 1213 | return t; |
| 1214 | } |
| 1215 | |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1216 | // We registered o in c_const_cache. |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1217 | // When o is a tuple or frozenset, we want to merge its |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1218 | // items too. |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1219 | if (PyTuple_CheckExact(o)) { |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1220 | Py_ssize_t len = PyTuple_GET_SIZE(o); |
| 1221 | for (Py_ssize_t i = 0; i < len; i++) { |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1222 | PyObject *item = PyTuple_GET_ITEM(o, i); |
| 1223 | PyObject *u = merge_consts_recursive(c, item); |
| 1224 | if (u == NULL) { |
| 1225 | Py_DECREF(key); |
| 1226 | return NULL; |
| 1227 | } |
| 1228 | |
| 1229 | // See _PyCode_ConstantKey() |
| 1230 | PyObject *v; // borrowed |
| 1231 | if (PyTuple_CheckExact(u)) { |
| 1232 | v = PyTuple_GET_ITEM(u, 1); |
| 1233 | } |
| 1234 | else { |
| 1235 | v = u; |
| 1236 | } |
| 1237 | if (v != item) { |
| 1238 | Py_INCREF(v); |
| 1239 | PyTuple_SET_ITEM(o, i, v); |
| 1240 | Py_DECREF(item); |
| 1241 | } |
| 1242 | |
| 1243 | Py_DECREF(u); |
| 1244 | } |
| 1245 | } |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1246 | else if (PyFrozenSet_CheckExact(o)) { |
Simeon | 63b5fc5 | 2019-04-09 19:36:57 -0400 | [diff] [blame] | 1247 | // *key* is tuple. And its first item is frozenset of |
INADA Naoki | f7e4d36 | 2018-11-29 00:58:46 +0900 | [diff] [blame] | 1248 | // constant keys. |
| 1249 | // See _PyCode_ConstantKey() for detail. |
| 1250 | assert(PyTuple_CheckExact(key)); |
| 1251 | assert(PyTuple_GET_SIZE(key) == 2); |
| 1252 | |
| 1253 | Py_ssize_t len = PySet_GET_SIZE(o); |
| 1254 | if (len == 0) { // empty frozenset should not be re-created. |
| 1255 | return key; |
| 1256 | } |
| 1257 | PyObject *tuple = PyTuple_New(len); |
| 1258 | if (tuple == NULL) { |
| 1259 | Py_DECREF(key); |
| 1260 | return NULL; |
| 1261 | } |
| 1262 | Py_ssize_t i = 0, pos = 0; |
| 1263 | PyObject *item; |
| 1264 | Py_hash_t hash; |
| 1265 | while (_PySet_NextEntry(o, &pos, &item, &hash)) { |
| 1266 | PyObject *k = merge_consts_recursive(c, item); |
| 1267 | if (k == NULL) { |
| 1268 | Py_DECREF(tuple); |
| 1269 | Py_DECREF(key); |
| 1270 | return NULL; |
| 1271 | } |
| 1272 | PyObject *u; |
| 1273 | if (PyTuple_CheckExact(k)) { |
| 1274 | u = PyTuple_GET_ITEM(k, 1); |
| 1275 | Py_INCREF(u); |
| 1276 | Py_DECREF(k); |
| 1277 | } |
| 1278 | else { |
| 1279 | u = k; |
| 1280 | } |
| 1281 | PyTuple_SET_ITEM(tuple, i, u); // Steals reference of u. |
| 1282 | i++; |
| 1283 | } |
| 1284 | |
| 1285 | // Instead of rewriting o, we create new frozenset and embed in the |
| 1286 | // key tuple. Caller should get merged frozenset from the key tuple. |
| 1287 | PyObject *new = PyFrozenSet_New(tuple); |
| 1288 | Py_DECREF(tuple); |
| 1289 | if (new == NULL) { |
| 1290 | Py_DECREF(key); |
| 1291 | return NULL; |
| 1292 | } |
| 1293 | assert(PyTuple_GET_ITEM(key, 1) == o); |
| 1294 | Py_DECREF(o); |
| 1295 | PyTuple_SET_ITEM(key, 1, new); |
| 1296 | } |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1297 | |
| 1298 | return key; |
| 1299 | } |
| 1300 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1301 | static Py_ssize_t |
| 1302 | compiler_add_const(struct compiler *c, PyObject *o) |
| 1303 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1304 | if (c->c_do_not_emit_bytecode) { |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1308 | PyObject *key = merge_consts_recursive(c, o); |
| 1309 | if (key == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1310 | return -1; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1311 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1312 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1313 | Py_ssize_t arg = compiler_add_o(c->u->u_consts, key); |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 1314 | Py_DECREF(key); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | return arg; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | static int |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1319 | compiler_addop_load_const(struct compiler *c, PyObject *o) |
| 1320 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1321 | if (c->c_do_not_emit_bytecode) { |
| 1322 | return 1; |
| 1323 | } |
| 1324 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1325 | Py_ssize_t arg = compiler_add_const(c, o); |
| 1326 | if (arg < 0) |
| 1327 | return 0; |
| 1328 | return compiler_addop_i(c, LOAD_CONST, arg); |
| 1329 | } |
| 1330 | |
| 1331 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1332 | compiler_addop_o(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1334 | { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1335 | if (c->c_do_not_emit_bytecode) { |
| 1336 | return 1; |
| 1337 | } |
| 1338 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1339 | Py_ssize_t arg = compiler_add_o(dict, o); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1340 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1341 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1342 | return compiler_addop_i(c, opcode, arg); |
| 1343 | } |
| 1344 | |
| 1345 | static int |
| 1346 | compiler_addop_name(struct compiler *c, int opcode, PyObject *dict, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1347 | PyObject *o) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1348 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1349 | Py_ssize_t arg; |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1350 | |
| 1351 | if (c->c_do_not_emit_bytecode) { |
| 1352 | return 1; |
| 1353 | } |
| 1354 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1355 | PyObject *mangled = _Py_Mangle(c->u->u_private, o); |
| 1356 | if (!mangled) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1357 | return 0; |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1358 | arg = compiler_add_o(dict, mangled); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1359 | Py_DECREF(mangled); |
| 1360 | if (arg < 0) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1361 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1362 | return compiler_addop_i(c, opcode, arg); |
| 1363 | } |
| 1364 | |
| 1365 | /* Add an opcode with an integer argument. |
| 1366 | Returns 0 on failure, 1 on success. |
| 1367 | */ |
| 1368 | |
| 1369 | static int |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1370 | compiler_addop_i(struct compiler *c, int opcode, Py_ssize_t oparg) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1371 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1372 | struct instr *i; |
| 1373 | int off; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1374 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1375 | if (c->c_do_not_emit_bytecode) { |
| 1376 | return 1; |
| 1377 | } |
| 1378 | |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1379 | /* oparg value is unsigned, but a signed C int is usually used to store |
| 1380 | it in the C code (like Python/ceval.c). |
| 1381 | |
| 1382 | Limit to 32-bit signed C int (rather than INT_MAX) for portability. |
| 1383 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1384 | The argument of a concrete bytecode instruction is limited to 8-bit. |
| 1385 | EXTENDED_ARG is used for 16, 24, and 32-bit arguments. */ |
| 1386 | assert(HAS_ARG(opcode)); |
Victor Stinner | 2ad474b | 2016-03-01 23:34:47 +0100 | [diff] [blame] | 1387 | assert(0 <= oparg && oparg <= 2147483647); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1388 | |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1389 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1390 | if (off < 0) |
| 1391 | return 0; |
| 1392 | i = &c->u->u_curblock->b_instr[off]; |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 1393 | i->i_opcode = opcode; |
| 1394 | i->i_oparg = Py_SAFE_DOWNCAST(oparg, Py_ssize_t, int); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1395 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1396 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
| 1399 | static int |
| 1400 | compiler_addop_j(struct compiler *c, int opcode, basicblock *b, int absolute) |
| 1401 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | struct instr *i; |
| 1403 | int off; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1404 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1405 | if (c->c_do_not_emit_bytecode) { |
| 1406 | return 1; |
| 1407 | } |
| 1408 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 1409 | assert(HAS_ARG(opcode)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1410 | assert(b != NULL); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 1411 | off = compiler_next_instr(c->u->u_curblock); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1412 | if (off < 0) |
| 1413 | return 0; |
| 1414 | i = &c->u->u_curblock->b_instr[off]; |
| 1415 | i->i_opcode = opcode; |
| 1416 | i->i_target = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1417 | if (absolute) |
| 1418 | i->i_jabs = 1; |
| 1419 | else |
| 1420 | i->i_jrel = 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1421 | i->i_lineno = c->u->u_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1422 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Victor Stinner | fc6f2ef | 2016-02-27 02:19:22 +0100 | [diff] [blame] | 1425 | /* NEXT_BLOCK() creates an implicit jump from the current block |
| 1426 | to the new block. |
| 1427 | |
| 1428 | The returns inside this macro make it impossible to decref objects |
| 1429 | created in the local function. Local objects should use the arena. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1430 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1431 | #define NEXT_BLOCK(C) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1432 | if (compiler_next_block((C)) == NULL) \ |
| 1433 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1434 | } |
| 1435 | |
| 1436 | #define ADDOP(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1437 | if (!compiler_addop((C), (OP))) \ |
| 1438 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1439 | } |
| 1440 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1441 | #define ADDOP_IN_SCOPE(C, OP) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1442 | if (!compiler_addop((C), (OP))) { \ |
| 1443 | compiler_exit_scope(c); \ |
| 1444 | return 0; \ |
| 1445 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1448 | #define ADDOP_LOAD_CONST(C, O) { \ |
| 1449 | if (!compiler_addop_load_const((C), (O))) \ |
| 1450 | return 0; \ |
| 1451 | } |
| 1452 | |
| 1453 | /* Same as ADDOP_LOAD_CONST, but steals a reference. */ |
| 1454 | #define ADDOP_LOAD_CONST_NEW(C, O) { \ |
| 1455 | PyObject *__new_const = (O); \ |
| 1456 | if (__new_const == NULL) { \ |
| 1457 | return 0; \ |
| 1458 | } \ |
| 1459 | if (!compiler_addop_load_const((C), __new_const)) { \ |
| 1460 | Py_DECREF(__new_const); \ |
| 1461 | return 0; \ |
| 1462 | } \ |
| 1463 | Py_DECREF(__new_const); \ |
| 1464 | } |
| 1465 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1466 | #define ADDOP_O(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1467 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1468 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1469 | } |
| 1470 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 1471 | /* Same as ADDOP_O, but steals a reference. */ |
| 1472 | #define ADDOP_N(C, OP, O, TYPE) { \ |
| 1473 | if (!compiler_addop_o((C), (OP), (C)->u->u_ ## TYPE, (O))) { \ |
| 1474 | Py_DECREF((O)); \ |
| 1475 | return 0; \ |
| 1476 | } \ |
| 1477 | Py_DECREF((O)); \ |
| 1478 | } |
| 1479 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1480 | #define ADDOP_NAME(C, OP, O, TYPE) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1481 | if (!compiler_addop_name((C), (OP), (C)->u->u_ ## TYPE, (O))) \ |
| 1482 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1483 | } |
| 1484 | |
| 1485 | #define ADDOP_I(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1486 | if (!compiler_addop_i((C), (OP), (O))) \ |
| 1487 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1488 | } |
| 1489 | |
| 1490 | #define ADDOP_JABS(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1491 | if (!compiler_addop_j((C), (OP), (O), 1)) \ |
| 1492 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
| 1495 | #define ADDOP_JREL(C, OP, O) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1496 | if (!compiler_addop_j((C), (OP), (O), 0)) \ |
| 1497 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1498 | } |
| 1499 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 1500 | |
| 1501 | #define ADDOP_COMPARE(C, CMP) { \ |
| 1502 | if (!compiler_addcompare((C), (cmpop_ty)(CMP))) \ |
| 1503 | return 0; \ |
| 1504 | } |
| 1505 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1506 | /* VISIT and VISIT_SEQ takes an ASDL type as their second argument. They use |
| 1507 | the ASDL name to synthesize the name of the C type and the visit function. |
| 1508 | */ |
| 1509 | |
| 1510 | #define VISIT(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1511 | if (!compiler_visit_ ## TYPE((C), (V))) \ |
| 1512 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1515 | #define VISIT_IN_SCOPE(C, TYPE, V) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1516 | if (!compiler_visit_ ## TYPE((C), (V))) { \ |
| 1517 | compiler_exit_scope(c); \ |
| 1518 | return 0; \ |
| 1519 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1522 | #define VISIT_SLICE(C, V, CTX) {\ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1523 | if (!compiler_visit_slice((C), (V), (CTX))) \ |
| 1524 | return 0; \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1525 | } |
| 1526 | |
| 1527 | #define VISIT_SEQ(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1528 | int _i; \ |
| 1529 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1530 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1531 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1532 | if (!compiler_visit_ ## TYPE((C), elt)) \ |
| 1533 | return 0; \ |
| 1534 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1535 | } |
| 1536 | |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1537 | #define VISIT_SEQ_IN_SCOPE(C, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1538 | int _i; \ |
| 1539 | asdl_seq *seq = (SEQ); /* avoid variable capture */ \ |
| 1540 | for (_i = 0; _i < asdl_seq_LEN(seq); _i++) { \ |
| 1541 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, _i); \ |
| 1542 | if (!compiler_visit_ ## TYPE((C), elt)) { \ |
| 1543 | compiler_exit_scope(c); \ |
| 1544 | return 0; \ |
| 1545 | } \ |
| 1546 | } \ |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1547 | } |
| 1548 | |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 1549 | /* These macros allows to check only for errors and not emmit bytecode |
| 1550 | * while visiting nodes. |
| 1551 | */ |
| 1552 | |
| 1553 | #define BEGIN_DO_NOT_EMIT_BYTECODE { \ |
| 1554 | c->c_do_not_emit_bytecode++; |
| 1555 | |
| 1556 | #define END_DO_NOT_EMIT_BYTECODE \ |
| 1557 | c->c_do_not_emit_bytecode--; \ |
| 1558 | } |
| 1559 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1560 | /* Search if variable annotations are present statically in a block. */ |
| 1561 | |
| 1562 | static int |
| 1563 | find_ann(asdl_seq *stmts) |
| 1564 | { |
| 1565 | int i, j, res = 0; |
| 1566 | stmt_ty st; |
| 1567 | |
| 1568 | for (i = 0; i < asdl_seq_LEN(stmts); i++) { |
| 1569 | st = (stmt_ty)asdl_seq_GET(stmts, i); |
| 1570 | switch (st->kind) { |
| 1571 | case AnnAssign_kind: |
| 1572 | return 1; |
| 1573 | case For_kind: |
| 1574 | res = find_ann(st->v.For.body) || |
| 1575 | find_ann(st->v.For.orelse); |
| 1576 | break; |
| 1577 | case AsyncFor_kind: |
| 1578 | res = find_ann(st->v.AsyncFor.body) || |
| 1579 | find_ann(st->v.AsyncFor.orelse); |
| 1580 | break; |
| 1581 | case While_kind: |
| 1582 | res = find_ann(st->v.While.body) || |
| 1583 | find_ann(st->v.While.orelse); |
| 1584 | break; |
| 1585 | case If_kind: |
| 1586 | res = find_ann(st->v.If.body) || |
| 1587 | find_ann(st->v.If.orelse); |
| 1588 | break; |
| 1589 | case With_kind: |
| 1590 | res = find_ann(st->v.With.body); |
| 1591 | break; |
| 1592 | case AsyncWith_kind: |
| 1593 | res = find_ann(st->v.AsyncWith.body); |
| 1594 | break; |
| 1595 | case Try_kind: |
| 1596 | for (j = 0; j < asdl_seq_LEN(st->v.Try.handlers); j++) { |
| 1597 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
| 1598 | st->v.Try.handlers, j); |
| 1599 | if (find_ann(handler->v.ExceptHandler.body)) { |
| 1600 | return 1; |
| 1601 | } |
| 1602 | } |
| 1603 | res = find_ann(st->v.Try.body) || |
| 1604 | find_ann(st->v.Try.finalbody) || |
| 1605 | find_ann(st->v.Try.orelse); |
| 1606 | break; |
| 1607 | default: |
| 1608 | res = 0; |
| 1609 | } |
| 1610 | if (res) { |
| 1611 | break; |
| 1612 | } |
| 1613 | } |
| 1614 | return res; |
| 1615 | } |
| 1616 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1617 | /* |
| 1618 | * Frame block handling functions |
| 1619 | */ |
| 1620 | |
| 1621 | static int |
| 1622 | compiler_push_fblock(struct compiler *c, enum fblocktype t, basicblock *b, |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1623 | basicblock *exit, void *datum) |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1624 | { |
| 1625 | struct fblockinfo *f; |
| 1626 | if (c->u->u_nfblocks >= CO_MAXBLOCKS) { |
| 1627 | PyErr_SetString(PyExc_SyntaxError, |
| 1628 | "too many statically nested blocks"); |
| 1629 | return 0; |
| 1630 | } |
| 1631 | f = &c->u->u_fblock[c->u->u_nfblocks++]; |
| 1632 | f->fb_type = t; |
| 1633 | f->fb_block = b; |
| 1634 | f->fb_exit = exit; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1635 | f->fb_datum = datum; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1636 | return 1; |
| 1637 | } |
| 1638 | |
| 1639 | static void |
| 1640 | compiler_pop_fblock(struct compiler *c, enum fblocktype t, basicblock *b) |
| 1641 | { |
| 1642 | struct compiler_unit *u = c->u; |
| 1643 | assert(u->u_nfblocks > 0); |
| 1644 | u->u_nfblocks--; |
| 1645 | assert(u->u_fblock[u->u_nfblocks].fb_type == t); |
| 1646 | assert(u->u_fblock[u->u_nfblocks].fb_block == b); |
| 1647 | } |
| 1648 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1649 | static int |
| 1650 | compiler_call_exit_with_nones(struct compiler *c) { |
| 1651 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 1652 | ADDOP(c, DUP_TOP); |
| 1653 | ADDOP(c, DUP_TOP); |
| 1654 | ADDOP_I(c, CALL_FUNCTION, 3); |
| 1655 | return 1; |
| 1656 | } |
| 1657 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1658 | /* Unwind a frame block. If preserve_tos is true, the TOS before |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1659 | * popping the blocks will be restored afterwards, unless another |
| 1660 | * return, break or continue is found. In which case, the TOS will |
| 1661 | * be popped. |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1662 | */ |
| 1663 | static int |
| 1664 | compiler_unwind_fblock(struct compiler *c, struct fblockinfo *info, |
| 1665 | int preserve_tos) |
| 1666 | { |
| 1667 | switch (info->fb_type) { |
| 1668 | case WHILE_LOOP: |
| 1669 | return 1; |
| 1670 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1671 | case FOR_LOOP: |
| 1672 | /* Pop the iterator */ |
| 1673 | if (preserve_tos) { |
| 1674 | ADDOP(c, ROT_TWO); |
| 1675 | } |
| 1676 | ADDOP(c, POP_TOP); |
| 1677 | return 1; |
| 1678 | |
| 1679 | case EXCEPT: |
| 1680 | ADDOP(c, POP_BLOCK); |
| 1681 | return 1; |
| 1682 | |
| 1683 | case FINALLY_TRY: |
| 1684 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1685 | if (preserve_tos) { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1686 | if (!compiler_push_fblock(c, POP_VALUE, NULL, NULL, NULL)) { |
| 1687 | return 0; |
| 1688 | } |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1689 | } |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1690 | /* Emit the finally block, restoring the line number when done */ |
| 1691 | int saved_lineno = c->u->u_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1692 | VISIT_SEQ(c, stmt, info->fb_datum); |
Mark Shannon | 88dce26 | 2019-12-30 09:53:36 +0000 | [diff] [blame] | 1693 | c->u->u_lineno = saved_lineno; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1694 | if (preserve_tos) { |
| 1695 | compiler_pop_fblock(c, POP_VALUE, NULL); |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1696 | } |
| 1697 | return 1; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 1698 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1699 | case FINALLY_END: |
| 1700 | if (preserve_tos) { |
| 1701 | ADDOP(c, ROT_FOUR); |
| 1702 | } |
| 1703 | ADDOP(c, POP_TOP); |
| 1704 | ADDOP(c, POP_TOP); |
| 1705 | ADDOP(c, POP_TOP); |
| 1706 | if (preserve_tos) { |
| 1707 | ADDOP(c, ROT_FOUR); |
| 1708 | } |
| 1709 | ADDOP(c, POP_EXCEPT); |
| 1710 | return 1; |
Serhiy Storchaka | ef61c52 | 2019-08-24 13:11:52 +0300 | [diff] [blame] | 1711 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1712 | case WITH: |
| 1713 | case ASYNC_WITH: |
| 1714 | ADDOP(c, POP_BLOCK); |
| 1715 | if (preserve_tos) { |
| 1716 | ADDOP(c, ROT_TWO); |
| 1717 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1718 | if(!compiler_call_exit_with_nones(c)) { |
| 1719 | return 0; |
| 1720 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1721 | if (info->fb_type == ASYNC_WITH) { |
| 1722 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1723 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1724 | ADDOP(c, YIELD_FROM); |
| 1725 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1726 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1727 | return 1; |
| 1728 | |
| 1729 | case HANDLER_CLEANUP: |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1730 | if (info->fb_datum) { |
| 1731 | ADDOP(c, POP_BLOCK); |
| 1732 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1733 | if (preserve_tos) { |
| 1734 | ADDOP(c, ROT_FOUR); |
| 1735 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1736 | ADDOP(c, POP_EXCEPT); |
| 1737 | if (info->fb_datum) { |
| 1738 | ADDOP_LOAD_CONST(c, Py_None); |
| 1739 | compiler_nameop(c, info->fb_datum, Store); |
| 1740 | compiler_nameop(c, info->fb_datum, Del); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1741 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1742 | return 1; |
| 1743 | |
| 1744 | case POP_VALUE: |
| 1745 | if (preserve_tos) { |
| 1746 | ADDOP(c, ROT_TWO); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1747 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1748 | ADDOP(c, POP_TOP); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 1749 | return 1; |
| 1750 | } |
| 1751 | Py_UNREACHABLE(); |
| 1752 | } |
| 1753 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 1754 | /** Unwind block stack. If loop is not NULL, then stop when the first loop is encountered. */ |
| 1755 | static int |
| 1756 | compiler_unwind_fblock_stack(struct compiler *c, int preserve_tos, struct fblockinfo **loop) { |
| 1757 | if (c->u->u_nfblocks == 0) { |
| 1758 | return 1; |
| 1759 | } |
| 1760 | struct fblockinfo *top = &c->u->u_fblock[c->u->u_nfblocks-1]; |
| 1761 | if (loop != NULL && (top->fb_type == WHILE_LOOP || top->fb_type == FOR_LOOP)) { |
| 1762 | *loop = top; |
| 1763 | return 1; |
| 1764 | } |
| 1765 | struct fblockinfo copy = *top; |
| 1766 | c->u->u_nfblocks--; |
| 1767 | if (!compiler_unwind_fblock(c, ©, preserve_tos)) { |
| 1768 | return 0; |
| 1769 | } |
| 1770 | if (!compiler_unwind_fblock_stack(c, preserve_tos, loop)) { |
| 1771 | return 0; |
| 1772 | } |
| 1773 | c->u->u_fblock[c->u->u_nfblocks] = copy; |
| 1774 | c->u->u_nfblocks++; |
| 1775 | return 1; |
| 1776 | } |
| 1777 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1778 | /* Compile a sequence of statements, checking for a docstring |
| 1779 | and for annotations. */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1780 | |
| 1781 | static int |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1782 | compiler_body(struct compiler *c, asdl_seq *stmts) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1783 | { |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1784 | int i = 0; |
| 1785 | stmt_ty st; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1786 | PyObject *docstring; |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1787 | |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1788 | /* Set current line number to the line number of first statement. |
| 1789 | This way line number for SETUP_ANNOTATIONS will always |
| 1790 | coincide with the line number of first "real" statement in module. |
Hansraj Das | 01171eb | 2019-10-09 07:54:02 +0530 | [diff] [blame] | 1791 | If body is empty, then lineno will be set later in assemble. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1792 | 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] | 1793 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 1794 | SET_LOC(c, st); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1795 | } |
| 1796 | /* Every annotated class and module should have __annotations__. */ |
| 1797 | if (find_ann(stmts)) { |
| 1798 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1799 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1800 | if (!asdl_seq_LEN(stmts)) |
| 1801 | return 1; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 1802 | /* if not -OO mode, set docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 1803 | if (c->c_optimize < 2) { |
| 1804 | docstring = _PyAST_GetDocString(stmts); |
| 1805 | if (docstring) { |
| 1806 | i = 1; |
| 1807 | st = (stmt_ty)asdl_seq_GET(stmts, 0); |
| 1808 | assert(st->kind == Expr_kind); |
| 1809 | VISIT(c, expr, st->v.Expr.value); |
| 1810 | if (!compiler_nameop(c, __doc__, Store)) |
| 1811 | return 0; |
| 1812 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1813 | } |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1814 | for (; i < asdl_seq_LEN(stmts); i++) |
| 1815 | VISIT(c, stmt, (stmt_ty)asdl_seq_GET(stmts, i)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1816 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1817 | } |
| 1818 | |
| 1819 | static PyCodeObject * |
| 1820 | compiler_mod(struct compiler *c, mod_ty mod) |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1821 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1822 | PyCodeObject *co; |
| 1823 | int addNone = 1; |
| 1824 | static PyObject *module; |
| 1825 | if (!module) { |
| 1826 | module = PyUnicode_InternFromString("<module>"); |
| 1827 | if (!module) |
| 1828 | return NULL; |
| 1829 | } |
| 1830 | /* Use 0 for firstlineno initially, will fixup in assemble(). */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1831 | if (!compiler_enter_scope(c, module, COMPILER_SCOPE_MODULE, mod, 0)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1832 | return NULL; |
| 1833 | switch (mod->kind) { |
| 1834 | case Module_kind: |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 1835 | if (!compiler_body(c, mod->v.Module.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1836 | compiler_exit_scope(c); |
| 1837 | return 0; |
| 1838 | } |
| 1839 | break; |
| 1840 | case Interactive_kind: |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1841 | if (find_ann(mod->v.Interactive.body)) { |
| 1842 | ADDOP(c, SETUP_ANNOTATIONS); |
| 1843 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1844 | c->c_interactive = 1; |
| 1845 | VISIT_SEQ_IN_SCOPE(c, stmt, |
| 1846 | mod->v.Interactive.body); |
| 1847 | break; |
| 1848 | case Expression_kind: |
| 1849 | VISIT_IN_SCOPE(c, expr, mod->v.Expression.body); |
| 1850 | addNone = 0; |
| 1851 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1852 | default: |
| 1853 | PyErr_Format(PyExc_SystemError, |
| 1854 | "module kind %d should not be possible", |
| 1855 | mod->kind); |
| 1856 | return 0; |
| 1857 | } |
| 1858 | co = assemble(c, addNone); |
| 1859 | compiler_exit_scope(c); |
| 1860 | return co; |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 1861 | } |
| 1862 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1863 | /* The test for LOCAL must come before the test for FREE in order to |
| 1864 | handle classes where name is both local and free. The local var is |
| 1865 | 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] | 1866 | */ |
| 1867 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1868 | static int |
| 1869 | get_ref_type(struct compiler *c, PyObject *name) |
| 1870 | { |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1871 | int scope; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1872 | if (c->u->u_scope_type == COMPILER_SCOPE_CLASS && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1873 | _PyUnicode_EqualToASCIIString(name, "__class__")) |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 1874 | return CELL; |
Victor Stinner | 0b1bc56 | 2013-05-16 22:17:17 +0200 | [diff] [blame] | 1875 | scope = PyST_GetScope(c->u->u_ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1876 | if (scope == 0) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1877 | _Py_FatalErrorFormat(__func__, |
| 1878 | "unknown scope for %.100s in %.100s(%s)\n" |
| 1879 | "symbols: %s\nlocals: %s\nglobals: %s", |
| 1880 | PyUnicode_AsUTF8(name), |
| 1881 | PyUnicode_AsUTF8(c->u->u_name), |
| 1882 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_id)), |
| 1883 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_ste->ste_symbols)), |
| 1884 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_varnames)), |
| 1885 | PyUnicode_AsUTF8(PyObject_Repr(c->u->u_names))); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1886 | } |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 1887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1888 | return scope; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
| 1891 | static int |
| 1892 | compiler_lookup_arg(PyObject *dict, PyObject *name) |
| 1893 | { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1894 | PyObject *v; |
| 1895 | v = PyDict_GetItem(dict, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1896 | if (v == NULL) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 1897 | return -1; |
Christian Heimes | 217cfd1 | 2007-12-02 14:31:20 +0000 | [diff] [blame] | 1898 | return PyLong_AS_LONG(v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
| 1901 | static int |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1902 | 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] | 1903 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 1904 | Py_ssize_t i, free = PyCode_GetNumFree(co); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 1905 | if (qualname == NULL) |
| 1906 | qualname = co->co_name; |
| 1907 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1908 | if (free) { |
| 1909 | for (i = 0; i < free; ++i) { |
| 1910 | /* Bypass com_addop_varname because it will generate |
| 1911 | LOAD_DEREF but LOAD_CLOSURE is needed. |
| 1912 | */ |
| 1913 | PyObject *name = PyTuple_GET_ITEM(co->co_freevars, i); |
| 1914 | int arg, reftype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1915 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1916 | /* Special case: If a class contains a method with a |
| 1917 | free variable that has the same name as a method, |
| 1918 | the name will be considered free *and* local in the |
| 1919 | class. It should be handled by the closure, as |
Min ho Kim | c4cacc8 | 2019-07-31 08:16:13 +1000 | [diff] [blame] | 1920 | well as by the normal name lookup logic. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1921 | */ |
| 1922 | reftype = get_ref_type(c, name); |
| 1923 | if (reftype == CELL) |
| 1924 | arg = compiler_lookup_arg(c->u->u_cellvars, name); |
| 1925 | else /* (reftype == FREE) */ |
| 1926 | arg = compiler_lookup_arg(c->u->u_freevars, name); |
| 1927 | if (arg == -1) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 1928 | _Py_FatalErrorFormat(__func__, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1929 | "lookup %s in %s %d %d\n" |
| 1930 | "freevars of %s: %s\n", |
| 1931 | PyUnicode_AsUTF8(PyObject_Repr(name)), |
| 1932 | PyUnicode_AsUTF8(c->u->u_name), |
| 1933 | reftype, arg, |
| 1934 | PyUnicode_AsUTF8(co->co_name), |
| 1935 | PyUnicode_AsUTF8(PyObject_Repr(co->co_freevars))); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1936 | } |
| 1937 | ADDOP_I(c, LOAD_CLOSURE, arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1939 | flags |= 0x08; |
| 1940 | ADDOP_I(c, BUILD_TUPLE, free); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 1942 | ADDOP_LOAD_CONST(c, (PyObject*)co); |
| 1943 | ADDOP_LOAD_CONST(c, qualname); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1944 | ADDOP_I(c, MAKE_FUNCTION, flags); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1945 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | static int |
| 1949 | compiler_decorators(struct compiler *c, asdl_seq* decos) |
| 1950 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1951 | int i; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1952 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1953 | if (!decos) |
| 1954 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1955 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1956 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 1957 | VISIT(c, expr, (expr_ty)asdl_seq_GET(decos, i)); |
| 1958 | } |
| 1959 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | static int |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1963 | compiler_visit_kwonlydefaults(struct compiler *c, asdl_seq *kwonlyargs, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1964 | asdl_seq *kw_defaults) |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1965 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1966 | /* Push a dict of keyword-only default values. |
| 1967 | |
| 1968 | Return 0 on error, -1 if no dict pushed, 1 if a dict is pushed. |
| 1969 | */ |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1970 | int i; |
| 1971 | PyObject *keys = NULL; |
| 1972 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1973 | for (i = 0; i < asdl_seq_LEN(kwonlyargs); i++) { |
| 1974 | arg_ty arg = asdl_seq_GET(kwonlyargs, i); |
| 1975 | expr_ty default_ = asdl_seq_GET(kw_defaults, i); |
| 1976 | if (default_) { |
Benjamin Peterson | 32c59b6 | 2012-04-17 19:53:21 -0400 | [diff] [blame] | 1977 | PyObject *mangled = _Py_Mangle(c->u->u_private, arg->arg); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1978 | if (!mangled) { |
| 1979 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1980 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1981 | if (keys == NULL) { |
| 1982 | keys = PyList_New(1); |
| 1983 | if (keys == NULL) { |
| 1984 | Py_DECREF(mangled); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 1985 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 1986 | } |
| 1987 | PyList_SET_ITEM(keys, 0, mangled); |
| 1988 | } |
| 1989 | else { |
| 1990 | int res = PyList_Append(keys, mangled); |
| 1991 | Py_DECREF(mangled); |
| 1992 | if (res == -1) { |
| 1993 | goto error; |
| 1994 | } |
| 1995 | } |
| 1996 | if (!compiler_visit_expr(c, default_)) { |
| 1997 | goto error; |
| 1998 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1999 | } |
| 2000 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2001 | if (keys != NULL) { |
| 2002 | Py_ssize_t default_count = PyList_GET_SIZE(keys); |
| 2003 | PyObject *keys_tuple = PyList_AsTuple(keys); |
| 2004 | Py_DECREF(keys); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2005 | ADDOP_LOAD_CONST_NEW(c, keys_tuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2006 | ADDOP_I(c, BUILD_CONST_KEY_MAP, default_count); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2007 | assert(default_count > 0); |
| 2008 | return 1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2009 | } |
| 2010 | else { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2011 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2012 | } |
| 2013 | |
| 2014 | error: |
| 2015 | Py_XDECREF(keys); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2016 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2017 | } |
| 2018 | |
| 2019 | static int |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2020 | compiler_visit_annexpr(struct compiler *c, expr_ty annotation) |
| 2021 | { |
Serhiy Storchaka | 64fddc4 | 2018-05-17 06:17:48 +0300 | [diff] [blame] | 2022 | ADDOP_LOAD_CONST_NEW(c, _PyAST_ExprAsUnicode(annotation)); |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2023 | return 1; |
| 2024 | } |
| 2025 | |
| 2026 | static int |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2027 | compiler_visit_argannotation(struct compiler *c, identifier id, |
| 2028 | expr_ty annotation, PyObject *names) |
| 2029 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2030 | if (annotation) { |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2031 | PyObject *mangled; |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 2032 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 2033 | VISIT(c, annexpr, annotation) |
| 2034 | } |
| 2035 | else { |
| 2036 | VISIT(c, expr, annotation); |
| 2037 | } |
Victor Stinner | 065efc3 | 2014-02-18 22:07:56 +0100 | [diff] [blame] | 2038 | mangled = _Py_Mangle(c->u->u_private, id); |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2039 | if (!mangled) |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2040 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2041 | if (PyList_Append(names, mangled) < 0) { |
| 2042 | Py_DECREF(mangled); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2043 | return 0; |
Yury Selivanov | 34ce99f | 2014-02-18 12:49:41 -0500 | [diff] [blame] | 2044 | } |
| 2045 | Py_DECREF(mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2046 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2047 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2048 | } |
| 2049 | |
| 2050 | static int |
| 2051 | compiler_visit_argannotations(struct compiler *c, asdl_seq* args, |
| 2052 | PyObject *names) |
| 2053 | { |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2054 | int i; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2055 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 2056 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2057 | if (!compiler_visit_argannotation( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2058 | c, |
| 2059 | arg->arg, |
| 2060 | arg->annotation, |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2061 | names)) |
| 2062 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2063 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2064 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | static int |
| 2068 | compiler_visit_annotations(struct compiler *c, arguments_ty args, |
| 2069 | expr_ty returns) |
| 2070 | { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2071 | /* Push arg annotation dict. |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2072 | The expressions are evaluated out-of-order wrt the source code. |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2073 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2074 | 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] | 2075 | */ |
| 2076 | static identifier return_str; |
| 2077 | PyObject *names; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 2078 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2079 | names = PyList_New(0); |
| 2080 | if (!names) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2081 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2082 | |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2083 | if (!compiler_visit_argannotations(c, args->args, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2084 | goto error; |
Pablo Galindo | a0c01bf | 2019-05-31 15:19:50 +0100 | [diff] [blame] | 2085 | if (!compiler_visit_argannotations(c, args->posonlyargs, names)) |
| 2086 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2087 | if (args->vararg && args->vararg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2088 | !compiler_visit_argannotation(c, args->vararg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2089 | args->vararg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2090 | goto error; |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2091 | if (!compiler_visit_argannotations(c, args->kwonlyargs, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2092 | goto error; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2093 | if (args->kwarg && args->kwarg->annotation && |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2094 | !compiler_visit_argannotation(c, args->kwarg->arg, |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 2095 | args->kwarg->annotation, names)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2096 | goto error; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2097 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2098 | if (!return_str) { |
| 2099 | return_str = PyUnicode_InternFromString("return"); |
| 2100 | if (!return_str) |
| 2101 | goto error; |
| 2102 | } |
Yury Selivanov | f315c1c | 2015-07-23 09:10:44 +0300 | [diff] [blame] | 2103 | if (!compiler_visit_argannotation(c, return_str, returns, names)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2104 | goto error; |
| 2105 | } |
| 2106 | |
| 2107 | len = PyList_GET_SIZE(names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2108 | if (len) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2109 | PyObject *keytuple = PyList_AsTuple(names); |
| 2110 | Py_DECREF(names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2111 | ADDOP_LOAD_CONST_NEW(c, keytuple); |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2112 | ADDOP_I(c, BUILD_CONST_KEY_MAP, len); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2113 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2114 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2115 | else { |
| 2116 | Py_DECREF(names); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2117 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2118 | } |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2119 | |
| 2120 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2121 | Py_DECREF(names); |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 2122 | return 0; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | static int |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2126 | compiler_visit_defaults(struct compiler *c, arguments_ty args) |
| 2127 | { |
| 2128 | VISIT_SEQ(c, expr, args->defaults); |
| 2129 | ADDOP_I(c, BUILD_TUPLE, asdl_seq_LEN(args->defaults)); |
| 2130 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2131 | } |
| 2132 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2133 | static Py_ssize_t |
| 2134 | compiler_default_arguments(struct compiler *c, arguments_ty args) |
| 2135 | { |
| 2136 | Py_ssize_t funcflags = 0; |
| 2137 | if (args->defaults && asdl_seq_LEN(args->defaults) > 0) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2138 | if (!compiler_visit_defaults(c, args)) |
| 2139 | return -1; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2140 | funcflags |= 0x01; |
| 2141 | } |
| 2142 | if (args->kwonlyargs) { |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2143 | int res = compiler_visit_kwonlydefaults(c, args->kwonlyargs, |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2144 | args->kw_defaults); |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2145 | if (res == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2146 | return -1; |
| 2147 | } |
| 2148 | else if (res > 0) { |
| 2149 | funcflags |= 0x02; |
| 2150 | } |
| 2151 | } |
| 2152 | return funcflags; |
| 2153 | } |
| 2154 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2155 | static int |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2156 | forbidden_name(struct compiler *c, identifier name, expr_context_ty ctx) |
| 2157 | { |
| 2158 | |
| 2159 | if (ctx == Store && _PyUnicode_EqualToASCIIString(name, "__debug__")) { |
| 2160 | compiler_error(c, "cannot assign to __debug__"); |
| 2161 | return 1; |
| 2162 | } |
| 2163 | return 0; |
| 2164 | } |
| 2165 | |
| 2166 | static int |
| 2167 | compiler_check_debug_one_arg(struct compiler *c, arg_ty arg) |
| 2168 | { |
| 2169 | if (arg != NULL) { |
| 2170 | if (forbidden_name(c, arg->arg, Store)) |
| 2171 | return 0; |
| 2172 | } |
| 2173 | return 1; |
| 2174 | } |
| 2175 | |
| 2176 | static int |
| 2177 | compiler_check_debug_args_seq(struct compiler *c, asdl_seq *args) |
| 2178 | { |
| 2179 | if (args != NULL) { |
Pablo Galindo | ee40e4b | 2020-04-23 03:43:08 +0100 | [diff] [blame] | 2180 | for (Py_ssize_t i = 0, n = asdl_seq_LEN(args); i < n; i++) { |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2181 | if (!compiler_check_debug_one_arg(c, asdl_seq_GET(args, i))) |
| 2182 | return 0; |
| 2183 | } |
| 2184 | } |
| 2185 | return 1; |
| 2186 | } |
| 2187 | |
| 2188 | static int |
| 2189 | compiler_check_debug_args(struct compiler *c, arguments_ty args) |
| 2190 | { |
| 2191 | if (!compiler_check_debug_args_seq(c, args->posonlyargs)) |
| 2192 | return 0; |
| 2193 | if (!compiler_check_debug_args_seq(c, args->args)) |
| 2194 | return 0; |
| 2195 | if (!compiler_check_debug_one_arg(c, args->vararg)) |
| 2196 | return 0; |
| 2197 | if (!compiler_check_debug_args_seq(c, args->kwonlyargs)) |
| 2198 | return 0; |
| 2199 | if (!compiler_check_debug_one_arg(c, args->kwarg)) |
| 2200 | return 0; |
| 2201 | return 1; |
| 2202 | } |
| 2203 | |
| 2204 | static int |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2205 | compiler_function(struct compiler *c, stmt_ty s, int is_async) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2206 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2207 | PyCodeObject *co; |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2208 | PyObject *qualname, *docstring = NULL; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2209 | arguments_ty args; |
| 2210 | expr_ty returns; |
| 2211 | identifier name; |
| 2212 | asdl_seq* decos; |
| 2213 | asdl_seq *body; |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2214 | Py_ssize_t i, funcflags; |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2215 | int annotations; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2216 | int scope_type; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2217 | int firstlineno; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2218 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2219 | if (is_async) { |
| 2220 | assert(s->kind == AsyncFunctionDef_kind); |
| 2221 | |
| 2222 | args = s->v.AsyncFunctionDef.args; |
| 2223 | returns = s->v.AsyncFunctionDef.returns; |
| 2224 | decos = s->v.AsyncFunctionDef.decorator_list; |
| 2225 | name = s->v.AsyncFunctionDef.name; |
| 2226 | body = s->v.AsyncFunctionDef.body; |
| 2227 | |
| 2228 | scope_type = COMPILER_SCOPE_ASYNC_FUNCTION; |
| 2229 | } else { |
| 2230 | assert(s->kind == FunctionDef_kind); |
| 2231 | |
| 2232 | args = s->v.FunctionDef.args; |
| 2233 | returns = s->v.FunctionDef.returns; |
| 2234 | decos = s->v.FunctionDef.decorator_list; |
| 2235 | name = s->v.FunctionDef.name; |
| 2236 | body = s->v.FunctionDef.body; |
| 2237 | |
| 2238 | scope_type = COMPILER_SCOPE_FUNCTION; |
| 2239 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2240 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2241 | if (!compiler_check_debug_args(c, args)) |
| 2242 | return 0; |
| 2243 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2244 | if (!compiler_decorators(c, decos)) |
| 2245 | return 0; |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 2246 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2247 | firstlineno = s->lineno; |
| 2248 | if (asdl_seq_LEN(decos)) { |
| 2249 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2250 | } |
| 2251 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2252 | funcflags = compiler_default_arguments(c, args); |
| 2253 | if (funcflags == -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2254 | return 0; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2255 | } |
| 2256 | |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2257 | annotations = compiler_visit_annotations(c, args, returns); |
| 2258 | if (annotations == 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2259 | return 0; |
| 2260 | } |
Serhiy Storchaka | 607f8a5 | 2016-06-15 20:07:53 +0300 | [diff] [blame] | 2261 | else if (annotations > 0) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2262 | funcflags |= 0x04; |
| 2263 | } |
| 2264 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2265 | if (!compiler_enter_scope(c, name, scope_type, (void *)s, firstlineno)) { |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2266 | return 0; |
| 2267 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2268 | |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2269 | /* if not -OO mode, add docstring */ |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2270 | if (c->c_optimize < 2) { |
| 2271 | docstring = _PyAST_GetDocString(body); |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2272 | } |
Serhiy Storchaka | 143ce5c | 2018-05-30 10:56:16 +0300 | [diff] [blame] | 2273 | if (compiler_add_const(c, docstring ? docstring : Py_None) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2274 | compiler_exit_scope(c); |
| 2275 | return 0; |
| 2276 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2278 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2279 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2280 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
INADA Naoki | cb41b27 | 2017-02-23 00:31:59 +0900 | [diff] [blame] | 2281 | VISIT_SEQ_IN_SCOPE(c, stmt, body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2282 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2283 | qualname = c->u->u_qualname; |
| 2284 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2285 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2286 | if (co == NULL) { |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2287 | Py_XDECREF(qualname); |
| 2288 | Py_XDECREF(co); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2289 | return 0; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2290 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2291 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2292 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2293 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2294 | Py_DECREF(co); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2295 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2296 | /* decorators */ |
| 2297 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2298 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2299 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2300 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2301 | return compiler_nameop(c, name, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
| 2304 | static int |
| 2305 | compiler_class(struct compiler *c, stmt_ty s) |
| 2306 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2307 | PyCodeObject *co; |
| 2308 | PyObject *str; |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2309 | int i, firstlineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2310 | asdl_seq* decos = s->v.ClassDef.decorator_list; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2311 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2312 | if (!compiler_decorators(c, decos)) |
| 2313 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2314 | |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2315 | firstlineno = s->lineno; |
| 2316 | if (asdl_seq_LEN(decos)) { |
| 2317 | firstlineno = ((expr_ty)asdl_seq_GET(decos, 0))->lineno; |
| 2318 | } |
| 2319 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2320 | /* ultimately generate code for: |
| 2321 | <name> = __build_class__(<func>, <name>, *<bases>, **<keywords>) |
| 2322 | where: |
| 2323 | <func> is a function/closure created from the class body; |
| 2324 | it has a single argument (__locals__) where the dict |
| 2325 | (or MutableSequence) representing the locals is passed |
| 2326 | <name> is the class name |
| 2327 | <bases> is the positional arguments and *varargs argument |
| 2328 | <keywords> is the keyword arguments and **kwds argument |
| 2329 | This borrows from compiler_call. |
| 2330 | */ |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 2331 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2332 | /* 1. compile the class body into a code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2333 | if (!compiler_enter_scope(c, s->v.ClassDef.name, |
Serhiy Storchaka | 95b6acf | 2018-10-30 13:16:02 +0200 | [diff] [blame] | 2334 | COMPILER_SCOPE_CLASS, (void *)s, firstlineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2335 | return 0; |
| 2336 | /* this block represents what we do in the new scope */ |
| 2337 | { |
| 2338 | /* use the class name for name mangling */ |
| 2339 | Py_INCREF(s->v.ClassDef.name); |
Serhiy Storchaka | 4884271 | 2016-04-06 09:45:48 +0300 | [diff] [blame] | 2340 | Py_XSETREF(c->u->u_private, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2341 | /* load (global) __name__ ... */ |
| 2342 | str = PyUnicode_InternFromString("__name__"); |
| 2343 | if (!str || !compiler_nameop(c, str, Load)) { |
| 2344 | Py_XDECREF(str); |
| 2345 | compiler_exit_scope(c); |
| 2346 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2347 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2348 | Py_DECREF(str); |
| 2349 | /* ... and store it as __module__ */ |
| 2350 | str = PyUnicode_InternFromString("__module__"); |
| 2351 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2352 | Py_XDECREF(str); |
| 2353 | compiler_exit_scope(c); |
| 2354 | return 0; |
| 2355 | } |
| 2356 | Py_DECREF(str); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2357 | assert(c->u->u_qualname); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2358 | ADDOP_LOAD_CONST(c, c->u->u_qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2359 | str = PyUnicode_InternFromString("__qualname__"); |
| 2360 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2361 | Py_XDECREF(str); |
| 2362 | compiler_exit_scope(c); |
| 2363 | return 0; |
| 2364 | } |
| 2365 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2366 | /* compile the body proper */ |
Serhiy Storchaka | 73cbe7a | 2018-05-29 12:04:55 +0300 | [diff] [blame] | 2367 | if (!compiler_body(c, s->v.ClassDef.body)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2368 | compiler_exit_scope(c); |
| 2369 | return 0; |
| 2370 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2371 | /* Return __classcell__ if it is referenced, otherwise return None */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2372 | if (c->u->u_ste->ste_needs_class_closure) { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2373 | /* Store __classcell__ into class namespace & return it */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2374 | str = PyUnicode_InternFromString("__class__"); |
| 2375 | if (str == NULL) { |
| 2376 | compiler_exit_scope(c); |
| 2377 | return 0; |
| 2378 | } |
| 2379 | i = compiler_lookup_arg(c->u->u_cellvars, str); |
| 2380 | Py_DECREF(str); |
Victor Stinner | 98e818b | 2013-11-05 18:07:34 +0100 | [diff] [blame] | 2381 | if (i < 0) { |
| 2382 | compiler_exit_scope(c); |
| 2383 | return 0; |
| 2384 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2385 | assert(i == 0); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2386 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2387 | ADDOP_I(c, LOAD_CLOSURE, i); |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2388 | ADDOP(c, DUP_TOP); |
Nick Coghlan | 944368e | 2016-09-11 14:45:49 +1000 | [diff] [blame] | 2389 | str = PyUnicode_InternFromString("__classcell__"); |
| 2390 | if (!str || !compiler_nameop(c, str, Store)) { |
| 2391 | Py_XDECREF(str); |
| 2392 | compiler_exit_scope(c); |
| 2393 | return 0; |
| 2394 | } |
| 2395 | Py_DECREF(str); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2396 | } |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2397 | else { |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2398 | /* No methods referenced __class__, so just return None */ |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 2399 | assert(PyDict_GET_SIZE(c->u->u_cellvars) == 0); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2400 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 2401 | } |
Nick Coghlan | 19d2467 | 2016-12-05 16:47:55 +1000 | [diff] [blame] | 2402 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2403 | /* create the code object */ |
| 2404 | co = assemble(c, 1); |
| 2405 | } |
| 2406 | /* leave the new scope */ |
| 2407 | compiler_exit_scope(c); |
| 2408 | if (co == NULL) |
| 2409 | return 0; |
Guido van Rossum | d59da4b | 2007-05-22 18:11:13 +0000 | [diff] [blame] | 2410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2411 | /* 2. load the 'build_class' function */ |
| 2412 | ADDOP(c, LOAD_BUILD_CLASS); |
| 2413 | |
| 2414 | /* 3. load a function (or closure) made from the code object */ |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2415 | compiler_make_closure(c, co, 0, NULL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2416 | Py_DECREF(co); |
| 2417 | |
| 2418 | /* 4. load class name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2419 | ADDOP_LOAD_CONST(c, s->v.ClassDef.name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2420 | |
| 2421 | /* 5. generate the rest of the code for the call */ |
| 2422 | if (!compiler_call_helper(c, 2, |
| 2423 | s->v.ClassDef.bases, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 2424 | s->v.ClassDef.keywords)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2425 | return 0; |
| 2426 | |
| 2427 | /* 6. apply decorators */ |
| 2428 | for (i = 0; i < asdl_seq_LEN(decos); i++) { |
| 2429 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 2430 | } |
| 2431 | |
| 2432 | /* 7. store into <name> */ |
| 2433 | if (!compiler_nameop(c, s->v.ClassDef.name, Store)) |
| 2434 | return 0; |
| 2435 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2436 | } |
| 2437 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 2438 | /* Return 0 if the expression is a constant value except named singletons. |
| 2439 | Return 1 otherwise. */ |
| 2440 | static int |
| 2441 | check_is_arg(expr_ty e) |
| 2442 | { |
| 2443 | if (e->kind != Constant_kind) { |
| 2444 | return 1; |
| 2445 | } |
| 2446 | PyObject *value = e->v.Constant.value; |
| 2447 | return (value == Py_None |
| 2448 | || value == Py_False |
| 2449 | || value == Py_True |
| 2450 | || value == Py_Ellipsis); |
| 2451 | } |
| 2452 | |
| 2453 | /* Check operands of identity chacks ("is" and "is not"). |
| 2454 | Emit a warning if any operand is a constant except named singletons. |
| 2455 | Return 0 on error. |
| 2456 | */ |
| 2457 | static int |
| 2458 | check_compare(struct compiler *c, expr_ty e) |
| 2459 | { |
| 2460 | Py_ssize_t i, n; |
| 2461 | int left = check_is_arg(e->v.Compare.left); |
| 2462 | n = asdl_seq_LEN(e->v.Compare.ops); |
| 2463 | for (i = 0; i < n; i++) { |
| 2464 | cmpop_ty op = (cmpop_ty)asdl_seq_GET(e->v.Compare.ops, i); |
| 2465 | int right = check_is_arg((expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2466 | if (op == Is || op == IsNot) { |
| 2467 | if (!right || !left) { |
| 2468 | const char *msg = (op == Is) |
| 2469 | ? "\"is\" with a literal. Did you mean \"==\"?" |
| 2470 | : "\"is not\" with a literal. Did you mean \"!=\"?"; |
| 2471 | return compiler_warn(c, msg); |
| 2472 | } |
| 2473 | } |
| 2474 | left = right; |
| 2475 | } |
| 2476 | return 1; |
| 2477 | } |
| 2478 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2479 | static int compiler_addcompare(struct compiler *c, cmpop_ty op) |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2480 | { |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2481 | int cmp; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2482 | switch (op) { |
| 2483 | case Eq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2484 | cmp = Py_EQ; |
| 2485 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2486 | case NotEq: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2487 | cmp = Py_NE; |
| 2488 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2489 | case Lt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2490 | cmp = Py_LT; |
| 2491 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2492 | case LtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2493 | cmp = Py_LE; |
| 2494 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2495 | case Gt: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2496 | cmp = Py_GT; |
| 2497 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2498 | case GtE: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2499 | cmp = Py_GE; |
| 2500 | break; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2501 | case Is: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2502 | ADDOP_I(c, IS_OP, 0); |
| 2503 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2504 | case IsNot: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2505 | ADDOP_I(c, IS_OP, 1); |
| 2506 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2507 | case In: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2508 | ADDOP_I(c, CONTAINS_OP, 0); |
| 2509 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2510 | case NotIn: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2511 | ADDOP_I(c, CONTAINS_OP, 1); |
| 2512 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2513 | default: |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2514 | Py_UNREACHABLE(); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2515 | } |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2516 | ADDOP_I(c, COMPARE_OP, cmp); |
| 2517 | return 1; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2518 | } |
| 2519 | |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2520 | |
| 2521 | |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2522 | static int |
| 2523 | compiler_jump_if(struct compiler *c, expr_ty e, basicblock *next, int cond) |
| 2524 | { |
| 2525 | switch (e->kind) { |
| 2526 | case UnaryOp_kind: |
| 2527 | if (e->v.UnaryOp.op == Not) |
| 2528 | return compiler_jump_if(c, e->v.UnaryOp.operand, next, !cond); |
| 2529 | /* fallback to general implementation */ |
| 2530 | break; |
| 2531 | case BoolOp_kind: { |
| 2532 | asdl_seq *s = e->v.BoolOp.values; |
| 2533 | Py_ssize_t i, n = asdl_seq_LEN(s) - 1; |
| 2534 | assert(n >= 0); |
| 2535 | int cond2 = e->v.BoolOp.op == Or; |
| 2536 | basicblock *next2 = next; |
| 2537 | if (!cond2 != !cond) { |
| 2538 | next2 = compiler_new_block(c); |
| 2539 | if (next2 == NULL) |
| 2540 | return 0; |
| 2541 | } |
| 2542 | for (i = 0; i < n; ++i) { |
| 2543 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, i), next2, cond2)) |
| 2544 | return 0; |
| 2545 | } |
| 2546 | if (!compiler_jump_if(c, (expr_ty)asdl_seq_GET(s, n), next, cond)) |
| 2547 | return 0; |
| 2548 | if (next2 != next) |
| 2549 | compiler_use_next_block(c, next2); |
| 2550 | return 1; |
| 2551 | } |
| 2552 | case IfExp_kind: { |
| 2553 | basicblock *end, *next2; |
| 2554 | end = compiler_new_block(c); |
| 2555 | if (end == NULL) |
| 2556 | return 0; |
| 2557 | next2 = compiler_new_block(c); |
| 2558 | if (next2 == NULL) |
| 2559 | return 0; |
| 2560 | if (!compiler_jump_if(c, e->v.IfExp.test, next2, 0)) |
| 2561 | return 0; |
| 2562 | if (!compiler_jump_if(c, e->v.IfExp.body, next, cond)) |
| 2563 | return 0; |
| 2564 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2565 | compiler_use_next_block(c, next2); |
| 2566 | if (!compiler_jump_if(c, e->v.IfExp.orelse, next, cond)) |
| 2567 | return 0; |
| 2568 | compiler_use_next_block(c, end); |
| 2569 | return 1; |
| 2570 | } |
| 2571 | case Compare_kind: { |
| 2572 | Py_ssize_t i, n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 2573 | if (n > 0) { |
Serhiy Storchaka | 4583525 | 2019-02-16 08:29:46 +0200 | [diff] [blame] | 2574 | if (!check_compare(c, e)) { |
| 2575 | return 0; |
| 2576 | } |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2577 | basicblock *cleanup = compiler_new_block(c); |
| 2578 | if (cleanup == NULL) |
| 2579 | return 0; |
| 2580 | VISIT(c, expr, e->v.Compare.left); |
| 2581 | for (i = 0; i < n; i++) { |
| 2582 | VISIT(c, expr, |
| 2583 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
| 2584 | ADDOP(c, DUP_TOP); |
| 2585 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 2586 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2587 | ADDOP_JABS(c, POP_JUMP_IF_FALSE, cleanup); |
| 2588 | NEXT_BLOCK(c); |
| 2589 | } |
| 2590 | 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] | 2591 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2592 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2593 | basicblock *end = compiler_new_block(c); |
| 2594 | if (end == NULL) |
| 2595 | return 0; |
| 2596 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2597 | compiler_use_next_block(c, cleanup); |
| 2598 | ADDOP(c, POP_TOP); |
| 2599 | if (!cond) { |
| 2600 | ADDOP_JREL(c, JUMP_FORWARD, next); |
| 2601 | } |
| 2602 | compiler_use_next_block(c, end); |
| 2603 | return 1; |
| 2604 | } |
| 2605 | /* fallback to general implementation */ |
| 2606 | break; |
| 2607 | } |
| 2608 | default: |
| 2609 | /* fallback to general implementation */ |
| 2610 | break; |
| 2611 | } |
| 2612 | |
| 2613 | /* general implementation */ |
| 2614 | VISIT(c, expr, e); |
| 2615 | ADDOP_JABS(c, cond ? POP_JUMP_IF_TRUE : POP_JUMP_IF_FALSE, next); |
| 2616 | return 1; |
| 2617 | } |
| 2618 | |
| 2619 | static int |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2620 | compiler_ifexp(struct compiler *c, expr_ty e) |
| 2621 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2622 | basicblock *end, *next; |
| 2623 | |
| 2624 | assert(e->kind == IfExp_kind); |
| 2625 | end = compiler_new_block(c); |
| 2626 | if (end == NULL) |
| 2627 | return 0; |
| 2628 | next = compiler_new_block(c); |
| 2629 | if (next == NULL) |
| 2630 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2631 | if (!compiler_jump_if(c, e->v.IfExp.test, next, 0)) |
| 2632 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2633 | VISIT(c, expr, e->v.IfExp.body); |
| 2634 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 2635 | compiler_use_next_block(c, next); |
| 2636 | VISIT(c, expr, e->v.IfExp.orelse); |
| 2637 | compiler_use_next_block(c, end); |
| 2638 | return 1; |
Thomas Wouters | dca3b9c | 2006-02-27 00:24:13 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2642 | compiler_lambda(struct compiler *c, expr_ty e) |
| 2643 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2644 | PyCodeObject *co; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2645 | PyObject *qualname; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2646 | static identifier name; |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2647 | Py_ssize_t funcflags; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2648 | arguments_ty args = e->v.Lambda.args; |
| 2649 | assert(e->kind == Lambda_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2650 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 2651 | if (!compiler_check_debug_args(c, args)) |
| 2652 | return 0; |
| 2653 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2654 | if (!name) { |
| 2655 | name = PyUnicode_InternFromString("<lambda>"); |
| 2656 | if (!name) |
| 2657 | return 0; |
| 2658 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2659 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2660 | funcflags = compiler_default_arguments(c, args); |
| 2661 | if (funcflags == -1) { |
| 2662 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2663 | } |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2664 | |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2665 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_LAMBDA, |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2666 | (void *)e, e->lineno)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2667 | return 0; |
Neal Norwitz | 4737b23 | 2005-11-19 23:58:29 +0000 | [diff] [blame] | 2668 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2669 | /* Make None the first constant, so the lambda can't have a |
| 2670 | docstring. */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2671 | if (compiler_add_const(c, Py_None) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2672 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2673 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2674 | c->u->u_argcount = asdl_seq_LEN(args->args); |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 2675 | c->u->u_posonlyargcount = asdl_seq_LEN(args->posonlyargs); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2676 | c->u->u_kwonlyargcount = asdl_seq_LEN(args->kwonlyargs); |
| 2677 | VISIT_IN_SCOPE(c, expr, e->v.Lambda.body); |
| 2678 | if (c->u->u_ste->ste_generator) { |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2679 | co = assemble(c, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2680 | } |
| 2681 | else { |
| 2682 | ADDOP_IN_SCOPE(c, RETURN_VALUE); |
Serhiy Storchaka | c775ad6 | 2015-03-11 18:20:35 +0200 | [diff] [blame] | 2683 | co = assemble(c, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2684 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2685 | qualname = c->u->u_qualname; |
| 2686 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2687 | compiler_exit_scope(c); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 2688 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2689 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2690 | |
Serhiy Storchaka | 64204de | 2016-06-12 17:36:24 +0300 | [diff] [blame] | 2691 | compiler_make_closure(c, co, funcflags, qualname); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 2692 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2693 | Py_DECREF(co); |
| 2694 | |
| 2695 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
| 2698 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2699 | compiler_if(struct compiler *c, stmt_ty s) |
| 2700 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2701 | basicblock *end, *next; |
| 2702 | int constant; |
| 2703 | assert(s->kind == If_kind); |
| 2704 | end = compiler_new_block(c); |
| 2705 | if (end == NULL) |
| 2706 | return 0; |
| 2707 | |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2708 | constant = expr_constant(s->v.If.test); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2709 | /* constant = 0: "if 0" |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2710 | * constant = 1: "if 1", "if 2", ... |
| 2711 | * constant = -1: rest */ |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2712 | if (constant == 0) { |
| 2713 | BEGIN_DO_NOT_EMIT_BYTECODE |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2714 | VISIT_SEQ(c, stmt, s->v.If.body); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2715 | END_DO_NOT_EMIT_BYTECODE |
| 2716 | if (s->v.If.orelse) { |
| 2717 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2718 | } |
| 2719 | } else if (constant == 1) { |
| 2720 | VISIT_SEQ(c, stmt, s->v.If.body); |
| 2721 | if (s->v.If.orelse) { |
| 2722 | BEGIN_DO_NOT_EMIT_BYTECODE |
| 2723 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2724 | END_DO_NOT_EMIT_BYTECODE |
| 2725 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2726 | } else { |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2727 | if (asdl_seq_LEN(s->v.If.orelse)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2728 | next = compiler_new_block(c); |
| 2729 | if (next == NULL) |
| 2730 | return 0; |
| 2731 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2732 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2733 | next = end; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2734 | } |
| 2735 | if (!compiler_jump_if(c, s->v.If.test, next, 0)) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2736 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2737 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2738 | VISIT_SEQ(c, stmt, s->v.If.body); |
Antoine Pitrou | e7811fc | 2014-09-18 03:06:50 +0200 | [diff] [blame] | 2739 | if (asdl_seq_LEN(s->v.If.orelse)) { |
| 2740 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2741 | compiler_use_next_block(c, next); |
| 2742 | VISIT_SEQ(c, stmt, s->v.If.orelse); |
| 2743 | } |
| 2744 | } |
| 2745 | compiler_use_next_block(c, end); |
| 2746 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2747 | } |
| 2748 | |
| 2749 | static int |
| 2750 | compiler_for(struct compiler *c, stmt_ty s) |
| 2751 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2752 | basicblock *start, *cleanup, *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2753 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2754 | start = compiler_new_block(c); |
| 2755 | cleanup = compiler_new_block(c); |
| 2756 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2757 | if (start == NULL || end == NULL || cleanup == NULL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2758 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2759 | } |
| 2760 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2761 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2762 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2763 | VISIT(c, expr, s->v.For.iter); |
| 2764 | ADDOP(c, GET_ITER); |
| 2765 | compiler_use_next_block(c, start); |
| 2766 | ADDOP_JREL(c, FOR_ITER, cleanup); |
| 2767 | VISIT(c, expr, s->v.For.target); |
| 2768 | VISIT_SEQ(c, stmt, s->v.For.body); |
| 2769 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2770 | compiler_use_next_block(c, cleanup); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2771 | |
| 2772 | compiler_pop_fblock(c, FOR_LOOP, start); |
| 2773 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2774 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2775 | compiler_use_next_block(c, end); |
| 2776 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2777 | } |
| 2778 | |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2779 | |
| 2780 | static int |
| 2781 | compiler_async_for(struct compiler *c, stmt_ty s) |
| 2782 | { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2783 | basicblock *start, *except, *end; |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 2784 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 2785 | c->u->u_ste->ste_coroutine = 1; |
| 2786 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION) { |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 2787 | return compiler_error(c, "'async for' outside async function"); |
| 2788 | } |
| 2789 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2790 | start = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2791 | except = compiler_new_block(c); |
| 2792 | end = compiler_new_block(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2793 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2794 | if (start == NULL || except == NULL || end == NULL) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2795 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2796 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2797 | VISIT(c, expr, s->v.AsyncFor.iter); |
| 2798 | ADDOP(c, GET_AITER); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2799 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2800 | compiler_use_next_block(c, start); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2801 | if (!compiler_push_fblock(c, FOR_LOOP, start, end, NULL)) { |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2802 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2803 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2804 | /* SETUP_FINALLY to guard the __anext__ call */ |
| 2805 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2806 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2807 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2808 | ADDOP(c, YIELD_FROM); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2809 | ADDOP(c, POP_BLOCK); /* for SETUP_FINALLY */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2810 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2811 | /* Success block for __anext__ */ |
| 2812 | VISIT(c, expr, s->v.AsyncFor.target); |
| 2813 | VISIT_SEQ(c, stmt, s->v.AsyncFor.body); |
| 2814 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 2815 | |
| 2816 | compiler_pop_fblock(c, FOR_LOOP, start); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2817 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2818 | /* Except block for __anext__ */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2819 | compiler_use_next_block(c, except); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 2820 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2821 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2822 | /* `else` block */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 2823 | VISIT_SEQ(c, stmt, s->v.For.orelse); |
| 2824 | |
| 2825 | compiler_use_next_block(c, end); |
| 2826 | |
| 2827 | return 1; |
| 2828 | } |
| 2829 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2830 | static int |
| 2831 | compiler_while(struct compiler *c, stmt_ty s) |
| 2832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2833 | basicblock *loop, *orelse, *end, *anchor = NULL; |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 2834 | int constant = expr_constant(s->v.While.test); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2835 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2836 | if (constant == 0) { |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2837 | BEGIN_DO_NOT_EMIT_BYTECODE |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2838 | // Push a dummy block so the VISIT_SEQ knows that we are |
| 2839 | // inside a while loop so it can correctly evaluate syntax |
| 2840 | // errors. |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2841 | if (!compiler_push_fblock(c, WHILE_LOOP, NULL, NULL, NULL)) { |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2842 | return 0; |
| 2843 | } |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2844 | VISIT_SEQ(c, stmt, s->v.While.body); |
Pablo Galindo | 6c3e66a | 2019-10-30 11:53:26 +0000 | [diff] [blame] | 2845 | // Remove the dummy block now that is not needed. |
| 2846 | compiler_pop_fblock(c, WHILE_LOOP, NULL); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2847 | END_DO_NOT_EMIT_BYTECODE |
| 2848 | if (s->v.While.orelse) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2849 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
Pablo Galindo | 18c5f9d | 2019-07-15 10:15:01 +0100 | [diff] [blame] | 2850 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2851 | return 1; |
| 2852 | } |
| 2853 | loop = compiler_new_block(c); |
| 2854 | end = compiler_new_block(c); |
| 2855 | if (constant == -1) { |
| 2856 | anchor = compiler_new_block(c); |
| 2857 | if (anchor == NULL) |
| 2858 | return 0; |
| 2859 | } |
| 2860 | if (loop == NULL || end == NULL) |
| 2861 | return 0; |
| 2862 | if (s->v.While.orelse) { |
| 2863 | orelse = compiler_new_block(c); |
| 2864 | if (orelse == NULL) |
| 2865 | return 0; |
| 2866 | } |
| 2867 | else |
| 2868 | orelse = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2869 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2870 | compiler_use_next_block(c, loop); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2871 | if (!compiler_push_fblock(c, WHILE_LOOP, loop, end, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2872 | return 0; |
| 2873 | if (constant == -1) { |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 2874 | if (!compiler_jump_if(c, s->v.While.test, anchor, 0)) |
| 2875 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2876 | } |
| 2877 | VISIT_SEQ(c, stmt, s->v.While.body); |
| 2878 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2879 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2880 | /* XXX should the two POP instructions be in a separate block |
| 2881 | if there is no else clause ? |
| 2882 | */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2883 | |
Benjamin Peterson | 3cda0ed | 2014-12-13 16:06:19 -0500 | [diff] [blame] | 2884 | if (constant == -1) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2885 | compiler_use_next_block(c, anchor); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2886 | compiler_pop_fblock(c, WHILE_LOOP, loop); |
| 2887 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2888 | if (orelse != NULL) /* what if orelse is just pass? */ |
| 2889 | VISIT_SEQ(c, stmt, s->v.While.orelse); |
| 2890 | compiler_use_next_block(c, end); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2891 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2892 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2893 | } |
| 2894 | |
| 2895 | static int |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2896 | compiler_return(struct compiler *c, stmt_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2897 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2898 | int preserve_tos = ((s->v.Return.value != NULL) && |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 2899 | (s->v.Return.value->kind != Constant_kind)); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2900 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 2901 | return compiler_error(c, "'return' outside function"); |
| 2902 | if (s->v.Return.value != NULL && |
| 2903 | c->u->u_ste->ste_coroutine && c->u->u_ste->ste_generator) |
| 2904 | { |
| 2905 | return compiler_error( |
| 2906 | c, "'return' with value in async generator"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2907 | } |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2908 | if (preserve_tos) { |
| 2909 | VISIT(c, expr, s->v.Return.value); |
| 2910 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2911 | if (!compiler_unwind_fblock_stack(c, preserve_tos, NULL)) |
| 2912 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2913 | if (s->v.Return.value == NULL) { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 2914 | ADDOP_LOAD_CONST(c, Py_None); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2915 | } |
| 2916 | else if (!preserve_tos) { |
| 2917 | VISIT(c, expr, s->v.Return.value); |
| 2918 | } |
| 2919 | ADDOP(c, RETURN_VALUE); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2920 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2921 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2922 | } |
| 2923 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2924 | static int |
| 2925 | compiler_break(struct compiler *c) |
| 2926 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2927 | struct fblockinfo *loop = NULL; |
| 2928 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2929 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2930 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2931 | if (loop == NULL) { |
| 2932 | return compiler_error(c, "'break' outside loop"); |
| 2933 | } |
| 2934 | if (!compiler_unwind_fblock(c, loop, 0)) { |
| 2935 | return 0; |
| 2936 | } |
| 2937 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_exit); |
| 2938 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2939 | } |
| 2940 | |
| 2941 | static int |
| 2942 | compiler_continue(struct compiler *c) |
| 2943 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2944 | struct fblockinfo *loop = NULL; |
| 2945 | if (!compiler_unwind_fblock_stack(c, 0, &loop)) { |
| 2946 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2947 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2948 | if (loop == NULL) { |
| 2949 | return compiler_error(c, "'continue' not properly in loop"); |
| 2950 | } |
| 2951 | ADDOP_JABS(c, JUMP_ABSOLUTE, loop->fb_block); |
| 2952 | return 1; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2953 | } |
| 2954 | |
| 2955 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2956 | /* Code generated for "try: <body> finally: <finalbody>" is as follows: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2957 | |
| 2958 | SETUP_FINALLY L |
| 2959 | <code for body> |
| 2960 | POP_BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2961 | <code for finalbody> |
| 2962 | JUMP E |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2963 | L: |
| 2964 | <code for finalbody> |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2965 | E: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2966 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2967 | The special instructions use the block stack. Each block |
| 2968 | stack entry contains the instruction that created it (here |
| 2969 | SETUP_FINALLY), the level of the value stack at the time the |
| 2970 | block stack entry was created, and a label (here L). |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2971 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2972 | SETUP_FINALLY: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2973 | Pushes the current value stack level and the label |
| 2974 | onto the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2975 | POP_BLOCK: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2976 | Pops en entry from the block stack. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2977 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2978 | The block stack is unwound when an exception is raised: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2979 | when a SETUP_FINALLY entry is found, the raised and the caught |
| 2980 | exceptions are pushed onto the value stack (and the exception |
| 2981 | condition is cleared), and the interpreter jumps to the label |
| 2982 | gotten from the block stack. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2983 | */ |
| 2984 | |
| 2985 | static int |
| 2986 | compiler_try_finally(struct compiler *c, stmt_ty s) |
| 2987 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2988 | basicblock *body, *end, *exit; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2989 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2990 | body = compiler_new_block(c); |
| 2991 | end = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2992 | exit = compiler_new_block(c); |
| 2993 | if (body == NULL || end == NULL || exit == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2994 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 2995 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 2996 | /* `try` block */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 2997 | ADDOP_JREL(c, SETUP_FINALLY, end); |
| 2998 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 2999 | 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] | 3000 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3001 | if (s->v.Try.handlers && asdl_seq_LEN(s->v.Try.handlers)) { |
| 3002 | if (!compiler_try_except(c, s)) |
| 3003 | return 0; |
| 3004 | } |
| 3005 | else { |
| 3006 | VISIT_SEQ(c, stmt, s->v.Try.body); |
| 3007 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3008 | ADDOP(c, POP_BLOCK); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3009 | compiler_pop_fblock(c, FINALLY_TRY, body); |
| 3010 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3011 | ADDOP_JREL(c, JUMP_FORWARD, exit); |
| 3012 | /* `finally` block */ |
| 3013 | compiler_use_next_block(c, end); |
| 3014 | if (!compiler_push_fblock(c, FINALLY_END, end, NULL, NULL)) |
| 3015 | return 0; |
| 3016 | VISIT_SEQ(c, stmt, s->v.Try.finalbody); |
| 3017 | compiler_pop_fblock(c, FINALLY_END, end); |
| 3018 | ADDOP(c, RERAISE); |
| 3019 | compiler_use_next_block(c, exit); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3020 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | /* |
Jeffrey Yasskin | 9de7ec7 | 2009-02-25 02:25:04 +0000 | [diff] [blame] | 3024 | 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] | 3025 | (The contents of the value stack is shown in [], with the top |
| 3026 | at the right; 'tb' is trace-back info, 'val' the exception's |
| 3027 | associated value, and 'exc' the exception.) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3028 | |
| 3029 | Value stack Label Instruction Argument |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3030 | [] SETUP_FINALLY L1 |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3031 | [] <code for S> |
| 3032 | [] POP_BLOCK |
| 3033 | [] JUMP_FORWARD L0 |
| 3034 | |
| 3035 | [tb, val, exc] L1: DUP ) |
| 3036 | [tb, val, exc, exc] <evaluate E1> ) |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3037 | [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] | 3038 | [tb, val, exc] POP |
| 3039 | [tb, val] <assign to V1> (or POP if no V1) |
| 3040 | [tb] POP |
| 3041 | [] <code for S1> |
| 3042 | JUMP_FORWARD L0 |
| 3043 | |
| 3044 | [tb, val, exc] L2: DUP |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3045 | .............................etc....................... |
| 3046 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3047 | [tb, val, exc] Ln+1: RERAISE # re-raise exception |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3048 | |
| 3049 | [] L0: <next statement> |
| 3050 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3051 | Of course, parts are not generated if Vi or Ei is not present. |
| 3052 | */ |
| 3053 | static int |
| 3054 | compiler_try_except(struct compiler *c, stmt_ty s) |
| 3055 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3056 | basicblock *body, *orelse, *except, *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3057 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3058 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3059 | body = compiler_new_block(c); |
| 3060 | except = compiler_new_block(c); |
| 3061 | orelse = compiler_new_block(c); |
| 3062 | end = compiler_new_block(c); |
| 3063 | if (body == NULL || except == NULL || orelse == NULL || end == NULL) |
| 3064 | return 0; |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3065 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3066 | compiler_use_next_block(c, body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3067 | if (!compiler_push_fblock(c, EXCEPT, body, NULL, NULL)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3068 | return 0; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3069 | VISIT_SEQ(c, stmt, s->v.Try.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3070 | ADDOP(c, POP_BLOCK); |
| 3071 | compiler_pop_fblock(c, EXCEPT, body); |
| 3072 | ADDOP_JREL(c, JUMP_FORWARD, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3073 | n = asdl_seq_LEN(s->v.Try.handlers); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3074 | compiler_use_next_block(c, except); |
| 3075 | for (i = 0; i < n; i++) { |
| 3076 | excepthandler_ty handler = (excepthandler_ty)asdl_seq_GET( |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3077 | s->v.Try.handlers, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3078 | if (!handler->v.ExceptHandler.type && i < n-1) |
| 3079 | return compiler_error(c, "default 'except:' must be last"); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3080 | SET_LOC(c, handler); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3081 | except = compiler_new_block(c); |
| 3082 | if (except == NULL) |
| 3083 | return 0; |
| 3084 | if (handler->v.ExceptHandler.type) { |
| 3085 | ADDOP(c, DUP_TOP); |
| 3086 | VISIT(c, expr, handler->v.ExceptHandler.type); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3087 | ADDOP_JABS(c, JUMP_IF_NOT_EXC_MATCH, except); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3088 | } |
| 3089 | ADDOP(c, POP_TOP); |
| 3090 | if (handler->v.ExceptHandler.name) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3091 | basicblock *cleanup_end, *cleanup_body; |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3092 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3093 | cleanup_end = compiler_new_block(c); |
| 3094 | cleanup_body = compiler_new_block(c); |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3095 | if (cleanup_end == NULL || cleanup_body == NULL) { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3096 | return 0; |
Zackery Spytz | 53ebf4b | 2018-10-11 23:54:03 -0600 | [diff] [blame] | 3097 | } |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3098 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3099 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3100 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3101 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3102 | /* |
| 3103 | try: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3104 | # body |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3105 | except type as name: |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3106 | try: |
| 3107 | # body |
| 3108 | finally: |
Chris Angelico | ad098b6 | 2019-05-21 23:34:19 +1000 | [diff] [blame] | 3109 | name = None # in case body contains "del name" |
Ezio Melotti | 1b6424f | 2013-04-19 07:10:09 +0300 | [diff] [blame] | 3110 | del name |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3111 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3112 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3113 | /* second try: */ |
| 3114 | ADDOP_JREL(c, SETUP_FINALLY, cleanup_end); |
| 3115 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3116 | 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] | 3117 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3118 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3119 | /* second # body */ |
| 3120 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3121 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3122 | ADDOP(c, POP_BLOCK); |
| 3123 | ADDOP(c, POP_EXCEPT); |
| 3124 | /* name = None; del name */ |
| 3125 | ADDOP_LOAD_CONST(c, Py_None); |
| 3126 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
| 3127 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
| 3128 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3129 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3130 | /* except: */ |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3131 | compiler_use_next_block(c, cleanup_end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3132 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3133 | /* name = None; del name */ |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3134 | ADDOP_LOAD_CONST(c, Py_None); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3135 | compiler_nameop(c, handler->v.ExceptHandler.name, Store); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3136 | compiler_nameop(c, handler->v.ExceptHandler.name, Del); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3137 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3138 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3139 | } |
| 3140 | else { |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3141 | basicblock *cleanup_body; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3142 | |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3143 | cleanup_body = compiler_new_block(c); |
Benjamin Peterson | 0a5dad9 | 2011-05-27 14:17:04 -0500 | [diff] [blame] | 3144 | if (!cleanup_body) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3145 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3146 | |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 3147 | ADDOP(c, POP_TOP); |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3148 | ADDOP(c, POP_TOP); |
| 3149 | compiler_use_next_block(c, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3150 | if (!compiler_push_fblock(c, HANDLER_CLEANUP, cleanup_body, NULL, NULL)) |
Benjamin Peterson | 74897ba | 2011-05-27 14:10:24 -0500 | [diff] [blame] | 3151 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3152 | VISIT_SEQ(c, stmt, handler->v.ExceptHandler.body); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3153 | compiler_pop_fblock(c, HANDLER_CLEANUP, cleanup_body); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3154 | ADDOP(c, POP_EXCEPT); |
| 3155 | ADDOP_JREL(c, JUMP_FORWARD, end); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3156 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3157 | compiler_use_next_block(c, except); |
| 3158 | } |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 3159 | ADDOP(c, RERAISE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3160 | compiler_use_next_block(c, orelse); |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3161 | VISIT_SEQ(c, stmt, s->v.Try.orelse); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3162 | compiler_use_next_block(c, end); |
| 3163 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3164 | } |
| 3165 | |
| 3166 | static int |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3167 | compiler_try(struct compiler *c, stmt_ty s) { |
| 3168 | if (s->v.Try.finalbody && asdl_seq_LEN(s->v.Try.finalbody)) |
| 3169 | return compiler_try_finally(c, s); |
| 3170 | else |
| 3171 | return compiler_try_except(c, s); |
| 3172 | } |
| 3173 | |
| 3174 | |
| 3175 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3176 | compiler_import_as(struct compiler *c, identifier name, identifier asname) |
| 3177 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3178 | /* The IMPORT_NAME opcode was already generated. This function |
| 3179 | merely needs to bind the result to a name. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3180 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3181 | 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] | 3182 | IMPORT_FROM for each name. |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3183 | */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3184 | Py_ssize_t len = PyUnicode_GET_LENGTH(name); |
| 3185 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3186 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3187 | return 0; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3188 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3189 | /* Consume the base module name to get the first attribute */ |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3190 | while (1) { |
| 3191 | Py_ssize_t pos = dot + 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3192 | PyObject *attr; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3193 | dot = PyUnicode_FindChar(name, '.', pos, len, 1); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3194 | if (dot == -2) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3195 | return 0; |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3196 | attr = PyUnicode_Substring(name, pos, (dot != -1) ? dot : len); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3197 | if (!attr) |
Serhiy Storchaka | 694de3b | 2016-06-15 20:06:07 +0300 | [diff] [blame] | 3198 | return 0; |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3199 | ADDOP_N(c, IMPORT_FROM, attr, names); |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3200 | if (dot == -1) { |
| 3201 | break; |
| 3202 | } |
| 3203 | ADDOP(c, ROT_TWO); |
| 3204 | ADDOP(c, POP_TOP); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3205 | } |
Serhiy Storchaka | 265fcc5 | 2017-08-29 15:47:44 +0300 | [diff] [blame] | 3206 | if (!compiler_nameop(c, asname, Store)) { |
| 3207 | return 0; |
| 3208 | } |
| 3209 | ADDOP(c, POP_TOP); |
| 3210 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3211 | } |
| 3212 | return compiler_nameop(c, asname, Store); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3213 | } |
| 3214 | |
| 3215 | static int |
| 3216 | compiler_import(struct compiler *c, stmt_ty s) |
| 3217 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3218 | /* The Import node stores a module name like a.b.c as a single |
| 3219 | string. This is convenient for all cases except |
| 3220 | import a.b.c as d |
| 3221 | where we need to parse that string to extract the individual |
| 3222 | module names. |
| 3223 | XXX Perhaps change the representation to make this case simpler? |
| 3224 | */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3225 | Py_ssize_t i, n = asdl_seq_LEN(s->v.Import.names); |
Thomas Wouters | f7f438b | 2006-02-28 16:09:29 +0000 | [diff] [blame] | 3226 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3227 | for (i = 0; i < n; i++) { |
| 3228 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.Import.names, i); |
| 3229 | int r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3230 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3231 | ADDOP_LOAD_CONST(c, _PyLong_Zero); |
| 3232 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3233 | ADDOP_NAME(c, IMPORT_NAME, alias->name, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3234 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3235 | if (alias->asname) { |
| 3236 | r = compiler_import_as(c, alias->name, alias->asname); |
| 3237 | if (!r) |
| 3238 | return r; |
| 3239 | } |
| 3240 | else { |
| 3241 | identifier tmp = alias->name; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3242 | Py_ssize_t dot = PyUnicode_FindChar( |
| 3243 | alias->name, '.', 0, PyUnicode_GET_LENGTH(alias->name), 1); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3244 | if (dot != -1) { |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3245 | tmp = PyUnicode_Substring(alias->name, 0, dot); |
Victor Stinner | 6b64a68 | 2013-07-11 22:50:45 +0200 | [diff] [blame] | 3246 | if (tmp == NULL) |
| 3247 | return 0; |
| 3248 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3249 | r = compiler_nameop(c, tmp, Store); |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3250 | if (dot != -1) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3251 | Py_DECREF(tmp); |
| 3252 | } |
| 3253 | if (!r) |
| 3254 | return r; |
| 3255 | } |
| 3256 | } |
| 3257 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3258 | } |
| 3259 | |
| 3260 | static int |
| 3261 | compiler_from_import(struct compiler *c, stmt_ty s) |
| 3262 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3263 | Py_ssize_t i, n = asdl_seq_LEN(s->v.ImportFrom.names); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3264 | PyObject *names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3265 | static PyObject *empty_string; |
Benjamin Peterson | 78565b2 | 2009-06-28 19:19:51 +0000 | [diff] [blame] | 3266 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3267 | if (!empty_string) { |
| 3268 | empty_string = PyUnicode_FromString(""); |
| 3269 | if (!empty_string) |
| 3270 | return 0; |
| 3271 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3272 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3273 | ADDOP_LOAD_CONST_NEW(c, PyLong_FromLong(s->v.ImportFrom.level)); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 3274 | |
| 3275 | names = PyTuple_New(n); |
| 3276 | if (!names) |
| 3277 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3278 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3279 | /* build up the names */ |
| 3280 | for (i = 0; i < n; i++) { |
| 3281 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3282 | Py_INCREF(alias->name); |
| 3283 | PyTuple_SET_ITEM(names, i, alias->name); |
| 3284 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3285 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3286 | if (s->lineno > c->c_future->ff_lineno && s->v.ImportFrom.module && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3287 | _PyUnicode_EqualToASCIIString(s->v.ImportFrom.module, "__future__")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3288 | Py_DECREF(names); |
| 3289 | return compiler_error(c, "from __future__ imports must occur " |
| 3290 | "at the beginning of the file"); |
| 3291 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3292 | ADDOP_LOAD_CONST_NEW(c, names); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3293 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3294 | if (s->v.ImportFrom.module) { |
| 3295 | ADDOP_NAME(c, IMPORT_NAME, s->v.ImportFrom.module, names); |
| 3296 | } |
| 3297 | else { |
| 3298 | ADDOP_NAME(c, IMPORT_NAME, empty_string, names); |
| 3299 | } |
| 3300 | for (i = 0; i < n; i++) { |
| 3301 | alias_ty alias = (alias_ty)asdl_seq_GET(s->v.ImportFrom.names, i); |
| 3302 | identifier store_name; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3303 | |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 3304 | if (i == 0 && PyUnicode_READ_CHAR(alias->name, 0) == '*') { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3305 | assert(n == 1); |
| 3306 | ADDOP(c, IMPORT_STAR); |
| 3307 | return 1; |
| 3308 | } |
| 3309 | |
| 3310 | ADDOP_NAME(c, IMPORT_FROM, alias->name, names); |
| 3311 | store_name = alias->name; |
| 3312 | if (alias->asname) |
| 3313 | store_name = alias->asname; |
| 3314 | |
| 3315 | if (!compiler_nameop(c, store_name, Store)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3316 | return 0; |
| 3317 | } |
| 3318 | } |
| 3319 | /* remove imported module */ |
| 3320 | ADDOP(c, POP_TOP); |
| 3321 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | static int |
| 3325 | compiler_assert(struct compiler *c, stmt_ty s) |
| 3326 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3327 | basicblock *end; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3328 | |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 3329 | if (c->c_optimize) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3330 | return 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3331 | if (s->v.Assert.test->kind == Tuple_kind && |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 3332 | asdl_seq_LEN(s->v.Assert.test->v.Tuple.elts) > 0) |
| 3333 | { |
| 3334 | if (!compiler_warn(c, "assertion is always true, " |
| 3335 | "perhaps remove parentheses?")) |
| 3336 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 3337 | return 0; |
| 3338 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3339 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3340 | end = compiler_new_block(c); |
| 3341 | if (end == NULL) |
| 3342 | return 0; |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 3343 | if (!compiler_jump_if(c, s->v.Assert.test, end, 1)) |
| 3344 | return 0; |
Zackery Spytz | ce6a070 | 2019-08-25 03:44:09 -0600 | [diff] [blame] | 3345 | ADDOP(c, LOAD_ASSERTION_ERROR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3346 | if (s->v.Assert.msg) { |
| 3347 | VISIT(c, expr, s->v.Assert.msg); |
| 3348 | ADDOP_I(c, CALL_FUNCTION, 1); |
| 3349 | } |
| 3350 | ADDOP_I(c, RAISE_VARARGS, 1); |
| 3351 | compiler_use_next_block(c, end); |
| 3352 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3353 | } |
| 3354 | |
| 3355 | static int |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3356 | compiler_visit_stmt_expr(struct compiler *c, expr_ty value) |
| 3357 | { |
| 3358 | if (c->c_interactive && c->c_nestlevel <= 1) { |
| 3359 | VISIT(c, expr, value); |
| 3360 | ADDOP(c, PRINT_EXPR); |
| 3361 | return 1; |
| 3362 | } |
| 3363 | |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3364 | if (value->kind == Constant_kind) { |
Victor Stinner | 15a3095 | 2016-02-08 22:45:06 +0100 | [diff] [blame] | 3365 | /* ignore constant statement */ |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3366 | return 1; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3367 | } |
| 3368 | |
| 3369 | VISIT(c, expr, value); |
| 3370 | ADDOP(c, POP_TOP); |
| 3371 | return 1; |
| 3372 | } |
| 3373 | |
| 3374 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3375 | compiler_visit_stmt(struct compiler *c, stmt_ty s) |
| 3376 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3377 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3378 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3379 | /* Always assign a lineno to the next instruction for a stmt. */ |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 3380 | SET_LOC(c, s); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 3381 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3382 | switch (s->kind) { |
| 3383 | case FunctionDef_kind: |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3384 | return compiler_function(c, s, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3385 | case ClassDef_kind: |
| 3386 | return compiler_class(c, s); |
| 3387 | case Return_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3388 | return compiler_return(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3389 | case Delete_kind: |
| 3390 | VISIT_SEQ(c, expr, s->v.Delete.targets) |
| 3391 | break; |
| 3392 | case Assign_kind: |
| 3393 | n = asdl_seq_LEN(s->v.Assign.targets); |
| 3394 | VISIT(c, expr, s->v.Assign.value); |
| 3395 | for (i = 0; i < n; i++) { |
| 3396 | if (i < n - 1) |
| 3397 | ADDOP(c, DUP_TOP); |
| 3398 | VISIT(c, expr, |
| 3399 | (expr_ty)asdl_seq_GET(s->v.Assign.targets, i)); |
| 3400 | } |
| 3401 | break; |
| 3402 | case AugAssign_kind: |
| 3403 | return compiler_augassign(c, s); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 3404 | case AnnAssign_kind: |
| 3405 | return compiler_annassign(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3406 | case For_kind: |
| 3407 | return compiler_for(c, s); |
| 3408 | case While_kind: |
| 3409 | return compiler_while(c, s); |
| 3410 | case If_kind: |
| 3411 | return compiler_if(c, s); |
| 3412 | case Raise_kind: |
| 3413 | n = 0; |
| 3414 | if (s->v.Raise.exc) { |
| 3415 | VISIT(c, expr, s->v.Raise.exc); |
| 3416 | n++; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3417 | if (s->v.Raise.cause) { |
| 3418 | VISIT(c, expr, s->v.Raise.cause); |
| 3419 | n++; |
| 3420 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3421 | } |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3422 | ADDOP_I(c, RAISE_VARARGS, (int)n); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3423 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 3424 | case Try_kind: |
| 3425 | return compiler_try(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3426 | case Assert_kind: |
| 3427 | return compiler_assert(c, s); |
| 3428 | case Import_kind: |
| 3429 | return compiler_import(c, s); |
| 3430 | case ImportFrom_kind: |
| 3431 | return compiler_from_import(c, s); |
| 3432 | case Global_kind: |
| 3433 | case Nonlocal_kind: |
| 3434 | break; |
| 3435 | case Expr_kind: |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 3436 | return compiler_visit_stmt_expr(c, s->v.Expr.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3437 | case Pass_kind: |
| 3438 | break; |
| 3439 | case Break_kind: |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 3440 | return compiler_break(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3441 | case Continue_kind: |
| 3442 | return compiler_continue(c); |
| 3443 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 3444 | return compiler_with(c, s, 0); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3445 | case AsyncFunctionDef_kind: |
| 3446 | return compiler_function(c, s, 1); |
| 3447 | case AsyncWith_kind: |
| 3448 | return compiler_async_with(c, s, 0); |
| 3449 | case AsyncFor_kind: |
| 3450 | return compiler_async_for(c, s); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3451 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 3452 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3453 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | static int |
| 3457 | unaryop(unaryop_ty op) |
| 3458 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3459 | switch (op) { |
| 3460 | case Invert: |
| 3461 | return UNARY_INVERT; |
| 3462 | case Not: |
| 3463 | return UNARY_NOT; |
| 3464 | case UAdd: |
| 3465 | return UNARY_POSITIVE; |
| 3466 | case USub: |
| 3467 | return UNARY_NEGATIVE; |
| 3468 | default: |
| 3469 | PyErr_Format(PyExc_SystemError, |
| 3470 | "unary op %d should not be possible", op); |
| 3471 | return 0; |
| 3472 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3473 | } |
| 3474 | |
| 3475 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3476 | binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3477 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3478 | switch (op) { |
| 3479 | case Add: |
| 3480 | return BINARY_ADD; |
| 3481 | case Sub: |
| 3482 | return BINARY_SUBTRACT; |
| 3483 | case Mult: |
| 3484 | return BINARY_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3485 | case MatMult: |
| 3486 | return BINARY_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3487 | case Div: |
| 3488 | return BINARY_TRUE_DIVIDE; |
| 3489 | case Mod: |
| 3490 | return BINARY_MODULO; |
| 3491 | case Pow: |
| 3492 | return BINARY_POWER; |
| 3493 | case LShift: |
| 3494 | return BINARY_LSHIFT; |
| 3495 | case RShift: |
| 3496 | return BINARY_RSHIFT; |
| 3497 | case BitOr: |
| 3498 | return BINARY_OR; |
| 3499 | case BitXor: |
| 3500 | return BINARY_XOR; |
| 3501 | case BitAnd: |
| 3502 | return BINARY_AND; |
| 3503 | case FloorDiv: |
| 3504 | return BINARY_FLOOR_DIVIDE; |
| 3505 | default: |
| 3506 | PyErr_Format(PyExc_SystemError, |
| 3507 | "binary op %d should not be possible", op); |
| 3508 | return 0; |
| 3509 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3510 | } |
| 3511 | |
| 3512 | static int |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3513 | inplace_binop(operator_ty op) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3514 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3515 | switch (op) { |
| 3516 | case Add: |
| 3517 | return INPLACE_ADD; |
| 3518 | case Sub: |
| 3519 | return INPLACE_SUBTRACT; |
| 3520 | case Mult: |
| 3521 | return INPLACE_MULTIPLY; |
Benjamin Peterson | d51374e | 2014-04-09 23:55:56 -0400 | [diff] [blame] | 3522 | case MatMult: |
| 3523 | return INPLACE_MATRIX_MULTIPLY; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3524 | case Div: |
| 3525 | return INPLACE_TRUE_DIVIDE; |
| 3526 | case Mod: |
| 3527 | return INPLACE_MODULO; |
| 3528 | case Pow: |
| 3529 | return INPLACE_POWER; |
| 3530 | case LShift: |
| 3531 | return INPLACE_LSHIFT; |
| 3532 | case RShift: |
| 3533 | return INPLACE_RSHIFT; |
| 3534 | case BitOr: |
| 3535 | return INPLACE_OR; |
| 3536 | case BitXor: |
| 3537 | return INPLACE_XOR; |
| 3538 | case BitAnd: |
| 3539 | return INPLACE_AND; |
| 3540 | case FloorDiv: |
| 3541 | return INPLACE_FLOOR_DIVIDE; |
| 3542 | default: |
| 3543 | PyErr_Format(PyExc_SystemError, |
| 3544 | "inplace binary op %d should not be possible", op); |
| 3545 | return 0; |
| 3546 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3547 | } |
| 3548 | |
| 3549 | static int |
| 3550 | compiler_nameop(struct compiler *c, identifier name, expr_context_ty ctx) |
| 3551 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3552 | int op, scope; |
| 3553 | Py_ssize_t arg; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3554 | enum { OP_FAST, OP_GLOBAL, OP_DEREF, OP_NAME } optype; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3555 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3556 | PyObject *dict = c->u->u_names; |
| 3557 | PyObject *mangled; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3558 | |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 3559 | assert(!_PyUnicode_EqualToASCIIString(name, "None") && |
| 3560 | !_PyUnicode_EqualToASCIIString(name, "True") && |
| 3561 | !_PyUnicode_EqualToASCIIString(name, "False")); |
Benjamin Peterson | 70b224d | 2012-12-06 17:49:58 -0500 | [diff] [blame] | 3562 | |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 3563 | if (forbidden_name(c, name, ctx)) |
| 3564 | return 0; |
| 3565 | |
Serhiy Storchaka | bd6ec4d | 2017-12-18 14:29:12 +0200 | [diff] [blame] | 3566 | mangled = _Py_Mangle(c->u->u_private, name); |
| 3567 | if (!mangled) |
| 3568 | return 0; |
| 3569 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3570 | op = 0; |
| 3571 | optype = OP_NAME; |
| 3572 | scope = PyST_GetScope(c->u->u_ste, mangled); |
| 3573 | switch (scope) { |
| 3574 | case FREE: |
| 3575 | dict = c->u->u_freevars; |
| 3576 | optype = OP_DEREF; |
| 3577 | break; |
| 3578 | case CELL: |
| 3579 | dict = c->u->u_cellvars; |
| 3580 | optype = OP_DEREF; |
| 3581 | break; |
| 3582 | case LOCAL: |
| 3583 | if (c->u->u_ste->ste_type == FunctionBlock) |
| 3584 | optype = OP_FAST; |
| 3585 | break; |
| 3586 | case GLOBAL_IMPLICIT: |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 3587 | if (c->u->u_ste->ste_type == FunctionBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3588 | optype = OP_GLOBAL; |
| 3589 | break; |
| 3590 | case GLOBAL_EXPLICIT: |
| 3591 | optype = OP_GLOBAL; |
| 3592 | break; |
| 3593 | default: |
| 3594 | /* scope can be 0 */ |
| 3595 | break; |
| 3596 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3597 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3598 | /* 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] | 3599 | assert(scope || PyUnicode_READ_CHAR(name, 0) == '_'); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3600 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3601 | switch (optype) { |
| 3602 | case OP_DEREF: |
| 3603 | switch (ctx) { |
Benjamin Peterson | 3b0431d | 2013-04-30 09:41:40 -0400 | [diff] [blame] | 3604 | case Load: |
| 3605 | op = (c->u->u_ste->ste_type == ClassBlock) ? LOAD_CLASSDEREF : LOAD_DEREF; |
| 3606 | break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3607 | case Store: op = STORE_DEREF; break; |
Amaury Forgeot d'Arc | ba117ef | 2010-09-10 21:39:53 +0000 | [diff] [blame] | 3608 | case Del: op = DELETE_DEREF; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3609 | } |
| 3610 | break; |
| 3611 | case OP_FAST: |
| 3612 | switch (ctx) { |
| 3613 | case Load: op = LOAD_FAST; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3614 | case Store: op = STORE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3615 | case Del: op = DELETE_FAST; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3616 | } |
Serhiy Storchaka | aa8e51f | 2018-04-01 00:29:37 +0300 | [diff] [blame] | 3617 | ADDOP_N(c, op, mangled, varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3618 | return 1; |
| 3619 | case OP_GLOBAL: |
| 3620 | switch (ctx) { |
| 3621 | case Load: op = LOAD_GLOBAL; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3622 | case Store: op = STORE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3623 | case Del: op = DELETE_GLOBAL; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3624 | } |
| 3625 | break; |
| 3626 | case OP_NAME: |
| 3627 | switch (ctx) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 3628 | case Load: op = LOAD_NAME; break; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 3629 | case Store: op = STORE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3630 | case Del: op = DELETE_NAME; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3631 | } |
| 3632 | break; |
| 3633 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3634 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3635 | assert(op); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 3636 | arg = compiler_add_o(dict, mangled); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3637 | Py_DECREF(mangled); |
| 3638 | if (arg < 0) |
| 3639 | return 0; |
| 3640 | return compiler_addop_i(c, op, arg); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3641 | } |
| 3642 | |
| 3643 | static int |
| 3644 | compiler_boolop(struct compiler *c, expr_ty e) |
| 3645 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3646 | basicblock *end; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3647 | int jumpi; |
| 3648 | Py_ssize_t i, n; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3649 | asdl_seq *s; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3650 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3651 | assert(e->kind == BoolOp_kind); |
| 3652 | if (e->v.BoolOp.op == And) |
| 3653 | jumpi = JUMP_IF_FALSE_OR_POP; |
| 3654 | else |
| 3655 | jumpi = JUMP_IF_TRUE_OR_POP; |
| 3656 | end = compiler_new_block(c); |
| 3657 | if (end == NULL) |
| 3658 | return 0; |
| 3659 | s = e->v.BoolOp.values; |
| 3660 | n = asdl_seq_LEN(s) - 1; |
| 3661 | assert(n >= 0); |
| 3662 | for (i = 0; i < n; ++i) { |
| 3663 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, i)); |
| 3664 | ADDOP_JABS(c, jumpi, end); |
| 3665 | } |
| 3666 | VISIT(c, expr, (expr_ty)asdl_seq_GET(s, n)); |
| 3667 | compiler_use_next_block(c, end); |
| 3668 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3669 | } |
| 3670 | |
| 3671 | static int |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3672 | starunpack_helper(struct compiler *c, asdl_seq *elts, int pushed, |
| 3673 | int build, int add, int extend, int tuple) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3674 | { |
| 3675 | Py_ssize_t n = asdl_seq_LEN(elts); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3676 | Py_ssize_t i, seen_star = 0; |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3677 | if (n > 2 && are_all_items_const(elts, 0, n)) { |
| 3678 | PyObject *folded = PyTuple_New(n); |
| 3679 | if (folded == NULL) { |
| 3680 | return 0; |
| 3681 | } |
| 3682 | PyObject *val; |
| 3683 | for (i = 0; i < n; i++) { |
| 3684 | val = ((expr_ty)asdl_seq_GET(elts, i))->v.Constant.value; |
| 3685 | Py_INCREF(val); |
| 3686 | PyTuple_SET_ITEM(folded, i, val); |
| 3687 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3688 | if (tuple) { |
| 3689 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3690 | } else { |
| 3691 | if (add == SET_ADD) { |
| 3692 | Py_SETREF(folded, PyFrozenSet_New(folded)); |
| 3693 | if (folded == NULL) { |
| 3694 | return 0; |
| 3695 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3696 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3697 | ADDOP_I(c, build, pushed); |
| 3698 | ADDOP_LOAD_CONST_NEW(c, folded); |
| 3699 | ADDOP_I(c, extend, 1); |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3700 | } |
Brandt Bucher | 6dd9b64 | 2019-11-25 22:16:53 -0800 | [diff] [blame] | 3701 | return 1; |
| 3702 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3703 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3704 | for (i = 0; i < n; i++) { |
| 3705 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3706 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3707 | seen_star = 1; |
| 3708 | } |
| 3709 | } |
| 3710 | if (seen_star) { |
| 3711 | seen_star = 0; |
| 3712 | for (i = 0; i < n; i++) { |
| 3713 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3714 | if (elt->kind == Starred_kind) { |
| 3715 | if (seen_star == 0) { |
| 3716 | ADDOP_I(c, build, i+pushed); |
| 3717 | seen_star = 1; |
| 3718 | } |
| 3719 | VISIT(c, expr, elt->v.Starred.value); |
| 3720 | ADDOP_I(c, extend, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3721 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3722 | else { |
| 3723 | VISIT(c, expr, elt); |
| 3724 | if (seen_star) { |
| 3725 | ADDOP_I(c, add, 1); |
| 3726 | } |
| 3727 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3728 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3729 | assert(seen_star); |
| 3730 | if (tuple) { |
| 3731 | ADDOP(c, LIST_TO_TUPLE); |
| 3732 | } |
| 3733 | } |
| 3734 | else { |
| 3735 | for (i = 0; i < n; i++) { |
| 3736 | expr_ty elt = asdl_seq_GET(elts, i); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3737 | VISIT(c, expr, elt); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3738 | } |
| 3739 | if (tuple) { |
| 3740 | ADDOP_I(c, BUILD_TUPLE, n+pushed); |
| 3741 | } else { |
| 3742 | ADDOP_I(c, build, n+pushed); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3743 | } |
| 3744 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3745 | return 1; |
| 3746 | } |
| 3747 | |
| 3748 | static int |
| 3749 | assignment_helper(struct compiler *c, asdl_seq *elts) |
| 3750 | { |
| 3751 | Py_ssize_t n = asdl_seq_LEN(elts); |
| 3752 | Py_ssize_t i; |
| 3753 | int seen_star = 0; |
| 3754 | for (i = 0; i < n; i++) { |
| 3755 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3756 | if (elt->kind == Starred_kind && !seen_star) { |
| 3757 | if ((i >= (1 << 8)) || |
| 3758 | (n-i-1 >= (INT_MAX >> 8))) |
| 3759 | return compiler_error(c, |
| 3760 | "too many expressions in " |
| 3761 | "star-unpacking assignment"); |
| 3762 | ADDOP_I(c, UNPACK_EX, (i + ((n-i-1) << 8))); |
| 3763 | seen_star = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3764 | } |
| 3765 | else if (elt->kind == Starred_kind) { |
| 3766 | return compiler_error(c, |
Furkan Önder | cb6534e | 2020-03-26 04:54:31 +0300 | [diff] [blame] | 3767 | "multiple starred expressions in assignment"); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3768 | } |
| 3769 | } |
| 3770 | if (!seen_star) { |
| 3771 | ADDOP_I(c, UNPACK_SEQUENCE, n); |
| 3772 | } |
Brandt Bucher | d5aa2e9 | 2020-03-07 19:44:18 -0800 | [diff] [blame] | 3773 | for (i = 0; i < n; i++) { |
| 3774 | expr_ty elt = asdl_seq_GET(elts, i); |
| 3775 | VISIT(c, expr, elt->kind != Starred_kind ? elt : elt->v.Starred.value); |
| 3776 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3777 | return 1; |
| 3778 | } |
| 3779 | |
| 3780 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3781 | compiler_list(struct compiler *c, expr_ty e) |
| 3782 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3783 | asdl_seq *elts = e->v.List.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3784 | if (e->v.List.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3785 | return assignment_helper(c, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3786 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3787 | else if (e->v.List.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3788 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3789 | LIST_APPEND, LIST_EXTEND, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3790 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3791 | else |
| 3792 | VISIT_SEQ(c, expr, elts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3793 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3794 | } |
| 3795 | |
| 3796 | static int |
| 3797 | compiler_tuple(struct compiler *c, expr_ty e) |
| 3798 | { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3799 | asdl_seq *elts = e->v.Tuple.elts; |
Serhiy Storchaka | d8b3a98 | 2019-03-05 20:42:06 +0200 | [diff] [blame] | 3800 | if (e->v.Tuple.ctx == Store) { |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3801 | return assignment_helper(c, elts); |
| 3802 | } |
| 3803 | else if (e->v.Tuple.ctx == Load) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3804 | return starunpack_helper(c, elts, 0, BUILD_LIST, |
| 3805 | LIST_APPEND, LIST_EXTEND, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3806 | } |
| 3807 | else |
| 3808 | VISIT_SEQ(c, expr, elts); |
| 3809 | return 1; |
| 3810 | } |
| 3811 | |
| 3812 | static int |
| 3813 | compiler_set(struct compiler *c, expr_ty e) |
| 3814 | { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 3815 | return starunpack_helper(c, e->v.Set.elts, 0, BUILD_SET, |
| 3816 | SET_ADD, SET_UPDATE, 0); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3817 | } |
| 3818 | |
| 3819 | static int |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3820 | are_all_items_const(asdl_seq *seq, Py_ssize_t begin, Py_ssize_t end) |
| 3821 | { |
| 3822 | Py_ssize_t i; |
| 3823 | for (i = begin; i < end; i++) { |
| 3824 | expr_ty key = (expr_ty)asdl_seq_GET(seq, i); |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3825 | if (key == NULL || key->kind != Constant_kind) |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3826 | return 0; |
| 3827 | } |
| 3828 | return 1; |
| 3829 | } |
| 3830 | |
| 3831 | static int |
| 3832 | compiler_subdict(struct compiler *c, expr_ty e, Py_ssize_t begin, Py_ssize_t end) |
| 3833 | { |
| 3834 | Py_ssize_t i, n = end - begin; |
| 3835 | PyObject *keys, *key; |
| 3836 | if (n > 1 && are_all_items_const(e->v.Dict.keys, begin, end)) { |
| 3837 | for (i = begin; i < end; i++) { |
| 3838 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3839 | } |
| 3840 | keys = PyTuple_New(n); |
| 3841 | if (keys == NULL) { |
| 3842 | return 0; |
| 3843 | } |
| 3844 | for (i = begin; i < end; i++) { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 3845 | 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] | 3846 | Py_INCREF(key); |
| 3847 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 3848 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 3849 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3850 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 3851 | } |
| 3852 | else { |
| 3853 | for (i = begin; i < end; i++) { |
| 3854 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.keys, i)); |
| 3855 | VISIT(c, expr, (expr_ty)asdl_seq_GET(e->v.Dict.values, i)); |
| 3856 | } |
| 3857 | ADDOP_I(c, BUILD_MAP, n); |
| 3858 | } |
| 3859 | return 1; |
| 3860 | } |
| 3861 | |
| 3862 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3863 | compiler_dict(struct compiler *c, expr_ty e) |
| 3864 | { |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 3865 | Py_ssize_t i, n, elements; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3866 | int have_dict; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3867 | int is_unpacking = 0; |
| 3868 | n = asdl_seq_LEN(e->v.Dict.values); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3869 | have_dict = 0; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3870 | elements = 0; |
| 3871 | for (i = 0; i < n; i++) { |
| 3872 | 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] | 3873 | if (is_unpacking) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3874 | if (elements) { |
| 3875 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3876 | return 0; |
| 3877 | } |
| 3878 | if (have_dict) { |
| 3879 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3880 | } |
| 3881 | have_dict = 1; |
| 3882 | elements = 0; |
| 3883 | } |
| 3884 | if (have_dict == 0) { |
| 3885 | ADDOP_I(c, BUILD_MAP, 0); |
| 3886 | have_dict = 1; |
| 3887 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3888 | 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] | 3889 | ADDOP_I(c, DICT_UPDATE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3890 | } |
| 3891 | else { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3892 | if (elements == 0xFFFF) { |
| 3893 | if (!compiler_subdict(c, e, i - elements, i)) { |
| 3894 | return 0; |
| 3895 | } |
| 3896 | if (have_dict) { |
| 3897 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3898 | } |
| 3899 | have_dict = 1; |
| 3900 | elements = 0; |
| 3901 | } |
| 3902 | else { |
| 3903 | elements++; |
| 3904 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3905 | } |
| 3906 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3907 | if (elements) { |
| 3908 | if (!compiler_subdict(c, e, n - elements, n)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 3909 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3910 | } |
| 3911 | if (have_dict) { |
| 3912 | ADDOP_I(c, DICT_UPDATE, 1); |
| 3913 | } |
| 3914 | have_dict = 1; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 3915 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 3916 | if (!have_dict) { |
| 3917 | ADDOP_I(c, BUILD_MAP, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3918 | } |
| 3919 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3920 | } |
| 3921 | |
| 3922 | static int |
| 3923 | compiler_compare(struct compiler *c, expr_ty e) |
| 3924 | { |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 3925 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3926 | |
Serhiy Storchaka | 3bcbedc | 2019-01-18 07:47:48 +0200 | [diff] [blame] | 3927 | if (!check_compare(c, e)) { |
| 3928 | return 0; |
| 3929 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3930 | VISIT(c, expr, e->v.Compare.left); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3931 | assert(asdl_seq_LEN(e->v.Compare.ops) > 0); |
| 3932 | n = asdl_seq_LEN(e->v.Compare.ops) - 1; |
| 3933 | if (n == 0) { |
| 3934 | 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] | 3935 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, 0)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3936 | } |
| 3937 | else { |
| 3938 | basicblock *cleanup = compiler_new_block(c); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3939 | if (cleanup == NULL) |
| 3940 | return 0; |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3941 | for (i = 0; i < n; i++) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3942 | VISIT(c, expr, |
| 3943 | (expr_ty)asdl_seq_GET(e->v.Compare.comparators, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3944 | ADDOP(c, DUP_TOP); |
| 3945 | ADDOP(c, ROT_THREE); |
Mark Shannon | 9af0e47 | 2020-01-14 10:12:45 +0000 | [diff] [blame] | 3946 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, i)); |
Serhiy Storchaka | 02b9ef2 | 2017-12-30 09:47:42 +0200 | [diff] [blame] | 3947 | ADDOP_JABS(c, JUMP_IF_FALSE_OR_POP, cleanup); |
| 3948 | NEXT_BLOCK(c); |
| 3949 | } |
| 3950 | 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] | 3951 | ADDOP_COMPARE(c, asdl_seq_GET(e->v.Compare.ops, n)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 3952 | basicblock *end = compiler_new_block(c); |
| 3953 | if (end == NULL) |
| 3954 | return 0; |
| 3955 | ADDOP_JREL(c, JUMP_FORWARD, end); |
| 3956 | compiler_use_next_block(c, cleanup); |
| 3957 | ADDOP(c, ROT_TWO); |
| 3958 | ADDOP(c, POP_TOP); |
| 3959 | compiler_use_next_block(c, end); |
| 3960 | } |
| 3961 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 3962 | } |
| 3963 | |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3964 | static PyTypeObject * |
| 3965 | infer_type(expr_ty e) |
| 3966 | { |
| 3967 | switch (e->kind) { |
| 3968 | case Tuple_kind: |
| 3969 | return &PyTuple_Type; |
| 3970 | case List_kind: |
| 3971 | case ListComp_kind: |
| 3972 | return &PyList_Type; |
| 3973 | case Dict_kind: |
| 3974 | case DictComp_kind: |
| 3975 | return &PyDict_Type; |
| 3976 | case Set_kind: |
| 3977 | case SetComp_kind: |
| 3978 | return &PySet_Type; |
| 3979 | case GeneratorExp_kind: |
| 3980 | return &PyGen_Type; |
| 3981 | case Lambda_kind: |
| 3982 | return &PyFunction_Type; |
| 3983 | case JoinedStr_kind: |
| 3984 | case FormattedValue_kind: |
| 3985 | return &PyUnicode_Type; |
| 3986 | case Constant_kind: |
Victor Stinner | a102ed7 | 2020-02-07 02:24:48 +0100 | [diff] [blame] | 3987 | return Py_TYPE(e->v.Constant.value); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 3988 | default: |
| 3989 | return NULL; |
| 3990 | } |
| 3991 | } |
| 3992 | |
| 3993 | static int |
| 3994 | check_caller(struct compiler *c, expr_ty e) |
| 3995 | { |
| 3996 | switch (e->kind) { |
| 3997 | case Constant_kind: |
| 3998 | case Tuple_kind: |
| 3999 | case List_kind: |
| 4000 | case ListComp_kind: |
| 4001 | case Dict_kind: |
| 4002 | case DictComp_kind: |
| 4003 | case Set_kind: |
| 4004 | case SetComp_kind: |
| 4005 | case GeneratorExp_kind: |
| 4006 | case JoinedStr_kind: |
| 4007 | case FormattedValue_kind: |
| 4008 | return compiler_warn(c, "'%.200s' object is not callable; " |
| 4009 | "perhaps you missed a comma?", |
| 4010 | infer_type(e)->tp_name); |
| 4011 | default: |
| 4012 | return 1; |
| 4013 | } |
| 4014 | } |
| 4015 | |
| 4016 | static int |
| 4017 | check_subscripter(struct compiler *c, expr_ty e) |
| 4018 | { |
| 4019 | PyObject *v; |
| 4020 | |
| 4021 | switch (e->kind) { |
| 4022 | case Constant_kind: |
| 4023 | v = e->v.Constant.value; |
| 4024 | if (!(v == Py_None || v == Py_Ellipsis || |
| 4025 | PyLong_Check(v) || PyFloat_Check(v) || PyComplex_Check(v) || |
| 4026 | PyAnySet_Check(v))) |
| 4027 | { |
| 4028 | return 1; |
| 4029 | } |
| 4030 | /* fall through */ |
| 4031 | case Set_kind: |
| 4032 | case SetComp_kind: |
| 4033 | case GeneratorExp_kind: |
| 4034 | case Lambda_kind: |
| 4035 | return compiler_warn(c, "'%.200s' object is not subscriptable; " |
| 4036 | "perhaps you missed a comma?", |
| 4037 | infer_type(e)->tp_name); |
| 4038 | default: |
| 4039 | return 1; |
| 4040 | } |
| 4041 | } |
| 4042 | |
| 4043 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4044 | check_index(struct compiler *c, expr_ty e, expr_ty s) |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4045 | { |
| 4046 | PyObject *v; |
| 4047 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 4048 | PyTypeObject *index_type = infer_type(s); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4049 | if (index_type == NULL |
| 4050 | || PyType_FastSubclass(index_type, Py_TPFLAGS_LONG_SUBCLASS) |
| 4051 | || index_type == &PySlice_Type) { |
| 4052 | return 1; |
| 4053 | } |
| 4054 | |
| 4055 | switch (e->kind) { |
| 4056 | case Constant_kind: |
| 4057 | v = e->v.Constant.value; |
| 4058 | if (!(PyUnicode_Check(v) || PyBytes_Check(v) || PyTuple_Check(v))) { |
| 4059 | return 1; |
| 4060 | } |
| 4061 | /* fall through */ |
| 4062 | case Tuple_kind: |
| 4063 | case List_kind: |
| 4064 | case ListComp_kind: |
| 4065 | case JoinedStr_kind: |
| 4066 | case FormattedValue_kind: |
| 4067 | return compiler_warn(c, "%.200s indices must be integers or slices, " |
| 4068 | "not %.200s; " |
| 4069 | "perhaps you missed a comma?", |
| 4070 | infer_type(e)->tp_name, |
| 4071 | index_type->tp_name); |
| 4072 | default: |
| 4073 | return 1; |
| 4074 | } |
| 4075 | } |
| 4076 | |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4077 | // 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] | 4078 | static int |
Yury Selivanov | f239213 | 2016-12-13 19:03:51 -0500 | [diff] [blame] | 4079 | maybe_optimize_method_call(struct compiler *c, expr_ty e) |
| 4080 | { |
| 4081 | Py_ssize_t argsl, i; |
| 4082 | expr_ty meth = e->v.Call.func; |
| 4083 | asdl_seq *args = e->v.Call.args; |
| 4084 | |
| 4085 | /* Check that the call node is an attribute access, and that |
| 4086 | the call doesn't have keyword parameters. */ |
| 4087 | if (meth->kind != Attribute_kind || meth->v.Attribute.ctx != Load || |
| 4088 | asdl_seq_LEN(e->v.Call.keywords)) |
| 4089 | return -1; |
| 4090 | |
| 4091 | /* Check that there are no *varargs types of arguments. */ |
| 4092 | argsl = asdl_seq_LEN(args); |
| 4093 | for (i = 0; i < argsl; i++) { |
| 4094 | expr_ty elt = asdl_seq_GET(args, i); |
| 4095 | if (elt->kind == Starred_kind) { |
| 4096 | return -1; |
| 4097 | } |
| 4098 | } |
| 4099 | |
| 4100 | /* Alright, we can optimize the code. */ |
| 4101 | VISIT(c, expr, meth->v.Attribute.value); |
| 4102 | ADDOP_NAME(c, LOAD_METHOD, meth->v.Attribute.attr, names); |
| 4103 | VISIT_SEQ(c, expr, e->v.Call.args); |
| 4104 | ADDOP_I(c, CALL_METHOD, asdl_seq_LEN(e->v.Call.args)); |
| 4105 | return 1; |
| 4106 | } |
| 4107 | |
| 4108 | static int |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4109 | validate_keywords(struct compiler *c, asdl_seq *keywords) |
| 4110 | { |
| 4111 | Py_ssize_t nkeywords = asdl_seq_LEN(keywords); |
| 4112 | for (Py_ssize_t i = 0; i < nkeywords; i++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4113 | keyword_ty key = ((keyword_ty)asdl_seq_GET(keywords, i)); |
| 4114 | if (key->arg == NULL) { |
| 4115 | continue; |
| 4116 | } |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 4117 | if (forbidden_name(c, key->arg, Store)) { |
| 4118 | return -1; |
| 4119 | } |
Zackery Spytz | 08050e9 | 2020-04-06 00:47:47 -0600 | [diff] [blame] | 4120 | for (Py_ssize_t j = i + 1; j < nkeywords; j++) { |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4121 | keyword_ty other = ((keyword_ty)asdl_seq_GET(keywords, j)); |
| 4122 | if (other->arg && !PyUnicode_Compare(key->arg, other->arg)) { |
| 4123 | PyObject *msg = PyUnicode_FromFormat("keyword argument repeated: %U", key->arg); |
| 4124 | if (msg == NULL) { |
| 4125 | return -1; |
| 4126 | } |
| 4127 | c->u->u_col_offset = other->col_offset; |
| 4128 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
| 4129 | Py_DECREF(msg); |
| 4130 | return -1; |
| 4131 | } |
| 4132 | } |
| 4133 | } |
| 4134 | return 0; |
| 4135 | } |
| 4136 | |
| 4137 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4138 | compiler_call(struct compiler *c, expr_ty e) |
| 4139 | { |
Zackery Spytz | 97f5de0 | 2019-03-22 01:30:32 -0600 | [diff] [blame] | 4140 | int ret = maybe_optimize_method_call(c, e); |
| 4141 | if (ret >= 0) { |
| 4142 | return ret; |
| 4143 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 4144 | if (!check_caller(c, e->v.Call.func)) { |
| 4145 | return 0; |
| 4146 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4147 | VISIT(c, expr, e->v.Call.func); |
| 4148 | return compiler_call_helper(c, 0, |
| 4149 | e->v.Call.args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4150 | e->v.Call.keywords); |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4151 | } |
| 4152 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4153 | static int |
| 4154 | compiler_joined_str(struct compiler *c, expr_ty e) |
| 4155 | { |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4156 | VISIT_SEQ(c, expr, e->v.JoinedStr.values); |
Serhiy Storchaka | 4cc30ae | 2016-12-11 19:37:19 +0200 | [diff] [blame] | 4157 | if (asdl_seq_LEN(e->v.JoinedStr.values) != 1) |
| 4158 | 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] | 4159 | return 1; |
| 4160 | } |
| 4161 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4162 | /* Used to implement f-strings. Format a single value. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4163 | static int |
| 4164 | compiler_formatted_value(struct compiler *c, expr_ty e) |
| 4165 | { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4166 | /* Our oparg encodes 2 pieces of information: the conversion |
| 4167 | character, and whether or not a format_spec was provided. |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4168 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4169 | Convert the conversion char to 3 bits: |
| 4170 | : 000 0x0 FVC_NONE The default if nothing specified. |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4171 | !s : 001 0x1 FVC_STR |
| 4172 | !r : 010 0x2 FVC_REPR |
| 4173 | !a : 011 0x3 FVC_ASCII |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4174 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4175 | next bit is whether or not we have a format spec: |
| 4176 | yes : 100 0x4 |
| 4177 | no : 000 0x0 |
| 4178 | */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4179 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4180 | int conversion = e->v.FormattedValue.conversion; |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4181 | int oparg; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4182 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4183 | /* The expression to be formatted. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4184 | VISIT(c, expr, e->v.FormattedValue.value); |
| 4185 | |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4186 | switch (conversion) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4187 | case 's': oparg = FVC_STR; break; |
| 4188 | case 'r': oparg = FVC_REPR; break; |
| 4189 | case 'a': oparg = FVC_ASCII; break; |
| 4190 | case -1: oparg = FVC_NONE; break; |
| 4191 | default: |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4192 | PyErr_Format(PyExc_SystemError, |
| 4193 | "Unrecognized conversion character %d", conversion); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4194 | return 0; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4195 | } |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4196 | if (e->v.FormattedValue.format_spec) { |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4197 | /* Evaluate the format spec, and update our opcode arg. */ |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4198 | VISIT(c, expr, e->v.FormattedValue.format_spec); |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4199 | oparg |= FVS_HAVE_SPEC; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4200 | } |
| 4201 | |
Eric V. Smith | a78c795 | 2015-11-03 12:45:05 -0500 | [diff] [blame] | 4202 | /* And push our opcode and oparg */ |
| 4203 | ADDOP_I(c, FORMAT_VALUE, oparg); |
Eric V. Smith | 9a4135e | 2019-05-08 16:28:48 -0400 | [diff] [blame] | 4204 | |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 4205 | return 1; |
| 4206 | } |
| 4207 | |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4208 | static int |
| 4209 | compiler_subkwargs(struct compiler *c, asdl_seq *keywords, Py_ssize_t begin, Py_ssize_t end) |
| 4210 | { |
| 4211 | Py_ssize_t i, n = end - begin; |
| 4212 | keyword_ty kw; |
| 4213 | PyObject *keys, *key; |
| 4214 | assert(n > 0); |
| 4215 | if (n > 1) { |
| 4216 | for (i = begin; i < end; i++) { |
| 4217 | kw = asdl_seq_GET(keywords, i); |
| 4218 | VISIT(c, expr, kw->value); |
| 4219 | } |
| 4220 | keys = PyTuple_New(n); |
| 4221 | if (keys == NULL) { |
| 4222 | return 0; |
| 4223 | } |
| 4224 | for (i = begin; i < end; i++) { |
| 4225 | key = ((keyword_ty) asdl_seq_GET(keywords, i))->arg; |
| 4226 | Py_INCREF(key); |
| 4227 | PyTuple_SET_ITEM(keys, i - begin, key); |
| 4228 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4229 | ADDOP_LOAD_CONST_NEW(c, keys); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4230 | ADDOP_I(c, BUILD_CONST_KEY_MAP, n); |
| 4231 | } |
| 4232 | else { |
| 4233 | /* a for loop only executes once */ |
| 4234 | for (i = begin; i < end; i++) { |
| 4235 | kw = asdl_seq_GET(keywords, i); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4236 | ADDOP_LOAD_CONST(c, kw->arg); |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4237 | VISIT(c, expr, kw->value); |
| 4238 | } |
| 4239 | ADDOP_I(c, BUILD_MAP, n); |
| 4240 | } |
| 4241 | return 1; |
| 4242 | } |
| 4243 | |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4244 | /* shared code between compiler_call and compiler_class */ |
| 4245 | static int |
| 4246 | compiler_call_helper(struct compiler *c, |
Victor Stinner | 976bb40 | 2016-03-23 11:36:19 +0100 | [diff] [blame] | 4247 | int n, /* Args already pushed */ |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4248 | asdl_seq *args, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4249 | asdl_seq *keywords) |
Guido van Rossum | 52cc1d8 | 2007-03-18 15:41:51 +0000 | [diff] [blame] | 4250 | { |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4251 | Py_ssize_t i, nseen, nelts, nkwelts; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4252 | |
Pablo Galindo | 254ec78 | 2020-04-03 20:37:13 +0100 | [diff] [blame] | 4253 | if (validate_keywords(c, keywords) == -1) { |
| 4254 | return 0; |
| 4255 | } |
| 4256 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4257 | nelts = asdl_seq_LEN(args); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4258 | nkwelts = asdl_seq_LEN(keywords); |
| 4259 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4260 | for (i = 0; i < nelts; i++) { |
| 4261 | expr_ty elt = asdl_seq_GET(args, i); |
| 4262 | if (elt->kind == Starred_kind) { |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4263 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4264 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4265 | } |
| 4266 | for (i = 0; i < nkwelts; i++) { |
| 4267 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4268 | if (kw->arg == NULL) { |
| 4269 | goto ex_call; |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4270 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4271 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4272 | |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4273 | /* No * or ** args, so can use faster calling sequence */ |
| 4274 | for (i = 0; i < nelts; i++) { |
| 4275 | expr_ty elt = asdl_seq_GET(args, i); |
| 4276 | assert(elt->kind != Starred_kind); |
| 4277 | VISIT(c, expr, elt); |
| 4278 | } |
| 4279 | if (nkwelts) { |
| 4280 | PyObject *names; |
| 4281 | VISIT_SEQ(c, keyword, keywords); |
| 4282 | names = PyTuple_New(nkwelts); |
| 4283 | if (names == NULL) { |
| 4284 | return 0; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4285 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4286 | for (i = 0; i < nkwelts; i++) { |
| 4287 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4288 | Py_INCREF(kw->arg); |
| 4289 | PyTuple_SET_ITEM(names, i, kw->arg); |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4290 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4291 | ADDOP_LOAD_CONST_NEW(c, names); |
| 4292 | ADDOP_I(c, CALL_FUNCTION_KW, n + nelts + nkwelts); |
| 4293 | return 1; |
| 4294 | } |
| 4295 | else { |
| 4296 | ADDOP_I(c, CALL_FUNCTION, n + nelts); |
| 4297 | return 1; |
| 4298 | } |
| 4299 | |
| 4300 | ex_call: |
| 4301 | |
| 4302 | /* Do positional arguments. */ |
| 4303 | if (n ==0 && nelts == 1 && ((expr_ty)asdl_seq_GET(args, 0))->kind == Starred_kind) { |
| 4304 | VISIT(c, expr, ((expr_ty)asdl_seq_GET(args, 0))->v.Starred.value); |
| 4305 | } |
| 4306 | else if (starunpack_helper(c, args, n, BUILD_LIST, |
| 4307 | LIST_APPEND, LIST_EXTEND, 1) == 0) { |
| 4308 | return 0; |
| 4309 | } |
| 4310 | /* Then keyword arguments */ |
| 4311 | if (nkwelts) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4312 | /* Has a new dict been pushed */ |
| 4313 | int have_dict = 0; |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4314 | |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4315 | nseen = 0; /* the number of keyword arguments on the stack following */ |
| 4316 | for (i = 0; i < nkwelts; i++) { |
| 4317 | keyword_ty kw = asdl_seq_GET(keywords, i); |
| 4318 | if (kw->arg == NULL) { |
| 4319 | /* A keyword argument unpacking. */ |
| 4320 | if (nseen) { |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4321 | if (!compiler_subkwargs(c, keywords, i - nseen, i)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4322 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4323 | } |
Miss Islington (bot) | 410b730 | 2020-06-01 09:07:32 -0700 | [diff] [blame] | 4324 | if (have_dict) { |
| 4325 | ADDOP_I(c, DICT_MERGE, 1); |
| 4326 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4327 | have_dict = 1; |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4328 | nseen = 0; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4329 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4330 | if (!have_dict) { |
| 4331 | ADDOP_I(c, BUILD_MAP, 0); |
| 4332 | have_dict = 1; |
| 4333 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4334 | VISIT(c, expr, kw->value); |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4335 | ADDOP_I(c, DICT_MERGE, 1); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4336 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4337 | else { |
| 4338 | nseen++; |
| 4339 | } |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4340 | } |
Victor Stinner | f9b760f | 2016-09-09 10:17:08 -0700 | [diff] [blame] | 4341 | if (nseen) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4342 | /* Pack up any trailing keyword arguments. */ |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4343 | if (!compiler_subkwargs(c, keywords, nkwelts - nseen, nkwelts)) { |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4344 | return 0; |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4345 | } |
| 4346 | if (have_dict) { |
| 4347 | ADDOP_I(c, DICT_MERGE, 1); |
| 4348 | } |
| 4349 | have_dict = 1; |
Serhiy Storchaka | 6a7506a | 2016-06-12 00:39:41 +0300 | [diff] [blame] | 4350 | } |
Mark Shannon | 8a4cd70 | 2020-01-27 09:57:45 +0000 | [diff] [blame] | 4351 | assert(have_dict); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4352 | } |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4353 | ADDOP_I(c, CALL_FUNCTION_EX, nkwelts > 0); |
| 4354 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4355 | } |
| 4356 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4357 | |
| 4358 | /* List and set comprehensions and generator expressions work by creating a |
| 4359 | nested function to perform the actual iteration. This means that the |
| 4360 | iteration variables don't leak into the current scope. |
| 4361 | The defined function is called immediately following its definition, with the |
| 4362 | result of that call being the result of the expression. |
| 4363 | The LC/SC version returns the populated container, while the GE version is |
| 4364 | flagged in symtable.c as a generator, so it returns the generator object |
| 4365 | when the function is called. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4366 | |
| 4367 | Possible cleanups: |
| 4368 | - iterate over the generator sequence instead of using recursion |
| 4369 | */ |
| 4370 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4371 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4372 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4373 | compiler_comprehension_generator(struct compiler *c, |
| 4374 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4375 | int depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4376 | expr_ty elt, expr_ty val, int type) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4377 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4378 | comprehension_ty gen; |
| 4379 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4380 | if (gen->is_async) { |
| 4381 | return compiler_async_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4382 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4383 | } else { |
| 4384 | return compiler_sync_comprehension_generator( |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4385 | c, generators, gen_index, depth, elt, val, type); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4386 | } |
| 4387 | } |
| 4388 | |
| 4389 | static int |
| 4390 | compiler_sync_comprehension_generator(struct compiler *c, |
| 4391 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4392 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4393 | expr_ty elt, expr_ty val, int type) |
| 4394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4395 | /* generate code for the iterator, then each of the ifs, |
| 4396 | and then write to the element */ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4398 | comprehension_ty gen; |
| 4399 | basicblock *start, *anchor, *skip, *if_cleanup; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 4400 | Py_ssize_t i, n; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4401 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4402 | start = compiler_new_block(c); |
| 4403 | skip = compiler_new_block(c); |
| 4404 | if_cleanup = compiler_new_block(c); |
| 4405 | anchor = compiler_new_block(c); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4406 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4407 | if (start == NULL || skip == NULL || if_cleanup == NULL || |
| 4408 | anchor == NULL) |
| 4409 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4410 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4411 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4412 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4413 | if (gen_index == 0) { |
| 4414 | /* Receive outermost iter as an implicit argument */ |
| 4415 | c->u->u_argcount = 1; |
| 4416 | ADDOP_I(c, LOAD_FAST, 0); |
| 4417 | } |
| 4418 | else { |
| 4419 | /* Sub-iter - calculate on the fly */ |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4420 | /* Fast path for the temporary variable assignment idiom: |
| 4421 | for y in [f(x)] |
| 4422 | */ |
| 4423 | asdl_seq *elts; |
| 4424 | switch (gen->iter->kind) { |
| 4425 | case List_kind: |
| 4426 | elts = gen->iter->v.List.elts; |
| 4427 | break; |
| 4428 | case Tuple_kind: |
| 4429 | elts = gen->iter->v.Tuple.elts; |
| 4430 | break; |
| 4431 | default: |
| 4432 | elts = NULL; |
| 4433 | } |
| 4434 | if (asdl_seq_LEN(elts) == 1) { |
| 4435 | expr_ty elt = asdl_seq_GET(elts, 0); |
| 4436 | if (elt->kind != Starred_kind) { |
| 4437 | VISIT(c, expr, elt); |
| 4438 | start = NULL; |
| 4439 | } |
| 4440 | } |
| 4441 | if (start) { |
| 4442 | VISIT(c, expr, gen->iter); |
| 4443 | ADDOP(c, GET_ITER); |
| 4444 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4445 | } |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4446 | if (start) { |
| 4447 | depth++; |
| 4448 | compiler_use_next_block(c, start); |
| 4449 | ADDOP_JREL(c, FOR_ITER, anchor); |
| 4450 | NEXT_BLOCK(c); |
| 4451 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4452 | VISIT(c, expr, gen->target); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4453 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4454 | /* XXX this needs to be cleaned up...a lot! */ |
| 4455 | n = asdl_seq_LEN(gen->ifs); |
| 4456 | for (i = 0; i < n; i++) { |
| 4457 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4458 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4459 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4460 | NEXT_BLOCK(c); |
| 4461 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4462 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4463 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4464 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4465 | generators, gen_index, depth, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4466 | elt, val, type)) |
| 4467 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4468 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4469 | /* only append after the last for generator */ |
| 4470 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4471 | /* comprehension specific code */ |
| 4472 | switch (type) { |
| 4473 | case COMP_GENEXP: |
| 4474 | VISIT(c, expr, elt); |
| 4475 | ADDOP(c, YIELD_VALUE); |
| 4476 | ADDOP(c, POP_TOP); |
| 4477 | break; |
| 4478 | case COMP_LISTCOMP: |
| 4479 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4480 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4481 | break; |
| 4482 | case COMP_SETCOMP: |
| 4483 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4484 | ADDOP_I(c, SET_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4485 | break; |
| 4486 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4487 | /* With '{k: v}', k is evaluated before v, so we do |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4488 | the same. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4489 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4490 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4491 | ADDOP_I(c, MAP_ADD, depth + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4492 | break; |
| 4493 | default: |
| 4494 | return 0; |
| 4495 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4496 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4497 | compiler_use_next_block(c, skip); |
| 4498 | } |
| 4499 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4500 | if (start) { |
| 4501 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4502 | compiler_use_next_block(c, anchor); |
| 4503 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4504 | |
| 4505 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4506 | } |
| 4507 | |
| 4508 | static int |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4509 | compiler_async_comprehension_generator(struct compiler *c, |
| 4510 | asdl_seq *generators, int gen_index, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4511 | int depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4512 | expr_ty elt, expr_ty val, int type) |
| 4513 | { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4514 | comprehension_ty gen; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4515 | basicblock *start, *if_cleanup, *except; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4516 | Py_ssize_t i, n; |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4517 | start = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4518 | except = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4519 | if_cleanup = compiler_new_block(c); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4520 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4521 | if (start == NULL || if_cleanup == NULL || except == NULL) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4522 | return 0; |
| 4523 | } |
| 4524 | |
| 4525 | gen = (comprehension_ty)asdl_seq_GET(generators, gen_index); |
| 4526 | |
| 4527 | if (gen_index == 0) { |
| 4528 | /* Receive outermost iter as an implicit argument */ |
| 4529 | c->u->u_argcount = 1; |
| 4530 | ADDOP_I(c, LOAD_FAST, 0); |
| 4531 | } |
| 4532 | else { |
| 4533 | /* Sub-iter - calculate on the fly */ |
| 4534 | VISIT(c, expr, gen->iter); |
| 4535 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4536 | } |
| 4537 | |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4538 | compiler_use_next_block(c, start); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4539 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4540 | ADDOP_JREL(c, SETUP_FINALLY, except); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4541 | ADDOP(c, GET_ANEXT); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4542 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4543 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4544 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 24d3201 | 2018-03-10 18:22:34 +0200 | [diff] [blame] | 4545 | VISIT(c, expr, gen->target); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4546 | |
| 4547 | n = asdl_seq_LEN(gen->ifs); |
| 4548 | for (i = 0; i < n; i++) { |
| 4549 | expr_ty e = (expr_ty)asdl_seq_GET(gen->ifs, i); |
Serhiy Storchaka | 36ff451 | 2017-06-11 14:50:22 +0300 | [diff] [blame] | 4550 | if (!compiler_jump_if(c, e, if_cleanup, 0)) |
| 4551 | return 0; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4552 | NEXT_BLOCK(c); |
| 4553 | } |
| 4554 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4555 | depth++; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4556 | if (++gen_index < asdl_seq_LEN(generators)) |
| 4557 | if (!compiler_comprehension_generator(c, |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4558 | generators, gen_index, depth, |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4559 | elt, val, type)) |
| 4560 | return 0; |
| 4561 | |
| 4562 | /* only append after the last for generator */ |
| 4563 | if (gen_index >= asdl_seq_LEN(generators)) { |
| 4564 | /* comprehension specific code */ |
| 4565 | switch (type) { |
| 4566 | case COMP_GENEXP: |
| 4567 | VISIT(c, expr, elt); |
| 4568 | ADDOP(c, YIELD_VALUE); |
| 4569 | ADDOP(c, POP_TOP); |
| 4570 | break; |
| 4571 | case COMP_LISTCOMP: |
| 4572 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4573 | ADDOP_I(c, LIST_APPEND, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4574 | break; |
| 4575 | case COMP_SETCOMP: |
| 4576 | VISIT(c, expr, elt); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4577 | ADDOP_I(c, SET_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4578 | break; |
| 4579 | case COMP_DICTCOMP: |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4580 | /* With '{k: v}', k is evaluated before v, so we do |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4581 | the same. */ |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4582 | VISIT(c, expr, elt); |
Jörn Heissler | c8a3541 | 2019-06-22 16:40:55 +0200 | [diff] [blame] | 4583 | VISIT(c, expr, val); |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4584 | ADDOP_I(c, MAP_ADD, depth + 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4585 | break; |
| 4586 | default: |
| 4587 | return 0; |
| 4588 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4589 | } |
| 4590 | compiler_use_next_block(c, if_cleanup); |
Serhiy Storchaka | 702f8f3 | 2018-03-23 14:34:35 +0200 | [diff] [blame] | 4591 | ADDOP_JABS(c, JUMP_ABSOLUTE, start); |
| 4592 | |
| 4593 | compiler_use_next_block(c, except); |
| 4594 | ADDOP(c, END_ASYNC_FOR); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4595 | |
| 4596 | return 1; |
| 4597 | } |
| 4598 | |
| 4599 | static int |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 4600 | compiler_comprehension(struct compiler *c, expr_ty e, int type, |
| 4601 | identifier name, asdl_seq *generators, expr_ty elt, |
| 4602 | expr_ty val) |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4603 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4604 | PyCodeObject *co = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4605 | comprehension_ty outermost; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4606 | PyObject *qualname = NULL; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4607 | int is_async_generator = 0; |
Pablo Galindo | 6488a4a | 2020-07-06 23:30:20 +0100 | [diff] [blame^] | 4608 | int top_level_await = IS_TOP_LEVEL_AWAIT(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4609 | |
Pablo Galindo | 6488a4a | 2020-07-06 23:30:20 +0100 | [diff] [blame^] | 4610 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4611 | int is_async_function = c->u->u_ste->ste_coroutine; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4612 | |
Batuhan TaÅŸkaya | 9052f7a | 2020-03-19 14:35:44 +0300 | [diff] [blame] | 4613 | outermost = (comprehension_ty) asdl_seq_GET(generators, 0); |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4614 | if (!compiler_enter_scope(c, name, COMPILER_SCOPE_COMPREHENSION, |
| 4615 | (void *)e, e->lineno)) |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4616 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4617 | goto error; |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4618 | } |
| 4619 | |
| 4620 | is_async_generator = c->u->u_ste->ste_coroutine; |
| 4621 | |
Pablo Galindo | 6488a4a | 2020-07-06 23:30:20 +0100 | [diff] [blame^] | 4622 | if (is_async_generator && !is_async_function && type != COMP_GENEXP && !top_level_await) { |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4623 | compiler_error(c, "asynchronous comprehension outside of " |
| 4624 | "an asynchronous function"); |
| 4625 | goto error_in_scope; |
| 4626 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4627 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4628 | if (type != COMP_GENEXP) { |
| 4629 | int op; |
| 4630 | switch (type) { |
| 4631 | case COMP_LISTCOMP: |
| 4632 | op = BUILD_LIST; |
| 4633 | break; |
| 4634 | case COMP_SETCOMP: |
| 4635 | op = BUILD_SET; |
| 4636 | break; |
| 4637 | case COMP_DICTCOMP: |
| 4638 | op = BUILD_MAP; |
| 4639 | break; |
| 4640 | default: |
| 4641 | PyErr_Format(PyExc_SystemError, |
| 4642 | "unknown comprehension type %d", type); |
| 4643 | goto error_in_scope; |
| 4644 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4645 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4646 | ADDOP_I(c, op, 0); |
| 4647 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4648 | |
Serhiy Storchaka | 8c579b1 | 2020-02-12 12:18:59 +0200 | [diff] [blame] | 4649 | if (!compiler_comprehension_generator(c, generators, 0, 0, elt, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4650 | val, type)) |
| 4651 | goto error_in_scope; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4652 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4653 | if (type != COMP_GENEXP) { |
| 4654 | ADDOP(c, RETURN_VALUE); |
| 4655 | } |
| 4656 | |
| 4657 | co = assemble(c, 1); |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4658 | qualname = c->u->u_qualname; |
| 4659 | Py_INCREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4660 | compiler_exit_scope(c); |
Pablo Galindo | 6488a4a | 2020-07-06 23:30:20 +0100 | [diff] [blame^] | 4661 | if (top_level_await && is_async_generator){ |
| 4662 | c->u->u_ste->ste_coroutine = 1; |
| 4663 | } |
Benjamin Peterson | 6b4f780 | 2013-10-20 17:50:28 -0400 | [diff] [blame] | 4664 | if (co == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4665 | goto error; |
| 4666 | |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4667 | if (!compiler_make_closure(c, co, 0, qualname)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4668 | goto error; |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4669 | Py_DECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4670 | Py_DECREF(co); |
| 4671 | |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4672 | VISIT(c, expr, outermost->iter); |
| 4673 | |
| 4674 | if (outermost->is_async) { |
| 4675 | ADDOP(c, GET_AITER); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4676 | } else { |
| 4677 | ADDOP(c, GET_ITER); |
| 4678 | } |
| 4679 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4680 | ADDOP_I(c, CALL_FUNCTION, 1); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4681 | |
| 4682 | if (is_async_generator && type != COMP_GENEXP) { |
| 4683 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4684 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 4685 | ADDOP(c, YIELD_FROM); |
| 4686 | } |
| 4687 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4688 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4689 | error_in_scope: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4690 | compiler_exit_scope(c); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4691 | error: |
Antoine Pitrou | 86a36b5 | 2011-11-25 18:56:07 +0100 | [diff] [blame] | 4692 | Py_XDECREF(qualname); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4693 | Py_XDECREF(co); |
| 4694 | return 0; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4698 | compiler_genexp(struct compiler *c, expr_ty e) |
| 4699 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4700 | static identifier name; |
| 4701 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4702 | name = PyUnicode_InternFromString("<genexpr>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4703 | if (!name) |
| 4704 | return 0; |
| 4705 | } |
| 4706 | assert(e->kind == GeneratorExp_kind); |
| 4707 | return compiler_comprehension(c, e, COMP_GENEXP, name, |
| 4708 | e->v.GeneratorExp.generators, |
| 4709 | e->v.GeneratorExp.elt, NULL); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4710 | } |
| 4711 | |
| 4712 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4713 | compiler_listcomp(struct compiler *c, expr_ty e) |
| 4714 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4715 | static identifier name; |
| 4716 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4717 | name = PyUnicode_InternFromString("<listcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4718 | if (!name) |
| 4719 | return 0; |
| 4720 | } |
| 4721 | assert(e->kind == ListComp_kind); |
| 4722 | return compiler_comprehension(c, e, COMP_LISTCOMP, name, |
| 4723 | e->v.ListComp.generators, |
| 4724 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4725 | } |
| 4726 | |
| 4727 | static int |
| 4728 | compiler_setcomp(struct compiler *c, expr_ty e) |
| 4729 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4730 | static identifier name; |
| 4731 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4732 | name = PyUnicode_InternFromString("<setcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4733 | if (!name) |
| 4734 | return 0; |
| 4735 | } |
| 4736 | assert(e->kind == SetComp_kind); |
| 4737 | return compiler_comprehension(c, e, COMP_SETCOMP, name, |
| 4738 | e->v.SetComp.generators, |
| 4739 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 4740 | } |
| 4741 | |
| 4742 | |
| 4743 | static int |
| 4744 | compiler_dictcomp(struct compiler *c, expr_ty e) |
| 4745 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4746 | static identifier name; |
| 4747 | if (!name) { |
Zackery Spytz | f303639 | 2018-04-15 16:12:29 -0600 | [diff] [blame] | 4748 | name = PyUnicode_InternFromString("<dictcomp>"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4749 | if (!name) |
| 4750 | return 0; |
| 4751 | } |
| 4752 | assert(e->kind == DictComp_kind); |
| 4753 | return compiler_comprehension(c, e, COMP_DICTCOMP, name, |
| 4754 | e->v.DictComp.generators, |
| 4755 | e->v.DictComp.key, e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 4756 | } |
| 4757 | |
| 4758 | |
| 4759 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4760 | compiler_visit_keyword(struct compiler *c, keyword_ty k) |
| 4761 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4762 | VISIT(c, expr, k->value); |
| 4763 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4764 | } |
| 4765 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4766 | /* Test whether expression is constant. For constants, report |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4767 | whether they are true or false. |
| 4768 | |
| 4769 | Return values: 1 for true, 0 for false, -1 for non-constant. |
| 4770 | */ |
| 4771 | |
| 4772 | static int |
Serhiy Storchaka | 3dfbaf5 | 2017-12-25 12:47:50 +0200 | [diff] [blame] | 4773 | expr_constant(expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4774 | { |
Serhiy Storchaka | 3f22811 | 2018-09-27 17:42:37 +0300 | [diff] [blame] | 4775 | if (e->kind == Constant_kind) { |
| 4776 | return PyObject_IsTrue(e->v.Constant.value); |
Benjamin Peterson | 442f209 | 2012-12-06 17:41:04 -0500 | [diff] [blame] | 4777 | } |
Serhiy Storchaka | 3325a67 | 2017-12-15 12:35:48 +0200 | [diff] [blame] | 4778 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4779 | } |
| 4780 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4781 | static int |
| 4782 | compiler_with_except_finish(struct compiler *c) { |
| 4783 | basicblock *exit; |
| 4784 | exit = compiler_new_block(c); |
| 4785 | if (exit == NULL) |
| 4786 | return 0; |
| 4787 | ADDOP_JABS(c, POP_JUMP_IF_TRUE, exit); |
| 4788 | ADDOP(c, RERAISE); |
| 4789 | compiler_use_next_block(c, exit); |
| 4790 | ADDOP(c, POP_TOP); |
| 4791 | ADDOP(c, POP_TOP); |
| 4792 | ADDOP(c, POP_TOP); |
| 4793 | ADDOP(c, POP_EXCEPT); |
| 4794 | ADDOP(c, POP_TOP); |
| 4795 | return 1; |
| 4796 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4797 | |
| 4798 | /* |
| 4799 | Implements the async with statement. |
| 4800 | |
| 4801 | The semantics outlined in that PEP are as follows: |
| 4802 | |
| 4803 | async with EXPR as VAR: |
| 4804 | BLOCK |
| 4805 | |
| 4806 | It is implemented roughly as: |
| 4807 | |
| 4808 | context = EXPR |
| 4809 | exit = context.__aexit__ # not calling it |
| 4810 | value = await context.__aenter__() |
| 4811 | try: |
| 4812 | VAR = value # if VAR present in the syntax |
| 4813 | BLOCK |
| 4814 | finally: |
| 4815 | if an exception was raised: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4816 | exc = copy of (exception, instance, traceback) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4817 | else: |
Serhiy Storchaka | ec466a1 | 2015-06-11 00:09:32 +0300 | [diff] [blame] | 4818 | exc = (None, None, None) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4819 | if not (await exit(*exc)): |
| 4820 | raise |
| 4821 | */ |
| 4822 | static int |
| 4823 | compiler_async_with(struct compiler *c, stmt_ty s, int pos) |
| 4824 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4825 | basicblock *block, *final, *exit; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4826 | withitem_ty item = asdl_seq_GET(s->v.AsyncWith.items, pos); |
| 4827 | |
| 4828 | assert(s->kind == AsyncWith_kind); |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 4829 | if (IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 4830 | c->u->u_ste->ste_coroutine = 1; |
| 4831 | } else if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION){ |
Zsolt Dollenstein | e239650 | 2018-04-27 08:58:56 -0700 | [diff] [blame] | 4832 | return compiler_error(c, "'async with' outside async function"); |
| 4833 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4834 | |
| 4835 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4836 | final = compiler_new_block(c); |
| 4837 | exit = compiler_new_block(c); |
| 4838 | if (!block || !final || !exit) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4839 | return 0; |
| 4840 | |
| 4841 | /* Evaluate EXPR */ |
| 4842 | VISIT(c, expr, item->context_expr); |
| 4843 | |
| 4844 | ADDOP(c, BEFORE_ASYNC_WITH); |
| 4845 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4846 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4847 | ADDOP(c, YIELD_FROM); |
| 4848 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4849 | ADDOP_JREL(c, SETUP_ASYNC_WITH, final); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4850 | |
| 4851 | /* SETUP_ASYNC_WITH pushes a finally block. */ |
| 4852 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4853 | if (!compiler_push_fblock(c, ASYNC_WITH, block, final, NULL)) { |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4854 | return 0; |
| 4855 | } |
| 4856 | |
| 4857 | if (item->optional_vars) { |
| 4858 | VISIT(c, expr, item->optional_vars); |
| 4859 | } |
| 4860 | else { |
| 4861 | /* Discard result from context.__aenter__() */ |
| 4862 | ADDOP(c, POP_TOP); |
| 4863 | } |
| 4864 | |
| 4865 | pos++; |
| 4866 | if (pos == asdl_seq_LEN(s->v.AsyncWith.items)) |
| 4867 | /* BLOCK code */ |
| 4868 | VISIT_SEQ(c, stmt, s->v.AsyncWith.body) |
| 4869 | else if (!compiler_async_with(c, s, pos)) |
| 4870 | return 0; |
| 4871 | |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4872 | compiler_pop_fblock(c, ASYNC_WITH, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4873 | ADDOP(c, POP_BLOCK); |
| 4874 | /* End of body; start the cleanup */ |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4875 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4876 | /* For successful outcome: |
| 4877 | * call __exit__(None, None, None) |
| 4878 | */ |
| 4879 | if(!compiler_call_exit_with_nones(c)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4880 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4881 | ADDOP(c, GET_AWAITABLE); |
| 4882 | ADDOP_O(c, LOAD_CONST, Py_None, consts); |
| 4883 | ADDOP(c, YIELD_FROM); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4884 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4885 | ADDOP(c, POP_TOP); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4886 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4887 | ADDOP_JABS(c, JUMP_ABSOLUTE, exit); |
| 4888 | |
| 4889 | /* For exceptional outcome: */ |
| 4890 | compiler_use_next_block(c, final); |
| 4891 | |
| 4892 | ADDOP(c, WITH_EXCEPT_START); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4893 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 4894 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4895 | ADDOP(c, YIELD_FROM); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4896 | compiler_with_except_finish(c); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4897 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4898 | compiler_use_next_block(c, exit); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 4899 | return 1; |
| 4900 | } |
| 4901 | |
| 4902 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4903 | /* |
| 4904 | Implements the with statement from PEP 343. |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4905 | with EXPR as VAR: |
| 4906 | BLOCK |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4907 | is implemented as: |
| 4908 | <code for EXPR> |
| 4909 | SETUP_WITH E |
| 4910 | <code to store to VAR> or POP_TOP |
| 4911 | <code for BLOCK> |
| 4912 | LOAD_CONST (None, None, None) |
| 4913 | CALL_FUNCTION_EX 0 |
| 4914 | JUMP_FORWARD EXIT |
| 4915 | E: WITH_EXCEPT_START (calls EXPR.__exit__) |
| 4916 | POP_JUMP_IF_TRUE T: |
| 4917 | RERAISE |
| 4918 | T: POP_TOP * 3 (remove exception from stack) |
| 4919 | POP_EXCEPT |
| 4920 | POP_TOP |
| 4921 | EXIT: |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4922 | */ |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4923 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4924 | static int |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4925 | compiler_with(struct compiler *c, stmt_ty s, int pos) |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4926 | { |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4927 | basicblock *block, *final, *exit; |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4928 | withitem_ty item = asdl_seq_GET(s->v.With.items, pos); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4929 | |
| 4930 | assert(s->kind == With_kind); |
| 4931 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4932 | block = compiler_new_block(c); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4933 | final = compiler_new_block(c); |
| 4934 | exit = compiler_new_block(c); |
| 4935 | if (!block || !final || !exit) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4936 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4937 | |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 4938 | /* Evaluate EXPR */ |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4939 | VISIT(c, expr, item->context_expr); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4940 | /* Will push bound __exit__ */ |
| 4941 | ADDOP_JREL(c, SETUP_WITH, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4942 | |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4943 | /* SETUP_WITH pushes a finally block. */ |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4944 | compiler_use_next_block(c, block); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4945 | if (!compiler_push_fblock(c, WITH, block, final, NULL)) { |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4946 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4947 | } |
| 4948 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4949 | if (item->optional_vars) { |
| 4950 | VISIT(c, expr, item->optional_vars); |
Benjamin Peterson | 876b2f2 | 2009-06-28 03:18:59 +0000 | [diff] [blame] | 4951 | } |
| 4952 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4953 | /* Discard result from context.__enter__() */ |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4954 | ADDOP(c, POP_TOP); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4955 | } |
| 4956 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 4957 | pos++; |
| 4958 | if (pos == asdl_seq_LEN(s->v.With.items)) |
| 4959 | /* BLOCK code */ |
| 4960 | VISIT_SEQ(c, stmt, s->v.With.body) |
| 4961 | else if (!compiler_with(c, s, pos)) |
| 4962 | return 0; |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4963 | |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4964 | ADDOP(c, POP_BLOCK); |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 4965 | compiler_pop_fblock(c, WITH, block); |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4966 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4967 | /* End of body; start the cleanup. */ |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 4968 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4969 | /* For successful outcome: |
| 4970 | * call __exit__(None, None, None) |
| 4971 | */ |
| 4972 | if (!compiler_call_exit_with_nones(c)) |
Antoine Pitrou | 9aee2c8 | 2010-06-22 21:49:39 +0000 | [diff] [blame] | 4973 | return 0; |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4974 | ADDOP(c, POP_TOP); |
| 4975 | ADDOP_JREL(c, JUMP_FORWARD, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4976 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4977 | /* For exceptional outcome: */ |
| 4978 | compiler_use_next_block(c, final); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4979 | |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 4980 | ADDOP(c, WITH_EXCEPT_START); |
| 4981 | compiler_with_except_finish(c); |
| 4982 | |
| 4983 | compiler_use_next_block(c, exit); |
Guido van Rossum | c2e2074 | 2006-02-27 22:32:47 +0000 | [diff] [blame] | 4984 | return 1; |
| 4985 | } |
| 4986 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4987 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 4988 | compiler_visit_expr1(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 4989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4990 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 4991 | case NamedExpr_kind: |
| 4992 | VISIT(c, expr, e->v.NamedExpr.value); |
| 4993 | ADDOP(c, DUP_TOP); |
| 4994 | VISIT(c, expr, e->v.NamedExpr.target); |
| 4995 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 4996 | case BoolOp_kind: |
| 4997 | return compiler_boolop(c, e); |
| 4998 | case BinOp_kind: |
| 4999 | VISIT(c, expr, e->v.BinOp.left); |
| 5000 | VISIT(c, expr, e->v.BinOp.right); |
Andy Lester | 76d5877 | 2020-03-10 21:18:12 -0500 | [diff] [blame] | 5001 | ADDOP(c, binop(e->v.BinOp.op)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5002 | break; |
| 5003 | case UnaryOp_kind: |
| 5004 | VISIT(c, expr, e->v.UnaryOp.operand); |
| 5005 | ADDOP(c, unaryop(e->v.UnaryOp.op)); |
| 5006 | break; |
| 5007 | case Lambda_kind: |
| 5008 | return compiler_lambda(c, e); |
| 5009 | case IfExp_kind: |
| 5010 | return compiler_ifexp(c, e); |
| 5011 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5012 | return compiler_dict(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5013 | case Set_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5014 | return compiler_set(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5015 | case GeneratorExp_kind: |
| 5016 | return compiler_genexp(c, e); |
| 5017 | case ListComp_kind: |
| 5018 | return compiler_listcomp(c, e); |
| 5019 | case SetComp_kind: |
| 5020 | return compiler_setcomp(c, e); |
| 5021 | case DictComp_kind: |
| 5022 | return compiler_dictcomp(c, e); |
| 5023 | case Yield_kind: |
| 5024 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5025 | return compiler_error(c, "'yield' outside function"); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5026 | if (e->v.Yield.value) { |
| 5027 | VISIT(c, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5028 | } |
| 5029 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5030 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5031 | } |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5032 | ADDOP(c, YIELD_VALUE); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5033 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5034 | case YieldFrom_kind: |
| 5035 | if (c->u->u_ste->ste_type != FunctionBlock) |
| 5036 | return compiler_error(c, "'yield' outside function"); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5037 | |
| 5038 | if (c->u->u_scope_type == COMPILER_SCOPE_ASYNC_FUNCTION) |
| 5039 | return compiler_error(c, "'yield from' inside async function"); |
| 5040 | |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5041 | VISIT(c, expr, e->v.YieldFrom.value); |
Yury Selivanov | 5376ba9 | 2015-06-22 12:19:30 -0400 | [diff] [blame] | 5042 | ADDOP(c, GET_YIELD_FROM_ITER); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5043 | ADDOP_LOAD_CONST(c, Py_None); |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 5044 | ADDOP(c, YIELD_FROM); |
| 5045 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5046 | case Await_kind: |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5047 | if (!IS_TOP_LEVEL_AWAIT(c)){ |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5048 | if (c->u->u_ste->ste_type != FunctionBlock){ |
| 5049 | return compiler_error(c, "'await' outside function"); |
| 5050 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5051 | |
Victor Stinner | 331a6a5 | 2019-05-27 16:39:22 +0200 | [diff] [blame] | 5052 | if (c->u->u_scope_type != COMPILER_SCOPE_ASYNC_FUNCTION && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5053 | c->u->u_scope_type != COMPILER_SCOPE_COMPREHENSION){ |
| 5054 | return compiler_error(c, "'await' outside async function"); |
| 5055 | } |
| 5056 | } |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5057 | |
| 5058 | VISIT(c, expr, e->v.Await.value); |
| 5059 | ADDOP(c, GET_AWAITABLE); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5060 | ADDOP_LOAD_CONST(c, Py_None); |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 5061 | ADDOP(c, YIELD_FROM); |
| 5062 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5063 | case Compare_kind: |
| 5064 | return compiler_compare(c, e); |
| 5065 | case Call_kind: |
| 5066 | return compiler_call(c, e); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5067 | case Constant_kind: |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5068 | ADDOP_LOAD_CONST(c, e->v.Constant.value); |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 5069 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 5070 | case JoinedStr_kind: |
| 5071 | return compiler_joined_str(c, e); |
| 5072 | case FormattedValue_kind: |
| 5073 | return compiler_formatted_value(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5074 | /* The following exprs can be assignment targets. */ |
| 5075 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5076 | VISIT(c, expr, e->v.Attribute.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5077 | switch (e->v.Attribute.ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5078 | case Load: |
| 5079 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
| 5080 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5081 | case Store: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5082 | if (forbidden_name(c, e->v.Attribute.attr, e->v.Attribute.ctx)) |
| 5083 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5084 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5085 | break; |
| 5086 | case Del: |
| 5087 | ADDOP_NAME(c, DELETE_ATTR, e->v.Attribute.attr, names); |
| 5088 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5089 | } |
| 5090 | break; |
| 5091 | case Subscript_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5092 | return compiler_subscript(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5093 | case Starred_kind: |
| 5094 | switch (e->v.Starred.ctx) { |
| 5095 | case Store: |
| 5096 | /* In all legitimate cases, the Starred node was already replaced |
| 5097 | * by compiler_list/compiler_tuple. XXX: is that okay? */ |
| 5098 | return compiler_error(c, |
| 5099 | "starred assignment target must be in a list or tuple"); |
| 5100 | default: |
| 5101 | return compiler_error(c, |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 5102 | "can't use starred expression here"); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5103 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5104 | break; |
| 5105 | case Slice_kind: |
| 5106 | return compiler_slice(c, e); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5107 | case Name_kind: |
| 5108 | return compiler_nameop(c, e->v.Name.id, e->v.Name.ctx); |
| 5109 | /* child nodes of List and Tuple will have expr_context set */ |
| 5110 | case List_kind: |
| 5111 | return compiler_list(c, e); |
| 5112 | case Tuple_kind: |
| 5113 | return compiler_tuple(c, e); |
| 5114 | } |
| 5115 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5116 | } |
| 5117 | |
| 5118 | static int |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5119 | compiler_visit_expr(struct compiler *c, expr_ty e) |
| 5120 | { |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5121 | int old_lineno = c->u->u_lineno; |
| 5122 | int old_col_offset = c->u->u_col_offset; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5123 | SET_LOC(c, e); |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5124 | int res = compiler_visit_expr1(c, e); |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5125 | c->u->u_lineno = old_lineno; |
Serhiy Storchaka | da8d72c | 2018-09-17 15:17:29 +0300 | [diff] [blame] | 5126 | c->u->u_col_offset = old_col_offset; |
| 5127 | return res; |
| 5128 | } |
| 5129 | |
| 5130 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5131 | compiler_augassign(struct compiler *c, stmt_ty s) |
| 5132 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5133 | assert(s->kind == AugAssign_kind); |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5134 | expr_ty e = s->v.AugAssign.target; |
| 5135 | |
| 5136 | int old_lineno = c->u->u_lineno; |
| 5137 | int old_col_offset = c->u->u_col_offset; |
| 5138 | SET_LOC(c, e); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5139 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5140 | switch (e->kind) { |
| 5141 | case Attribute_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5142 | VISIT(c, expr, e->v.Attribute.value); |
| 5143 | ADDOP(c, DUP_TOP); |
| 5144 | ADDOP_NAME(c, LOAD_ATTR, e->v.Attribute.attr, names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5145 | break; |
| 5146 | case Subscript_kind: |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5147 | VISIT(c, expr, e->v.Subscript.value); |
| 5148 | VISIT(c, expr, e->v.Subscript.slice); |
| 5149 | ADDOP(c, DUP_TOP_TWO); |
| 5150 | ADDOP(c, BINARY_SUBSCR); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5151 | break; |
| 5152 | case Name_kind: |
| 5153 | if (!compiler_nameop(c, e->v.Name.id, Load)) |
| 5154 | return 0; |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5155 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5156 | default: |
| 5157 | PyErr_Format(PyExc_SystemError, |
| 5158 | "invalid node type (%d) for augmented assignment", |
| 5159 | e->kind); |
| 5160 | return 0; |
| 5161 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5162 | |
| 5163 | c->u->u_lineno = old_lineno; |
| 5164 | c->u->u_col_offset = old_col_offset; |
| 5165 | |
| 5166 | VISIT(c, expr, s->v.AugAssign.value); |
| 5167 | ADDOP(c, inplace_binop(s->v.AugAssign.op)); |
| 5168 | |
| 5169 | SET_LOC(c, e); |
| 5170 | |
| 5171 | switch (e->kind) { |
| 5172 | case Attribute_kind: |
| 5173 | ADDOP(c, ROT_TWO); |
| 5174 | ADDOP_NAME(c, STORE_ATTR, e->v.Attribute.attr, names); |
| 5175 | break; |
| 5176 | case Subscript_kind: |
| 5177 | ADDOP(c, ROT_THREE); |
| 5178 | ADDOP(c, STORE_SUBSCR); |
| 5179 | break; |
| 5180 | case Name_kind: |
| 5181 | return compiler_nameop(c, e->v.Name.id, Store); |
| 5182 | default: |
| 5183 | Py_UNREACHABLE(); |
| 5184 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5185 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5186 | } |
| 5187 | |
| 5188 | static int |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5189 | check_ann_expr(struct compiler *c, expr_ty e) |
| 5190 | { |
| 5191 | VISIT(c, expr, e); |
| 5192 | ADDOP(c, POP_TOP); |
| 5193 | return 1; |
| 5194 | } |
| 5195 | |
| 5196 | static int |
| 5197 | check_annotation(struct compiler *c, stmt_ty s) |
| 5198 | { |
| 5199 | /* Annotations are only evaluated in a module or class. */ |
| 5200 | if (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5201 | c->u->u_scope_type == COMPILER_SCOPE_CLASS) { |
| 5202 | return check_ann_expr(c, s->v.AnnAssign.annotation); |
| 5203 | } |
| 5204 | return 1; |
| 5205 | } |
| 5206 | |
| 5207 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5208 | check_ann_subscr(struct compiler *c, expr_ty e) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5209 | { |
| 5210 | /* We check that everything in a subscript is defined at runtime. */ |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5211 | switch (e->kind) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5212 | case Slice_kind: |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5213 | 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] | 5214 | return 0; |
| 5215 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5216 | if (e->v.Slice.upper && !check_ann_expr(c, e->v.Slice.upper)) { |
| 5217 | return 0; |
| 5218 | } |
| 5219 | if (e->v.Slice.step && !check_ann_expr(c, e->v.Slice.step)) { |
| 5220 | return 0; |
| 5221 | } |
| 5222 | return 1; |
| 5223 | case Tuple_kind: { |
| 5224 | /* extended slice */ |
| 5225 | asdl_seq *elts = e->v.Tuple.elts; |
| 5226 | Py_ssize_t i, n = asdl_seq_LEN(elts); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5227 | for (i = 0; i < n; i++) { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5228 | if (!check_ann_subscr(c, asdl_seq_GET(elts, i))) { |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5229 | return 0; |
| 5230 | } |
| 5231 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5232 | return 1; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5233 | } |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5234 | default: |
| 5235 | return check_ann_expr(c, e); |
| 5236 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5237 | } |
| 5238 | |
| 5239 | static int |
| 5240 | compiler_annassign(struct compiler *c, stmt_ty s) |
| 5241 | { |
| 5242 | expr_ty targ = s->v.AnnAssign.target; |
Guido van Rossum | 015d874 | 2016-09-11 09:45:24 -0700 | [diff] [blame] | 5243 | PyObject* mangled; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5244 | |
| 5245 | assert(s->kind == AnnAssign_kind); |
| 5246 | |
| 5247 | /* We perform the actual assignment first. */ |
| 5248 | if (s->v.AnnAssign.value) { |
| 5249 | VISIT(c, expr, s->v.AnnAssign.value); |
| 5250 | VISIT(c, expr, targ); |
| 5251 | } |
| 5252 | switch (targ->kind) { |
| 5253 | case Name_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5254 | if (forbidden_name(c, targ->v.Name.id, Store)) |
| 5255 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5256 | /* If we have a simple name in a module or class, store annotation. */ |
| 5257 | if (s->v.AnnAssign.simple && |
| 5258 | (c->u->u_scope_type == COMPILER_SCOPE_MODULE || |
| 5259 | c->u->u_scope_type == COMPILER_SCOPE_CLASS)) { |
Guido van Rossum | 95e4d58 | 2018-01-26 08:20:18 -0800 | [diff] [blame] | 5260 | if (c->c_future->ff_features & CO_FUTURE_ANNOTATIONS) { |
| 5261 | VISIT(c, annexpr, s->v.AnnAssign.annotation) |
| 5262 | } |
| 5263 | else { |
| 5264 | VISIT(c, expr, s->v.AnnAssign.annotation); |
| 5265 | } |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5266 | ADDOP_NAME(c, LOAD_NAME, __annotations__, names); |
Serhiy Storchaka | a95d986 | 2018-03-24 22:42:35 +0200 | [diff] [blame] | 5267 | mangled = _Py_Mangle(c->u->u_private, targ->v.Name.id); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5268 | ADDOP_LOAD_CONST_NEW(c, mangled); |
Mark Shannon | 332cd5e | 2018-01-30 00:41:04 +0000 | [diff] [blame] | 5269 | ADDOP(c, STORE_SUBSCR); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5270 | } |
| 5271 | break; |
| 5272 | case Attribute_kind: |
Pablo Galindo | c5fc156 | 2020-04-22 23:29:27 +0100 | [diff] [blame] | 5273 | if (forbidden_name(c, targ->v.Attribute.attr, Store)) |
| 5274 | return 0; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 5275 | if (!s->v.AnnAssign.value && |
| 5276 | !check_ann_expr(c, targ->v.Attribute.value)) { |
| 5277 | return 0; |
| 5278 | } |
| 5279 | break; |
| 5280 | case Subscript_kind: |
| 5281 | if (!s->v.AnnAssign.value && |
| 5282 | (!check_ann_expr(c, targ->v.Subscript.value) || |
| 5283 | !check_ann_subscr(c, targ->v.Subscript.slice))) { |
| 5284 | return 0; |
| 5285 | } |
| 5286 | break; |
| 5287 | default: |
| 5288 | PyErr_Format(PyExc_SystemError, |
| 5289 | "invalid node type (%d) for annotated assignment", |
| 5290 | targ->kind); |
| 5291 | return 0; |
| 5292 | } |
| 5293 | /* Annotation is evaluated last. */ |
| 5294 | if (!s->v.AnnAssign.simple && !check_annotation(c, s)) { |
| 5295 | return 0; |
| 5296 | } |
| 5297 | return 1; |
| 5298 | } |
| 5299 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5300 | /* Raises a SyntaxError and returns 0. |
| 5301 | If something goes wrong, a different exception may be raised. |
| 5302 | */ |
| 5303 | |
| 5304 | static int |
| 5305 | compiler_error(struct compiler *c, const char *errstr) |
| 5306 | { |
Benjamin Peterson | 43b0686 | 2011-05-27 09:08:01 -0500 | [diff] [blame] | 5307 | PyObject *loc; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5308 | PyObject *u = NULL, *v = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5309 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5310 | loc = PyErr_ProgramTextObject(c->c_filename, c->u->u_lineno); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5311 | if (!loc) { |
| 5312 | Py_INCREF(Py_None); |
| 5313 | loc = Py_None; |
| 5314 | } |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 5315 | u = Py_BuildValue("(OiiO)", c->c_filename, c->u->u_lineno, |
Ammar Askar | 025eb98 | 2018-09-24 17:12:49 -0400 | [diff] [blame] | 5316 | c->u->u_col_offset + 1, loc); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5317 | if (!u) |
| 5318 | goto exit; |
| 5319 | v = Py_BuildValue("(zO)", errstr, u); |
| 5320 | if (!v) |
| 5321 | goto exit; |
| 5322 | PyErr_SetObject(PyExc_SyntaxError, v); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5323 | exit: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5324 | Py_DECREF(loc); |
| 5325 | Py_XDECREF(u); |
| 5326 | Py_XDECREF(v); |
| 5327 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5328 | } |
| 5329 | |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5330 | /* Emits a SyntaxWarning and returns 1 on success. |
| 5331 | If a SyntaxWarning raised as error, replaces it with a SyntaxError |
| 5332 | and returns 0. |
| 5333 | */ |
| 5334 | static int |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5335 | compiler_warn(struct compiler *c, const char *format, ...) |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5336 | { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5337 | va_list vargs; |
| 5338 | #ifdef HAVE_STDARG_PROTOTYPES |
| 5339 | va_start(vargs, format); |
| 5340 | #else |
| 5341 | va_start(vargs); |
| 5342 | #endif |
| 5343 | PyObject *msg = PyUnicode_FromFormatV(format, vargs); |
| 5344 | va_end(vargs); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5345 | if (msg == NULL) { |
| 5346 | return 0; |
| 5347 | } |
| 5348 | if (PyErr_WarnExplicitObject(PyExc_SyntaxWarning, msg, c->c_filename, |
| 5349 | c->u->u_lineno, NULL, NULL) < 0) |
| 5350 | { |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5351 | if (PyErr_ExceptionMatches(PyExc_SyntaxWarning)) { |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5352 | /* Replace the SyntaxWarning exception with a SyntaxError |
| 5353 | to get a more accurate error report */ |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5354 | PyErr_Clear(); |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5355 | assert(PyUnicode_AsUTF8(msg) != NULL); |
| 5356 | compiler_error(c, PyUnicode_AsUTF8(msg)); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5357 | } |
Serhiy Storchaka | 62e4481 | 2019-02-16 08:12:19 +0200 | [diff] [blame] | 5358 | Py_DECREF(msg); |
Serhiy Storchaka | d31e773 | 2018-10-21 10:09:39 +0300 | [diff] [blame] | 5359 | return 0; |
| 5360 | } |
| 5361 | Py_DECREF(msg); |
| 5362 | return 1; |
| 5363 | } |
| 5364 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5365 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5366 | compiler_subscript(struct compiler *c, expr_ty e) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5367 | { |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5368 | expr_context_ty ctx = e->v.Subscript.ctx; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5369 | int op = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5370 | |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5371 | if (ctx == Load) { |
| 5372 | if (!check_subscripter(c, e->v.Subscript.value)) { |
| 5373 | return 0; |
| 5374 | } |
| 5375 | if (!check_index(c, e->v.Subscript.value, e->v.Subscript.slice)) { |
| 5376 | return 0; |
| 5377 | } |
| 5378 | } |
| 5379 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5380 | switch (ctx) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5381 | case Load: op = BINARY_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5382 | case Store: op = STORE_SUBSCR; break; |
| 5383 | case Del: op = DELETE_SUBSCR; break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5384 | } |
Serhiy Storchaka | 6b97598 | 2020-03-17 23:41:08 +0200 | [diff] [blame] | 5385 | assert(op); |
| 5386 | VISIT(c, expr, e->v.Subscript.value); |
| 5387 | VISIT(c, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5388 | ADDOP(c, op); |
| 5389 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5390 | } |
| 5391 | |
| 5392 | static int |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 5393 | compiler_slice(struct compiler *c, expr_ty s) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5394 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5395 | int n = 2; |
| 5396 | assert(s->kind == Slice_kind); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5397 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5398 | /* only handles the cases where BUILD_SLICE is emitted */ |
| 5399 | if (s->v.Slice.lower) { |
| 5400 | VISIT(c, expr, s->v.Slice.lower); |
| 5401 | } |
| 5402 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5403 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5404 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5405 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5406 | if (s->v.Slice.upper) { |
| 5407 | VISIT(c, expr, s->v.Slice.upper); |
| 5408 | } |
| 5409 | else { |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5410 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5411 | } |
| 5412 | |
| 5413 | if (s->v.Slice.step) { |
| 5414 | n++; |
| 5415 | VISIT(c, expr, s->v.Slice.step); |
| 5416 | } |
| 5417 | ADDOP_I(c, BUILD_SLICE, n); |
| 5418 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5419 | } |
| 5420 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5421 | /* End of the compiler section, beginning of the assembler section */ |
| 5422 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5423 | /* do depth-first search of basic block graph, starting with block. |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5424 | post records the block indices in post-order. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5425 | |
| 5426 | XXX must handle implicit jumps from one block to next |
| 5427 | */ |
| 5428 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5429 | struct assembler { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5430 | PyObject *a_bytecode; /* string containing bytecode */ |
| 5431 | int a_offset; /* offset into bytecode */ |
| 5432 | int a_nblocks; /* number of reachable blocks */ |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5433 | basicblock **a_postorder; /* list of blocks in dfs postorder */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5434 | PyObject *a_lnotab; /* string containing lnotab */ |
| 5435 | int a_lnotab_off; /* offset into lnotab */ |
| 5436 | int a_lineno; /* last lineno of emitted instruction */ |
| 5437 | int a_lineno_off; /* bytecode offset of last lineno */ |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 5438 | }; |
| 5439 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5440 | static void |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5441 | dfs(struct compiler *c, basicblock *b, struct assembler *a, int end) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5442 | { |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5443 | int i, j; |
| 5444 | |
| 5445 | /* Get rid of recursion for normal control flow. |
| 5446 | Since the number of blocks is limited, unused space in a_postorder |
| 5447 | (from a_nblocks to end) can be used as a stack for still not ordered |
| 5448 | blocks. */ |
| 5449 | for (j = end; b && !b->b_seen; b = b->b_next) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5450 | b->b_seen = 1; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5451 | assert(a->a_nblocks < j); |
| 5452 | a->a_postorder[--j] = b; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5453 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5454 | while (j < end) { |
| 5455 | b = a->a_postorder[j++]; |
| 5456 | for (i = 0; i < b->b_iused; i++) { |
| 5457 | struct instr *instr = &b->b_instr[i]; |
| 5458 | if (instr->i_jrel || instr->i_jabs) |
| 5459 | dfs(c, instr->i_target, a, j); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5460 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5461 | assert(a->a_nblocks < j); |
| 5462 | a->a_postorder[a->a_nblocks++] = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5463 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5464 | } |
| 5465 | |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5466 | Py_LOCAL_INLINE(void) |
| 5467 | stackdepth_push(basicblock ***sp, basicblock *b, int depth) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5468 | { |
Serhiy Storchaka | 520b7ae | 2018-02-22 23:33:30 +0200 | [diff] [blame] | 5469 | assert(b->b_startdepth < 0 || b->b_startdepth == depth); |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5470 | if (b->b_startdepth < depth && b->b_startdepth < 100) { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5471 | assert(b->b_startdepth < 0); |
| 5472 | b->b_startdepth = depth; |
| 5473 | *(*sp)++ = b; |
Serhiy Storchaka | d4864c6 | 2018-01-09 21:54:52 +0200 | [diff] [blame] | 5474 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5475 | } |
| 5476 | |
| 5477 | /* Find the flow path that needs the largest stack. We assume that |
| 5478 | * cycles in the flow graph have no net effect on the stack depth. |
| 5479 | */ |
| 5480 | static int |
| 5481 | stackdepth(struct compiler *c) |
| 5482 | { |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5483 | basicblock *b, *entryblock = NULL; |
| 5484 | basicblock **stack, **sp; |
| 5485 | int nblocks = 0, maxdepth = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5486 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5487 | b->b_startdepth = INT_MIN; |
| 5488 | entryblock = b; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5489 | nblocks++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5490 | } |
| 5491 | if (!entryblock) |
| 5492 | return 0; |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5493 | stack = (basicblock **)PyObject_Malloc(sizeof(basicblock *) * nblocks); |
| 5494 | if (!stack) { |
| 5495 | PyErr_NoMemory(); |
| 5496 | return -1; |
| 5497 | } |
| 5498 | |
| 5499 | sp = stack; |
| 5500 | stackdepth_push(&sp, entryblock, 0); |
| 5501 | while (sp != stack) { |
| 5502 | b = *--sp; |
| 5503 | int depth = b->b_startdepth; |
| 5504 | assert(depth >= 0); |
| 5505 | basicblock *next = b->b_next; |
| 5506 | for (int i = 0; i < b->b_iused; i++) { |
| 5507 | struct instr *instr = &b->b_instr[i]; |
| 5508 | int effect = stack_effect(instr->i_opcode, instr->i_oparg, 0); |
| 5509 | if (effect == PY_INVALID_STACK_EFFECT) { |
Victor Stinner | 87d3b9d | 2020-03-25 19:27:36 +0100 | [diff] [blame] | 5510 | _Py_FatalErrorFormat(__func__, |
| 5511 | "opcode = %d", instr->i_opcode); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5512 | } |
| 5513 | int new_depth = depth + effect; |
| 5514 | if (new_depth > maxdepth) { |
| 5515 | maxdepth = new_depth; |
| 5516 | } |
| 5517 | assert(depth >= 0); /* invalid code or bug in stackdepth() */ |
| 5518 | if (instr->i_jrel || instr->i_jabs) { |
| 5519 | effect = stack_effect(instr->i_opcode, instr->i_oparg, 1); |
| 5520 | assert(effect != PY_INVALID_STACK_EFFECT); |
| 5521 | int target_depth = depth + effect; |
| 5522 | if (target_depth > maxdepth) { |
| 5523 | maxdepth = target_depth; |
| 5524 | } |
| 5525 | assert(target_depth >= 0); /* invalid code or bug in stackdepth() */ |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5526 | stackdepth_push(&sp, instr->i_target, target_depth); |
| 5527 | } |
| 5528 | depth = new_depth; |
| 5529 | if (instr->i_opcode == JUMP_ABSOLUTE || |
| 5530 | instr->i_opcode == JUMP_FORWARD || |
| 5531 | instr->i_opcode == RETURN_VALUE || |
Mark Shannon | fee5526 | 2019-11-21 09:11:43 +0000 | [diff] [blame] | 5532 | instr->i_opcode == RAISE_VARARGS || |
| 5533 | instr->i_opcode == RERAISE) |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5534 | { |
| 5535 | /* remaining code is dead */ |
| 5536 | next = NULL; |
| 5537 | break; |
| 5538 | } |
| 5539 | } |
| 5540 | if (next != NULL) { |
| 5541 | stackdepth_push(&sp, next, depth); |
| 5542 | } |
| 5543 | } |
| 5544 | PyObject_Free(stack); |
| 5545 | return maxdepth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | static int |
| 5549 | assemble_init(struct assembler *a, int nblocks, int firstlineno) |
| 5550 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5551 | memset(a, 0, sizeof(struct assembler)); |
| 5552 | a->a_lineno = firstlineno; |
| 5553 | a->a_bytecode = PyBytes_FromStringAndSize(NULL, DEFAULT_CODE_SIZE); |
| 5554 | if (!a->a_bytecode) |
| 5555 | return 0; |
| 5556 | a->a_lnotab = PyBytes_FromStringAndSize(NULL, DEFAULT_LNOTAB_SIZE); |
| 5557 | if (!a->a_lnotab) |
| 5558 | return 0; |
Benjamin Peterson | 2f8bfef | 2016-09-07 09:26:18 -0700 | [diff] [blame] | 5559 | if ((size_t)nblocks > SIZE_MAX / sizeof(basicblock *)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5560 | PyErr_NoMemory(); |
| 5561 | return 0; |
| 5562 | } |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5563 | a->a_postorder = (basicblock **)PyObject_Malloc( |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5564 | sizeof(basicblock *) * nblocks); |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5565 | if (!a->a_postorder) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5566 | PyErr_NoMemory(); |
| 5567 | return 0; |
| 5568 | } |
| 5569 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5570 | } |
| 5571 | |
| 5572 | static void |
| 5573 | assemble_free(struct assembler *a) |
| 5574 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5575 | Py_XDECREF(a->a_bytecode); |
| 5576 | Py_XDECREF(a->a_lnotab); |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5577 | if (a->a_postorder) |
| 5578 | PyObject_Free(a->a_postorder); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5579 | } |
| 5580 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5581 | static int |
| 5582 | blocksize(basicblock *b) |
| 5583 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5584 | int i; |
| 5585 | int size = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5586 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5587 | for (i = 0; i < b->b_iused; i++) |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5588 | size += instrsize(b->b_instr[i].i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5589 | return size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5590 | } |
| 5591 | |
Alexandre Vassalotti | 7b82b40 | 2009-07-21 04:30:03 +0000 | [diff] [blame] | 5592 | /* Appends a pair to the end of the line number table, a_lnotab, representing |
| 5593 | the instruction's bytecode offset and line number. See |
| 5594 | Objects/lnotab_notes.txt for the description of the line number table. */ |
Tim Peters | 2a7f384 | 2001-06-09 09:26:21 +0000 | [diff] [blame] | 5595 | |
Guido van Rossum | f68d8e5 | 2001-04-14 17:55:09 +0000 | [diff] [blame] | 5596 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5597 | assemble_lnotab(struct assembler *a, struct instr *i) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5598 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5599 | int d_bytecode, d_lineno; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5600 | Py_ssize_t len; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5601 | unsigned char *lnotab; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5602 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5603 | d_lineno = i->i_lineno - a->a_lineno; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5604 | if (d_lineno == 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5605 | return 1; |
Serhiy Storchaka | 61cb3d0 | 2020-03-17 18:07:30 +0200 | [diff] [blame] | 5606 | } |
| 5607 | |
| 5608 | d_bytecode = (a->a_offset - a->a_lineno_off) * sizeof(_Py_CODEUNIT); |
| 5609 | assert(d_bytecode >= 0); |
Guido van Rossum | 4bad92c | 1991-07-27 21:34:52 +0000 | [diff] [blame] | 5610 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5611 | if (d_bytecode > 255) { |
| 5612 | int j, nbytes, ncodes = d_bytecode / 255; |
| 5613 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5614 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5615 | if (nbytes >= len) { |
| 5616 | if ((len <= INT_MAX / 2) && (len * 2 < nbytes)) |
| 5617 | len = nbytes; |
| 5618 | else if (len <= INT_MAX / 2) |
| 5619 | len *= 2; |
| 5620 | else { |
| 5621 | PyErr_NoMemory(); |
| 5622 | return 0; |
| 5623 | } |
| 5624 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5625 | return 0; |
| 5626 | } |
| 5627 | lnotab = (unsigned char *) |
| 5628 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5629 | for (j = 0; j < ncodes; j++) { |
| 5630 | *lnotab++ = 255; |
| 5631 | *lnotab++ = 0; |
| 5632 | } |
| 5633 | d_bytecode -= ncodes * 255; |
| 5634 | a->a_lnotab_off += ncodes * 2; |
| 5635 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5636 | assert(0 <= d_bytecode && d_bytecode <= 255); |
| 5637 | |
| 5638 | if (d_lineno < -128 || 127 < d_lineno) { |
| 5639 | int j, nbytes, ncodes, k; |
| 5640 | if (d_lineno < 0) { |
| 5641 | k = -128; |
| 5642 | /* use division on positive numbers */ |
| 5643 | ncodes = (-d_lineno) / 128; |
| 5644 | } |
| 5645 | else { |
| 5646 | k = 127; |
| 5647 | ncodes = d_lineno / 127; |
| 5648 | } |
| 5649 | d_lineno -= ncodes * k; |
| 5650 | assert(ncodes >= 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5651 | nbytes = a->a_lnotab_off + 2 * ncodes; |
| 5652 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5653 | if (nbytes >= len) { |
| 5654 | if ((len <= INT_MAX / 2) && len * 2 < nbytes) |
| 5655 | len = nbytes; |
| 5656 | else if (len <= INT_MAX / 2) |
| 5657 | len *= 2; |
| 5658 | else { |
| 5659 | PyErr_NoMemory(); |
| 5660 | return 0; |
| 5661 | } |
| 5662 | if (_PyBytes_Resize(&a->a_lnotab, len) < 0) |
| 5663 | return 0; |
| 5664 | } |
| 5665 | lnotab = (unsigned char *) |
| 5666 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
| 5667 | *lnotab++ = d_bytecode; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5668 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5669 | d_bytecode = 0; |
| 5670 | for (j = 1; j < ncodes; j++) { |
| 5671 | *lnotab++ = 0; |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5672 | *lnotab++ = k; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5673 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5674 | a->a_lnotab_off += ncodes * 2; |
| 5675 | } |
Victor Stinner | f3914eb | 2016-01-20 12:16:21 +0100 | [diff] [blame] | 5676 | assert(-128 <= d_lineno && d_lineno <= 127); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5677 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5678 | len = PyBytes_GET_SIZE(a->a_lnotab); |
| 5679 | if (a->a_lnotab_off + 2 >= len) { |
| 5680 | if (_PyBytes_Resize(&a->a_lnotab, len * 2) < 0) |
| 5681 | return 0; |
| 5682 | } |
| 5683 | lnotab = (unsigned char *) |
| 5684 | PyBytes_AS_STRING(a->a_lnotab) + a->a_lnotab_off; |
Tim Peters | 51e2651 | 2001-09-07 08:45:55 +0000 | [diff] [blame] | 5685 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5686 | a->a_lnotab_off += 2; |
| 5687 | if (d_bytecode) { |
| 5688 | *lnotab++ = d_bytecode; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5689 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5690 | } |
| 5691 | else { /* First line of a block; def stmt, etc. */ |
| 5692 | *lnotab++ = 0; |
Victor Stinner | 4f2dab5 | 2011-05-27 16:46:51 +0200 | [diff] [blame] | 5693 | *lnotab++ = d_lineno; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5694 | } |
| 5695 | a->a_lineno = i->i_lineno; |
| 5696 | a->a_lineno_off = a->a_offset; |
| 5697 | return 1; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5698 | } |
| 5699 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5700 | /* assemble_emit() |
| 5701 | Extend the bytecode with a new instruction. |
| 5702 | Update lnotab if necessary. |
Jeremy Hylton | 376e63d | 2003-08-28 14:42:14 +0000 | [diff] [blame] | 5703 | */ |
| 5704 | |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5705 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5706 | assemble_emit(struct assembler *a, struct instr *i) |
Guido van Rossum | 4ca6c9d | 1994-08-29 12:16:12 +0000 | [diff] [blame] | 5707 | { |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5708 | int size, arg = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5709 | Py_ssize_t len = PyBytes_GET_SIZE(a->a_bytecode); |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5710 | _Py_CODEUNIT *code; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5711 | |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5712 | arg = i->i_oparg; |
| 5713 | size = instrsize(arg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5714 | if (i->i_lineno && !assemble_lnotab(a, i)) |
| 5715 | return 0; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5716 | if (a->a_offset + size >= len / (int)sizeof(_Py_CODEUNIT)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5717 | if (len > PY_SSIZE_T_MAX / 2) |
| 5718 | return 0; |
| 5719 | if (_PyBytes_Resize(&a->a_bytecode, len * 2) < 0) |
| 5720 | return 0; |
| 5721 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5722 | code = (_Py_CODEUNIT *)PyBytes_AS_STRING(a->a_bytecode) + a->a_offset; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5723 | a->a_offset += size; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5724 | write_op_arg(code, i->i_opcode, arg, size); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5725 | return 1; |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5726 | } |
| 5727 | |
Neal Norwitz | 7d37f2f | 2005-10-23 22:40:47 +0000 | [diff] [blame] | 5728 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5729 | assemble_jump_offsets(struct assembler *a, struct compiler *c) |
Anthony Baxter | c2a5a63 | 2004-08-02 06:10:11 +0000 | [diff] [blame] | 5730 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5731 | basicblock *b; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5732 | int bsize, totsize, extended_arg_recompile; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5733 | int i; |
Guido van Rossum | c5e9629 | 1991-12-10 13:53:51 +0000 | [diff] [blame] | 5734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5735 | /* Compute the size of each block and fixup jump args. |
| 5736 | Replace block pointer with position in bytecode. */ |
| 5737 | do { |
| 5738 | totsize = 0; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 5739 | for (i = a->a_nblocks - 1; i >= 0; i--) { |
| 5740 | b = a->a_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5741 | bsize = blocksize(b); |
| 5742 | b->b_offset = totsize; |
| 5743 | totsize += bsize; |
| 5744 | } |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5745 | extended_arg_recompile = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5746 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 5747 | bsize = b->b_offset; |
| 5748 | for (i = 0; i < b->b_iused; i++) { |
| 5749 | struct instr *instr = &b->b_instr[i]; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5750 | int isize = instrsize(instr->i_oparg); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5751 | /* Relative jumps are computed relative to |
| 5752 | the instruction pointer after fetching |
| 5753 | the jump instruction. |
| 5754 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5755 | bsize += isize; |
| 5756 | if (instr->i_jabs || instr->i_jrel) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5757 | instr->i_oparg = instr->i_target->b_offset; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5758 | if (instr->i_jrel) { |
| 5759 | instr->i_oparg -= bsize; |
| 5760 | } |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 5761 | instr->i_oparg *= sizeof(_Py_CODEUNIT); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5762 | if (instrsize(instr->i_oparg) != isize) { |
| 5763 | extended_arg_recompile = 1; |
| 5764 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5765 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5766 | } |
| 5767 | } |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5768 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5769 | /* XXX: This is an awful hack that could hurt performance, but |
| 5770 | on the bright side it should work until we come up |
| 5771 | with a better solution. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5772 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5773 | The issue is that in the first loop blocksize() is called |
| 5774 | which calls instrsize() which requires i_oparg be set |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5775 | appropriately. There is a bootstrap problem because |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5776 | i_oparg is calculated in the second loop above. |
Neal Norwitz | f1d5068 | 2005-10-23 23:00:41 +0000 | [diff] [blame] | 5777 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5778 | So we loop until we stop seeing new EXTENDED_ARGs. |
| 5779 | The only EXTENDED_ARGs that could be popping up are |
| 5780 | ones in jump instructions. So this should converge |
| 5781 | fairly quickly. |
| 5782 | */ |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5783 | } while (extended_arg_recompile); |
Guido van Rossum | 10dc2e8 | 1990-11-18 17:27:39 +0000 | [diff] [blame] | 5784 | } |
| 5785 | |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5786 | static PyObject * |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5787 | dict_keys_inorder(PyObject *dict, Py_ssize_t offset) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5788 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5789 | PyObject *tuple, *k, *v; |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5790 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5792 | tuple = PyTuple_New(size); |
| 5793 | if (tuple == NULL) |
| 5794 | return NULL; |
| 5795 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5796 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5797 | Py_INCREF(k); |
| 5798 | assert((i - offset) < size); |
| 5799 | assert((i - offset) >= 0); |
| 5800 | PyTuple_SET_ITEM(tuple, i - offset, k); |
| 5801 | } |
| 5802 | return tuple; |
| 5803 | } |
| 5804 | |
| 5805 | static PyObject * |
| 5806 | consts_dict_keys_inorder(PyObject *dict) |
| 5807 | { |
| 5808 | PyObject *consts, *k, *v; |
| 5809 | Py_ssize_t i, pos = 0, size = PyDict_GET_SIZE(dict); |
| 5810 | |
| 5811 | consts = PyList_New(size); /* PyCode_Optimize() requires a list */ |
| 5812 | if (consts == NULL) |
| 5813 | return NULL; |
| 5814 | while (PyDict_Next(dict, &pos, &k, &v)) { |
| 5815 | i = PyLong_AS_LONG(v); |
Serhiy Storchaka | b7e1eff | 2018-04-19 08:28:04 +0300 | [diff] [blame] | 5816 | /* The keys of the dictionary can be tuples wrapping a contant. |
| 5817 | * (see compiler_add_o and _PyCode_ConstantKey). In that case |
| 5818 | * the object we want is always second. */ |
| 5819 | if (PyTuple_CheckExact(k)) { |
| 5820 | k = PyTuple_GET_ITEM(k, 1); |
| 5821 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5822 | Py_INCREF(k); |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5823 | assert(i < size); |
| 5824 | assert(i >= 0); |
| 5825 | PyList_SET_ITEM(consts, i, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5826 | } |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5827 | return consts; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5828 | } |
| 5829 | |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5830 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5831 | compute_code_flags(struct compiler *c) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5832 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5833 | PySTEntryObject *ste = c->u->u_ste; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5834 | int flags = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5835 | if (ste->ste_type == FunctionBlock) { |
Benjamin Peterson | 1dfd247 | 2015-04-27 21:44:22 -0400 | [diff] [blame] | 5836 | flags |= CO_NEWLOCALS | CO_OPTIMIZED; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5837 | if (ste->ste_nested) |
| 5838 | flags |= CO_NESTED; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5839 | if (ste->ste_generator && !ste->ste_coroutine) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5840 | flags |= CO_GENERATOR; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 5841 | if (!ste->ste_generator && ste->ste_coroutine) |
| 5842 | flags |= CO_COROUTINE; |
| 5843 | if (ste->ste_generator && ste->ste_coroutine) |
| 5844 | flags |= CO_ASYNC_GENERATOR; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5845 | if (ste->ste_varargs) |
| 5846 | flags |= CO_VARARGS; |
| 5847 | if (ste->ste_varkeywords) |
| 5848 | flags |= CO_VARKEYWORDS; |
| 5849 | } |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5851 | /* (Only) inherit compilerflags in PyCF_MASK */ |
| 5852 | flags |= (c->c_flags->cf_flags & PyCF_MASK); |
Thomas Wouters | 5e9f1fa | 2006-02-28 20:02:27 +0000 | [diff] [blame] | 5853 | |
Pablo Galindo | 9023581 | 2020-03-15 04:29:22 +0000 | [diff] [blame] | 5854 | if ((IS_TOP_LEVEL_AWAIT(c)) && |
Matthias Bussonnier | 565b4f1 | 2019-05-21 13:12:03 -0700 | [diff] [blame] | 5855 | ste->ste_coroutine && |
| 5856 | !ste->ste_generator) { |
| 5857 | flags |= CO_COROUTINE; |
| 5858 | } |
| 5859 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5860 | return flags; |
Jeremy Hylton | 29906ee | 2001-02-27 04:23:34 +0000 | [diff] [blame] | 5861 | } |
| 5862 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5863 | // Merge *tuple* with constant cache. |
| 5864 | // Unlike merge_consts_recursive(), this function doesn't work recursively. |
| 5865 | static int |
| 5866 | merge_const_tuple(struct compiler *c, PyObject **tuple) |
| 5867 | { |
| 5868 | assert(PyTuple_CheckExact(*tuple)); |
| 5869 | |
| 5870 | PyObject *key = _PyCode_ConstantKey(*tuple); |
| 5871 | if (key == NULL) { |
| 5872 | return 0; |
| 5873 | } |
| 5874 | |
| 5875 | // t is borrowed reference |
| 5876 | PyObject *t = PyDict_SetDefault(c->c_const_cache, key, key); |
| 5877 | Py_DECREF(key); |
| 5878 | if (t == NULL) { |
| 5879 | return 0; |
| 5880 | } |
| 5881 | if (t == key) { // tuple is new constant. |
| 5882 | return 1; |
| 5883 | } |
| 5884 | |
| 5885 | PyObject *u = PyTuple_GET_ITEM(t, 1); |
| 5886 | Py_INCREF(u); |
| 5887 | Py_DECREF(*tuple); |
| 5888 | *tuple = u; |
| 5889 | return 1; |
| 5890 | } |
| 5891 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5892 | static PyCodeObject * |
| 5893 | makecode(struct compiler *c, struct assembler *a) |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5894 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5895 | PyObject *tmp; |
| 5896 | PyCodeObject *co = NULL; |
| 5897 | PyObject *consts = NULL; |
| 5898 | PyObject *names = NULL; |
| 5899 | PyObject *varnames = NULL; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5900 | PyObject *name = NULL; |
| 5901 | PyObject *freevars = NULL; |
| 5902 | PyObject *cellvars = NULL; |
| 5903 | PyObject *bytecode = NULL; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5904 | Py_ssize_t nlocals; |
| 5905 | int nlocals_int; |
| 5906 | int flags; |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5907 | int posorkeywordargcount, posonlyargcount, kwonlyargcount, maxdepth; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 5908 | |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5909 | consts = consts_dict_keys_inorder(c->u->u_consts); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5910 | names = dict_keys_inorder(c->u->u_names, 0); |
| 5911 | varnames = dict_keys_inorder(c->u->u_varnames, 0); |
| 5912 | if (!consts || !names || !varnames) |
| 5913 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5914 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5915 | cellvars = dict_keys_inorder(c->u->u_cellvars, 0); |
| 5916 | if (!cellvars) |
| 5917 | goto error; |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 5918 | freevars = dict_keys_inorder(c->u->u_freevars, PyTuple_GET_SIZE(cellvars)); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5919 | if (!freevars) |
| 5920 | goto error; |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5921 | |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5922 | if (!merge_const_tuple(c, &names) || |
| 5923 | !merge_const_tuple(c, &varnames) || |
| 5924 | !merge_const_tuple(c, &cellvars) || |
| 5925 | !merge_const_tuple(c, &freevars)) |
| 5926 | { |
| 5927 | goto error; |
| 5928 | } |
| 5929 | |
Serhiy Storchaka | 5ab81d7 | 2016-12-16 16:18:57 +0200 | [diff] [blame] | 5930 | nlocals = PyDict_GET_SIZE(c->u->u_varnames); |
Victor Stinner | ad9a066 | 2013-11-19 22:23:20 +0100 | [diff] [blame] | 5931 | assert(nlocals < INT_MAX); |
| 5932 | nlocals_int = Py_SAFE_DOWNCAST(nlocals, Py_ssize_t, int); |
| 5933 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5934 | flags = compute_code_flags(c); |
| 5935 | if (flags < 0) |
| 5936 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5938 | bytecode = PyCode_Optimize(a->a_bytecode, consts, names, a->a_lnotab); |
| 5939 | if (!bytecode) |
| 5940 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5941 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5942 | tmp = PyList_AsTuple(consts); /* PyCode_New requires a tuple */ |
| 5943 | if (!tmp) |
| 5944 | goto error; |
| 5945 | Py_DECREF(consts); |
| 5946 | consts = tmp; |
INADA Naoki | c2e1607 | 2018-11-26 21:23:22 +0900 | [diff] [blame] | 5947 | if (!merge_const_tuple(c, &consts)) { |
| 5948 | goto error; |
| 5949 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5950 | |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 5951 | posonlyargcount = Py_SAFE_DOWNCAST(c->u->u_posonlyargcount, Py_ssize_t, int); |
Pablo Galindo | cd74e66 | 2019-06-01 18:08:04 +0100 | [diff] [blame] | 5952 | posorkeywordargcount = Py_SAFE_DOWNCAST(c->u->u_argcount, Py_ssize_t, int); |
Victor Stinner | f8e3221 | 2013-11-19 23:56:34 +0100 | [diff] [blame] | 5953 | kwonlyargcount = Py_SAFE_DOWNCAST(c->u->u_kwonlyargcount, Py_ssize_t, int); |
Serhiy Storchaka | 782d6fe | 2018-01-11 20:20:13 +0200 | [diff] [blame] | 5954 | maxdepth = stackdepth(c); |
| 5955 | if (maxdepth < 0) { |
| 5956 | goto error; |
| 5957 | } |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5958 | co = PyCode_NewWithPosOnlyArgs(posonlyargcount+posorkeywordargcount, |
Mark Shannon | 13bc139 | 2020-01-23 09:25:17 +0000 | [diff] [blame] | 5959 | posonlyargcount, kwonlyargcount, nlocals_int, |
Pablo Galindo | 4a2edc3 | 2019-07-01 11:35:05 +0100 | [diff] [blame] | 5960 | maxdepth, flags, bytecode, consts, names, |
| 5961 | varnames, freevars, cellvars, c->c_filename, |
| 5962 | c->u->u_name, c->u->u_firstlineno, a->a_lnotab); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 5963 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5964 | Py_XDECREF(consts); |
| 5965 | Py_XDECREF(names); |
| 5966 | Py_XDECREF(varnames); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5967 | Py_XDECREF(name); |
| 5968 | Py_XDECREF(freevars); |
| 5969 | Py_XDECREF(cellvars); |
| 5970 | Py_XDECREF(bytecode); |
| 5971 | return co; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 5972 | } |
| 5973 | |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5974 | |
| 5975 | /* For debugging purposes only */ |
| 5976 | #if 0 |
| 5977 | static void |
| 5978 | dump_instr(const struct instr *i) |
| 5979 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5980 | const char *jrel = i->i_jrel ? "jrel " : ""; |
| 5981 | const char *jabs = i->i_jabs ? "jabs " : ""; |
| 5982 | char arg[128]; |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5983 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5984 | *arg = '\0'; |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5985 | if (HAS_ARG(i->i_opcode)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5986 | sprintf(arg, "arg: %d ", i->i_oparg); |
Serhiy Storchaka | b0f80b0 | 2016-05-24 09:15:14 +0300 | [diff] [blame] | 5987 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5988 | fprintf(stderr, "line: %d, opcode: %d %s%s%s\n", |
| 5989 | i->i_lineno, i->i_opcode, arg, jabs, jrel); |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 5990 | } |
| 5991 | |
| 5992 | static void |
| 5993 | dump_basicblock(const basicblock *b) |
| 5994 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 5995 | const char *seen = b->b_seen ? "seen " : ""; |
| 5996 | const char *b_return = b->b_return ? "return " : ""; |
| 5997 | fprintf(stderr, "used: %d, depth: %d, offset: %d %s%s\n", |
| 5998 | b->b_iused, b->b_startdepth, b->b_offset, seen, b_return); |
| 5999 | if (b->b_instr) { |
| 6000 | int i; |
| 6001 | for (i = 0; i < b->b_iused; i++) { |
| 6002 | fprintf(stderr, " [%02d] ", i); |
| 6003 | dump_instr(b->b_instr + i); |
| 6004 | } |
| 6005 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 6006 | } |
| 6007 | #endif |
| 6008 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6009 | static PyCodeObject * |
| 6010 | assemble(struct compiler *c, int addNone) |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6011 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6012 | basicblock *b, *entryblock; |
| 6013 | struct assembler a; |
| 6014 | int i, j, nblocks; |
| 6015 | PyCodeObject *co = NULL; |
Jeremy Hylton | 64949cb | 2001-01-25 20:06:59 +0000 | [diff] [blame] | 6016 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6017 | /* Make sure every block that falls off the end returns None. |
| 6018 | XXX NEXT_BLOCK() isn't quite right, because if the last |
| 6019 | block ends with a jump or return b_next shouldn't set. |
| 6020 | */ |
| 6021 | if (!c->u->u_curblock->b_return) { |
| 6022 | NEXT_BLOCK(c); |
| 6023 | if (addNone) |
Serhiy Storchaka | d70c2a6 | 2018-04-20 16:01:25 +0300 | [diff] [blame] | 6024 | ADDOP_LOAD_CONST(c, Py_None); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6025 | ADDOP(c, RETURN_VALUE); |
| 6026 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6027 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6028 | nblocks = 0; |
| 6029 | entryblock = NULL; |
| 6030 | for (b = c->u->u_blocks; b != NULL; b = b->b_list) { |
| 6031 | nblocks++; |
| 6032 | entryblock = b; |
| 6033 | } |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6034 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6035 | /* Set firstlineno if it wasn't explicitly set. */ |
| 6036 | if (!c->u->u_firstlineno) { |
Ned Deily | dc35cda | 2016-08-17 17:18:33 -0400 | [diff] [blame] | 6037 | if (entryblock && entryblock->b_instr && entryblock->b_instr->i_lineno) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6038 | c->u->u_firstlineno = entryblock->b_instr->i_lineno; |
| 6039 | else |
| 6040 | c->u->u_firstlineno = 1; |
| 6041 | } |
| 6042 | if (!assemble_init(&a, nblocks, c->u->u_firstlineno)) |
| 6043 | goto error; |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6044 | dfs(c, entryblock, &a, nblocks); |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6045 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6046 | /* Can't modify the bytecode after computing jump offsets. */ |
| 6047 | assemble_jump_offsets(&a, c); |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6048 | |
T. Wouters | 99b54d6 | 2019-09-12 07:05:33 -0700 | [diff] [blame] | 6049 | /* Emit code in reverse postorder from dfs. */ |
| 6050 | for (i = a.a_nblocks - 1; i >= 0; i--) { |
| 6051 | b = a.a_postorder[i]; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6052 | for (j = 0; j < b->b_iused; j++) |
| 6053 | if (!assemble_emit(&a, &b->b_instr[j])) |
| 6054 | goto error; |
| 6055 | } |
Tim Peters | b6c3cea | 2001-06-26 03:36:28 +0000 | [diff] [blame] | 6056 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6057 | if (_PyBytes_Resize(&a.a_lnotab, a.a_lnotab_off) < 0) |
| 6058 | goto error; |
Serhiy Storchaka | ab87400 | 2016-09-11 13:48:15 +0300 | [diff] [blame] | 6059 | 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] | 6060 | goto error; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6061 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6062 | co = makecode(c, &a); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 6063 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 6064 | assemble_free(&a); |
| 6065 | return co; |
Jeremy Hylton | e36f778 | 2001-01-19 03:21:30 +0000 | [diff] [blame] | 6066 | } |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6067 | |
| 6068 | #undef PyAST_Compile |
Benjamin Peterson | e502451 | 2018-09-12 12:06:42 -0700 | [diff] [blame] | 6069 | PyCodeObject * |
Georg Brandl | 8334fd9 | 2010-12-04 10:26:46 +0000 | [diff] [blame] | 6070 | PyAST_Compile(mod_ty mod, const char *filename, PyCompilerFlags *flags, |
| 6071 | PyArena *arena) |
| 6072 | { |
| 6073 | return PyAST_CompileEx(mod, filename, flags, -1, arena); |
| 6074 | } |