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