blob: 10a47d1215e66572fe5187785da58125c232315f [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02002#include "pycore_pystate.h" // _PyThreadState_GET()
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003#include "symtable.h"
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:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 PySymtable_Free(st);
247 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 Stinner14e461d2013-08-26 22:28:21 +0200263PySymtable_BuildObject(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) {
275 PySymtable_Free(st);
276 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) {
285 PySymtable_Free(st);
286 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)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000298 PySymtable_Free(st);
299 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)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 PySymtable_Free(st);
329 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);
336 PySymtable_Free(st);
337 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;
342 PySymtable_Free(st);
343 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344 error:
Andy Lester95668422020-03-06 09:46:04 -0600345 (void) symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000346 PySymtable_Free(st);
347 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000348}
349
Victor Stinner14e461d2013-08-26 22:28:21 +0200350struct symtable *
351PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
352{
353 PyObject *filename;
354 struct symtable *st;
355 filename = PyUnicode_DecodeFSDefault(filename_str);
356 if (filename == NULL)
357 return NULL;
358 st = PySymtable_BuildObject(mod, filename, future);
359 Py_DECREF(filename);
360 return st;
361}
362
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363void
364PySymtable_Free(struct symtable *st)
365{
Victor Stinner14e461d2013-08-26 22:28:21 +0200366 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 Py_XDECREF(st->st_blocks);
368 Py_XDECREF(st->st_stack);
369 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000370}
371
372PySTEntryObject *
373PySymtable_Lookup(struct symtable *st, void *key)
374{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000377 k = PyLong_FromVoidPtr(key);
378 if (k == NULL)
379 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200380 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000381 if (v) {
382 assert(PySTEntry_Check(v));
383 Py_INCREF(v);
384 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200385 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 PyErr_SetString(PyExc_KeyError,
387 "unknown symbol table entry");
388 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000389
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 Py_DECREF(k);
391 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000392}
393
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000394static long
395_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000396{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200397 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000398 if (!v)
399 return 0;
400 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000401 return PyLong_AS_LONG(v);
402}
403
404int
405PyST_GetScope(PySTEntryObject *ste, PyObject *name)
406{
407 long symbol = _PyST_GetSymbol(ste, name);
408 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409}
410
Benjamin Petersond9c87022012-10-31 20:26:20 -0400411static int
412error_at_directive(PySTEntryObject *ste, PyObject *name)
413{
414 Py_ssize_t i;
415 PyObject *data;
416 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600417 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400418 data = PyList_GET_ITEM(ste->ste_directives, i);
419 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600420 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
421 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
422 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
423 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400424 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600425
426 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700427 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400428 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600429 PyErr_SetString(PyExc_RuntimeError,
430 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400431 return 0;
432}
433
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434
435/* Analyze raw symbol information to determine scope of each name.
436
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000437 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000443 explicit global is declared with the global statement. An implicit
444 global is a free variable for which the compiler has found no binding
445 in an enclosing function scope. The implicit global is either a global
446 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
447 to handle these names to implement slightly odd semantics. In such a
448 block, the name is treated as global until it is assigned to; then it
449 is treated as a local.
450
451 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000452 The first pass collects raw facts from the AST via the symtable_visit_*
453 functions: the name is a parameter here, the name is used but not defined
454 here, etc. The second pass analyzes these facts during a pass over the
455 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456
457 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000458 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000459 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000460 Names which are explicitly declared nonlocal must exist in this set of
461 visible names - if they do not, a syntax error is raised. After doing
462 the local analysis, it analyzes each of its child blocks using an
463 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464
Nick Coghlan650f0d02007-04-15 12:05:43 +0000465 The children update the free variable set. If a local variable is added to
466 the free variable set by the child, the variable is marked as a cell. The
467 function object being defined must provide runtime storage for the variable
468 that may outlive the function's frame. Cell variables are removed from the
469 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000470
Nick Coghlan650f0d02007-04-15 12:05:43 +0000471 During analysis, the names are:
472 symbols: dict mapping from symbol names to flag values (including offset scope values)
473 scopes: dict mapping from symbol names to scope values (no offset)
474 local: set of all symbol names local to the current scope
475 bound: set of all symbol names local to a containing function scope
476 free: set of all symbol names referenced but not bound in child scopes
477 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478*/
479
480#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000481 PyObject *o = PyLong_FromLong(I); \
482 if (!o) \
483 return 0; \
484 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
485 Py_DECREF(o); \
486 return 0; \
487 } \
488 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489}
490
491/* Decide on scope of name, given flags.
492
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000493 The namespace dictionaries may be modified to record information
494 about the new name. For example, a new global will add an entry to
495 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000496*/
497
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000499analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000500 PyObject *bound, PyObject *local, PyObject *free,
501 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000502{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 if (flags & DEF_NONLOCAL) {
505 PyErr_Format(PyExc_SyntaxError,
506 "name '%U' is nonlocal and global",
507 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400508 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000509 }
510 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
511 if (PySet_Add(global, name) < 0)
512 return 0;
513 if (bound && (PySet_Discard(bound, name) < 0))
514 return 0;
515 return 1;
516 }
517 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 if (!bound) {
519 PyErr_Format(PyExc_SyntaxError,
520 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400521 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 }
523 if (!PySet_Contains(bound, name)) {
524 PyErr_Format(PyExc_SyntaxError,
525 "no binding for nonlocal '%U' found",
526 name);
527
Benjamin Petersond9c87022012-10-31 20:26:20 -0400528 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 }
530 SET_SCOPE(scopes, name, FREE);
531 ste->ste_free = 1;
532 return PySet_Add(free, name) >= 0;
533 }
534 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000535 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000536 if (PySet_Add(local, name) < 0)
537 return 0;
538 if (PySet_Discard(global, name) < 0)
539 return 0;
540 return 1;
541 }
542 /* If an enclosing block has a binding for this name, it
543 is a free variable rather than a global variable.
544 Note that having a non-NULL bound implies that the block
545 is nested.
546 */
547 if (bound && PySet_Contains(bound, name)) {
548 SET_SCOPE(scopes, name, FREE);
549 ste->ste_free = 1;
550 return PySet_Add(free, name) >= 0;
551 }
552 /* If a parent has a global statement, then call it global
553 explicit? It could also be global implicit.
554 */
555 if (global && PySet_Contains(global, name)) {
556 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
557 return 1;
558 }
559 if (ste->ste_nested)
560 ste->ste_free = 1;
561 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
562 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000563}
564
565#undef SET_SCOPE
566
567/* If a name is defined in free and also in locals, then this block
568 provides the binding for the free variable. The name should be
569 marked CELL in this block and removed from the free list.
570
571 Note that the current block's free variables are included in free.
572 That's safe because no name can be free and local in the same scope.
573*/
574
575static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500576analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000577{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000578 PyObject *name, *v, *v_cell;
579 int success = 0;
580 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 v_cell = PyLong_FromLong(CELL);
583 if (!v_cell)
584 return 0;
585 while (PyDict_Next(scopes, &pos, &name, &v)) {
586 long scope;
587 assert(PyLong_Check(v));
588 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000589 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000590 continue;
591 if (!PySet_Contains(free, name))
592 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 /* Replace LOCAL with CELL for this name, and remove
594 from free. It is safe to replace the value of name
595 in the dict, because it will not cause a resize.
596 */
597 if (PyDict_SetItem(scopes, name, v_cell) < 0)
598 goto error;
599 if (PySet_Discard(free, name) < 0)
600 goto error;
601 }
602 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000603 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000604 Py_DECREF(v_cell);
605 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000606}
607
Benjamin Peterson312595c2013-05-15 15:26:42 -0500608static int
609drop_class_free(PySTEntryObject *ste, PyObject *free)
610{
611 int res;
612 if (!GET_IDENTIFIER(__class__))
613 return 0;
614 res = PySet_Discard(free, __class__);
615 if (res < 0)
616 return 0;
617 if (res)
618 ste->ste_needs_class_closure = 1;
619 return 1;
620}
621
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000622/* Enter the final scope information into the ste_symbols dict.
623 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 * All arguments are dicts. Modifies symbols, others are read-only.
625*/
626static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000628 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 PyObject *name = NULL, *itr = NULL;
631 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
632 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000633
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 /* Update scope information for all symbols in this scope */
635 while (PyDict_Next(symbols, &pos, &name, &v)) {
636 long scope, flags;
637 assert(PyLong_Check(v));
638 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200639 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 assert(v_scope && PyLong_Check(v_scope));
641 scope = PyLong_AS_LONG(v_scope);
642 flags |= (scope << SCOPE_OFFSET);
643 v_new = PyLong_FromLong(flags);
644 if (!v_new)
645 return 0;
646 if (PyDict_SetItem(symbols, name, v_new) < 0) {
647 Py_DECREF(v_new);
648 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000649 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 Py_DECREF(v_new);
651 }
652
653 /* Record not yet resolved free variables from children (if any) */
654 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
655 if (!v_free)
656 return 0;
657
658 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600659 if (itr == NULL) {
660 Py_DECREF(v_free);
661 return 0;
662 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000663
664 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200665 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000666
667 /* Handle symbol that already exists in this scope */
668 if (v) {
669 /* Handle a free variable in a method of
670 the class that has the same name as a local
671 or global in the class scope.
672 */
673 if (classflag &&
674 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
675 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
676 v_new = PyLong_FromLong(flags);
677 if (!v_new) {
678 goto error;
679 }
680 if (PyDict_SetItem(symbols, name, v_new) < 0) {
681 Py_DECREF(v_new);
682 goto error;
683 }
684 Py_DECREF(v_new);
685 }
686 /* It's a cell, or already free in this scope */
687 Py_DECREF(name);
688 continue;
689 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200690 else if (PyErr_Occurred()) {
691 goto error;
692 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200694 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000695 Py_DECREF(name);
696 continue; /* it's a global */
697 }
698 /* Propagate new free symbol up the lexical stack */
699 if (PyDict_SetItem(symbols, name, v_free) < 0) {
700 goto error;
701 }
702 Py_DECREF(name);
703 }
704 Py_DECREF(itr);
705 Py_DECREF(v_free);
706 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000707error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 Py_XDECREF(v_free);
709 Py_XDECREF(itr);
710 Py_XDECREF(name);
711 return 0;
712}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
714/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000715
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000716 Arguments:
717 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000718 bound -- set of variables bound in enclosing scopes (input). bound
719 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000720 free -- set of free variables in enclosed scopes (output)
721 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000722
723 The implementation uses two mutually recursive functions,
724 analyze_block() and analyze_child_block(). analyze_block() is
725 responsible for analyzing the individual names defined in a block.
726 analyze_child_block() prepares temporary namespace dictionaries
727 used to evaluated nested blocks.
728
729 The two functions exist because a child block should see the name
730 bindings of its enclosing blocks, but those bindings should not
731 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000732*/
733
734static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
736 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000737
738static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000739analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
740 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
743 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
744 PyObject *temp;
745 int i, success = 0;
746 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 local = PySet_New(NULL); /* collect new names bound in block */
749 if (!local)
750 goto error;
751 scopes = PyDict_New(); /* collect scopes defined for each name */
752 if (!scopes)
753 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000754
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000755 /* Allocate new global and bound variable dictionaries. These
756 dictionaries hold the names visible in nested blocks. For
757 ClassBlocks, the bound and global names are initialized
758 before analyzing names, because class bindings aren't
759 visible in methods. For other blocks, they are initialized
760 after names are analyzed.
761 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000762
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000763 /* TODO(jhylton): Package these dicts in a struct so that we
764 can write reasonable helper functions?
765 */
766 newglobal = PySet_New(NULL);
767 if (!newglobal)
768 goto error;
769 newfree = PySet_New(NULL);
770 if (!newfree)
771 goto error;
772 newbound = PySet_New(NULL);
773 if (!newbound)
774 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000776 /* Class namespace has no effect on names visible in
777 nested functions, so populate the global and bound
778 sets to be passed to child blocks before analyzing
779 this one.
780 */
781 if (ste->ste_type == ClassBlock) {
782 /* Pass down known globals */
783 temp = PyNumber_InPlaceOr(newglobal, global);
784 if (!temp)
785 goto error;
786 Py_DECREF(temp);
787 /* Pass down previously bound symbols */
788 if (bound) {
789 temp = PyNumber_InPlaceOr(newbound, bound);
790 if (!temp)
791 goto error;
792 Py_DECREF(temp);
793 }
794 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
797 long flags = PyLong_AS_LONG(v);
798 if (!analyze_name(ste, scopes, name, flags,
799 bound, local, free, global))
800 goto error;
801 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000802
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 /* Populate global and bound sets to be passed to children. */
804 if (ste->ste_type != ClassBlock) {
805 /* Add function locals to bound set */
806 if (ste->ste_type == FunctionBlock) {
807 temp = PyNumber_InPlaceOr(newbound, local);
808 if (!temp)
809 goto error;
810 Py_DECREF(temp);
811 }
812 /* Pass down previously bound symbols */
813 if (bound) {
814 temp = PyNumber_InPlaceOr(newbound, bound);
815 if (!temp)
816 goto error;
817 Py_DECREF(temp);
818 }
819 /* Pass down known globals */
820 temp = PyNumber_InPlaceOr(newglobal, global);
821 if (!temp)
822 goto error;
823 Py_DECREF(temp);
824 }
825 else {
826 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000827 if (!GET_IDENTIFIER(__class__))
828 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000829 if (PySet_Add(newbound, __class__) < 0)
830 goto error;
831 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000832
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300833 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000834
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000835 newbound, newglobal now contain the names visible in
836 nested blocks. The free variables in the children will
837 be collected in allfree.
838 */
839 allfree = PySet_New(NULL);
840 if (!allfree)
841 goto error;
842 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
843 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
844 PySTEntryObject* entry;
845 assert(c && PySTEntry_Check(c));
846 entry = (PySTEntryObject*)c;
847 if (!analyze_child_block(entry, newbound, newfree, newglobal,
848 allfree))
849 goto error;
850 /* Check if any children have free variables */
851 if (entry->ste_free || entry->ste_child_free)
852 ste->ste_child_free = 1;
853 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000854
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 temp = PyNumber_InPlaceOr(newfree, allfree);
856 if (!temp)
857 goto error;
858 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500861 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500863 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 goto error;
865 /* Records the results of the analysis in the symbol table entry */
866 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
867 ste->ste_type == ClassBlock))
868 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000870 temp = PyNumber_InPlaceOr(free, newfree);
871 if (!temp)
872 goto error;
873 Py_DECREF(temp);
874 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000876 Py_XDECREF(scopes);
877 Py_XDECREF(local);
878 Py_XDECREF(newbound);
879 Py_XDECREF(newglobal);
880 Py_XDECREF(newfree);
881 Py_XDECREF(allfree);
882 if (!success)
883 assert(PyErr_Occurred());
884 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000885}
886
887static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
889 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000890{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
892 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000893
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000895
Martin Panter3ee62702016-06-04 04:57:19 +0000896 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 current block. The analyze_block() call modifies these
898 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 */
901 temp_bound = PySet_New(bound);
902 if (!temp_bound)
903 goto error;
904 temp_free = PySet_New(free);
905 if (!temp_free)
906 goto error;
907 temp_global = PySet_New(global);
908 if (!temp_global)
909 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
912 goto error;
913 temp = PyNumber_InPlaceOr(child_free, temp_free);
914 if (!temp)
915 goto error;
916 Py_DECREF(temp);
917 Py_DECREF(temp_bound);
918 Py_DECREF(temp_free);
919 Py_DECREF(temp_global);
920 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000921 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000922 Py_XDECREF(temp_bound);
923 Py_XDECREF(temp_free);
924 Py_XDECREF(temp_global);
925 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000926}
927
928static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929symtable_analyze(struct symtable *st)
930{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 PyObject *free, *global;
932 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 free = PySet_New(NULL);
935 if (!free)
936 return 0;
937 global = PySet_New(NULL);
938 if (!global) {
939 Py_DECREF(free);
940 return 0;
941 }
942 r = analyze_block(st->st_top, NULL, free, global);
943 Py_DECREF(free);
944 Py_DECREF(global);
945 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946}
947
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000948/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000949 This reference is released when the block is exited, via the DECREF
950 in symtable_exit_block().
951*/
952
953static int
Andy Lester95668422020-03-06 09:46:04 -0600954symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955{
Benjamin Peterson609da582011-06-29 22:52:39 -0500956 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957
Benjamin Peterson609da582011-06-29 22:52:39 -0500958 st->st_cur = NULL;
959 size = PyList_GET_SIZE(st->st_stack);
960 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500961 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500963 if (--size)
964 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 }
966 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967}
968
969static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000971 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972{
Benjamin Peterson609da582011-06-29 22:52:39 -0500973 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974
Benjamin Peterson609da582011-06-29 22:52:39 -0500975 ste = ste_new(st, name, block, ast, lineno, col_offset);
976 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500978 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
979 Py_DECREF(ste);
980 return 0;
981 }
982 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000983 /* bpo-37757: For now, disallow *all* assignment expressions in the
984 * outermost iterator expression of a comprehension, even those inside
985 * a nested comprehension or a lambda expression.
986 */
987 if (prev) {
988 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
989 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500990 /* The entry is owned by the stack. Borrow it for st_cur. */
991 Py_DECREF(ste);
992 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000993 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 st->st_global = st->st_cur->ste_symbols;
995 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500996 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000997 return 0;
998 }
999 }
1000 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001}
1002
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001003static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001004symtable_lookup(struct symtable *st, PyObject *name)
1005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyObject *mangled = _Py_Mangle(st->st_private, name);
1007 if (!mangled)
1008 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +01001009 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1010 Py_DECREF(mangled);
1011 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012}
1013
1014static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001015symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001016{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 PyObject *o;
1018 PyObject *dict;
1019 long val;
1020 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021
Jeremy Hylton81e95022007-02-27 06:50:52 +00001022
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 if (!mangled)
1024 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001025 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001026 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 val = PyLong_AS_LONG(o);
1028 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1029 /* Is it better to use 'mangled' or 'name' here? */
1030 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001031 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001032 ste->ste_lineno,
1033 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001034 goto error;
1035 }
1036 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001037 }
1038 else if (PyErr_Occurred()) {
1039 goto error;
1040 }
1041 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001042 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001043 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001044 if (ste->ste_comp_iter_target) {
1045 /* This name is an iteration variable in a comprehension,
1046 * so check for a binding conflict with any named expressions.
1047 * Otherwise, mark it as an iteration variable so subsequent
1048 * named expressions can check for conflicts.
1049 */
1050 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1051 PyErr_Format(PyExc_SyntaxError,
1052 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1053 PyErr_SyntaxLocationObject(st->st_filename,
1054 ste->ste_lineno,
1055 ste->ste_col_offset + 1);
1056 goto error;
1057 }
1058 val |= DEF_COMP_ITER;
1059 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 o = PyLong_FromLong(val);
1061 if (o == NULL)
1062 goto error;
1063 if (PyDict_SetItem(dict, mangled, o) < 0) {
1064 Py_DECREF(o);
1065 goto error;
1066 }
1067 Py_DECREF(o);
1068
1069 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001070 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 goto error;
1072 } else if (flag & DEF_GLOBAL) {
1073 /* XXX need to update DEF_GLOBAL for other flags too;
1074 perhaps only DEF_FREE_GLOBAL */
1075 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001076 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 val |= PyLong_AS_LONG(o);
1078 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001079 else if (PyErr_Occurred()) {
1080 goto error;
1081 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001083 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 goto error;
1085 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1086 Py_DECREF(o);
1087 goto error;
1088 }
1089 Py_DECREF(o);
1090 }
1091 Py_DECREF(mangled);
1092 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001093
1094error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 Py_DECREF(mangled);
1096 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097}
1098
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001099static int
1100symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1101 return symtable_add_def_helper(st, name, flag, st->st_cur);
1102}
1103
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001104/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1105 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001106 function.
1107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1109 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001110
1111 VISIT_QUIT macro returns the specified value exiting from the function but
1112 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001113*/
1114
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001115#define VISIT_QUIT(ST, X) \
1116 return --(ST)->recursion_depth,(X)
1117
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001120 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001124 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1126 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1127 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001128 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001131
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001132#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001134 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1136 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1137 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001138 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001140}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001141
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001142#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001144 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001146 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001148 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001149 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001151}
1152
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001153static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001154symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001155{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001156 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001157 int res;
1158 if (!st->st_cur->ste_directives) {
1159 st->st_cur->ste_directives = PyList_New(0);
1160 if (!st->st_cur->ste_directives)
1161 return 0;
1162 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001163 mangled = _Py_Mangle(st->st_private, name);
1164 if (!mangled)
1165 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001166 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001167 if (!data)
1168 return 0;
1169 res = PyList_Append(st->st_cur->ste_directives, data);
1170 Py_DECREF(data);
1171 return res == 0;
1172}
1173
1174
1175static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001176symtable_visit_stmt(struct symtable *st, stmt_ty s)
1177{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001178 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001179 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001180 "maximum recursion depth exceeded during compilation");
1181 VISIT_QUIT(st, 0);
1182 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 switch (s->kind) {
1184 case FunctionDef_kind:
1185 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001186 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 if (s->v.FunctionDef.args->defaults)
1188 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1189 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001190 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001191 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001192 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001193 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 if (s->v.FunctionDef.decorator_list)
1195 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1196 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001197 FunctionBlock, (void *)s, s->lineno,
1198 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001199 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001200 VISIT(st, arguments, s->v.FunctionDef.args);
1201 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001202 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001203 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 break;
1205 case ClassDef_kind: {
1206 PyObject *tmp;
1207 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001208 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1210 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 if (s->v.ClassDef.decorator_list)
1212 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1213 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001214 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001215 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 tmp = st->st_private;
1217 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001218 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001220 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001221 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001222 break;
1223 }
1224 case Return_kind:
1225 if (s->v.Return.value) {
1226 VISIT(st, expr, s->v.Return.value);
1227 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001228 }
1229 break;
1230 case Delete_kind:
1231 VISIT_SEQ(st, expr, s->v.Delete.targets);
1232 break;
1233 case Assign_kind:
1234 VISIT_SEQ(st, expr, s->v.Assign.targets);
1235 VISIT(st, expr, s->v.Assign.value);
1236 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001237 case AnnAssign_kind:
1238 if (s->v.AnnAssign.target->kind == Name_kind) {
1239 expr_ty e_name = s->v.AnnAssign.target;
1240 long cur = symtable_lookup(st, e_name->v.Name.id);
1241 if (cur < 0) {
1242 VISIT_QUIT(st, 0);
1243 }
1244 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001245 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001246 && s->v.AnnAssign.simple) {
1247 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001248 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1249 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001250 PyErr_SyntaxLocationObject(st->st_filename,
1251 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001252 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001253 VISIT_QUIT(st, 0);
1254 }
1255 if (s->v.AnnAssign.simple &&
1256 !symtable_add_def(st, e_name->v.Name.id,
1257 DEF_ANNOT | DEF_LOCAL)) {
1258 VISIT_QUIT(st, 0);
1259 }
1260 else {
1261 if (s->v.AnnAssign.value
1262 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1263 VISIT_QUIT(st, 0);
1264 }
1265 }
1266 }
1267 else {
1268 VISIT(st, expr, s->v.AnnAssign.target);
1269 }
1270 VISIT(st, expr, s->v.AnnAssign.annotation);
1271 if (s->v.AnnAssign.value) {
1272 VISIT(st, expr, s->v.AnnAssign.value);
1273 }
1274 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001275 case AugAssign_kind:
1276 VISIT(st, expr, s->v.AugAssign.target);
1277 VISIT(st, expr, s->v.AugAssign.value);
1278 break;
1279 case For_kind:
1280 VISIT(st, expr, s->v.For.target);
1281 VISIT(st, expr, s->v.For.iter);
1282 VISIT_SEQ(st, stmt, s->v.For.body);
1283 if (s->v.For.orelse)
1284 VISIT_SEQ(st, stmt, s->v.For.orelse);
1285 break;
1286 case While_kind:
1287 VISIT(st, expr, s->v.While.test);
1288 VISIT_SEQ(st, stmt, s->v.While.body);
1289 if (s->v.While.orelse)
1290 VISIT_SEQ(st, stmt, s->v.While.orelse);
1291 break;
1292 case If_kind:
1293 /* XXX if 0: and lookup_yield() hacks */
1294 VISIT(st, expr, s->v.If.test);
1295 VISIT_SEQ(st, stmt, s->v.If.body);
1296 if (s->v.If.orelse)
1297 VISIT_SEQ(st, stmt, s->v.If.orelse);
1298 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001299 case Match_kind:
1300 VISIT(st, expr, s->v.Match.subject);
1301 VISIT_SEQ(st, match_case, s->v.Match.cases);
1302 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 case Raise_kind:
1304 if (s->v.Raise.exc) {
1305 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001306 if (s->v.Raise.cause) {
1307 VISIT(st, expr, s->v.Raise.cause);
1308 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001309 }
1310 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001311 case Try_kind:
1312 VISIT_SEQ(st, stmt, s->v.Try.body);
1313 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1314 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1315 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 break;
1317 case Assert_kind:
1318 VISIT(st, expr, s->v.Assert.test);
1319 if (s->v.Assert.msg)
1320 VISIT(st, expr, s->v.Assert.msg);
1321 break;
1322 case Import_kind:
1323 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 break;
1325 case ImportFrom_kind:
1326 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 break;
1328 case Global_kind: {
1329 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001330 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1332 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001333 long cur = symtable_lookup(st, name);
1334 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001335 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001336 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1337 const char* msg;
1338 if (cur & DEF_PARAM) {
1339 msg = GLOBAL_PARAM;
1340 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001341 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001342 } else if (cur & DEF_ANNOT) {
1343 msg = GLOBAL_ANNOT;
1344 } else { /* DEF_LOCAL */
1345 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001346 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001347 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001348 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001349 PyErr_SyntaxLocationObject(st->st_filename,
1350 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001351 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001352 VISIT_QUIT(st, 0);
1353 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001355 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001356 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001357 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 }
1359 break;
1360 }
1361 case Nonlocal_kind: {
1362 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001363 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001364 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1365 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 long cur = symtable_lookup(st, name);
1367 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001368 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001369 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1370 const char* msg;
1371 if (cur & DEF_PARAM) {
1372 msg = NONLOCAL_PARAM;
1373 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001374 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001375 } else if (cur & DEF_ANNOT) {
1376 msg = NONLOCAL_ANNOT;
1377 } else { /* DEF_LOCAL */
1378 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001379 }
1380 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001381 PyErr_SyntaxLocationObject(st->st_filename,
1382 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001383 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001384 VISIT_QUIT(st, 0);
1385 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001386 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001387 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001388 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001389 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001390 }
1391 break;
1392 }
1393 case Expr_kind:
1394 VISIT(st, expr, s->v.Expr.value);
1395 break;
1396 case Pass_kind:
1397 case Break_kind:
1398 case Continue_kind:
1399 /* nothing to do here */
1400 break;
1401 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001402 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 VISIT_SEQ(st, stmt, s->v.With.body);
1404 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001405 case AsyncFunctionDef_kind:
1406 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1407 VISIT_QUIT(st, 0);
1408 if (s->v.AsyncFunctionDef.args->defaults)
1409 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1410 if (s->v.AsyncFunctionDef.args->kw_defaults)
1411 VISIT_SEQ_WITH_NULL(st, expr,
1412 s->v.AsyncFunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001413 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001414 s->v.AsyncFunctionDef.returns))
1415 VISIT_QUIT(st, 0);
1416 if (s->v.AsyncFunctionDef.decorator_list)
1417 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1418 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1419 FunctionBlock, (void *)s, s->lineno,
1420 s->col_offset))
1421 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001422 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001423 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1424 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001425 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001426 VISIT_QUIT(st, 0);
1427 break;
1428 case AsyncWith_kind:
1429 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1430 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1431 break;
1432 case AsyncFor_kind:
1433 VISIT(st, expr, s->v.AsyncFor.target);
1434 VISIT(st, expr, s->v.AsyncFor.iter);
1435 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1436 if (s->v.AsyncFor.orelse)
1437 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1438 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001440 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001441}
1442
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001443static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001444symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1445{
1446 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001447 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001448
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001449 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001450 Py_ssize_t i, size;
1451 struct _symtable_entry *ste;
1452 size = PyList_GET_SIZE(st->st_stack);
1453 assert(size);
1454
1455 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1456 for (i = size - 1; i >= 0; i--) {
1457 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1458
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001459 /* If we find a comprehension scope, check for a target
1460 * binding conflict with iteration variables, otherwise skip it
1461 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001462 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001463 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1464 if (target_in_scope & DEF_COMP_ITER) {
1465 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1466 PyErr_SyntaxLocationObject(st->st_filename,
1467 e->lineno,
1468 e->col_offset);
1469 VISIT_QUIT(st, 0);
1470 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001471 continue;
1472 }
1473
Pablo Galindofd5c4142019-10-14 05:18:05 +01001474 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001475 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001476 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1477 if (target_in_scope & DEF_GLOBAL) {
1478 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1479 VISIT_QUIT(st, 0);
1480 } else {
1481 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1482 VISIT_QUIT(st, 0);
1483 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001484 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001485 VISIT_QUIT(st, 0);
1486
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001487 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001488 }
1489 /* If we find a ModuleBlock entry, add as GLOBAL */
1490 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001491 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001492 VISIT_QUIT(st, 0);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001493 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001494 VISIT_QUIT(st, 0);
1495
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001496 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001497 }
1498 /* Disallow usage in ClassBlock */
1499 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001500 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001501 PyErr_SyntaxLocationObject(st->st_filename,
1502 e->lineno,
1503 e->col_offset);
1504 VISIT_QUIT(st, 0);
1505 }
1506 }
1507
1508 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1509 and should never fall to this case
1510 */
1511 assert(0);
1512 return 0;
1513}
1514
1515static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001516symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1517{
1518 if (st->st_cur->ste_comp_iter_expr > 0) {
1519 /* Assignment isn't allowed in a comprehension iterable expression */
1520 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1521 PyErr_SyntaxLocationObject(st->st_filename,
1522 e->lineno,
1523 e->col_offset);
Nick Coghlan06145232019-08-29 23:26:53 +10001524 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001525 }
1526 if (st->st_cur->ste_comprehension) {
1527 /* Inside a comprehension body, so find the right target scope */
1528 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001529 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001530 }
1531 VISIT(st, expr, e->v.NamedExpr.value);
1532 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001533 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001534}
1535
1536static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537symtable_visit_expr(struct symtable *st, expr_ty e)
1538{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001539 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001540 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001541 "maximum recursion depth exceeded during compilation");
1542 VISIT_QUIT(st, 0);
1543 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001545 case NamedExpr_kind:
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001546 if(!symtable_handle_namedexpr(st, e))
1547 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001548 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001549 case BoolOp_kind:
1550 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1551 break;
1552 case BinOp_kind:
1553 VISIT(st, expr, e->v.BinOp.left);
1554 VISIT(st, expr, e->v.BinOp.right);
1555 break;
1556 case UnaryOp_kind:
1557 VISIT(st, expr, e->v.UnaryOp.operand);
1558 break;
1559 case Lambda_kind: {
1560 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001561 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (e->v.Lambda.args->defaults)
1563 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001564 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001565 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001567 FunctionBlock, (void *)e, e->lineno,
1568 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001569 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001570 VISIT(st, arguments, e->v.Lambda.args);
1571 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001572 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001573 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 break;
1575 }
1576 case IfExp_kind:
1577 VISIT(st, expr, e->v.IfExp.test);
1578 VISIT(st, expr, e->v.IfExp.body);
1579 VISIT(st, expr, e->v.IfExp.orelse);
1580 break;
1581 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001582 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 VISIT_SEQ(st, expr, e->v.Dict.values);
1584 break;
1585 case Set_kind:
1586 VISIT_SEQ(st, expr, e->v.Set.elts);
1587 break;
1588 case GeneratorExp_kind:
1589 if (!symtable_visit_genexp(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 ListComp_kind:
1593 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001594 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 break;
1596 case SetComp_kind:
1597 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001598 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001599 break;
1600 case DictComp_kind:
1601 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001602 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001603 break;
1604 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001605 if (e->v.Yield.value)
1606 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001608 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001609 case YieldFrom_kind:
1610 VISIT(st, expr, e->v.YieldFrom.value);
1611 st->st_cur->ste_generator = 1;
1612 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001613 case Await_kind:
1614 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001615 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001616 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 case Compare_kind:
1618 VISIT(st, expr, e->v.Compare.left);
1619 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1620 break;
1621 case Call_kind:
1622 VISIT(st, expr, e->v.Call.func);
1623 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001624 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001626 case FormattedValue_kind:
1627 VISIT(st, expr, e->v.FormattedValue.value);
1628 if (e->v.FormattedValue.format_spec)
1629 VISIT(st, expr, e->v.FormattedValue.format_spec);
1630 break;
1631 case JoinedStr_kind:
1632 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1633 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001634 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 /* Nothing to do here. */
1636 break;
1637 /* The following exprs can be assignment targets. */
1638 case Attribute_kind:
1639 VISIT(st, expr, e->v.Attribute.value);
1640 break;
1641 case Subscript_kind:
1642 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001643 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001644 break;
1645 case Starred_kind:
1646 VISIT(st, expr, e->v.Starred.value);
1647 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001648 case Slice_kind:
1649 if (e->v.Slice.lower)
1650 VISIT(st, expr, e->v.Slice.lower)
1651 if (e->v.Slice.upper)
1652 VISIT(st, expr, e->v.Slice.upper)
1653 if (e->v.Slice.step)
1654 VISIT(st, expr, e->v.Slice.step)
1655 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 case Name_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08001657 // Don't make "_" a local when used in a pattern:
1658 if (st->in_pattern &&
1659 e->v.Name.ctx == Store &&
1660 _PyUnicode_EqualToASCIIString(e->v.Name.id, "_"))
1661 {
1662 break;
1663 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 if (!symtable_add_def(st, e->v.Name.id,
1665 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001666 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 /* Special-case super: it counts as a use of __class__ */
1668 if (e->v.Name.ctx == Load &&
1669 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001670 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001671 if (!GET_IDENTIFIER(__class__) ||
1672 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001673 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 }
1675 break;
1676 /* child nodes of List and Tuple will have expr_context set */
1677 case List_kind:
1678 VISIT_SEQ(st, expr, e->v.List.elts);
1679 break;
1680 case Tuple_kind:
1681 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1682 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001683 case MatchAs_kind:
1684 VISIT(st, expr, e->v.MatchAs.pattern);
1685 symtable_add_def(st, e->v.MatchAs.name, DEF_LOCAL);
1686 break;
1687 case MatchOr_kind:
1688 VISIT_SEQ(st, expr, e->v.MatchOr.patterns);
1689 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001691 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001692}
1693
1694static int
1695symtable_implicit_arg(struct symtable *st, int pos)
1696{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1698 if (id == NULL)
1699 return 0;
1700 if (!symtable_add_def(st, id, DEF_PARAM)) {
1701 Py_DECREF(id);
1702 return 0;
1703 }
1704 Py_DECREF(id);
1705 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001706}
1707
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001709symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001710{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 if (!args)
1714 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 for (i = 0; i < asdl_seq_LEN(args); i++) {
1717 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1718 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1719 return 0;
1720 }
1721
1722 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001723}
1724
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001726symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 if (!args)
1731 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001732
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 for (i = 0; i < asdl_seq_LEN(args); i++) {
1734 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1735 if (arg->annotation)
1736 VISIT(st, expr, arg->annotation);
1737 }
1738
1739 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001740}
1741
Neal Norwitzc1505362006-12-28 06:47:50 +00001742static int
Andy Lester95668422020-03-06 09:46:04 -06001743symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001744{
Anthony Sottileec007cb2020-01-04 20:57:21 -05001745 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1746 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 if (a->args && !symtable_visit_argannotations(st, a->args))
1748 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001749 if (a->vararg && a->vararg->annotation)
1750 VISIT(st, expr, a->vararg->annotation);
1751 if (a->kwarg && a->kwarg->annotation)
1752 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1754 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001755 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001756 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001758}
1759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001760static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761symtable_visit_arguments(struct symtable *st, arguments_ty a)
1762{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 /* skip default arguments inside function block
1764 XXX should ast be different?
1765 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001766 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1767 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 if (a->args && !symtable_visit_params(st, a->args))
1769 return 0;
1770 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1771 return 0;
1772 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001773 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774 return 0;
1775 st->st_cur->ste_varargs = 1;
1776 }
1777 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001778 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 return 0;
1780 st->st_cur->ste_varkeywords = 1;
1781 }
1782 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001783}
1784
1785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001786static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001787symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1788{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789 if (eh->v.ExceptHandler.type)
1790 VISIT(st, expr, eh->v.ExceptHandler.type);
1791 if (eh->v.ExceptHandler.name)
1792 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1793 return 0;
1794 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1795 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001796}
1797
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001798static int
1799symtable_visit_withitem(struct symtable *st, withitem_ty item)
1800{
1801 VISIT(st, expr, item->context_expr);
1802 if (item->optional_vars) {
1803 VISIT(st, expr, item->optional_vars);
1804 }
1805 return 1;
1806}
1807
Brandt Bucher145bf262021-02-26 14:51:55 -08001808static int
1809symtable_visit_match_case(struct symtable *st, match_case_ty m)
1810{
1811 assert(!st->in_pattern);
1812 st->in_pattern = 1;
1813 VISIT(st, expr, m->pattern);
1814 assert(st->in_pattern);
1815 st->in_pattern = 0;
1816 if (m->guard) {
1817 VISIT(st, expr, m->guard);
1818 }
1819 VISIT_SEQ(st, stmt, m->body);
1820 return 1;
1821}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824symtable_visit_alias(struct symtable *st, alias_ty a)
1825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001827 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001828 dotted package name (e.g. spam.eggs)
1829 */
1830 PyObject *store_name;
1831 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001832 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1833 PyUnicode_GET_LENGTH(name), 1);
1834 if (dot != -1) {
1835 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 if (!store_name)
1837 return 0;
1838 }
1839 else {
1840 store_name = name;
1841 Py_INCREF(store_name);
1842 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001843 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001844 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1845 Py_DECREF(store_name);
1846 return r;
1847 }
1848 else {
1849 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001850 int lineno = st->st_cur->ste_lineno;
1851 int col_offset = st->st_cur->ste_col_offset;
1852 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001853 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001854 Py_DECREF(store_name);
1855 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 Py_DECREF(store_name);
1858 return 1;
1859 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001860}
1861
1862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001864symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1865{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001866 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001867 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001868 st->st_cur->ste_comp_iter_target = 0;
1869 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001871 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001873 if (lc->is_async) {
1874 st->st_cur->ste_coroutine = 1;
1875 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001877}
1878
1879
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881symtable_visit_keyword(struct symtable *st, keyword_ty k)
1882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 VISIT(st, expr, k->value);
1884 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001885}
1886
1887
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001889symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01001890 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001891 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001892{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 comprehension_ty outermost = ((comprehension_ty)
1895 asdl_seq_GET(generators, 0));
1896 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001897 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001898 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001899 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 /* Create comprehension scope for the rest */
1901 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001902 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1903 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 return 0;
1905 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001906 if (outermost->is_async) {
1907 st->st_cur->ste_coroutine = 1;
1908 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001909 st->st_cur->ste_comprehension = 1;
1910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 /* Outermost iter is received as an argument */
1912 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06001913 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 return 0;
1915 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001916 /* Visit iteration variable target, and mark them as such */
1917 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001918 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001919 st->st_cur->ste_comp_iter_target = 0;
1920 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001921 VISIT_SEQ(st, expr, outermost->ifs);
1922 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001923 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001924 VISIT(st, expr, value);
1925 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001926 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001927 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001928 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1929 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1930 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1931 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001932 PyErr_SyntaxLocationObject(st->st_filename,
1933 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001934 st->st_cur->ste_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06001935 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001936 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001937 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001938 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06001939 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001940}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001941
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001943symtable_visit_genexp(struct symtable *st, expr_ty e)
1944{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001945 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1946 e->v.GeneratorExp.generators,
1947 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001948}
1949
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001951symtable_visit_listcomp(struct symtable *st, expr_ty e)
1952{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001953 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1954 e->v.ListComp.generators,
1955 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001956}
1957
1958static int
1959symtable_visit_setcomp(struct symtable *st, expr_ty e)
1960{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001961 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1962 e->v.SetComp.generators,
1963 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001964}
1965
1966static int
1967symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1968{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001969 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1970 e->v.DictComp.generators,
1971 e->v.DictComp.key,
1972 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001973}