blob: 2795e0f1115b8df9cd3344f88c560d310c2ee7d8 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner621cebe2018-11-12 16:53:38 +01002#include "pycore_pystate.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00003#include "symtable.h"
Victor Stinner3bb183d2018-11-22 18:38:38 +01004#undef Yield /* undefine macro conflicting with <winbase.h> */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00005#include "structmember.h"
6
Neal Norwitz5d0ad502005-12-19 04:27:42 +00007/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02008#define GLOBAL_PARAM \
9"name '%U' is parameter and global"
10
11#define NONLOCAL_PARAM \
12"name '%U' is parameter and nonlocal"
13
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000014#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070015"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000016
Jeremy Hylton81e95022007-02-27 06:50:52 +000017#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070018"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000019
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000020#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070021"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000022
Jeremy Hylton81e95022007-02-27 06:50:52 +000023#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070024"name '%U' is used prior to nonlocal declaration"
25
26#define GLOBAL_ANNOT \
27"annotated name '%U' can't be global"
28
29#define NONLOCAL_ANNOT \
30"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000031
Neal Norwitz5d0ad502005-12-19 04:27:42 +000032#define IMPORT_STAR_WARNING "import * only allowed at module level"
33
Emily Morehouse8f59ee02019-01-24 16:49:56 -070034#define NAMED_EXPR_COMP_IN_CLASS \
Nick Coghlan6ca03072019-08-26 00:41:47 +100035"assignment expression within a comprehension cannot be used in a class body"
36
37#define NAMED_EXPR_COMP_CONFLICT \
38"assignment expression cannot rebind comprehension iteration variable '%U'"
39
40#define NAMED_EXPR_COMP_INNER_LOOP_CONFLICT \
41"comprehension inner loop cannot rebind assignment expression target '%U'"
42
43#define NAMED_EXPR_COMP_ITER_EXPR \
44"assignment expression cannot be used in a comprehension iterable expression"
Emily Morehouse8f59ee02019-01-24 16:49:56 -070045
Neal Norwitz090b3dd2006-02-28 22:36:46 +000046static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000047ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000048 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000049{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020051 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000052
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 k = PyLong_FromVoidPtr(key);
54 if (k == NULL)
55 goto fail;
56 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020057 if (ste == NULL) {
58 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020060 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020062 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000063
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020065 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000066
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000067 ste->ste_symbols = NULL;
68 ste->ste_varnames = NULL;
69 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Benjamin Petersond9c87022012-10-31 20:26:20 -040071 ste->ste_directives = NULL;
72
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 ste->ste_nested = 0;
75 ste->ste_free = 0;
76 ste->ste_varargs = 0;
77 ste->ste_varkeywords = 0;
78 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000079 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000080 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000081 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000082
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000083 if (st->st_cur != NULL &&
84 (st->st_cur->ste_nested ||
85 st->st_cur->ste_type == FunctionBlock))
86 ste->ste_nested = 1;
87 ste->ste_child_free = 0;
88 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070089 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070090 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000091 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050092 ste->ste_needs_class_closure = 0;
Nick Coghlan6ca03072019-08-26 00:41:47 +100093 ste->ste_comp_iter_target = 0;
94 ste->ste_comp_iter_expr = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000095
Victor Stinner9a4fb662013-07-11 22:49:00 +020096 ste->ste_symbols = PyDict_New();
97 ste->ste_varnames = PyList_New(0);
98 ste->ste_children = PyList_New(0);
99 if (ste->ste_symbols == NULL
100 || ste->ste_varnames == NULL
101 || ste->ste_children == NULL)
102 goto fail;
103
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000104 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
105 goto fail;
106
107 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 Py_XDECREF(ste);
110 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000111}
112
113static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000114ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000115{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
117 ste->ste_name,
118 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000119}
120
121static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000122ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000123{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 ste->ste_table = NULL;
125 Py_XDECREF(ste->ste_id);
126 Py_XDECREF(ste->ste_name);
127 Py_XDECREF(ste->ste_symbols);
128 Py_XDECREF(ste->ste_varnames);
129 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400130 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000132}
133
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000134#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000135
Guido van Rossum6f799372001-09-20 20:46:19 +0000136static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 {"id", T_OBJECT, OFF(ste_id), READONLY},
138 {"name", T_OBJECT, OFF(ste_name), READONLY},
139 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
140 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
141 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000142 {"nested", T_INT, OFF(ste_nested), READONLY},
143 {"type", T_INT, OFF(ste_type), READONLY},
144 {"lineno", T_INT, OFF(ste_lineno), READONLY},
145 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000146};
147
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000148PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000149 PyVarObject_HEAD_INIT(&PyType_Type, 0)
150 "symtable entry",
151 sizeof(PySTEntryObject),
152 0,
153 (destructor)ste_dealloc, /* tp_dealloc */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200154 0, /* tp_vectorcall_offset */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 0, /* tp_getattr */
156 0, /* tp_setattr */
Jeroen Demeyer530f5062019-05-31 04:13:39 +0200157 0, /* tp_as_async */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000158 (reprfunc)ste_repr, /* tp_repr */
159 0, /* tp_as_number */
160 0, /* tp_as_sequence */
161 0, /* tp_as_mapping */
162 0, /* tp_hash */
163 0, /* tp_call */
164 0, /* tp_str */
165 PyObject_GenericGetAttr, /* tp_getattro */
166 0, /* tp_setattro */
167 0, /* tp_as_buffer */
168 Py_TPFLAGS_DEFAULT, /* tp_flags */
169 0, /* tp_doc */
170 0, /* tp_traverse */
171 0, /* tp_clear */
172 0, /* tp_richcompare */
173 0, /* tp_weaklistoffset */
174 0, /* tp_iter */
175 0, /* tp_iternext */
176 0, /* tp_methods */
177 ste_memberlist, /* tp_members */
178 0, /* tp_getset */
179 0, /* tp_base */
180 0, /* tp_dict */
181 0, /* tp_descr_get */
182 0, /* tp_descr_set */
183 0, /* tp_dictoffset */
184 0, /* tp_init */
185 0, /* tp_alloc */
186 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000187};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000188
189static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000190static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000191 _Py_block_ty block, void *ast, int lineno,
192 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193static int symtable_exit_block(struct symtable *st, void *ast);
194static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
195static int symtable_visit_expr(struct symtable *st, expr_ty s);
196static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000197static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
198static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000199static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200static int symtable_visit_arguments(struct symtable *st, arguments_ty);
201static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
202static int symtable_visit_alias(struct symtable *st, alias_ty);
203static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
204static int symtable_visit_keyword(struct symtable *st, keyword_ty);
205static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000206static int symtable_visit_params(struct symtable *st, asdl_seq *args);
207static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400209static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500210static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
212
Nick Coghlan650f0d02007-04-15 12:05:43 +0000213static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000214 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500215 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216
217#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000218 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000219
220#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000221"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222
223static struct symtable *
224symtable_new(void)
225{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000227
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000228 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700229 if (st == NULL) {
230 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000231 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700232 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 st->st_filename = NULL;
235 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000236
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 if ((st->st_stack = PyList_New(0)) == NULL)
238 goto fail;
239 if ((st->st_blocks = PyDict_New()) == NULL)
240 goto fail;
241 st->st_cur = NULL;
242 st->st_private = NULL;
243 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000244 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 PySymtable_Free(st);
246 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000247}
248
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000249/* When compiling the use of C stack is probably going to be a lot
250 lighter than when executing Python code but still can overflow
251 and causing a Python crash if not checked (e.g. eval("()"*300000)).
252 Using the current recursion limit for the compiler seems too
253 restrictive (it caused at least one test to fail) so a factor is
254 used to allow deeper recursion when compiling an expression.
255
256 Using a scaling factor means this should automatically adjust when
257 the recursion limit is adjusted for small or large C stack allocations.
258*/
259#define COMPILER_STACK_FRAME_SCALE 3
260
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000261struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200262PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000263{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000264 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 asdl_seq *seq;
266 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000267 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400268 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000269
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000270 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200271 return NULL;
272 if (filename == NULL) {
273 PySymtable_Free(st);
274 return NULL;
275 }
276 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 st->st_filename = filename;
278 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000279
280 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100281 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000282 if (!tstate) {
283 PySymtable_Free(st);
284 return NULL;
285 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400286 /* Be careful here to prevent overflow. */
287 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
288 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
289 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
290 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000291
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000292 /* Make the initial symbol information gathering pass */
293 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000294 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000295 PySymtable_Free(st);
296 return NULL;
297 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000298
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000299 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000300 switch (mod->kind) {
301 case Module_kind:
302 seq = mod->v.Module.body;
303 for (i = 0; i < asdl_seq_LEN(seq); i++)
304 if (!symtable_visit_stmt(st,
305 (stmt_ty)asdl_seq_GET(seq, i)))
306 goto error;
307 break;
308 case Expression_kind:
309 if (!symtable_visit_expr(st, mod->v.Expression.body))
310 goto error;
311 break;
312 case Interactive_kind:
313 seq = mod->v.Interactive.body;
314 for (i = 0; i < asdl_seq_LEN(seq); i++)
315 if (!symtable_visit_stmt(st,
316 (stmt_ty)asdl_seq_GET(seq, i)))
317 goto error;
318 break;
319 case Suite_kind:
320 PyErr_SetString(PyExc_RuntimeError,
321 "this compiler does not handle Suites");
322 goto error;
Guido van Rossum522346d2019-02-11 11:34:50 -0800323 case FunctionType_kind:
324 PyErr_SetString(PyExc_RuntimeError,
325 "this compiler does not handle FunctionTypes");
326 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 }
328 if (!symtable_exit_block(st, (void *)mod)) {
329 PySymtable_Free(st);
330 return NULL;
331 }
332 /* Make the second symbol analysis pass */
333 if (symtable_analyze(st))
334 return st;
335 PySymtable_Free(st);
336 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000338 (void) symtable_exit_block(st, (void *)mod);
339 PySymtable_Free(st);
340 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000341}
342
Victor Stinner14e461d2013-08-26 22:28:21 +0200343struct symtable *
344PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
345{
346 PyObject *filename;
347 struct symtable *st;
348 filename = PyUnicode_DecodeFSDefault(filename_str);
349 if (filename == NULL)
350 return NULL;
351 st = PySymtable_BuildObject(mod, filename, future);
352 Py_DECREF(filename);
353 return st;
354}
355
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000356void
357PySymtable_Free(struct symtable *st)
358{
Victor Stinner14e461d2013-08-26 22:28:21 +0200359 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000360 Py_XDECREF(st->st_blocks);
361 Py_XDECREF(st->st_stack);
362 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000363}
364
365PySTEntryObject *
366PySymtable_Lookup(struct symtable *st, void *key)
367{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 k = PyLong_FromVoidPtr(key);
371 if (k == NULL)
372 return NULL;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200373 v = PyDict_GetItemWithError(st->st_blocks, k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374 if (v) {
375 assert(PySTEntry_Check(v));
376 Py_INCREF(v);
377 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200378 else if (!PyErr_Occurred()) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyErr_SetString(PyExc_KeyError,
380 "unknown symbol table entry");
381 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 Py_DECREF(k);
384 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000385}
386
Nick Coghlan6ca03072019-08-26 00:41:47 +1000387static long
388_PyST_GetSymbol(PySTEntryObject *ste, PyObject *name)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000389{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000390 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
391 if (!v)
392 return 0;
393 assert(PyLong_Check(v));
Nick Coghlan6ca03072019-08-26 00:41:47 +1000394 return PyLong_AS_LONG(v);
395}
396
397int
398PyST_GetScope(PySTEntryObject *ste, PyObject *name)
399{
400 long symbol = _PyST_GetSymbol(ste, name);
401 return (symbol >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000402}
403
Benjamin Petersond9c87022012-10-31 20:26:20 -0400404static int
405error_at_directive(PySTEntryObject *ste, PyObject *name)
406{
407 Py_ssize_t i;
408 PyObject *data;
409 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600410 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400411 data = PyList_GET_ITEM(ste->ste_directives, i);
412 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600413 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
414 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
415 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
416 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400417 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600418
419 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700420 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400421 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600422 PyErr_SetString(PyExc_RuntimeError,
423 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400424 return 0;
425}
426
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000427
428/* Analyze raw symbol information to determine scope of each name.
429
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000430 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000431 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000432 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000434
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000435 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000436 explicit global is declared with the global statement. An implicit
437 global is a free variable for which the compiler has found no binding
438 in an enclosing function scope. The implicit global is either a global
439 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
440 to handle these names to implement slightly odd semantics. In such a
441 block, the name is treated as global until it is assigned to; then it
442 is treated as a local.
443
444 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000445 The first pass collects raw facts from the AST via the symtable_visit_*
446 functions: the name is a parameter here, the name is used but not defined
447 here, etc. The second pass analyzes these facts during a pass over the
448 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000449
450 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000451 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000452 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000453 Names which are explicitly declared nonlocal must exist in this set of
454 visible names - if they do not, a syntax error is raised. After doing
455 the local analysis, it analyzes each of its child blocks using an
456 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000457
Nick Coghlan650f0d02007-04-15 12:05:43 +0000458 The children update the free variable set. If a local variable is added to
459 the free variable set by the child, the variable is marked as a cell. The
460 function object being defined must provide runtime storage for the variable
461 that may outlive the function's frame. Cell variables are removed from the
462 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000463
Nick Coghlan650f0d02007-04-15 12:05:43 +0000464 During analysis, the names are:
465 symbols: dict mapping from symbol names to flag values (including offset scope values)
466 scopes: dict mapping from symbol names to scope values (no offset)
467 local: set of all symbol names local to the current scope
468 bound: set of all symbol names local to a containing function scope
469 free: set of all symbol names referenced but not bound in child scopes
470 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471*/
472
473#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000474 PyObject *o = PyLong_FromLong(I); \
475 if (!o) \
476 return 0; \
477 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
478 Py_DECREF(o); \
479 return 0; \
480 } \
481 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000482}
483
484/* Decide on scope of name, given flags.
485
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000486 The namespace dictionaries may be modified to record information
487 about the new name. For example, a new global will add an entry to
488 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000489*/
490
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000492analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 PyObject *bound, PyObject *local, PyObject *free,
494 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000495{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 if (flags & DEF_NONLOCAL) {
498 PyErr_Format(PyExc_SyntaxError,
499 "name '%U' is nonlocal and global",
500 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400501 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 }
503 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
504 if (PySet_Add(global, name) < 0)
505 return 0;
506 if (bound && (PySet_Discard(bound, name) < 0))
507 return 0;
508 return 1;
509 }
510 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (!bound) {
512 PyErr_Format(PyExc_SyntaxError,
513 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400514 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000515 }
516 if (!PySet_Contains(bound, name)) {
517 PyErr_Format(PyExc_SyntaxError,
518 "no binding for nonlocal '%U' found",
519 name);
520
Benjamin Petersond9c87022012-10-31 20:26:20 -0400521 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 }
523 SET_SCOPE(scopes, name, FREE);
524 ste->ste_free = 1;
525 return PySet_Add(free, name) >= 0;
526 }
527 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000528 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000529 if (PySet_Add(local, name) < 0)
530 return 0;
531 if (PySet_Discard(global, name) < 0)
532 return 0;
533 return 1;
534 }
535 /* If an enclosing block has a binding for this name, it
536 is a free variable rather than a global variable.
537 Note that having a non-NULL bound implies that the block
538 is nested.
539 */
540 if (bound && PySet_Contains(bound, name)) {
541 SET_SCOPE(scopes, name, FREE);
542 ste->ste_free = 1;
543 return PySet_Add(free, name) >= 0;
544 }
545 /* If a parent has a global statement, then call it global
546 explicit? It could also be global implicit.
547 */
548 if (global && PySet_Contains(global, name)) {
549 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
550 return 1;
551 }
552 if (ste->ste_nested)
553 ste->ste_free = 1;
554 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
555 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556}
557
558#undef SET_SCOPE
559
560/* If a name is defined in free and also in locals, then this block
561 provides the binding for the free variable. The name should be
562 marked CELL in this block and removed from the free list.
563
564 Note that the current block's free variables are included in free.
565 That's safe because no name can be free and local in the same scope.
566*/
567
568static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500569analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 PyObject *name, *v, *v_cell;
572 int success = 0;
573 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000574
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000575 v_cell = PyLong_FromLong(CELL);
576 if (!v_cell)
577 return 0;
578 while (PyDict_Next(scopes, &pos, &name, &v)) {
579 long scope;
580 assert(PyLong_Check(v));
581 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000582 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000583 continue;
584 if (!PySet_Contains(free, name))
585 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000586 /* Replace LOCAL with CELL for this name, and remove
587 from free. It is safe to replace the value of name
588 in the dict, because it will not cause a resize.
589 */
590 if (PyDict_SetItem(scopes, name, v_cell) < 0)
591 goto error;
592 if (PySet_Discard(free, name) < 0)
593 goto error;
594 }
595 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 Py_DECREF(v_cell);
598 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599}
600
Benjamin Peterson312595c2013-05-15 15:26:42 -0500601static int
602drop_class_free(PySTEntryObject *ste, PyObject *free)
603{
604 int res;
605 if (!GET_IDENTIFIER(__class__))
606 return 0;
607 res = PySet_Discard(free, __class__);
608 if (res < 0)
609 return 0;
610 if (res)
611 ste->ste_needs_class_closure = 1;
612 return 1;
613}
614
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000615/* Enter the final scope information into the ste_symbols dict.
616 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000617 * All arguments are dicts. Modifies symbols, others are read-only.
618*/
619static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000620update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000621 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000623 PyObject *name = NULL, *itr = NULL;
624 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
625 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000626
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000627 /* Update scope information for all symbols in this scope */
628 while (PyDict_Next(symbols, &pos, &name, &v)) {
629 long scope, flags;
630 assert(PyLong_Check(v));
631 flags = PyLong_AS_LONG(v);
632 v_scope = PyDict_GetItem(scopes, name);
633 assert(v_scope && PyLong_Check(v_scope));
634 scope = PyLong_AS_LONG(v_scope);
635 flags |= (scope << SCOPE_OFFSET);
636 v_new = PyLong_FromLong(flags);
637 if (!v_new)
638 return 0;
639 if (PyDict_SetItem(symbols, name, v_new) < 0) {
640 Py_DECREF(v_new);
641 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000642 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 Py_DECREF(v_new);
644 }
645
646 /* Record not yet resolved free variables from children (if any) */
647 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
648 if (!v_free)
649 return 0;
650
651 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600652 if (itr == NULL) {
653 Py_DECREF(v_free);
654 return 0;
655 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656
657 while ((name = PyIter_Next(itr))) {
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200658 v = PyDict_GetItemWithError(symbols, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659
660 /* Handle symbol that already exists in this scope */
661 if (v) {
662 /* Handle a free variable in a method of
663 the class that has the same name as a local
664 or global in the class scope.
665 */
666 if (classflag &&
667 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
668 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
669 v_new = PyLong_FromLong(flags);
670 if (!v_new) {
671 goto error;
672 }
673 if (PyDict_SetItem(symbols, name, v_new) < 0) {
674 Py_DECREF(v_new);
675 goto error;
676 }
677 Py_DECREF(v_new);
678 }
679 /* It's a cell, or already free in this scope */
680 Py_DECREF(name);
681 continue;
682 }
Serhiy Storchakaa24107b2019-02-25 17:59:46 +0200683 else if (PyErr_Occurred()) {
684 goto error;
685 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200687 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 Py_DECREF(name);
689 continue; /* it's a global */
690 }
691 /* Propagate new free symbol up the lexical stack */
692 if (PyDict_SetItem(symbols, name, v_free) < 0) {
693 goto error;
694 }
695 Py_DECREF(name);
696 }
697 Py_DECREF(itr);
698 Py_DECREF(v_free);
699 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000700error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701 Py_XDECREF(v_free);
702 Py_XDECREF(itr);
703 Py_XDECREF(name);
704 return 0;
705}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000706
707/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000708
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000709 Arguments:
710 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000711 bound -- set of variables bound in enclosing scopes (input). bound
712 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713 free -- set of free variables in enclosed scopes (output)
714 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000715
716 The implementation uses two mutually recursive functions,
717 analyze_block() and analyze_child_block(). analyze_block() is
718 responsible for analyzing the individual names defined in a block.
719 analyze_child_block() prepares temporary namespace dictionaries
720 used to evaluated nested blocks.
721
722 The two functions exist because a child block should see the name
723 bindings of its enclosing blocks, but those bindings should not
724 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000725*/
726
727static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000728analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
729 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000730
731static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
733 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000734{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
736 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
737 PyObject *temp;
738 int i, success = 0;
739 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000740
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000741 local = PySet_New(NULL); /* collect new names bound in block */
742 if (!local)
743 goto error;
744 scopes = PyDict_New(); /* collect scopes defined for each name */
745 if (!scopes)
746 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 /* Allocate new global and bound variable dictionaries. These
749 dictionaries hold the names visible in nested blocks. For
750 ClassBlocks, the bound and global names are initialized
751 before analyzing names, because class bindings aren't
752 visible in methods. For other blocks, they are initialized
753 after names are analyzed.
754 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000755
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 /* TODO(jhylton): Package these dicts in a struct so that we
757 can write reasonable helper functions?
758 */
759 newglobal = PySet_New(NULL);
760 if (!newglobal)
761 goto error;
762 newfree = PySet_New(NULL);
763 if (!newfree)
764 goto error;
765 newbound = PySet_New(NULL);
766 if (!newbound)
767 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* Class namespace has no effect on names visible in
770 nested functions, so populate the global and bound
771 sets to be passed to child blocks before analyzing
772 this one.
773 */
774 if (ste->ste_type == ClassBlock) {
775 /* Pass down known globals */
776 temp = PyNumber_InPlaceOr(newglobal, global);
777 if (!temp)
778 goto error;
779 Py_DECREF(temp);
780 /* Pass down previously bound symbols */
781 if (bound) {
782 temp = PyNumber_InPlaceOr(newbound, bound);
783 if (!temp)
784 goto error;
785 Py_DECREF(temp);
786 }
787 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000788
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000789 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
790 long flags = PyLong_AS_LONG(v);
791 if (!analyze_name(ste, scopes, name, flags,
792 bound, local, free, global))
793 goto error;
794 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000795
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000796 /* Populate global and bound sets to be passed to children. */
797 if (ste->ste_type != ClassBlock) {
798 /* Add function locals to bound set */
799 if (ste->ste_type == FunctionBlock) {
800 temp = PyNumber_InPlaceOr(newbound, local);
801 if (!temp)
802 goto error;
803 Py_DECREF(temp);
804 }
805 /* Pass down previously bound symbols */
806 if (bound) {
807 temp = PyNumber_InPlaceOr(newbound, bound);
808 if (!temp)
809 goto error;
810 Py_DECREF(temp);
811 }
812 /* Pass down known globals */
813 temp = PyNumber_InPlaceOr(newglobal, global);
814 if (!temp)
815 goto error;
816 Py_DECREF(temp);
817 }
818 else {
819 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000820 if (!GET_IDENTIFIER(__class__))
821 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000822 if (PySet_Add(newbound, __class__) < 0)
823 goto error;
824 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000825
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300826 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000827
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 newbound, newglobal now contain the names visible in
829 nested blocks. The free variables in the children will
830 be collected in allfree.
831 */
832 allfree = PySet_New(NULL);
833 if (!allfree)
834 goto error;
835 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
836 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
837 PySTEntryObject* entry;
838 assert(c && PySTEntry_Check(c));
839 entry = (PySTEntryObject*)c;
840 if (!analyze_child_block(entry, newbound, newfree, newglobal,
841 allfree))
842 goto error;
843 /* Check if any children have free variables */
844 if (entry->ste_free || entry->ste_child_free)
845 ste->ste_child_free = 1;
846 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 temp = PyNumber_InPlaceOr(newfree, allfree);
849 if (!temp)
850 goto error;
851 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000853 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500854 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500856 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 goto error;
858 /* Records the results of the analysis in the symbol table entry */
859 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
860 ste->ste_type == ClassBlock))
861 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000862
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 temp = PyNumber_InPlaceOr(free, newfree);
864 if (!temp)
865 goto error;
866 Py_DECREF(temp);
867 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000868 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 Py_XDECREF(scopes);
870 Py_XDECREF(local);
871 Py_XDECREF(newbound);
872 Py_XDECREF(newglobal);
873 Py_XDECREF(newfree);
874 Py_XDECREF(allfree);
875 if (!success)
876 assert(PyErr_Occurred());
877 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000878}
879
880static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000881analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
882 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000883{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
885 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000886
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000888
Martin Panter3ee62702016-06-04 04:57:19 +0000889 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000890 current block. The analyze_block() call modifies these
891 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000892
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000893 */
894 temp_bound = PySet_New(bound);
895 if (!temp_bound)
896 goto error;
897 temp_free = PySet_New(free);
898 if (!temp_free)
899 goto error;
900 temp_global = PySet_New(global);
901 if (!temp_global)
902 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000903
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000904 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
905 goto error;
906 temp = PyNumber_InPlaceOr(child_free, temp_free);
907 if (!temp)
908 goto error;
909 Py_DECREF(temp);
910 Py_DECREF(temp_bound);
911 Py_DECREF(temp_free);
912 Py_DECREF(temp_global);
913 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000914 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 Py_XDECREF(temp_bound);
916 Py_XDECREF(temp_free);
917 Py_XDECREF(temp_global);
918 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000919}
920
921static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000922symtable_analyze(struct symtable *st)
923{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000924 PyObject *free, *global;
925 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000926
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000927 free = PySet_New(NULL);
928 if (!free)
929 return 0;
930 global = PySet_New(NULL);
931 if (!global) {
932 Py_DECREF(free);
933 return 0;
934 }
935 r = analyze_block(st->st_top, NULL, free, global);
936 Py_DECREF(free);
937 Py_DECREF(global);
938 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000941/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000942 This reference is released when the block is exited, via the DECREF
943 in symtable_exit_block().
944*/
945
946static int
947symtable_exit_block(struct symtable *st, void *ast)
948{
Benjamin Peterson609da582011-06-29 22:52:39 -0500949 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000950
Benjamin Peterson609da582011-06-29 22:52:39 -0500951 st->st_cur = NULL;
952 size = PyList_GET_SIZE(st->st_stack);
953 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500954 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000955 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500956 if (--size)
957 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000958 }
959 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960}
961
962static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000964 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000965{
Benjamin Peterson609da582011-06-29 22:52:39 -0500966 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000967
Benjamin Peterson609da582011-06-29 22:52:39 -0500968 ste = ste_new(st, name, block, ast, lineno, col_offset);
969 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500971 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
972 Py_DECREF(ste);
973 return 0;
974 }
975 prev = st->st_cur;
Nick Coghlan6ca03072019-08-26 00:41:47 +1000976 /* bpo-37757: For now, disallow *all* assignment expressions in the
977 * outermost iterator expression of a comprehension, even those inside
978 * a nested comprehension or a lambda expression.
979 */
980 if (prev) {
981 ste->ste_comp_iter_expr = prev->ste_comp_iter_expr;
982 }
Benjamin Peterson609da582011-06-29 22:52:39 -0500983 /* The entry is owned by the stack. Borrow it for st_cur. */
984 Py_DECREF(ste);
985 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000986 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000987 st->st_global = st->st_cur->ste_symbols;
988 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500989 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000990 return 0;
991 }
992 }
993 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000994}
995
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000996static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000997symtable_lookup(struct symtable *st, PyObject *name)
998{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000999 PyObject *mangled = _Py_Mangle(st->st_private, name);
1000 if (!mangled)
1001 return 0;
Miss Islington (bot)ed8af332019-08-26 08:49:44 -07001002 long ret = _PyST_GetSymbol(st->st_cur, mangled);
1003 Py_DECREF(mangled);
1004 return ret;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001005}
1006
1007static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001008symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001009{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001010 PyObject *o;
1011 PyObject *dict;
1012 long val;
1013 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001014
Jeremy Hylton81e95022007-02-27 06:50:52 +00001015
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001016 if (!mangled)
1017 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001018 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001019 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 val = PyLong_AS_LONG(o);
1021 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1022 /* Is it better to use 'mangled' or 'name' here? */
1023 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001024 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001025 ste->ste_lineno,
1026 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 goto error;
1028 }
1029 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001030 }
1031 else if (PyErr_Occurred()) {
1032 goto error;
1033 }
1034 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001036 }
Nick Coghlan6ca03072019-08-26 00:41:47 +10001037 if (ste->ste_comp_iter_target) {
1038 /* This name is an iteration variable in a comprehension,
1039 * so check for a binding conflict with any named expressions.
1040 * Otherwise, mark it as an iteration variable so subsequent
1041 * named expressions can check for conflicts.
1042 */
1043 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1044 PyErr_Format(PyExc_SyntaxError,
1045 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1046 PyErr_SyntaxLocationObject(st->st_filename,
1047 ste->ste_lineno,
1048 ste->ste_col_offset + 1);
1049 goto error;
1050 }
1051 val |= DEF_COMP_ITER;
1052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 o = PyLong_FromLong(val);
1054 if (o == NULL)
1055 goto error;
1056 if (PyDict_SetItem(dict, mangled, o) < 0) {
1057 Py_DECREF(o);
1058 goto error;
1059 }
1060 Py_DECREF(o);
1061
1062 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001063 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 goto error;
1065 } else if (flag & DEF_GLOBAL) {
1066 /* XXX need to update DEF_GLOBAL for other flags too;
1067 perhaps only DEF_FREE_GLOBAL */
1068 val = flag;
1069 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1070 val |= PyLong_AS_LONG(o);
1071 }
1072 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; \
1114 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1115 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; \
1124 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1125 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; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001134 asdl_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);
Yury Selivanov75445082015-05-11 22:57:16 -04001181 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1182 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);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 if (!symtable_exit_block(st, s))
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;
1210 if (!symtable_exit_block(st, s))
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;
1289 case Raise_kind:
1290 if (s->v.Raise.exc) {
1291 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001292 if (s->v.Raise.cause) {
1293 VISIT(st, expr, s->v.Raise.cause);
1294 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 }
1296 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001297 case Try_kind:
1298 VISIT_SEQ(st, stmt, s->v.Try.body);
1299 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1300 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1301 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001302 break;
1303 case Assert_kind:
1304 VISIT(st, expr, s->v.Assert.test);
1305 if (s->v.Assert.msg)
1306 VISIT(st, expr, s->v.Assert.msg);
1307 break;
1308 case Import_kind:
1309 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001310 break;
1311 case ImportFrom_kind:
1312 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 break;
1314 case Global_kind: {
1315 int i;
1316 asdl_seq *seq = s->v.Global.names;
1317 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1318 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 long cur = symtable_lookup(st, name);
1320 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001321 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001322 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1323 const char* msg;
1324 if (cur & DEF_PARAM) {
1325 msg = GLOBAL_PARAM;
1326 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001327 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001328 } else if (cur & DEF_ANNOT) {
1329 msg = GLOBAL_ANNOT;
1330 } else { /* DEF_LOCAL */
1331 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001332 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001333 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001334 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001335 PyErr_SyntaxLocationObject(st->st_filename,
1336 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001337 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001338 VISIT_QUIT(st, 0);
1339 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001340 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001341 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001342 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001343 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001344 }
1345 break;
1346 }
1347 case Nonlocal_kind: {
1348 int i;
1349 asdl_seq *seq = s->v.Nonlocal.names;
1350 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1351 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001352 long cur = symtable_lookup(st, name);
1353 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001354 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001355 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1356 const char* msg;
1357 if (cur & DEF_PARAM) {
1358 msg = NONLOCAL_PARAM;
1359 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001360 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001361 } else if (cur & DEF_ANNOT) {
1362 msg = NONLOCAL_ANNOT;
1363 } else { /* DEF_LOCAL */
1364 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001365 }
1366 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001367 PyErr_SyntaxLocationObject(st->st_filename,
1368 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001369 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001370 VISIT_QUIT(st, 0);
1371 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001373 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001374 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001375 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001376 }
1377 break;
1378 }
1379 case Expr_kind:
1380 VISIT(st, expr, s->v.Expr.value);
1381 break;
1382 case Pass_kind:
1383 case Break_kind:
1384 case Continue_kind:
1385 /* nothing to do here */
1386 break;
1387 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001388 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001389 VISIT_SEQ(st, stmt, s->v.With.body);
1390 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001391 case AsyncFunctionDef_kind:
1392 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1393 VISIT_QUIT(st, 0);
1394 if (s->v.AsyncFunctionDef.args->defaults)
1395 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1396 if (s->v.AsyncFunctionDef.args->kw_defaults)
1397 VISIT_SEQ_WITH_NULL(st, expr,
1398 s->v.AsyncFunctionDef.args->kw_defaults);
1399 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1400 s->v.AsyncFunctionDef.returns))
1401 VISIT_QUIT(st, 0);
1402 if (s->v.AsyncFunctionDef.decorator_list)
1403 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1404 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1405 FunctionBlock, (void *)s, s->lineno,
1406 s->col_offset))
1407 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001408 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001409 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1410 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1411 if (!symtable_exit_block(st, s))
1412 VISIT_QUIT(st, 0);
1413 break;
1414 case AsyncWith_kind:
1415 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1416 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1417 break;
1418 case AsyncFor_kind:
1419 VISIT(st, expr, s->v.AsyncFor.target);
1420 VISIT(st, expr, s->v.AsyncFor.iter);
1421 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1422 if (s->v.AsyncFor.orelse)
1423 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1424 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001426 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001427}
1428
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001430symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1431{
1432 assert(st->st_stack);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001433 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001434
Nick Coghlan6ca03072019-08-26 00:41:47 +10001435 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001436 Py_ssize_t i, size;
1437 struct _symtable_entry *ste;
1438 size = PyList_GET_SIZE(st->st_stack);
1439 assert(size);
1440
1441 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1442 for (i = size - 1; i >= 0; i--) {
1443 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1444
Nick Coghlan6ca03072019-08-26 00:41:47 +10001445 /* If we find a comprehension scope, check for a target
1446 * binding conflict with iteration variables, otherwise skip it
1447 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001448 if (ste->ste_comprehension) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001449 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1450 if (target_in_scope & DEF_COMP_ITER) {
1451 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1452 PyErr_SyntaxLocationObject(st->st_filename,
1453 e->lineno,
1454 e->col_offset);
1455 VISIT_QUIT(st, 0);
1456 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001457 continue;
1458 }
1459
1460 /* If we find a FunctionBlock entry, add as NONLOCAL/LOCAL */
1461 if (ste->ste_type == FunctionBlock) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001462 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001463 VISIT_QUIT(st, 0);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001464 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001465 VISIT_QUIT(st, 0);
1466
Nick Coghlan6ca03072019-08-26 00:41:47 +10001467 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001468 }
1469 /* If we find a ModuleBlock entry, add as GLOBAL */
1470 if (ste->ste_type == ModuleBlock) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001471 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001472 VISIT_QUIT(st, 0);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001473 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001474 VISIT_QUIT(st, 0);
1475
Nick Coghlan6ca03072019-08-26 00:41:47 +10001476 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001477 }
1478 /* Disallow usage in ClassBlock */
1479 if (ste->ste_type == ClassBlock) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001480 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001481 PyErr_SyntaxLocationObject(st->st_filename,
1482 e->lineno,
1483 e->col_offset);
1484 VISIT_QUIT(st, 0);
1485 }
1486 }
1487
1488 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1489 and should never fall to this case
1490 */
1491 assert(0);
1492 return 0;
1493}
1494
1495static int
Nick Coghlan6ca03072019-08-26 00:41:47 +10001496symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1497{
1498 if (st->st_cur->ste_comp_iter_expr > 0) {
1499 /* Assignment isn't allowed in a comprehension iterable expression */
1500 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1501 PyErr_SyntaxLocationObject(st->st_filename,
1502 e->lineno,
1503 e->col_offset);
1504 VISIT_QUIT(st, 0);
1505 }
1506 if (st->st_cur->ste_comprehension) {
1507 /* Inside a comprehension body, so find the right target scope */
1508 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
1509 VISIT_QUIT(st, 0);
1510 }
1511 VISIT(st, expr, e->v.NamedExpr.value);
1512 VISIT(st, expr, e->v.NamedExpr.target);
Pablo Galindo37694252019-08-26 16:27:31 +01001513 VISIT_QUIT(st, 1);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001514}
1515
1516static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001517symtable_visit_expr(struct symtable *st, expr_ty e)
1518{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001519 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001520 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001521 "maximum recursion depth exceeded during compilation");
1522 VISIT_QUIT(st, 0);
1523 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001525 case NamedExpr_kind:
Pablo Galindo37694252019-08-26 16:27:31 +01001526 if(!symtable_handle_namedexpr(st, e))
1527 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001528 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001529 case BoolOp_kind:
1530 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1531 break;
1532 case BinOp_kind:
1533 VISIT(st, expr, e->v.BinOp.left);
1534 VISIT(st, expr, e->v.BinOp.right);
1535 break;
1536 case UnaryOp_kind:
1537 VISIT(st, expr, e->v.UnaryOp.operand);
1538 break;
1539 case Lambda_kind: {
1540 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001541 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (e->v.Lambda.args->defaults)
1543 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001544 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001545 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001546 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001547 FunctionBlock, (void *)e, e->lineno,
1548 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001549 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001550 VISIT(st, arguments, e->v.Lambda.args);
1551 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001552 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001553 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 break;
1555 }
1556 case IfExp_kind:
1557 VISIT(st, expr, e->v.IfExp.test);
1558 VISIT(st, expr, e->v.IfExp.body);
1559 VISIT(st, expr, e->v.IfExp.orelse);
1560 break;
1561 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001562 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001563 VISIT_SEQ(st, expr, e->v.Dict.values);
1564 break;
1565 case Set_kind:
1566 VISIT_SEQ(st, expr, e->v.Set.elts);
1567 break;
1568 case GeneratorExp_kind:
1569 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001570 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571 break;
1572 case ListComp_kind:
1573 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001574 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001575 break;
1576 case SetComp_kind:
1577 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001578 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 break;
1580 case DictComp_kind:
1581 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001582 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 break;
1584 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001585 if (e->v.Yield.value)
1586 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001587 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001589 case YieldFrom_kind:
1590 VISIT(st, expr, e->v.YieldFrom.value);
1591 st->st_cur->ste_generator = 1;
1592 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001593 case Await_kind:
1594 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001595 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001596 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 case Compare_kind:
1598 VISIT(st, expr, e->v.Compare.left);
1599 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1600 break;
1601 case Call_kind:
1602 VISIT(st, expr, e->v.Call.func);
1603 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001604 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001605 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001606 case FormattedValue_kind:
1607 VISIT(st, expr, e->v.FormattedValue.value);
1608 if (e->v.FormattedValue.format_spec)
1609 VISIT(st, expr, e->v.FormattedValue.format_spec);
1610 break;
1611 case JoinedStr_kind:
1612 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1613 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001614 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001615 /* Nothing to do here. */
1616 break;
1617 /* The following exprs can be assignment targets. */
1618 case Attribute_kind:
1619 VISIT(st, expr, e->v.Attribute.value);
1620 break;
1621 case Subscript_kind:
1622 VISIT(st, expr, e->v.Subscript.value);
1623 VISIT(st, slice, e->v.Subscript.slice);
1624 break;
1625 case Starred_kind:
1626 VISIT(st, expr, e->v.Starred.value);
1627 break;
1628 case Name_kind:
1629 if (!symtable_add_def(st, e->v.Name.id,
1630 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001631 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001632 /* Special-case super: it counts as a use of __class__ */
1633 if (e->v.Name.ctx == Load &&
1634 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001635 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001636 if (!GET_IDENTIFIER(__class__) ||
1637 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001638 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 }
1640 break;
1641 /* child nodes of List and Tuple will have expr_context set */
1642 case List_kind:
1643 VISIT_SEQ(st, expr, e->v.List.elts);
1644 break;
1645 case Tuple_kind:
1646 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1647 break;
1648 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001649 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001650}
1651
1652static int
1653symtable_implicit_arg(struct symtable *st, int pos)
1654{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1656 if (id == NULL)
1657 return 0;
1658 if (!symtable_add_def(st, id, DEF_PARAM)) {
1659 Py_DECREF(id);
1660 return 0;
1661 }
1662 Py_DECREF(id);
1663 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664}
1665
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001666static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001667symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671 if (!args)
1672 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001673
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 for (i = 0; i < asdl_seq_LEN(args); i++) {
1675 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1676 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1677 return 0;
1678 }
1679
1680 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681}
1682
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001683static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001684symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001685{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001686 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001687
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001688 if (!args)
1689 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001690
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001691 for (i = 0; i < asdl_seq_LEN(args); i++) {
1692 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1693 if (arg->annotation)
1694 VISIT(st, expr, arg->annotation);
1695 }
1696
1697 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001698}
1699
Neal Norwitzc1505362006-12-28 06:47:50 +00001700static int
Yury Selivanov75445082015-05-11 22:57:16 -04001701symtable_visit_annotations(struct symtable *st, stmt_ty s,
1702 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 if (a->args && !symtable_visit_argannotations(st, a->args))
1705 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001706 if (a->vararg && a->vararg->annotation)
1707 VISIT(st, expr, a->vararg->annotation);
1708 if (a->kwarg && a->kwarg->annotation)
1709 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1711 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001712 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001713 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001714 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001715}
1716
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001718symtable_visit_arguments(struct symtable *st, arguments_ty a)
1719{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 /* skip default arguments inside function block
1721 XXX should ast be different?
1722 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001723 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1724 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001725 if (a->args && !symtable_visit_params(st, a->args))
1726 return 0;
1727 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1728 return 0;
1729 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001730 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001731 return 0;
1732 st->st_cur->ste_varargs = 1;
1733 }
1734 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001735 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001736 return 0;
1737 st->st_cur->ste_varkeywords = 1;
1738 }
1739 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740}
1741
1742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001744symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 if (eh->v.ExceptHandler.type)
1747 VISIT(st, expr, eh->v.ExceptHandler.type);
1748 if (eh->v.ExceptHandler.name)
1749 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1750 return 0;
1751 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1752 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001753}
1754
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001755static int
1756symtable_visit_withitem(struct symtable *st, withitem_ty item)
1757{
1758 VISIT(st, expr, item->context_expr);
1759 if (item->optional_vars) {
1760 VISIT(st, expr, item->optional_vars);
1761 }
1762 return 1;
1763}
1764
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001767symtable_visit_alias(struct symtable *st, alias_ty a)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001770 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001771 dotted package name (e.g. spam.eggs)
1772 */
1773 PyObject *store_name;
1774 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001775 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1776 PyUnicode_GET_LENGTH(name), 1);
1777 if (dot != -1) {
1778 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 if (!store_name)
1780 return 0;
1781 }
1782 else {
1783 store_name = name;
1784 Py_INCREF(store_name);
1785 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001786 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1788 Py_DECREF(store_name);
1789 return r;
1790 }
1791 else {
1792 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001793 int lineno = st->st_cur->ste_lineno;
1794 int col_offset = st->st_cur->ste_col_offset;
1795 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001796 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001797 Py_DECREF(store_name);
1798 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001799 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001800 Py_DECREF(store_name);
1801 return 1;
1802 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803}
1804
1805
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001807symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1808{
Nick Coghlan6ca03072019-08-26 00:41:47 +10001809 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001810 VISIT(st, expr, lc->target);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001811 st->st_cur->ste_comp_iter_target = 0;
1812 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001813 VISIT(st, expr, lc->iter);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001814 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001816 if (lc->is_async) {
1817 st->st_cur->ste_coroutine = 1;
1818 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820}
1821
1822
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001823static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824symtable_visit_keyword(struct symtable *st, keyword_ty k)
1825{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001826 VISIT(st, expr, k->value);
1827 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828}
1829
1830
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001831static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001832symtable_visit_slice(struct symtable *st, slice_ty s)
1833{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001834 switch (s->kind) {
1835 case Slice_kind:
1836 if (s->v.Slice.lower)
1837 VISIT(st, expr, s->v.Slice.lower)
1838 if (s->v.Slice.upper)
1839 VISIT(st, expr, s->v.Slice.upper)
1840 if (s->v.Slice.step)
1841 VISIT(st, expr, s->v.Slice.step)
1842 break;
1843 case ExtSlice_kind:
1844 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1845 break;
1846 case Index_kind:
1847 VISIT(st, expr, s->v.Index.value)
1848 break;
1849 }
1850 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001851}
1852
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001853static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001854symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001855 identifier scope_name, asdl_seq *generators,
1856 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001857{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001858 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 comprehension_ty outermost = ((comprehension_ty)
1860 asdl_seq_GET(generators, 0));
1861 /* Outermost iterator is evaluated in current scope */
Nick Coghlan6ca03072019-08-26 00:41:47 +10001862 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001863 VISIT(st, expr, outermost->iter);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001864 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 /* Create comprehension scope for the rest */
1866 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001867 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1868 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001869 return 0;
1870 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001871 if (outermost->is_async) {
1872 st->st_cur->ste_coroutine = 1;
1873 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001874 st->st_cur->ste_comprehension = 1;
1875
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001876 /* Outermost iter is received as an argument */
1877 if (!symtable_implicit_arg(st, 0)) {
1878 symtable_exit_block(st, (void *)e);
1879 return 0;
1880 }
Nick Coghlan6ca03072019-08-26 00:41:47 +10001881 /* Visit iteration variable target, and mark them as such */
1882 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001883 VISIT(st, expr, outermost->target);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001884 st->st_cur->ste_comp_iter_target = 0;
1885 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001886 VISIT_SEQ(st, expr, outermost->ifs);
1887 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001888 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001889 VISIT(st, expr, value);
1890 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001891 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001892 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001893 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1894 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1895 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1896 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001897 PyErr_SyntaxLocationObject(st->st_filename,
1898 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001899 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001900 symtable_exit_block(st, (void *)e);
1901 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001902 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001903 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001904 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001905}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001906
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001907static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001908symtable_visit_genexp(struct symtable *st, expr_ty e)
1909{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001910 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1911 e->v.GeneratorExp.generators,
1912 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001913}
1914
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001915static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001916symtable_visit_listcomp(struct symtable *st, expr_ty e)
1917{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001918 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1919 e->v.ListComp.generators,
1920 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001921}
1922
1923static int
1924symtable_visit_setcomp(struct symtable *st, expr_ty e)
1925{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001926 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1927 e->v.SetComp.generators,
1928 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001929}
1930
1931static int
1932symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1933{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001934 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1935 e->v.DictComp.generators,
1936 e->v.DictComp.key,
1937 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001938}