blob: 78874404ade3d46d58d90b5af296c013fd5d3a69 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner94faa072021-03-23 20:47:40 +01002#include "pycore_ast.h" // identifier, stmt_ty
Victor Stinner28ad12f2021-03-19 12:41:49 +01003#include "pycore_pystate.h" // _PyThreadState_GET()
4#include "pycore_symtable.h" // PySTEntryObject
Victor Stinner3bb183d2018-11-22 18:38:38 +01005#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner4a21e572020-04-15 02:35:41 +02006#include "structmember.h" // PyMemberDef
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00007
Neal Norwitz5d0ad502005-12-19 04:27:42 +00008/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02009#define GLOBAL_PARAM \
10"name '%U' is parameter and global"
11
12#define NONLOCAL_PARAM \
13"name '%U' is parameter and nonlocal"
14
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000015#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070016"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000017
Jeremy Hylton81e95022007-02-27 06:50:52 +000018#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070019"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000020
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000021#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070022"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000023
Jeremy Hylton81e95022007-02-27 06:50:52 +000024#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070025"name '%U' is used prior to nonlocal declaration"
26
27#define GLOBAL_ANNOT \
28"annotated name '%U' can't be global"
29
30#define NONLOCAL_ANNOT \
31"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000032
Neal Norwitz5d0ad502005-12-19 04:27:42 +000033#define IMPORT_STAR_WARNING "import * only allowed at module level"
34
Emily Morehouse8f59ee02019-01-24 16:49:56 -070035#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100036"assignment expression within a comprehension cannot be used in a class body"
37
38#define NAMED_EXPR_COMP_CONFLICT \
39"assignment expression cannot rebind comprehension iteration variable '%U'"
40
41#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
42"comprehension inner loop cannot rebind assignment expression target '%U'"
43
44#define NAMED_EXPR_COMP_ITER_EXPR \
45"assignment expression cannot be used in a comprehension iterable expression"
Emily Morehouse8f59ee02019-01-24 16:49:56 -070046
Neal Norwitz090b3dd2006-02-28 22:36:46 +000047static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000048ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000049 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000050{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020052 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000053
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000054 k = PyLong_FromVoidPtr(key);
55 if (k == NULL)
56 goto fail;
57 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020058 if (ste == NULL) {
59 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020061 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020063 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000064
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020066 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000067
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 ste->ste_symbols = NULL;
69 ste->ste_varnames = NULL;
70 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000071
Benjamin Petersond9c87022012-10-31 20:26:20 -040072 ste->ste_directives = NULL;
73
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 ste->ste_nested = 0;
76 ste->ste_free = 0;
77 ste->ste_varargs = 0;
78 ste->ste_varkeywords = 0;
79 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000080 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000081 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000082 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084 if (st->st_cur != NULL &&
85 (st->st_cur->ste_nested ||
86 st->st_cur->ste_type == FunctionBlock))
87 ste->ste_nested = 1;
88 ste->ste_child_free = 0;
89 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070090 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070091 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000092 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050093 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100094 ste->ste_comp_iter_target = 0;
95 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096
Victor Stinner9a4fb662013-07-11 22:49:00 +020097 ste->ste_symbols = PyDict_New();
98 ste->ste_varnames = PyList_New(0);
99 ste->ste_children = PyList_New(0);
100 if (ste->ste_symbols == NULL
101 || ste->ste_varnames == NULL
102 || ste->ste_children == NULL)
103 goto fail;
104
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
106 goto fail;
107
108 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000109 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000110 Py_XDECREF(ste);
111 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112}
113
114static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000115ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000116{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000117 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
118 ste->ste_name,
119 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120}
121
122static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000125 ste->ste_table = NULL;
126 Py_XDECREF(ste->ste_id);
127 Py_XDECREF(ste->ste_name);
128 Py_XDECREF(ste->ste_symbols);
129 Py_XDECREF(ste->ste_varnames);
130 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400131 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100132 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000133}
134
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000135#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000136
Guido van Rossum6f799372001-09-20 20:46:19 +0000137static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 {"id", T_OBJECT, OFF(ste_id), READONLY},
139 {"name", T_OBJECT, OFF(ste_name), READONLY},
140 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
141 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
142 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000143 {"nested", T_INT, OFF(ste_nested), READONLY},
144 {"type", T_INT, OFF(ste_type), READONLY},
145 {"lineno", T_INT, OFF(ste_lineno), READONLY},
146 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000147};
148
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000149PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 PyVarObject_HEAD_INIT(&PyType_Type, 0)
151 "symtable entry",
152 sizeof(PySTEntryObject),
153 0,
154 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200155 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000156 0, /* tp_getattr */
157 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200158 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000159 (reprfunc)ste_repr, /* tp_repr */
160 0, /* tp_as_number */
161 0, /* tp_as_sequence */
162 0, /* tp_as_mapping */
163 0, /* tp_hash */
164 0, /* tp_call */
165 0, /* tp_str */
166 PyObject_GenericGetAttr, /* tp_getattro */
167 0, /* tp_setattro */
168 0, /* tp_as_buffer */
169 Py_TPFLAGS_DEFAULT, /* tp_flags */
170 0, /* tp_doc */
171 0, /* tp_traverse */
172 0, /* tp_clear */
173 0, /* tp_richcompare */
174 0, /* tp_weaklistoffset */
175 0, /* tp_iter */
176 0, /* tp_iternext */
177 0, /* tp_methods */
178 ste_memberlist, /* tp_members */
179 0, /* tp_getset */
180 0, /* tp_base */
181 0, /* tp_dict */
182 0, /* tp_descr_get */
183 0, /* tp_descr_set */
184 0, /* tp_dictoffset */
185 0, /* tp_init */
186 0, /* tp_alloc */
187 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000188};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189
190static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000192 _Py_block_ty block, void *ast, int lineno,
193 int col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600194static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000195static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
196static int symtable_visit_expr(struct symtable *st, expr_ty s);
197static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000198static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
199static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000200static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201static int symtable_visit_arguments(struct symtable *st, arguments_ty);
202static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
203static int symtable_visit_alias(struct symtable *st, alias_ty);
204static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
205static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100206static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
207static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208static int symtable_implicit_arg(struct symtable *st, int pos);
Andy Lester95668422020-03-06 09:46:04 -0600209static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500210static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Brandt Bucher145bf262021-02-26 14:51:55 -0800211static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212
213
Nick Coghlan650f0d02007-04-15 12:05:43 +0000214static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500216 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000217
218#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220
221#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000222"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000223
224static struct symtable *
225symtable_new(void)
226{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000228
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000229 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700230 if (st == NULL) {
231 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700233 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000235 st->st_filename = NULL;
236 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000237
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000238 if ((st->st_stack = PyList_New(0)) == NULL)
239 goto fail;
240 if ((st->st_blocks = PyDict_New()) == NULL)
241 goto fail;
242 st->st_cur = NULL;
243 st->st_private = NULL;
Brandt Bucher145bf262021-02-26 14:51:55 -0800244 st->in_pattern = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246 fail:
Victor Stinner28ad12f2021-03-19 12:41:49 +0100247 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000249}
250
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000251/* When compiling the use of C stack is probably going to be a lot
252 lighter than when executing Python code but still can overflow
253 and causing a Python crash if not checked (e.g. eval("()"*300000)).
254 Using the current recursion limit for the compiler seems too
255 restrictive (it caused at least one test to fail) so a factor is
256 used to allow deeper recursion when compiling an expression.
257
258 Using a scaling factor means this should automatically adjust when
259 the recursion limit is adjusted for small or large C stack allocations.
260*/
261#define COMPILER_STACK_FRAME_SCALE 3
262
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263struct symtable *
Victor Stinner28ad12f2021-03-19 12:41:49 +0100264_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000265{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000266 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100267 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000268 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000269 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400270 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000271 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000272
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000273 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200274 return NULL;
275 if (filename == NULL) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100276 _PySymtable_Free(st);
Victor Stinner14e461d2013-08-26 22:28:21 +0200277 return NULL;
278 }
279 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 st->st_filename = filename;
281 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000282
283 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100284 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000285 if (!tstate) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100286 _PySymtable_Free(st);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000287 return NULL;
288 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400289 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000290 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400291 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000292 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400293 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
294 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000295
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 /* Make the initial symbol information gathering pass */
297 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000298 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100299 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 return NULL;
301 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000302
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 switch (mod->kind) {
305 case Module_kind:
306 seq = mod->v.Module.body;
307 for (i = 0; i < asdl_seq_LEN(seq); i++)
308 if (!symtable_visit_stmt(st,
309 (stmt_ty)asdl_seq_GET(seq, i)))
310 goto error;
311 break;
312 case Expression_kind:
313 if (!symtable_visit_expr(st, mod->v.Expression.body))
314 goto error;
315 break;
316 case Interactive_kind:
317 seq = mod->v.Interactive.body;
318 for (i = 0; i < asdl_seq_LEN(seq); i++)
319 if (!symtable_visit_stmt(st,
320 (stmt_ty)asdl_seq_GET(seq, i)))
321 goto error;
322 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800323 case FunctionType_kind:
324 PyErr_SetString(PyExc_RuntimeError,
325 "this compiler does not handle FunctionTypes");
326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 }
Andy Lester95668422020-03-06 09:46:04 -0600328 if (!symtable_exit_block(st)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100329 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000330 return NULL;
331 }
Nick Coghlan06145232019-08-29 23:26:53 +1000332 /* Check that the recursion depth counting balanced correctly */
333 if (st->recursion_depth != starting_recursion_depth) {
334 PyErr_Format(PyExc_SystemError,
335 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
336 starting_recursion_depth, st->recursion_depth);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100337 _PySymtable_Free(st);
Nick Coghlan06145232019-08-29 23:26:53 +1000338 return NULL;
339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000340 /* Make the second symbol analysis pass */
341 if (symtable_analyze(st))
342 return st;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100343 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345 error:
Andy Lester95668422020-03-06 09:46:04 -0600346 (void) symtable_exit_block(st);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100347 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000349}
350
Victor Stinner14e461d2013-08-26 22:28:21 +0200351
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352void
Victor Stinner28ad12f2021-03-19 12:41:49 +0100353_PySymtable_Free(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000354{
Victor Stinner14e461d2013-08-26 22:28:21 +0200355 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 Py_XDECREF(st->st_blocks);
357 Py_XDECREF(st->st_stack);
358 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000359}
360
361PySTEntryObject *
362PySymtable_Lookup(struct symtable *st, void *key)
363{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 k = PyLong_FromVoidPtr(key);
367 if (k == NULL)
368 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200369 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 if (v) {
371 assert(PySTEntry_Check(v));
372 Py_INCREF(v);
373 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200374 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 PyErr_SetString(PyExc_KeyError,
376 "unknown symbol table entry");
377 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000378
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 Py_DECREF(k);
380 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000381}
382
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000383static long
384_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200386 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000387 if (!v)
388 return 0;
389 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000390 return PyLong_AS_LONG(v);
391}
392
393int
Victor Stinner28ad12f2021-03-19 12:41:49 +0100394_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000395{
396 long symbol = _PyST_GetSymbol(ste, name);
397 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000398}
399
Benjamin Petersond9c87022012-10-31 20:26:20 -0400400static int
401error_at_directive(PySTEntryObject *ste, PyObject *name)
402{
403 Py_ssize_t i;
404 PyObject *data;
405 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600406 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400407 data = PyList_GET_ITEM(ste->ste_directives, i);
408 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600409 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
410 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
411 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
412 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400413 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600414
415 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700416 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400417 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600418 PyErr_SetString(PyExc_RuntimeError,
419 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400420 return 0;
421}
422
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423
424/* Analyze raw symbol information to determine scope of each name.
425
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000426 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000428 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000429 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000430
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 explicit global is declared with the global statement. An implicit
433 global is a free variable for which the compiler has found no binding
434 in an enclosing function scope. The implicit global is either a global
435 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
436 to handle these names to implement slightly odd semantics. In such a
437 block, the name is treated as global until it is assigned to; then it
438 is treated as a local.
439
440 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000441 The first pass collects raw facts from the AST via the symtable_visit_*
442 functions: the name is a parameter here, the name is used but not defined
443 here, etc. The second pass analyzes these facts during a pass over the
444 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445
446 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000447 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000448 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000449 Names which are explicitly declared nonlocal must exist in this set of
450 visible names - if they do not, a syntax error is raised. After doing
451 the local analysis, it analyzes each of its child blocks using an
452 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453
Nick Coghlan650f0d02007-04-15 12:05:43 +0000454 The children update the free variable set. If a local variable is added to
455 the free variable set by the child, the variable is marked as a cell. The
456 function object being defined must provide runtime storage for the variable
457 that may outlive the function's frame. Cell variables are removed from the
458 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000459
Nick Coghlan650f0d02007-04-15 12:05:43 +0000460 During analysis, the names are:
461 symbols: dict mapping from symbol names to flag values (including offset scope values)
462 scopes: dict mapping from symbol names to scope values (no offset)
463 local: set of all symbol names local to the current scope
464 bound: set of all symbol names local to a containing function scope
465 free: set of all symbol names referenced but not bound in child scopes
466 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000467*/
468
469#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 PyObject *o = PyLong_FromLong(I); \
471 if (!o) \
472 return 0; \
473 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
474 Py_DECREF(o); \
475 return 0; \
476 } \
477 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000478}
479
480/* Decide on scope of name, given flags.
481
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000482 The namespace dictionaries may be modified to record information
483 about the new name. For example, a new global will add an entry to
484 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000485*/
486
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000488analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyObject *bound, PyObject *local, PyObject *free,
490 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000491{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000492 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (flags & DEF_NONLOCAL) {
494 PyErr_Format(PyExc_SyntaxError,
495 "name '%U' is nonlocal and global",
496 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400497 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
500 if (PySet_Add(global, name) < 0)
501 return 0;
502 if (bound && (PySet_Discard(bound, name) < 0))
503 return 0;
504 return 1;
505 }
506 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 if (!bound) {
508 PyErr_Format(PyExc_SyntaxError,
509 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400510 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 }
512 if (!PySet_Contains(bound, name)) {
513 PyErr_Format(PyExc_SyntaxError,
514 "no binding for nonlocal '%U' found",
515 name);
516
Benjamin Petersond9c87022012-10-31 20:26:20 -0400517 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000518 }
519 SET_SCOPE(scopes, name, FREE);
520 ste->ste_free = 1;
521 return PySet_Add(free, name) >= 0;
522 }
523 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000524 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000525 if (PySet_Add(local, name) < 0)
526 return 0;
527 if (PySet_Discard(global, name) < 0)
528 return 0;
529 return 1;
530 }
531 /* If an enclosing block has a binding for this name, it
532 is a free variable rather than a global variable.
533 Note that having a non-NULL bound implies that the block
534 is nested.
535 */
536 if (bound && PySet_Contains(bound, name)) {
537 SET_SCOPE(scopes, name, FREE);
538 ste->ste_free = 1;
539 return PySet_Add(free, name) >= 0;
540 }
541 /* If a parent has a global statement, then call it global
542 explicit? It could also be global implicit.
543 */
544 if (global && PySet_Contains(global, name)) {
545 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
546 return 1;
547 }
548 if (ste->ste_nested)
549 ste->ste_free = 1;
550 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
551 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552}
553
554#undef SET_SCOPE
555
556/* If a name is defined in free and also in locals, then this block
557 provides the binding for the free variable. The name should be
558 marked CELL in this block and removed from the free list.
559
560 Note that the current block's free variables are included in free.
561 That's safe because no name can be free and local in the same scope.
562*/
563
564static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500565analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000566{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000567 PyObject *name, *v, *v_cell;
568 int success = 0;
569 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 v_cell = PyLong_FromLong(CELL);
572 if (!v_cell)
573 return 0;
574 while (PyDict_Next(scopes, &pos, &name, &v)) {
575 long scope;
576 assert(PyLong_Check(v));
577 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000578 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 continue;
580 if (!PySet_Contains(free, name))
581 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000582 /* Replace LOCAL with CELL for this name, and remove
583 from free. It is safe to replace the value of name
584 in the dict, because it will not cause a resize.
585 */
586 if (PyDict_SetItem(scopes, name, v_cell) < 0)
587 goto error;
588 if (PySet_Discard(free, name) < 0)
589 goto error;
590 }
591 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000592 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000593 Py_DECREF(v_cell);
594 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000595}
596
Benjamin Peterson312595c2013-05-15 15:26:42 -0500597static int
598drop_class_free(PySTEntryObject *ste, PyObject *free)
599{
600 int res;
601 if (!GET_IDENTIFIER(__class__))
602 return 0;
603 res = PySet_Discard(free, __class__);
604 if (res < 0)
605 return 0;
606 if (res)
607 ste->ste_needs_class_closure = 1;
608 return 1;
609}
610
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000611/* Enter the final scope information into the ste_symbols dict.
612 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000613 * All arguments are dicts. Modifies symbols, others are read-only.
614*/
615static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000616update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000617 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 PyObject *name = NULL, *itr = NULL;
620 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
621 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 /* Update scope information for all symbols in this scope */
624 while (PyDict_Next(symbols, &pos, &name, &v)) {
625 long scope, flags;
626 assert(PyLong_Check(v));
627 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200628 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000629 assert(v_scope && PyLong_Check(v_scope));
630 scope = PyLong_AS_LONG(v_scope);
631 flags |= (scope << SCOPE_OFFSET);
632 v_new = PyLong_FromLong(flags);
633 if (!v_new)
634 return 0;
635 if (PyDict_SetItem(symbols, name, v_new) < 0) {
636 Py_DECREF(v_new);
637 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000638 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000639 Py_DECREF(v_new);
640 }
641
642 /* Record not yet resolved free variables from children (if any) */
643 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
644 if (!v_free)
645 return 0;
646
647 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600648 if (itr == NULL) {
649 Py_DECREF(v_free);
650 return 0;
651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000652
653 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200654 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000655
656 /* Handle symbol that already exists in this scope */
657 if (v) {
658 /* Handle a free variable in a method of
659 the class that has the same name as a local
660 or global in the class scope.
661 */
662 if (classflag &&
663 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
664 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
665 v_new = PyLong_FromLong(flags);
666 if (!v_new) {
667 goto error;
668 }
669 if (PyDict_SetItem(symbols, name, v_new) < 0) {
670 Py_DECREF(v_new);
671 goto error;
672 }
673 Py_DECREF(v_new);
674 }
675 /* It's a cell, or already free in this scope */
676 Py_DECREF(name);
677 continue;
678 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200679 else if (PyErr_Occurred()) {
680 goto error;
681 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000682 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200683 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 Py_DECREF(name);
685 continue; /* it's a global */
686 }
687 /* Propagate new free symbol up the lexical stack */
688 if (PyDict_SetItem(symbols, name, v_free) < 0) {
689 goto error;
690 }
691 Py_DECREF(name);
692 }
693 Py_DECREF(itr);
694 Py_DECREF(v_free);
695 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000696error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000697 Py_XDECREF(v_free);
698 Py_XDECREF(itr);
699 Py_XDECREF(name);
700 return 0;
701}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000702
703/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000704
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705 Arguments:
706 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000707 bound -- set of variables bound in enclosing scopes (input). bound
708 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709 free -- set of free variables in enclosed scopes (output)
710 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000711
712 The implementation uses two mutually recursive functions,
713 analyze_block() and analyze_child_block(). analyze_block() is
714 responsible for analyzing the individual names defined in a block.
715 analyze_child_block() prepares temporary namespace dictionaries
716 used to evaluated nested blocks.
717
718 The two functions exist because a child block should see the name
719 bindings of its enclosing blocks, but those bindings should not
720 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000721*/
722
723static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000724analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
725 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000726
727static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
729 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000731 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
732 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
733 PyObject *temp;
734 int i, success = 0;
735 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000736
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 local = PySet_New(NULL); /* collect new names bound in block */
738 if (!local)
739 goto error;
740 scopes = PyDict_New(); /* collect scopes defined for each name */
741 if (!scopes)
742 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000743
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000744 /* Allocate new global and bound variable dictionaries. These
745 dictionaries hold the names visible in nested blocks. For
746 ClassBlocks, the bound and global names are initialized
747 before analyzing names, because class bindings aren't
748 visible in methods. For other blocks, they are initialized
749 after names are analyzed.
750 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 /* TODO(jhylton): Package these dicts in a struct so that we
753 can write reasonable helper functions?
754 */
755 newglobal = PySet_New(NULL);
756 if (!newglobal)
757 goto error;
758 newfree = PySet_New(NULL);
759 if (!newfree)
760 goto error;
761 newbound = PySet_New(NULL);
762 if (!newbound)
763 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000764
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000765 /* Class namespace has no effect on names visible in
766 nested functions, so populate the global and bound
767 sets to be passed to child blocks before analyzing
768 this one.
769 */
770 if (ste->ste_type == ClassBlock) {
771 /* Pass down known globals */
772 temp = PyNumber_InPlaceOr(newglobal, global);
773 if (!temp)
774 goto error;
775 Py_DECREF(temp);
776 /* Pass down previously bound symbols */
777 if (bound) {
778 temp = PyNumber_InPlaceOr(newbound, bound);
779 if (!temp)
780 goto error;
781 Py_DECREF(temp);
782 }
783 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000784
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000785 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
786 long flags = PyLong_AS_LONG(v);
787 if (!analyze_name(ste, scopes, name, flags,
788 bound, local, free, global))
789 goto error;
790 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000791
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 /* Populate global and bound sets to be passed to children. */
793 if (ste->ste_type != ClassBlock) {
794 /* Add function locals to bound set */
795 if (ste->ste_type == FunctionBlock) {
796 temp = PyNumber_InPlaceOr(newbound, local);
797 if (!temp)
798 goto error;
799 Py_DECREF(temp);
800 }
801 /* Pass down previously bound symbols */
802 if (bound) {
803 temp = PyNumber_InPlaceOr(newbound, bound);
804 if (!temp)
805 goto error;
806 Py_DECREF(temp);
807 }
808 /* Pass down known globals */
809 temp = PyNumber_InPlaceOr(newglobal, global);
810 if (!temp)
811 goto error;
812 Py_DECREF(temp);
813 }
814 else {
815 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000816 if (!GET_IDENTIFIER(__class__))
817 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000818 if (PySet_Add(newbound, __class__) < 0)
819 goto error;
820 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000821
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300822 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 newbound, newglobal now contain the names visible in
825 nested blocks. The free variables in the children will
826 be collected in allfree.
827 */
828 allfree = PySet_New(NULL);
829 if (!allfree)
830 goto error;
831 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
832 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
833 PySTEntryObject* entry;
834 assert(c && PySTEntry_Check(c));
835 entry = (PySTEntryObject*)c;
836 if (!analyze_child_block(entry, newbound, newfree, newglobal,
837 allfree))
838 goto error;
839 /* Check if any children have free variables */
840 if (entry->ste_free || entry->ste_child_free)
841 ste->ste_child_free = 1;
842 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000843
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000844 temp = PyNumber_InPlaceOr(newfree, allfree);
845 if (!temp)
846 goto error;
847 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000849 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500850 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500852 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 goto error;
854 /* Records the results of the analysis in the symbol table entry */
855 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
856 ste->ste_type == ClassBlock))
857 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000858
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 temp = PyNumber_InPlaceOr(free, newfree);
860 if (!temp)
861 goto error;
862 Py_DECREF(temp);
863 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000864 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000865 Py_XDECREF(scopes);
866 Py_XDECREF(local);
867 Py_XDECREF(newbound);
868 Py_XDECREF(newglobal);
869 Py_XDECREF(newfree);
870 Py_XDECREF(allfree);
871 if (!success)
872 assert(PyErr_Occurred());
873 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000874}
875
876static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
878 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000879{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
881 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000884
Martin Panter3ee62702016-06-04 04:57:19 +0000885 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 current block. The analyze_block() call modifies these
887 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000888
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 */
890 temp_bound = PySet_New(bound);
891 if (!temp_bound)
892 goto error;
893 temp_free = PySet_New(free);
894 if (!temp_free)
895 goto error;
896 temp_global = PySet_New(global);
897 if (!temp_global)
898 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
901 goto error;
902 temp = PyNumber_InPlaceOr(child_free, temp_free);
903 if (!temp)
904 goto error;
905 Py_DECREF(temp);
906 Py_DECREF(temp_bound);
907 Py_DECREF(temp_free);
908 Py_DECREF(temp_global);
909 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000910 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000911 Py_XDECREF(temp_bound);
912 Py_XDECREF(temp_free);
913 Py_XDECREF(temp_global);
914 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000915}
916
917static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918symtable_analyze(struct symtable *st)
919{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 PyObject *free, *global;
921 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 free = PySet_New(NULL);
924 if (!free)
925 return 0;
926 global = PySet_New(NULL);
927 if (!global) {
928 Py_DECREF(free);
929 return 0;
930 }
931 r = analyze_block(st->st_top, NULL, free, global);
932 Py_DECREF(free);
933 Py_DECREF(global);
934 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000935}
936
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000937/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938 This reference is released when the block is exited, via the DECREF
939 in symtable_exit_block().
940*/
941
942static int
Andy Lester95668422020-03-06 09:46:04 -0600943symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944{
Benjamin Peterson609da582011-06-29 22:52:39 -0500945 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946
Benjamin Peterson609da582011-06-29 22:52:39 -0500947 st->st_cur = NULL;
948 size = PyList_GET_SIZE(st->st_stack);
949 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500950 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500952 if (--size)
953 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 }
955 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000956}
957
958static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000960 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961{
Benjamin Peterson609da582011-06-29 22:52:39 -0500962 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963
Benjamin Peterson609da582011-06-29 22:52:39 -0500964 ste = ste_new(st, name, block, ast, lineno, col_offset);
965 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500967 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
968 Py_DECREF(ste);
969 return 0;
970 }
971 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000972 /* bpo-37757: For now, disallow *all* assignment expressions in the
973 * outermost iterator expression of a comprehension, even those inside
974 * a nested comprehension or a lambda expression.
975 */
976 if (prev) {
977 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
978 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500979 /* The entry is owned by the stack. Borrow it for st_cur. */
980 Py_DECREF(ste);
981 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000982 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 st->st_global = st->st_cur->ste_symbols;
984 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500985 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000986 return 0;
987 }
988 }
989 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000990}
991
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000992static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000993symtable_lookup(struct symtable *st, PyObject *name)
994{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 PyObject *mangled = _Py_Mangle(st->st_private, name);
996 if (!mangled)
997 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +0100998 long ret = _PyST_GetSymbol(st->st_cur, mangled);
999 Py_DECREF(mangled);
1000 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001001}
1002
1003static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001004symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001006 PyObject *o;
1007 PyObject *dict;
1008 long val;
1009 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010
Jeremy Hylton81e95022007-02-27 06:50:52 +00001011
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001012 if (!mangled)
1013 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001014 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001015 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 val = PyLong_AS_LONG(o);
1017 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1018 /* Is it better to use 'mangled' or 'name' here? */
1019 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001020 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001021 ste->ste_lineno,
1022 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001023 goto error;
1024 }
1025 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001026 }
1027 else if (PyErr_Occurred()) {
1028 goto error;
1029 }
1030 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001032 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001033 if (ste->ste_comp_iter_target) {
1034 /* This name is an iteration variable in a comprehension,
1035 * so check for a binding conflict with any named expressions.
1036 * Otherwise, mark it as an iteration variable so subsequent
1037 * named expressions can check for conflicts.
1038 */
1039 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1040 PyErr_Format(PyExc_SyntaxError,
1041 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1042 PyErr_SyntaxLocationObject(st->st_filename,
1043 ste->ste_lineno,
1044 ste->ste_col_offset + 1);
1045 goto error;
1046 }
1047 val |= DEF_COMP_ITER;
1048 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 o = PyLong_FromLong(val);
1050 if (o == NULL)
1051 goto error;
1052 if (PyDict_SetItem(dict, mangled, o) < 0) {
1053 Py_DECREF(o);
1054 goto error;
1055 }
1056 Py_DECREF(o);
1057
1058 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001059 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 goto error;
1061 } else if (flag & DEF_GLOBAL) {
1062 /* XXX need to update DEF_GLOBAL for other flags too;
1063 perhaps only DEF_FREE_GLOBAL */
1064 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001065 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 val |= PyLong_AS_LONG(o);
1067 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001068 else if (PyErr_Occurred()) {
1069 goto error;
1070 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001071 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001072 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 goto error;
1074 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1075 Py_DECREF(o);
1076 goto error;
1077 }
1078 Py_DECREF(o);
1079 }
1080 Py_DECREF(mangled);
1081 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001082
1083error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001084 Py_DECREF(mangled);
1085 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086}
1087
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001088static int
1089symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1090 return symtable_add_def_helper(st, name, flag, st->st_cur);
1091}
1092
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001093/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1094 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001095 function.
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1098 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001099
1100 VISIT_QUIT macro returns the specified value exiting from the function but
1101 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102*/
1103
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001104#define VISIT_QUIT(ST, X) \
1105 return --(ST)->recursion_depth,(X)
1106
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001108 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001109 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001110
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001112 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001113 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1115 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1116 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001117 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001119}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001120
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001121#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001122 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001123 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001124 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1125 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1126 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001127 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001128 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001129}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001130
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001131#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001132 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001133 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001135 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001136 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001137 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 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001140}
1141
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001142static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001143symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001144{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001145 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001146 int res;
1147 if (!st->st_cur->ste_directives) {
1148 st->st_cur->ste_directives = PyList_New(0);
1149 if (!st->st_cur->ste_directives)
1150 return 0;
1151 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001152 mangled = _Py_Mangle(st->st_private, name);
1153 if (!mangled)
1154 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001155 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001156 if (!data)
1157 return 0;
1158 res = PyList_Append(st->st_cur->ste_directives, data);
1159 Py_DECREF(data);
1160 return res == 0;
1161}
1162
1163
1164static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001165symtable_visit_stmt(struct symtable *st, stmt_ty s)
1166{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001167 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001168 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001169 "maximum recursion depth exceeded during compilation");
1170 VISIT_QUIT(st, 0);
1171 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 switch (s->kind) {
1173 case FunctionDef_kind:
1174 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001175 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001176 if (s->v.FunctionDef.args->defaults)
1177 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1178 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001179 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001180 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001181 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001182 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001183 if (s->v.FunctionDef.decorator_list)
1184 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1185 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001186 FunctionBlock, (void *)s, s->lineno,
1187 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001188 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001189 VISIT(st, arguments, s->v.FunctionDef.args);
1190 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001191 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001192 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001193 break;
1194 case ClassDef_kind: {
1195 PyObject *tmp;
1196 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001197 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001198 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1199 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001200 if (s->v.ClassDef.decorator_list)
1201 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1202 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001203 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001204 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 tmp = st->st_private;
1206 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001207 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001209 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001210 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001211 break;
1212 }
1213 case Return_kind:
1214 if (s->v.Return.value) {
1215 VISIT(st, expr, s->v.Return.value);
1216 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 }
1218 break;
1219 case Delete_kind:
1220 VISIT_SEQ(st, expr, s->v.Delete.targets);
1221 break;
1222 case Assign_kind:
1223 VISIT_SEQ(st, expr, s->v.Assign.targets);
1224 VISIT(st, expr, s->v.Assign.value);
1225 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001226 case AnnAssign_kind:
1227 if (s->v.AnnAssign.target->kind == Name_kind) {
1228 expr_ty e_name = s->v.AnnAssign.target;
1229 long cur = symtable_lookup(st, e_name->v.Name.id);
1230 if (cur < 0) {
1231 VISIT_QUIT(st, 0);
1232 }
1233 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001234 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001235 && s->v.AnnAssign.simple) {
1236 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001237 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1238 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001239 PyErr_SyntaxLocationObject(st->st_filename,
1240 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001241 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001242 VISIT_QUIT(st, 0);
1243 }
1244 if (s->v.AnnAssign.simple &&
1245 !symtable_add_def(st, e_name->v.Name.id,
1246 DEF_ANNOT | DEF_LOCAL)) {
1247 VISIT_QUIT(st, 0);
1248 }
1249 else {
1250 if (s->v.AnnAssign.value
1251 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1252 VISIT_QUIT(st, 0);
1253 }
1254 }
1255 }
1256 else {
1257 VISIT(st, expr, s->v.AnnAssign.target);
1258 }
1259 VISIT(st, expr, s->v.AnnAssign.annotation);
1260 if (s->v.AnnAssign.value) {
1261 VISIT(st, expr, s->v.AnnAssign.value);
1262 }
1263 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 case AugAssign_kind:
1265 VISIT(st, expr, s->v.AugAssign.target);
1266 VISIT(st, expr, s->v.AugAssign.value);
1267 break;
1268 case For_kind:
1269 VISIT(st, expr, s->v.For.target);
1270 VISIT(st, expr, s->v.For.iter);
1271 VISIT_SEQ(st, stmt, s->v.For.body);
1272 if (s->v.For.orelse)
1273 VISIT_SEQ(st, stmt, s->v.For.orelse);
1274 break;
1275 case While_kind:
1276 VISIT(st, expr, s->v.While.test);
1277 VISIT_SEQ(st, stmt, s->v.While.body);
1278 if (s->v.While.orelse)
1279 VISIT_SEQ(st, stmt, s->v.While.orelse);
1280 break;
1281 case If_kind:
1282 /* XXX if 0: and lookup_yield() hacks */
1283 VISIT(st, expr, s->v.If.test);
1284 VISIT_SEQ(st, stmt, s->v.If.body);
1285 if (s->v.If.orelse)
1286 VISIT_SEQ(st, stmt, s->v.If.orelse);
1287 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001288 case Match_kind:
1289 VISIT(st, expr, s->v.Match.subject);
1290 VISIT_SEQ(st, match_case, s->v.Match.cases);
1291 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 case Raise_kind:
1293 if (s->v.Raise.exc) {
1294 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001295 if (s->v.Raise.cause) {
1296 VISIT(st, expr, s->v.Raise.cause);
1297 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 }
1299 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001300 case Try_kind:
1301 VISIT_SEQ(st, stmt, s->v.Try.body);
1302 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1303 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1304 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001305 break;
1306 case Assert_kind:
1307 VISIT(st, expr, s->v.Assert.test);
1308 if (s->v.Assert.msg)
1309 VISIT(st, expr, s->v.Assert.msg);
1310 break;
1311 case Import_kind:
1312 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 break;
1314 case ImportFrom_kind:
1315 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 break;
1317 case Global_kind: {
1318 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001319 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1321 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001322 long cur = symtable_lookup(st, name);
1323 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001324 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001325 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1326 const char* msg;
1327 if (cur & DEF_PARAM) {
1328 msg = GLOBAL_PARAM;
1329 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001330 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001331 } else if (cur & DEF_ANNOT) {
1332 msg = GLOBAL_ANNOT;
1333 } else { /* DEF_LOCAL */
1334 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001335 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001336 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001337 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001338 PyErr_SyntaxLocationObject(st->st_filename,
1339 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001340 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001341 VISIT_QUIT(st, 0);
1342 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001344 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001345 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001346 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001347 }
1348 break;
1349 }
1350 case Nonlocal_kind: {
1351 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001352 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001353 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1354 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 long cur = symtable_lookup(st, name);
1356 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001357 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001358 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1359 const char* msg;
1360 if (cur & DEF_PARAM) {
1361 msg = NONLOCAL_PARAM;
1362 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001363 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001364 } else if (cur & DEF_ANNOT) {
1365 msg = NONLOCAL_ANNOT;
1366 } else { /* DEF_LOCAL */
1367 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001368 }
1369 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001370 PyErr_SyntaxLocationObject(st->st_filename,
1371 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001372 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001373 VISIT_QUIT(st, 0);
1374 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001375 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001376 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001377 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001378 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 }
1380 break;
1381 }
1382 case Expr_kind:
1383 VISIT(st, expr, s->v.Expr.value);
1384 break;
1385 case Pass_kind:
1386 case Break_kind:
1387 case Continue_kind:
1388 /* nothing to do here */
1389 break;
1390 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001391 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 VISIT_SEQ(st, stmt, s->v.With.body);
1393 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001394 case AsyncFunctionDef_kind:
1395 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1396 VISIT_QUIT(st, 0);
1397 if (s->v.AsyncFunctionDef.args->defaults)
1398 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1399 if (s->v.AsyncFunctionDef.args->kw_defaults)
1400 VISIT_SEQ_WITH_NULL(st, expr,
1401 s->v.AsyncFunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001402 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001403 s->v.AsyncFunctionDef.returns))
1404 VISIT_QUIT(st, 0);
1405 if (s->v.AsyncFunctionDef.decorator_list)
1406 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1407 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1408 FunctionBlock, (void *)s, s->lineno,
1409 s->col_offset))
1410 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001411 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001412 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1413 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001414 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001415 VISIT_QUIT(st, 0);
1416 break;
1417 case AsyncWith_kind:
1418 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1419 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1420 break;
1421 case AsyncFor_kind:
1422 VISIT(st, expr, s->v.AsyncFor.target);
1423 VISIT(st, expr, s->v.AsyncFor.iter);
1424 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1425 if (s->v.AsyncFor.orelse)
1426 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1427 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001429 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001430}
1431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001433symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1434{
1435 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001436 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001437
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001438 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001439 Py_ssize_t i, size;
1440 struct _symtable_entry *ste;
1441 size = PyList_GET_SIZE(st->st_stack);
1442 assert(size);
1443
1444 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1445 for (i = size - 1; i >= 0; i--) {
1446 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1447
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001448 /* If we find a comprehension scope, check for a target
1449 * binding conflict with iteration variables, otherwise skip it
1450 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001451 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001452 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1453 if (target_in_scope & DEF_COMP_ITER) {
1454 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1455 PyErr_SyntaxLocationObject(st->st_filename,
1456 e->lineno,
1457 e->col_offset);
1458 VISIT_QUIT(st, 0);
1459 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001460 continue;
1461 }
1462
Pablo Galindofd5c4142019-10-14 05:18:05 +01001463 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001464 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001465 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1466 if (target_in_scope & DEF_GLOBAL) {
1467 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1468 VISIT_QUIT(st, 0);
1469 } else {
1470 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1471 VISIT_QUIT(st, 0);
1472 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001473 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001474 VISIT_QUIT(st, 0);
1475
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001476 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001477 }
1478 /* If we find a ModuleBlock entry, add as GLOBAL */
1479 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001480 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001481 VISIT_QUIT(st, 0);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001482 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001483 VISIT_QUIT(st, 0);
1484
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001485 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001486 }
1487 /* Disallow usage in ClassBlock */
1488 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001489 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001490 PyErr_SyntaxLocationObject(st->st_filename,
1491 e->lineno,
1492 e->col_offset);
1493 VISIT_QUIT(st, 0);
1494 }
1495 }
1496
1497 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1498 and should never fall to this case
1499 */
1500 assert(0);
1501 return 0;
1502}
1503
1504static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001505symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1506{
1507 if (st->st_cur->ste_comp_iter_expr > 0) {
1508 /* Assignment isn't allowed in a comprehension iterable expression */
1509 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1510 PyErr_SyntaxLocationObject(st->st_filename,
1511 e->lineno,
1512 e->col_offset);
Nick Coghlan06145232019-08-29 23:26:53 +10001513 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001514 }
1515 if (st->st_cur->ste_comprehension) {
1516 /* Inside a comprehension body, so find the right target scope */
1517 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001518 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001519 }
1520 VISIT(st, expr, e->v.NamedExpr.value);
1521 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001522 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001523}
1524
1525static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001526symtable_visit_expr(struct symtable *st, expr_ty e)
1527{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001528 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001529 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001530 "maximum recursion depth exceeded during compilation");
1531 VISIT_QUIT(st, 0);
1532 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001534 case NamedExpr_kind:
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001535 if(!symtable_handle_namedexpr(st, e))
1536 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001537 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 case BoolOp_kind:
1539 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1540 break;
1541 case BinOp_kind:
1542 VISIT(st, expr, e->v.BinOp.left);
1543 VISIT(st, expr, e->v.BinOp.right);
1544 break;
1545 case UnaryOp_kind:
1546 VISIT(st, expr, e->v.UnaryOp.operand);
1547 break;
1548 case Lambda_kind: {
1549 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001550 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001551 if (e->v.Lambda.args->defaults)
1552 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001553 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001554 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001556 FunctionBlock, (void *)e, e->lineno,
1557 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001558 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001559 VISIT(st, arguments, e->v.Lambda.args);
1560 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001561 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001562 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 break;
1564 }
1565 case IfExp_kind:
1566 VISIT(st, expr, e->v.IfExp.test);
1567 VISIT(st, expr, e->v.IfExp.body);
1568 VISIT(st, expr, e->v.IfExp.orelse);
1569 break;
1570 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001571 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 VISIT_SEQ(st, expr, e->v.Dict.values);
1573 break;
1574 case Set_kind:
1575 VISIT_SEQ(st, expr, e->v.Set.elts);
1576 break;
1577 case GeneratorExp_kind:
1578 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001579 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001580 break;
1581 case ListComp_kind:
1582 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001583 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 break;
1585 case SetComp_kind:
1586 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001587 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 break;
1589 case DictComp_kind:
1590 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001591 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592 break;
1593 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001594 if (e->v.Yield.value)
1595 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001598 case YieldFrom_kind:
1599 VISIT(st, expr, e->v.YieldFrom.value);
1600 st->st_cur->ste_generator = 1;
1601 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001602 case Await_kind:
1603 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001604 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001605 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001606 case Compare_kind:
1607 VISIT(st, expr, e->v.Compare.left);
1608 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1609 break;
1610 case Call_kind:
1611 VISIT(st, expr, e->v.Call.func);
1612 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001613 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001615 case FormattedValue_kind:
1616 VISIT(st, expr, e->v.FormattedValue.value);
1617 if (e->v.FormattedValue.format_spec)
1618 VISIT(st, expr, e->v.FormattedValue.format_spec);
1619 break;
1620 case JoinedStr_kind:
1621 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1622 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001623 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001624 /* Nothing to do here. */
1625 break;
1626 /* The following exprs can be assignment targets. */
1627 case Attribute_kind:
1628 VISIT(st, expr, e->v.Attribute.value);
1629 break;
1630 case Subscript_kind:
1631 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001632 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 break;
1634 case Starred_kind:
1635 VISIT(st, expr, e->v.Starred.value);
1636 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001637 case Slice_kind:
1638 if (e->v.Slice.lower)
1639 VISIT(st, expr, e->v.Slice.lower)
1640 if (e->v.Slice.upper)
1641 VISIT(st, expr, e->v.Slice.upper)
1642 if (e->v.Slice.step)
1643 VISIT(st, expr, e->v.Slice.step)
1644 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001645 case Name_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08001646 // Don't make "_" a local when used in a pattern:
1647 if (st->in_pattern &&
1648 e->v.Name.ctx == Store &&
1649 _PyUnicode_EqualToASCIIString(e->v.Name.id, "_"))
1650 {
1651 break;
1652 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 if (!symtable_add_def(st, e->v.Name.id,
1654 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001655 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656 /* Special-case super: it counts as a use of __class__ */
1657 if (e->v.Name.ctx == Load &&
1658 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001659 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001660 if (!GET_IDENTIFIER(__class__) ||
1661 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001662 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 }
1664 break;
1665 /* child nodes of List and Tuple will have expr_context set */
1666 case List_kind:
1667 VISIT_SEQ(st, expr, e->v.List.elts);
1668 break;
1669 case Tuple_kind:
1670 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1671 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001672 case MatchAs_kind:
1673 VISIT(st, expr, e->v.MatchAs.pattern);
1674 symtable_add_def(st, e->v.MatchAs.name, DEF_LOCAL);
1675 break;
1676 case MatchOr_kind:
1677 VISIT_SEQ(st, expr, e->v.MatchOr.patterns);
1678 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001680 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681}
1682
1683static int
1684symtable_implicit_arg(struct symtable *st, int pos)
1685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1687 if (id == NULL)
1688 return 0;
1689 if (!symtable_add_def(st, id, DEF_PARAM)) {
1690 Py_DECREF(id);
1691 return 0;
1692 }
1693 Py_DECREF(id);
1694 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695}
1696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001698symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001701
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 if (!args)
1703 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 for (i = 0; i < asdl_seq_LEN(args); i++) {
1706 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1707 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1708 return 0;
1709 }
1710
1711 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001712}
1713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001715symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001716{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001719 if (!args)
1720 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001721
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 for (i = 0; i < asdl_seq_LEN(args); i++) {
1723 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1724 if (arg->annotation)
1725 VISIT(st, expr, arg->annotation);
1726 }
1727
1728 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001729}
1730
Neal Norwitzc1505362006-12-28 06:47:50 +00001731static int
Andy Lester95668422020-03-06 09:46:04 -06001732symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001733{
Anthony Sottileec007cb2020-01-04 20:57:21 -05001734 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1735 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 if (a->args && !symtable_visit_argannotations(st, a->args))
1737 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001738 if (a->vararg && a->vararg->annotation)
1739 VISIT(st, expr, a->vararg->annotation);
1740 if (a->kwarg && a->kwarg->annotation)
1741 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1743 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001744 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001745 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001747}
1748
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001749static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001750symtable_visit_arguments(struct symtable *st, arguments_ty a)
1751{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001752 /* skip default arguments inside function block
1753 XXX should ast be different?
1754 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001755 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1756 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001757 if (a->args && !symtable_visit_params(st, a->args))
1758 return 0;
1759 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1760 return 0;
1761 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001762 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 return 0;
1764 st->st_cur->ste_varargs = 1;
1765 }
1766 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001767 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001768 return 0;
1769 st->st_cur->ste_varkeywords = 1;
1770 }
1771 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001772}
1773
1774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 if (eh->v.ExceptHandler.type)
1779 VISIT(st, expr, eh->v.ExceptHandler.type);
1780 if (eh->v.ExceptHandler.name)
1781 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1782 return 0;
1783 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1784 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001785}
1786
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001787static int
1788symtable_visit_withitem(struct symtable *st, withitem_ty item)
1789{
1790 VISIT(st, expr, item->context_expr);
1791 if (item->optional_vars) {
1792 VISIT(st, expr, item->optional_vars);
1793 }
1794 return 1;
1795}
1796
Brandt Bucher145bf262021-02-26 14:51:55 -08001797static int
1798symtable_visit_match_case(struct symtable *st, match_case_ty m)
1799{
1800 assert(!st->in_pattern);
1801 st->in_pattern = 1;
1802 VISIT(st, expr, m->pattern);
1803 assert(st->in_pattern);
1804 st->in_pattern = 0;
1805 if (m->guard) {
1806 VISIT(st, expr, m->guard);
1807 }
1808 VISIT_SEQ(st, stmt, m->body);
1809 return 1;
1810}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001811
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001812static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001813symtable_visit_alias(struct symtable *st, alias_ty a)
1814{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001816 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001817 dotted package name (e.g. spam.eggs)
1818 */
1819 PyObject *store_name;
1820 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001821 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1822 PyUnicode_GET_LENGTH(name), 1);
1823 if (dot != -1) {
1824 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001825 if (!store_name)
1826 return 0;
1827 }
1828 else {
1829 store_name = name;
1830 Py_INCREF(store_name);
1831 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001832 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1834 Py_DECREF(store_name);
1835 return r;
1836 }
1837 else {
1838 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001839 int lineno = st->st_cur->ste_lineno;
1840 int col_offset = st->st_cur->ste_col_offset;
1841 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001842 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001843 Py_DECREF(store_name);
1844 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001845 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 Py_DECREF(store_name);
1847 return 1;
1848 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001849}
1850
1851
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001852static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1854{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001855 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001856 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001857 st->st_cur->ste_comp_iter_target = 0;
1858 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001860 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001862 if (lc->is_async) {
1863 st->st_cur->ste_coroutine = 1;
1864 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001866}
1867
1868
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001870symtable_visit_keyword(struct symtable *st, keyword_ty k)
1871{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 VISIT(st, expr, k->value);
1873 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001874}
1875
1876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001878symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01001879 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001880 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001881{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001882 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 comprehension_ty outermost = ((comprehension_ty)
1884 asdl_seq_GET(generators, 0));
1885 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001886 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001887 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001888 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 /* Create comprehension scope for the rest */
1890 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001891 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1892 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001893 return 0;
1894 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001895 if (outermost->is_async) {
1896 st->st_cur->ste_coroutine = 1;
1897 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001898 st->st_cur->ste_comprehension = 1;
1899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 /* Outermost iter is received as an argument */
1901 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06001902 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903 return 0;
1904 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001905 /* Visit iteration variable target, and mark them as such */
1906 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001907 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001908 st->st_cur->ste_comp_iter_target = 0;
1909 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001910 VISIT_SEQ(st, expr, outermost->ifs);
1911 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001912 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001913 VISIT(st, expr, value);
1914 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001915 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001916 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001917 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1918 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1919 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1920 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001921 PyErr_SyntaxLocationObject(st->st_filename,
1922 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001923 st->st_cur->ste_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06001924 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001925 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001926 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001927 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06001928 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001929}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001930
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001931static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001932symtable_visit_genexp(struct symtable *st, expr_ty e)
1933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1935 e->v.GeneratorExp.generators,
1936 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001937}
1938
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001939static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001940symtable_visit_listcomp(struct symtable *st, expr_ty e)
1941{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001942 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1943 e->v.ListComp.generators,
1944 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001945}
1946
1947static int
1948symtable_visit_setcomp(struct symtable *st, expr_ty e)
1949{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001950 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1951 e->v.SetComp.generators,
1952 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001953}
1954
1955static int
1956symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1957{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001958 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1959 e->v.DictComp.generators,
1960 e->v.DictComp.key,
1961 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001962}
Victor Stinner28ad12f2021-03-19 12:41:49 +01001963
1964
1965struct symtable *
1966_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
1967 int start, PyCompilerFlags *flags)
1968{
1969 struct symtable *st;
1970 mod_ty mod;
1971 PyArena *arena;
1972
1973 arena = PyArena_New();
1974 if (arena == NULL)
1975 return NULL;
1976
1977 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1978 if (mod == NULL) {
1979 PyArena_Free(arena);
1980 return NULL;
1981 }
1982 st = _PySymtable_Build(mod, filename, 0);
1983 PyArena_Free(arena);
1984 return st;
1985}