blob: e620f1ecc1bc2d71ec9a6bbfbf2f1db9ba0aa086 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner94faa072021-03-23 20:47:40 +01002#include "pycore_ast.h" // identifier, stmt_ty
Victor Stinnera81fca62021-03-24 00:51:50 +01003#include "pycore_compile.h" // _Py_Mangle()
Victor Stinner57364ce2021-03-24 01:29:09 +01004#include "pycore_parser.h" // _PyParser_ASTFromString()
Victor Stinner28ad12f2021-03-19 12:41:49 +01005#include "pycore_pystate.h" // _PyThreadState_GET()
6#include "pycore_symtable.h" // PySTEntryObject
Victor Stinner4a21e572020-04-15 02:35:41 +02007#include "structmember.h" // PyMemberDef
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00008
Neal Norwitz5d0ad502005-12-19 04:27:42 +00009/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +020010#define GLOBAL_PARAM \
11"name '%U' is parameter and global"
12
13#define NONLOCAL_PARAM \
14"name '%U' is parameter and nonlocal"
15
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070017"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000018
Jeremy Hylton81e95022007-02-27 06:50:52 +000019#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070020"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070023"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000024
Jeremy Hylton81e95022007-02-27 06:50:52 +000025#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070026"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 Hylton81e95022007-02-27 06:50:52 +000033
Neal Norwitz5d0ad502005-12-19 04:27:42 +000034#define IMPORT_STAR_WARNING "import * only allowed at module level"
35
Emily Morehouse8f59ee02019-01-24 16:49:56 -070036#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100037"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 Morehouse8f59ee02019-01-24 16:49:56 -070047
Neal Norwitz090b3dd2006-02-28 22:36:46 +000048static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000049ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Pablo Galindoa77aac42021-04-23 14:27:05 +010050 void *key, int lineno, int col_offset,
51 int end_lineno, int end_col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020054 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 k = PyLong_FromVoidPtr(key);
57 if (k == NULL)
58 goto fail;
59 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020060 if (ste == NULL) {
61 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020063 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020065 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020068 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000069
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070 ste->ste_symbols = NULL;
71 ste->ste_varnames = NULL;
72 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073
Benjamin Petersond9c87022012-10-31 20:26:20 -040074 ste->ste_directives = NULL;
75
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000077 ste->ste_nested = 0;
78 ste->ste_free = 0;
79 ste->ste_varargs = 0;
80 ste->ste_varkeywords = 0;
81 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000082 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000084 ste->ste_col_offset = col_offset;
Pablo Galindoa77aac42021-04-23 14:27:05 +010085 ste->ste_end_lineno = end_lineno;
86 ste->ste_end_col_offset = end_col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088 if (st->st_cur != NULL &&
89 (st->st_cur->ste_nested ||
90 st->st_cur->ste_type == FunctionBlock))
91 ste->ste_nested = 1;
92 ste->ste_child_free = 0;
93 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070094 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070095 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000096 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050097 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100098 ste->ste_comp_iter_target = 0;
99 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100
Victor Stinner9a4fb662013-07-11 22:49:00 +0200101 ste->ste_symbols = PyDict_New();
102 ste->ste_varnames = PyList_New(0);
103 ste->ste_children = PyList_New(0);
104 if (ste->ste_symbols == NULL
105 || ste->ste_varnames == NULL
106 || ste->ste_children == NULL)
107 goto fail;
108
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
110 goto fail;
111
112 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000114 Py_XDECREF(ste);
115 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116}
117
118static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000121 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
122 ste->ste_name,
123 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124}
125
126static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000127ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000128{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000129 ste->ste_table = NULL;
130 Py_XDECREF(ste->ste_id);
131 Py_XDECREF(ste->ste_name);
132 Py_XDECREF(ste->ste_symbols);
133 Py_XDECREF(ste->ste_varnames);
134 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400135 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100136 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000137}
138
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000139#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000140
Guido van Rossum6f799372001-09-20 20:46:19 +0000141static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 {"id", T_OBJECT, OFF(ste_id), READONLY},
143 {"name", T_OBJECT, OFF(ste_name), READONLY},
144 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
145 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
146 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000147 {"nested", T_INT, OFF(ste_nested), READONLY},
148 {"type", T_INT, OFF(ste_type), READONLY},
149 {"lineno", T_INT, OFF(ste_lineno), READONLY},
150 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000151};
152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000153PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000154 PyVarObject_HEAD_INIT(&PyType_Type, 0)
155 "symtable entry",
156 sizeof(PySTEntryObject),
157 0,
158 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200159 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 0, /* tp_getattr */
161 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200162 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000163 (reprfunc)ste_repr, /* tp_repr */
164 0, /* tp_as_number */
165 0, /* tp_as_sequence */
166 0, /* tp_as_mapping */
167 0, /* tp_hash */
168 0, /* tp_call */
169 0, /* tp_str */
170 PyObject_GenericGetAttr, /* tp_getattro */
171 0, /* tp_setattro */
172 0, /* tp_as_buffer */
173 Py_TPFLAGS_DEFAULT, /* tp_flags */
174 0, /* tp_doc */
175 0, /* tp_traverse */
176 0, /* tp_clear */
177 0, /* tp_richcompare */
178 0, /* tp_weaklistoffset */
179 0, /* tp_iter */
180 0, /* tp_iternext */
181 0, /* tp_methods */
182 ste_memberlist, /* tp_members */
183 0, /* tp_getset */
184 0, /* tp_base */
185 0, /* tp_dict */
186 0, /* tp_descr_get */
187 0, /* tp_descr_set */
188 0, /* tp_dictoffset */
189 0, /* tp_init */
190 0, /* tp_alloc */
191 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000192};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193
194static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000195static int symtable_enter_block(struct symtable *st, identifier name,
Pablo Galindoa77aac42021-04-23 14:27:05 +0100196 _Py_block_ty block, void *ast,
197 int lineno, int col_offset,
198 int end_lineno, int end_col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600199static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
201static int symtable_visit_expr(struct symtable *st, expr_ty s);
202static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000203static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
204static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000205static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000206static int symtable_visit_arguments(struct symtable *st, arguments_ty);
207static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
208static int symtable_visit_alias(struct symtable *st, alias_ty);
209static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
210static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100211static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
212static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213static int symtable_implicit_arg(struct symtable *st, int pos);
Andy Lester95668422020-03-06 09:46:04 -0600214static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500215static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Brandt Bucher145bf262021-02-26 14:51:55 -0800216static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
Nick Coghlan1e7b8582021-04-29 15:58:44 +1000217static int symtable_visit_pattern(struct symtable *st, pattern_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218
219
Nick Coghlan650f0d02007-04-15 12:05:43 +0000220static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500222 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
224#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226
227#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000228"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
230static struct symtable *
231symtable_new(void)
232{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700236 if (st == NULL) {
237 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700239 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000240
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000241 st->st_filename = NULL;
242 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000243
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 if ((st->st_stack = PyList_New(0)) == NULL)
245 goto fail;
246 if ((st->st_blocks = PyDict_New()) == NULL)
247 goto fail;
248 st->st_cur = NULL;
249 st->st_private = NULL;
250 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000251 fail:
Victor Stinner28ad12f2021-03-19 12:41:49 +0100252 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000253 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254}
255
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000256/* When compiling the use of C stack is probably going to be a lot
257 lighter than when executing Python code but still can overflow
258 and causing a Python crash if not checked (e.g. eval("()"*300000)).
259 Using the current recursion limit for the compiler seems too
260 restrictive (it caused at least one test to fail) so a factor is
261 used to allow deeper recursion when compiling an expression.
262
263 Using a scaling factor means this should automatically adjust when
264 the recursion limit is adjusted for small or large C stack allocations.
265*/
266#define COMPILER_STACK_FRAME_SCALE 3
267
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000268struct symtable *
Victor Stinner28ad12f2021-03-19 12:41:49 +0100269_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000270{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000271 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100272 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000274 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400275 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000276 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000277
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000278 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200279 return NULL;
280 if (filename == NULL) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100281 _PySymtable_Free(st);
Victor Stinner14e461d2013-08-26 22:28:21 +0200282 return NULL;
283 }
284 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 st->st_filename = filename;
286 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000287
288 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100289 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000290 if (!tstate) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100291 _PySymtable_Free(st);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000292 return NULL;
293 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400294 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000295 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400296 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000297 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400298 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
299 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000300
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 /* Make the initial symbol information gathering pass */
302 if (!GET_IDENTIFIER(top) ||
Pablo Galindoa77aac42021-04-23 14:27:05 +0100303 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0, 0, 0)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100304 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 return NULL;
306 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000307
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000309 switch (mod->kind) {
310 case Module_kind:
311 seq = mod->v.Module.body;
312 for (i = 0; i < asdl_seq_LEN(seq); i++)
313 if (!symtable_visit_stmt(st,
314 (stmt_ty)asdl_seq_GET(seq, i)))
315 goto error;
316 break;
317 case Expression_kind:
318 if (!symtable_visit_expr(st, mod->v.Expression.body))
319 goto error;
320 break;
321 case Interactive_kind:
322 seq = mod->v.Interactive.body;
323 for (i = 0; i < asdl_seq_LEN(seq); i++)
324 if (!symtable_visit_stmt(st,
325 (stmt_ty)asdl_seq_GET(seq, i)))
326 goto error;
327 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800328 case FunctionType_kind:
329 PyErr_SetString(PyExc_RuntimeError,
330 "this compiler does not handle FunctionTypes");
331 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000332 }
Andy Lester95668422020-03-06 09:46:04 -0600333 if (!symtable_exit_block(st)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100334 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 return NULL;
336 }
Nick Coghlan06145232019-08-29 23:26:53 +1000337 /* Check that the recursion depth counting balanced correctly */
338 if (st->recursion_depth != starting_recursion_depth) {
339 PyErr_Format(PyExc_SystemError,
340 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
341 starting_recursion_depth, st->recursion_depth);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100342 _PySymtable_Free(st);
Nick Coghlan06145232019-08-29 23:26:53 +1000343 return NULL;
344 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 /* Make the second symbol analysis pass */
346 if (symtable_analyze(st))
347 return st;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100348 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350 error:
Andy Lester95668422020-03-06 09:46:04 -0600351 (void) symtable_exit_block(st);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100352 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354}
355
Victor Stinner14e461d2013-08-26 22:28:21 +0200356
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000357void
Victor Stinner28ad12f2021-03-19 12:41:49 +0100358_PySymtable_Free(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359{
Victor Stinner14e461d2013-08-26 22:28:21 +0200360 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 Py_XDECREF(st->st_blocks);
362 Py_XDECREF(st->st_stack);
363 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364}
365
366PySTEntryObject *
367PySymtable_Lookup(struct symtable *st, void *key)
368{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 k = PyLong_FromVoidPtr(key);
372 if (k == NULL)
373 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200374 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 if (v) {
376 assert(PySTEntry_Check(v));
377 Py_INCREF(v);
378 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200379 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 PyErr_SetString(PyExc_KeyError,
381 "unknown symbol table entry");
382 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000383
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 Py_DECREF(k);
385 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386}
387
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000388static long
389_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200391 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000392 if (!v)
393 return 0;
394 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000395 return PyLong_AS_LONG(v);
396}
397
398int
Victor Stinner28ad12f2021-03-19 12:41:49 +0100399_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000400{
401 long symbol = _PyST_GetSymbol(ste, name);
402 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403}
404
Benjamin Petersond9c87022012-10-31 20:26:20 -0400405static int
406error_at_directive(PySTEntryObject *ste, PyObject *name)
407{
408 Py_ssize_t i;
409 PyObject *data;
410 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600411 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400412 data = PyList_GET_ITEM(ste->ste_directives, i);
413 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600414 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
415 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
Pablo Galindoa77aac42021-04-23 14:27:05 +0100416 PyErr_RangedSyntaxLocationObject(ste->ste_table->st_filename,
417 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
418 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1,
419 PyLong_AsLong(PyTuple_GET_ITEM(data, 3)),
420 PyLong_AsLong(PyTuple_GET_ITEM(data, 4)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600421
422 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700423 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400424 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600425 PyErr_SetString(PyExc_RuntimeError,
426 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400427 return 0;
428}
429
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430
431/* Analyze raw symbol information to determine scope of each name.
432
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000433 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000434 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000435 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439 explicit global is declared with the global statement. An implicit
440 global is a free variable for which the compiler has found no binding
441 in an enclosing function scope. The implicit global is either a global
442 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
443 to handle these names to implement slightly odd semantics. In such a
444 block, the name is treated as global until it is assigned to; then it
445 is treated as a local.
446
447 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000448 The first pass collects raw facts from the AST via the symtable_visit_*
449 functions: the name is a parameter here, the name is used but not defined
450 here, etc. The second pass analyzes these facts during a pass over the
451 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452
453 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000455 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000456 Names which are explicitly declared nonlocal must exist in this set of
457 visible names - if they do not, a syntax error is raised. After doing
458 the local analysis, it analyzes each of its child blocks using an
459 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000460
Nick Coghlan650f0d02007-04-15 12:05:43 +0000461 The children update the free variable set. If a local variable is added to
462 the free variable set by the child, the variable is marked as a cell. The
463 function object being defined must provide runtime storage for the variable
464 that may outlive the function's frame. Cell variables are removed from the
465 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000466
Nick Coghlan650f0d02007-04-15 12:05:43 +0000467 During analysis, the names are:
468 symbols: dict mapping from symbol names to flag values (including offset scope values)
469 scopes: dict mapping from symbol names to scope values (no offset)
470 local: set of all symbol names local to the current scope
471 bound: set of all symbol names local to a containing function scope
472 free: set of all symbol names referenced but not bound in child scopes
473 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000474*/
475
476#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000477 PyObject *o = PyLong_FromLong(I); \
478 if (!o) \
479 return 0; \
480 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
481 Py_DECREF(o); \
482 return 0; \
483 } \
484 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485}
486
487/* Decide on scope of name, given flags.
488
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000489 The namespace dictionaries may be modified to record information
490 about the new name. For example, a new global will add an entry to
491 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492*/
493
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000495analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 PyObject *bound, PyObject *local, PyObject *free,
497 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000498{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 if (flags & DEF_NONLOCAL) {
501 PyErr_Format(PyExc_SyntaxError,
502 "name '%U' is nonlocal and global",
503 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400504 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 }
506 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
507 if (PySet_Add(global, name) < 0)
508 return 0;
509 if (bound && (PySet_Discard(bound, name) < 0))
510 return 0;
511 return 1;
512 }
513 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000514 if (!bound) {
515 PyErr_Format(PyExc_SyntaxError,
516 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400517 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 }
519 if (!PySet_Contains(bound, name)) {
520 PyErr_Format(PyExc_SyntaxError,
521 "no binding for nonlocal '%U' found",
522 name);
523
Benjamin Petersond9c87022012-10-31 20:26:20 -0400524 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 }
526 SET_SCOPE(scopes, name, FREE);
527 ste->ste_free = 1;
528 return PySet_Add(free, name) >= 0;
529 }
530 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000531 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (PySet_Add(local, name) < 0)
533 return 0;
534 if (PySet_Discard(global, name) < 0)
535 return 0;
536 return 1;
537 }
538 /* If an enclosing block has a binding for this name, it
539 is a free variable rather than a global variable.
540 Note that having a non-NULL bound implies that the block
541 is nested.
542 */
543 if (bound && PySet_Contains(bound, name)) {
544 SET_SCOPE(scopes, name, FREE);
545 ste->ste_free = 1;
546 return PySet_Add(free, name) >= 0;
547 }
548 /* If a parent has a global statement, then call it global
549 explicit? It could also be global implicit.
550 */
551 if (global && PySet_Contains(global, name)) {
552 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
553 return 1;
554 }
555 if (ste->ste_nested)
556 ste->ste_free = 1;
557 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
558 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000559}
560
561#undef SET_SCOPE
562
563/* If a name is defined in free and also in locals, then this block
564 provides the binding for the free variable. The name should be
565 marked CELL in this block and removed from the free list.
566
567 Note that the current block's free variables are included in free.
568 That's safe because no name can be free and local in the same scope.
569*/
570
571static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500572analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 PyObject *name, *v, *v_cell;
575 int success = 0;
576 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 v_cell = PyLong_FromLong(CELL);
579 if (!v_cell)
580 return 0;
581 while (PyDict_Next(scopes, &pos, &name, &v)) {
582 long scope;
583 assert(PyLong_Check(v));
584 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000585 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 continue;
587 if (!PySet_Contains(free, name))
588 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589 /* Replace LOCAL with CELL for this name, and remove
590 from free. It is safe to replace the value of name
591 in the dict, because it will not cause a resize.
592 */
593 if (PyDict_SetItem(scopes, name, v_cell) < 0)
594 goto error;
595 if (PySet_Discard(free, name) < 0)
596 goto error;
597 }
598 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 Py_DECREF(v_cell);
601 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602}
603
Benjamin Peterson312595c2013-05-15 15:26:42 -0500604static int
605drop_class_free(PySTEntryObject *ste, PyObject *free)
606{
607 int res;
608 if (!GET_IDENTIFIER(__class__))
609 return 0;
610 res = PySet_Discard(free, __class__);
611 if (res < 0)
612 return 0;
613 if (res)
614 ste->ste_needs_class_closure = 1;
615 return 1;
616}
617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618/* Enter the final scope information into the ste_symbols dict.
619 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000620 * All arguments are dicts. Modifies symbols, others are read-only.
621*/
622static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000624 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000625{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000626 PyObject *name = NULL, *itr = NULL;
627 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
628 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 /* Update scope information for all symbols in this scope */
631 while (PyDict_Next(symbols, &pos, &name, &v)) {
632 long scope, flags;
633 assert(PyLong_Check(v));
634 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200635 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000636 assert(v_scope && PyLong_Check(v_scope));
637 scope = PyLong_AS_LONG(v_scope);
638 flags |= (scope << SCOPE_OFFSET);
639 v_new = PyLong_FromLong(flags);
640 if (!v_new)
641 return 0;
642 if (PyDict_SetItem(symbols, name, v_new) < 0) {
643 Py_DECREF(v_new);
644 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000645 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000646 Py_DECREF(v_new);
647 }
648
649 /* Record not yet resolved free variables from children (if any) */
650 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
651 if (!v_free)
652 return 0;
653
654 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600655 if (itr == NULL) {
656 Py_DECREF(v_free);
657 return 0;
658 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659
660 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200661 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000662
663 /* Handle symbol that already exists in this scope */
664 if (v) {
665 /* Handle a free variable in a method of
666 the class that has the same name as a local
667 or global in the class scope.
668 */
669 if (classflag &&
670 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
671 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
672 v_new = PyLong_FromLong(flags);
673 if (!v_new) {
674 goto error;
675 }
676 if (PyDict_SetItem(symbols, name, v_new) < 0) {
677 Py_DECREF(v_new);
678 goto error;
679 }
680 Py_DECREF(v_new);
681 }
682 /* It's a cell, or already free in this scope */
683 Py_DECREF(name);
684 continue;
685 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200686 else if (PyErr_Occurred()) {
687 goto error;
688 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000689 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200690 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 Py_DECREF(name);
692 continue; /* it's a global */
693 }
694 /* Propagate new free symbol up the lexical stack */
695 if (PyDict_SetItem(symbols, name, v_free) < 0) {
696 goto error;
697 }
698 Py_DECREF(name);
699 }
700 Py_DECREF(itr);
701 Py_DECREF(v_free);
702 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000703error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 Py_XDECREF(v_free);
705 Py_XDECREF(itr);
706 Py_XDECREF(name);
707 return 0;
708}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709
710/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000711
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000712 Arguments:
713 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000714 bound -- set of variables bound in enclosing scopes (input). bound
715 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 free -- set of free variables in enclosed scopes (output)
717 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000718
719 The implementation uses two mutually recursive functions,
720 analyze_block() and analyze_child_block(). analyze_block() is
721 responsible for analyzing the individual names defined in a block.
722 analyze_child_block() prepares temporary namespace dictionaries
723 used to evaluated nested blocks.
724
725 The two functions exist because a child block should see the name
726 bindings of its enclosing blocks, but those bindings should not
727 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000728*/
729
730static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
732 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000733
734static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
736 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
739 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
740 PyObject *temp;
741 int i, success = 0;
742 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 local = PySet_New(NULL); /* collect new names bound in block */
745 if (!local)
746 goto error;
747 scopes = PyDict_New(); /* collect scopes defined for each name */
748 if (!scopes)
749 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 /* Allocate new global and bound variable dictionaries. These
752 dictionaries hold the names visible in nested blocks. For
753 ClassBlocks, the bound and global names are initialized
754 before analyzing names, because class bindings aren't
755 visible in methods. For other blocks, they are initialized
756 after names are analyzed.
757 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000758
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000759 /* TODO(jhylton): Package these dicts in a struct so that we
760 can write reasonable helper functions?
761 */
762 newglobal = PySet_New(NULL);
763 if (!newglobal)
764 goto error;
765 newfree = PySet_New(NULL);
766 if (!newfree)
767 goto error;
768 newbound = PySet_New(NULL);
769 if (!newbound)
770 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000772 /* Class namespace has no effect on names visible in
773 nested functions, so populate the global and bound
774 sets to be passed to child blocks before analyzing
775 this one.
776 */
777 if (ste->ste_type == ClassBlock) {
778 /* Pass down known globals */
779 temp = PyNumber_InPlaceOr(newglobal, global);
780 if (!temp)
781 goto error;
782 Py_DECREF(temp);
783 /* Pass down previously bound symbols */
784 if (bound) {
785 temp = PyNumber_InPlaceOr(newbound, bound);
786 if (!temp)
787 goto error;
788 Py_DECREF(temp);
789 }
790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
793 long flags = PyLong_AS_LONG(v);
794 if (!analyze_name(ste, scopes, name, flags,
795 bound, local, free, global))
796 goto error;
797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 /* Populate global and bound sets to be passed to children. */
800 if (ste->ste_type != ClassBlock) {
801 /* Add function locals to bound set */
802 if (ste->ste_type == FunctionBlock) {
803 temp = PyNumber_InPlaceOr(newbound, local);
804 if (!temp)
805 goto error;
806 Py_DECREF(temp);
807 }
808 /* Pass down previously bound symbols */
809 if (bound) {
810 temp = PyNumber_InPlaceOr(newbound, bound);
811 if (!temp)
812 goto error;
813 Py_DECREF(temp);
814 }
815 /* Pass down known globals */
816 temp = PyNumber_InPlaceOr(newglobal, global);
817 if (!temp)
818 goto error;
819 Py_DECREF(temp);
820 }
821 else {
822 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000823 if (!GET_IDENTIFIER(__class__))
824 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 if (PySet_Add(newbound, __class__) < 0)
826 goto error;
827 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000828
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300829 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 newbound, newglobal now contain the names visible in
832 nested blocks. The free variables in the children will
833 be collected in allfree.
834 */
835 allfree = PySet_New(NULL);
836 if (!allfree)
837 goto error;
838 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
839 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
840 PySTEntryObject* entry;
841 assert(c && PySTEntry_Check(c));
842 entry = (PySTEntryObject*)c;
843 if (!analyze_child_block(entry, newbound, newfree, newglobal,
844 allfree))
845 goto error;
846 /* Check if any children have free variables */
847 if (entry->ste_free || entry->ste_child_free)
848 ste->ste_child_free = 1;
849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 temp = PyNumber_InPlaceOr(newfree, allfree);
852 if (!temp)
853 goto error;
854 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000855
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000856 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500857 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500859 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 goto error;
861 /* Records the results of the analysis in the symbol table entry */
862 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
863 ste->ste_type == ClassBlock))
864 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 temp = PyNumber_InPlaceOr(free, newfree);
867 if (!temp)
868 goto error;
869 Py_DECREF(temp);
870 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000871 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 Py_XDECREF(scopes);
873 Py_XDECREF(local);
874 Py_XDECREF(newbound);
875 Py_XDECREF(newglobal);
876 Py_XDECREF(newfree);
877 Py_XDECREF(allfree);
878 if (!success)
879 assert(PyErr_Occurred());
880 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000881}
882
883static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
885 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000886{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
888 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000891
Martin Panter3ee62702016-06-04 04:57:19 +0000892 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 current block. The analyze_block() call modifies these
894 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000895
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 */
897 temp_bound = PySet_New(bound);
898 if (!temp_bound)
899 goto error;
900 temp_free = PySet_New(free);
901 if (!temp_free)
902 goto error;
903 temp_global = PySet_New(global);
904 if (!temp_global)
905 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
908 goto error;
909 temp = PyNumber_InPlaceOr(child_free, temp_free);
910 if (!temp)
911 goto error;
912 Py_DECREF(temp);
913 Py_DECREF(temp_bound);
914 Py_DECREF(temp_free);
915 Py_DECREF(temp_global);
916 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000917 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000918 Py_XDECREF(temp_bound);
919 Py_XDECREF(temp_free);
920 Py_XDECREF(temp_global);
921 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000922}
923
924static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000925symtable_analyze(struct symtable *st)
926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 PyObject *free, *global;
928 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000930 free = PySet_New(NULL);
931 if (!free)
932 return 0;
933 global = PySet_New(NULL);
934 if (!global) {
935 Py_DECREF(free);
936 return 0;
937 }
938 r = analyze_block(st->st_top, NULL, free, global);
939 Py_DECREF(free);
940 Py_DECREF(global);
941 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942}
943
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000944/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945 This reference is released when the block is exited, via the DECREF
946 in symtable_exit_block().
947*/
948
949static int
Andy Lester95668422020-03-06 09:46:04 -0600950symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000951{
Benjamin Peterson609da582011-06-29 22:52:39 -0500952 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953
Benjamin Peterson609da582011-06-29 22:52:39 -0500954 st->st_cur = NULL;
955 size = PyList_GET_SIZE(st->st_stack);
956 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500957 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500959 if (--size)
960 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000961 }
962 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963}
964
965static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Pablo Galindoa77aac42021-04-23 14:27:05 +0100967 void *ast, int lineno, int col_offset,
968 int end_lineno, int end_col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969{
Benjamin Peterson609da582011-06-29 22:52:39 -0500970 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000971
Pablo Galindoa77aac42021-04-23 14:27:05 +0100972 ste = ste_new(st, name, block, ast, lineno, col_offset, end_lineno, end_col_offset);
Benjamin Peterson609da582011-06-29 22:52:39 -0500973 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000974 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500975 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
976 Py_DECREF(ste);
977 return 0;
978 }
979 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000980 /* bpo-37757: For now, disallow *all* assignment expressions in the
981 * outermost iterator expression of a comprehension, even those inside
982 * a nested comprehension or a lambda expression.
983 */
984 if (prev) {
985 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
986 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500987 /* The entry is owned by the stack. Borrow it for st_cur. */
988 Py_DECREF(ste);
989 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000990 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 st->st_global = st->st_cur->ste_symbols;
992 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500993 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 return 0;
995 }
996 }
997 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000998}
999
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001000static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001symtable_lookup(struct symtable *st, PyObject *name)
1002{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001003 PyObject *mangled = _Py_Mangle(st->st_private, name);
1004 if (!mangled)
1005 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +01001006 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1007 Py_DECREF(mangled);
1008 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009}
1010
1011static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001012symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 PyObject *o;
1015 PyObject *dict;
1016 long val;
1017 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001018
Jeremy Hylton81e95022007-02-27 06:50:52 +00001019
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 if (!mangled)
1021 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001022 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001023 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 val = PyLong_AS_LONG(o);
1025 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1026 /* Is it better to use 'mangled' or 'name' here? */
1027 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001028 PyErr_RangedSyntaxLocationObject(st->st_filename,
1029 ste->ste_lineno,
1030 ste->ste_col_offset + 1,
1031 ste->ste_end_lineno,
1032 ste->ste_end_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 goto error;
1034 }
1035 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001036 }
1037 else if (PyErr_Occurred()) {
1038 goto error;
1039 }
1040 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001042 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001043 if (ste->ste_comp_iter_target) {
1044 /* This name is an iteration variable in a comprehension,
1045 * so check for a binding conflict with any named expressions.
1046 * Otherwise, mark it as an iteration variable so subsequent
1047 * named expressions can check for conflicts.
1048 */
1049 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1050 PyErr_Format(PyExc_SyntaxError,
1051 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001052 PyErr_RangedSyntaxLocationObject(st->st_filename,
1053 ste->ste_lineno,
1054 ste->ste_col_offset + 1,
1055 ste->ste_end_lineno,
1056 ste->ste_end_col_offset + 1);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001057 goto error;
1058 }
1059 val |= DEF_COMP_ITER;
1060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 o = PyLong_FromLong(val);
1062 if (o == NULL)
1063 goto error;
1064 if (PyDict_SetItem(dict, mangled, o) < 0) {
1065 Py_DECREF(o);
1066 goto error;
1067 }
1068 Py_DECREF(o);
1069
1070 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001071 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 goto error;
1073 } else if (flag & DEF_GLOBAL) {
1074 /* XXX need to update DEF_GLOBAL for other flags too;
1075 perhaps only DEF_FREE_GLOBAL */
1076 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001077 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 val |= PyLong_AS_LONG(o);
1079 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001080 else if (PyErr_Occurred()) {
1081 goto error;
1082 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 goto error;
1086 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1087 Py_DECREF(o);
1088 goto error;
1089 }
1090 Py_DECREF(o);
1091 }
1092 Py_DECREF(mangled);
1093 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001094
1095error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 Py_DECREF(mangled);
1097 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098}
1099
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001100static int
1101symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1102 return symtable_add_def_helper(st, name, flag, st->st_cur);
1103}
1104
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001105/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1106 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 function.
1108
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1110 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001111
1112 VISIT_QUIT macro returns the specified value exiting from the function but
1113 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001114*/
1115
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001116#define VISIT_QUIT(ST, X) \
1117 return --(ST)->recursion_depth,(X)
1118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001121 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001123#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001125 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001126 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1127 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1128 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001129 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001131}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001133#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001135 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1137 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1138 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001139 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001142
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001143#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001145 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001147 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001149 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001150 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001151 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001152}
1153
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001154static int
Pablo Galindoa77aac42021-04-23 14:27:05 +01001155symtable_record_directive(struct symtable *st, identifier name, int lineno,
1156 int col_offset, int end_lineno, int end_col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001157{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001158 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001159 int res;
1160 if (!st->st_cur->ste_directives) {
1161 st->st_cur->ste_directives = PyList_New(0);
1162 if (!st->st_cur->ste_directives)
1163 return 0;
1164 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001165 mangled = _Py_Mangle(st->st_private, name);
1166 if (!mangled)
1167 return 0;
Pablo Galindoa77aac42021-04-23 14:27:05 +01001168 data = Py_BuildValue("(Niiii)", mangled, lineno, col_offset, end_lineno, end_col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001169 if (!data)
1170 return 0;
1171 res = PyList_Append(st->st_cur->ste_directives, data);
1172 Py_DECREF(data);
1173 return res == 0;
1174}
1175
1176
1177static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001178symtable_visit_stmt(struct symtable *st, stmt_ty s)
1179{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001180 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001181 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001182 "maximum recursion depth exceeded during compilation");
1183 VISIT_QUIT(st, 0);
1184 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 switch (s->kind) {
1186 case FunctionDef_kind:
1187 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001188 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 if (s->v.FunctionDef.args->defaults)
1190 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1191 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001192 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001193 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001194 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001195 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001196 if (s->v.FunctionDef.decorator_list)
1197 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1198 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001199 FunctionBlock, (void *)s,
1200 s->lineno, s->col_offset,
1201 s->end_lineno, s->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001202 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001203 VISIT(st, arguments, s->v.FunctionDef.args);
1204 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001205 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001206 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 break;
1208 case ClassDef_kind: {
1209 PyObject *tmp;
1210 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001211 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1213 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 if (s->v.ClassDef.decorator_list)
1215 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1216 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001217 (void *)s, s->lineno, s->col_offset,
1218 s->end_lineno, s->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001219 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 tmp = st->st_private;
1221 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001222 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001223 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001224 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001225 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 break;
1227 }
1228 case Return_kind:
1229 if (s->v.Return.value) {
1230 VISIT(st, expr, s->v.Return.value);
1231 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001232 }
1233 break;
1234 case Delete_kind:
1235 VISIT_SEQ(st, expr, s->v.Delete.targets);
1236 break;
1237 case Assign_kind:
1238 VISIT_SEQ(st, expr, s->v.Assign.targets);
1239 VISIT(st, expr, s->v.Assign.value);
1240 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001241 case AnnAssign_kind:
1242 if (s->v.AnnAssign.target->kind == Name_kind) {
1243 expr_ty e_name = s->v.AnnAssign.target;
1244 long cur = symtable_lookup(st, e_name->v.Name.id);
1245 if (cur < 0) {
1246 VISIT_QUIT(st, 0);
1247 }
1248 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001249 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001250 && s->v.AnnAssign.simple) {
1251 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001252 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1253 e_name->v.Name.id);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001254 PyErr_RangedSyntaxLocationObject(st->st_filename,
1255 s->lineno,
1256 s->col_offset + 1,
1257 s->end_lineno,
1258 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001259 VISIT_QUIT(st, 0);
1260 }
1261 if (s->v.AnnAssign.simple &&
1262 !symtable_add_def(st, e_name->v.Name.id,
1263 DEF_ANNOT | DEF_LOCAL)) {
1264 VISIT_QUIT(st, 0);
1265 }
1266 else {
1267 if (s->v.AnnAssign.value
1268 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1269 VISIT_QUIT(st, 0);
1270 }
1271 }
1272 }
1273 else {
1274 VISIT(st, expr, s->v.AnnAssign.target);
1275 }
1276 VISIT(st, expr, s->v.AnnAssign.annotation);
1277 if (s->v.AnnAssign.value) {
1278 VISIT(st, expr, s->v.AnnAssign.value);
1279 }
1280 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 case AugAssign_kind:
1282 VISIT(st, expr, s->v.AugAssign.target);
1283 VISIT(st, expr, s->v.AugAssign.value);
1284 break;
1285 case For_kind:
1286 VISIT(st, expr, s->v.For.target);
1287 VISIT(st, expr, s->v.For.iter);
1288 VISIT_SEQ(st, stmt, s->v.For.body);
1289 if (s->v.For.orelse)
1290 VISIT_SEQ(st, stmt, s->v.For.orelse);
1291 break;
1292 case While_kind:
1293 VISIT(st, expr, s->v.While.test);
1294 VISIT_SEQ(st, stmt, s->v.While.body);
1295 if (s->v.While.orelse)
1296 VISIT_SEQ(st, stmt, s->v.While.orelse);
1297 break;
1298 case If_kind:
1299 /* XXX if 0: and lookup_yield() hacks */
1300 VISIT(st, expr, s->v.If.test);
1301 VISIT_SEQ(st, stmt, s->v.If.body);
1302 if (s->v.If.orelse)
1303 VISIT_SEQ(st, stmt, s->v.If.orelse);
1304 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001305 case Match_kind:
1306 VISIT(st, expr, s->v.Match.subject);
1307 VISIT_SEQ(st, match_case, s->v.Match.cases);
1308 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 case Raise_kind:
1310 if (s->v.Raise.exc) {
1311 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001312 if (s->v.Raise.cause) {
1313 VISIT(st, expr, s->v.Raise.cause);
1314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 }
1316 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001317 case Try_kind:
1318 VISIT_SEQ(st, stmt, s->v.Try.body);
1319 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1320 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1321 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 break;
1323 case Assert_kind:
1324 VISIT(st, expr, s->v.Assert.test);
1325 if (s->v.Assert.msg)
1326 VISIT(st, expr, s->v.Assert.msg);
1327 break;
1328 case Import_kind:
1329 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 break;
1331 case ImportFrom_kind:
1332 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 break;
1334 case Global_kind: {
1335 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001336 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001337 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1338 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001339 long cur = symtable_lookup(st, name);
1340 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001341 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001342 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1343 const char* msg;
1344 if (cur & DEF_PARAM) {
1345 msg = GLOBAL_PARAM;
1346 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001347 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001348 } else if (cur & DEF_ANNOT) {
1349 msg = GLOBAL_ANNOT;
1350 } else { /* DEF_LOCAL */
1351 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001352 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001353 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001354 msg, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001355 PyErr_RangedSyntaxLocationObject(st->st_filename,
1356 s->lineno,
1357 s->col_offset + 1,
1358 s->end_lineno,
1359 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001360 VISIT_QUIT(st, 0);
1361 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001362 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001363 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001364 if (!symtable_record_directive(st, name, s->lineno, s->col_offset,
1365 s->end_lineno, s->end_col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001366 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 }
1368 break;
1369 }
1370 case Nonlocal_kind: {
1371 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001372 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001373 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1374 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 long cur = symtable_lookup(st, name);
1376 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001377 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001378 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1379 const char* msg;
1380 if (cur & DEF_PARAM) {
1381 msg = NONLOCAL_PARAM;
1382 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001383 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001384 } else if (cur & DEF_ANNOT) {
1385 msg = NONLOCAL_ANNOT;
1386 } else { /* DEF_LOCAL */
1387 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001388 }
1389 PyErr_Format(PyExc_SyntaxError, msg, name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001390 PyErr_RangedSyntaxLocationObject(st->st_filename,
1391 s->lineno,
1392 s->col_offset + 1,
1393 s->end_lineno,
1394 s->end_col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001395 VISIT_QUIT(st, 0);
1396 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001398 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001399 if (!symtable_record_directive(st, name, s->lineno, s->col_offset,
1400 s->end_lineno, s->end_col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001401 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 }
1403 break;
1404 }
1405 case Expr_kind:
1406 VISIT(st, expr, s->v.Expr.value);
1407 break;
1408 case Pass_kind:
1409 case Break_kind:
1410 case Continue_kind:
1411 /* nothing to do here */
1412 break;
1413 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001414 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 VISIT_SEQ(st, stmt, s->v.With.body);
1416 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001417 case AsyncFunctionDef_kind:
1418 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1419 VISIT_QUIT(st, 0);
1420 if (s->v.AsyncFunctionDef.args->defaults)
1421 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1422 if (s->v.AsyncFunctionDef.args->kw_defaults)
1423 VISIT_SEQ_WITH_NULL(st, expr,
1424 s->v.AsyncFunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001425 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001426 s->v.AsyncFunctionDef.returns))
1427 VISIT_QUIT(st, 0);
1428 if (s->v.AsyncFunctionDef.decorator_list)
1429 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1430 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001431 FunctionBlock, (void *)s,
1432 s->lineno, s->col_offset,
1433 s->end_lineno, s->end_col_offset))
Yury Selivanov75445082015-05-11 22:57:16 -04001434 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001435 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001436 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1437 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001438 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001439 VISIT_QUIT(st, 0);
1440 break;
1441 case AsyncWith_kind:
1442 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1443 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1444 break;
1445 case AsyncFor_kind:
1446 VISIT(st, expr, s->v.AsyncFor.target);
1447 VISIT(st, expr, s->v.AsyncFor.iter);
1448 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1449 if (s->v.AsyncFor.orelse)
1450 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1451 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001452 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001453 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001454}
1455
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001457symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1458{
1459 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001460 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001461
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001462 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001463 Py_ssize_t i, size;
1464 struct _symtable_entry *ste;
1465 size = PyList_GET_SIZE(st->st_stack);
1466 assert(size);
1467
1468 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1469 for (i = size - 1; i >= 0; i--) {
1470 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1471
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001472 /* If we find a comprehension scope, check for a target
1473 * binding conflict with iteration variables, otherwise skip it
1474 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001475 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001476 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1477 if (target_in_scope & DEF_COMP_ITER) {
1478 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001479 PyErr_RangedSyntaxLocationObject(st->st_filename,
1480 e->lineno,
1481 e->col_offset + 1,
1482 e->end_lineno,
1483 e->end_col_offset + 1);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001484 VISIT_QUIT(st, 0);
1485 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001486 continue;
1487 }
1488
Pablo Galindofd5c4142019-10-14 05:18:05 +01001489 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001490 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001491 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1492 if (target_in_scope & DEF_GLOBAL) {
1493 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1494 VISIT_QUIT(st, 0);
1495 } else {
1496 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1497 VISIT_QUIT(st, 0);
1498 }
Pablo Galindoa77aac42021-04-23 14:27:05 +01001499 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset,
1500 e->end_lineno, e->end_col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001501 VISIT_QUIT(st, 0);
1502
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001503 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001504 }
1505 /* If we find a ModuleBlock entry, add as GLOBAL */
1506 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001507 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001508 VISIT_QUIT(st, 0);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001509 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset,
1510 e->end_lineno, e->end_col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001511 VISIT_QUIT(st, 0);
1512
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001513 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001514 }
1515 /* Disallow usage in ClassBlock */
1516 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001517 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001518 PyErr_RangedSyntaxLocationObject(st->st_filename,
1519 e->lineno,
1520 e->col_offset + 1,
1521 e->end_lineno,
1522 e->end_col_offset + 1);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001523 VISIT_QUIT(st, 0);
1524 }
1525 }
1526
1527 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1528 and should never fall to this case
1529 */
1530 assert(0);
1531 return 0;
1532}
1533
1534static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001535symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1536{
1537 if (st->st_cur->ste_comp_iter_expr > 0) {
1538 /* Assignment isn't allowed in a comprehension iterable expression */
1539 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001540 PyErr_RangedSyntaxLocationObject(st->st_filename,
1541 e->lineno,
1542 e->col_offset + 1,
1543 e->end_lineno,
1544 e->end_col_offset + 1);
Nick Coghlan06145232019-08-29 23:26:53 +10001545 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001546 }
1547 if (st->st_cur->ste_comprehension) {
1548 /* Inside a comprehension body, so find the right target scope */
1549 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001550 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001551 }
1552 VISIT(st, expr, e->v.NamedExpr.value);
1553 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001554 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001555}
1556
1557static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001558symtable_visit_expr(struct symtable *st, expr_ty e)
1559{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001560 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001561 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001562 "maximum recursion depth exceeded during compilation");
1563 VISIT_QUIT(st, 0);
1564 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001566 case NamedExpr_kind:
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001567 if(!symtable_handle_namedexpr(st, e))
1568 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001569 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 case BoolOp_kind:
1571 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1572 break;
1573 case BinOp_kind:
1574 VISIT(st, expr, e->v.BinOp.left);
1575 VISIT(st, expr, e->v.BinOp.right);
1576 break;
1577 case UnaryOp_kind:
1578 VISIT(st, expr, e->v.UnaryOp.operand);
1579 break;
1580 case Lambda_kind: {
1581 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001582 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 if (e->v.Lambda.args->defaults)
1584 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001585 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001586 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 if (!symtable_enter_block(st, lambda,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001588 FunctionBlock, (void *)e,
1589 e->lineno, e->col_offset,
1590 e->end_lineno, e->end_col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001591 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001592 VISIT(st, arguments, e->v.Lambda.args);
1593 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001594 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001595 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 break;
1597 }
1598 case IfExp_kind:
1599 VISIT(st, expr, e->v.IfExp.test);
1600 VISIT(st, expr, e->v.IfExp.body);
1601 VISIT(st, expr, e->v.IfExp.orelse);
1602 break;
1603 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001604 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 VISIT_SEQ(st, expr, e->v.Dict.values);
1606 break;
1607 case Set_kind:
1608 VISIT_SEQ(st, expr, e->v.Set.elts);
1609 break;
1610 case GeneratorExp_kind:
1611 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001612 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 break;
1614 case ListComp_kind:
1615 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001616 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 break;
1618 case SetComp_kind:
1619 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001620 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 break;
1622 case DictComp_kind:
1623 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001624 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 break;
1626 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001627 if (e->v.Yield.value)
1628 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001631 case YieldFrom_kind:
1632 VISIT(st, expr, e->v.YieldFrom.value);
1633 st->st_cur->ste_generator = 1;
1634 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001635 case Await_kind:
1636 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001637 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001638 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 case Compare_kind:
1640 VISIT(st, expr, e->v.Compare.left);
1641 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1642 break;
1643 case Call_kind:
1644 VISIT(st, expr, e->v.Call.func);
1645 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001646 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001648 case FormattedValue_kind:
1649 VISIT(st, expr, e->v.FormattedValue.value);
1650 if (e->v.FormattedValue.format_spec)
1651 VISIT(st, expr, e->v.FormattedValue.format_spec);
1652 break;
1653 case JoinedStr_kind:
1654 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1655 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001656 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 /* Nothing to do here. */
1658 break;
1659 /* The following exprs can be assignment targets. */
1660 case Attribute_kind:
1661 VISIT(st, expr, e->v.Attribute.value);
1662 break;
1663 case Subscript_kind:
1664 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001665 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666 break;
1667 case Starred_kind:
1668 VISIT(st, expr, e->v.Starred.value);
1669 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001670 case Slice_kind:
1671 if (e->v.Slice.lower)
1672 VISIT(st, expr, e->v.Slice.lower)
1673 if (e->v.Slice.upper)
1674 VISIT(st, expr, e->v.Slice.upper)
1675 if (e->v.Slice.step)
1676 VISIT(st, expr, e->v.Slice.step)
1677 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 case Name_kind:
1679 if (!symtable_add_def(st, e->v.Name.id,
1680 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001681 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 /* Special-case super: it counts as a use of __class__ */
1683 if (e->v.Name.ctx == Load &&
1684 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001685 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001686 if (!GET_IDENTIFIER(__class__) ||
1687 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001688 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001689 }
1690 break;
1691 /* child nodes of List and Tuple will have expr_context set */
1692 case List_kind:
1693 VISIT_SEQ(st, expr, e->v.List.elts);
1694 break;
1695 case Tuple_kind:
1696 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1697 break;
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001698 }
1699 VISIT_QUIT(st, 1);
1700}
1701
1702static int
1703symtable_visit_pattern(struct symtable *st, pattern_ty p)
1704{
1705 if (++st->recursion_depth > st->recursion_limit) {
1706 PyErr_SetString(PyExc_RecursionError,
1707 "maximum recursion depth exceeded during compilation");
1708 VISIT_QUIT(st, 0);
1709 }
1710 switch (p->kind) {
1711 case MatchValue_kind:
1712 VISIT(st, expr, p->v.MatchValue.value);
1713 break;
1714 case MatchSingleton_kind:
1715 /* Nothing to do here. */
1716 break;
1717 case MatchSequence_kind:
1718 VISIT_SEQ(st, pattern, p->v.MatchSequence.patterns);
1719 break;
1720 case MatchStar_kind:
1721 if (p->v.MatchStar.name) {
1722 symtable_add_def(st, p->v.MatchStar.name, DEF_LOCAL);
1723 }
1724 break;
1725 case MatchMapping_kind:
1726 VISIT_SEQ(st, expr, p->v.MatchMapping.keys);
1727 VISIT_SEQ(st, pattern, p->v.MatchMapping.patterns);
1728 if (p->v.MatchMapping.rest) {
1729 symtable_add_def(st, p->v.MatchMapping.rest, DEF_LOCAL);
1730 }
1731 break;
1732 case MatchClass_kind:
1733 VISIT(st, expr, p->v.MatchClass.cls);
1734 VISIT_SEQ(st, pattern, p->v.MatchClass.patterns);
1735 VISIT_SEQ(st, pattern, p->v.MatchClass.kwd_patterns);
1736 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001737 case MatchAs_kind:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001738 if (p->v.MatchAs.pattern) {
1739 VISIT(st, pattern, p->v.MatchAs.pattern);
1740 }
1741 if (p->v.MatchAs.name) {
1742 symtable_add_def(st, p->v.MatchAs.name, DEF_LOCAL);
1743 }
Brandt Bucher145bf262021-02-26 14:51:55 -08001744 break;
1745 case MatchOr_kind:
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001746 VISIT_SEQ(st, pattern, p->v.MatchOr.patterns);
Brandt Bucher145bf262021-02-26 14:51:55 -08001747 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001749 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750}
1751
1752static int
1753symtable_implicit_arg(struct symtable *st, int pos)
1754{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001755 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1756 if (id == NULL)
1757 return 0;
1758 if (!symtable_add_def(st, id, DEF_PARAM)) {
1759 Py_DECREF(id);
1760 return 0;
1761 }
1762 Py_DECREF(id);
1763 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764}
1765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001767symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001770
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 if (!args)
1772 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 for (i = 0; i < asdl_seq_LEN(args); i++) {
1775 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1776 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1777 return 0;
1778 }
1779
1780 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001781}
1782
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001784symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001787
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001788 if (!args)
1789 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 for (i = 0; i < asdl_seq_LEN(args); i++) {
1792 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1793 if (arg->annotation)
1794 VISIT(st, expr, arg->annotation);
1795 }
1796
1797 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001798}
1799
Neal Norwitzc1505362006-12-28 06:47:50 +00001800static int
Andy Lester95668422020-03-06 09:46:04 -06001801symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001802{
Anthony Sottileec007cb2020-01-04 20:57:21 -05001803 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1804 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001805 if (a->args && !symtable_visit_argannotations(st, a->args))
1806 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001807 if (a->vararg && a->vararg->annotation)
1808 VISIT(st, expr, a->vararg->annotation);
1809 if (a->kwarg && a->kwarg->annotation)
1810 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1812 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001813 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001814 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816}
1817
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819symtable_visit_arguments(struct symtable *st, arguments_ty a)
1820{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821 /* skip default arguments inside function block
1822 XXX should ast be different?
1823 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001824 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1825 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 if (a->args && !symtable_visit_params(st, a->args))
1827 return 0;
1828 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1829 return 0;
1830 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001831 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 return 0;
1833 st->st_cur->ste_varargs = 1;
1834 }
1835 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001836 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001837 return 0;
1838 st->st_cur->ste_varkeywords = 1;
1839 }
1840 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001841}
1842
1843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001845symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1846{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 if (eh->v.ExceptHandler.type)
1848 VISIT(st, expr, eh->v.ExceptHandler.type);
1849 if (eh->v.ExceptHandler.name)
1850 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1851 return 0;
1852 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1853 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854}
1855
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001856static int
1857symtable_visit_withitem(struct symtable *st, withitem_ty item)
1858{
1859 VISIT(st, expr, item->context_expr);
1860 if (item->optional_vars) {
1861 VISIT(st, expr, item->optional_vars);
1862 }
1863 return 1;
1864}
1865
Brandt Bucher145bf262021-02-26 14:51:55 -08001866static int
1867symtable_visit_match_case(struct symtable *st, match_case_ty m)
1868{
Nick Coghlan1e7b8582021-04-29 15:58:44 +10001869 VISIT(st, pattern, m->pattern);
Brandt Bucher145bf262021-02-26 14:51:55 -08001870 if (m->guard) {
1871 VISIT(st, expr, m->guard);
1872 }
1873 VISIT_SEQ(st, stmt, m->body);
1874 return 1;
1875}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001878symtable_visit_alias(struct symtable *st, alias_ty a)
1879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001881 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 dotted package name (e.g. spam.eggs)
1883 */
1884 PyObject *store_name;
1885 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001886 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1887 PyUnicode_GET_LENGTH(name), 1);
1888 if (dot != -1) {
1889 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 if (!store_name)
1891 return 0;
1892 }
1893 else {
1894 store_name = name;
1895 Py_INCREF(store_name);
1896 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001897 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1899 Py_DECREF(store_name);
1900 return r;
1901 }
1902 else {
1903 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001904 int lineno = st->st_cur->ste_lineno;
1905 int col_offset = st->st_cur->ste_col_offset;
Pablo Galindoa77aac42021-04-23 14:27:05 +01001906 int end_lineno = st->st_cur->ste_end_lineno;
1907 int end_col_offset = st->st_cur->ste_end_col_offset;
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001908 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Pablo Galindoa77aac42021-04-23 14:27:05 +01001909 PyErr_RangedSyntaxLocationObject(st->st_filename,
1910 lineno, col_offset + 1,
1911 end_lineno, end_col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001912 Py_DECREF(store_name);
1913 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915 Py_DECREF(store_name);
1916 return 1;
1917 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001918}
1919
1920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001921static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001922symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1923{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001924 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001925 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001926 st->st_cur->ste_comp_iter_target = 0;
1927 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001928 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001929 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001931 if (lc->is_async) {
1932 st->st_cur->ste_coroutine = 1;
1933 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001935}
1936
1937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001939symtable_visit_keyword(struct symtable *st, keyword_ty k)
1940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 VISIT(st, expr, k->value);
1942 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001943}
1944
1945
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001946static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001947symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01001948 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001949 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001952 comprehension_ty outermost = ((comprehension_ty)
1953 asdl_seq_GET(generators, 0));
1954 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001955 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001956 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001957 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 /* Create comprehension scope for the rest */
1959 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001960 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
Pablo Galindoa77aac42021-04-23 14:27:05 +01001961 e->lineno, e->col_offset,
1962 e->end_lineno, e->end_col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001963 return 0;
1964 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001965 if (outermost->is_async) {
1966 st->st_cur->ste_coroutine = 1;
1967 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001968 st->st_cur->ste_comprehension = 1;
1969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001970 /* Outermost iter is received as an argument */
1971 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06001972 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001973 return 0;
1974 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001975 /* Visit iteration variable target, and mark them as such */
1976 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001977 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001978 st->st_cur->ste_comp_iter_target = 0;
1979 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001980 VISIT_SEQ(st, expr, outermost->ifs);
1981 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001982 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001983 VISIT(st, expr, value);
1984 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001985 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001986 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001987 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1988 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1989 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1990 "'yield' inside generator expression");
Pablo Galindoa77aac42021-04-23 14:27:05 +01001991 PyErr_RangedSyntaxLocationObject(st->st_filename,
1992 st->st_cur->ste_lineno,
1993 st->st_cur->ste_col_offset + 1,
1994 st->st_cur->ste_end_lineno,
1995 st->st_cur->ste_end_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06001996 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001997 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001998 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001999 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06002000 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00002001}
Nick Coghlan650f0d02007-04-15 12:05:43 +00002002
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002003static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002004symtable_visit_genexp(struct symtable *st, expr_ty e)
2005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002006 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
2007 e->v.GeneratorExp.generators,
2008 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002009}
2010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002011static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00002012symtable_visit_listcomp(struct symtable *st, expr_ty e)
2013{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002014 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
2015 e->v.ListComp.generators,
2016 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002017}
2018
2019static int
2020symtable_visit_setcomp(struct symtable *st, expr_ty e)
2021{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002022 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
2023 e->v.SetComp.generators,
2024 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00002025}
2026
2027static int
2028symtable_visit_dictcomp(struct symtable *st, expr_ty e)
2029{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00002030 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
2031 e->v.DictComp.generators,
2032 e->v.DictComp.key,
2033 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00002034}
Victor Stinner28ad12f2021-03-19 12:41:49 +01002035
2036
2037struct symtable *
2038_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
2039 int start, PyCompilerFlags *flags)
2040{
2041 struct symtable *st;
2042 mod_ty mod;
2043 PyArena *arena;
2044
Victor Stinner8370e072021-03-24 02:23:01 +01002045 arena = _PyArena_New();
Victor Stinner28ad12f2021-03-19 12:41:49 +01002046 if (arena == NULL)
2047 return NULL;
2048
Victor Stinner57364ce2021-03-24 01:29:09 +01002049 mod = _PyParser_ASTFromString(str, filename, start, flags, arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002050 if (mod == NULL) {
Victor Stinner8370e072021-03-24 02:23:01 +01002051 _PyArena_Free(arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002052 return NULL;
2053 }
2054 st = _PySymtable_Build(mod, filename, 0);
Victor Stinner8370e072021-03-24 02:23:01 +01002055 _PyArena_Free(arena);
Victor Stinner28ad12f2021-03-19 12:41:49 +01002056 return st;
2057}