blob: e48baa808d41a60be09f1dd03c3bee063457377b [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;
Nick Coghlan6ca03072019-08-26 00:41:47 +10001002 return _PyST_GetSymbol(st->st_cur, mangled);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001003}
1004
1005static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001006symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001007{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001008 PyObject *o;
1009 PyObject *dict;
1010 long val;
1011 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001012
Jeremy Hylton81e95022007-02-27 06:50:52 +00001013
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001014 if (!mangled)
1015 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001016 dict = ste->ste_symbols;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001017 if ((o = PyDict_GetItemWithError(dict, mangled))) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 val = PyLong_AS_LONG(o);
1019 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
1020 /* Is it better to use 'mangled' or 'name' here? */
1021 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +02001022 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001023 ste->ste_lineno,
1024 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001025 goto error;
1026 }
1027 val |= flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001028 }
1029 else if (PyErr_Occurred()) {
1030 goto error;
1031 }
1032 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 val = flag;
Serhiy Storchakaa24107b2019-02-25 17:59:46 +02001034 }
Nick Coghlan6ca03072019-08-26 00:41:47 +10001035 if (ste->ste_comp_iter_target) {
1036 /* This name is an iteration variable in a comprehension,
1037 * so check for a binding conflict with any named expressions.
1038 * Otherwise, mark it as an iteration variable so subsequent
1039 * named expressions can check for conflicts.
1040 */
1041 if (val & (DEF_GLOBAL | DEF_NONLOCAL)) {
1042 PyErr_Format(PyExc_SyntaxError,
1043 NAMED_EXPR_COMP_INNER_LOOP_CONFLICT, name);
1044 PyErr_SyntaxLocationObject(st->st_filename,
1045 ste->ste_lineno,
1046 ste->ste_col_offset + 1);
1047 goto error;
1048 }
1049 val |= DEF_COMP_ITER;
1050 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 o = PyLong_FromLong(val);
1052 if (o == NULL)
1053 goto error;
1054 if (PyDict_SetItem(dict, mangled, o) < 0) {
1055 Py_DECREF(o);
1056 goto error;
1057 }
1058 Py_DECREF(o);
1059
1060 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001061 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 goto error;
1063 } else if (flag & DEF_GLOBAL) {
1064 /* XXX need to update DEF_GLOBAL for other flags too;
1065 perhaps only DEF_FREE_GLOBAL */
1066 val = flag;
1067 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1068 val |= PyLong_AS_LONG(o);
1069 }
1070 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 goto error;
1073 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1074 Py_DECREF(o);
1075 goto error;
1076 }
1077 Py_DECREF(o);
1078 }
1079 Py_DECREF(mangled);
1080 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001081
1082error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 Py_DECREF(mangled);
1084 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001085}
1086
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001087static int
1088symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1089 return symtable_add_def_helper(st, name, flag, st->st_cur);
1090}
1091
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001092/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1093 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 function.
1095
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001096 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1097 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001098
1099 VISIT_QUIT macro returns the specified value exiting from the function but
1100 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001101*/
1102
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001103#define VISIT_QUIT(ST, X) \
1104 return --(ST)->recursion_depth,(X)
1105
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001106#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001107 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001108 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001109
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001110#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001111 int i; \
1112 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1113 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1114 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1115 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001116 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001117 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001118}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001119
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001121 int i; \
1122 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1123 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1124 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1125 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001126 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001128}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001129
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001130#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001132 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001134 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001136 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001137 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001139}
1140
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001141static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001142symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001143{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001144 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001145 int res;
1146 if (!st->st_cur->ste_directives) {
1147 st->st_cur->ste_directives = PyList_New(0);
1148 if (!st->st_cur->ste_directives)
1149 return 0;
1150 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001151 mangled = _Py_Mangle(st->st_private, name);
1152 if (!mangled)
1153 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001154 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001155 if (!data)
1156 return 0;
1157 res = PyList_Append(st->st_cur->ste_directives, data);
1158 Py_DECREF(data);
1159 return res == 0;
1160}
1161
1162
1163static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001164symtable_visit_stmt(struct symtable *st, stmt_ty s)
1165{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001166 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001167 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001168 "maximum recursion depth exceeded during compilation");
1169 VISIT_QUIT(st, 0);
1170 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 switch (s->kind) {
1172 case FunctionDef_kind:
1173 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001174 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001175 if (s->v.FunctionDef.args->defaults)
1176 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1177 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001178 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001179 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1180 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001181 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001182 if (s->v.FunctionDef.decorator_list)
1183 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1184 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001185 FunctionBlock, (void *)s, s->lineno,
1186 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001187 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001188 VISIT(st, arguments, s->v.FunctionDef.args);
1189 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001190 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001191 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001192 break;
1193 case ClassDef_kind: {
1194 PyObject *tmp;
1195 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001196 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001197 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1198 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001199 if (s->v.ClassDef.decorator_list)
1200 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1201 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001202 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001203 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001204 tmp = st->st_private;
1205 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001206 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 st->st_private = tmp;
1208 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001209 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001210 break;
1211 }
1212 case Return_kind:
1213 if (s->v.Return.value) {
1214 VISIT(st, expr, s->v.Return.value);
1215 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001216 }
1217 break;
1218 case Delete_kind:
1219 VISIT_SEQ(st, expr, s->v.Delete.targets);
1220 break;
1221 case Assign_kind:
1222 VISIT_SEQ(st, expr, s->v.Assign.targets);
1223 VISIT(st, expr, s->v.Assign.value);
1224 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001225 case AnnAssign_kind:
1226 if (s->v.AnnAssign.target->kind == Name_kind) {
1227 expr_ty e_name = s->v.AnnAssign.target;
1228 long cur = symtable_lookup(st, e_name->v.Name.id);
1229 if (cur < 0) {
1230 VISIT_QUIT(st, 0);
1231 }
1232 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001233 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001234 && s->v.AnnAssign.simple) {
1235 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001236 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1237 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001238 PyErr_SyntaxLocationObject(st->st_filename,
1239 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001240 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001241 VISIT_QUIT(st, 0);
1242 }
1243 if (s->v.AnnAssign.simple &&
1244 !symtable_add_def(st, e_name->v.Name.id,
1245 DEF_ANNOT | DEF_LOCAL)) {
1246 VISIT_QUIT(st, 0);
1247 }
1248 else {
1249 if (s->v.AnnAssign.value
1250 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1251 VISIT_QUIT(st, 0);
1252 }
1253 }
1254 }
1255 else {
1256 VISIT(st, expr, s->v.AnnAssign.target);
1257 }
1258 VISIT(st, expr, s->v.AnnAssign.annotation);
1259 if (s->v.AnnAssign.value) {
1260 VISIT(st, expr, s->v.AnnAssign.value);
1261 }
1262 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001263 case AugAssign_kind:
1264 VISIT(st, expr, s->v.AugAssign.target);
1265 VISIT(st, expr, s->v.AugAssign.value);
1266 break;
1267 case For_kind:
1268 VISIT(st, expr, s->v.For.target);
1269 VISIT(st, expr, s->v.For.iter);
1270 VISIT_SEQ(st, stmt, s->v.For.body);
1271 if (s->v.For.orelse)
1272 VISIT_SEQ(st, stmt, s->v.For.orelse);
1273 break;
1274 case While_kind:
1275 VISIT(st, expr, s->v.While.test);
1276 VISIT_SEQ(st, stmt, s->v.While.body);
1277 if (s->v.While.orelse)
1278 VISIT_SEQ(st, stmt, s->v.While.orelse);
1279 break;
1280 case If_kind:
1281 /* XXX if 0: and lookup_yield() hacks */
1282 VISIT(st, expr, s->v.If.test);
1283 VISIT_SEQ(st, stmt, s->v.If.body);
1284 if (s->v.If.orelse)
1285 VISIT_SEQ(st, stmt, s->v.If.orelse);
1286 break;
1287 case Raise_kind:
1288 if (s->v.Raise.exc) {
1289 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001290 if (s->v.Raise.cause) {
1291 VISIT(st, expr, s->v.Raise.cause);
1292 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 }
1294 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001295 case Try_kind:
1296 VISIT_SEQ(st, stmt, s->v.Try.body);
1297 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1298 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1299 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 break;
1301 case Assert_kind:
1302 VISIT(st, expr, s->v.Assert.test);
1303 if (s->v.Assert.msg)
1304 VISIT(st, expr, s->v.Assert.msg);
1305 break;
1306 case Import_kind:
1307 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001308 break;
1309 case ImportFrom_kind:
1310 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001311 break;
1312 case Global_kind: {
1313 int i;
1314 asdl_seq *seq = s->v.Global.names;
1315 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1316 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 long cur = symtable_lookup(st, name);
1318 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001319 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001320 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1321 const char* msg;
1322 if (cur & DEF_PARAM) {
1323 msg = GLOBAL_PARAM;
1324 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001325 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001326 } else if (cur & DEF_ANNOT) {
1327 msg = GLOBAL_ANNOT;
1328 } else { /* DEF_LOCAL */
1329 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001330 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001331 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001332 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001333 PyErr_SyntaxLocationObject(st->st_filename,
1334 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001335 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001336 VISIT_QUIT(st, 0);
1337 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001338 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001339 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001340 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001341 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001342 }
1343 break;
1344 }
1345 case Nonlocal_kind: {
1346 int i;
1347 asdl_seq *seq = s->v.Nonlocal.names;
1348 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1349 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001350 long cur = symtable_lookup(st, name);
1351 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001352 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001353 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1354 const char* msg;
1355 if (cur & DEF_PARAM) {
1356 msg = NONLOCAL_PARAM;
1357 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001358 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001359 } else if (cur & DEF_ANNOT) {
1360 msg = NONLOCAL_ANNOT;
1361 } else { /* DEF_LOCAL */
1362 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001363 }
1364 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001365 PyErr_SyntaxLocationObject(st->st_filename,
1366 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001367 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001368 VISIT_QUIT(st, 0);
1369 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001371 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001372 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001373 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001374 }
1375 break;
1376 }
1377 case Expr_kind:
1378 VISIT(st, expr, s->v.Expr.value);
1379 break;
1380 case Pass_kind:
1381 case Break_kind:
1382 case Continue_kind:
1383 /* nothing to do here */
1384 break;
1385 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001386 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001387 VISIT_SEQ(st, stmt, s->v.With.body);
1388 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001389 case AsyncFunctionDef_kind:
1390 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1391 VISIT_QUIT(st, 0);
1392 if (s->v.AsyncFunctionDef.args->defaults)
1393 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1394 if (s->v.AsyncFunctionDef.args->kw_defaults)
1395 VISIT_SEQ_WITH_NULL(st, expr,
1396 s->v.AsyncFunctionDef.args->kw_defaults);
1397 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1398 s->v.AsyncFunctionDef.returns))
1399 VISIT_QUIT(st, 0);
1400 if (s->v.AsyncFunctionDef.decorator_list)
1401 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1402 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1403 FunctionBlock, (void *)s, s->lineno,
1404 s->col_offset))
1405 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001406 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001407 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1408 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1409 if (!symtable_exit_block(st, s))
1410 VISIT_QUIT(st, 0);
1411 break;
1412 case AsyncWith_kind:
1413 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1414 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1415 break;
1416 case AsyncFor_kind:
1417 VISIT(st, expr, s->v.AsyncFor.target);
1418 VISIT(st, expr, s->v.AsyncFor.iter);
1419 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1420 if (s->v.AsyncFor.orelse)
1421 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1422 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001424 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001425}
1426
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001428symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1429{
1430 assert(st->st_stack);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001431 assert(e->kind == Name_kind);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001432
Nick Coghlan6ca03072019-08-26 00:41:47 +10001433 PyObject *target_name = e->v.Name.id;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001434 Py_ssize_t i, size;
1435 struct _symtable_entry *ste;
1436 size = PyList_GET_SIZE(st->st_stack);
1437 assert(size);
1438
1439 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1440 for (i = size - 1; i >= 0; i--) {
1441 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1442
Nick Coghlan6ca03072019-08-26 00:41:47 +10001443 /* If we find a comprehension scope, check for a target
1444 * binding conflict with iteration variables, otherwise skip it
1445 */
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001446 if (ste->ste_comprehension) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001447 long target_in_scope = _PyST_GetSymbol(ste, target_name);
1448 if (target_in_scope & DEF_COMP_ITER) {
1449 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_CONFLICT, target_name);
1450 PyErr_SyntaxLocationObject(st->st_filename,
1451 e->lineno,
1452 e->col_offset);
1453 VISIT_QUIT(st, 0);
1454 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001455 continue;
1456 }
1457
1458 /* If we find a FunctionBlock entry, add as NONLOCAL/LOCAL */
1459 if (ste->ste_type == FunctionBlock) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001460 if (!symtable_add_def(st, target_name, DEF_NONLOCAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001461 VISIT_QUIT(st, 0);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001462 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001463 VISIT_QUIT(st, 0);
1464
Nick Coghlan6ca03072019-08-26 00:41:47 +10001465 return symtable_add_def_helper(st, target_name, DEF_LOCAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001466 }
1467 /* If we find a ModuleBlock entry, add as GLOBAL */
1468 if (ste->ste_type == ModuleBlock) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001469 if (!symtable_add_def(st, target_name, DEF_GLOBAL))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001470 VISIT_QUIT(st, 0);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001471 if (!symtable_record_directive(st, target_name, e->lineno, e->col_offset))
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001472 VISIT_QUIT(st, 0);
1473
Nick Coghlan6ca03072019-08-26 00:41:47 +10001474 return symtable_add_def_helper(st, target_name, DEF_GLOBAL, ste);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001475 }
1476 /* Disallow usage in ClassBlock */
1477 if (ste->ste_type == ClassBlock) {
Nick Coghlan6ca03072019-08-26 00:41:47 +10001478 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_IN_CLASS);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001479 PyErr_SyntaxLocationObject(st->st_filename,
1480 e->lineno,
1481 e->col_offset);
1482 VISIT_QUIT(st, 0);
1483 }
1484 }
1485
1486 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1487 and should never fall to this case
1488 */
1489 assert(0);
1490 return 0;
1491}
1492
1493static int
Nick Coghlan6ca03072019-08-26 00:41:47 +10001494symtable_handle_namedexpr(struct symtable *st, expr_ty e)
1495{
1496 if (st->st_cur->ste_comp_iter_expr > 0) {
1497 /* Assignment isn't allowed in a comprehension iterable expression */
1498 PyErr_Format(PyExc_SyntaxError, NAMED_EXPR_COMP_ITER_EXPR);
1499 PyErr_SyntaxLocationObject(st->st_filename,
1500 e->lineno,
1501 e->col_offset);
1502 VISIT_QUIT(st, 0);
1503 }
1504 if (st->st_cur->ste_comprehension) {
1505 /* Inside a comprehension body, so find the right target scope */
1506 if (!symtable_extend_namedexpr_scope(st, e->v.NamedExpr.target))
1507 VISIT_QUIT(st, 0);
1508 }
1509 VISIT(st, expr, e->v.NamedExpr.value);
1510 VISIT(st, expr, e->v.NamedExpr.target);
1511}
1512
1513static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514symtable_visit_expr(struct symtable *st, expr_ty e)
1515{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001516 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001517 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001518 "maximum recursion depth exceeded during compilation");
1519 VISIT_QUIT(st, 0);
1520 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001522 case NamedExpr_kind:
Nick Coghlan6ca03072019-08-26 00:41:47 +10001523 symtable_handle_namedexpr(st, e);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001524 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 case BoolOp_kind:
1526 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1527 break;
1528 case BinOp_kind:
1529 VISIT(st, expr, e->v.BinOp.left);
1530 VISIT(st, expr, e->v.BinOp.right);
1531 break;
1532 case UnaryOp_kind:
1533 VISIT(st, expr, e->v.UnaryOp.operand);
1534 break;
1535 case Lambda_kind: {
1536 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001537 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (e->v.Lambda.args->defaults)
1539 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001540 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001541 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001543 FunctionBlock, (void *)e, e->lineno,
1544 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001545 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001546 VISIT(st, arguments, e->v.Lambda.args);
1547 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001548 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001549 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001550 break;
1551 }
1552 case IfExp_kind:
1553 VISIT(st, expr, e->v.IfExp.test);
1554 VISIT(st, expr, e->v.IfExp.body);
1555 VISIT(st, expr, e->v.IfExp.orelse);
1556 break;
1557 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001558 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001559 VISIT_SEQ(st, expr, e->v.Dict.values);
1560 break;
1561 case Set_kind:
1562 VISIT_SEQ(st, expr, e->v.Set.elts);
1563 break;
1564 case GeneratorExp_kind:
1565 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001566 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567 break;
1568 case ListComp_kind:
1569 if (!symtable_visit_listcomp(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 SetComp_kind:
1573 if (!symtable_visit_setcomp(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 DictComp_kind:
1577 if (!symtable_visit_dictcomp(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 Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001581 if (e->v.Yield.value)
1582 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001585 case YieldFrom_kind:
1586 VISIT(st, expr, e->v.YieldFrom.value);
1587 st->st_cur->ste_generator = 1;
1588 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001589 case Await_kind:
1590 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001591 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001592 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593 case Compare_kind:
1594 VISIT(st, expr, e->v.Compare.left);
1595 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1596 break;
1597 case Call_kind:
1598 VISIT(st, expr, e->v.Call.func);
1599 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001600 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001601 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001602 case FormattedValue_kind:
1603 VISIT(st, expr, e->v.FormattedValue.value);
1604 if (e->v.FormattedValue.format_spec)
1605 VISIT(st, expr, e->v.FormattedValue.format_spec);
1606 break;
1607 case JoinedStr_kind:
1608 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1609 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001610 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001611 /* Nothing to do here. */
1612 break;
1613 /* The following exprs can be assignment targets. */
1614 case Attribute_kind:
1615 VISIT(st, expr, e->v.Attribute.value);
1616 break;
1617 case Subscript_kind:
1618 VISIT(st, expr, e->v.Subscript.value);
1619 VISIT(st, slice, e->v.Subscript.slice);
1620 break;
1621 case Starred_kind:
1622 VISIT(st, expr, e->v.Starred.value);
1623 break;
1624 case Name_kind:
1625 if (!symtable_add_def(st, e->v.Name.id,
1626 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001627 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001628 /* Special-case super: it counts as a use of __class__ */
1629 if (e->v.Name.ctx == Load &&
1630 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001631 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001632 if (!GET_IDENTIFIER(__class__) ||
1633 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001634 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 }
1636 break;
1637 /* child nodes of List and Tuple will have expr_context set */
1638 case List_kind:
1639 VISIT_SEQ(st, expr, e->v.List.elts);
1640 break;
1641 case Tuple_kind:
1642 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1643 break;
1644 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001645 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001646}
1647
1648static int
1649symtable_implicit_arg(struct symtable *st, int pos)
1650{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1652 if (id == NULL)
1653 return 0;
1654 if (!symtable_add_def(st, id, DEF_PARAM)) {
1655 Py_DECREF(id);
1656 return 0;
1657 }
1658 Py_DECREF(id);
1659 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001660}
1661
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001662static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001663symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 if (!args)
1668 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001669
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 for (i = 0; i < asdl_seq_LEN(args); i++) {
1671 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1672 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1673 return 0;
1674 }
1675
1676 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001680symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001683
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 if (!args)
1685 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001686
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001687 for (i = 0; i < asdl_seq_LEN(args); i++) {
1688 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1689 if (arg->annotation)
1690 VISIT(st, expr, arg->annotation);
1691 }
1692
1693 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001694}
1695
Neal Norwitzc1505362006-12-28 06:47:50 +00001696static int
Yury Selivanov75445082015-05-11 22:57:16 -04001697symtable_visit_annotations(struct symtable *st, stmt_ty s,
1698 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001699{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001700 if (a->args && !symtable_visit_argannotations(st, a->args))
1701 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001702 if (a->vararg && a->vararg->annotation)
1703 VISIT(st, expr, a->vararg->annotation);
1704 if (a->kwarg && a->kwarg->annotation)
1705 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1707 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001708 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001709 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001710 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001711}
1712
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001714symtable_visit_arguments(struct symtable *st, arguments_ty a)
1715{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 /* skip default arguments inside function block
1717 XXX should ast be different?
1718 */
Pablo Galindo8c77b8c2019-04-29 13:36:57 +01001719 if (a->posonlyargs && !symtable_visit_params(st, a->posonlyargs))
1720 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001721 if (a->args && !symtable_visit_params(st, a->args))
1722 return 0;
1723 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1724 return 0;
1725 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001726 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001727 return 0;
1728 st->st_cur->ste_varargs = 1;
1729 }
1730 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001731 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001732 return 0;
1733 st->st_cur->ste_varkeywords = 1;
1734 }
1735 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001736}
1737
1738
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001740symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1741{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 if (eh->v.ExceptHandler.type)
1743 VISIT(st, expr, eh->v.ExceptHandler.type);
1744 if (eh->v.ExceptHandler.name)
1745 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1746 return 0;
1747 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1748 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001749}
1750
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001751static int
1752symtable_visit_withitem(struct symtable *st, withitem_ty item)
1753{
1754 VISIT(st, expr, item->context_expr);
1755 if (item->optional_vars) {
1756 VISIT(st, expr, item->optional_vars);
1757 }
1758 return 1;
1759}
1760
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001763symtable_visit_alias(struct symtable *st, alias_ty a)
1764{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001765 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001766 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 dotted package name (e.g. spam.eggs)
1768 */
1769 PyObject *store_name;
1770 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001771 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1772 PyUnicode_GET_LENGTH(name), 1);
1773 if (dot != -1) {
1774 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 if (!store_name)
1776 return 0;
1777 }
1778 else {
1779 store_name = name;
1780 Py_INCREF(store_name);
1781 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001782 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1784 Py_DECREF(store_name);
1785 return r;
1786 }
1787 else {
1788 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001789 int lineno = st->st_cur->ste_lineno;
1790 int col_offset = st->st_cur->ste_col_offset;
1791 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001792 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001793 Py_DECREF(store_name);
1794 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001795 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001796 Py_DECREF(store_name);
1797 return 1;
1798 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001799}
1800
1801
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001803symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1804{
Nick Coghlan6ca03072019-08-26 00:41:47 +10001805 st->st_cur->ste_comp_iter_target = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001806 VISIT(st, expr, lc->target);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001807 st->st_cur->ste_comp_iter_target = 0;
1808 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001809 VISIT(st, expr, lc->iter);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001810 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001811 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001812 if (lc->is_async) {
1813 st->st_cur->ste_coroutine = 1;
1814 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001815 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001816}
1817
1818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001819static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001820symtable_visit_keyword(struct symtable *st, keyword_ty k)
1821{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001822 VISIT(st, expr, k->value);
1823 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001824}
1825
1826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001827static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001828symtable_visit_slice(struct symtable *st, slice_ty s)
1829{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001830 switch (s->kind) {
1831 case Slice_kind:
1832 if (s->v.Slice.lower)
1833 VISIT(st, expr, s->v.Slice.lower)
1834 if (s->v.Slice.upper)
1835 VISIT(st, expr, s->v.Slice.upper)
1836 if (s->v.Slice.step)
1837 VISIT(st, expr, s->v.Slice.step)
1838 break;
1839 case ExtSlice_kind:
1840 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1841 break;
1842 case Index_kind:
1843 VISIT(st, expr, s->v.Index.value)
1844 break;
1845 }
1846 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001847}
1848
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001849static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001850symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001851 identifier scope_name, asdl_seq *generators,
1852 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001853{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001854 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001855 comprehension_ty outermost = ((comprehension_ty)
1856 asdl_seq_GET(generators, 0));
1857 /* Outermost iterator is evaluated in current scope */
Nick Coghlan6ca03072019-08-26 00:41:47 +10001858 st->st_cur->ste_comp_iter_expr++;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001859 VISIT(st, expr, outermost->iter);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001860 st->st_cur->ste_comp_iter_expr--;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001861 /* Create comprehension scope for the rest */
1862 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001863 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1864 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001865 return 0;
1866 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001867 if (outermost->is_async) {
1868 st->st_cur->ste_coroutine = 1;
1869 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001870 st->st_cur->ste_comprehension = 1;
1871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001872 /* Outermost iter is received as an argument */
1873 if (!symtable_implicit_arg(st, 0)) {
1874 symtable_exit_block(st, (void *)e);
1875 return 0;
1876 }
Nick Coghlan6ca03072019-08-26 00:41:47 +10001877 /* Visit iteration variable target, and mark them as such */
1878 st->st_cur->ste_comp_iter_target = 1;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001879 VISIT(st, expr, outermost->target);
Nick Coghlan6ca03072019-08-26 00:41:47 +10001880 st->st_cur->ste_comp_iter_target = 0;
1881 /* Visit the rest of the comprehension body */
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001882 VISIT_SEQ(st, expr, outermost->ifs);
1883 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001884 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001885 VISIT(st, expr, value);
1886 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001887 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001888 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001889 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1890 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1891 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1892 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001893 PyErr_SyntaxLocationObject(st->st_filename,
1894 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001895 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001896 symtable_exit_block(st, (void *)e);
1897 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001898 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001899 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001900 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001901}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001902
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001903static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001904symtable_visit_genexp(struct symtable *st, expr_ty e)
1905{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001906 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1907 e->v.GeneratorExp.generators,
1908 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001909}
1910
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001911static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001912symtable_visit_listcomp(struct symtable *st, expr_ty e)
1913{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001914 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1915 e->v.ListComp.generators,
1916 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001917}
1918
1919static int
1920symtable_visit_setcomp(struct symtable *st, expr_ty e)
1921{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001922 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1923 e->v.SetComp.generators,
1924 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001925}
1926
1927static int
1928symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1929{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001930 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1931 e->v.DictComp.generators,
1932 e->v.DictComp.key,
1933 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001934}