Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 1 | #include "Python.h" |
Victor Stinner | 94faa07 | 2021-03-23 20:47:40 +0100 | [diff] [blame] | 2 | #include "pycore_ast.h" // identifier, stmt_ty |
Victor Stinner | a81fca6 | 2021-03-24 00:51:50 +0100 | [diff] [blame] | 3 | #include "pycore_compile.h" // _Py_Mangle() |
Victor Stinner | 57364ce | 2021-03-24 01:29:09 +0100 | [diff] [blame] | 4 | #include "pycore_parser.h" // _PyParser_ASTFromString() |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 5 | #include "pycore_pystate.h" // _PyThreadState_GET() |
| 6 | #include "pycore_symtable.h" // PySTEntryObject |
Victor Stinner | 4a21e57 | 2020-04-15 02:35:41 +0200 | [diff] [blame] | 7 | #include "structmember.h" // PyMemberDef |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 8 | |
Neal Norwitz | 5d0ad50 | 2005-12-19 04:27:42 +0000 | [diff] [blame] | 9 | /* error strings used for warnings */ |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 10 | #define GLOBAL_PARAM \ |
| 11 | "name '%U' is parameter and global" |
| 12 | |
| 13 | #define NONLOCAL_PARAM \ |
| 14 | "name '%U' is parameter and nonlocal" |
| 15 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 16 | #define GLOBAL_AFTER_ASSIGN \ |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 17 | "name '%U' is assigned to before global declaration" |
Jeremy Hylton | 2990640 | 2001-12-10 00:53:18 +0000 | [diff] [blame] | 18 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 19 | #define NONLOCAL_AFTER_ASSIGN \ |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 20 | "name '%U' is assigned to before nonlocal declaration" |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 21 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 22 | #define GLOBAL_AFTER_USE \ |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 23 | "name '%U' is used prior to global declaration" |
Jeremy Hylton | 2990640 | 2001-12-10 00:53:18 +0000 | [diff] [blame] | 24 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 25 | #define NONLOCAL_AFTER_USE \ |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 26 | "name '%U' is used prior to nonlocal declaration" |
| 27 | |
| 28 | #define GLOBAL_ANNOT \ |
| 29 | "annotated name '%U' can't be global" |
| 30 | |
| 31 | #define NONLOCAL_ANNOT \ |
| 32 | "annotated name '%U' can't be nonlocal" |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 33 | |
Neal Norwitz | 5d0ad50 | 2005-12-19 04:27:42 +0000 | [diff] [blame] | 34 | #define IMPORT_STAR_WARNING "import * only allowed at module level" |
| 35 | |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 36 | #define NAMED_EXPR_COMP_IN_CLASS \ |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 37 | "assignment expression within a comprehension cannot be used in a class body" |
| 38 | |
| 39 | #define NAMED_EXPR_COMP_CONFLICT \ |
| 40 | "assignment expression cannot rebind comprehension iteration variable '%U'" |
| 41 | |
| 42 | #define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \ |
| 43 | "comprehension inner loop cannot rebind assignment expression target '%U'" |
| 44 | |
| 45 | #define NAMED_EXPR_COMP_ITER_EXPR \ |
| 46 | "assignment expression cannot be used in a comprehension iterable expression" |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 47 | |
Neal Norwitz | 090b3dd | 2006-02-28 22:36:46 +0000 | [diff] [blame] | 48 | static PySTEntryObject * |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 49 | ste_new(struct symtable *st, identifier name, _Py_block_ty block, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 50 | void *key, int lineno, int col_offset, |
| 51 | int end_lineno, int end_col_offset) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 52 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 53 | PySTEntryObject *ste = NULL; |
Christian Heimes | 837e53a | 2012-09-10 03:08:46 +0200 | [diff] [blame] | 54 | PyObject *k = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 55 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 56 | k = PyLong_FromVoidPtr(key); |
| 57 | if (k == NULL) |
| 58 | goto fail; |
| 59 | ste = PyObject_New(PySTEntryObject, &PySTEntry_Type); |
Christian Heimes | 55ad651 | 2012-09-12 17:58:10 +0200 | [diff] [blame] | 60 | if (ste == NULL) { |
| 61 | Py_DECREF(k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 62 | goto fail; |
Christian Heimes | 55ad651 | 2012-09-12 17:58:10 +0200 | [diff] [blame] | 63 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 64 | ste->ste_table = st; |
Christian Heimes | 1526582 | 2012-09-12 17:52:46 +0200 | [diff] [blame] | 65 | ste->ste_id = k; /* ste owns reference to k */ |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 66 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 67 | Py_INCREF(name); |
Victor Stinner | 9a4fb66 | 2013-07-11 22:49:00 +0200 | [diff] [blame] | 68 | ste->ste_name = name; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 69 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 70 | ste->ste_symbols = NULL; |
| 71 | ste->ste_varnames = NULL; |
| 72 | ste->ste_children = NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 73 | |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 74 | ste->ste_directives = NULL; |
| 75 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 76 | ste->ste_type = block; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 77 | ste->ste_nested = 0; |
| 78 | ste->ste_free = 0; |
| 79 | ste->ste_varargs = 0; |
| 80 | ste->ste_varkeywords = 0; |
| 81 | ste->ste_opt_lineno = 0; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 82 | ste->ste_opt_col_offset = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | ste->ste_lineno = lineno; |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 84 | ste->ste_col_offset = col_offset; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 85 | ste->ste_end_lineno = end_lineno; |
| 86 | ste->ste_end_col_offset = end_col_offset; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | if (st->st_cur != NULL && |
| 89 | (st->st_cur->ste_nested || |
| 90 | st->st_cur->ste_type == FunctionBlock)) |
| 91 | ste->ste_nested = 1; |
| 92 | ste->ste_child_free = 0; |
| 93 | ste->ste_generator = 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 94 | ste->ste_coroutine = 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 95 | ste->ste_comprehension = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 96 | ste->ste_returns_value = 0; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 97 | ste->ste_needs_class_closure = 0; |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 98 | ste->ste_comp_iter_target = 0; |
| 99 | ste->ste_comp_iter_expr = 0; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 100 | |
Victor Stinner | 9a4fb66 | 2013-07-11 22:49:00 +0200 | [diff] [blame] | 101 | ste->ste_symbols = PyDict_New(); |
| 102 | ste->ste_varnames = PyList_New(0); |
| 103 | ste->ste_children = PyList_New(0); |
| 104 | if (ste->ste_symbols == NULL |
| 105 | || ste->ste_varnames == NULL |
| 106 | || ste->ste_children == NULL) |
| 107 | goto fail; |
| 108 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 109 | if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0) |
| 110 | goto fail; |
| 111 | |
| 112 | return ste; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 113 | fail: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | Py_XDECREF(ste); |
| 115 | return NULL; |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | static PyObject * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 119 | ste_repr(PySTEntryObject *ste) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 120 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 121 | return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>", |
| 122 | ste->ste_name, |
| 123 | PyLong_AS_LONG(ste->ste_id), ste->ste_lineno); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static void |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 127 | ste_dealloc(PySTEntryObject *ste) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 128 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 129 | ste->ste_table = NULL; |
| 130 | Py_XDECREF(ste->ste_id); |
| 131 | Py_XDECREF(ste->ste_name); |
| 132 | Py_XDECREF(ste->ste_symbols); |
| 133 | Py_XDECREF(ste->ste_varnames); |
| 134 | Py_XDECREF(ste->ste_children); |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 135 | Py_XDECREF(ste->ste_directives); |
Victor Stinner | 32bd68c | 2020-12-01 10:37:39 +0100 | [diff] [blame] | 136 | PyObject_Free(ste); |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 137 | } |
| 138 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 139 | #define OFF(x) offsetof(PySTEntryObject, x) |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 140 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 141 | static PyMemberDef ste_memberlist[] = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 142 | {"id", T_OBJECT, OFF(ste_id), READONLY}, |
| 143 | {"name", T_OBJECT, OFF(ste_name), READONLY}, |
| 144 | {"symbols", T_OBJECT, OFF(ste_symbols), READONLY}, |
| 145 | {"varnames", T_OBJECT, OFF(ste_varnames), READONLY}, |
| 146 | {"children", T_OBJECT, OFF(ste_children), READONLY}, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 147 | {"nested", T_INT, OFF(ste_nested), READONLY}, |
| 148 | {"type", T_INT, OFF(ste_type), READONLY}, |
| 149 | {"lineno", T_INT, OFF(ste_lineno), READONLY}, |
| 150 | {NULL} |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 153 | PyTypeObject PySTEntry_Type = { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 154 | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
| 155 | "symtable entry", |
| 156 | sizeof(PySTEntryObject), |
| 157 | 0, |
| 158 | (destructor)ste_dealloc, /* tp_dealloc */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 159 | 0, /* tp_vectorcall_offset */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 160 | 0, /* tp_getattr */ |
| 161 | 0, /* tp_setattr */ |
Jeroen Demeyer | 530f506 | 2019-05-31 04:13:39 +0200 | [diff] [blame] | 162 | 0, /* tp_as_async */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 163 | (reprfunc)ste_repr, /* tp_repr */ |
| 164 | 0, /* tp_as_number */ |
| 165 | 0, /* tp_as_sequence */ |
| 166 | 0, /* tp_as_mapping */ |
| 167 | 0, /* tp_hash */ |
| 168 | 0, /* tp_call */ |
| 169 | 0, /* tp_str */ |
| 170 | PyObject_GenericGetAttr, /* tp_getattro */ |
| 171 | 0, /* tp_setattro */ |
| 172 | 0, /* tp_as_buffer */ |
| 173 | Py_TPFLAGS_DEFAULT, /* tp_flags */ |
| 174 | 0, /* tp_doc */ |
| 175 | 0, /* tp_traverse */ |
| 176 | 0, /* tp_clear */ |
| 177 | 0, /* tp_richcompare */ |
| 178 | 0, /* tp_weaklistoffset */ |
| 179 | 0, /* tp_iter */ |
| 180 | 0, /* tp_iternext */ |
| 181 | 0, /* tp_methods */ |
| 182 | ste_memberlist, /* tp_members */ |
| 183 | 0, /* tp_getset */ |
| 184 | 0, /* tp_base */ |
| 185 | 0, /* tp_dict */ |
| 186 | 0, /* tp_descr_get */ |
| 187 | 0, /* tp_descr_set */ |
| 188 | 0, /* tp_dictoffset */ |
| 189 | 0, /* tp_init */ |
| 190 | 0, /* tp_alloc */ |
| 191 | 0, /* tp_new */ |
Jeremy Hylton | cb17ae8 | 2001-02-09 22:22:18 +0000 | [diff] [blame] | 192 | }; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 193 | |
| 194 | static int symtable_analyze(struct symtable *st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 195 | static int symtable_enter_block(struct symtable *st, identifier name, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 196 | _Py_block_ty block, void *ast, |
| 197 | int lineno, int col_offset, |
| 198 | int end_lineno, int end_col_offset); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 199 | static int symtable_exit_block(struct symtable *st); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 200 | static int symtable_visit_stmt(struct symtable *st, stmt_ty s); |
| 201 | static int symtable_visit_expr(struct symtable *st, expr_ty s); |
| 202 | static int symtable_visit_genexp(struct symtable *st, expr_ty s); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 203 | static int symtable_visit_listcomp(struct symtable *st, expr_ty s); |
| 204 | static int symtable_visit_setcomp(struct symtable *st, expr_ty s); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 205 | static int symtable_visit_dictcomp(struct symtable *st, expr_ty s); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 206 | static int symtable_visit_arguments(struct symtable *st, arguments_ty); |
| 207 | static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty); |
| 208 | static int symtable_visit_alias(struct symtable *st, alias_ty); |
| 209 | static int symtable_visit_comprehension(struct symtable *st, comprehension_ty); |
| 210 | static int symtable_visit_keyword(struct symtable *st, keyword_ty); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 211 | static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args); |
| 212 | static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 213 | static int symtable_implicit_arg(struct symtable *st, int pos); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 214 | static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty); |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 215 | static int symtable_visit_withitem(struct symtable *st, withitem_ty item); |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 216 | static int symtable_visit_match_case(struct symtable *st, match_case_ty m); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 217 | |
| 218 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 219 | static identifier top = NULL, lambda = NULL, genexpr = NULL, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 220 | listcomp = NULL, setcomp = NULL, dictcomp = NULL, |
Benjamin Peterson | e8e1459 | 2013-05-16 14:37:25 -0500 | [diff] [blame] | 221 | __class__ = NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 222 | |
| 223 | #define GET_IDENTIFIER(VAR) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 224 | ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR))) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 225 | |
| 226 | #define DUPLICATE_ARGUMENT \ |
Neal Norwitz | a5d16a3 | 2007-08-24 22:53:58 +0000 | [diff] [blame] | 227 | "duplicate argument '%U' in function definition" |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 228 | |
| 229 | static struct symtable * |
| 230 | symtable_new(void) |
| 231 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 232 | struct symtable *st; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 233 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 234 | st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable)); |
Zackery Spytz | ad65f15 | 2018-11-16 08:58:55 -0700 | [diff] [blame] | 235 | if (st == NULL) { |
| 236 | PyErr_NoMemory(); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 237 | return NULL; |
Zackery Spytz | ad65f15 | 2018-11-16 08:58:55 -0700 | [diff] [blame] | 238 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 239 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 240 | st->st_filename = NULL; |
| 241 | st->st_blocks = NULL; |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 242 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 243 | if ((st->st_stack = PyList_New(0)) == NULL) |
| 244 | goto fail; |
| 245 | if ((st->st_blocks = PyDict_New()) == NULL) |
| 246 | goto fail; |
| 247 | st->st_cur = NULL; |
| 248 | st->st_private = NULL; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 249 | st->in_pattern = 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 250 | return st; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 251 | fail: |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 252 | _PySymtable_Free(st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 253 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 254 | } |
| 255 | |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 256 | /* When compiling the use of C stack is probably going to be a lot |
| 257 | lighter than when executing Python code but still can overflow |
| 258 | and causing a Python crash if not checked (e.g. eval("()"*300000)). |
| 259 | Using the current recursion limit for the compiler seems too |
| 260 | restrictive (it caused at least one test to fail) so a factor is |
| 261 | used to allow deeper recursion when compiling an expression. |
| 262 | |
| 263 | Using a scaling factor means this should automatically adjust when |
| 264 | the recursion limit is adjusted for small or large C stack allocations. |
| 265 | */ |
| 266 | #define COMPILER_STACK_FRAME_SCALE 3 |
| 267 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 268 | struct symtable * |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 269 | _PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 270 | { |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 271 | struct symtable *st = symtable_new(); |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 272 | asdl_stmt_seq *seq; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 273 | int i; |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 274 | PyThreadState *tstate; |
Benjamin Peterson | 305e5aa | 2013-09-26 22:17:45 -0400 | [diff] [blame] | 275 | int recursion_limit = Py_GetRecursionLimit(); |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 276 | int starting_recursion_depth; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 277 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 278 | if (st == NULL) |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 279 | return NULL; |
| 280 | if (filename == NULL) { |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 281 | _PySymtable_Free(st); |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 282 | return NULL; |
| 283 | } |
| 284 | Py_INCREF(filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 285 | st->st_filename = filename; |
| 286 | st->st_future = future; |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 287 | |
| 288 | /* Setup recursion depth check counters */ |
Victor Stinner | 50b4857 | 2018-11-01 01:51:40 +0100 | [diff] [blame] | 289 | tstate = _PyThreadState_GET(); |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 290 | if (!tstate) { |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 291 | _PySymtable_Free(st); |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 292 | return NULL; |
| 293 | } |
Benjamin Peterson | 305e5aa | 2013-09-26 22:17:45 -0400 | [diff] [blame] | 294 | /* Be careful here to prevent overflow. */ |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 295 | starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ? |
Benjamin Peterson | 305e5aa | 2013-09-26 22:17:45 -0400 | [diff] [blame] | 296 | tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth; |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 297 | st->recursion_depth = starting_recursion_depth; |
Benjamin Peterson | 305e5aa | 2013-09-26 22:17:45 -0400 | [diff] [blame] | 298 | st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ? |
| 299 | recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit; |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 300 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 301 | /* Make the initial symbol information gathering pass */ |
| 302 | if (!GET_IDENTIFIER(top) || |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 303 | !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0, 0, 0)) { |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 304 | _PySymtable_Free(st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 305 | return NULL; |
| 306 | } |
Thomas Wouters | 0e3f591 | 2006-08-11 14:57:12 +0000 | [diff] [blame] | 307 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 308 | st->st_top = st->st_cur; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 309 | switch (mod->kind) { |
| 310 | case Module_kind: |
| 311 | seq = mod->v.Module.body; |
| 312 | for (i = 0; i < asdl_seq_LEN(seq); i++) |
| 313 | if (!symtable_visit_stmt(st, |
| 314 | (stmt_ty)asdl_seq_GET(seq, i))) |
| 315 | goto error; |
| 316 | break; |
| 317 | case Expression_kind: |
| 318 | if (!symtable_visit_expr(st, mod->v.Expression.body)) |
| 319 | goto error; |
| 320 | break; |
| 321 | case Interactive_kind: |
| 322 | seq = mod->v.Interactive.body; |
| 323 | for (i = 0; i < asdl_seq_LEN(seq); i++) |
| 324 | if (!symtable_visit_stmt(st, |
| 325 | (stmt_ty)asdl_seq_GET(seq, i))) |
| 326 | goto error; |
| 327 | break; |
Guido van Rossum | 522346d | 2019-02-11 11:34:50 -0800 | [diff] [blame] | 328 | case FunctionType_kind: |
| 329 | PyErr_SetString(PyExc_RuntimeError, |
| 330 | "this compiler does not handle FunctionTypes"); |
| 331 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 332 | } |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 333 | if (!symtable_exit_block(st)) { |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 334 | _PySymtable_Free(st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 337 | /* Check that the recursion depth counting balanced correctly */ |
| 338 | if (st->recursion_depth != starting_recursion_depth) { |
| 339 | PyErr_Format(PyExc_SystemError, |
| 340 | "symtable analysis recursion depth mismatch (before=%d, after=%d)", |
| 341 | starting_recursion_depth, st->recursion_depth); |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 342 | _PySymtable_Free(st); |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 345 | /* Make the second symbol analysis pass */ |
| 346 | if (symtable_analyze(st)) |
| 347 | return st; |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 348 | _PySymtable_Free(st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 349 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 350 | error: |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 351 | (void) symtable_exit_block(st); |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 352 | _PySymtable_Free(st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 353 | return NULL; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 354 | } |
| 355 | |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 356 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 357 | void |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 358 | _PySymtable_Free(struct symtable *st) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 359 | { |
Victor Stinner | 14e461d | 2013-08-26 22:28:21 +0200 | [diff] [blame] | 360 | Py_XDECREF(st->st_filename); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 361 | Py_XDECREF(st->st_blocks); |
| 362 | Py_XDECREF(st->st_stack); |
| 363 | PyMem_Free((void *)st); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | PySTEntryObject * |
| 367 | PySymtable_Lookup(struct symtable *st, void *key) |
| 368 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 369 | PyObject *k, *v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 370 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 371 | k = PyLong_FromVoidPtr(key); |
| 372 | if (k == NULL) |
| 373 | return NULL; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 374 | v = PyDict_GetItemWithError(st->st_blocks, k); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 375 | if (v) { |
| 376 | assert(PySTEntry_Check(v)); |
| 377 | Py_INCREF(v); |
| 378 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 379 | else if (!PyErr_Occurred()) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 380 | PyErr_SetString(PyExc_KeyError, |
| 381 | "unknown symbol table entry"); |
| 382 | } |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 383 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 384 | Py_DECREF(k); |
| 385 | return (PySTEntryObject *)v; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 388 | static long |
| 389 | _PyST_GetSymbol(PySTEntryObject *ste, PyObject *name) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 390 | { |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 391 | PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 392 | if (!v) |
| 393 | return 0; |
| 394 | assert(PyLong_Check(v)); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 395 | return PyLong_AS_LONG(v); |
| 396 | } |
| 397 | |
| 398 | int |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 399 | _PyST_GetScope(PySTEntryObject *ste, PyObject *name) |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 400 | { |
| 401 | long symbol = _PyST_GetSymbol(ste, name); |
| 402 | return (symbol >> SCOPE_OFFSET) & SCOPE_MASK; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 405 | static int |
| 406 | error_at_directive(PySTEntryObject *ste, PyObject *name) |
| 407 | { |
| 408 | Py_ssize_t i; |
| 409 | PyObject *data; |
| 410 | assert(ste->ste_directives); |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 411 | for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) { |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 412 | data = PyList_GET_ITEM(ste->ste_directives, i); |
| 413 | assert(PyTuple_CheckExact(data)); |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 414 | assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0))); |
| 415 | if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) { |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 416 | PyErr_RangedSyntaxLocationObject(ste->ste_table->st_filename, |
| 417 | PyLong_AsLong(PyTuple_GET_ITEM(data, 1)), |
| 418 | PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1, |
| 419 | PyLong_AsLong(PyTuple_GET_ITEM(data, 3)), |
| 420 | PyLong_AsLong(PyTuple_GET_ITEM(data, 4)) + 1); |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 421 | |
| 422 | return 0; |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 423 | } |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 424 | } |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 425 | PyErr_SetString(PyExc_RuntimeError, |
| 426 | "BUG: internal directive bookkeeping broken"); |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 427 | return 0; |
| 428 | } |
| 429 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 430 | |
| 431 | /* Analyze raw symbol information to determine scope of each name. |
| 432 | |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 433 | The next several functions are helpers for symtable_analyze(), |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 434 | which determines whether a name is local, global, or free. In addition, |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 435 | it determines which local variables are cell variables; they provide |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 436 | bindings that are used for free variables in enclosed blocks. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 437 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 438 | There are also two kinds of global variables, implicit and explicit. An |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 439 | explicit global is declared with the global statement. An implicit |
| 440 | global is a free variable for which the compiler has found no binding |
| 441 | in an enclosing function scope. The implicit global is either a global |
| 442 | or a builtin. Python's module and class blocks use the xxx_NAME opcodes |
| 443 | to handle these names to implement slightly odd semantics. In such a |
| 444 | block, the name is treated as global until it is assigned to; then it |
| 445 | is treated as a local. |
| 446 | |
| 447 | The symbol table requires two passes to determine the scope of each name. |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 448 | The first pass collects raw facts from the AST via the symtable_visit_* |
| 449 | functions: the name is a parameter here, the name is used but not defined |
| 450 | here, etc. The second pass analyzes these facts during a pass over the |
| 451 | PySTEntryObjects created during pass 1. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 452 | |
| 453 | When a function is entered during the second pass, the parent passes |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 454 | the set of all name bindings visible to its children. These bindings |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 455 | are used to determine if non-local variables are free or implicit globals. |
Nick Coghlan | 4138bfe | 2007-04-23 10:14:27 +0000 | [diff] [blame] | 456 | Names which are explicitly declared nonlocal must exist in this set of |
| 457 | visible names - if they do not, a syntax error is raised. After doing |
| 458 | the local analysis, it analyzes each of its child blocks using an |
| 459 | updated set of name bindings. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 460 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 461 | The children update the free variable set. If a local variable is added to |
| 462 | the free variable set by the child, the variable is marked as a cell. The |
| 463 | function object being defined must provide runtime storage for the variable |
| 464 | that may outlive the function's frame. Cell variables are removed from the |
| 465 | free set before the analyze function returns to its parent. |
Nick Coghlan | 4138bfe | 2007-04-23 10:14:27 +0000 | [diff] [blame] | 466 | |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 467 | During analysis, the names are: |
| 468 | symbols: dict mapping from symbol names to flag values (including offset scope values) |
| 469 | scopes: dict mapping from symbol names to scope values (no offset) |
| 470 | local: set of all symbol names local to the current scope |
| 471 | bound: set of all symbol names local to a containing function scope |
| 472 | free: set of all symbol names referenced but not bound in child scopes |
| 473 | global: set of all symbol names explicitly declared as global |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 474 | */ |
| 475 | |
| 476 | #define SET_SCOPE(DICT, NAME, I) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 477 | PyObject *o = PyLong_FromLong(I); \ |
| 478 | if (!o) \ |
| 479 | return 0; \ |
| 480 | if (PyDict_SetItem((DICT), (NAME), o) < 0) { \ |
| 481 | Py_DECREF(o); \ |
| 482 | return 0; \ |
| 483 | } \ |
| 484 | Py_DECREF(o); \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 485 | } |
| 486 | |
| 487 | /* Decide on scope of name, given flags. |
| 488 | |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 489 | The namespace dictionaries may be modified to record information |
| 490 | about the new name. For example, a new global will add an entry to |
| 491 | global. A name that was global can be changed to local. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 492 | */ |
| 493 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 495 | analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 496 | PyObject *bound, PyObject *local, PyObject *free, |
| 497 | PyObject *global) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 498 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 499 | if (flags & DEF_GLOBAL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 500 | if (flags & DEF_NONLOCAL) { |
| 501 | PyErr_Format(PyExc_SyntaxError, |
| 502 | "name '%U' is nonlocal and global", |
| 503 | name); |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 504 | return error_at_directive(ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 505 | } |
| 506 | SET_SCOPE(scopes, name, GLOBAL_EXPLICIT); |
| 507 | if (PySet_Add(global, name) < 0) |
| 508 | return 0; |
| 509 | if (bound && (PySet_Discard(bound, name) < 0)) |
| 510 | return 0; |
| 511 | return 1; |
| 512 | } |
| 513 | if (flags & DEF_NONLOCAL) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 514 | if (!bound) { |
| 515 | PyErr_Format(PyExc_SyntaxError, |
| 516 | "nonlocal declaration not allowed at module level"); |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 517 | return error_at_directive(ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 518 | } |
| 519 | if (!PySet_Contains(bound, name)) { |
| 520 | PyErr_Format(PyExc_SyntaxError, |
| 521 | "no binding for nonlocal '%U' found", |
| 522 | name); |
| 523 | |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 524 | return error_at_directive(ste, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 525 | } |
| 526 | SET_SCOPE(scopes, name, FREE); |
| 527 | ste->ste_free = 1; |
| 528 | return PySet_Add(free, name) >= 0; |
| 529 | } |
| 530 | if (flags & DEF_BOUND) { |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 531 | SET_SCOPE(scopes, name, LOCAL); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 532 | if (PySet_Add(local, name) < 0) |
| 533 | return 0; |
| 534 | if (PySet_Discard(global, name) < 0) |
| 535 | return 0; |
| 536 | return 1; |
| 537 | } |
| 538 | /* If an enclosing block has a binding for this name, it |
| 539 | is a free variable rather than a global variable. |
| 540 | Note that having a non-NULL bound implies that the block |
| 541 | is nested. |
| 542 | */ |
| 543 | if (bound && PySet_Contains(bound, name)) { |
| 544 | SET_SCOPE(scopes, name, FREE); |
| 545 | ste->ste_free = 1; |
| 546 | return PySet_Add(free, name) >= 0; |
| 547 | } |
| 548 | /* If a parent has a global statement, then call it global |
| 549 | explicit? It could also be global implicit. |
| 550 | */ |
| 551 | if (global && PySet_Contains(global, name)) { |
| 552 | SET_SCOPE(scopes, name, GLOBAL_IMPLICIT); |
| 553 | return 1; |
| 554 | } |
| 555 | if (ste->ste_nested) |
| 556 | ste->ste_free = 1; |
| 557 | SET_SCOPE(scopes, name, GLOBAL_IMPLICIT); |
| 558 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | #undef SET_SCOPE |
| 562 | |
| 563 | /* If a name is defined in free and also in locals, then this block |
| 564 | provides the binding for the free variable. The name should be |
| 565 | marked CELL in this block and removed from the free list. |
| 566 | |
| 567 | Note that the current block's free variables are included in free. |
| 568 | That's safe because no name can be free and local in the same scope. |
| 569 | */ |
| 570 | |
| 571 | static int |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 572 | analyze_cells(PyObject *scopes, PyObject *free) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 573 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 574 | PyObject *name, *v, *v_cell; |
| 575 | int success = 0; |
| 576 | Py_ssize_t pos = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 577 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 578 | v_cell = PyLong_FromLong(CELL); |
| 579 | if (!v_cell) |
| 580 | return 0; |
| 581 | while (PyDict_Next(scopes, &pos, &name, &v)) { |
| 582 | long scope; |
| 583 | assert(PyLong_Check(v)); |
| 584 | scope = PyLong_AS_LONG(v); |
Benjamin Peterson | 20f9c3c | 2010-07-20 22:39:34 +0000 | [diff] [blame] | 585 | if (scope != LOCAL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 586 | continue; |
| 587 | if (!PySet_Contains(free, name)) |
| 588 | continue; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 589 | /* Replace LOCAL with CELL for this name, and remove |
| 590 | from free. It is safe to replace the value of name |
| 591 | in the dict, because it will not cause a resize. |
| 592 | */ |
| 593 | if (PyDict_SetItem(scopes, name, v_cell) < 0) |
| 594 | goto error; |
| 595 | if (PySet_Discard(free, name) < 0) |
| 596 | goto error; |
| 597 | } |
| 598 | success = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 599 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 600 | Py_DECREF(v_cell); |
| 601 | return success; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 604 | static int |
| 605 | drop_class_free(PySTEntryObject *ste, PyObject *free) |
| 606 | { |
| 607 | int res; |
| 608 | if (!GET_IDENTIFIER(__class__)) |
| 609 | return 0; |
| 610 | res = PySet_Discard(free, __class__); |
| 611 | if (res < 0) |
| 612 | return 0; |
| 613 | if (res) |
| 614 | ste->ste_needs_class_closure = 1; |
| 615 | return 1; |
| 616 | } |
| 617 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 618 | /* Enter the final scope information into the ste_symbols dict. |
| 619 | * |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 620 | * All arguments are dicts. Modifies symbols, others are read-only. |
| 621 | */ |
| 622 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 623 | update_symbols(PyObject *symbols, PyObject *scopes, |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 624 | PyObject *bound, PyObject *free, int classflag) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 625 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 626 | PyObject *name = NULL, *itr = NULL; |
| 627 | PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL; |
| 628 | Py_ssize_t pos = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 629 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 630 | /* Update scope information for all symbols in this scope */ |
| 631 | while (PyDict_Next(symbols, &pos, &name, &v)) { |
| 632 | long scope, flags; |
| 633 | assert(PyLong_Check(v)); |
| 634 | flags = PyLong_AS_LONG(v); |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 635 | v_scope = PyDict_GetItemWithError(scopes, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 636 | assert(v_scope && PyLong_Check(v_scope)); |
| 637 | scope = PyLong_AS_LONG(v_scope); |
| 638 | flags |= (scope << SCOPE_OFFSET); |
| 639 | v_new = PyLong_FromLong(flags); |
| 640 | if (!v_new) |
| 641 | return 0; |
| 642 | if (PyDict_SetItem(symbols, name, v_new) < 0) { |
| 643 | Py_DECREF(v_new); |
| 644 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 645 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 646 | Py_DECREF(v_new); |
| 647 | } |
| 648 | |
| 649 | /* Record not yet resolved free variables from children (if any) */ |
| 650 | v_free = PyLong_FromLong(FREE << SCOPE_OFFSET); |
| 651 | if (!v_free) |
| 652 | return 0; |
| 653 | |
| 654 | itr = PyObject_GetIter(free); |
Zackery Spytz | fc439d2 | 2018-10-10 23:05:35 -0600 | [diff] [blame] | 655 | if (itr == NULL) { |
| 656 | Py_DECREF(v_free); |
| 657 | return 0; |
| 658 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 659 | |
| 660 | while ((name = PyIter_Next(itr))) { |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 661 | v = PyDict_GetItemWithError(symbols, name); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 662 | |
| 663 | /* Handle symbol that already exists in this scope */ |
| 664 | if (v) { |
| 665 | /* Handle a free variable in a method of |
| 666 | the class that has the same name as a local |
| 667 | or global in the class scope. |
| 668 | */ |
| 669 | if (classflag && |
| 670 | PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) { |
| 671 | long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS; |
| 672 | v_new = PyLong_FromLong(flags); |
| 673 | if (!v_new) { |
| 674 | goto error; |
| 675 | } |
| 676 | if (PyDict_SetItem(symbols, name, v_new) < 0) { |
| 677 | Py_DECREF(v_new); |
| 678 | goto error; |
| 679 | } |
| 680 | Py_DECREF(v_new); |
| 681 | } |
| 682 | /* It's a cell, or already free in this scope */ |
| 683 | Py_DECREF(name); |
| 684 | continue; |
| 685 | } |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 686 | else if (PyErr_Occurred()) { |
| 687 | goto error; |
| 688 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 689 | /* Handle global symbol */ |
Christian Heimes | 45af0c8 | 2016-09-09 00:22:28 +0200 | [diff] [blame] | 690 | if (bound && !PySet_Contains(bound, name)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 691 | Py_DECREF(name); |
| 692 | continue; /* it's a global */ |
| 693 | } |
| 694 | /* Propagate new free symbol up the lexical stack */ |
| 695 | if (PyDict_SetItem(symbols, name, v_free) < 0) { |
| 696 | goto error; |
| 697 | } |
| 698 | Py_DECREF(name); |
| 699 | } |
| 700 | Py_DECREF(itr); |
| 701 | Py_DECREF(v_free); |
| 702 | return 1; |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 703 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 704 | Py_XDECREF(v_free); |
| 705 | Py_XDECREF(itr); |
| 706 | Py_XDECREF(name); |
| 707 | return 0; |
| 708 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 709 | |
| 710 | /* Make final symbol table decisions for block of ste. |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 711 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 712 | Arguments: |
| 713 | ste -- current symtable entry (input/output) |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 714 | bound -- set of variables bound in enclosing scopes (input). bound |
| 715 | is NULL for module blocks. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 716 | free -- set of free variables in enclosed scopes (output) |
| 717 | globals -- set of declared global variables in enclosing scopes (input) |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 718 | |
| 719 | The implementation uses two mutually recursive functions, |
| 720 | analyze_block() and analyze_child_block(). analyze_block() is |
| 721 | responsible for analyzing the individual names defined in a block. |
| 722 | analyze_child_block() prepares temporary namespace dictionaries |
| 723 | used to evaluated nested blocks. |
| 724 | |
| 725 | The two functions exist because a child block should see the name |
| 726 | bindings of its enclosing blocks, but those bindings should not |
| 727 | propagate back to a parent block. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 728 | */ |
| 729 | |
| 730 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 731 | analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, |
| 732 | PyObject *global, PyObject* child_free); |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 733 | |
| 734 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 735 | analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free, |
| 736 | PyObject *global) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 737 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 738 | PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL; |
| 739 | PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL; |
| 740 | PyObject *temp; |
| 741 | int i, success = 0; |
| 742 | Py_ssize_t pos = 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 743 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 744 | local = PySet_New(NULL); /* collect new names bound in block */ |
| 745 | if (!local) |
| 746 | goto error; |
| 747 | scopes = PyDict_New(); /* collect scopes defined for each name */ |
| 748 | if (!scopes) |
| 749 | goto error; |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 751 | /* Allocate new global and bound variable dictionaries. These |
| 752 | dictionaries hold the names visible in nested blocks. For |
| 753 | ClassBlocks, the bound and global names are initialized |
| 754 | before analyzing names, because class bindings aren't |
| 755 | visible in methods. For other blocks, they are initialized |
| 756 | after names are analyzed. |
| 757 | */ |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 758 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 759 | /* TODO(jhylton): Package these dicts in a struct so that we |
| 760 | can write reasonable helper functions? |
| 761 | */ |
| 762 | newglobal = PySet_New(NULL); |
| 763 | if (!newglobal) |
| 764 | goto error; |
| 765 | newfree = PySet_New(NULL); |
| 766 | if (!newfree) |
| 767 | goto error; |
| 768 | newbound = PySet_New(NULL); |
| 769 | if (!newbound) |
| 770 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 771 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 772 | /* Class namespace has no effect on names visible in |
| 773 | nested functions, so populate the global and bound |
| 774 | sets to be passed to child blocks before analyzing |
| 775 | this one. |
| 776 | */ |
| 777 | if (ste->ste_type == ClassBlock) { |
| 778 | /* Pass down known globals */ |
| 779 | temp = PyNumber_InPlaceOr(newglobal, global); |
| 780 | if (!temp) |
| 781 | goto error; |
| 782 | Py_DECREF(temp); |
| 783 | /* Pass down previously bound symbols */ |
| 784 | if (bound) { |
| 785 | temp = PyNumber_InPlaceOr(newbound, bound); |
| 786 | if (!temp) |
| 787 | goto error; |
| 788 | Py_DECREF(temp); |
| 789 | } |
| 790 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 791 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 792 | while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) { |
| 793 | long flags = PyLong_AS_LONG(v); |
| 794 | if (!analyze_name(ste, scopes, name, flags, |
| 795 | bound, local, free, global)) |
| 796 | goto error; |
| 797 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 799 | /* Populate global and bound sets to be passed to children. */ |
| 800 | if (ste->ste_type != ClassBlock) { |
| 801 | /* Add function locals to bound set */ |
| 802 | if (ste->ste_type == FunctionBlock) { |
| 803 | temp = PyNumber_InPlaceOr(newbound, local); |
| 804 | if (!temp) |
| 805 | goto error; |
| 806 | Py_DECREF(temp); |
| 807 | } |
| 808 | /* Pass down previously bound symbols */ |
| 809 | if (bound) { |
| 810 | temp = PyNumber_InPlaceOr(newbound, bound); |
| 811 | if (!temp) |
| 812 | goto error; |
| 813 | Py_DECREF(temp); |
| 814 | } |
| 815 | /* Pass down known globals */ |
| 816 | temp = PyNumber_InPlaceOr(newglobal, global); |
| 817 | if (!temp) |
| 818 | goto error; |
| 819 | Py_DECREF(temp); |
| 820 | } |
| 821 | else { |
| 822 | /* Special-case __class__ */ |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 823 | if (!GET_IDENTIFIER(__class__)) |
| 824 | goto error; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 825 | if (PySet_Add(newbound, __class__) < 0) |
| 826 | goto error; |
| 827 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 828 | |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 829 | /* Recursively call analyze_child_block() on each child block. |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 830 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 831 | newbound, newglobal now contain the names visible in |
| 832 | nested blocks. The free variables in the children will |
| 833 | be collected in allfree. |
| 834 | */ |
| 835 | allfree = PySet_New(NULL); |
| 836 | if (!allfree) |
| 837 | goto error; |
| 838 | for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) { |
| 839 | PyObject *c = PyList_GET_ITEM(ste->ste_children, i); |
| 840 | PySTEntryObject* entry; |
| 841 | assert(c && PySTEntry_Check(c)); |
| 842 | entry = (PySTEntryObject*)c; |
| 843 | if (!analyze_child_block(entry, newbound, newfree, newglobal, |
| 844 | allfree)) |
| 845 | goto error; |
| 846 | /* Check if any children have free variables */ |
| 847 | if (entry->ste_free || entry->ste_child_free) |
| 848 | ste->ste_child_free = 1; |
| 849 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 850 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 851 | temp = PyNumber_InPlaceOr(newfree, allfree); |
| 852 | if (!temp) |
| 853 | goto error; |
| 854 | Py_DECREF(temp); |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 855 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 856 | /* Check if any local variables must be converted to cell variables */ |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 857 | if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 858 | goto error; |
Benjamin Peterson | 312595c | 2013-05-15 15:26:42 -0500 | [diff] [blame] | 859 | else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 860 | goto error; |
| 861 | /* Records the results of the analysis in the symbol table entry */ |
| 862 | if (!update_symbols(ste->ste_symbols, scopes, bound, newfree, |
| 863 | ste->ste_type == ClassBlock)) |
| 864 | goto error; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 865 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 866 | temp = PyNumber_InPlaceOr(free, newfree); |
| 867 | if (!temp) |
| 868 | goto error; |
| 869 | Py_DECREF(temp); |
| 870 | success = 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 871 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 872 | Py_XDECREF(scopes); |
| 873 | Py_XDECREF(local); |
| 874 | Py_XDECREF(newbound); |
| 875 | Py_XDECREF(newglobal); |
| 876 | Py_XDECREF(newfree); |
| 877 | Py_XDECREF(allfree); |
| 878 | if (!success) |
| 879 | assert(PyErr_Occurred()); |
| 880 | return success; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 884 | analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free, |
| 885 | PyObject *global, PyObject* child_free) |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 886 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 887 | PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL; |
| 888 | PyObject *temp; |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 889 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 890 | /* Copy the bound and global dictionaries. |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 891 | |
Martin Panter | 3ee6270 | 2016-06-04 04:57:19 +0000 | [diff] [blame] | 892 | These dictionaries are used by all blocks enclosed by the |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 893 | current block. The analyze_block() call modifies these |
| 894 | dictionaries. |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 895 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 896 | */ |
| 897 | temp_bound = PySet_New(bound); |
| 898 | if (!temp_bound) |
| 899 | goto error; |
| 900 | temp_free = PySet_New(free); |
| 901 | if (!temp_free) |
| 902 | goto error; |
| 903 | temp_global = PySet_New(global); |
| 904 | if (!temp_global) |
| 905 | goto error; |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 906 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 907 | if (!analyze_block(entry, temp_bound, temp_free, temp_global)) |
| 908 | goto error; |
| 909 | temp = PyNumber_InPlaceOr(child_free, temp_free); |
| 910 | if (!temp) |
| 911 | goto error; |
| 912 | Py_DECREF(temp); |
| 913 | Py_DECREF(temp_bound); |
| 914 | Py_DECREF(temp_free); |
| 915 | Py_DECREF(temp_global); |
| 916 | return 1; |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 917 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 918 | Py_XDECREF(temp_bound); |
| 919 | Py_XDECREF(temp_free); |
| 920 | Py_XDECREF(temp_global); |
| 921 | return 0; |
Jeremy Hylton | f37708e | 2009-03-31 15:26:37 +0000 | [diff] [blame] | 922 | } |
| 923 | |
| 924 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 925 | symtable_analyze(struct symtable *st) |
| 926 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 927 | PyObject *free, *global; |
| 928 | int r; |
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 | free = PySet_New(NULL); |
| 931 | if (!free) |
| 932 | return 0; |
| 933 | global = PySet_New(NULL); |
| 934 | if (!global) { |
| 935 | Py_DECREF(free); |
| 936 | return 0; |
| 937 | } |
| 938 | r = analyze_block(st->st_top, NULL, free, global); |
| 939 | Py_DECREF(free); |
| 940 | Py_DECREF(global); |
| 941 | return r; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Benjamin Peterson | 55e00f2 | 2008-08-17 18:02:44 +0000 | [diff] [blame] | 944 | /* symtable_enter_block() gets a reference via ste_new. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 945 | This reference is released when the block is exited, via the DECREF |
| 946 | in symtable_exit_block(). |
| 947 | */ |
| 948 | |
| 949 | static int |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 950 | symtable_exit_block(struct symtable *st) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 951 | { |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 952 | Py_ssize_t size; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 953 | |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 954 | st->st_cur = NULL; |
| 955 | size = PyList_GET_SIZE(st->st_stack); |
| 956 | if (size) { |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 957 | if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 958 | return 0; |
Benjamin Peterson | 9d872e1 | 2011-07-02 09:22:13 -0500 | [diff] [blame] | 959 | if (--size) |
| 960 | st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 961 | } |
| 962 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 963 | } |
| 964 | |
| 965 | static int |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 966 | symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 967 | void *ast, int lineno, int col_offset, |
| 968 | int end_lineno, int end_col_offset) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 969 | { |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 970 | PySTEntryObject *prev = NULL, *ste; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 971 | |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 972 | ste = ste_new(st, name, block, ast, lineno, col_offset, end_lineno, end_col_offset); |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 973 | if (ste == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 974 | return 0; |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 975 | if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) { |
| 976 | Py_DECREF(ste); |
| 977 | return 0; |
| 978 | } |
| 979 | prev = st->st_cur; |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 980 | /* bpo-37757: For now, disallow *all* assignment expressions in the |
| 981 | * outermost iterator expression of a comprehension, even those inside |
| 982 | * a nested comprehension or a lambda expression. |
| 983 | */ |
| 984 | if (prev) { |
| 985 | ste->ste_comp_iter_expr = prev->ste_comp_iter_expr; |
| 986 | } |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 987 | /* The entry is owned by the stack. Borrow it for st_cur. */ |
| 988 | Py_DECREF(ste); |
| 989 | st->st_cur = ste; |
Benjamin Peterson | 230b206 | 2010-10-16 03:45:45 +0000 | [diff] [blame] | 990 | if (block == ModuleBlock) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 991 | st->st_global = st->st_cur->ste_symbols; |
| 992 | if (prev) { |
Benjamin Peterson | 609da58 | 2011-06-29 22:52:39 -0500 | [diff] [blame] | 993 | if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 994 | return 0; |
| 995 | } |
| 996 | } |
| 997 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 998 | } |
| 999 | |
Neal Norwitz | 46b7bda | 2006-01-08 01:06:06 +0000 | [diff] [blame] | 1000 | static long |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1001 | symtable_lookup(struct symtable *st, PyObject *name) |
| 1002 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1003 | PyObject *mangled = _Py_Mangle(st->st_private, name); |
| 1004 | if (!mangled) |
| 1005 | return 0; |
Pablo Galindo | 4901dc4 | 2019-08-26 16:14:07 +0100 | [diff] [blame] | 1006 | long ret = _PyST_GetSymbol(st->st_cur, mangled); |
| 1007 | Py_DECREF(mangled); |
| 1008 | return ret; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1009 | } |
| 1010 | |
| 1011 | static int |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1012 | symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1013 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1014 | PyObject *o; |
| 1015 | PyObject *dict; |
| 1016 | long val; |
| 1017 | PyObject *mangled = _Py_Mangle(st->st_private, name); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1018 | |
Jeremy Hylton | 81e9502 | 2007-02-27 06:50:52 +0000 | [diff] [blame] | 1019 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1020 | if (!mangled) |
| 1021 | return 0; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1022 | dict = ste->ste_symbols; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1023 | if ((o = PyDict_GetItemWithError(dict, mangled))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1024 | val = PyLong_AS_LONG(o); |
| 1025 | if ((flag & DEF_PARAM) && (val & DEF_PARAM)) { |
| 1026 | /* Is it better to use 'mangled' or 'name' here? */ |
| 1027 | PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1028 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1029 | ste->ste_lineno, |
| 1030 | ste->ste_col_offset + 1, |
| 1031 | ste->ste_end_lineno, |
| 1032 | ste->ste_end_col_offset + 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1033 | goto error; |
| 1034 | } |
| 1035 | val |= flag; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1036 | } |
| 1037 | else if (PyErr_Occurred()) { |
| 1038 | goto error; |
| 1039 | } |
| 1040 | else { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1041 | val = flag; |
Serhiy Storchaka | a24107b | 2019-02-25 17:59:46 +0200 | [diff] [blame] | 1042 | } |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1043 | if (ste->ste_comp_iter_target) { |
| 1044 | /* This name is an iteration variable in a comprehension, |
| 1045 | * so check for a binding conflict with any named expressions. |
| 1046 | * Otherwise, mark it as an iteration variable so subsequent |
| 1047 | * named expressions can check for conflicts. |
| 1048 | */ |
| 1049 | if (val & (DEF_GLOBAL | DEF_NONLOCAL)) { |
| 1050 | PyErr_Format(PyExc_SyntaxError, |
| 1051 | NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1052 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1053 | ste->ste_lineno, |
| 1054 | ste->ste_col_offset + 1, |
| 1055 | ste->ste_end_lineno, |
| 1056 | ste->ste_end_col_offset + 1); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1057 | goto error; |
| 1058 | } |
| 1059 | val |= DEF_COMP_ITER; |
| 1060 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1061 | o = PyLong_FromLong(val); |
| 1062 | if (o == NULL) |
| 1063 | goto error; |
| 1064 | if (PyDict_SetItem(dict, mangled, o) < 0) { |
| 1065 | Py_DECREF(o); |
| 1066 | goto error; |
| 1067 | } |
| 1068 | Py_DECREF(o); |
| 1069 | |
| 1070 | if (flag & DEF_PARAM) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1071 | if (PyList_Append(ste->ste_varnames, mangled) < 0) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1072 | goto error; |
| 1073 | } else if (flag & DEF_GLOBAL) { |
| 1074 | /* XXX need to update DEF_GLOBAL for other flags too; |
| 1075 | perhaps only DEF_FREE_GLOBAL */ |
| 1076 | val = flag; |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1077 | if ((o = PyDict_GetItemWithError(st->st_global, mangled))) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1078 | val |= PyLong_AS_LONG(o); |
| 1079 | } |
Serhiy Storchaka | fb5db7e | 2020-10-26 08:43:39 +0200 | [diff] [blame] | 1080 | else if (PyErr_Occurred()) { |
| 1081 | goto error; |
| 1082 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1083 | o = PyLong_FromLong(val); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1084 | if (o == NULL) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1085 | goto error; |
| 1086 | if (PyDict_SetItem(st->st_global, mangled, o) < 0) { |
| 1087 | Py_DECREF(o); |
| 1088 | goto error; |
| 1089 | } |
| 1090 | Py_DECREF(o); |
| 1091 | } |
| 1092 | Py_DECREF(mangled); |
| 1093 | return 1; |
Neil Schemenauer | 8b528b2 | 2005-10-23 18:37:42 +0000 | [diff] [blame] | 1094 | |
| 1095 | error: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1096 | Py_DECREF(mangled); |
| 1097 | return 0; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1100 | static int |
| 1101 | symtable_add_def(struct symtable *st, PyObject *name, int flag) { |
| 1102 | return symtable_add_def_helper(st, name, flag, st->st_cur); |
| 1103 | } |
| 1104 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1105 | /* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument. |
| 1106 | They use the ASDL name to synthesize the name of the C type and the visit |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1107 | function. |
| 1108 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1109 | VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is |
| 1110 | useful if the first node in the sequence requires special treatment. |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1111 | |
| 1112 | VISIT_QUIT macro returns the specified value exiting from the function but |
| 1113 | first adjusts current recursion counter depth. |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1114 | */ |
| 1115 | |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1116 | #define VISIT_QUIT(ST, X) \ |
| 1117 | return --(ST)->recursion_depth,(X) |
| 1118 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1119 | #define VISIT(ST, TYPE, V) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1120 | if (!symtable_visit_ ## TYPE((ST), (V))) \ |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1121 | VISIT_QUIT((ST), 0); |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1122 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1123 | #define VISIT_SEQ(ST, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1124 | int i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1125 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1126 | for (i = 0; i < asdl_seq_LEN(seq); i++) { \ |
| 1127 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
| 1128 | if (!symtable_visit_ ## TYPE((ST), elt)) \ |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1129 | VISIT_QUIT((ST), 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1130 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1131 | } |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1132 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1133 | #define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1134 | int i; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1135 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1136 | for (i = (START); i < asdl_seq_LEN(seq); i++) { \ |
| 1137 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
| 1138 | if (!symtable_visit_ ## TYPE((ST), elt)) \ |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1139 | VISIT_QUIT((ST), 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1140 | } \ |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1141 | } |
Neal Norwitz | b6fc9df | 2005-11-13 18:50:34 +0000 | [diff] [blame] | 1142 | |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1143 | #define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1144 | int i = 0; \ |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1145 | asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1146 | for (i = 0; i < asdl_seq_LEN(seq); i++) { \ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1147 | TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1148 | if (!elt) continue; /* can be NULL */ \ |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1149 | if (!symtable_visit_ ## TYPE((ST), elt)) \ |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1150 | VISIT_QUIT((ST), 0); \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1151 | } \ |
Guido van Rossum | 4f72a78 | 2006-10-27 23:31:49 +0000 | [diff] [blame] | 1152 | } |
| 1153 | |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1154 | static int |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1155 | symtable_record_directive(struct symtable *st, identifier name, int lineno, |
| 1156 | int col_offset, int end_lineno, int end_col_offset) |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 1157 | { |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 1158 | PyObject *data, *mangled; |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 1159 | int res; |
| 1160 | if (!st->st_cur->ste_directives) { |
| 1161 | st->st_cur->ste_directives = PyList_New(0); |
| 1162 | if (!st->st_cur->ste_directives) |
| 1163 | return 0; |
| 1164 | } |
Benjamin Peterson | 3cc8f4b | 2015-12-29 10:08:34 -0600 | [diff] [blame] | 1165 | mangled = _Py_Mangle(st->st_private, name); |
| 1166 | if (!mangled) |
| 1167 | return 0; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1168 | data = Py_BuildValue("(Niiii)", mangled, lineno, col_offset, end_lineno, end_col_offset); |
Benjamin Peterson | d9c8702 | 2012-10-31 20:26:20 -0400 | [diff] [blame] | 1169 | if (!data) |
| 1170 | return 0; |
| 1171 | res = PyList_Append(st->st_cur->ste_directives, data); |
| 1172 | Py_DECREF(data); |
| 1173 | return res == 0; |
| 1174 | } |
| 1175 | |
| 1176 | |
| 1177 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1178 | symtable_visit_stmt(struct symtable *st, stmt_ty s) |
| 1179 | { |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1180 | if (++st->recursion_depth > st->recursion_limit) { |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 1181 | PyErr_SetString(PyExc_RecursionError, |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1182 | "maximum recursion depth exceeded during compilation"); |
| 1183 | VISIT_QUIT(st, 0); |
| 1184 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1185 | switch (s->kind) { |
| 1186 | case FunctionDef_kind: |
| 1187 | if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1188 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1189 | if (s->v.FunctionDef.args->defaults) |
| 1190 | VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults); |
| 1191 | if (s->v.FunctionDef.args->kw_defaults) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1192 | VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1193 | if (!symtable_visit_annotations(st, s->v.FunctionDef.args, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1194 | s->v.FunctionDef.returns)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1195 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1196 | if (s->v.FunctionDef.decorator_list) |
| 1197 | VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list); |
| 1198 | if (!symtable_enter_block(st, s->v.FunctionDef.name, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1199 | FunctionBlock, (void *)s, |
| 1200 | s->lineno, s->col_offset, |
| 1201 | s->end_lineno, s->end_col_offset)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1202 | VISIT_QUIT(st, 0); |
Benjamin Peterson | c2575d5 | 2011-06-29 15:27:14 -0500 | [diff] [blame] | 1203 | VISIT(st, arguments, s->v.FunctionDef.args); |
| 1204 | VISIT_SEQ(st, stmt, s->v.FunctionDef.body); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1205 | if (!symtable_exit_block(st)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1206 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1207 | break; |
| 1208 | case ClassDef_kind: { |
| 1209 | PyObject *tmp; |
| 1210 | if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1211 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1212 | VISIT_SEQ(st, expr, s->v.ClassDef.bases); |
| 1213 | VISIT_SEQ(st, keyword, s->v.ClassDef.keywords); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1214 | if (s->v.ClassDef.decorator_list) |
| 1215 | VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list); |
| 1216 | if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1217 | (void *)s, s->lineno, s->col_offset, |
| 1218 | s->end_lineno, s->end_col_offset)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1219 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1220 | tmp = st->st_private; |
| 1221 | st->st_private = s->v.ClassDef.name; |
Benjamin Peterson | c2575d5 | 2011-06-29 15:27:14 -0500 | [diff] [blame] | 1222 | VISIT_SEQ(st, stmt, s->v.ClassDef.body); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1223 | st->st_private = tmp; |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1224 | if (!symtable_exit_block(st)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1225 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1226 | break; |
| 1227 | } |
| 1228 | case Return_kind: |
| 1229 | if (s->v.Return.value) { |
| 1230 | VISIT(st, expr, s->v.Return.value); |
| 1231 | st->st_cur->ste_returns_value = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1232 | } |
| 1233 | break; |
| 1234 | case Delete_kind: |
| 1235 | VISIT_SEQ(st, expr, s->v.Delete.targets); |
| 1236 | break; |
| 1237 | case Assign_kind: |
| 1238 | VISIT_SEQ(st, expr, s->v.Assign.targets); |
| 1239 | VISIT(st, expr, s->v.Assign.value); |
| 1240 | break; |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1241 | case AnnAssign_kind: |
| 1242 | if (s->v.AnnAssign.target->kind == Name_kind) { |
| 1243 | expr_ty e_name = s->v.AnnAssign.target; |
| 1244 | long cur = symtable_lookup(st, e_name->v.Name.id); |
| 1245 | if (cur < 0) { |
| 1246 | VISIT_QUIT(st, 0); |
| 1247 | } |
| 1248 | if ((cur & (DEF_GLOBAL | DEF_NONLOCAL)) |
Pablo Galindo | de2aea0 | 2018-10-14 18:01:03 +0100 | [diff] [blame] | 1249 | && (st->st_cur->ste_symbols != st->st_global) |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1250 | && s->v.AnnAssign.simple) { |
| 1251 | PyErr_Format(PyExc_SyntaxError, |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 1252 | cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT, |
| 1253 | e_name->v.Name.id); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1254 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1255 | s->lineno, |
| 1256 | s->col_offset + 1, |
| 1257 | s->end_lineno, |
| 1258 | s->end_col_offset + 1); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1259 | VISIT_QUIT(st, 0); |
| 1260 | } |
| 1261 | if (s->v.AnnAssign.simple && |
| 1262 | !symtable_add_def(st, e_name->v.Name.id, |
| 1263 | DEF_ANNOT | DEF_LOCAL)) { |
| 1264 | VISIT_QUIT(st, 0); |
| 1265 | } |
| 1266 | else { |
| 1267 | if (s->v.AnnAssign.value |
| 1268 | && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) { |
| 1269 | VISIT_QUIT(st, 0); |
| 1270 | } |
| 1271 | } |
| 1272 | } |
| 1273 | else { |
| 1274 | VISIT(st, expr, s->v.AnnAssign.target); |
| 1275 | } |
| 1276 | VISIT(st, expr, s->v.AnnAssign.annotation); |
| 1277 | if (s->v.AnnAssign.value) { |
| 1278 | VISIT(st, expr, s->v.AnnAssign.value); |
| 1279 | } |
| 1280 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1281 | case AugAssign_kind: |
| 1282 | VISIT(st, expr, s->v.AugAssign.target); |
| 1283 | VISIT(st, expr, s->v.AugAssign.value); |
| 1284 | break; |
| 1285 | case For_kind: |
| 1286 | VISIT(st, expr, s->v.For.target); |
| 1287 | VISIT(st, expr, s->v.For.iter); |
| 1288 | VISIT_SEQ(st, stmt, s->v.For.body); |
| 1289 | if (s->v.For.orelse) |
| 1290 | VISIT_SEQ(st, stmt, s->v.For.orelse); |
| 1291 | break; |
| 1292 | case While_kind: |
| 1293 | VISIT(st, expr, s->v.While.test); |
| 1294 | VISIT_SEQ(st, stmt, s->v.While.body); |
| 1295 | if (s->v.While.orelse) |
| 1296 | VISIT_SEQ(st, stmt, s->v.While.orelse); |
| 1297 | break; |
| 1298 | case If_kind: |
| 1299 | /* XXX if 0: and lookup_yield() hacks */ |
| 1300 | VISIT(st, expr, s->v.If.test); |
| 1301 | VISIT_SEQ(st, stmt, s->v.If.body); |
| 1302 | if (s->v.If.orelse) |
| 1303 | VISIT_SEQ(st, stmt, s->v.If.orelse); |
| 1304 | break; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1305 | case Match_kind: |
| 1306 | VISIT(st, expr, s->v.Match.subject); |
| 1307 | VISIT_SEQ(st, match_case, s->v.Match.cases); |
| 1308 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1309 | case Raise_kind: |
| 1310 | if (s->v.Raise.exc) { |
| 1311 | VISIT(st, expr, s->v.Raise.exc); |
Eli Bendersky | dd97fbb | 2011-04-10 07:37:26 +0300 | [diff] [blame] | 1312 | if (s->v.Raise.cause) { |
| 1313 | VISIT(st, expr, s->v.Raise.cause); |
| 1314 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1315 | } |
| 1316 | break; |
Benjamin Peterson | 43af12b | 2011-05-29 11:43:10 -0500 | [diff] [blame] | 1317 | case Try_kind: |
| 1318 | VISIT_SEQ(st, stmt, s->v.Try.body); |
| 1319 | VISIT_SEQ(st, stmt, s->v.Try.orelse); |
| 1320 | VISIT_SEQ(st, excepthandler, s->v.Try.handlers); |
| 1321 | VISIT_SEQ(st, stmt, s->v.Try.finalbody); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1322 | break; |
| 1323 | case Assert_kind: |
| 1324 | VISIT(st, expr, s->v.Assert.test); |
| 1325 | if (s->v.Assert.msg) |
| 1326 | VISIT(st, expr, s->v.Assert.msg); |
| 1327 | break; |
| 1328 | case Import_kind: |
| 1329 | VISIT_SEQ(st, alias, s->v.Import.names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1330 | break; |
| 1331 | case ImportFrom_kind: |
| 1332 | VISIT_SEQ(st, alias, s->v.ImportFrom.names); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1333 | break; |
| 1334 | case Global_kind: { |
| 1335 | int i; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1336 | asdl_identifier_seq *seq = s->v.Global.names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1337 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 1338 | identifier name = (identifier)asdl_seq_GET(seq, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1339 | long cur = symtable_lookup(st, name); |
| 1340 | if (cur < 0) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1341 | VISIT_QUIT(st, 0); |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 1342 | if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) { |
| 1343 | const char* msg; |
| 1344 | if (cur & DEF_PARAM) { |
| 1345 | msg = GLOBAL_PARAM; |
| 1346 | } else if (cur & USE) { |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 1347 | msg = GLOBAL_AFTER_USE; |
Christian Heimes | 517507c | 2016-09-23 20:26:30 +0200 | [diff] [blame] | 1348 | } else if (cur & DEF_ANNOT) { |
| 1349 | msg = GLOBAL_ANNOT; |
| 1350 | } else { /* DEF_LOCAL */ |
| 1351 | msg = GLOBAL_AFTER_ASSIGN; |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 1352 | } |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1353 | PyErr_Format(PyExc_SyntaxError, |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 1354 | msg, name); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1355 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1356 | s->lineno, |
| 1357 | s->col_offset + 1, |
| 1358 | s->end_lineno, |
| 1359 | s->end_col_offset + 1); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1360 | VISIT_QUIT(st, 0); |
| 1361 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1362 | if (!symtable_add_def(st, name, DEF_GLOBAL)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1363 | VISIT_QUIT(st, 0); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1364 | if (!symtable_record_directive(st, name, s->lineno, s->col_offset, |
| 1365 | s->end_lineno, s->end_col_offset)) |
Nick Coghlan | e69bfc3 | 2012-11-04 23:53:15 +1000 | [diff] [blame] | 1366 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1367 | } |
| 1368 | break; |
| 1369 | } |
| 1370 | case Nonlocal_kind: { |
| 1371 | int i; |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1372 | asdl_identifier_seq *seq = s->v.Nonlocal.names; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1373 | for (i = 0; i < asdl_seq_LEN(seq); i++) { |
| 1374 | identifier name = (identifier)asdl_seq_GET(seq, i); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1375 | long cur = symtable_lookup(st, name); |
| 1376 | if (cur < 0) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1377 | VISIT_QUIT(st, 0); |
Ivan Levkivskyi | 8c83c23 | 2017-10-26 23:28:35 +0200 | [diff] [blame] | 1378 | if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) { |
| 1379 | const char* msg; |
| 1380 | if (cur & DEF_PARAM) { |
| 1381 | msg = NONLOCAL_PARAM; |
| 1382 | } else if (cur & USE) { |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 1383 | msg = NONLOCAL_AFTER_USE; |
Christian Heimes | 517507c | 2016-09-23 20:26:30 +0200 | [diff] [blame] | 1384 | } else if (cur & DEF_ANNOT) { |
| 1385 | msg = NONLOCAL_ANNOT; |
| 1386 | } else { /* DEF_LOCAL */ |
| 1387 | msg = NONLOCAL_AFTER_ASSIGN; |
Guido van Rossum | 6cff874 | 2016-09-09 09:36:26 -0700 | [diff] [blame] | 1388 | } |
| 1389 | PyErr_Format(PyExc_SyntaxError, msg, name); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1390 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1391 | s->lineno, |
| 1392 | s->col_offset + 1, |
| 1393 | s->end_lineno, |
| 1394 | s->end_col_offset + 1); |
Yury Selivanov | f8cb8a1 | 2016-09-08 20:50:03 -0700 | [diff] [blame] | 1395 | VISIT_QUIT(st, 0); |
| 1396 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1397 | if (!symtable_add_def(st, name, DEF_NONLOCAL)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1398 | VISIT_QUIT(st, 0); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1399 | if (!symtable_record_directive(st, name, s->lineno, s->col_offset, |
| 1400 | s->end_lineno, s->end_col_offset)) |
Nick Coghlan | e69bfc3 | 2012-11-04 23:53:15 +1000 | [diff] [blame] | 1401 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1402 | } |
| 1403 | break; |
| 1404 | } |
| 1405 | case Expr_kind: |
| 1406 | VISIT(st, expr, s->v.Expr.value); |
| 1407 | break; |
| 1408 | case Pass_kind: |
| 1409 | case Break_kind: |
| 1410 | case Continue_kind: |
| 1411 | /* nothing to do here */ |
| 1412 | break; |
| 1413 | case With_kind: |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 1414 | VISIT_SEQ(st, withitem, s->v.With.items); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1415 | VISIT_SEQ(st, stmt, s->v.With.body); |
| 1416 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1417 | case AsyncFunctionDef_kind: |
| 1418 | if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL)) |
| 1419 | VISIT_QUIT(st, 0); |
| 1420 | if (s->v.AsyncFunctionDef.args->defaults) |
| 1421 | VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults); |
| 1422 | if (s->v.AsyncFunctionDef.args->kw_defaults) |
| 1423 | VISIT_SEQ_WITH_NULL(st, expr, |
| 1424 | s->v.AsyncFunctionDef.args->kw_defaults); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1425 | if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args, |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1426 | s->v.AsyncFunctionDef.returns)) |
| 1427 | VISIT_QUIT(st, 0); |
| 1428 | if (s->v.AsyncFunctionDef.decorator_list) |
| 1429 | VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list); |
| 1430 | if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1431 | FunctionBlock, (void *)s, |
| 1432 | s->lineno, s->col_offset, |
| 1433 | s->end_lineno, s->end_col_offset)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1434 | VISIT_QUIT(st, 0); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1435 | st->st_cur->ste_coroutine = 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1436 | VISIT(st, arguments, s->v.AsyncFunctionDef.args); |
| 1437 | VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1438 | if (!symtable_exit_block(st)) |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1439 | VISIT_QUIT(st, 0); |
| 1440 | break; |
| 1441 | case AsyncWith_kind: |
| 1442 | VISIT_SEQ(st, withitem, s->v.AsyncWith.items); |
| 1443 | VISIT_SEQ(st, stmt, s->v.AsyncWith.body); |
| 1444 | break; |
| 1445 | case AsyncFor_kind: |
| 1446 | VISIT(st, expr, s->v.AsyncFor.target); |
| 1447 | VISIT(st, expr, s->v.AsyncFor.iter); |
| 1448 | VISIT_SEQ(st, stmt, s->v.AsyncFor.body); |
| 1449 | if (s->v.AsyncFor.orelse) |
| 1450 | VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse); |
| 1451 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1452 | } |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1453 | VISIT_QUIT(st, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1454 | } |
| 1455 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1456 | static int |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1457 | symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e) |
| 1458 | { |
| 1459 | assert(st->st_stack); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1460 | assert(e->kind == Name_kind); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1461 | |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1462 | PyObject *target_name = e->v.Name.id; |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1463 | Py_ssize_t i, size; |
| 1464 | struct _symtable_entry *ste; |
| 1465 | size = PyList_GET_SIZE(st->st_stack); |
| 1466 | assert(size); |
| 1467 | |
| 1468 | /* Iterate over the stack in reverse and add to the nearest adequate scope */ |
| 1469 | for (i = size - 1; i >= 0; i--) { |
| 1470 | ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i); |
| 1471 | |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1472 | /* If we find a comprehension scope, check for a target |
| 1473 | * binding conflict with iteration variables, otherwise skip it |
| 1474 | */ |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1475 | if (ste->ste_comprehension) { |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1476 | long target_in_scope = _PyST_GetSymbol(ste, target_name); |
| 1477 | if (target_in_scope & DEF_COMP_ITER) { |
| 1478 | PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1479 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1480 | e->lineno, |
| 1481 | e->col_offset + 1, |
| 1482 | e->end_lineno, |
| 1483 | e->end_col_offset + 1); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1484 | VISIT_QUIT(st, 0); |
| 1485 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1486 | continue; |
| 1487 | } |
| 1488 | |
Pablo Galindo | fd5c414 | 2019-10-14 05:18:05 +0100 | [diff] [blame] | 1489 | /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */ |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1490 | if (ste->ste_type == FunctionBlock) { |
Pablo Galindo | fd5c414 | 2019-10-14 05:18:05 +0100 | [diff] [blame] | 1491 | long target_in_scope = _PyST_GetSymbol(ste, target_name); |
| 1492 | if (target_in_scope & DEF_GLOBAL) { |
| 1493 | if (!symtable_add_def(st, target_name, DEF_GLOBAL)) |
| 1494 | VISIT_QUIT(st, 0); |
| 1495 | } else { |
| 1496 | if (!symtable_add_def(st, target_name, DEF_NONLOCAL)) |
| 1497 | VISIT_QUIT(st, 0); |
| 1498 | } |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1499 | if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset, |
| 1500 | e->end_lineno, e->end_col_offset)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1501 | VISIT_QUIT(st, 0); |
| 1502 | |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1503 | return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1504 | } |
| 1505 | /* If we find a ModuleBlock entry, add as GLOBAL */ |
| 1506 | if (ste->ste_type == ModuleBlock) { |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1507 | if (!symtable_add_def(st, target_name, DEF_GLOBAL)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1508 | VISIT_QUIT(st, 0); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1509 | if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset, |
| 1510 | e->end_lineno, e->end_col_offset)) |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1511 | VISIT_QUIT(st, 0); |
| 1512 | |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1513 | return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1514 | } |
| 1515 | /* Disallow usage in ClassBlock */ |
| 1516 | if (ste->ste_type == ClassBlock) { |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1517 | PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1518 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1519 | e->lineno, |
| 1520 | e->col_offset + 1, |
| 1521 | e->end_lineno, |
| 1522 | e->end_col_offset + 1); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1523 | VISIT_QUIT(st, 0); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock |
| 1528 | and should never fall to this case |
| 1529 | */ |
| 1530 | assert(0); |
| 1531 | return 0; |
| 1532 | } |
| 1533 | |
| 1534 | static int |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1535 | symtable_handle_namedexpr(struct symtable *st, expr_ty e) |
| 1536 | { |
| 1537 | if (st->st_cur->ste_comp_iter_expr > 0) { |
| 1538 | /* Assignment isn't allowed in a comprehension iterable expression */ |
| 1539 | PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1540 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1541 | e->lineno, |
| 1542 | e->col_offset + 1, |
| 1543 | e->end_lineno, |
| 1544 | e->end_col_offset + 1); |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 1545 | return 0; |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1546 | } |
| 1547 | if (st->st_cur->ste_comprehension) { |
| 1548 | /* Inside a comprehension body, so find the right target scope */ |
| 1549 | if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target)) |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 1550 | return 0; |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1551 | } |
| 1552 | VISIT(st, expr, e->v.NamedExpr.value); |
| 1553 | VISIT(st, expr, e->v.NamedExpr.target); |
Nick Coghlan | 0614523 | 2019-08-29 23:26:53 +1000 | [diff] [blame] | 1554 | return 1; |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1555 | } |
| 1556 | |
| 1557 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1558 | symtable_visit_expr(struct symtable *st, expr_ty e) |
| 1559 | { |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1560 | if (++st->recursion_depth > st->recursion_limit) { |
Yury Selivanov | f488fb4 | 2015-07-03 01:04:23 -0400 | [diff] [blame] | 1561 | PyErr_SetString(PyExc_RecursionError, |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1562 | "maximum recursion depth exceeded during compilation"); |
| 1563 | VISIT_QUIT(st, 0); |
| 1564 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1565 | switch (e->kind) { |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1566 | case NamedExpr_kind: |
Pablo Galindo | 0e4ea16 | 2019-08-26 15:52:25 +0100 | [diff] [blame] | 1567 | if(!symtable_handle_namedexpr(st, e)) |
| 1568 | VISIT_QUIT(st, 0); |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1569 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1570 | case BoolOp_kind: |
| 1571 | VISIT_SEQ(st, expr, e->v.BoolOp.values); |
| 1572 | break; |
| 1573 | case BinOp_kind: |
| 1574 | VISIT(st, expr, e->v.BinOp.left); |
| 1575 | VISIT(st, expr, e->v.BinOp.right); |
| 1576 | break; |
| 1577 | case UnaryOp_kind: |
| 1578 | VISIT(st, expr, e->v.UnaryOp.operand); |
| 1579 | break; |
| 1580 | case Lambda_kind: { |
| 1581 | if (!GET_IDENTIFIER(lambda)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1582 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1583 | if (e->v.Lambda.args->defaults) |
| 1584 | VISIT_SEQ(st, expr, e->v.Lambda.args->defaults); |
Amaury Forgeot d'Arc | 97c1bef | 2011-11-04 22:17:45 +0100 | [diff] [blame] | 1585 | if (e->v.Lambda.args->kw_defaults) |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1586 | VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1587 | if (!symtable_enter_block(st, lambda, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1588 | FunctionBlock, (void *)e, |
| 1589 | e->lineno, e->col_offset, |
| 1590 | e->end_lineno, e->end_col_offset)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1591 | VISIT_QUIT(st, 0); |
Benjamin Peterson | c2575d5 | 2011-06-29 15:27:14 -0500 | [diff] [blame] | 1592 | VISIT(st, arguments, e->v.Lambda.args); |
| 1593 | VISIT(st, expr, e->v.Lambda.body); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1594 | if (!symtable_exit_block(st)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1595 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1596 | break; |
| 1597 | } |
| 1598 | case IfExp_kind: |
| 1599 | VISIT(st, expr, e->v.IfExp.test); |
| 1600 | VISIT(st, expr, e->v.IfExp.body); |
| 1601 | VISIT(st, expr, e->v.IfExp.orelse); |
| 1602 | break; |
| 1603 | case Dict_kind: |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1604 | VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1605 | VISIT_SEQ(st, expr, e->v.Dict.values); |
| 1606 | break; |
| 1607 | case Set_kind: |
| 1608 | VISIT_SEQ(st, expr, e->v.Set.elts); |
| 1609 | break; |
| 1610 | case GeneratorExp_kind: |
| 1611 | if (!symtable_visit_genexp(st, e)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1612 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1613 | break; |
| 1614 | case ListComp_kind: |
| 1615 | if (!symtable_visit_listcomp(st, e)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1616 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1617 | break; |
| 1618 | case SetComp_kind: |
| 1619 | if (!symtable_visit_setcomp(st, e)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1620 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1621 | break; |
| 1622 | case DictComp_kind: |
| 1623 | if (!symtable_visit_dictcomp(st, e)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1624 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1625 | break; |
| 1626 | case Yield_kind: |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 1627 | if (e->v.Yield.value) |
| 1628 | VISIT(st, expr, e->v.Yield.value); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1629 | st->st_cur->ste_generator = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1630 | break; |
Mark Dickinson | ded35ae | 2012-11-25 14:36:26 +0000 | [diff] [blame] | 1631 | case YieldFrom_kind: |
| 1632 | VISIT(st, expr, e->v.YieldFrom.value); |
| 1633 | st->st_cur->ste_generator = 1; |
| 1634 | break; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1635 | case Await_kind: |
| 1636 | VISIT(st, expr, e->v.Await.value); |
Yury Selivanov | eb63645 | 2016-09-08 22:01:51 -0700 | [diff] [blame] | 1637 | st->st_cur->ste_coroutine = 1; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1638 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1639 | case Compare_kind: |
| 1640 | VISIT(st, expr, e->v.Compare.left); |
| 1641 | VISIT_SEQ(st, expr, e->v.Compare.comparators); |
| 1642 | break; |
| 1643 | case Call_kind: |
| 1644 | VISIT(st, expr, e->v.Call.func); |
| 1645 | VISIT_SEQ(st, expr, e->v.Call.args); |
Benjamin Peterson | 025e9eb | 2015-05-05 20:16:41 -0400 | [diff] [blame] | 1646 | VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1647 | break; |
Eric V. Smith | 235a6f0 | 2015-09-19 14:51:32 -0400 | [diff] [blame] | 1648 | case FormattedValue_kind: |
| 1649 | VISIT(st, expr, e->v.FormattedValue.value); |
| 1650 | if (e->v.FormattedValue.format_spec) |
| 1651 | VISIT(st, expr, e->v.FormattedValue.format_spec); |
| 1652 | break; |
| 1653 | case JoinedStr_kind: |
| 1654 | VISIT_SEQ(st, expr, e->v.JoinedStr.values); |
| 1655 | break; |
Victor Stinner | f2c1aa1 | 2016-01-26 00:40:57 +0100 | [diff] [blame] | 1656 | case Constant_kind: |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1657 | /* Nothing to do here. */ |
| 1658 | break; |
| 1659 | /* The following exprs can be assignment targets. */ |
| 1660 | case Attribute_kind: |
| 1661 | VISIT(st, expr, e->v.Attribute.value); |
| 1662 | break; |
| 1663 | case Subscript_kind: |
| 1664 | VISIT(st, expr, e->v.Subscript.value); |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 1665 | VISIT(st, expr, e->v.Subscript.slice); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1666 | break; |
| 1667 | case Starred_kind: |
| 1668 | VISIT(st, expr, e->v.Starred.value); |
| 1669 | break; |
Serhiy Storchaka | 13d52c2 | 2020-03-10 18:52:34 +0200 | [diff] [blame] | 1670 | case Slice_kind: |
| 1671 | if (e->v.Slice.lower) |
| 1672 | VISIT(st, expr, e->v.Slice.lower) |
| 1673 | if (e->v.Slice.upper) |
| 1674 | VISIT(st, expr, e->v.Slice.upper) |
| 1675 | if (e->v.Slice.step) |
| 1676 | VISIT(st, expr, e->v.Slice.step) |
| 1677 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1678 | case Name_kind: |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1679 | // Don't make "_" a local when used in a pattern: |
| 1680 | if (st->in_pattern && |
| 1681 | e->v.Name.ctx == Store && |
| 1682 | _PyUnicode_EqualToASCIIString(e->v.Name.id, "_")) |
| 1683 | { |
| 1684 | break; |
| 1685 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1686 | if (!symtable_add_def(st, e->v.Name.id, |
| 1687 | e->v.Name.ctx == Load ? USE : DEF_LOCAL)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1688 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1689 | /* Special-case super: it counts as a use of __class__ */ |
| 1690 | if (e->v.Name.ctx == Load && |
| 1691 | st->st_cur->ste_type == FunctionBlock && |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1692 | _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) { |
Nick Coghlan | 0b43bcf | 2012-05-27 18:17:07 +1000 | [diff] [blame] | 1693 | if (!GET_IDENTIFIER(__class__) || |
| 1694 | !symtable_add_def(st, __class__, USE)) |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1695 | VISIT_QUIT(st, 0); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1696 | } |
| 1697 | break; |
| 1698 | /* child nodes of List and Tuple will have expr_context set */ |
| 1699 | case List_kind: |
| 1700 | VISIT_SEQ(st, expr, e->v.List.elts); |
| 1701 | break; |
| 1702 | case Tuple_kind: |
| 1703 | VISIT_SEQ(st, expr, e->v.Tuple.elts); |
| 1704 | break; |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1705 | case MatchAs_kind: |
| 1706 | VISIT(st, expr, e->v.MatchAs.pattern); |
| 1707 | symtable_add_def(st, e->v.MatchAs.name, DEF_LOCAL); |
| 1708 | break; |
| 1709 | case MatchOr_kind: |
| 1710 | VISIT_SEQ(st, expr, e->v.MatchOr.patterns); |
| 1711 | break; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1712 | } |
Nick Coghlan | aab9c2b | 2012-11-04 23:14:34 +1000 | [diff] [blame] | 1713 | VISIT_QUIT(st, 1); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | static int |
| 1717 | symtable_implicit_arg(struct symtable *st, int pos) |
| 1718 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1719 | PyObject *id = PyUnicode_FromFormat(".%d", pos); |
| 1720 | if (id == NULL) |
| 1721 | return 0; |
| 1722 | if (!symtable_add_def(st, id, DEF_PARAM)) { |
| 1723 | Py_DECREF(id); |
| 1724 | return 0; |
| 1725 | } |
| 1726 | Py_DECREF(id); |
| 1727 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1728 | } |
| 1729 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1730 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1731 | symtable_visit_params(struct symtable *st, asdl_arg_seq *args) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1732 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1733 | int i; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1734 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1735 | if (!args) |
| 1736 | return -1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1737 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1738 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1739 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
| 1740 | if (!symtable_add_def(st, arg->arg, DEF_PARAM)) |
| 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1745 | } |
| 1746 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1747 | static int |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1748 | symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1749 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1750 | int i; |
Guido van Rossum | 1bc535d | 2007-05-15 18:46:22 +0000 | [diff] [blame] | 1751 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1752 | if (!args) |
| 1753 | return -1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1754 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1755 | for (i = 0; i < asdl_seq_LEN(args); i++) { |
| 1756 | arg_ty arg = (arg_ty)asdl_seq_GET(args, i); |
| 1757 | if (arg->annotation) |
| 1758 | VISIT(st, expr, arg->annotation); |
| 1759 | } |
| 1760 | |
| 1761 | return 1; |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1762 | } |
| 1763 | |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1764 | static int |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1765 | symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns) |
Neal Norwitz | c150536 | 2006-12-28 06:47:50 +0000 | [diff] [blame] | 1766 | { |
Anthony Sottile | ec007cb | 2020-01-04 20:57:21 -0500 | [diff] [blame] | 1767 | if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs)) |
| 1768 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1769 | if (a->args && !symtable_visit_argannotations(st, a->args)) |
| 1770 | return 0; |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1771 | if (a->vararg && a->vararg->annotation) |
| 1772 | VISIT(st, expr, a->vararg->annotation); |
| 1773 | if (a->kwarg && a->kwarg->annotation) |
| 1774 | VISIT(st, expr, a->kwarg->annotation); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1775 | if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs)) |
| 1776 | return 0; |
Yury Selivanov | 7544508 | 2015-05-11 22:57:16 -0400 | [diff] [blame] | 1777 | if (returns) |
Yury Selivanov | b7666a3 | 2015-07-22 14:48:57 +0300 | [diff] [blame] | 1778 | VISIT(st, expr, returns); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1779 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1780 | } |
| 1781 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1782 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1783 | symtable_visit_arguments(struct symtable *st, arguments_ty a) |
| 1784 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1785 | /* skip default arguments inside function block |
| 1786 | XXX should ast be different? |
| 1787 | */ |
Pablo Galindo | 8c77b8c | 2019-04-29 13:36:57 +0100 | [diff] [blame] | 1788 | if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs)) |
| 1789 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1790 | if (a->args && !symtable_visit_params(st, a->args)) |
| 1791 | return 0; |
| 1792 | if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs)) |
| 1793 | return 0; |
| 1794 | if (a->vararg) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1795 | if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1796 | return 0; |
| 1797 | st->st_cur->ste_varargs = 1; |
| 1798 | } |
| 1799 | if (a->kwarg) { |
Benjamin Peterson | cda75be | 2013-03-18 10:48:58 -0700 | [diff] [blame] | 1800 | if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1801 | return 0; |
| 1802 | st->st_cur->ste_varkeywords = 1; |
| 1803 | } |
| 1804 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1808 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1809 | symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh) |
| 1810 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1811 | if (eh->v.ExceptHandler.type) |
| 1812 | VISIT(st, expr, eh->v.ExceptHandler.type); |
| 1813 | if (eh->v.ExceptHandler.name) |
| 1814 | if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL)) |
| 1815 | return 0; |
| 1816 | VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body); |
| 1817 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1818 | } |
| 1819 | |
Benjamin Peterson | bf1bbc1 | 2011-05-27 13:58:08 -0500 | [diff] [blame] | 1820 | static int |
| 1821 | symtable_visit_withitem(struct symtable *st, withitem_ty item) |
| 1822 | { |
| 1823 | VISIT(st, expr, item->context_expr); |
| 1824 | if (item->optional_vars) { |
| 1825 | VISIT(st, expr, item->optional_vars); |
| 1826 | } |
| 1827 | return 1; |
| 1828 | } |
| 1829 | |
Brandt Bucher | 145bf26 | 2021-02-26 14:51:55 -0800 | [diff] [blame] | 1830 | static int |
| 1831 | symtable_visit_match_case(struct symtable *st, match_case_ty m) |
| 1832 | { |
| 1833 | assert(!st->in_pattern); |
| 1834 | st->in_pattern = 1; |
| 1835 | VISIT(st, expr, m->pattern); |
| 1836 | assert(st->in_pattern); |
| 1837 | st->in_pattern = 0; |
| 1838 | if (m->guard) { |
| 1839 | VISIT(st, expr, m->guard); |
| 1840 | } |
| 1841 | VISIT_SEQ(st, stmt, m->body); |
| 1842 | return 1; |
| 1843 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1844 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1845 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1846 | symtable_visit_alias(struct symtable *st, alias_ty a) |
| 1847 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1848 | /* Compute store_name, the name actually bound by the import |
Benjamin Peterson | c629d51 | 2010-06-11 21:53:07 +0000 | [diff] [blame] | 1849 | operation. It is different than a->name when a->name is a |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1850 | dotted package name (e.g. spam.eggs) |
| 1851 | */ |
| 1852 | PyObject *store_name; |
| 1853 | PyObject *name = (a->asname == NULL) ? a->name : a->asname; |
Martin v. Löwis | d63a3b8 | 2011-09-28 07:41:54 +0200 | [diff] [blame] | 1854 | Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0, |
| 1855 | PyUnicode_GET_LENGTH(name), 1); |
| 1856 | if (dot != -1) { |
| 1857 | store_name = PyUnicode_Substring(name, 0, dot); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1858 | if (!store_name) |
| 1859 | return 0; |
| 1860 | } |
| 1861 | else { |
| 1862 | store_name = name; |
| 1863 | Py_INCREF(store_name); |
| 1864 | } |
Serhiy Storchaka | f4934ea | 2016-11-16 10:17:58 +0200 | [diff] [blame] | 1865 | if (!_PyUnicode_EqualToASCIIString(name, "*")) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1866 | int r = symtable_add_def(st, store_name, DEF_IMPORT); |
| 1867 | Py_DECREF(store_name); |
| 1868 | return r; |
| 1869 | } |
| 1870 | else { |
| 1871 | if (st->st_cur->ste_type != ModuleBlock) { |
Benjamin Peterson | b7149ca | 2011-06-20 22:09:13 -0500 | [diff] [blame] | 1872 | int lineno = st->st_cur->ste_lineno; |
| 1873 | int col_offset = st->st_cur->ste_col_offset; |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1874 | int end_lineno = st->st_cur->ste_end_lineno; |
| 1875 | int end_col_offset = st->st_cur->ste_end_col_offset; |
Benjamin Peterson | b7149ca | 2011-06-20 22:09:13 -0500 | [diff] [blame] | 1876 | PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1877 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1878 | lineno, col_offset + 1, |
| 1879 | end_lineno, end_col_offset + 1); |
Benjamin Peterson | b7149ca | 2011-06-20 22:09:13 -0500 | [diff] [blame] | 1880 | Py_DECREF(store_name); |
| 1881 | return 0; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1882 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1883 | Py_DECREF(store_name); |
| 1884 | return 1; |
| 1885 | } |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
| 1888 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1889 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1890 | symtable_visit_comprehension(struct symtable *st, comprehension_ty lc) |
| 1891 | { |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1892 | st->st_cur->ste_comp_iter_target = 1; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1893 | VISIT(st, expr, lc->target); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1894 | st->st_cur->ste_comp_iter_target = 0; |
| 1895 | st->st_cur->ste_comp_iter_expr++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1896 | VISIT(st, expr, lc->iter); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1897 | st->st_cur->ste_comp_iter_expr--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1898 | VISIT_SEQ(st, expr, lc->ifs); |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1899 | if (lc->is_async) { |
| 1900 | st->st_cur->ste_coroutine = 1; |
| 1901 | } |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1902 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1903 | } |
| 1904 | |
| 1905 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1906 | static int |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1907 | symtable_visit_keyword(struct symtable *st, keyword_ty k) |
| 1908 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1909 | VISIT(st, expr, k->value); |
| 1910 | return 1; |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1914 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1915 | symtable_handle_comprehension(struct symtable *st, expr_ty e, |
Pablo Galindo | a5634c4 | 2020-09-16 19:42:00 +0100 | [diff] [blame] | 1916 | identifier scope_name, asdl_comprehension_seq *generators, |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1917 | expr_ty elt, expr_ty value) |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1918 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1919 | int is_generator = (e->kind == GeneratorExp_kind); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1920 | comprehension_ty outermost = ((comprehension_ty) |
| 1921 | asdl_seq_GET(generators, 0)); |
| 1922 | /* Outermost iterator is evaluated in current scope */ |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1923 | st->st_cur->ste_comp_iter_expr++; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1924 | VISIT(st, expr, outermost->iter); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1925 | st->st_cur->ste_comp_iter_expr--; |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1926 | /* Create comprehension scope for the rest */ |
| 1927 | if (!scope_name || |
Benjamin Peterson | d4efd9e | 2010-09-20 23:02:10 +0000 | [diff] [blame] | 1928 | !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e, |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1929 | e->lineno, e->col_offset, |
| 1930 | e->end_lineno, e->end_col_offset)) { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1931 | return 0; |
| 1932 | } |
Yury Selivanov | 52c4e7c | 2016-09-09 10:36:01 -0700 | [diff] [blame] | 1933 | if (outermost->is_async) { |
| 1934 | st->st_cur->ste_coroutine = 1; |
| 1935 | } |
Emily Morehouse | 8f59ee0 | 2019-01-24 16:49:56 -0700 | [diff] [blame] | 1936 | st->st_cur->ste_comprehension = 1; |
| 1937 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1938 | /* Outermost iter is received as an argument */ |
| 1939 | if (!symtable_implicit_arg(st, 0)) { |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1940 | symtable_exit_block(st); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1941 | return 0; |
| 1942 | } |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1943 | /* Visit iteration variable target, and mark them as such */ |
| 1944 | st->st_cur->ste_comp_iter_target = 1; |
Benjamin Peterson | c2575d5 | 2011-06-29 15:27:14 -0500 | [diff] [blame] | 1945 | VISIT(st, expr, outermost->target); |
Nick Coghlan | 5dbe0f5 | 2019-08-25 23:45:40 +1000 | [diff] [blame] | 1946 | st->st_cur->ste_comp_iter_target = 0; |
| 1947 | /* Visit the rest of the comprehension body */ |
Benjamin Peterson | c2575d5 | 2011-06-29 15:27:14 -0500 | [diff] [blame] | 1948 | VISIT_SEQ(st, expr, outermost->ifs); |
| 1949 | VISIT_SEQ_TAIL(st, comprehension, generators, 1); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1950 | if (value) |
Benjamin Peterson | c2575d5 | 2011-06-29 15:27:14 -0500 | [diff] [blame] | 1951 | VISIT(st, expr, value); |
| 1952 | VISIT(st, expr, elt); |
Serhiy Storchaka | 73a7e9b | 2017-12-01 06:54:17 +0200 | [diff] [blame] | 1953 | if (st->st_cur->ste_generator) { |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1954 | PyErr_SetString(PyExc_SyntaxError, |
Serhiy Storchaka | 73a7e9b | 2017-12-01 06:54:17 +0200 | [diff] [blame] | 1955 | (e->kind == ListComp_kind) ? "'yield' inside list comprehension" : |
| 1956 | (e->kind == SetComp_kind) ? "'yield' inside set comprehension" : |
| 1957 | (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" : |
| 1958 | "'yield' inside generator expression"); |
Pablo Galindo | a77aac4 | 2021-04-23 14:27:05 +0100 | [diff] [blame^] | 1959 | PyErr_RangedSyntaxLocationObject(st->st_filename, |
| 1960 | st->st_cur->ste_lineno, |
| 1961 | st->st_cur->ste_col_offset + 1, |
| 1962 | st->st_cur->ste_end_lineno, |
| 1963 | st->st_cur->ste_end_col_offset + 1); |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1964 | symtable_exit_block(st); |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1965 | return 0; |
Serhiy Storchaka | 73a7e9b | 2017-12-01 06:54:17 +0200 | [diff] [blame] | 1966 | } |
Serhiy Storchaka | 07ca9af | 2018-02-04 10:53:48 +0200 | [diff] [blame] | 1967 | st->st_cur->ste_generator = is_generator; |
Andy Lester | 9566842 | 2020-03-06 09:46:04 -0600 | [diff] [blame] | 1968 | return symtable_exit_block(st); |
Jeremy Hylton | 3e0055f | 2005-10-20 19:59:25 +0000 | [diff] [blame] | 1969 | } |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1970 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1971 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1972 | symtable_visit_genexp(struct symtable *st, expr_ty e) |
| 1973 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1974 | return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr), |
| 1975 | e->v.GeneratorExp.generators, |
| 1976 | e->v.GeneratorExp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1979 | static int |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1980 | symtable_visit_listcomp(struct symtable *st, expr_ty e) |
| 1981 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1982 | return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp), |
| 1983 | e->v.ListComp.generators, |
| 1984 | e->v.ListComp.elt, NULL); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 1985 | } |
| 1986 | |
| 1987 | static int |
| 1988 | symtable_visit_setcomp(struct symtable *st, expr_ty e) |
| 1989 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1990 | return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp), |
| 1991 | e->v.SetComp.generators, |
| 1992 | e->v.SetComp.elt, NULL); |
Guido van Rossum | 992d4a3 | 2007-07-11 13:09:30 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
| 1995 | static int |
| 1996 | symtable_visit_dictcomp(struct symtable *st, expr_ty e) |
| 1997 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 1998 | return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp), |
| 1999 | e->v.DictComp.generators, |
| 2000 | e->v.DictComp.key, |
| 2001 | e->v.DictComp.value); |
Nick Coghlan | 650f0d0 | 2007-04-15 12:05:43 +0000 | [diff] [blame] | 2002 | } |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 2003 | |
| 2004 | |
| 2005 | struct symtable * |
| 2006 | _Py_SymtableStringObjectFlags(const char *str, PyObject *filename, |
| 2007 | int start, PyCompilerFlags *flags) |
| 2008 | { |
| 2009 | struct symtable *st; |
| 2010 | mod_ty mod; |
| 2011 | PyArena *arena; |
| 2012 | |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 2013 | arena = _PyArena_New(); |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 2014 | if (arena == NULL) |
| 2015 | return NULL; |
| 2016 | |
Victor Stinner | 57364ce | 2021-03-24 01:29:09 +0100 | [diff] [blame] | 2017 | mod = _PyParser_ASTFromString(str, filename, start, flags, arena); |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 2018 | if (mod == NULL) { |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 2019 | _PyArena_Free(arena); |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 2020 | return NULL; |
| 2021 | } |
| 2022 | st = _PySymtable_Build(mod, filename, 0); |
Victor Stinner | 8370e07 | 2021-03-24 02:23:01 +0100 | [diff] [blame] | 2023 | _PyArena_Free(arena); |
Victor Stinner | 28ad12f | 2021-03-19 12:41:49 +0100 | [diff] [blame] | 2024 | return st; |
| 2025 | } |