blob: 677b6043438ea72889ec9e33cf1d1f028619decb [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
Neal Norwitz090b3dd2006-02-28 22:36:46 +000034static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000035ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000036 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000037{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000038 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020039 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000040
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 k = PyLong_FromVoidPtr(key);
42 if (k == NULL)
43 goto fail;
44 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020045 if (ste == NULL) {
46 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000047 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020048 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000049 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020050 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000052 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020053 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000054
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000055 ste->ste_symbols = NULL;
56 ste->ste_varnames = NULL;
57 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Benjamin Petersond9c87022012-10-31 20:26:20 -040059 ste->ste_directives = NULL;
60
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000061 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 ste->ste_nested = 0;
63 ste->ste_free = 0;
64 ste->ste_varargs = 0;
65 ste->ste_varkeywords = 0;
66 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000067 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000068 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000069 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000070
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000071 if (st->st_cur != NULL &&
72 (st->st_cur->ste_nested ||
73 st->st_cur->ste_type == FunctionBlock))
74 ste->ste_nested = 1;
75 ste->ste_child_free = 0;
76 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070077 ste->ste_coroutine = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000078 ste->ste_returns_value = 0;
Benjamin Peterson312595c2013-05-15 15:26:42 -050079 ste->ste_needs_class_closure = 0;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000080
Victor Stinner9a4fb662013-07-11 22:49:00 +020081 ste->ste_symbols = PyDict_New();
82 ste->ste_varnames = PyList_New(0);
83 ste->ste_children = PyList_New(0);
84 if (ste->ste_symbols == NULL
85 || ste->ste_varnames == NULL
86 || ste->ste_children == NULL)
87 goto fail;
88
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000089 if (PyDict_SetItem(st->st_blocks, ste->ste_id, (PyObject *)ste) < 0)
90 goto fail;
91
92 return ste;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000093 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000094 Py_XDECREF(ste);
95 return NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000096}
97
98static PyObject *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000099ste_repr(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000100{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000101 return PyUnicode_FromFormat("<symtable entry %U(%ld), line %d>",
102 ste->ste_name,
103 PyLong_AS_LONG(ste->ste_id), ste->ste_lineno);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000104}
105
106static void
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000107ste_dealloc(PySTEntryObject *ste)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000108{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000109 ste->ste_table = NULL;
110 Py_XDECREF(ste->ste_id);
111 Py_XDECREF(ste->ste_name);
112 Py_XDECREF(ste->ste_symbols);
113 Py_XDECREF(ste->ste_varnames);
114 Py_XDECREF(ste->ste_children);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400115 Py_XDECREF(ste->ste_directives);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject_Del(ste);
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000117}
118
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000119#define OFF(x) offsetof(PySTEntryObject, x)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000120
Guido van Rossum6f799372001-09-20 20:46:19 +0000121static PyMemberDef ste_memberlist[] = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000122 {"id", T_OBJECT, OFF(ste_id), READONLY},
123 {"name", T_OBJECT, OFF(ste_name), READONLY},
124 {"symbols", T_OBJECT, OFF(ste_symbols), READONLY},
125 {"varnames", T_OBJECT, OFF(ste_varnames), READONLY},
126 {"children", T_OBJECT, OFF(ste_children), READONLY},
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000127 {"nested", T_INT, OFF(ste_nested), READONLY},
128 {"type", T_INT, OFF(ste_type), READONLY},
129 {"lineno", T_INT, OFF(ste_lineno), READONLY},
130 {NULL}
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000131};
132
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000133PyTypeObject PySTEntry_Type = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 PyVarObject_HEAD_INIT(&PyType_Type, 0)
135 "symtable entry",
136 sizeof(PySTEntryObject),
137 0,
138 (destructor)ste_dealloc, /* tp_dealloc */
139 0, /* tp_print */
140 0, /* tp_getattr */
141 0, /* tp_setattr */
142 0, /* tp_reserved */
143 (reprfunc)ste_repr, /* tp_repr */
144 0, /* tp_as_number */
145 0, /* tp_as_sequence */
146 0, /* tp_as_mapping */
147 0, /* tp_hash */
148 0, /* tp_call */
149 0, /* tp_str */
150 PyObject_GenericGetAttr, /* tp_getattro */
151 0, /* tp_setattro */
152 0, /* tp_as_buffer */
153 Py_TPFLAGS_DEFAULT, /* tp_flags */
154 0, /* tp_doc */
155 0, /* tp_traverse */
156 0, /* tp_clear */
157 0, /* tp_richcompare */
158 0, /* tp_weaklistoffset */
159 0, /* tp_iter */
160 0, /* tp_iternext */
161 0, /* tp_methods */
162 ste_memberlist, /* tp_members */
163 0, /* tp_getset */
164 0, /* tp_base */
165 0, /* tp_dict */
166 0, /* tp_descr_get */
167 0, /* tp_descr_set */
168 0, /* tp_dictoffset */
169 0, /* tp_init */
170 0, /* tp_alloc */
171 0, /* tp_new */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +0000172};
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000173
174static int symtable_analyze(struct symtable *st);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175static int symtable_enter_block(struct symtable *st, identifier name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000176 _Py_block_ty block, void *ast, int lineno,
177 int col_offset);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000178static int symtable_exit_block(struct symtable *st, void *ast);
179static int symtable_visit_stmt(struct symtable *st, stmt_ty s);
180static int symtable_visit_expr(struct symtable *st, expr_ty s);
181static int symtable_visit_genexp(struct symtable *st, expr_ty s);
Nick Coghlan650f0d02007-04-15 12:05:43 +0000182static int symtable_visit_listcomp(struct symtable *st, expr_ty s);
183static int symtable_visit_setcomp(struct symtable *st, expr_ty s);
Guido van Rossum992d4a32007-07-11 13:09:30 +0000184static int symtable_visit_dictcomp(struct symtable *st, expr_ty s);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000185static int symtable_visit_arguments(struct symtable *st, arguments_ty);
186static int symtable_visit_excepthandler(struct symtable *st, excepthandler_ty);
187static int symtable_visit_alias(struct symtable *st, alias_ty);
188static int symtable_visit_comprehension(struct symtable *st, comprehension_ty);
189static int symtable_visit_keyword(struct symtable *st, keyword_ty);
190static int symtable_visit_slice(struct symtable *st, slice_ty);
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000191static int symtable_visit_params(struct symtable *st, asdl_seq *args);
192static int symtable_visit_argannotations(struct symtable *st, asdl_seq *args);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000193static int symtable_implicit_arg(struct symtable *st, int pos);
Yury Selivanov75445082015-05-11 22:57:16 -0400194static int symtable_visit_annotations(struct symtable *st, stmt_ty s, arguments_ty, expr_ty);
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -0500195static int symtable_visit_withitem(struct symtable *st, withitem_ty item);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000196
197
Nick Coghlan650f0d02007-04-15 12:05:43 +0000198static identifier top = NULL, lambda = NULL, genexpr = NULL,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000199 listcomp = NULL, setcomp = NULL, dictcomp = NULL,
Benjamin Petersone8e14592013-05-16 14:37:25 -0500200 __class__ = NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000201
202#define GET_IDENTIFIER(VAR) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000203 ((VAR) ? (VAR) : ((VAR) = PyUnicode_InternFromString(# VAR)))
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000204
205#define DUPLICATE_ARGUMENT \
Neal Norwitza5d16a32007-08-24 22:53:58 +0000206"duplicate argument '%U' in function definition"
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000207
208static struct symtable *
209symtable_new(void)
210{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 struct symtable *st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000212
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 st = (struct symtable *)PyMem_Malloc(sizeof(struct symtable));
Zackery Spytzad65f152018-11-16 08:58:55 -0700214 if (st == NULL) {
215 PyErr_NoMemory();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000216 return NULL;
Zackery Spytzad65f152018-11-16 08:58:55 -0700217 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 st->st_filename = NULL;
220 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000221
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 if ((st->st_stack = PyList_New(0)) == NULL)
223 goto fail;
224 if ((st->st_blocks = PyDict_New()) == NULL)
225 goto fail;
226 st->st_cur = NULL;
227 st->st_private = NULL;
228 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000229 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000230 PySymtable_Free(st);
231 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000232}
233
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000234/* When compiling the use of C stack is probably going to be a lot
235 lighter than when executing Python code but still can overflow
236 and causing a Python crash if not checked (e.g. eval("()"*300000)).
237 Using the current recursion limit for the compiler seems too
238 restrictive (it caused at least one test to fail) so a factor is
239 used to allow deeper recursion when compiling an expression.
240
241 Using a scaling factor means this should automatically adjust when
242 the recursion limit is adjusted for small or large C stack allocations.
243*/
244#define COMPILER_STACK_FRAME_SCALE 3
245
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000246struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200247PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000249 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 asdl_seq *seq;
251 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000252 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400253 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000254
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200256 return NULL;
257 if (filename == NULL) {
258 PySymtable_Free(st);
259 return NULL;
260 }
261 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000262 st->st_filename = filename;
263 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000264
265 /* Setup recursion depth check counters */
Victor Stinner50b48572018-11-01 01:51:40 +0100266 tstate = _PyThreadState_GET();
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000267 if (!tstate) {
268 PySymtable_Free(st);
269 return NULL;
270 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400271 /* Be careful here to prevent overflow. */
272 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
273 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
274 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
275 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000276
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000277 /* Make the initial symbol information gathering pass */
278 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000279 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 PySymtable_Free(st);
281 return NULL;
282 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000283
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000285 switch (mod->kind) {
286 case Module_kind:
287 seq = mod->v.Module.body;
288 for (i = 0; i < asdl_seq_LEN(seq); i++)
289 if (!symtable_visit_stmt(st,
290 (stmt_ty)asdl_seq_GET(seq, i)))
291 goto error;
292 break;
293 case Expression_kind:
294 if (!symtable_visit_expr(st, mod->v.Expression.body))
295 goto error;
296 break;
297 case Interactive_kind:
298 seq = mod->v.Interactive.body;
299 for (i = 0; i < asdl_seq_LEN(seq); i++)
300 if (!symtable_visit_stmt(st,
301 (stmt_ty)asdl_seq_GET(seq, i)))
302 goto error;
303 break;
304 case Suite_kind:
305 PyErr_SetString(PyExc_RuntimeError,
306 "this compiler does not handle Suites");
307 goto error;
308 }
309 if (!symtable_exit_block(st, (void *)mod)) {
310 PySymtable_Free(st);
311 return NULL;
312 }
313 /* Make the second symbol analysis pass */
314 if (symtable_analyze(st))
315 return st;
316 PySymtable_Free(st);
317 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000318 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000319 (void) symtable_exit_block(st, (void *)mod);
320 PySymtable_Free(st);
321 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000322}
323
Victor Stinner14e461d2013-08-26 22:28:21 +0200324struct symtable *
325PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
326{
327 PyObject *filename;
328 struct symtable *st;
329 filename = PyUnicode_DecodeFSDefault(filename_str);
330 if (filename == NULL)
331 return NULL;
332 st = PySymtable_BuildObject(mod, filename, future);
333 Py_DECREF(filename);
334 return st;
335}
336
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000337void
338PySymtable_Free(struct symtable *st)
339{
Victor Stinner14e461d2013-08-26 22:28:21 +0200340 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 Py_XDECREF(st->st_blocks);
342 Py_XDECREF(st->st_stack);
343 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000344}
345
346PySTEntryObject *
347PySymtable_Lookup(struct symtable *st, void *key)
348{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000349 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000350
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 k = PyLong_FromVoidPtr(key);
352 if (k == NULL)
353 return NULL;
354 v = PyDict_GetItem(st->st_blocks, k);
355 if (v) {
356 assert(PySTEntry_Check(v));
357 Py_INCREF(v);
358 }
359 else {
360 PyErr_SetString(PyExc_KeyError,
361 "unknown symbol table entry");
362 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000363
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 Py_DECREF(k);
365 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000366}
367
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000368int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000369PyST_GetScope(PySTEntryObject *ste, PyObject *name)
370{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000371 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
372 if (!v)
373 return 0;
374 assert(PyLong_Check(v));
375 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000376}
377
Benjamin Petersond9c87022012-10-31 20:26:20 -0400378static int
379error_at_directive(PySTEntryObject *ste, PyObject *name)
380{
381 Py_ssize_t i;
382 PyObject *data;
383 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600384 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400385 data = PyList_GET_ITEM(ste->ste_directives, i);
386 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600387 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
388 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
389 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
390 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
Ammar Askar025eb982018-09-24 17:12:49 -0400391 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600392
393 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700394 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400395 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600396 PyErr_SetString(PyExc_RuntimeError,
397 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400398 return 0;
399}
400
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000401
402/* Analyze raw symbol information to determine scope of each name.
403
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000404 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000406 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410 explicit global is declared with the global statement. An implicit
411 global is a free variable for which the compiler has found no binding
412 in an enclosing function scope. The implicit global is either a global
413 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
414 to handle these names to implement slightly odd semantics. In such a
415 block, the name is treated as global until it is assigned to; then it
416 is treated as a local.
417
418 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000419 The first pass collects raw facts from the AST via the symtable_visit_*
420 functions: the name is a parameter here, the name is used but not defined
421 here, etc. The second pass analyzes these facts during a pass over the
422 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000423
424 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000425 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000426 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000427 Names which are explicitly declared nonlocal must exist in this set of
428 visible names - if they do not, a syntax error is raised. After doing
429 the local analysis, it analyzes each of its child blocks using an
430 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000431
Nick Coghlan650f0d02007-04-15 12:05:43 +0000432 The children update the free variable set. If a local variable is added to
433 the free variable set by the child, the variable is marked as a cell. The
434 function object being defined must provide runtime storage for the variable
435 that may outlive the function's frame. Cell variables are removed from the
436 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000437
Nick Coghlan650f0d02007-04-15 12:05:43 +0000438 During analysis, the names are:
439 symbols: dict mapping from symbol names to flag values (including offset scope values)
440 scopes: dict mapping from symbol names to scope values (no offset)
441 local: set of all symbol names local to the current scope
442 bound: set of all symbol names local to a containing function scope
443 free: set of all symbol names referenced but not bound in child scopes
444 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000445*/
446
447#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000448 PyObject *o = PyLong_FromLong(I); \
449 if (!o) \
450 return 0; \
451 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
452 Py_DECREF(o); \
453 return 0; \
454 } \
455 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000456}
457
458/* Decide on scope of name, given flags.
459
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000460 The namespace dictionaries may be modified to record information
461 about the new name. For example, a new global will add an entry to
462 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000463*/
464
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000465static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000466analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 PyObject *bound, PyObject *local, PyObject *free,
468 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000469{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000470 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000471 if (flags & DEF_NONLOCAL) {
472 PyErr_Format(PyExc_SyntaxError,
473 "name '%U' is nonlocal and global",
474 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400475 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000476 }
477 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
478 if (PySet_Add(global, name) < 0)
479 return 0;
480 if (bound && (PySet_Discard(bound, name) < 0))
481 return 0;
482 return 1;
483 }
484 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 if (!bound) {
486 PyErr_Format(PyExc_SyntaxError,
487 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400488 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 }
490 if (!PySet_Contains(bound, name)) {
491 PyErr_Format(PyExc_SyntaxError,
492 "no binding for nonlocal '%U' found",
493 name);
494
Benjamin Petersond9c87022012-10-31 20:26:20 -0400495 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000496 }
497 SET_SCOPE(scopes, name, FREE);
498 ste->ste_free = 1;
499 return PySet_Add(free, name) >= 0;
500 }
501 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000502 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000503 if (PySet_Add(local, name) < 0)
504 return 0;
505 if (PySet_Discard(global, name) < 0)
506 return 0;
507 return 1;
508 }
509 /* If an enclosing block has a binding for this name, it
510 is a free variable rather than a global variable.
511 Note that having a non-NULL bound implies that the block
512 is nested.
513 */
514 if (bound && PySet_Contains(bound, name)) {
515 SET_SCOPE(scopes, name, FREE);
516 ste->ste_free = 1;
517 return PySet_Add(free, name) >= 0;
518 }
519 /* If a parent has a global statement, then call it global
520 explicit? It could also be global implicit.
521 */
522 if (global && PySet_Contains(global, name)) {
523 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
524 return 1;
525 }
526 if (ste->ste_nested)
527 ste->ste_free = 1;
528 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
529 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000530}
531
532#undef SET_SCOPE
533
534/* If a name is defined in free and also in locals, then this block
535 provides the binding for the free variable. The name should be
536 marked CELL in this block and removed from the free list.
537
538 Note that the current block's free variables are included in free.
539 That's safe because no name can be free and local in the same scope.
540*/
541
542static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500543analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000544{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545 PyObject *name, *v, *v_cell;
546 int success = 0;
547 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000548
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 v_cell = PyLong_FromLong(CELL);
550 if (!v_cell)
551 return 0;
552 while (PyDict_Next(scopes, &pos, &name, &v)) {
553 long scope;
554 assert(PyLong_Check(v));
555 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000556 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 continue;
558 if (!PySet_Contains(free, name))
559 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000560 /* Replace LOCAL with CELL for this name, and remove
561 from free. It is safe to replace the value of name
562 in the dict, because it will not cause a resize.
563 */
564 if (PyDict_SetItem(scopes, name, v_cell) < 0)
565 goto error;
566 if (PySet_Discard(free, name) < 0)
567 goto error;
568 }
569 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000570 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000571 Py_DECREF(v_cell);
572 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000573}
574
Benjamin Peterson312595c2013-05-15 15:26:42 -0500575static int
576drop_class_free(PySTEntryObject *ste, PyObject *free)
577{
578 int res;
579 if (!GET_IDENTIFIER(__class__))
580 return 0;
581 res = PySet_Discard(free, __class__);
582 if (res < 0)
583 return 0;
584 if (res)
585 ste->ste_needs_class_closure = 1;
586 return 1;
587}
588
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000589/* Enter the final scope information into the ste_symbols dict.
590 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000591 * All arguments are dicts. Modifies symbols, others are read-only.
592*/
593static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000594update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000595 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000596{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000597 PyObject *name = NULL, *itr = NULL;
598 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
599 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000600
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000601 /* Update scope information for all symbols in this scope */
602 while (PyDict_Next(symbols, &pos, &name, &v)) {
603 long scope, flags;
604 assert(PyLong_Check(v));
605 flags = PyLong_AS_LONG(v);
606 v_scope = PyDict_GetItem(scopes, name);
607 assert(v_scope && PyLong_Check(v_scope));
608 scope = PyLong_AS_LONG(v_scope);
609 flags |= (scope << SCOPE_OFFSET);
610 v_new = PyLong_FromLong(flags);
611 if (!v_new)
612 return 0;
613 if (PyDict_SetItem(symbols, name, v_new) < 0) {
614 Py_DECREF(v_new);
615 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000616 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000617 Py_DECREF(v_new);
618 }
619
620 /* Record not yet resolved free variables from children (if any) */
621 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
622 if (!v_free)
623 return 0;
624
625 itr = PyObject_GetIter(free);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600626 if (itr == NULL) {
627 Py_DECREF(v_free);
628 return 0;
629 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000630
631 while ((name = PyIter_Next(itr))) {
632 v = PyDict_GetItem(symbols, name);
633
634 /* Handle symbol that already exists in this scope */
635 if (v) {
636 /* Handle a free variable in a method of
637 the class that has the same name as a local
638 or global in the class scope.
639 */
640 if (classflag &&
641 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
642 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
643 v_new = PyLong_FromLong(flags);
644 if (!v_new) {
645 goto error;
646 }
647 if (PyDict_SetItem(symbols, name, v_new) < 0) {
648 Py_DECREF(v_new);
649 goto error;
650 }
651 Py_DECREF(v_new);
652 }
653 /* It's a cell, or already free in this scope */
654 Py_DECREF(name);
655 continue;
656 }
657 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200658 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000659 Py_DECREF(name);
660 continue; /* it's a global */
661 }
662 /* Propagate new free symbol up the lexical stack */
663 if (PyDict_SetItem(symbols, name, v_free) < 0) {
664 goto error;
665 }
666 Py_DECREF(name);
667 }
668 Py_DECREF(itr);
669 Py_DECREF(v_free);
670 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000671error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000672 Py_XDECREF(v_free);
673 Py_XDECREF(itr);
674 Py_XDECREF(name);
675 return 0;
676}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000677
678/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000679
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000680 Arguments:
681 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000682 bound -- set of variables bound in enclosing scopes (input). bound
683 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000684 free -- set of free variables in enclosed scopes (output)
685 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000686
687 The implementation uses two mutually recursive functions,
688 analyze_block() and analyze_child_block(). analyze_block() is
689 responsible for analyzing the individual names defined in a block.
690 analyze_child_block() prepares temporary namespace dictionaries
691 used to evaluated nested blocks.
692
693 The two functions exist because a child block should see the name
694 bindings of its enclosing blocks, but those bindings should not
695 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000696*/
697
698static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000699analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
700 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000701
702static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000703analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
704 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
707 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
708 PyObject *temp;
709 int i, success = 0;
710 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000711
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000712 local = PySet_New(NULL); /* collect new names bound in block */
713 if (!local)
714 goto error;
715 scopes = PyDict_New(); /* collect scopes defined for each name */
716 if (!scopes)
717 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000718
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000719 /* Allocate new global and bound variable dictionaries. These
720 dictionaries hold the names visible in nested blocks. For
721 ClassBlocks, the bound and global names are initialized
722 before analyzing names, because class bindings aren't
723 visible in methods. For other blocks, they are initialized
724 after names are analyzed.
725 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000726
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 /* TODO(jhylton): Package these dicts in a struct so that we
728 can write reasonable helper functions?
729 */
730 newglobal = PySet_New(NULL);
731 if (!newglobal)
732 goto error;
733 newfree = PySet_New(NULL);
734 if (!newfree)
735 goto error;
736 newbound = PySet_New(NULL);
737 if (!newbound)
738 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000739
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 /* Class namespace has no effect on names visible in
741 nested functions, so populate the global and bound
742 sets to be passed to child blocks before analyzing
743 this one.
744 */
745 if (ste->ste_type == ClassBlock) {
746 /* Pass down known globals */
747 temp = PyNumber_InPlaceOr(newglobal, global);
748 if (!temp)
749 goto error;
750 Py_DECREF(temp);
751 /* Pass down previously bound symbols */
752 if (bound) {
753 temp = PyNumber_InPlaceOr(newbound, bound);
754 if (!temp)
755 goto error;
756 Py_DECREF(temp);
757 }
758 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000759
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000760 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
761 long flags = PyLong_AS_LONG(v);
762 if (!analyze_name(ste, scopes, name, flags,
763 bound, local, free, global))
764 goto error;
765 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000766
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000767 /* Populate global and bound sets to be passed to children. */
768 if (ste->ste_type != ClassBlock) {
769 /* Add function locals to bound set */
770 if (ste->ste_type == FunctionBlock) {
771 temp = PyNumber_InPlaceOr(newbound, local);
772 if (!temp)
773 goto error;
774 Py_DECREF(temp);
775 }
776 /* Pass down previously bound symbols */
777 if (bound) {
778 temp = PyNumber_InPlaceOr(newbound, bound);
779 if (!temp)
780 goto error;
781 Py_DECREF(temp);
782 }
783 /* Pass down known globals */
784 temp = PyNumber_InPlaceOr(newglobal, global);
785 if (!temp)
786 goto error;
787 Py_DECREF(temp);
788 }
789 else {
790 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000791 if (!GET_IDENTIFIER(__class__))
792 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000793 if (PySet_Add(newbound, __class__) < 0)
794 goto error;
795 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000796
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300797 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000798
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000799 newbound, newglobal now contain the names visible in
800 nested blocks. The free variables in the children will
801 be collected in allfree.
802 */
803 allfree = PySet_New(NULL);
804 if (!allfree)
805 goto error;
806 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
807 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
808 PySTEntryObject* entry;
809 assert(c && PySTEntry_Check(c));
810 entry = (PySTEntryObject*)c;
811 if (!analyze_child_block(entry, newbound, newfree, newglobal,
812 allfree))
813 goto error;
814 /* Check if any children have free variables */
815 if (entry->ste_free || entry->ste_child_free)
816 ste->ste_child_free = 1;
817 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000818
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 temp = PyNumber_InPlaceOr(newfree, allfree);
820 if (!temp)
821 goto error;
822 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000823
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000824 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500825 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500827 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 goto error;
829 /* Records the results of the analysis in the symbol table entry */
830 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
831 ste->ste_type == ClassBlock))
832 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000833
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000834 temp = PyNumber_InPlaceOr(free, newfree);
835 if (!temp)
836 goto error;
837 Py_DECREF(temp);
838 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000839 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 Py_XDECREF(scopes);
841 Py_XDECREF(local);
842 Py_XDECREF(newbound);
843 Py_XDECREF(newglobal);
844 Py_XDECREF(newfree);
845 Py_XDECREF(allfree);
846 if (!success)
847 assert(PyErr_Occurred());
848 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000849}
850
851static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
853 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000854{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000855 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
856 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000857
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000858 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000859
Martin Panter3ee62702016-06-04 04:57:19 +0000860 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000861 current block. The analyze_block() call modifies these
862 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000863
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000864 */
865 temp_bound = PySet_New(bound);
866 if (!temp_bound)
867 goto error;
868 temp_free = PySet_New(free);
869 if (!temp_free)
870 goto error;
871 temp_global = PySet_New(global);
872 if (!temp_global)
873 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000874
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000875 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
876 goto error;
877 temp = PyNumber_InPlaceOr(child_free, temp_free);
878 if (!temp)
879 goto error;
880 Py_DECREF(temp);
881 Py_DECREF(temp_bound);
882 Py_DECREF(temp_free);
883 Py_DECREF(temp_global);
884 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000885 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000886 Py_XDECREF(temp_bound);
887 Py_XDECREF(temp_free);
888 Py_XDECREF(temp_global);
889 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000890}
891
892static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000893symtable_analyze(struct symtable *st)
894{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000895 PyObject *free, *global;
896 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000897
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000898 free = PySet_New(NULL);
899 if (!free)
900 return 0;
901 global = PySet_New(NULL);
902 if (!global) {
903 Py_DECREF(free);
904 return 0;
905 }
906 r = analyze_block(st->st_top, NULL, free, global);
907 Py_DECREF(free);
908 Py_DECREF(global);
909 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000910}
911
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000912/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000913 This reference is released when the block is exited, via the DECREF
914 in symtable_exit_block().
915*/
916
917static int
918symtable_exit_block(struct symtable *st, void *ast)
919{
Benjamin Peterson609da582011-06-29 22:52:39 -0500920 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000921
Benjamin Peterson609da582011-06-29 22:52:39 -0500922 st->st_cur = NULL;
923 size = PyList_GET_SIZE(st->st_stack);
924 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500925 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000926 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500927 if (--size)
928 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000929 }
930 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000931}
932
933static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000934symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000935 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000936{
Benjamin Peterson609da582011-06-29 22:52:39 -0500937 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938
Benjamin Peterson609da582011-06-29 22:52:39 -0500939 ste = ste_new(st, name, block, ast, lineno, col_offset);
940 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000941 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500942 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
943 Py_DECREF(ste);
944 return 0;
945 }
946 prev = st->st_cur;
947 /* The entry is owned by the stack. Borrow it for st_cur. */
948 Py_DECREF(ste);
949 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000950 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000951 st->st_global = st->st_cur->ste_symbols;
952 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500953 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000954 return 0;
955 }
956 }
957 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000958}
959
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000960static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000961symtable_lookup(struct symtable *st, PyObject *name)
962{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000963 PyObject *o;
964 PyObject *mangled = _Py_Mangle(st->st_private, name);
965 if (!mangled)
966 return 0;
967 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
968 Py_DECREF(mangled);
969 if (!o)
970 return 0;
971 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000972}
973
974static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000976{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977 PyObject *o;
978 PyObject *dict;
979 long val;
980 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000981
Jeremy Hylton81e95022007-02-27 06:50:52 +0000982
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000983 if (!mangled)
984 return 0;
985 dict = st->st_cur->ste_symbols;
986 if ((o = PyDict_GetItem(dict, mangled))) {
987 val = PyLong_AS_LONG(o);
988 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
989 /* Is it better to use 'mangled' or 'name' here? */
990 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200991 PyErr_SyntaxLocationObject(st->st_filename,
992 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -0400993 st->st_cur->ste_col_offset + 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000994 goto error;
995 }
996 val |= flag;
997 } else
998 val = flag;
999 o = PyLong_FromLong(val);
1000 if (o == NULL)
1001 goto error;
1002 if (PyDict_SetItem(dict, mangled, o) < 0) {
1003 Py_DECREF(o);
1004 goto error;
1005 }
1006 Py_DECREF(o);
1007
1008 if (flag & DEF_PARAM) {
1009 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1010 goto error;
1011 } else if (flag & DEF_GLOBAL) {
1012 /* XXX need to update DEF_GLOBAL for other flags too;
1013 perhaps only DEF_FREE_GLOBAL */
1014 val = flag;
1015 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1016 val |= PyLong_AS_LONG(o);
1017 }
1018 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001019 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 goto error;
1021 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1022 Py_DECREF(o);
1023 goto error;
1024 }
1025 Py_DECREF(o);
1026 }
1027 Py_DECREF(mangled);
1028 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001029
1030error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001031 Py_DECREF(mangled);
1032 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001033}
1034
1035/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1036 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001037 function.
1038
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001039 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1040 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001041
1042 VISIT_QUIT macro returns the specified value exiting from the function but
1043 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001044*/
1045
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001046#define VISIT_QUIT(ST, X) \
1047 return --(ST)->recursion_depth,(X)
1048
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001049#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001050 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001051 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001052
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001053#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001054 int i; \
1055 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1056 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1057 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1058 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001059 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001060 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001061}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001062
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001064 int i; \
1065 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1066 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1067 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1068 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001069 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001070 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001071}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001072
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001073#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001074 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001075 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001077 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001079 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001080 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001082}
1083
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001084static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001085symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1086{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001087 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001088 int res;
1089 if (!st->st_cur->ste_directives) {
1090 st->st_cur->ste_directives = PyList_New(0);
1091 if (!st->st_cur->ste_directives)
1092 return 0;
1093 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001094 mangled = _Py_Mangle(st->st_private, name);
1095 if (!mangled)
1096 return 0;
1097 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001098 if (!data)
1099 return 0;
1100 res = PyList_Append(st->st_cur->ste_directives, data);
1101 Py_DECREF(data);
1102 return res == 0;
1103}
1104
1105
1106static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001107symtable_visit_stmt(struct symtable *st, stmt_ty s)
1108{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001109 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001110 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001111 "maximum recursion depth exceeded during compilation");
1112 VISIT_QUIT(st, 0);
1113 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001114 switch (s->kind) {
1115 case FunctionDef_kind:
1116 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001117 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001118 if (s->v.FunctionDef.args->defaults)
1119 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1120 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001121 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001122 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1123 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001124 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 if (s->v.FunctionDef.decorator_list)
1126 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1127 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001128 FunctionBlock, (void *)s, s->lineno,
1129 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001130 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001131 VISIT(st, arguments, s->v.FunctionDef.args);
1132 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001133 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001134 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 break;
1136 case ClassDef_kind: {
1137 PyObject *tmp;
1138 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001139 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001140 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1141 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 if (s->v.ClassDef.decorator_list)
1143 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1144 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001145 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001146 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001147 tmp = st->st_private;
1148 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001149 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 st->st_private = tmp;
1151 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001152 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001153 break;
1154 }
1155 case Return_kind:
1156 if (s->v.Return.value) {
1157 VISIT(st, expr, s->v.Return.value);
1158 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001159 }
1160 break;
1161 case Delete_kind:
1162 VISIT_SEQ(st, expr, s->v.Delete.targets);
1163 break;
1164 case Assign_kind:
1165 VISIT_SEQ(st, expr, s->v.Assign.targets);
1166 VISIT(st, expr, s->v.Assign.value);
1167 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001168 case AnnAssign_kind:
1169 if (s->v.AnnAssign.target->kind == Name_kind) {
1170 expr_ty e_name = s->v.AnnAssign.target;
1171 long cur = symtable_lookup(st, e_name->v.Name.id);
1172 if (cur < 0) {
1173 VISIT_QUIT(st, 0);
1174 }
1175 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
Pablo Galindode2aea02018-10-14 18:01:03 +01001176 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001177 && s->v.AnnAssign.simple) {
1178 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001179 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1180 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001181 PyErr_SyntaxLocationObject(st->st_filename,
1182 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001183 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001184 VISIT_QUIT(st, 0);
1185 }
1186 if (s->v.AnnAssign.simple &&
1187 !symtable_add_def(st, e_name->v.Name.id,
1188 DEF_ANNOT | DEF_LOCAL)) {
1189 VISIT_QUIT(st, 0);
1190 }
1191 else {
1192 if (s->v.AnnAssign.value
1193 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1194 VISIT_QUIT(st, 0);
1195 }
1196 }
1197 }
1198 else {
1199 VISIT(st, expr, s->v.AnnAssign.target);
1200 }
1201 VISIT(st, expr, s->v.AnnAssign.annotation);
1202 if (s->v.AnnAssign.value) {
1203 VISIT(st, expr, s->v.AnnAssign.value);
1204 }
1205 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001206 case AugAssign_kind:
1207 VISIT(st, expr, s->v.AugAssign.target);
1208 VISIT(st, expr, s->v.AugAssign.value);
1209 break;
1210 case For_kind:
1211 VISIT(st, expr, s->v.For.target);
1212 VISIT(st, expr, s->v.For.iter);
1213 VISIT_SEQ(st, stmt, s->v.For.body);
1214 if (s->v.For.orelse)
1215 VISIT_SEQ(st, stmt, s->v.For.orelse);
1216 break;
1217 case While_kind:
1218 VISIT(st, expr, s->v.While.test);
1219 VISIT_SEQ(st, stmt, s->v.While.body);
1220 if (s->v.While.orelse)
1221 VISIT_SEQ(st, stmt, s->v.While.orelse);
1222 break;
1223 case If_kind:
1224 /* XXX if 0: and lookup_yield() hacks */
1225 VISIT(st, expr, s->v.If.test);
1226 VISIT_SEQ(st, stmt, s->v.If.body);
1227 if (s->v.If.orelse)
1228 VISIT_SEQ(st, stmt, s->v.If.orelse);
1229 break;
1230 case Raise_kind:
1231 if (s->v.Raise.exc) {
1232 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001233 if (s->v.Raise.cause) {
1234 VISIT(st, expr, s->v.Raise.cause);
1235 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001236 }
1237 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001238 case Try_kind:
1239 VISIT_SEQ(st, stmt, s->v.Try.body);
1240 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1241 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1242 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 break;
1244 case Assert_kind:
1245 VISIT(st, expr, s->v.Assert.test);
1246 if (s->v.Assert.msg)
1247 VISIT(st, expr, s->v.Assert.msg);
1248 break;
1249 case Import_kind:
1250 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001251 break;
1252 case ImportFrom_kind:
1253 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001254 break;
1255 case Global_kind: {
1256 int i;
1257 asdl_seq *seq = s->v.Global.names;
1258 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1259 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001260 long cur = symtable_lookup(st, name);
1261 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001262 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001263 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1264 const char* msg;
1265 if (cur & DEF_PARAM) {
1266 msg = GLOBAL_PARAM;
1267 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001268 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001269 } else if (cur & DEF_ANNOT) {
1270 msg = GLOBAL_ANNOT;
1271 } else { /* DEF_LOCAL */
1272 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001273 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001274 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001275 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001276 PyErr_SyntaxLocationObject(st->st_filename,
1277 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001278 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001279 VISIT_QUIT(st, 0);
1280 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001281 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001282 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001283 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001284 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001285 }
1286 break;
1287 }
1288 case Nonlocal_kind: {
1289 int i;
1290 asdl_seq *seq = s->v.Nonlocal.names;
1291 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1292 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001293 long cur = symtable_lookup(st, name);
1294 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001295 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001296 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1297 const char* msg;
1298 if (cur & DEF_PARAM) {
1299 msg = NONLOCAL_PARAM;
1300 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001301 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001302 } else if (cur & DEF_ANNOT) {
1303 msg = NONLOCAL_ANNOT;
1304 } else { /* DEF_LOCAL */
1305 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001306 }
1307 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001308 PyErr_SyntaxLocationObject(st->st_filename,
1309 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001310 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001311 VISIT_QUIT(st, 0);
1312 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001313 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001314 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001315 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001316 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 }
1318 break;
1319 }
1320 case Expr_kind:
1321 VISIT(st, expr, s->v.Expr.value);
1322 break;
1323 case Pass_kind:
1324 case Break_kind:
1325 case Continue_kind:
1326 /* nothing to do here */
1327 break;
1328 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001329 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001330 VISIT_SEQ(st, stmt, s->v.With.body);
1331 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001332 case AsyncFunctionDef_kind:
1333 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1334 VISIT_QUIT(st, 0);
1335 if (s->v.AsyncFunctionDef.args->defaults)
1336 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1337 if (s->v.AsyncFunctionDef.args->kw_defaults)
1338 VISIT_SEQ_WITH_NULL(st, expr,
1339 s->v.AsyncFunctionDef.args->kw_defaults);
1340 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1341 s->v.AsyncFunctionDef.returns))
1342 VISIT_QUIT(st, 0);
1343 if (s->v.AsyncFunctionDef.decorator_list)
1344 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1345 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1346 FunctionBlock, (void *)s, s->lineno,
1347 s->col_offset))
1348 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001349 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001350 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1351 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1352 if (!symtable_exit_block(st, s))
1353 VISIT_QUIT(st, 0);
1354 break;
1355 case AsyncWith_kind:
1356 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1357 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1358 break;
1359 case AsyncFor_kind:
1360 VISIT(st, expr, s->v.AsyncFor.target);
1361 VISIT(st, expr, s->v.AsyncFor.iter);
1362 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1363 if (s->v.AsyncFor.orelse)
1364 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1365 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001367 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001368}
1369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001370static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001371symtable_visit_expr(struct symtable *st, expr_ty e)
1372{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001373 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001374 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001375 "maximum recursion depth exceeded during compilation");
1376 VISIT_QUIT(st, 0);
1377 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001378 switch (e->kind) {
1379 case BoolOp_kind:
1380 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1381 break;
1382 case BinOp_kind:
1383 VISIT(st, expr, e->v.BinOp.left);
1384 VISIT(st, expr, e->v.BinOp.right);
1385 break;
1386 case UnaryOp_kind:
1387 VISIT(st, expr, e->v.UnaryOp.operand);
1388 break;
1389 case Lambda_kind: {
1390 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001391 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001392 if (e->v.Lambda.args->defaults)
1393 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001394 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001395 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001396 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001397 FunctionBlock, (void *)e, e->lineno,
1398 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001399 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001400 VISIT(st, arguments, e->v.Lambda.args);
1401 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001402 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001403 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 break;
1405 }
1406 case IfExp_kind:
1407 VISIT(st, expr, e->v.IfExp.test);
1408 VISIT(st, expr, e->v.IfExp.body);
1409 VISIT(st, expr, e->v.IfExp.orelse);
1410 break;
1411 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001412 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001413 VISIT_SEQ(st, expr, e->v.Dict.values);
1414 break;
1415 case Set_kind:
1416 VISIT_SEQ(st, expr, e->v.Set.elts);
1417 break;
1418 case GeneratorExp_kind:
1419 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001420 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001421 break;
1422 case ListComp_kind:
1423 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001424 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001425 break;
1426 case SetComp_kind:
1427 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001428 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001429 break;
1430 case DictComp_kind:
1431 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001432 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001433 break;
1434 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001435 if (e->v.Yield.value)
1436 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001439 case YieldFrom_kind:
1440 VISIT(st, expr, e->v.YieldFrom.value);
1441 st->st_cur->ste_generator = 1;
1442 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001443 case Await_kind:
1444 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001445 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001446 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001447 case Compare_kind:
1448 VISIT(st, expr, e->v.Compare.left);
1449 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1450 break;
1451 case Call_kind:
1452 VISIT(st, expr, e->v.Call.func);
1453 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001454 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001455 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001456 case FormattedValue_kind:
1457 VISIT(st, expr, e->v.FormattedValue.value);
1458 if (e->v.FormattedValue.format_spec)
1459 VISIT(st, expr, e->v.FormattedValue.format_spec);
1460 break;
1461 case JoinedStr_kind:
1462 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1463 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001464 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001465 /* Nothing to do here. */
1466 break;
1467 /* The following exprs can be assignment targets. */
1468 case Attribute_kind:
1469 VISIT(st, expr, e->v.Attribute.value);
1470 break;
1471 case Subscript_kind:
1472 VISIT(st, expr, e->v.Subscript.value);
1473 VISIT(st, slice, e->v.Subscript.slice);
1474 break;
1475 case Starred_kind:
1476 VISIT(st, expr, e->v.Starred.value);
1477 break;
1478 case Name_kind:
1479 if (!symtable_add_def(st, e->v.Name.id,
1480 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001481 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001482 /* Special-case super: it counts as a use of __class__ */
1483 if (e->v.Name.ctx == Load &&
1484 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001485 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001486 if (!GET_IDENTIFIER(__class__) ||
1487 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001488 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001489 }
1490 break;
1491 /* child nodes of List and Tuple will have expr_context set */
1492 case List_kind:
1493 VISIT_SEQ(st, expr, e->v.List.elts);
1494 break;
1495 case Tuple_kind:
1496 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1497 break;
1498 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001499 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001500}
1501
1502static int
1503symtable_implicit_arg(struct symtable *st, int pos)
1504{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001505 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1506 if (id == NULL)
1507 return 0;
1508 if (!symtable_add_def(st, id, DEF_PARAM)) {
1509 Py_DECREF(id);
1510 return 0;
1511 }
1512 Py_DECREF(id);
1513 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001514}
1515
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001516static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001517symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001519 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001520
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 if (!args)
1522 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001523
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001524 for (i = 0; i < asdl_seq_LEN(args); i++) {
1525 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1526 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1527 return 0;
1528 }
1529
1530 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001531}
1532
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001533static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001534symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001537
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 if (!args)
1539 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001540
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001541 for (i = 0; i < asdl_seq_LEN(args); i++) {
1542 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1543 if (arg->annotation)
1544 VISIT(st, expr, arg->annotation);
1545 }
1546
1547 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001548}
1549
Neal Norwitzc1505362006-12-28 06:47:50 +00001550static int
Yury Selivanov75445082015-05-11 22:57:16 -04001551symtable_visit_annotations(struct symtable *st, stmt_ty s,
1552 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001553{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001554 if (a->args && !symtable_visit_argannotations(st, a->args))
1555 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001556 if (a->vararg && a->vararg->annotation)
1557 VISIT(st, expr, a->vararg->annotation);
1558 if (a->kwarg && a->kwarg->annotation)
1559 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1561 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001562 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001563 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001565}
1566
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001567static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001568symtable_visit_arguments(struct symtable *st, arguments_ty a)
1569{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 /* skip default arguments inside function block
1571 XXX should ast be different?
1572 */
1573 if (a->args && !symtable_visit_params(st, a->args))
1574 return 0;
1575 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1576 return 0;
1577 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001578 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001579 return 0;
1580 st->st_cur->ste_varargs = 1;
1581 }
1582 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001583 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001584 return 0;
1585 st->st_cur->ste_varkeywords = 1;
1586 }
1587 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001588}
1589
1590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001591static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1593{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001594 if (eh->v.ExceptHandler.type)
1595 VISIT(st, expr, eh->v.ExceptHandler.type);
1596 if (eh->v.ExceptHandler.name)
1597 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1598 return 0;
1599 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1600 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001601}
1602
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001603static int
1604symtable_visit_withitem(struct symtable *st, withitem_ty item)
1605{
1606 VISIT(st, expr, item->context_expr);
1607 if (item->optional_vars) {
1608 VISIT(st, expr, item->optional_vars);
1609 }
1610 return 1;
1611}
1612
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001613
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001614static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615symtable_visit_alias(struct symtable *st, alias_ty a)
1616{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001617 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001618 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 dotted package name (e.g. spam.eggs)
1620 */
1621 PyObject *store_name;
1622 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001623 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1624 PyUnicode_GET_LENGTH(name), 1);
1625 if (dot != -1) {
1626 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001627 if (!store_name)
1628 return 0;
1629 }
1630 else {
1631 store_name = name;
1632 Py_INCREF(store_name);
1633 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001634 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001635 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1636 Py_DECREF(store_name);
1637 return r;
1638 }
1639 else {
1640 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001641 int lineno = st->st_cur->ste_lineno;
1642 int col_offset = st->st_cur->ste_col_offset;
1643 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001644 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001645 Py_DECREF(store_name);
1646 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001647 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001648 Py_DECREF(store_name);
1649 return 1;
1650 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001651}
1652
1653
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1656{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001657 VISIT(st, expr, lc->target);
1658 VISIT(st, expr, lc->iter);
1659 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001660 if (lc->is_async) {
1661 st->st_cur->ste_coroutine = 1;
1662 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 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_keyword(struct symtable *st, keyword_ty k)
1669{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001670 VISIT(st, expr, k->value);
1671 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672}
1673
1674
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001675static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676symtable_visit_slice(struct symtable *st, slice_ty s)
1677{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001678 switch (s->kind) {
1679 case Slice_kind:
1680 if (s->v.Slice.lower)
1681 VISIT(st, expr, s->v.Slice.lower)
1682 if (s->v.Slice.upper)
1683 VISIT(st, expr, s->v.Slice.upper)
1684 if (s->v.Slice.step)
1685 VISIT(st, expr, s->v.Slice.step)
1686 break;
1687 case ExtSlice_kind:
1688 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1689 break;
1690 case Index_kind:
1691 VISIT(st, expr, s->v.Index.value)
1692 break;
1693 }
1694 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001695}
1696
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001697static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001698symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001699 identifier scope_name, asdl_seq *generators,
1700 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001702 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703 comprehension_ty outermost = ((comprehension_ty)
1704 asdl_seq_GET(generators, 0));
1705 /* Outermost iterator is evaluated in current scope */
1706 VISIT(st, expr, outermost->iter);
1707 /* Create comprehension scope for the rest */
1708 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001709 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1710 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001711 return 0;
1712 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001713 if (outermost->is_async) {
1714 st->st_cur->ste_coroutine = 1;
1715 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001716 /* Outermost iter is received as an argument */
1717 if (!symtable_implicit_arg(st, 0)) {
1718 symtable_exit_block(st, (void *)e);
1719 return 0;
1720 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001721 VISIT(st, expr, outermost->target);
1722 VISIT_SEQ(st, expr, outermost->ifs);
1723 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001724 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001725 VISIT(st, expr, value);
1726 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001727 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001728 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001729 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1730 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1731 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1732 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001733 PyErr_SyntaxLocationObject(st->st_filename,
1734 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001735 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001736 symtable_exit_block(st, (void *)e);
1737 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001738 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001739 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001740 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001741}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001742
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001743static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001744symtable_visit_genexp(struct symtable *st, expr_ty e)
1745{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001746 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1747 e->v.GeneratorExp.generators,
1748 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001749}
1750
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001751static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001752symtable_visit_listcomp(struct symtable *st, expr_ty e)
1753{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001754 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1755 e->v.ListComp.generators,
1756 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001757}
1758
1759static int
1760symtable_visit_setcomp(struct symtable *st, expr_ty e)
1761{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1763 e->v.SetComp.generators,
1764 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001765}
1766
1767static int
1768symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1769{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001770 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1771 e->v.DictComp.generators,
1772 e->v.DictComp.key,
1773 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001774}