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