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