blob: dae6fda0e4c736d1fcf3920bdb199c76b0c294a5 [file] [log] [blame]
Jeremy Hyltoncb17ae82001-02-09 22:22:18 +00001#include "Python.h"
Victor Stinner27e2d1f2018-11-01 00:52:28 +01002#include "pycore_state.h"
Eric Snow2ebc5ce2017-09-07 23:51:28 -06003#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)),
Ammar Askar025eb982018-09-24 17:12:49 -0400393 PyLong_AsLong(PyTuple_GET_ITEM(data, 2)) + 1);
Benjamin Peterson3cc8f4b2015-12-29 10:08:34 -0600394
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);
Zackery Spytzfc439d22018-10-10 23:05:35 -0600628 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,
Ammar Askar025eb982018-09-24 17:12:49 -0400995 st->st_cur->ste_col_offset + 1);
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))
Pablo Galindode2aea02018-10-14 18:01:03 +01001178 && (st->st_cur->ste_symbols != st->st_global)
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001179 && s->v.AnnAssign.simple) {
1180 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001181 cur & DEF_GLOBAL ? GLOBAL_ANNOT : NONLOCAL_ANNOT,
1182 e_name->v.Name.id);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001183 PyErr_SyntaxLocationObject(st->st_filename,
1184 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001185 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001186 VISIT_QUIT(st, 0);
1187 }
1188 if (s->v.AnnAssign.simple &&
1189 !symtable_add_def(st, e_name->v.Name.id,
1190 DEF_ANNOT | DEF_LOCAL)) {
1191 VISIT_QUIT(st, 0);
1192 }
1193 else {
1194 if (s->v.AnnAssign.value
1195 && !symtable_add_def(st, e_name->v.Name.id, DEF_LOCAL)) {
1196 VISIT_QUIT(st, 0);
1197 }
1198 }
1199 }
1200 else {
1201 VISIT(st, expr, s->v.AnnAssign.target);
1202 }
1203 VISIT(st, expr, s->v.AnnAssign.annotation);
1204 if (s->v.AnnAssign.value) {
1205 VISIT(st, expr, s->v.AnnAssign.value);
1206 }
1207 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001208 case AugAssign_kind:
1209 VISIT(st, expr, s->v.AugAssign.target);
1210 VISIT(st, expr, s->v.AugAssign.value);
1211 break;
1212 case For_kind:
1213 VISIT(st, expr, s->v.For.target);
1214 VISIT(st, expr, s->v.For.iter);
1215 VISIT_SEQ(st, stmt, s->v.For.body);
1216 if (s->v.For.orelse)
1217 VISIT_SEQ(st, stmt, s->v.For.orelse);
1218 break;
1219 case While_kind:
1220 VISIT(st, expr, s->v.While.test);
1221 VISIT_SEQ(st, stmt, s->v.While.body);
1222 if (s->v.While.orelse)
1223 VISIT_SEQ(st, stmt, s->v.While.orelse);
1224 break;
1225 case If_kind:
1226 /* XXX if 0: and lookup_yield() hacks */
1227 VISIT(st, expr, s->v.If.test);
1228 VISIT_SEQ(st, stmt, s->v.If.body);
1229 if (s->v.If.orelse)
1230 VISIT_SEQ(st, stmt, s->v.If.orelse);
1231 break;
1232 case Raise_kind:
1233 if (s->v.Raise.exc) {
1234 VISIT(st, expr, s->v.Raise.exc);
Eli Benderskydd97fbb2011-04-10 07:37:26 +03001235 if (s->v.Raise.cause) {
1236 VISIT(st, expr, s->v.Raise.cause);
1237 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001238 }
1239 break;
Benjamin Peterson43af12b2011-05-29 11:43:10 -05001240 case Try_kind:
1241 VISIT_SEQ(st, stmt, s->v.Try.body);
1242 VISIT_SEQ(st, stmt, s->v.Try.orelse);
1243 VISIT_SEQ(st, excepthandler, s->v.Try.handlers);
1244 VISIT_SEQ(st, stmt, s->v.Try.finalbody);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001245 break;
1246 case Assert_kind:
1247 VISIT(st, expr, s->v.Assert.test);
1248 if (s->v.Assert.msg)
1249 VISIT(st, expr, s->v.Assert.msg);
1250 break;
1251 case Import_kind:
1252 VISIT_SEQ(st, alias, s->v.Import.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001253 break;
1254 case ImportFrom_kind:
1255 VISIT_SEQ(st, alias, s->v.ImportFrom.names);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001256 break;
1257 case Global_kind: {
1258 int i;
1259 asdl_seq *seq = s->v.Global.names;
1260 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1261 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001262 long cur = symtable_lookup(st, name);
1263 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001264 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001265 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1266 const char* msg;
1267 if (cur & DEF_PARAM) {
1268 msg = GLOBAL_PARAM;
1269 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001270 msg = GLOBAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001271 } else if (cur & DEF_ANNOT) {
1272 msg = GLOBAL_ANNOT;
1273 } else { /* DEF_LOCAL */
1274 msg = GLOBAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001275 }
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001276 PyErr_Format(PyExc_SyntaxError,
Guido van Rossum6cff8742016-09-09 09:36:26 -07001277 msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001278 PyErr_SyntaxLocationObject(st->st_filename,
1279 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001280 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001281 VISIT_QUIT(st, 0);
1282 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001283 if (!symtable_add_def(st, name, DEF_GLOBAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001284 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001285 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001286 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001287 }
1288 break;
1289 }
1290 case Nonlocal_kind: {
1291 int i;
1292 asdl_seq *seq = s->v.Nonlocal.names;
1293 for (i = 0; i < asdl_seq_LEN(seq); i++) {
1294 identifier name = (identifier)asdl_seq_GET(seq, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 long cur = symtable_lookup(st, name);
1296 if (cur < 0)
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001297 VISIT_QUIT(st, 0);
Ivan Levkivskyi8c83c232017-10-26 23:28:35 +02001298 if (cur & (DEF_PARAM | DEF_LOCAL | USE | DEF_ANNOT)) {
1299 const char* msg;
1300 if (cur & DEF_PARAM) {
1301 msg = NONLOCAL_PARAM;
1302 } else if (cur & USE) {
Guido van Rossum6cff8742016-09-09 09:36:26 -07001303 msg = NONLOCAL_AFTER_USE;
Christian Heimes517507c2016-09-23 20:26:30 +02001304 } else if (cur & DEF_ANNOT) {
1305 msg = NONLOCAL_ANNOT;
1306 } else { /* DEF_LOCAL */
1307 msg = NONLOCAL_AFTER_ASSIGN;
Guido van Rossum6cff8742016-09-09 09:36:26 -07001308 }
1309 PyErr_Format(PyExc_SyntaxError, msg, name);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001310 PyErr_SyntaxLocationObject(st->st_filename,
1311 s->lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001312 s->col_offset + 1);
Yury Selivanovf8cb8a12016-09-08 20:50:03 -07001313 VISIT_QUIT(st, 0);
1314 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001315 if (!symtable_add_def(st, name, DEF_NONLOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001316 VISIT_QUIT(st, 0);
Benjamin Petersond9c87022012-10-31 20:26:20 -04001317 if (!symtable_record_directive(st, name, s))
Nick Coghlane69bfc32012-11-04 23:53:15 +10001318 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001319 }
1320 break;
1321 }
1322 case Expr_kind:
1323 VISIT(st, expr, s->v.Expr.value);
1324 break;
1325 case Pass_kind:
1326 case Break_kind:
1327 case Continue_kind:
1328 /* nothing to do here */
1329 break;
1330 case With_kind:
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001331 VISIT_SEQ(st, withitem, s->v.With.items);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001332 VISIT_SEQ(st, stmt, s->v.With.body);
1333 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001334 case AsyncFunctionDef_kind:
1335 if (!symtable_add_def(st, s->v.AsyncFunctionDef.name, DEF_LOCAL))
1336 VISIT_QUIT(st, 0);
1337 if (s->v.AsyncFunctionDef.args->defaults)
1338 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.args->defaults);
1339 if (s->v.AsyncFunctionDef.args->kw_defaults)
1340 VISIT_SEQ_WITH_NULL(st, expr,
1341 s->v.AsyncFunctionDef.args->kw_defaults);
1342 if (!symtable_visit_annotations(st, s, s->v.AsyncFunctionDef.args,
1343 s->v.AsyncFunctionDef.returns))
1344 VISIT_QUIT(st, 0);
1345 if (s->v.AsyncFunctionDef.decorator_list)
1346 VISIT_SEQ(st, expr, s->v.AsyncFunctionDef.decorator_list);
1347 if (!symtable_enter_block(st, s->v.AsyncFunctionDef.name,
1348 FunctionBlock, (void *)s, s->lineno,
1349 s->col_offset))
1350 VISIT_QUIT(st, 0);
Yury Selivanoveb636452016-09-08 22:01:51 -07001351 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001352 VISIT(st, arguments, s->v.AsyncFunctionDef.args);
1353 VISIT_SEQ(st, stmt, s->v.AsyncFunctionDef.body);
1354 if (!symtable_exit_block(st, s))
1355 VISIT_QUIT(st, 0);
1356 break;
1357 case AsyncWith_kind:
1358 VISIT_SEQ(st, withitem, s->v.AsyncWith.items);
1359 VISIT_SEQ(st, stmt, s->v.AsyncWith.body);
1360 break;
1361 case AsyncFor_kind:
1362 VISIT(st, expr, s->v.AsyncFor.target);
1363 VISIT(st, expr, s->v.AsyncFor.iter);
1364 VISIT_SEQ(st, stmt, s->v.AsyncFor.body);
1365 if (s->v.AsyncFor.orelse)
1366 VISIT_SEQ(st, stmt, s->v.AsyncFor.orelse);
1367 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001368 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001369 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001370}
1371
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001373symtable_visit_expr(struct symtable *st, expr_ty e)
1374{
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001375 if (++st->recursion_depth > st->recursion_limit) {
Yury Selivanovf488fb42015-07-03 01:04:23 -04001376 PyErr_SetString(PyExc_RecursionError,
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001377 "maximum recursion depth exceeded during compilation");
1378 VISIT_QUIT(st, 0);
1379 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001380 switch (e->kind) {
1381 case BoolOp_kind:
1382 VISIT_SEQ(st, expr, e->v.BoolOp.values);
1383 break;
1384 case BinOp_kind:
1385 VISIT(st, expr, e->v.BinOp.left);
1386 VISIT(st, expr, e->v.BinOp.right);
1387 break;
1388 case UnaryOp_kind:
1389 VISIT(st, expr, e->v.UnaryOp.operand);
1390 break;
1391 case Lambda_kind: {
1392 if (!GET_IDENTIFIER(lambda))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001393 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001394 if (e->v.Lambda.args->defaults)
1395 VISIT_SEQ(st, expr, e->v.Lambda.args->defaults);
Amaury Forgeot d'Arc97c1bef2011-11-04 22:17:45 +01001396 if (e->v.Lambda.args->kw_defaults)
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001397 VISIT_SEQ_WITH_NULL(st, expr, e->v.Lambda.args->kw_defaults);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001398 if (!symtable_enter_block(st, lambda,
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001399 FunctionBlock, (void *)e, e->lineno,
1400 e->col_offset))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001401 VISIT_QUIT(st, 0);
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001402 VISIT(st, arguments, e->v.Lambda.args);
1403 VISIT(st, expr, e->v.Lambda.body);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001404 if (!symtable_exit_block(st, (void *)e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001405 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001406 break;
1407 }
1408 case IfExp_kind:
1409 VISIT(st, expr, e->v.IfExp.test);
1410 VISIT(st, expr, e->v.IfExp.body);
1411 VISIT(st, expr, e->v.IfExp.orelse);
1412 break;
1413 case Dict_kind:
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001414 VISIT_SEQ_WITH_NULL(st, expr, e->v.Dict.keys);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001415 VISIT_SEQ(st, expr, e->v.Dict.values);
1416 break;
1417 case Set_kind:
1418 VISIT_SEQ(st, expr, e->v.Set.elts);
1419 break;
1420 case GeneratorExp_kind:
1421 if (!symtable_visit_genexp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001422 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001423 break;
1424 case ListComp_kind:
1425 if (!symtable_visit_listcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001426 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001427 break;
1428 case SetComp_kind:
1429 if (!symtable_visit_setcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001430 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001431 break;
1432 case DictComp_kind:
1433 if (!symtable_visit_dictcomp(st, e))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001434 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001435 break;
1436 case Yield_kind:
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001437 if (e->v.Yield.value)
1438 VISIT(st, expr, e->v.Yield.value);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001439 st->st_cur->ste_generator = 1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001440 break;
Mark Dickinsonded35ae2012-11-25 14:36:26 +00001441 case YieldFrom_kind:
1442 VISIT(st, expr, e->v.YieldFrom.value);
1443 st->st_cur->ste_generator = 1;
1444 break;
Yury Selivanov75445082015-05-11 22:57:16 -04001445 case Await_kind:
1446 VISIT(st, expr, e->v.Await.value);
Yury Selivanoveb636452016-09-08 22:01:51 -07001447 st->st_cur->ste_coroutine = 1;
Yury Selivanov75445082015-05-11 22:57:16 -04001448 break;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001449 case Compare_kind:
1450 VISIT(st, expr, e->v.Compare.left);
1451 VISIT_SEQ(st, expr, e->v.Compare.comparators);
1452 break;
1453 case Call_kind:
1454 VISIT(st, expr, e->v.Call.func);
1455 VISIT_SEQ(st, expr, e->v.Call.args);
Benjamin Peterson025e9eb2015-05-05 20:16:41 -04001456 VISIT_SEQ_WITH_NULL(st, keyword, e->v.Call.keywords);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 break;
Eric V. Smith235a6f02015-09-19 14:51:32 -04001458 case FormattedValue_kind:
1459 VISIT(st, expr, e->v.FormattedValue.value);
1460 if (e->v.FormattedValue.format_spec)
1461 VISIT(st, expr, e->v.FormattedValue.format_spec);
1462 break;
1463 case JoinedStr_kind:
1464 VISIT_SEQ(st, expr, e->v.JoinedStr.values);
1465 break;
Victor Stinnerf2c1aa12016-01-26 00:40:57 +01001466 case Constant_kind:
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 /* Nothing to do here. */
1468 break;
1469 /* The following exprs can be assignment targets. */
1470 case Attribute_kind:
1471 VISIT(st, expr, e->v.Attribute.value);
1472 break;
1473 case Subscript_kind:
1474 VISIT(st, expr, e->v.Subscript.value);
1475 VISIT(st, slice, e->v.Subscript.slice);
1476 break;
1477 case Starred_kind:
1478 VISIT(st, expr, e->v.Starred.value);
1479 break;
1480 case Name_kind:
1481 if (!symtable_add_def(st, e->v.Name.id,
1482 e->v.Name.ctx == Load ? USE : DEF_LOCAL))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001483 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 /* Special-case super: it counts as a use of __class__ */
1485 if (e->v.Name.ctx == Load &&
1486 st->st_cur->ste_type == FunctionBlock &&
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001487 _PyUnicode_EqualToASCIIString(e->v.Name.id, "super")) {
Nick Coghlan0b43bcf2012-05-27 18:17:07 +10001488 if (!GET_IDENTIFIER(__class__) ||
1489 !symtable_add_def(st, __class__, USE))
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001490 VISIT_QUIT(st, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001491 }
1492 break;
1493 /* child nodes of List and Tuple will have expr_context set */
1494 case List_kind:
1495 VISIT_SEQ(st, expr, e->v.List.elts);
1496 break;
1497 case Tuple_kind:
1498 VISIT_SEQ(st, expr, e->v.Tuple.elts);
1499 break;
1500 }
Nick Coghlanaab9c2b2012-11-04 23:14:34 +10001501 VISIT_QUIT(st, 1);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001502}
1503
1504static int
1505symtable_implicit_arg(struct symtable *st, int pos)
1506{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001507 PyObject *id = PyUnicode_FromFormat(".%d", pos);
1508 if (id == NULL)
1509 return 0;
1510 if (!symtable_add_def(st, id, DEF_PARAM)) {
1511 Py_DECREF(id);
1512 return 0;
1513 }
1514 Py_DECREF(id);
1515 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001516}
1517
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001518static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001519symtable_visit_params(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001520{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001521 int i;
Neal Norwitzc1505362006-12-28 06:47:50 +00001522
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 if (!args)
1524 return -1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001525
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001526 for (i = 0; i < asdl_seq_LEN(args); i++) {
1527 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1528 if (!symtable_add_def(st, arg->arg, DEF_PARAM))
1529 return 0;
1530 }
1531
1532 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001533}
1534
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001535static int
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001536symtable_visit_argannotations(struct symtable *st, asdl_seq *args)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001537{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 int i;
Guido van Rossum1bc535d2007-05-15 18:46:22 +00001539
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001540 if (!args)
1541 return -1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001542
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001543 for (i = 0; i < asdl_seq_LEN(args); i++) {
1544 arg_ty arg = (arg_ty)asdl_seq_GET(args, i);
1545 if (arg->annotation)
1546 VISIT(st, expr, arg->annotation);
1547 }
1548
1549 return 1;
Neal Norwitzc1505362006-12-28 06:47:50 +00001550}
1551
Neal Norwitzc1505362006-12-28 06:47:50 +00001552static int
Yury Selivanov75445082015-05-11 22:57:16 -04001553symtable_visit_annotations(struct symtable *st, stmt_ty s,
1554 arguments_ty a, expr_ty returns)
Neal Norwitzc1505362006-12-28 06:47:50 +00001555{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001556 if (a->args && !symtable_visit_argannotations(st, a->args))
1557 return 0;
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001558 if (a->vararg && a->vararg->annotation)
1559 VISIT(st, expr, a->vararg->annotation);
1560 if (a->kwarg && a->kwarg->annotation)
1561 VISIT(st, expr, a->kwarg->annotation);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001562 if (a->kwonlyargs && !symtable_visit_argannotations(st, a->kwonlyargs))
1563 return 0;
Yury Selivanov75445082015-05-11 22:57:16 -04001564 if (returns)
Yury Selivanovb7666a32015-07-22 14:48:57 +03001565 VISIT(st, expr, returns);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001566 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001567}
1568
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001569static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001570symtable_visit_arguments(struct symtable *st, arguments_ty a)
1571{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001572 /* skip default arguments inside function block
1573 XXX should ast be different?
1574 */
1575 if (a->args && !symtable_visit_params(st, a->args))
1576 return 0;
1577 if (a->kwonlyargs && !symtable_visit_params(st, a->kwonlyargs))
1578 return 0;
1579 if (a->vararg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001580 if (!symtable_add_def(st, a->vararg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001581 return 0;
1582 st->st_cur->ste_varargs = 1;
1583 }
1584 if (a->kwarg) {
Benjamin Petersoncda75be2013-03-18 10:48:58 -07001585 if (!symtable_add_def(st, a->kwarg->arg, DEF_PARAM))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001586 return 0;
1587 st->st_cur->ste_varkeywords = 1;
1588 }
1589 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001590}
1591
1592
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001593static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001594symtable_visit_excepthandler(struct symtable *st, excepthandler_ty eh)
1595{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001596 if (eh->v.ExceptHandler.type)
1597 VISIT(st, expr, eh->v.ExceptHandler.type);
1598 if (eh->v.ExceptHandler.name)
1599 if (!symtable_add_def(st, eh->v.ExceptHandler.name, DEF_LOCAL))
1600 return 0;
1601 VISIT_SEQ(st, stmt, eh->v.ExceptHandler.body);
1602 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001603}
1604
Benjamin Petersonbf1bbc12011-05-27 13:58:08 -05001605static int
1606symtable_visit_withitem(struct symtable *st, withitem_ty item)
1607{
1608 VISIT(st, expr, item->context_expr);
1609 if (item->optional_vars) {
1610 VISIT(st, expr, item->optional_vars);
1611 }
1612 return 1;
1613}
1614
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001615
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001616static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001617symtable_visit_alias(struct symtable *st, alias_ty a)
1618{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001619 /* Compute store_name, the name actually bound by the import
Benjamin Petersonc629d512010-06-11 21:53:07 +00001620 operation. It is different than a->name when a->name is a
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001621 dotted package name (e.g. spam.eggs)
1622 */
1623 PyObject *store_name;
1624 PyObject *name = (a->asname == NULL) ? a->name : a->asname;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001625 Py_ssize_t dot = PyUnicode_FindChar(name, '.', 0,
1626 PyUnicode_GET_LENGTH(name), 1);
1627 if (dot != -1) {
1628 store_name = PyUnicode_Substring(name, 0, dot);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001629 if (!store_name)
1630 return 0;
1631 }
1632 else {
1633 store_name = name;
1634 Py_INCREF(store_name);
1635 }
Serhiy Storchakaf4934ea2016-11-16 10:17:58 +02001636 if (!_PyUnicode_EqualToASCIIString(name, "*")) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001637 int r = symtable_add_def(st, store_name, DEF_IMPORT);
1638 Py_DECREF(store_name);
1639 return r;
1640 }
1641 else {
1642 if (st->st_cur->ste_type != ModuleBlock) {
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001643 int lineno = st->st_cur->ste_lineno;
1644 int col_offset = st->st_cur->ste_col_offset;
1645 PyErr_SetString(PyExc_SyntaxError, IMPORT_STAR_WARNING);
Ammar Askar025eb982018-09-24 17:12:49 -04001646 PyErr_SyntaxLocationObject(st->st_filename, lineno, col_offset + 1);
Benjamin Petersonb7149ca2011-06-20 22:09:13 -05001647 Py_DECREF(store_name);
1648 return 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001649 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001650 Py_DECREF(store_name);
1651 return 1;
1652 }
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001653}
1654
1655
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001656static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001657symtable_visit_comprehension(struct symtable *st, comprehension_ty lc)
1658{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001659 VISIT(st, expr, lc->target);
1660 VISIT(st, expr, lc->iter);
1661 VISIT_SEQ(st, expr, lc->ifs);
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001662 if (lc->is_async) {
1663 st->st_cur->ste_coroutine = 1;
1664 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001665 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001666}
1667
1668
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001669static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001670symtable_visit_keyword(struct symtable *st, keyword_ty k)
1671{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001672 VISIT(st, expr, k->value);
1673 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001674}
1675
1676
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001677static int
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001678symtable_visit_slice(struct symtable *st, slice_ty s)
1679{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001680 switch (s->kind) {
1681 case Slice_kind:
1682 if (s->v.Slice.lower)
1683 VISIT(st, expr, s->v.Slice.lower)
1684 if (s->v.Slice.upper)
1685 VISIT(st, expr, s->v.Slice.upper)
1686 if (s->v.Slice.step)
1687 VISIT(st, expr, s->v.Slice.step)
1688 break;
1689 case ExtSlice_kind:
1690 VISIT_SEQ(st, slice, s->v.ExtSlice.dims)
1691 break;
1692 case Index_kind:
1693 VISIT(st, expr, s->v.Index.value)
1694 break;
1695 }
1696 return 1;
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001697}
1698
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001699static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001700symtable_handle_comprehension(struct symtable *st, expr_ty e,
Guido van Rossum992d4a32007-07-11 13:09:30 +00001701 identifier scope_name, asdl_seq *generators,
1702 expr_ty elt, expr_ty value)
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001703{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001704 int is_generator = (e->kind == GeneratorExp_kind);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001705 comprehension_ty outermost = ((comprehension_ty)
1706 asdl_seq_GET(generators, 0));
1707 /* Outermost iterator is evaluated in current scope */
1708 VISIT(st, expr, outermost->iter);
1709 /* Create comprehension scope for the rest */
1710 if (!scope_name ||
Benjamin Petersond4efd9e2010-09-20 23:02:10 +00001711 !symtable_enter_block(st, scope_name, FunctionBlock, (void *)e,
1712 e->lineno, e->col_offset)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001713 return 0;
1714 }
Yury Selivanov52c4e7c2016-09-09 10:36:01 -07001715 if (outermost->is_async) {
1716 st->st_cur->ste_coroutine = 1;
1717 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001718 /* Outermost iter is received as an argument */
1719 if (!symtable_implicit_arg(st, 0)) {
1720 symtable_exit_block(st, (void *)e);
1721 return 0;
1722 }
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001723 VISIT(st, expr, outermost->target);
1724 VISIT_SEQ(st, expr, outermost->ifs);
1725 VISIT_SEQ_TAIL(st, comprehension, generators, 1);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001726 if (value)
Benjamin Petersonc2575d52011-06-29 15:27:14 -05001727 VISIT(st, expr, value);
1728 VISIT(st, expr, elt);
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001729 if (st->st_cur->ste_generator) {
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001730 PyErr_SetString(PyExc_SyntaxError,
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001731 (e->kind == ListComp_kind) ? "'yield' inside list comprehension" :
1732 (e->kind == SetComp_kind) ? "'yield' inside set comprehension" :
1733 (e->kind == DictComp_kind) ? "'yield' inside dict comprehension" :
1734 "'yield' inside generator expression");
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001735 PyErr_SyntaxLocationObject(st->st_filename,
1736 st->st_cur->ste_lineno,
Ammar Askar025eb982018-09-24 17:12:49 -04001737 st->st_cur->ste_col_offset + 1);
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001738 symtable_exit_block(st, (void *)e);
1739 return 0;
Serhiy Storchaka73a7e9b2017-12-01 06:54:17 +02001740 }
Serhiy Storchaka07ca9af2018-02-04 10:53:48 +02001741 st->st_cur->ste_generator = is_generator;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001742 return symtable_exit_block(st, (void *)e);
Jeremy Hylton3e0055f2005-10-20 19:59:25 +00001743}
Nick Coghlan650f0d02007-04-15 12:05:43 +00001744
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001745static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001746symtable_visit_genexp(struct symtable *st, expr_ty e)
1747{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001748 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(genexpr),
1749 e->v.GeneratorExp.generators,
1750 e->v.GeneratorExp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001751}
1752
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001753static int
Nick Coghlan650f0d02007-04-15 12:05:43 +00001754symtable_visit_listcomp(struct symtable *st, expr_ty e)
1755{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001756 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(listcomp),
1757 e->v.ListComp.generators,
1758 e->v.ListComp.elt, NULL);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001759}
1760
1761static int
1762symtable_visit_setcomp(struct symtable *st, expr_ty e)
1763{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001764 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(setcomp),
1765 e->v.SetComp.generators,
1766 e->v.SetComp.elt, NULL);
Guido van Rossum992d4a32007-07-11 13:09:30 +00001767}
1768
1769static int
1770symtable_visit_dictcomp(struct symtable *st, expr_ty e)
1771{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001772 return symtable_handle_comprehension(st, e, GET_IDENTIFIER(dictcomp),
1773 e->v.DictComp.generators,
1774 e->v.DictComp.key,
1775 e->v.DictComp.value);
Nick Coghlan650f0d02007-04-15 12:05:43 +00001776}