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