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