blob: 85648f21e68894e67ee47b86c20c17fc22e3d412 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner28ad12f2021-03-19 12:41:49 +01002#include "pycore_pystate.h" // _PyThreadState_GET()
3#include "pycore_symtable.h" // PySTEntryObject
Victor Stinner3bb183d2018-11-22 18:38:38 +01004#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner4a21e572020-04-15 02:35:41 +02005#include "structmember.h" // PyMemberDef
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00006
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02008#define GLOBAL_PARAM \
9"name '%U' is parameter and global"
10
11#define NONLOCAL_PARAM \
12"name '%U' is parameter and nonlocal"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070015"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070018"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070021"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000022
Jeremy Hylton81e95022007-02-27 06:50:52 +000023#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070024"name '%U' is used prior to nonlocal declaration"
25
26#define GLOBAL_ANNOT \
27"annotated name '%U' can't be global"
28
29#define NONLOCAL_ANNOT \
30"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000031
Neal Norwitz5d0ad502005-12-19 04:27:42 +000032#define IMPORT_STAR_WARNING "import * only allowed at module level"
33
Emily Morehouse8f59ee02019-01-24 16:49:56 -070034#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100035"assignment expression within a comprehension cannot be used in a class body"
36
37#define NAMED_EXPR_COMP_CONFLICT \
38"assignment expression cannot rebind comprehension iteration variable '%U'"
39
40#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
41"comprehension inner loop cannot rebind assignment expression target '%U'"
42
43#define NAMED_EXPR_COMP_ITER_EXPR \
44"assignment expression cannot be used in a comprehension iterable expression"
Emily Morehouse8f59ee02019-01-24 16:49:56 -070045
Neal Norwitz090b3dd2006-02-28 22:36:46 +000046static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000047ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000048 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020051 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 k = PyLong_FromVoidPtr(key);
54 if (k == NULL)
55 goto fail;
56 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020057 if (ste == NULL) {
58 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020062 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020065 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ste->ste_symbols = NULL;
68 ste->ste_varnames = NULL;
69 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Benjamin Petersond9c87022012-10-31 20:26:20 -040071 ste->ste_directives = NULL;
72
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 ste->ste_nested = 0;
75 ste->ste_free = 0;
76 ste->ste_varargs = 0;
77 ste->ste_varkeywords = 0;
78 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000079 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000081 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if (st->st_cur != NULL &&
84 (st->st_cur->ste_nested ||
85 st->st_cur->ste_type == FunctionBlock))
86 ste->ste_nested = 1;
87 ste->ste_child_free = 0;
88 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070089 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070090 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050092 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100093 ste->ste_comp_iter_target = 0;
94 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095
Victor Stinner9a4fb662013-07-11 22:49:00 +020096 ste->ste_symbols = PyDict_New();
97 ste->ste_varnames = PyList_New(0);
98 ste->ste_children = PyList_New(0);
99 if (ste->ste_symbols == NULL
100 || ste->ste_varnames == NULL
101 || ste->ste_children == NULL)
102 goto fail;
103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
105 goto fail;
106
107 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_XDECREF(ste);
110 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111}
112
113static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
117 ste->ste_name,
118 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119}
120
121static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 ste->ste_table = NULL;
125 Py_XDECREF(ste->ste_id);
126 Py_XDECREF(ste->ste_name);
127 Py_XDECREF(ste->ste_symbols);
128 Py_XDECREF(ste->ste_varnames);
129 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400130 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100131 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132}
133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000135
Guido van Rossum6f799372001-09-20 20:46:19 +0000136static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 {"id", T_OBJECT, OFF(ste_id), READONLY},
138 {"name", T_OBJECT, OFF(ste_name), READONLY},
139 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
140 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
141 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 {"nested", T_INT, OFF(ste_nested), READONLY},
143 {"type", T_INT, OFF(ste_type), READONLY},
144 {"lineno", T_INT, OFF(ste_lineno), READONLY},
145 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000146};
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
150 "symtable entry",
151 sizeof(PySTEntryObject),
152 0,
153 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200154 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 0, /* tp_getattr */
156 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200157 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 (reprfunc)ste_repr, /* tp_repr */
159 0, /* tp_as_number */
160 0, /* tp_as_sequence */
161 0, /* tp_as_mapping */
162 0, /* tp_hash */
163 0, /* tp_call */
164 0, /* tp_str */
165 PyObject_GenericGetAttr, /* tp_getattro */
166 0, /* tp_setattro */
167 0, /* tp_as_buffer */
168 Py_TPFLAGS_DEFAULT, /* tp_flags */
169 0, /* tp_doc */
170 0, /* tp_traverse */
171 0, /* tp_clear */
172 0, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
174 0, /* tp_iter */
175 0, /* tp_iternext */
176 0, /* tp_methods */
177 ste_memberlist, /* tp_members */
178 0, /* tp_getset */
179 0, /* tp_base */
180 0, /* tp_dict */
181 0, /* tp_descr_get */
182 0, /* tp_descr_set */
183 0, /* tp_dictoffset */
184 0, /* tp_init */
185 0, /* tp_alloc */
186 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000187};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000191 _Py_block_ty block, void *ast, int lineno,
192 int col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600193static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
195static int symtable_visit_expr(struct symtable *st, expr_ty s);
196static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000197static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
198static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000199static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200static int symtable_visit_arguments(struct symtable *st, arguments_ty);
201static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
202static int symtable_visit_alias(struct symtable *st, alias_ty);
203static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
204static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100205static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
206static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207static int symtable_implicit_arg(struct symtable *st, int pos);
Andy Lester95668422020-03-06 09:46:04 -0600208static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500209static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Brandt Bucher145bf262021-02-26 14:51:55 -0800210static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
212
Nick Coghlan650f0d02007-04-15 12:05:43 +0000213static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500215 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216
217#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219
220#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000221"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222
223static struct symtable *
224symtable_new(void)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700229 if (st == NULL) {
230 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 st->st_filename = NULL;
235 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if ((st->st_stack = PyList_New(0)) == NULL)
238 goto fail;
239 if ((st->st_blocks = PyDict_New()) == NULL)
240 goto fail;
241 st->st_cur = NULL;
242 st->st_private = NULL;
Brandt Bucher145bf262021-02-26 14:51:55 -0800243 st->in_pattern = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000245 fail:
Victor Stinner28ad12f2021-03-19 12:41:49 +0100246 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248}
249
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000250/* When compiling the use of C stack is probably going to be a lot
251 lighter than when executing Python code but still can overflow
252 and causing a Python crash if not checked (e.g. eval("()"*300000)).
253 Using the current recursion limit for the compiler seems too
254 restrictive (it caused at least one test to fail) so a factor is
255 used to allow deeper recursion when compiling an expression.
256
257 Using a scaling factor means this should automatically adjust when
258 the recursion limit is adjusted for small or large C stack allocations.
259*/
260#define COMPILER_STACK_FRAME_SCALE 3
261
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262struct symtable *
Victor Stinner28ad12f2021-03-19 12:41:49 +0100263_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000265 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100266 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000267 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000268 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400269 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000270 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000271
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200273 return NULL;
274 if (filename == NULL) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100275 _PySymtable_Free(st);
Victor Stinner14e461d2013-08-26 22:28:21 +0200276 return NULL;
277 }
278 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 st->st_filename = filename;
280 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000281
282 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100283 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000284 if (!tstate) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100285 _PySymtable_Free(st);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000286 return NULL;
287 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400288 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000289 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400290 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000291 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400292 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
293 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000294
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 /* Make the initial symbol information gathering pass */
296 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000297 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100298 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 return NULL;
300 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000301
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000302 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 switch (mod->kind) {
304 case Module_kind:
305 seq = mod->v.Module.body;
306 for (i = 0; i < asdl_seq_LEN(seq); i++)
307 if (!symtable_visit_stmt(st,
308 (stmt_ty)asdl_seq_GET(seq, i)))
309 goto error;
310 break;
311 case Expression_kind:
312 if (!symtable_visit_expr(st, mod->v.Expression.body))
313 goto error;
314 break;
315 case Interactive_kind:
316 seq = mod->v.Interactive.body;
317 for (i = 0; i < asdl_seq_LEN(seq); i++)
318 if (!symtable_visit_stmt(st,
319 (stmt_ty)asdl_seq_GET(seq, i)))
320 goto error;
321 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800322 case FunctionType_kind:
323 PyErr_SetString(PyExc_RuntimeError,
324 "this compiler does not handle FunctionTypes");
325 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 }
Andy Lester95668422020-03-06 09:46:04 -0600327 if (!symtable_exit_block(st)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100328 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 return NULL;
330 }
Nick Coghlan06145232019-08-29 23:26:53 +1000331 /* Check that the recursion depth counting balanced correctly */
332 if (st->recursion_depth != starting_recursion_depth) {
333 PyErr_Format(PyExc_SystemError,
334 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
335 starting_recursion_depth, st->recursion_depth);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100336 _PySymtable_Free(st);
Nick Coghlan06145232019-08-29 23:26:53 +1000337 return NULL;
338 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 /* Make the second symbol analysis pass */
340 if (symtable_analyze(st))
341 return st;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100342 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344 error:
Andy Lester95668422020-03-06 09:46:04 -0600345 (void) symtable_exit_block(st);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100346 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000347 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348}
349
Victor Stinner14e461d2013-08-26 22:28:21 +0200350
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000351void
Victor Stinner28ad12f2021-03-19 12:41:49 +0100352_PySymtable_Free(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353{
Victor Stinner14e461d2013-08-26 22:28:21 +0200354 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000355 Py_XDECREF(st->st_blocks);
356 Py_XDECREF(st->st_stack);
357 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358}
359
360PySTEntryObject *
361PySymtable_Lookup(struct symtable *st, void *key)
362{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000363 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000364
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 k = PyLong_FromVoidPtr(key);
366 if (k == NULL)
367 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200368 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000369 if (v) {
370 assert(PySTEntry_Check(v));
371 Py_INCREF(v);
372 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200373 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 PyErr_SetString(PyExc_KeyError,
375 "unknown symbol table entry");
376 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000377
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000378 Py_DECREF(k);
379 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000380}
381
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000382static long
383_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200385 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 if (!v)
387 return 0;
388 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000389 return PyLong_AS_LONG(v);
390}
391
392int
Victor Stinner28ad12f2021-03-19 12:41:49 +0100393_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000394{
395 long symbol = _PyST_GetSymbol(ste, name);
396 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000397}
398
Benjamin Petersond9c87022012-10-31 20:26:20 -0400399static int
400error_at_directive(PySTEntryObject *ste, PyObject *name)
401{
402 Py_ssize_t i;
403 PyObject *data;
404 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600405 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400406 data = PyList_GET_ITEM(ste->ste_directives, i);
407 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600408 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
409 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
410 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
411 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400412 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600413
414 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700415 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400416 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600417 PyErr_SetString(PyExc_RuntimeError,
418 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400419 return 0;
420}
421
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000422
423/* Analyze raw symbol information to determine scope of each name.
424
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000425 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431 explicit global is declared with the global statement. An implicit
432 global is a free variable for which the compiler has found no binding
433 in an enclosing function scope. The implicit global is either a global
434 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
435 to handle these names to implement slightly odd semantics. In such a
436 block, the name is treated as global until it is assigned to; then it
437 is treated as a local.
438
439 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000440 The first pass collects raw facts from the AST via the symtable_visit_*
441 functions: the name is a parameter here, the name is used but not defined
442 here, etc. The second pass analyzes these facts during a pass over the
443 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000444
445 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000446 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000447 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000448 Names which are explicitly declared nonlocal must exist in this set of
449 visible names - if they do not, a syntax error is raised. After doing
450 the local analysis, it analyzes each of its child blocks using an
451 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000452
Nick Coghlan650f0d02007-04-15 12:05:43 +0000453 The children update the free variable set. If a local variable is added to
454 the free variable set by the child, the variable is marked as a cell. The
455 function object being defined must provide runtime storage for the variable
456 that may outlive the function's frame. Cell variables are removed from the
457 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000458
Nick Coghlan650f0d02007-04-15 12:05:43 +0000459 During analysis, the names are:
460 symbols: dict mapping from symbol names to flag values (including offset scope values)
461 scopes: dict mapping from symbol names to scope values (no offset)
462 local: set of all symbol names local to the current scope
463 bound: set of all symbol names local to a containing function scope
464 free: set of all symbol names referenced but not bound in child scopes
465 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000466*/
467
468#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyObject *o = PyLong_FromLong(I); \
470 if (!o) \
471 return 0; \
472 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
473 Py_DECREF(o); \
474 return 0; \
475 } \
476 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477}
478
479/* Decide on scope of name, given flags.
480
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000481 The namespace dictionaries may be modified to record information
482 about the new name. For example, a new global will add an entry to
483 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000484*/
485
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000486static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000487analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488 PyObject *bound, PyObject *local, PyObject *free,
489 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000490{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 if (flags & DEF_NONLOCAL) {
493 PyErr_Format(PyExc_SyntaxError,
494 "name '%U' is nonlocal and global",
495 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400496 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
498 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
499 if (PySet_Add(global, name) < 0)
500 return 0;
501 if (bound && (PySet_Discard(bound, name) < 0))
502 return 0;
503 return 1;
504 }
505 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (!bound) {
507 PyErr_Format(PyExc_SyntaxError,
508 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400509 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 }
511 if (!PySet_Contains(bound, name)) {
512 PyErr_Format(PyExc_SyntaxError,
513 "no binding for nonlocal '%U' found",
514 name);
515
Benjamin Petersond9c87022012-10-31 20:26:20 -0400516 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000517 }
518 SET_SCOPE(scopes, name, FREE);
519 ste->ste_free = 1;
520 return PySet_Add(free, name) >= 0;
521 }
522 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000523 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000524 if (PySet_Add(local, name) < 0)
525 return 0;
526 if (PySet_Discard(global, name) < 0)
527 return 0;
528 return 1;
529 }
530 /* If an enclosing block has a binding for this name, it
531 is a free variable rather than a global variable.
532 Note that having a non-NULL bound implies that the block
533 is nested.
534 */
535 if (bound && PySet_Contains(bound, name)) {
536 SET_SCOPE(scopes, name, FREE);
537 ste->ste_free = 1;
538 return PySet_Add(free, name) >= 0;
539 }
540 /* If a parent has a global statement, then call it global
541 explicit? It could also be global implicit.
542 */
543 if (global && PySet_Contains(global, name)) {
544 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
545 return 1;
546 }
547 if (ste->ste_nested)
548 ste->ste_free = 1;
549 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
550 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000551}
552
553#undef SET_SCOPE
554
555/* If a name is defined in free and also in locals, then this block
556 provides the binding for the free variable. The name should be
557 marked CELL in this block and removed from the free list.
558
559 Note that the current block's free variables are included in free.
560 That's safe because no name can be free and local in the same scope.
561*/
562
563static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500564analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000565{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 PyObject *name, *v, *v_cell;
567 int success = 0;
568 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000569
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 v_cell = PyLong_FromLong(CELL);
571 if (!v_cell)
572 return 0;
573 while (PyDict_Next(scopes, &pos, &name, &v)) {
574 long scope;
575 assert(PyLong_Check(v));
576 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000577 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 continue;
579 if (!PySet_Contains(free, name))
580 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000581 /* Replace LOCAL with CELL for this name, and remove
582 from free. It is safe to replace the value of name
583 in the dict, because it will not cause a resize.
584 */
585 if (PyDict_SetItem(scopes, name, v_cell) < 0)
586 goto error;
587 if (PySet_Discard(free, name) < 0)
588 goto error;
589 }
590 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000592 Py_DECREF(v_cell);
593 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000594}
595
Benjamin Peterson312595c2013-05-15 15:26:42 -0500596static int
597drop_class_free(PySTEntryObject *ste, PyObject *free)
598{
599 int res;
600 if (!GET_IDENTIFIER(__class__))
601 return 0;
602 res = PySet_Discard(free, __class__);
603 if (res < 0)
604 return 0;
605 if (res)
606 ste->ste_needs_class_closure = 1;
607 return 1;
608}
609
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610/* Enter the final scope information into the ste_symbols dict.
611 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000612 * All arguments are dicts. Modifies symbols, others are read-only.
613*/
614static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000616 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000618 PyObject *name = NULL, *itr = NULL;
619 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
620 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622 /* Update scope information for all symbols in this scope */
623 while (PyDict_Next(symbols, &pos, &name, &v)) {
624 long scope, flags;
625 assert(PyLong_Check(v));
626 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200627 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 assert(v_scope && PyLong_Check(v_scope));
629 scope = PyLong_AS_LONG(v_scope);
630 flags |= (scope << SCOPE_OFFSET);
631 v_new = PyLong_FromLong(flags);
632 if (!v_new)
633 return 0;
634 if (PyDict_SetItem(symbols, name, v_new) < 0) {
635 Py_DECREF(v_new);
636 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 Py_DECREF(v_new);
639 }
640
641 /* Record not yet resolved free variables from children (if any) */
642 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
643 if (!v_free)
644 return 0;
645
646 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600647 if (itr == NULL) {
648 Py_DECREF(v_free);
649 return 0;
650 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000651
652 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200653 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000654
655 /* Handle symbol that already exists in this scope */
656 if (v) {
657 /* Handle a free variable in a method of
658 the class that has the same name as a local
659 or global in the class scope.
660 */
661 if (classflag &&
662 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
663 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
664 v_new = PyLong_FromLong(flags);
665 if (!v_new) {
666 goto error;
667 }
668 if (PyDict_SetItem(symbols, name, v_new) < 0) {
669 Py_DECREF(v_new);
670 goto error;
671 }
672 Py_DECREF(v_new);
673 }
674 /* It's a cell, or already free in this scope */
675 Py_DECREF(name);
676 continue;
677 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200678 else if (PyErr_Occurred()) {
679 goto error;
680 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000681 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200682 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 Py_DECREF(name);
684 continue; /* it's a global */
685 }
686 /* Propagate new free symbol up the lexical stack */
687 if (PyDict_SetItem(symbols, name, v_free) < 0) {
688 goto error;
689 }
690 Py_DECREF(name);
691 }
692 Py_DECREF(itr);
693 Py_DECREF(v_free);
694 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000695error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 Py_XDECREF(v_free);
697 Py_XDECREF(itr);
698 Py_XDECREF(name);
699 return 0;
700}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000701
702/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000703
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704 Arguments:
705 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000706 bound -- set of variables bound in enclosing scopes (input). bound
707 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000708 free -- set of free variables in enclosed scopes (output)
709 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000710
711 The implementation uses two mutually recursive functions,
712 analyze_block() and analyze_child_block(). analyze_block() is
713 responsible for analyzing the individual names defined in a block.
714 analyze_child_block() prepares temporary namespace dictionaries
715 used to evaluated nested blocks.
716
717 The two functions exist because a child block should see the name
718 bindings of its enclosing blocks, but those bindings should not
719 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720*/
721
722static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000723analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
724 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000725
726static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
728 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000729{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
731 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
732 PyObject *temp;
733 int i, success = 0;
734 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000735
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000736 local = PySet_New(NULL); /* collect new names bound in block */
737 if (!local)
738 goto error;
739 scopes = PyDict_New(); /* collect scopes defined for each name */
740 if (!scopes)
741 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 /* Allocate new global and bound variable dictionaries. These
744 dictionaries hold the names visible in nested blocks. For
745 ClassBlocks, the bound and global names are initialized
746 before analyzing names, because class bindings aren't
747 visible in methods. For other blocks, they are initialized
748 after names are analyzed.
749 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000751 /* TODO(jhylton): Package these dicts in a struct so that we
752 can write reasonable helper functions?
753 */
754 newglobal = PySet_New(NULL);
755 if (!newglobal)
756 goto error;
757 newfree = PySet_New(NULL);
758 if (!newfree)
759 goto error;
760 newbound = PySet_New(NULL);
761 if (!newbound)
762 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 /* Class namespace has no effect on names visible in
765 nested functions, so populate the global and bound
766 sets to be passed to child blocks before analyzing
767 this one.
768 */
769 if (ste->ste_type == ClassBlock) {
770 /* Pass down known globals */
771 temp = PyNumber_InPlaceOr(newglobal, global);
772 if (!temp)
773 goto error;
774 Py_DECREF(temp);
775 /* Pass down previously bound symbols */
776 if (bound) {
777 temp = PyNumber_InPlaceOr(newbound, bound);
778 if (!temp)
779 goto error;
780 Py_DECREF(temp);
781 }
782 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000783
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
785 long flags = PyLong_AS_LONG(v);
786 if (!analyze_name(ste, scopes, name, flags,
787 bound, local, free, global))
788 goto error;
789 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000790
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000791 /* Populate global and bound sets to be passed to children. */
792 if (ste->ste_type != ClassBlock) {
793 /* Add function locals to bound set */
794 if (ste->ste_type == FunctionBlock) {
795 temp = PyNumber_InPlaceOr(newbound, local);
796 if (!temp)
797 goto error;
798 Py_DECREF(temp);
799 }
800 /* Pass down previously bound symbols */
801 if (bound) {
802 temp = PyNumber_InPlaceOr(newbound, bound);
803 if (!temp)
804 goto error;
805 Py_DECREF(temp);
806 }
807 /* Pass down known globals */
808 temp = PyNumber_InPlaceOr(newglobal, global);
809 if (!temp)
810 goto error;
811 Py_DECREF(temp);
812 }
813 else {
814 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000815 if (!GET_IDENTIFIER(__class__))
816 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000817 if (PySet_Add(newbound, __class__) < 0)
818 goto error;
819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300821 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 newbound, newglobal now contain the names visible in
824 nested blocks. The free variables in the children will
825 be collected in allfree.
826 */
827 allfree = PySet_New(NULL);
828 if (!allfree)
829 goto error;
830 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
831 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
832 PySTEntryObject* entry;
833 assert(c && PySTEntry_Check(c));
834 entry = (PySTEntryObject*)c;
835 if (!analyze_child_block(entry, newbound, newfree, newglobal,
836 allfree))
837 goto error;
838 /* Check if any children have free variables */
839 if (entry->ste_free || entry->ste_child_free)
840 ste->ste_child_free = 1;
841 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000842
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000843 temp = PyNumber_InPlaceOr(newfree, allfree);
844 if (!temp)
845 goto error;
846 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500849 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500851 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 goto error;
853 /* Records the results of the analysis in the symbol table entry */
854 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
855 ste->ste_type == ClassBlock))
856 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 temp = PyNumber_InPlaceOr(free, newfree);
859 if (!temp)
860 goto error;
861 Py_DECREF(temp);
862 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000863 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 Py_XDECREF(scopes);
865 Py_XDECREF(local);
866 Py_XDECREF(newbound);
867 Py_XDECREF(newglobal);
868 Py_XDECREF(newfree);
869 Py_XDECREF(allfree);
870 if (!success)
871 assert(PyErr_Occurred());
872 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873}
874
875static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
877 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000878{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000879 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
880 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000881
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000882 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000883
Martin Panter3ee62702016-06-04 04:57:19 +0000884 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 current block. The analyze_block() call modifies these
886 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 */
889 temp_bound = PySet_New(bound);
890 if (!temp_bound)
891 goto error;
892 temp_free = PySet_New(free);
893 if (!temp_free)
894 goto error;
895 temp_global = PySet_New(global);
896 if (!temp_global)
897 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000899 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
900 goto error;
901 temp = PyNumber_InPlaceOr(child_free, temp_free);
902 if (!temp)
903 goto error;
904 Py_DECREF(temp);
905 Py_DECREF(temp_bound);
906 Py_DECREF(temp_free);
907 Py_DECREF(temp_global);
908 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000909 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 Py_XDECREF(temp_bound);
911 Py_XDECREF(temp_free);
912 Py_XDECREF(temp_global);
913 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000914}
915
916static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000917symtable_analyze(struct symtable *st)
918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 PyObject *free, *global;
920 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 free = PySet_New(NULL);
923 if (!free)
924 return 0;
925 global = PySet_New(NULL);
926 if (!global) {
927 Py_DECREF(free);
928 return 0;
929 }
930 r = analyze_block(st->st_top, NULL, free, global);
931 Py_DECREF(free);
932 Py_DECREF(global);
933 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000934}
935
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000936/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000937 This reference is released when the block is exited, via the DECREF
938 in symtable_exit_block().
939*/
940
941static int
Andy Lester95668422020-03-06 09:46:04 -0600942symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000943{
Benjamin Peterson609da582011-06-29 22:52:39 -0500944 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945
Benjamin Peterson609da582011-06-29 22:52:39 -0500946 st->st_cur = NULL;
947 size = PyList_GET_SIZE(st->st_stack);
948 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500949 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000950 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500951 if (--size)
952 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 }
954 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955}
956
957static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000959 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960{
Benjamin Peterson609da582011-06-29 22:52:39 -0500961 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962
Benjamin Peterson609da582011-06-29 22:52:39 -0500963 ste = ste_new(st, name, block, ast, lineno, col_offset);
964 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500966 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
967 Py_DECREF(ste);
968 return 0;
969 }
970 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000971 /* bpo-37757: For now, disallow *all* assignment expressions in the
972 * outermost iterator expression of a comprehension, even those inside
973 * a nested comprehension or a lambda expression.
974 */
975 if (prev) {
976 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
977 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500978 /* The entry is owned by the stack. Borrow it for st_cur. */
979 Py_DECREF(ste);
980 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000981 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000982 st->st_global = st->st_cur->ste_symbols;
983 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500984 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 return 0;
986 }
987 }
988 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989}
990
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000991static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000992symtable_lookup(struct symtable *st, PyObject *name)
993{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 PyObject *mangled = _Py_Mangle(st->st_private, name);
995 if (!mangled)
996 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +0100997 long ret = _PyST_GetSymbol(st->st_cur, mangled);
998 Py_DECREF(mangled);
999 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001000}
1001
1002static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001003symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 PyObject *o;
1006 PyObject *dict;
1007 long val;
1008 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009
Jeremy Hylton81e95022007-02-27 06:50:52 +00001010
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 if (!mangled)
1012 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001013 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001014 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 val = PyLong_AS_LONG(o);
1016 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1017 /* Is it better to use 'mangled' or 'name' here? */
1018 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001019 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001020 ste->ste_lineno,
1021 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 goto error;
1023 }
1024 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001025 }
1026 else if (PyErr_Occurred()) {
1027 goto error;
1028 }
1029 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001030 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001031 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001032 if (ste->ste_comp_iter_target) {
1033 /* This name is an iteration variable in a comprehension,
1034 * so check for a binding conflict with any named expressions.
1035 * Otherwise, mark it as an iteration variable so subsequent
1036 * named expressions can check for conflicts.
1037 */
1038 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1039 PyErr_Format(PyExc_SyntaxError,
1040 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1041 PyErr_SyntaxLocationObject(st->st_filename,
1042 ste->ste_lineno,
1043 ste->ste_col_offset + 1);
1044 goto error;
1045 }
1046 val |= DEF_COMP_ITER;
1047 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001048 o = PyLong_FromLong(val);
1049 if (o == NULL)
1050 goto error;
1051 if (PyDict_SetItem(dict, mangled, o) < 0) {
1052 Py_DECREF(o);
1053 goto error;
1054 }
1055 Py_DECREF(o);
1056
1057 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001058 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001059 goto error;
1060 } else if (flag & DEF_GLOBAL) {
1061 /* XXX need to update DEF_GLOBAL for other flags too;
1062 perhaps only DEF_FREE_GLOBAL */
1063 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001064 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001065 val |= PyLong_AS_LONG(o);
1066 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001067 else if (PyErr_Occurred()) {
1068 goto error;
1069 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 goto error;
1073 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1074 Py_DECREF(o);
1075 goto error;
1076 }
1077 Py_DECREF(o);
1078 }
1079 Py_DECREF(mangled);
1080 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001081
1082error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 Py_DECREF(mangled);
1084 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085}
1086
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001087static int
1088symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1089 return symtable_add_def_helper(st, name, flag, st->st_cur);
1090}
1091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1093 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 function.
1095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1097 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001098
1099 VISIT_QUIT macro returns the specified value exiting from the function but
1100 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101*/
1102
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001103#define VISIT_QUIT(ST, X) \
1104 return --(ST)->recursion_depth,(X)
1105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001108 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001112 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1114 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1115 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001116 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001122 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1124 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1125 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001126 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001129
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001130#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001132 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001134 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001136 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001137 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001139}
1140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001142symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001143{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001144 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001145 int res;
1146 if (!st->st_cur->ste_directives) {
1147 st->st_cur->ste_directives = PyList_New(0);
1148 if (!st->st_cur->ste_directives)
1149 return 0;
1150 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001151 mangled = _Py_Mangle(st->st_private, name);
1152 if (!mangled)
1153 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001154 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001155 if (!data)
1156 return 0;
1157 res = PyList_Append(st->st_cur->ste_directives, data);
1158 Py_DECREF(data);
1159 return res == 0;
1160}
1161
1162
1163static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164symtable_visit_stmt(struct symtable *st, stmt_ty s)
1165{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001166 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001167 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001168 "maximum recursion depth exceeded during compilation");
1169 VISIT_QUIT(st, 0);
1170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 switch (s->kind) {
1172 case FunctionDef_kind:
1173 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001174 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 if (s->v.FunctionDef.args->defaults)
1176 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1177 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001178 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001179 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001180 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001181 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (s->v.FunctionDef.decorator_list)
1183 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1184 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001185 FunctionBlock, (void *)s, s->lineno,
1186 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001187 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001188 VISIT(st, arguments, s->v.FunctionDef.args);
1189 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001190 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001191 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 break;
1193 case ClassDef_kind: {
1194 PyObject *tmp;
1195 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001196 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1198 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (s->v.ClassDef.decorator_list)
1200 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1201 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001202 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001203 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 tmp = st->st_private;
1205 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001206 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001208 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001209 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 break;
1211 }
1212 case Return_kind:
1213 if (s->v.Return.value) {
1214 VISIT(st, expr, s->v.Return.value);
1215 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 }
1217 break;
1218 case Delete_kind:
1219 VISIT_SEQ(st, expr, s->v.Delete.targets);
1220 break;
1221 case Assign_kind:
1222 VISIT_SEQ(st, expr, s->v.Assign.targets);
1223 VISIT(st, expr, s->v.Assign.value);
1224 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001225 case AnnAssign_kind:
1226 if (s->v.AnnAssign.target->kind == Name_kind) {
1227 expr_ty e_name = s->v.AnnAssign.target;
1228 long cur = symtable_lookup(st, e_name->v.Name.id);
1229 if (cur < 0) {
1230 VISIT_QUIT(st, 0);
1231 }
1232 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001233 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001234 && s->v.AnnAssign.simple) {
1235 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001236 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1237 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001238 PyErr_SyntaxLocationObject(st->st_filename,
1239 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001240 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001241 VISIT_QUIT(st, 0);
1242 }
1243 if (s->v.AnnAssign.simple &&
1244 !symtable_add_def(st, e_name->v.Name.id,
1245 DEF_ANNOT | DEF_LOCAL)) {
1246 VISIT_QUIT(st, 0);
1247 }
1248 else {
1249 if (s->v.AnnAssign.value
1250 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1251 VISIT_QUIT(st, 0);
1252 }
1253 }
1254 }
1255 else {
1256 VISIT(st, expr, s->v.AnnAssign.target);
1257 }
1258 VISIT(st, expr, s->v.AnnAssign.annotation);
1259 if (s->v.AnnAssign.value) {
1260 VISIT(st, expr, s->v.AnnAssign.value);
1261 }
1262 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 case AugAssign_kind:
1264 VISIT(st, expr, s->v.AugAssign.target);
1265 VISIT(st, expr, s->v.AugAssign.value);
1266 break;
1267 case For_kind:
1268 VISIT(st, expr, s->v.For.target);
1269 VISIT(st, expr, s->v.For.iter);
1270 VISIT_SEQ(st, stmt, s->v.For.body);
1271 if (s->v.For.orelse)
1272 VISIT_SEQ(st, stmt, s->v.For.orelse);
1273 break;
1274 case While_kind:
1275 VISIT(st, expr, s->v.While.test);
1276 VISIT_SEQ(st, stmt, s->v.While.body);
1277 if (s->v.While.orelse)
1278 VISIT_SEQ(st, stmt, s->v.While.orelse);
1279 break;
1280 case If_kind:
1281 /* XXX if 0: and lookup_yield() hacks */
1282 VISIT(st, expr, s->v.If.test);
1283 VISIT_SEQ(st, stmt, s->v.If.body);
1284 if (s->v.If.orelse)
1285 VISIT_SEQ(st, stmt, s->v.If.orelse);
1286 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001287 case Match_kind:
1288 VISIT(st, expr, s->v.Match.subject);
1289 VISIT_SEQ(st, match_case, s->v.Match.cases);
1290 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001291 case Raise_kind:
1292 if (s->v.Raise.exc) {
1293 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001294 if (s->v.Raise.cause) {
1295 VISIT(st, expr, s->v.Raise.cause);
1296 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001297 }
1298 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001299 case Try_kind:
1300 VISIT_SEQ(st, stmt, s->v.Try.body);
1301 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1302 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1303 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001304 break;
1305 case Assert_kind:
1306 VISIT(st, expr, s->v.Assert.test);
1307 if (s->v.Assert.msg)
1308 VISIT(st, expr, s->v.Assert.msg);
1309 break;
1310 case Import_kind:
1311 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 break;
1313 case ImportFrom_kind:
1314 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 break;
1316 case Global_kind: {
1317 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001318 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1320 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 long cur = symtable_lookup(st, name);
1322 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001323 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001324 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1325 const char* msg;
1326 if (cur & DEF_PARAM) {
1327 msg = GLOBAL_PARAM;
1328 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001329 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001330 } else if (cur & DEF_ANNOT) {
1331 msg = GLOBAL_ANNOT;
1332 } else { /* DEF_LOCAL */
1333 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001334 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001335 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001336 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001337 PyErr_SyntaxLocationObject(st->st_filename,
1338 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001339 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001340 VISIT_QUIT(st, 0);
1341 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001343 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001344 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001345 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 }
1347 break;
1348 }
1349 case Nonlocal_kind: {
1350 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001351 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1353 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 long cur = symtable_lookup(st, name);
1355 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001356 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001357 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1358 const char* msg;
1359 if (cur & DEF_PARAM) {
1360 msg = NONLOCAL_PARAM;
1361 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001362 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001363 } else if (cur & DEF_ANNOT) {
1364 msg = NONLOCAL_ANNOT;
1365 } else { /* DEF_LOCAL */
1366 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001367 }
1368 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001369 PyErr_SyntaxLocationObject(st->st_filename,
1370 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001371 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001372 VISIT_QUIT(st, 0);
1373 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001375 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001376 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001377 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 }
1379 break;
1380 }
1381 case Expr_kind:
1382 VISIT(st, expr, s->v.Expr.value);
1383 break;
1384 case Pass_kind:
1385 case Break_kind:
1386 case Continue_kind:
1387 /* nothing to do here */
1388 break;
1389 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001390 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 VISIT_SEQ(st, stmt, s->v.With.body);
1392 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001393 case AsyncFunctionDef_kind:
1394 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1395 VISIT_QUIT(st, 0);
1396 if (s->v.AsyncFunctionDef.args->defaults)
1397 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1398 if (s->v.AsyncFunctionDef.args->kw_defaults)
1399 VISIT_SEQ_WITH_NULL(st, expr,
1400 s->v.AsyncFunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001401 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001402 s->v.AsyncFunctionDef.returns))
1403 VISIT_QUIT(st, 0);
1404 if (s->v.AsyncFunctionDef.decorator_list)
1405 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1406 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1407 FunctionBlock, (void *)s, s->lineno,
1408 s->col_offset))
1409 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001410 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001411 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1412 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001413 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001414 VISIT_QUIT(st, 0);
1415 break;
1416 case AsyncWith_kind:
1417 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1418 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1419 break;
1420 case AsyncFor_kind:
1421 VISIT(st, expr, s->v.AsyncFor.target);
1422 VISIT(st, expr, s->v.AsyncFor.iter);
1423 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1424 if (s->v.AsyncFor.orelse)
1425 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1426 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001428 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001429}
1430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001432symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1433{
1434 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001435 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001436
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001437 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001438 Py_ssize_t i, size;
1439 struct _symtable_entry *ste;
1440 size = PyList_GET_SIZE(st->st_stack);
1441 assert(size);
1442
1443 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1444 for (i = size - 1; i >= 0; i--) {
1445 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1446
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001447 /* If we find a comprehension scope, check for a target
1448 * binding conflict with iteration variables, otherwise skip it
1449 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001450 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001451 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1452 if (target_in_scope & DEF_COMP_ITER) {
1453 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1454 PyErr_SyntaxLocationObject(st->st_filename,
1455 e->lineno,
1456 e->col_offset);
1457 VISIT_QUIT(st, 0);
1458 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001459 continue;
1460 }
1461
Pablo Galindofd5c4142019-10-14 05:18:05 +01001462 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001463 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001464 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1465 if (target_in_scope & DEF_GLOBAL) {
1466 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1467 VISIT_QUIT(st, 0);
1468 } else {
1469 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1470 VISIT_QUIT(st, 0);
1471 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001472 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001473 VISIT_QUIT(st, 0);
1474
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001475 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001476 }
1477 /* If we find a ModuleBlock entry, add as GLOBAL */
1478 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001479 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001480 VISIT_QUIT(st, 0);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001481 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001482 VISIT_QUIT(st, 0);
1483
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001484 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001485 }
1486 /* Disallow usage in ClassBlock */
1487 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001488 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001489 PyErr_SyntaxLocationObject(st->st_filename,
1490 e->lineno,
1491 e->col_offset);
1492 VISIT_QUIT(st, 0);
1493 }
1494 }
1495
1496 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1497 and should never fall to this case
1498 */
1499 assert(0);
1500 return 0;
1501}
1502
1503static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001504symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1505{
1506 if (st->st_cur->ste_comp_iter_expr > 0) {
1507 /* Assignment isn't allowed in a comprehension iterable expression */
1508 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1509 PyErr_SyntaxLocationObject(st->st_filename,
1510 e->lineno,
1511 e->col_offset);
Nick Coghlan06145232019-08-29 23:26:53 +10001512 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001513 }
1514 if (st->st_cur->ste_comprehension) {
1515 /* Inside a comprehension body, so find the right target scope */
1516 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001517 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001518 }
1519 VISIT(st, expr, e->v.NamedExpr.value);
1520 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001521 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001522}
1523
1524static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525symtable_visit_expr(struct symtable *st, expr_ty e)
1526{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001527 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001528 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001529 "maximum recursion depth exceeded during compilation");
1530 VISIT_QUIT(st, 0);
1531 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001532 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001533 case NamedExpr_kind:
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001534 if(!symtable_handle_namedexpr(st, e))
1535 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001536 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537 case BoolOp_kind:
1538 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1539 break;
1540 case BinOp_kind:
1541 VISIT(st, expr, e->v.BinOp.left);
1542 VISIT(st, expr, e->v.BinOp.right);
1543 break;
1544 case UnaryOp_kind:
1545 VISIT(st, expr, e->v.UnaryOp.operand);
1546 break;
1547 case Lambda_kind: {
1548 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001549 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 if (e->v.Lambda.args->defaults)
1551 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001552 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001553 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001555 FunctionBlock, (void *)e, e->lineno,
1556 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001557 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001558 VISIT(st, arguments, e->v.Lambda.args);
1559 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001560 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001561 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 break;
1563 }
1564 case IfExp_kind:
1565 VISIT(st, expr, e->v.IfExp.test);
1566 VISIT(st, expr, e->v.IfExp.body);
1567 VISIT(st, expr, e->v.IfExp.orelse);
1568 break;
1569 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001570 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 VISIT_SEQ(st, expr, e->v.Dict.values);
1572 break;
1573 case Set_kind:
1574 VISIT_SEQ(st, expr, e->v.Set.elts);
1575 break;
1576 case GeneratorExp_kind:
1577 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001578 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 break;
1580 case ListComp_kind:
1581 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001582 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 break;
1584 case SetComp_kind:
1585 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001586 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 break;
1588 case DictComp_kind:
1589 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001590 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591 break;
1592 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001593 if (e->v.Yield.value)
1594 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001597 case YieldFrom_kind:
1598 VISIT(st, expr, e->v.YieldFrom.value);
1599 st->st_cur->ste_generator = 1;
1600 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001601 case Await_kind:
1602 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001603 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001604 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 case Compare_kind:
1606 VISIT(st, expr, e->v.Compare.left);
1607 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1608 break;
1609 case Call_kind:
1610 VISIT(st, expr, e->v.Call.func);
1611 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001612 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001613 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001614 case FormattedValue_kind:
1615 VISIT(st, expr, e->v.FormattedValue.value);
1616 if (e->v.FormattedValue.format_spec)
1617 VISIT(st, expr, e->v.FormattedValue.format_spec);
1618 break;
1619 case JoinedStr_kind:
1620 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1621 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001622 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* Nothing to do here. */
1624 break;
1625 /* The following exprs can be assignment targets. */
1626 case Attribute_kind:
1627 VISIT(st, expr, e->v.Attribute.value);
1628 break;
1629 case Subscript_kind:
1630 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001631 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 break;
1633 case Starred_kind:
1634 VISIT(st, expr, e->v.Starred.value);
1635 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001636 case Slice_kind:
1637 if (e->v.Slice.lower)
1638 VISIT(st, expr, e->v.Slice.lower)
1639 if (e->v.Slice.upper)
1640 VISIT(st, expr, e->v.Slice.upper)
1641 if (e->v.Slice.step)
1642 VISIT(st, expr, e->v.Slice.step)
1643 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 case Name_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08001645 // Don't make "_" a local when used in a pattern:
1646 if (st->in_pattern &&
1647 e->v.Name.ctx == Store &&
1648 _PyUnicode_EqualToASCIIString(e->v.Name.id, "_"))
1649 {
1650 break;
1651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 if (!symtable_add_def(st, e->v.Name.id,
1653 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001654 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 /* Special-case super: it counts as a use of __class__ */
1656 if (e->v.Name.ctx == Load &&
1657 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001658 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001659 if (!GET_IDENTIFIER(__class__) ||
1660 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001661 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662 }
1663 break;
1664 /* child nodes of List and Tuple will have expr_context set */
1665 case List_kind:
1666 VISIT_SEQ(st, expr, e->v.List.elts);
1667 break;
1668 case Tuple_kind:
1669 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1670 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001671 case MatchAs_kind:
1672 VISIT(st, expr, e->v.MatchAs.pattern);
1673 symtable_add_def(st, e->v.MatchAs.name, DEF_LOCAL);
1674 break;
1675 case MatchOr_kind:
1676 VISIT_SEQ(st, expr, e->v.MatchOr.patterns);
1677 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001679 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680}
1681
1682static int
1683symtable_implicit_arg(struct symtable *st, int pos)
1684{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001685 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1686 if (id == NULL)
1687 return 0;
1688 if (!symtable_add_def(st, id, DEF_PARAM)) {
1689 Py_DECREF(id);
1690 return 0;
1691 }
1692 Py_DECREF(id);
1693 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001694}
1695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001697symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001698{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 if (!args)
1702 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 for (i = 0; i < asdl_seq_LEN(args); i++) {
1705 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1706 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1707 return 0;
1708 }
1709
1710 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711}
1712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001714symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001717
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 if (!args)
1719 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 for (i = 0; i < asdl_seq_LEN(args); i++) {
1722 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1723 if (arg->annotation)
1724 VISIT(st, expr, arg->annotation);
1725 }
1726
1727 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001728}
1729
Neal Norwitzc1505362006-12-28 06:47:50 +00001730static int
Andy Lester95668422020-03-06 09:46:04 -06001731symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001732{
Anthony Sottileec007cb2020-01-04 20:57:21 -05001733 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1734 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001735 if (a->args && !symtable_visit_argannotations(st, a->args))
1736 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001737 if (a->vararg && a->vararg->annotation)
1738 VISIT(st, expr, a->vararg->annotation);
1739 if (a->kwarg && a->kwarg->annotation)
1740 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001741 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1742 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001743 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001744 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001746}
1747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749symtable_visit_arguments(struct symtable *st, arguments_ty a)
1750{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751 /* skip default arguments inside function block
1752 XXX should ast be different?
1753 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001754 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1755 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 if (a->args && !symtable_visit_params(st, a->args))
1757 return 0;
1758 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1759 return 0;
1760 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001761 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return 0;
1763 st->st_cur->ste_varargs = 1;
1764 }
1765 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001766 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 return 0;
1768 st->st_cur->ste_varkeywords = 1;
1769 }
1770 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771}
1772
1773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001775symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 if (eh->v.ExceptHandler.type)
1778 VISIT(st, expr, eh->v.ExceptHandler.type);
1779 if (eh->v.ExceptHandler.name)
1780 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1781 return 0;
1782 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1783 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001784}
1785
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001786static int
1787symtable_visit_withitem(struct symtable *st, withitem_ty item)
1788{
1789 VISIT(st, expr, item->context_expr);
1790 if (item->optional_vars) {
1791 VISIT(st, expr, item->optional_vars);
1792 }
1793 return 1;
1794}
1795
Brandt Bucher145bf262021-02-26 14:51:55 -08001796static int
1797symtable_visit_match_case(struct symtable *st, match_case_ty m)
1798{
1799 assert(!st->in_pattern);
1800 st->in_pattern = 1;
1801 VISIT(st, expr, m->pattern);
1802 assert(st->in_pattern);
1803 st->in_pattern = 0;
1804 if (m->guard) {
1805 VISIT(st, expr, m->guard);
1806 }
1807 VISIT_SEQ(st, stmt, m->body);
1808 return 1;
1809}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001810
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812symtable_visit_alias(struct symtable *st, alias_ty a)
1813{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001814 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001815 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 dotted package name (e.g. spam.eggs)
1817 */
1818 PyObject *store_name;
1819 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001820 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1821 PyUnicode_GET_LENGTH(name), 1);
1822 if (dot != -1) {
1823 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 if (!store_name)
1825 return 0;
1826 }
1827 else {
1828 store_name = name;
1829 Py_INCREF(store_name);
1830 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001831 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1833 Py_DECREF(store_name);
1834 return r;
1835 }
1836 else {
1837 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001838 int lineno = st->st_cur->ste_lineno;
1839 int col_offset = st->st_cur->ste_col_offset;
1840 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001841 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001842 Py_DECREF(store_name);
1843 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 Py_DECREF(store_name);
1846 return 1;
1847 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001848}
1849
1850
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001851static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001852symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1853{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001854 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001856 st->st_cur->ste_comp_iter_target = 0;
1857 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001859 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001861 if (lc->is_async) {
1862 st->st_cur->ste_coroutine = 1;
1863 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001865}
1866
1867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001868static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001869symtable_visit_keyword(struct symtable *st, keyword_ty k)
1870{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001871 VISIT(st, expr, k->value);
1872 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001873}
1874
1875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001877symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01001878 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001879 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001881 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 comprehension_ty outermost = ((comprehension_ty)
1883 asdl_seq_GET(generators, 0));
1884 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001885 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001886 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001887 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 /* Create comprehension scope for the rest */
1889 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001890 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1891 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001892 return 0;
1893 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001894 if (outermost->is_async) {
1895 st->st_cur->ste_coroutine = 1;
1896 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001897 st->st_cur->ste_comprehension = 1;
1898
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001899 /* Outermost iter is received as an argument */
1900 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06001901 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001902 return 0;
1903 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001904 /* Visit iteration variable target, and mark them as such */
1905 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001906 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001907 st->st_cur->ste_comp_iter_target = 0;
1908 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001909 VISIT_SEQ(st, expr, outermost->ifs);
1910 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001912 VISIT(st, expr, value);
1913 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001914 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001915 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001916 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1917 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1918 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1919 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001920 PyErr_SyntaxLocationObject(st->st_filename,
1921 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001922 st->st_cur->ste_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06001923 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001924 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001925 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001926 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06001927 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001928}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001929
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001931symtable_visit_genexp(struct symtable *st, expr_ty e)
1932{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001933 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1934 e->v.GeneratorExp.generators,
1935 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001936}
1937
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001938static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001939symtable_visit_listcomp(struct symtable *st, expr_ty e)
1940{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001941 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1942 e->v.ListComp.generators,
1943 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001944}
1945
1946static int
1947symtable_visit_setcomp(struct symtable *st, expr_ty e)
1948{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001949 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1950 e->v.SetComp.generators,
1951 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001952}
1953
1954static int
1955symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1956{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001957 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1958 e->v.DictComp.generators,
1959 e->v.DictComp.key,
1960 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001961}
Victor Stinner28ad12f2021-03-19 12:41:49 +01001962
1963
1964struct symtable *
1965_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
1966 int start, PyCompilerFlags *flags)
1967{
1968 struct symtable *st;
1969 mod_ty mod;
1970 PyArena *arena;
1971
1972 arena = PyArena_New();
1973 if (arena == NULL)
1974 return NULL;
1975
1976 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1977 if (mod == NULL) {
1978 PyArena_Free(arena);
1979 return NULL;
1980 }
1981 st = _PySymtable_Build(mod, filename, 0);
1982 PyArena_Free(arena);
1983 return st;
1984}