blob: cade3045b3fccdaaed15a4e7d59dfc05976bb091 [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 \
35"named expression within a comprehension cannot be used in a class body"
36
Neal Norwitz090b3dd2006-02-28 22:36:46 +000037static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000038ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000039 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000040{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020042 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000043
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000044 k = PyLong_FromVoidPtr(key);
45 if (k == NULL)
46 goto fail;
47 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020048 if (ste == NULL) {
49 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000050 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020051 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020053 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020056 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000057
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000058 ste->ste_symbols = NULL;
59 ste->ste_varnames = NULL;
60 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000061
Benjamin Petersond9c87022012-10-31 20:26:20 -040062 ste->ste_directives = NULL;
63
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000064 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 ste->ste_nested = 0;
66 ste->ste_free = 0;
67 ste->ste_varargs = 0;
68 ste->ste_varkeywords = 0;
69 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000070 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000072 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000073
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000074 if (st->st_cur != NULL &&
75 (st->st_cur->ste_nested ||
76 st->st_cur->ste_type == FunctionBlock))
77 ste->ste_nested = 1;
78 ste->ste_child_free = 0;
79 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070080 ste->ste_coroutine = 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -070081 ste->ste_comprehension = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050083 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000084
Victor Stinner9a4fb662013-07-11 22:49:00 +020085 ste->ste_symbols = PyDict_New();
86 ste->ste_varnames = PyList_New(0);
87 ste->ste_children = PyList_New(0);
88 if (ste->ste_symbols == NULL
89 || ste->ste_varnames == NULL
90 || ste->ste_children == NULL)
91 goto fail;
92
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000093 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
94 goto fail;
95
96 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000097 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000098 Py_XDECREF(ste);
99 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100}
101
102static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000103ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000104{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
106 ste->ste_name,
107 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108}
109
110static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000111ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000112{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 ste->ste_table = NULL;
114 Py_XDECREF(ste->ste_id);
115 Py_XDECREF(ste->ste_name);
116 Py_XDECREF(ste->ste_symbols);
117 Py_XDECREF(ste->ste_varnames);
118 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400119 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000121}
122
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000123#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000124
Guido van Rossum6f799372001-09-20 20:46:19 +0000125static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 {"id", T_OBJECT, OFF(ste_id), READONLY},
127 {"name", T_OBJECT, OFF(ste_name), READONLY},
128 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
129 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
130 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000131 {"nested", T_INT, OFF(ste_nested), READONLY},
132 {"type", T_INT, OFF(ste_type), READONLY},
133 {"lineno", T_INT, OFF(ste_lineno), READONLY},
134 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000135};
136
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000137PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000138 PyVarObject_HEAD_INIT(&PyType_Type, 0)
139 "symtable entry",
140 sizeof(PySTEntryObject),
141 0,
142 (destructor)ste_dealloc, /* tp_dealloc */
143 0, /* tp_print */
144 0, /* tp_getattr */
145 0, /* tp_setattr */
146 0, /* tp_reserved */
147 (reprfunc)ste_repr, /* tp_repr */
148 0, /* tp_as_number */
149 0, /* tp_as_sequence */
150 0, /* tp_as_mapping */
151 0, /* tp_hash */
152 0, /* tp_call */
153 0, /* tp_str */
154 PyObject_GenericGetAttr, /* tp_getattro */
155 0, /* tp_setattro */
156 0, /* tp_as_buffer */
157 Py_TPFLAGS_DEFAULT, /* tp_flags */
158 0, /* tp_doc */
159 0, /* tp_traverse */
160 0, /* tp_clear */
161 0, /* tp_richcompare */
162 0, /* tp_weaklistoffset */
163 0, /* tp_iter */
164 0, /* tp_iternext */
165 0, /* tp_methods */
166 ste_memberlist, /* tp_members */
167 0, /* tp_getset */
168 0, /* tp_base */
169 0, /* tp_dict */
170 0, /* tp_descr_get */
171 0, /* tp_descr_set */
172 0, /* tp_dictoffset */
173 0, /* tp_init */
174 0, /* tp_alloc */
175 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000176};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000177
178static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000179static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000180 _Py_block_ty block, void *ast, int lineno,
181 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000182static int symtable_exit_block(struct symtable *st, void *ast);
183static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
184static int symtable_visit_expr(struct symtable *st, expr_ty s);
185static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000186static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
187static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000188static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000189static int symtable_visit_arguments(struct symtable *st, arguments_ty);
190static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
191static int symtable_visit_alias(struct symtable *st, alias_ty);
192static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
193static int symtable_visit_keyword(struct symtable *st, keyword_ty);
194static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000195static int symtable_visit_params(struct symtable *st, asdl_seq *args);
196static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000197static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400198static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500199static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000200
201
Nick Coghlan650f0d02007-04-15 12:05:43 +0000202static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500204 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000205
206#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000208
209#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000210"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000211
212static struct symtable *
213symtable_new(void)
214{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700218 if (st == NULL) {
219 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700221 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000222
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000223 st->st_filename = NULL;
224 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000225
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 if ((st->st_stack = PyList_New(0)) == NULL)
227 goto fail;
228 if ((st->st_blocks = PyDict_New()) == NULL)
229 goto fail;
230 st->st_cur = NULL;
231 st->st_private = NULL;
232 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000233 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000234 PySymtable_Free(st);
235 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000236}
237
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000238/* When compiling the use of C stack is probably going to be a lot
239 lighter than when executing Python code but still can overflow
240 and causing a Python crash if not checked (e.g. eval("()"*300000)).
241 Using the current recursion limit for the compiler seems too
242 restrictive (it caused at least one test to fail) so a factor is
243 used to allow deeper recursion when compiling an expression.
244
245 Using a scaling factor means this should automatically adjust when
246 the recursion limit is adjusted for small or large C stack allocations.
247*/
248#define COMPILER_STACK_FRAME_SCALE 3
249
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200251PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000252{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000253 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000254 asdl_seq *seq;
255 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000256 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400257 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000258
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200260 return NULL;
261 if (filename == NULL) {
262 PySymtable_Free(st);
263 return NULL;
264 }
265 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000266 st->st_filename = filename;
267 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000268
269 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100270 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000271 if (!tstate) {
272 PySymtable_Free(st);
273 return NULL;
274 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400275 /* Be careful here to prevent overflow. */
276 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
277 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
278 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
279 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000280
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000281 /* Make the initial symbol information gathering pass */
282 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000283 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 PySymtable_Free(st);
285 return NULL;
286 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000289 switch (mod->kind) {
290 case Module_kind:
291 seq = mod->v.Module.body;
292 for (i = 0; i < asdl_seq_LEN(seq); i++)
293 if (!symtable_visit_stmt(st,
294 (stmt_ty)asdl_seq_GET(seq, i)))
295 goto error;
296 break;
297 case Expression_kind:
298 if (!symtable_visit_expr(st, mod->v.Expression.body))
299 goto error;
300 break;
301 case Interactive_kind:
302 seq = mod->v.Interactive.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 Suite_kind:
309 PyErr_SetString(PyExc_RuntimeError,
310 "this compiler does not handle Suites");
311 goto error;
Guido van Rossum522346d2019-02-11 11:34:50 -0800312 case FunctionType_kind:
313 PyErr_SetString(PyExc_RuntimeError,
314 "this compiler does not handle FunctionTypes");
315 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 }
317 if (!symtable_exit_block(st, (void *)mod)) {
318 PySymtable_Free(st);
319 return NULL;
320 }
321 /* Make the second symbol analysis pass */
322 if (symtable_analyze(st))
323 return st;
324 PySymtable_Free(st);
325 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000326 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 (void) symtable_exit_block(st, (void *)mod);
328 PySymtable_Free(st);
329 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000330}
331
Victor Stinner14e461d2013-08-26 22:28:21 +0200332struct symtable *
333PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
334{
335 PyObject *filename;
336 struct symtable *st;
337 filename = PyUnicode_DecodeFSDefault(filename_str);
338 if (filename == NULL)
339 return NULL;
340 st = PySymtable_BuildObject(mod, filename, future);
341 Py_DECREF(filename);
342 return st;
343}
344
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000345void
346PySymtable_Free(struct symtable *st)
347{
Victor Stinner14e461d2013-08-26 22:28:21 +0200348 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 Py_XDECREF(st->st_blocks);
350 Py_XDECREF(st->st_stack);
351 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352}
353
354PySTEntryObject *
355PySymtable_Lookup(struct symtable *st, void *key)
356{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000357 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000358
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 k = PyLong_FromVoidPtr(key);
360 if (k == NULL)
361 return NULL;
362 v = PyDict_GetItem(st->st_blocks, k);
363 if (v) {
364 assert(PySTEntry_Check(v));
365 Py_INCREF(v);
366 }
367 else {
368 PyErr_SetString(PyExc_KeyError,
369 "unknown symbol table entry");
370 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372 Py_DECREF(k);
373 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000374}
375
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000377PyST_GetScope(PySTEntryObject *ste, PyObject *name)
378{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000379 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
380 if (!v)
381 return 0;
382 assert(PyLong_Check(v));
383 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000384}
385
Benjamin Petersond9c87022012-10-31 20:26:20 -0400386static int
387error_at_directive(PySTEntryObject *ste, PyObject *name)
388{
389 Py_ssize_t i;
390 PyObject *data;
391 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600392 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400393 data = PyList_GET_ITEM(ste->ste_directives, i);
394 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600395 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
396 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
397 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
398 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400399 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600400
401 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700402 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400403 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600404 PyErr_SetString(PyExc_RuntimeError,
405 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400406 return 0;
407}
408
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000409
410/* Analyze raw symbol information to determine scope of each name.
411
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000412 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000413 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000414 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000415 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000416
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000417 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000418 explicit global is declared with the global statement. An implicit
419 global is a free variable for which the compiler has found no binding
420 in an enclosing function scope. The implicit global is either a global
421 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
422 to handle these names to implement slightly odd semantics. In such a
423 block, the name is treated as global until it is assigned to; then it
424 is treated as a local.
425
426 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000427 The first pass collects raw facts from the AST via the symtable_visit_*
428 functions: the name is a parameter here, the name is used but not defined
429 here, etc. The second pass analyzes these facts during a pass over the
430 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
432 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000433 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000434 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000435 Names which are explicitly declared nonlocal must exist in this set of
436 visible names - if they do not, a syntax error is raised. After doing
437 the local analysis, it analyzes each of its child blocks using an
438 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000439
Nick Coghlan650f0d02007-04-15 12:05:43 +0000440 The children update the free variable set. If a local variable is added to
441 the free variable set by the child, the variable is marked as a cell. The
442 function object being defined must provide runtime storage for the variable
443 that may outlive the function's frame. Cell variables are removed from the
444 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000445
Nick Coghlan650f0d02007-04-15 12:05:43 +0000446 During analysis, the names are:
447 symbols: dict mapping from symbol names to flag values (including offset scope values)
448 scopes: dict mapping from symbol names to scope values (no offset)
449 local: set of all symbol names local to the current scope
450 bound: set of all symbol names local to a containing function scope
451 free: set of all symbol names referenced but not bound in child scopes
452 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000453*/
454
455#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 PyObject *o = PyLong_FromLong(I); \
457 if (!o) \
458 return 0; \
459 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
460 Py_DECREF(o); \
461 return 0; \
462 } \
463 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000464}
465
466/* Decide on scope of name, given flags.
467
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000468 The namespace dictionaries may be modified to record information
469 about the new name. For example, a new global will add an entry to
470 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471*/
472
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000474analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyObject *bound, PyObject *local, PyObject *free,
476 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000477{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000479 if (flags & DEF_NONLOCAL) {
480 PyErr_Format(PyExc_SyntaxError,
481 "name '%U' is nonlocal and global",
482 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400483 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 }
485 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
486 if (PySet_Add(global, name) < 0)
487 return 0;
488 if (bound && (PySet_Discard(bound, name) < 0))
489 return 0;
490 return 1;
491 }
492 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000493 if (!bound) {
494 PyErr_Format(PyExc_SyntaxError,
495 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400496 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000497 }
498 if (!PySet_Contains(bound, name)) {
499 PyErr_Format(PyExc_SyntaxError,
500 "no binding for nonlocal '%U' found",
501 name);
502
Benjamin Petersond9c87022012-10-31 20:26:20 -0400503 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000504 }
505 SET_SCOPE(scopes, name, FREE);
506 ste->ste_free = 1;
507 return PySet_Add(free, name) >= 0;
508 }
509 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000510 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000511 if (PySet_Add(local, name) < 0)
512 return 0;
513 if (PySet_Discard(global, name) < 0)
514 return 0;
515 return 1;
516 }
517 /* If an enclosing block has a binding for this name, it
518 is a free variable rather than a global variable.
519 Note that having a non-NULL bound implies that the block
520 is nested.
521 */
522 if (bound && PySet_Contains(bound, name)) {
523 SET_SCOPE(scopes, name, FREE);
524 ste->ste_free = 1;
525 return PySet_Add(free, name) >= 0;
526 }
527 /* If a parent has a global statement, then call it global
528 explicit? It could also be global implicit.
529 */
530 if (global && PySet_Contains(global, name)) {
531 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
532 return 1;
533 }
534 if (ste->ste_nested)
535 ste->ste_free = 1;
536 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
537 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000538}
539
540#undef SET_SCOPE
541
542/* If a name is defined in free and also in locals, then this block
543 provides the binding for the free variable. The name should be
544 marked CELL in this block and removed from the free list.
545
546 Note that the current block's free variables are included in free.
547 That's safe because no name can be free and local in the same scope.
548*/
549
550static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500551analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000552{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 PyObject *name, *v, *v_cell;
554 int success = 0;
555 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000556
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 v_cell = PyLong_FromLong(CELL);
558 if (!v_cell)
559 return 0;
560 while (PyDict_Next(scopes, &pos, &name, &v)) {
561 long scope;
562 assert(PyLong_Check(v));
563 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000564 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000565 continue;
566 if (!PySet_Contains(free, name))
567 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000568 /* Replace LOCAL with CELL for this name, and remove
569 from free. It is safe to replace the value of name
570 in the dict, because it will not cause a resize.
571 */
572 if (PyDict_SetItem(scopes, name, v_cell) < 0)
573 goto error;
574 if (PySet_Discard(free, name) < 0)
575 goto error;
576 }
577 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000578 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000579 Py_DECREF(v_cell);
580 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000581}
582
Benjamin Peterson312595c2013-05-15 15:26:42 -0500583static int
584drop_class_free(PySTEntryObject *ste, PyObject *free)
585{
586 int res;
587 if (!GET_IDENTIFIER(__class__))
588 return 0;
589 res = PySet_Discard(free, __class__);
590 if (res < 0)
591 return 0;
592 if (res)
593 ste->ste_needs_class_closure = 1;
594 return 1;
595}
596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597/* Enter the final scope information into the ste_symbols dict.
598 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000599 * All arguments are dicts. Modifies symbols, others are read-only.
600*/
601static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000602update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000603 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000604{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000605 PyObject *name = NULL, *itr = NULL;
606 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
607 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000609 /* Update scope information for all symbols in this scope */
610 while (PyDict_Next(symbols, &pos, &name, &v)) {
611 long scope, flags;
612 assert(PyLong_Check(v));
613 flags = PyLong_AS_LONG(v);
614 v_scope = PyDict_GetItem(scopes, name);
615 assert(v_scope && PyLong_Check(v_scope));
616 scope = PyLong_AS_LONG(v_scope);
617 flags |= (scope << SCOPE_OFFSET);
618 v_new = PyLong_FromLong(flags);
619 if (!v_new)
620 return 0;
621 if (PyDict_SetItem(symbols, name, v_new) < 0) {
622 Py_DECREF(v_new);
623 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000624 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000625 Py_DECREF(v_new);
626 }
627
628 /* Record not yet resolved free variables from children (if any) */
629 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
630 if (!v_free)
631 return 0;
632
633 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600634 if (itr == NULL) {
635 Py_DECREF(v_free);
636 return 0;
637 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638
639 while ((name = PyIter_Next(itr))) {
640 v = PyDict_GetItem(symbols, name);
641
642 /* Handle symbol that already exists in this scope */
643 if (v) {
644 /* Handle a free variable in a method of
645 the class that has the same name as a local
646 or global in the class scope.
647 */
648 if (classflag &&
649 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
650 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
651 v_new = PyLong_FromLong(flags);
652 if (!v_new) {
653 goto error;
654 }
655 if (PyDict_SetItem(symbols, name, v_new) < 0) {
656 Py_DECREF(v_new);
657 goto error;
658 }
659 Py_DECREF(v_new);
660 }
661 /* It's a cell, or already free in this scope */
662 Py_DECREF(name);
663 continue;
664 }
665 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200666 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000667 Py_DECREF(name);
668 continue; /* it's a global */
669 }
670 /* Propagate new free symbol up the lexical stack */
671 if (PyDict_SetItem(symbols, name, v_free) < 0) {
672 goto error;
673 }
674 Py_DECREF(name);
675 }
676 Py_DECREF(itr);
677 Py_DECREF(v_free);
678 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000679error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000680 Py_XDECREF(v_free);
681 Py_XDECREF(itr);
682 Py_XDECREF(name);
683 return 0;
684}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000685
686/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000687
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000688 Arguments:
689 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000690 bound -- set of variables bound in enclosing scopes (input). bound
691 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000692 free -- set of free variables in enclosed scopes (output)
693 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000694
695 The implementation uses two mutually recursive functions,
696 analyze_block() and analyze_child_block(). analyze_block() is
697 responsible for analyzing the individual names defined in a block.
698 analyze_child_block() prepares temporary namespace dictionaries
699 used to evaluated nested blocks.
700
701 The two functions exist because a child block should see the name
702 bindings of its enclosing blocks, but those bindings should not
703 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000704*/
705
706static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000707analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
708 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000709
710static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
712 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
715 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
716 PyObject *temp;
717 int i, success = 0;
718 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000719
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000720 local = PySet_New(NULL); /* collect new names bound in block */
721 if (!local)
722 goto error;
723 scopes = PyDict_New(); /* collect scopes defined for each name */
724 if (!scopes)
725 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* Allocate new global and bound variable dictionaries. These
728 dictionaries hold the names visible in nested blocks. For
729 ClassBlocks, the bound and global names are initialized
730 before analyzing names, because class bindings aren't
731 visible in methods. For other blocks, they are initialized
732 after names are analyzed.
733 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000734
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000735 /* TODO(jhylton): Package these dicts in a struct so that we
736 can write reasonable helper functions?
737 */
738 newglobal = PySet_New(NULL);
739 if (!newglobal)
740 goto error;
741 newfree = PySet_New(NULL);
742 if (!newfree)
743 goto error;
744 newbound = PySet_New(NULL);
745 if (!newbound)
746 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000747
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000748 /* Class namespace has no effect on names visible in
749 nested functions, so populate the global and bound
750 sets to be passed to child blocks before analyzing
751 this one.
752 */
753 if (ste->ste_type == ClassBlock) {
754 /* Pass down known globals */
755 temp = PyNumber_InPlaceOr(newglobal, global);
756 if (!temp)
757 goto error;
758 Py_DECREF(temp);
759 /* Pass down previously bound symbols */
760 if (bound) {
761 temp = PyNumber_InPlaceOr(newbound, bound);
762 if (!temp)
763 goto error;
764 Py_DECREF(temp);
765 }
766 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000767
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000768 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
769 long flags = PyLong_AS_LONG(v);
770 if (!analyze_name(ste, scopes, name, flags,
771 bound, local, free, global))
772 goto error;
773 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000774
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 /* Populate global and bound sets to be passed to children. */
776 if (ste->ste_type != ClassBlock) {
777 /* Add function locals to bound set */
778 if (ste->ste_type == FunctionBlock) {
779 temp = PyNumber_InPlaceOr(newbound, local);
780 if (!temp)
781 goto error;
782 Py_DECREF(temp);
783 }
784 /* Pass down previously bound symbols */
785 if (bound) {
786 temp = PyNumber_InPlaceOr(newbound, bound);
787 if (!temp)
788 goto error;
789 Py_DECREF(temp);
790 }
791 /* Pass down known globals */
792 temp = PyNumber_InPlaceOr(newglobal, global);
793 if (!temp)
794 goto error;
795 Py_DECREF(temp);
796 }
797 else {
798 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000799 if (!GET_IDENTIFIER(__class__))
800 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 if (PySet_Add(newbound, __class__) < 0)
802 goto error;
803 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000804
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300805 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000806
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000807 newbound, newglobal now contain the names visible in
808 nested blocks. The free variables in the children will
809 be collected in allfree.
810 */
811 allfree = PySet_New(NULL);
812 if (!allfree)
813 goto error;
814 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
815 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
816 PySTEntryObject* entry;
817 assert(c && PySTEntry_Check(c));
818 entry = (PySTEntryObject*)c;
819 if (!analyze_child_block(entry, newbound, newfree, newglobal,
820 allfree))
821 goto error;
822 /* Check if any children have free variables */
823 if (entry->ste_free || entry->ste_child_free)
824 ste->ste_child_free = 1;
825 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000826
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 temp = PyNumber_InPlaceOr(newfree, allfree);
828 if (!temp)
829 goto error;
830 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000831
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000832 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500833 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500835 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 goto error;
837 /* Records the results of the analysis in the symbol table entry */
838 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
839 ste->ste_type == ClassBlock))
840 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 temp = PyNumber_InPlaceOr(free, newfree);
843 if (!temp)
844 goto error;
845 Py_DECREF(temp);
846 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000847 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000848 Py_XDECREF(scopes);
849 Py_XDECREF(local);
850 Py_XDECREF(newbound);
851 Py_XDECREF(newglobal);
852 Py_XDECREF(newfree);
853 Py_XDECREF(allfree);
854 if (!success)
855 assert(PyErr_Occurred());
856 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000857}
858
859static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
861 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000862{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
864 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000867
Martin Panter3ee62702016-06-04 04:57:19 +0000868 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000869 current block. The analyze_block() call modifies these
870 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000871
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000872 */
873 temp_bound = PySet_New(bound);
874 if (!temp_bound)
875 goto error;
876 temp_free = PySet_New(free);
877 if (!temp_free)
878 goto error;
879 temp_global = PySet_New(global);
880 if (!temp_global)
881 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000882
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000883 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
884 goto error;
885 temp = PyNumber_InPlaceOr(child_free, temp_free);
886 if (!temp)
887 goto error;
888 Py_DECREF(temp);
889 Py_DECREF(temp_bound);
890 Py_DECREF(temp_free);
891 Py_DECREF(temp_global);
892 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000893 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000894 Py_XDECREF(temp_bound);
895 Py_XDECREF(temp_free);
896 Py_XDECREF(temp_global);
897 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000898}
899
900static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000901symtable_analyze(struct symtable *st)
902{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000903 PyObject *free, *global;
904 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000905
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000906 free = PySet_New(NULL);
907 if (!free)
908 return 0;
909 global = PySet_New(NULL);
910 if (!global) {
911 Py_DECREF(free);
912 return 0;
913 }
914 r = analyze_block(st->st_top, NULL, free, global);
915 Py_DECREF(free);
916 Py_DECREF(global);
917 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000918}
919
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000920/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921 This reference is released when the block is exited, via the DECREF
922 in symtable_exit_block().
923*/
924
925static int
926symtable_exit_block(struct symtable *st, void *ast)
927{
Benjamin Peterson609da582011-06-29 22:52:39 -0500928 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000929
Benjamin Peterson609da582011-06-29 22:52:39 -0500930 st->st_cur = NULL;
931 size = PyList_GET_SIZE(st->st_stack);
932 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500933 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500935 if (--size)
936 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000937 }
938 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000939}
940
941static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000942symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000943 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000944{
Benjamin Peterson609da582011-06-29 22:52:39 -0500945 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000946
Benjamin Peterson609da582011-06-29 22:52:39 -0500947 ste = ste_new(st, name, block, ast, lineno, col_offset);
948 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000949 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500950 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
951 Py_DECREF(ste);
952 return 0;
953 }
954 prev = st->st_cur;
955 /* The entry is owned by the stack. Borrow it for st_cur. */
956 Py_DECREF(ste);
957 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000958 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000959 st->st_global = st->st_cur->ste_symbols;
960 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500961 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 return 0;
963 }
964 }
965 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000966}
967
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000968static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000969symtable_lookup(struct symtable *st, PyObject *name)
970{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000971 PyObject *o;
972 PyObject *mangled = _Py_Mangle(st->st_private, name);
973 if (!mangled)
974 return 0;
975 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
976 Py_DECREF(mangled);
977 if (!o)
978 return 0;
979 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000980}
981
982static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700983symtable_add_def_helper(struct symtable *st, PyObject *name, int flag, struct _symtable_entry *ste)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000984{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 PyObject *o;
986 PyObject *dict;
987 long val;
988 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000989
Jeremy Hylton81e95022007-02-27 06:50:52 +0000990
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000991 if (!mangled)
992 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -0700993 dict = ste->ste_symbols;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 if ((o = PyDict_GetItem(dict, mangled))) {
995 val = PyLong_AS_LONG(o);
996 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
997 /* Is it better to use 'mangled' or 'name' here? */
998 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200999 PyErr_SyntaxLocationObject(st->st_filename,
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001000 ste->ste_lineno,
1001 ste->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002 goto error;
1003 }
1004 val |= flag;
1005 } else
1006 val = flag;
1007 o = PyLong_FromLong(val);
1008 if (o == NULL)
1009 goto error;
1010 if (PyDict_SetItem(dict, mangled, o) < 0) {
1011 Py_DECREF(o);
1012 goto error;
1013 }
1014 Py_DECREF(o);
1015
1016 if (flag & DEF_PARAM) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001017 if (PyList_Append(ste->ste_varnames, mangled) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001018 goto error;
1019 } else if (flag & DEF_GLOBAL) {
1020 /* XXX need to update DEF_GLOBAL for other flags too;
1021 perhaps only DEF_FREE_GLOBAL */
1022 val = flag;
1023 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1024 val |= PyLong_AS_LONG(o);
1025 }
1026 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001027 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001028 goto error;
1029 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1030 Py_DECREF(o);
1031 goto error;
1032 }
1033 Py_DECREF(o);
1034 }
1035 Py_DECREF(mangled);
1036 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001037
1038error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 Py_DECREF(mangled);
1040 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041}
1042
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001043static int
1044symtable_add_def(struct symtable *st, PyObject *name, int flag) {
1045 return symtable_add_def_helper(st, name, flag, st->st_cur);
1046}
1047
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001048/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1049 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 function.
1051
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001052 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1053 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001054
1055 VISIT_QUIT macro returns the specified value exiting from the function but
1056 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001057*/
1058
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001059#define VISIT_QUIT(ST, X) \
1060 return --(ST)->recursion_depth,(X)
1061
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001062#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001064 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001065
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001066#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 int i; \
1068 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1069 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1070 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1071 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001072 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001073 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001074}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001075
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001076#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 int i; \
1078 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1079 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1080 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1081 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001082 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001085
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001086#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001087 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001088 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001089 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001090 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001091 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001092 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001093 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001094 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001095}
1096
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001097static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001098symtable_record_directive(struct symtable *st, identifier name, int lineno, int col_offset)
Benjamin Petersond9c87022012-10-31 20:26:20 -04001099{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001100 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001101 int res;
1102 if (!st->st_cur->ste_directives) {
1103 st->st_cur->ste_directives = PyList_New(0);
1104 if (!st->st_cur->ste_directives)
1105 return 0;
1106 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001107 mangled = _Py_Mangle(st->st_private, name);
1108 if (!mangled)
1109 return 0;
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001110 data = Py_BuildValue("(Nii)", mangled, lineno, col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001111 if (!data)
1112 return 0;
1113 res = PyList_Append(st->st_cur->ste_directives, data);
1114 Py_DECREF(data);
1115 return res == 0;
1116}
1117
1118
1119static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001120symtable_visit_stmt(struct symtable *st, stmt_ty s)
1121{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001122 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001123 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001124 "maximum recursion depth exceeded during compilation");
1125 VISIT_QUIT(st, 0);
1126 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 switch (s->kind) {
1128 case FunctionDef_kind:
1129 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001130 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001131 if (s->v.FunctionDef.args->defaults)
1132 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1133 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001134 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001135 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1136 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001137 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001138 if (s->v.FunctionDef.decorator_list)
1139 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1140 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001141 FunctionBlock, (void *)s, s->lineno,
1142 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001143 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001144 VISIT(st, arguments, s->v.FunctionDef.args);
1145 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001146 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001147 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 break;
1149 case ClassDef_kind: {
1150 PyObject *tmp;
1151 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001152 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1154 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 if (s->v.ClassDef.decorator_list)
1156 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1157 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001158 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001159 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001160 tmp = st->st_private;
1161 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001162 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001163 st->st_private = tmp;
1164 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001165 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001166 break;
1167 }
1168 case Return_kind:
1169 if (s->v.Return.value) {
1170 VISIT(st, expr, s->v.Return.value);
1171 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001172 }
1173 break;
1174 case Delete_kind:
1175 VISIT_SEQ(st, expr, s->v.Delete.targets);
1176 break;
1177 case Assign_kind:
1178 VISIT_SEQ(st, expr, s->v.Assign.targets);
1179 VISIT(st, expr, s->v.Assign.value);
1180 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001181 case AnnAssign_kind:
1182 if (s->v.AnnAssign.target->kind == Name_kind) {
1183 expr_ty e_name = s->v.AnnAssign.target;
1184 long cur = symtable_lookup(st, e_name->v.Name.id);
1185 if (cur < 0) {
1186 VISIT_QUIT(st, 0);
1187 }
1188 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001189 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001190 && s->v.AnnAssign.simple) {
1191 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001192 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1193 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001194 PyErr_SyntaxLocationObject(st->st_filename,
1195 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001196 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001197 VISIT_QUIT(st, 0);
1198 }
1199 if (s->v.AnnAssign.simple &&
1200 !symtable_add_def(st, e_name->v.Name.id,
1201 DEF_ANNOT | DEF_LOCAL)) {
1202 VISIT_QUIT(st, 0);
1203 }
1204 else {
1205 if (s->v.AnnAssign.value
1206 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1207 VISIT_QUIT(st, 0);
1208 }
1209 }
1210 }
1211 else {
1212 VISIT(st, expr, s->v.AnnAssign.target);
1213 }
1214 VISIT(st, expr, s->v.AnnAssign.annotation);
1215 if (s->v.AnnAssign.value) {
1216 VISIT(st, expr, s->v.AnnAssign.value);
1217 }
1218 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 case AugAssign_kind:
1220 VISIT(st, expr, s->v.AugAssign.target);
1221 VISIT(st, expr, s->v.AugAssign.value);
1222 break;
1223 case For_kind:
1224 VISIT(st, expr, s->v.For.target);
1225 VISIT(st, expr, s->v.For.iter);
1226 VISIT_SEQ(st, stmt, s->v.For.body);
1227 if (s->v.For.orelse)
1228 VISIT_SEQ(st, stmt, s->v.For.orelse);
1229 break;
1230 case While_kind:
1231 VISIT(st, expr, s->v.While.test);
1232 VISIT_SEQ(st, stmt, s->v.While.body);
1233 if (s->v.While.orelse)
1234 VISIT_SEQ(st, stmt, s->v.While.orelse);
1235 break;
1236 case If_kind:
1237 /* XXX if 0: and lookup_yield() hacks */
1238 VISIT(st, expr, s->v.If.test);
1239 VISIT_SEQ(st, stmt, s->v.If.body);
1240 if (s->v.If.orelse)
1241 VISIT_SEQ(st, stmt, s->v.If.orelse);
1242 break;
1243 case Raise_kind:
1244 if (s->v.Raise.exc) {
1245 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001246 if (s->v.Raise.cause) {
1247 VISIT(st, expr, s->v.Raise.cause);
1248 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001249 }
1250 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001251 case Try_kind:
1252 VISIT_SEQ(st, stmt, s->v.Try.body);
1253 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1254 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1255 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 break;
1257 case Assert_kind:
1258 VISIT(st, expr, s->v.Assert.test);
1259 if (s->v.Assert.msg)
1260 VISIT(st, expr, s->v.Assert.msg);
1261 break;
1262 case Import_kind:
1263 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001264 break;
1265 case ImportFrom_kind:
1266 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 break;
1268 case Global_kind: {
1269 int i;
1270 asdl_seq *seq = s->v.Global.names;
1271 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1272 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001273 long cur = symtable_lookup(st, name);
1274 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001275 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001276 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1277 const char* msg;
1278 if (cur & DEF_PARAM) {
1279 msg = GLOBAL_PARAM;
1280 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001281 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001282 } else if (cur & DEF_ANNOT) {
1283 msg = GLOBAL_ANNOT;
1284 } else { /* DEF_LOCAL */
1285 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001286 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001287 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001288 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001289 PyErr_SyntaxLocationObject(st->st_filename,
1290 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001291 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001292 VISIT_QUIT(st, 0);
1293 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001295 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001296 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001297 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 }
1299 break;
1300 }
1301 case Nonlocal_kind: {
1302 int i;
1303 asdl_seq *seq = s->v.Nonlocal.names;
1304 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1305 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 long cur = symtable_lookup(st, name);
1307 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001308 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001309 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1310 const char* msg;
1311 if (cur & DEF_PARAM) {
1312 msg = NONLOCAL_PARAM;
1313 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001314 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001315 } else if (cur & DEF_ANNOT) {
1316 msg = NONLOCAL_ANNOT;
1317 } else { /* DEF_LOCAL */
1318 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001319 }
1320 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001321 PyErr_SyntaxLocationObject(st->st_filename,
1322 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001323 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001324 VISIT_QUIT(st, 0);
1325 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001326 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001327 VISIT_QUIT(st, 0);
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001328 if (!symtable_record_directive(st, name, s->lineno, s->col_offset))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001329 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 }
1331 break;
1332 }
1333 case Expr_kind:
1334 VISIT(st, expr, s->v.Expr.value);
1335 break;
1336 case Pass_kind:
1337 case Break_kind:
1338 case Continue_kind:
1339 /* nothing to do here */
1340 break;
1341 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001342 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001343 VISIT_SEQ(st, stmt, s->v.With.body);
1344 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001345 case AsyncFunctionDef_kind:
1346 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1347 VISIT_QUIT(st, 0);
1348 if (s->v.AsyncFunctionDef.args->defaults)
1349 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1350 if (s->v.AsyncFunctionDef.args->kw_defaults)
1351 VISIT_SEQ_WITH_NULL(st, expr,
1352 s->v.AsyncFunctionDef.args->kw_defaults);
1353 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1354 s->v.AsyncFunctionDef.returns))
1355 VISIT_QUIT(st, 0);
1356 if (s->v.AsyncFunctionDef.decorator_list)
1357 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1358 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1359 FunctionBlock, (void *)s, s->lineno,
1360 s->col_offset))
1361 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001362 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001363 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1364 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1365 if (!symtable_exit_block(st, s))
1366 VISIT_QUIT(st, 0);
1367 break;
1368 case AsyncWith_kind:
1369 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1370 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1371 break;
1372 case AsyncFor_kind:
1373 VISIT(st, expr, s->v.AsyncFor.target);
1374 VISIT(st, expr, s->v.AsyncFor.iter);
1375 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1376 if (s->v.AsyncFor.orelse)
1377 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1378 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001380 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001381}
1382
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001383static int
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001384symtable_extend_namedexpr_scope(struct symtable *st, expr_ty e)
1385{
1386 assert(st->st_stack);
1387
1388 Py_ssize_t i, size;
1389 struct _symtable_entry *ste;
1390 size = PyList_GET_SIZE(st->st_stack);
1391 assert(size);
1392
1393 /* Iterate over the stack in reverse and add to the nearest adequate scope */
1394 for (i = size - 1; i >= 0; i--) {
1395 ste = (struct _symtable_entry *) PyList_GET_ITEM(st->st_stack, i);
1396
1397 /* If our current entry is a comprehension, skip it */
1398 if (ste->ste_comprehension) {
1399 continue;
1400 }
1401
1402 /* If we find a FunctionBlock entry, add as NONLOCAL/LOCAL */
1403 if (ste->ste_type == FunctionBlock) {
1404 if (!symtable_add_def(st, e->v.Name.id, DEF_NONLOCAL))
1405 VISIT_QUIT(st, 0);
1406 if (!symtable_record_directive(st, e->v.Name.id, e->lineno, e->col_offset))
1407 VISIT_QUIT(st, 0);
1408
1409 return symtable_add_def_helper(st, e->v.Name.id, DEF_LOCAL, ste);
1410 }
1411 /* If we find a ModuleBlock entry, add as GLOBAL */
1412 if (ste->ste_type == ModuleBlock) {
1413 if (!symtable_add_def(st, e->v.Name.id, DEF_GLOBAL))
1414 VISIT_QUIT(st, 0);
1415 if (!symtable_record_directive(st, e->v.Name.id, e->lineno, e->col_offset))
1416 VISIT_QUIT(st, 0);
1417
1418 return symtable_add_def_helper(st, e->v.Name.id, DEF_GLOBAL, ste);
1419 }
1420 /* Disallow usage in ClassBlock */
1421 if (ste->ste_type == ClassBlock) {
1422 PyErr_Format(PyExc_TargetScopeError, NAMED_EXPR_COMP_IN_CLASS, e->v.Name.id);
1423 PyErr_SyntaxLocationObject(st->st_filename,
1424 e->lineno,
1425 e->col_offset);
1426 VISIT_QUIT(st, 0);
1427 }
1428 }
1429
1430 /* We should always find either a FunctionBlock, ModuleBlock or ClassBlock
1431 and should never fall to this case
1432 */
1433 assert(0);
1434 return 0;
1435}
1436
1437static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001438symtable_visit_expr(struct symtable *st, expr_ty e)
1439{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001440 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001441 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001442 "maximum recursion depth exceeded during compilation");
1443 VISIT_QUIT(st, 0);
1444 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001445 switch (e->kind) {
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001446 case NamedExpr_kind:
1447 VISIT(st, expr, e->v.NamedExpr.value);
1448 VISIT(st, expr, e->v.NamedExpr.target);
1449 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001450 case BoolOp_kind:
1451 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1452 break;
1453 case BinOp_kind:
1454 VISIT(st, expr, e->v.BinOp.left);
1455 VISIT(st, expr, e->v.BinOp.right);
1456 break;
1457 case UnaryOp_kind:
1458 VISIT(st, expr, e->v.UnaryOp.operand);
1459 break;
1460 case Lambda_kind: {
1461 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001462 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001463 if (e->v.Lambda.args->defaults)
1464 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001465 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001466 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001468 FunctionBlock, (void *)e, e->lineno,
1469 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001470 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001471 VISIT(st, arguments, e->v.Lambda.args);
1472 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001473 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001474 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001475 break;
1476 }
1477 case IfExp_kind:
1478 VISIT(st, expr, e->v.IfExp.test);
1479 VISIT(st, expr, e->v.IfExp.body);
1480 VISIT(st, expr, e->v.IfExp.orelse);
1481 break;
1482 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001483 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 VISIT_SEQ(st, expr, e->v.Dict.values);
1485 break;
1486 case Set_kind:
1487 VISIT_SEQ(st, expr, e->v.Set.elts);
1488 break;
1489 case GeneratorExp_kind:
1490 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001491 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001492 break;
1493 case ListComp_kind:
1494 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001495 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001496 break;
1497 case SetComp_kind:
1498 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001499 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001500 break;
1501 case DictComp_kind:
1502 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001503 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 break;
1505 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001506 if (e->v.Yield.value)
1507 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001508 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001510 case YieldFrom_kind:
1511 VISIT(st, expr, e->v.YieldFrom.value);
1512 st->st_cur->ste_generator = 1;
1513 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001514 case Await_kind:
1515 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001516 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001517 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518 case Compare_kind:
1519 VISIT(st, expr, e->v.Compare.left);
1520 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1521 break;
1522 case Call_kind:
1523 VISIT(st, expr, e->v.Call.func);
1524 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001525 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001527 case FormattedValue_kind:
1528 VISIT(st, expr, e->v.FormattedValue.value);
1529 if (e->v.FormattedValue.format_spec)
1530 VISIT(st, expr, e->v.FormattedValue.format_spec);
1531 break;
1532 case JoinedStr_kind:
1533 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1534 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001535 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 /* Nothing to do here. */
1537 break;
1538 /* The following exprs can be assignment targets. */
1539 case Attribute_kind:
1540 VISIT(st, expr, e->v.Attribute.value);
1541 break;
1542 case Subscript_kind:
1543 VISIT(st, expr, e->v.Subscript.value);
1544 VISIT(st, slice, e->v.Subscript.slice);
1545 break;
1546 case Starred_kind:
1547 VISIT(st, expr, e->v.Starred.value);
1548 break;
1549 case Name_kind:
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001550 /* Special-case: named expr */
1551 if (e->v.Name.ctx == NamedStore && st->st_cur->ste_comprehension) {
1552 if(!symtable_extend_namedexpr_scope(st, e))
1553 VISIT_QUIT(st, 0);
1554 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001555 if (!symtable_add_def(st, e->v.Name.id,
1556 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001557 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 /* Special-case super: it counts as a use of __class__ */
1559 if (e->v.Name.ctx == Load &&
1560 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001561 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001562 if (!GET_IDENTIFIER(__class__) ||
1563 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001564 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001565 }
1566 break;
1567 /* child nodes of List and Tuple will have expr_context set */
1568 case List_kind:
1569 VISIT_SEQ(st, expr, e->v.List.elts);
1570 break;
1571 case Tuple_kind:
1572 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1573 break;
1574 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001575 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001576}
1577
1578static int
1579symtable_implicit_arg(struct symtable *st, int pos)
1580{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1582 if (id == NULL)
1583 return 0;
1584 if (!symtable_add_def(st, id, DEF_PARAM)) {
1585 Py_DECREF(id);
1586 return 0;
1587 }
1588 Py_DECREF(id);
1589 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590}
1591
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001592static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001593symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597 if (!args)
1598 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001599
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 for (i = 0; i < asdl_seq_LEN(args); i++) {
1601 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1602 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1603 return 0;
1604 }
1605
1606 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607}
1608
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001609static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001610symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001611{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001612 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614 if (!args)
1615 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001616
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 for (i = 0; i < asdl_seq_LEN(args); i++) {
1618 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1619 if (arg->annotation)
1620 VISIT(st, expr, arg->annotation);
1621 }
1622
1623 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001624}
1625
Neal Norwitzc1505362006-12-28 06:47:50 +00001626static int
Yury Selivanov75445082015-05-11 22:57:16 -04001627symtable_visit_annotations(struct symtable *st, stmt_ty s,
1628 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001629{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001630 if (a->args && !symtable_visit_argannotations(st, a->args))
1631 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001632 if (a->vararg && a->vararg->annotation)
1633 VISIT(st, expr, a->vararg->annotation);
1634 if (a->kwarg && a->kwarg->annotation)
1635 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001636 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1637 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001638 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001639 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001640 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001641}
1642
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001643static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001644symtable_visit_arguments(struct symtable *st, arguments_ty a)
1645{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001646 /* skip default arguments inside function block
1647 XXX should ast be different?
1648 */
1649 if (a->args && !symtable_visit_params(st, a->args))
1650 return 0;
1651 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1652 return 0;
1653 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001654 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001655 return 0;
1656 st->st_cur->ste_varargs = 1;
1657 }
1658 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001659 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660 return 0;
1661 st->st_cur->ste_varkeywords = 1;
1662 }
1663 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001664}
1665
1666
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 if (eh->v.ExceptHandler.type)
1671 VISIT(st, expr, eh->v.ExceptHandler.type);
1672 if (eh->v.ExceptHandler.name)
1673 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1674 return 0;
1675 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1676 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001677}
1678
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001679static int
1680symtable_visit_withitem(struct symtable *st, withitem_ty item)
1681{
1682 VISIT(st, expr, item->context_expr);
1683 if (item->optional_vars) {
1684 VISIT(st, expr, item->optional_vars);
1685 }
1686 return 1;
1687}
1688
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001689
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001690static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001691symtable_visit_alias(struct symtable *st, alias_ty a)
1692{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001693 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001694 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001695 dotted package name (e.g. spam.eggs)
1696 */
1697 PyObject *store_name;
1698 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001699 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1700 PyUnicode_GET_LENGTH(name), 1);
1701 if (dot != -1) {
1702 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 if (!store_name)
1704 return 0;
1705 }
1706 else {
1707 store_name = name;
1708 Py_INCREF(store_name);
1709 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001710 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1712 Py_DECREF(store_name);
1713 return r;
1714 }
1715 else {
1716 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001717 int lineno = st->st_cur->ste_lineno;
1718 int col_offset = st->st_cur->ste_col_offset;
1719 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001720 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001721 Py_DECREF(store_name);
1722 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001723 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 Py_DECREF(store_name);
1725 return 1;
1726 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001727}
1728
1729
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001731symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1732{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001733 VISIT(st, expr, lc->target);
1734 VISIT(st, expr, lc->iter);
1735 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001736 if (lc->is_async) {
1737 st->st_cur->ste_coroutine = 1;
1738 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001739 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_keyword(struct symtable *st, keyword_ty k)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 VISIT(st, expr, k->value);
1747 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001748}
1749
1750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001752symtable_visit_slice(struct symtable *st, slice_ty s)
1753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 switch (s->kind) {
1755 case Slice_kind:
1756 if (s->v.Slice.lower)
1757 VISIT(st, expr, s->v.Slice.lower)
1758 if (s->v.Slice.upper)
1759 VISIT(st, expr, s->v.Slice.upper)
1760 if (s->v.Slice.step)
1761 VISIT(st, expr, s->v.Slice.step)
1762 break;
1763 case ExtSlice_kind:
1764 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1765 break;
1766 case Index_kind:
1767 VISIT(st, expr, s->v.Index.value)
1768 break;
1769 }
1770 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001771}
1772
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001773static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001774symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001775 identifier scope_name, asdl_seq *generators,
1776 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001777{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001778 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001779 comprehension_ty outermost = ((comprehension_ty)
1780 asdl_seq_GET(generators, 0));
1781 /* Outermost iterator is evaluated in current scope */
1782 VISIT(st, expr, outermost->iter);
1783 /* Create comprehension scope for the rest */
1784 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001785 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1786 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001787 return 0;
1788 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001789 if (outermost->is_async) {
1790 st->st_cur->ste_coroutine = 1;
1791 }
Emily Morehouse8f59ee02019-01-24 16:49:56 -07001792 st->st_cur->ste_comprehension = 1;
1793
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001794 /* Outermost iter is received as an argument */
1795 if (!symtable_implicit_arg(st, 0)) {
1796 symtable_exit_block(st, (void *)e);
1797 return 0;
1798 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001799 VISIT(st, expr, outermost->target);
1800 VISIT_SEQ(st, expr, outermost->ifs);
1801 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001802 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001803 VISIT(st, expr, value);
1804 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001805 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001806 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001807 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1808 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1809 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1810 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001811 PyErr_SyntaxLocationObject(st->st_filename,
1812 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001813 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001814 symtable_exit_block(st, (void *)e);
1815 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001816 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001817 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001818 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001819}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001821static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001822symtable_visit_genexp(struct symtable *st, expr_ty e)
1823{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001824 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1825 e->v.GeneratorExp.generators,
1826 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001827}
1828
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001829static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001830symtable_visit_listcomp(struct symtable *st, expr_ty e)
1831{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001832 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1833 e->v.ListComp.generators,
1834 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001835}
1836
1837static int
1838symtable_visit_setcomp(struct symtable *st, expr_ty e)
1839{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001840 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1841 e->v.SetComp.generators,
1842 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001843}
1844
1845static int
1846symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1847{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001848 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1849 e->v.DictComp.generators,
1850 e->v.DictComp.key,
1851 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001852}