blob: 81eea95f4907d3cabd9d93211742975d3646ff1f [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);
Miss Islington (bot)1c2cb512018-10-10 22:24:14 -0700628 if (itr == NULL) {
629 Py_DECREF(v_free);
630 return 0;
631 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632
633 while ((name = PyIter_Next(itr))) {
634 v = PyDict_GetItem(symbols, name);
635
636 /* Handle symbol that already exists in this scope */
637 if (v) {
638 /* Handle a free variable in a method of
639 the class that has the same name as a local
640 or global in the class scope.
641 */
642 if (classflag &&
643 PyLong_AS_LONG(v) & (DEF_BOUND | DEF_GLOBAL)) {
644 long flags = PyLong_AS_LONG(v) | DEF_FREE_CLASS;
645 v_new = PyLong_FromLong(flags);
646 if (!v_new) {
647 goto error;
648 }
649 if (PyDict_SetItem(symbols, name, v_new) < 0) {
650 Py_DECREF(v_new);
651 goto error;
652 }
653 Py_DECREF(v_new);
654 }
655 /* It's a cell, or already free in this scope */
656 Py_DECREF(name);
657 continue;
658 }
659 /* Handle global symbol */
Christian Heimes45af0c82016-09-09 00:22:28 +0200660 if (bound && !PySet_Contains(bound, name)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000661 Py_DECREF(name);
662 continue; /* it's a global */
663 }
664 /* Propagate new free symbol up the lexical stack */
665 if (PyDict_SetItem(symbols, name, v_free) < 0) {
666 goto error;
667 }
668 Py_DECREF(name);
669 }
670 Py_DECREF(itr);
671 Py_DECREF(v_free);
672 return 1;
Nick Coghlan650f0d02007-04-15 12:05:43 +0000673error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 Py_XDECREF(v_free);
675 Py_XDECREF(itr);
676 Py_XDECREF(name);
677 return 0;
678}
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000679
680/* Make final symbol table decisions for block of ste.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000681
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000682 Arguments:
683 ste -- current symtable entry (input/output)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000684 bound -- set of variables bound in enclosing scopes (input). bound
685 is NULL for module blocks.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000686 free -- set of free variables in enclosed scopes (output)
687 globals -- set of declared global variables in enclosing scopes (input)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000688
689 The implementation uses two mutually recursive functions,
690 analyze_block() and analyze_child_block(). analyze_block() is
691 responsible for analyzing the individual names defined in a block.
692 analyze_child_block() prepares temporary namespace dictionaries
693 used to evaluated nested blocks.
694
695 The two functions exist because a child block should see the name
696 bindings of its enclosing blocks, but those bindings should not
697 propagate back to a parent block.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000698*/
699
700static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000701analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
702 PyObject *global, PyObject* child_free);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000703
704static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000705analyze_block(PySTEntryObject *ste, PyObject *bound, PyObject *free,
706 PyObject *global)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyObject *name, *v, *local = NULL, *scopes = NULL, *newbound = NULL;
709 PyObject *newglobal = NULL, *newfree = NULL, *allfree = NULL;
710 PyObject *temp;
711 int i, success = 0;
712 Py_ssize_t pos = 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000713
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000714 local = PySet_New(NULL); /* collect new names bound in block */
715 if (!local)
716 goto error;
717 scopes = PyDict_New(); /* collect scopes defined for each name */
718 if (!scopes)
719 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000720
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000721 /* Allocate new global and bound variable dictionaries. These
722 dictionaries hold the names visible in nested blocks. For
723 ClassBlocks, the bound and global names are initialized
724 before analyzing names, because class bindings aren't
725 visible in methods. For other blocks, they are initialized
726 after names are analyzed.
727 */
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000728
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000729 /* TODO(jhylton): Package these dicts in a struct so that we
730 can write reasonable helper functions?
731 */
732 newglobal = PySet_New(NULL);
733 if (!newglobal)
734 goto error;
735 newfree = PySet_New(NULL);
736 if (!newfree)
737 goto error;
738 newbound = PySet_New(NULL);
739 if (!newbound)
740 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000741
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 /* Class namespace has no effect on names visible in
743 nested functions, so populate the global and bound
744 sets to be passed to child blocks before analyzing
745 this one.
746 */
747 if (ste->ste_type == ClassBlock) {
748 /* Pass down known globals */
749 temp = PyNumber_InPlaceOr(newglobal, global);
750 if (!temp)
751 goto error;
752 Py_DECREF(temp);
753 /* Pass down previously bound symbols */
754 if (bound) {
755 temp = PyNumber_InPlaceOr(newbound, bound);
756 if (!temp)
757 goto error;
758 Py_DECREF(temp);
759 }
760 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000761
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000762 while (PyDict_Next(ste->ste_symbols, &pos, &name, &v)) {
763 long flags = PyLong_AS_LONG(v);
764 if (!analyze_name(ste, scopes, name, flags,
765 bound, local, free, global))
766 goto error;
767 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000768
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000769 /* Populate global and bound sets to be passed to children. */
770 if (ste->ste_type != ClassBlock) {
771 /* Add function locals to bound set */
772 if (ste->ste_type == FunctionBlock) {
773 temp = PyNumber_InPlaceOr(newbound, local);
774 if (!temp)
775 goto error;
776 Py_DECREF(temp);
777 }
778 /* Pass down previously bound symbols */
779 if (bound) {
780 temp = PyNumber_InPlaceOr(newbound, bound);
781 if (!temp)
782 goto error;
783 Py_DECREF(temp);
784 }
785 /* Pass down known globals */
786 temp = PyNumber_InPlaceOr(newglobal, global);
787 if (!temp)
788 goto error;
789 Py_DECREF(temp);
790 }
791 else {
792 /* Special-case __class__ */
Nick Coghlan0b43bcf2012-05-27 18:17:07 +1000793 if (!GET_IDENTIFIER(__class__))
794 goto error;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 if (PySet_Add(newbound, __class__) < 0)
796 goto error;
797 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000798
Eli Benderskydd97fbb2011-04-10 07:37:26 +0300799 /* Recursively call analyze_child_block() on each child block.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000800
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000801 newbound, newglobal now contain the names visible in
802 nested blocks. The free variables in the children will
803 be collected in allfree.
804 */
805 allfree = PySet_New(NULL);
806 if (!allfree)
807 goto error;
808 for (i = 0; i < PyList_GET_SIZE(ste->ste_children); ++i) {
809 PyObject *c = PyList_GET_ITEM(ste->ste_children, i);
810 PySTEntryObject* entry;
811 assert(c && PySTEntry_Check(c));
812 entry = (PySTEntryObject*)c;
813 if (!analyze_child_block(entry, newbound, newfree, newglobal,
814 allfree))
815 goto error;
816 /* Check if any children have free variables */
817 if (entry->ste_free || entry->ste_child_free)
818 ste->ste_child_free = 1;
819 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000820
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000821 temp = PyNumber_InPlaceOr(newfree, allfree);
822 if (!temp)
823 goto error;
824 Py_DECREF(temp);
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000825
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000826 /* Check if any local variables must be converted to cell variables */
Benjamin Peterson312595c2013-05-15 15:26:42 -0500827 if (ste->ste_type == FunctionBlock && !analyze_cells(scopes, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000828 goto error;
Benjamin Peterson312595c2013-05-15 15:26:42 -0500829 else if (ste->ste_type == ClassBlock && !drop_class_free(ste, newfree))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000830 goto error;
831 /* Records the results of the analysis in the symbol table entry */
832 if (!update_symbols(ste->ste_symbols, scopes, bound, newfree,
833 ste->ste_type == ClassBlock))
834 goto error;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000835
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000836 temp = PyNumber_InPlaceOr(free, newfree);
837 if (!temp)
838 goto error;
839 Py_DECREF(temp);
840 success = 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000841 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000842 Py_XDECREF(scopes);
843 Py_XDECREF(local);
844 Py_XDECREF(newbound);
845 Py_XDECREF(newglobal);
846 Py_XDECREF(newfree);
847 Py_XDECREF(allfree);
848 if (!success)
849 assert(PyErr_Occurred());
850 return success;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000851}
852
853static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854analyze_child_block(PySTEntryObject *entry, PyObject *bound, PyObject *free,
855 PyObject *global, PyObject* child_free)
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000856{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000857 PyObject *temp_bound = NULL, *temp_global = NULL, *temp_free = NULL;
858 PyObject *temp;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000859
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000860 /* Copy the bound and global dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000861
Martin Panter3ee62702016-06-04 04:57:19 +0000862 These dictionaries are used by all blocks enclosed by the
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000863 current block. The analyze_block() call modifies these
864 dictionaries.
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000865
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000866 */
867 temp_bound = PySet_New(bound);
868 if (!temp_bound)
869 goto error;
870 temp_free = PySet_New(free);
871 if (!temp_free)
872 goto error;
873 temp_global = PySet_New(global);
874 if (!temp_global)
875 goto error;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000876
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000877 if (!analyze_block(entry, temp_bound, temp_free, temp_global))
878 goto error;
879 temp = PyNumber_InPlaceOr(child_free, temp_free);
880 if (!temp)
881 goto error;
882 Py_DECREF(temp);
883 Py_DECREF(temp_bound);
884 Py_DECREF(temp_free);
885 Py_DECREF(temp_global);
886 return 1;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000887 error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000888 Py_XDECREF(temp_bound);
889 Py_XDECREF(temp_free);
890 Py_XDECREF(temp_global);
891 return 0;
Jeremy Hyltonf37708e2009-03-31 15:26:37 +0000892}
893
894static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000895symtable_analyze(struct symtable *st)
896{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 PyObject *free, *global;
898 int r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000899
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000900 free = PySet_New(NULL);
901 if (!free)
902 return 0;
903 global = PySet_New(NULL);
904 if (!global) {
905 Py_DECREF(free);
906 return 0;
907 }
908 r = analyze_block(st->st_top, NULL, free, global);
909 Py_DECREF(free);
910 Py_DECREF(global);
911 return r;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000912}
913
Benjamin Peterson55e00f22008-08-17 18:02:44 +0000914/* symtable_enter_block() gets a reference via ste_new.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000915 This reference is released when the block is exited, via the DECREF
916 in symtable_exit_block().
917*/
918
919static int
920symtable_exit_block(struct symtable *st, void *ast)
921{
Benjamin Peterson609da582011-06-29 22:52:39 -0500922 Py_ssize_t size;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000923
Benjamin Peterson609da582011-06-29 22:52:39 -0500924 st->st_cur = NULL;
925 size = PyList_GET_SIZE(st->st_stack);
926 if (size) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500927 if (PyList_SetSlice(st->st_stack, size - 1, size, NULL) < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000928 return 0;
Benjamin Peterson9d872e12011-07-02 09:22:13 -0500929 if (--size)
930 st->st_cur = (PySTEntryObject *)PyList_GET_ITEM(st->st_stack, size - 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000931 }
932 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000933}
934
935static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000936symtable_enter_block(struct symtable *st, identifier name, _Py_block_ty block,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +0000937 void *ast, int lineno, int col_offset)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000938{
Benjamin Peterson609da582011-06-29 22:52:39 -0500939 PySTEntryObject *prev = NULL, *ste;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000940
Benjamin Peterson609da582011-06-29 22:52:39 -0500941 ste = ste_new(st, name, block, ast, lineno, col_offset);
942 if (ste == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000943 return 0;
Benjamin Peterson609da582011-06-29 22:52:39 -0500944 if (PyList_Append(st->st_stack, (PyObject *)ste) < 0) {
945 Py_DECREF(ste);
946 return 0;
947 }
948 prev = st->st_cur;
949 /* The entry is owned by the stack. Borrow it for st_cur. */
950 Py_DECREF(ste);
951 st->st_cur = ste;
Benjamin Peterson230b2062010-10-16 03:45:45 +0000952 if (block == ModuleBlock)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000953 st->st_global = st->st_cur->ste_symbols;
954 if (prev) {
Benjamin Peterson609da582011-06-29 22:52:39 -0500955 if (PyList_Append(prev->ste_children, (PyObject *)ste) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000956 return 0;
957 }
958 }
959 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000960}
961
Neal Norwitz46b7bda2006-01-08 01:06:06 +0000962static long
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000963symtable_lookup(struct symtable *st, PyObject *name)
964{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000965 PyObject *o;
966 PyObject *mangled = _Py_Mangle(st->st_private, name);
967 if (!mangled)
968 return 0;
969 o = PyDict_GetItem(st->st_cur->ste_symbols, mangled);
970 Py_DECREF(mangled);
971 if (!o)
972 return 0;
973 return PyLong_AsLong(o);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000974}
975
976static int
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000977symtable_add_def(struct symtable *st, PyObject *name, int flag)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000978{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000979 PyObject *o;
980 PyObject *dict;
981 long val;
982 PyObject *mangled = _Py_Mangle(st->st_private, name);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +0000983
Jeremy Hylton81e95022007-02-27 06:50:52 +0000984
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000985 if (!mangled)
986 return 0;
987 dict = st->st_cur->ste_symbols;
988 if ((o = PyDict_GetItem(dict, mangled))) {
989 val = PyLong_AS_LONG(o);
990 if ((flag & DEF_PARAM) && (val & DEF_PARAM)) {
991 /* Is it better to use 'mangled' or 'name' here? */
992 PyErr_Format(PyExc_SyntaxError, DUPLICATE_ARGUMENT, name);
Victor Stinner14e461d2013-08-26 22:28:21 +0200993 PyErr_SyntaxLocationObject(st->st_filename,
994 st->st_cur->ste_lineno,
995 st->st_cur->ste_col_offset);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000996 goto error;
997 }
998 val |= flag;
999 } else
1000 val = flag;
1001 o = PyLong_FromLong(val);
1002 if (o == NULL)
1003 goto error;
1004 if (PyDict_SetItem(dict, mangled, o) < 0) {
1005 Py_DECREF(o);
1006 goto error;
1007 }
1008 Py_DECREF(o);
1009
1010 if (flag & DEF_PARAM) {
1011 if (PyList_Append(st->st_cur->ste_varnames, mangled) < 0)
1012 goto error;
1013 } else if (flag & DEF_GLOBAL) {
1014 /* XXX need to update DEF_GLOBAL for other flags too;
1015 perhaps only DEF_FREE_GLOBAL */
1016 val = flag;
1017 if ((o = PyDict_GetItem(st->st_global, mangled))) {
1018 val |= PyLong_AS_LONG(o);
1019 }
1020 o = PyLong_FromLong(val);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001021 if (o == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001022 goto error;
1023 if (PyDict_SetItem(st->st_global, mangled, o) < 0) {
1024 Py_DECREF(o);
1025 goto error;
1026 }
1027 Py_DECREF(o);
1028 }
1029 Py_DECREF(mangled);
1030 return 1;
Neil Schemenauer8b528b22005-10-23 18:37:42 +00001031
1032error:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001033 Py_DECREF(mangled);
1034 return 0;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001035}
1036
1037/* VISIT, VISIT_SEQ and VIST_SEQ_TAIL take an ASDL type as their second argument.
1038 They use the ASDL name to synthesize the name of the C type and the visit
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001039 function.
1040
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001041 VISIT_SEQ_TAIL permits the start of an ASDL sequence to be skipped, which is
1042 useful if the first node in the sequence requires special treatment.
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001043
1044 VISIT_QUIT macro returns the specified value exiting from the function but
1045 first adjusts current recursion counter depth.
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001046*/
1047
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001048#define VISIT_QUIT(ST, X) \
1049 return --(ST)->recursion_depth,(X)
1050
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001051#define VISIT(ST, TYPE, V) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 if (!symtable_visit_ ## TYPE((ST), (V))) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001053 VISIT_QUIT((ST), 0);
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001054
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001055#define VISIT_SEQ(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 int i; \
1057 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1058 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
1059 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1060 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001061 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001062 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001063}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001064
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001065#define VISIT_SEQ_TAIL(ST, TYPE, SEQ, START) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001066 int i; \
1067 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
1068 for (i = (START); i < asdl_seq_LEN(seq); i++) { \
1069 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
1070 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001071 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001072 } \
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001073}
Neal Norwitzb6fc9df2005-11-13 18:50:34 +00001074
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001075#define VISIT_SEQ_WITH_NULL(ST, TYPE, SEQ) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001076 int i = 0; \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001077 asdl_seq *seq = (SEQ); /* avoid variable capture */ \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001078 for (i = 0; i < asdl_seq_LEN(seq); i++) { \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001079 TYPE ## _ty elt = (TYPE ## _ty)asdl_seq_GET(seq, i); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001080 if (!elt) continue; /* can be NULL */ \
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001081 if (!symtable_visit_ ## TYPE((ST), elt)) \
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001082 VISIT_QUIT((ST), 0); \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001083 } \
Guido van Rossum4f72a782006-10-27 23:31:49 +00001084}
1085
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001086static int
Benjamin Petersond9c87022012-10-31 20:26:20 -04001087symtable_record_directive(struct symtable *st, identifier name, stmt_ty s)
1088{
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001089 PyObject *data, *mangled;
Benjamin Petersond9c87022012-10-31 20:26:20 -04001090 int res;
1091 if (!st->st_cur->ste_directives) {
1092 st->st_cur->ste_directives = PyList_New(0);
1093 if (!st->st_cur->ste_directives)
1094 return 0;
1095 }
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -06001096 mangled = _Py_Mangle(st->st_private, name);
1097 if (!mangled)
1098 return 0;
1099 data = Py_BuildValue("(Nii)", mangled, s->lineno, s->col_offset);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001100 if (!data)
1101 return 0;
1102 res = PyList_Append(st->st_cur->ste_directives, data);
1103 Py_DECREF(data);
1104 return res == 0;
1105}
1106
1107
1108static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001109symtable_visit_stmt(struct symtable *st, stmt_ty s)
1110{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001111 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001112 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001113 "maximum recursion depth exceeded during compilation");
1114 VISIT_QUIT(st, 0);
1115 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001116 switch (s->kind) {
1117 case FunctionDef_kind:
1118 if (!symtable_add_def(st, s->v.FunctionDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001119 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001120 if (s->v.FunctionDef.args->defaults)
1121 VISIT_SEQ(st, expr, s->v.FunctionDef.args->defaults);
1122 if (s->v.FunctionDef.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001123 VISIT_SEQ_WITH_NULL(st, expr, s->v.FunctionDef.args->kw_defaults);
Yury Selivanov75445082015-05-11 22:57:16 -04001124 if (!symtable_visit_annotations(st, s, s->v.FunctionDef.args,
1125 s->v.FunctionDef.returns))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001126 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 if (s->v.FunctionDef.decorator_list)
1128 VISIT_SEQ(st, expr, s->v.FunctionDef.decorator_list);
1129 if (!symtable_enter_block(st, s->v.FunctionDef.name,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001130 FunctionBlock, (void *)s, s->lineno,
1131 s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001132 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001133 VISIT(st, arguments, s->v.FunctionDef.args);
1134 VISIT_SEQ(st, stmt, s->v.FunctionDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001135 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001136 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001137 break;
1138 case ClassDef_kind: {
1139 PyObject *tmp;
1140 if (!symtable_add_def(st, s->v.ClassDef.name, DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001141 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001142 VISIT_SEQ(st, expr, s->v.ClassDef.bases);
1143 VISIT_SEQ(st, keyword, s->v.ClassDef.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001144 if (s->v.ClassDef.decorator_list)
1145 VISIT_SEQ(st, expr, s->v.ClassDef.decorator_list);
1146 if (!symtable_enter_block(st, s->v.ClassDef.name, ClassBlock,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001147 (void *)s, s->lineno, s->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001148 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001149 tmp = st->st_private;
1150 st->st_private = s->v.ClassDef.name;
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001151 VISIT_SEQ(st, stmt, s->v.ClassDef.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001152 st->st_private = tmp;
1153 if (!symtable_exit_block(st, s))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001154 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 break;
1156 }
1157 case Return_kind:
1158 if (s->v.Return.value) {
1159 VISIT(st, expr, s->v.Return.value);
1160 st->st_cur->ste_returns_value = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001161 }
1162 break;
1163 case Delete_kind:
1164 VISIT_SEQ(st, expr, s->v.Delete.targets);
1165 break;
1166 case Assign_kind:
1167 VISIT_SEQ(st, expr, s->v.Assign.targets);
1168 VISIT(st, expr, s->v.Assign.value);
1169 break;
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001170 case AnnAssign_kind:
1171 if (s->v.AnnAssign.target->kind == Name_kind) {
1172 expr_ty e_name = s->v.AnnAssign.target;
1173 long cur = symtable_lookup(st, e_name->v.Name.id);
1174 if (cur < 0) {
1175 VISIT_QUIT(st, 0);
1176 }
1177 if ((cur & (DEF_GLOBAL | DEF_NONLOCAL))
1178 && s->v.AnnAssign.simple) {
1179 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001180 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1181 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001182 PyErr_SyntaxLocationObject(st->st_filename,
1183 s->lineno,
1184 s->col_offset);
1185 VISIT_QUIT(st, 0);
1186 }
1187 if (s->v.AnnAssign.simple &&
1188 !symtable_add_def(st, e_name->v.Name.id,
1189 DEF_ANNOT | DEF_LOCAL)) {
1190 VISIT_QUIT(st, 0);
1191 }
1192 else {
1193 if (s->v.AnnAssign.value
1194 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1195 VISIT_QUIT(st, 0);
1196 }
1197 }
1198 }
1199 else {
1200 VISIT(st, expr, s->v.AnnAssign.target);
1201 }
1202 VISIT(st, expr, s->v.AnnAssign.annotation);
1203 if (s->v.AnnAssign.value) {
1204 VISIT(st, expr, s->v.AnnAssign.value);
1205 }
1206 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001207 case AugAssign_kind:
1208 VISIT(st, expr, s->v.AugAssign.target);
1209 VISIT(st, expr, s->v.AugAssign.value);
1210 break;
1211 case For_kind:
1212 VISIT(st, expr, s->v.For.target);
1213 VISIT(st, expr, s->v.For.iter);
1214 VISIT_SEQ(st, stmt, s->v.For.body);
1215 if (s->v.For.orelse)
1216 VISIT_SEQ(st, stmt, s->v.For.orelse);
1217 break;
1218 case While_kind:
1219 VISIT(st, expr, s->v.While.test);
1220 VISIT_SEQ(st, stmt, s->v.While.body);
1221 if (s->v.While.orelse)
1222 VISIT_SEQ(st, stmt, s->v.While.orelse);
1223 break;
1224 case If_kind:
1225 /* XXX if 0: and lookup_yield() hacks */
1226 VISIT(st, expr, s->v.If.test);
1227 VISIT_SEQ(st, stmt, s->v.If.body);
1228 if (s->v.If.orelse)
1229 VISIT_SEQ(st, stmt, s->v.If.orelse);
1230 break;
1231 case Raise_kind:
1232 if (s->v.Raise.exc) {
1233 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001234 if (s->v.Raise.cause) {
1235 VISIT(st, expr, s->v.Raise.cause);
1236 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001237 }
1238 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001239 case Try_kind:
1240 VISIT_SEQ(st, stmt, s->v.Try.body);
1241 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1242 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1243 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001244 break;
1245 case Assert_kind:
1246 VISIT(st, expr, s->v.Assert.test);
1247 if (s->v.Assert.msg)
1248 VISIT(st, expr, s->v.Assert.msg);
1249 break;
1250 case Import_kind:
1251 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001252 break;
1253 case ImportFrom_kind:
1254 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001255 break;
1256 case Global_kind: {
1257 int i;
1258 asdl_seq *seq = s->v.Global.names;
1259 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1260 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001261 long cur = symtable_lookup(st, name);
1262 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001263 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001264 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1265 const char* msg;
1266 if (cur & DEF_PARAM) {
1267 msg = GLOBAL_PARAM;
1268 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001269 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001270 } else if (cur & DEF_ANNOT) {
1271 msg = GLOBAL_ANNOT;
1272 } else { /* DEF_LOCAL */
1273 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001274 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001275 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001276 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001277 PyErr_SyntaxLocationObject(st->st_filename,
1278 s->lineno,
1279 s->col_offset);
1280 VISIT_QUIT(st, 0);
1281 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001282 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001283 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001284 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001285 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 }
1287 break;
1288 }
1289 case Nonlocal_kind: {
1290 int i;
1291 asdl_seq *seq = s->v.Nonlocal.names;
1292 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1293 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001294 long cur = symtable_lookup(st, name);
1295 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001296 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001297 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1298 const char* msg;
1299 if (cur & DEF_PARAM) {
1300 msg = NONLOCAL_PARAM;
1301 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001302 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001303 } else if (cur & DEF_ANNOT) {
1304 msg = NONLOCAL_ANNOT;
1305 } else { /* DEF_LOCAL */
1306 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001307 }
1308 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001309 PyErr_SyntaxLocationObject(st->st_filename,
1310 s->lineno,
1311 s->col_offset);
1312 VISIT_QUIT(st, 0);
1313 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001314 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001315 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001316 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001317 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001318 }
1319 break;
1320 }
1321 case Expr_kind:
1322 VISIT(st, expr, s->v.Expr.value);
1323 break;
1324 case Pass_kind:
1325 case Break_kind:
1326 case Continue_kind:
1327 /* nothing to do here */
1328 break;
1329 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001330 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001331 VISIT_SEQ(st, stmt, s->v.With.body);
1332 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001333 case AsyncFunctionDef_kind:
1334 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1335 VISIT_QUIT(st, 0);
1336 if (s->v.AsyncFunctionDef.args->defaults)
1337 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1338 if (s->v.AsyncFunctionDef.args->kw_defaults)
1339 VISIT_SEQ_WITH_NULL(st, expr,
1340 s->v.AsyncFunctionDef.args->kw_defaults);
1341 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1342 s->v.AsyncFunctionDef.returns))
1343 VISIT_QUIT(st, 0);
1344 if (s->v.AsyncFunctionDef.decorator_list)
1345 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1346 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1347 FunctionBlock, (void *)s, s->lineno,
1348 s->col_offset))
1349 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001350 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001351 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1352 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1353 if (!symtable_exit_block(st, s))
1354 VISIT_QUIT(st, 0);
1355 break;
1356 case AsyncWith_kind:
1357 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1358 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1359 break;
1360 case AsyncFor_kind:
1361 VISIT(st, expr, s->v.AsyncFor.target);
1362 VISIT(st, expr, s->v.AsyncFor.iter);
1363 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1364 if (s->v.AsyncFor.orelse)
1365 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1366 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001367 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001368 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001369}
1370
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001371static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001372symtable_visit_expr(struct symtable *st, expr_ty e)
1373{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001374 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001375 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001376 "maximum recursion depth exceeded during compilation");
1377 VISIT_QUIT(st, 0);
1378 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001379 switch (e->kind) {
1380 case BoolOp_kind:
1381 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1382 break;
1383 case BinOp_kind:
1384 VISIT(st, expr, e->v.BinOp.left);
1385 VISIT(st, expr, e->v.BinOp.right);
1386 break;
1387 case UnaryOp_kind:
1388 VISIT(st, expr, e->v.UnaryOp.operand);
1389 break;
1390 case Lambda_kind: {
1391 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001392 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001393 if (e->v.Lambda.args->defaults)
1394 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001395 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001396 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001397 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001398 FunctionBlock, (void *)e, e->lineno,
1399 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001400 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001401 VISIT(st, arguments, e->v.Lambda.args);
1402 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001403 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001404 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001405 break;
1406 }
1407 case IfExp_kind:
1408 VISIT(st, expr, e->v.IfExp.test);
1409 VISIT(st, expr, e->v.IfExp.body);
1410 VISIT(st, expr, e->v.IfExp.orelse);
1411 break;
1412 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001413 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001414 VISIT_SEQ(st, expr, e->v.Dict.values);
1415 break;
1416 case Set_kind:
1417 VISIT_SEQ(st, expr, e->v.Set.elts);
1418 break;
1419 case GeneratorExp_kind:
1420 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001421 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001422 break;
1423 case ListComp_kind:
1424 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001425 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001426 break;
1427 case SetComp_kind:
1428 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001429 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001430 break;
1431 case DictComp_kind:
1432 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001433 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001434 break;
1435 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001436 if (e->v.Yield.value)
1437 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001438 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001440 case YieldFrom_kind:
1441 VISIT(st, expr, e->v.YieldFrom.value);
1442 st->st_cur->ste_generator = 1;
1443 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001444 case Await_kind:
1445 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001446 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001447 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001448 case Compare_kind:
1449 VISIT(st, expr, e->v.Compare.left);
1450 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1451 break;
1452 case Call_kind:
1453 VISIT(st, expr, e->v.Call.func);
1454 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001455 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001456 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001457 case FormattedValue_kind:
1458 VISIT(st, expr, e->v.FormattedValue.value);
1459 if (e->v.FormattedValue.format_spec)
1460 VISIT(st, expr, e->v.FormattedValue.format_spec);
1461 break;
1462 case JoinedStr_kind:
1463 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1464 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001465 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001466 case Num_kind:
1467 case Str_kind:
1468 case Bytes_kind:
1469 case Ellipsis_kind:
Benjamin Peterson442f2092012-12-06 17:41:04 -05001470 case NameConstant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001471 /* Nothing to do here. */
1472 break;
1473 /* The following exprs can be assignment targets. */
1474 case Attribute_kind:
1475 VISIT(st, expr, e->v.Attribute.value);
1476 break;
1477 case Subscript_kind:
1478 VISIT(st, expr, e->v.Subscript.value);
1479 VISIT(st, slice, e->v.Subscript.slice);
1480 break;
1481 case Starred_kind:
1482 VISIT(st, expr, e->v.Starred.value);
1483 break;
1484 case Name_kind:
1485 if (!symtable_add_def(st, e->v.Name.id,
1486 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001487 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001488 /* Special-case super: it counts as a use of __class__ */
1489 if (e->v.Name.ctx == Load &&
1490 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001491 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001492 if (!GET_IDENTIFIER(__class__) ||
1493 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001494 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 }
1496 break;
1497 /* child nodes of List and Tuple will have expr_context set */
1498 case List_kind:
1499 VISIT_SEQ(st, expr, e->v.List.elts);
1500 break;
1501 case Tuple_kind:
1502 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1503 break;
1504 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001505 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001506}
1507
1508static int
1509symtable_implicit_arg(struct symtable *st, int pos)
1510{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001511 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1512 if (id == NULL)
1513 return 0;
1514 if (!symtable_add_def(st, id, DEF_PARAM)) {
1515 Py_DECREF(id);
1516 return 0;
1517 }
1518 Py_DECREF(id);
1519 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520}
1521
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001522static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001523symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001524{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001525 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001526
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001527 if (!args)
1528 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001529
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001530 for (i = 0; i < asdl_seq_LEN(args); i++) {
1531 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1532 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1533 return 0;
1534 }
1535
1536 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537}
1538
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001539static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001540symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001541{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001542 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001543
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001544 if (!args)
1545 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001546
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001547 for (i = 0; i < asdl_seq_LEN(args); i++) {
1548 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1549 if (arg->annotation)
1550 VISIT(st, expr, arg->annotation);
1551 }
1552
1553 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001554}
1555
Neal Norwitzc1505362006-12-28 06:47:50 +00001556static int
Yury Selivanov75445082015-05-11 22:57:16 -04001557symtable_visit_annotations(struct symtable *st, stmt_ty s,
1558 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001559{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001560 if (a->args && !symtable_visit_argannotations(st, a->args))
1561 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001562 if (a->vararg && a->vararg->annotation)
1563 VISIT(st, expr, a->vararg->annotation);
1564 if (a->kwarg && a->kwarg->annotation)
1565 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1567 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001568 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001569 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001570 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001571}
1572
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001573static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001574symtable_visit_arguments(struct symtable *st, arguments_ty a)
1575{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001576 /* skip default arguments inside function block
1577 XXX should ast be different?
1578 */
1579 if (a->args && !symtable_visit_params(st, a->args))
1580 return 0;
1581 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1582 return 0;
1583 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001584 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001585 return 0;
1586 st->st_cur->ste_varargs = 1;
1587 }
1588 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001589 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001590 return 0;
1591 st->st_cur->ste_varkeywords = 1;
1592 }
1593 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594}
1595
1596
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001597static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001598symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1599{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001600 if (eh->v.ExceptHandler.type)
1601 VISIT(st, expr, eh->v.ExceptHandler.type);
1602 if (eh->v.ExceptHandler.name)
1603 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1604 return 0;
1605 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1606 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001607}
1608
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001609static int
1610symtable_visit_withitem(struct symtable *st, withitem_ty item)
1611{
1612 VISIT(st, expr, item->context_expr);
1613 if (item->optional_vars) {
1614 VISIT(st, expr, item->optional_vars);
1615 }
1616 return 1;
1617}
1618
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001619
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001620static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001621symtable_visit_alias(struct symtable *st, alias_ty a)
1622{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001623 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001624 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001625 dotted package name (e.g. spam.eggs)
1626 */
1627 PyObject *store_name;
1628 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001629 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1630 PyUnicode_GET_LENGTH(name), 1);
1631 if (dot != -1) {
1632 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001633 if (!store_name)
1634 return 0;
1635 }
1636 else {
1637 store_name = name;
1638 Py_INCREF(store_name);
1639 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001640 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001641 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1642 Py_DECREF(store_name);
1643 return r;
1644 }
1645 else {
1646 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001647 int lineno = st->st_cur->ste_lineno;
1648 int col_offset = st->st_cur->ste_col_offset;
1649 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Victor Stinner14e461d2013-08-26 22:28:21 +02001650 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001651 Py_DECREF(store_name);
1652 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001653 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001654 Py_DECREF(store_name);
1655 return 1;
1656 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657}
1658
1659
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001660static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001661symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1662{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001663 VISIT(st, expr, lc->target);
1664 VISIT(st, expr, lc->iter);
1665 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001666 if (lc->is_async) {
1667 st->st_cur->ste_coroutine = 1;
1668 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670}
1671
1672
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001673static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674symtable_visit_keyword(struct symtable *st, keyword_ty k)
1675{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001676 VISIT(st, expr, k->value);
1677 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678}
1679
1680
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001681static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001682symtable_visit_slice(struct symtable *st, slice_ty s)
1683{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001684 switch (s->kind) {
1685 case Slice_kind:
1686 if (s->v.Slice.lower)
1687 VISIT(st, expr, s->v.Slice.lower)
1688 if (s->v.Slice.upper)
1689 VISIT(st, expr, s->v.Slice.upper)
1690 if (s->v.Slice.step)
1691 VISIT(st, expr, s->v.Slice.step)
1692 break;
1693 case ExtSlice_kind:
1694 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1695 break;
1696 case Index_kind:
1697 VISIT(st, expr, s->v.Index.value)
1698 break;
1699 }
1700 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001701}
1702
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001703static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001704symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001705 identifier scope_name, asdl_seq *generators,
1706 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001707{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001708 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001709 comprehension_ty outermost = ((comprehension_ty)
1710 asdl_seq_GET(generators, 0));
1711 /* Outermost iterator is evaluated in current scope */
1712 VISIT(st, expr, outermost->iter);
1713 /* Create comprehension scope for the rest */
1714 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001715 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1716 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001717 return 0;
1718 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001719 if (outermost->is_async) {
1720 st->st_cur->ste_coroutine = 1;
1721 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001722 /* Outermost iter is received as an argument */
1723 if (!symtable_implicit_arg(st, 0)) {
1724 symtable_exit_block(st, (void *)e);
1725 return 0;
1726 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001727 VISIT(st, expr, outermost->target);
1728 VISIT_SEQ(st, expr, outermost->ifs);
1729 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001730 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001731 VISIT(st, expr, value);
1732 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001733 if (st->st_cur->ste_generator) {
1734 PyObject *msg = PyUnicode_FromString(
1735 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1736 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1737 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1738 "'yield' inside generator expression");
1739 if (msg == NULL) {
1740 symtable_exit_block(st, (void *)e);
1741 return 0;
1742 }
1743 if (PyErr_WarnExplicitObject(PyExc_DeprecationWarning,
1744 msg, st->st_filename, st->st_cur->ste_lineno,
1745 NULL, NULL) == -1)
1746 {
1747 if (PyErr_ExceptionMatches(PyExc_DeprecationWarning)) {
1748 /* Replace the DeprecationWarning exception with a SyntaxError
1749 to get a more accurate error report */
1750 PyErr_Clear();
1751 PyErr_SetObject(PyExc_SyntaxError, msg);
1752 PyErr_SyntaxLocationObject(st->st_filename,
1753 st->st_cur->ste_lineno,
1754 st->st_cur->ste_col_offset);
1755 }
1756 Py_DECREF(msg);
1757 symtable_exit_block(st, (void *)e);
1758 return 0;
1759 }
1760 Py_DECREF(msg);
1761 }
1762 st->st_cur->ste_generator |= is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001763 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001764}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001765
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001766static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001767symtable_visit_genexp(struct symtable *st, expr_ty e)
1768{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001769 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1770 e->v.GeneratorExp.generators,
1771 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001772}
1773
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001774static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001775symtable_visit_listcomp(struct symtable *st, expr_ty e)
1776{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001777 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1778 e->v.ListComp.generators,
1779 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001780}
1781
1782static int
1783symtable_visit_setcomp(struct symtable *st, expr_ty e)
1784{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001785 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1786 e->v.SetComp.generators,
1787 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001788}
1789
1790static int
1791symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1792{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001793 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1794 e->v.DictComp.generators,
1795 e->v.DictComp.key,
1796 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001797}