blob: a149e28dcb3c431bcaf9edb09bd2a5d6ae79d6a2 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner94faa072021-03-23 20:47:40 +01002#include "pycore_ast.h" // identifier, stmt_ty
Victor Stinnera81fca62021-03-24 00:51:50 +01003#undef Yield /* undefine macro conflicting with <winbase.h> */
4#include "pycore_compile.h" // _Py_Mangle()
Victor Stinner28ad12f2021-03-19 12:41:49 +01005#include "pycore_pystate.h" // _PyThreadState_GET()
6#include "pycore_symtable.h" // PySTEntryObject
Victor Stinner4a21e572020-04-15 02:35:41 +02007#include "structmember.h" // PyMemberDef
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00008
Neal Norwitz5d0ad502005-12-19 04:27:42 +00009/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +020010#define GLOBAL_PARAM \
11"name '%U' is parameter and global"
12
13#define NONLOCAL_PARAM \
14"name '%U' is parameter and nonlocal"
15
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000016#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070017"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000018
Jeremy Hylton81e95022007-02-27 06:50:52 +000019#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070020"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000021
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000022#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070023"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000024
Jeremy Hylton81e95022007-02-27 06:50:52 +000025#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070026"name '%U' is used prior to nonlocal declaration"
27
28#define GLOBAL_ANNOT \
29"annotated name '%U' can't be global"
30
31#define NONLOCAL_ANNOT \
32"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000033
Neal Norwitz5d0ad502005-12-19 04:27:42 +000034#define IMPORT_STAR_WARNING "import * only allowed at module level"
35
Emily Morehouse8f59ee02019-01-24 16:49:56 -070036#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100037"assignment expression within a comprehension cannot be used in a class body"
38
39#define NAMED_EXPR_COMP_CONFLICT \
40"assignment expression cannot rebind comprehension iteration variable '%U'"
41
42#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
43"comprehension inner loop cannot rebind assignment expression target '%U'"
44
45#define NAMED_EXPR_COMP_ITER_EXPR \
46"assignment expression cannot be used in a comprehension iterable expression"
Emily Morehouse8f59ee02019-01-24 16:49:56 -070047
Neal Norwitz090b3dd2006-02-28 22:36:46 +000048static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000049ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000050 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020053 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 k = PyLong_FromVoidPtr(key);
56 if (k == NULL)
57 goto fail;
58 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020059 if (ste == NULL) {
60 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020062 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000063 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020064 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000065
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020067 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000068
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000069 ste->ste_symbols = NULL;
70 ste->ste_varnames = NULL;
71 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000072
Benjamin Petersond9c87022012-10-31 20:26:20 -040073 ste->ste_directives = NULL;
74
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000076 ste->ste_nested = 0;
77 ste->ste_free = 0;
78 ste->ste_varargs = 0;
79 ste->ste_varkeywords = 0;
80 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000081 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000083 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000085 if (st->st_cur != NULL &&
86 (st->st_cur->ste_nested ||
87 st->st_cur->ste_type == FunctionBlock))
88 ste->ste_nested = 1;
89 ste->ste_child_free = 0;
90 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070091 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070092 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050094 ste->ste_needs_class_closure = 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +100095 ste->ste_comp_iter_target = 0;
96 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097
Victor Stinner9a4fb662013-07-11 22:49:00 +020098 ste->ste_symbols = PyDict_New();
99 ste->ste_varnames = PyList_New(0);
100 ste->ste_children = PyList_New(0);
101 if (ste->ste_symbols == NULL
102 || ste->ste_varnames == NULL
103 || ste->ste_children == NULL)
104 goto fail;
105
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
107 goto fail;
108
109 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000110 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 Py_XDECREF(ste);
112 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000113}
114
115static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000116ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
119 ste->ste_name,
120 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121}
122
123static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000124ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000125{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 ste->ste_table = NULL;
127 Py_XDECREF(ste->ste_id);
128 Py_XDECREF(ste->ste_name);
129 Py_XDECREF(ste->ste_symbols);
130 Py_XDECREF(ste->ste_varnames);
131 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400132 Py_XDECREF(ste->ste_directives);
Victor Stinner32bd68c2020-12-01 10:37:39 +0100133 PyObject_Free(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000134}
135
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000136#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000137
Guido van Rossum6f799372001-09-20 20:46:19 +0000138static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 {"id", T_OBJECT, OFF(ste_id), READONLY},
140 {"name", T_OBJECT, OFF(ste_name), READONLY},
141 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
142 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
143 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000144 {"nested", T_INT, OFF(ste_nested), READONLY},
145 {"type", T_INT, OFF(ste_type), READONLY},
146 {"lineno", T_INT, OFF(ste_lineno), READONLY},
147 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000148};
149
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000150PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000151 PyVarObject_HEAD_INIT(&PyType_Type, 0)
152 "symtable entry",
153 sizeof(PySTEntryObject),
154 0,
155 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200156 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000157 0, /* tp_getattr */
158 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200159 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000160 (reprfunc)ste_repr, /* tp_repr */
161 0, /* tp_as_number */
162 0, /* tp_as_sequence */
163 0, /* tp_as_mapping */
164 0, /* tp_hash */
165 0, /* tp_call */
166 0, /* tp_str */
167 PyObject_GenericGetAttr, /* tp_getattro */
168 0, /* tp_setattro */
169 0, /* tp_as_buffer */
170 Py_TPFLAGS_DEFAULT, /* tp_flags */
171 0, /* tp_doc */
172 0, /* tp_traverse */
173 0, /* tp_clear */
174 0, /* tp_richcompare */
175 0, /* tp_weaklistoffset */
176 0, /* tp_iter */
177 0, /* tp_iternext */
178 0, /* tp_methods */
179 ste_memberlist, /* tp_members */
180 0, /* tp_getset */
181 0, /* tp_base */
182 0, /* tp_dict */
183 0, /* tp_descr_get */
184 0, /* tp_descr_set */
185 0, /* tp_dictoffset */
186 0, /* tp_init */
187 0, /* tp_alloc */
188 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000189};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000190
191static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000192static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000193 _Py_block_ty block, void *ast, int lineno,
194 int col_offset);
Andy Lester95668422020-03-06 09:46:04 -0600195static int symtable_exit_block(struct symtable *st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
197static int symtable_visit_expr(struct symtable *st, expr_ty s);
198static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000199static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
200static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000201static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000202static int symtable_visit_arguments(struct symtable *st, arguments_ty);
203static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
204static int symtable_visit_alias(struct symtable *st, alias_ty);
205static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
206static int symtable_visit_keyword(struct symtable *st, keyword_ty);
Pablo Galindoa5634c42020-09-16 19:42:00 +0100207static int symtable_visit_params(struct symtable *st, asdl_arg_seq *args);
208static int symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000209static int symtable_implicit_arg(struct symtable *st, int pos);
Andy Lester95668422020-03-06 09:46:04 -0600210static int symtable_visit_annotations(struct symtable *st, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500211static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Brandt Bucher145bf262021-02-26 14:51:55 -0800212static int symtable_visit_match_case(struct symtable *st, match_case_ty m);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000213
214
Nick Coghlan650f0d02007-04-15 12:05:43 +0000215static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500217 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218
219#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000221
222#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000223"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000224
225static struct symtable *
226symtable_new(void)
227{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700231 if (st == NULL) {
232 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700234 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000235
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000236 st->st_filename = NULL;
237 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000238
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 if ((st->st_stack = PyList_New(0)) == NULL)
240 goto fail;
241 if ((st->st_blocks = PyDict_New()) == NULL)
242 goto fail;
243 st->st_cur = NULL;
244 st->st_private = NULL;
Brandt Bucher145bf262021-02-26 14:51:55 -0800245 st->in_pattern = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000246 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247 fail:
Victor Stinner28ad12f2021-03-19 12:41:49 +0100248 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000249 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250}
251
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000252/* When compiling the use of C stack is probably going to be a lot
253 lighter than when executing Python code but still can overflow
254 and causing a Python crash if not checked (e.g. eval("()"*300000)).
255 Using the current recursion limit for the compiler seems too
256 restrictive (it caused at least one test to fail) so a factor is
257 used to allow deeper recursion when compiling an expression.
258
259 Using a scaling factor means this should automatically adjust when
260 the recursion limit is adjusted for small or large C stack allocations.
261*/
262#define COMPILER_STACK_FRAME_SCALE 3
263
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000264struct symtable *
Victor Stinner28ad12f2021-03-19 12:41:49 +0100265_PySymtable_Build(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000266{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000267 struct symtable *st = symtable_new();
Pablo Galindoa5634c42020-09-16 19:42:00 +0100268 asdl_stmt_seq *seq;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000269 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000270 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400271 int recursion_limit = Py_GetRecursionLimit();
Nick Coghlan06145232019-08-29 23:26:53 +1000272 int starting_recursion_depth;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000273
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200275 return NULL;
276 if (filename == NULL) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100277 _PySymtable_Free(st);
Victor Stinner14e461d2013-08-26 22:28:21 +0200278 return NULL;
279 }
280 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 st->st_filename = filename;
282 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000283
284 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100285 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000286 if (!tstate) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100287 _PySymtable_Free(st);
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000288 return NULL;
289 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400290 /* Be careful here to prevent overflow. */
Nick Coghlan06145232019-08-29 23:26:53 +1000291 starting_recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400292 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
Nick Coghlan06145232019-08-29 23:26:53 +1000293 st->recursion_depth = starting_recursion_depth;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400294 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
295 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000296
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000297 /* Make the initial symbol information gathering pass */
298 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000299 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100300 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000301 return NULL;
302 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000303
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000304 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000305 switch (mod->kind) {
306 case Module_kind:
307 seq = mod->v.Module.body;
308 for (i = 0; i < asdl_seq_LEN(seq); i++)
309 if (!symtable_visit_stmt(st,
310 (stmt_ty)asdl_seq_GET(seq, i)))
311 goto error;
312 break;
313 case Expression_kind:
314 if (!symtable_visit_expr(st, mod->v.Expression.body))
315 goto error;
316 break;
317 case Interactive_kind:
318 seq = mod->v.Interactive.body;
319 for (i = 0; i < asdl_seq_LEN(seq); i++)
320 if (!symtable_visit_stmt(st,
321 (stmt_ty)asdl_seq_GET(seq, i)))
322 goto error;
323 break;
Guido van Rossum522346d2019-02-11 11:34:50 -0800324 case FunctionType_kind:
325 PyErr_SetString(PyExc_RuntimeError,
326 "this compiler does not handle FunctionTypes");
327 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000328 }
Andy Lester95668422020-03-06 09:46:04 -0600329 if (!symtable_exit_block(st)) {
Victor Stinner28ad12f2021-03-19 12:41:49 +0100330 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000331 return NULL;
332 }
Nick Coghlan06145232019-08-29 23:26:53 +1000333 /* Check that the recursion depth counting balanced correctly */
334 if (st->recursion_depth != starting_recursion_depth) {
335 PyErr_Format(PyExc_SystemError,
336 "symtable analysis recursion depth mismatch (before=%d, after=%d)",
337 starting_recursion_depth, st->recursion_depth);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100338 _PySymtable_Free(st);
Nick Coghlan06145232019-08-29 23:26:53 +1000339 return NULL;
340 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 /* Make the second symbol analysis pass */
342 if (symtable_analyze(st))
343 return st;
Victor Stinner28ad12f2021-03-19 12:41:49 +0100344 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000345 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346 error:
Andy Lester95668422020-03-06 09:46:04 -0600347 (void) symtable_exit_block(st);
Victor Stinner28ad12f2021-03-19 12:41:49 +0100348 _PySymtable_Free(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350}
351
Victor Stinner14e461d2013-08-26 22:28:21 +0200352
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000353void
Victor Stinner28ad12f2021-03-19 12:41:49 +0100354_PySymtable_Free(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000355{
Victor Stinner14e461d2013-08-26 22:28:21 +0200356 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 Py_XDECREF(st->st_blocks);
358 Py_XDECREF(st->st_stack);
359 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000360}
361
362PySTEntryObject *
363PySymtable_Lookup(struct symtable *st, void *key)
364{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000365 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000367 k = PyLong_FromVoidPtr(key);
368 if (k == NULL)
369 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200370 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 if (v) {
372 assert(PySTEntry_Check(v));
373 Py_INCREF(v);
374 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200375 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyErr_SetString(PyExc_KeyError,
377 "unknown symbol table entry");
378 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000379
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000380 Py_DECREF(k);
381 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000382}
383
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000384static long
385_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000386{
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200387 PyObject *v = PyDict_GetItemWithError(ste->ste_symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 if (!v)
389 return 0;
390 assert(PyLong_Check(v));
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000391 return PyLong_AS_LONG(v);
392}
393
394int
Victor Stinner28ad12f2021-03-19 12:41:49 +0100395_PyST_GetScope(PySTEntryObject *ste, PyObject *name)
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000396{
397 long symbol = _PyST_GetSymbol(ste, name);
398 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000399}
400
Benjamin Petersond9c87022012-10-31 20:26:20 -0400401static int
402error_at_directive(PySTEntryObject *ste, PyObject *name)
403{
404 Py_ssize_t i;
405 PyObject *data;
406 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600407 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400408 data = PyList_GET_ITEM(ste->ste_directives, i);
409 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600410 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
411 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
412 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
413 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400414 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600415
416 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700417 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400418 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600419 PyErr_SetString(PyExc_RuntimeError,
420 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400421 return 0;
422}
423
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000424
425/* Analyze raw symbol information to determine scope of each name.
426
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000427 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000428 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000429 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000432 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433 explicit global is declared with the global statement. An implicit
434 global is a free variable for which the compiler has found no binding
435 in an enclosing function scope. The implicit global is either a global
436 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
437 to handle these names to implement slightly odd semantics. In such a
438 block, the name is treated as global until it is assigned to; then it
439 is treated as a local.
440
441 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000442 The first pass collects raw facts from the AST via the symtable_visit_*
443 functions: the name is a parameter here, the name is used but not defined
444 here, etc. The second pass analyzes these facts during a pass over the
445 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000446
447 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000449 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000450 Names which are explicitly declared nonlocal must exist in this set of
451 visible names - if they do not, a syntax error is raised. After doing
452 the local analysis, it analyzes each of its child blocks using an
453 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000454
Nick Coghlan650f0d02007-04-15 12:05:43 +0000455 The children update the free variable set. If a local variable is added to
456 the free variable set by the child, the variable is marked as a cell. The
457 function object being defined must provide runtime storage for the variable
458 that may outlive the function's frame. Cell variables are removed from the
459 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000460
Nick Coghlan650f0d02007-04-15 12:05:43 +0000461 During analysis, the names are:
462 symbols: dict mapping from symbol names to flag values (including offset scope values)
463 scopes: dict mapping from symbol names to scope values (no offset)
464 local: set of all symbol names local to the current scope
465 bound: set of all symbol names local to a containing function scope
466 free: set of all symbol names referenced but not bound in child scopes
467 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000468*/
469
470#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 PyObject *o = PyLong_FromLong(I); \
472 if (!o) \
473 return 0; \
474 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
475 Py_DECREF(o); \
476 return 0; \
477 } \
478 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000479}
480
481/* Decide on scope of name, given flags.
482
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000483 The namespace dictionaries may be modified to record information
484 about the new name. For example, a new global will add an entry to
485 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000486*/
487
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000488static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000489analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000490 PyObject *bound, PyObject *local, PyObject *free,
491 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000492{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 if (flags & DEF_NONLOCAL) {
495 PyErr_Format(PyExc_SyntaxError,
496 "name '%U' is nonlocal and global",
497 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400498 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000499 }
500 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
501 if (PySet_Add(global, name) < 0)
502 return 0;
503 if (bound && (PySet_Discard(bound, name) < 0))
504 return 0;
505 return 1;
506 }
507 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000508 if (!bound) {
509 PyErr_Format(PyExc_SyntaxError,
510 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400511 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000512 }
513 if (!PySet_Contains(bound, name)) {
514 PyErr_Format(PyExc_SyntaxError,
515 "no binding for nonlocal '%U' found",
516 name);
517
Benjamin Petersond9c87022012-10-31 20:26:20 -0400518 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 }
520 SET_SCOPE(scopes, name, FREE);
521 ste->ste_free = 1;
522 return PySet_Add(free, name) >= 0;
523 }
524 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000525 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (PySet_Add(local, name) < 0)
527 return 0;
528 if (PySet_Discard(global, name) < 0)
529 return 0;
530 return 1;
531 }
532 /* If an enclosing block has a binding for this name, it
533 is a free variable rather than a global variable.
534 Note that having a non-NULL bound implies that the block
535 is nested.
536 */
537 if (bound && PySet_Contains(bound, name)) {
538 SET_SCOPE(scopes, name, FREE);
539 ste->ste_free = 1;
540 return PySet_Add(free, name) >= 0;
541 }
542 /* If a parent has a global statement, then call it global
543 explicit? It could also be global implicit.
544 */
545 if (global && PySet_Contains(global, name)) {
546 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
547 return 1;
548 }
549 if (ste->ste_nested)
550 ste->ste_free = 1;
551 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
552 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000553}
554
555#undef SET_SCOPE
556
557/* If a name is defined in free and also in locals, then this block
558 provides the binding for the free variable. The name should be
559 marked CELL in this block and removed from the free list.
560
561 Note that the current block's free variables are included in free.
562 That's safe because no name can be free and local in the same scope.
563*/
564
565static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500566analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000567{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 PyObject *name, *v, *v_cell;
569 int success = 0;
570 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000571
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000572 v_cell = PyLong_FromLong(CELL);
573 if (!v_cell)
574 return 0;
575 while (PyDict_Next(scopes, &pos, &name, &v)) {
576 long scope;
577 assert(PyLong_Check(v));
578 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000579 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000580 continue;
581 if (!PySet_Contains(free, name))
582 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 /* Replace LOCAL with CELL for this name, and remove
584 from free. It is safe to replace the value of name
585 in the dict, because it will not cause a resize.
586 */
587 if (PyDict_SetItem(scopes, name, v_cell) < 0)
588 goto error;
589 if (PySet_Discard(free, name) < 0)
590 goto error;
591 }
592 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594 Py_DECREF(v_cell);
595 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596}
597
Benjamin Peterson312595c2013-05-15 15:26:42 -0500598static int
599drop_class_free(PySTEntryObject *ste, PyObject *free)
600{
601 int res;
602 if (!GET_IDENTIFIER(__class__))
603 return 0;
604 res = PySet_Discard(free, __class__);
605 if (res < 0)
606 return 0;
607 if (res)
608 ste->ste_needs_class_closure = 1;
609 return 1;
610}
611
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000612/* Enter the final scope information into the ste_symbols dict.
613 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000614 * All arguments are dicts. Modifies symbols, others are read-only.
615*/
616static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000618 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000619{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620 PyObject *name = NULL, *itr = NULL;
621 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
622 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000623
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000624 /* Update scope information for all symbols in this scope */
625 while (PyDict_Next(symbols, &pos, &name, &v)) {
626 long scope, flags;
627 assert(PyLong_Check(v));
628 flags = PyLong_AS_LONG(v);
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +0200629 v_scope = PyDict_GetItemWithError(scopes, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630 assert(v_scope && PyLong_Check(v_scope));
631 scope = PyLong_AS_LONG(v_scope);
632 flags |= (scope << SCOPE_OFFSET);
633 v_new = PyLong_FromLong(flags);
634 if (!v_new)
635 return 0;
636 if (PyDict_SetItem(symbols, name, v_new) < 0) {
637 Py_DECREF(v_new);
638 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000639 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000640 Py_DECREF(v_new);
641 }
642
643 /* Record not yet resolved free variables from children (if any) */
644 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
645 if (!v_free)
646 return 0;
647
648 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600649 if (itr == NULL) {
650 Py_DECREF(v_free);
651 return 0;
652 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653
654 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200655 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656
657 /* Handle symbol that already exists in this scope */
658 if (v) {
659 /* Handle a free variable in a method of
660 the class that has the same name as a local
661 or global in the class scope.
662 */
663 if (classflag &&
664 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
665 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
666 v_new = PyLong_FromLong(flags);
667 if (!v_new) {
668 goto error;
669 }
670 if (PyDict_SetItem(symbols, name, v_new) < 0) {
671 Py_DECREF(v_new);
672 goto error;
673 }
674 Py_DECREF(v_new);
675 }
676 /* It's a cell, or already free in this scope */
677 Py_DECREF(name);
678 continue;
679 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200680 else if (PyErr_Occurred()) {
681 goto error;
682 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000683 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200684 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000685 Py_DECREF(name);
686 continue; /* it's a global */
687 }
688 /* Propagate new free symbol up the lexical stack */
689 if (PyDict_SetItem(symbols, name, v_free) < 0) {
690 goto error;
691 }
692 Py_DECREF(name);
693 }
694 Py_DECREF(itr);
695 Py_DECREF(v_free);
696 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000697error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000698 Py_XDECREF(v_free);
699 Py_XDECREF(itr);
700 Py_XDECREF(name);
701 return 0;
702}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000703
704/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000705
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706 Arguments:
707 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000708 bound -- set of variables bound in enclosing scopes (input). bound
709 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000710 free -- set of free variables in enclosed scopes (output)
711 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000712
713 The implementation uses two mutually recursive functions,
714 analyze_block() and analyze_child_block(). analyze_block() is
715 responsible for analyzing the individual names defined in a block.
716 analyze_child_block() prepares temporary namespace dictionaries
717 used to evaluated nested blocks.
718
719 The two functions exist because a child block should see the name
720 bindings of its enclosing blocks, but those bindings should not
721 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000722*/
723
724static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
726 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000727
728static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
730 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000731{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
733 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
734 PyObject *temp;
735 int i, success = 0;
736 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000737
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000738 local = PySet_New(NULL); /* collect new names bound in block */
739 if (!local)
740 goto error;
741 scopes = PyDict_New(); /* collect scopes defined for each name */
742 if (!scopes)
743 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 /* Allocate new global and bound variable dictionaries. These
746 dictionaries hold the names visible in nested blocks. For
747 ClassBlocks, the bound and global names are initialized
748 before analyzing names, because class bindings aren't
749 visible in methods. For other blocks, they are initialized
750 after names are analyzed.
751 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 /* TODO(jhylton): Package these dicts in a struct so that we
754 can write reasonable helper functions?
755 */
756 newglobal = PySet_New(NULL);
757 if (!newglobal)
758 goto error;
759 newfree = PySet_New(NULL);
760 if (!newfree)
761 goto error;
762 newbound = PySet_New(NULL);
763 if (!newbound)
764 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000766 /* Class namespace has no effect on names visible in
767 nested functions, so populate the global and bound
768 sets to be passed to child blocks before analyzing
769 this one.
770 */
771 if (ste->ste_type == ClassBlock) {
772 /* Pass down known globals */
773 temp = PyNumber_InPlaceOr(newglobal, global);
774 if (!temp)
775 goto error;
776 Py_DECREF(temp);
777 /* Pass down previously bound symbols */
778 if (bound) {
779 temp = PyNumber_InPlaceOr(newbound, bound);
780 if (!temp)
781 goto error;
782 Py_DECREF(temp);
783 }
784 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000785
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
787 long flags = PyLong_AS_LONG(v);
788 if (!analyze_name(ste, scopes, name, flags,
789 bound, local, free, global))
790 goto error;
791 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000792
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 /* Populate global and bound sets to be passed to children. */
794 if (ste->ste_type != ClassBlock) {
795 /* Add function locals to bound set */
796 if (ste->ste_type == FunctionBlock) {
797 temp = PyNumber_InPlaceOr(newbound, local);
798 if (!temp)
799 goto error;
800 Py_DECREF(temp);
801 }
802 /* Pass down previously bound symbols */
803 if (bound) {
804 temp = PyNumber_InPlaceOr(newbound, bound);
805 if (!temp)
806 goto error;
807 Py_DECREF(temp);
808 }
809 /* Pass down known globals */
810 temp = PyNumber_InPlaceOr(newglobal, global);
811 if (!temp)
812 goto error;
813 Py_DECREF(temp);
814 }
815 else {
816 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000817 if (!GET_IDENTIFIER(__class__))
818 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 if (PySet_Add(newbound, __class__) < 0)
820 goto error;
821 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000822
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300823 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000824
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000825 newbound, newglobal now contain the names visible in
826 nested blocks. The free variables in the children will
827 be collected in allfree.
828 */
829 allfree = PySet_New(NULL);
830 if (!allfree)
831 goto error;
832 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
833 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
834 PySTEntryObject* entry;
835 assert(c && PySTEntry_Check(c));
836 entry = (PySTEntryObject*)c;
837 if (!analyze_child_block(entry, newbound, newfree, newglobal,
838 allfree))
839 goto error;
840 /* Check if any children have free variables */
841 if (entry->ste_free || entry->ste_child_free)
842 ste->ste_child_free = 1;
843 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000844
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000845 temp = PyNumber_InPlaceOr(newfree, allfree);
846 if (!temp)
847 goto error;
848 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000849
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000850 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500851 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500853 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 goto error;
855 /* Records the results of the analysis in the symbol table entry */
856 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
857 ste->ste_type == ClassBlock))
858 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 temp = PyNumber_InPlaceOr(free, newfree);
861 if (!temp)
862 goto error;
863 Py_DECREF(temp);
864 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000865 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 Py_XDECREF(scopes);
867 Py_XDECREF(local);
868 Py_XDECREF(newbound);
869 Py_XDECREF(newglobal);
870 Py_XDECREF(newfree);
871 Py_XDECREF(allfree);
872 if (!success)
873 assert(PyErr_Occurred());
874 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000875}
876
877static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000878analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
879 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000880{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
882 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000883
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000885
Martin Panter3ee62702016-06-04 04:57:19 +0000886 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 current block. The analyze_block() call modifies these
888 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000889
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 */
891 temp_bound = PySet_New(bound);
892 if (!temp_bound)
893 goto error;
894 temp_free = PySet_New(free);
895 if (!temp_free)
896 goto error;
897 temp_global = PySet_New(global);
898 if (!temp_global)
899 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000901 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
902 goto error;
903 temp = PyNumber_InPlaceOr(child_free, temp_free);
904 if (!temp)
905 goto error;
906 Py_DECREF(temp);
907 Py_DECREF(temp_bound);
908 Py_DECREF(temp_free);
909 Py_DECREF(temp_global);
910 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000911 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000912 Py_XDECREF(temp_bound);
913 Py_XDECREF(temp_free);
914 Py_XDECREF(temp_global);
915 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000916}
917
918static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000919symtable_analyze(struct symtable *st)
920{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921 PyObject *free, *global;
922 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 free = PySet_New(NULL);
925 if (!free)
926 return 0;
927 global = PySet_New(NULL);
928 if (!global) {
929 Py_DECREF(free);
930 return 0;
931 }
932 r = analyze_block(st->st_top, NULL, free, global);
933 Py_DECREF(free);
934 Py_DECREF(global);
935 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936}
937
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000938/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939 This reference is released when the block is exited, via the DECREF
940 in symtable_exit_block().
941*/
942
943static int
Andy Lester95668422020-03-06 09:46:04 -0600944symtable_exit_block(struct symtable *st)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000945{
Benjamin Peterson609da582011-06-29 22:52:39 -0500946 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000947
Benjamin Peterson609da582011-06-29 22:52:39 -0500948 st->st_cur = NULL;
949 size = PyList_GET_SIZE(st->st_stack);
950 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500951 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500953 if (--size)
954 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 }
956 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000957}
958
959static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000960symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000961 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000962{
Benjamin Peterson609da582011-06-29 22:52:39 -0500963 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000964
Benjamin Peterson609da582011-06-29 22:52:39 -0500965 ste = ste_new(st, name, block, ast, lineno, col_offset);
966 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000967 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500968 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
969 Py_DECREF(ste);
970 return 0;
971 }
972 prev = st->st_cur;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +1000973 /* bpo-37757: For now, disallow *all* assignment expressions in the
974 * outermost iterator expression of a comprehension, even those inside
975 * a nested comprehension or a lambda expression.
976 */
977 if (prev) {
978 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
979 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500980 /* The entry is owned by the stack. Borrow it for st_cur. */
981 Py_DECREF(ste);
982 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000983 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 st->st_global = st->st_cur->ste_symbols;
985 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500986 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 return 0;
988 }
989 }
990 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000991}
992
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000993static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994symtable_lookup(struct symtable *st, PyObject *name)
995{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 PyObject *mangled = _Py_Mangle(st->st_private, name);
997 if (!mangled)
998 return 0;
Pablo Galindo4901dc42019-08-26 16:14:07 +0100999 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1000 Py_DECREF(mangled);
1001 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001002}
1003
1004static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001005symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001006{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001007 PyObject *o;
1008 PyObject *dict;
1009 long val;
1010 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001011
Jeremy Hylton81e95022007-02-27 06:50:52 +00001012
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001013 if (!mangled)
1014 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001015 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001016 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001017 val = PyLong_AS_LONG(o);
1018 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1019 /* Is it better to use 'mangled' or 'name' here? */
1020 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001021 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001022 ste->ste_lineno,
1023 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001024 goto error;
1025 }
1026 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001027 }
1028 else if (PyErr_Occurred()) {
1029 goto error;
1030 }
1031 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001032 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001033 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001034 if (ste->ste_comp_iter_target) {
1035 /* This name is an iteration variable in a comprehension,
1036 * so check for a binding conflict with any named expressions.
1037 * Otherwise, mark it as an iteration variable so subsequent
1038 * named expressions can check for conflicts.
1039 */
1040 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1041 PyErr_Format(PyExc_SyntaxError,
1042 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1043 PyErr_SyntaxLocationObject(st->st_filename,
1044 ste->ste_lineno,
1045 ste->ste_col_offset + 1);
1046 goto error;
1047 }
1048 val |= DEF_COMP_ITER;
1049 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 o = PyLong_FromLong(val);
1051 if (o == NULL)
1052 goto error;
1053 if (PyDict_SetItem(dict, mangled, o) < 0) {
1054 Py_DECREF(o);
1055 goto error;
1056 }
1057 Py_DECREF(o);
1058
1059 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001060 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 goto error;
1062 } else if (flag & DEF_GLOBAL) {
1063 /* XXX need to update DEF_GLOBAL for other flags too;
1064 perhaps only DEF_FREE_GLOBAL */
1065 val = flag;
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001066 if ((o = PyDict_GetItemWithError(st->st_global, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 val |= PyLong_AS_LONG(o);
1068 }
Serhiy Storchakafb5db7e2020-10-26 08:43:39 +02001069 else if (PyErr_Occurred()) {
1070 goto error;
1071 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 goto error;
1075 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1076 Py_DECREF(o);
1077 goto error;
1078 }
1079 Py_DECREF(o);
1080 }
1081 Py_DECREF(mangled);
1082 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001083
1084error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001085 Py_DECREF(mangled);
1086 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001087}
1088
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001089static int
1090symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1091 return symtable_add_def_helper(st, name, flag, st->st_cur);
1092}
1093
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001094/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1095 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001096 function.
1097
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001098 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1099 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001100
1101 VISIT_QUIT macro returns the specified value exiting from the function but
1102 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001103*/
1104
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001105#define VISIT_QUIT(ST, X) \
1106 return --(ST)->recursion_depth,(X)
1107
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001108#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001109 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001110 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001111
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001112#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001113 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001114 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001115 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1116 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1117 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001118 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001119 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001121
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001122#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001123 int i; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001124 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1126 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1127 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001128 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001129 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001130}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001131
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001132#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 int i = 0; \
Pablo Galindoa5634c42020-09-16 19:42:00 +01001134 asdl_ ## TYPE ## _seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001136 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001138 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001139 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001141}
1142
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001143static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001144symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001145{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001146 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001147 int res;
1148 if (!st->st_cur->ste_directives) {
1149 st->st_cur->ste_directives = PyList_New(0);
1150 if (!st->st_cur->ste_directives)
1151 return 0;
1152 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001153 mangled = _Py_Mangle(st->st_private, name);
1154 if (!mangled)
1155 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001156 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001157 if (!data)
1158 return 0;
1159 res = PyList_Append(st->st_cur->ste_directives, data);
1160 Py_DECREF(data);
1161 return res == 0;
1162}
1163
1164
1165static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001166symtable_visit_stmt(struct symtable *st, stmt_ty s)
1167{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001168 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001169 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001170 "maximum recursion depth exceeded during compilation");
1171 VISIT_QUIT(st, 0);
1172 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001173 switch (s->kind) {
1174 case FunctionDef_kind:
1175 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001176 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001177 if (s->v.FunctionDef.args->defaults)
1178 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1179 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001180 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001181 if (!symtable_visit_annotations(st, s->v.FunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001182 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001183 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001184 if (s->v.FunctionDef.decorator_list)
1185 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1186 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001187 FunctionBlock, (void *)s, s->lineno,
1188 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001189 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001190 VISIT(st, arguments, s->v.FunctionDef.args);
1191 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001192 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001193 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001194 break;
1195 case ClassDef_kind: {
1196 PyObject *tmp;
1197 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001198 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1200 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001201 if (s->v.ClassDef.decorator_list)
1202 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1203 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001204 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001205 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 tmp = st->st_private;
1207 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001208 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001209 st->st_private = tmp;
Andy Lester95668422020-03-06 09:46:04 -06001210 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001211 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001212 break;
1213 }
1214 case Return_kind:
1215 if (s->v.Return.value) {
1216 VISIT(st, expr, s->v.Return.value);
1217 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001218 }
1219 break;
1220 case Delete_kind:
1221 VISIT_SEQ(st, expr, s->v.Delete.targets);
1222 break;
1223 case Assign_kind:
1224 VISIT_SEQ(st, expr, s->v.Assign.targets);
1225 VISIT(st, expr, s->v.Assign.value);
1226 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001227 case AnnAssign_kind:
1228 if (s->v.AnnAssign.target->kind == Name_kind) {
1229 expr_ty e_name = s->v.AnnAssign.target;
1230 long cur = symtable_lookup(st, e_name->v.Name.id);
1231 if (cur < 0) {
1232 VISIT_QUIT(st, 0);
1233 }
1234 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001235 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001236 && s->v.AnnAssign.simple) {
1237 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001238 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1239 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001240 PyErr_SyntaxLocationObject(st->st_filename,
1241 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001242 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001243 VISIT_QUIT(st, 0);
1244 }
1245 if (s->v.AnnAssign.simple &&
1246 !symtable_add_def(st, e_name->v.Name.id,
1247 DEF_ANNOT | DEF_LOCAL)) {
1248 VISIT_QUIT(st, 0);
1249 }
1250 else {
1251 if (s->v.AnnAssign.value
1252 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1253 VISIT_QUIT(st, 0);
1254 }
1255 }
1256 }
1257 else {
1258 VISIT(st, expr, s->v.AnnAssign.target);
1259 }
1260 VISIT(st, expr, s->v.AnnAssign.annotation);
1261 if (s->v.AnnAssign.value) {
1262 VISIT(st, expr, s->v.AnnAssign.value);
1263 }
1264 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001265 case AugAssign_kind:
1266 VISIT(st, expr, s->v.AugAssign.target);
1267 VISIT(st, expr, s->v.AugAssign.value);
1268 break;
1269 case For_kind:
1270 VISIT(st, expr, s->v.For.target);
1271 VISIT(st, expr, s->v.For.iter);
1272 VISIT_SEQ(st, stmt, s->v.For.body);
1273 if (s->v.For.orelse)
1274 VISIT_SEQ(st, stmt, s->v.For.orelse);
1275 break;
1276 case While_kind:
1277 VISIT(st, expr, s->v.While.test);
1278 VISIT_SEQ(st, stmt, s->v.While.body);
1279 if (s->v.While.orelse)
1280 VISIT_SEQ(st, stmt, s->v.While.orelse);
1281 break;
1282 case If_kind:
1283 /* XXX if 0: and lookup_yield() hacks */
1284 VISIT(st, expr, s->v.If.test);
1285 VISIT_SEQ(st, stmt, s->v.If.body);
1286 if (s->v.If.orelse)
1287 VISIT_SEQ(st, stmt, s->v.If.orelse);
1288 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001289 case Match_kind:
1290 VISIT(st, expr, s->v.Match.subject);
1291 VISIT_SEQ(st, match_case, s->v.Match.cases);
1292 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 case Raise_kind:
1294 if (s->v.Raise.exc) {
1295 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001296 if (s->v.Raise.cause) {
1297 VISIT(st, expr, s->v.Raise.cause);
1298 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001299 }
1300 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001301 case Try_kind:
1302 VISIT_SEQ(st, stmt, s->v.Try.body);
1303 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1304 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1305 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 break;
1307 case Assert_kind:
1308 VISIT(st, expr, s->v.Assert.test);
1309 if (s->v.Assert.msg)
1310 VISIT(st, expr, s->v.Assert.msg);
1311 break;
1312 case Import_kind:
1313 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 break;
1315 case ImportFrom_kind:
1316 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 break;
1318 case Global_kind: {
1319 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001320 asdl_identifier_seq *seq = s->v.Global.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001321 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1322 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001323 long cur = symtable_lookup(st, name);
1324 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001325 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001326 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1327 const char* msg;
1328 if (cur & DEF_PARAM) {
1329 msg = GLOBAL_PARAM;
1330 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001331 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001332 } else if (cur & DEF_ANNOT) {
1333 msg = GLOBAL_ANNOT;
1334 } else { /* DEF_LOCAL */
1335 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001336 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001337 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001338 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001339 PyErr_SyntaxLocationObject(st->st_filename,
1340 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001341 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001342 VISIT_QUIT(st, 0);
1343 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001345 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001346 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001347 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001348 }
1349 break;
1350 }
1351 case Nonlocal_kind: {
1352 int i;
Pablo Galindoa5634c42020-09-16 19:42:00 +01001353 asdl_identifier_seq *seq = s->v.Nonlocal.names;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001354 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1355 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001356 long cur = symtable_lookup(st, name);
1357 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001358 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001359 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1360 const char* msg;
1361 if (cur & DEF_PARAM) {
1362 msg = NONLOCAL_PARAM;
1363 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001364 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001365 } else if (cur & DEF_ANNOT) {
1366 msg = NONLOCAL_ANNOT;
1367 } else { /* DEF_LOCAL */
1368 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001369 }
1370 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001371 PyErr_SyntaxLocationObject(st->st_filename,
1372 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001373 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001374 VISIT_QUIT(st, 0);
1375 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001377 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001378 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001379 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 }
1381 break;
1382 }
1383 case Expr_kind:
1384 VISIT(st, expr, s->v.Expr.value);
1385 break;
1386 case Pass_kind:
1387 case Break_kind:
1388 case Continue_kind:
1389 /* nothing to do here */
1390 break;
1391 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001392 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 VISIT_SEQ(st, stmt, s->v.With.body);
1394 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001395 case AsyncFunctionDef_kind:
1396 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1397 VISIT_QUIT(st, 0);
1398 if (s->v.AsyncFunctionDef.args->defaults)
1399 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1400 if (s->v.AsyncFunctionDef.args->kw_defaults)
1401 VISIT_SEQ_WITH_NULL(st, expr,
1402 s->v.AsyncFunctionDef.args->kw_defaults);
Andy Lester95668422020-03-06 09:46:04 -06001403 if (!symtable_visit_annotations(st, s->v.AsyncFunctionDef.args,
Yury Selivanov75445082015-05-11 22:57:16 -04001404 s->v.AsyncFunctionDef.returns))
1405 VISIT_QUIT(st, 0);
1406 if (s->v.AsyncFunctionDef.decorator_list)
1407 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1408 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1409 FunctionBlock, (void *)s, s->lineno,
1410 s->col_offset))
1411 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001412 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001413 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1414 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
Andy Lester95668422020-03-06 09:46:04 -06001415 if (!symtable_exit_block(st))
Yury Selivanov75445082015-05-11 22:57:16 -04001416 VISIT_QUIT(st, 0);
1417 break;
1418 case AsyncWith_kind:
1419 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1420 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1421 break;
1422 case AsyncFor_kind:
1423 VISIT(st, expr, s->v.AsyncFor.target);
1424 VISIT(st, expr, s->v.AsyncFor.iter);
1425 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1426 if (s->v.AsyncFor.orelse)
1427 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1428 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001430 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001431}
1432
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001434symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1435{
1436 assert(st->st_stack);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001437 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001438
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001439 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001440 Py_ssize_t i, size;
1441 struct _symtable_entry *ste;
1442 size = PyList_GET_SIZE(st->st_stack);
1443 assert(size);
1444
1445 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1446 for (i = size - 1; i >= 0; i--) {
1447 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1448
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001449 /* If we find a comprehension scope, check for a target
1450 * binding conflict with iteration variables, otherwise skip it
1451 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001452 if (ste->ste_comprehension) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001453 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1454 if (target_in_scope & DEF_COMP_ITER) {
1455 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1456 PyErr_SyntaxLocationObject(st->st_filename,
1457 e->lineno,
1458 e->col_offset);
1459 VISIT_QUIT(st, 0);
1460 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001461 continue;
1462 }
1463
Pablo Galindofd5c4142019-10-14 05:18:05 +01001464 /* If we find a FunctionBlock entry, add as GLOBAL/LOCAL or NONLOCAL/LOCAL */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001465 if (ste->ste_type == FunctionBlock) {
Pablo Galindofd5c4142019-10-14 05:18:05 +01001466 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1467 if (target_in_scope & DEF_GLOBAL) {
1468 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
1469 VISIT_QUIT(st, 0);
1470 } else {
1471 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
1472 VISIT_QUIT(st, 0);
1473 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001474 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001475 VISIT_QUIT(st, 0);
1476
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001477 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001478 }
1479 /* If we find a ModuleBlock entry, add as GLOBAL */
1480 if (ste->ste_type == ModuleBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001481 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001482 VISIT_QUIT(st, 0);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001483 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001484 VISIT_QUIT(st, 0);
1485
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001486 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001487 }
1488 /* Disallow usage in ClassBlock */
1489 if (ste->ste_type == ClassBlock) {
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001490 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001491 PyErr_SyntaxLocationObject(st->st_filename,
1492 e->lineno,
1493 e->col_offset);
1494 VISIT_QUIT(st, 0);
1495 }
1496 }
1497
1498 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1499 and should never fall to this case
1500 */
1501 assert(0);
1502 return 0;
1503}
1504
1505static int
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001506symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1507{
1508 if (st->st_cur->ste_comp_iter_expr > 0) {
1509 /* Assignment isn't allowed in a comprehension iterable expression */
1510 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1511 PyErr_SyntaxLocationObject(st->st_filename,
1512 e->lineno,
1513 e->col_offset);
Nick Coghlan06145232019-08-29 23:26:53 +10001514 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001515 }
1516 if (st->st_cur->ste_comprehension) {
1517 /* Inside a comprehension body, so find the right target scope */
1518 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
Nick Coghlan06145232019-08-29 23:26:53 +10001519 return 0;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001520 }
1521 VISIT(st, expr, e->v.NamedExpr.value);
1522 VISIT(st, expr, e->v.NamedExpr.target);
Nick Coghlan06145232019-08-29 23:26:53 +10001523 return 1;
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001524}
1525
1526static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527symtable_visit_expr(struct symtable *st, expr_ty e)
1528{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001529 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001530 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001531 "maximum recursion depth exceeded during compilation");
1532 VISIT_QUIT(st, 0);
1533 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001534 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001535 case NamedExpr_kind:
Pablo Galindo0e4ea162019-08-26 15:52:25 +01001536 if(!symtable_handle_namedexpr(st, e))
1537 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001538 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539 case BoolOp_kind:
1540 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1541 break;
1542 case BinOp_kind:
1543 VISIT(st, expr, e->v.BinOp.left);
1544 VISIT(st, expr, e->v.BinOp.right);
1545 break;
1546 case UnaryOp_kind:
1547 VISIT(st, expr, e->v.UnaryOp.operand);
1548 break;
1549 case Lambda_kind: {
1550 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001551 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (e->v.Lambda.args->defaults)
1553 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001554 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001555 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001557 FunctionBlock, (void *)e, e->lineno,
1558 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001559 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001560 VISIT(st, arguments, e->v.Lambda.args);
1561 VISIT(st, expr, e->v.Lambda.body);
Andy Lester95668422020-03-06 09:46:04 -06001562 if (!symtable_exit_block(st))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001563 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 break;
1565 }
1566 case IfExp_kind:
1567 VISIT(st, expr, e->v.IfExp.test);
1568 VISIT(st, expr, e->v.IfExp.body);
1569 VISIT(st, expr, e->v.IfExp.orelse);
1570 break;
1571 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001572 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573 VISIT_SEQ(st, expr, e->v.Dict.values);
1574 break;
1575 case Set_kind:
1576 VISIT_SEQ(st, expr, e->v.Set.elts);
1577 break;
1578 case GeneratorExp_kind:
1579 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001580 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 break;
1582 case ListComp_kind:
1583 if (!symtable_visit_listcomp(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 SetComp_kind:
1587 if (!symtable_visit_setcomp(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 DictComp_kind:
1591 if (!symtable_visit_dictcomp(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 Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001595 if (e->v.Yield.value)
1596 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001599 case YieldFrom_kind:
1600 VISIT(st, expr, e->v.YieldFrom.value);
1601 st->st_cur->ste_generator = 1;
1602 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001603 case Await_kind:
1604 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001605 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001606 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001607 case Compare_kind:
1608 VISIT(st, expr, e->v.Compare.left);
1609 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1610 break;
1611 case Call_kind:
1612 VISIT(st, expr, e->v.Call.func);
1613 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001614 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001616 case FormattedValue_kind:
1617 VISIT(st, expr, e->v.FormattedValue.value);
1618 if (e->v.FormattedValue.format_spec)
1619 VISIT(st, expr, e->v.FormattedValue.format_spec);
1620 break;
1621 case JoinedStr_kind:
1622 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1623 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001624 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 /* Nothing to do here. */
1626 break;
1627 /* The following exprs can be assignment targets. */
1628 case Attribute_kind:
1629 VISIT(st, expr, e->v.Attribute.value);
1630 break;
1631 case Subscript_kind:
1632 VISIT(st, expr, e->v.Subscript.value);
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001633 VISIT(st, expr, e->v.Subscript.slice);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001634 break;
1635 case Starred_kind:
1636 VISIT(st, expr, e->v.Starred.value);
1637 break;
Serhiy Storchaka13d52c22020-03-10 18:52:34 +02001638 case Slice_kind:
1639 if (e->v.Slice.lower)
1640 VISIT(st, expr, e->v.Slice.lower)
1641 if (e->v.Slice.upper)
1642 VISIT(st, expr, e->v.Slice.upper)
1643 if (e->v.Slice.step)
1644 VISIT(st, expr, e->v.Slice.step)
1645 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 case Name_kind:
Brandt Bucher145bf262021-02-26 14:51:55 -08001647 // Don't make "_" a local when used in a pattern:
1648 if (st->in_pattern &&
1649 e->v.Name.ctx == Store &&
1650 _PyUnicode_EqualToASCIIString(e->v.Name.id, "_"))
1651 {
1652 break;
1653 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 if (!symtable_add_def(st, e->v.Name.id,
1655 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001656 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 /* Special-case super: it counts as a use of __class__ */
1658 if (e->v.Name.ctx == Load &&
1659 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001660 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001661 if (!GET_IDENTIFIER(__class__) ||
1662 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001663 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001664 }
1665 break;
1666 /* child nodes of List and Tuple will have expr_context set */
1667 case List_kind:
1668 VISIT_SEQ(st, expr, e->v.List.elts);
1669 break;
1670 case Tuple_kind:
1671 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1672 break;
Brandt Bucher145bf262021-02-26 14:51:55 -08001673 case MatchAs_kind:
1674 VISIT(st, expr, e->v.MatchAs.pattern);
1675 symtable_add_def(st, e->v.MatchAs.name, DEF_LOCAL);
1676 break;
1677 case MatchOr_kind:
1678 VISIT_SEQ(st, expr, e->v.MatchOr.patterns);
1679 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001681 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682}
1683
1684static int
1685symtable_implicit_arg(struct symtable *st, int pos)
1686{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1688 if (id == NULL)
1689 return 0;
1690 if (!symtable_add_def(st, id, DEF_PARAM)) {
1691 Py_DECREF(id);
1692 return 0;
1693 }
1694 Py_DECREF(id);
1695 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001696}
1697
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001698static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001699symtable_visit_params(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001700{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (!args)
1704 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 for (i = 0; i < asdl_seq_LEN(args); i++) {
1707 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1708 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1709 return 0;
1710 }
1711
1712 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001713}
1714
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715static int
Pablo Galindoa5634c42020-09-16 19:42:00 +01001716symtable_visit_argannotations(struct symtable *st, asdl_arg_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001717{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 if (!args)
1721 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001722
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 for (i = 0; i < asdl_seq_LEN(args); i++) {
1724 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1725 if (arg->annotation)
1726 VISIT(st, expr, arg->annotation);
1727 }
1728
1729 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001730}
1731
Neal Norwitzc1505362006-12-28 06:47:50 +00001732static int
Andy Lester95668422020-03-06 09:46:04 -06001733symtable_visit_annotations(struct symtable *st, arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001734{
Anthony Sottileec007cb2020-01-04 20:57:21 -05001735 if (a->posonlyargs && !symtable_visit_argannotations(st, a->posonlyargs))
1736 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001737 if (a->args && !symtable_visit_argannotations(st, a->args))
1738 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001739 if (a->vararg && a->vararg->annotation)
1740 VISIT(st, expr, a->vararg->annotation);
1741 if (a->kwarg && a->kwarg->annotation)
1742 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1744 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001745 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001746 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001747 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748}
1749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001750static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001751symtable_visit_arguments(struct symtable *st, arguments_ty a)
1752{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753 /* skip default arguments inside function block
1754 XXX should ast be different?
1755 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001756 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1757 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001758 if (a->args && !symtable_visit_params(st, a->args))
1759 return 0;
1760 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1761 return 0;
1762 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001763 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 return 0;
1765 st->st_cur->ste_varargs = 1;
1766 }
1767 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001768 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return 0;
1770 st->st_cur->ste_varkeywords = 1;
1771 }
1772 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001773}
1774
1775
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001776static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1778{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 if (eh->v.ExceptHandler.type)
1780 VISIT(st, expr, eh->v.ExceptHandler.type);
1781 if (eh->v.ExceptHandler.name)
1782 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1783 return 0;
1784 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1785 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001786}
1787
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001788static int
1789symtable_visit_withitem(struct symtable *st, withitem_ty item)
1790{
1791 VISIT(st, expr, item->context_expr);
1792 if (item->optional_vars) {
1793 VISIT(st, expr, item->optional_vars);
1794 }
1795 return 1;
1796}
1797
Brandt Bucher145bf262021-02-26 14:51:55 -08001798static int
1799symtable_visit_match_case(struct symtable *st, match_case_ty m)
1800{
1801 assert(!st->in_pattern);
1802 st->in_pattern = 1;
1803 VISIT(st, expr, m->pattern);
1804 assert(st->in_pattern);
1805 st->in_pattern = 0;
1806 if (m->guard) {
1807 VISIT(st, expr, m->guard);
1808 }
1809 VISIT_SEQ(st, stmt, m->body);
1810 return 1;
1811}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001812
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001814symtable_visit_alias(struct symtable *st, alias_ty a)
1815{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001816 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001817 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 dotted package name (e.g. spam.eggs)
1819 */
1820 PyObject *store_name;
1821 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001822 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1823 PyUnicode_GET_LENGTH(name), 1);
1824 if (dot != -1) {
1825 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 if (!store_name)
1827 return 0;
1828 }
1829 else {
1830 store_name = name;
1831 Py_INCREF(store_name);
1832 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001833 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1835 Py_DECREF(store_name);
1836 return r;
1837 }
1838 else {
1839 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001840 int lineno = st->st_cur->ste_lineno;
1841 int col_offset = st->st_cur->ste_col_offset;
1842 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001843 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001844 Py_DECREF(store_name);
1845 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001846 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001847 Py_DECREF(store_name);
1848 return 1;
1849 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001850}
1851
1852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001854symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1855{
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001856 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001857 VISIT(st, expr, lc->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001858 st->st_cur->ste_comp_iter_target = 0;
1859 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001860 VISIT(st, expr, lc->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001861 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001862 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001863 if (lc->is_async) {
1864 st->st_cur->ste_coroutine = 1;
1865 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001866 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001867}
1868
1869
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001870static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001871symtable_visit_keyword(struct symtable *st, keyword_ty k)
1872{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001873 VISIT(st, expr, k->value);
1874 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001875}
1876
1877
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001878static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001879symtable_handle_comprehension(struct symtable *st, expr_ty e,
Pablo Galindoa5634c42020-09-16 19:42:00 +01001880 identifier scope_name, asdl_comprehension_seq *generators,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001881 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001882{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001883 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 comprehension_ty outermost = ((comprehension_ty)
1885 asdl_seq_GET(generators, 0));
1886 /* Outermost iterator is evaluated in current scope */
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001887 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 VISIT(st, expr, outermost->iter);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001889 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001890 /* Create comprehension scope for the rest */
1891 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001892 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1893 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001894 return 0;
1895 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001896 if (outermost->is_async) {
1897 st->st_cur->ste_coroutine = 1;
1898 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001899 st->st_cur->ste_comprehension = 1;
1900
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001901 /* Outermost iter is received as an argument */
1902 if (!symtable_implicit_arg(st, 0)) {
Andy Lester95668422020-03-06 09:46:04 -06001903 symtable_exit_block(st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 return 0;
1905 }
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001906 /* Visit iteration variable target, and mark them as such */
1907 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001908 VISIT(st, expr, outermost->target);
Nick Coghlan5dbe0f52019-08-25 23:45:40 +10001909 st->st_cur->ste_comp_iter_target = 0;
1910 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001911 VISIT_SEQ(st, expr, outermost->ifs);
1912 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001913 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001914 VISIT(st, expr, value);
1915 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001916 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001917 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001918 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1919 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1920 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1921 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001922 PyErr_SyntaxLocationObject(st->st_filename,
1923 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001924 st->st_cur->ste_col_offset + 1);
Andy Lester95668422020-03-06 09:46:04 -06001925 symtable_exit_block(st);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001926 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001927 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001928 st->st_cur->ste_generator = is_generator;
Andy Lester95668422020-03-06 09:46:04 -06001929 return symtable_exit_block(st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001930}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001931
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001932static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001933symtable_visit_genexp(struct symtable *st, expr_ty e)
1934{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001935 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1936 e->v.GeneratorExp.generators,
1937 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001938}
1939
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001940static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001941symtable_visit_listcomp(struct symtable *st, expr_ty e)
1942{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001943 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1944 e->v.ListComp.generators,
1945 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001946}
1947
1948static int
1949symtable_visit_setcomp(struct symtable *st, expr_ty e)
1950{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001951 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1952 e->v.SetComp.generators,
1953 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001954}
1955
1956static int
1957symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1958{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001959 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1960 e->v.DictComp.generators,
1961 e->v.DictComp.key,
1962 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001963}
Victor Stinner28ad12f2021-03-19 12:41:49 +01001964
1965
1966struct symtable *
1967_Py_SymtableStringObjectFlags(const char *str, PyObject *filename,
1968 int start, PyCompilerFlags *flags)
1969{
1970 struct symtable *st;
1971 mod_ty mod;
1972 PyArena *arena;
1973
1974 arena = PyArena_New();
1975 if (arena == NULL)
1976 return NULL;
1977
1978 mod = PyParser_ASTFromStringObject(str, filename, start, flags, arena);
1979 if (mod == NULL) {
1980 PyArena_Free(arena);
1981 return NULL;
1982 }
1983 st = _PySymtable_Build(mod, filename, 0);
1984 PyArena_Free(arena);
1985 return st;
1986}