blob: 1c328a99614032bc6edaff7ff3e03088691a1735 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06002#include "internal/pystate.h"
3#ifdef Yield
4#undef Yield /* undefine conflicting macro from winbase.h */
5#endif
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00006#include "Python-ast.h"
7#include "code.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00008#include "symtable.h"
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00009#include "structmember.h"
10
Neal Norwitz5d0ad502005-12-19 04:27:42 +000011/* error strings used for warnings */
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +020012#define GLOBAL_PARAM \
13"name '%U' is parameter and global"
14
15#define NONLOCAL_PARAM \
16"name '%U' is parameter and nonlocal"
17
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000018#define GLOBAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070019"name '%U' is assigned to before global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000020
Jeremy Hylton81e95022007-02-27 06:50:52 +000021#define NONLOCAL_AFTER_ASSIGN \
Guido van Rossum6cff8742016-09-09 09:36:26 -070022"name '%U' is assigned to before nonlocal declaration"
Jeremy Hylton81e95022007-02-27 06:50:52 +000023
Jeremy Hylton3e0055f2005-10-20 19:59:25 +000024#define GLOBAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070025"name '%U' is used prior to global declaration"
Jeremy Hylton29906402001-12-10 00:53:18 +000026
Jeremy Hylton81e95022007-02-27 06:50:52 +000027#define NONLOCAL_AFTER_USE \
Guido van Rossum6cff8742016-09-09 09:36:26 -070028"name '%U' is used prior to nonlocal declaration"
29
30#define GLOBAL_ANNOT \
31"annotated name '%U' can't be global"
32
33#define NONLOCAL_ANNOT \
34"annotated name '%U' can't be nonlocal"
Jeremy Hylton81e95022007-02-27 06:50:52 +000035
Neal Norwitz5d0ad502005-12-19 04:27:42 +000036#define IMPORT_STAR_WARNING "import * only allowed at module level"
37
Neal Norwitz090b3dd2006-02-28 22:36:46 +000038static PySTEntryObject *
Benjamin Peterson55e00f22008-08-17 18:02:44 +000039ste_new(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000040 void *key, int lineno, int col_offset)
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000041{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000042 PySTEntryObject *ste = NULL;
Christian Heimes837e53a2012-09-10 03:08:46 +020043 PyObject *k = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000044
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000045 k = PyLong_FromVoidPtr(key);
46 if (k == NULL)
47 goto fail;
48 ste = PyObject_New(PySTEntryObject, &PySTEntry_Type);
Christian Heimes55ad6512012-09-12 17:58:10 +020049 if (ste == NULL) {
50 Py_DECREF(k);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000051 goto fail;
Christian Heimes55ad6512012-09-12 17:58:10 +020052 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000053 ste->ste_table = st;
Christian Heimes15265822012-09-12 17:52:46 +020054 ste->ste_id = k; /* ste owns reference to k */
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000056 Py_INCREF(name);
Victor Stinner9a4fb662013-07-11 22:49:00 +020057 ste->ste_name = name;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000058
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000059 ste->ste_symbols = NULL;
60 ste->ste_varnames = NULL;
61 ste->ste_children = NULL;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000062
Benjamin Petersond9c87022012-10-31 20:26:20 -040063 ste->ste_directives = NULL;
64
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000065 ste->ste_type = block;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000066 ste->ste_nested = 0;
67 ste->ste_free = 0;
68 ste->ste_varargs = 0;
69 ste->ste_varkeywords = 0;
70 ste->ste_opt_lineno = 0;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000071 ste->ste_opt_col_offset = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000072 ste->ste_lineno = lineno;
Benjamin Petersond4efd9e2010-09-20 23:02:10 +000073 ste->ste_col_offset = col_offset;
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +000074
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 if (st->st_cur != NULL &&
76 (st->st_cur->ste_nested ||
77 st->st_cur->ste_type == FunctionBlock))
78 ste->ste_nested = 1;
79 ste->ste_child_free = 0;
80 ste->ste_generator = 0;
Yury Selivanoveb636452016-09-08 22:01:51 -070081 ste->ste_coroutine = 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));
218 if (st == NULL)
219 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000220
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000221 st->st_filename = NULL;
222 st->st_blocks = NULL;
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000224 if ((st->st_stack = PyList_New(0)) == NULL)
225 goto fail;
226 if ((st->st_blocks = PyDict_New()) == NULL)
227 goto fail;
228 st->st_cur = NULL;
229 st->st_private = NULL;
230 return st;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000231 fail:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000232 PySymtable_Free(st);
233 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000234}
235
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000236/* When compiling the use of C stack is probably going to be a lot
237 lighter than when executing Python code but still can overflow
238 and causing a Python crash if not checked (e.g. eval("()"*300000)).
239 Using the current recursion limit for the compiler seems too
240 restrictive (it caused at least one test to fail) so a factor is
241 used to allow deeper recursion when compiling an expression.
242
243 Using a scaling factor means this should automatically adjust when
244 the recursion limit is adjusted for small or large C stack allocations.
245*/
246#define COMPILER_STACK_FRAME_SCALE 3
247
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000248struct symtable *
Victor Stinner14e461d2013-08-26 22:28:21 +0200249PySymtable_BuildObject(mod_ty mod, PyObject *filename, PyFutureFeatures *future)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000250{
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000251 struct symtable *st = symtable_new();
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000252 asdl_seq *seq;
253 int i;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000254 PyThreadState *tstate;
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400255 int recursion_limit = Py_GetRecursionLimit();
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000256
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 if (st == NULL)
Victor Stinner14e461d2013-08-26 22:28:21 +0200258 return NULL;
259 if (filename == NULL) {
260 PySymtable_Free(st);
261 return NULL;
262 }
263 Py_INCREF(filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000264 st->st_filename = filename;
265 st->st_future = future;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000266
267 /* Setup recursion depth check counters */
268 tstate = PyThreadState_GET();
269 if (!tstate) {
270 PySymtable_Free(st);
271 return NULL;
272 }
Benjamin Peterson305e5aa2013-09-26 22:17:45 -0400273 /* Be careful here to prevent overflow. */
274 st->recursion_depth = (tstate->recursion_depth < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
275 tstate->recursion_depth * COMPILER_STACK_FRAME_SCALE : tstate->recursion_depth;
276 st->recursion_limit = (recursion_limit < INT_MAX / COMPILER_STACK_FRAME_SCALE) ?
277 recursion_limit * COMPILER_STACK_FRAME_SCALE : recursion_limit;
Nick Coghlanaab9c2b2012-11-04 23:14:34 +1000278
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000279 /* Make the initial symbol information gathering pass */
280 if (!GET_IDENTIFIER(top) ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000281 !symtable_enter_block(st, top, ModuleBlock, (void *)mod, 0, 0)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 PySymtable_Free(st);
283 return NULL;
284 }
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 st->st_top = st->st_cur;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000287 switch (mod->kind) {
288 case Module_kind:
289 seq = mod->v.Module.body;
290 for (i = 0; i < asdl_seq_LEN(seq); i++)
291 if (!symtable_visit_stmt(st,
292 (stmt_ty)asdl_seq_GET(seq, i)))
293 goto error;
294 break;
295 case Expression_kind:
296 if (!symtable_visit_expr(st, mod->v.Expression.body))
297 goto error;
298 break;
299 case Interactive_kind:
300 seq = mod->v.Interactive.body;
301 for (i = 0; i < asdl_seq_LEN(seq); i++)
302 if (!symtable_visit_stmt(st,
303 (stmt_ty)asdl_seq_GET(seq, i)))
304 goto error;
305 break;
306 case Suite_kind:
307 PyErr_SetString(PyExc_RuntimeError,
308 "this compiler does not handle Suites");
309 goto error;
310 }
311 if (!symtable_exit_block(st, (void *)mod)) {
312 PySymtable_Free(st);
313 return NULL;
314 }
315 /* Make the second symbol analysis pass */
316 if (symtable_analyze(st))
317 return st;
318 PySymtable_Free(st);
319 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000320 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000321 (void) symtable_exit_block(st, (void *)mod);
322 PySymtable_Free(st);
323 return NULL;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000324}
325
Victor Stinner14e461d2013-08-26 22:28:21 +0200326struct symtable *
327PySymtable_Build(mod_ty mod, const char *filename_str, PyFutureFeatures *future)
328{
329 PyObject *filename;
330 struct symtable *st;
331 filename = PyUnicode_DecodeFSDefault(filename_str);
332 if (filename == NULL)
333 return NULL;
334 st = PySymtable_BuildObject(mod, filename, future);
335 Py_DECREF(filename);
336 return st;
337}
338
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000339void
340PySymtable_Free(struct symtable *st)
341{
Victor Stinner14e461d2013-08-26 22:28:21 +0200342 Py_XDECREF(st->st_filename);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000343 Py_XDECREF(st->st_blocks);
344 Py_XDECREF(st->st_stack);
345 PyMem_Free((void *)st);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000346}
347
348PySTEntryObject *
349PySymtable_Lookup(struct symtable *st, void *key)
350{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000351 PyObject *k, *v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000352
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000353 k = PyLong_FromVoidPtr(key);
354 if (k == NULL)
355 return NULL;
356 v = PyDict_GetItem(st->st_blocks, k);
357 if (v) {
358 assert(PySTEntry_Check(v));
359 Py_INCREF(v);
360 }
361 else {
362 PyErr_SetString(PyExc_KeyError,
363 "unknown symbol table entry");
364 }
Neal Norwitzb6fc9df2005-11-13 18:50:34 +0000365
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000366 Py_DECREF(k);
367 return (PySTEntryObject *)v;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000368}
369
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000371PyST_GetScope(PySTEntryObject *ste, PyObject *name)
372{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000373 PyObject *v = PyDict_GetItem(ste->ste_symbols, name);
374 if (!v)
375 return 0;
376 assert(PyLong_Check(v));
377 return (PyLong_AS_LONG(v) >> SCOPE_OFFSET) & SCOPE_MASK;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000378}
379
Benjamin Petersond9c87022012-10-31 20:26:20 -0400380static int
381error_at_directive(PySTEntryObject *ste, PyObject *name)
382{
383 Py_ssize_t i;
384 PyObject *data;
385 assert(ste->ste_directives);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600386 for (i = 0; i < PyList_GET_SIZE(ste->ste_directives); i++) {
Benjamin Petersond9c87022012-10-31 20:26:20 -0400387 data = PyList_GET_ITEM(ste->ste_directives, i);
388 assert(PyTuple_CheckExact(data));
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600389 assert(PyUnicode_CheckExact(PyTuple_GET_ITEM(data, 0)));
390 if (PyUnicode_Compare(PyTuple_GET_ITEM(data, 0), name) == 0) {
391 PyErr_SyntaxLocationObject(ste->ste_table->st_filename,
392 PyLong_AsLong(PyTuple_GET_ITEM(data, 1)),
393 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)));
394
395 return 0;
Yury Selivanoveb636452016-09-08 22:01:51 -0700396 }
Benjamin Petersond9c87022012-10-31 20:26:20 -0400397 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600398 PyErr_SetString(PyExc_RuntimeError,
399 "BUG: internal directive bookkeeping broken");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400400 return 0;
401}
402
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000403
404/* Analyze raw symbol information to determine scope of each name.
405
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000406 The next several functions are helpers for symtable_analyze(),
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 which determines whether a name is local, global, or free. In addition,
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000408 it determines which local variables are cell variables; they provide
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000409 bindings that are used for free variables in enclosed blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000410
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000411 There are also two kinds of global variables, implicit and explicit. An
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000412 explicit global is declared with the global statement. An implicit
413 global is a free variable for which the compiler has found no binding
414 in an enclosing function scope. The implicit global is either a global
415 or a builtin. Python's module and class blocks use the xxx_NAME opcodes
416 to handle these names to implement slightly odd semantics. In such a
417 block, the name is treated as global until it is assigned to; then it
418 is treated as a local.
419
420 The symbol table requires two passes to determine the scope of each name.
Nick Coghlan650f0d02007-04-15 12:05:43 +0000421 The first pass collects raw facts from the AST via the symtable_visit_*
422 functions: the name is a parameter here, the name is used but not defined
423 here, etc. The second pass analyzes these facts during a pass over the
424 PySTEntryObjects created during pass 1.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000425
426 When a function is entered during the second pass, the parent passes
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000427 the set of all name bindings visible to its children. These bindings
Nick Coghlan650f0d02007-04-15 12:05:43 +0000428 are used to determine if non-local variables are free or implicit globals.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000429 Names which are explicitly declared nonlocal must exist in this set of
430 visible names - if they do not, a syntax error is raised. After doing
431 the local analysis, it analyzes each of its child blocks using an
432 updated set of name bindings.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000433
Nick Coghlan650f0d02007-04-15 12:05:43 +0000434 The children update the free variable set. If a local variable is added to
435 the free variable set by the child, the variable is marked as a cell. The
436 function object being defined must provide runtime storage for the variable
437 that may outlive the function's frame. Cell variables are removed from the
438 free set before the analyze function returns to its parent.
Nick Coghlan4138bfe2007-04-23 10:14:27 +0000439
Nick Coghlan650f0d02007-04-15 12:05:43 +0000440 During analysis, the names are:
441 symbols: dict mapping from symbol names to flag values (including offset scope values)
442 scopes: dict mapping from symbol names to scope values (no offset)
443 local: set of all symbol names local to the current scope
444 bound: set of all symbol names local to a containing function scope
445 free: set of all symbol names referenced but not bound in child scopes
446 global: set of all symbol names explicitly declared as global
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000447*/
448
449#define SET_SCOPE(DICT, NAME, I) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000450 PyObject *o = PyLong_FromLong(I); \
451 if (!o) \
452 return 0; \
453 if (PyDict_SetItem((DICT), (NAME), o) < 0) { \
454 Py_DECREF(o); \
455 return 0; \
456 } \
457 Py_DECREF(o); \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000458}
459
460/* Decide on scope of name, given flags.
461
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000462 The namespace dictionaries may be modified to record information
463 about the new name. For example, a new global will add an entry to
464 global. A name that was global can be changed to local.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000465*/
466
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467static int
Nick Coghlan650f0d02007-04-15 12:05:43 +0000468analyze_name(PySTEntryObject *ste, PyObject *scopes, PyObject *name, long flags,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000469 PyObject *bound, PyObject *local, PyObject *free,
470 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000471{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 if (flags & DEF_GLOBAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000473 if (flags & DEF_NONLOCAL) {
474 PyErr_Format(PyExc_SyntaxError,
475 "name '%U' is nonlocal and global",
476 name);
Benjamin Petersond9c87022012-10-31 20:26:20 -0400477 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000478 }
479 SET_SCOPE(scopes, name, GLOBAL_EXPLICIT);
480 if (PySet_Add(global, name) < 0)
481 return 0;
482 if (bound && (PySet_Discard(bound, name) < 0))
483 return 0;
484 return 1;
485 }
486 if (flags & DEF_NONLOCAL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000487 if (!bound) {
488 PyErr_Format(PyExc_SyntaxError,
489 "nonlocal declaration not allowed at module level");
Benjamin Petersond9c87022012-10-31 20:26:20 -0400490 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000491 }
492 if (!PySet_Contains(bound, name)) {
493 PyErr_Format(PyExc_SyntaxError,
494 "no binding for nonlocal '%U' found",
495 name);
496
Benjamin Petersond9c87022012-10-31 20:26:20 -0400497 return error_at_directive(ste, name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 }
499 SET_SCOPE(scopes, name, FREE);
500 ste->ste_free = 1;
501 return PySet_Add(free, name) >= 0;
502 }
503 if (flags & DEF_BOUND) {
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000504 SET_SCOPE(scopes, name, LOCAL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000505 if (PySet_Add(local, name) < 0)
506 return 0;
507 if (PySet_Discard(global, name) < 0)
508 return 0;
509 return 1;
510 }
511 /* If an enclosing block has a binding for this name, it
512 is a free variable rather than a global variable.
513 Note that having a non-NULL bound implies that the block
514 is nested.
515 */
516 if (bound && PySet_Contains(bound, name)) {
517 SET_SCOPE(scopes, name, FREE);
518 ste->ste_free = 1;
519 return PySet_Add(free, name) >= 0;
520 }
521 /* If a parent has a global statement, then call it global
522 explicit? It could also be global implicit.
523 */
524 if (global && PySet_Contains(global, name)) {
525 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
526 return 1;
527 }
528 if (ste->ste_nested)
529 ste->ste_free = 1;
530 SET_SCOPE(scopes, name, GLOBAL_IMPLICIT);
531 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000532}
533
534#undef SET_SCOPE
535
536/* If a name is defined in free and also in locals, then this block
537 provides the binding for the free variable. The name should be
538 marked CELL in this block and removed from the free list.
539
540 Note that the current block's free variables are included in free.
541 That's safe because no name can be free and local in the same scope.
542*/
543
544static int
Benjamin Peterson312595c2013-05-15 15:26:42 -0500545analyze_cells(PyObject *scopes, PyObject *free)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000546{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 PyObject *name, *v, *v_cell;
548 int success = 0;
549 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000550
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 v_cell = PyLong_FromLong(CELL);
552 if (!v_cell)
553 return 0;
554 while (PyDict_Next(scopes, &pos, &name, &v)) {
555 long scope;
556 assert(PyLong_Check(v));
557 scope = PyLong_AS_LONG(v);
Benjamin Peterson20f9c3c2010-07-20 22:39:34 +0000558 if (scope != LOCAL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000559 continue;
560 if (!PySet_Contains(free, name))
561 continue;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 /* Replace LOCAL with CELL for this name, and remove
563 from free. It is safe to replace the value of name
564 in the dict, because it will not cause a resize.
565 */
566 if (PyDict_SetItem(scopes, name, v_cell) < 0)
567 goto error;
568 if (PySet_Discard(free, name) < 0)
569 goto error;
570 }
571 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000572 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000573 Py_DECREF(v_cell);
574 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000575}
576
Benjamin Peterson312595c2013-05-15 15:26:42 -0500577static int
578drop_class_free(PySTEntryObject *ste, PyObject *free)
579{
580 int res;
581 if (!GET_IDENTIFIER(__class__))
582 return 0;
583 res = PySet_Discard(free, __class__);
584 if (res < 0)
585 return 0;
586 if (res)
587 ste->ste_needs_class_closure = 1;
588 return 1;
589}
590
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000591/* Enter the final scope information into the ste_symbols dict.
592 *
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000593 * All arguments are dicts. Modifies symbols, others are read-only.
594*/
595static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596update_symbols(PyObject *symbols, PyObject *scopes,
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000597 PyObject *bound, PyObject *free, int classflag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000598{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 PyObject *name = NULL, *itr = NULL;
600 PyObject *v = NULL, *v_scope = NULL, *v_new = NULL, *v_free = NULL;
601 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000602
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 /* Update scope information for all symbols in this scope */
604 while (PyDict_Next(symbols, &pos, &name, &v)) {
605 long scope, flags;
606 assert(PyLong_Check(v));
607 flags = PyLong_AS_LONG(v);
608 v_scope = PyDict_GetItem(scopes, name);
609 assert(v_scope && PyLong_Check(v_scope));
610 scope = PyLong_AS_LONG(v_scope);
611 flags |= (scope << SCOPE_OFFSET);
612 v_new = PyLong_FromLong(flags);
613 if (!v_new)
614 return 0;
615 if (PyDict_SetItem(symbols, name, v_new) < 0) {
616 Py_DECREF(v_new);
617 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000618 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000619 Py_DECREF(v_new);
620 }
621
622 /* Record not yet resolved free variables from children (if any) */
623 v_free = PyLong_FromLong(FREE << SCOPE_OFFSET);
624 if (!v_free)
625 return 0;
626
627 itr = PyObject_GetIter(free);
628 if (!itr)
629 goto error;
630
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,
993 st->st_cur->ste_col_offset);
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))
1176 && s->v.AnnAssign.simple) {
1177 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001178 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1179 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001180 PyErr_SyntaxLocationObject(st->st_filename,
1181 s->lineno,
1182 s->col_offset);
1183 VISIT_QUIT(st, 0);
1184 }
1185 if (s->v.AnnAssign.simple &&
1186 !symtable_add_def(st, e_name->v.Name.id,
1187 DEF_ANNOT | DEF_LOCAL)) {
1188 VISIT_QUIT(st, 0);
1189 }
1190 else {
1191 if (s->v.AnnAssign.value
1192 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1193 VISIT_QUIT(st, 0);
1194 }
1195 }
1196 }
1197 else {
1198 VISIT(st, expr, s->v.AnnAssign.target);
1199 }
1200 VISIT(st, expr, s->v.AnnAssign.annotation);
1201 if (s->v.AnnAssign.value) {
1202 VISIT(st, expr, s->v.AnnAssign.value);
1203 }
1204 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001205 case AugAssign_kind:
1206 VISIT(st, expr, s->v.AugAssign.target);
1207 VISIT(st, expr, s->v.AugAssign.value);
1208 break;
1209 case For_kind:
1210 VISIT(st, expr, s->v.For.target);
1211 VISIT(st, expr, s->v.For.iter);
1212 VISIT_SEQ(st, stmt, s->v.For.body);
1213 if (s->v.For.orelse)
1214 VISIT_SEQ(st, stmt, s->v.For.orelse);
1215 break;
1216 case While_kind:
1217 VISIT(st, expr, s->v.While.test);
1218 VISIT_SEQ(st, stmt, s->v.While.body);
1219 if (s->v.While.orelse)
1220 VISIT_SEQ(st, stmt, s->v.While.orelse);
1221 break;
1222 case If_kind:
1223 /* XXX if 0: and lookup_yield() hacks */
1224 VISIT(st, expr, s->v.If.test);
1225 VISIT_SEQ(st, stmt, s->v.If.body);
1226 if (s->v.If.orelse)
1227 VISIT_SEQ(st, stmt, s->v.If.orelse);
1228 break;
1229 case Raise_kind:
1230 if (s->v.Raise.exc) {
1231 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001232 if (s->v.Raise.cause) {
1233 VISIT(st, expr, s->v.Raise.cause);
1234 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001235 }
1236 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001237 case Try_kind:
1238 VISIT_SEQ(st, stmt, s->v.Try.body);
1239 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1240 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1241 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001242 break;
1243 case Assert_kind:
1244 VISIT(st, expr, s->v.Assert.test);
1245 if (s->v.Assert.msg)
1246 VISIT(st, expr, s->v.Assert.msg);
1247 break;
1248 case Import_kind:
1249 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001250 break;
1251 case ImportFrom_kind:
1252 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 break;
1254 case Global_kind: {
1255 int i;
1256 asdl_seq *seq = s->v.Global.names;
1257 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1258 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001259 long cur = symtable_lookup(st, name);
1260 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001261 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001262 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1263 const char* msg;
1264 if (cur & DEF_PARAM) {
1265 msg = GLOBAL_PARAM;
1266 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001267 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001268 } else if (cur & DEF_ANNOT) {
1269 msg = GLOBAL_ANNOT;
1270 } else { /* DEF_LOCAL */
1271 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001272 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001273 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001274 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001275 PyErr_SyntaxLocationObject(st->st_filename,
1276 s->lineno,
1277 s->col_offset);
1278 VISIT_QUIT(st, 0);
1279 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001280 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001281 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001282 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001283 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001284 }
1285 break;
1286 }
1287 case Nonlocal_kind: {
1288 int i;
1289 asdl_seq *seq = s->v.Nonlocal.names;
1290 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1291 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 long cur = symtable_lookup(st, name);
1293 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001294 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001295 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1296 const char* msg;
1297 if (cur & DEF_PARAM) {
1298 msg = NONLOCAL_PARAM;
1299 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001300 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001301 } else if (cur & DEF_ANNOT) {
1302 msg = NONLOCAL_ANNOT;
1303 } else { /* DEF_LOCAL */
1304 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001305 }
1306 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001307 PyErr_SyntaxLocationObject(st->st_filename,
1308 s->lineno,
1309 s->col_offset);
1310 VISIT_QUIT(st, 0);
1311 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001313 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001314 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001315 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001316 }
1317 break;
1318 }
1319 case Expr_kind:
1320 VISIT(st, expr, s->v.Expr.value);
1321 break;
1322 case Pass_kind:
1323 case Break_kind:
1324 case Continue_kind:
1325 /* nothing to do here */
1326 break;
1327 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001328 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001329 VISIT_SEQ(st, stmt, s->v.With.body);
1330 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001331 case AsyncFunctionDef_kind:
1332 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1333 VISIT_QUIT(st, 0);
1334 if (s->v.AsyncFunctionDef.args->defaults)
1335 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1336 if (s->v.AsyncFunctionDef.args->kw_defaults)
1337 VISIT_SEQ_WITH_NULL(st, expr,
1338 s->v.AsyncFunctionDef.args->kw_defaults);
1339 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1340 s->v.AsyncFunctionDef.returns))
1341 VISIT_QUIT(st, 0);
1342 if (s->v.AsyncFunctionDef.decorator_list)
1343 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1344 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1345 FunctionBlock, (void *)s, s->lineno,
1346 s->col_offset))
1347 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001348 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001349 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1350 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1351 if (!symtable_exit_block(st, s))
1352 VISIT_QUIT(st, 0);
1353 break;
1354 case AsyncWith_kind:
1355 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1356 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1357 break;
1358 case AsyncFor_kind:
1359 VISIT(st, expr, s->v.AsyncFor.target);
1360 VISIT(st, expr, s->v.AsyncFor.iter);
1361 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1362 if (s->v.AsyncFor.orelse)
1363 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1364 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001365 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001366 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001367}
1368
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001369static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370symtable_visit_expr(struct symtable *st, expr_ty e)
1371{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001372 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001373 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001374 "maximum recursion depth exceeded during compilation");
1375 VISIT_QUIT(st, 0);
1376 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001377 switch (e->kind) {
1378 case BoolOp_kind:
1379 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1380 break;
1381 case BinOp_kind:
1382 VISIT(st, expr, e->v.BinOp.left);
1383 VISIT(st, expr, e->v.BinOp.right);
1384 break;
1385 case UnaryOp_kind:
1386 VISIT(st, expr, e->v.UnaryOp.operand);
1387 break;
1388 case Lambda_kind: {
1389 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001390 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001391 if (e->v.Lambda.args->defaults)
1392 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001393 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001394 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001395 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001396 FunctionBlock, (void *)e, e->lineno,
1397 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001398 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001399 VISIT(st, arguments, e->v.Lambda.args);
1400 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001401 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001402 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 break;
1404 }
1405 case IfExp_kind:
1406 VISIT(st, expr, e->v.IfExp.test);
1407 VISIT(st, expr, e->v.IfExp.body);
1408 VISIT(st, expr, e->v.IfExp.orelse);
1409 break;
1410 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001411 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001412 VISIT_SEQ(st, expr, e->v.Dict.values);
1413 break;
1414 case Set_kind:
1415 VISIT_SEQ(st, expr, e->v.Set.elts);
1416 break;
1417 case GeneratorExp_kind:
1418 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001419 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001420 break;
1421 case ListComp_kind:
1422 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001423 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001424 break;
1425 case SetComp_kind:
1426 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001427 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001428 break;
1429 case DictComp_kind:
1430 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001431 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001432 break;
1433 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001434 if (e->v.Yield.value)
1435 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001436 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001437 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001438 case YieldFrom_kind:
1439 VISIT(st, expr, e->v.YieldFrom.value);
1440 st->st_cur->ste_generator = 1;
1441 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001442 case Await_kind:
1443 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001444 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001445 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001446 case Compare_kind:
1447 VISIT(st, expr, e->v.Compare.left);
1448 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1449 break;
1450 case Call_kind:
1451 VISIT(st, expr, e->v.Call.func);
1452 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001453 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001454 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001455 case FormattedValue_kind:
1456 VISIT(st, expr, e->v.FormattedValue.value);
1457 if (e->v.FormattedValue.format_spec)
1458 VISIT(st, expr, e->v.FormattedValue.format_spec);
1459 break;
1460 case JoinedStr_kind:
1461 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1462 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001463 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001464 case Num_kind:
1465 case Str_kind:
1466 case Bytes_kind:
1467 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001468 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001469 /* Nothing to do here. */
1470 break;
1471 /* The following exprs can be assignment targets. */
1472 case Attribute_kind:
1473 VISIT(st, expr, e->v.Attribute.value);
1474 break;
1475 case Subscript_kind:
1476 VISIT(st, expr, e->v.Subscript.value);
1477 VISIT(st, slice, e->v.Subscript.slice);
1478 break;
1479 case Starred_kind:
1480 VISIT(st, expr, e->v.Starred.value);
1481 break;
1482 case Name_kind:
1483 if (!symtable_add_def(st, e->v.Name.id,
1484 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001485 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001486 /* Special-case super: it counts as a use of __class__ */
1487 if (e->v.Name.ctx == Load &&
1488 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001489 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001490 if (!GET_IDENTIFIER(__class__) ||
1491 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001492 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 }
1494 break;
1495 /* child nodes of List and Tuple will have expr_context set */
1496 case List_kind:
1497 VISIT_SEQ(st, expr, e->v.List.elts);
1498 break;
1499 case Tuple_kind:
1500 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1501 break;
1502 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001503 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001504}
1505
1506static int
1507symtable_implicit_arg(struct symtable *st, int pos)
1508{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001509 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1510 if (id == NULL)
1511 return 0;
1512 if (!symtable_add_def(st, id, DEF_PARAM)) {
1513 Py_DECREF(id);
1514 return 0;
1515 }
1516 Py_DECREF(id);
1517 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001518}
1519
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001520static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001521symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001524
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 if (!args)
1526 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001527
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001528 for (i = 0; i < asdl_seq_LEN(args); i++) {
1529 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1530 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1531 return 0;
1532 }
1533
1534 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001535}
1536
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001537static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001538symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001539{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001541
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 if (!args)
1543 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001544
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001545 for (i = 0; i < asdl_seq_LEN(args); i++) {
1546 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1547 if (arg->annotation)
1548 VISIT(st, expr, arg->annotation);
1549 }
1550
1551 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001552}
1553
Neal Norwitzc1505362006-12-28 06:47:50 +00001554static int
Yury Selivanov75445082015-05-11 22:57:16 -04001555symtable_visit_annotations(struct symtable *st, stmt_ty s,
1556 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001557{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001558 if (a->args && !symtable_visit_argannotations(st, a->args))
1559 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001560 if (a->vararg && a->vararg->annotation)
1561 VISIT(st, expr, a->vararg->annotation);
1562 if (a->kwarg && a->kwarg->annotation)
1563 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001564 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1565 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001566 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001567 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001568 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001569}
1570
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001571static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001572symtable_visit_arguments(struct symtable *st, arguments_ty a)
1573{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001574 /* skip default arguments inside function block
1575 XXX should ast be different?
1576 */
1577 if (a->args && !symtable_visit_params(st, a->args))
1578 return 0;
1579 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1580 return 0;
1581 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001582 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001583 return 0;
1584 st->st_cur->ste_varargs = 1;
1585 }
1586 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001587 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001588 return 0;
1589 st->st_cur->ste_varkeywords = 1;
1590 }
1591 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001592}
1593
1594
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001595static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001596symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1597{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001598 if (eh->v.ExceptHandler.type)
1599 VISIT(st, expr, eh->v.ExceptHandler.type);
1600 if (eh->v.ExceptHandler.name)
1601 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1602 return 0;
1603 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1604 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001605}
1606
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001607static int
1608symtable_visit_withitem(struct symtable *st, withitem_ty item)
1609{
1610 VISIT(st, expr, item->context_expr);
1611 if (item->optional_vars) {
1612 VISIT(st, expr, item->optional_vars);
1613 }
1614 return 1;
1615}
1616
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001618static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619symtable_visit_alias(struct symtable *st, alias_ty a)
1620{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001622 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 dotted package name (e.g. spam.eggs)
1624 */
1625 PyObject *store_name;
1626 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001627 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1628 PyUnicode_GET_LENGTH(name), 1);
1629 if (dot != -1) {
1630 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001631 if (!store_name)
1632 return 0;
1633 }
1634 else {
1635 store_name = name;
1636 Py_INCREF(store_name);
1637 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001638 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001639 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1640 Py_DECREF(store_name);
1641 return r;
1642 }
1643 else {
1644 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001645 int lineno = st->st_cur->ste_lineno;
1646 int col_offset = st->st_cur->ste_col_offset;
1647 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001648 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001649 Py_DECREF(store_name);
1650 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001651 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001652 Py_DECREF(store_name);
1653 return 1;
1654 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001655}
1656
1657
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001658static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001659symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1660{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001661 VISIT(st, expr, lc->target);
1662 VISIT(st, expr, lc->iter);
1663 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001664 if (lc->is_async) {
1665 st->st_cur->ste_coroutine = 1;
1666 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001667 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001668}
1669
1670
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001671static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001672symtable_visit_keyword(struct symtable *st, keyword_ty k)
1673{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001674 VISIT(st, expr, k->value);
1675 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001676}
1677
1678
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001679static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001680symtable_visit_slice(struct symtable *st, slice_ty s)
1681{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001682 switch (s->kind) {
1683 case Slice_kind:
1684 if (s->v.Slice.lower)
1685 VISIT(st, expr, s->v.Slice.lower)
1686 if (s->v.Slice.upper)
1687 VISIT(st, expr, s->v.Slice.upper)
1688 if (s->v.Slice.step)
1689 VISIT(st, expr, s->v.Slice.step)
1690 break;
1691 case ExtSlice_kind:
1692 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1693 break;
1694 case Index_kind:
1695 VISIT(st, expr, s->v.Index.value)
1696 break;
1697 }
1698 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001699}
1700
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001701static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001702symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001703 identifier scope_name, asdl_seq *generators,
1704 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001705{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001706 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001707 comprehension_ty outermost = ((comprehension_ty)
1708 asdl_seq_GET(generators, 0));
1709 /* Outermost iterator is evaluated in current scope */
1710 VISIT(st, expr, outermost->iter);
1711 /* Create comprehension scope for the rest */
1712 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001713 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1714 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001715 return 0;
1716 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001717 if (outermost->is_async) {
1718 st->st_cur->ste_coroutine = 1;
1719 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001720 /* Outermost iter is received as an argument */
1721 if (!symtable_implicit_arg(st, 0)) {
1722 symtable_exit_block(st, (void *)e);
1723 return 0;
1724 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001725 VISIT(st, expr, outermost->target);
1726 VISIT_SEQ(st, expr, outermost->ifs);
1727 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001728 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001729 VISIT(st, expr, value);
1730 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001731 if (st->st_cur->ste_generator) {
1732 PyObject *msg = PyUnicode_FromString(
1733 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1734 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1735 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1736 "'yield' inside generator expression");
1737 if (msg == NULL) {
1738 symtable_exit_block(st, (void *)e);
1739 return 0;
1740 }
1741 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning,
1742 msg, st->st_filename, st->st_cur->ste_lineno,
1743 NULL, NULL) == -1)
1744 {
1745 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
1746 /* Replace the DeprecationWarning exception with a SyntaxError
1747 to get a more accurate error report */
1748 PyErr_Clear();
1749 PyErr_SetObject(PyExc_SyntaxError, msg);
1750 PyErr_SyntaxLocationObject(st->st_filename,
1751 st->st_cur->ste_lineno,
1752 st->st_cur->ste_col_offset);
1753 }
1754 Py_DECREF(msg);
1755 symtable_exit_block(st, (void *)e);
1756 return 0;
1757 }
1758 Py_DECREF(msg);
1759 }
1760 st->st_cur->ste_generator |= is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001761 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001762}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001763
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001765symtable_visit_genexp(struct symtable *st, expr_ty e)
1766{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001767 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1768 e->v.GeneratorExp.generators,
1769 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001770}
1771
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001773symtable_visit_listcomp(struct symtable *st, expr_ty e)
1774{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001775 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1776 e->v.ListComp.generators,
1777 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001778}
1779
1780static int
1781symtable_visit_setcomp(struct symtable *st, expr_ty e)
1782{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001783 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1784 e->v.SetComp.generators,
1785 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001786}
1787
1788static int
1789symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1790{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001791 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1792 e->v.DictComp.generators,
1793 e->v.DictComp.key,
1794 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001795}