blob: cce1b1b5f3226a83e34dd08a522c16890bc13cb3 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinnere5014be2020-04-14 17:52:15 +02002#include "pycore_pystate.h" // _PyThreadState_GET()
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003#include "symtable.h"
Victor Stinner3bb183d2018-11-22 18:38:38 +01004#undef Yield /* undefine macro conflicting with <winbase.h> */
Victor Stinner4a21e572020-04-15 02:35:41 +02005#include "structmember.h" // PyMemberDef
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00006
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02008#define GLOBAL_PARAM \
9"name '%U' is parameter and global"
10
11#define NONLOCAL_PARAM \
12"name '%U' is parameter and nonlocal"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070015"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070018"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070021"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000022
Jeremy Hylton81e95022007-02-27 06:50:52 +000023#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070024"name '%U' is used prior to nonlocal declaration"
25
26#define GLOBAL_ANNOT \
27"annotated name '%U' can't be global"
28
29#define NONLOCAL_ANNOT \
30"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000031
Neal Norwitz5d0ad502005-12-19 04:27:42 +000032#define IMPORT_STAR_WARNING "import * only allowed at module level"
33
Emily Morehouse8f59ee02019-01-24 16:49:56 -070034#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100035"assignment expression within a comprehension cannot be used in a class body"
36
37#define NAMED_EXPR_COMP_CONFLICT \
38"assignment expression cannot rebind comprehension iteration variable '%U'"
39
40#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
41"comprehension inner loop cannot rebind assignment expression target '%U'"
42
43#define NAMED_EXPR_COMP_ITER_EXPR \
44"assignment expression cannot be used in a comprehension iterable expression"
Emily Morehouse8f59ee02019-01-24 16:49:56 -070045
Neal Norwitz090b3dd2006-02-28 22:36:46 +000046static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000047ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000048 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020051 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 k = PyLong_FromVoidPtr(key);
54 if (k == NULL)
55 goto fail;
56 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020057 if (ste == NULL) {
58 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020062 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020065 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ste->ste_symbols = NULL;
68 ste->ste_varnames = NULL;
69 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Benjamin Petersond9c87022012-10-31 20:26:20 -040071 ste->ste_directives = NULL;
72
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 ste->ste_nested = 0;
75 ste->ste_free = 0;
76 ste->ste_varargs = 0;
77 ste->ste_varkeywords = 0;
78 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000079 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000081 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if (st->st_cur != NULL &&
84 (st->st_cur->ste_nested ||
85 st->st_cur->ste_type == FunctionBlock))
86 ste->ste_nested = 1;
87 ste->ste_child_free = 0;
88 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070089 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070090 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050092 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100093 ste->ste_comp_iter_target = 0;
94 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095
Victor Stinner9a4fb662013-07-11 22:49:00 +020096 ste->ste_symbols = PyDict_New();
97 ste->ste_varnames = PyList_New(0);
98 ste->ste_children = PyList_New(0);
99 if (ste->ste_symbols == NULL
100 || ste->ste_varnames == NULL
101 || ste->ste_children == NULL)
102 goto fail;
103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
105 goto fail;
106
107 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_XDECREF(ste);
110 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111}
112
113static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
117 ste->ste_name,
118 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119}
120
121static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 ste->ste_table = NULL;
125 Py_XDECREF(ste->ste_id);
126 Py_XDECREF(ste->ste_name);
127 Py_XDECREF(ste->ste_symbols);
128 Py_XDECREF(ste->ste_varnames);
129 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400130 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100131 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132}
133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000135
Guido van Rossum6f799372001-09-20 20:46:19 +0000136static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 {"id", T_OBJECT, OFF(ste_id), READONLY},
138 {"name", T_OBJECT, OFF(ste_name), READONLY},
139 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
140 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
141 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 {"nested", T_INT, OFF(ste_nested), READONLY},
143 {"type", T_INT, OFF(ste_type), READONLY},
144 {"lineno", T_INT, OFF(ste_lineno), READONLY},
145 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000146};
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
150 "symtable entry",
151 sizeof(PySTEntryObject),
152 0,
153 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200154 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 0, /* tp_getattr */
156 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200157 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 (reprfunc)ste_repr, /* tp_repr */
159 0, /* tp_as_number */
160 0, /* tp_as_sequence */
161 0, /* tp_as_mapping */
162 0, /* tp_hash */
163 0, /* tp_call */
164 0, /* tp_str */
165 PyObject_GenericGetAttr, /* tp_getattro */
166 0, /* tp_setattro */
167 0, /* tp_as_buffer */
168 Py_TPFLAGS_DEFAULT, /* tp_flags */
169 0, /* tp_doc */
170 0, /* tp_traverse */
171 0, /* tp_clear */
172 0, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
174 0, /* tp_iter */
175 0, /* tp_iternext */
176 0, /* tp_methods */
177 ste_memberlist, /* tp_members */
178 0, /* tp_getset */
179 0, /* tp_base */
180 0, /* tp_dict */
181 0, /* tp_descr_get */
182 0, /* tp_descr_set */
183 0, /* tp_dictoffset */
184 0, /* tp_init */
185 0, /* tp_alloc */
186 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000187};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000191 _Py_block_ty block, void *ast, int lineno,
192 int col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600193static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000194static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
195static int symtable_visit_expr(struct symtable *st, expr_ty s);
196static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000197static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
198static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000199static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200static int symtable_visit_arguments(struct symtable *st, arguments_ty);
201static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
202static int symtable_visit_alias(struct symtable *st, alias_ty);
203static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
204static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100205static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
206static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207static int symtable_implicit_arg(struct symtable *st, int pos);
Andy Lester95668422020-03-06 09:46:04 -0600208static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500209static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000210
211
Nick Coghlan650f0d02007-04-15 12:05:43 +0000212static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500214 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000215
216#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218
219#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000220"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221
222static struct symtable *
223symtable_new(void)
224{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000225 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000226
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000227 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700228 if (st == NULL) {
229 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700231 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 st->st_filename = NULL;
234 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 if ((st->st_stack = PyList_New(0)) == NULL)
237 goto fail;
238 if ((st->st_blocks = PyDict_New()) == NULL)
239 goto fail;
240 st->st_cur = NULL;
241 st->st_private = NULL;
242 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000243 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000244 PySymtable_Free(st);
245 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246}
247
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000248/* When compiling the use of C stack is probably going to be a lot
249 lighter than when executing Python code but still can overflow
250 and causing a Python crash if not checked (e.g. eval("()"*300000)).
251 Using the current recursion limit for the compiler seems too
252 restrictive (it caused at least one test to fail) so a factor is
253 used to allow deeper recursion when compiling an expression.
254
255 Using a scaling factor means this should automatically adjust when
256 the recursion limit is adjusted for small or large C stack allocations.
257*/
258#define COMPILER_STACK_FRAME_SCALE 3
259
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000260struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200261PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000262{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000263 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100264 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000266 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400267 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000268 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200271 return NULL;
272 if (filename == NULL) {
273 PySymtable_Free(st);
274 return NULL;
275 }
276 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 st->st_filename = filename;
278 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000279
280 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100281 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000282 if (!tstate) {
283 PySymtable_Free(st);
284 return NULL;
285 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400286 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000287 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400288 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000289 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400290 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
291 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000292
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000293 /* Make the initial symbol information gathering pass */
294 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000295 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000296 PySymtable_Free(st);
297 return NULL;
298 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 switch (mod->kind) {
302 case Module_kind:
303 seq = mod->v.Module.body;
304 for (i = 0; i < asdl_seq_LEN(seq); i++)
305 if (!symtable_visit_stmt(st,
306 (stmt_ty)asdl_seq_GET(seq, i)))
307 goto error;
308 break;
309 case Expression_kind:
310 if (!symtable_visit_expr(st, mod->v.Expression.body))
311 goto error;
312 break;
313 case Interactive_kind:
314 seq = mod->v.Interactive.body;
315 for (i = 0; i < asdl_seq_LEN(seq); i++)
316 if (!symtable_visit_stmt(st,
317 (stmt_ty)asdl_seq_GET(seq, i)))
318 goto error;
319 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800320 case FunctionType_kind:
321 PyErr_SetString(PyExc_RuntimeError,
322 "this compiler does not handle FunctionTypes");
323 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 }
Andy Lester95668422020-03-06 09:46:04 -0600325 if (!symtable_exit_block(st)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000326 PySymtable_Free(st);
327 return NULL;
328 }
Nick Coghlan06145232019-08-29 23:26:53 +1000329 /* Check that the recursion depth counting balanced correctly */
330 if (st->recursion_depth != starting_recursion_depth) {
331 PyErr_Format(PyExc_SystemError,
332 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
333 starting_recursion_depth, st->recursion_depth);
334 PySymtable_Free(st);
335 return NULL;
336 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000337 /* Make the second symbol analysis pass */
338 if (symtable_analyze(st))
339 return st;
340 PySymtable_Free(st);
341 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000342 error:
Andy Lester95668422020-03-06 09:46:04 -0600343 (void) symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000344 PySymtable_Free(st);
345 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346}
347
Victor Stinner14e461d2013-08-26 22:28:21 +0200348struct symtable *
349PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
350{
351 PyObject *filename;
352 struct symtable *st;
353 filename = PyUnicode_DecodeFSDefault(filename_str);
354 if (filename == NULL)
355 return NULL;
356 st = PySymtable_BuildObject(mod, filename, future);
357 Py_DECREF(filename);
358 return st;
359}
360
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000361void
362PySymtable_Free(struct symtable *st)
363{
Victor Stinner14e461d2013-08-26 22:28:21 +0200364 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 Py_XDECREF(st->st_blocks);
366 Py_XDECREF(st->st_stack);
367 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368}
369
370PySTEntryObject *
371PySymtable_Lookup(struct symtable *st, void *key)
372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000375 k = PyLong_FromVoidPtr(key);
376 if (k == NULL)
377 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200378 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 if (v) {
380 assert(PySTEntry_Check(v));
381 Py_INCREF(v);
382 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200383 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000384 PyErr_SetString(PyExc_KeyError,
385 "unknown symbol table entry");
386 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000387
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 Py_DECREF(k);
389 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000390}
391
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000392static long
393_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000394{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200395 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000396 if (!v)
397 return 0;
398 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000399 return PyLong_AS_LONG(v);
400}
401
402int
403PyST_GetScope(PySTEntryObject *ste, PyObject *name)
404{
405 long symbol = _PyST_GetSymbol(ste, name);
406 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000407}
408
Benjamin Petersond9c87022012-10-31 20:26:20 -0400409static int
410error_at_directive(PySTEntryObject *ste, PyObject *name)
411{
412 Py_ssize_t i;
413 PyObject *data;
414 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600415 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400416 data = PyList_GET_ITEM(ste->ste_directives, i);
417 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600418 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
419 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
420 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
421 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400422 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600423
424 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700425 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400426 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600427 PyErr_SetString(PyExc_RuntimeError,
428 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400429 return 0;
430}
431
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432
433/* Analyze raw symbol information to determine scope of each name.
434
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000435 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000437 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000441 explicit global is declared with the global statement. An implicit
442 global is a free variable for which the compiler has found no binding
443 in an enclosing function scope. The implicit global is either a global
444 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
445 to handle these names to implement slightly odd semantics. In such a
446 block, the name is treated as global until it is assigned to; then it
447 is treated as a local.
448
449 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000450 The first pass collects raw facts from the AST via the symtable_visit_*
451 functions: the name is a parameter here, the name is used but not defined
452 here, etc. The second pass analyzes these facts during a pass over the
453 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454
455 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000457 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000458 Names which are explicitly declared nonlocal must exist in this set of
459 visible names - if they do not, a syntax error is raised. After doing
460 the local analysis, it analyzes each of its child blocks using an
461 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000462
Nick Coghlan650f0d02007-04-15 12:05:43 +0000463 The children update the free variable set. If a local variable is added to
464 the free variable set by the child, the variable is marked as a cell. The
465 function object being defined must provide runtime storage for the variable
466 that may outlive the function's frame. Cell variables are removed from the
467 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000468
Nick Coghlan650f0d02007-04-15 12:05:43 +0000469 During analysis, the names are:
470 symbols: dict mapping from symbol names to flag values (including offset scope values)
471 scopes: dict mapping from symbol names to scope values (no offset)
472 local: set of all symbol names local to the current scope
473 bound: set of all symbol names local to a containing function scope
474 free: set of all symbol names referenced but not bound in child scopes
475 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000476*/
477
478#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 PyObject *o = PyLong_FromLong(I); \
480 if (!o) \
481 return 0; \
482 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
483 Py_DECREF(o); \
484 return 0; \
485 } \
486 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000487}
488
489/* Decide on scope of name, given flags.
490
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000491 The namespace dictionaries may be modified to record information
492 about the new name. For example, a new global will add an entry to
493 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000494*/
495
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000497analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 PyObject *bound, PyObject *local, PyObject *free,
499 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000500{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000501 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (flags & DEF_NONLOCAL) {
503 PyErr_Format(PyExc_SyntaxError,
504 "name '%U' is nonlocal and global",
505 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400506 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000507 }
508 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
509 if (PySet_Add(global, name) < 0)
510 return 0;
511 if (bound && (PySet_Discard(bound, name) < 0))
512 return 0;
513 return 1;
514 }
515 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000516 if (!bound) {
517 PyErr_Format(PyExc_SyntaxError,
518 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400519 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000520 }
521 if (!PySet_Contains(bound, name)) {
522 PyErr_Format(PyExc_SyntaxError,
523 "no binding for nonlocal '%U' found",
524 name);
525
Benjamin Petersond9c87022012-10-31 20:26:20 -0400526 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000527 }
528 SET_SCOPE(scopes, name, FREE);
529 ste->ste_free = 1;
530 return PySet_Add(free, name) >= 0;
531 }
532 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000533 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000534 if (PySet_Add(local, name) < 0)
535 return 0;
536 if (PySet_Discard(global, name) < 0)
537 return 0;
538 return 1;
539 }
540 /* If an enclosing block has a binding for this name, it
541 is a free variable rather than a global variable.
542 Note that having a non-NULL bound implies that the block
543 is nested.
544 */
545 if (bound && PySet_Contains(bound, name)) {
546 SET_SCOPE(scopes, name, FREE);
547 ste->ste_free = 1;
548 return PySet_Add(free, name) >= 0;
549 }
550 /* If a parent has a global statement, then call it global
551 explicit? It could also be global implicit.
552 */
553 if (global && PySet_Contains(global, name)) {
554 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
555 return 1;
556 }
557 if (ste->ste_nested)
558 ste->ste_free = 1;
559 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
560 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000561}
562
563#undef SET_SCOPE
564
565/* If a name is defined in free and also in locals, then this block
566 provides the binding for the free variable. The name should be
567 marked CELL in this block and removed from the free list.
568
569 Note that the current block's free variables are included in free.
570 That's safe because no name can be free and local in the same scope.
571*/
572
573static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500574analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000576 PyObject *name, *v, *v_cell;
577 int success = 0;
578 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000579
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 v_cell = PyLong_FromLong(CELL);
581 if (!v_cell)
582 return 0;
583 while (PyDict_Next(scopes, &pos, &name, &v)) {
584 long scope;
585 assert(PyLong_Check(v));
586 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000587 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000588 continue;
589 if (!PySet_Contains(free, name))
590 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591 /* Replace LOCAL with CELL for this name, and remove
592 from free. It is safe to replace the value of name
593 in the dict, because it will not cause a resize.
594 */
595 if (PyDict_SetItem(scopes, name, v_cell) < 0)
596 goto error;
597 if (PySet_Discard(free, name) < 0)
598 goto error;
599 }
600 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000601 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602 Py_DECREF(v_cell);
603 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604}
605
Benjamin Peterson312595c2013-05-15 15:26:42 -0500606static int
607drop_class_free(PySTEntryObject *ste, PyObject *free)
608{
609 int res;
610 if (!GET_IDENTIFIER(__class__))
611 return 0;
612 res = PySet_Discard(free, __class__);
613 if (res < 0)
614 return 0;
615 if (res)
616 ste->ste_needs_class_closure = 1;
617 return 1;
618}
619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620/* Enter the final scope information into the ste_symbols dict.
621 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622 * All arguments are dicts. Modifies symbols, others are read-only.
623*/
624static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000626 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000627{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000628 PyObject *name = NULL, *itr = NULL;
629 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
630 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000631
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 /* Update scope information for all symbols in this scope */
633 while (PyDict_Next(symbols, &pos, &name, &v)) {
634 long scope, flags;
635 assert(PyLong_Check(v));
636 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200637 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 assert(v_scope && PyLong_Check(v_scope));
639 scope = PyLong_AS_LONG(v_scope);
640 flags |= (scope << SCOPE_OFFSET);
641 v_new = PyLong_FromLong(flags);
642 if (!v_new)
643 return 0;
644 if (PyDict_SetItem(symbols, name, v_new) < 0) {
645 Py_DECREF(v_new);
646 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000648 Py_DECREF(v_new);
649 }
650
651 /* Record not yet resolved free variables from children (if any) */
652 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
653 if (!v_free)
654 return 0;
655
656 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600657 if (itr == NULL) {
658 Py_DECREF(v_free);
659 return 0;
660 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661
662 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200663 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000664
665 /* Handle symbol that already exists in this scope */
666 if (v) {
667 /* Handle a free variable in a method of
668 the class that has the same name as a local
669 or global in the class scope.
670 */
671 if (classflag &&
672 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
673 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
674 v_new = PyLong_FromLong(flags);
675 if (!v_new) {
676 goto error;
677 }
678 if (PyDict_SetItem(symbols, name, v_new) < 0) {
679 Py_DECREF(v_new);
680 goto error;
681 }
682 Py_DECREF(v_new);
683 }
684 /* It's a cell, or already free in this scope */
685 Py_DECREF(name);
686 continue;
687 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200688 else if (PyErr_Occurred()) {
689 goto error;
690 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000691 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200692 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000693 Py_DECREF(name);
694 continue; /* it's a global */
695 }
696 /* Propagate new free symbol up the lexical stack */
697 if (PyDict_SetItem(symbols, name, v_free) < 0) {
698 goto error;
699 }
700 Py_DECREF(name);
701 }
702 Py_DECREF(itr);
703 Py_DECREF(v_free);
704 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000705error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 Py_XDECREF(v_free);
707 Py_XDECREF(itr);
708 Py_XDECREF(name);
709 return 0;
710}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
712/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000713
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000714 Arguments:
715 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000716 bound -- set of variables bound in enclosing scopes (input). bound
717 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000718 free -- set of free variables in enclosed scopes (output)
719 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000720
721 The implementation uses two mutually recursive functions,
722 analyze_block() and analyze_child_block(). analyze_block() is
723 responsible for analyzing the individual names defined in a block.
724 analyze_child_block() prepares temporary namespace dictionaries
725 used to evaluated nested blocks.
726
727 The two functions exist because a child block should see the name
728 bindings of its enclosing blocks, but those bindings should not
729 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000730*/
731
732static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000733analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
734 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000735
736static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
738 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
741 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
742 PyObject *temp;
743 int i, success = 0;
744 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000745
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000746 local = PySet_New(NULL); /* collect new names bound in block */
747 if (!local)
748 goto error;
749 scopes = PyDict_New(); /* collect scopes defined for each name */
750 if (!scopes)
751 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* Allocate new global and bound variable dictionaries. These
754 dictionaries hold the names visible in nested blocks. For
755 ClassBlocks, the bound and global names are initialized
756 before analyzing names, because class bindings aren't
757 visible in methods. For other blocks, they are initialized
758 after names are analyzed.
759 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000760
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 /* TODO(jhylton): Package these dicts in a struct so that we
762 can write reasonable helper functions?
763 */
764 newglobal = PySet_New(NULL);
765 if (!newglobal)
766 goto error;
767 newfree = PySet_New(NULL);
768 if (!newfree)
769 goto error;
770 newbound = PySet_New(NULL);
771 if (!newbound)
772 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000774 /* Class namespace has no effect on names visible in
775 nested functions, so populate the global and bound
776 sets to be passed to child blocks before analyzing
777 this one.
778 */
779 if (ste->ste_type == ClassBlock) {
780 /* Pass down known globals */
781 temp = PyNumber_InPlaceOr(newglobal, global);
782 if (!temp)
783 goto error;
784 Py_DECREF(temp);
785 /* Pass down previously bound symbols */
786 if (bound) {
787 temp = PyNumber_InPlaceOr(newbound, bound);
788 if (!temp)
789 goto error;
790 Py_DECREF(temp);
791 }
792 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000794 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
795 long flags = PyLong_AS_LONG(v);
796 if (!analyze_name(ste, scopes, name, flags,
797 bound, local, free, global))
798 goto error;
799 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 /* Populate global and bound sets to be passed to children. */
802 if (ste->ste_type != ClassBlock) {
803 /* Add function locals to bound set */
804 if (ste->ste_type == FunctionBlock) {
805 temp = PyNumber_InPlaceOr(newbound, local);
806 if (!temp)
807 goto error;
808 Py_DECREF(temp);
809 }
810 /* Pass down previously bound symbols */
811 if (bound) {
812 temp = PyNumber_InPlaceOr(newbound, bound);
813 if (!temp)
814 goto error;
815 Py_DECREF(temp);
816 }
817 /* Pass down known globals */
818 temp = PyNumber_InPlaceOr(newglobal, global);
819 if (!temp)
820 goto error;
821 Py_DECREF(temp);
822 }
823 else {
824 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000825 if (!GET_IDENTIFIER(__class__))
826 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 if (PySet_Add(newbound, __class__) < 0)
828 goto error;
829 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000830
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300831 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000832
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000833 newbound, newglobal now contain the names visible in
834 nested blocks. The free variables in the children will
835 be collected in allfree.
836 */
837 allfree = PySet_New(NULL);
838 if (!allfree)
839 goto error;
840 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
841 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
842 PySTEntryObject* entry;
843 assert(c && PySTEntry_Check(c));
844 entry = (PySTEntryObject*)c;
845 if (!analyze_child_block(entry, newbound, newfree, newglobal,
846 allfree))
847 goto error;
848 /* Check if any children have free variables */
849 if (entry->ste_free || entry->ste_child_free)
850 ste->ste_child_free = 1;
851 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 temp = PyNumber_InPlaceOr(newfree, allfree);
854 if (!temp)
855 goto error;
856 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500859 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500861 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 goto error;
863 /* Records the results of the analysis in the symbol table entry */
864 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
865 ste->ste_type == ClassBlock))
866 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000867
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 temp = PyNumber_InPlaceOr(free, newfree);
869 if (!temp)
870 goto error;
871 Py_DECREF(temp);
872 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000873 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000874 Py_XDECREF(scopes);
875 Py_XDECREF(local);
876 Py_XDECREF(newbound);
877 Py_XDECREF(newglobal);
878 Py_XDECREF(newfree);
879 Py_XDECREF(allfree);
880 if (!success)
881 assert(PyErr_Occurred());
882 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000883}
884
885static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
887 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000888{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000889 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
890 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000891
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000892 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000893
Martin Panter3ee62702016-06-04 04:57:19 +0000894 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 current block. The analyze_block() call modifies these
896 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 */
899 temp_bound = PySet_New(bound);
900 if (!temp_bound)
901 goto error;
902 temp_free = PySet_New(free);
903 if (!temp_free)
904 goto error;
905 temp_global = PySet_New(global);
906 if (!temp_global)
907 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000908
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000909 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
910 goto error;
911 temp = PyNumber_InPlaceOr(child_free, temp_free);
912 if (!temp)
913 goto error;
914 Py_DECREF(temp);
915 Py_DECREF(temp_bound);
916 Py_DECREF(temp_free);
917 Py_DECREF(temp_global);
918 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000919 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000920 Py_XDECREF(temp_bound);
921 Py_XDECREF(temp_free);
922 Py_XDECREF(temp_global);
923 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000924}
925
926static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000927symtable_analyze(struct symtable *st)
928{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 PyObject *free, *global;
930 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000932 free = PySet_New(NULL);
933 if (!free)
934 return 0;
935 global = PySet_New(NULL);
936 if (!global) {
937 Py_DECREF(free);
938 return 0;
939 }
940 r = analyze_block(st->st_top, NULL, free, global);
941 Py_DECREF(free);
942 Py_DECREF(global);
943 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944}
945
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000946/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947 This reference is released when the block is exited, via the DECREF
948 in symtable_exit_block().
949*/
950
951static int
Andy Lester95668422020-03-06 09:46:04 -0600952symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000953{
Benjamin Peterson609da582011-06-29 22:52:39 -0500954 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000955
Benjamin Peterson609da582011-06-29 22:52:39 -0500956 st->st_cur = NULL;
957 size = PyList_GET_SIZE(st->st_stack);
958 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500959 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500961 if (--size)
962 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 }
964 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965}
966
967static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000969 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000970{
Benjamin Peterson609da582011-06-29 22:52:39 -0500971 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972
Benjamin Peterson609da582011-06-29 22:52:39 -0500973 ste = ste_new(st, name, block, ast, lineno, col_offset);
974 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500976 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
977 Py_DECREF(ste);
978 return 0;
979 }
980 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000981 /* bpo-37757: For now, disallow *all* assignment expressions in the
982 * outermost iterator expression of a comprehension, even those inside
983 * a nested comprehension or a lambda expression.
984 */
985 if (prev) {
986 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
987 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500988 /* The entry is owned by the stack. Borrow it for st_cur. */
989 Py_DECREF(ste);
990 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000991 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000992 st->st_global = st->st_cur->ste_symbols;
993 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500994 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000995 return 0;
996 }
997 }
998 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000999}
1000
Neal Norwitz46b7bda2006-01-08 01:06:06 +00001001static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002symtable_lookup(struct symtable *st, PyObject *name)
1003{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001004 PyObject *mangled = _Py_Mangle(st->st_private, name);
1005 if (!mangled)
1006 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +01001007 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1008 Py_DECREF(mangled);
1009 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001010}
1011
1012static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001013symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001015 PyObject *o;
1016 PyObject *dict;
1017 long val;
1018 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019
Jeremy Hylton81e95022007-02-27 06:50:52 +00001020
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001021 if (!mangled)
1022 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001023 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001024 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 val = PyLong_AS_LONG(o);
1026 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1027 /* Is it better to use 'mangled' or 'name' here? */
1028 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001029 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001030 ste->ste_lineno,
1031 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 goto error;
1033 }
1034 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001035 }
1036 else if (PyErr_Occurred()) {
1037 goto error;
1038 }
1039 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001040 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001041 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001042 if (ste->ste_comp_iter_target) {
1043 /* This name is an iteration variable in a comprehension,
1044 * so check for a binding conflict with any named expressions.
1045 * Otherwise, mark it as an iteration variable so subsequent
1046 * named expressions can check for conflicts.
1047 */
1048 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1049 PyErr_Format(PyExc_SyntaxError,
1050 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1051 PyErr_SyntaxLocationObject(st->st_filename,
1052 ste->ste_lineno,
1053 ste->ste_col_offset + 1);
1054 goto error;
1055 }
1056 val |= DEF_COMP_ITER;
1057 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001058 o = PyLong_FromLong(val);
1059 if (o == NULL)
1060 goto error;
1061 if (PyDict_SetItem(dict, mangled, o) < 0) {
1062 Py_DECREF(o);
1063 goto error;
1064 }
1065 Py_DECREF(o);
1066
1067 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001068 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 goto error;
1070 } else if (flag & DEF_GLOBAL) {
1071 /* XXX need to update DEF_GLOBAL for other flags too;
1072 perhaps only DEF_FREE_GLOBAL */
1073 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001074 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 val |= PyLong_AS_LONG(o);
1076 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001077 else if (PyErr_Occurred()) {
1078 goto error;
1079 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001081 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001082 goto error;
1083 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1084 Py_DECREF(o);
1085 goto error;
1086 }
1087 Py_DECREF(o);
1088 }
1089 Py_DECREF(mangled);
1090 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001091
1092error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001093 Py_DECREF(mangled);
1094 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001095}
1096
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001097static int
1098symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1099 return symtable_add_def_helper(st, name, flag, st->st_cur);
1100}
1101
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001102/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1103 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001104 function.
1105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1107 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001108
1109 VISIT_QUIT macro returns the specified value exiting from the function but
1110 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001111*/
1112
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001113#define VISIT_QUIT(ST, X) \
1114 return --(ST)->recursion_depth,(X)
1115
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001116#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001118 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001122 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1124 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1125 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001126 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001129
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001132 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1134 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1135 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001136 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001138}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001139
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001140#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001141 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001142 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001144 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001145 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001146 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001147 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001149}
1150
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001151static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001152symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001153{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001154 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001155 int res;
1156 if (!st->st_cur->ste_directives) {
1157 st->st_cur->ste_directives = PyList_New(0);
1158 if (!st->st_cur->ste_directives)
1159 return 0;
1160 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001161 mangled = _Py_Mangle(st->st_private, name);
1162 if (!mangled)
1163 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001164 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001165 if (!data)
1166 return 0;
1167 res = PyList_Append(st->st_cur->ste_directives, data);
1168 Py_DECREF(data);
1169 return res == 0;
1170}
1171
1172
1173static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001174symtable_visit_stmt(struct symtable *st, stmt_ty s)
1175{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001176 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001177 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001178 "maximum recursion depth exceeded during compilation");
1179 VISIT_QUIT(st, 0);
1180 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001181 switch (s->kind) {
1182 case FunctionDef_kind:
1183 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001184 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001185 if (s->v.FunctionDef.args->defaults)
1186 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1187 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001188 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001189 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001190 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001191 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (s->v.FunctionDef.decorator_list)
1193 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1194 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001195 FunctionBlock, (void *)s, s->lineno,
1196 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001197 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001198 VISIT(st, arguments, s->v.FunctionDef.args);
1199 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001200 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001201 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001202 break;
1203 case ClassDef_kind: {
1204 PyObject *tmp;
1205 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001206 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1208 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 if (s->v.ClassDef.decorator_list)
1210 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1211 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001212 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001213 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001214 tmp = st->st_private;
1215 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001216 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001217 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001218 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001219 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001220 break;
1221 }
1222 case Return_kind:
1223 if (s->v.Return.value) {
1224 VISIT(st, expr, s->v.Return.value);
1225 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001226 }
1227 break;
1228 case Delete_kind:
1229 VISIT_SEQ(st, expr, s->v.Delete.targets);
1230 break;
1231 case Assign_kind:
1232 VISIT_SEQ(st, expr, s->v.Assign.targets);
1233 VISIT(st, expr, s->v.Assign.value);
1234 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001235 case AnnAssign_kind:
1236 if (s->v.AnnAssign.target->kind == Name_kind) {
1237 expr_ty e_name = s->v.AnnAssign.target;
1238 long cur = symtable_lookup(st, e_name->v.Name.id);
1239 if (cur < 0) {
1240 VISIT_QUIT(st, 0);
1241 }
1242 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001243 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001244 && s->v.AnnAssign.simple) {
1245 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001246 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1247 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001248 PyErr_SyntaxLocationObject(st->st_filename,
1249 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001250 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001251 VISIT_QUIT(st, 0);
1252 }
1253 if (s->v.AnnAssign.simple &&
1254 !symtable_add_def(st, e_name->v.Name.id,
1255 DEF_ANNOT | DEF_LOCAL)) {
1256 VISIT_QUIT(st, 0);
1257 }
1258 else {
1259 if (s->v.AnnAssign.value
1260 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1261 VISIT_QUIT(st, 0);
1262 }
1263 }
1264 }
1265 else {
1266 VISIT(st, expr, s->v.AnnAssign.target);
1267 }
1268 VISIT(st, expr, s->v.AnnAssign.annotation);
1269 if (s->v.AnnAssign.value) {
1270 VISIT(st, expr, s->v.AnnAssign.value);
1271 }
1272 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 case AugAssign_kind:
1274 VISIT(st, expr, s->v.AugAssign.target);
1275 VISIT(st, expr, s->v.AugAssign.value);
1276 break;
1277 case For_kind:
1278 VISIT(st, expr, s->v.For.target);
1279 VISIT(st, expr, s->v.For.iter);
1280 VISIT_SEQ(st, stmt, s->v.For.body);
1281 if (s->v.For.orelse)
1282 VISIT_SEQ(st, stmt, s->v.For.orelse);
1283 break;
1284 case While_kind:
1285 VISIT(st, expr, s->v.While.test);
1286 VISIT_SEQ(st, stmt, s->v.While.body);
1287 if (s->v.While.orelse)
1288 VISIT_SEQ(st, stmt, s->v.While.orelse);
1289 break;
1290 case If_kind:
1291 /* XXX if 0: and lookup_yield() hacks */
1292 VISIT(st, expr, s->v.If.test);
1293 VISIT_SEQ(st, stmt, s->v.If.body);
1294 if (s->v.If.orelse)
1295 VISIT_SEQ(st, stmt, s->v.If.orelse);
1296 break;
1297 case Raise_kind:
1298 if (s->v.Raise.exc) {
1299 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001300 if (s->v.Raise.cause) {
1301 VISIT(st, expr, s->v.Raise.cause);
1302 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001303 }
1304 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001305 case Try_kind:
1306 VISIT_SEQ(st, stmt, s->v.Try.body);
1307 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1308 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1309 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 break;
1311 case Assert_kind:
1312 VISIT(st, expr, s->v.Assert.test);
1313 if (s->v.Assert.msg)
1314 VISIT(st, expr, s->v.Assert.msg);
1315 break;
1316 case Import_kind:
1317 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 break;
1319 case ImportFrom_kind:
1320 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 break;
1322 case Global_kind: {
1323 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001324 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001325 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1326 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001327 long cur = symtable_lookup(st, name);
1328 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001329 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001330 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1331 const char* msg;
1332 if (cur & DEF_PARAM) {
1333 msg = GLOBAL_PARAM;
1334 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001335 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001336 } else if (cur & DEF_ANNOT) {
1337 msg = GLOBAL_ANNOT;
1338 } else { /* DEF_LOCAL */
1339 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001340 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001341 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001342 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001343 PyErr_SyntaxLocationObject(st->st_filename,
1344 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001345 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001346 VISIT_QUIT(st, 0);
1347 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001349 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001350 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001351 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 }
1353 break;
1354 }
1355 case Nonlocal_kind: {
1356 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001357 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001358 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1359 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001360 long cur = symtable_lookup(st, name);
1361 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001362 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001363 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1364 const char* msg;
1365 if (cur & DEF_PARAM) {
1366 msg = NONLOCAL_PARAM;
1367 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001368 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001369 } else if (cur & DEF_ANNOT) {
1370 msg = NONLOCAL_ANNOT;
1371 } else { /* DEF_LOCAL */
1372 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001373 }
1374 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001375 PyErr_SyntaxLocationObject(st->st_filename,
1376 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001377 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001378 VISIT_QUIT(st, 0);
1379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001381 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001382 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001383 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001384 }
1385 break;
1386 }
1387 case Expr_kind:
1388 VISIT(st, expr, s->v.Expr.value);
1389 break;
1390 case Pass_kind:
1391 case Break_kind:
1392 case Continue_kind:
1393 /* nothing to do here */
1394 break;
1395 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001396 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 VISIT_SEQ(st, stmt, s->v.With.body);
1398 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001399 case AsyncFunctionDef_kind:
1400 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1401 VISIT_QUIT(st, 0);
1402 if (s->v.AsyncFunctionDef.args->defaults)
1403 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1404 if (s->v.AsyncFunctionDef.args->kw_defaults)
1405 VISIT_SEQ_WITH_NULL(st, expr,
1406 s->v.AsyncFunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001407 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001408 s->v.AsyncFunctionDef.returns))
1409 VISIT_QUIT(st, 0);
1410 if (s->v.AsyncFunctionDef.decorator_list)
1411 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1412 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1413 FunctionBlock, (void *)s, s->lineno,
1414 s->col_offset))
1415 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001416 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001417 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1418 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001419 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001420 VISIT_QUIT(st, 0);
1421 break;
1422 case AsyncWith_kind:
1423 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1424 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1425 break;
1426 case AsyncFor_kind:
1427 VISIT(st, expr, s->v.AsyncFor.target);
1428 VISIT(st, expr, s->v.AsyncFor.iter);
1429 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1430 if (s->v.AsyncFor.orelse)
1431 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1432 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001434 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001435}
1436
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001438symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1439{
1440 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001441 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001442
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001443 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001444 Py_ssize_t i, size;
1445 struct _symtable_entry *ste;
1446 size = PyList_GET_SIZE(st->st_stack);
1447 assert(size);
1448
1449 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1450 for (i = size - 1; i >= 0; i--) {
1451 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1452
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001453 /* If we find a comprehension scope, check for a target
1454 * binding conflict with iteration variables, otherwise skip it
1455 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001456 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001457 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1458 if (target_in_scope & DEF_COMP_ITER) {
1459 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1460 PyErr_SyntaxLocationObject(st->st_filename,
1461 e->lineno,
1462 e->col_offset);
1463 VISIT_QUIT(st, 0);
1464 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001465 continue;
1466 }
1467
Pablo Galindofd5c4142019-10-14 05:18:05 +01001468 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001469 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001470 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1471 if (target_in_scope & DEF_GLOBAL) {
1472 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1473 VISIT_QUIT(st, 0);
1474 } else {
1475 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1476 VISIT_QUIT(st, 0);
1477 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001478 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001479 VISIT_QUIT(st, 0);
1480
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001481 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001482 }
1483 /* If we find a ModuleBlock entry, add as GLOBAL */
1484 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001485 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001486 VISIT_QUIT(st, 0);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001487 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001488 VISIT_QUIT(st, 0);
1489
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001490 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001491 }
1492 /* Disallow usage in ClassBlock */
1493 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001494 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001495 PyErr_SyntaxLocationObject(st->st_filename,
1496 e->lineno,
1497 e->col_offset);
1498 VISIT_QUIT(st, 0);
1499 }
1500 }
1501
1502 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1503 and should never fall to this case
1504 */
1505 assert(0);
1506 return 0;
1507}
1508
1509static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001510symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1511{
1512 if (st->st_cur->ste_comp_iter_expr > 0) {
1513 /* Assignment isn't allowed in a comprehension iterable expression */
1514 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1515 PyErr_SyntaxLocationObject(st->st_filename,
1516 e->lineno,
1517 e->col_offset);
Nick Coghlan06145232019-08-29 23:26:53 +10001518 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001519 }
1520 if (st->st_cur->ste_comprehension) {
1521 /* Inside a comprehension body, so find the right target scope */
1522 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001523 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001524 }
1525 VISIT(st, expr, e->v.NamedExpr.value);
1526 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001527 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001528}
1529
1530static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531symtable_visit_expr(struct symtable *st, expr_ty e)
1532{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001533 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001534 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001535 "maximum recursion depth exceeded during compilation");
1536 VISIT_QUIT(st, 0);
1537 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001539 case NamedExpr_kind:
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001540 if(!symtable_handle_namedexpr(st, e))
1541 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001542 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 case BoolOp_kind:
1544 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1545 break;
1546 case BinOp_kind:
1547 VISIT(st, expr, e->v.BinOp.left);
1548 VISIT(st, expr, e->v.BinOp.right);
1549 break;
1550 case UnaryOp_kind:
1551 VISIT(st, expr, e->v.UnaryOp.operand);
1552 break;
1553 case Lambda_kind: {
1554 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001555 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (e->v.Lambda.args->defaults)
1557 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001558 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001559 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001561 FunctionBlock, (void *)e, e->lineno,
1562 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001563 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001564 VISIT(st, arguments, e->v.Lambda.args);
1565 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001566 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001567 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 break;
1569 }
1570 case IfExp_kind:
1571 VISIT(st, expr, e->v.IfExp.test);
1572 VISIT(st, expr, e->v.IfExp.body);
1573 VISIT(st, expr, e->v.IfExp.orelse);
1574 break;
1575 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001576 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001577 VISIT_SEQ(st, expr, e->v.Dict.values);
1578 break;
1579 case Set_kind:
1580 VISIT_SEQ(st, expr, e->v.Set.elts);
1581 break;
1582 case GeneratorExp_kind:
1583 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001584 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 break;
1586 case ListComp_kind:
1587 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001588 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001589 break;
1590 case SetComp_kind:
1591 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001592 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 break;
1594 case DictComp_kind:
1595 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001596 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 break;
1598 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001599 if (e->v.Yield.value)
1600 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001602 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001603 case YieldFrom_kind:
1604 VISIT(st, expr, e->v.YieldFrom.value);
1605 st->st_cur->ste_generator = 1;
1606 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001607 case Await_kind:
1608 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001609 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001610 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 case Compare_kind:
1612 VISIT(st, expr, e->v.Compare.left);
1613 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1614 break;
1615 case Call_kind:
1616 VISIT(st, expr, e->v.Call.func);
1617 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001618 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001620 case FormattedValue_kind:
1621 VISIT(st, expr, e->v.FormattedValue.value);
1622 if (e->v.FormattedValue.format_spec)
1623 VISIT(st, expr, e->v.FormattedValue.format_spec);
1624 break;
1625 case JoinedStr_kind:
1626 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1627 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001628 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 /* Nothing to do here. */
1630 break;
1631 /* The following exprs can be assignment targets. */
1632 case Attribute_kind:
1633 VISIT(st, expr, e->v.Attribute.value);
1634 break;
1635 case Subscript_kind:
1636 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001637 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001638 break;
1639 case Starred_kind:
1640 VISIT(st, expr, e->v.Starred.value);
1641 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001642 case Slice_kind:
1643 if (e->v.Slice.lower)
1644 VISIT(st, expr, e->v.Slice.lower)
1645 if (e->v.Slice.upper)
1646 VISIT(st, expr, e->v.Slice.upper)
1647 if (e->v.Slice.step)
1648 VISIT(st, expr, e->v.Slice.step)
1649 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 case Name_kind:
1651 if (!symtable_add_def(st, e->v.Name.id,
1652 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001653 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 /* Special-case super: it counts as a use of __class__ */
1655 if (e->v.Name.ctx == Load &&
1656 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001657 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001658 if (!GET_IDENTIFIER(__class__) ||
1659 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001660 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 }
1662 break;
1663 /* child nodes of List and Tuple will have expr_context set */
1664 case List_kind:
1665 VISIT_SEQ(st, expr, e->v.List.elts);
1666 break;
1667 case Tuple_kind:
1668 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1669 break;
1670 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001671 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672}
1673
1674static int
1675symtable_implicit_arg(struct symtable *st, int pos)
1676{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1678 if (id == NULL)
1679 return 0;
1680 if (!symtable_add_def(st, id, DEF_PARAM)) {
1681 Py_DECREF(id);
1682 return 0;
1683 }
1684 Py_DECREF(id);
1685 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001686}
1687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001689symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001690{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001692
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 if (!args)
1694 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001696 for (i = 0; i < asdl_seq_LEN(args); i++) {
1697 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1698 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1699 return 0;
1700 }
1701
1702 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703}
1704
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001706symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001709
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (!args)
1711 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 for (i = 0; i < asdl_seq_LEN(args); i++) {
1714 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1715 if (arg->annotation)
1716 VISIT(st, expr, arg->annotation);
1717 }
1718
1719 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001720}
1721
Neal Norwitzc1505362006-12-28 06:47:50 +00001722static int
Andy Lester95668422020-03-06 09:46:04 -06001723symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001724{
Anthony Sottileec007cb2020-01-04 20:57:21 -05001725 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1726 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 if (a->args && !symtable_visit_argannotations(st, a->args))
1728 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001729 if (a->vararg && a->vararg->annotation)
1730 VISIT(st, expr, a->vararg->annotation);
1731 if (a->kwarg && a->kwarg->annotation)
1732 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1734 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001735 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001736 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001738}
1739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741symtable_visit_arguments(struct symtable *st, arguments_ty a)
1742{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 /* skip default arguments inside function block
1744 XXX should ast be different?
1745 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001746 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1747 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 if (a->args && !symtable_visit_params(st, a->args))
1749 return 0;
1750 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1751 return 0;
1752 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001753 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 return 0;
1755 st->st_cur->ste_varargs = 1;
1756 }
1757 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001758 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001759 return 0;
1760 st->st_cur->ste_varkeywords = 1;
1761 }
1762 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763}
1764
1765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 if (eh->v.ExceptHandler.type)
1770 VISIT(st, expr, eh->v.ExceptHandler.type);
1771 if (eh->v.ExceptHandler.name)
1772 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1773 return 0;
1774 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1775 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001776}
1777
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001778static int
1779symtable_visit_withitem(struct symtable *st, withitem_ty item)
1780{
1781 VISIT(st, expr, item->context_expr);
1782 if (item->optional_vars) {
1783 VISIT(st, expr, item->optional_vars);
1784 }
1785 return 1;
1786}
1787
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001789static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001790symtable_visit_alias(struct symtable *st, alias_ty a)
1791{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001792 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001793 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 dotted package name (e.g. spam.eggs)
1795 */
1796 PyObject *store_name;
1797 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001798 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1799 PyUnicode_GET_LENGTH(name), 1);
1800 if (dot != -1) {
1801 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (!store_name)
1803 return 0;
1804 }
1805 else {
1806 store_name = name;
1807 Py_INCREF(store_name);
1808 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001809 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1811 Py_DECREF(store_name);
1812 return r;
1813 }
1814 else {
1815 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001816 int lineno = st->st_cur->ste_lineno;
1817 int col_offset = st->st_cur->ste_col_offset;
1818 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001819 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001820 Py_DECREF(store_name);
1821 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823 Py_DECREF(store_name);
1824 return 1;
1825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001826}
1827
1828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001830symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1831{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001832 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001833 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001834 st->st_cur->ste_comp_iter_target = 0;
1835 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001836 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001837 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001838 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001839 if (lc->is_async) {
1840 st->st_cur->ste_coroutine = 1;
1841 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001842 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001843}
1844
1845
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847symtable_visit_keyword(struct symtable *st, keyword_ty k)
1848{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849 VISIT(st, expr, k->value);
1850 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851}
1852
1853
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001855symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01001856 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001857 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001858{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 comprehension_ty outermost = ((comprehension_ty)
1861 asdl_seq_GET(generators, 0));
1862 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001863 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001864 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001865 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 /* Create comprehension scope for the rest */
1867 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001868 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1869 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870 return 0;
1871 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001872 if (outermost->is_async) {
1873 st->st_cur->ste_coroutine = 1;
1874 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001875 st->st_cur->ste_comprehension = 1;
1876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001877 /* Outermost iter is received as an argument */
1878 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06001879 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001880 return 0;
1881 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001882 /* Visit iteration variable target, and mark them as such */
1883 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001884 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001885 st->st_cur->ste_comp_iter_target = 0;
1886 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001887 VISIT_SEQ(st, expr, outermost->ifs);
1888 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001889 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001890 VISIT(st, expr, value);
1891 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001892 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001893 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001894 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1895 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1896 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1897 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001898 PyErr_SyntaxLocationObject(st->st_filename,
1899 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001900 st->st_cur->ste_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06001901 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001902 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001903 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001904 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06001905 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001906}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001907
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001908static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001909symtable_visit_genexp(struct symtable *st, expr_ty e)
1910{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1912 e->v.GeneratorExp.generators,
1913 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001914}
1915
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001916static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001917symtable_visit_listcomp(struct symtable *st, expr_ty e)
1918{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001919 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1920 e->v.ListComp.generators,
1921 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001922}
1923
1924static int
1925symtable_visit_setcomp(struct symtable *st, expr_ty e)
1926{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001927 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1928 e->v.SetComp.generators,
1929 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001930}
1931
1932static int
1933symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1936 e->v.DictComp.generators,
1937 e->v.DictComp.key,
1938 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001939}